From 96be28967f76ee625de5bd21ce37dd2edd86bf12 Mon Sep 17 00:00:00 2001 From: jk20012001 Date: Tue, 18 Jul 2023 18:13:51 +0800 Subject: [PATCH 1/9] coat lighting model --- .../assets/chunks/common/lighting/brdf.chunk | 35 +- .../assets/chunks/common/lighting/bxdf.chunk | 1 + .../lighting-intermediate-data.chunk | 10 +- .../data-structures/lighting-result.chunk | 29 +- .../lighting-flow/common-flow.chunk | 87 +++- .../model-functions/standard.chunk | 82 +++- .../model-functions/toon.chunk | 18 + .../surfaces/data-structures/standard.chunk | 7 +- .../default-functions/standard-fs.chunk | 45 +- .../effect-macros/common-macros.chunk | 11 +- .../module-functions/debug-view.chunk | 31 +- .../module-functions/standard-fs.chunk | 199 +++++++-- .../surfaces/module-functions/toon-fs.chunk | 11 +- .../surfaces/module-functions/unlit-fs.chunk | 6 +- .../assets/effects/advanced/car-paint.effect | 422 ++++++++++++++++++ .../effects/advanced/car-paint.effect.meta | 11 + .../dcc/vat/houdini-fluid-v3-liquid.effect | 2 +- .../util/dcc/vat/zeno-fluid-liquid.effect | 2 +- 18 files changed, 913 insertions(+), 96 deletions(-) create mode 100644 editor/assets/effects/advanced/car-paint.effect create mode 100644 editor/assets/effects/advanced/car-paint.effect.meta diff --git a/editor/assets/chunks/common/lighting/brdf.chunk b/editor/assets/chunks/common/lighting/brdf.chunk index 976a93914bc..faca74ce2e4 100644 --- a/editor/assets/chunks/common/lighting/brdf.chunk +++ b/editor/assets/chunks/common/lighting/brdf.chunk @@ -15,13 +15,36 @@ float V_Kelemen(float LoH) { return 0.25 / (LoH * LoH); } - +float square(float a) { return a * a;} +vec2 square(vec2 a) { return a * a;} +vec3 square(vec3 a) { return a * a;} +float G_Schlick( float roughness, float NoV, float NoL ) +{ + float k = square( 0.5 + 0.5*roughness ); + float G_SchlickV = NoV * (1.0 - k) + k; + float G_SchlickL = NoL * (1.0 - k) + k; + return 0.25 / ( G_SchlickV * G_SchlickL ); +} vec3 F_Schlick( vec3 specularColor, float VoH ) { float Fc = exp2( (-5.55473 * VoH - 6.98316) * VoH ); float selfShadowTerm = saturate(50.0 * specularColor.g); return specularColor * (1.0 - Fc) + vec3(selfShadowTerm * Fc); } +vec3 F_SchlickMultiplier( vec3 specularColor, float VoH ) +{ + float Fc = exp2( (-5.55473 * VoH - 6.98316) * VoH ); + float selfShadowTerm = saturate(50.0 * specularColor.g); + return vec3(1.0 - Fc) + vec3(selfShadowTerm * Fc) / (specularColor + vec3(EPSILON)); +} + +vec3 F_Fresnel( vec3 SpecularColor, float VoH ) +{ + vec3 specularColorSqrt = sqrt( clamp( vec3(0.0), vec3(0.99), SpecularColor ) ); + vec3 n = ( 1.0 + specularColorSqrt ) / ( 1.0 - specularColorSqrt ); + vec3 g = sqrt( n*n + VoH*VoH - 1.0 ); + return 0.5 * square( (g - VoH) / (g + VoH) ) * ( 1.0 + square( ((g+VoH)*VoH - 1.0) / ((g-VoH)*VoH + 1.0) ) ); +} float D_GGX(float roughness, float NoH) { @@ -92,6 +115,16 @@ vec3 IntegratedGFApprox (vec3 specular, float roughness, float NoV) { AB.y *= clamp(50.0 * specular.g, 0.0, 1.0); return max(vec3(0.0), specular * AB.x + AB.y); } +void IntegratedGFMultiplier (out vec3 integratedGF, out vec3 integratedF, vec3 specular, float roughness, float NoV) { + const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022); + const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04); + vec4 r = roughness * c0 + c1; + float a004 = min(r.x * r.x, exp2(-9.28 * NoV)) * r.x + r.y; + vec2 AB = vec2(-1.04, 1.04) * a004 + r.zw; + AB.y *= clamp(50.0 * specular.g, 0.0, 1.0); + integratedF = vec3(max(0.0, AB.x)); + integratedGF = max(vec3(0.0), vec3(AB.x) + vec3(AB.y) / (specular + EPSILON_LOWP)); +} //Diffuse_Lambert #define DiffuseCoefficient_EnergyConservation INV_PI diff --git a/editor/assets/chunks/common/lighting/bxdf.chunk b/editor/assets/chunks/common/lighting/bxdf.chunk index b3fd91dfbc7..e08f373a64a 100644 --- a/editor/assets/chunks/common/lighting/bxdf.chunk +++ b/editor/assets/chunks/common/lighting/bxdf.chunk @@ -1,5 +1,6 @@ // saturated N dot V +// for env reflection with non-metal and smooth surface float CalculateFresnelCoefficient(float ior, float NoVSat) { float g, c, n, prev, next; diff --git a/editor/assets/chunks/lighting-models/data-structures/lighting-intermediate-data.chunk b/editor/assets/chunks/lighting-models/data-structures/lighting-intermediate-data.chunk index 7a4b39bd1a9..9a1a190c4de 100644 --- a/editor/assets/chunks/lighting-models/data-structures/lighting-intermediate-data.chunk +++ b/editor/assets/chunks/lighting-models/data-structures/lighting-intermediate-data.chunk @@ -5,9 +5,9 @@ struct LightingIntermediateData float distToCamera, distToCameraSqr; // pos->camera float angleAttenuation, distAttenuation; - float NoL, NoV, NoH;//, VoH; + float NoL, NoV, NoH, VoH; float NoLSat, NoVSat, NoHSat;//, VoHSat; // clamped to 0-1 - float NoVAbsSat; // abs and clamped to 1 + float NoVAbsSat, VoHAbsSat; // abs and clamped to 1 HIGHP_VALUE_STRUCT_DEFINE(vec3, worldPosition); @@ -16,7 +16,7 @@ struct LightingIntermediateData // material data float specularParam; // roughness or specular power - float ior; + float ior, layerOpacity; #if CC_SURFACES_LIGHTING_ANISOTROPIC float anisotropyShape; @@ -92,9 +92,11 @@ void CCSurfacesLightingGetIntermediateData_PerLight(inout LightingIntermediateDa // dot data.NoL = dot(data.N, data.L); data.NoH = dot(data.N, data.H); - // data.VoH = dot(data.V, data.H); + data.VoH = dot(data.V, data.H); data.NoLSat = max(data.NoL, 0.0); data.NoHSat = max(data.NoH, 0.0); // data.VoHSat = max(data.VoH, 0.0); + + data.VoHAbsSat = max(abs(data.VoH), 0.0); } diff --git a/editor/assets/chunks/lighting-models/data-structures/lighting-result.chunk b/editor/assets/chunks/lighting-models/data-structures/lighting-result.chunk index 6ab96c09d35..913a6e7d009 100644 --- a/editor/assets/chunks/lighting-models/data-structures/lighting-result.chunk +++ b/editor/assets/chunks/lighting-models/data-structures/lighting-result.chunk @@ -1,18 +1,21 @@ struct LightingResult { // material-dependent lighting data - vec3 diffuseColorWithLighting, specularColorWithLighting, specularColorWithEnvLighting; + vec3 diffuseColorWithLighting, specularColorWithLighting; // pure lighting - vec3 directDiffuse, directSpecular; - vec3 environmentDiffuse, environmentSpecular; + vec3 directDiffuse, directSpecular, directGF; + vec3 environmentDiffuse, environmentSpecular, environmentGF; float shadow, ao; vec3 lightmapColor; vec3 emissive; // advanced -#if CC_SURFACES_LIGHTING_USE_FRESNEL - float fresnel; + vec3 fresnel; // for transmit materials , rim or special effects + +#if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + vec3 directDiffuseSubLayers, directSpecularSubLayers; + vec3 environmentDiffuseSubLayers, environmentSpecularSubLayers; #endif #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR @@ -20,10 +23,12 @@ struct LightingResult #endif #if CC_SURFACES_LIGHTING_TRANSMIT_DIFFUSE vec3 directTransmitDiffuse, environmentTransmitDiffuse; -#endif +#endif #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR vec3 direct2ndSpecular, environment2ndSpecular; - vec3 specularColorWithLighting2ndSpecular, specularColorWithEnvLighting2ndSpecular; + vec3 specularColorWithLighting2ndSpecular; + vec3 directGF2ndSpecular, environmentGF2ndSpecular; + vec3 directSubLayerF, environmentSubLayerF; #endif #if CC_SURFACES_LIGHTING_TT vec3 directTT; @@ -31,3 +36,13 @@ struct LightingResult #endif //vec3 scattered; }; + + +/* for multi-layer materials, should define: +struct LightingResultLayer +{ + vec3 diffuseColorWithLighting, specularColorWithLighting; + vec3 directDiffuse, directSpecular, directGF; + vec3 environmentDiffuse, environmentSpecular, environmentGF; +} +*/ \ No newline at end of file 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 a9a6316003a..d857c4c88de 100644 --- a/editor/assets/chunks/lighting-models/lighting-flow/common-flow.chunk +++ b/editor/assets/chunks/lighting-models/lighting-flow/common-flow.chunk @@ -11,16 +11,15 @@ CCSurfacesInitializeLightingIntermediateData(lightingData, surfaceData); LightingResult lightingResult; - CCSurfacesLightingInitializeColorWithLighting(lightingResult.diffuseColorWithLighting, lightingResult.specularColorWithLighting, lightingResult.specularColorWithEnvLighting, surfaceData, lightingData); + CCSurfacesLightingInitializeColorWithLighting(lightingResult.diffuseColorWithLighting, lightingResult.specularColorWithLighting, surfaceData, lightingData); lightingResultAccumulated.diffuseColorWithLighting = lightingResult.diffuseColorWithLighting; lightingResultAccumulated.specularColorWithLighting = lightingResult.specularColorWithLighting; - lightingResultAccumulated.specularColorWithEnvLighting = lightingResult.specularColorWithEnvLighting; #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR vec3 diff; SurfacesMaterialData surfaceData2ndSpecular; CCSurfacesGetSurfacesMaterialData2ndSpecular(surfaceData2ndSpecular, surfaceData); - CCSurfacesLightingInitializeColorWithLighting(diff, lightingResultAccumulated.specularColorWithLighting2ndSpecular, lightingResultAccumulated.specularColorWithEnvLighting2ndSpecular, surfaceData2ndSpecular, lightingData); + CCSurfacesLightingInitializeColorWithLighting(diff, lightingResultAccumulated.specularColorWithLighting2ndSpecular, surfaceData2ndSpecular, lightingData); #endif int numLights = CC_PIPELINE_TYPE == CC_PIPELINE_TYPE_FORWARD ? LIGHTS_PER_PASS : int(cc_lightDir[0].w); @@ -29,15 +28,18 @@ vec3 lightDirWithLength = IS_RANGED_DIRECTIONAL_LIGHT(cc_lightPos[i].w) ? -normalize(cc_lightDir[i].xyz) : cc_lightPos[i].xyz - worldPos; CCSurfacesLightingCalculateIntermediateData_PerLight(lightingData, surfaceData, lightDirWithLength); - CCSurfacesLightingCalculateColorWithLighting(lightingResult.diffuseColorWithLighting, lightingResult.specularColorWithLighting, lightingResult.specularColorWithEnvLighting, surfaceData, lightingData); + CCSurfacesLightingCalculateColorWithLighting(lightingResult.diffuseColorWithLighting, lightingResult.specularColorWithLighting, surfaceData, lightingData); vec3 diffuseLighting, specularLighting; CCSurfacesLightingCalculateDirect(diffuseLighting, specularLighting, lightingData, cc_lightColor[i]); - float fresnel = 1.0; #if CC_SURFACES_LIGHTING_USE_FRESNEL - fresnel = lightingResult.fresnel = CCSurfaceLightingCalculateFresnel(lightingData); + vec3 fresnel = CCSurfaceLightingCalculateExtraFresnel(lightingData); + #else + vec3 fresnel = vec3(1.0); #endif + lightingResult.fresnel = fresnel; + float shadow = 1.0; #if CC_RECEIVE_SHADOW && CC_SHADOW_TYPE == CC_SHADOW_MAP @@ -84,7 +86,8 @@ lightingData.angleAttenuation = angleAtt; lightingData.distAttenuation = distAtt; lightingResult.directDiffuse = diffuseLighting * multiplier; - lightingResult.directSpecular = specularLighting * multiplier * fresnel; + lightingResult.directSpecular = specularLighting * multiplier; + CCSurfaceLightingCalculateDirectFresnel(lightingResult.directGF, lightingData, lightingResult.specularColorWithLighting, vec3(1.0)); vec4 attenuatedLightColorAndIntensity = vec4(cc_lightColor[i].xyz, cc_lightColor[i].w * multiplier); @@ -106,7 +109,9 @@ CCSurfacesInitializeLightingIntermediateData(lightingData2ndSpecular, surfaceData2ndSpecular); CCSurfacesLightingCalculateIntermediateData_PerLight(lightingData2ndSpecular, surfaceData, lightDirWithLength); CCSurfacesLightingCalculateDirect2ndSpecular(lightingResult.direct2ndSpecular, lightingData2ndSpecular, attenuatedLightColorAndIntensity, surfaceData2ndSpecular.intensity2ndSpecular, lightingResult.directSpecular); - lightingResult.direct2ndSpecular *= multiplier * fresnel; + CCSurfaceLightingCalculateDirectFresnel(lightingResult.directGF2ndSpecular, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, vec3(1.0)); + + lightingResult.direct2ndSpecular *= multiplier; #endif #if CC_SURFACES_LIGHTING_TT @@ -139,7 +144,7 @@ CCSurfacesInitializeLightingIntermediateData(lightingData, surfaceData); CCSurfacesInitializeLightingResult(lightingResult, surfaceData); - CCSurfacesLightingInitializeColorWithLighting(lightingResult.diffuseColorWithLighting, lightingResult.specularColorWithLighting, lightingResult.specularColorWithEnvLighting, surfaceData, lightingData); + CCSurfacesLightingInitializeColorWithLighting(lightingResult.diffuseColorWithLighting, lightingResult.specularColorWithLighting, surfaceData, lightingData); CCSurfacesLightingCalculateIntermediateData_PerLight(lightingData, surfaceData, -cc_mainLitDir.xyz); @@ -200,20 +205,24 @@ #endif + // apply SSR, local probe & global probe + vec3 unused; + CCSurfacesLightingCalculateEnvironment(lightingResult.environmentDiffuse, lightingResult.environmentSpecular, lightingData, cc_ambientSky.w); + CCSurfaceLightingCalculateEnvironmentFresnel(lightingResult.environmentGF, unused, lightingData, lightingResult.specularColorWithLighting); + // dynamic & stationary dir light lightingResult.directDiffuse = lightingResult.directSpecular = vec3(0.0); #if !CC_DISABLE_DIRECTIONAL_LIGHT && !CC_FORWARD_ADD - CCSurfacesLightingCalculateColorWithLighting(lightingResult.diffuseColorWithLighting, lightingResult.specularColorWithLighting, lightingResult.specularColorWithEnvLighting, surfaceData, lightingData); + CCSurfacesLightingCalculateColorWithLighting(lightingResult.diffuseColorWithLighting, lightingResult.specularColorWithLighting, surfaceData, lightingData); CCSurfacesLightingCalculateDirect(lightingResult.directDiffuse, lightingResult.directSpecular, lightingData, cc_mainLitColor); + CCSurfaceLightingCalculateDirectFresnel(lightingResult.directGF, lightingData, lightingResult.specularColorWithLighting, lightingResult.environmentGF); #endif - // apply SSR, local probe & global probe - CCSurfacesLightingCalculateEnvironment(lightingResult.environmentDiffuse, lightingResult.environmentSpecular, lightingData, cc_ambientSky.w); - - #if CC_SURFACES_LIGHTING_USE_FRESNEL - lightingResult.fresnel = CCSurfaceLightingCalculateFresnel(lightingData); + lightingResult.fresnel = CCSurfaceLightingCalculateExtraFresnel(lightingData); + #else + lightingResult.fresnel = vec3(1.0); #endif #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR @@ -236,11 +245,53 @@ LightingIntermediateData lightingData2ndSpecular; CCSurfacesInitializeLightingIntermediateData(lightingData2ndSpecular, surfaceData2ndSpecular); CCSurfacesLightingCalculateIntermediateData_PerLight(lightingData2ndSpecular, surfaceData2ndSpecular, -cc_mainLitDir.xyz); - CCSurfacesLightingCalculateDirect2ndSpecular(lightingResult.direct2ndSpecular, lightingData2ndSpecular, cc_mainLitColor, surfaceData2ndSpecular.intensity2ndSpecular, lightingResult.directSpecular); - CCSurfacesLightingCalculateEnvironment2ndSpecular(lightingResult.environment2ndSpecular, lightingData2ndSpecular, cc_ambientSky.w, surfaceData2ndSpecular.intensity2ndSpecular, lightingResult.environmentSpecular); vec3 diff; - CCSurfacesLightingInitializeColorWithLighting(diff, lightingResult.specularColorWithLighting2ndSpecular, lightingResult.specularColorWithEnvLighting2ndSpecular, surfaceData2ndSpecular, lightingData2ndSpecular); + 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); + + CCSurfacesLightingCalculateDirect2ndSpecular(lightingResult.direct2ndSpecular, lightingData2ndSpecular, cc_mainLitColor, surfaceData2ndSpecular.intensity2ndSpecular, lightingResult.directSpecular); + CCSurfaceLightingCalculateDirectFresnel(lightingResult.directGF2ndSpecular, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, lightingResult.environmentGF2ndSpecular); + + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + vec3 lastLayerF, zeroVector = vec3(0.0); + vec3 blendedBaseD = lightingResult.directDiffuse, blendedBaseS = lightingResult.directSpecular; + CCSurfaceLightingCalculateDirectSubLayerFresnel(lightingResult.directSubLayerF, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular); + InitializeLayerBlending(blendedBaseD, blendedBaseS, + lightingResult.directDiffuseSubLayers, lightingResult.directSpecularSubLayers, + lastLayerF, + lightingResult.directDiffuse, lightingResult.directSpecular + ); + CalculateLayerBlending (blendedBaseD, blendedBaseS, + lightingResult.directDiffuseSubLayers, lightingResult.directSpecularSubLayers, + lastLayerF, + zeroVector, zeroVector, + lightingResult.direct2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, + lightingData2ndSpecular.layerOpacity, lightingResult.directSubLayerF + ); + lightingResult.directDiffuse = blendedBaseD; + lightingResult.directSpecular = blendedBaseS; + + zeroVector = vec3(0.0); + blendedBaseD = lightingResult.environmentDiffuse; blendedBaseS = lightingResult.environmentSpecular; + CCSurfaceLightingCalculateEnvironmentSubLayerFresnel(lightingResult.environmentSubLayerF, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular); + InitializeLayerBlending(blendedBaseD, blendedBaseS, + lightingResult.environmentDiffuseSubLayers, lightingResult.environmentSpecularSubLayers, + lastLayerF, + lightingResult.environmentDiffuse, lightingResult.environmentSpecular + ); + CalculateLayerBlending (blendedBaseD, blendedBaseS, + lightingResult.environmentDiffuseSubLayers, lightingResult.environmentSpecularSubLayers, + lastLayerF, + zeroVector, zeroVector, + lightingResult.environment2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, + lightingData2ndSpecular.layerOpacity, lightingResult.environmentSubLayerF + ); + lightingResult.environmentDiffuse = blendedBaseD; + lightingResult.environmentSpecular = blendedBaseS; + #endif #endif #if CC_SURFACES_LIGHTING_TT diff --git a/editor/assets/chunks/lighting-models/model-functions/standard.chunk b/editor/assets/chunks/lighting-models/model-functions/standard.chunk index 922bc513a21..d0bbef405e9 100644 --- a/editor/assets/chunks/lighting-models/model-functions/standard.chunk +++ b/editor/assets/chunks/lighting-models/model-functions/standard.chunk @@ -59,12 +59,42 @@ void CCSurfacesLightingCalculateEnvironment(out vec3 lightingDiffuse, out vec3 l } #if CC_SURFACES_LIGHTING_USE_FRESNEL - float CCSurfaceLightingCalculateFresnel(in LightingIntermediateData lightingData) - { - return CalculateFresnelCoefficient(lightingData.ior, abs(lightingData.NoV)); //NoVSat for single side, and NoVAbs for two sided - } +vec3 CCSurfaceLightingCalculateExtraFresnel(in LightingIntermediateData lightingData) +{ + float fresnel = CalculateFresnelCoefficient(lightingData.ior, lightingData.NoVAbsSat); //NoVSat for single side, and NoVAbsSat for two sided + return vec3(fresnel); +} #endif +void CCSurfaceLightingCalculateDirectFresnel(out vec3 directGF, in LightingIntermediateData lightingData, vec3 specularColor, in vec3 environmentGF) +{ + #if CC_SURFACES_USE_LEGACY_COMPATIBLE_LIGHTING + // local lighting also use EnvIntegratedFresnel for estimated GF terms, calculate accurate GF instead in the future, CCSurfacesDebugViewLightingResult also needs modified + directGF = environmentGF; + #else + float roughness = lightingData.specularParam; + directGF = F_SchlickMultiplier(specularColor, lightingData.VoHAbsSat) * G_Schlick(roughness, lightingData.NoVSat, lightingData.NoLSat); + #endif +} + +void CCSurfaceLightingCalculateEnvironmentFresnel(out vec3 integratedGF, vec3 integratedF, in LightingIntermediateData lightingData, vec3 specularColor) +{ + float roughness = lightingData.specularParam; + IntegratedGFMultiplier(integratedGF, integratedF, specularColor, roughness, lightingData.NoVAbsSat); +} + +void CCSurfaceLightingCalculateDirectSubLayerFresnel(out vec3 subLayerF, in LightingIntermediateData lightingData, vec3 specularColor) +{ + float VoH = lightingData.VoHAbsSat; + subLayerF = vec3(exp2( (-5.55473 * VoH - 6.98316) * VoH )); +} +void CCSurfaceLightingCalculateEnvironmentSubLayerFresnel(out vec3 subLayerF, in LightingIntermediateData lightingData, vec3 specularColor) +{ + // ior related fresnel: + subLayerF = vec3(CalculateFresnelCoefficient(lightingData.ior, lightingData.NoVSat)); +} + + #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR // do not support anisotropy void CCSurfacesLightingCalculateDirectTransmitSpecular(out vec3 lightingSpecular, in LightingIntermediateData lightingData, in vec4 lightSourceColorAndIntensity) @@ -218,3 +248,47 @@ void CCSurfacesLightingCalculateEnvironment(out vec3 lightingDiffuse, out vec3 l lightingResult.directTT = ttLighting * DiffuseCoefficient_EnergyConservation * lightSourceColorAndIntensity.xyz* lightSourceColorAndIntensity.w; } #endif + + +// LastLayerF for temporary use +// S is spec without GF +void InitializeLayerBlending(out vec3 blendedBaseLayerD, out vec3 blendedBaseLayerS, + out vec3 blendedSubLayerD, out vec3 blendedSubLayerS, + out vec3 lastLayerF, + in vec3 baseD, in vec3 baseS + ) +{ + blendedBaseLayerD = baseD; + blendedBaseLayerS = baseS; + blendedSubLayerD = blendedSubLayerS = vec3(0.0); + lastLayerF = vec3(1.0); +} + +// use subLayerF instead of subLayerGF for ior-dependent 0-1 range +void CalculateLayerBlending(inout vec3 blendedBaseLayerD, inout vec3 blendedBaseLayerS, + inout vec3 blendedSubLayerD, inout vec3 blendedSubLayerS, + inout vec3 lastLayerF, + in vec3 subLayerD, in vec3 subLayerDiffuseColor, + in vec3 subLayerS, in vec3 subLayerSpecularColor, + in float subLayerOpacity, inout vec3 subLayerF + ) +{ + subLayerF = saturate(subLayerF * subLayerOpacity); + blendedSubLayerD = blendedSubLayerD * (vec3(1.0) - lastLayerF) + subLayerD * subLayerDiffuseColor * subLayerF; + blendedSubLayerS = blendedSubLayerS *(vec3(1.0) - lastLayerF) + subLayerS * subLayerSpecularColor * subLayerF; + blendedBaseLayerD *= vec3(1.0) - subLayerF; + blendedBaseLayerS *= vec3(1.0) - subLayerF; + lastLayerF = subLayerF; +} +/*void CCSurfacesLightingInitializeLayerBlending(inout LightingResult lightingResult, out vec3 lastLayerF) +{ + InitializeLayerBlending(lightingResult.directDiffuseSubLayers, lightingResult.directDiffuseSubLayers, + lightingResult.directDiffuseSubLayers, lightingResult.directDiffuseSubLayers); +} +void CCSurfacesLightingCalculateLayerBlending(inout LightingResult lightingResult, inout vec3 lastLayerF, + in vec3 subLayerD, in vec3 subLayerS, inout vec3 subLayerF + ) +{ + // CalculateLayerBlending(lightingResult.); +} +*/ \ No newline at end of file diff --git a/editor/assets/chunks/lighting-models/model-functions/toon.chunk b/editor/assets/chunks/lighting-models/model-functions/toon.chunk index cdea2dff79a..1e4088faae4 100644 --- a/editor/assets/chunks/lighting-models/model-functions/toon.chunk +++ b/editor/assets/chunks/lighting-models/model-functions/toon.chunk @@ -40,3 +40,21 @@ void CCSurfacesLightingCalculateEnvironment(out vec3 lightingDiffuse, out vec3 l lightingDiffuse = vec3(0.0); lightingSpecular = vec3(0.0); } + + +#if CC_SURFACES_LIGHTING_USE_FRESNEL +vec3 CCSurfaceLightingCalculateExtraFresnel(in LightingIntermediateData lightingData) +{ + return vec3(1.0); +} +#endif + +void CCSurfaceLightingCalculateDirectFresnel(out vec3 fresnel, in LightingIntermediateData lightingData, vec3 specularColor, in vec3 environmentGF) +{ + fresnel = vec3(1.0); +} + +void CCSurfaceLightingCalculateEnvironmentFresnel(out vec3 integratedGF, out vec3 integratedF, in LightingIntermediateData lightingData, vec3 specularColor) +{ + integratedF = integratedGF = vec3(1.0); +} diff --git a/editor/assets/chunks/surfaces/data-structures/standard.chunk b/editor/assets/chunks/surfaces/data-structures/standard.chunk index 52cbfb976b9..fd8f052538a 100644 --- a/editor/assets/chunks/surfaces/data-structures/standard.chunk +++ b/editor/assets/chunks/surfaces/data-structures/standard.chunk @@ -27,9 +27,14 @@ struct SurfacesMaterialData vec4 transmitDiffuseParams; #endif #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR + // for storage only, copied with CCSurfacesGetSurfacesMaterialData2ndSpecular vec3 baseColor2ndSpecular, color2ndSpecular; - float intensity2ndSpecular, roughness2ndSpecular; + float intensity2ndSpecular, roughness2ndSpecular, metallic2ndSpecular; vec3 worldNormal2ndSpecular, worldTangent2ndSpecular, worldBinormal2ndSpecular; + #if CC_SURFACES_LIGHTING_ANISOTROPIC + float anisotropyShape2ndSpecular; + #endif + float ior2ndSpecular, opacity2ndSpecular; // for multi-layer #endif #if CC_SURFACES_LIGHTING_TT vec3 baseColorTT; diff --git a/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk b/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk index e7f3446aacd..230cbf6f2bb 100644 --- a/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk +++ b/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk @@ -164,6 +164,18 @@ vec3 SurfacesFragmentModifyTTColor(in vec3 baseColor) } #endif +#ifndef CC_SURFACES_FRAGMENT_MODIFY_SSS_PARAMS +// depends on CC_SURFACES_FRAGMENT_MODIFY_SSS_PARAMS +vec4 SurfacesFragmentModifySSSParams() +{ + // x: sssIntensity, moisture + // y: sssCurvature, for preintegrated sss + // z: sssColoration, for sss transmitted light color with dark area + // w: unused + return vec4(1.0, 0.1, 1.0, 0.0); +} +#endif + #ifndef CC_SURFACES_FRAGMENT_MODIFY_DUAL_LOBE_SPECULAR_PARAMS // depends on CC_SURFACES_FRAGMENT_MODIFY_DUAL_LOBE_SPECULAR_PARAMS vec4 SurfacesFragmentModifyDualLobeSpecularParams(float roughness) @@ -174,15 +186,32 @@ vec4 SurfacesFragmentModifyDualLobeSpecularParams(float roughness) } #endif -#ifndef CC_SURFACES_FRAGMENT_MODIFY_SSS_PARAMS -// depends on CC_SURFACES_FRAGMENT_MODIFY_SSS_PARAMS -vec4 SurfacesFragmentModifySSSParams() +#ifndef CC_SURFACES_FRAGMENT_MODIFY_CLEAR_COAT_PARAMS +// depends on CC_SURFACES_FRAGMENT_MODIFY_CLEAR_COAT_PARAMS +vec4 SurfacesFragmentModifyClearCoatParams() { - // x: sssIntensity, moisture - // y: sssCurvature, for preintegrated sss - // z: sssColoration, for sss transmitted light color with dark area - // w: unused - return vec4(1.0, 0.1, 1.0, 0.0); + //x: coat roughness + //y: coat ior + //z: coat opacity + //w: coat intensity + return vec4(0.2, 1.5, 1.0, 1.0); +} +#endif + +#ifndef CC_SURFACES_FRAGMENT_MODIFY_CLEAR_COAT_COLOR +// depends on CC_SURFACES_FRAGMENT_MODIFY_CLEAR_COAT_COLOR +vec3 SurfacesFragmentModifyClearCoatColor() +{ + // for iridescense color variation + return vec3(1.0, 1.0, 1.0); +} +#endif + +#ifndef CC_SURFACES_FRAGMENT_MODIFY_CLEAR_COAT_WORLD_NORMAL +// depends on CC_SURFACES_FRAGMENT_MODIFY_CLEAR_COAT_WORLD_NORMAL +vec3 SurfacesFragmentModifyClearCoatWorldNormal() +{ + return normalize(FSInput_worldNormal); } #endif diff --git a/editor/assets/chunks/surfaces/effect-macros/common-macros.chunk b/editor/assets/chunks/surfaces/effect-macros/common-macros.chunk index ec593297817..6c8caca0b8d 100644 --- a/editor/assets/chunks/surfaces/effect-macros/common-macros.chunk +++ b/editor/assets/chunks/surfaces/effect-macros/common-macros.chunk @@ -87,11 +87,20 @@ #define CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR 1 #endif #endif - #ifndef CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR #define CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR 0 #endif +// this value should be sub-layers count +#ifndef CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + #if CC_SURFACES_LIGHTING_CLEAR_COAT + #define CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND 1 + #endif +#endif +#ifndef CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + #define CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND 0 +#endif + // render-to-color-map can disable functionality to avoid error data map #ifndef CC_SURFACES_ENABLE_DEBUG_VIEW diff --git a/editor/assets/chunks/surfaces/module-functions/debug-view.chunk b/editor/assets/chunks/surfaces/module-functions/debug-view.chunk index 2ea1d14f81e..5f380b79db6 100644 --- a/editor/assets/chunks/surfaces/module-functions/debug-view.chunk +++ b/editor/assets/chunks/surfaces/module-functions/debug-view.chunk @@ -110,13 +110,13 @@ vec4 CCSurfacesDebugDisplayInvalidInputData(vec4 color, vec3 data) if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_DIRECT_SPECULAR)) { - color.rgb = lightingResult.directSpecular * lightingResult.specularColorWithEnvLighting; + color.rgb = lightingResult.directSpecular * lightingResult.specularColorWithLighting * lightingResult.directGF; isSRGBColor = true; } if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_DIRECT_ALL)) { - color.rgb = lightingResult.directDiffuse * lightingResult.diffuseColorWithLighting + lightingResult.directSpecular * lightingResult.specularColorWithLighting; + color.rgb = lightingResult.directDiffuse * lightingResult.diffuseColorWithLighting + lightingResult.directSpecular * lightingResult.specularColorWithLighting * lightingResult.directGF; isSRGBColor = true; } @@ -128,13 +128,13 @@ vec4 CCSurfacesDebugDisplayInvalidInputData(vec4 color, vec3 data) if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_ENV_SPECULAR)) { - color.rgb = lightingResult.environmentSpecular * lightingResult.specularColorWithEnvLighting; + color.rgb = lightingResult.environmentSpecular * lightingResult.specularColorWithLighting * lightingResult.environmentGF; isSRGBColor = true; } if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_ENV_ALL)) { - color.rgb = lightingResult.environmentDiffuse * lightingResult.diffuseColorWithLighting + lightingResult.environmentSpecular * lightingResult.specularColorWithLighting; + color.rgb = lightingResult.environmentDiffuse * lightingResult.diffuseColorWithLighting + lightingResult.environmentSpecular * lightingResult.specularColorWithLighting * lightingResult.environmentGF; isSRGBColor = true; } @@ -162,14 +162,11 @@ vec4 CCSurfacesDebugDisplayInvalidInputData(vec4 color, vec3 data) isSRGBColor = false; } - float fresnel = 0.0; + vec3 fresnel = lightingResult.fresnel; vec3 directTransmitSpecular = vec3(0.0), environmentTransmitSpecular = vec3(0.0); vec3 directTransmitDiffuse = vec3(0.0), environmentTransmitDiffuse = vec3(0.0); vec3 diffuseColorWithLightingTT = vec3(0.0), specularColorWithLighting2ndSpecular = vec3(0.0); vec3 direct2ndSpecular = vec3(0.0), environment2ndSpecular = vec3(0.0); - #if CC_SURFACES_LIGHTING_USE_FRESNEL - fresnel = lightingResult.fresnel; - #endif #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR directTransmitSpecular = lightingResult.directTransmitSpecular; environmentTransmitSpecular = lightingResult.environmentTransmitSpecular; @@ -179,11 +176,19 @@ vec4 CCSurfacesDebugDisplayInvalidInputData(vec4 color, vec3 data) environmentTransmitDiffuse = lightingResult.environmentTransmitDiffuse; #endif #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR - direct2ndSpecular = lightingResult.direct2ndSpecular; - environment2ndSpecular = lightingResult.environment2ndSpecular; - specularColorWithLighting2ndSpecular = lightingResult.specularColorWithEnvLighting2ndSpecular; + direct2ndSpecular = lightingResult.direct2ndSpecular * lightingResult.directGF2ndSpecular; + environment2ndSpecular = lightingResult.environment2ndSpecular * lightingResult.environmentGF2ndSpecular; + specularColorWithLighting2ndSpecular = lightingResult.specularColorWithLighting2ndSpecular; + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + fresnel = lightingResult.environmentSubLayerF; + #endif + #endif + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + direct2ndSpecular = lightingResult.directSpecularSubLayers; + environment2ndSpecular = lightingResult.environmentSpecularSubLayers; #endif + if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_FRESNEL)) { color.rgb = vec3(fresnel); @@ -287,9 +292,7 @@ vec4 CCSurfacesDebugDisplayInvalidInputData(vec4 color, vec3 data) if (!IS_DEBUG_VIEW_COMPOSITE_ENABLE_AO) lightingResult.ao = 1.0; - #if CC_SURFACES_LIGHTING_USE_FRESNEL if (!IS_DEBUG_VIEW_COMPOSITE_ENABLE_FRESNEL) - lightingResult.fresnel = 1.0; - #endif + lightingResult.fresnel = vec3(1.0); } #endif diff --git a/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk b/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk index ce3c9a81b59..f292782b3cc 100644 --- a/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk +++ b/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk @@ -47,12 +47,18 @@ void CCSurfacesFragmentGetMaterialData(inout SurfacesMaterialData surfaceData) // skip in shader graph, need extra code vec4 trtParams = SurfacesFragmentModifyTRTParams(); surfaceData.roughness2ndSpecular = saturate(surfaceData.roughness + trtParams.x); + surfaceData.metallic2ndSpecular = surfaceData.metallic; surfaceData.intensity2ndSpecular = trtParams.w; surfaceData.baseColor2ndSpecular = vec3(1.0); // specular color use specified trt color only surfaceData.color2ndSpecular = SurfacesFragmentModifyTRTColor(); 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; + #endif RotateNormalAndBinormal(surfaceData.worldBinormal2ndSpecular, surfaceData.worldNormal2ndSpecular, surfaceData.worldTangent2ndSpecular, trtParams.y, FSInput_mirrorNormal); #endif @@ -67,12 +73,37 @@ void CCSurfacesFragmentGetMaterialData(inout SurfacesMaterialData surfaceData) // skip in shader graph, need extra code vec4 dualLobeParams = SurfacesFragmentModifyDualLobeSpecularParams(surfaceData.roughness); surfaceData.roughness2ndSpecular = saturate(dualLobeParams.x); + surfaceData.metallic2ndSpecular = surfaceData.metallic; surfaceData.intensity2ndSpecular = dualLobeParams.w; surfaceData.baseColor2ndSpecular = surfaceData.baseColor.rgb; surfaceData.color2ndSpecular = vec3(1.0); // no extra coloration surfaceData.worldNormal2ndSpecular = surfaceData.worldNormal; surfaceData.worldTangent2ndSpecular = surfaceData.worldTangent; surfaceData.worldBinormal2ndSpecular = surfaceData.worldBinormal; + surfaceData.ior2ndSpecular = surfaceData.ior; + surfaceData.opacity2ndSpecular = 1.0; // unused + #if CC_SURFACES_LIGHTING_ANISOTROPIC + surfaceData.anisotropyShape2ndSpecular = surfaceData.anisotropyShape; + #endif +#endif + +#if CC_SURFACES_LIGHTING_CLEAR_COAT + // skip in shader graph, need extra code + vec4 clearCoatParams = SurfacesFragmentModifyClearCoatParams(); + surfaceData.roughness2ndSpecular = clearCoatParams.x; + surfaceData.metallic2ndSpecular = 0.0; + surfaceData.ior2ndSpecular = clearCoatParams.y; + surfaceData.opacity2ndSpecular = clearCoatParams.z; + surfaceData.intensity2ndSpecular = clearCoatParams.w; + surfaceData.baseColor2ndSpecular = vec3(1.0); // specular color use specified coat color only + surfaceData.color2ndSpecular = SurfacesFragmentModifyClearCoatColor(); + surfaceData.worldNormal2ndSpecular = SurfacesFragmentModifyClearCoatWorldNormal(); + surfaceData.worldBinormal2ndSpecular = surfaceData.worldBinormal; + surfaceData.worldTangent2ndSpecular = CalculateTangent(surfaceData.worldNormal2ndSpecular, surfaceData.worldBinormal2ndSpecular); + #if CC_SURFACES_LIGHTING_ANISOTROPIC + surfaceData.anisotropyShape2ndSpecular = surfaceData.anisotropyShape; + // float anisoRotation = xxx; RotateNormalAndBinormal(surfaceData.worldBinormal2ndSpecular, surfaceData.worldNormal2ndSpecular, surfaceData.worldTangent2ndSpecular, anisoRotation, FSInput_mirrorNormal); + #endif #endif #if CC_SURFACES_LIGHTING_SSS @@ -114,19 +145,20 @@ vec3 CCSurfacesGetSpecularColor(in SurfacesMaterialData surfaceData) } // Diffuse/Specular Color with BRDF lighting preparation -void CCSurfacesLightingInitializeColorWithLighting(inout vec3 diffuseColorWithLighting, inout vec3 specularColorWithLighting, inout vec3 specularColorWithEnvLighting, in SurfacesMaterialData surfaceData, in LightingIntermediateData lightingData) +void CCSurfacesLightingInitializeColorWithLighting(inout vec3 diffuseColorWithLighting, inout vec3 specularColorWithLighting, in SurfacesMaterialData surfaceData, in LightingIntermediateData lightingData) { diffuseColorWithLighting = CCSurfacesGetDiffuseColor(surfaceData); specularColorWithLighting = CCSurfacesGetSpecularColor(surfaceData).xyz; // (Intergrated) GF/4Pi use approximate value for both direct lighting and environment lighting // accuracy value can be calculated in LightingCalculateDirect/Environment instead of IntegratedGFApprox - specularColorWithEnvLighting = IntegratedGFApprox(specularColorWithLighting, surfaceData.roughness, lightingData.NoVAbsSat); + // specularColorWithEnvLighting = IntegratedGFApprox(specularColorWithLighting, surfaceData.roughness, lightingData.NoVAbsSat); } // Update two colors with BRDF which depend on lights (optional) -void CCSurfacesLightingCalculateColorWithLighting(inout vec3 diffuseColorWithLighting, inout vec3 specularColorWithLighting, inout vec3 specularColorWithEnvLighting, in SurfacesMaterialData surfaceData, in LightingIntermediateData lightingData) +void CCSurfacesLightingCalculateColorWithLighting(inout vec3 diffuseColorWithLighting, inout vec3 specularColorWithLighting, in SurfacesMaterialData surfaceData, in LightingIntermediateData lightingData) { } + // Copy material data to lighting data // such as tangent data for anisotropic materials void CCSurfacesInitializeLightingIntermediateData(inout LightingIntermediateData lightingData, in SurfacesMaterialData surfaceData) @@ -140,6 +172,7 @@ void CCSurfacesInitializeLightingIntermediateData(inout LightingIntermediateData ); lightingData.specularParam = surfaceData.roughness; lightingData.ior = surfaceData.ior; + lightingData.layerOpacity = surfaceData.baseColor.a; #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR || CC_SURFACES_LIGHTING_TRANSMIT_DIFFUSE lightingData.transmitScatteringParams = surfaceData.transmitScatteringParams; lightingData.inScatteringColor = surfaceData.inScatteringColor; @@ -178,11 +211,16 @@ void CCSurfacesGetLightingIntermediateDataTransmitDiffuse(inout LightingIntermed void CCSurfacesGetSurfacesMaterialData2ndSpecular(inout SurfacesMaterialData surfaceData2ndSpecular, in SurfacesMaterialData surfaceData) { surfaceData2ndSpecular = surfaceData; - surfaceData2ndSpecular.baseColor = vec4(surfaceData.baseColor2ndSpecular, 1.0); + surfaceData2ndSpecular.baseColor = vec4(surfaceData.baseColor2ndSpecular, surfaceData.opacity2ndSpecular); surfaceData2ndSpecular.roughness = surfaceData.roughness2ndSpecular; + surfaceData2ndSpecular.metallic = surfaceData.metallic2ndSpecular; surfaceData2ndSpecular.worldNormal = surfaceData.worldNormal2ndSpecular; surfaceData2ndSpecular.worldTangent = surfaceData.worldTangent2ndSpecular; surfaceData2ndSpecular.worldBinormal = surfaceData.worldBinormal2ndSpecular; + surfaceData2ndSpecular.ior = surfaceData.ior2ndSpecular; + #if CC_SURFACES_LIGHTING_ANISOTROPIC + surfaceData2ndSpecular.anisotropyShape = surfaceData.anisotropyShape2ndSpecular; + #endif } #endif @@ -191,12 +229,21 @@ void CCSurfacesInitializeLightingResult(inout LightingResult lightingResult, in { lightingResult.ao = surfaceData.ao; lightingResult.emissive = surfaceData.emissive; + lightingResult.directGF = vec3(1.0); +#if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + lightingResult.directDiffuseSubLayers = lightingResult.directSpecularSubLayers = vec3(0.0); + lightingResult.environmentDiffuseSubLayers = lightingResult.environmentSpecularSubLayers = vec3(0.0); +#endif } // Init accumulated lighting results for additive pass void CCSurfacesInitializeLightingResult(inout LightingResult lightingResult) { lightingResult.directDiffuse = lightingResult.directSpecular = vec3(0.0); + lightingResult.directGF = vec3(1.0); +#if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + lightingResult.directDiffuseSubLayers = lightingResult.directSpecularSubLayers = vec3(0.0); +#endif #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR lightingResult.directTransmitSpecular = vec3(0.0); @@ -212,20 +259,28 @@ void CCSurfacesInitializeLightingResult(inout LightingResult lightingResult) #endif } -// Accumulated lighting results for additive pass +//todo: vec3 CCSurfacesLightingBlendLayer(LightingResult lightingResult, MaterialSurfaceData surfaceData)D1, S1, F1, D2, S2, F2),需要带着材质颜色来混 +//多光源下只支持在一个pass内完成,即在common-flow中循环每个光源都是一个lightingResult,每个都调用BlendLayer,然后累加最终颜色即可 + +// Accumulated lighting results for additive pass only void CCSurfacesAccumulateLightingResult(inout LightingResult lightingResultAccumulated, in LightingResult lightingResult) { lightingResultAccumulated.directDiffuse += lightingResult.directDiffuse * lightingResult.shadow; - lightingResultAccumulated.directSpecular += lightingResult.directSpecular * lightingResult.shadow; + lightingResultAccumulated.directSpecular += lightingResult.directSpecular * lightingResult.shadow * lightingResult.directGF * lightingResult.fresnel; + + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + lightingResultAccumulated.directDiffuseSubLayers += lightingResult.directDiffuseSubLayers * lightingResult.shadow; + lightingResultAccumulated.directSpecularSubLayers += lightingResult.directSpecularSubLayers * lightingResult.shadow; + #endif #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR - lightingResultAccumulated.directTransmitSpecular += lightingResult.directTransmitSpecular; + lightingResultAccumulated.directTransmitSpecular += lightingResult.directTransmitSpecular * (vec3(1.0) - lightingResult.fresnel); #endif #if CC_SURFACES_LIGHTING_TRANSMIT_DIFFUSE lightingResultAccumulated.directTransmitDiffuse += lightingResult.directTransmitDiffuse; #endif #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR - lightingResultAccumulated.direct2ndSpecular += lightingResult.direct2ndSpecular * lightingResult.shadow; + lightingResultAccumulated.direct2ndSpecular += lightingResult.direct2ndSpecular * lightingResult.shadow * lightingResult.directGF2ndSpecular * lightingResult.fresnel; #endif #if CC_SURFACES_LIGHTING_TT lightingResultAccumulated.directTT += lightingResult.directTT * lightingResult.shadow; @@ -256,16 +311,20 @@ vec4 CCSurfacesShading(in SurfacesMaterialData surfaceData, in LightingResult li { vec4 color = vec4(0.0, 0.0, 0.0, surfaceData.baseColor.a); - // todo: local lighting also use EnvLightingSpecularColor for estimated GF terms, calculate accurate GF instead in the future, CCSurfacesDebugViewLightingResult also needs modified #if CC_FORWARD_ADD // shadow and fresnel has already been applied with common flow color.xyz += lightingResult.directDiffuse * lightingResult.diffuseColorWithLighting - + lightingResult.directSpecular * lightingResult.specularColorWithEnvLighting + + lightingResult.directSpecular * lightingResult.specularColorWithLighting * lightingResult.directGF #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR - + lightingResult.directTransmitSpecular * lightingResult.specularColorWithEnvLighting + + lightingResult.directTransmitSpecular * lightingResult.specularColorWithLighting * lightingResult.directGF #endif - #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR - + lightingResult.direct2ndSpecular * lightingResult.specularColorWithEnvLighting2ndSpecular * surfaceData.color2ndSpecular + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + + lightingResult.directDiffuseSubLayers + + lightingResult.directSpecularSubLayers + #else + #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR + + lightingResult.direct2ndSpecular * lightingResult.specularColorWithLighting2ndSpecular * surfaceData.color2ndSpecular * lightingResult.directGF2ndSpecular + #endif #endif #if CC_SURFACES_LIGHTING_TT + lightingResult.directTT * lightingResult.diffuseColorWithLightingTT @@ -276,20 +335,22 @@ vec4 CCSurfacesShading(in SurfacesMaterialData surfaceData, in LightingResult li ; #else - float fresnel = 1.0; - #if CC_SURFACES_LIGHTING_USE_FRESNEL - fresnel = lightingResult.fresnel; - #endif - float invFresnel = 1.0 - fresnel; + vec3 fresnel = lightingResult.fresnel; + vec3 invFresnel = vec3(1.0) - fresnel; color.xyz += ( lightingResult.directDiffuse * lightingResult.diffuseColorWithLighting - + lightingResult.directSpecular * lightingResult.specularColorWithEnvLighting * fresnel + + lightingResult.directSpecular * lightingResult.specularColorWithLighting * lightingResult.directGF * fresnel #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR - + lightingResult.directTransmitSpecular * lightingResult.specularColorWithEnvLighting * invFresnel + + lightingResult.directTransmitSpecular * lightingResult.specularColorWithLighting * lightingResult.directGF * invFresnel #endif - #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR - + lightingResult.direct2ndSpecular * lightingResult.specularColorWithEnvLighting2ndSpecular * surfaceData.color2ndSpecular + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + + lightingResult.directDiffuseSubLayers + + lightingResult.directSpecularSubLayers + #else + #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR + + lightingResult.direct2ndSpecular * lightingResult.specularColorWithLighting2ndSpecular * surfaceData.color2ndSpecular * lightingResult.directGF2ndSpecular + #endif #endif #if CC_SURFACES_LIGHTING_TT + lightingResult.directTT * lightingResult.diffuseColorWithLightingTT @@ -309,12 +370,17 @@ vec4 CCSurfacesShading(in SurfacesMaterialData surfaceData, in LightingResult li color.xyz += ( lightingResult.environmentDiffuse * lightingResult.diffuseColorWithLighting - + lightingResult.environmentSpecular * lightingResult.specularColorWithEnvLighting * fresnel + + lightingResult.environmentSpecular * lightingResult.specularColorWithLighting * lightingResult.environmentGF * fresnel #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR - + lightingResult.environmentTransmitSpecular * lightingResult.specularColorWithEnvLighting * invFresnel + + lightingResult.environmentTransmitSpecular * lightingResult.specularColorWithLighting * lightingResult.environmentGF * invFresnel #endif - #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR - + lightingResult.environment2ndSpecular * lightingResult.specularColorWithEnvLighting2ndSpecular * surfaceData.color2ndSpecular + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + + lightingResult.environmentDiffuseSubLayers + + lightingResult.environmentSpecularSubLayers + #else + #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR + + lightingResult.environment2ndSpecular * lightingResult.specularColorWithLighting2ndSpecular * surfaceData.color2ndSpecular * lightingResult.environmentGF2ndSpecular + #endif #endif ) * lightingResult.ao @@ -324,8 +390,87 @@ vec4 CCSurfacesShading(in SurfacesMaterialData surfaceData, in LightingResult li ; color.xyz += lightingResult.emissive; -#endif + return color; + //todo: + //ior and fresnel range? + //整理代码blend layers + + //test + LightingIntermediateData lightingData; + CCSurfacesInitializeLightingIntermediateData(lightingData, surfaceData); + CCSurfacesLightingCalculateIntermediateData_PerLight(lightingData, surfaceData, -cc_mainLitDir.xyz); + + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR + color.xyz = lightingResult.environmentSpecularSubLayers; + // color.xyz = lightingResult.environment2ndSpecular * lightingResult.specularColorWithLighting2ndSpecular * surfaceData.color2ndSpecular * lightingResult.environmentGF2ndSpecular; + color.xyz = lightingResult.environmentSpecular * lightingResult.specularColorWithLighting * lightingResult.environmentGF; + // color.xyz = vec3(lightingResult.environmentSubLayerF); + return color; + #endif + + float VoH = lightingData.VoH; + float Fc = exp2( (-5.55473 * VoH - 6.98316) * VoH ); + + float LoH = dot(lightingData.L, lightingData.H); + color.xyz = F_Schlick(lightingResult.specularColorWithLighting, lightingData.VoH); + color.xyz = F_Schlick(lightingResult.specularColorWithLighting, LoH); + color.xyz = lightingResult.environmentSpecularSubLayers; + color.xyz = vec3(1.0 - Fc); + return color; + color.xyz = vec3(lightingResult.environmentGF2ndSpecular * lightingResult.environment2ndSpecular * lightingResult.specularColorWithLighting2ndSpecular); + color.xyz = vec3(lightingResult.directSpecular * lightingResult.specularColorWithLighting * lightingResult.directGF * fresnel); + color.xyz = vec3(lightingResult.environmentSpecular * lightingResult.specularColorWithLighting * lightingResult.environmentGF); + color.xyz = vec3(0.0); + color.xyz += + ( lightingResult.directDiffuse * lightingResult.diffuseColorWithLighting + + lightingResult.directSpecular * lightingResult.specularColorWithLighting * lightingResult.directGF * fresnel + #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR + + lightingResult.directTransmitSpecular * lightingResult.specularColorWithLighting * lightingResult.directGF * invFresnel + #endif + #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR + + lightingResult.direct2ndSpecular * lightingResult.specularColorWithLighting2ndSpecular * surfaceData.color2ndSpecular * lightingResult.directGF2ndSpecular + #endif + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + + lightingResult.directDiffuseSubLayers + + lightingResult.directSpecularSubLayers + #endif + #if CC_SURFACES_LIGHTING_TT + + lightingResult.directTT * lightingResult.diffuseColorWithLightingTT + #endif + ) + * lightingResult.shadow + #if CC_SURFACES_LIGHTING_TRANSMIT_DIFFUSE + + lightingResult.directTransmitDiffuse + #endif + ; + + #if CC_SURFACES_USE_LIGHT_MAP == LIGHT_MAP_TYPE_ALL_IN_ONE + color.xyz += lightingResult.lightmapColor * lightingResult.diffuseColorWithLighting * lightingResult.shadow; //apply real-time shadows + #elif CC_SURFACES_USE_LIGHT_MAP == LIGHT_MAP_TYPE_INDIRECT_OCCLUSION + color.xyz += lightingResult.lightmapColor * lightingResult.diffuseColorWithLighting; + #endif + + color.xyz += + ( lightingResult.environmentDiffuse * lightingResult.diffuseColorWithLighting + + lightingResult.environmentSpecular * lightingResult.specularColorWithLighting * lightingResult.environmentGF * fresnel + #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR + + lightingResult.environment2ndSpecular * lightingResult.specularColorWithLighting2ndSpecular * surfaceData.color2ndSpecular * lightingResult.environmentGF2ndSpecular + #endif + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + + lightingResult.environmentDiffuseSubLayers + + lightingResult.environmentSpecularSubLayers + #endif + ) + * lightingResult.ao + #if CC_SURFACES_LIGHTING_TRANSMIT_DIFFUSE + + lightingResult.environmentTransmitDiffuse + #endif + ; + #endif + return color; +#endif return color; } diff --git a/editor/assets/chunks/surfaces/module-functions/toon-fs.chunk b/editor/assets/chunks/surfaces/module-functions/toon-fs.chunk index 5afc2f508e4..f2019bc22e3 100644 --- a/editor/assets/chunks/surfaces/module-functions/toon-fs.chunk +++ b/editor/assets/chunks/surfaces/module-functions/toon-fs.chunk @@ -50,12 +50,12 @@ vec3 CCSurfacesGetSpecularColor(in SurfacesMaterialData surfaceData) { return surfaceData.specular.xyz; } -void CCSurfacesLightingInitializeColorWithLighting(inout vec3 diffuseColorWithLighting, inout vec3 specularColorWithLighting, inout vec3 specularColorWithEnvLighting, in SurfacesMaterialData surfaceData, in LightingIntermediateData lightingData) +void CCSurfacesLightingInitializeColorWithLighting(inout vec3 diffuseColorWithLighting, inout vec3 specularColorWithLighting, in SurfacesMaterialData surfaceData, in LightingIntermediateData lightingData) { diffuseColorWithLighting = vec3(0.0); - specularColorWithEnvLighting = specularColorWithLighting = surfaceData.specular.xyz * surfaceData.baseStep; + specularColorWithLighting = surfaceData.specular.xyz * surfaceData.baseStep; } -void CCSurfacesLightingCalculateColorWithLighting(inout vec3 diffuseColorWithLighting, inout vec3 specularColorWithLighting, inout vec3 specularColorWithEnvLighting, in SurfacesMaterialData surfaceData, in LightingIntermediateData lightingData) +void CCSurfacesLightingCalculateColorWithLighting(inout vec3 diffuseColorWithLighting, inout vec3 specularColorWithLighting, in SurfacesMaterialData surfaceData, in LightingIntermediateData lightingData) { float NL = 0.5 * lightingData.NoL + 0.5; @@ -68,7 +68,6 @@ void CCSurfacesLightingCalculateColorWithLighting(inout vec3 diffuseColorWithLig diffuseColorWithLighting = diffuse * surfaceData.baseStep; } - // Copy material data to lighting data // such as tangent data for anisotropic materials void CCSurfacesInitializeLightingIntermediateData(inout LightingIntermediateData lightingData, in SurfacesMaterialData surfaceData) @@ -140,8 +139,8 @@ vec4 CCSurfacesShading(in SurfacesMaterialData surfaceData, in LightingResult li LightingIntermediateData lightingData; lightingData.NoL = -1.0; vec3 backLightingDiffuse, backLightingSpecular; - CCSurfacesLightingInitializeColorWithLighting(backLightingDiffuse, backLightingSpecular, backLightingSpecular, surfaceData, lightingData); - CCSurfacesLightingCalculateColorWithLighting(backLightingDiffuse, backLightingSpecular, backLightingSpecular, surfaceData, lightingData); + CCSurfacesLightingInitializeColorWithLighting(backLightingDiffuse, backLightingSpecular, surfaceData, lightingData); + CCSurfacesLightingCalculateColorWithLighting(backLightingDiffuse, backLightingSpecular, surfaceData, lightingData); color.xyz += mix(lightingResult.directDiffuse, lightingResult.lightmapColor, lightmapCoef) * mix(backLightingDiffuse, lightingResult.diffuseColorWithLighting, lightingResult.shadow) diff --git a/editor/assets/chunks/surfaces/module-functions/unlit-fs.chunk b/editor/assets/chunks/surfaces/module-functions/unlit-fs.chunk index edc14d1b03f..ed8a5ce578c 100644 --- a/editor/assets/chunks/surfaces/module-functions/unlit-fs.chunk +++ b/editor/assets/chunks/surfaces/module-functions/unlit-fs.chunk @@ -27,13 +27,13 @@ vec3 CCSurfacesGetSpecularColor(in SurfacesMaterialData surfaceData) } // Diffuse/Specular Color with BRDF lighting preparation -void CCSurfacesLightingInitializeColorWithLighting(inout vec3 diffuseColorWithLighting, inout vec3 specularColorWithLighting, inout vec3 specularColorWithEnvLighting, in SurfacesMaterialData surfaceData, in LightingIntermediateData lightingData) +void CCSurfacesLightingInitializeColorWithLighting(inout vec3 diffuseColorWithLighting, inout vec3 specularColorWithLighting, in SurfacesMaterialData surfaceData, in LightingIntermediateData lightingData) { diffuseColorWithLighting = CCSurfacesGetDiffuseColor(surfaceData); - specularColorWithEnvLighting = specularColorWithLighting = CCSurfacesGetSpecularColor(surfaceData).xyz; + specularColorWithLighting = CCSurfacesGetSpecularColor(surfaceData).xyz; } // Update two colors with BRDF which depend on lights (optional) -void CCSurfacesLightingCalculateColorWithLighting(inout vec3 diffuseColorWithLighting, inout vec3 specularColorWithLighting, inout vec3 specularColorWithEnvLighting, in SurfacesMaterialData surfaceData, in LightingIntermediateData lightingData) +void CCSurfacesLightingCalculateColorWithLighting(inout vec3 diffuseColorWithLighting, inout vec3 specularColorWithLighting, in SurfacesMaterialData surfaceData, in LightingIntermediateData lightingData) { } diff --git a/editor/assets/effects/advanced/car-paint.effect b/editor/assets/effects/advanced/car-paint.effect new file mode 100644 index 00000000000..6aa62b53de2 --- /dev/null +++ b/editor/assets/effects/advanced/car-paint.effect @@ -0,0 +1,422 @@ +// Copyright (c) 2017-2022 Xiamen Yaji Software Co., Ltd. + +CCEffect %{ + techniques: + - name: opaque + passes: + - vert: standard-vs + frag: standard-fs + properties: &props + tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] } + mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, linear: true, editor: { displayName: Albedo, type: color } } + albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } + alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST, slide: true, range: [0, 1.0], step: 0.001 } } + occlusion: { value: 0.0, target: pbrParams.x, editor: { slide: true, range: [0, 1.0], step: 0.001 } } + roughness: { value: 0.5, target: pbrParams.y, editor: { slide: true, range: [0, 1.0], step: 0.001 } } + metallic: { value: 0.0, target: pbrParams.z, editor: { slide: true, range: [0, 1.0], step: 0.001 } } + specularIntensity: { value: 0.5, target: pbrParams.w, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } } + normalStrength: { value: 1.0, target: emissiveScaleParam.w, editor: { parent: USE_NORMAL_MAP, slide: true, range: [0, 5.0], step: 0.001 } } + anisotropyIntensity: { value: 1.0, target: anisotropyParam.x, editor: { parent: IS_ANISOTROPY, slide : true, range : [0.0, 1.0] , step : 0.0001 } } + anisotropyRotation: { value: 0.0, target: anisotropyParam.y, editor: { parent: IS_ANISOTROPY, slide : true, range : [0, 1.0] , step : 0.0001 } } + anisotropyMapResolutionHeight: { value: 0.0, target: anisotropyParam.w, editor: { parent: FIX_ANISOTROPIC_ROTATION_MAP } } + addOnShadowBias: { value: 0.0, target: anisotropyParam.z } + coatRoughness: { value: 0.05, target: coatParam.x, editor: { slide : true, range : [0, 1.0] , step : 0.0001 } } + coatIOR: { value: 1.0, target: coatParam.y, editor: { slide : true, range : [1.0, 1.5] , step : 0.0001 } } + coatOpacity: { value: 1.0, target: coatParam.z, editor: { slide : true, range : [0.0, 1.0] , step : 0.0001 } } + coatIntensity: { value: 1.0, target: coatParam.w, editor: { slide : true, range : [0.0, 3.0] , step : 0.0001 } } + coatColor: { value: [1.0, 1.0, 1.0, 1.0], linear: true, editor: { displayName: CoatColor, type: color } } + flakeDensity: { value: 0.25, target: flakeParam.x, editor: { parent: USE_FLAKE, slide: true, range: [0, 1.0], step: 0.001 } } + flakeDensityMin: { value: 0.25, target: flakeParam.y, editor: { parent: USE_FLAKE, slide: true, range: [0, 1.0], step: 0.001 } } + flakeTiling: { value: 8.0, target: flakeParam.z, editor: { parent: USE_FLAKE, slide: true, range: [1.0, 20.0], step: 0.001 } } + coatNormalStrength: { value: 1.0, target: flakeParam.w, editor: { parent: USE_FLAKE, slide: true, range: [0, 5.0], step: 0.001 } } + mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } + normalMap: { value: normal } + coatNormalMap: { value: normal, editor: { parent: USE_FLAKE } } + flakeDataMap: { value: white, editor: { parent: USE_FLAKE } } + pbrMap: { value: grey } + occlusionMap: { value: white } + anisotropyMap: { value: black, editor : { parent: IS_ANISOTROPY } } + anisotropyMapNearestFilter: { value: black, editor : { parent: FIX_ANISOTROPIC_ROTATION_MAP } } + - &forward-add + vert: standard-vs + frag: standard-fs + phase: forward-add + propertyIndex: 0 + embeddedMacros: { CC_FORWARD_ADD: true } + depthStencilState: + depthFunc: equal + depthTest: true + depthWrite: false + blendState: + targets: + - blend: true + blendSrc: one + blendDst: one + blendSrcAlpha: zero + blendDstAlpha: one + - &shadow-caster + vert: shadow-caster-vs + frag: shadow-caster-fs + phase: shadow-caster + propertyIndex: 0 + rasterizerState: + cullMode: front + properties: + tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] } + mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, editor: { displayName: Albedo, type: color } } + albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } + alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } } + mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } + - &reflect-map + vert: standard-vs + frag: reflect-map-fs + phase: reflect-map + propertyIndex: 0 + - &planar-shadow + vert: planar-shadow-vs + frag: planar-shadow-fs + phase: planar-shadow + propertyIndex: 0 + depthStencilState: + depthTest: true + depthWrite: false + stencilTestFront: true + stencilFuncFront: not_equal + stencilPassOpFront: replace + stencilRef: 0x80 # only use the leftmost bit + stencilReadMask: 0x80 + stencilWriteMask: 0x80 + blendState: + targets: + - blend: true + blendSrc: src_alpha + blendDst: one_minus_src_alpha + blendDstAlpha: one_minus_src_alpha + - &deferred + vert: standard-vs + frag: standard-fs + pass: gbuffer + phase: gbuffer + embeddedMacros: { CC_PIPELINE_TYPE: 1 } + propertyIndex: 0 + - name: transparent + passes: + - vert: standard-vs + frag: standard-fs + embeddedMacros: { CC_FORCE_FORWARD_SHADING: true } + depthStencilState: &d1 + depthTest: true + depthWrite: false + blendState: &b1 + targets: + - blend: true + blendSrc: src_alpha + blendDst: one_minus_src_alpha + blendDstAlpha: one_minus_src_alpha + properties: *props + - *forward-add + - *shadow-caster + - *planar-shadow + - &deferred-forward + vert: standard-vs + frag: standard-fs + phase: deferred-forward + embeddedMacros: { CC_PIPELINE_TYPE: 0 } + propertyIndex: 0 + depthStencilState: *d1 + blendState: *b1 +}% + +CCProgram shared-ubos %{ + uniform Constants { + vec4 tilingOffset; + vec4 albedo; + vec4 albedoScaleAndCutoff; + vec4 pbrParams; + vec4 emissiveScaleParam; + vec4 anisotropyParam; + vec4 coatParam; + vec4 coatColor; + vec4 flakeParam; + }; +}% + +CCProgram macro-remapping %{ + // ui displayed macros + #pragma define-meta HAS_SECOND_UV + #pragma define-meta USE_TWOSIDE + #pragma define-meta IS_ANISOTROPY + #pragma define-meta USE_VERTEX_COLOR + #pragma define-meta FIX_ANISOTROPIC_ROTATION_MAP + #pragma define-meta USE_FLAKE + + #define CC_SURFACES_USE_SECOND_UV HAS_SECOND_UV + #define CC_SURFACES_USE_TWO_SIDED USE_TWOSIDE + #define CC_SURFACES_LIGHTING_ANISOTROPIC IS_ANISOTROPY + #define CC_SURFACES_USE_VERTEX_COLOR USE_VERTEX_COLOR + #define CC_SURFACES_LIGHTING_CLEAR_COAT 1 + + // depend on UI macros +#if IS_ANISOTROPY || USE_NORMAL_MAP || USE_FLAKE + #define CC_SURFACES_USE_TANGENT_SPACE 1 +#endif + + // functionality for each effect + #define CC_SURFACES_LIGHTING_ANISOTROPIC_ENVCONVOLUTION_COUNT 31 +}% + +CCProgram surface-vertex %{ + #define CC_SURFACES_VERTEX_MODIFY_SHADOW_BIAS + vec2 SurfacesVertexModifyShadowBias(in SurfacesStandardVertexIntermediate In, vec2 originShadowBias) + { + return originShadowBias + vec2(anisotropyParam.z, 0.0); + } + + #define CC_SURFACES_VERTEX_MODIFY_UV + void SurfacesVertexModifyUV(inout SurfacesStandardVertexIntermediate In) + { + In.texCoord = In.texCoord * tilingOffset.xy + tilingOffset.zw; + #if CC_SURFACES_USE_SECOND_UV + In.texCoord1 = In.texCoord1 * tilingOffset.xy + tilingOffset.zw; + #endif + } +}% + +CCProgram surface-fragment %{ + #if USE_ALBEDO_MAP + uniform sampler2D albedoMap; + #pragma define-meta ALBEDO_UV options([v_uv, v_uv1]) + #endif + #if USE_NORMAL_MAP + uniform sampler2D normalMap; + #pragma define-meta NORMAL_UV options([v_uv, v_uv1]) + #endif + #pragma define-meta DEFAULT_UV options([v_uv, v_uv1]) + #if USE_PBR_MAP + uniform sampler2D pbrMap; + #endif + #if USE_OCCLUSION_MAP + uniform sampler2D occlusionMap; + #endif + #if IS_ANISOTROPY && USE_ANISOTROPY_MAP + uniform sampler2D anisotropyMap; + uniform sampler2D anisotropyMapNearestFilter; + #endif + #if USE_FLAKE + uniform sampler2D coatNormalMap; + uniform sampler2D flakeDataMap; + #endif + + #pragma define OCCLUSION_CHANNEL r + #pragma define ROUGHNESS_CHANNEL g + #pragma define METALLIC_CHANNEL b + #pragma define SPECULAR_INTENSITY_CHANNEL a + + #if USE_ALPHA_TEST + #pragma define-meta ALPHA_TEST_CHANNEL options([a, r]) + #endif + + #define CC_SURFACES_FRAGMENT_MODIFY_ANISOTROPY_PARAMS + vec4 SurfacesFragmentModifyAnisotropyParams(out float isRotation) + { + float anisotropyRotation = anisotropyParam.y * PI; + float anisotropyShape = anisotropyParam.x; + #if IS_ANISOTROPY && USE_ANISOTROPY_MAP + // Rotation angle should disable trilinear filtering + vec4 tex = texture(anisotropyMap, DEFAULT_UV); + anisotropyRotation = fract(anisotropyRotation * 0.5 + tex.y) * PI2; + // less value is better for SP exported shape + anisotropyShape *= tex.x; + #endif + + // fix rotation map seam line of black and white + #if FIX_ANISOTROPIC_ROTATION_MAP + #if IS_ANISOTROPY && USE_ANISOTROPY_MAP + vec4 reference = texture(anisotropyMapNearestFilter, DEFAULT_UV); + vec2 oneTap = vec2(0.0, 1.0 / anisotropyParam.w); + float threshold = 0.2; + // scan more taps for stable result + vec4 sample1 = texture(anisotropyMapNearestFilter, DEFAULT_UV + oneTap); + vec4 sample2 = texture(anisotropyMapNearestFilter, DEFAULT_UV - oneTap); + if (abs(sample1.y - reference.y) > threshold || abs(sample2.y - reference.y) > threshold) { + tex.y = reference.y; + } + anisotropyRotation = fract(anisotropyParam.y * PI * 0.5 + tex.y) * PI2; + #endif + #endif + + isRotation = 1.0; + return vec4(anisotropyShape, anisotropyRotation, 0.0, 0.0); + } + + #define CC_SURFACES_FRAGMENT_MODIFY_CLEAR_COAT_PARAMS + vec4 SurfacesFragmentModifyClearCoatParams() + { + return vec4(coatParam.x, coatParam.y - 0.99, coatParam.z, coatParam.w * 2.0); + } + + #define CC_SURFACES_FRAGMENT_MODIFY_CLEAR_COAT_COLOR + vec3 SurfacesFragmentModifyClearCoatColor() + { + return coatColor.xyz; + } + + #define CC_SURFACES_FRAGMENT_MODIFY_CLEAR_COAT_WORLD_NORMAL + vec3 SurfacesFragmentModifyClearCoatWorldNormal() + { + vec3 normal = FSInput_worldNormal; + #if USE_FLAKE + float density = flakeParam.x, densityMin = flakeParam.y, flakeTiling = flakeParam.z; + vec2 uv = FSInput_texcoord * flakeTiling; + // vec4 data = fragTextureLod(flakeDataMap, uv, 0.0); + // vec3 nmmp = (fragTextureLod(coatNormalMap, uv, 0.0).xyz - vec3(0.5)) * 2.0; + vec4 data = texture(flakeDataMap, uv); + vec3 nmmp = (texture(coatNormalMap, uv).xyz - vec3(0.5)) * 2.0; + float C = (data.a - 1.0) / density + 1.0; + float bumpIntensity = saturate(max(densityMin, C)); + nmmp = lerp(vec3(0.0, 0.0, 1.0), nmmp, bumpIntensity); + normal = CalculateNormalFromTangentSpace(nmmp, flakeParam.w, normalize(normal.xyz), normalize(FSInput_worldTangent), FSInput_mirrorNormal); + #endif + + return normalize(normal); + } + + #include + + + #include + #define CC_SURFACES_FRAGMENT_MODIFY_SHARED_DATA + void SurfacesFragmentModifySharedData(inout SurfacesMaterialData surfaceData) + { + #if USE_FLAKE + float density = flakeParam.x, densityMin = flakeParam.y, flakeTiling = flakeParam.z; + vec2 uv = FSInput_texcoord * flakeTiling; + + vec4 data = fragTextureLod(flakeDataMap, uv, 0.0); + float C = (data.a - 1.0) / density + 1.0; + + // normal + // vec3 nmmp = fragTextureLod(coatNormalMap, uv, 0.0).xyz - vec3(0.5); + // float bumpIntensity = saturate(max(densityMin, C)); + // nmmp = lerp(vec3(0.0, 0.0, 1.0), nmmp, bumpIntensity); + // surfaceData.normal = CalculateNormalFromTangentSpace(nmmp, flakeParam.w, normalize(normal.xyz), normalize(FSInput_worldTangent), FSInput_mirrorNormal); + + // color + vec4 flakeA = vec4(0.972,0.96,0.915,1); + vec4 flakeB = vec4(1.0,0.78,0.344,1); + vec4 flakeColor = lerp(flakeA, flakeB, data.r); + float flakeTintWeight = 0.1; + + vec4 color = surfaceData.baseColor; + color = lerp(color, flakeColor, saturate(C) * flakeTintWeight); + color = lerp(surfaceData.baseColor, color, surfaceData.metallic); + surfaceData.baseColor = color; + #endif + } +}% + +CCProgram standard-vs %{ + precision highp float; + + // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros + #include + #include + + // 2. common include with corresponding shader stage, include before surface functions + #include + + // 3. user surface functions that can use user (effect) parameters (ubo Constants) + // see surfaces/default-functions/xxx.chunk + #include + #include + + // 4. surface include with corresponding shader stage and shading-model (optional) + #include + + // 5. shader entry with corresponding shader stage and technique usage/type + #include +}% + + +CCProgram shadow-caster-vs %{ + precision highp float; + #include + #include + #include + #include + #include + #include +}% + +CCProgram planar-shadow-vs %{ + precision highp float; + #include + #include + #include + #include + #include + #include +}% + + +CCProgram standard-fs %{ + // shading-model : standard + // lighting-model : standard (isotropy / anisotropy pbr) + // shader stage : fs + // technique usage/type : render-to-scene + + precision highp float; + // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros + #include + #include + + // 2. common include with corresponding shader stage, include before surface functions + #include + + // 3. user surface functions that can use user (effect) parameters (ubo Constants) + // see surfaces/default-functions/xxx.chunk + #include + #include + + // 4. lighting-model (optional) + #include + + // 5. surface include with corresponding shader stage and shading-model (optional) + #include + + // 6. shader entry with corresponding shader stage and technique usage/type + #include +}% + +CCProgram shadow-caster-fs %{ + precision highp float; + #include + #include + #include + #include + #include + #include +}% + +CCProgram planar-shadow-fs %{ + precision highp float; + #include + #include + #include + #include + #include + #include +}% + +CCProgram reflect-map-fs %{ + precision highp float; + #include + #include + #include + #include + #include + #include + #include + #include +}% + diff --git a/editor/assets/effects/advanced/car-paint.effect.meta b/editor/assets/effects/advanced/car-paint.effect.meta new file mode 100644 index 00000000000..ab9bcd058f7 --- /dev/null +++ b/editor/assets/effects/advanced/car-paint.effect.meta @@ -0,0 +1,11 @@ +{ + "ver": "1.7.0", + "importer": "effect", + "imported": true, + "uuid": "304a12db-3955-46e4-b712-e5e26f45258b", + "files": [ + ".json" + ], + "subMetas": {}, + "userData": {} +} diff --git a/editor/assets/effects/util/dcc/vat/houdini-fluid-v3-liquid.effect b/editor/assets/effects/util/dcc/vat/houdini-fluid-v3-liquid.effect index 25d9e5c09f8..2a4d39a1e9a 100644 --- a/editor/assets/effects/util/dcc/vat/houdini-fluid-v3-liquid.effect +++ b/editor/assets/effects/util/dcc/vat/houdini-fluid-v3-liquid.effect @@ -301,7 +301,7 @@ CCProgram surface-fragment %{ return normalize(CalculateNormalFromTangentSpace(nmmp, normalIntensity, N, T, FSInput_mirrorNormal)); } - float GetFresnel(float fresnel) + vec3 GetFresnel(vec3 fresnel) { return saturate(fresnel * 1.5); } diff --git a/editor/assets/effects/util/dcc/vat/zeno-fluid-liquid.effect b/editor/assets/effects/util/dcc/vat/zeno-fluid-liquid.effect index fb1af249d9d..e553d178eda 100644 --- a/editor/assets/effects/util/dcc/vat/zeno-fluid-liquid.effect +++ b/editor/assets/effects/util/dcc/vat/zeno-fluid-liquid.effect @@ -226,7 +226,7 @@ CCProgram surface-fragment %{ return normalize(CalculateNormalFromTangentSpace(nmmp, normalIntensity, N, T, FSInput_mirrorNormal)); } - float GetFresnel(float fresnel) + vec3 GetFresnel(vec3 fresnel) { return saturate(fresnel * 1.5); } From 6282c75f66ffa98e7192ad257d7e71b5da2c6b9b Mon Sep 17 00:00:00 2001 From: jk20012001 Date: Wed, 19 Jul 2023 18:38:27 +0800 Subject: [PATCH 2/9] add glass --- .../assets/chunks/common/lighting/brdf.chunk | 1 + .../assets/chunks/common/lighting/bxdf.chunk | 34 +- .../data-structures/lighting-result.chunk | 2 +- .../lighting-flow/common-flow.chunk | 46 +-- .../model-functions/standard.chunk | 105 ++++-- .../module-functions/standard-fs.chunk | 81 +--- .../assets/effects/advanced/car-paint.effect | 33 +- editor/assets/effects/advanced/glass.effect | 352 ++++++++++++++++++ .../assets/effects/advanced/glass.effect.meta | 11 + 9 files changed, 487 insertions(+), 178 deletions(-) create mode 100644 editor/assets/effects/advanced/glass.effect create mode 100644 editor/assets/effects/advanced/glass.effect.meta diff --git a/editor/assets/chunks/common/lighting/brdf.chunk b/editor/assets/chunks/common/lighting/brdf.chunk index faca74ce2e4..a8577220805 100644 --- a/editor/assets/chunks/common/lighting/brdf.chunk +++ b/editor/assets/chunks/common/lighting/brdf.chunk @@ -1,5 +1,6 @@ #include +// for dielectric(non-metal) surfaces float F0ToIor(float F0) { return 2.0f / (1.0f - sqrt(F0)) - 1.0f; diff --git a/editor/assets/chunks/common/lighting/bxdf.chunk b/editor/assets/chunks/common/lighting/bxdf.chunk index e08f373a64a..04fb9662a7c 100644 --- a/editor/assets/chunks/common/lighting/bxdf.chunk +++ b/editor/assets/chunks/common/lighting/bxdf.chunk @@ -1,6 +1,6 @@ // saturated N dot V -// for env reflection with non-metal and smooth surface +// for env reflection with dielectric(non-metal) and smooth surface float CalculateFresnelCoefficient(float ior, float NoVSat) { float g, c, n, prev, next; @@ -22,3 +22,35 @@ vec3 CalculateScattering(vec3 unscatteredColor, float distance, float outScatter vec3 outScattered = unscatteredColor * extinction.x * outScatterColor; return outScattered + inScattered; } + + +// LastLayerF for temporary use +// S is spec without GF +void InitializeLayerBlending(out vec3 blendedBaseLayerD, out vec3 blendedBaseLayerS, + out vec3 blendedSubLayerD, out vec3 blendedSubLayerS, + out vec3 lastLayerF, + in vec3 baseD, in vec3 baseS + ) +{ + blendedBaseLayerD = baseD; + blendedBaseLayerS = baseS; + blendedSubLayerD = blendedSubLayerS = vec3(0.0); + lastLayerF = vec3(1.0); +} + +// use subLayerF instead of subLayerGF for ior-dependent 0-1 range +void CalculateLayerBlending(inout vec3 blendedBaseLayerD, inout vec3 blendedBaseLayerS, + inout vec3 blendedSubLayerD, inout vec3 blendedSubLayerS, + inout vec3 lastLayerF, + in vec3 subLayerD, in vec3 subLayerDiffuseColor, + in vec3 subLayerS, in vec3 subLayerSpecularColor, + in float subLayerOpacity, inout vec3 subLayerF + ) +{ + subLayerF = saturate(subLayerF * subLayerOpacity); + blendedSubLayerD = blendedSubLayerD * (vec3(1.0) - lastLayerF) + subLayerD * subLayerDiffuseColor * subLayerF; + blendedSubLayerS = blendedSubLayerS *(vec3(1.0) - lastLayerF) + subLayerS * subLayerSpecularColor * subLayerF; + blendedBaseLayerD *= vec3(1.0) - subLayerF; + blendedBaseLayerS *= vec3(1.0) - subLayerF; + lastLayerF = subLayerF; +} \ No newline at end of file diff --git a/editor/assets/chunks/lighting-models/data-structures/lighting-result.chunk b/editor/assets/chunks/lighting-models/data-structures/lighting-result.chunk index 913a6e7d009..9105b8878a9 100644 --- a/editor/assets/chunks/lighting-models/data-structures/lighting-result.chunk +++ b/editor/assets/chunks/lighting-models/data-structures/lighting-result.chunk @@ -39,7 +39,7 @@ struct LightingResult /* for multi-layer materials, should define: -struct LightingResultLayer +struct LightingResultPerLayer { vec3 diffuseColorWithLighting, specularColorWithLighting; vec3 directDiffuse, directSpecular, directGF; 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 d857c4c88de..d21d83c428d 100644 --- a/editor/assets/chunks/lighting-models/lighting-flow/common-flow.chunk +++ b/editor/assets/chunks/lighting-models/lighting-flow/common-flow.chunk @@ -118,6 +118,9 @@ CCSurfacesLightingCalculateDirectTT(lightingResult, lightingData, attenuatedLightColorAndIntensity); #endif + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + CCSurfacesLightingCalculateDirectMultiLayerBlending(lightingResult, lightingData2ndSpecular); + #endif // user-defined lighting model #ifdef CC_SURFACES_LIGHTING_MODIFY_FINAL_RESULT @@ -254,50 +257,17 @@ CCSurfacesLightingCalculateDirect2ndSpecular(lightingResult.direct2ndSpecular, lightingData2ndSpecular, cc_mainLitColor, surfaceData2ndSpecular.intensity2ndSpecular, lightingResult.directSpecular); CCSurfaceLightingCalculateDirectFresnel(lightingResult.directGF2ndSpecular, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, lightingResult.environmentGF2ndSpecular); - - #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND - vec3 lastLayerF, zeroVector = vec3(0.0); - vec3 blendedBaseD = lightingResult.directDiffuse, blendedBaseS = lightingResult.directSpecular; - CCSurfaceLightingCalculateDirectSubLayerFresnel(lightingResult.directSubLayerF, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular); - InitializeLayerBlending(blendedBaseD, blendedBaseS, - lightingResult.directDiffuseSubLayers, lightingResult.directSpecularSubLayers, - lastLayerF, - lightingResult.directDiffuse, lightingResult.directSpecular - ); - CalculateLayerBlending (blendedBaseD, blendedBaseS, - lightingResult.directDiffuseSubLayers, lightingResult.directSpecularSubLayers, - lastLayerF, - zeroVector, zeroVector, - lightingResult.direct2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, - lightingData2ndSpecular.layerOpacity, lightingResult.directSubLayerF - ); - lightingResult.directDiffuse = blendedBaseD; - lightingResult.directSpecular = blendedBaseS; - - zeroVector = vec3(0.0); - blendedBaseD = lightingResult.environmentDiffuse; blendedBaseS = lightingResult.environmentSpecular; - CCSurfaceLightingCalculateEnvironmentSubLayerFresnel(lightingResult.environmentSubLayerF, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular); - InitializeLayerBlending(blendedBaseD, blendedBaseS, - lightingResult.environmentDiffuseSubLayers, lightingResult.environmentSpecularSubLayers, - lastLayerF, - lightingResult.environmentDiffuse, lightingResult.environmentSpecular - ); - CalculateLayerBlending (blendedBaseD, blendedBaseS, - lightingResult.environmentDiffuseSubLayers, lightingResult.environmentSpecularSubLayers, - lastLayerF, - zeroVector, zeroVector, - lightingResult.environment2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, - lightingData2ndSpecular.layerOpacity, lightingResult.environmentSubLayerF - ); - lightingResult.environmentDiffuse = blendedBaseD; - lightingResult.environmentSpecular = blendedBaseS; - #endif #endif #if CC_SURFACES_LIGHTING_TT CCSurfacesLightingCalculateDirectTT(lightingResult, lightingData, cc_mainLitColor); #endif + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + CCSurfacesLightingCalculateDirectMultiLayerBlending(lightingResult, lightingData2ndSpecular); + CCSurfacesLightingCalculateEnvironmentMultiLayerBlending(lightingResult, lightingData2ndSpecular); + #endif + // user-defined lighting model #ifdef CC_SURFACES_LIGHTING_MODIFY_FINAL_RESULT LightingMiscData miscData; diff --git a/editor/assets/chunks/lighting-models/model-functions/standard.chunk b/editor/assets/chunks/lighting-models/model-functions/standard.chunk index d0bbef405e9..ed23f849f8d 100644 --- a/editor/assets/chunks/lighting-models/model-functions/standard.chunk +++ b/editor/assets/chunks/lighting-models/model-functions/standard.chunk @@ -249,46 +249,69 @@ void CCSurfaceLightingCalculateEnvironmentSubLayerFresnel(out vec3 subLayerF, in } #endif +#if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + // todo: layer result in LightingResult separate to single structure like LightingResultPerLayer + void CCSurfacesLightingCalculateDirectMultiLayerBlending(inout LightingResult lightingResult + , in LightingIntermediateData lightingData2ndLayer + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND > 1 + // add more layers... + #endif + ) + { + vec3 lastLayerF, zeroVector = vec3(0.0); + vec3 blendedBaseD = lightingResult.directDiffuse, blendedBaseS = lightingResult.directSpecular; + InitializeLayerBlending(blendedBaseD, blendedBaseS, + lightingResult.directDiffuseSubLayers, lightingResult.directSpecularSubLayers, + lastLayerF, + lightingResult.directDiffuse, lightingResult.directSpecular + ); -// LastLayerF for temporary use -// S is spec without GF -void InitializeLayerBlending(out vec3 blendedBaseLayerD, out vec3 blendedBaseLayerS, - out vec3 blendedSubLayerD, out vec3 blendedSubLayerS, - out vec3 lastLayerF, - in vec3 baseD, in vec3 baseS - ) -{ - blendedBaseLayerD = baseD; - blendedBaseLayerS = baseS; - blendedSubLayerD = blendedSubLayerS = vec3(0.0); - lastLayerF = vec3(1.0); -} + CCSurfaceLightingCalculateDirectSubLayerFresnel(lightingResult.directSubLayerF, lightingData2ndLayer, lightingResult.specularColorWithLighting2ndSpecular); + CalculateLayerBlending (blendedBaseD, blendedBaseS, + lightingResult.directDiffuseSubLayers, lightingResult.directSpecularSubLayers, + lastLayerF, + zeroVector, zeroVector, + lightingResult.direct2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, + lightingData2ndLayer.layerOpacity, lightingResult.directSubLayerF + ); + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND > 1 + // add more layers... + #endif -// use subLayerF instead of subLayerGF for ior-dependent 0-1 range -void CalculateLayerBlending(inout vec3 blendedBaseLayerD, inout vec3 blendedBaseLayerS, - inout vec3 blendedSubLayerD, inout vec3 blendedSubLayerS, - inout vec3 lastLayerF, - in vec3 subLayerD, in vec3 subLayerDiffuseColor, - in vec3 subLayerS, in vec3 subLayerSpecularColor, - in float subLayerOpacity, inout vec3 subLayerF - ) -{ - subLayerF = saturate(subLayerF * subLayerOpacity); - blendedSubLayerD = blendedSubLayerD * (vec3(1.0) - lastLayerF) + subLayerD * subLayerDiffuseColor * subLayerF; - blendedSubLayerS = blendedSubLayerS *(vec3(1.0) - lastLayerF) + subLayerS * subLayerSpecularColor * subLayerF; - blendedBaseLayerD *= vec3(1.0) - subLayerF; - blendedBaseLayerS *= vec3(1.0) - subLayerF; - lastLayerF = subLayerF; -} -/*void CCSurfacesLightingInitializeLayerBlending(inout LightingResult lightingResult, out vec3 lastLayerF) -{ - InitializeLayerBlending(lightingResult.directDiffuseSubLayers, lightingResult.directDiffuseSubLayers, - lightingResult.directDiffuseSubLayers, lightingResult.directDiffuseSubLayers); -} -void CCSurfacesLightingCalculateLayerBlending(inout LightingResult lightingResult, inout vec3 lastLayerF, - in vec3 subLayerD, in vec3 subLayerS, inout vec3 subLayerF - ) -{ - // CalculateLayerBlending(lightingResult.); -} -*/ \ No newline at end of file + // output + lightingResult.directDiffuse = blendedBaseD; + lightingResult.directSpecular = blendedBaseS; + } + + void CCSurfacesLightingCalculateEnvironmentMultiLayerBlending(inout LightingResult lightingResult + , in LightingIntermediateData lightingData2ndLayer + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND > 1 + // add more layers... + #endif + ) + { + vec3 lastLayerF, zeroVector = vec3(0.0); + vec3 blendedBaseD = lightingResult.environmentDiffuse, blendedBaseS = lightingResult.environmentSpecular; + InitializeLayerBlending(blendedBaseD, blendedBaseS, + lightingResult.environmentDiffuseSubLayers, lightingResult.environmentSpecularSubLayers, + lastLayerF, + lightingResult.environmentDiffuse, lightingResult.environmentSpecular + ); + + CCSurfaceLightingCalculateEnvironmentSubLayerFresnel(lightingResult.environmentSubLayerF, lightingData2ndLayer, lightingResult.specularColorWithLighting2ndSpecular); + CalculateLayerBlending (blendedBaseD, blendedBaseS, + lightingResult.environmentDiffuseSubLayers, lightingResult.environmentSpecularSubLayers, + lastLayerF, + zeroVector, zeroVector, + lightingResult.environment2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, + lightingData2ndLayer.layerOpacity, lightingResult.environmentSubLayerF + ); + #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND > 1 + // add more layers... + #endif + + // output + lightingResult.environmentDiffuse = blendedBaseD; + lightingResult.environmentSpecular = blendedBaseS; + } +#endif diff --git a/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk b/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk index f292782b3cc..7bd93a3cda9 100644 --- a/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk +++ b/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk @@ -390,87 +390,8 @@ vec4 CCSurfacesShading(in SurfacesMaterialData surfaceData, in LightingResult li ; color.xyz += lightingResult.emissive; - return color; - - //todo: - //ior and fresnel range? - //整理代码blend layers - - //test - LightingIntermediateData lightingData; - CCSurfacesInitializeLightingIntermediateData(lightingData, surfaceData); - CCSurfacesLightingCalculateIntermediateData_PerLight(lightingData, surfaceData, -cc_mainLitDir.xyz); - - #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND - #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR - color.xyz = lightingResult.environmentSpecularSubLayers; - // color.xyz = lightingResult.environment2ndSpecular * lightingResult.specularColorWithLighting2ndSpecular * surfaceData.color2ndSpecular * lightingResult.environmentGF2ndSpecular; - color.xyz = lightingResult.environmentSpecular * lightingResult.specularColorWithLighting * lightingResult.environmentGF; - // color.xyz = vec3(lightingResult.environmentSubLayerF); - return color; - #endif - - float VoH = lightingData.VoH; - float Fc = exp2( (-5.55473 * VoH - 6.98316) * VoH ); - - float LoH = dot(lightingData.L, lightingData.H); - color.xyz = F_Schlick(lightingResult.specularColorWithLighting, lightingData.VoH); - color.xyz = F_Schlick(lightingResult.specularColorWithLighting, LoH); - color.xyz = lightingResult.environmentSpecularSubLayers; - color.xyz = vec3(1.0 - Fc); - return color; - color.xyz = vec3(lightingResult.environmentGF2ndSpecular * lightingResult.environment2ndSpecular * lightingResult.specularColorWithLighting2ndSpecular); - color.xyz = vec3(lightingResult.directSpecular * lightingResult.specularColorWithLighting * lightingResult.directGF * fresnel); - color.xyz = vec3(lightingResult.environmentSpecular * lightingResult.specularColorWithLighting * lightingResult.environmentGF); - color.xyz = vec3(0.0); - color.xyz += - ( lightingResult.directDiffuse * lightingResult.diffuseColorWithLighting - + lightingResult.directSpecular * lightingResult.specularColorWithLighting * lightingResult.directGF * fresnel - #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR - + lightingResult.directTransmitSpecular * lightingResult.specularColorWithLighting * lightingResult.directGF * invFresnel - #endif - #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR - + lightingResult.direct2ndSpecular * lightingResult.specularColorWithLighting2ndSpecular * surfaceData.color2ndSpecular * lightingResult.directGF2ndSpecular - #endif - #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND - + lightingResult.directDiffuseSubLayers - + lightingResult.directSpecularSubLayers - #endif - #if CC_SURFACES_LIGHTING_TT - + lightingResult.directTT * lightingResult.diffuseColorWithLightingTT - #endif - ) - * lightingResult.shadow - #if CC_SURFACES_LIGHTING_TRANSMIT_DIFFUSE - + lightingResult.directTransmitDiffuse - #endif - ; - - #if CC_SURFACES_USE_LIGHT_MAP == LIGHT_MAP_TYPE_ALL_IN_ONE - color.xyz += lightingResult.lightmapColor * lightingResult.diffuseColorWithLighting * lightingResult.shadow; //apply real-time shadows - #elif CC_SURFACES_USE_LIGHT_MAP == LIGHT_MAP_TYPE_INDIRECT_OCCLUSION - color.xyz += lightingResult.lightmapColor * lightingResult.diffuseColorWithLighting; - #endif - - color.xyz += - ( lightingResult.environmentDiffuse * lightingResult.diffuseColorWithLighting - + lightingResult.environmentSpecular * lightingResult.specularColorWithLighting * lightingResult.environmentGF * fresnel - #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR - + lightingResult.environment2ndSpecular * lightingResult.specularColorWithLighting2ndSpecular * surfaceData.color2ndSpecular * lightingResult.environmentGF2ndSpecular - #endif - #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND - + lightingResult.environmentDiffuseSubLayers - + lightingResult.environmentSpecularSubLayers - #endif - ) - * lightingResult.ao - #if CC_SURFACES_LIGHTING_TRANSMIT_DIFFUSE - + lightingResult.environmentTransmitDiffuse - #endif - ; - #endif - return color; #endif + return color; } diff --git a/editor/assets/effects/advanced/car-paint.effect b/editor/assets/effects/advanced/car-paint.effect index 6aa62b53de2..1a1d900767b 100644 --- a/editor/assets/effects/advanced/car-paint.effect +++ b/editor/assets/effects/advanced/car-paint.effect @@ -26,8 +26,8 @@ CCEffect %{ coatIntensity: { value: 1.0, target: coatParam.w, editor: { slide : true, range : [0.0, 3.0] , step : 0.0001 } } coatColor: { value: [1.0, 1.0, 1.0, 1.0], linear: true, editor: { displayName: CoatColor, type: color } } flakeDensity: { value: 0.25, target: flakeParam.x, editor: { parent: USE_FLAKE, slide: true, range: [0, 1.0], step: 0.001 } } - flakeDensityMin: { value: 0.25, target: flakeParam.y, editor: { parent: USE_FLAKE, slide: true, range: [0, 1.0], step: 0.001 } } - flakeTiling: { value: 8.0, target: flakeParam.z, editor: { parent: USE_FLAKE, slide: true, range: [1.0, 20.0], step: 0.001 } } + flakeColorIntensity: { value: 0.5, target: flakeParam.y, editor: { parent: USE_FLAKE, slide: true, range: [0, 1.0], step: 0.001 } } + flakeTiling: { value: 10.0, target: flakeParam.z, editor: { parent: USE_FLAKE, slide: true, range: [1.0, 40.0], step: 0.001 } } coatNormalStrength: { value: 1.0, target: flakeParam.w, editor: { parent: USE_FLAKE, slide: true, range: [0, 5.0], step: 0.001 } } mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } normalMap: { value: normal } @@ -261,17 +261,22 @@ CCProgram surface-fragment %{ return coatColor.xyz; } + vec4 sampleFlake(sampler2D tex, vec2 uv) + { + vec4 blurred = texture(tex, uv); + vec4 detailed = fragTextureLod(tex, uv, 1.0); + return lerp(blurred, detailed, 0.3); + } + #define CC_SURFACES_FRAGMENT_MODIFY_CLEAR_COAT_WORLD_NORMAL vec3 SurfacesFragmentModifyClearCoatWorldNormal() { vec3 normal = FSInput_worldNormal; #if USE_FLAKE - float density = flakeParam.x, densityMin = flakeParam.y, flakeTiling = flakeParam.z; + float density = flakeParam.x, densityMin = 0.25, flakeTiling = flakeParam.z; vec2 uv = FSInput_texcoord * flakeTiling; - // vec4 data = fragTextureLod(flakeDataMap, uv, 0.0); - // vec3 nmmp = (fragTextureLod(coatNormalMap, uv, 0.0).xyz - vec3(0.5)) * 2.0; - vec4 data = texture(flakeDataMap, uv); - vec3 nmmp = (texture(coatNormalMap, uv).xyz - vec3(0.5)) * 2.0; + vec4 data = sampleFlake(flakeDataMap, uv); + vec3 nmmp = (sampleFlake(coatNormalMap, uv).xyz - vec3(0.5)) * 2.0; float C = (data.a - 1.0) / density + 1.0; float bumpIntensity = saturate(max(densityMin, C)); nmmp = lerp(vec3(0.0, 0.0, 1.0), nmmp, bumpIntensity); @@ -289,27 +294,21 @@ CCProgram surface-fragment %{ void SurfacesFragmentModifySharedData(inout SurfacesMaterialData surfaceData) { #if USE_FLAKE - float density = flakeParam.x, densityMin = flakeParam.y, flakeTiling = flakeParam.z; + float density = flakeParam.x, flakeColorIntensity = flakeParam.y, flakeTiling = flakeParam.z; vec2 uv = FSInput_texcoord * flakeTiling; - vec4 data = fragTextureLod(flakeDataMap, uv, 0.0); + vec4 data = sampleFlake(flakeDataMap, uv); float C = (data.a - 1.0) / density + 1.0; - // normal - // vec3 nmmp = fragTextureLod(coatNormalMap, uv, 0.0).xyz - vec3(0.5); - // float bumpIntensity = saturate(max(densityMin, C)); - // nmmp = lerp(vec3(0.0, 0.0, 1.0), nmmp, bumpIntensity); - // surfaceData.normal = CalculateNormalFromTangentSpace(nmmp, flakeParam.w, normalize(normal.xyz), normalize(FSInput_worldTangent), FSInput_mirrorNormal); - // color vec4 flakeA = vec4(0.972,0.96,0.915,1); vec4 flakeB = vec4(1.0,0.78,0.344,1); vec4 flakeColor = lerp(flakeA, flakeB, data.r); - float flakeTintWeight = 0.1; + float flakeTintWeight = 1.0; vec4 color = surfaceData.baseColor; color = lerp(color, flakeColor, saturate(C) * flakeTintWeight); - color = lerp(surfaceData.baseColor, color, surfaceData.metallic); + color = lerp(surfaceData.baseColor, color, surfaceData.metallic * flakeColorIntensity); surfaceData.baseColor = color; #endif } diff --git a/editor/assets/effects/advanced/glass.effect b/editor/assets/effects/advanced/glass.effect new file mode 100644 index 00000000000..e83f9b7f513 --- /dev/null +++ b/editor/assets/effects/advanced/glass.effect @@ -0,0 +1,352 @@ +// Copyright (c) 2017-2022 Xiamen Yaji Software Co., Ltd. + +CCEffect %{ + techniques: + - name: opaque + passes: + - vert: standard-vs + frag: standard-fs + properties: &props + tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] } + mainColor: { value: [1.0, 1.0, 1.0, 0.5], target: albedo, linear: true, editor: { displayName: Albedo, type: color } } + albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } + alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } } + roughness: { value: 0.0, target: pbrParams.y, editor: { slide: true, range: [0, 1.0], step: 0.001 } } + metallic: { value: 0.0, target: pbrParams.z, editor: { slide: true, range: [0, 1.0], step: 0.001 } } + specularIntensity: { value: 0.5, target: pbrParams.w, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } } + F0: { value: 0.3, target: emissiveScaleParam.x, editor: { slide: true, range: [0, 1.0], step: 0.001 } } + F90: { value: 0.8, target: emissiveScaleParam.y, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } } + normalStrength: { value: 1.0, target: emissiveScaleParam.w, editor: { parent: USE_NORMAL_MAP, slide: true, range: [0, 5.0], step: 0.001 } } + mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } + normalMap: { value: normal } + pbrMap: { value: grey } + - &forward-add + vert: standard-vs + frag: standard-fs + phase: forward-add + propertyIndex: 0 + embeddedMacros: { CC_FORWARD_ADD: true } + depthStencilState: + depthFunc: equal + depthTest: true + depthWrite: false + blendState: + targets: + - blend: true + blendSrc: one + blendDst: one + blendSrcAlpha: zero + blendDstAlpha: one + - &shadow-caster + vert: shadow-caster-vs + frag: shadow-caster-fs + phase: shadow-caster + propertyIndex: 0 + rasterizerState: + cullMode: front + properties: + tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] } + mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, editor: { displayName: Albedo, type: color } } + albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } + alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } } + mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } + - &reflect-map + vert: standard-vs + frag: reflect-map-fs + phase: reflect-map + propertyIndex: 0 + - &planar-shadow + vert: planar-shadow-vs + frag: planar-shadow-fs + phase: planar-shadow + propertyIndex: 0 + depthStencilState: + depthTest: true + depthWrite: false + stencilTestFront: true + stencilFuncFront: not_equal + stencilPassOpFront: replace + stencilRef: 0x80 # only use the leftmost bit + stencilReadMask: 0x80 + stencilWriteMask: 0x80 + blendState: + targets: + - blend: true + blendSrc: src_alpha + blendDst: one_minus_src_alpha + blendDstAlpha: one_minus_src_alpha + - &deferred + vert: standard-vs + frag: standard-fs + pass: gbuffer + phase: gbuffer + embeddedMacros: { CC_PIPELINE_TYPE: 1 } + propertyIndex: 0 + - name: transparent + passes: + - vert: standard-vs + frag: standard-fs + embeddedMacros: { CC_FORCE_FORWARD_SHADING: true } + depthStencilState: &d1 + depthTest: true + depthWrite: false + blendState: &b1 + targets: + - blend: true + blendSrc: src_alpha + blendDst: one_minus_src_alpha + blendDstAlpha: one_minus_src_alpha + properties: *props + - *forward-add + - *shadow-caster + - *planar-shadow + - &deferred-forward + vert: standard-vs + frag: standard-fs + phase: deferred-forward + embeddedMacros: { CC_PIPELINE_TYPE: 0 } + propertyIndex: 0 + depthStencilState: *d1 + blendState: *b1 +}% + +CCProgram shared-ubos %{ + uniform Constants { + vec4 tilingOffset; + vec4 albedo; + vec4 albedoScaleAndCutoff; + vec4 pbrParams; + vec4 emissiveScaleParam; + }; +}% + +CCProgram macro-remapping %{ + // ui displayed macros + #pragma define-meta HAS_SECOND_UV + #pragma define-meta USE_TWOSIDE + #pragma define-meta USE_VERTEX_COLOR + + #define CC_SURFACES_USE_SECOND_UV HAS_SECOND_UV + #define CC_SURFACES_USE_TWO_SIDED USE_TWOSIDE + #define CC_SURFACES_USE_VERTEX_COLOR USE_VERTEX_COLOR + + // depend on UI macros +#if USE_NORMAL_MAP + #define CC_SURFACES_USE_TANGENT_SPACE 1 +#endif +}% + +CCProgram surface-vertex %{ + #define CC_SURFACES_VERTEX_MODIFY_UV + void SurfacesVertexModifyUV(inout SurfacesStandardVertexIntermediate In) + { + In.texCoord = In.texCoord * tilingOffset.xy + tilingOffset.zw; + #if CC_SURFACES_USE_SECOND_UV + In.texCoord1 = In.texCoord1 * tilingOffset.xy + tilingOffset.zw; + #endif + } +}% + +CCProgram surface-fragment %{ + #if USE_ALBEDO_MAP + uniform sampler2D albedoMap; + #pragma define-meta ALBEDO_UV options([v_uv, v_uv1]) + #endif + #if USE_NORMAL_MAP + uniform sampler2D normalMap; + #pragma define-meta NORMAL_UV options([v_uv, v_uv1]) + #endif + #if USE_PBR_MAP + uniform sampler2D pbrMap; + #endif + #pragma define-meta DEFAULT_UV options([v_uv, v_uv1]) + + #if USE_ALPHA_TEST + #pragma define-meta ALPHA_TEST_CHANNEL options([a, r]) + #endif + + + #define CC_SURFACES_FRAGMENT_MODIFY_BASECOLOR_AND_TRANSPARENCY + vec4 SurfacesFragmentModifyBaseColorAndTransparency() + { + vec4 baseColor = albedo; + + #if USE_VERTEX_COLOR + baseColor.rgb *= SRGBToLinear(FSInput_vertexColor.rgb); // use linear + baseColor.a *= FSInput_vertexColor.a; + #endif + + #if USE_ALBEDO_MAP + vec4 texColor = texture(albedoMap, ALBEDO_UV); + texColor.rgb = SRGBToLinear(texColor.rgb); + baseColor *= texColor; + #endif + + #if USE_ALPHA_TEST + if (baseColor.ALPHA_TEST_CHANNEL < albedoScaleAndCutoff.w) discard; + #endif + + baseColor.rgb *= albedoScaleAndCutoff.xyz; + return baseColor; + } + + #define CC_SURFACES_FRAGMENT_ALPHA_CLIP_ONLY + void SurfacesFragmentAlphaClipOnly() + { + #if USE_ALPHA_TEST + float alpha = albedo.ALPHA_TEST_CHANNEL; + #if USE_VERTEX_COLOR + alpha *= FSInput_vertexColor.a; + #endif + #if USE_ALBEDO_MAP + alpha = texture(albedoMap, ALBEDO_UV).ALPHA_TEST_CHANNEL; + #endif + + if (alpha < albedoScaleAndCutoff.w) discard; + #endif + } + + #define CC_SURFACES_FRAGMENT_MODIFY_WORLD_NORMAL + vec3 SurfacesFragmentModifyWorldNormal() + { + vec3 normal = FSInput_worldNormal; + #if USE_NORMAL_MAP + vec3 nmmp = texture(normalMap, NORMAL_UV).xyz - vec3(0.5); + normal = CalculateNormalFromTangentSpace(nmmp, emissiveScaleParam.w, normalize(normal.xyz), normalize(FSInput_worldTangent), FSInput_mirrorNormal); + #endif + + return normalize(normal); + } + + #define CC_SURFACES_FRAGMENT_MODIFY_PBRPARAMS + vec4 SurfacesFragmentModifyPBRParams() + { + vec4 pbr = pbrParams; + pbr.x = 1.0; + #if USE_PBR_MAP + vec4 res = texture(pbrMap, DEFAULT_UV); + pbr.yzw *= res.yzw; + #endif + + return pbr; + } + + #include + #include + #define CC_SURFACES_FRAGMENT_MODIFY_SHARED_DATA + void SurfacesFragmentModifySharedData(inout SurfacesMaterialData surfaceData) + { + // fresnel + vec3 viewDir = normalize(cc_cameraPos.xyz - surfaceData.worldPos); + float NoVSat = saturate(dot(surfaceData.worldNormal, viewDir)); + float F0 = emissiveScaleParam.x, F90 = emissiveScaleParam.y; + F0 = min(F0, F90); + surfaceData.baseColor.a = lerp(F90, F0, NoVSat); + } +}% + +CCProgram standard-vs %{ + precision highp float; + + // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros + #include + #include + + // 2. common include with corresponding shader stage, include before surface functions + #include + + // 3. user surface functions that can use user (effect) parameters (ubo Constants) + // see surfaces/default-functions/xxx.chunk + #include + #include + + // 4. surface include with corresponding shader stage and shading-model (optional) + #include + + // 5. shader entry with corresponding shader stage and technique usage/type + #include +}% + + +CCProgram shadow-caster-vs %{ + precision highp float; + #include + #include + #include + #include + #include + #include +}% + +CCProgram planar-shadow-vs %{ + precision highp float; + #include + #include + #include + #include + #include + #include +}% + + +CCProgram standard-fs %{ + // shading-model : standard + // lighting-model : standard (isotropy / anisotropy pbr) + // shader stage : fs + // technique usage/type : render-to-scene + + precision highp float; + // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros + #include + #include + + // 2. common include with corresponding shader stage, include before surface functions + #include + + // 3. user surface functions that can use user (effect) parameters (ubo Constants) + // see surfaces/default-functions/xxx.chunk + #include + #include + + // 4. lighting-model (optional) + #include + + // 5. surface include with corresponding shader stage and shading-model (optional) + #include + + // 6. shader entry with corresponding shader stage and technique usage/type + #include +}% + +CCProgram shadow-caster-fs %{ + precision highp float; + #include + #include + #include + #include + #include + #include +}% + +CCProgram planar-shadow-fs %{ + precision highp float; + #include + #include + #include + #include + #include + #include +}% + +CCProgram reflect-map-fs %{ + precision highp float; + #include + #include + #include + #include + #include + #include + #include + #include +}% + diff --git a/editor/assets/effects/advanced/glass.effect.meta b/editor/assets/effects/advanced/glass.effect.meta new file mode 100644 index 00000000000..ff18657e4e4 --- /dev/null +++ b/editor/assets/effects/advanced/glass.effect.meta @@ -0,0 +1,11 @@ +{ + "ver": "1.7.0", + "importer": "effect", + "imported": true, + "uuid": "f288f946-150b-443d-b4b3-0227c5117c93", + "files": [ + ".json" + ], + "subMetas": {}, + "userData": {} +} From 623d508945bd5801d6abe0aa5db69289b49ab788 Mon Sep 17 00:00:00 2001 From: jk20012001 Date: Wed, 19 Jul 2023 19:11:10 +0800 Subject: [PATCH 3/9] code style --- editor/assets/chunks/common/lighting/bxdf.chunk | 2 +- .../lighting-models/data-structures/lighting-result.chunk | 3 +-- .../assets/chunks/surfaces/module-functions/standard-fs.chunk | 3 --- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/editor/assets/chunks/common/lighting/bxdf.chunk b/editor/assets/chunks/common/lighting/bxdf.chunk index 04fb9662a7c..a1f9f98544f 100644 --- a/editor/assets/chunks/common/lighting/bxdf.chunk +++ b/editor/assets/chunks/common/lighting/bxdf.chunk @@ -53,4 +53,4 @@ void CalculateLayerBlending(inout vec3 blendedBaseLayerD, inout vec3 blendedBase blendedBaseLayerD *= vec3(1.0) - subLayerF; blendedBaseLayerS *= vec3(1.0) - subLayerF; lastLayerF = subLayerF; -} \ No newline at end of file +} diff --git a/editor/assets/chunks/lighting-models/data-structures/lighting-result.chunk b/editor/assets/chunks/lighting-models/data-structures/lighting-result.chunk index 9105b8878a9..d6310b378a2 100644 --- a/editor/assets/chunks/lighting-models/data-structures/lighting-result.chunk +++ b/editor/assets/chunks/lighting-models/data-structures/lighting-result.chunk @@ -44,5 +44,4 @@ struct LightingResultPerLayer vec3 diffuseColorWithLighting, specularColorWithLighting; vec3 directDiffuse, directSpecular, directGF; vec3 environmentDiffuse, environmentSpecular, environmentGF; -} -*/ \ No newline at end of file +}*/ diff --git a/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk b/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk index 7bd93a3cda9..6399cee8adc 100644 --- a/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk +++ b/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk @@ -259,9 +259,6 @@ void CCSurfacesInitializeLightingResult(inout LightingResult lightingResult) #endif } -//todo: vec3 CCSurfacesLightingBlendLayer(LightingResult lightingResult, MaterialSurfaceData surfaceData)D1, S1, F1, D2, S2, F2),需要带着材质颜色来混 -//多光源下只支持在一个pass内完成,即在common-flow中循环每个光源都是一个lightingResult,每个都调用BlendLayer,然后累加最终颜色即可 - // Accumulated lighting results for additive pass only void CCSurfacesAccumulateLightingResult(inout LightingResult lightingResultAccumulated, in LightingResult lightingResult) { From 6bfcb0fafe25721670e4092c57444710eb2a1ad2 Mon Sep 17 00:00:00 2001 From: jk20012001 Date: Wed, 19 Jul 2023 19:41:44 +0800 Subject: [PATCH 4/9] code --- .../chunks/lighting-models/model-functions/standard.chunk | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/editor/assets/chunks/lighting-models/model-functions/standard.chunk b/editor/assets/chunks/lighting-models/model-functions/standard.chunk index ed23f849f8d..26d3c5c2c2f 100644 --- a/editor/assets/chunks/lighting-models/model-functions/standard.chunk +++ b/editor/assets/chunks/lighting-models/model-functions/standard.chunk @@ -258,7 +258,7 @@ void CCSurfaceLightingCalculateEnvironmentSubLayerFresnel(out vec3 subLayerF, in #endif ) { - vec3 lastLayerF, zeroVector = vec3(0.0); + vec3 lastLayerF, zeroDiffuse = vec3(0.0); vec3 blendedBaseD = lightingResult.directDiffuse, blendedBaseS = lightingResult.directSpecular; InitializeLayerBlending(blendedBaseD, blendedBaseS, lightingResult.directDiffuseSubLayers, lightingResult.directSpecularSubLayers, @@ -270,7 +270,7 @@ void CCSurfaceLightingCalculateEnvironmentSubLayerFresnel(out vec3 subLayerF, in CalculateLayerBlending (blendedBaseD, blendedBaseS, lightingResult.directDiffuseSubLayers, lightingResult.directSpecularSubLayers, lastLayerF, - zeroVector, zeroVector, + zeroDiffuse, zeroDiffuse, lightingResult.direct2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, lightingData2ndLayer.layerOpacity, lightingResult.directSubLayerF ); @@ -290,7 +290,7 @@ void CCSurfaceLightingCalculateEnvironmentSubLayerFresnel(out vec3 subLayerF, in #endif ) { - vec3 lastLayerF, zeroVector = vec3(0.0); + vec3 lastLayerF, zeroDiffuse = vec3(0.0); vec3 blendedBaseD = lightingResult.environmentDiffuse, blendedBaseS = lightingResult.environmentSpecular; InitializeLayerBlending(blendedBaseD, blendedBaseS, lightingResult.environmentDiffuseSubLayers, lightingResult.environmentSpecularSubLayers, @@ -302,7 +302,7 @@ void CCSurfaceLightingCalculateEnvironmentSubLayerFresnel(out vec3 subLayerF, in CalculateLayerBlending (blendedBaseD, blendedBaseS, lightingResult.environmentDiffuseSubLayers, lightingResult.environmentSpecularSubLayers, lastLayerF, - zeroVector, zeroVector, + zeroDiffuse, zeroDiffuse, lightingResult.environment2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, lightingData2ndLayer.layerOpacity, lightingResult.environmentSubLayerF ); From ed5356d0ed1ff8f879d861ccd5cd594504f82e91 Mon Sep 17 00:00:00 2001 From: jk20012001 Date: Thu, 20 Jul 2023 00:45:27 +0800 Subject: [PATCH 5/9] optimize effect --- .../assets/effects/advanced/car-paint.effect | 65 ++++--------------- editor/assets/effects/advanced/glass.effect | 2 - 2 files changed, 12 insertions(+), 55 deletions(-) diff --git a/editor/assets/effects/advanced/car-paint.effect b/editor/assets/effects/advanced/car-paint.effect index 1a1d900767b..8839a016ec0 100644 --- a/editor/assets/effects/advanced/car-paint.effect +++ b/editor/assets/effects/advanced/car-paint.effect @@ -9,6 +9,7 @@ CCEffect %{ properties: &props tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] } mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, linear: true, editor: { displayName: Albedo, type: color } } + gradientColor: { value: [1.0, 1.0, 1.0, 1.0], linear: true, editor: { parent: USE_GRADIENT_COLOR, type: color } } albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST, slide: true, range: [0, 1.0], step: 0.001 } } occlusion: { value: 0.0, target: pbrParams.x, editor: { slide: true, range: [0, 1.0], step: 0.001 } } @@ -16,10 +17,7 @@ CCEffect %{ metallic: { value: 0.0, target: pbrParams.z, editor: { slide: true, range: [0, 1.0], step: 0.001 } } specularIntensity: { value: 0.5, target: pbrParams.w, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } } normalStrength: { value: 1.0, target: emissiveScaleParam.w, editor: { parent: USE_NORMAL_MAP, slide: true, range: [0, 5.0], step: 0.001 } } - anisotropyIntensity: { value: 1.0, target: anisotropyParam.x, editor: { parent: IS_ANISOTROPY, slide : true, range : [0.0, 1.0] , step : 0.0001 } } - anisotropyRotation: { value: 0.0, target: anisotropyParam.y, editor: { parent: IS_ANISOTROPY, slide : true, range : [0, 1.0] , step : 0.0001 } } - anisotropyMapResolutionHeight: { value: 0.0, target: anisotropyParam.w, editor: { parent: FIX_ANISOTROPIC_ROTATION_MAP } } - addOnShadowBias: { value: 0.0, target: anisotropyParam.z } + addOnShadowBias: { value: 0.0, target: emissiveScaleParam.z } coatRoughness: { value: 0.05, target: coatParam.x, editor: { slide : true, range : [0, 1.0] , step : 0.0001 } } coatIOR: { value: 1.0, target: coatParam.y, editor: { slide : true, range : [1.0, 1.5] , step : 0.0001 } } coatOpacity: { value: 1.0, target: coatParam.z, editor: { slide : true, range : [0.0, 1.0] , step : 0.0001 } } @@ -35,8 +33,6 @@ CCEffect %{ flakeDataMap: { value: white, editor: { parent: USE_FLAKE } } pbrMap: { value: grey } occlusionMap: { value: white } - anisotropyMap: { value: black, editor : { parent: IS_ANISOTROPY } } - anisotropyMapNearestFilter: { value: black, editor : { parent: FIX_ANISOTROPIC_ROTATION_MAP } } - &forward-add vert: standard-vs frag: standard-fs @@ -134,10 +130,10 @@ CCProgram shared-ubos %{ vec4 albedoScaleAndCutoff; vec4 pbrParams; vec4 emissiveScaleParam; - vec4 anisotropyParam; vec4 coatParam; vec4 coatColor; vec4 flakeParam; + vec4 gradientColor; }; }% @@ -145,31 +141,25 @@ CCProgram macro-remapping %{ // ui displayed macros #pragma define-meta HAS_SECOND_UV #pragma define-meta USE_TWOSIDE - #pragma define-meta IS_ANISOTROPY #pragma define-meta USE_VERTEX_COLOR - #pragma define-meta FIX_ANISOTROPIC_ROTATION_MAP #pragma define-meta USE_FLAKE #define CC_SURFACES_USE_SECOND_UV HAS_SECOND_UV #define CC_SURFACES_USE_TWO_SIDED USE_TWOSIDE - #define CC_SURFACES_LIGHTING_ANISOTROPIC IS_ANISOTROPY #define CC_SURFACES_USE_VERTEX_COLOR USE_VERTEX_COLOR #define CC_SURFACES_LIGHTING_CLEAR_COAT 1 // depend on UI macros -#if IS_ANISOTROPY || USE_NORMAL_MAP || USE_FLAKE +#if USE_NORMAL_MAP || USE_FLAKE #define CC_SURFACES_USE_TANGENT_SPACE 1 #endif - - // functionality for each effect - #define CC_SURFACES_LIGHTING_ANISOTROPIC_ENVCONVOLUTION_COUNT 31 }% CCProgram surface-vertex %{ #define CC_SURFACES_VERTEX_MODIFY_SHADOW_BIAS vec2 SurfacesVertexModifyShadowBias(in SurfacesStandardVertexIntermediate In, vec2 originShadowBias) { - return originShadowBias + vec2(anisotropyParam.z, 0.0); + return originShadowBias + vec2(emissiveScaleParam.z, 0.0); } #define CC_SURFACES_VERTEX_MODIFY_UV @@ -198,10 +188,6 @@ CCProgram surface-fragment %{ #if USE_OCCLUSION_MAP uniform sampler2D occlusionMap; #endif - #if IS_ANISOTROPY && USE_ANISOTROPY_MAP - uniform sampler2D anisotropyMap; - uniform sampler2D anisotropyMapNearestFilter; - #endif #if USE_FLAKE uniform sampler2D coatNormalMap; uniform sampler2D flakeDataMap; @@ -216,39 +202,6 @@ CCProgram surface-fragment %{ #pragma define-meta ALPHA_TEST_CHANNEL options([a, r]) #endif - #define CC_SURFACES_FRAGMENT_MODIFY_ANISOTROPY_PARAMS - vec4 SurfacesFragmentModifyAnisotropyParams(out float isRotation) - { - float anisotropyRotation = anisotropyParam.y * PI; - float anisotropyShape = anisotropyParam.x; - #if IS_ANISOTROPY && USE_ANISOTROPY_MAP - // Rotation angle should disable trilinear filtering - vec4 tex = texture(anisotropyMap, DEFAULT_UV); - anisotropyRotation = fract(anisotropyRotation * 0.5 + tex.y) * PI2; - // less value is better for SP exported shape - anisotropyShape *= tex.x; - #endif - - // fix rotation map seam line of black and white - #if FIX_ANISOTROPIC_ROTATION_MAP - #if IS_ANISOTROPY && USE_ANISOTROPY_MAP - vec4 reference = texture(anisotropyMapNearestFilter, DEFAULT_UV); - vec2 oneTap = vec2(0.0, 1.0 / anisotropyParam.w); - float threshold = 0.2; - // scan more taps for stable result - vec4 sample1 = texture(anisotropyMapNearestFilter, DEFAULT_UV + oneTap); - vec4 sample2 = texture(anisotropyMapNearestFilter, DEFAULT_UV - oneTap); - if (abs(sample1.y - reference.y) > threshold || abs(sample2.y - reference.y) > threshold) { - tex.y = reference.y; - } - anisotropyRotation = fract(anisotropyParam.y * PI * 0.5 + tex.y) * PI2; - #endif - #endif - - isRotation = 1.0; - return vec4(anisotropyShape, anisotropyRotation, 0.0, 0.0); - } - #define CC_SURFACES_FRAGMENT_MODIFY_CLEAR_COAT_PARAMS vec4 SurfacesFragmentModifyClearCoatParams() { @@ -292,7 +245,13 @@ CCProgram surface-fragment %{ #include #define CC_SURFACES_FRAGMENT_MODIFY_SHARED_DATA void SurfacesFragmentModifySharedData(inout SurfacesMaterialData surfaceData) - { + { + #if USE_GRADIENT_COLOR + vec3 viewDir = normalize(cc_cameraPos.xyz - surfaceData.worldPos); + float NoVSat = saturate(dot(surfaceData.worldNormal, viewDir)); + surfaceData.baseColor = lerp(gradientColor, surfaceData.baseColor, NoVSat); + #endif + #if USE_FLAKE float density = flakeParam.x, flakeColorIntensity = flakeParam.y, flakeTiling = flakeParam.z; vec2 uv = FSInput_texcoord * flakeTiling; diff --git a/editor/assets/effects/advanced/glass.effect b/editor/assets/effects/advanced/glass.effect index e83f9b7f513..7de468e3b97 100644 --- a/editor/assets/effects/advanced/glass.effect +++ b/editor/assets/effects/advanced/glass.effect @@ -122,11 +122,9 @@ CCProgram shared-ubos %{ CCProgram macro-remapping %{ // ui displayed macros - #pragma define-meta HAS_SECOND_UV #pragma define-meta USE_TWOSIDE #pragma define-meta USE_VERTEX_COLOR - #define CC_SURFACES_USE_SECOND_UV HAS_SECOND_UV #define CC_SURFACES_USE_TWO_SIDED USE_TWOSIDE #define CC_SURFACES_USE_VERTEX_COLOR USE_VERTEX_COLOR From e8c263a0c7fb3b6d1d816790bf73ae328a160285 Mon Sep 17 00:00:00 2001 From: jk20012001 Date: Thu, 20 Jul 2023 14:52:05 +0800 Subject: [PATCH 6/9] fix gradient color --- .../assets/chunks/common/lighting/bxdf.chunk | 16 +++++++++++----- .../lighting-intermediate-data.chunk | 1 + .../model-functions/standard.chunk | 5 ++++- .../assets/effects/advanced/car-paint.effect | 13 ++++++++----- editor/assets/effects/advanced/glass.effect | 18 ++++++++++++------ 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/editor/assets/chunks/common/lighting/bxdf.chunk b/editor/assets/chunks/common/lighting/bxdf.chunk index a1f9f98544f..594f7932944 100644 --- a/editor/assets/chunks/common/lighting/bxdf.chunk +++ b/editor/assets/chunks/common/lighting/bxdf.chunk @@ -1,6 +1,6 @@ // saturated N dot V -// for env reflection with dielectric(non-metal) and smooth surface +// for env reflection with dielectric(non-metal) and smooth surfaces float CalculateFresnelCoefficient(float ior, float NoVSat) { float g, c, n, prev, next; @@ -14,13 +14,19 @@ float CalculateFresnelCoefficient(float ior, float NoVSat) return 0.5 * prev * (1.0 + next); } +// for arbitary smooth surfaces +float CalculateFresnelCoefficient(float F0, float F90, float NoVSat) +{ + return mix(F90, F0, NoVSat); +} + vec3 CalculateScattering(vec3 unscatteredColor, float distance, float outScatterExtinctCoef, float inScatterExtinctCoef, float inScatterCoef, vec3 inScatterColor, vec3 outScatterColor) { - vec2 e = vec2(outScatterExtinctCoef, inScatterExtinctCoef * inScatterCoef); - vec2 extinction = exp(-e * distance); - vec3 inScattered = (1.0 - extinction.y) * inScatterColor; + vec2 e = vec2(outScatterExtinctCoef, inScatterExtinctCoef * inScatterCoef); + vec2 extinction = exp(-e * distance); + vec3 inScattered = (1.0 - extinction.y) * inScatterColor; vec3 outScattered = unscatteredColor * extinction.x * outScatterColor; - return outScattered + inScattered; + return outScattered + inScattered; } diff --git a/editor/assets/chunks/lighting-models/data-structures/lighting-intermediate-data.chunk b/editor/assets/chunks/lighting-models/data-structures/lighting-intermediate-data.chunk index 9a1a190c4de..54e54b14bbf 100644 --- a/editor/assets/chunks/lighting-models/data-structures/lighting-intermediate-data.chunk +++ b/editor/assets/chunks/lighting-models/data-structures/lighting-intermediate-data.chunk @@ -16,6 +16,7 @@ struct LightingIntermediateData // material data float specularParam; // roughness or specular power + // todo: add F0 & F90 float ior, layerOpacity; #if CC_SURFACES_LIGHTING_ANISOTROPIC diff --git a/editor/assets/chunks/lighting-models/model-functions/standard.chunk b/editor/assets/chunks/lighting-models/model-functions/standard.chunk index 26d3c5c2c2f..ef4c1788286 100644 --- a/editor/assets/chunks/lighting-models/model-functions/standard.chunk +++ b/editor/assets/chunks/lighting-models/model-functions/standard.chunk @@ -90,8 +90,11 @@ void CCSurfaceLightingCalculateDirectSubLayerFresnel(out vec3 subLayerF, in Ligh } void CCSurfaceLightingCalculateEnvironmentSubLayerFresnel(out vec3 subLayerF, in LightingIntermediateData lightingData, vec3 specularColor) { - // ior related fresnel: + // ior related: subLayerF = vec3(CalculateFresnelCoefficient(lightingData.ior, lightingData.NoVSat)); + // F0 F90 related: + // if (equalf(lightingData.ior, 0.0)) + // subLayerF = vec3(CalculateFresnelCoefficient(lightingData.F0, lightingData.F90, lightingData.NoVSat)); } diff --git a/editor/assets/effects/advanced/car-paint.effect b/editor/assets/effects/advanced/car-paint.effect index 8839a016ec0..74389ebb92c 100644 --- a/editor/assets/effects/advanced/car-paint.effect +++ b/editor/assets/effects/advanced/car-paint.effect @@ -9,7 +9,6 @@ CCEffect %{ properties: &props tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] } mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, linear: true, editor: { displayName: Albedo, type: color } } - gradientColor: { value: [1.0, 1.0, 1.0, 1.0], linear: true, editor: { parent: USE_GRADIENT_COLOR, type: color } } albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST, slide: true, range: [0, 1.0], step: 0.001 } } occlusion: { value: 0.0, target: pbrParams.x, editor: { slide: true, range: [0, 1.0], step: 0.001 } } @@ -27,6 +26,8 @@ CCEffect %{ flakeColorIntensity: { value: 0.5, target: flakeParam.y, editor: { parent: USE_FLAKE, slide: true, range: [0, 1.0], step: 0.001 } } flakeTiling: { value: 10.0, target: flakeParam.z, editor: { parent: USE_FLAKE, slide: true, range: [1.0, 40.0], step: 0.001 } } coatNormalStrength: { value: 1.0, target: flakeParam.w, editor: { parent: USE_FLAKE, slide: true, range: [0, 5.0], step: 0.001 } } + gradientColor: { value: [1.0, 1.0, 1.0, 1.0], linear: true, editor: { parent: USE_GRADIENT_COLOR, type: color } } + gradientIntensity: { value: 0.5, target: emissiveScaleParam.y, editor: { parent: USE_GRADIENT_COLOR, slide: true, range: [0, 1.0], step: 0.001 } } mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } normalMap: { value: normal } coatNormalMap: { value: normal, editor: { parent: USE_FLAKE } } @@ -249,7 +250,7 @@ CCProgram surface-fragment %{ #if USE_GRADIENT_COLOR vec3 viewDir = normalize(cc_cameraPos.xyz - surfaceData.worldPos); float NoVSat = saturate(dot(surfaceData.worldNormal, viewDir)); - surfaceData.baseColor = lerp(gradientColor, surfaceData.baseColor, NoVSat); + surfaceData.baseColor.rgb = lerp(gradientColor, surfaceData.baseColor, pow(NoVSat, emissiveScaleParam.y)).rgb; #endif #if USE_FLAKE @@ -258,17 +259,19 @@ CCProgram surface-fragment %{ vec4 data = sampleFlake(flakeDataMap, uv); float C = (data.a - 1.0) / density + 1.0; + float flakeTintWeight = 1.0; + float flakeWeight = saturate(C) * flakeTintWeight * flakeColorIntensity; // color vec4 flakeA = vec4(0.972,0.96,0.915,1); vec4 flakeB = vec4(1.0,0.78,0.344,1); vec4 flakeColor = lerp(flakeA, flakeB, data.r); - float flakeTintWeight = 1.0; vec4 color = surfaceData.baseColor; - color = lerp(color, flakeColor, saturate(C) * flakeTintWeight); - color = lerp(surfaceData.baseColor, color, surfaceData.metallic * flakeColorIntensity); + color = lerp(color, flakeColor, flakeWeight); + color = lerp(surfaceData.baseColor, color, surfaceData.metallic); surfaceData.baseColor = color; + surfaceData.roughness *= lerp(1.0, 0.5, flakeWeight); #endif } }% diff --git a/editor/assets/effects/advanced/glass.effect b/editor/assets/effects/advanced/glass.effect index 7de468e3b97..74a284f7f78 100644 --- a/editor/assets/effects/advanced/glass.effect +++ b/editor/assets/effects/advanced/glass.effect @@ -8,14 +8,16 @@ CCEffect %{ frag: standard-fs properties: &props tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] } - mainColor: { value: [1.0, 1.0, 1.0, 0.5], target: albedo, linear: true, editor: { displayName: Albedo, type: color } } + mainColor: { value: [0.05, 0.05, 0.05, 1.0], target: albedo, linear: true, editor: { displayName: Albedo, type: color } } albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } } roughness: { value: 0.0, target: pbrParams.y, editor: { slide: true, range: [0, 1.0], step: 0.001 } } metallic: { value: 0.0, target: pbrParams.z, editor: { slide: true, range: [0, 1.0], step: 0.001 } } - specularIntensity: { value: 0.5, target: pbrParams.w, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } } - F0: { value: 0.3, target: emissiveScaleParam.x, editor: { slide: true, range: [0, 1.0], step: 0.001 } } - F90: { value: 0.8, target: emissiveScaleParam.y, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } } + specularIntensity: { value: 0.2, target: pbrParams.w, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } } + F0: { value: 0.7, target: emissiveScaleParam.x, editor: { slide: true, range: [0, 1.0], step: 0.001 } } + F90: { value: 0.9, target: emissiveScaleParam.y, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } } + gradientColor: { value: [0.0, 0.05, 0.0, 1.0], linear: true, editor: { parent: USE_GRADIENT_COLOR, type: color } } + gradientIntensity: { value: 0.2, target: emissiveScaleParam.z, editor: { parent: USE_GRADIENT_COLOR, slide: true, range: [0, 1.0], step: 0.001 } } normalStrength: { value: 1.0, target: emissiveScaleParam.w, editor: { parent: USE_NORMAL_MAP, slide: true, range: [0, 5.0], step: 0.001 } } mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } normalMap: { value: normal } @@ -117,6 +119,7 @@ CCProgram shared-ubos %{ vec4 albedoScaleAndCutoff; vec4 pbrParams; vec4 emissiveScaleParam; + vec4 gradientColor; }; }% @@ -238,8 +241,11 @@ CCProgram surface-fragment %{ vec3 viewDir = normalize(cc_cameraPos.xyz - surfaceData.worldPos); float NoVSat = saturate(dot(surfaceData.worldNormal, viewDir)); float F0 = emissiveScaleParam.x, F90 = emissiveScaleParam.y; - F0 = min(F0, F90); - surfaceData.baseColor.a = lerp(F90, F0, NoVSat); + surfaceData.baseColor.a = CalculateFresnelCoefficient(min(F0, F90), F90, NoVSat); + + #if USE_GRADIENT_COLOR + surfaceData.baseColor.rgb = lerp(gradientColor * albedoScaleAndCutoff, surfaceData.baseColor, pow(NoVSat, emissiveScaleParam.z)).rgb; + #endif } }% From feec922d904fea5506a5de83b0f857149b3d58b1 Mon Sep 17 00:00:00 2001 From: jk20012001 Date: Thu, 20 Jul 2023 17:30:49 +0800 Subject: [PATCH 7/9] effect update --- .../assets/effects/advanced/car-paint.effect | 6 +- editor/assets/effects/advanced/glass.effect | 58 +++++++++---------- editor/assets/effects/advanced/hair.effect | 2 +- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/editor/assets/effects/advanced/car-paint.effect b/editor/assets/effects/advanced/car-paint.effect index 74389ebb92c..6e5aede6071 100644 --- a/editor/assets/effects/advanced/car-paint.effect +++ b/editor/assets/effects/advanced/car-paint.effect @@ -22,10 +22,10 @@ CCEffect %{ coatOpacity: { value: 1.0, target: coatParam.z, editor: { slide : true, range : [0.0, 1.0] , step : 0.0001 } } coatIntensity: { value: 1.0, target: coatParam.w, editor: { slide : true, range : [0.0, 3.0] , step : 0.0001 } } coatColor: { value: [1.0, 1.0, 1.0, 1.0], linear: true, editor: { displayName: CoatColor, type: color } } - flakeDensity: { value: 0.25, target: flakeParam.x, editor: { parent: USE_FLAKE, slide: true, range: [0, 1.0], step: 0.001 } } - flakeColorIntensity: { value: 0.5, target: flakeParam.y, editor: { parent: USE_FLAKE, slide: true, range: [0, 1.0], step: 0.001 } } + flakeDensity: { value: 0.5, target: flakeParam.x, editor: { parent: USE_FLAKE, slide: true, range: [0, 1.0], step: 0.001 } } + flakeColorIntensity: { value: 0.2, target: flakeParam.y, editor: { parent: USE_FLAKE, slide: true, range: [0, 1.0], step: 0.001 } } flakeTiling: { value: 10.0, target: flakeParam.z, editor: { parent: USE_FLAKE, slide: true, range: [1.0, 40.0], step: 0.001 } } - coatNormalStrength: { value: 1.0, target: flakeParam.w, editor: { parent: USE_FLAKE, slide: true, range: [0, 5.0], step: 0.001 } } + coatNormalStrength: { value: 0.1, target: flakeParam.w, editor: { parent: USE_FLAKE, slide: true, range: [0, 1.0], step: 0.001 } } gradientColor: { value: [1.0, 1.0, 1.0, 1.0], linear: true, editor: { parent: USE_GRADIENT_COLOR, type: color } } gradientIntensity: { value: 0.5, target: emissiveScaleParam.y, editor: { parent: USE_GRADIENT_COLOR, slide: true, range: [0, 1.0], step: 0.001 } } mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } diff --git a/editor/assets/effects/advanced/glass.effect b/editor/assets/effects/advanced/glass.effect index 74a284f7f78..f8c76e0ce2e 100644 --- a/editor/assets/effects/advanced/glass.effect +++ b/editor/assets/effects/advanced/glass.effect @@ -10,7 +10,6 @@ CCEffect %{ tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] } mainColor: { value: [0.05, 0.05, 0.05, 1.0], target: albedo, linear: true, editor: { displayName: Albedo, type: color } } albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } - alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } } roughness: { value: 0.0, target: pbrParams.y, editor: { slide: true, range: [0, 1.0], step: 0.001 } } metallic: { value: 0.0, target: pbrParams.z, editor: { slide: true, range: [0, 1.0], step: 0.001 } } specularIntensity: { value: 0.2, target: pbrParams.w, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } } @@ -50,7 +49,6 @@ CCEffect %{ tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] } mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, editor: { displayName: Albedo, type: color } } albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } - alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } } mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } - &reflect-map vert: standard-vs @@ -127,9 +125,12 @@ CCProgram macro-remapping %{ // ui displayed macros #pragma define-meta USE_TWOSIDE #pragma define-meta USE_VERTEX_COLOR + #pragma define-meta USE_PROBE_REFRACTION editor({ tooltip: 'Use transparency technique when disabled, otherwise use opaque technique' }) #define CC_SURFACES_USE_TWO_SIDED USE_TWOSIDE #define CC_SURFACES_USE_VERTEX_COLOR USE_VERTEX_COLOR + #define CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR USE_PROBE_REFRACTION + #define CC_SURFACES_LIGHTING_USE_FRESNEL USE_PROBE_REFRACTION // depend on UI macros #if USE_NORMAL_MAP @@ -162,11 +163,6 @@ CCProgram surface-fragment %{ #endif #pragma define-meta DEFAULT_UV options([v_uv, v_uv1]) - #if USE_ALPHA_TEST - #pragma define-meta ALPHA_TEST_CHANNEL options([a, r]) - #endif - - #define CC_SURFACES_FRAGMENT_MODIFY_BASECOLOR_AND_TRANSPARENCY vec4 SurfacesFragmentModifyBaseColorAndTransparency() { @@ -183,30 +179,10 @@ CCProgram surface-fragment %{ baseColor *= texColor; #endif - #if USE_ALPHA_TEST - if (baseColor.ALPHA_TEST_CHANNEL < albedoScaleAndCutoff.w) discard; - #endif - baseColor.rgb *= albedoScaleAndCutoff.xyz; return baseColor; } - #define CC_SURFACES_FRAGMENT_ALPHA_CLIP_ONLY - void SurfacesFragmentAlphaClipOnly() - { - #if USE_ALPHA_TEST - float alpha = albedo.ALPHA_TEST_CHANNEL; - #if USE_VERTEX_COLOR - alpha *= FSInput_vertexColor.a; - #endif - #if USE_ALBEDO_MAP - alpha = texture(albedoMap, ALBEDO_UV).ALPHA_TEST_CHANNEL; - #endif - - if (alpha < albedoScaleAndCutoff.w) discard; - #endif - } - #define CC_SURFACES_FRAGMENT_MODIFY_WORLD_NORMAL vec3 SurfacesFragmentModifyWorldNormal() { @@ -237,16 +213,38 @@ CCProgram surface-fragment %{ #define CC_SURFACES_FRAGMENT_MODIFY_SHARED_DATA void SurfacesFragmentModifySharedData(inout SurfacesMaterialData surfaceData) { - // fresnel vec3 viewDir = normalize(cc_cameraPos.xyz - surfaceData.worldPos); float NoVSat = saturate(dot(surfaceData.worldNormal, viewDir)); - float F0 = emissiveScaleParam.x, F90 = emissiveScaleParam.y; - surfaceData.baseColor.a = CalculateFresnelCoefficient(min(F0, F90), F90, NoVSat); + + #if !USE_PROBE_REFRACTION + // fresnel for alpha blend + float F0 = emissiveScaleParam.x, F90 = emissiveScaleParam.y; + surfaceData.baseColor.a = CalculateFresnelCoefficient(min(F0, F90), F90, NoVSat); + #endif #if USE_GRADIENT_COLOR surfaceData.baseColor.rgb = lerp(gradientColor * albedoScaleAndCutoff, surfaceData.baseColor, pow(NoVSat, emissiveScaleParam.z)).rgb; #endif } + + #include + #define CC_SURFACES_LIGHTING_MODIFY_FINAL_RESULT + void SurfacesLightingModifyFinalResult(inout LightingResult result, in LightingIntermediateData lightingData, in SurfacesMaterialData surfaceData, in LightingMiscData miscData) + { + #if USE_PROBE_REFRACTION + // fresnel for refraction + vec3 viewDir = normalize(cc_cameraPos.xyz - surfaceData.worldPos); + float NoVSat = saturate(dot(surfaceData.worldNormal, viewDir)); + float F0 = emissiveScaleParam.x, F90 = emissiveScaleParam.y; + float fresnel = CalculateFresnelCoefficient(min(F0, F90), F90, NoVSat); + result.fresnel = vec3(fresnel); + #else + // simply fix oit depth issue for bright color alpha blending + float threshold = 0.5, bright = dot(result.environmentSpecular, vec3(0.33333)); + if (bright > threshold) + result.environmentSpecular = result.environmentSpecular / bright * threshold; + #endif + } }% CCProgram standard-vs %{ diff --git a/editor/assets/effects/advanced/hair.effect b/editor/assets/effects/advanced/hair.effect index 3a2d18e2b2e..9c500f38a29 100644 --- a/editor/assets/effects/advanced/hair.effect +++ b/editor/assets/effects/advanced/hair.effect @@ -26,7 +26,7 @@ CCEffect %{ anisotropyMap: { value: black } roughnessTRT: { value: 0.2, target: trtParams.x, editor: { displayName: TRT Roughness Offset, slide: true, range: [0, 1.0], step: 0.001, tooltip: 'The extend range of internal reflection lighting around the direct specular' } } offsetTRT: { value: 0.0, target: trtParams.y, editor: { displayName: TRT Lighting Offset, slide: true, range: [-0.5, 0.5], step: 0.001, tooltip: 'The position offset of internal reflection lighting compared to direct specular' } } - IntensityTRT: { value: 1.0, target: trtParams.w, editor: { displayName: TRT Intensity, slide: true, range: [0, 5.0], step: 0.01 } } + IntensityTRT: { value: 2.0, target: trtParams.w, editor: { displayName: TRT Intensity, slide: true, range: [0, 10.0], step: 0.01 } } baseColorTRT: { value: [0.5, 0.33, 0.18, 1.0], target: trtColor, linear: true, editor: { displayName: TRT Albedo, type: color, tooltip: 'The color of internal reflection lighting' } } ScatterCoefTT: { value: 0.0, target: ttParams.x, editor: { displayName: TT Scatter Coef, slide: true, range: [0, 1.0], step: 0.01, tooltip: 'The saturation of internal transmit lighting color' } } IntensityTT: { value: 1.0, target: ttParams.w, editor: { displayName: TT Intensity, slide: true, range: [0, 1.0], step: 0.01 } } From 38e22013d059452545d3ce9c01bbbb506ac3d50b Mon Sep 17 00:00:00 2001 From: jk20012001 Date: Thu, 20 Jul 2023 18:29:10 +0800 Subject: [PATCH 8/9] code style --- editor/assets/chunks/common/lighting/brdf.chunk | 13 +------------ editor/assets/chunks/common/lighting/bxdf.chunk | 2 +- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/editor/assets/chunks/common/lighting/brdf.chunk b/editor/assets/chunks/common/lighting/brdf.chunk index a8577220805..ef4fea792a1 100644 --- a/editor/assets/chunks/common/lighting/brdf.chunk +++ b/editor/assets/chunks/common/lighting/brdf.chunk @@ -12,10 +12,6 @@ float IorToF0(float ior) return F0_sqrt * F0_sqrt; } -float V_Kelemen(float LoH) -{ - return 0.25 / (LoH * LoH); -} float square(float a) { return a * a;} vec2 square(vec2 a) { return a * a;} vec3 square(vec3 a) { return a * a;} @@ -26,6 +22,7 @@ float G_Schlick( float roughness, float NoV, float NoL ) float G_SchlickL = NoL * (1.0 - k) + k; return 0.25 / ( G_SchlickV * G_SchlickL ); } + vec3 F_Schlick( vec3 specularColor, float VoH ) { float Fc = exp2( (-5.55473 * VoH - 6.98316) * VoH ); @@ -39,14 +36,6 @@ vec3 F_SchlickMultiplier( vec3 specularColor, float VoH ) return vec3(1.0 - Fc) + vec3(selfShadowTerm * Fc) / (specularColor + vec3(EPSILON)); } -vec3 F_Fresnel( vec3 SpecularColor, float VoH ) -{ - vec3 specularColorSqrt = sqrt( clamp( vec3(0.0), vec3(0.99), SpecularColor ) ); - vec3 n = ( 1.0 + specularColorSqrt ) / ( 1.0 - specularColorSqrt ); - vec3 g = sqrt( n*n + VoH*VoH - 1.0 ); - return 0.5 * square( (g - VoH) / (g + VoH) ) * ( 1.0 + square( ((g+VoH)*VoH - 1.0) / ((g-VoH)*VoH + 1.0) ) ); -} - float D_GGX(float roughness, float NoH) { float m = roughness * roughness; diff --git a/editor/assets/chunks/common/lighting/bxdf.chunk b/editor/assets/chunks/common/lighting/bxdf.chunk index 594f7932944..ba06af07417 100644 --- a/editor/assets/chunks/common/lighting/bxdf.chunk +++ b/editor/assets/chunks/common/lighting/bxdf.chunk @@ -25,7 +25,7 @@ vec3 CalculateScattering(vec3 unscatteredColor, float distance, float outScatter vec2 e = vec2(outScatterExtinctCoef, inScatterExtinctCoef * inScatterCoef); vec2 extinction = exp(-e * distance); vec3 inScattered = (1.0 - extinction.y) * inScatterColor; - vec3 outScattered = unscatteredColor * extinction.x * outScatterColor; + vec3 outScattered = unscatteredColor * extinction.x * outScatterColor; return outScattered + inScattered; } From 24eee0e894b325c54a2b32b3d0e13b291206b1a1 Mon Sep 17 00:00:00 2001 From: jk20012001 Date: Thu, 20 Jul 2023 18:55:12 +0800 Subject: [PATCH 9/9] code style --- .../model-functions/standard.chunk | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/editor/assets/chunks/lighting-models/model-functions/standard.chunk b/editor/assets/chunks/lighting-models/model-functions/standard.chunk index ef4c1788286..accd9389b63 100644 --- a/editor/assets/chunks/lighting-models/model-functions/standard.chunk +++ b/editor/assets/chunks/lighting-models/model-functions/standard.chunk @@ -83,20 +83,21 @@ void CCSurfaceLightingCalculateEnvironmentFresnel(out vec3 integratedGF, vec3 in IntegratedGFMultiplier(integratedGF, integratedF, specularColor, roughness, lightingData.NoVAbsSat); } -void CCSurfaceLightingCalculateDirectSubLayerFresnel(out vec3 subLayerF, in LightingIntermediateData lightingData, vec3 specularColor) -{ - float VoH = lightingData.VoHAbsSat; - subLayerF = vec3(exp2( (-5.55473 * VoH - 6.98316) * VoH )); -} -void CCSurfaceLightingCalculateEnvironmentSubLayerFresnel(out vec3 subLayerF, in LightingIntermediateData lightingData, vec3 specularColor) -{ - // ior related: - subLayerF = vec3(CalculateFresnelCoefficient(lightingData.ior, lightingData.NoVSat)); - // F0 F90 related: - // if (equalf(lightingData.ior, 0.0)) - // subLayerF = vec3(CalculateFresnelCoefficient(lightingData.F0, lightingData.F90, lightingData.NoVSat)); -} - +#if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND + void CCSurfaceLightingCalculateDirectSubLayerFresnel(out vec3 subLayerF, in LightingIntermediateData lightingData, vec3 specularColor) + { + float VoH = lightingData.VoHAbsSat; + subLayerF = vec3(exp2( (-5.55473 * VoH - 6.98316) * VoH )); + } + void CCSurfaceLightingCalculateEnvironmentSubLayerFresnel(out vec3 subLayerF, in LightingIntermediateData lightingData, vec3 specularColor) + { + // ior related: + subLayerF = vec3(CalculateFresnelCoefficient(lightingData.ior, lightingData.NoVSat)); + // F0 F90 related: + // if (equalf(lightingData.ior, 0.0)) + // subLayerF = vec3(CalculateFresnelCoefficient(lightingData.F0, lightingData.F90, lightingData.NoVSat)); + } +#endif #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR // do not support anisotropy