From 15eec3bc265a5ccab84969d9e84c78226956e7cb Mon Sep 17 00:00:00 2001 From: jk20012001 Date: Fri, 25 Aug 2023 18:31:28 +0800 Subject: [PATCH] sheen lighting model (#16058) (#16102) --- .../chunks/common/graph-expression/base.chunk | 42 ++++++++++++----- .../assets/chunks/common/lighting/brdf.chunk | 47 ++++++++++++++++++- .../lighting-flow/common-flow.chunk | 26 +++++----- .../model-functions/standard.chunk | 31 ++++++++++++ .../default-functions/standard-fs.chunk | 4 +- .../module-functions/standard-fs.chunk | 4 +- editor/assets/effects/advanced/fabric.effect | 28 ++++++----- 7 files changed, 140 insertions(+), 42 deletions(-) diff --git a/editor/assets/chunks/common/graph-expression/base.chunk b/editor/assets/chunks/common/graph-expression/base.chunk index c3ef49a59d..6b565e91c6 100644 --- a/editor/assets/chunks/common/graph-expression/base.chunk +++ b/editor/assets/chunks/common/graph-expression/base.chunk @@ -18,19 +18,6 @@ vec2 Rotator(vec2 uv, vec2 centerUV, float time, float speed) return vec2(dir.x, dir.z) + centerUV; } -// tangent space -> world space -// same as TransformVector -vec3 TransformNormalMap(vec3 vectorFromNormalMap) -{ - vec3 vectorTS = normalize(vectorFromNormalMap - vec3(0.5)); - vec3 normal = normalize(FSInput_worldNormal); - vec3 tangent = normalize(FSInput_worldTangent); - vec3 binormal = CalculateBinormal(normal, tangent, FSInput_mirrorNormal); - vec3 vecWS = vectorTS.x * tangent + vectorTS.y * binormal + vectorTS.z * normal; - return vecWS * vec3(1.0, 1.0, -1.0); // inverse Z for RH -} - - // return 0.0 when out of range // hardness: 0 - 1 float SphereMask(vec2 center, vec2 point, float radius, float hardness) @@ -45,3 +32,32 @@ float SphereMask(vec3 center, vec3 point, float radius, float hardness) float coef = pow(saturate(length / radius), 1.0 / (1.0 - saturate(sqrt(hardness)) + EPSILON)); return length > radius ? 0.0 : 1.0 - coef; } + + +// For Fragment Shader + +// tangent space -> world space +// same as TransformVector +vec3 TransformNormalMap(vec3 vectorFromNormalMap) +{ + vec3 vectorTS = normalize(vectorFromNormalMap - vec3(0.5)); + vec3 normal = normalize(FSInput_worldNormal); + vec3 tangent = normalize(FSInput_worldTangent); + vec3 binormal = CalculateBinormal(normal, tangent, FSInput_mirrorNormal); + vec3 vecWS = vectorTS.x * tangent + vectorTS.y * binormal + vectorTS.z * normal; + return vecWS * vec3(1.0, 1.0, -1.0); // inverse Z for RH +} + +// WorldPos -> UV +vec2 GetTriplanarMappingUV() +{ + vec3 Up = vec3(0.0, 1.0, 0.0); + vec3 U = cross(Up, FSInput_worldNormal.xyz); + if (length(U) < 0.01) + U = vec3(1.0, 0.0, 0.0); + else + U = normalize(U); + vec3 V = cross(U, FSInput_worldNormal.xyz); + V = normalize(V); + return fract(vec2(dot(FSInput_worldPos.xyz, U), dot(FSInput_worldPos.xyz, V))); +} diff --git a/editor/assets/chunks/common/lighting/brdf.chunk b/editor/assets/chunks/common/lighting/brdf.chunk index ef4fea792a..79405c200a 100644 --- a/editor/assets/chunks/common/lighting/brdf.chunk +++ b/editor/assets/chunks/common/lighting/brdf.chunk @@ -116,5 +116,50 @@ void IntegratedGFMultiplier (out vec3 integratedGF, out vec3 integratedF, vec3 s integratedGF = max(vec3(0.0), vec3(AB.x) + vec3(AB.y) / (specular + EPSILON_LOWP)); } -//Diffuse_Lambert +// isotropic sheen +float Sheen_HorizonFading(float NoL) +{ + const float horizonFade = 1.3; + float horiz = saturate( 1.0 + horizonFade * NoL); + return horiz * horiz; +} + +float Sheen(float NoHSat, float NoL, float NoV, float roughness) +{ + if (NoL <= 0.0 || NoV <= 0.0) + return 0.0; + float NoH2 = NoHSat*NoHSat; + float NoL2 = NoL*NoL; + float NoV2 = NoV*NoV; + roughness += EPSILON_LOWP; + float r2 = roughness*roughness; + + float t = 1.0 - NoH2 + NoH2/r2; + float Pi_D = 1.0 / (roughness * t * t); + + float Li = sqrt(1.0 - NoL2 + r2*NoL2) / NoL; + float Lo = sqrt(1.0 - NoV2 + r2*NoV2) / NoV; + float G = (1.0 - exp(-(Li + Lo))) / (Li + Lo); + + return Sheen_HorizonFading(NoL) * Pi_D * G / NoV; +} +// simplify estimation for env lighting convolution +float Sheen(float NoV, float roughness) +{ + NoV *= NoV; + float NoV2 = NoV*NoV; + roughness += EPSILON_LOWP; + float r2 = roughness*roughness; + + float t = 1.0 - NoV2 + NoV2/r2; + float Pi_D = 1.0 / (roughness * t * t); + + float Lo = sqrt(1.0 - NoV2 + r2*NoV2) / NoV; + float G = (1.0 - exp(-Lo)) / Lo; + + float sheen = Pi_D * G / NoV; + return pow(max(0.0, sheen), 0.5); +} + +// Diffuse_Lambert #define DiffuseCoefficient_EnergyConservation INV_PI diff --git a/editor/assets/chunks/lighting-models/lighting-flow/common-flow.chunk b/editor/assets/chunks/lighting-models/lighting-flow/common-flow.chunk index 0324db26f3..14e26fb2c2 100644 --- a/editor/assets/chunks/lighting-models/lighting-flow/common-flow.chunk +++ b/editor/assets/chunks/lighting-models/lighting-flow/common-flow.chunk @@ -109,11 +109,11 @@ LightingIntermediateData lightingData2ndSpecular; CCSurfacesInitializeLightingIntermediateData(lightingData2ndSpecular, surfaceData2ndSpecular); CCSurfacesLightingCalculateIntermediateData_PerLight(lightingData2ndSpecular, surfaceData, lightDirWithLength); - CCSurfacesLightingCalculateDirect2ndSpecular(lightingResult.direct2ndSpecular, lightingData2ndSpecular, attenuatedLightColorAndIntensity, surfaceData2ndSpecular.intensity2ndSpecular, lightingResult.directSpecular); - CCSurfaceLightingCalculateDirectFresnel(lightingResult.directGF2ndSpecular, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, vec3(1.0)); - #if CC_SURFACES_LIGHTING_SHEEN - vec3 extraF = CCSurfaceLightingCalculateExtraFresnel(lightingData2ndSpecular); - lightingResult.directGF2ndSpecular *= extraF; + #if !CC_SURFACES_LIGHTING_SHEEN + CCSurfacesLightingCalculateDirect2ndSpecular(lightingResult.direct2ndSpecular, lightingData2ndSpecular, attenuatedLightColorAndIntensity, surfaceData2ndSpecular.intensity2ndSpecular, lightingResult.directSpecular); + CCSurfaceLightingCalculateDirectFresnel(lightingResult.directGF2ndSpecular, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, vec3(1.0)); + #else + CCSurfacesLightingCalculateDirectSheen(lightingResult.direct2ndSpecular, lightingResult.directGF2ndSpecular, lightingData2ndSpecular, attenuatedLightColorAndIntensity, surfaceData2ndSpecular.intensity2ndSpecular); #endif lightingResult.direct2ndSpecular *= multiplier; @@ -258,15 +258,15 @@ vec3 diff; CCSurfacesLightingInitializeColorWithLighting(diff, lightingResult.specularColorWithLighting2ndSpecular, surfaceData2ndSpecular, lightingData2ndSpecular); - CCSurfacesLightingCalculateEnvironment2ndSpecular(lightingResult.environment2ndSpecular, lightingData2ndSpecular, cc_ambientSky.w, surfaceData2ndSpecular.intensity2ndSpecular, lightingResult.environmentSpecular); - CCSurfaceLightingCalculateEnvironmentFresnel(lightingResult.environmentGF2ndSpecular, lightingResult.environmentSubLayerF, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular); + #if !CC_SURFACES_LIGHTING_SHEEN + CCSurfacesLightingCalculateEnvironment2ndSpecular(lightingResult.environment2ndSpecular, lightingData2ndSpecular, cc_ambientSky.w, surfaceData2ndSpecular.intensity2ndSpecular, lightingResult.environmentSpecular); + CCSurfaceLightingCalculateEnvironmentFresnel(lightingResult.environmentGF2ndSpecular, lightingResult.environmentSubLayerF, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular); - CCSurfacesLightingCalculateDirect2ndSpecular(lightingResult.direct2ndSpecular, lightingData2ndSpecular, cc_mainLitColor, surfaceData2ndSpecular.intensity2ndSpecular, lightingResult.directSpecular); - CCSurfaceLightingCalculateDirectFresnel(lightingResult.directGF2ndSpecular, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, lightingResult.environmentGF2ndSpecular); - #if CC_SURFACES_LIGHTING_SHEEN - vec3 extraF = CCSurfaceLightingCalculateExtraFresnel(lightingData2ndSpecular); - lightingResult.environmentGF2ndSpecular *= extraF; - lightingResult.directGF2ndSpecular *= extraF; + CCSurfacesLightingCalculateDirect2ndSpecular(lightingResult.direct2ndSpecular, lightingData2ndSpecular, cc_mainLitColor, surfaceData2ndSpecular.intensity2ndSpecular, lightingResult.directSpecular); + CCSurfaceLightingCalculateDirectFresnel(lightingResult.directGF2ndSpecular, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, lightingResult.environmentGF2ndSpecular); + #else + CCSurfacesLightingCalculateDirectSheen(lightingResult.direct2ndSpecular, lightingResult.directGF2ndSpecular, lightingData2ndSpecular, cc_mainLitColor, surfaceData2ndSpecular.intensity2ndSpecular); + CCSurfacesLightingCalculateEnvironmentSheen(lightingResult.environment2ndSpecular, lightingResult.environmentGF2ndSpecular, lightingData2ndSpecular, cc_ambientSky.w, surfaceData2ndSpecular.intensity2ndSpecular); #endif #endif diff --git a/editor/assets/chunks/lighting-models/model-functions/standard.chunk b/editor/assets/chunks/lighting-models/model-functions/standard.chunk index a0a5054f12..3898c96d44 100644 --- a/editor/assets/chunks/lighting-models/model-functions/standard.chunk +++ b/editor/assets/chunks/lighting-models/model-functions/standard.chunk @@ -234,6 +234,37 @@ void CCSurfaceLightingCalculateEnvironmentFresnel(out vec3 integratedGF, vec3 in specularLighting = vec3(0.0); #endif } + + void CCSurfacesLightingCalculateDirectSheen(out vec3 specularLighting, out vec3 directGF, in LightingIntermediateData lightingData, in vec4 lightSourceColorAndIntensity, float intensitySpecular) + { + #if !CC_SURFACES_LIGHTING_DISABLE_SPECULAR + float sheen = Sheen(lightingData.NoHSat, lightingData.NoL, lightingData.NoV, lightingData.specularParam); + specularLighting = vec3(sheen) * intensitySpecular * lightSourceColorAndIntensity.xyz * lightSourceColorAndIntensity.w; + directGF = vec3(1.0); + #else + specularLighting = vec3(0.0); + #endif + } + + void CCSurfacesLightingCalculateEnvironmentSheen(out vec3 specularLighting, out vec3 environmentGF, in LightingIntermediateData lightingData, float lightIntensity, float intensitySpecular) + { + #if !CC_SURFACES_LIGHTING_DISABLE_SPECULAR + LightingIntermediateData lightingDataSheen = lightingData; + float roughness = lightingData.specularParam; + + // sample max distribute normal-vertical-direction reflection with fixed roughness + vec3 L = normalize(mix(lightingDataSheen.B, lightingDataSheen.N, 0.3)); + lightingDataSheen.specularParam = mix(0.5, 0.9, roughness); + lightingDataSheen.V = lightingDataSheen.N = L; + specularLighting = CalculateEnvironmentSpecular(lightingDataSheen, lightIntensity); + specularLighting *= intensitySpecular; + + // range limitation + environmentGF = vec3(Sheen(lightingData.NoV, roughness)); + #else + specularLighting = vec3(0.0); + #endif + } #endif #if CC_SURFACES_LIGHTING_TT diff --git a/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk b/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk index 038a9e7599..8229737b28 100644 --- a/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk +++ b/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk @@ -220,8 +220,8 @@ vec3 SurfacesFragmentModifyClearCoatWorldNormal() vec4 SurfacesFragmentModifySheenParams() { //x: sheen roughness - //y: sheen ior - //z: sheen opacity + //y: sheen opacity + //z: unused //w: sheen intensity return vec4(0.7, 1.0, 1.0, 1.0); } diff --git a/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk b/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk index 86db9d4f50..d3deafc53c 100644 --- a/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk +++ b/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk @@ -91,14 +91,14 @@ void CCSurfacesFragmentGetMaterialData(inout SurfacesMaterialData surfaceData) // skip in shader graph, need extra code vec4 sheenParams = SurfacesFragmentModifySheenParams(); surfaceData.roughness2ndSpecular = saturate(sheenParams.x); - surfaceData.ior2ndSpecular = sheenParams.y; - surfaceData.intensity2ndSpecular = sheenParams.z * sheenParams.w; + surfaceData.intensity2ndSpecular = sheenParams.y * sheenParams.w; surfaceData.metallic2ndSpecular = 1.0; surfaceData.baseColor2ndSpecular = SurfacesFragmentModifySheenColor(); surfaceData.color2ndSpecular = vec3(1.0); // no extra coloration surfaceData.worldNormal2ndSpecular = surfaceData.worldNormal; surfaceData.worldTangent2ndSpecular = surfaceData.worldTangent; surfaceData.worldBinormal2ndSpecular = surfaceData.worldBinormal; + surfaceData.ior2ndSpecular = surfaceData.ior; //unused surfaceData.opacity2ndSpecular = 1.0; // unused #if CC_SURFACES_LIGHTING_ANISOTROPIC surfaceData.anisotropyShape2ndSpecular = surfaceData.anisotropyShape; diff --git a/editor/assets/effects/advanced/fabric.effect b/editor/assets/effects/advanced/fabric.effect index 4803793548..c690c8ec88 100644 --- a/editor/assets/effects/advanced/fabric.effect +++ b/editor/assets/effects/advanced/fabric.effect @@ -27,9 +27,10 @@ CCEffect %{ anisotropyMap: { value: black, editor : { parent: IS_ANISOTROPY } } sheenColor: { value: [1.0, 1.0, 1.0, 1.0], linear: true, editor: { type: color } } sheenRoughness: { value: 1.0, target: emissiveScaleParam.x, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } } - sheenIOR: { value: 1.0, target: emissiveScaleParam.y, editor: { slide: true, range: [1.0, 2.0], step: 0.001 } } - sheenOpacity: { value: 1.0, target: emissiveScaleParam.z, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } } - sheenMap: { value: white, editor: { parent: USE_SHEEN_MAP, tooltips: 'r: sheen roughness g: sheen opacity' } } + sheenOpacity: { value: 1.0, target: emissiveScaleParam.y, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } } + sheenIntensity: { value: 1.0, target: emissiveScaleParam.w, editor: { slide: true, range: [1.0, 3.0], step: 0.001 } } + sheenColorMap: { value: white, editor: { parent: USE_SHEEN_COLOR_MAP } } + sheenDataMap: { value: white, editor: { parent: USE_SHEEN_DATA_MAP, tooltips: 'r: sheen roughness g: sheen opacity' } } - &forward-add vert: standard-vs frag: standard-fs @@ -145,10 +146,8 @@ CCProgram macro-remapping %{ #define CC_SURFACES_USE_VERTEX_COLOR USE_VERTEX_COLOR #define CC_SURFACES_LIGHTING_SHEEN 1 - // depend on UI macros -#if IS_ANISOTROPY || USE_NORMAL_MAP + // sheen need tangent #define CC_SURFACES_USE_TANGENT_SPACE 1 -#endif // functionality for each effect #define CC_SURFACES_LIGHTING_ANISOTROPIC_ENVCONVOLUTION_COUNT 31 @@ -190,8 +189,11 @@ CCProgram surface-fragment %{ #if IS_ANISOTROPY && USE_ANISOTROPY_MAP uniform sampler2D anisotropyMap; #endif - #if USE_SHEEN_MAP - uniform sampler2D sheenMap; + #if USE_SHEEN_COLOR_MAP + uniform sampler2D sheenColorMap; + #endif + #if USE_SHEEN_DATA_MAP + uniform sampler2D sheenDataMap; #endif #pragma define OCCLUSION_CHANNEL r @@ -223,9 +225,9 @@ CCProgram surface-fragment %{ #define CC_SURFACES_FRAGMENT_MODIFY_SHEEN_PARAMS vec4 SurfacesFragmentModifySheenParams() { - vec4 param = vec4(emissiveScaleParam.x, emissiveScaleParam.y - 0.9, emissiveScaleParam.z, 1.0); - #if USE_SHEEN_MAP - param.xz *= texture(sheenMap, FSInput_texcoord).xy; + vec4 param = emissiveScaleParam; + #if USE_SHEEN_DATA_MAP + param.xy *= texture(sheenDataMap, FSInput_texcoord).xy; #endif return param; } @@ -233,7 +235,11 @@ CCProgram surface-fragment %{ #define CC_SURFACES_FRAGMENT_MODIFY_SHEEN_COLOR vec3 SurfacesFragmentModifySheenColor() { + #if USE_SHEEN_COLOR_MAP + return sheenColor.rgb * texture(sheenColorMap, FSInput_texcoord).rgb; + #else return sheenColor.rgb; + #endif } #include