diff --git a/editor/assets/chunks/builtin/functionalities/sh.chunk b/editor/assets/chunks/builtin/functionalities/sh.chunk index 952763b3fe..7430c4939f 100644 --- a/editor/assets/chunks/builtin/functionalities/sh.chunk +++ b/editor/assets/chunks/builtin/functionalities/sh.chunk @@ -3,17 +3,16 @@ #if CC_USE_LIGHT_PROBE vec3 SHEvaluate(vec3 normal) { -#if USE_INSTANCING vec3 result; + +#if USE_INSTANCING // calculate linear and const terms vec4 normal4 = vec4(normal, 1.0); result.r = dot(v_sh_linear_const_r, normal4); result.g = dot(v_sh_linear_const_g, normal4); result.b = dot(v_sh_linear_const_b, normal4); - return result; #else - vec3 result; // calculate linear and const terms vec4 normal4 = vec4(normal, 1.0); result.r = dot(cc_sh_linear_const_r, normal4); @@ -28,8 +27,14 @@ vec3 SHEvaluate(vec3 normal) result.g += dot(cc_sh_quadratic_g, n14); result.b += dot(cc_sh_quadratic_b, n14); result += (cc_sh_quadratic_a.rgb * n5); - - return result; #endif + + #if CC_USE_HDR + // convert from standard camera exposure parameters to current exposure value + // baked in LDR scene still regarded as exposured with standard camera parameters + result *= cc_exposure.w * cc_exposure.x; + #endif + + return result; } #endif diff --git a/editor/assets/chunks/common/debug/debug-view-define.chunk b/editor/assets/chunks/common/debug/debug-view-define.chunk index 701aa143ea..8b60c4819c 100644 --- a/editor/assets/chunks/common/debug/debug-view-define.chunk +++ b/editor/assets/chunks/common/debug/debug-view-define.chunk @@ -22,6 +22,8 @@ #define CC_SURFACES_DEBUG_VIEW_METALLIC CC_SURFACES_DEBUG_VIEW_TRANSPARENCY + 1 #define CC_SURFACES_DEBUG_VIEW_ROUGHNESS CC_SURFACES_DEBUG_VIEW_METALLIC + 1 #define CC_SURFACES_DEBUG_VIEW_SPECULAR_INTENSITY CC_SURFACES_DEBUG_VIEW_ROUGHNESS + 1 +//todo +#define CC_SURFACES_DEBUG_VIEW_IOR CC_SURFACES_DEBUG_VIEW_SPECULAR_INTENSITY + 1000 // lighting data #define CC_SURFACES_DEBUG_VIEW_DIRECT_DIFFUSE CC_SURFACES_DEBUG_VIEW_SPECULAR_INTENSITY + 1 @@ -35,6 +37,15 @@ #define CC_SURFACES_DEBUG_VIEW_SHADOW CC_SURFACES_DEBUG_VIEW_LIGHT_MAP + 1 #define CC_SURFACES_DEBUG_VIEW_AO CC_SURFACES_DEBUG_VIEW_SHADOW + 1 +//todo +#define CC_SURFACES_DEBUG_VIEW_FRESNEL CC_SURFACES_DEBUG_VIEW_AO + 1000 +#define CC_SURFACES_DEBUG_VIEW_TRANSMIT_DIRECT_DIFFUSE CC_SURFACES_DEBUG_VIEW_FRESNEL + 1 +#define CC_SURFACES_DEBUG_VIEW_TRANSMIT_DIRECT_SPECULAR CC_SURFACES_DEBUG_VIEW_TRANSMIT_DIRECT_DIFFUSE + 1 +#define CC_SURFACES_DEBUG_VIEW_TRANSMIT_ENV_DIFFUSE CC_SURFACES_DEBUG_VIEW_TRANSMIT_DIRECT_SPECULAR + 1 +#define CC_SURFACES_DEBUG_VIEW_TRANSMIT_ENV_SPECULAR CC_SURFACES_DEBUG_VIEW_TRANSMIT_ENV_DIFFUSE + 1 +#define CC_SURFACES_DEBUG_VIEW_TRT_DIRECT CC_SURFACES_DEBUG_VIEW_TRANSMIT_ENV_SPECULAR + 1 +#define CC_SURFACES_DEBUG_VIEW_TRT_ENVIRONMENT CC_SURFACES_DEBUG_VIEW_TRT_DIRECT + 1 + #define CC_SURFACES_DEBUG_VIEW_FOG CC_SURFACES_DEBUG_VIEW_AO + 1 // Debug view UI and macros diff --git a/editor/assets/chunks/common/lighting/functions.chunk b/editor/assets/chunks/common/lighting/functions.chunk index 9b98ea2707..3619208527 100644 --- a/editor/assets/chunks/common/lighting/functions.chunk +++ b/editor/assets/chunks/common/lighting/functions.chunk @@ -43,3 +43,28 @@ vec3 CalculateReflectDirection(vec3 N, vec3 V, float NoV) N *= sideSign; return reflect(-V, N); } + +// for bumped planar reflection +vec3 CalculatePlanarReflectPositionOnPlane(vec3 N, vec3 V, vec3 worldPos, vec4 plane, vec3 cameraPos, float probeReflectedDepth) +{ + float distPixelToPlane = -dot(plane, vec4(worldPos, 1.0)); + // bring plane to worldPos, avoid artifacts when reflected point away from plane (do not bring worldPos to plane) + plane.w += distPixelToPlane; + float distCameraToPlane = abs(-dot(plane, vec4(cameraPos, 1.0))); + vec3 planeN = plane.xyz; + vec3 virtualCameraPos = cameraPos - 2.0 * distCameraToPlane * planeN; + /*support for two-sided reflections + float sideSignPlaneN = dot(planeN, V) < 0.0 ? -1.0 : 1.0; + float sideSignN = dot(N, V) < 0.0 ? -1.0 : 1.0; + planeN *= sideSignPlaneN; + N *= sideSignN;*/ + vec3 bumpedR = normalize(reflect(-V, N)); //R' + + // actually reflected pos alone bumpedR direction, avoid tracing by specified a fake depth + vec3 reflectedPointPos = worldPos + probeReflectedDepth * bumpedR; + vec3 virtualCameraToReflectedPoint = normalize(reflectedPointPos - virtualCameraPos); + + // the ray from virtual camera to reflected point, will intersect with plane on P' + float y = distCameraToPlane / max(EPSILON_LOWP, dot(planeN, virtualCameraToReflectedPoint)); + return virtualCameraPos + y * virtualCameraToReflectedPoint; +} diff --git a/editor/assets/chunks/common/mesh/vat-animation.chunk b/editor/assets/chunks/common/mesh/vat-animation.chunk index 040f86d5e3..3d59a7b503 100644 --- a/editor/assets/chunks/common/mesh/vat-animation.chunk +++ b/editor/assets/chunks/common/mesh/vat-animation.chunk @@ -59,8 +59,33 @@ float VATGetAnimUV(out vec2 thisFrameUV, out vec2 nextFrameUV, vec2 meshUV, floa return frameLerp; } +void VATGetLocalPositionRigidBody(inout vec3 meshLocalPos, inout vec3 meshLocalNormal, inout vec3 meshLocalTangent, vec3 vatBoundingBoxMin, vec3 vatBoundingBoxMax, vec2 thisFrameUV, sampler2D vatColorTexture, sampler2D vatPositionTexture, sampler2D vatPositionSignTexture, sampler2D vatRotationTexture, sampler2D vatRotationSignTexture, sampler2D vatRotationAlphaTexture) +{ + // bounding box in exported json is unscaled + vatBoundingBoxMin *= VAT_UNITY_TO_COCOS_COORDINATE_SCALE; + vatBoundingBoxMax *= VAT_UNITY_TO_COCOS_COORDINATE_SCALE; + vec3 size = vatBoundingBoxMax - vatBoundingBoxMin; + + // pivot + vec3 pivot = SampleTextureExr(vatColorTexture, thisFrameUV).xzy * VAT_UNITY_TO_COCOS_COORDINATE_SCALE; + pivot = pivot * size + vatBoundingBoxMin; + + pivot = vec3(0.0); + + vec3 originPos = meshLocalPos - pivot; + + vec4 quat = SampleTextureExrWithAlpha(vatRotationTexture, vatRotationSignTexture, vatRotationAlphaTexture, thisFrameUV); + // quat = quat * 2.0 - vec4(1.0); + vec3 rotatedPos = originPos; + // rotateVecFromQuat(rotatedPos, quat); + rotatedPos = originPos + 2.0 * cross(quat.xyz, cross(quat.xyz, originPos) + quat.w * originPos); + + vec3 animateOffset = SampleTextureExr(vatPositionTexture, vatPositionSignTexture, thisFrameUV) * VAT_UNITY_TO_COCOS_COORDINATE_SCALE; + meshLocalPos += rotatedPos + animateOffset * vec3(-1.0, 1.0, 1.0); +} + // return absolute position for fluid -// return position offset for rigid-body and soft-body +// return position offset for soft-body vec3 VATGetLocalPosition(vec2 thisFrameUV, sampler2D vatPositionTexture, sampler2D vatPositionSignTexture) { vec3 thisFramePos = SampleTextureExr(vatPositionTexture, vatPositionSignTexture, thisFrameUV); diff --git a/editor/assets/chunks/common/texture/texture-misc.chunk b/editor/assets/chunks/common/texture/texture-misc.chunk index 8c401792ec..acf1bb6ed8 100644 --- a/editor/assets/chunks/common/texture/texture-misc.chunk +++ b/editor/assets/chunks/common/texture/texture-misc.chunk @@ -1,5 +1,11 @@ // for exr data texture and sub resources +vec3 SampleTextureExr(sampler2D exrRGBE, vec2 uv) +{ + vec3 data = unpackRGBE(texture(exrRGBE, uv)); + return data; +} + vec3 SampleTextureExr(sampler2D exrRGBE, sampler2D exrSign, vec2 uv) { vec3 data = unpackRGBE(texture(exrRGBE, uv)); 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 de2424b16d..5b777fbc61 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 @@ -21,16 +21,15 @@ struct LightingIntermediateData float anisotropyShape; #endif +#if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR || CC_SURFACES_LIGHTING_TRANSMITTENCE + vec4 inScatteringColorAndIntensity; + vec4 transmitParams; //x: extinction coef y: transmit distance(water depth, leaf thickness) zw: scatter parameters for in-scattering +#endif #if CC_SURFACES_LIGHTING_TRANSMITTENCE vec4 shadowPosAndDepth; //xy: shadowmap uv, z: camera depth from pixel LVP, w: camera depth from shadowmap - vec4 transmittenceParams; //zw: min/max DepthWS for shadowmap and 0 means disable shadow transmit, x: transmit coef, y: additional transmit depth mask, 可补充shadowmap cover不到的地方,也可以在minmax屏蔽掉shadowmap之后给专门的薄片穿透 + vec4 transmittenceParams; //zw: min/max DepthWS for shadowmap transmit and 0 means disable shadow transmit, x: transmit irradiance apply shadow, y: additional transmit depth mask, 可补充shadowmap cover不到的地方,也可以在minmax屏蔽掉shadowmap之后给专门的薄片穿透 #endif - // #if CC_SURFACES_LIGHTING_SPECULAR_2ND - // float specularParam2nd, specularIntensity2nd, specularRotation2nd; // for hair - // vec3 specularColor2nd; - // #endif // #if CC_SURFACES_LIGHTING_CLEAR_COAT - // float ior; // float roughnessClearCoat; // for clear coat or multi-layer pbr // #endif }; @@ -61,6 +60,10 @@ void CCSurfacesLightingGetIntermediateData_PerPixel(inout LightingIntermediateDa #if CC_SURFACES_LIGHTING_ANISOTROPIC data.anisotropyShape = anisotropyShape; #endif +#if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR || CC_SURFACES_LIGHTING_TRANSMITTENCE + data.inScatteringColorAndIntensity = vec4(0.0); + data.transmitParams = vec4(0.0); +#endif #if CC_SURFACES_LIGHTING_TRANSMITTENCE data.shadowPosAndDepth = vec4(0.0, 0.0, 999999.0, 999999.0); #endif 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 34b9ca0726..42115ec163 100644 --- a/editor/assets/chunks/lighting-models/data-structures/lighting-result.chunk +++ b/editor/assets/chunks/lighting-models/data-structures/lighting-result.chunk @@ -9,15 +9,15 @@ struct LightingResult float shadow, ao; vec3 lightmapColor; vec3 emissive; -#if CC_SURFACES_LIGHTING_USE_FRESNEL + float fresnel; -#endif + #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR vec3 directTransmitSpecular; vec3 environmentTransmitSpecular; #endif #if CC_SURFACES_LIGHTING_TRANSMITTENCE - vec3 transmittence; + vec3 directTransmittence, environmentTransmittence; #endif #if CC_SURFACES_LIGHTING_TRT vec3 directTRT, environmentTRT; 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 d20d88e9a4..effc507aef 100644 --- a/editor/assets/chunks/lighting-models/lighting-flow/common-flow.chunk +++ b/editor/assets/chunks/lighting-models/lighting-flow/common-flow.chunk @@ -5,13 +5,13 @@ vec3 worldPos; HIGHP_VALUE_FROM_STRUCT_DEFINED(worldPos, surfaceData.worldPos); + CCSurfacesInitializeLightingResult(lightingResult); + LightingIntermediateData lightingData; CCSurfacesInitializeLightingIntermediateData(lightingData, surfaceData); CCSurfacesLightingInitializeColorWithLighting(lightingResult.diffuseColorWithLighting, lightingResult.specularColorWithLighting, surfaceData, lightingData); - lightingResult.directDiffuse = lightingResult.directSpecular = vec3(0.0); - int numLights = CC_PIPELINE_TYPE == CC_PIPELINE_TYPE_FORWARD ? LIGHTS_PER_PASS : int(cc_lightDir[0].w); for (int i = 0; i < LIGHTS_PER_PASS; i++) { if (i >= numLights) break; @@ -24,13 +24,9 @@ float fresnel = 1.0; #if CC_SURFACES_LIGHTING_USE_FRESNEL - fresnel = lightingResult.fresnel = CCSurfaceLightingCalculateFresnel(lightingData); - #endif - - #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR - vec3 transmitSpecularLighting; - CCSurfacesLightingCalculateDirectTransmitSpecular(transmitSpecularLighting, lightingData, cc_lightColor[i]); + fresnel = CCSurfaceLightingCalculateFresnel(lightingData); #endif + lightingResult.fresnel = fresnel; float shadow = 1.0; #if CC_RECEIVE_SHADOW && CC_SHADOW_TYPE == CC_SHADOW_MAP @@ -41,9 +37,13 @@ shadow = CCSpotShadowFactorBase(shadowNDCPosWithBias, shadowPos, worldPos, shadowBias); } #if CC_SURFACES_LIGHTING_TRANSMITTENCE - lightingData.shadowPosAndDepth.xy = shadowNDCPosWithBias.xy; - lightingData.shadowPosAndDepth.z = shadowPos.z; - lightingData.shadowPosAndDepth.w = GetViewSpaceDepthFromNDCDepth_Perspective(SampleShadowMap(shadowNDCPosWithBias.xyz, cc_spotShadowMap), shadowNDCPosWithBias.w, cc_shadowInvProjDepthInfo.x, cc_shadowInvProjDepthInfo.y); + #if CC_SURFACES_LIGHTING_USE_SHADOWMAP_TRANSMITTENCE + lightingData.shadowPosAndDepth.xy = shadowNDCPosWithBias.xy; + lightingData.shadowPosAndDepth.z = shadowPos.z; + lightingData.shadowPosAndDepth.w = GetViewSpaceDepthFromNDCDepth_Perspective(SampleShadowMap(shadowNDCPosWithBias.xyz, cc_spotShadowMap), shadowNDCPosWithBias.w, cc_shadowInvProjDepthInfo.x, cc_shadowInvProjDepthInfo.y); + #else + lightingData.transmittenceParams.zw = vec2(0.0); + #endif #endif } #endif @@ -67,13 +67,23 @@ lightingResult.directSpecular += specularLighting * multiplier * fresnel; #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR + vec3 transmitSpecularLighting; + CCSurfacesLightingCalculateDirectTransmitSpecular(transmitSpecularLighting, lightingData, cc_lightColor[i]); lightingResult.directTransmitSpecular += transmitSpecularLighting * multiplier * (1.0 - fresnel); #endif + #if CC_SURFACES_LIGHTING_TRANSMITTENCE - lightingResult.transmittence = CCSurfacesLightingCalculateDirectTransmittence(lightingResult, lightingData, cc_lightColor[i]); + vec3 transmittence; + CCSurfacesLightingCalculateDirectTransmittence(transmittence, lightingResult, lightingData, cc_lightColor[i]); + lightingResult.directTransmittence += transmittence; #endif + #if CC_SURFACES_LIGHTING_TRT - CCSurfacesLightingCalculateDirectTRT(lightingResult.directTRT, lightingData, cc_lightColor[i]); + vec3 TRTLighting; + LightingIntermediateData lightingDataTRT; + CCSurfacesGetLightingIntermediateDataTRT(lightingDataTRT, lightingData, surfaceData); + CCSurfacesLightingCalculateDirectTRT(TRTLighting, lightingDataTRT, cc_lightColor[i]); + lightingResult.directTRT += TRTLighting; #endif } } @@ -106,9 +116,13 @@ } } #if CC_SURFACES_LIGHTING_TRANSMITTENCE - lightingData.shadowPosAndDepth.xy = shadowNDCPosWithBias.xy; - lightingData.shadowPosAndDepth.z = shadowPos.z; - lightingData.shadowPosAndDepth.w = GetViewSpaceDepthFromNDCDepth_Orthgraphic(SampleShadowMap(shadowNDCPosWithBias.xyz, cc_shadowMap), cc_shadowProjDepthInfo.x, cc_shadowProjDepthInfo.y); + #if CC_SURFACES_LIGHTING_USE_SHADOWMAP_TRANSMITTENCE + lightingData.shadowPosAndDepth.xy = shadowNDCPosWithBias.xy; + lightingData.shadowPosAndDepth.z = shadowPos.z; + lightingData.shadowPosAndDepth.w = GetViewSpaceDepthFromNDCDepth_Orthgraphic(SampleShadowMap(shadowNDCPosWithBias.xyz, cc_shadowMap), cc_shadowProjDepthInfo.x, cc_shadowProjDepthInfo.y); + #else + lightingData.transmittenceParams.zw = vec2(0.0); + #endif #endif } #endif @@ -138,6 +152,8 @@ #if CC_SURFACES_LIGHTING_USE_FRESNEL lightingResult.fresnel = CCSurfaceLightingCalculateFresnel(lightingData); + #else + lightingResult.fresnel = 1.0; #endif #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR @@ -146,11 +162,11 @@ #endif #if CC_SURFACES_LIGHTING_TRANSMITTENCE - lightingResult.transmittence = CCSurfacesLightingCalculateDirectTransmittence(lightingResult, lightingData, cc_mainLitColor); - lightingResult.transmittence += CCSurfacesLightingCalculateEnvironmentTransmittence(lightingResult, lightingData, cc_ambientSky.w); + CCSurfacesLightingCalculateDirectTransmittence(lightingResult.directTransmittence, lightingResult, lightingData, cc_mainLitColor); + CCSurfacesLightingCalculateEnvironmentTransmittence(lightingResult.environmentTransmittence, lightingResult, lightingData, cc_ambientSky.w); #endif #if CC_SURFACES_LIGHTING_TRT - LightingIntermediateData lightingDataTRT; + LightingIntermediateData lightingDataTRT; CCSurfacesGetLightingIntermediateDataTRT(lightingDataTRT, lightingData, surfaceData); CCSurfacesLightingCalculateDirectTRT(lightingResult.directTRT, lightingDataTRT, cc_mainLitColor); CCSurfacesLightingCalculateEnvironmentTRT(lightingResult.environmentTRT, lightingDataTRT, cc_ambientSky.w); diff --git a/editor/assets/chunks/lighting-models/model-functions/standard.chunk b/editor/assets/chunks/lighting-models/model-functions/standard.chunk index a1bfba152f..37e817c6d2 100644 --- a/editor/assets/chunks/lighting-models/model-functions/standard.chunk +++ b/editor/assets/chunks/lighting-models/model-functions/standard.chunk @@ -38,7 +38,7 @@ void CCSurfacesLightingCalculateEnvironment(out vec3 lightingDiffuse, out vec3 l #endif #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR - // do not support planar reflection and anisotropy + // do not support anisotropy void CCSurfacesLightingCalculateDirectTransmitSpecular(out vec3 lightingSpecular, in LightingIntermediateData lightingData, in vec4 lightSourceColorAndIntensity) { float roughness = lightingData.specularParam; @@ -56,37 +56,47 @@ void CCSurfacesLightingCalculateEnvironment(out vec3 lightingDiffuse, out vec3 l vec3 R = CalculateRefractDirection(lightingData.N, lightingData.V, lightingData.NoV, lightingData.ior); float roughness = lightingData.specularParam; - #if USE_REFLECTION_PROBE - #if USE_REFLECTION_PROBE == REFLECTION_PROBE_TYPE_CUBE + #if CC_USE_REFLECTION_PROBE + #if CC_USE_REFLECTION_PROBE == REFLECTION_PROBE_TYPE_CUBE envSpec = EnvReflection(cc_reflectionProbeCubemap, R, roughness, cc_ambientGround.w); #endif //todo: planar refraction from scene color #endif - #if CC_USE_IBL && USE_REFLECTION_PROBE != REFLECTION_PROBE_TYPE_CUBE + #if CC_USE_IBL && CC_USE_REFLECTION_PROBE != REFLECTION_PROBE_TYPE_CUBE envSpec = EnvReflection(cc_environment, R, roughness, cc_ambientGround.w); #endif lightingSpecular = envSpec * lightIntensity; + + // extinction and in-scattering + float extinction = lightingData.transmitParams.x, depth = lightingData.transmitParams.y; + float outScattered = exp(-extinction * depth); + lightingSpecular = lightingSpecular * outScattered; + float inScatterIntensity = lightingData.transmitParams.z; + lightingSpecular += (1.0 - outScattered) * inScatterIntensity * lightingData.inScatteringColorAndIntensity.xyz * lightingData.inScatteringColorAndIntensity.w; } #endif #if CC_SURFACES_LIGHTING_TRANSMITTENCE - vec3 CCSurfacesLightingCalculateDirectTransmittence(in LightingResult lightingResult, in LightingIntermediateData lightingData, in vec4 lightSourceColorAndIntensity) + void CCSurfacesLightingCalculateDirectTransmittence(out vec3 transmittence, in LightingResult lightingResult, in LightingIntermediateData lightingData, in vec4 lightSourceColorAndIntensity) { vec3 backIrradiance = vec3(saturate(-lightingData.NoL)) * lightSourceColorAndIntensity.rgb * lightSourceColorAndIntensity.w; vec3 transmitDiffuse = backIrradiance * DiffuseCoefficient_EnergyConservation; //todo: add some + lightingData.transmitParams; lightingData.transmittenceParams; - return vec3(0.0); + transmittence = vec3(0.0); } - vec3 CCSurfacesLightingCalculateEnvironmentTransmittence(in LightingResult lightingResult, in LightingIntermediateData lightingData, float lightIntensity) + void CCSurfacesLightingCalculateEnvironmentTransmittence(out vec3 transmittence, in LightingResult lightingResult, in LightingIntermediateData lightingData, float lightIntensity) { // negativate lightingData.N before invoking CalculateEnvironmentDiffuse lightingData.N *= -1.0; vec3 backIrradiance = CalculateEnvironmentDiffuse(lightingData, lightIntensity); + lightingData.N *= -1.0; + lightingData.transmitParams; lightingData.transmittenceParams; - return vec3(0.0); + transmittence = vec3(0.0); //backIrradiance * lightingResult.ao; } #endif @@ -100,25 +110,26 @@ void CCSurfacesLightingCalculateEnvironment(out vec3 lightingDiffuse, out vec3 l } #endif - void CCSurfacesLightingCalculateDirectTRT(out vec3 TRTColor, in LightingIntermediateData lightingData, in vec4 lightSourceColorAndIntensity) + void CCSurfacesLightingCalculateDirectTRT(out vec3 TRTLighting, in LightingIntermediateData lightingData, in vec4 lightSourceColorAndIntensity) { vec3 unused; // light color has less influence - CCSurfacesLightingCalculateDirect(unused, TRTColor, lightingData, vec4(1.0)); - vec3 Color = SurfacesLightingGetTRTSpecularColor(TRTColor.x, true); + CCSurfacesLightingCalculateDirect(unused, TRTLighting, lightingData, vec4(1.0)); + float brdf = TRTLighting.x; + vec3 Color = SurfacesLightingGetTRTSpecularColor(brdf, true); // todo: modify Color to add some light color influence - TRTColor *= Color * lightSourceColorAndIntensity.w; + TRTLighting *= Color * lightSourceColorAndIntensity.w; } - void CCSurfacesLightingCalculateEnvironmentTRT(out vec3 TRTColor, in LightingIntermediateData lightingData, float lightIntensity) + void CCSurfacesLightingCalculateEnvironmentTRT(out vec3 TRTLighting, in LightingIntermediateData lightingData, float lightIntensity) { vec3 unused; // light color has less influence - TRTColor = CalculateEnvironmentSpecular(lightingData, 1.0); - float brdf = length(TRTColor); + TRTLighting = CalculateEnvironmentSpecular(lightingData, 1.0); + float brdf = length(TRTLighting); vec3 Color = SurfacesLightingGetTRTSpecularColor(brdf, false); // todo: modify Color to add some light color influence - TRTColor *= Color * lightIntensity; + TRTLighting *= Color * lightIntensity; } #endif diff --git a/editor/assets/chunks/shading-entries/main-functions/render-to-scene/vs.chunk b/editor/assets/chunks/shading-entries/main-functions/render-to-scene/vs.chunk index eefaaaf9a6..392f91b91a 100644 --- a/editor/assets/chunks/shading-entries/main-functions/render-to-scene/vs.chunk +++ b/editor/assets/chunks/shading-entries/main-functions/render-to-scene/vs.chunk @@ -9,7 +9,8 @@ void main() In.normal.xyz = SurfacesVertexModifyLocalNormal(In); #if CC_SURFACES_USE_TANGENT_SPACE In.tangent = SurfacesVertexModifyLocalTangent(In); - #endif + #endif + SurfacesVertexModifyLocalSharedData(In); // World Space CCSurfacesVertexWorldTransform(In); @@ -20,13 +21,13 @@ void main() In.clipPos = SurfacesVertexModifyClipPos(In); // Other Surfaces Function - SurfacesVertexModifyUV(In); - SurfacesVertexModifySharedData(In); - vec3 viewDirect = normalize(cc_cameraPos.xyz - In.worldPos); In.worldNormal.w = dot(In.worldNormal.xyz, viewDirect) < 0.0 ? -1.0 : 1.0; In.worldNormal.xyz = SurfacesVertexModifyWorldNormal(In); + SurfacesVertexModifyUV(In); + SurfacesVertexModifySharedData(In); + // Other Data CCSurfacesVertexTransformUV(In); CCSurfacesVertexTransferFog(In); diff --git a/editor/assets/chunks/shading-entries/main-functions/render-to-shadowmap/vs.chunk b/editor/assets/chunks/shading-entries/main-functions/render-to-shadowmap/vs.chunk index 9b17e812bd..cecd57a617 100644 --- a/editor/assets/chunks/shading-entries/main-functions/render-to-shadowmap/vs.chunk +++ b/editor/assets/chunks/shading-entries/main-functions/render-to-shadowmap/vs.chunk @@ -11,6 +11,7 @@ void main() CCSurfacesVertexInput(In); CCSurfacesVertexAnimation(In); In.position.xyz = SurfacesVertexModifyLocalPos(In); + SurfacesVertexModifyLocalSharedData(In); // World Space CCSurfacesVertexWorldTransform(In); diff --git a/editor/assets/chunks/surfaces/data-structures/standard.chunk b/editor/assets/chunks/surfaces/data-structures/standard.chunk index 0584bf453f..b9cbf3f61d 100644 --- a/editor/assets/chunks/surfaces/data-structures/standard.chunk +++ b/editor/assets/chunks/surfaces/data-structures/standard.chunk @@ -19,6 +19,10 @@ struct SurfacesMaterialData float anisotropyShape; #endif +#if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR || CC_SURFACES_LIGHTING_TRANSMITTENCE + vec4 inScatteringColorAndIntensity; + vec4 transmitParams; +#endif #if CC_SURFACES_LIGHTING_TRANSMITTENCE vec4 transmittenceParams; #endif diff --git a/editor/assets/chunks/surfaces/default-functions/common-vs.chunk b/editor/assets/chunks/surfaces/default-functions/common-vs.chunk index 5f1118a99e..794b3dfeac 100644 --- a/editor/assets/chunks/surfaces/default-functions/common-vs.chunk +++ b/editor/assets/chunks/surfaces/default-functions/common-vs.chunk @@ -21,6 +21,15 @@ vec3 SurfacesVertexModifyLocalNormal(in SurfacesStandardVertexIntermediate In) #endif #endif +// some vertex datas use shared raw data, avoid sample / calculate same raw data multiply times, use this function for better performance +// this function invokes before world transform +#ifndef CC_SURFACES_VERTEX_MODIFY_LOCAL_SHARED_DATA +void SurfacesVertexModifyLocalSharedData(inout SurfacesStandardVertexIntermediate In) +{ +} +#endif + + #ifndef CC_SURFACES_VERTEX_MODIFY_WORLD_POS vec3 SurfacesVertexModifyWorldPos(in SurfacesStandardVertexIntermediate In) { diff --git a/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk b/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk index 70fe9b108b..b257e8cc72 100644 --- a/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk +++ b/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk @@ -70,10 +70,22 @@ vec4 SurfacesFragmentModifyPBRParams() } #endif -#ifndef CC_SURFACES_FRAGMENT_MODIFY_TRANSMITTENCE_PARAMS -vec4 SurfacesFragmentModifytransmittenceParamss() +#ifndef CC_SURFACES_FRAGMENT_MODIFY_TRANSMIT_PARAMS +void SurfacesFragmentModifyTransmitParams(out vec4 transmitParams, out vec4 inScatteringColorAndIntensity) { - // x: exponential transmit coef + // x: extinction coef + // y: transmit distance(water depth, leaf thickness) + // zw: scatter params for in-scattering + transmitParams = vec4(1.0, 0.0, 0.0, 0.0); + + inScatteringColorAndIntensity = vec4(0.0, 0.0, 0.0, 0.0); +} +#endif + +#ifndef CC_SURFACES_FRAGMENT_MODIFY_TRANSMITTENCE_PARAMS +vec4 SurfacesFragmentModifyTransmittenceParams() +{ + // x: transmit irradiance apply shadow // y: additional transmit depth mask, add light transport for area which cannot be covered by shadowmap transmittence, or stand for thin transmittence // zw: min/max DepthWS for shadowmap, 0 means disable shadow transmit return vec4(1.0, 0.0, 0.0, 0.0); diff --git a/editor/assets/chunks/surfaces/effect-macros/common-macros.chunk b/editor/assets/chunks/surfaces/effect-macros/common-macros.chunk index 217fc24906..d1334755cd 100644 --- a/editor/assets/chunks/surfaces/effect-macros/common-macros.chunk +++ b/editor/assets/chunks/surfaces/effect-macros/common-macros.chunk @@ -59,6 +59,9 @@ #ifndef CC_SURFACES_LIGHTING_TRANSMITTENCE #define CC_SURFACES_LIGHTING_TRANSMITTENCE 0 #endif +#ifndef CC_SURFACES_LIGHTING_USE_SHADOWMAP_TRANSMITTENCE + #define CC_SURFACES_LIGHTING_USE_SHADOWMAP_TRANSMITTENCE 0 +#endif #ifndef CC_SURFACES_LIGHTING_TRT #define CC_SURFACES_LIGHTING_TRT 0 #endif diff --git a/editor/assets/chunks/surfaces/module-functions/debug-view.chunk b/editor/assets/chunks/surfaces/module-functions/debug-view.chunk index 1aff7c1717..1538ae4bcd 100644 --- a/editor/assets/chunks/surfaces/module-functions/debug-view.chunk +++ b/editor/assets/chunks/surfaces/module-functions/debug-view.chunk @@ -100,68 +100,114 @@ vec4 CCSurfacesDebugDisplayInvalidInputData(vec4 color, vec3 data) #if CC_USE_DEBUG_VIEW == CC_SURFACES_DEBUG_VIEW_SINGLE bool CCSurfacesDebugViewLightingResult(inout vec4 color, in LightingResult lightingResult) { - bool isDebugMatched = false; + // whether need tone mapping & LinearToSRGB + bool isSRGBColor = false; if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_DIRECT_DIFFUSE)) { color.rgb = lightingResult.directDiffuse * lightingResult.diffuseColorWithLighting; - isDebugMatched = true; + isSRGBColor = true; } if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_DIRECT_SPECULAR)) { color.rgb = lightingResult.directSpecular * lightingResult.specularColorWithLighting; - isDebugMatched = true; + isSRGBColor = true; } if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_DIRECT_ALL)) { color.rgb = lightingResult.directDiffuse * lightingResult.diffuseColorWithLighting + lightingResult.directSpecular * lightingResult.specularColorWithLighting; - isDebugMatched = true; + isSRGBColor = true; } if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_ENV_DIFFUSE)) { color.rgb = lightingResult.environmentDiffuse * lightingResult.diffuseColorWithLighting; - isDebugMatched = true; + isSRGBColor = true; } if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_ENV_SPECULAR)) { color.rgb = lightingResult.environmentSpecular * lightingResult.specularColorWithLighting; - isDebugMatched = true; + isSRGBColor = true; } if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_ENV_ALL)) { color.rgb = lightingResult.environmentDiffuse * lightingResult.diffuseColorWithLighting + lightingResult.environmentSpecular * lightingResult.specularColorWithLighting; - isDebugMatched = true; + isSRGBColor = true; } if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_LIGHT_MAP)) { color.rgb = lightingResult.lightmapColor; - isDebugMatched = true; + isSRGBColor = true; } if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_EMISSIVE)) { color.rgb = lightingResult.emissive; - isDebugMatched = true; + isSRGBColor = true; } if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_AO)) { color.rgb = vec3(lightingResult.ao); - isDebugMatched = true; + isSRGBColor = false; } if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_SHADOW)) { color.rgb = vec3(lightingResult.shadow); - isDebugMatched = true; + isSRGBColor = false; } - return isDebugMatched; + if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_FRESNEL)) + { + color.rgb = vec3(lightingResult.fresnel); + isSRGBColor = false; + } + + #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR + if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_TRANSMIT_DIRECT_SPECULAR)) + { + color.rgb = lightingResult.directTransmitSpecular * lightingResult.specularColorWithLighting; + isSRGBColor = true; + } + if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_TRANSMIT_ENV_SPECULAR)) + { + color.rgb = lightingResult.environmentTransmitSpecular * lightingResult.specularColorWithLighting; + isSRGBColor = true; + } + #endif + + #if CC_SURFACES_LIGHTING_TRANSMITTENCE + if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_TRANSMIT_DIRECT_DIFFUSE)) + { + color.rgb = lightingResult.directTransmittence; + isSRGBColor = true; + } + if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_TRANSMIT_ENV_DIFFUSE)) + { + color.rgb = lightingResult.environmentTransmittence; + isSRGBColor = true; + } + #endif + + #if CC_SURFACES_LIGHTING_TRT + if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_TRT_DIRECT)) + { + color.rgb = lightingResult.directTRT * lightingResult.specularColorWithLighting; + isSRGBColor = true; + } + if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_TRT_ENVIRONMENT)) + { + color.rgb = lightingResult.environmentTRT * lightingResult.specularColorWithLighting; + isSRGBColor = true; + } + #endif + + return isSRGBColor; } #endif diff --git a/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk b/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk index 950d2fdba5..ce01e23a22 100644 --- a/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk +++ b/editor/assets/chunks/surfaces/module-functions/standard-fs.chunk @@ -34,8 +34,11 @@ void CCSurfacesFragmentGetMaterialData(inout SurfacesMaterialData surfaceData) surfaceData.metallic = pbr.z; surfaceData.specularIntensity = pbr.w; +#if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR || CC_SURFACES_LIGHTING_TRANSMITTENCE + SurfacesFragmentModifyTransmitParams(surfaceData.transmitParams, surfaceData.inScatteringColorAndIntensity); +#endif #if CC_SURFACES_LIGHTING_TRANSMITTENCE - surfaceData.transmittenceParams =SurfacesFragmentModifytransmittenceParamss(); + surfaceData.transmittenceParams = SurfacesFragmentModifyTransmittenceParams(); #endif #if CC_SURFACES_LIGHTING_TRT vec4 trt = SurfacesFragmentModifyTRTParams(); @@ -94,13 +97,18 @@ void CCSurfacesInitializeLightingIntermediateData(inout LightingIntermediateData ); lightingData.specularParam = surfaceData.roughness; lightingData.ior = surfaceData.ior; + +#if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR || CC_SURFACES_LIGHTING_TRANSMITTENCE + lightingData.inScatteringColorAndIntensity = surfaceData.inScatteringColorAndIntensity; + lightingData.transmitParams = surfaceData.transmitParams; +#endif +#if CC_SURFACES_LIGHTING_TRANSMITTENCE + lightingData.transmittenceParams = surfaceData.transmittenceParams; +#endif } void CCSurfacesLightingCalculateIntermediateData_PerLight(inout LightingIntermediateData lightingData, in SurfacesMaterialData surfaceData, vec3 lightDirWithDist) { CCSurfacesLightingGetIntermediateData_PerLight(lightingData, lightDirWithDist); -#if CC_SURFACES_LIGHTING_TRANSMITTENCE - lightingData.transmittenceParams = surfaceData.transmittenceParams; -#endif } // pass original lightingData, get TRT lightingData @@ -113,13 +121,29 @@ void CCSurfacesGetLightingIntermediateDataTRT(out LightingIntermediateData light #endif -// Copy material data to lighting results +// Copy material data to lighting results for base pass void CCSurfacesInitializeLightingResult(inout LightingResult lightingResult, in SurfacesMaterialData surfaceData) { lightingResult.ao = surfaceData.ao; lightingResult.emissive = surfaceData.emissive; } +// Init accumulated lighting results for additive pass +void CCSurfacesInitializeLightingResult(inout LightingResult lightingResult) +{ + lightingResult.directDiffuse = lightingResult.directSpecular = vec3(0.0); + +#if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR + lightingResult.directTransmitSpecular = vec3(0.0); +#endif +#if CC_SURFACES_LIGHTING_TRANSMITTENCE + lightingResult.directTransmittence = vec3(0.0); +#endif +#if CC_SURFACES_LIGHTING_TRT + lightingResult.directTRT = vec3(0.0); +#endif +} + #if CC_PIPELINE_TYPE == CC_PIPELINE_TYPE_DEFERRED @@ -153,16 +177,16 @@ 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 - lightingResult.fresnel; + color.xyz += ( lightingResult.directDiffuse * lightingResult.diffuseColorWithLighting - + lightingResult.directSpecular * lightingResult.specularColorWithLighting * fresnel + + lightingResult.directSpecular * lightingResult.specularColorWithLighting * lightingResult.fresnel #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR - + lightingResult.directTransmitSpecular * lightingResult.specularColorWithLighting * (1.0 - fresnel) + + lightingResult.directTransmitSpecular * lightingResult.specularColorWithLighting * invFresnel + #endif + #if CC_SURFACES_LIGHTING_TRANSMITTENCE + + lightingResult.directTransmittence #endif #if CC_SURFACES_LIGHTING_TRT + lightingResult.directTRT * lightingResult.specularColorWithLighting @@ -178,9 +202,12 @@ vec4 CCSurfacesShading(in SurfacesMaterialData surfaceData, in LightingResult li color.xyz += ( lightingResult.environmentDiffuse * lightingResult.diffuseColorWithLighting - + lightingResult.environmentSpecular * lightingResult.specularColorWithLighting * fresnel + + lightingResult.environmentSpecular * lightingResult.specularColorWithLighting * lightingResult.fresnel #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR - + lightingResult.environmentTransmitSpecular * lightingResult.specularColorWithLighting * (1.0 - fresnel) + + lightingResult.environmentTransmitSpecular * lightingResult.specularColorWithLighting * invFresnel + #endif + #if CC_SURFACES_LIGHTING_TRANSMITTENCE + + lightingResult.environmentTransmittence #endif #if CC_SURFACES_LIGHTING_TRT + lightingResult.environmentTRT * lightingResult.specularColorWithLighting @@ -247,6 +274,10 @@ void CCSurfacesDebugViewSurfaceData(inout vec4 color, in SurfacesMaterialData su scalar = surfaceData.specularIntensity; color = vec4(scalar, scalar, scalar, 1.0); } + if (IS_DEBUG_VIEW_SINGLE_MODE(CC_SURFACES_DEBUG_VIEW_IOR)) { + scalar = surfaceData.ior; + color = vec4(scalar, scalar, scalar, 1.0); + } } #endif diff --git a/editor/assets/chunks/surfaces/module-functions/toon-fs.chunk b/editor/assets/chunks/surfaces/module-functions/toon-fs.chunk index d2ebbfa913..7bed9daa3d 100644 --- a/editor/assets/chunks/surfaces/module-functions/toon-fs.chunk +++ b/editor/assets/chunks/surfaces/module-functions/toon-fs.chunk @@ -83,12 +83,18 @@ void CCSurfacesLightingCalculateIntermediateData_PerLight(inout LightingIntermed CCSurfacesLightingGetIntermediateData_PerLight(lightingData, lightDirWithDist); } -// Copy material data to lighting results +// Copy material data to lighting results for base pass void CCSurfacesInitializeLightingResult(inout LightingResult lightingResult, in SurfacesMaterialData surfaceData) { lightingResult.emissive = surfaceData.emissive; } +// Init accumulated lighting results for additive pass +void CCSurfacesInitializeLightingResult(inout LightingResult lightingResult) +{ + lightingResult.directDiffuse = lightingResult.directSpecular = vec3(0.0); +} + // Lighting #if CC_PIPELINE_TYPE == CC_PIPELINE_TYPE_DEFERRED vec4 CCSurfacesDeferredOutput0(in SurfacesMaterialData surfaceData)