From 4f7ffd857a1b3242c0dac85e0f3be4da01b77b2c Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 23 Jan 2025 16:40:46 +0800 Subject: [PATCH] refactor: adapter 1.4 digital human shader --- .../advanced-shader/eye/EyeForwardPass.glsl | 11 +- .../advanced-shader/hair/HairForwardPass.glsl | 11 +- .../advanced-shader/sss/SSSForwardPass.glsl | 11 +- .../advanced-shader/sss/SSSLightDirect.glsl | 6 +- .../thinfilm/IridescenceForwardPass.glsl | 110 ------------------ .../thinfilm/IridescenceFunction.glsl | 100 ---------------- .../thinfilm/IridescenceIndirectLight.glsl | 19 --- .../thinfilm/IridescencedirectLight.glsl | 15 --- .../advanced-shader/thinfilm/Iridescene.gs | 69 ----------- 9 files changed, 26 insertions(+), 326 deletions(-) delete mode 100644 packages/custom-material/src/advanced-shader/thinfilm/IridescenceForwardPass.glsl delete mode 100644 packages/custom-material/src/advanced-shader/thinfilm/IridescenceFunction.glsl delete mode 100644 packages/custom-material/src/advanced-shader/thinfilm/IridescenceIndirectLight.glsl delete mode 100644 packages/custom-material/src/advanced-shader/thinfilm/IridescencedirectLight.glsl delete mode 100644 packages/custom-material/src/advanced-shader/thinfilm/Iridescene.gs diff --git a/packages/custom-material/src/advanced-shader/eye/EyeForwardPass.glsl b/packages/custom-material/src/advanced-shader/eye/EyeForwardPass.glsl index b005cb07..2fa0f2d7 100644 --- a/packages/custom-material/src/advanced-shader/eye/EyeForwardPass.glsl +++ b/packages/custom-material/src/advanced-shader/eye/EyeForwardPass.glsl @@ -84,11 +84,11 @@ void PBRFragment(Varyings varyings) { surfaceData.normal =calculateEyeNormal(varyings.uv, tbn, gl_FrontFacing); surfaceData.f0 = 0.04; - // Can modify surfaceData here initBRDFData(surfaceData, brdfData); - vec4 color = vec4(0, 0, 0, surfaceData.opacity); + vec3 totalDiffuseColor = vec3(0, 0, 0); + vec3 totalSpecularColor = vec3(0, 0, 0); // Get shadow attenuation float shadowAttenuation = 1.0; @@ -102,10 +102,13 @@ void PBRFragment(Varyings varyings) { #endif // Evaluate direct lighting - evaluateDirectRadiance(varyings, surfaceData, brdfData, shadowAttenuation, color.rgb); + evaluateDirectRadiance(varyings, surfaceData, brdfData, shadowAttenuation, totalDiffuseColor, totalSpecularColor); // IBL - evaluateIBL(varyings, surfaceData, brdfData, color.rgb); + evaluateIBL(varyings, surfaceData, brdfData, totalDiffuseColor, totalSpecularColor); + + // Final color + vec4 color = vec4(totalDiffuseColor + totalSpecularColor, surfaceData.opacity); // Emissive color.rgb += surfaceData.emissiveColor; diff --git a/packages/custom-material/src/advanced-shader/hair/HairForwardPass.glsl b/packages/custom-material/src/advanced-shader/hair/HairForwardPass.glsl index 6d1a9dd3..1c4bd894 100644 --- a/packages/custom-material/src/advanced-shader/hair/HairForwardPass.glsl +++ b/packages/custom-material/src/advanced-shader/hair/HairForwardPass.glsl @@ -72,7 +72,9 @@ void PBRFragment(Varyings varyings) { // Can modify surfaceData here initBRDFData(surfaceData, brdfData); - vec4 color = vec4(0, 0, 0, surfaceData.opacity); + + vec3 totalDiffuseColor = vec3(0, 0, 0); + vec3 totalSpecularColor = vec3(0, 0, 0); // Get shadow attenuation float shadowAttenuation = 1.0; @@ -86,10 +88,13 @@ void PBRFragment(Varyings varyings) { #endif // Evaluate direct lighting - evaluateDirectRadiance(varyings, surfaceData, brdfData, shadowAttenuation, color.rgb); + evaluateDirectRadiance(varyings, surfaceData, brdfData, shadowAttenuation, totalDiffuseColor, totalSpecularColor); // IBL - evaluateIBL(varyings, surfaceData, brdfData, color.rgb); + evaluateIBL(varyings, surfaceData, brdfData, totalDiffuseColor, totalSpecularColor); + + // Final color + vec4 color = vec4(totalDiffuseColor + totalSpecularColor, surfaceData.opacity); // Emissive color.rgb += surfaceData.emissiveColor; diff --git a/packages/custom-material/src/advanced-shader/sss/SSSForwardPass.glsl b/packages/custom-material/src/advanced-shader/sss/SSSForwardPass.glsl index ada335f3..f3e86a42 100644 --- a/packages/custom-material/src/advanced-shader/sss/SSSForwardPass.glsl +++ b/packages/custom-material/src/advanced-shader/sss/SSSForwardPass.glsl @@ -72,8 +72,8 @@ void PBRFragment(Varyings varyings) { // Can modify surfaceData here initBRDFData(surfaceData, brdfData); - vec4 color = vec4(0, 0, 0, surfaceData.opacity); - + vec3 totalDiffuseColor = vec3(0, 0, 0); + vec3 totalSpecularColor = vec3(0, 0, 0); // Get shadow attenuation float shadowAttenuation = 1.0; #if defined(SCENE_DIRECT_LIGHT_COUNT) && defined(NEED_CALCULATE_SHADOWS) @@ -86,10 +86,13 @@ void PBRFragment(Varyings varyings) { #endif // Evaluate direct lighting - evaluateDirectRadiance(varyings, surfaceData, brdfData, shadowAttenuation, color.rgb); + evaluateDirectRadiance(varyings, surfaceData, brdfData, shadowAttenuation, totalDiffuseColor, totalSpecularColor); // IBL - evaluateIBL(varyings, surfaceData, brdfData, color.rgb); + evaluateIBL(varyings, surfaceData, brdfData, totalDiffuseColor, totalSpecularColor); + + // Final color + vec4 color = vec4(totalDiffuseColor + totalSpecularColor, surfaceData.opacity); // Emissive color.rgb += surfaceData.emissiveColor; diff --git a/packages/custom-material/src/advanced-shader/sss/SSSLightDirect.glsl b/packages/custom-material/src/advanced-shader/sss/SSSLightDirect.glsl index 7cf8a61d..df8c4f2c 100644 --- a/packages/custom-material/src/advanced-shader/sss/SSSLightDirect.glsl +++ b/packages/custom-material/src/advanced-shader/sss/SSSLightDirect.glsl @@ -4,7 +4,7 @@ #include "ReflectionLobe.glsl" #include "./SSSFunction.glsl" -void surfaceShadingSSS(Varyings varyings, SurfaceData surfaceData, BRDFData brdfData, vec3 incidentDirection, vec3 lightColor, inout vec3 color) { +void surfaceShadingSSS(Varyings varyings, SurfaceData surfaceData, BRDFData brdfData, vec3 incidentDirection, vec3 lightColor, inout vec3 totalDiffuseColor, inout vec3 totalSpecularColor) { vec3 diffuseColor = vec3(0); vec3 specularColor = vec3(0); @@ -31,7 +31,9 @@ void surfaceShadingSSS(Varyings varyings, SurfaceData surfaceData, BRDFData brdf if(surfaceData.dotNV > EPSILON){ specularLobe(varyings, surfaceData, brdfData, incidentDirection, attenuationIrradiance, specularColor); } - color += diffuseColor + specularColor; + + totalDiffuseColor += diffuseColor; + totalSpecularColor += specularColor; } #include "LightDirectPBR.glsl" \ No newline at end of file diff --git a/packages/custom-material/src/advanced-shader/thinfilm/IridescenceForwardPass.glsl b/packages/custom-material/src/advanced-shader/thinfilm/IridescenceForwardPass.glsl deleted file mode 100644 index c4ff18e6..00000000 --- a/packages/custom-material/src/advanced-shader/thinfilm/IridescenceForwardPass.glsl +++ /dev/null @@ -1,110 +0,0 @@ -#ifndef FORWARD_PASS_PBR_INCLUDED -#define FORWARD_PASS_PBR_INCLUDED - -#include "Common.glsl" -#include "Fog.glsl" - -#include "AttributesPBR.glsl" -#include "VaryingsPBR.glsl" -#include "./IridescencedirectLight.glsl" -#include "./IridescenceIndirectLight.glsl" - -#include "VertexPBR.glsl" -#include "FragmentPBR.glsl" - - -Varyings PBRVertex(Attributes attributes) { - Varyings varyings; - - varyings.uv = getUV0(attributes); - #ifdef RENDERER_HAS_UV1 - varyings.uv1 = attributes.TEXCOORD_1; - #endif - - #ifdef RENDERER_ENABLE_VERTEXCOLOR - varyings.vertexColor = attributes.COLOR_0; - #endif - - - VertexInputs vertexInputs = getVertexInputs(attributes); - - // positionWS - varyings.positionWS = vertexInputs.positionWS; - - // positionVS - #if SCENE_FOG_MODE != 0 - varyings.positionVS = vertexInputs.positionVS; - #endif - - // normalWS、tangentWS、bitangentWS - #ifdef RENDERER_HAS_NORMAL - varyings.normalWS = vertexInputs.normalWS; - #ifdef RENDERER_HAS_TANGENT - varyings.tangentWS = vertexInputs.tangentWS; - varyings.bitangentWS = vertexInputs.bitangentWS; - #endif - #endif - - // ShadowCoord - #if defined(NEED_CALCULATE_SHADOWS) && (SCENE_SHADOW_CASCADED_COUNT == 1) - varyings.shadowCoord = getShadowCoord(vertexInputs.positionWS); - #endif - - gl_Position = renderer_MVPMat * vertexInputs.positionOS; - - return varyings; -} - - -void PBRFragment(Varyings varyings) { - BRDFData brdfData; - - // Get aoUV - vec2 aoUV = varyings.uv; - #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(RENDERER_HAS_UV1) - if(material_OcclusionTextureCoord == 1.0){ - aoUV = varyings.uv1; - } - #endif - - SurfaceData surfaceData = getSurfaceData(varyings, aoUV, gl_FrontFacing); - surfaceData.f0 = 0.04; - - // Can modify surfaceData here - initBRDFData(surfaceData, brdfData); - - vec4 color = vec4(0, 0, 0, surfaceData.opacity); - - // Get shadow attenuation - float shadowAttenuation = 1.0; - #if defined(SCENE_DIRECT_LIGHT_COUNT) && defined(NEED_CALCULATE_SHADOWS) - #if SCENE_SHADOW_CASCADED_COUNT == 1 - vec3 shadowCoord = varyings.shadowCoord; - #else - vec3 shadowCoord = getShadowCoord(varyings.positionWS); - #endif - shadowAttenuation *= sampleShadowMap(varyings.positionWS, shadowCoord); - #endif - - // Evaluate direct lighting - evaluateDirectRadiance(varyings, surfaceData, brdfData, shadowAttenuation, color.rgb); - - // IBL - evaluateIBL(varyings, surfaceData, brdfData, color.rgb); - - // Emissive - color.rgb += surfaceData.emissiveColor; - - - #if SCENE_FOG_MODE != 0 - color = fog(color, varyings.positionVS); - #endif - - #ifndef ENGINE_IS_COLORSPACE_GAMMA - color = linearToGamma(color); - #endif - - gl_FragColor = color; -} - -#endif \ No newline at end of file diff --git a/packages/custom-material/src/advanced-shader/thinfilm/IridescenceFunction.glsl b/packages/custom-material/src/advanced-shader/thinfilm/IridescenceFunction.glsl deleted file mode 100644 index 231a0ea2..00000000 --- a/packages/custom-material/src/advanced-shader/thinfilm/IridescenceFunction.glsl +++ /dev/null @@ -1,100 +0,0 @@ -#ifndef IRIDESCENCE_FUNCTION -#define IRIDESCENCE_FUNCTION - -float material_Eta2; -float material_IridescenceThickness; -float material_Iridescence; - -float sqr(float x) { - return x * x; -} - -vec3 iorToFresnel(vec3 transmittedIor, float incidentIor){ - return pow((transmittedIor - incidentIor) / (transmittedIor + incidentIor),vec3(2.0,2.0,2.0)); -} - -vec3 fresnelToIor(vec3 F0){ - vec3 sqrtF0 = sqrt(F0); - return (vec3(1.0) + sqrtF0) / (vec3(1.0) - sqrtF0); -} - - vec3 evalSensitivity(float opd, vec3 shift){ - // Use Gaussian fits, given by 3 parameters: val, pos and var - float phase = 2.0 * PI * opd * 1.0e-6; - const vec3 val = vec3(5.4856e-13, 4.4201e-13, 5.2481e-13); - const vec3 pos = vec3(1.6810e+6, 1.7953e+6, 2.2084e+6); - const vec3 var = vec3(4.3278e+9, 9.3046e+9, 6.6121e+9); - vec3 xyz = val * sqrt(2.0*PI * var) * cos(pos * phase + shift) * exp(-var * sqr(phase)); - xyz.x += 9.7470e-14 * sqrt(2.0*PI * 4.5282e+9) * cos(2.2399e+6 * phase + shift[0]) * exp(-4.5282e+9 * sqr(phase)); - xyz /= 1.0685e-7; - - const mat3 XYZ_TO_RGB = mat3( 3.2404542, -0.9692660, 0.0556434, - -1.5371385, 1.8760108, -0.2040259, - -0.4985314, 0.0415560, 1.0572252); - vec3 rgb = XYZ_TO_RGB * xyz; - return rgb; -} - -vec3 thinFilmIridescence(float cosTheta1, float eta2, vec3 baseF0,float iridescenceThickness){ - - const float eta1 = 1.0; - float dinc = 2.0 * iridescenceThickness; - float sinTheta2 = pow(eta1 / eta2, 2.0) * (1.0 - pow(cosTheta1 , 2.0)); - float cosTheta2Sq = (1.0 - sinTheta2); - float cosTheta2 = sqrt(cosTheta2Sq); - - // First interface - float R0 = iorToFresnel(vec3(eta2), eta1).x; - float R12 = F_Schlick(R0, cosTheta1); - float R21 = R12; - float T121 =1.0 - R12; - - float phi12 = 0.0; - float phi21 = PI - phi12; - - vec3 baseIor = fresnelToIor(baseF0 + 0.0001); - vec3 R1 =iorToFresnel(baseIor, eta2); - vec3 R23 = F_Schlick(R1, cosTheta2); - - vec3 phi23 =vec3(0.0); - if (baseIor[0] < eta2) {phi23[0] = PI;} - if (baseIor[1] < eta2) {phi23[1] = PI;} - if (baseIor[2] < eta2) {phi23[2] = PI;} - - // Phase shift - float OPD = dinc * cosTheta1 * eta2; - vec3 phi = vec3(phi21,phi21,phi21) + phi23; - - vec3 R123 = clamp(R12 * R23, 1e-5, 0.9999); - vec3 r123 = sqrt(R123); - vec3 Rs = sqr(T121) * R23 / (vec3(1.0)-R123); - - vec3 C0 = R12 + Rs; - vec3 iridescence = vec3(0); - iridescence = C0; - - vec3 Cm = Rs - T121; - for (int m = 1; m <= 2; ++m) - { - Cm *= r123; - vec3 Sm = 2.0 * evalSensitivity(float(m) * OPD, float(m) * phi); - iridescence += Cm * Sm; - } - iridescence = max(iridescence, vec3(0)); - - return iridescence; -} - -vec3 directBDRFIridescence(SurfaceData surfaceData, vec3 light, BRDFData brdfData ){ -// Compute dot products - float NdotL = saturate(dot(surfaceData.normal, light ) ); - float NdotV = saturate(dot(surfaceData.normal, surfaceData.viewDir ) ); - vec3 halfDir = normalize(light + surfaceData.viewDir ); - float NdotH = saturate(dot(surfaceData.normal, halfDir ) ); - float cosTheta1 = dot(halfDir, light); - - vec3 iridescence = thinFilmIridescence(cosTheta1,material_Eta2,brdfData.specularColor,material_IridescenceThickness); - return iridescence; -} - -#endif \ No newline at end of file diff --git a/packages/custom-material/src/advanced-shader/thinfilm/IridescenceIndirectLight.glsl b/packages/custom-material/src/advanced-shader/thinfilm/IridescenceIndirectLight.glsl deleted file mode 100644 index 919da1b4..00000000 --- a/packages/custom-material/src/advanced-shader/thinfilm/IridescenceIndirectLight.glsl +++ /dev/null @@ -1,19 +0,0 @@ -#define FUNCTION_SPECULAR_IBL evaluateSpecularIBLIridescence -#include "BRDF.glsl" -#include "./IridescenceFunction.glsl" -#include "LightIndirectFunctions.glsl" - -void evaluateSpecularIBLIridescence(Varyings varyings, SurfaceData surfaceData, BRDFData brdfData, float radianceAttenuation, inout vec3 specularColor){ - - vec3 reflectdir = reflect(surfaceData.normal,-surfaceData.viewDir); - vec3 halfdir = reflectdir + surfaceData.viewDir; - float cosTheta1= dot(halfdir,reflectdir); - vec3 fresnelIridescent = thinFilmIridescence(cosTheta1, material_Eta2 , brdfData.specularColor ,material_IridescenceThickness); - vec3 envBRDF =envBRDFApprox(brdfData.specularColor, brdfData.roughness, surfaceData.dotNV ); - vec3 fator = mix(envBRDF,fresnelIridescent,material_Iridescence); - - vec3 radiance = getLightProbeRadiance(surfaceData, surfaceData.normal, brdfData.roughness); - specularColor += surfaceData.specularAO * radianceAttenuation * radiance * fator ; -} - -#include "LightIndirectPBR.glsl" \ No newline at end of file diff --git a/packages/custom-material/src/advanced-shader/thinfilm/IridescencedirectLight.glsl b/packages/custom-material/src/advanced-shader/thinfilm/IridescencedirectLight.glsl deleted file mode 100644 index cc635e9c..00000000 --- a/packages/custom-material/src/advanced-shader/thinfilm/IridescencedirectLight.glsl +++ /dev/null @@ -1,15 +0,0 @@ -#define FUNCTION_SPECULAR_LOBE specularLobeIridescence - -#include "BRDF.glsl" -#include "./IridescenceFunction.glsl" - -void specularLobeIridescence(Varyings varyings, SurfaceData surfaceData, BRDFData brdfData, vec3 incidentDirection, vec3 attenuationIrradiance, inout vec3 specularColor){ - - vec3 thin = directBDRFIridescence(surfaceData, incidentDirection, brdfData); - vec3 BRDF_Specular = BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.normal, brdfData.specularColor, brdfData.roughness); - vec3 factor =mix(BRDF_Specular,thin,material_Iridescence); - - specularColor += attenuationIrradiance * factor; -} - -#include "LightDirectPBR.glsl" \ No newline at end of file diff --git a/packages/custom-material/src/advanced-shader/thinfilm/Iridescene.gs b/packages/custom-material/src/advanced-shader/thinfilm/Iridescene.gs deleted file mode 100644 index 9c2a10f7..00000000 --- a/packages/custom-material/src/advanced-shader/thinfilm/Iridescene.gs +++ /dev/null @@ -1,69 +0,0 @@ -Shader "Advanced/Thin.gs" { - EditorProperties { - Header("Base"){ - material_BaseColor("BaseColor", Color) = (1, 1, 1, 1); - material_BaseTexture("BaseTexture", Texture2D); - } - - Header("Thin Film"){ - material_Eta2( "IridescentIOR", Range( 1, 10, 0.1 ) ) =2; - material_Iridescence("Iridescence", Range(0.0 , 1 , 0.01)) = 0.7; - material_IridescenceThickness("IridescentThickness", Range(0.01 , 5 , 0.01)) = 0.1; - } - - Header("Metal Roughness") { - material_Metal( "Metal", Range(0,1,0.01) ) = 1; - material_Roughness( "Roughness", Range( 0, 1, 0.01 ) ) = 1; - material_RoughnessMetallicTexture("RoughnessMetallicTexture", Texture2D); - } - - Header("Normal") { - material_NormalTexture("NormalTexture", Texture2D); - material_NormalIntensity("NormalIntensity", Range(0, 5, 0.01)) = 1; - } - - Header("Emissive") { - material_EmissiveColor("EmissiveColor", Color ) = (0, 0, 0, 1); - material_EmissiveTexture("EmissiveTexture", Texture2D); - } - - Header("Occlusion") { - material_OcclusionTexture("OcclusionTexture", Texture2D); - material_OcclusionIntensity("OcclusionIntensity", Range(0, 5, 0.01)) = 1; - material_OcclusionTextureCoord("OcclusionTextureCoord", Float) = 0; - } - - Header("Common") { - material_AlphaCutoff( "AlphaCutoff", Range(0, 1, 0.01) ) = 0; - material_TilingOffset("TilingOffset", Vector4) = (1, 1, 0, 0); - } - } - - EditorMacros { - Header("Conditional Macors") { - MATERIAL_HAS_BASETEXTURE("HAS_BASETEXTURE"); - MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE("HAS_ROUGHNESS_METALLIC_TEXTURE"); - MATERIAL_HAS_NORMALTEXTURE("HAS_NORMALTEXTURE"); - MATERIAL_HAS_EMISSIVETEXTURE("HAS_EMISSIVETEXTURE"); - MATERIAL_HAS_OCCLUSION_TEXTURE("HAS_OCCLUSION_TEXTURE"); - MATERIAL_IS_TRANSPARENT("IS_TRANSPARENT"); - MATERIAL_IS_ALPHA_CUTOFF("IS_ALPHA_CUTOFF"); - } - } - - SubShader "Default" { - UsePass "pbr/Default/ShadowCaster" - - Pass "Forward Pass" { - Tags { pipelineStage = "Forward"} - - #define IS_METALLIC_WORKFLOW - - VertexShader = PBRVertex; - FragmentShader = PBRFragment; - - #include "./IridescenceForwardPass.glsl" - } - } - } - \ No newline at end of file