From 534693ea708dfcaf8c21f94d0f2fb28e9f0e4def Mon Sep 17 00:00:00 2001 From: Mike Bond Date: Tue, 26 Nov 2024 08:59:58 -0800 Subject: [PATCH] PR cleanup --- .vscode/settings.json | 2 +- .../Node/Blocks/PBR/reflectionBlock.ts | 10 +- .../core/src/Materials/PBR/pbrBaseMaterial.ts | 7 +- .../Textures/Filtering/hdrFiltering.ts | 2 +- .../ShadersInclude/hdrFilteringFunctions.fx | 17 +- .../ShadersInclude/pbrBlockReflection.fx | 26 +- .../ShadersInclude/pbrBlockSubSurface.fx | 24 +- .../pbrFragmentSamplersDeclaration.fx | 10 +- packages/dev/core/src/Shaders/pbr.fragment.fx | 30 +- .../ShadersInclude/hdrFilteringFunctions.fx | 6 +- .../ShadersInclude/pbrBlockClearcoat.fx | 659 +++++------ .../ShadersInclude/pbrBlockReflection.fx | 199 ++-- .../ShadersInclude/pbrBlockSheen.fx | 141 +-- .../ShadersInclude/pbrBlockSubSurface.fx | 1052 ++++++++--------- .../pbrFragmentSamplersDeclaration.fx | 7 + .../dev/core/src/ShadersWGSL/pbr.fragment.fx | 393 +++--- packages/dev/core/src/scene.ts | 17 +- 17 files changed, 1208 insertions(+), 1394 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 1c3e0c3a85d4..b56492ef9917 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -33,5 +33,5 @@ "[css]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, - "editor.formatOnSave": true + "editor.formatOnSave": false } diff --git a/packages/dev/core/src/Materials/Node/Blocks/PBR/reflectionBlock.ts b/packages/dev/core/src/Materials/Node/Blocks/PBR/reflectionBlock.ts index 021e28b77d7e..87213c76b1e8 100644 --- a/packages/dev/core/src/Materials/Node/Blocks/PBR/reflectionBlock.ts +++ b/packages/dev/core/src/Materials/Node/Blocks/PBR/reflectionBlock.ts @@ -456,10 +456,12 @@ export class ReflectionBlock extends ReflectionTextureBaseBlock { #endif #ifdef REALTIME_FILTERING , ${this._vReflectionFilteringInfoName} - , icdfxSampler // ** not handled ** - ${isWebGPU ? `, icdfxSamplerSampler` : ""} - , icdfySampler // ** not handled ** - ${isWebGPU ? `, icdfySamplerSampler` : ""} + #ifdef IBL_CDF_FILTERING + , icdfxSampler // ** not handled ** + ${isWebGPU ? `, icdfxSamplerSampler` : ""} + , icdfySampler // ** not handled ** + ${isWebGPU ? `, icdfySamplerSampler` : ""} + #endif #endif ); #endif\n`; diff --git a/packages/dev/core/src/Materials/PBR/pbrBaseMaterial.ts b/packages/dev/core/src/Materials/PBR/pbrBaseMaterial.ts index 80e601926bc9..653d363ca8c5 100644 --- a/packages/dev/core/src/Materials/PBR/pbrBaseMaterial.ts +++ b/packages/dev/core/src/Materials/PBR/pbrBaseMaterial.ts @@ -80,6 +80,7 @@ export class PBRMaterialDefines extends MaterialDefines implements IImageProcess public NUM_SAMPLES = "0"; public REALTIME_FILTERING = false; + public IBL_CDF_FILTERING = false; public MAINUV1 = false; public MAINUV2 = false; @@ -1655,6 +1656,9 @@ export abstract class PBRBaseMaterial extends PushMaterial { } defines.REALTIME_FILTERING = true; + if (this.getScene().useEnvironmentCDFMaps) { + defines.IBL_CDF_FILTERING = true; + } } else { defines.REALTIME_FILTERING = false; } @@ -2302,8 +2306,7 @@ export abstract class PBRBaseMaterial extends PushMaterial { //if realtime filtering //set the needed cdf maps - if (this.realTimeFiltering) { - this.getScene().useEnvironmentCDFMaps = true; + if (this.realTimeFiltering && this.getScene().useEnvironmentCDFMaps) { ubo.setTexture("icdfxSampler", this.getScene().importanceSamplingRenderer?.getIcdfxTexture()); ubo.setTexture("icdfySampler", this.getScene().importanceSamplingRenderer?.getIcdfyTexture()); } diff --git a/packages/dev/core/src/Materials/Textures/Filtering/hdrFiltering.ts b/packages/dev/core/src/Materials/Textures/Filtering/hdrFiltering.ts index fb3777a8acc1..473715828fa3 100644 --- a/packages/dev/core/src/Materials/Textures/Filtering/hdrFiltering.ts +++ b/packages/dev/core/src/Materials/Textures/Filtering/hdrFiltering.ts @@ -34,7 +34,7 @@ export class HDRFiltering { private _effectRenderer: EffectRenderer; private _effectWrapper: EffectWrapper; - private _lodGenerationOffset: number = -2; + private _lodGenerationOffset: number = 0; private _lodGenerationScale: number = 0.8; /** diff --git a/packages/dev/core/src/Shaders/ShadersInclude/hdrFilteringFunctions.fx b/packages/dev/core/src/Shaders/ShadersInclude/hdrFilteringFunctions.fx index 64e834e17fa0..f232a0d50b80 100644 --- a/packages/dev/core/src/Shaders/ShadersInclude/hdrFilteringFunctions.fx +++ b/packages/dev/core/src/Shaders/ShadersInclude/hdrFilteringFunctions.fx @@ -185,7 +185,7 @@ #define inline vec3 irradiance(samplerCube inputTexture, vec3 inputN, vec2 filteringInfo - #ifdef REALTIME_FILTERING + #ifdef IBL_CDF_FILTERING , sampler2D icdfxSampler, sampler2D icdfySampler #endif ) @@ -193,7 +193,7 @@ vec3 n = normalize(inputN); vec3 result = vec3(0.0); - #ifndef REALTIME_FILTERING + #ifndef IBL_CDF_FILTERING vec3 tangent = abs(n.z) < 0.999 ? vec3(0., 0., 1.) : vec3(1., 0., 0.); tangent = normalize(cross(tangent, n)); vec3 bitangent = cross(n, tangent); @@ -212,7 +212,7 @@ { vec2 Xi = hammersley(i, NUM_SAMPLES); - #ifdef REALTIME_FILTERING + #ifdef IBL_CDF_FILTERING vec2 T; T.x = textureCubeLodEXT(icdfxSampler, vec2(Xi.x, 0.0), 0.0).x; T.y = textureCubeLodEXT(icdfySampler, vec2(T.x, Xi.y), 0.0).x; @@ -233,7 +233,7 @@ float l = log4(omegaS) - log4(omegaP) + log4(K); float mipLevel = clamp(l, 0.0, maxLevel); - #ifdef REALTIME_FILTERING + #ifdef IBL_CDF_FILTERING vec3 c = textureCubeLodEXT(inputTexture, Ls, mipLevel).rgb; #else vec3 c = textureCubeLodEXT(inputTexture, tbn * Ls, mipLevel).rgb; @@ -242,7 +242,7 @@ c = toLinearSpace(c); #endif - #ifdef REALTIME_FILTERING + #ifdef IBL_CDF_FILTERING result += c * NoL; #else result += c; @@ -256,11 +256,7 @@ } #define inline - vec3 radiance(float alphaG, samplerCube inputTexture, vec3 inputN, vec2 filteringInfo - #ifdef REALTIME_FILTERING - , sampler2D icdfxSampler, sampler2D icdfySampler - #endif - ) + vec3 radiance(float alphaG, samplerCube inputTexture, vec3 inputN, vec2 filteringInfo) { vec3 n = normalize(inputN); vec3 c = textureCube(inputTexture, n).rgb; // Don't put it in the "if (alphaG == 0.)" branch for uniformity (analysis) reasons! @@ -307,7 +303,6 @@ float mipLevel = clamp(float(l), 0.0, maxLevel); weight += NoL; - vec3 c = textureCubeLodEXT(inputTexture, tbn * L, mipLevel).rgb; #ifdef GAMMA_INPUT c = toLinearSpace(c); diff --git a/packages/dev/core/src/Shaders/ShadersInclude/pbrBlockReflection.fx b/packages/dev/core/src/Shaders/ShadersInclude/pbrBlockReflection.fx index 4a6867811d7a..e52f3251f2c8 100644 --- a/packages/dev/core/src/Shaders/ShadersInclude/pbrBlockReflection.fx +++ b/packages/dev/core/src/Shaders/ShadersInclude/pbrBlockReflection.fx @@ -84,8 +84,10 @@ #endif #ifdef REALTIME_FILTERING in vec2 vReflectionFilteringInfo, - in sampler2D icdfxSampler, - in sampler2D icdfySampler, + #ifdef IBL_CDF_FILTERING + in sampler2D icdfxSampler, + in sampler2D icdfySampler, + #endif #endif out vec4 environmentRadiance ) @@ -120,7 +122,7 @@ float requestedReflectionLOD = reflectionLOD; #endif #ifdef REALTIME_FILTERING - environmentRadiance = vec4(radiance(alphaG, reflectionSampler, reflectionCoords, vReflectionFilteringInfo, icdfxSampler, icdfySampler), 1.0); + environmentRadiance = vec4(radiance(alphaG, reflectionSampler, reflectionCoords, vReflectionFilteringInfo), 1.0); #else environmentRadiance = sampleReflectionLod(reflectionSampler, reflectionCoords, reflectionLOD); #endif @@ -206,8 +208,10 @@ #endif #ifdef REALTIME_FILTERING , in vec2 vReflectionFilteringInfo - , in sampler2D icdfxSampler - , in sampler2D icdfySampler + #ifdef IBL_CDF_FILTERING + , in sampler2D icdfxSampler + , in sampler2D icdfySampler + #endif #endif ) { @@ -254,8 +258,10 @@ #endif #ifdef REALTIME_FILTERING vReflectionFilteringInfo, - icdfxSampler, - icdfySampler, + #ifdef IBL_CDF_FILTERING + icdfxSampler, + icdfySampler, + #endif #endif environmentRadiance ); @@ -282,7 +288,11 @@ #endif #if defined(REALTIME_FILTERING) - environmentIrradiance = irradiance(reflectionSampler, irradianceVector, vReflectionFilteringInfo, icdfxSampler, icdfySampler); + environmentIrradiance = irradiance(reflectionSampler, irradianceVector, vReflectionFilteringInfo + #ifdef IBL_CDF_FILTERING + , icdfxSampler, icdfySampler + #endif + ); #else environmentIrradiance = computeEnvironmentIrradiance(irradianceVector); #endif diff --git a/packages/dev/core/src/Shaders/ShadersInclude/pbrBlockSubSurface.fx b/packages/dev/core/src/Shaders/ShadersInclude/pbrBlockSubSurface.fx index 28b466d8d24b..7e4d59ec7bf3 100644 --- a/packages/dev/core/src/Shaders/ShadersInclude/pbrBlockSubSurface.fx +++ b/packages/dev/core/src/Shaders/ShadersInclude/pbrBlockSubSurface.fx @@ -63,6 +63,10 @@ struct subSurfaceOutParams #endif #ifdef REALTIME_FILTERING , in vec2 vRefractionFilteringInfo + #ifdef IBL_CDF_FILTERING + , in sampler2D icdfxSampler + , in sampler2D icdfySampler + #endif #endif #ifdef SS_USE_LOCAL_REFRACTIONMAP_CUBIC , in vec3 refractionPosition @@ -120,7 +124,7 @@ struct subSurfaceOutParams #endif #if defined(REALTIME_FILTERING) && defined(SS_REFRACTIONMAP_3D) - environmentRefraction = vec4(radiance(alphaG, refractionSampler, refractionCoords, vRefractionFilteringInfo, sampler2D icdfxSampler, sampler2D icdfySampler), 1.0); + environmentRefraction = vec4(radiance(alphaG, refractionSampler, refractionCoords, vRefractionFilteringInfo), 1.0); #else environmentRefraction = sampleRefractionLod(refractionSampler, refractionCoords, requestedRefractionLOD); #endif @@ -180,6 +184,10 @@ struct subSurfaceOutParams #if defined(REALTIME_FILTERING) , in samplerCube reflectionSampler , in vec2 vReflectionFilteringInfo + #ifdef IBL_CDF_FILTERING + , in sampler2D icdfxSampler + , in sampler2D icdfySampler + #endif #endif #endif #ifdef USEIRRADIANCEMAP @@ -230,6 +238,10 @@ struct subSurfaceOutParams #endif #ifdef REALTIME_FILTERING , in vec2 vRefractionFilteringInfo + #ifdef IBL_CDF_FILTERING + , in sampler2D icdfxSampler + , in sampler2D icdfySampler + #endif #endif #ifdef SS_USE_LOCAL_REFRACTIONMAP_CUBIC , in vec3 refractionPosition @@ -386,6 +398,10 @@ struct subSurfaceOutParams #endif #ifdef REALTIME_FILTERING , vRefractionFilteringInfo + #ifdef IBL_CDF_FILTERING + , icdfxSampler + , icdfySampler + #endif #endif #ifdef SS_USE_LOCAL_REFRACTIONMAP_CUBIC , refractionPosition @@ -490,7 +506,11 @@ struct subSurfaceOutParams #if defined(USESPHERICALFROMREFLECTIONMAP) #if defined(REALTIME_FILTERING) - vec3 refractionIrradiance = irradiance(reflectionSampler, -irradianceVector, vReflectionFilteringInfo, icdfxSampler, icdfySampler); + vec3 refractionIrradiance = irradiance(reflectionSampler, -irradianceVector, vReflectionFilteringInfo + #ifdef IBL_CDF_FILTERING + , icdfxSampler, icdfySampler + #endif + ); #else vec3 refractionIrradiance = computeEnvironmentIrradiance(-irradianceVector); #endif diff --git a/packages/dev/core/src/Shaders/ShadersInclude/pbrFragmentSamplersDeclaration.fx b/packages/dev/core/src/Shaders/ShadersInclude/pbrFragmentSamplersDeclaration.fx index 44f1089deec6..d1f794a7ff67 100644 --- a/packages/dev/core/src/Shaders/ShadersInclude/pbrFragmentSamplersDeclaration.fx +++ b/packages/dev/core/src/Shaders/ShadersInclude/pbrFragmentSamplersDeclaration.fx @@ -70,11 +70,6 @@ #endif #endif - #ifdef REALTIME_FILTERING - uniform sampler2D icdfxSampler; - uniform sampler2D icdfySampler; - #endif - #ifdef REFLECTIONMAP_SKYBOX varying vec3 vPositionUVW; #else @@ -120,4 +115,9 @@ #include(_DEFINENAME_,SS_REFRACTIONINTENSITY_TEXTURE,_VARYINGNAME_,RefractionIntensity,_SAMPLERNAME_,refractionIntensity) #include(_DEFINENAME_,SS_TRANSLUCENCYINTENSITY_TEXTURE,_VARYINGNAME_,TranslucencyIntensity,_SAMPLERNAME_,translucencyIntensity) #include(_DEFINENAME_,SS_TRANSLUCENCYCOLOR_TEXTURE,_VARYINGNAME_,TranslucencyColor,_SAMPLERNAME_,translucencyColor) +#endif + +#ifdef IBL_CDF_FILTERING + uniform sampler2D icdfxSampler; + uniform sampler2D icdfySampler; #endif \ No newline at end of file diff --git a/packages/dev/core/src/Shaders/pbr.fragment.fx b/packages/dev/core/src/Shaders/pbr.fragment.fx index 0fcca17761c0..f2a15c9e6ed4 100644 --- a/packages/dev/core/src/Shaders/pbr.fragment.fx +++ b/packages/dev/core/src/Shaders/pbr.fragment.fx @@ -298,8 +298,10 @@ void main(void) { #endif #ifdef REALTIME_FILTERING , vReflectionFilteringInfo - , icdfxSampler - , icdfySampler + #ifdef IBL_CDF_FILTERING + , icdfxSampler + , icdfySampler + #endif #endif ); #else @@ -358,8 +360,10 @@ void main(void) { #endif #ifdef REALTIME_FILTERING , vReflectionFilteringInfo - , icdfxSampler - , icdfySampler + #ifdef IBL_CDF_FILTERING + , icdfxSampler + , icdfySampler + #endif #endif #if !defined(REFLECTIONMAP_SKYBOX) && defined(RADIANCEOCCLUSION) , seo @@ -479,8 +483,10 @@ void main(void) { #endif #ifdef REALTIME_FILTERING , vReflectionFilteringInfo - , icdfxSampler - , icdfySampler + #ifdef IBL_CDF_FILTERING + , icdfxSampler + , icdfySampler + #endif #endif #endif #if defined(CLEARCOAT_BUMP) || defined(TWOSIDEDLIGHTING) @@ -539,8 +545,10 @@ void main(void) { #if defined(REALTIME_FILTERING) , reflectionSampler , vReflectionFilteringInfo - , icdfxSampler - , icdfySampler + #ifdef IBL_CDF_FILTERING + , icdfxSampler + , icdfySampler + #endif #endif #endif #ifdef USEIRRADIANCEMAP @@ -579,8 +587,10 @@ void main(void) { #endif #ifdef REALTIME_FILTERING , vRefractionFilteringInfo - , icdfxSampler - , icdfySampler + #ifdef IBL_CDF_FILTERING + , icdfxSampler + , icdfySampler + #endif #endif #ifdef SS_USE_LOCAL_REFRACTIONMAP_CUBIC , vRefractionPosition diff --git a/packages/dev/core/src/ShadersWGSL/ShadersInclude/hdrFilteringFunctions.fx b/packages/dev/core/src/ShadersWGSL/ShadersInclude/hdrFilteringFunctions.fx index 9d10627d1279..5f6bc616a378 100644 --- a/packages/dev/core/src/ShadersWGSL/ShadersInclude/hdrFilteringFunctions.fx +++ b/packages/dev/core/src/ShadersWGSL/ShadersInclude/hdrFilteringFunctions.fx @@ -191,11 +191,7 @@ return result; } - fn radiance(alphaG: f32, inputTexture: texture_cube, inputSampler: sampler, inputN: vec3f, filteringInfo: vec2f - #ifdef REALTIME_FILTERING - , icdfxSampler: sampler, icdfySampler: sampler - #endif - ) -> vec3f + fn radiance(alphaG: f32, inputTexture: texture_cube, inputSampler: sampler, inputN: vec3f, filteringInfo: vec2f) -> vec3f { var n: vec3f = normalize(inputN); var c: vec3f = textureSample(inputTexture, inputSampler, n).rgb; // Don't put it in the "if (alphaG == 0.)" branch for uniformity (analysis) reasons! diff --git a/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockClearcoat.fx b/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockClearcoat.fx index fc2a8a43e26b..2dfc87b18ee2 100644 --- a/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockClearcoat.fx +++ b/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockClearcoat.fx @@ -38,366 +38,301 @@ struct clearcoatOutParams #ifdef CLEARCOAT #define pbr_inline -fn clearcoatBlock(vPositionW - : vec3f, geometricNormalW - : vec3f, viewDirectionW - : vec3f, vClearCoatParams - : vec2f -#if defined(CLEARCOAT_TEXTURE_ROUGHNESS) && \ - !defined(CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL) && \ - !defined(CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE) - , - clearCoatMapRoughnessData - : vec4f -#endif - , - specularEnvironmentR0 - : vec3f -#ifdef CLEARCOAT_TEXTURE - , - clearCoatMapData - : vec2f -#endif -#ifdef CLEARCOAT_TINT - , - vClearCoatTintParams - : vec4f, clearCoatColorAtDistance - : f32, vClearCoatRefractionParams - : vec4f -#ifdef CLEARCOAT_TINT_TEXTURE - , - clearCoatTintMapData - : vec4f -#endif -#endif -#ifdef CLEARCOAT_BUMP - , - vClearCoatBumpInfos - : vec2f, clearCoatBumpMapData - : vec4f, vClearCoatBumpUV - : vec2f -#if defined(TANGENT) && defined(NORMAL) - , - vTBN - : mat3x3f -#else - , - vClearCoatTangentSpaceParams - : vec2f -#endif -#ifdef OBJECTSPACE_NORMALMAP - , - normalMatrix - : mat4x4f -#endif -#endif -#if defined(FORCENORMALFORWARD) && defined(NORMAL) - , - faceNormal - : vec3f -#endif -#ifdef REFLECTION - , - vReflectionMicrosurfaceInfos - : vec3f, vReflectionInfos - : vec2f, vReflectionColor - : vec3f, vLightingIntensity - : vec4f -#ifdef REFLECTIONMAP_3D - , - reflectionSampler - : texture_cube, reflectionSamplerSampler - : sampler -#else - , - reflectionSampler - : texture_2d, reflectionSamplerSampler - : sampler -#endif -#ifndef LODBASEDMICROSFURACE -#ifdef REFLECTIONMAP_3D - , - reflectionLowSampler - : texture_cube, reflectionLowSamplerSampler - : sampler, reflectionHighSampler - : texture_cube, reflectionHighSamplerSampler - : sampler -#else - , - reflectionLowSampler - : texture_2d, reflectionLowSamplerSampler - : sampler, reflectionHighSampler - : texture_2d, reflectionHighSamplerSampler - : sampler -#endif -#endif -#ifdef REALTIME_FILTERING - , - vReflectionFilteringInfo - : vec2f, icdfxSampler - : texture_2d, icdfxSamplerSampler - : sampler, icdfySampler - : texture_2d, icdfySamplerSampler - : sampler -#endif -#endif -#if defined(CLEARCOAT_BUMP) || defined(TWOSIDEDLIGHTING) - , - frontFacingMultiplier - : f32 -#endif - ) - ->clearcoatOutParams { - var outParams : clearcoatOutParams; - // Clear COAT parameters. - var clearCoatIntensity : f32 = vClearCoatParams.x; - var clearCoatRoughness : f32 = vClearCoatParams.y; - -#ifdef CLEARCOAT_TEXTURE - clearCoatIntensity *= clearCoatMapData.x; -#ifdef CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE - clearCoatRoughness *= clearCoatMapData.y; -#endif -#if DEBUGMODE > 0 - outParams.clearCoatMapData = clearCoatMapData; -#endif -#endif - -#if defined(CLEARCOAT_TEXTURE_ROUGHNESS) && \ - !defined(CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE) - clearCoatRoughness *= clearCoatMapRoughnessData.y; -#endif - - outParams.clearCoatIntensity = clearCoatIntensity; - outParams.clearCoatRoughness = clearCoatRoughness; - -#ifdef CLEARCOAT_TINT - var clearCoatColor : vec3f = vClearCoatTintParams.rgb; - var clearCoatThickness : f32 = vClearCoatTintParams.a; - -#ifdef CLEARCOAT_TINT_TEXTURE -#ifdef CLEARCOAT_TINT_GAMMATEXTURE - clearCoatColor *= toLinearSpaceVec3(clearCoatTintMapData.rgb); -#else - clearCoatColor *= clearCoatTintMapData.rgb; -#endif - clearCoatThickness *= clearCoatTintMapData.a; -#if DEBUGMODE > 0 - outParams.clearCoatTintMapData = clearCoatTintMapData; -#endif -#endif - - outParams.clearCoatColor = - computeColorAtDistanceInMedia(clearCoatColor, clearCoatColorAtDistance); - outParams.clearCoatThickness = clearCoatThickness; -#endif - -// remapping and linearization of clear coat roughness -// Let s see how it ends up in gltf -// clearCoatRoughness = mix(0.089, 0.6, clearCoatRoughness); - -// Remap F0 to account for the change of interface within the material. -#ifdef CLEARCOAT_REMAP_F0 - var specularEnvironmentR0Updated - : vec3f = getR0RemappedForClearCoat(specularEnvironmentR0); -#else - var specularEnvironmentR0Updated : vec3f = specularEnvironmentR0; -#endif - outParams.specularEnvironmentR0 = mix( - specularEnvironmentR0, specularEnvironmentR0Updated, clearCoatIntensity); - - // Needs to use the geometric normal before bump for this. - var clearCoatNormalW : vec3f = geometricNormalW; - -#ifdef CLEARCOAT_BUMP -#ifdef NORMALXYSCALE - var clearCoatNormalScale : f32 = 1.0; -#else - var clearCoatNormalScale : f32 = vClearCoatBumpInfos.y; -#endif - -#if defined(TANGENT) && defined(NORMAL) - var TBNClearCoat : mat3x3f = vTBN; -#else - // flip the uv for the backface - var TBNClearCoatUV : vec2f = vClearCoatBumpUV * frontFacingMultiplier; - var TBNClearCoat - : mat3x3f = - cotangent_frame(clearCoatNormalW * clearCoatNormalScale, vPositionW, - TBNClearCoatUV, vClearCoatTangentSpaceParams); -#endif - -#if DEBUGMODE > 0 - outParams.TBNClearCoat = TBNClearCoat; -#endif - -#ifdef OBJECTSPACE_NORMALMAP - clearCoatNormalW = normalize(clearCoatBumpMapData.xyz * 2.0 - 1.0); - clearCoatNormalW = normalize( - mat3x3f(normalMatrix[0].xyz, normalMatrix[1].xyz, normalMatrix[2].xyz) * - clearCoatNormalW); -#else - clearCoatNormalW = perturbNormal(TBNClearCoat, clearCoatBumpMapData.xyz, - vClearCoatBumpInfos.y); -#endif -#endif - -#if defined(FORCENORMALFORWARD) && defined(NORMAL) - clearCoatNormalW *= sign(dot(clearCoatNormalW, faceNormal)); -#endif - -#if defined(TWOSIDEDLIGHTING) && defined(NORMAL) - clearCoatNormalW = clearCoatNormalW * frontFacingMultiplier; -#endif - - outParams.clearCoatNormalW = clearCoatNormalW; - - // Clear Coat AA - outParams.clearCoatAARoughnessFactors = - getAARoughnessFactors(clearCoatNormalW.xyz); - - // Compute N dot V. - var clearCoatNdotVUnclamped : f32 = dot(clearCoatNormalW, viewDirectionW); - // The order 1886 page 3. - var clearCoatNdotV : f32 = absEps(clearCoatNdotVUnclamped); - -#if DEBUGMODE > 0 - outParams.clearCoatNdotV = clearCoatNdotV; -#endif - -#ifdef CLEARCOAT_TINT - // Used later on in the light fragment and ibl. - var clearCoatVRefract : vec3f = refract(-viewDirectionW, clearCoatNormalW, - vClearCoatRefractionParams.y); - // The order 1886 page 3. - outParams.clearCoatNdotVRefract = - absEps(dot(clearCoatNormalW, clearCoatVRefract)); -#endif - -#if defined(ENVIRONMENTBRDF) && \ - (!defined(REFLECTIONMAP_SKYBOX) || defined(MS_BRDF_ENERGY_CONSERVATION)) - // BRDF Lookup - var environmentClearCoatBrdf - : vec3f = getBRDFLookup(clearCoatNdotV, clearCoatRoughness); -#endif - -// Clear Coat Reflection -#if defined(REFLECTION) - var clearCoatAlphaG : f32 = - convertRoughnessToAverageSlope(clearCoatRoughness); - -#ifdef SPECULARAA - // Adapt linear roughness (alphaG) to geometric curvature of the current - // pixel. - clearCoatAlphaG += outParams.clearCoatAARoughnessFactors.y; -#endif - - var environmentClearCoatRadiance : vec4f = vec4f(0., 0., 0., 0.); - - var clearCoatReflectionVector - : vec3f = - computeReflectionCoords(vec4f(vPositionW, 1.0), clearCoatNormalW); -#ifdef REFLECTIONMAP_OPPOSITEZ - clearCoatReflectionVector.z *= -1.0; -#endif - -// _____________________________ 2D vs 3D Maps ________________________________ -#ifdef REFLECTIONMAP_3D - var clearCoatReflectionCoords : vec3f = clearCoatReflectionVector; -#else - var clearCoatReflectionCoords : vec2f = clearCoatReflectionVector.xy; -#ifdef REFLECTIONMAP_PROJECTION - clearCoatReflectionCoords /= clearCoatReflectionVector.z; -#endif - clearCoatReflectionCoords.y = 1.0 - clearCoatReflectionCoords.y; -#endif - - environmentClearCoatRadiance = sampleReflectionTexture( - clearCoatAlphaG, vReflectionMicrosurfaceInfos, vReflectionInfos, - vReflectionColor -#if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX) - , - clearCoatNdotVUnclamped -#endif -#ifdef LINEARSPECULARREFLECTION - , - clearCoatRoughness -#endif - , - reflectionSampler, reflectionSamplerSampler, clearCoatReflectionCoords -#ifndef LODBASEDMICROSFURACE - , - reflectionLowSampler, reflectionLowSamplerSampler, reflectionHighSampler, - reflectionHighSamplerSampler -#endif -#ifdef REALTIME_FILTERING - , - vReflectionFilteringInfo, icdfxSampler, icdfxSamplerSampler, icdfySampler, - icdfySamplerSampler -#endif - ); - -#if DEBUGMODE > 0 - outParams.environmentClearCoatRadiance = environmentClearCoatRadiance; -#endif - -// _________________________ Clear Coat Environment Oclusion -// __________________________ -#if defined(ENVIRONMENTBRDF) && !defined(REFLECTIONMAP_SKYBOX) - var clearCoatEnvironmentReflectance - : vec3f = getReflectanceFromBRDFLookup( - vec3f(uniforms.vClearCoatRefractionParams.x), - environmentClearCoatBrdf); - -#ifdef HORIZONOCCLUSION -#ifdef BUMP -#ifdef REFLECTIONMAP_3D - var clearCoatEho : f32 = environmentHorizonOcclusion( - -viewDirectionW, clearCoatNormalW, geometricNormalW); - clearCoatEnvironmentReflectance *= clearCoatEho; -#endif -#endif -#endif -#else - // Jones implementation of a well balanced fast analytical solution. - var clearCoatEnvironmentReflectance - : vec3f = getReflectanceFromAnalyticalBRDFLookup_Jones( - clearCoatNdotV, vec3f(1.), vec3f(1.), - sqrt(1. - clearCoatRoughness)); -#endif - - clearCoatEnvironmentReflectance *= clearCoatIntensity; - -#if DEBUGMODE > 0 - outParams.clearCoatEnvironmentReflectance = clearCoatEnvironmentReflectance; -#endif - - outParams.finalClearCoatRadianceScaled = environmentClearCoatRadiance.rgb * - clearCoatEnvironmentReflectance * - vLightingIntensity.z; -#endif - -#if defined(CLEARCOAT_TINT) - // NdotL = NdotV in IBL - outParams.absorption = computeClearCoatAbsorption( - outParams.clearCoatNdotVRefract, outParams.clearCoatNdotVRefract, - outParams.clearCoatColor, clearCoatThickness, clearCoatIntensity); -#endif - - // clear coat energy conservation - var fresnelIBLClearCoat - : f32 = fresnelSchlickGGX(clearCoatNdotV, - uniforms.vClearCoatRefractionParams.x, - CLEARCOATREFLECTANCE90); - fresnelIBLClearCoat *= clearCoatIntensity; - - outParams.conservationFactor = (1. - fresnelIBLClearCoat); - -#if defined(ENVIRONMENTBRDF) && defined(MS_BRDF_ENERGY_CONSERVATION) - outParams.energyConservationFactorClearCoat = getEnergyConservationFactor( - outParams.specularEnvironmentR0, environmentClearCoatBrdf); -#endif - - return outParams; -} + fn clearcoatBlock( + vPositionW: vec3f + , geometricNormalW: vec3f + , viewDirectionW: vec3f + , vClearCoatParams: vec2f + #if defined(CLEARCOAT_TEXTURE_ROUGHNESS) && !defined(CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL) && !defined(CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE) + , clearCoatMapRoughnessData: vec4f + #endif + , specularEnvironmentR0: vec3f + #ifdef CLEARCOAT_TEXTURE + , clearCoatMapData: vec2f + #endif + #ifdef CLEARCOAT_TINT + , vClearCoatTintParams: vec4f + , clearCoatColorAtDistance: f32 + , vClearCoatRefractionParams: vec4f + #ifdef CLEARCOAT_TINT_TEXTURE + , clearCoatTintMapData: vec4f + #endif + #endif + #ifdef CLEARCOAT_BUMP + , vClearCoatBumpInfos: vec2f + , clearCoatBumpMapData: vec4f + , vClearCoatBumpUV: vec2f + #if defined(TANGENT) && defined(NORMAL) + , vTBN: mat3x3f + #else + , vClearCoatTangentSpaceParams: vec2f + #endif + #ifdef OBJECTSPACE_NORMALMAP + , normalMatrix: mat4x4f + #endif + #endif + #if defined(FORCENORMALFORWARD) && defined(NORMAL) + , faceNormal: vec3f + #endif + #ifdef REFLECTION + , vReflectionMicrosurfaceInfos: vec3f + , vReflectionInfos: vec2f + , vReflectionColor: vec3f + , vLightingIntensity: vec4f + #ifdef REFLECTIONMAP_3D + , reflectionSampler: texture_cube + , reflectionSamplerSampler: sampler + #else + , reflectionSampler: texture_2d + , reflectionSamplerSampler: sampler + #endif + #ifndef LODBASEDMICROSFURACE + #ifdef REFLECTIONMAP_3D + , reflectionLowSampler: texture_cube + , reflectionLowSamplerSampler: sampler + , reflectionHighSampler: texture_cube + , reflectionHighSamplerSampler: sampler + #else + , reflectionLowSampler: texture_2d + , reflectionLowSamplerSampler: sampler + , reflectionHighSampler: texture_2d + , reflectionHighSamplerSampler: sampler + #endif + #endif + #ifdef REALTIME_FILTERING + , vReflectionFilteringInfo: vec2f + #endif + #endif + #if defined(CLEARCOAT_BUMP) || defined(TWOSIDEDLIGHTING) + , frontFacingMultiplier: f32 + #endif + ) -> clearcoatOutParams + { + var outParams: clearcoatOutParams; + // Clear COAT parameters. + var clearCoatIntensity: f32 = vClearCoatParams.x; + var clearCoatRoughness: f32 = vClearCoatParams.y; + + #ifdef CLEARCOAT_TEXTURE + clearCoatIntensity *= clearCoatMapData.x; + #ifdef CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE + clearCoatRoughness *= clearCoatMapData.y; + #endif + #if DEBUGMODE > 0 + outParams.clearCoatMapData = clearCoatMapData; + #endif + #endif + + + #if defined(CLEARCOAT_TEXTURE_ROUGHNESS) && !defined(CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE) + clearCoatRoughness *= clearCoatMapRoughnessData.y; + #endif + + outParams.clearCoatIntensity = clearCoatIntensity; + outParams.clearCoatRoughness = clearCoatRoughness; + + #ifdef CLEARCOAT_TINT + var clearCoatColor: vec3f = vClearCoatTintParams.rgb; + var clearCoatThickness: f32 = vClearCoatTintParams.a; + + #ifdef CLEARCOAT_TINT_TEXTURE + #ifdef CLEARCOAT_TINT_GAMMATEXTURE + clearCoatColor *= toLinearSpaceVec3(clearCoatTintMapData.rgb); + #else + clearCoatColor *= clearCoatTintMapData.rgb; + #endif + clearCoatThickness *= clearCoatTintMapData.a; + #if DEBUGMODE > 0 + outParams.clearCoatTintMapData = clearCoatTintMapData; + #endif + #endif + + outParams.clearCoatColor = computeColorAtDistanceInMedia(clearCoatColor, clearCoatColorAtDistance); + outParams.clearCoatThickness = clearCoatThickness; + #endif + + // remapping and linearization of clear coat roughness + // Let s see how it ends up in gltf + // clearCoatRoughness = mix(0.089, 0.6, clearCoatRoughness); + + // Remap F0 to account for the change of interface within the material. + #ifdef CLEARCOAT_REMAP_F0 + var specularEnvironmentR0Updated: vec3f = getR0RemappedForClearCoat(specularEnvironmentR0); + #else + var specularEnvironmentR0Updated: vec3f = specularEnvironmentR0; + #endif + outParams.specularEnvironmentR0 = mix(specularEnvironmentR0, specularEnvironmentR0Updated, clearCoatIntensity); + + // Needs to use the geometric normal before bump for this. + var clearCoatNormalW: vec3f = geometricNormalW; + + #ifdef CLEARCOAT_BUMP + #ifdef NORMALXYSCALE + var clearCoatNormalScale: f32 = 1.0; + #else + var clearCoatNormalScale: f32 = vClearCoatBumpInfos.y; + #endif + + #if defined(TANGENT) && defined(NORMAL) + var TBNClearCoat: mat3x3f = vTBN; + #else + // flip the uv for the backface + var TBNClearCoatUV: vec2f = vClearCoatBumpUV * frontFacingMultiplier; + var TBNClearCoat: mat3x3f = cotangent_frame(clearCoatNormalW * clearCoatNormalScale, vPositionW, TBNClearCoatUV, vClearCoatTangentSpaceParams); + #endif + + #if DEBUGMODE > 0 + outParams.TBNClearCoat = TBNClearCoat; + #endif + + #ifdef OBJECTSPACE_NORMALMAP + clearCoatNormalW = normalize(clearCoatBumpMapData.xyz * 2.0 - 1.0); + clearCoatNormalW = normalize( mat3x3f(normalMatrix[0].xyz, normalMatrix[1].xyz, normalMatrix[2].xyz) * clearCoatNormalW); + #else + clearCoatNormalW = perturbNormal(TBNClearCoat, clearCoatBumpMapData.xyz, vClearCoatBumpInfos.y); + #endif + #endif + + #if defined(FORCENORMALFORWARD) && defined(NORMAL) + clearCoatNormalW *= sign(dot(clearCoatNormalW, faceNormal)); + #endif + + #if defined(TWOSIDEDLIGHTING) && defined(NORMAL) + clearCoatNormalW = clearCoatNormalW * frontFacingMultiplier; + #endif + + outParams.clearCoatNormalW = clearCoatNormalW; + + // Clear Coat AA + outParams.clearCoatAARoughnessFactors = getAARoughnessFactors(clearCoatNormalW.xyz); + + // Compute N dot V. + var clearCoatNdotVUnclamped: f32 = dot(clearCoatNormalW, viewDirectionW); + // The order 1886 page 3. + var clearCoatNdotV: f32 = absEps(clearCoatNdotVUnclamped); + + #if DEBUGMODE > 0 + outParams.clearCoatNdotV = clearCoatNdotV; + #endif + + #ifdef CLEARCOAT_TINT + // Used later on in the light fragment and ibl. + var clearCoatVRefract: vec3f = refract(-viewDirectionW, clearCoatNormalW, vClearCoatRefractionParams.y); + // The order 1886 page 3. + outParams.clearCoatNdotVRefract = absEps(dot(clearCoatNormalW, clearCoatVRefract)); + #endif + + #if defined(ENVIRONMENTBRDF) && (!defined(REFLECTIONMAP_SKYBOX) || defined(MS_BRDF_ENERGY_CONSERVATION)) + // BRDF Lookup + var environmentClearCoatBrdf: vec3f = getBRDFLookup(clearCoatNdotV, clearCoatRoughness); + #endif + + // Clear Coat Reflection + #if defined(REFLECTION) + var clearCoatAlphaG: f32 = convertRoughnessToAverageSlope(clearCoatRoughness); + + #ifdef SPECULARAA + // Adapt linear roughness (alphaG) to geometric curvature of the current pixel. + clearCoatAlphaG += outParams.clearCoatAARoughnessFactors.y; + #endif + + var environmentClearCoatRadiance: vec4f = vec4f(0., 0., 0., 0.); + + var clearCoatReflectionVector: vec3f = computeReflectionCoords( vec4f(vPositionW, 1.0), clearCoatNormalW); + #ifdef REFLECTIONMAP_OPPOSITEZ + clearCoatReflectionVector.z *= -1.0; + #endif + + // _____________________________ 2D vs 3D Maps ________________________________ + #ifdef REFLECTIONMAP_3D + var clearCoatReflectionCoords: vec3f = clearCoatReflectionVector; + #else + var clearCoatReflectionCoords: vec2f = clearCoatReflectionVector.xy; + #ifdef REFLECTIONMAP_PROJECTION + clearCoatReflectionCoords /= clearCoatReflectionVector.z; + #endif + clearCoatReflectionCoords.y = 1.0 - clearCoatReflectionCoords.y; + #endif + + environmentClearCoatRadiance = sampleReflectionTexture( + clearCoatAlphaG + , vReflectionMicrosurfaceInfos + , vReflectionInfos + , vReflectionColor + #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX) + , clearCoatNdotVUnclamped + #endif + #ifdef LINEARSPECULARREFLECTION + , clearCoatRoughness + #endif + , reflectionSampler + , reflectionSamplerSampler + , clearCoatReflectionCoords + #ifndef LODBASEDMICROSFURACE + , reflectionLowSampler + , reflectionLowSamplerSampler + , reflectionHighSampler + , reflectionHighSamplerSampler + #endif + #ifdef REALTIME_FILTERING + , vReflectionFilteringInfo + #ifdef IBL_CDF_FILTERING + , uniforms.icdfxSampler + , uniforms.icdfxSamplerSampler + , uniforms.icdfySampler + , uniforms.icdfySamplerSampler + #endif + #endif + ); + + #if DEBUGMODE > 0 + outParams.environmentClearCoatRadiance = environmentClearCoatRadiance; + #endif + + // _________________________ Clear Coat Environment Oclusion __________________________ + #if defined(ENVIRONMENTBRDF) && !defined(REFLECTIONMAP_SKYBOX) + var clearCoatEnvironmentReflectance: vec3f = getReflectanceFromBRDFLookup(vec3f(uniforms.vClearCoatRefractionParams.x), environmentClearCoatBrdf); + + #ifdef HORIZONOCCLUSION + #ifdef BUMP + #ifdef REFLECTIONMAP_3D + var clearCoatEho: f32 = environmentHorizonOcclusion(-viewDirectionW, clearCoatNormalW, geometricNormalW); + clearCoatEnvironmentReflectance *= clearCoatEho; + #endif + #endif + #endif + #else + // Jones implementation of a well balanced fast analytical solution. + var clearCoatEnvironmentReflectance: vec3f = getReflectanceFromAnalyticalBRDFLookup_Jones(clearCoatNdotV, vec3f(1.), vec3f(1.), sqrt(1. - clearCoatRoughness)); + #endif + + clearCoatEnvironmentReflectance *= clearCoatIntensity; + + #if DEBUGMODE > 0 + outParams.clearCoatEnvironmentReflectance = clearCoatEnvironmentReflectance; + #endif + + outParams.finalClearCoatRadianceScaled = + environmentClearCoatRadiance.rgb * + clearCoatEnvironmentReflectance * + vLightingIntensity.z; + #endif + + #if defined(CLEARCOAT_TINT) + // NdotL = NdotV in IBL + outParams.absorption = computeClearCoatAbsorption(outParams.clearCoatNdotVRefract, outParams.clearCoatNdotVRefract, outParams.clearCoatColor, clearCoatThickness, clearCoatIntensity); + #endif + + // clear coat energy conservation + var fresnelIBLClearCoat: f32 = fresnelSchlickGGX(clearCoatNdotV, uniforms.vClearCoatRefractionParams.x, CLEARCOATREFLECTANCE90); + fresnelIBLClearCoat *= clearCoatIntensity; + + outParams.conservationFactor = (1. - fresnelIBLClearCoat); + + #if defined(ENVIRONMENTBRDF) && defined(MS_BRDF_ENERGY_CONSERVATION) + outParams.energyConservationFactorClearCoat = getEnergyConservationFactor(outParams.specularEnvironmentR0, environmentClearCoatBrdf); + #endif + + return outParams; + } #endif diff --git a/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockReflection.fx b/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockReflection.fx index 315ed2ae7f46..b4166e40552a 100644 --- a/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockReflection.fx +++ b/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockReflection.fx @@ -64,27 +64,21 @@ } #define pbr_inline - fn sampleReflectionTexture(alphaG - : f32, vReflectionMicrosurfaceInfos - : vec3f, vReflectionInfos - : vec2f, vReflectionColor - : vec3f -#if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX) - , - NdotVUnclamped - : f32 + fn sampleReflectionTexture( + alphaG: f32 + , vReflectionMicrosurfaceInfos: vec3f + , vReflectionInfos: vec2f + , vReflectionColor: vec3f + #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX) + , NdotVUnclamped: f32 #endif #ifdef LINEARSPECULARREFLECTION - , - roughness - : f32 + , roughness: f32 #endif #ifdef REFLECTIONMAP_3D - , - reflectionSampler - : texture_cube, reflectionSamplerSampler - : sampler, reflectionCoords - : vec3f + , reflectionSampler: texture_cube + , reflectionSamplerSampler: sampler + , reflectionCoords: vec3f #else , reflectionSampler: texture_2d , reflectionSamplerSampler: sampler @@ -92,32 +86,22 @@ #endif #ifndef LODBASEDMICROSFURACE #ifdef REFLECTIONMAP_3D - , - reflectionLowSampler - : texture_cube, reflectionLowSamplerSampler - : sampler, reflectionHighSampler - : texture_cube, reflectionHighSamplerSampler - : sampler + , reflectionLowSampler: texture_cube + , reflectionLowSamplerSampler: sampler + , reflectionHighSampler: texture_cube + , reflectionHighSamplerSampler: sampler #else - , - reflectionLowSampler - : texture_2d, reflectionLowSamplerSampler - : sampler, reflectionHighSampler - : texture_2d, reflectionHighSamplerSampler - : sampler + , reflectionLowSampler: texture_2d + , reflectionLowSamplerSampler: sampler + , reflectionHighSampler: texture_2d + , reflectionHighSamplerSampler: sampler #endif #endif #ifdef REALTIME_FILTERING - , - vReflectionFilteringInfo - : vec2f, icdfxSampler - : texture_2d, icdfxSamplerSampler - : sampler, icdfySampler - : texture_2d, icdfySamplerSampler - : sampler -#endif - ) - ->vec4f { + , vReflectionFilteringInfo: vec2f + #endif + ) -> vec4f + { var environmentRadiance: vec4f; // _____________________________ 2D vs 3D Maps ________________________________ #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX) @@ -149,14 +133,8 @@ var requestedReflectionLOD: f32 = reflectionLOD; #endif #ifdef REALTIME_FILTERING - environmentRadiance = - vec4f(radiance(alphaG, reflectionSampler, - reflectionSamplerSampler, reflectionCoords, - vReflectionFilteringInfo, icdfxSampler, - icdfxSamplerSampler, icdfySampler, - icdfySamplerSampler), - 1.0); -#else + environmentRadiance = vec4f(radiance(alphaG, reflectionSampler, reflectionSamplerSampler, reflectionCoords, vReflectionFilteringInfo), 1.0); + #else environmentRadiance = textureSampleLevel(reflectionSampler, reflectionSamplerSampler, reflectionCoords, reflectionLOD); #endif #else @@ -196,91 +174,65 @@ return vec4f(envRadiance, environmentRadiance.a); } -#define pbr_inline - fn reflectionBlock(vPositionW - : vec3f, normalW - : vec3f, alphaG - : f32, vReflectionMicrosurfaceInfos - : vec3f, vReflectionInfos - : vec2f, vReflectionColor - : vec3f -#ifdef ANISOTROPIC - , - anisotropicOut - : anisotropicOutParams + #define pbr_inline + fn reflectionBlock( + vPositionW: vec3f + , normalW: vec3f + , alphaG: f32 + , vReflectionMicrosurfaceInfos: vec3f + , vReflectionInfos: vec2f + , vReflectionColor: vec3f + #ifdef ANISOTROPIC + , anisotropicOut: anisotropicOutParams #endif #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX) - , - NdotVUnclamped - : f32 + , NdotVUnclamped: f32 #endif #ifdef LINEARSPECULARREFLECTION - , - roughness - : f32 + , roughness: f32 #endif #ifdef REFLECTIONMAP_3D - , - reflectionSampler - : texture_cube, reflectionSamplerSampler - : sampler + , reflectionSampler: texture_cube + , reflectionSamplerSampler: sampler #else , reflectionSampler: texture_2d , reflectionSamplerSampler: sampler #endif #if defined(NORMAL) && defined(USESPHERICALINVERTEX) - , - vEnvironmentIrradiance - : vec3f + , vEnvironmentIrradiance: vec3f #endif #ifdef USESPHERICALFROMREFLECTIONMAP #if !defined(NORMAL) || !defined(USESPHERICALINVERTEX) - , - reflectionMatrix - : mat4x4f + , reflectionMatrix: mat4x4f #endif #endif #ifdef USEIRRADIANCEMAP #ifdef REFLECTIONMAP_3D - , - irradianceSampler - : texture_cube, irradianceSamplerSampler - : sampler + , irradianceSampler: texture_cube + , irradianceSamplerSampler: sampler #else - , - irradianceSampler - : texture_2d, irradianceSamplerSampler - : sampler + , irradianceSampler: texture_2d + , irradianceSamplerSampler: sampler #endif #endif #ifndef LODBASEDMICROSFURACE #ifdef REFLECTIONMAP_3D - , - reflectionLowSampler - : texture_cube, reflectionLowSamplerSampler - : sampler, reflectionHighSampler - : texture_cube, reflectionHighSamplerSampler - : sampler + , reflectionLowSampler: texture_cube + , reflectionLowSamplerSampler: sampler + , reflectionHighSampler: texture_cube + , reflectionHighSamplerSampler: sampler #else - , - reflectionLowSampler - : texture_2d, reflectionLowSamplerSampler - : sampler, reflectionHighSampler - : texture_2d, reflectionHighSamplerSampler - : sampler + , reflectionLowSampler: texture_2d + , reflectionLowSamplerSampler: sampler + , reflectionHighSampler: texture_2d + , reflectionHighSamplerSampler: sampler #endif #endif #ifdef REALTIME_FILTERING - , - vReflectionFilteringInfo - : vec2f, icdfxSampler - : texture_2d, icdfxSamplerSampler - : sampler, icdfySampler - : texture_2d, icdfySamplerSampler - : sampler -#endif - ) - ->reflectionOutParams { + , vReflectionFilteringInfo: vec2f + #endif + ) -> reflectionOutParams + { var outParams: reflectionOutParams; // _____________________________ Radiance ________________________________ var environmentRadiance: vec4f = vec4f(0., 0., 0., 0.); @@ -300,37 +252,34 @@ ); environmentRadiance = sampleReflectionTexture( - alphaG, vReflectionMicrosurfaceInfos, vReflectionInfos, - vReflectionColor -#if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX) - , - NdotVUnclamped + alphaG + , vReflectionMicrosurfaceInfos + , vReflectionInfos + , vReflectionColor + #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX) + , NdotVUnclamped #endif #ifdef LINEARSPECULARREFLECTION - , - roughness + , roughness #endif #ifdef REFLECTIONMAP_3D - , - reflectionSampler, reflectionSamplerSampler, reflectionCoords + , reflectionSampler + , reflectionSamplerSampler + , reflectionCoords #else , reflectionSampler , reflectionSamplerSampler , reflectionCoords #endif #ifndef LODBASEDMICROSFURACE - , - reflectionLowSampler, reflectionLowSamplerSampler, - reflectionHighSampler, reflectionHighSamplerSampler + , reflectionLowSampler + , reflectionLowSamplerSampler + , reflectionHighSampler + , reflectionHighSamplerSampler #endif #ifdef REALTIME_FILTERING - , - vReflectionFilteringInfo, icdfxSampler - : texture_2d, icdfxSamplerSampler - : sampler, icdfySampler - : texture_2d, icdfySamplerSampler - : sampler -#endif + , vReflectionFilteringInfo + #endif ); // _____________________________ Irradiance ________________________________ @@ -355,7 +304,7 @@ #endif #if defined(REALTIME_FILTERING) - environmentIrradiance = irradiance(reflectionSampler, reflectionSamplerSampler, irradianceVector, vReflectionFilteringInfo, icdfxSampler, icdfySampler); + environmentIrradiance = irradiance(reflectionSampler, reflectionSamplerSampler, irradianceVector, vReflectionFilteringInfo); #else environmentIrradiance = computeEnvironmentIrradiance(irradianceVector); #endif diff --git a/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockSheen.fx b/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockSheen.fx index 92f76ae4dc17..d837b3670909 100644 --- a/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockSheen.fx +++ b/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockSheen.fx @@ -24,103 +24,69 @@ }; #define pbr_inline - fn sheenBlock(vSheenColor - : vec4f -#ifdef SHEEN_ROUGHNESS - , - vSheenRoughness - : f32 + fn sheenBlock( + vSheenColor: vec4f + #ifdef SHEEN_ROUGHNESS + , vSheenRoughness: f32 #if defined(SHEEN_TEXTURE_ROUGHNESS) && !defined(SHEEN_USE_ROUGHNESS_FROM_MAINTEXTURE) - , - sheenMapRoughnessData - : vec4f + , sheenMapRoughnessData: vec4f #endif #endif - , - roughness - : f32 + , roughness: f32 #ifdef SHEEN_TEXTURE - , - sheenMapData - : vec4f, sheenMapLevel - : f32 + , sheenMapData: vec4f + , sheenMapLevel: f32 #endif - , - reflectance - : f32 + , reflectance: f32 #ifdef SHEEN_LINKWITHALBEDO - , - baseColor - : vec3f, surfaceAlbedo - : vec3f + , baseColor: vec3f + , surfaceAlbedo: vec3f #endif #ifdef ENVIRONMENTBRDF - , - NdotV - : f32, environmentBrdf - : vec3f + , NdotV: f32 + , environmentBrdf: vec3f #endif #if defined(REFLECTION) && defined(ENVIRONMENTBRDF) - , - AARoughnessFactors - : vec2f, vReflectionMicrosurfaceInfos - : vec3f, vReflectionInfos - : vec2f, vReflectionColor - : vec3f, vLightingIntensity - : vec4f + , AARoughnessFactors: vec2f + , vReflectionMicrosurfaceInfos: vec3f + , vReflectionInfos: vec2f + , vReflectionColor: vec3f + , vLightingIntensity: vec4f #ifdef REFLECTIONMAP_3D - , - reflectionSampler - : texture_cube, reflectionSamplerSampler - : sampler, reflectionCoords - : vec3f + , reflectionSampler: texture_cube + , reflectionSamplerSampler: sampler + , reflectionCoords: vec3f #else , reflectionSampler: texture_2d , reflectionSamplerSampler: sampler , reflectionCoords: vec2f #endif - , - NdotVUnclamped - : f32 + , NdotVUnclamped: f32 #ifndef LODBASEDMICROSFURACE #ifdef REFLECTIONMAP_3D - , - reflectionLowSampler - : texture_cube, reflectionLowSamplerSampler - : sampler, reflectionHighSampler - : texture_cube, reflectionHighSamplerSampler - : sampler + , reflectionLowSampler: texture_cube + , reflectionLowSamplerSampler: sampler + , reflectionHighSampler: texture_cube + , reflectionHighSamplerSampler: sampler #else - , - reflectionLowSampler - : texture_2d, reflectionLowSamplerSampler - : sampler, reflectionHighSampler - : texture_2d, reflectionHighSamplerSampler - : sampler + , reflectionLowSampler: texture_2d + , reflectionLowSamplerSampler: sampler + , reflectionHighSampler: texture_2d + , reflectionHighSamplerSampler: sampler #endif #endif #ifdef REALTIME_FILTERING - , - vReflectionFilteringInfo - : vec2f, icdfxSampler - : texture_2d, icdfxSamplerSampler - : sampler, icdfySampler - : texture_2d, icdfySamplerSampler - : sampler -#endif + , vReflectionFilteringInfo: vec2f + #endif #if !defined(REFLECTIONMAP_SKYBOX) && defined(RADIANCEOCCLUSION) - , - seo - : f32 + , seo: f32 #endif #if !defined(REFLECTIONMAP_SKYBOX) && defined(HORIZONOCCLUSION) && defined(BUMP) && defined(REFLECTIONMAP_3D) - , - eho - : f32 + , eho: f32 #endif #endif - ) - ->sheenOutParams { + ) -> sheenOutParams + { var outParams: sheenOutParams; var sheenIntensity: f32 = vSheenColor.a; @@ -199,31 +165,28 @@ var environmentSheenRadiance: vec4f = vec4f(0., 0., 0., 0.); environmentSheenRadiance = sampleReflectionTexture( - sheenAlphaG, vReflectionMicrosurfaceInfos, vReflectionInfos, - vReflectionColor -#if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX) - , - NdotVUnclamped + sheenAlphaG + , vReflectionMicrosurfaceInfos + , vReflectionInfos + , vReflectionColor + #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX) + , NdotVUnclamped #endif #ifdef LINEARSPECULARREFLECTION - , - sheenRoughness + , sheenRoughness #endif - , - reflectionSampler, reflectionSamplerSampler, reflectionCoords + , reflectionSampler + , reflectionSamplerSampler + , reflectionCoords #ifndef LODBASEDMICROSFURACE - , - reflectionLowSampler, reflectionLowSamplerSampler, - reflectionHighSampler, reflectionHighSamplerSampler + , reflectionLowSampler + , reflectionLowSamplerSampler + , reflectionHighSampler + , reflectionHighSamplerSampler #endif #ifdef REALTIME_FILTERING - , - vReflectionFilteringInfo, icdfxSampler - : texture_2d, icdfxSamplerSampler - : sampler, icdfySampler - : texture_2d, icdfySamplerSampler - : sampler -#endif + , vReflectionFilteringInfo + #endif ); var sheenEnvironmentReflectance: vec3f = getSheenReflectanceFromBRDFLookup(sheenColor, environmentSheenBrdf); diff --git a/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockSubSurface.fx b/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockSubSurface.fx index b52bc77ef126..fc02b76a296c 100644 --- a/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockSubSurface.fx +++ b/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrBlockSubSurface.fx @@ -32,527 +32,431 @@ struct subSurfaceOutParams #ifdef SUBSURFACE #ifdef SS_REFRACTION #define pbr_inline -fn sampleEnvironmentRefraction(ior - : f32, thickness - : f32, refractionLOD - : f32, normalW - : vec3f, vPositionW - : vec3f, viewDirectionW - : vec3f, view - : mat4x4f, vRefractionInfos - : vec4f, refractionMatrix - : mat4x4f, vRefractionMicrosurfaceInfos - : vec4f, alphaG - : f32 -#ifdef SS_REFRACTIONMAP_3D - , - refractionSampler - : texture_cube, refractionSamplerSampler - : sampler -#ifndef LODBASEDMICROSFURACE - , - refractionLowSampler - : texture_cube, refractionLowSamplerSampler - : sampler, refractionHighSampler - : texture_cube, refractionHighSamplerSampler - : sampler -#endif -#else - , - refractionSampler - : texture_2d, refractionSamplerSampler - : sampler -#ifndef LODBASEDMICROSFURACE - , - refractionLowSampler - : texture_2d, refractionLowSamplerSampler - : sampler, refractionHighSampler - : texture_2d, refractionHighSamplerSampler - : sampler -#endif -#endif -#ifdef ANISOTROPIC - , - anisotropicOut - : anisotropicOutParams -#endif -#ifdef REALTIME_FILTERING - , - vRefractionFilteringInfo - : vec2f, icdfxSampler - : texture_2d, icdfxSamplerSampler - : sampler, icdfySampler - : texture_2d, icdfySamplerSampler - : sampler -#endif -#ifdef SS_USE_LOCAL_REFRACTIONMAP_CUBIC - , - refractionPosition - : vec3f, refractionSize - : vec3f -#endif - ) - ->vec4f { - var environmentRefraction : vec4f = vec4f(0., 0., 0., 0.); -#ifdef ANISOTROPIC - var refractionVector - : vec3f = refract(-viewDirectionW, anisotropicOut.anisotropicNormal, ior); -#else - var refractionVector : vec3f = refract(-viewDirectionW, normalW, ior); -#endif - -#ifdef SS_REFRACTIONMAP_OPPOSITEZ - refractionVector.z *= -1.0; -#endif - -// _____________________________ 2D vs 3D Maps ________________________________ -#ifdef SS_REFRACTIONMAP_3D -#ifdef SS_USE_LOCAL_REFRACTIONMAP_CUBIC - refractionVector = parallaxCorrectNormal(vPositionW, refractionVector, - refractionSize, refractionPosition); -#endif - refractionVector.y = refractionVector.y * vRefractionInfos.w; - var refractionCoords : vec3f = refractionVector; - refractionCoords = (refractionMatrix * vec4f(refractionCoords, 0)).xyz; -#else -#ifdef SS_USE_THICKNESS_AS_DEPTH - var vRefractionUVW - : vec3f = (refractionMatrix * - (view * vec4f(vPositionW + refractionVector * thickness, 1.0))) - .xyz; -#else - var vRefractionUVW - : vec3f = - (refractionMatrix * - (view * - vec4f(vPositionW + refractionVector * vRefractionInfos.z, 1.0))) - .xyz; -#endif - var refractionCoords : vec2f = vRefractionUVW.xy / vRefractionUVW.z; - refractionCoords.y = 1.0 - refractionCoords.y; -#endif - -#ifdef LODBASEDMICROSFURACE - // Apply environment convolution scale/offset filter tuning parameters to the - // mipmap LOD selection - var lod = refractionLOD * vRefractionMicrosurfaceInfos.y + - vRefractionMicrosurfaceInfos.z; - -#ifdef SS_LODINREFRACTIONALPHA - // Automatic LOD adjustment to ensure that the smoothness-based environment - // LOD selection is constrained to appropriate LOD levels in order to prevent - // aliasing. The environment map is first sampled without custom LOD selection - // to determine the hardware-selected LOD, and this is then used to constrain - // the final LOD selection so that excessive surface smoothness does not cause - // aliasing (e.g. on curved geometry where the normal is varying rapidly). - - // Note: Shader Model 4.1 or higher can provide this directly via - // CalculateLevelOfDetail(), and manual calculation via derivatives is also - // possible, but for simplicity we use the hardware LOD calculation with the - // alpha channel containing the LOD for each mipmap. - var automaticRefractionLOD - : f32 = - UNPACK_LOD(textureSample(refractionSampler, - refractionSamplerSampler, refractionCoords) - .a); - var requestedRefractionLOD : f32 = max(automaticRefractionLOD, lod); -#else - var requestedRefractionLOD : f32 = lod; -#endif - -#if defined(REALTIME_FILTERING) && defined(SS_REFRACTIONMAP_3D) - environmentRefraction = - vec4f(radiance(alphaG, refractionSampler, refractionSamplerSampler, - refractionCoords, vRefractionFilteringInfo, icdfxSampler, - icdfxSamplerSampler, icdfySampler, icdfySamplerSampler), - 1.0); -#else - environmentRefraction = - textureSampleLevel(refractionSampler, refractionSamplerSampler, - refractionCoords, requestedRefractionLOD); -#endif -#else - var lodRefractionNormalized - : f32 = saturate(refractionLOD / log2(vRefractionMicrosurfaceInfos.x)); - var lodRefractionNormalizedDoubled : f32 = lodRefractionNormalized * 2.0; - - var environmentRefractionMid - : vec4f = textureSample(refractionSampler, refractionSamplerSampler, - refractionCoords); - if (lodRefractionNormalizedDoubled < 1.0) { - environmentRefraction = - mix(textureSample(refractionHighSampler, refractionHighSamplerSampler, - refractionCoords), - environmentRefractionMid, lodRefractionNormalizedDoubled); - } else { - environmentRefraction = - mix(environmentRefractionMid, - textureSample(refractionLowSampler, refractionLowSamplerSampler, - refractionCoords), - lodRefractionNormalizedDoubled - 1.0); - } -#endif - - var refraction = environmentRefraction.rgb; - -#ifdef SS_RGBDREFRACTION - refraction = fromRGBD(environmentRefraction); -#endif - -#ifdef SS_GAMMAREFRACTION - refraction = toLinearSpaceVec3(environmentRefraction.rgb); -#endif - - return vec4f(refraction, environmentRefraction.a); -} -#endif -#define pbr_inline -fn subSurfaceBlock(vSubSurfaceIntensity - : vec3f, vThicknessParam - : vec2f, vTintColor - : vec4f, normalW - : vec3f, specularEnvironmentReflectance - : vec3f -#ifdef SS_THICKNESSANDMASK_TEXTURE - , - thicknessMap - : vec4f -#endif -#ifdef SS_REFRACTIONINTENSITY_TEXTURE - , - refractionIntensityMap - : vec4f -#endif -#ifdef SS_TRANSLUCENCYINTENSITY_TEXTURE - , - translucencyIntensityMap - : vec4f -#endif -#ifdef REFLECTION -#ifdef SS_TRANSLUCENCY - , - reflectionMatrix - : mat4x4f -#ifdef USESPHERICALFROMREFLECTIONMAP -#if !defined(NORMAL) || !defined(USESPHERICALINVERTEX) - , - irradianceVector_ - : vec3f -#endif -#if defined(REALTIME_FILTERING) - , - reflectionSampler - : texture_cube, reflectionSamplerSampler - : sampler, vReflectionFilteringInfo - : vec2f, icdfxSampler - : texture_2d, icdfxSamplerSampler - : sampler, icdfySampler - : texture_2d, icdfySamplerSampler - : sampler -#endif -#endif -#ifdef USEIRRADIANCEMAP -#ifdef REFLECTIONMAP_3D - , - irradianceSampler - : texture_cube, irradianceSamplerSampler - : sampler -#else - , - irradianceSampler - : texture_2d, irradianceSamplerSampler - : sampler -#endif -#endif -#endif -#endif -#if defined(SS_REFRACTION) || defined(SS_TRANSLUCENCY) - , - surfaceAlbedo - : vec3f -#endif -#ifdef SS_REFRACTION - , - vPositionW - : vec3f, viewDirectionW - : vec3f, view - : mat4x4f, vRefractionInfos - : vec4f, refractionMatrix - : mat4x4f, vRefractionMicrosurfaceInfos - : vec4f, vLightingIntensity - : vec4f -#ifdef SS_LINKREFRACTIONTOTRANSPARENCY - , - alpha - : f32 -#endif -#ifdef SS_LODINREFRACTIONALPHA - , - NdotVUnclamped - : f32 -#endif -#ifdef SS_LINEARSPECULARREFRACTION - , - roughness - : f32 -#endif - , - alphaG - : f32 -#ifdef SS_REFRACTIONMAP_3D - , - refractionSampler - : texture_cube, refractionSamplerSampler - : sampler -#ifndef LODBASEDMICROSFURACE - , - refractionLowSampler - : texture_cube, refractionLowSamplerSampler - : sampler, refractionHighSampler - : texture_cube, refractionHighSamplerSampler - : sampler -#endif -#else - , - refractionSampler - : texture_2d, refractionSamplerSampler - : sampler -#ifndef LODBASEDMICROSFURACE - , - refractionLowSampler - : texture_2d, refractionLowSamplerSampler - : sampler, refractionHighSampler - : texture_2d, refractionHighSamplerSampler - : sampler -#endif -#endif -#ifdef ANISOTROPIC - , - anisotropicOut - : anisotropicOutParams -#endif -#ifdef REALTIME_FILTERING - , - vRefractionFilteringInfo - : vec2f, icdfxSampler - : texture_2d, icdfxSamplerSampler - : sampler, icdfySampler - : texture_2d, icdfySamplerSampler - : sampler -#endif -#ifdef SS_USE_LOCAL_REFRACTIONMAP_CUBIC - , - refractionPosition - : vec3f, refractionSize - : vec3f -#endif -#ifdef SS_DISPERSION - , - dispersion - : f32 -#endif -#endif -#ifdef SS_TRANSLUCENCY - , - vDiffusionDistance - : vec3f, vTranslucencyColor - : vec4f -#ifdef SS_TRANSLUCENCYCOLOR_TEXTURE - , - translucencyColorMap - : vec4f -#endif -#endif - ) - ->subSurfaceOutParams { - var outParams : subSurfaceOutParams; - outParams.specularEnvironmentReflectance = specularEnvironmentReflectance; - -// ______________________________________________________________________________________ -// _____________________________ Intensities & thickness -// ________________________________ -// ______________________________________________________________________________________ -#ifdef SS_REFRACTION - var refractionIntensity : f32 = vSubSurfaceIntensity.x; -#ifdef SS_LINKREFRACTIONTOTRANSPARENCY - refractionIntensity *= (1.0 - alpha); - // Put alpha back to 1; - outParams.alpha = 1.0; -#endif -#endif + fn sampleEnvironmentRefraction( + ior: f32 + , thickness: f32 + , refractionLOD: f32 + , normalW: vec3f + , vPositionW: vec3f + , viewDirectionW: vec3f + , view: mat4x4f + , vRefractionInfos: vec4f + , refractionMatrix: mat4x4f + , vRefractionMicrosurfaceInfos: vec4f + , alphaG: f32 + #ifdef SS_REFRACTIONMAP_3D + , refractionSampler: texture_cube + , refractionSamplerSampler: sampler + #ifndef LODBASEDMICROSFURACE + , refractionLowSampler: texture_cube + , refractionLowSamplerSampler: sampler + , refractionHighSampler: texture_cube + , refractionHighSamplerSampler: sampler + #endif + #else + , refractionSampler: texture_2d + , refractionSamplerSampler: sampler + #ifndef LODBASEDMICROSFURACE + , refractionLowSampler: texture_2d + , refractionLowSamplerSampler: sampler + , refractionHighSampler: texture_2d + , refractionHighSamplerSampler: sampler + #endif + #endif + #ifdef ANISOTROPIC + , anisotropicOut: anisotropicOutParams + #endif + #ifdef REALTIME_FILTERING + , vRefractionFilteringInfo: vec2f + #ifdef IBL_CDF_FILTERING + , icdfxSampler: texture_2d + , icdfxSamplerSampler: sampler + , icdfySampler: texture_2d + , icdfySamplerSampler: sampler + #endif + #endif + #ifdef SS_USE_LOCAL_REFRACTIONMAP_CUBIC + , refractionPosition: vec3f + , refractionSize: vec3f + #endif + ) -> vec4f { + var environmentRefraction: vec4f = vec4f(0., 0., 0., 0.); + #ifdef ANISOTROPIC + var refractionVector: vec3f = refract(-viewDirectionW, anisotropicOut.anisotropicNormal, ior); + #else + var refractionVector: vec3f = refract(-viewDirectionW, normalW, ior); + #endif + + #ifdef SS_REFRACTIONMAP_OPPOSITEZ + refractionVector.z *= -1.0; + #endif + + // _____________________________ 2D vs 3D Maps ________________________________ + #ifdef SS_REFRACTIONMAP_3D + #ifdef SS_USE_LOCAL_REFRACTIONMAP_CUBIC + refractionVector = parallaxCorrectNormal(vPositionW, refractionVector, refractionSize, refractionPosition); + #endif + refractionVector.y = refractionVector.y * vRefractionInfos.w; + var refractionCoords: vec3f = refractionVector; + refractionCoords = (refractionMatrix * vec4f(refractionCoords, 0)).xyz; + #else + #ifdef SS_USE_THICKNESS_AS_DEPTH + var vRefractionUVW: vec3f = (refractionMatrix * (view * vec4f(vPositionW + refractionVector * thickness, 1.0))).xyz; + #else + var vRefractionUVW: vec3f = (refractionMatrix * (view * vec4f(vPositionW + refractionVector * vRefractionInfos.z, 1.0))).xyz; + #endif + var refractionCoords: vec2f = vRefractionUVW.xy / vRefractionUVW.z; + refractionCoords.y = 1.0 - refractionCoords.y; + #endif + + #ifdef LODBASEDMICROSFURACE + // Apply environment convolution scale/offset filter tuning parameters to the mipmap LOD selection + var lod = refractionLOD * vRefractionMicrosurfaceInfos.y + vRefractionMicrosurfaceInfos.z; + + #ifdef SS_LODINREFRACTIONALPHA + // Automatic LOD adjustment to ensure that the smoothness-based environment LOD selection + // is constrained to appropriate LOD levels in order to prevent aliasing. + // The environment map is first sampled without custom LOD selection to determine + // the hardware-selected LOD, and this is then used to constrain the final LOD selection + // so that excessive surface smoothness does not cause aliasing (e.g. on curved geometry + // where the normal is varying rapidly). + + // Note: Shader Model 4.1 or higher can provide this directly via CalculateLevelOfDetail(), and + // manual calculation via derivatives is also possible, but for simplicity we use the + // hardware LOD calculation with the alpha channel containing the LOD for each mipmap. + var automaticRefractionLOD: f32 = UNPACK_LOD(textureSample(refractionSampler, refractionSamplerSampler, refractionCoords).a); + var requestedRefractionLOD: f32 = max(automaticRefractionLOD, lod); + #else + var requestedRefractionLOD: f32 = lod; + #endif + + #if defined(REALTIME_FILTERING) && defined(SS_REFRACTIONMAP_3D) + environmentRefraction = vec4f(radiance(alphaG, refractionSampler, refractionSamplerSampler, refractionCoords, vRefractionFilteringInfo), 1.0); + #else + environmentRefraction = textureSampleLevel(refractionSampler, refractionSamplerSampler, refractionCoords, requestedRefractionLOD); + #endif + #else + var lodRefractionNormalized: f32 = saturate(refractionLOD / log2(vRefractionMicrosurfaceInfos.x)); + var lodRefractionNormalizedDoubled: f32 = lodRefractionNormalized * 2.0; + + var environmentRefractionMid: vec4f = textureSample(refractionSampler, refractionSamplerSampler, refractionCoords); + if (lodRefractionNormalizedDoubled < 1.0){ + environmentRefraction = mix( + textureSample(refractionHighSampler, refractionHighSamplerSampler, refractionCoords), + environmentRefractionMid, + lodRefractionNormalizedDoubled + ); + } else { + environmentRefraction = mix( + environmentRefractionMid, + textureSample(refractionLowSampler, refractionLowSamplerSampler, refractionCoords), + lodRefractionNormalizedDoubled - 1.0 + ); + } + #endif -#ifdef SS_TRANSLUCENCY - var translucencyIntensity : f32 = vSubSurfaceIntensity.y; -#endif + var refraction = environmentRefraction.rgb; -#ifdef SS_THICKNESSANDMASK_TEXTURE -#ifdef SS_USE_GLTF_TEXTURES - var thickness : f32 = thicknessMap.g * vThicknessParam.y + vThicknessParam.x; -#else - var thickness : f32 = thicknessMap.r * vThicknessParam.y + vThicknessParam.x; -#endif + #ifdef SS_RGBDREFRACTION + refraction = fromRGBD(environmentRefraction); + #endif -#if DEBUGMODE > 0 - outParams.thicknessMap = thicknessMap; -#endif + #ifdef SS_GAMMAREFRACTION + refraction = toLinearSpaceVec3(environmentRefraction.rgb); + #endif -#if defined(SS_REFRACTION) && \ - defined(SS_REFRACTION_USE_INTENSITY_FROM_THICKNESS) -#ifdef SS_USE_GLTF_TEXTURES - refractionIntensity *= thicknessMap.r; -#else - refractionIntensity *= thicknessMap.g; -#endif -#endif - -#if defined(SS_TRANSLUCENCY) && \ - defined(SS_TRANSLUCENCY_USE_INTENSITY_FROM_THICKNESS) -#ifdef SS_USE_GLTF_TEXTURES - translucencyIntensity *= thicknessMap.a; -#else - translucencyIntensity *= thicknessMap.b; -#endif -#endif -#else - var thickness : f32 = vThicknessParam.y; -#endif - -#if defined(SS_REFRACTION) && defined(SS_REFRACTIONINTENSITY_TEXTURE) -#ifdef SS_USE_GLTF_TEXTURES - refractionIntensity *= refractionIntensityMap.r; -#else - refractionIntensity *= refractionIntensityMap.g; -#endif -#endif + return vec4f(refraction, environmentRefraction.a); + } + #endif + #define pbr_inline + fn subSurfaceBlock( + vSubSurfaceIntensity: vec3f + , vThicknessParam: vec2f + , vTintColor: vec4f + , normalW: vec3f + , specularEnvironmentReflectance: vec3f + #ifdef SS_THICKNESSANDMASK_TEXTURE + , thicknessMap: vec4f + #endif + #ifdef SS_REFRACTIONINTENSITY_TEXTURE + , refractionIntensityMap: vec4f + #endif + #ifdef SS_TRANSLUCENCYINTENSITY_TEXTURE + , translucencyIntensityMap: vec4f + #endif + #ifdef REFLECTION + #ifdef SS_TRANSLUCENCY + , reflectionMatrix: mat4x4f + #ifdef USESPHERICALFROMREFLECTIONMAP + #if !defined(NORMAL) || !defined(USESPHERICALINVERTEX) + , irradianceVector_: vec3f + #endif + #if defined(REALTIME_FILTERING) + , reflectionSampler: texture_cube + , reflectionSamplerSampler: sampler + , vReflectionFilteringInfo: vec2f + #ifdef IBL_CDF_FILTERING + , icdfxSampler: texture_2d + , icdfxSamplerSampler: sampler + , icdfySampler: texture_2d + , icdfySamplerSampler: sampler + #endif + #endif + #endif + #ifdef USEIRRADIANCEMAP + #ifdef REFLECTIONMAP_3D + , irradianceSampler: texture_cube + , irradianceSamplerSampler: sampler + #else + , irradianceSampler: texture_2d + , irradianceSamplerSampler: sampler + #endif + #endif + #endif + #endif + #if defined(SS_REFRACTION) || defined(SS_TRANSLUCENCY) + , surfaceAlbedo: vec3f + #endif + #ifdef SS_REFRACTION + , vPositionW: vec3f + , viewDirectionW: vec3f + , view: mat4x4f + , vRefractionInfos: vec4f + , refractionMatrix: mat4x4f + , vRefractionMicrosurfaceInfos: vec4f + , vLightingIntensity: vec4f + #ifdef SS_LINKREFRACTIONTOTRANSPARENCY + , alpha: f32 + #endif + #ifdef SS_LODINREFRACTIONALPHA + , NdotVUnclamped: f32 + #endif + #ifdef SS_LINEARSPECULARREFRACTION + , roughness: f32 + #endif + , alphaG: f32 + #ifdef SS_REFRACTIONMAP_3D + , refractionSampler: texture_cube + , refractionSamplerSampler: sampler + #ifndef LODBASEDMICROSFURACE + , refractionLowSampler: texture_cube + , refractionLowSamplerSampler: sampler + , refractionHighSampler: texture_cube + , refractionHighSamplerSampler: sampler + #endif + #else + , refractionSampler: texture_2d + , refractionSamplerSampler: sampler + #ifndef LODBASEDMICROSFURACE + , refractionLowSampler: texture_2d + , refractionLowSamplerSampler: sampler + , refractionHighSampler: texture_2d + , refractionHighSamplerSampler: sampler + #endif + #endif + #ifdef ANISOTROPIC + , anisotropicOut: anisotropicOutParams + #endif + #ifdef REALTIME_FILTERING + , vRefractionFilteringInfo: vec2f + #ifdef IBL_CDF_FILTERING + , icdfxSampler: texture_2d + , icdfxSamplerSampler: sampler + , icdfySampler: texture_2d + , icdfySamplerSampler: sampler + #endif + #endif + #ifdef SS_USE_LOCAL_REFRACTIONMAP_CUBIC + , refractionPosition: vec3f + , refractionSize: vec3f + #endif + #ifdef SS_DISPERSION + , dispersion: f32 + #endif + #endif + #ifdef SS_TRANSLUCENCY + , vDiffusionDistance: vec3f + , vTranslucencyColor: vec4f + #ifdef SS_TRANSLUCENCYCOLOR_TEXTURE + , translucencyColorMap: vec4f + #endif + #endif + ) -> subSurfaceOutParams + { + var outParams: subSurfaceOutParams; + outParams.specularEnvironmentReflectance = specularEnvironmentReflectance; + + // ______________________________________________________________________________________ + // _____________________________ Intensities & thickness ________________________________ + // ______________________________________________________________________________________ + #ifdef SS_REFRACTION + var refractionIntensity: f32 = vSubSurfaceIntensity.x; + #ifdef SS_LINKREFRACTIONTOTRANSPARENCY + refractionIntensity *= (1.0 - alpha); + // Put alpha back to 1; + outParams.alpha = 1.0; + #endif + #endif -#if defined(SS_TRANSLUCENCY) && defined(SS_TRANSLUCENCYINTENSITY_TEXTURE) -#ifdef SS_USE_GLTF_TEXTURES - translucencyIntensity *= translucencyIntensityMap.a; -#else - translucencyIntensity *= translucencyIntensityMap.b; -#endif -#endif + #ifdef SS_TRANSLUCENCY + var translucencyIntensity: f32 = vSubSurfaceIntensity.y; + #endif -// _________________________________________________________________________________________ -// _____________________________ Translucency transmittance -// ________________________________ -// _________________________________________________________________________________________ -#ifdef SS_TRANSLUCENCY - thickness = maxEps(thickness); - var translucencyColor : vec4f = vTranslucencyColor; -#ifdef SS_TRANSLUCENCYCOLOR_TEXTURE - translucencyColor *= translucencyColorMap; -#endif + #ifdef SS_THICKNESSANDMASK_TEXTURE + #ifdef SS_USE_GLTF_TEXTURES + var thickness: f32 = thicknessMap.g * vThicknessParam.y + vThicknessParam.x; + #else + var thickness: f32 = thicknessMap.r * vThicknessParam.y + vThicknessParam.x; + #endif + + #if DEBUGMODE > 0 + outParams.thicknessMap = thicknessMap; + #endif + + #if defined(SS_REFRACTION) && defined(SS_REFRACTION_USE_INTENSITY_FROM_THICKNESS) + #ifdef SS_USE_GLTF_TEXTURES + refractionIntensity *= thicknessMap.r; + #else + refractionIntensity *= thicknessMap.g; + #endif + #endif + + #if defined(SS_TRANSLUCENCY) && defined(SS_TRANSLUCENCY_USE_INTENSITY_FROM_THICKNESS) + #ifdef SS_USE_GLTF_TEXTURES + translucencyIntensity *= thicknessMap.a; + #else + translucencyIntensity *= thicknessMap.b; + #endif + #endif + #else + var thickness: f32 = vThicknessParam.y; + #endif - var transmittance : vec3f = transmittanceBRDF_Burley( - translucencyColor.rgb, vDiffusionDistance, thickness); - transmittance *= translucencyIntensity; - outParams.transmittance = transmittance; - outParams.translucencyIntensity = translucencyIntensity; -#endif + #if defined(SS_REFRACTION) && defined(SS_REFRACTIONINTENSITY_TEXTURE) + #ifdef SS_USE_GLTF_TEXTURES + refractionIntensity *= refractionIntensityMap.r; + #else + refractionIntensity *= refractionIntensityMap.g; + #endif + #endif -// _____________________________________________________________________________________ -// _____________________________ Refraction environment -// ________________________________ -// _____________________________________________________________________________________ -#ifdef SS_REFRACTION - var environmentRefraction : vec4f = vec4f(0., 0., 0., 0.); - -// vRefractionInfos.y is the IOR of the volume. -// vRefractionMicrosurfaceInfos.w is the IOR of the surface. -#ifdef SS_HAS_THICKNESS - var ior : f32 = vRefractionInfos.y; -#else - var ior : f32 = vRefractionMicrosurfaceInfos.w; -#endif -// Scale roughness with IOR so that an IOR of 1.0 results in no microfacet -// refraction and an IOR of 1.5 results in the default amount of microfacet -// refraction. -#ifdef SS_LODINREFRACTIONALPHA - var refractionAlphaG : f32 = alphaG; - refractionAlphaG = mix(alphaG, 0.0, clamp(ior * 3.0 - 2.0, 0.0, 1.0)); - var refractionLOD - : f32 = getLodFromAlphaGNdotV(vRefractionMicrosurfaceInfos.x, - refractionAlphaG, NdotVUnclamped); -#elif defined(SS_LINEARSPECULARREFRACTION) - var refractionRoughness : f32 = alphaG; - refractionRoughness = mix(alphaG, 0.0, clamp(ior * 3.0 - 2.0, 0.0, 1.0)); - var refractionLOD : f32 = getLinearLodFromRoughness( - vRefractionMicrosurfaceInfos.x, refractionRoughness); -#else - var refractionAlphaG : f32 = alphaG; - refractionAlphaG = mix(alphaG, 0.0, clamp(ior * 3.0 - 2.0, 0.0, 1.0)); - var refractionLOD : f32 = getLodFromAlphaG(vRefractionMicrosurfaceInfos.x, - refractionAlphaG); -#endif + #if defined(SS_TRANSLUCENCY) && defined(SS_TRANSLUCENCYINTENSITY_TEXTURE) + #ifdef SS_USE_GLTF_TEXTURES + translucencyIntensity *= translucencyIntensityMap.a; + #else + translucencyIntensity *= translucencyIntensityMap.b; + #endif + #endif - var refraction_ior : f32 = vRefractionInfos.y; -#ifdef SS_DISPERSION - var realIOR : f32 = 1.0 / refraction_ior; - // The 0.04 value is completely empirical - var iorDispersionSpread : f32 = 0.04 * dispersion * (realIOR - 1.0); - var iors : vec3f = - vec3f(1.0 / (realIOR - iorDispersionSpread), refraction_ior, - 1.0 / (realIOR + iorDispersionSpread)); - for (var i : i32 = 0; i < 3; i++) { - refraction_ior = iors[i]; -#endif - var envSample : vec4f = sampleEnvironmentRefraction( - refraction_ior, thickness, refractionLOD, normalW, - vPositionW, viewDirectionW, view, vRefractionInfos, - refractionMatrix, vRefractionMicrosurfaceInfos, alphaG -#ifdef SS_REFRACTIONMAP_3D - , - refractionSampler, refractionSamplerSampler -#ifndef LODBASEDMICROSFURACE - , - refractionLowSampler, refractionLowSamplerSampler, - refractionHighSampler, refractionHighSamplerSampler -#endif -#else - , - refractionSampler, refractionSamplerSampler -#ifndef LODBASEDMICROSFURACE - , - refractionLowSampler, refractionLowSamplerSampler, - refractionHighSampler, refractionHighSamplerSampler -#endif -#endif -#ifdef ANISOTROPIC - , - anisotropicOut -#endif -#ifdef REALTIME_FILTERING - , - vRefractionFilteringInfo, icdfxSampler - : texture_2d, icdfxSamplerSampler - : sampler, icdfySampler - : texture_2d, icdfySamplerSampler - : sampler -#endif -#ifdef SS_USE_LOCAL_REFRACTIONMAP_CUBIC - , - refractionPosition, refractionSize -#endif - ); + // _________________________________________________________________________________________ + // _____________________________ Translucency transmittance ________________________________ + // _________________________________________________________________________________________ + #ifdef SS_TRANSLUCENCY + thickness = maxEps(thickness); + var translucencyColor: vec4f = vTranslucencyColor; + #ifdef SS_TRANSLUCENCYCOLOR_TEXTURE + translucencyColor *= translucencyColorMap; + #endif + + var transmittance: vec3f = transmittanceBRDF_Burley(translucencyColor.rgb, vDiffusionDistance, thickness); + transmittance *= translucencyIntensity; + outParams.transmittance = transmittance; + outParams.translucencyIntensity = translucencyIntensity; + #endif -#ifdef SS_DISPERSION - environmentRefraction[i] = envSample[i]; - } -#else - environmentRefraction = envSample; -#endif + // _____________________________________________________________________________________ + // _____________________________ Refraction environment ________________________________ + // _____________________________________________________________________________________ + #ifdef SS_REFRACTION + var environmentRefraction: vec4f = vec4f(0., 0., 0., 0.); + + // vRefractionInfos.y is the IOR of the volume. + // vRefractionMicrosurfaceInfos.w is the IOR of the surface. + #ifdef SS_HAS_THICKNESS + var ior: f32 = vRefractionInfos.y; + #else + var ior: f32 = vRefractionMicrosurfaceInfos.w; + #endif + // Scale roughness with IOR so that an IOR of 1.0 results in no microfacet refraction and + // an IOR of 1.5 results in the default amount of microfacet refraction. + #ifdef SS_LODINREFRACTIONALPHA + var refractionAlphaG: f32 = alphaG; + refractionAlphaG = mix(alphaG, 0.0, clamp(ior * 3.0 - 2.0, 0.0, 1.0)); + var refractionLOD: f32 = getLodFromAlphaGNdotV(vRefractionMicrosurfaceInfos.x, refractionAlphaG, NdotVUnclamped); + #elif defined(SS_LINEARSPECULARREFRACTION) + var refractionRoughness: f32 = alphaG; + refractionRoughness = mix(alphaG, 0.0, clamp(ior * 3.0 - 2.0, 0.0, 1.0)); + var refractionLOD: f32 = getLinearLodFromRoughness(vRefractionMicrosurfaceInfos.x, refractionRoughness); + #else + var refractionAlphaG: f32 = alphaG; + refractionAlphaG = mix(alphaG, 0.0, clamp(ior * 3.0 - 2.0, 0.0, 1.0)); + var refractionLOD: f32 = getLodFromAlphaG(vRefractionMicrosurfaceInfos.x, refractionAlphaG); + #endif + + var refraction_ior: f32 = vRefractionInfos.y; + #ifdef SS_DISPERSION + var realIOR: f32 = 1.0 / refraction_ior; + // The 0.04 value is completely empirical + var iorDispersionSpread: f32 = 0.04 * dispersion * (realIOR - 1.0); + var iors: vec3f = vec3f(1.0/(realIOR - iorDispersionSpread), refraction_ior, 1.0/(realIOR + iorDispersionSpread)); + for (var i: i32 = 0; i < 3; i++) { + refraction_ior = iors[i]; + #endif + var envSample: vec4f = sampleEnvironmentRefraction(refraction_ior, thickness, refractionLOD, normalW, vPositionW, viewDirectionW, view, vRefractionInfos, refractionMatrix, vRefractionMicrosurfaceInfos, alphaG + #ifdef SS_REFRACTIONMAP_3D + , refractionSampler + , refractionSamplerSampler + #ifndef LODBASEDMICROSFURACE + , refractionLowSampler + , refractionLowSamplerSampler + , refractionHighSampler + , refractionHighSamplerSampler + #endif + #else + , refractionSampler + , refractionSamplerSampler + #ifndef LODBASEDMICROSFURACE + , refractionLowSampler + , refractionLowSamplerSampler + , refractionHighSampler + , refractionHighSamplerSampler + #endif + #endif + #ifdef ANISOTROPIC + , anisotropicOut + #endif + #ifdef REALTIME_FILTERING + , vRefractionFilteringInfo + #ifdef IBL_CDF_FILTERING + , icdfxSampler + , icdfxSamplerSampler + , icdfySampler + , icdfySamplerSampler + #endif + #endif + #ifdef SS_USE_LOCAL_REFRACTIONMAP_CUBIC + , refractionPosition + , refractionSize + #endif + ); + + #ifdef SS_DISPERSION + environmentRefraction[i] = envSample[i]; + } + #else + environmentRefraction = envSample; + #endif // _____________________________ Levels _____________________________________ environmentRefraction = vec4f(environmentRefraction.rgb * vRefractionInfos.x, environmentRefraction.a); -#endif + #endif // _______________________________________________________________________________ // _____________________________ Final Refraction ________________________________ // _______________________________________________________________________________ -#ifdef SS_REFRACTION + #ifdef SS_REFRACTION var refractionTransmittance: vec3f = vec3f(refractionIntensity); -#ifdef SS_THICKNESSANDMASK_TEXTURE + #ifdef SS_THICKNESSANDMASK_TEXTURE var volumeAlbedo: vec3f = computeColorAtDistanceInMedia(vTintColor.rgb, vTintColor.w); // // Simulate Flat Surface @@ -563,37 +467,34 @@ fn subSurfaceBlock(vSubSurfaceIntensity // thickness *= -NdotRefract; refractionTransmittance *= cocaLambertVec3(volumeAlbedo, thickness); -#elif defined(SS_LINKREFRACTIONTOTRANSPARENCY) - // Tvar the: i32 material with albedo. - var maxChannel - : f32 = max(max(surfaceAlbedo.r, surfaceAlbedo.g), surfaceAlbedo.b); - var volumeAlbedo : vec3f = saturateVec3(maxChannel * surfaceAlbedo); - - // Tvar reflectance: i32 - environmentRefraction = - vec4f(environmentRefraction.rgb * volumeAlbedo, environmentRefraction.a); -#else - // Compute tvar from: i32 min distance only. - var volumeAlbedo - : vec3f = computeColorAtDistanceInMedia(vTintColor.rgb, vTintColor.w); - refractionTransmittance *= cocaLambertVec3(volumeAlbedo, vThicknessParam.y); -#endif + #elif defined(SS_LINKREFRACTIONTOTRANSPARENCY) + // Tvar the: i32 material with albedo. + var maxChannel: f32 = max(max(surfaceAlbedo.r, surfaceAlbedo.g), surfaceAlbedo.b); + var volumeAlbedo: vec3f = saturateVec3(maxChannel * surfaceAlbedo); + + // Tvar reflectance: i32 + environmentRefraction = vec4f(environmentRefraction.rgb * volumeAlbedo, environmentRefraction.a); + #else + // Compute tvar from: i32 min distance only. + var volumeAlbedo: vec3f = computeColorAtDistanceInMedia(vTintColor.rgb, vTintColor.w); + refractionTransmittance *= cocaLambertVec3(volumeAlbedo, vThicknessParam.y); + #endif -#ifdef SS_ALBEDOFORREFRACTIONTINT + #ifdef SS_ALBEDOFORREFRACTIONTINT // Tvar the: i32 transmission with albedo. environmentRefraction = vec4f(environmentRefraction.rgb * surfaceAlbedo.rgb, environmentRefraction.a); -#endif + #endif // Decrease Albedo Contribution outParams.surfaceAlbedo = surfaceAlbedo * (1. - refractionIntensity); -#ifdef REFLECTION + #ifdef REFLECTION // Decrease irradiance Contribution outParams.refractionFactorForIrradiance = (1. - refractionIntensity); //environmentIrradiance *= (1. - refractionIntensity); -#endif + #endif -#ifdef UNUSED_MULTIPLEBOUNCES + #ifdef UNUSED_MULTIPLEBOUNCES // Keeping track in case of back compat issue. // The following code is broken and has never worked cause the mix is reversed. Fixing it // Introduces more reflection at grazing angle than expected and we can not find it back in any @@ -602,81 +503,86 @@ fn subSurfaceBlock(vSubSurfaceIntensity // Add Multiple internal bounces. var bounceSpecularEnvironmentReflectance: vec3f = (2.0 * specularEnvironmentReflectance) / (1.0 + specularEnvironmentReflectance); outParams.specularEnvironmentReflectance = mix(bounceSpecularEnvironmentReflectance, specularEnvironmentReflectance, refractionIntensity); -#endif + #endif // In theory T = 1 - R. refractionTransmittance *= 1.0 - outParams.specularEnvironmentReflectance; -#if DEBUGMODE > 0 + #if DEBUGMODE > 0 outParams.refractionTransmittance = refractionTransmittance; -#endif + #endif outParams.finalRefraction = environmentRefraction.rgb * refractionTransmittance * vLightingIntensity.z; -#if DEBUGMODE > 0 + #if DEBUGMODE > 0 outParams.environmentRefraction = environmentRefraction; -#endif -#endif + #endif + #endif // __________________________________________________________________________________ // _______________________________ IBL Translucency ________________________________ // __________________________________________________________________________________ -#if defined(REFLECTION) && defined(SS_TRANSLUCENCY) -#if defined(NORMAL) && defined(USESPHERICALINVERTEX) || !defined(USESPHERICALFROMREFLECTIONMAP) + #if defined(REFLECTION) && defined(SS_TRANSLUCENCY) + #if defined(NORMAL) && defined(USESPHERICALINVERTEX) || !defined(USESPHERICALFROMREFLECTIONMAP) var irradianceVector: vec3f = (reflectionMatrix * vec4f(normalW, 0)).xyz; -#ifdef REFLECTIONMAP_OPPOSITEZ + #ifdef REFLECTIONMAP_OPPOSITEZ irradianceVector.z *= -1.0; -#endif -#ifdef INVERTCUBICMAP + #endif + #ifdef INVERTCUBICMAP irradianceVector.y *= -1.0; -#endif -#else - var irradianceVector : vec3f = irradianceVector_; -#endif - -#if defined(USESPHERICALFROMREFLECTIONMAP) -#if defined(REALTIME_FILTERING) - var refractionIrradiance: vec3f = irradiance(reflectionSampler, reflectionSamplerSampler, -irradianceVector, vReflectionFilteringInfo, icdfxSampler, icdfySampler); -#else + #endif + #else + var irradianceVector: vec3f = irradianceVector_; + #endif + + #if defined(USESPHERICALFROMREFLECTIONMAP) + #if defined(REALTIME_FILTERING) + var refractionIrradiance: vec3f = irradiance(reflectionSampler, reflectionSamplerSampler, -irradianceVector, vReflectionFilteringInfo + #ifdef IBL_CDF_FILTERING + , icdfxSampler + , icdfxSamplerSampler + , icdfySampler + , icdfySamplerSampler + #endif); + #else var refractionIrradiance: vec3f = computeEnvironmentIrradiance(-irradianceVector); -#endif -#elif defined(USEIRRADIANCEMAP) -#ifdef REFLECTIONMAP_3D - var irradianceCoords : vec3f = irradianceVector; -#else - var irradianceCoords : vec2f = irradianceVector.xy; -#ifdef REFLECTIONMAP_PROJECTION - irradianceCoords /= irradianceVector.z; -#endif - irradianceCoords.y = 1.0 - irradianceCoords.y; -#endif - - var temp : vec4f = textureSample(irradianceSampler, irradianceSamplerSampler, - -irradianceCoords); - var refractionIrradiance = temp.rgb; - -#ifdef RGBDREFLECTION - refractionIrradiance = fromRGBD(temp).rgb; -#endif - -#ifdef GAMMAREFLECTION - refractionIrradiance = toLinearSpaceVec3(refractionIrradiance); -#endif -#else - var refractionIrradiance : vec3f = vec3f(0.); -#endif + #endif + #elif defined(USEIRRADIANCEMAP) + #ifdef REFLECTIONMAP_3D + var irradianceCoords: vec3f = irradianceVector; + #else + var irradianceCoords: vec2f = irradianceVector.xy; + #ifdef REFLECTIONMAP_PROJECTION + irradianceCoords /= irradianceVector.z; + #endif + irradianceCoords.y = 1.0 - irradianceCoords.y; + #endif + + var temp: vec4f = textureSample(irradianceSampler, irradianceSamplerSampler, -irradianceCoords); + var refractionIrradiance = temp.rgb; + + #ifdef RGBDREFLECTION + refractionIrradiance = fromRGBD(temp).rgb; + #endif + + #ifdef GAMMAREFLECTION + refractionIrradiance = toLinearSpaceVec3(refractionIrradiance); + #endif + #else + var refractionIrradiance: vec3f = vec3f(0.); + #endif refractionIrradiance *= transmittance; -#ifdef SS_ALBEDOFORTRANSLUCENCYTINT + #ifdef SS_ALBEDOFORTRANSLUCENCYTINT // Tvar the: i32 transmission with albedo. refractionIrradiance *= surfaceAlbedo.rgb; -#endif + #endif outParams.refractionIrradiance = refractionIrradiance; -#endif + #endif return outParams; -} + } #endif diff --git a/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrFragmentSamplersDeclaration.fx b/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrFragmentSamplersDeclaration.fx index edf0f424c880..dc56f6a93f99 100644 --- a/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrFragmentSamplersDeclaration.fx +++ b/packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrFragmentSamplersDeclaration.fx @@ -123,4 +123,11 @@ #include(_DEFINENAME_,SS_REFRACTIONINTENSITY_TEXTURE,_VARYINGNAME_,RefractionIntensity,_SAMPLERNAME_,refractionIntensity) #include(_DEFINENAME_,SS_TRANSLUCENCYINTENSITY_TEXTURE,_VARYINGNAME_,TranslucencyIntensity,_SAMPLERNAME_,translucencyIntensity) #include(_DEFINENAME_,SS_TRANSLUCENCYCOLOR_TEXTURE,_VARYINGNAME_,TranslucencyColor,_SAMPLERNAME_,translucencyColor) +#endif + +#ifdef IBL_CDF_FILTERING + var icdfxSamplerSampler: sampler; + var icdfxSampler: texture_2d; + var icdfySamplerSampler: sampler; + var icdfySampler: texture_2d; #endif \ No newline at end of file diff --git a/packages/dev/core/src/ShadersWGSL/pbr.fragment.fx b/packages/dev/core/src/ShadersWGSL/pbr.fragment.fx index 773fc6c3896d..494d280f5c2e 100644 --- a/packages/dev/core/src/ShadersWGSL/pbr.fragment.fx +++ b/packages/dev/core/src/ShadersWGSL/pbr.fragment.fx @@ -251,48 +251,51 @@ fn main(input: FragmentInputs) -> FragmentOutputs { var reflectionOut: reflectionOutParams; #ifndef USE_CUSTOM_REFLECTION - reflectionOut = - reflectionBlock(fragmentInputs.vPositionW, normalW, alphaG, - uniforms.vReflectionMicrosurfaceInfos, - uniforms.vReflectionInfos, uniforms.vReflectionColor -#ifdef ANISOTROPIC - , - anisotropicOut + reflectionOut = reflectionBlock( + fragmentInputs.vPositionW + , normalW + , alphaG + , uniforms.vReflectionMicrosurfaceInfos + , uniforms.vReflectionInfos + , uniforms.vReflectionColor + #ifdef ANISOTROPIC + , anisotropicOut #endif #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX) - , - NdotVUnclamped + , NdotVUnclamped #endif #ifdef LINEARSPECULARREFLECTION - , - roughness + , roughness #endif - , - reflectionSampler, reflectionSamplerSampler + , reflectionSampler + , reflectionSamplerSampler #if defined(NORMAL) && defined(USESPHERICALINVERTEX) - , - fragmentInputs.vEnvironmentIrradiance + , fragmentInputs.vEnvironmentIrradiance #endif #ifdef USESPHERICALFROMREFLECTIONMAP #if !defined(NORMAL) || !defined(USESPHERICALINVERTEX) - , - uniforms.reflectionMatrix + , uniforms.reflectionMatrix #endif #endif #ifdef USEIRRADIANCEMAP - , - irradianceSampler, irradianceSamplerSampler + , irradianceSampler + , irradianceSamplerSampler #endif #ifndef LODBASEDMICROSFURACE - , - reflectionLowSampler, reflectionLowSamplerSampler, - reflectionHighSampler, reflectionHighSamplerSampler + , reflectionLowSampler + , reflectionLowSamplerSampler + , reflectionHighSampler + , reflectionHighSamplerSampler #endif #ifdef REALTIME_FILTERING - , - uniforms.vReflectionFilteringInfo, - uniforms.icdfxSampler, uniforms.icdfySampler -#endif + , uniforms.vReflectionFilteringInfo + #ifdef IBL_CDF_FILTERING + , uniforms.icdfxSampler + , uniforms.icdfxSamplerSampler + , uniforms.icdfySampler + , uniforms.icdfySamplerSampler + #endif + #endif ); #else #define CUSTOM_REFLECTION @@ -313,58 +316,61 @@ fn main(input: FragmentInputs) -> FragmentOutputs { var sheenMapRoughnessData: vec4f = textureSample(sheenRoughnessSampler, sheenRoughnessSamplerSampler, fragmentInputs.vSheenRoughnessUV + uvOffset) * uniforms.vSheenInfos.w; #endif - sheenOut = sheenBlock( - uniforms.vSheenColor -#ifdef SHEEN_ROUGHNESS - , - uniforms.vSheenRoughness + sheenOut = sheenBlock( + uniforms.vSheenColor + #ifdef SHEEN_ROUGHNESS + , uniforms.vSheenRoughness #if defined(SHEEN_TEXTURE_ROUGHNESS) && !defined(SHEEN_USE_ROUGHNESS_FROM_MAINTEXTURE) - , - sheenMapRoughnessData + , sheenMapRoughnessData #endif #endif - , - roughness + , roughness #ifdef SHEEN_TEXTURE - , - sheenMapData, uniforms.vSheenInfos.y + , sheenMapData + , uniforms.vSheenInfos.y #endif - , - reflectance + , reflectance #ifdef SHEEN_LINKWITHALBEDO - , - baseColor, surfaceAlbedo + , baseColor + , surfaceAlbedo #endif #ifdef ENVIRONMENTBRDF - , - NdotV, environmentBrdf + , NdotV + , environmentBrdf #endif #if defined(REFLECTION) && defined(ENVIRONMENTBRDF) - , - AARoughnessFactors, uniforms.vReflectionMicrosurfaceInfos, - uniforms.vReflectionInfos, uniforms.vReflectionColor, - uniforms.vLightingIntensity, reflectionSampler, - reflectionSamplerSampler, reflectionOut.reflectionCoords, - NdotVUnclamped + , AARoughnessFactors + , uniforms.vReflectionMicrosurfaceInfos + , uniforms.vReflectionInfos + , uniforms.vReflectionColor + , uniforms.vLightingIntensity + , reflectionSampler + , reflectionSamplerSampler + , reflectionOut.reflectionCoords + , NdotVUnclamped #ifndef LODBASEDMICROSFURACE - , - reflectionLowSampler, reflectionLowSamplerSampler, - reflectionHighSampler, reflectionHighSamplerSampler + , reflectionLowSampler + , reflectionLowSamplerSampler + , reflectionHighSampler + , reflectionHighSamplerSampler #endif #ifdef REALTIME_FILTERING - , - vReflectionFilteringInfo, icdfxSampler, icdfySampler -#endif + , vReflectionFilteringInfo + #ifdef IBL_CDF_FILTERING + , uniforms.icdfxSampler + , uniforms.icdfxSamplerSampler + , uniforms.icdfySampler + , uniforms.icdfySamplerSampler + #endif + #endif #if !defined(REFLECTIONMAP_SKYBOX) && defined(RADIANCEOCCLUSION) - , - seo + , seo #endif #if !defined(REFLECTIONMAP_SKYBOX) && defined(HORIZONOCCLUSION) && defined(BUMP) && defined(REFLECTIONMAP_3D) - , - eho + , eho #endif #endif - ); + ); #ifdef SHEEN_LINKWITHALBEDO surfaceAlbedo = sheenOut.surfaceAlbedo; @@ -427,70 +433,69 @@ fn main(input: FragmentInputs) -> FragmentOutputs { var clearCoatBumpMapData: vec4f = textureSample(clearCoatBumpSampler, clearCoatBumpSamplerSampler, fragmentInputs.vClearCoatBumpUV + uvOffset); #endif - clearcoatOut = clearcoatBlock( - fragmentInputs.vPositionW, geometricNormalW, viewDirectionW, - uniforms.vClearCoatParams -#if defined(CLEARCOAT_TEXTURE_ROUGHNESS) && !defined(CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE) - , - clearCoatMapRoughnessData + clearcoatOut = clearcoatBlock( + fragmentInputs.vPositionW + , geometricNormalW + , viewDirectionW + , uniforms.vClearCoatParams + #if defined(CLEARCOAT_TEXTURE_ROUGHNESS) && !defined(CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE) + , clearCoatMapRoughnessData #endif - , - specularEnvironmentR0 + , specularEnvironmentR0 #ifdef CLEARCOAT_TEXTURE - , - clearCoatMapData + , clearCoatMapData #endif #ifdef CLEARCOAT_TINT - , - uniforms.vClearCoatTintParams, - uniforms.clearCoatColorAtDistance, - uniforms.vClearCoatRefractionParams + , uniforms.vClearCoatTintParams + , uniforms.clearCoatColorAtDistance + , uniforms.vClearCoatRefractionParams #ifdef CLEARCOAT_TINT_TEXTURE - , - clearCoatTintMapData + , clearCoatTintMapData #endif #endif #ifdef CLEARCOAT_BUMP - , - uniforms.vClearCoatBumpInfos, clearCoatBumpMapData, - fragmentInputs.vClearCoatBumpUV + , uniforms.vClearCoatBumpInfos + , clearCoatBumpMapData + , fragmentInputs.vClearCoatBumpUV #if defined(TANGENT) && defined(NORMAL) - , - vTBN + , vTBN #else , uniforms.vClearCoatTangentSpaceParams #endif #ifdef OBJECTSPACE_NORMALMAP - , - uniforms.normalMatrix + , uniforms.normalMatrix #endif #endif #if defined(FORCENORMALFORWARD) && defined(NORMAL) - , - faceNormal + , faceNormal #endif #ifdef REFLECTION - , - uniforms.vReflectionMicrosurfaceInfos, - uniforms.vReflectionInfos, uniforms.vReflectionColor, - uniforms.vLightingIntensity, reflectionSampler, - reflectionSamplerSampler + , uniforms.vReflectionMicrosurfaceInfos + , uniforms.vReflectionInfos + , uniforms.vReflectionColor + , uniforms.vLightingIntensity + , reflectionSampler + , reflectionSamplerSampler #ifndef LODBASEDMICROSFURACE - , - reflectionLowSampler, reflectionLowSamplerSampler, - reflectionHighSampler, reflectionHighSamplerSampler + , reflectionLowSampler + , reflectionLowSamplerSampler + , reflectionHighSampler + , reflectionHighSamplerSampler #endif #ifdef REALTIME_FILTERING - , - uniforms.vReflectionFilteringInfo, uniforms.icdfxSampler, - uniforms.icdfySampler -#endif + , uniforms.vReflectionFilteringInfo + #ifdef IBL_CDF_FILTERING + , uniforms.icdfxSampler + , uniforms.icdfxSamplerSampler + , uniforms.icdfySampler + , uniforms.icdfySamplerSampler + #endif + #endif #endif #if defined(CLEARCOAT_BUMP) || defined(TWOSIDEDLIGHTING) - , - select(-1., 1., fragmentInputs.frontFacing) + , select(-1., 1., fragmentInputs.frontFacing) #endif - ); + ); #else clearcoatOut.specularEnvironmentR0 = specularEnvironmentR0; #endif @@ -518,117 +523,121 @@ fn main(input: FragmentInputs) -> FragmentOutputs { var translucencyColorMap: vec4f = textureSample(translucencyColorSampler, translucencyColorSamplerSampler, fragmentInputs.vTranslucencyColorUV + uvOffset); #endif - subSurfaceOut = subSurfaceBlock( - uniforms.vSubSurfaceIntensity, uniforms.vThicknessParam, - uniforms.vTintColor, normalW, specularEnvironmentReflectance -#ifdef SS_THICKNESSANDMASK_TEXTURE - , - thicknessMap + subSurfaceOut = subSurfaceBlock( + uniforms.vSubSurfaceIntensity + , uniforms.vThicknessParam + , uniforms.vTintColor + , normalW + , specularEnvironmentReflectance + #ifdef SS_THICKNESSANDMASK_TEXTURE + , thicknessMap #endif #ifdef SS_REFRACTIONINTENSITY_TEXTURE - , - refractionIntensityMap + , refractionIntensityMap #endif #ifdef SS_TRANSLUCENCYINTENSITY_TEXTURE - , - translucencyIntensityMap + , translucencyIntensityMap #endif #ifdef REFLECTION #ifdef SS_TRANSLUCENCY - , - uniforms.reflectionMatrix + , uniforms.reflectionMatrix #ifdef USESPHERICALFROMREFLECTIONMAP #if !defined(NORMAL) || !defined(USESPHERICALINVERTEX) - , - reflectionOut.irradianceVector + , reflectionOut.irradianceVector #endif #if defined(REALTIME_FILTERING) - , - reflectionSampler, reflectionSamplerSampler, - vReflectionFilteringInfo, icdfxSampler, icdfySampler -#endif -#endif -#ifdef USEIRRADIANCEMAP - , - irradianceSampler, irradianceSamplerSampler -#endif -#endif -#endif -#if defined(SS_REFRACTION) || defined(SS_TRANSLUCENCY) - , - surfaceAlbedo -#endif -#ifdef SS_REFRACTION - , - fragmentInputs.vPositionW, viewDirectionW, scene.view, - uniforms.vRefractionInfos, uniforms.refractionMatrix, - uniforms.vRefractionMicrosurfaceInfos, - uniforms.vLightingIntensity -#ifdef SS_LINKREFRACTIONTOTRANSPARENCY - , - alpha -#endif -#ifdef SS_LODINREFRACTIONALPHA - , - NdotVUnclamped -#endif -#ifdef SS_LINEARSPECULARREFRACTION - , - roughness -#endif - , - alphaG, refractionSampler, refractionSamplerSampler -#ifndef LODBASEDMICROSFURACE - , - refractionLowSampler, refractionLowSamplerSampler, - refractionHighSampler, refractionHighSamplerSampler -#endif -#ifdef ANISOTROPIC - , - anisotropicOut -#endif -#ifdef REALTIME_FILTERING - , - uniforms.vRefractionFilteringInfo, uniforms.icdfxSampler, - uniforms.icdfySampler -#endif -#ifdef SS_USE_LOCAL_REFRACTIONMAP_CUBIC - , - uniforms.vRefractionPosition, uniforms.vRefractionSize -#endif -#ifdef SS_DISPERSION - , - dispersion -#endif -#endif -#ifdef SS_TRANSLUCENCY - , - uniforms.vDiffusionDistance, uniforms.vTranslucencyColor -#ifdef SS_TRANSLUCENCYCOLOR_TEXTURE - , - translucencyColorMap -#endif -#endif - ); + , reflectionSampler + , reflectionSamplerSampler + , vReflectionFilteringInfo + #ifdef IBL_CDF_FILTERING + , uniforms.icdfxSampler + , uniforms.icdfxSamplerSampler + , uniforms.icdfySampler + , uniforms.icdfySamplerSampler + #endif + #endif + #endif + #ifdef USEIRRADIANCEMAP + , irradianceSampler + , irradianceSamplerSampler + #endif + #endif + #endif + #if defined(SS_REFRACTION) || defined(SS_TRANSLUCENCY) + , surfaceAlbedo + #endif + #ifdef SS_REFRACTION + , fragmentInputs.vPositionW + , viewDirectionW + , scene.view + , uniforms.vRefractionInfos + , uniforms.refractionMatrix + , uniforms.vRefractionMicrosurfaceInfos + , uniforms.vLightingIntensity + #ifdef SS_LINKREFRACTIONTOTRANSPARENCY + , alpha + #endif + #ifdef SS_LODINREFRACTIONALPHA + , NdotVUnclamped + #endif + #ifdef SS_LINEARSPECULARREFRACTION + , roughness + #endif + , alphaG + , refractionSampler + , refractionSamplerSampler + #ifndef LODBASEDMICROSFURACE + , refractionLowSampler + , refractionLowSamplerSampler + , refractionHighSampler + , refractionHighSamplerSampler + #endif + #ifdef ANISOTROPIC + , anisotropicOut + #endif + #ifdef REALTIME_FILTERING + , uniforms.vRefractionFilteringInfo + #ifdef IBL_CDF_FILTERING + , uniforms.icdfxSampler + , uniforms.icdfxSamplerSampler + , uniforms.icdfySampler + , uniforms.icdfySamplerSampler + #endif + #endif + #ifdef SS_USE_LOCAL_REFRACTIONMAP_CUBIC + , uniforms.vRefractionPosition + , uniforms.vRefractionSize + #endif + #ifdef SS_DISPERSION + , dispersion + #endif + #endif + #ifdef SS_TRANSLUCENCY + , uniforms.vDiffusionDistance + , uniforms.vTranslucencyColor + #ifdef SS_TRANSLUCENCYCOLOR_TEXTURE + , translucencyColorMap + #endif + #endif + ); -#ifdef SS_REFRACTION + #ifdef SS_REFRACTION surfaceAlbedo = subSurfaceOut.surfaceAlbedo; -#ifdef SS_LINKREFRACTIONTOTRANSPARENCY + #ifdef SS_LINKREFRACTIONTOTRANSPARENCY alpha = subSurfaceOut.alpha; -#endif -#endif -#else - subSurfaceOut.specularEnvironmentReflectance = - specularEnvironmentReflectance; -#endif + #endif + #endif + #else + subSurfaceOut.specularEnvironmentReflectance = specularEnvironmentReflectance; + #endif // _____________________________ Direct Lighting Info __________________________________ -#include + #include -#include [0..maxSimultaneousLights] + #include[0..maxSimultaneousLights] // _____________________________ Compute Final Lit Components ________________________ -#include + #include #endif // UNLIT #include diff --git a/packages/dev/core/src/scene.ts b/packages/dev/core/src/scene.ts index e4011124c67e..c02b08de5b83 100644 --- a/packages/dev/core/src/scene.ts +++ b/packages/dev/core/src/scene.ts @@ -515,9 +515,6 @@ export class Scene implements IAnimatable, IClipPlanesHolder, IAssetContainer { } this._environmentTexture = value; - if (!this._importanceSamplingRenderer && value) { - this._importanceSamplingRenderer = new ImportanceSamplingRenderer(this); - } if (this._importanceSamplingRenderer && value) { this._importanceSamplingRenderer.iblSource = value; } @@ -525,6 +522,11 @@ export class Scene implements IAnimatable, IClipPlanesHolder, IAssetContainer { } private _useEnvironmentCDFMaps = false; + + /** + * Specifies whether IBL filtering will use the CDF (cumulative distribution function) + * maps for importance sampling. + */ public get useEnvironmentCDFMaps(): boolean { return this._useEnvironmentCDFMaps; } @@ -534,13 +536,20 @@ export class Scene implements IAnimatable, IClipPlanesHolder, IAssetContainer { } this._useEnvironmentCDFMaps = value; - if (this.environmentTexture && this._useEnvironmentCDFMaps && !this._importanceSamplingRenderer) { + if (this._useEnvironmentCDFMaps && !this._importanceSamplingRenderer) { this._importanceSamplingRenderer = new ImportanceSamplingRenderer(this); + } + if (this._useEnvironmentCDFMaps && this.environmentTexture) { this._importanceSamplingRenderer.iblSource = this.environmentTexture; } this.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag); } private _importanceSamplingRenderer: ImportanceSamplingRenderer; + + /** + * The renderer that creates CDF (cumulative distribution function) textures + * for importance sampling. + */ public get importanceSamplingRenderer(): ImportanceSamplingRenderer { return this._importanceSamplingRenderer; }