playcanvas 2.24.0-beta.34 → 2.24.0-beta.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/playcanvas/src/core/core.js +2 -2
- package/build/playcanvas/src/framework/lightmapper/lightmapper.js +3 -0
- package/build/playcanvas/src/scene/lighting/lighting-params.d.ts +58 -0
- package/build/playcanvas/src/scene/lighting/lighting-params.js +35 -1
- package/build/playcanvas/src/scene/lighting/lights-buffer.d.ts +7 -1
- package/build/playcanvas/src/scene/lighting/lights-buffer.js +6 -4
- package/build/playcanvas/src/scene/lighting/world-clusters.js +2 -2
- package/build/playcanvas/src/scene/materials/lit-material-options-builder.js +4 -1
- package/build/playcanvas/src/scene/materials/standard-material-options-builder.js +5 -2
- package/build/playcanvas/src/scene/shader-lib/glsl/chunks/lit/frag/clusteredLight.d.ts +1 -1
- package/build/playcanvas/src/scene/shader-lib/glsl/chunks/lit/frag/clusteredLight.js +58 -32
- package/build/playcanvas/src/scene/shader-lib/glsl/chunks/lit/frag/lighting/lightFunctionLight.d.ts +1 -1
- package/build/playcanvas/src/scene/shader-lib/glsl/chunks/lit/frag/lighting/lightFunctionLight.js +3 -3
- package/build/playcanvas/src/scene/shader-lib/glsl/chunks/lit/frag/ltc.d.ts +1 -1
- package/build/playcanvas/src/scene/shader-lib/glsl/chunks/lit/frag/ltc.js +13 -2
- package/build/playcanvas/src/scene/shader-lib/glsl/chunks/lit/frag/pass-forward/litForwardBackend.d.ts +1 -1
- package/build/playcanvas/src/scene/shader-lib/glsl/chunks/lit/frag/pass-forward/litForwardBackend.js +6 -4
- package/build/playcanvas/src/scene/shader-lib/programs/lit-shader-options.d.ts +8 -0
- package/build/playcanvas/src/scene/shader-lib/programs/lit-shader-options.js +11 -1
- package/build/playcanvas/src/scene/shader-lib/programs/lit-shader.js +10 -1
- package/build/playcanvas/src/scene/shader-lib/wgsl/chunks/lit/frag/clusteredLight.d.ts +1 -1
- package/build/playcanvas/src/scene/shader-lib/wgsl/chunks/lit/frag/clusteredLight.js +59 -32
- package/build/playcanvas/src/scene/shader-lib/wgsl/chunks/lit/frag/lighting/lightFunctionLight.d.ts +1 -1
- package/build/playcanvas/src/scene/shader-lib/wgsl/chunks/lit/frag/lighting/lightFunctionLight.js +3 -3
- package/build/playcanvas/src/scene/shader-lib/wgsl/chunks/lit/frag/ltc.d.ts +1 -1
- package/build/playcanvas/src/scene/shader-lib/wgsl/chunks/lit/frag/ltc.js +13 -2
- package/build/playcanvas/src/scene/shader-lib/wgsl/chunks/lit/frag/pass-forward/litForwardBackend.d.ts +1 -1
- package/build/playcanvas/src/scene/shader-lib/wgsl/chunks/lit/frag/pass-forward/litForwardBackend.js +6 -4
- package/build/playcanvas.d.ts +73 -1
- package/build/playcanvas.dbg/src/core/core.js +2 -2
- package/build/playcanvas.dbg/src/framework/lightmapper/lightmapper.js +3 -0
- package/build/playcanvas.dbg/src/scene/lighting/lighting-params.js +83 -1
- package/build/playcanvas.dbg/src/scene/lighting/lights-buffer.js +12 -4
- package/build/playcanvas.dbg/src/scene/lighting/world-clusters.js +2 -2
- package/build/playcanvas.dbg/src/scene/materials/lit-material-options-builder.js +4 -1
- package/build/playcanvas.dbg/src/scene/materials/standard-material-options-builder.js +5 -2
- package/build/playcanvas.dbg/src/scene/shader-lib/glsl/chunks/lit/frag/clusteredLight.js +75 -39
- package/build/playcanvas.dbg/src/scene/shader-lib/glsl/chunks/lit/frag/lighting/lightFunctionLight.js +4 -3
- package/build/playcanvas.dbg/src/scene/shader-lib/glsl/chunks/lit/frag/ltc.js +17 -2
- package/build/playcanvas.dbg/src/scene/shader-lib/glsl/chunks/lit/frag/pass-forward/litForwardBackend.js +10 -6
- package/build/playcanvas.dbg/src/scene/shader-lib/programs/lit-shader-options.js +18 -1
- package/build/playcanvas.dbg/src/scene/shader-lib/programs/lit-shader.js +10 -1
- package/build/playcanvas.dbg/src/scene/shader-lib/wgsl/chunks/lit/frag/clusteredLight.js +76 -39
- package/build/playcanvas.dbg/src/scene/shader-lib/wgsl/chunks/lit/frag/lighting/lightFunctionLight.js +4 -3
- package/build/playcanvas.dbg/src/scene/shader-lib/wgsl/chunks/lit/frag/ltc.js +17 -2
- package/build/playcanvas.dbg/src/scene/shader-lib/wgsl/chunks/lit/frag/pass-forward/litForwardBackend.js +10 -6
- package/build/playcanvas.dbg.js +339 -113
- package/build/playcanvas.dbg.mjs +339 -113
- package/build/playcanvas.js +226 -95
- package/build/playcanvas.min.js +254 -175
- package/build/playcanvas.min.mjs +256 -177
- package/build/playcanvas.mjs +226 -95
- package/build/playcanvas.prf/src/core/core.js +2 -2
- package/build/playcanvas.prf/src/framework/lightmapper/lightmapper.js +3 -0
- package/build/playcanvas.prf/src/scene/lighting/lighting-params.js +35 -1
- package/build/playcanvas.prf/src/scene/lighting/lights-buffer.js +6 -4
- package/build/playcanvas.prf/src/scene/lighting/world-clusters.js +2 -2
- package/build/playcanvas.prf/src/scene/materials/lit-material-options-builder.js +4 -1
- package/build/playcanvas.prf/src/scene/materials/standard-material-options-builder.js +5 -2
- package/build/playcanvas.prf/src/scene/shader-lib/glsl/chunks/lit/frag/clusteredLight.js +58 -32
- package/build/playcanvas.prf/src/scene/shader-lib/glsl/chunks/lit/frag/lighting/lightFunctionLight.js +3 -3
- package/build/playcanvas.prf/src/scene/shader-lib/glsl/chunks/lit/frag/ltc.js +13 -2
- package/build/playcanvas.prf/src/scene/shader-lib/glsl/chunks/lit/frag/pass-forward/litForwardBackend.js +6 -4
- package/build/playcanvas.prf/src/scene/shader-lib/programs/lit-shader-options.js +11 -1
- package/build/playcanvas.prf/src/scene/shader-lib/programs/lit-shader.js +10 -1
- package/build/playcanvas.prf/src/scene/shader-lib/wgsl/chunks/lit/frag/clusteredLight.js +59 -32
- package/build/playcanvas.prf/src/scene/shader-lib/wgsl/chunks/lit/frag/lighting/lightFunctionLight.js +3 -3
- package/build/playcanvas.prf/src/scene/shader-lib/wgsl/chunks/lit/frag/ltc.js +13 -2
- package/build/playcanvas.prf/src/scene/shader-lib/wgsl/chunks/lit/frag/pass-forward/litForwardBackend.js +6 -4
- package/build/playcanvas.prf.js +226 -95
- package/build/playcanvas.prf.mjs +226 -95
- package/package.json +1 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: "\n// Real-Time Polygonal-Light Shading with Linearly Transformed Cosines\n// by Eric Heitz, Jonathan Dupuy, Stephen Hill and David Neubelt\n// code: https://github.com/selfshadow/ltc_code/\n\nmat3 transposeMat3( const in mat3 m ) {\n mat3 tmp;\n tmp[ 0 ] = vec3( m[ 0 ].x, m[ 1 ].x, m[ 2 ].x );\n tmp[ 1 ] = vec3( m[ 0 ].y, m[ 1 ].y, m[ 2 ].y );\n tmp[ 2 ] = vec3( m[ 0 ].z, m[ 1 ].z, m[ 2 ].z );\n return tmp;\n}\n\nvec2 LTC_Uv( const in vec3 N, const in vec3 V, const in float roughness ) {\n const float LUT_SIZE = 64.0;\n const float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;\n const float LUT_BIAS = 0.5 / LUT_SIZE;\n float dotNV = saturate( dot( N, V ) );\n // texture parameterized by sqrt( GGX alpha ) and sqrt( 1 - cos( theta ) )\n vec2 uv = vec2( roughness, sqrt( 1.0 - dotNV ) );\n uv = uv * LUT_SCALE + LUT_BIAS;\n return uv;\n}\n\nfloat LTC_ClippedSphereFormFactor( const in vec3 f ) {\n // Real-Time Area Lighting: a Journey from Research to Production (p.102)\n // An approximation of the form factor of a horizon-clipped rectangle.\n float l = length( f );\n return max( ( l * l + f.z ) / ( l + 1.0 ), 0.0 );\n}\n\nvec3 LTC_EdgeVectorFormFactor( const in vec3 v1, const in vec3 v2 ) {\n float x = dot( v1, v2 );\n float y = abs( x );\n // rational polynomial approximation to theta / sin( theta ) / 2PI\n float a = 0.8543985 + ( 0.4965155 + 0.0145206 * y ) * y;\n float b = 3.4175940 + ( 4.1616724 + y ) * y;\n float v = a / b;\n float theta_sintheta = ( x > 0.0 ) ? v : 0.5 * inversesqrt( max( 1.0 - x * x, 1e-7 ) ) - v;\n return cross( v1, v2 ) * theta_sintheta;\n}\n\nstruct Coords {\n vec3 coord0;\n vec3 coord1;\n vec3 coord2;\n vec3 coord3;\n};\n\nfloat LTC_EvaluateRect( const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in Coords rectCoords) {\n // bail if point is on back side of plane of light\n // assumes ccw winding order of light vertices\n vec3 v1 = rectCoords.coord1 - rectCoords.coord0;\n vec3 v2 = rectCoords.coord3 - rectCoords.coord0;\n \n vec3 lightNormal = cross( v1, v2 );\n // if( dot( lightNormal, P - rectCoords.coord0 ) < 0.0 ) return 0.0;\n float factor = sign(-dot( lightNormal, P - rectCoords.coord0 ));\n\n // construct orthonormal basis around N\n vec3 T1, T2;\n T1 = normalize( V - N * dot( V, N ) );\n T2 = factor * cross( N, T1 ); // negated from paper; possibly due to a different handedness of world coordinate system\n // compute transform\n mat3 mat = mInv * transposeMat3( mat3( T1, T2, N ) );\n // transform rect\n vec3 coords[ 4 ];\n coords[ 0 ] = mat * ( rectCoords.coord0 - P );\n coords[ 1 ] = mat * ( rectCoords.coord1 - P );\n coords[ 2 ] = mat * ( rectCoords.coord2 - P );\n coords[ 3 ] = mat * ( rectCoords.coord3 - P );\n // project rect onto sphere\n coords[ 0 ] = normalize( coords[ 0 ] );\n coords[ 1 ] = normalize( coords[ 1 ] );\n coords[ 2 ] = normalize( coords[ 2 ] );\n coords[ 3 ] = normalize( coords[ 3 ] );\n // calculate vector form factor\n vec3 vectorFormFactor = vec3( 0.0 );\n vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 0 ], coords[ 1 ] );\n vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 1 ], coords[ 2 ] );\n vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 2 ], coords[ 3 ] );\n vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 3 ], coords[ 0 ] );\n // adjust for horizon clipping\n float result = LTC_ClippedSphereFormFactor( vectorFormFactor );\n\n return result;\n}\n\nCoords dLTCCoords;\nCoords getLTCLightCoords(vec3 lightPos, vec3 halfWidth, vec3 halfHeight){\n Coords coords;\n coords.coord0 = lightPos + halfWidth - halfHeight;\n coords.coord1 = lightPos - halfWidth - halfHeight;\n coords.coord2 = lightPos - halfWidth + halfHeight;\n coords.coord3 = lightPos + halfWidth + halfHeight;\n return coords;\n}\n\nfloat dSphereRadius;\nCoords getSphereLightCoords(vec3 lightPos, vec3 halfWidth, vec3 halfHeight){\n // used for simple sphere light falloff\n // also, the code only handles a spherical light, it cannot be non-uniformly scaled in world space, and so we enforce it here\n dSphereRadius = max(length(halfWidth), length(halfHeight));\n\n // Billboard the 2d light quad to reflection vector, as it's used for specular. This allows us to use disk math for the sphere.\n vec3 f = reflect(normalize(lightPos - view_position), vNormalW);\n vec3 w = normalize(cross(f, halfHeight));\n vec3 h = normalize(cross(f, w));\n\n return getLTCLightCoords(lightPos, w * dSphereRadius, h * dSphereRadius);\n}\n\n// used for LTC LUT texture lookup\nvec2 dLTCUV;\n#ifdef LIT_CLEARCOAT\n vec2 ccLTCUV;\n#endif\n\nvec2 getLTCLightUV(float gloss, vec3 worldNormal, vec3 viewDir)\n{\n float roughness = max((1.0 - gloss) * (1.0 - gloss), 0.001);\n return LTC_Uv( worldNormal, viewDir, roughness );\n}\n\n// used for energy conservation and to modulate specular\nvec3 dLTCSpecFres;\n#ifdef LIT_CLEARCOAT\n vec3 ccLTCSpecFres;\n#endif\n\nvec3 getLTCLightSpecFres(vec2 uv, vec3 specularity)\n{\n vec4 t2 = texture2DLod(areaLightsLutTex2, uv, 0.0);\n return specularity * t2.x + ( vec3( 1.0 ) - specularity) * t2.y;\n}\n\nvoid calcLTCLightValues(float gloss, vec3 worldNormal, vec3 viewDir, vec3 specularity, float clearcoatGloss, vec3 clearcoatWorldNormal, float clearcoatSpecularity)\n{\n dLTCUV = getLTCLightUV(gloss, worldNormal, viewDir);\n dLTCSpecFres = getLTCLightSpecFres(dLTCUV, specularity); \n\n#ifdef LIT_CLEARCOAT\n ccLTCUV = getLTCLightUV(clearcoatGloss, clearcoatWorldNormal, viewDir);\n ccLTCSpecFres = getLTCLightSpecFres(ccLTCUV, vec3(clearcoatSpecularity));\n#endif\n}\n\nvoid calcRectLightValues(vec3 lightPos, vec3 halfWidth, vec3 halfHeight) {\n dLTCCoords = getLTCLightCoords(lightPos, halfWidth, halfHeight);\n}\nvoid calcDiskLightValues(vec3 lightPos, vec3 halfWidth, vec3 halfHeight) {\n calcRectLightValues(lightPos, halfWidth, halfHeight);\n}\nvoid calcSphereLightValues(vec3 lightPos, vec3 halfWidth, vec3 halfHeight) {\n dLTCCoords = getSphereLightCoords(lightPos, halfWidth, halfHeight);\n}\n\n// An extended version of the implementation from \"How to solve a cubic equation, revisited\"\n// http://momentsingraphics.de/?p=105\nvec3 SolveCubic(vec4 Coefficient)\n{\n float pi = 3.14159;\n // Normalize the polynomial\n Coefficient.xyz /= Coefficient.w;\n // Divide middle coefficients by three\n Coefficient.yz /= 3.0;\n\n float A = Coefficient.w;\n float B = Coefficient.z;\n float C = Coefficient.y;\n float D = Coefficient.x;\n\n // Compute the Hessian and the discriminant\n vec3 Delta = vec3(\n -Coefficient.z * Coefficient.z + Coefficient.y,\n -Coefficient.y * Coefficient.z + Coefficient.x,\n dot(vec2(Coefficient.z, -Coefficient.y), Coefficient.xy)\n );\n\n float Discriminant = dot(vec2(4.0 * Delta.x, -Delta.y), Delta.zy);\n\n vec2 xlc, xsc;\n\n // Algorithm A\n {\n float A_a = 1.0;\n float C_a = Delta.x;\n float D_a = -2.0 * B * Delta.x + Delta.y;\n\n // Take the cubic root of a normalized complex number\n float Theta = atan(sqrt(Discriminant), -D_a) / 3.0;\n\n float x_1a = 2.0 * sqrt(-C_a) * cos(Theta);\n float x_3a = 2.0 * sqrt(-C_a) * cos(Theta + (2.0 / 3.0) * pi);\n\n float xl;\n if ((x_1a + x_3a) > 2.0 * B)\n xl = x_1a;\n else\n xl = x_3a;\n\n xlc = vec2(xl - B, A);\n }\n\n // Algorithm D\n {\n float A_d = D;\n float C_d = Delta.z;\n float D_d = -D * Delta.y + 2.0 * C * Delta.z;\n\n // Take the cubic root of a normalized complex number\n float Theta = atan(D * sqrt(Discriminant), -D_d) / 3.0;\n\n float x_1d = 2.0 * sqrt(-C_d) * cos(Theta);\n float x_3d = 2.0 * sqrt(-C_d) * cos(Theta + (2.0 / 3.0) * pi);\n\n float xs;\n if (x_1d + x_3d < 2.0 * C)\n xs = x_1d;\n else\n xs = x_3d;\n\n xsc = vec2(-D, xs + C);\n }\n\n float E = xlc.y * xsc.y;\n float F = -xlc.x * xsc.y - xlc.y * xsc.x;\n float G = xlc.x * xsc.x;\n\n vec2 xmc = vec2(C * F - B * G, -B * F + C * E);\n\n vec3 Root = vec3(xsc.x / xsc.y, xmc.x / xmc.y, xlc.x / xlc.y);\n\n if (Root.x < Root.y && Root.x < Root.z)\n Root.xyz = Root.yxz;\n else if (Root.z < Root.x && Root.z < Root.y)\n Root.xyz = Root.xzy;\n\n return Root;\n}\n\nfloat LTC_EvaluateDisk(vec3 N, vec3 V, vec3 P, mat3 Minv, Coords points)\n{\n // construct orthonormal basis around N\n vec3 T1 = normalize(V - N * dot(V, N));\n vec3 T2 = cross(N, T1);\n\n // rotate area light in (T1, T2, N) basis\n //mat3 R = transpose(mat3(T1, T2, N));\n mat3 R = transposeMat3( mat3( T1, T2, N ) );\n // polygon (allocate 5 vertices for clipping)\n vec3 L_[ 3 ];\n L_[ 0 ] = R * ( points.coord0 - P );\n L_[ 1 ] = R * ( points.coord1 - P );\n L_[ 2 ] = R * ( points.coord2 - P );\n\n // init ellipse\n vec3 C = 0.5 * (L_[0] + L_[2]);\n vec3 V1 = 0.5 * (L_[1] - L_[2]);\n vec3 V2 = 0.5 * (L_[1] - L_[0]);\n\n C = Minv * C;\n V1 = Minv * V1;\n V2 = Minv * V2;\n\n //if(dot(cross(V1, V2), C) > 0.0)\n // return 0.0;\n\n // compute eigenvectors of ellipse\n float a, b;\n float d11 = dot(V1, V1);\n float d22 = dot(V2, V2);\n float d12 = dot(V1, V2);\n if (abs(d12) / sqrt(d11 * d22) > 0.0001)\n {\n float tr = d11 + d22;\n float det = -d12 * d12 + d11 * d22;\n\n // use sqrt matrix to solve for eigenvalues\n det = sqrt(det);\n float u = 0.5 * sqrt(tr - 2.0 * det);\n float v = 0.5 * sqrt(tr + 2.0 * det);\n float e_max = (u + v) * (u + v);\n float e_min = (u - v) * (u - v);\n\n vec3 V1_, V2_;\n\n if (d11 > d22)\n {\n V1_ = d12 * V1 + (e_max - d11) * V2;\n V2_ = d12 * V1 + (e_min - d11) * V2;\n }\n else\n {\n V1_ = d12*V2 + (e_max - d22)*V1;\n V2_ = d12*V2 + (e_min - d22)*V1;\n }\n\n a = 1.0 / e_max;\n b = 1.0 / e_min;\n V1 = normalize(V1_);\n V2 = normalize(V2_);\n }\n else\n {\n a = 1.0 / dot(V1, V1);\n b = 1.0 / dot(V2, V2);\n V1 *= sqrt(a);\n V2 *= sqrt(b);\n }\n\n vec3 V3 = normalize(cross(V1, V2));\n if (dot(C, V3) < 0.0)\n V3 *= -1.0;\n\n float L = dot(V3, C);\n float x0 = dot(V1, C) / L;\n float y0 = dot(V2, C) / L;\n\n float E1 = inversesqrt(a);\n float E2 = inversesqrt(b);\n\n a *= L * L;\n b *= L * L;\n\n float c0 = a * b;\n float c1 = a * b * (1.0 + x0 * x0 + y0 * y0) - a - b;\n float c2 = 1.0 - a * (1.0 + x0 * x0) - b * (1.0 + y0 * y0);\n float c3 = 1.0;\n\n vec3 roots = SolveCubic(vec4(c0, c1, c2, c3));\n float e1 = roots.x;\n float e2 = roots.y;\n float e3 = roots.z;\n\n vec3 avgDir = vec3(a * x0 / (a - e2), b * y0 / (b - e2), 1.0);\n\n mat3 rotate = mat3(V1, V2, V3);\n\n avgDir = rotate * avgDir;\n avgDir = normalize(avgDir);\n\n float L1 = sqrt(-e2 / e3);\n float L2 = sqrt(-e2 / e1);\n\n float formFactor = max(0.0, L1 * L2 * inversesqrt((1.0 + L1 * L1) * (1.0 + L2 * L2)));\n \n const float LUT_SIZE = 64.0;\n const float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;\n const float LUT_BIAS = 0.5 / LUT_SIZE;\n\n // use tabulated horizon-clipped sphere\n vec2 uv = vec2(avgDir.z * 0.5 + 0.5, formFactor);\n uv = uv*LUT_SCALE + LUT_BIAS;\n\n float scale = texture2DLod(areaLightsLutTex2, uv, 0.0).w;\n\n return formFactor*scale;\n}\n\n// LTC_EvaluateDisk in some rare cases generates NaN values in a or b, just before 'float c0 = a * b;'\n// Get rid of those Nan values before they propagate further, as in case of bloom / DOF blurs they\n// propagate to large areas. I didn't find the actual reason where those come from, so that is still TODO.\n// Note that only disk/sphere lights are causing it, so only handle those.\nfloat FixNan(float value) {\n #ifdef WEBGPU\n return value != value ? 0.0 : value; // isnan does not transpile correctly, use a workaround\n #else\n return isnan(value) ? 0.0 : value;\n #endif\n}\n\nfloat getRectLightDiffuse(vec3 worldNormal, vec3 viewDir, vec3 lightDir, vec3 lightDirNorm) {\n return LTC_EvaluateRect( worldNormal, viewDir, vPositionW, mat3( 1.0 ), dLTCCoords );\n}\n\nfloat getDiskLightDiffuse(vec3 worldNormal, vec3 viewDir, vec3 lightDir, vec3 lightDirNorm) {\n return FixNan(LTC_EvaluateDisk( worldNormal, viewDir, vPositionW, mat3( 1.0 ), dLTCCoords ));\n}\n\nfloat getSphereLightDiffuse(vec3 worldNormal, vec3 viewDir, vec3 lightDir, vec3 lightDirNorm) {\n // NB: this could be improved further with distance based wrap lighting\n float falloff = dSphereRadius / (dot(lightDir, lightDir) + dSphereRadius);\n return FixNan(getLightDiffuse(worldNormal, viewDir, lightDirNorm) * falloff);\n}\n\nmat3 getLTCLightInvMat(vec2 uv)\n{\n vec4 t1 = texture2DLod(areaLightsLutTex1, uv, 0.0);\n\n return mat3(\n vec3( t1.x, 0, t1.y ),\n vec3( 0, 1, 0 ),\n vec3( t1.z, 0, t1.w )\n );\n}\n\nfloat calcRectLightSpecular(vec3 worldNormal, vec3 viewDir, vec2 uv) {\n mat3 mInv = getLTCLightInvMat(uv);\n return LTC_EvaluateRect( worldNormal, viewDir, vPositionW, mInv, dLTCCoords );\n}\n\nfloat getRectLightSpecular(vec3 worldNormal, vec3 viewDir) {\n return calcRectLightSpecular(worldNormal, viewDir, dLTCUV);\n}\n\nfloat calcDiskLightSpecular(vec3 worldNormal, vec3 viewDir, vec2 uv) {\n mat3 mInv = getLTCLightInvMat(uv);\n return LTC_EvaluateDisk( worldNormal, viewDir, vPositionW, mInv, dLTCCoords );\n}\n\nfloat getDiskLightSpecular(vec3 worldNormal, vec3 viewDir) {\n return calcDiskLightSpecular(worldNormal, viewDir, dLTCUV);\n}\n\nfloat getSphereLightSpecular(vec3 worldNormal, vec3 viewDir) {\n return calcDiskLightSpecular(worldNormal, viewDir, dLTCUV);\n}\n";
|
|
1
|
+
declare const _default: "\n// Real-Time Polygonal-Light Shading with Linearly Transformed Cosines\n// by Eric Heitz, Jonathan Dupuy, Stephen Hill and David Neubelt\n// code: https://github.com/selfshadow/ltc_code/\n\nmat3 transposeMat3( const in mat3 m ) {\n mat3 tmp;\n tmp[ 0 ] = vec3( m[ 0 ].x, m[ 1 ].x, m[ 2 ].x );\n tmp[ 1 ] = vec3( m[ 0 ].y, m[ 1 ].y, m[ 2 ].y );\n tmp[ 2 ] = vec3( m[ 0 ].z, m[ 1 ].z, m[ 2 ].z );\n return tmp;\n}\n\nvec2 LTC_Uv( const in vec3 N, const in vec3 V, const in float roughness ) {\n const float LUT_SIZE = 64.0;\n const float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;\n const float LUT_BIAS = 0.5 / LUT_SIZE;\n float dotNV = saturate( dot( N, V ) );\n // texture parameterized by sqrt( GGX alpha ) and sqrt( 1 - cos( theta ) )\n vec2 uv = vec2( roughness, sqrt( 1.0 - dotNV ) );\n uv = uv * LUT_SCALE + LUT_BIAS;\n return uv;\n}\n\nfloat LTC_ClippedSphereFormFactor( const in vec3 f ) {\n // Real-Time Area Lighting: a Journey from Research to Production (p.102)\n // An approximation of the form factor of a horizon-clipped rectangle.\n float l = length( f );\n return max( ( l * l + f.z ) / ( l + 1.0 ), 0.0 );\n}\n\nvec3 LTC_EdgeVectorFormFactor( const in vec3 v1, const in vec3 v2 ) {\n float x = dot( v1, v2 );\n float y = abs( x );\n // rational polynomial approximation to theta / sin( theta ) / 2PI\n float a = 0.8543985 + ( 0.4965155 + 0.0145206 * y ) * y;\n float b = 3.4175940 + ( 4.1616724 + y ) * y;\n float v = a / b;\n float theta_sintheta = ( x > 0.0 ) ? v : 0.5 * inversesqrt( max( 1.0 - x * x, 1e-7 ) ) - v;\n return cross( v1, v2 ) * theta_sintheta;\n}\n\nstruct Coords {\n vec3 coord0;\n vec3 coord1;\n vec3 coord2;\n vec3 coord3;\n};\n\nfloat LTC_EvaluateRect( const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in Coords rectCoords) {\n // bail if point is on back side of plane of light\n // assumes ccw winding order of light vertices\n vec3 v1 = rectCoords.coord1 - rectCoords.coord0;\n vec3 v2 = rectCoords.coord3 - rectCoords.coord0;\n \n vec3 lightNormal = cross( v1, v2 );\n // if( dot( lightNormal, P - rectCoords.coord0 ) < 0.0 ) return 0.0;\n float factor = sign(-dot( lightNormal, P - rectCoords.coord0 ));\n\n // construct orthonormal basis around N\n vec3 T1, T2;\n T1 = normalize( V - N * dot( V, N ) );\n T2 = factor * cross( N, T1 ); // negated from paper; possibly due to a different handedness of world coordinate system\n // compute transform\n mat3 mat = mInv * transposeMat3( mat3( T1, T2, N ) );\n // transform rect\n vec3 coords[ 4 ];\n coords[ 0 ] = mat * ( rectCoords.coord0 - P );\n coords[ 1 ] = mat * ( rectCoords.coord1 - P );\n coords[ 2 ] = mat * ( rectCoords.coord2 - P );\n coords[ 3 ] = mat * ( rectCoords.coord3 - P );\n // project rect onto sphere\n coords[ 0 ] = normalize( coords[ 0 ] );\n coords[ 1 ] = normalize( coords[ 1 ] );\n coords[ 2 ] = normalize( coords[ 2 ] );\n coords[ 3 ] = normalize( coords[ 3 ] );\n // calculate vector form factor\n vec3 vectorFormFactor = vec3( 0.0 );\n vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 0 ], coords[ 1 ] );\n vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 1 ], coords[ 2 ] );\n vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 2 ], coords[ 3 ] );\n vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 3 ], coords[ 0 ] );\n // adjust for horizon clipping\n float result = LTC_ClippedSphereFormFactor( vectorFormFactor );\n\n return result;\n}\n\nCoords dLTCCoords;\nCoords getLTCLightCoords(vec3 lightPos, vec3 halfWidth, vec3 halfHeight){\n Coords coords;\n coords.coord0 = lightPos + halfWidth - halfHeight;\n coords.coord1 = lightPos - halfWidth - halfHeight;\n coords.coord2 = lightPos - halfWidth + halfHeight;\n coords.coord3 = lightPos + halfWidth + halfHeight;\n return coords;\n}\n\nfloat dSphereRadius;\nCoords getSphereLightCoords(vec3 lightPos, vec3 halfWidth, vec3 halfHeight){\n // used for simple sphere light falloff\n // also, the code only handles a spherical light, it cannot be non-uniformly scaled in world space, and so we enforce it here\n dSphereRadius = max(length(halfWidth), length(halfHeight));\n\n // Billboard the 2d light quad to reflection vector, as it's used for specular. This allows us to use disk math for the sphere.\n vec3 f = reflect(normalize(lightPos - view_position), vNormalW);\n vec3 w = normalize(cross(f, halfHeight));\n vec3 h = normalize(cross(f, w));\n\n return getLTCLightCoords(lightPos, w * dSphereRadius, h * dSphereRadius);\n}\n\n// used for LTC LUT texture lookup\nvec2 dLTCUV;\n#ifdef LIT_CLEARCOAT\n vec2 ccLTCUV;\n#endif\n\nvec2 getLTCLightUV(float gloss, vec3 worldNormal, vec3 viewDir)\n{\n float roughness = max((1.0 - gloss) * (1.0 - gloss), 0.001);\n return LTC_Uv( worldNormal, viewDir, roughness );\n}\n\n// used for energy conservation and to modulate specular\nvec3 dLTCSpecFres;\n#ifdef LIT_CLEARCOAT\n vec3 ccLTCSpecFres;\n#endif\n\nvec3 getLTCLightSpecFres(vec2 uv, vec3 specularity)\n{\n vec4 t2 = texture2DLod(areaLightsLutTex2, uv, 0.0);\n return specularity * t2.x + ( vec3( 1.0 ) - specularity) * t2.y;\n}\n\nvoid calcLTCLightValues(float gloss, vec3 worldNormal, vec3 viewDir, vec3 specularity, float clearcoatGloss, vec3 clearcoatWorldNormal, float clearcoatReflectance)\n{\n dLTCUV = getLTCLightUV(gloss, worldNormal, viewDir);\n dLTCSpecFres = getLTCLightSpecFres(dLTCUV, specularity); \n\n#ifdef LIT_CLEARCOAT\n ccLTCUV = getLTCLightUV(clearcoatGloss, clearcoatWorldNormal, viewDir);\n ccLTCSpecFres = getLTCLightSpecFres(ccLTCUV, vec3(clearcoatReflectance));\n#endif\n}\n\nvoid calcRectLightValues(vec3 lightPos, vec3 halfWidth, vec3 halfHeight) {\n dLTCCoords = getLTCLightCoords(lightPos, halfWidth, halfHeight);\n}\nvoid calcDiskLightValues(vec3 lightPos, vec3 halfWidth, vec3 halfHeight) {\n calcRectLightValues(lightPos, halfWidth, halfHeight);\n}\nvoid calcSphereLightValues(vec3 lightPos, vec3 halfWidth, vec3 halfHeight) {\n dLTCCoords = getSphereLightCoords(lightPos, halfWidth, halfHeight);\n}\n\n// An extended version of the implementation from \"How to solve a cubic equation, revisited\"\n// http://momentsingraphics.de/?p=105\nvec3 SolveCubic(vec4 Coefficient)\n{\n float pi = 3.14159;\n // Normalize the polynomial\n Coefficient.xyz /= Coefficient.w;\n // Divide middle coefficients by three\n Coefficient.yz /= 3.0;\n\n float A = Coefficient.w;\n float B = Coefficient.z;\n float C = Coefficient.y;\n float D = Coefficient.x;\n\n // Compute the Hessian and the discriminant\n vec3 Delta = vec3(\n -Coefficient.z * Coefficient.z + Coefficient.y,\n -Coefficient.y * Coefficient.z + Coefficient.x,\n dot(vec2(Coefficient.z, -Coefficient.y), Coefficient.xy)\n );\n\n float Discriminant = dot(vec2(4.0 * Delta.x, -Delta.y), Delta.zy);\n\n vec2 xlc, xsc;\n\n // Algorithm A\n {\n float A_a = 1.0;\n float C_a = Delta.x;\n float D_a = -2.0 * B * Delta.x + Delta.y;\n\n // Take the cubic root of a normalized complex number\n float Theta = atan(sqrt(Discriminant), -D_a) / 3.0;\n\n float x_1a = 2.0 * sqrt(-C_a) * cos(Theta);\n float x_3a = 2.0 * sqrt(-C_a) * cos(Theta + (2.0 / 3.0) * pi);\n\n float xl;\n if ((x_1a + x_3a) > 2.0 * B)\n xl = x_1a;\n else\n xl = x_3a;\n\n xlc = vec2(xl - B, A);\n }\n\n // Algorithm D\n {\n float A_d = D;\n float C_d = Delta.z;\n float D_d = -D * Delta.y + 2.0 * C * Delta.z;\n\n // Take the cubic root of a normalized complex number\n float Theta = atan(D * sqrt(Discriminant), -D_d) / 3.0;\n\n float x_1d = 2.0 * sqrt(-C_d) * cos(Theta);\n float x_3d = 2.0 * sqrt(-C_d) * cos(Theta + (2.0 / 3.0) * pi);\n\n float xs;\n if (x_1d + x_3d < 2.0 * C)\n xs = x_1d;\n else\n xs = x_3d;\n\n xsc = vec2(-D, xs + C);\n }\n\n float E = xlc.y * xsc.y;\n float F = -xlc.x * xsc.y - xlc.y * xsc.x;\n float G = xlc.x * xsc.x;\n\n vec2 xmc = vec2(C * F - B * G, -B * F + C * E);\n\n vec3 Root = vec3(xsc.x / xsc.y, xmc.x / xmc.y, xlc.x / xlc.y);\n\n if (Root.x < Root.y && Root.x < Root.z)\n Root.xyz = Root.yxz;\n else if (Root.z < Root.x && Root.z < Root.y)\n Root.xyz = Root.xzy;\n\n return Root;\n}\n\nfloat LTC_EvaluateDisk(vec3 N, vec3 V, vec3 P, mat3 Minv, Coords points)\n{\n // construct orthonormal basis around N\n vec3 T1 = normalize(V - N * dot(V, N));\n vec3 T2 = cross(N, T1);\n\n // rotate area light in (T1, T2, N) basis\n //mat3 R = transpose(mat3(T1, T2, N));\n mat3 R = transposeMat3( mat3( T1, T2, N ) );\n // polygon (allocate 5 vertices for clipping)\n vec3 L_[ 3 ];\n L_[ 0 ] = R * ( points.coord0 - P );\n L_[ 1 ] = R * ( points.coord1 - P );\n L_[ 2 ] = R * ( points.coord2 - P );\n\n // init ellipse\n vec3 C = 0.5 * (L_[0] + L_[2]);\n vec3 V1 = 0.5 * (L_[1] - L_[2]);\n vec3 V2 = 0.5 * (L_[1] - L_[0]);\n\n C = Minv * C;\n V1 = Minv * V1;\n V2 = Minv * V2;\n\n //if(dot(cross(V1, V2), C) > 0.0)\n // return 0.0;\n\n // compute eigenvectors of ellipse\n float a, b;\n float d11 = dot(V1, V1);\n float d22 = dot(V2, V2);\n float d12 = dot(V1, V2);\n if (abs(d12) / sqrt(d11 * d22) > 0.0001)\n {\n float tr = d11 + d22;\n float det = -d12 * d12 + d11 * d22;\n\n // use sqrt matrix to solve for eigenvalues\n det = sqrt(det);\n float u = 0.5 * sqrt(tr - 2.0 * det);\n float v = 0.5 * sqrt(tr + 2.0 * det);\n float e_max = (u + v) * (u + v);\n float e_min = (u - v) * (u - v);\n\n vec3 V1_, V2_;\n\n if (d11 > d22)\n {\n V1_ = d12 * V1 + (e_max - d11) * V2;\n V2_ = d12 * V1 + (e_min - d11) * V2;\n }\n else\n {\n V1_ = d12*V2 + (e_max - d22)*V1;\n V2_ = d12*V2 + (e_min - d22)*V1;\n }\n\n a = 1.0 / e_max;\n b = 1.0 / e_min;\n V1 = normalize(V1_);\n V2 = normalize(V2_);\n }\n else\n {\n a = 1.0 / dot(V1, V1);\n b = 1.0 / dot(V2, V2);\n V1 *= sqrt(a);\n V2 *= sqrt(b);\n }\n\n vec3 V3 = normalize(cross(V1, V2));\n if (dot(C, V3) < 0.0)\n V3 *= -1.0;\n\n float L = dot(V3, C);\n float x0 = dot(V1, C) / L;\n float y0 = dot(V2, C) / L;\n\n float E1 = inversesqrt(a);\n float E2 = inversesqrt(b);\n\n a *= L * L;\n b *= L * L;\n\n float c0 = a * b;\n float c1 = a * b * (1.0 + x0 * x0 + y0 * y0) - a - b;\n float c2 = 1.0 - a * (1.0 + x0 * x0) - b * (1.0 + y0 * y0);\n float c3 = 1.0;\n\n vec3 roots = SolveCubic(vec4(c0, c1, c2, c3));\n float e1 = roots.x;\n float e2 = roots.y;\n float e3 = roots.z;\n\n vec3 avgDir = vec3(a * x0 / (a - e2), b * y0 / (b - e2), 1.0);\n\n mat3 rotate = mat3(V1, V2, V3);\n\n avgDir = rotate * avgDir;\n avgDir = normalize(avgDir);\n\n float L1 = sqrt(-e2 / e3);\n float L2 = sqrt(-e2 / e1);\n\n float formFactor = max(0.0, L1 * L2 * inversesqrt((1.0 + L1 * L1) * (1.0 + L2 * L2)));\n \n const float LUT_SIZE = 64.0;\n const float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;\n const float LUT_BIAS = 0.5 / LUT_SIZE;\n\n // use tabulated horizon-clipped sphere\n vec2 uv = vec2(avgDir.z * 0.5 + 0.5, formFactor);\n uv = uv*LUT_SCALE + LUT_BIAS;\n\n float scale = texture2DLod(areaLightsLutTex2, uv, 0.0).w;\n\n return formFactor*scale;\n}\n\n// LTC_EvaluateDisk in some rare cases generates NaN values in a or b, just before 'float c0 = a * b;'\n// Get rid of those Nan values before they propagate further, as in case of bloom / DOF blurs they\n// propagate to large areas. I didn't find the actual reason where those come from, so that is still TODO.\n// Note that only disk/sphere lights are causing it, so only handle those.\nfloat FixNan(float value) {\n #ifdef WEBGPU\n return value != value ? 0.0 : value; // isnan does not transpile correctly, use a workaround\n #else\n return isnan(value) ? 0.0 : value;\n #endif\n}\n\nfloat getRectLightDiffuse(vec3 worldNormal, vec3 viewDir, vec3 lightDir, vec3 lightDirNorm) {\n return LTC_EvaluateRect( worldNormal, viewDir, vPositionW, mat3( 1.0 ), dLTCCoords );\n}\n\nfloat getDiskLightDiffuse(vec3 worldNormal, vec3 viewDir, vec3 lightDir, vec3 lightDirNorm) {\n return FixNan(LTC_EvaluateDisk( worldNormal, viewDir, vPositionW, mat3( 1.0 ), dLTCCoords ));\n}\n\nfloat getSphereLightDiffuse(vec3 worldNormal, vec3 viewDir, vec3 lightDir, vec3 lightDirNorm) {\n // NB: this could be improved further with distance based wrap lighting\n float falloff = dSphereRadius / (dot(lightDir, lightDir) + dSphereRadius);\n return FixNan(getLightDiffuse(worldNormal, viewDir, lightDirNorm) * falloff);\n}\n\nmat3 getLTCLightInvMat(vec2 uv)\n{\n vec4 t1 = texture2DLod(areaLightsLutTex1, uv, 0.0);\n\n return mat3(\n vec3( t1.x, 0, t1.y ),\n vec3( 0, 1, 0 ),\n vec3( t1.z, 0, t1.w )\n );\n}\n\nfloat calcRectLightSpecular(vec3 worldNormal, vec3 viewDir, vec2 uv) {\n mat3 mInv = getLTCLightInvMat(uv);\n return LTC_EvaluateRect( worldNormal, viewDir, vPositionW, mInv, dLTCCoords );\n}\n\nfloat getRectLightSpecular(vec3 worldNormal, vec3 viewDir) {\n return calcRectLightSpecular(worldNormal, viewDir, dLTCUV);\n}\n\nfloat calcDiskLightSpecular(vec3 worldNormal, vec3 viewDir, vec2 uv) {\n mat3 mInv = getLTCLightInvMat(uv);\n return LTC_EvaluateDisk( worldNormal, viewDir, vPositionW, mInv, dLTCCoords );\n}\n\nfloat getDiskLightSpecular(vec3 worldNormal, vec3 viewDir) {\n return calcDiskLightSpecular(worldNormal, viewDir, dLTCUV);\n}\n\nfloat getSphereLightSpecular(vec3 worldNormal, vec3 viewDir) {\n return calcDiskLightSpecular(worldNormal, viewDir, dLTCUV);\n}\n\n#ifdef LIT_CLEARCOAT\n // the clear coat specular, with the LTC matrix of the clear coat gloss and normal\n float getRectLightSpecularCC(vec3 worldNormal, vec3 viewDir) {\n return calcRectLightSpecular(worldNormal, viewDir, ccLTCUV);\n }\n\n float getDiskLightSpecularCC(vec3 worldNormal, vec3 viewDir) {\n return calcDiskLightSpecular(worldNormal, viewDir, ccLTCUV);\n }\n\n float getSphereLightSpecularCC(vec3 worldNormal, vec3 viewDir) {\n return calcDiskLightSpecular(worldNormal, viewDir, ccLTCUV);\n }\n#endif\n";
|
|
2
2
|
export default _default;
|
|
@@ -96,13 +96,13 @@ vec3 getLTCLightSpecFres(vec2 uv, vec3 specularity)
|
|
|
96
96
|
vec4 t2 = texture2DLod(areaLightsLutTex2, uv, 0.0);
|
|
97
97
|
return specularity * t2.x + ( vec3( 1.0 ) - specularity) * t2.y;
|
|
98
98
|
}
|
|
99
|
-
void calcLTCLightValues(float gloss, vec3 worldNormal, vec3 viewDir, vec3 specularity, float clearcoatGloss, vec3 clearcoatWorldNormal, float
|
|
99
|
+
void calcLTCLightValues(float gloss, vec3 worldNormal, vec3 viewDir, vec3 specularity, float clearcoatGloss, vec3 clearcoatWorldNormal, float clearcoatReflectance)
|
|
100
100
|
{
|
|
101
101
|
dLTCUV = getLTCLightUV(gloss, worldNormal, viewDir);
|
|
102
102
|
dLTCSpecFres = getLTCLightSpecFres(dLTCUV, specularity);
|
|
103
103
|
#ifdef LIT_CLEARCOAT
|
|
104
104
|
ccLTCUV = getLTCLightUV(clearcoatGloss, clearcoatWorldNormal, viewDir);
|
|
105
|
-
ccLTCSpecFres = getLTCLightSpecFres(ccLTCUV, vec3(
|
|
105
|
+
ccLTCSpecFres = getLTCLightSpecFres(ccLTCUV, vec3(clearcoatReflectance));
|
|
106
106
|
#endif
|
|
107
107
|
}
|
|
108
108
|
void calcRectLightValues(vec3 lightPos, vec3 halfWidth, vec3 halfHeight) {
|
|
@@ -297,6 +297,17 @@ float getDiskLightSpecular(vec3 worldNormal, vec3 viewDir) {
|
|
|
297
297
|
float getSphereLightSpecular(vec3 worldNormal, vec3 viewDir) {
|
|
298
298
|
return calcDiskLightSpecular(worldNormal, viewDir, dLTCUV);
|
|
299
299
|
}
|
|
300
|
+
#ifdef LIT_CLEARCOAT
|
|
301
|
+
float getRectLightSpecularCC(vec3 worldNormal, vec3 viewDir) {
|
|
302
|
+
return calcRectLightSpecular(worldNormal, viewDir, ccLTCUV);
|
|
303
|
+
}
|
|
304
|
+
float getDiskLightSpecularCC(vec3 worldNormal, vec3 viewDir) {
|
|
305
|
+
return calcDiskLightSpecular(worldNormal, viewDir, ccLTCUV);
|
|
306
|
+
}
|
|
307
|
+
float getSphereLightSpecularCC(vec3 worldNormal, vec3 viewDir) {
|
|
308
|
+
return calcDiskLightSpecular(worldNormal, viewDir, ccLTCUV);
|
|
309
|
+
}
|
|
310
|
+
#endif
|
|
300
311
|
`;
|
|
301
312
|
export {
|
|
302
313
|
ltc_default as default
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: "\nvoid evaluateBackend() {\n\n // apply SSAO during lighting\n #ifdef LIT_SSAO\n litArgs_ao *= texture2DLod(ssaoTexture, gl_FragCoord.xy * ssaoTextureSizeInv, 0.0).r;\n #endif\n\n // transform tangent space normals to world space\n #ifdef LIT_NEEDS_NORMAL\n #ifdef LIT_SPECULAR\n getReflDir(litArgs_worldNormal, dViewDirW, litArgs_gloss, dTBN);\n #endif\n\n #ifdef LIT_CLEARCOAT\n ccReflDirW = normalize(-reflect(dViewDirW, litArgs_clearcoat_worldNormal));\n #endif\n #endif\n\n #if defined(LIT_SPECULAR_OR_REFLECTION) || defined(LIT_REFRACTION)\n #ifdef LIT_METALNESS\n float f0 = 1.0 / litArgs_ior;\n f0 = (f0 - 1.0) / (f0 + 1.0);\n f0 *= f0;\n #ifdef LIT_SPECULARITY_FACTOR\n litArgs_specularity = getSpecularModulate(litArgs_specularity, litArgs_albedo, litArgs_metalness, f0, litArgs_specularityFactor);\n #else\n litArgs_specularity = getSpecularModulate(litArgs_specularity, litArgs_albedo, litArgs_metalness, f0, 1.0);\n #endif\n litArgs_albedo = getAlbedoModulate(litArgs_albedo, litArgs_metalness);\n\n // the diffuse transmission is a dielectric lobe, like the diffuse reflection\n #ifdef LIT_DIFFUSE_TRANSMISSION\n litArgs_diffuseTransmission_color = getAlbedoModulate(litArgs_diffuseTransmission_color, litArgs_metalness);\n #endif\n #endif\n\n #ifdef LIT_IRIDESCENCE\n vec3 iridescenceFresnel = getIridescence(saturate(dot(dViewDirW, litArgs_worldNormal)), litArgs_specularity, litArgs_iridescence_thickness);\n #endif\n #endif\n\n // ambient\n #ifdef LIT_ADD_AMBIENT\n\n // the ambient light arriving at the back of the surface, which the diffuse transmission\n // lets through, is added to the diffuse light first and moved out of it\n #ifdef LIT_DIFFUSE_TRANSMISSION\n addAmbient(-litArgs_worldNormal);\n dDiffuseTransmissionLight = dDiffuseLight;\n dDiffuseLight = vec3(0);\n #endif\n\n addAmbient(litArgs_worldNormal);\n\n #ifdef LIT_SPECULAR\n dDiffuseLight = dDiffuseLight * (1.0 - litArgs_specularity);\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight *= 1.0 - litArgs_specularity;\n #endif\n #endif\n\n // move ambient color out of diffuse (used by Lightmapper, to multiply ambient color by accumulated AO)\n #ifdef LIT_SEPARATE_AMBIENT\n vec3 dAmbientLight = dDiffuseLight;\n dDiffuseLight = vec3(0);\n #endif\n #endif\n\n dDiffuseLight *= litArgs_ambient;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight *= litArgs_ambient;\n #endif\n\n // the ambient occlusion darkens the ambient light here, and with occludeDirect, which is 0 or 1,\n // the direct light as well, once that is added below\n #ifdef LIT_AO\n occludeDiffuse(mix(litArgs_ao, 1.0, material_occludeDirect));\n #endif\n\n #ifdef LIT_LIGHTMAP\n addLightMap(\n litArgs_lightmap, \n litArgs_lightmapDir, \n litArgs_worldNormal, \n dViewDirW, \n dReflDirW, \n litArgs_gloss, \n litArgs_specularity, \n dVertexNormalW,\n dTBN\n #if defined(LIT_IRIDESCENCE)\n , iridescenceFresnel,\n litArgs_iridescence_intensity\n #endif\n );\n #endif\n\n #ifdef LIT_LIGHTING || LIT_REFLECTIONS\n\n #ifdef LIT_REFLECTIONS\n\n #ifdef LIT_CLEARCOAT\n addReflectionCC(ccReflDirW, litArgs_clearcoat_gloss);\n \n #ifdef LIT_SPECULAR_FRESNEL\n ccFresnel = getFresnelCC(dot(dViewDirW, litArgs_clearcoat_worldNormal));\n ccReflection *= ccFresnel;\n #else\n ccFresnel = 0.0;\n #endif\n #endif\n\n #ifdef LIT_SPECULARITY_FACTOR\n ccReflection *= litArgs_specularityFactor;\n #endif\n\n #ifdef LIT_SHEEN\n addReflectionSheen(litArgs_worldNormal, dViewDirW, litArgs_sheen_gloss);\n #endif\n\n // Fresnel has to be applied to reflections\n addReflection(dReflDirW, litArgs_gloss);\n\n #ifdef LIT_FRESNEL_MODEL\n\n dReflection.rgb *= getFresnel(\n dot(dViewDirW, litArgs_worldNormal), \n litArgs_gloss, \n litArgs_specularity\n #if defined(LIT_IRIDESCENCE)\n , iridescenceFresnel,\n litArgs_iridescence_intensity\n #endif\n );\n\n #else\n\n dReflection.rgb *= litArgs_specularity;\n\n #endif\n\n #endif\n\n #ifdef AREA_LIGHTS\n // specular has to be accumulated differently if we want area lights to look correct\n dSpecularLight *= litArgs_specularity;\n\n #ifdef LIT_SPECULAR\n // evaluate material based area lights data, shared by all area lights\n calcLTCLightValues(litArgs_gloss, litArgs_worldNormal, dViewDirW, litArgs_specularity, litArgs_clearcoat_gloss, litArgs_clearcoat_worldNormal,
|
|
1
|
+
declare const _default: "\nvoid evaluateBackend() {\n\n // apply SSAO during lighting\n #ifdef LIT_SSAO\n litArgs_ao *= texture2DLod(ssaoTexture, gl_FragCoord.xy * ssaoTextureSizeInv, 0.0).r;\n #endif\n\n // transform tangent space normals to world space\n #ifdef LIT_NEEDS_NORMAL\n #ifdef LIT_SPECULAR\n getReflDir(litArgs_worldNormal, dViewDirW, litArgs_gloss, dTBN);\n #endif\n\n #ifdef LIT_CLEARCOAT\n ccReflDirW = normalize(-reflect(dViewDirW, litArgs_clearcoat_worldNormal));\n #endif\n #endif\n\n #if defined(LIT_SPECULAR_OR_REFLECTION) || defined(LIT_REFRACTION)\n #ifdef LIT_METALNESS\n float f0 = 1.0 / litArgs_ior;\n f0 = (f0 - 1.0) / (f0 + 1.0);\n f0 *= f0;\n #ifdef LIT_SPECULARITY_FACTOR\n litArgs_specularity = getSpecularModulate(litArgs_specularity, litArgs_albedo, litArgs_metalness, f0, litArgs_specularityFactor);\n #else\n litArgs_specularity = getSpecularModulate(litArgs_specularity, litArgs_albedo, litArgs_metalness, f0, 1.0);\n #endif\n litArgs_albedo = getAlbedoModulate(litArgs_albedo, litArgs_metalness);\n\n // the diffuse transmission is a dielectric lobe, like the diffuse reflection\n #ifdef LIT_DIFFUSE_TRANSMISSION\n litArgs_diffuseTransmission_color = getAlbedoModulate(litArgs_diffuseTransmission_color, litArgs_metalness);\n #endif\n #endif\n\n #ifdef LIT_IRIDESCENCE\n vec3 iridescenceFresnel = getIridescence(saturate(dot(dViewDirW, litArgs_worldNormal)), litArgs_specularity, litArgs_iridescence_thickness);\n #endif\n #endif\n\n // ambient\n #ifdef LIT_ADD_AMBIENT\n\n // the ambient light arriving at the back of the surface, which the diffuse transmission\n // lets through, is added to the diffuse light first and moved out of it\n #ifdef LIT_DIFFUSE_TRANSMISSION\n addAmbient(-litArgs_worldNormal);\n dDiffuseTransmissionLight = dDiffuseLight;\n dDiffuseLight = vec3(0);\n #endif\n\n addAmbient(litArgs_worldNormal);\n\n #ifdef LIT_SPECULAR\n dDiffuseLight = dDiffuseLight * (1.0 - litArgs_specularity);\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight *= 1.0 - litArgs_specularity;\n #endif\n #endif\n\n // move ambient color out of diffuse (used by Lightmapper, to multiply ambient color by accumulated AO)\n #ifdef LIT_SEPARATE_AMBIENT\n vec3 dAmbientLight = dDiffuseLight;\n dDiffuseLight = vec3(0);\n #endif\n #endif\n\n dDiffuseLight *= litArgs_ambient;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight *= litArgs_ambient;\n #endif\n\n // the ambient occlusion darkens the ambient light here, and with occludeDirect, which is 0 or 1,\n // the direct light as well, once that is added below\n #ifdef LIT_AO\n occludeDiffuse(mix(litArgs_ao, 1.0, material_occludeDirect));\n #endif\n\n #ifdef LIT_LIGHTMAP\n addLightMap(\n litArgs_lightmap, \n litArgs_lightmapDir, \n litArgs_worldNormal, \n dViewDirW, \n dReflDirW, \n litArgs_gloss, \n litArgs_specularity, \n dVertexNormalW,\n dTBN\n #if defined(LIT_IRIDESCENCE)\n , iridescenceFresnel,\n litArgs_iridescence_intensity\n #endif\n );\n #endif\n\n #ifdef LIT_LIGHTING || LIT_REFLECTIONS\n\n #ifdef LIT_REFLECTIONS\n\n #ifdef LIT_CLEARCOAT\n addReflectionCC(ccReflDirW, litArgs_clearcoat_gloss);\n \n #ifdef LIT_SPECULAR_FRESNEL\n ccFresnel = getFresnelCC(dot(dViewDirW, litArgs_clearcoat_worldNormal));\n ccReflection *= ccFresnel;\n #else\n ccFresnel = 0.0;\n #endif\n #endif\n\n #ifdef LIT_SPECULARITY_FACTOR\n ccReflection *= litArgs_specularityFactor;\n #endif\n\n #ifdef LIT_SHEEN\n addReflectionSheen(litArgs_worldNormal, dViewDirW, litArgs_sheen_gloss);\n #endif\n\n // Fresnel has to be applied to reflections\n addReflection(dReflDirW, litArgs_gloss);\n\n #ifdef LIT_FRESNEL_MODEL\n\n dReflection.rgb *= getFresnel(\n dot(dViewDirW, litArgs_worldNormal), \n litArgs_gloss, \n litArgs_specularity\n #if defined(LIT_IRIDESCENCE)\n , iridescenceFresnel,\n litArgs_iridescence_intensity\n #endif\n );\n\n #else\n\n dReflection.rgb *= litArgs_specularity;\n\n #endif\n\n #endif\n\n #ifdef AREA_LIGHTS\n // specular has to be accumulated differently if we want area lights to look correct\n dSpecularLight *= litArgs_specularity;\n\n #ifdef LIT_SPECULAR\n // the clear coat reflects the area lights with its own reflectance, as it reflects the\n // other lights and the environment: a dielectric, or no Fresnel without the Fresnel\n // model. Its strength scales all of its light when combined.\n #ifdef LIT_SPECULAR_FRESNEL\n float ccLTCReflectance = 0.04;\n #else\n float ccLTCReflectance = 1.0;\n #endif\n\n // evaluate material based area lights data, shared by all area lights\n calcLTCLightValues(litArgs_gloss, litArgs_worldNormal, dViewDirW, litArgs_specularity, litArgs_clearcoat_gloss, litArgs_clearcoat_worldNormal, ccLTCReflectance);\n #endif\n #endif\n \n // LOOP - evaluate all non-clustered lights\n #ifdef LIGHT_COUNT > 0\n #include \"lightEvaluationPS, LIGHT_COUNT\"\n #endif\n\n // clustered lighting\n #ifdef LIT_CLUSTERED_LIGHTS\n addClusteredLights(litArgs_worldNormal, dViewDirW, dReflDirW,\n #if defined(LIT_CLEARCOAT)\n ccReflDirW,\n #endif\n litArgs_gloss, litArgs_specularity, dVertexNormalW, dTBN, \n #if defined(LIT_IRIDESCENCE)\n iridescenceFresnel,\n #endif\n litArgs_clearcoat_worldNormal, litArgs_clearcoat_gloss, litArgs_sheen_gloss, litArgs_iridescence_intensity\n );\n #endif\n\n #ifdef AREA_LIGHTS\n\n #ifdef LIT_SPECULAR\n litArgs_specularity = vec3(1);\n #endif\n\n #endif\n\n #endif\n\n // the diffuse transmission takes its share of the light entering the surface, and the diffuse\n // reflection the rest\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseLight *= 1.0 - litArgs_diffuseTransmission_intensity;\n dDiffuseTransmissionLight *= litArgs_diffuseTransmission_intensity;\n #endif\n\n // refraction is not gated on lighting / reflections, so that it also works without them\n #ifdef LIT_REFRACTION\n addRefraction(\n litArgs_worldNormal,\n dViewDirW,\n litArgs_thickness,\n litArgs_gloss,\n litArgs_specularity,\n litArgs_albedo,\n litArgs_transmission,\n litArgs_ior,\n litArgs_dispersion\n #if defined(LIT_IRIDESCENCE)\n , iridescenceFresnel,\n litArgs_iridescence_intensity\n #endif\n );\n\n // refraction replaces the diffuse transmission as it replaces the diffuse reflection\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight *= 1.0 - litArgs_transmission;\n #endif\n #endif\n\n // apply ambient occlusion\n #ifdef LIT_AO\n occludeDiffuse(mix(1.0, litArgs_ao, material_occludeDirect));\n\n #if LIT_OCCLUDE_SPECULAR != NONE\n occludeSpecular(litArgs_gloss, litArgs_ao, litArgs_worldNormal, dViewDirW);\n #endif\n #endif\n\n #if !defined(LIT_OPACITY_FADES_SPECULAR)\n\n #if LIT_BLEND_TYPE == NORMAL || LIT_BLEND_TYPE == PREMULTIPLIED\n\n float specLum = dot((dSpecularLight + dReflection.rgb * dReflection.a), vec3( 0.2126, 0.7152, 0.0722 ));\n #ifdef LIT_CLEARCOAT\n specLum += dot(ccSpecularLight * litArgs_clearcoat_specularity + ccReflection * litArgs_clearcoat_specularity, vec3( 0.2126, 0.7152, 0.0722 ));\n #endif\n litArgs_opacity = clamp(litArgs_opacity + gammaCorrectInput(specLum), 0.0, 1.0);\n\n #endif\n\n litArgs_opacity *= material_alphaFade;\n\n #endif\n\n // end chunks - when baking lightmap\n #ifdef LIT_LIGHTMAP_BAKING\n #ifdef LIT_LIGHTMAP_BAKING_COLOR\n #include \"bakeLmEndPS\"\n #endif\n #ifdef LIT_LIGHTMAP_BAKING_DIR\n #include \"bakeDirLmEndPS\"\n #endif\n #else\n // end chunks - in all other cases\n #include \"endPS\"\n #include \"outputAlphaPS\"\n #endif\n\n #ifdef LIT_MSDF\n gl_FragColor = applyMsdf(gl_FragColor);\n #endif\n\n #include \"outputPS\"\n #include \"debugOutputPS\"\n\n #ifdef LIT_SHADOW_CATCHER\n // output when the shadow catcher is enabled - accumulated shadows\n gl_FragColor.rgb = vec3(dShadowCatcher);\n #endif\n\n #include \"outlineOutputPS\"\n}\n";
|
|
2
2
|
export default _default;
|
package/build/playcanvas/src/scene/shader-lib/glsl/chunks/lit/frag/pass-forward/litForwardBackend.js
CHANGED
|
@@ -108,7 +108,12 @@ void evaluateBackend() {
|
|
|
108
108
|
#ifdef AREA_LIGHTS
|
|
109
109
|
dSpecularLight *= litArgs_specularity;
|
|
110
110
|
#ifdef LIT_SPECULAR
|
|
111
|
-
|
|
111
|
+
#ifdef LIT_SPECULAR_FRESNEL
|
|
112
|
+
float ccLTCReflectance = 0.04;
|
|
113
|
+
#else
|
|
114
|
+
float ccLTCReflectance = 1.0;
|
|
115
|
+
#endif
|
|
116
|
+
calcLTCLightValues(litArgs_gloss, litArgs_worldNormal, dViewDirW, litArgs_specularity, litArgs_clearcoat_gloss, litArgs_clearcoat_worldNormal, ccLTCReflectance);
|
|
112
117
|
#endif
|
|
113
118
|
#endif
|
|
114
119
|
|
|
@@ -128,9 +133,6 @@ void evaluateBackend() {
|
|
|
128
133
|
);
|
|
129
134
|
#endif
|
|
130
135
|
#ifdef AREA_LIGHTS
|
|
131
|
-
#ifdef LIT_CLEARCOAT
|
|
132
|
-
litArgs_clearcoat_specularity = 1.0;
|
|
133
|
-
#endif
|
|
134
136
|
#ifdef LIT_SPECULAR
|
|
135
137
|
litArgs_specularity = vec3(1);
|
|
136
138
|
#endif
|
|
@@ -57,6 +57,14 @@ export class LitShaderOptions {
|
|
|
57
57
|
clusteredLightingShadowsEnabled: boolean;
|
|
58
58
|
clusteredLightingShadowType: number;
|
|
59
59
|
clusteredLightingAreaLightsEnabled: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Bit mask (1 << shape) of the area light shapes the clustered lighting compiles code for.
|
|
62
|
+
* Defaults to all shapes.
|
|
63
|
+
*
|
|
64
|
+
* @type {number}
|
|
65
|
+
* @ignore
|
|
66
|
+
*/
|
|
67
|
+
clusteredLightingAreaLightShapes: number;
|
|
60
68
|
vertexColors: boolean;
|
|
61
69
|
useVertexColorGamma: boolean;
|
|
62
70
|
lightMapEnabled: boolean;
|
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
BLEND_NONE,
|
|
3
|
+
DITHER_NONE,
|
|
4
|
+
FOG_NONE,
|
|
5
|
+
GAMMA_NONE,
|
|
6
|
+
LIGHTSHAPE_DISK,
|
|
7
|
+
LIGHTSHAPE_RECT,
|
|
8
|
+
LIGHTSHAPE_SPHERE,
|
|
9
|
+
REFLECTIONSRC_NONE
|
|
10
|
+
} from "../../constants.js";
|
|
2
11
|
class LitShaderOptions {
|
|
3
12
|
hasTangents = false;
|
|
4
13
|
shaderChunks = null;
|
|
@@ -20,6 +29,7 @@ class LitShaderOptions {
|
|
|
20
29
|
clusteredLightingShadowsEnabled = false;
|
|
21
30
|
clusteredLightingShadowType = 0;
|
|
22
31
|
clusteredLightingAreaLightsEnabled = false;
|
|
32
|
+
clusteredLightingAreaLightShapes = 1 << LIGHTSHAPE_RECT | 1 << LIGHTSHAPE_DISK | 1 << LIGHTSHAPE_SPHERE;
|
|
23
33
|
vertexColors = false;
|
|
24
34
|
useVertexColorGamma = false;
|
|
25
35
|
lightMapEnabled = false;
|
|
@@ -26,6 +26,9 @@ import {
|
|
|
26
26
|
} from "../../../platform/graphics/constants.js";
|
|
27
27
|
import {
|
|
28
28
|
LIGHTSHAPE_PUNCTUAL,
|
|
29
|
+
LIGHTSHAPE_RECT,
|
|
30
|
+
LIGHTSHAPE_DISK,
|
|
31
|
+
LIGHTSHAPE_SPHERE,
|
|
29
32
|
LIGHTTYPE_DIRECTIONAL,
|
|
30
33
|
LIGHTTYPE_OMNI,
|
|
31
34
|
LIGHTTYPE_SPOT,
|
|
@@ -254,7 +257,13 @@ class LitShader {
|
|
|
254
257
|
if (clusteredLightingEnabled && this.lighting) {
|
|
255
258
|
fDefines.set("LIT_CLUSTERED_LIGHTS", true);
|
|
256
259
|
if (options.clusteredLightingCookiesEnabled) fDefines.set("CLUSTER_COOKIES", true);
|
|
257
|
-
if (options.clusteredLightingAreaLightsEnabled)
|
|
260
|
+
if (options.clusteredLightingAreaLightsEnabled) {
|
|
261
|
+
fDefines.set("CLUSTER_AREALIGHTS", true);
|
|
262
|
+
const shapes = options.clusteredLightingAreaLightShapes;
|
|
263
|
+
if (shapes & 1 << LIGHTSHAPE_RECT) fDefines.set("CLUSTER_AREALIGHT_RECT", true);
|
|
264
|
+
if (shapes & 1 << LIGHTSHAPE_DISK) fDefines.set("CLUSTER_AREALIGHT_DISK", true);
|
|
265
|
+
if (shapes & 1 << LIGHTSHAPE_SPHERE) fDefines.set("CLUSTER_AREALIGHT_SPHERE", true);
|
|
266
|
+
}
|
|
258
267
|
if (options.lightMaskDynamic) fDefines.set("CLUSTER_MESH_DYNAMIC_LIGHTS", true);
|
|
259
268
|
if (options.clusteredLightingShadowsEnabled && !options.noShadow) {
|
|
260
269
|
const clusteredShadowInfo = shadowTypeInfo.get(options.clusteredLightingShadowType);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: "\n\n#include \"lightBufferDefinesPS\"\n\n// include this before shadow / cookie code\n#include \"clusteredLightUtilsPS\"\n\n#ifdef CLUSTER_COOKIES\n #include \"clusteredLightCookiesPS\"\n#endif\n\n#ifdef CLUSTER_SHADOWS\n #include \"clusteredLightShadowsPS\"\n#endif\n\nvar clusterWorldTexture: texture_2d<u32>;\nvar lightsTexture: texture_2d<uff>;\n\n#ifdef CLUSTER_SHADOWS\n // TODO: when VSM shadow is supported, it needs to use sampler2D in webgl2\n var shadowAtlasTexture: texture_depth_2d;\n var shadowAtlasTextureSampler: sampler_comparison;\n#endif\n\n#ifdef CLUSTER_COOKIES\n var cookieAtlasTexture: texture_2d<f32>;\n var cookieAtlasTextureSampler: sampler;\n#endif\n\nuniform clusterMaxCells: i32;\n\n// number of lights in the cluster structure\nuniform numClusteredLights: i32;\n\n// width of the cluster texture\nuniform clusterTextureWidth: i32;\n\nuniform clusterCellsCountByBoundsSize: vec3f;\nuniform clusterBoundsMin: vec3f;\nuniform clusterBoundsDelta: vec3f;\nuniform clusterCellsDot: vec3i;\nuniform clusterCellsMax: vec3i;\nuniform shadowAtlasParams: vec2f;\n\n// structure storing light properties of a clustered light. Vectors and scalars are interleaved\n// so each vec3 packs with an adjacent 4-byte field into a 16-byte slot, minimizing padding for\n// compilers that don't reorder struct members.\nstruct ClusterLightData {\n\n // world space position\n position: vec3f,\n\n // light index in the lights texture\n lightIndex: i32,\n\n // world space direction (spot light only)\n direction: vec3f,\n\n // area light shape\n shape: u32,\n\n // color\n color: vec3f,\n\n // 0.0 if the light doesn't cast shadows\n shadowIntensity: f32,\n\n // range of the light\n range: f32,\n\n // compressed biases, two half-floats stored in a float\n biasesData: f32,\n\n // intensity of the cookie\n cookieIntensity: f32,\n\n // true for spot lights\n isSpot: bool,\n\n // light follow mode\n falloffModeLinear: bool,\n\n // light mask (mutually exclusive)\n isDynamic: bool,\n isLightmapped: bool\n}\n\n// Spot light cone angles, decoded on demand only when the light is a spot light.\nstruct ClusterLightSpotData {\n innerConeAngleCos: f32,\n outerConeAngleCos: f32\n}\n\n// Area light dimensions and orientation, decoded on demand only for non-punctual lights.\nstruct ClusterLightAreaData {\n halfWidth: vec3f,\n halfHeight: vec3f\n}\n\n// Shadow bias parameters, decoded on demand only when the light casts shadows.\nstruct ClusterLightShadowData {\n shadowBias: f32,\n shadowNormalBias: f32\n}\n\n// Note: on some devices (tested on Pixel 3A XL), this matrix when stored inside the light struct has lower precision compared to\n// when stored outside, so we store it outside to avoid spot shadow flickering. This might need to be done to other / all members\n// of the structure if further similar issues are observed.\n\n// shadow (spot light only) / cookie projection matrix\nvar<private> lightProjectionMatrix: mat4x4f;\n\n// NOTE: On some Samsung devices, these values can suffer precision / corruption issues when stored\n// as members of ClusterLightData. Keep them as module-scope temporaries instead. See issue #7800.\nvar<private> clusterLightData_flags: u32; // 32bit of flags\nvar<private> clusterLightData_anglesData: f32; // compressed angles, two half-floats stored in a float\nvar<private> clusterLightData_colorBFlagsData: u32; // blue color component and angle flags (as uint for efficient bit operations)\n\nfn sampleLightTextureF(lightIndex: i32, index: i32) -> vec4f {\n return textureLoad(lightsTexture, vec2<i32>(index, lightIndex), 0);\n}\n\nfn decodeClusterLightCore(lightIndex: i32) -> ClusterLightData {\n var clusterLightData: ClusterLightData;\n\n // light index\n clusterLightData.lightIndex = lightIndex;\n\n // sample data encoding half-float values into 32bit uints\n let halfData: vec4f = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_COLOR_ANGLES_BIAS});\n\n // store values needed by later decode steps (anglesData / colorBFlagsData live outside the\n // struct due to Samsung precision issues - see #7800)\n clusterLightData_anglesData = halfData.z;\n clusterLightData.biasesData = halfData.w;\n clusterLightData_colorBFlagsData = bitcast<u32>(halfData.y);\n\n // decompress color half-floats\n let colorRG: vec2f = unpack2x16float(bitcast<u32>(halfData.x));\n let colorB_flags: vec2f = unpack2x16float(clusterLightData_colorBFlagsData);\n clusterLightData.color = vec3f(colorRG, colorB_flags.x) * {LIGHT_COLOR_DIVIDER};\n\n // position and range, full floats\n let lightPosRange: vec4f = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_POSITION_RANGE});\n clusterLightData.position = lightPosRange.xyz;\n clusterLightData.range = lightPosRange.w;\n\n // spot direction & flags data\n let lightDir_Flags: vec4f = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_DIRECTION_FLAGS});\n\n // spot light direction\n clusterLightData.direction = lightDir_Flags.xyz;\n\n // 32bit flags (kept outside the struct, see #7800)\n clusterLightData_flags = bitcast<u32>(lightDir_Flags.w);\n clusterLightData.isSpot = (clusterLightData_flags & (1u << 30u)) != 0u;\n clusterLightData.shape = (clusterLightData_flags >> 28u) & 0x3u;\n clusterLightData.falloffModeLinear = (clusterLightData_flags & (1u << 27u)) == 0u;\n clusterLightData.shadowIntensity = f32((clusterLightData_flags >> 0u) & 0xFFu) / 255.0;\n clusterLightData.cookieIntensity = f32((clusterLightData_flags >> 8u) & 0xFFu) / 255.0;\n clusterLightData.isDynamic = (clusterLightData_flags & (1u << 22u)) != 0u;\n clusterLightData.isLightmapped = (clusterLightData_flags & (1u << 21u)) != 0u;\n\n return clusterLightData;\n}\n\nfn decodeClusterLightSpot() -> ClusterLightSpotData {\n // decompress spot light angles\n let angleFlags: u32 = (clusterLightData_colorBFlagsData >> 16u) & 0xFFFFu; // Extract upper 16 bits as integer\n\n let angleValues: vec2f = unpack2x16float(bitcast<u32>(clusterLightData_anglesData));\n let innerVal: f32 = angleValues.x;\n let outerVal: f32 = angleValues.y;\n\n // decode based on flags (branch-free)\n let innerIsVersine: bool = (angleFlags & 1u) != 0u; // bit 0: inner angle format\n let outerIsVersine: bool = ((angleFlags >> 1u) & 1u) != 0u; // bit 1: outer angle format\n\n return ClusterLightSpotData(\n select(innerVal, 1.0 - innerVal, innerIsVersine),\n select(outerVal, 1.0 - outerVal, outerIsVersine)\n );\n}\n\nfn decodeClusterLightOmniAtlasViewport(lightIndex: i32) -> vec3f {\n return sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_0}).xyz;\n}\n\nfn decodeClusterLightAreaData(lightIndex: i32) -> ClusterLightAreaData {\n return ClusterLightAreaData(\n sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_AREA_DATA_WIDTH}).xyz,\n sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_AREA_DATA_HEIGHT}).xyz\n );\n}\n\nfn decodeClusterLightProjectionMatrixData(lightIndex: i32) -> mat4x4f {\n // shadow matrix\n let m0: vec4f = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_0});\n let m1: vec4f = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_1});\n let m2: vec4f = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_2});\n let m3: vec4f = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_3});\n return mat4x4f(m0, m1, m2, m3);\n}\n\nfn decodeClusterLightShadowData(biasesData: f32) -> ClusterLightShadowData {\n // shadow biases\n let biases: vec2f = unpack2x16float(bitcast<u32>(biasesData));\n return ClusterLightShadowData(biases.x, biases.y);\n}\n\nfn decodeClusterLightCookieData() -> vec4f {\n\n // extract channel mask from flags\n let cookieFlags: u32 = (clusterLightData_flags >> 23u) & 0x0Fu; // 4bits, each bit enables a channel\n let mask_uvec: vec4<u32> = vec4<u32>(cookieFlags) & vec4<u32>(1u, 2u, 4u, 8u);\n return step(vec4f(1.0), vec4f(mask_uvec)); // Normalize to 0.0 or 1.0\n}\n\nfn evaluateLight(\n light: ClusterLightData,\n worldNormal: vec3f,\n viewDir: vec3f,\n reflectionDir: vec3f,\n#if defined(LIT_CLEARCOAT)\n clearcoatReflectionDir: vec3f,\n#endif\n gloss: f32,\n specularity: vec3f,\n geometricNormal: vec3f,\n tbn: mat3x3f,\n#if defined(LIT_IRIDESCENCE)\n iridescenceFresnel: vec3f,\n#endif\n clearcoat_worldNormal: vec3f,\n clearcoat_gloss: f32,\n sheen_gloss: f32,\n iridescence_intensity: f32\n) {\n\n var cookieAttenuation: vec3f = vec3f(1.0);\n var diffuseAttenuation: f32 = 1.0;\n var falloffAttenuation: f32 = 1.0;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n // the whole attenuation of the light arriving at the back of the surface, which the diffuse\n // transmission lets through\n var transmissionAttenuation: f32 = 0.0;\n #endif\n\n // evaluate omni part of the light\n let lightDirW: vec3f = evalOmniLight(light.position);\n let lightDirNormW: vec3f = normalize(lightDirW);\n\n #ifdef CLUSTER_AREALIGHTS\n\n // distance attenuation\n if (light.shape != {LIGHTSHAPE_PUNCTUAL}) { // area light\n\n // area lights\n let areaData: ClusterLightAreaData = decodeClusterLightAreaData(light.lightIndex);\n\n // handle light shape\n if (light.shape == {LIGHTSHAPE_RECT}) {\n calcRectLightValues(light.position, areaData.halfWidth, areaData.halfHeight);\n } else if (light.shape == {LIGHTSHAPE_DISK}) {\n calcDiskLightValues(light.position, areaData.halfWidth, areaData.halfHeight);\n } else { // sphere\n calcSphereLightValues(light.position, areaData.halfWidth, areaData.halfHeight);\n }\n\n falloffAttenuation = getFalloffWindow(light.range, lightDirW);\n\n } else\n\n #endif\n\n { // punctual light\n\n if (light.falloffModeLinear) {\n falloffAttenuation = getFalloffLinear(light.range, lightDirW);\n } else {\n falloffAttenuation = getFalloffInvSquared(light.range, lightDirW);\n }\n }\n\n if (falloffAttenuation > 0.00001) {\n\n #ifdef CLUSTER_AREALIGHTS\n\n if (light.shape != {LIGHTSHAPE_PUNCTUAL}) { // area light\n\n // handle light shape\n if (light.shape == {LIGHTSHAPE_RECT}) {\n diffuseAttenuation = getRectLightDiffuse(worldNormal, viewDir, lightDirW, lightDirNormW) * 16.0;\n #ifdef LIT_DIFFUSE_TRANSMISSION\n transmissionAttenuation = getRectLightDiffuse(-worldNormal, viewDir, lightDirW, lightDirNormW) * 16.0;\n #endif\n } else if (light.shape == {LIGHTSHAPE_DISK}) {\n diffuseAttenuation = getDiskLightDiffuse(worldNormal, viewDir, lightDirW, lightDirNormW) * 16.0;\n #ifdef LIT_DIFFUSE_TRANSMISSION\n transmissionAttenuation = getDiskLightDiffuse(-worldNormal, viewDir, lightDirW, lightDirNormW) * 16.0;\n #endif\n } else { // sphere\n diffuseAttenuation = getSphereLightDiffuse(worldNormal, viewDir, lightDirW, lightDirNormW) * 16.0;\n #ifdef LIT_DIFFUSE_TRANSMISSION\n transmissionAttenuation = getSphereLightDiffuse(-worldNormal, viewDir, lightDirW, lightDirNormW) * 16.0;\n #endif\n }\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n transmissionAttenuation = transmissionAttenuation * falloffAttenuation;\n #endif\n\n } else\n\n #endif\n\n {\n // diffuse transmission, evaluated before the diffuse lighting multiplies into the\n // falloff\n #ifdef LIT_DIFFUSE_TRANSMISSION\n transmissionAttenuation = falloffAttenuation * getLightDiffuse(-worldNormal, viewDir, lightDirNormW);\n #endif\n\n falloffAttenuation = falloffAttenuation * getLightDiffuse(worldNormal, viewDir, lightDirNormW);\n }\n\n // spot light falloff\n if (light.isSpot) {\n let spotData: ClusterLightSpotData = decodeClusterLightSpot();\n let spotEffect: f32 = getSpotEffect(light.direction, spotData.innerConeAngleCos, spotData.outerConeAngleCos, lightDirNormW);\n falloffAttenuation = falloffAttenuation * spotEffect;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n transmissionAttenuation = transmissionAttenuation * spotEffect;\n #endif\n }\n\n #if defined(CLUSTER_COOKIES) || defined(CLUSTER_SHADOWS)\n\n // with diffuse transmission, the light can also reach the surface from behind\n #ifdef LIT_DIFFUSE_TRANSMISSION\n if (max(falloffAttenuation, transmissionAttenuation) > 0.00001) {\n #else\n if (falloffAttenuation > 0.00001) {\n #endif\n\n // shadow / cookie\n if (light.shadowIntensity > 0.0 || light.cookieIntensity > 0.0) {\n\n var omniAtlasViewport: vec3f = vec3f(0.0);\n\n // shared shadow / cookie data depends on light type\n if (light.isSpot) {\n lightProjectionMatrix = decodeClusterLightProjectionMatrixData(light.lightIndex);\n } else {\n omniAtlasViewport = decodeClusterLightOmniAtlasViewport(light.lightIndex);\n }\n\n let shadowTextureResolution: f32 = uniform.shadowAtlasParams.x;\n let shadowEdgePixels: f32 = uniform.shadowAtlasParams.y;\n\n #ifdef CLUSTER_COOKIES\n\n // cookie\n if (light.cookieIntensity > 0.0) {\n let cookieChannelMask: vec4f = decodeClusterLightCookieData();\n\n if (light.isSpot) {\n cookieAttenuation = getCookie2DClustered(cookieAtlasTexture, cookieAtlasTextureSampler, lightProjectionMatrix, vPositionW, light.cookieIntensity, cookieChannelMask);\n } else {\n cookieAttenuation = getCookieCubeClustered(cookieAtlasTexture, cookieAtlasTextureSampler, lightDirW, light.cookieIntensity, cookieChannelMask, shadowTextureResolution, shadowEdgePixels, omniAtlasViewport);\n }\n }\n\n #endif\n\n #ifdef CLUSTER_SHADOWS\n\n // shadow\n if (light.shadowIntensity > 0.0) {\n let shadowData: ClusterLightShadowData = decodeClusterLightShadowData(light.biasesData);\n\n let shadowParams: vec4f = vec4f(shadowTextureResolution, shadowData.shadowNormalBias, shadowData.shadowBias, 1.0 / light.range);\n\n // The normal offset moves the sample off the surface, towards the light. A\n // surface with diffuse transmission is also lit from behind, so the side\n // facing the light is used\n #ifdef LIT_DIFFUSE_TRANSMISSION\n let shadowNormal: vec3f = faceForward(geometricNormal, lightDirNormW, geometricNormal);\n #else\n let shadowNormal: vec3f = geometricNormal;\n #endif\n\n if (light.isSpot) {\n\n // spot shadow\n let shadowCoord: vec3f = getShadowCoordPerspZbufferNormalOffset(lightProjectionMatrix, shadowParams, shadowNormal);\n\n #if defined(CLUSTER_SHADOW_TYPE_PCF1)\n let shadow: f32 = getShadowSpotClusteredPCF1(shadowAtlasTexture, shadowAtlasTextureSampler, shadowCoord, shadowParams);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF3)\n let shadow: f32 = getShadowSpotClusteredPCF3(shadowAtlasTexture, shadowAtlasTextureSampler, shadowCoord, shadowParams);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF5)\n let shadow: f32 = getShadowSpotClusteredPCF5(shadowAtlasTexture, shadowAtlasTextureSampler, shadowCoord, shadowParams);\n #elif defined(CLUSTER_SHADOW_TYPE_PCSS)\n let shadow: f32 = getShadowSpotClusteredPCSS(shadowAtlasTexture, shadowAtlasTextureSampler, shadowCoord, shadowParams);\n #endif\n falloffAttenuation = falloffAttenuation * mix(1.0, shadow, light.shadowIntensity);\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n transmissionAttenuation = transmissionAttenuation * mix(1.0, shadow, light.shadowIntensity);\n #endif\n\n } else {\n\n // omni shadow\n let dir: vec3f = normalOffsetPointShadow(shadowParams, light.position, lightDirW, lightDirNormW, shadowNormal); // normalBias adjusted for distance\n\n #if defined(CLUSTER_SHADOW_TYPE_PCF1)\n let shadow: f32 = getShadowOmniClusteredPCF1(shadowAtlasTexture, shadowAtlasTextureSampler, shadowParams, omniAtlasViewport, shadowEdgePixels, dir);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF3)\n let shadow: f32 = getShadowOmniClusteredPCF3(shadowAtlasTexture, shadowAtlasTextureSampler, shadowParams, omniAtlasViewport, shadowEdgePixels, dir);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF5)\n let shadow: f32 = getShadowOmniClusteredPCF5(shadowAtlasTexture, shadowAtlasTextureSampler, shadowParams, omniAtlasViewport, shadowEdgePixels, dir);\n #endif\n falloffAttenuation = falloffAttenuation * mix(1.0, shadow, light.shadowIntensity);\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n transmissionAttenuation = transmissionAttenuation * mix(1.0, shadow, light.shadowIntensity);\n #endif\n }\n }\n\n #endif\n }\n }\n\n #endif\n\n // diffuse / specular / clearcoat\n #ifdef CLUSTER_AREALIGHTS\n\n if (light.shape != {LIGHTSHAPE_PUNCTUAL}) { // area light\n\n // area light diffuse\n {\n var areaDiffuse: vec3f = (diffuseAttenuation * falloffAttenuation) * light.color * cookieAttenuation;\n\n #if defined(LIT_SPECULAR)\n areaDiffuse = mix(areaDiffuse, vec3f(0.0), dLTCSpecFres);\n #endif\n\n // area light diffuse - it does not mix diffuse lighting into specular attenuation\n dDiffuseLight = dDiffuseLight + areaDiffuse;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n var areaTransmission: vec3f = transmissionAttenuation * light.color * cookieAttenuation;\n\n #if defined(LIT_SPECULAR)\n areaTransmission = mix(areaTransmission, vec3f(0.0), dLTCSpecFres);\n #endif\n\n dDiffuseTransmissionLight = dDiffuseTransmissionLight + areaTransmission;\n #endif\n }\n\n // specular and clear coat are material settings and get included by a define based on the material\n #ifdef LIT_SPECULAR\n\n // area light specular\n var areaLightSpecular: f32; // Use var because assigned in if/else\n\n if (light.shape == {LIGHTSHAPE_RECT}) {\n areaLightSpecular = getRectLightSpecular(worldNormal, viewDir);\n } else if (light.shape == {LIGHTSHAPE_DISK}) {\n areaLightSpecular = getDiskLightSpecular(worldNormal, viewDir);\n } else { // sphere\n areaLightSpecular = getSphereLightSpecular(worldNormal, viewDir);\n }\n\n dSpecularLight = dSpecularLight + dLTCSpecFres * areaLightSpecular * falloffAttenuation * light.color * cookieAttenuation;\n\n #ifdef LIT_CLEARCOAT\n\n // area light specular clear coat\n var areaLightSpecularCC: f32; // Use var because assigned in if/else\n\n if (light.shape == {LIGHTSHAPE_RECT}) {\n areaLightSpecularCC = getRectLightSpecular(clearcoat_worldNormal, viewDir);\n } else if (light.shape == {LIGHTSHAPE_DISK}) {\n areaLightSpecularCC = getDiskLightSpecular(clearcoat_worldNormal, viewDir);\n } else { // sphere\n areaLightSpecularCC = getSphereLightSpecular(clearcoat_worldNormal, viewDir);\n }\n\n ccSpecularLight = ccSpecularLight + ccLTCSpecFres * areaLightSpecularCC * falloffAttenuation * light.color * cookieAttenuation;\n\n #endif\n\n #endif\n\n } else\n\n #endif\n\n { // punctual light\n\n // punctual light diffuse\n {\n var punctualDiffuse: vec3f = falloffAttenuation * light.color * cookieAttenuation;\n\n #if defined(CLUSTER_AREALIGHTS)\n #if defined(LIT_SPECULAR)\n punctualDiffuse = mix(punctualDiffuse, vec3f(0.0), specularity);\n #endif\n #endif\n\n dDiffuseLight = dDiffuseLight + punctualDiffuse;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n var punctualTransmission: vec3f = transmissionAttenuation * light.color * cookieAttenuation;\n\n #if defined(CLUSTER_AREALIGHTS)\n #if defined(LIT_SPECULAR)\n punctualTransmission = mix(punctualTransmission, vec3f(0.0), specularity);\n #endif\n #endif\n\n dDiffuseTransmissionLight = dDiffuseTransmissionLight + punctualTransmission;\n #endif\n }\n\n // specular and clear coat are material settings and get included by a define based on the material\n #ifdef LIT_SPECULAR\n\n let halfDir: vec3f = normalize(-lightDirNormW + viewDir);\n\n // specular\n #ifdef LIT_SPECULAR_FRESNEL\n dSpecularLight = dSpecularLight +\n getLightSpecular(halfDir, reflectionDir, worldNormal, viewDir, lightDirNormW, gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation *\n getFresnel(\n dot(viewDir, halfDir),\n gloss,\n specularity\n #if defined(LIT_IRIDESCENCE)\n , iridescenceFresnel,\n iridescence_intensity\n #endif\n );\n #else\n dSpecularLight = dSpecularLight + getLightSpecular(halfDir, reflectionDir, worldNormal, viewDir, lightDirNormW, gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation * specularity;\n #endif\n\n #ifdef LIT_CLEARCOAT\n #ifdef LIT_SPECULAR_FRESNEL\n ccSpecularLight = ccSpecularLight + getLightSpecular(halfDir, clearcoatReflectionDir, clearcoat_worldNormal, viewDir, lightDirNormW, clearcoat_gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation * getFresnelCC(dot(viewDir, halfDir));\n #else\n ccSpecularLight = ccSpecularLight + getLightSpecular(halfDir, clearcoatReflectionDir, clearcoat_worldNormal, viewDir, lightDirNormW, clearcoat_gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation;\n #endif\n #endif\n\n #ifdef LIT_SHEEN\n sSpecularLight = sSpecularLight + getLightSpecularSheen(halfDir, worldNormal, viewDir, lightDirNormW, sheen_gloss) * falloffAttenuation * light.color * cookieAttenuation;\n #endif\n\n #endif\n }\n }\n\n // Write to global attenuation values (for lightmapper)\n dAtten = falloffAttenuation;\n dLightDirNormW = lightDirNormW;\n}\n\n\nfn evaluateClusterLight(\n lightIndex: i32,\n worldNormal: vec3f,\n viewDir: vec3f,\n reflectionDir: vec3f,\n#if defined(LIT_CLEARCOAT)\n clearcoatReflectionDir: vec3f,\n#endif\n gloss: f32,\n specularity: vec3f,\n geometricNormal: vec3f,\n tbn: mat3x3f,\n#if defined(LIT_IRIDESCENCE)\n iridescenceFresnel: vec3f,\n#endif\n clearcoat_worldNormal: vec3f,\n clearcoat_gloss: f32,\n sheen_gloss: f32,\n iridescence_intensity: f32\n) {\n\n // decode core light data from textures\n let clusterLightData: ClusterLightData = decodeClusterLightCore(lightIndex);\n\n // evaluate light if it uses accepted light mask\n #ifdef CLUSTER_MESH_DYNAMIC_LIGHTS\n let acceptLightMask: bool = clusterLightData.isDynamic;\n #else\n let acceptLightMask: bool = clusterLightData.isLightmapped;\n #endif\n\n if (acceptLightMask) {\n evaluateLight(\n clusterLightData,\n worldNormal,\n viewDir,\n reflectionDir,\n#if defined(LIT_CLEARCOAT)\n clearcoatReflectionDir,\n#endif\n gloss,\n specularity,\n geometricNormal,\n tbn,\n#if defined(LIT_IRIDESCENCE)\n iridescenceFresnel,\n#endif\n clearcoat_worldNormal,\n clearcoat_gloss,\n sheen_gloss,\n iridescence_intensity\n );\n }\n}\n\n\nfn addClusteredLights(\n worldNormal: vec3f,\n viewDir: vec3f,\n reflectionDir: vec3f,\n#if defined(LIT_CLEARCOAT)\n clearcoatReflectionDir: vec3f,\n#endif\n gloss: f32,\n specularity: vec3f,\n geometricNormal: vec3f,\n tbn: mat3x3f,\n#if defined(LIT_IRIDESCENCE)\n iridescenceFresnel: vec3f,\n#endif\n clearcoat_worldNormal: vec3f,\n clearcoat_gloss: f32,\n sheen_gloss: f32,\n iridescence_intensity: f32\n) {\n\n // skip if no lights (index 0 is reserved for 'no light')\n if (uniform.numClusteredLights <= 1) {\n return;\n }\n\n // world space position to 3d integer cell coordinates in the cluster structure\n let cellCoords: vec3i = vec3i(floor((vPositionW - uniform.clusterBoundsMin) * uniform.clusterCellsCountByBoundsSize));\n\n // no lighting when cell coordinate is out of range\n if (!(any(cellCoords < vec3i(0)) || any(cellCoords >= uniform.clusterCellsMax))) {\n\n // cell index (mapping from 3d cell coordinates to linear memory)\n let cellIndex: i32 = cellCoords.x * uniform.clusterCellsDot.x + cellCoords.y * uniform.clusterCellsDot.y + cellCoords.z * uniform.clusterCellsDot.z;\n\n // convert cell index to uv coordinates\n let clusterV: i32 = cellIndex / uniform.clusterTextureWidth;\n let clusterU: i32 = cellIndex - clusterV * uniform.clusterTextureWidth;\n\n // loop over maximum number of light cells\n for (var lightCellIndex: i32 = 0; lightCellIndex < uniform.clusterMaxCells; lightCellIndex = lightCellIndex + 1) {\n\n // using a single channel texture with data in red channel\n let lightIndex: u32 = textureLoad(clusterWorldTexture, vec2<i32>(clusterU + lightCellIndex, clusterV), 0).r;\n\n if (lightIndex == 0u) {\n break;\n }\n\n evaluateClusterLight(\n i32(lightIndex),\n worldNormal,\n viewDir,\n reflectionDir,\n#if defined(LIT_CLEARCOAT)\n clearcoatReflectionDir,\n#endif\n gloss,\n specularity,\n geometricNormal,\n tbn,\n#if defined(LIT_IRIDESCENCE)\n iridescenceFresnel,\n#endif\n clearcoat_worldNormal,\n clearcoat_gloss,\n sheen_gloss,\n iridescence_intensity\n );\n }\n }\n}";
|
|
1
|
+
declare const _default: "\n\n#include \"lightBufferDefinesPS\"\n\n// include this before shadow / cookie code\n#include \"clusteredLightUtilsPS\"\n\n#ifdef CLUSTER_COOKIES\n #include \"clusteredLightCookiesPS\"\n#endif\n\n#ifdef CLUSTER_SHADOWS\n #include \"clusteredLightShadowsPS\"\n#endif\n\nvar clusterWorldTexture: texture_2d<u32>;\nvar lightsTexture: texture_2d<uff>;\n\n#ifdef CLUSTER_SHADOWS\n // TODO: when VSM shadow is supported, it needs to use sampler2D in webgl2\n var shadowAtlasTexture: texture_depth_2d;\n var shadowAtlasTextureSampler: sampler_comparison;\n#endif\n\n#ifdef CLUSTER_COOKIES\n var cookieAtlasTexture: texture_2d<f32>;\n var cookieAtlasTextureSampler: sampler;\n#endif\n\nuniform clusterMaxCells: i32;\n\n// number of lights in the cluster structure\nuniform numClusteredLights: i32;\n\n// width of the cluster texture\nuniform clusterTextureWidth: i32;\n\nuniform clusterCellsCountByBoundsSize: vec3f;\nuniform clusterBoundsMin: vec3f;\nuniform clusterBoundsDelta: vec3f;\nuniform clusterCellsDot: vec3i;\nuniform clusterCellsMax: vec3i;\nuniform shadowAtlasParams: vec2f;\n\n// structure storing light properties of a clustered light. Vectors and scalars are interleaved\n// so each vec3 packs with an adjacent 4-byte field into a 16-byte slot, minimizing padding for\n// compilers that don't reorder struct members.\nstruct ClusterLightData {\n\n // world space position\n position: vec3f,\n\n // light index in the lights texture\n lightIndex: i32,\n\n // world space direction (spot light only)\n direction: vec3f,\n\n // area light shape\n shape: u32,\n\n // color\n color: vec3f,\n\n // 0.0 if the light doesn't cast shadows\n shadowIntensity: f32,\n\n // range of the light\n range: f32,\n\n // compressed biases, two half-floats stored in a float\n biasesData: f32,\n\n // intensity of the cookie\n cookieIntensity: f32,\n\n // true for spot lights\n isSpot: bool,\n\n // light follow mode\n falloffModeLinear: bool,\n\n // light mask (mutually exclusive)\n isDynamic: bool,\n isLightmapped: bool\n}\n\n// Spot light cone angles, decoded on demand only when the light is a spot light.\nstruct ClusterLightSpotData {\n innerConeAngleCos: f32,\n outerConeAngleCos: f32\n}\n\n// Area light dimensions and orientation, decoded on demand only for non-punctual lights.\nstruct ClusterLightAreaData {\n halfWidth: vec3f,\n halfHeight: vec3f\n}\n\n// Shadow bias parameters, decoded on demand only when the light casts shadows.\nstruct ClusterLightShadowData {\n shadowBias: f32,\n shadowNormalBias: f32\n}\n\n// Note: on some devices (tested on Pixel 3A XL), this matrix when stored inside the light struct has lower precision compared to\n// when stored outside, so we store it outside to avoid spot shadow flickering. This might need to be done to other / all members\n// of the structure if further similar issues are observed.\n\n// shadow (spot light only) / cookie projection matrix\nvar<private> lightProjectionMatrix: mat4x4f;\n\n// NOTE: On some Samsung devices, these values can suffer precision / corruption issues when stored\n// as members of ClusterLightData. Keep them as module-scope temporaries instead. See issue #7800.\nvar<private> clusterLightData_flags: u32; // 32bit of flags\nvar<private> clusterLightData_anglesData: f32; // compressed angles, two half-floats stored in a float\nvar<private> clusterLightData_colorBFlagsData: u32; // blue color component and angle flags (as uint for efficient bit operations)\n\nfn sampleLightTextureF(lightIndex: i32, index: i32) -> vec4f {\n return textureLoad(lightsTexture, vec2<i32>(index, lightIndex), 0);\n}\n\nfn decodeClusterLightCore(lightIndex: i32) -> ClusterLightData {\n var clusterLightData: ClusterLightData;\n\n // light index\n clusterLightData.lightIndex = lightIndex;\n\n // sample data encoding half-float values into 32bit uints\n let halfData: vec4f = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_COLOR_ANGLES_BIAS});\n\n // store values needed by later decode steps (anglesData / colorBFlagsData live outside the\n // struct due to Samsung precision issues - see #7800)\n clusterLightData_anglesData = halfData.z;\n clusterLightData.biasesData = halfData.w;\n clusterLightData_colorBFlagsData = bitcast<u32>(halfData.y);\n\n // decompress color half-floats\n let colorRG: vec2f = unpack2x16float(bitcast<u32>(halfData.x));\n let colorB_flags: vec2f = unpack2x16float(clusterLightData_colorBFlagsData);\n clusterLightData.color = vec3f(colorRG, colorB_flags.x) * {LIGHT_COLOR_DIVIDER};\n\n // position and range, full floats\n let lightPosRange: vec4f = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_POSITION_RANGE});\n clusterLightData.position = lightPosRange.xyz;\n clusterLightData.range = lightPosRange.w;\n\n // spot direction & flags data\n let lightDir_Flags: vec4f = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_DIRECTION_FLAGS});\n\n // spot light direction\n clusterLightData.direction = lightDir_Flags.xyz;\n\n // 32bit flags (kept outside the struct, see #7800)\n clusterLightData_flags = bitcast<u32>(lightDir_Flags.w);\n clusterLightData.isSpot = (clusterLightData_flags & (1u << 30u)) != 0u;\n clusterLightData.shape = (clusterLightData_flags >> 28u) & 0x3u;\n clusterLightData.falloffModeLinear = (clusterLightData_flags & (1u << 27u)) == 0u;\n clusterLightData.shadowIntensity = f32((clusterLightData_flags >> 0u) & 0xFFu) / 255.0;\n clusterLightData.cookieIntensity = f32((clusterLightData_flags >> 8u) & 0xFFu) / 255.0;\n clusterLightData.isDynamic = (clusterLightData_flags & (1u << 22u)) != 0u;\n clusterLightData.isLightmapped = (clusterLightData_flags & (1u << 21u)) != 0u;\n\n return clusterLightData;\n}\n\nfn decodeClusterLightSpot() -> ClusterLightSpotData {\n // decompress spot light angles\n let angleFlags: u32 = (clusterLightData_colorBFlagsData >> 16u) & 0xFFFFu; // Extract upper 16 bits as integer\n\n let angleValues: vec2f = unpack2x16float(bitcast<u32>(clusterLightData_anglesData));\n let innerVal: f32 = angleValues.x;\n let outerVal: f32 = angleValues.y;\n\n // decode based on flags (branch-free)\n let innerIsVersine: bool = (angleFlags & 1u) != 0u; // bit 0: inner angle format\n let outerIsVersine: bool = ((angleFlags >> 1u) & 1u) != 0u; // bit 1: outer angle format\n\n return ClusterLightSpotData(\n select(innerVal, 1.0 - innerVal, innerIsVersine),\n select(outerVal, 1.0 - outerVal, outerIsVersine)\n );\n}\n\nfn decodeClusterLightOmniAtlasViewport(lightIndex: i32) -> vec3f {\n return sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_0}).xyz;\n}\n\nfn decodeClusterLightAreaData(lightIndex: i32) -> ClusterLightAreaData {\n return ClusterLightAreaData(\n sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_AREA_DATA_WIDTH}).xyz,\n sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_AREA_DATA_HEIGHT}).xyz\n );\n}\n\nfn decodeClusterLightProjectionMatrixData(lightIndex: i32) -> mat4x4f {\n // shadow matrix\n let m0: vec4f = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_0});\n let m1: vec4f = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_1});\n let m2: vec4f = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_2});\n let m3: vec4f = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_3});\n return mat4x4f(m0, m1, m2, m3);\n}\n\nfn decodeClusterLightShadowData(biasesData: f32) -> ClusterLightShadowData {\n // shadow biases\n let biases: vec2f = unpack2x16float(bitcast<u32>(biasesData));\n return ClusterLightShadowData(biases.x, biases.y);\n}\n\nfn decodeClusterLightCookieData() -> vec4f {\n\n // extract channel mask from flags\n let cookieFlags: u32 = (clusterLightData_flags >> 23u) & 0x0Fu; // 4bits, each bit enables a channel\n let mask_uvec: vec4<u32> = vec4<u32>(cookieFlags) & vec4<u32>(1u, 2u, 4u, 8u);\n return step(vec4f(1.0), vec4f(mask_uvec)); // Normalize to 0.0 or 1.0\n}\n\nfn evaluateLight(\n light: ClusterLightData,\n worldNormal: vec3f,\n viewDir: vec3f,\n reflectionDir: vec3f,\n#if defined(LIT_CLEARCOAT)\n clearcoatReflectionDir: vec3f,\n#endif\n gloss: f32,\n specularity: vec3f,\n geometricNormal: vec3f,\n tbn: mat3x3f,\n#if defined(LIT_IRIDESCENCE)\n iridescenceFresnel: vec3f,\n#endif\n clearcoat_worldNormal: vec3f,\n clearcoat_gloss: f32,\n sheen_gloss: f32,\n iridescence_intensity: f32\n) {\n\n var cookieAttenuation: vec3f = vec3f(1.0);\n var diffuseAttenuation: f32 = 1.0;\n var falloffAttenuation: f32 = 1.0;\n\n #ifdef CLUSTER_AREALIGHTS\n // LTC specular terms of an area light, evaluated together with its diffuse term\n var areaLightSpecular: f32 = 0.0;\n var areaLightSpecularCC: f32 = 0.0;\n #endif\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n // the whole attenuation of the light arriving at the back of the surface, which the diffuse\n // transmission lets through\n var transmissionAttenuation: f32 = 0.0;\n #endif\n\n // evaluate omni part of the light\n let lightDirW: vec3f = evalOmniLight(light.position);\n let lightDirNormW: vec3f = normalize(lightDirW);\n\n #ifdef CLUSTER_AREALIGHTS\n\n // distance attenuation\n if (light.shape != {LIGHTSHAPE_PUNCTUAL}) { // area light\n\n // area lights\n let areaData: ClusterLightAreaData = decodeClusterLightAreaData(light.lightIndex);\n\n // handle light shape, only the enabled shapes are compiled (a disk uses the rect coordinates)\n #ifdef CLUSTER_AREALIGHT_SPHERE\n if (light.shape == {LIGHTSHAPE_SPHERE}) {\n calcSphereLightValues(light.position, areaData.halfWidth, areaData.halfHeight);\n } else\n #endif\n {\n calcRectLightValues(light.position, areaData.halfWidth, areaData.halfHeight);\n }\n\n falloffAttenuation = getFalloffWindow(light.range, lightDirW);\n\n } else\n\n #endif\n\n { // punctual light\n\n if (light.falloffModeLinear) {\n falloffAttenuation = getFalloffLinear(light.range, lightDirW);\n } else {\n falloffAttenuation = getFalloffInvSquared(light.range, lightDirW);\n }\n }\n\n if (falloffAttenuation > 0.00001) {\n\n #ifdef CLUSTER_AREALIGHTS\n\n if (light.shape != {LIGHTSHAPE_PUNCTUAL}) { // area light\n\n // All LTC evaluations of the light - diffuse (0), diffuse transmission (1), specular (2)\n // and clear coat specular (3) - go through a single loop, so the large LTC functions are\n // emitted once instead of once per term and shape. Shader compilers inline all calls,\n // and on Windows (FXC / DXC) the compile time of the inlined copies dominates.\n let areaIsRect: bool = light.shape == {LIGHTSHAPE_RECT};\n let areaIsDisk: bool = light.shape == {LIGHTSHAPE_DISK};\n let identityInvMat = mat3x3f(vec3f(1.0, 0.0, 0.0), vec3f(0.0, 1.0, 0.0), vec3f(0.0, 0.0, 1.0));\n\n // LTC matrices of the specular and the clear coat specular, from their own gloss and normal\n #ifdef LIT_SPECULAR\n let areaSpecularInvMat: mat3x3f = getLTCLightInvMat(dLTCUV);\n #endif\n #ifdef LIT_CLEARCOAT\n let areaClearcoatInvMat: mat3x3f = getLTCLightInvMat(ccLTCUV);\n #endif\n\n for (var lobe: i32 = 0; lobe < 4; lobe++) {\n\n #ifndef LIT_DIFFUSE_TRANSMISSION\n if (lobe == 1) { continue; }\n #endif\n #ifndef LIT_SPECULAR\n if (lobe == 2) { break; }\n #endif\n #ifndef LIT_CLEARCOAT\n if (lobe == 3) { break; }\n #endif\n\n var lobeNormal: vec3f = select(worldNormal, -worldNormal, lobe == 1);\n var lobeInvMat: mat3x3f = identityInvMat;\n #ifdef LIT_SPECULAR\n if (lobe >= 2) { lobeInvMat = areaSpecularInvMat; }\n #endif\n #ifdef LIT_CLEARCOAT\n if (lobe == 3) {\n lobeNormal = clearcoat_worldNormal;\n lobeInvMat = areaClearcoatInvMat;\n }\n #endif\n\n // only the enabled area light shapes are compiled\n var lobeValue: f32 = 0.0;\n #ifdef CLUSTER_AREALIGHT_RECT\n if (areaIsRect) {\n lobeValue = LTC_EvaluateRect(lobeNormal, viewDir, vPositionW, lobeInvMat, dLTCCoords);\n } else\n #endif\n #if defined(CLUSTER_AREALIGHT_DISK) || defined(CLUSTER_AREALIGHT_SPHERE)\n if (areaIsDisk || lobe >= 2) {\n // the specular of a sphere light uses the disk facing the reflection\n lobeValue = LTC_EvaluateDisk(lobeNormal, viewDir, vPositionW, lobeInvMat, dLTCCoords);\n if (lobe < 2) { lobeValue = FixNan(lobeValue); }\n } else\n #endif\n {\n #ifdef CLUSTER_AREALIGHT_SPHERE\n lobeValue = getSphereLightDiffuse(lobeNormal, viewDir, lightDirW, lightDirNormW);\n #endif\n }\n\n if (lobe == 0) { diffuseAttenuation = lobeValue * 16.0; }\n #ifdef LIT_DIFFUSE_TRANSMISSION\n if (lobe == 1) { transmissionAttenuation = lobeValue * 16.0; }\n #endif\n if (lobe == 2) { areaLightSpecular = lobeValue; }\n if (lobe == 3) { areaLightSpecularCC = lobeValue; }\n }\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n transmissionAttenuation = transmissionAttenuation * falloffAttenuation;\n #endif\n\n } else\n\n #endif\n\n {\n // diffuse transmission, evaluated before the diffuse lighting multiplies into the\n // falloff\n #ifdef LIT_DIFFUSE_TRANSMISSION\n transmissionAttenuation = falloffAttenuation * getLightDiffuse(-worldNormal, viewDir, lightDirNormW);\n #endif\n\n falloffAttenuation = falloffAttenuation * getLightDiffuse(worldNormal, viewDir, lightDirNormW);\n }\n\n // spot light falloff\n if (light.isSpot) {\n let spotData: ClusterLightSpotData = decodeClusterLightSpot();\n let spotEffect: f32 = getSpotEffect(light.direction, spotData.innerConeAngleCos, spotData.outerConeAngleCos, lightDirNormW);\n falloffAttenuation = falloffAttenuation * spotEffect;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n transmissionAttenuation = transmissionAttenuation * spotEffect;\n #endif\n }\n\n #if defined(CLUSTER_COOKIES) || defined(CLUSTER_SHADOWS)\n\n // with diffuse transmission, the light can also reach the surface from behind\n #ifdef LIT_DIFFUSE_TRANSMISSION\n if (max(falloffAttenuation, transmissionAttenuation) > 0.00001) {\n #else\n if (falloffAttenuation > 0.00001) {\n #endif\n\n // shadow / cookie\n if (light.shadowIntensity > 0.0 || light.cookieIntensity > 0.0) {\n\n var omniAtlasViewport: vec3f = vec3f(0.0);\n\n // shared shadow / cookie data depends on light type\n if (light.isSpot) {\n lightProjectionMatrix = decodeClusterLightProjectionMatrixData(light.lightIndex);\n } else {\n omniAtlasViewport = decodeClusterLightOmniAtlasViewport(light.lightIndex);\n }\n\n let shadowTextureResolution: f32 = uniform.shadowAtlasParams.x;\n let shadowEdgePixels: f32 = uniform.shadowAtlasParams.y;\n\n #ifdef CLUSTER_COOKIES\n\n // cookie\n if (light.cookieIntensity > 0.0) {\n let cookieChannelMask: vec4f = decodeClusterLightCookieData();\n\n if (light.isSpot) {\n cookieAttenuation = getCookie2DClustered(cookieAtlasTexture, cookieAtlasTextureSampler, lightProjectionMatrix, vPositionW, light.cookieIntensity, cookieChannelMask);\n } else {\n cookieAttenuation = getCookieCubeClustered(cookieAtlasTexture, cookieAtlasTextureSampler, lightDirW, light.cookieIntensity, cookieChannelMask, shadowTextureResolution, shadowEdgePixels, omniAtlasViewport);\n }\n }\n\n #endif\n\n #ifdef CLUSTER_SHADOWS\n\n // shadow\n if (light.shadowIntensity > 0.0) {\n let shadowData: ClusterLightShadowData = decodeClusterLightShadowData(light.biasesData);\n\n let shadowParams: vec4f = vec4f(shadowTextureResolution, shadowData.shadowNormalBias, shadowData.shadowBias, 1.0 / light.range);\n\n // The normal offset moves the sample off the surface, towards the light. A\n // surface with diffuse transmission is also lit from behind, so the side\n // facing the light is used\n #ifdef LIT_DIFFUSE_TRANSMISSION\n let shadowNormal: vec3f = faceForward(geometricNormal, lightDirNormW, geometricNormal);\n #else\n let shadowNormal: vec3f = geometricNormal;\n #endif\n\n if (light.isSpot) {\n\n // spot shadow\n let shadowCoord: vec3f = getShadowCoordPerspZbufferNormalOffset(lightProjectionMatrix, shadowParams, shadowNormal);\n\n #if defined(CLUSTER_SHADOW_TYPE_PCF1)\n let shadow: f32 = getShadowSpotClusteredPCF1(shadowAtlasTexture, shadowAtlasTextureSampler, shadowCoord, shadowParams);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF3)\n let shadow: f32 = getShadowSpotClusteredPCF3(shadowAtlasTexture, shadowAtlasTextureSampler, shadowCoord, shadowParams);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF5)\n let shadow: f32 = getShadowSpotClusteredPCF5(shadowAtlasTexture, shadowAtlasTextureSampler, shadowCoord, shadowParams);\n #elif defined(CLUSTER_SHADOW_TYPE_PCSS)\n let shadow: f32 = getShadowSpotClusteredPCSS(shadowAtlasTexture, shadowAtlasTextureSampler, shadowCoord, shadowParams);\n #endif\n falloffAttenuation = falloffAttenuation * mix(1.0, shadow, light.shadowIntensity);\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n transmissionAttenuation = transmissionAttenuation * mix(1.0, shadow, light.shadowIntensity);\n #endif\n\n } else {\n\n // omni shadow\n let dir: vec3f = normalOffsetPointShadow(shadowParams, light.position, lightDirW, lightDirNormW, shadowNormal); // normalBias adjusted for distance\n\n #if defined(CLUSTER_SHADOW_TYPE_PCF1)\n let shadow: f32 = getShadowOmniClusteredPCF1(shadowAtlasTexture, shadowAtlasTextureSampler, shadowParams, omniAtlasViewport, shadowEdgePixels, dir);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF3)\n let shadow: f32 = getShadowOmniClusteredPCF3(shadowAtlasTexture, shadowAtlasTextureSampler, shadowParams, omniAtlasViewport, shadowEdgePixels, dir);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF5)\n let shadow: f32 = getShadowOmniClusteredPCF5(shadowAtlasTexture, shadowAtlasTextureSampler, shadowParams, omniAtlasViewport, shadowEdgePixels, dir);\n #endif\n falloffAttenuation = falloffAttenuation * mix(1.0, shadow, light.shadowIntensity);\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n transmissionAttenuation = transmissionAttenuation * mix(1.0, shadow, light.shadowIntensity);\n #endif\n }\n }\n\n #endif\n }\n }\n\n #endif\n\n // diffuse / specular / clearcoat\n #ifdef CLUSTER_AREALIGHTS\n\n if (light.shape != {LIGHTSHAPE_PUNCTUAL}) { // area light\n\n // area light diffuse\n {\n var areaDiffuse: vec3f = (diffuseAttenuation * falloffAttenuation) * light.color * cookieAttenuation;\n\n #if defined(LIT_SPECULAR)\n areaDiffuse = mix(areaDiffuse, vec3f(0.0), dLTCSpecFres);\n #endif\n\n // area light diffuse - it does not mix diffuse lighting into specular attenuation\n dDiffuseLight = dDiffuseLight + areaDiffuse;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n var areaTransmission: vec3f = transmissionAttenuation * light.color * cookieAttenuation;\n\n #if defined(LIT_SPECULAR)\n areaTransmission = mix(areaTransmission, vec3f(0.0), dLTCSpecFres);\n #endif\n\n dDiffuseTransmissionLight = dDiffuseTransmissionLight + areaTransmission;\n #endif\n }\n\n // specular and clear coat are material settings and get included by a define based on the material\n #ifdef LIT_SPECULAR\n\n // area light specular, evaluated together with the diffuse term\n dSpecularLight = dSpecularLight + dLTCSpecFres * areaLightSpecular * falloffAttenuation * light.color * cookieAttenuation;\n\n #ifdef LIT_CLEARCOAT\n\n // area light specular clear coat\n ccSpecularLight = ccSpecularLight + ccLTCSpecFres * areaLightSpecularCC * falloffAttenuation * light.color * cookieAttenuation;\n\n #endif\n\n #endif\n\n } else\n\n #endif\n\n { // punctual light\n\n // punctual light diffuse\n {\n var punctualDiffuse: vec3f = falloffAttenuation * light.color * cookieAttenuation;\n\n #if defined(CLUSTER_AREALIGHTS)\n #if defined(LIT_SPECULAR)\n punctualDiffuse = mix(punctualDiffuse, vec3f(0.0), specularity);\n #endif\n #endif\n\n dDiffuseLight = dDiffuseLight + punctualDiffuse;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n var punctualTransmission: vec3f = transmissionAttenuation * light.color * cookieAttenuation;\n\n #if defined(CLUSTER_AREALIGHTS)\n #if defined(LIT_SPECULAR)\n punctualTransmission = mix(punctualTransmission, vec3f(0.0), specularity);\n #endif\n #endif\n\n dDiffuseTransmissionLight = dDiffuseTransmissionLight + punctualTransmission;\n #endif\n }\n\n // specular and clear coat are material settings and get included by a define based on the material\n #ifdef LIT_SPECULAR\n\n let halfDir: vec3f = normalize(-lightDirNormW + viewDir);\n\n // specular\n #ifdef LIT_SPECULAR_FRESNEL\n dSpecularLight = dSpecularLight +\n getLightSpecular(halfDir, reflectionDir, worldNormal, viewDir, lightDirNormW, gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation *\n getFresnel(\n dot(viewDir, halfDir),\n gloss,\n specularity\n #if defined(LIT_IRIDESCENCE)\n , iridescenceFresnel,\n iridescence_intensity\n #endif\n );\n #else\n dSpecularLight = dSpecularLight + getLightSpecular(halfDir, reflectionDir, worldNormal, viewDir, lightDirNormW, gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation * specularity;\n #endif\n\n #ifdef LIT_CLEARCOAT\n #ifdef LIT_SPECULAR_FRESNEL\n ccSpecularLight = ccSpecularLight + getLightSpecular(halfDir, clearcoatReflectionDir, clearcoat_worldNormal, viewDir, lightDirNormW, clearcoat_gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation * getFresnelCC(dot(viewDir, halfDir));\n #else\n ccSpecularLight = ccSpecularLight + getLightSpecular(halfDir, clearcoatReflectionDir, clearcoat_worldNormal, viewDir, lightDirNormW, clearcoat_gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation;\n #endif\n #endif\n\n #ifdef LIT_SHEEN\n sSpecularLight = sSpecularLight + getLightSpecularSheen(halfDir, worldNormal, viewDir, lightDirNormW, sheen_gloss) * falloffAttenuation * light.color * cookieAttenuation;\n #endif\n\n #endif\n }\n }\n\n // Write to global attenuation values (for lightmapper)\n dAtten = falloffAttenuation;\n dLightDirNormW = lightDirNormW;\n}\n\n\nfn evaluateClusterLight(\n lightIndex: i32,\n worldNormal: vec3f,\n viewDir: vec3f,\n reflectionDir: vec3f,\n#if defined(LIT_CLEARCOAT)\n clearcoatReflectionDir: vec3f,\n#endif\n gloss: f32,\n specularity: vec3f,\n geometricNormal: vec3f,\n tbn: mat3x3f,\n#if defined(LIT_IRIDESCENCE)\n iridescenceFresnel: vec3f,\n#endif\n clearcoat_worldNormal: vec3f,\n clearcoat_gloss: f32,\n sheen_gloss: f32,\n iridescence_intensity: f32\n) {\n\n // decode core light data from textures\n let clusterLightData: ClusterLightData = decodeClusterLightCore(lightIndex);\n\n // evaluate light if it uses accepted light mask\n #ifdef CLUSTER_MESH_DYNAMIC_LIGHTS\n let acceptLightMask: bool = clusterLightData.isDynamic;\n #else\n let acceptLightMask: bool = clusterLightData.isLightmapped;\n #endif\n\n if (acceptLightMask) {\n evaluateLight(\n clusterLightData,\n worldNormal,\n viewDir,\n reflectionDir,\n#if defined(LIT_CLEARCOAT)\n clearcoatReflectionDir,\n#endif\n gloss,\n specularity,\n geometricNormal,\n tbn,\n#if defined(LIT_IRIDESCENCE)\n iridescenceFresnel,\n#endif\n clearcoat_worldNormal,\n clearcoat_gloss,\n sheen_gloss,\n iridescence_intensity\n );\n }\n}\n\n\nfn addClusteredLights(\n worldNormal: vec3f,\n viewDir: vec3f,\n reflectionDir: vec3f,\n#if defined(LIT_CLEARCOAT)\n clearcoatReflectionDir: vec3f,\n#endif\n gloss: f32,\n specularity: vec3f,\n geometricNormal: vec3f,\n tbn: mat3x3f,\n#if defined(LIT_IRIDESCENCE)\n iridescenceFresnel: vec3f,\n#endif\n clearcoat_worldNormal: vec3f,\n clearcoat_gloss: f32,\n sheen_gloss: f32,\n iridescence_intensity: f32\n) {\n\n // skip if no lights (index 0 is reserved for 'no light')\n if (uniform.numClusteredLights <= 1) {\n return;\n }\n\n // world space position to 3d integer cell coordinates in the cluster structure\n let cellCoords: vec3i = vec3i(floor((vPositionW - uniform.clusterBoundsMin) * uniform.clusterCellsCountByBoundsSize));\n\n // no lighting when cell coordinate is out of range\n if (!(any(cellCoords < vec3i(0)) || any(cellCoords >= uniform.clusterCellsMax))) {\n\n // cell index (mapping from 3d cell coordinates to linear memory)\n let cellIndex: i32 = cellCoords.x * uniform.clusterCellsDot.x + cellCoords.y * uniform.clusterCellsDot.y + cellCoords.z * uniform.clusterCellsDot.z;\n\n // convert cell index to uv coordinates\n let clusterV: i32 = cellIndex / uniform.clusterTextureWidth;\n let clusterU: i32 = cellIndex - clusterV * uniform.clusterTextureWidth;\n\n // loop over maximum number of light cells\n for (var lightCellIndex: i32 = 0; lightCellIndex < uniform.clusterMaxCells; lightCellIndex = lightCellIndex + 1) {\n\n // using a single channel texture with data in red channel\n let lightIndex: u32 = textureLoad(clusterWorldTexture, vec2<i32>(clusterU + lightCellIndex, clusterV), 0).r;\n\n if (lightIndex == 0u) {\n break;\n }\n\n evaluateClusterLight(\n i32(lightIndex),\n worldNormal,\n viewDir,\n reflectionDir,\n#if defined(LIT_CLEARCOAT)\n clearcoatReflectionDir,\n#endif\n gloss,\n specularity,\n geometricNormal,\n tbn,\n#if defined(LIT_IRIDESCENCE)\n iridescenceFresnel,\n#endif\n clearcoat_worldNormal,\n clearcoat_gloss,\n sheen_gloss,\n iridescence_intensity\n );\n }\n }\n}";
|
|
2
2
|
export default _default;
|
|
@@ -145,6 +145,10 @@ fn evaluateLight(
|
|
|
145
145
|
var cookieAttenuation: vec3f = vec3f(1.0);
|
|
146
146
|
var diffuseAttenuation: f32 = 1.0;
|
|
147
147
|
var falloffAttenuation: f32 = 1.0;
|
|
148
|
+
#ifdef CLUSTER_AREALIGHTS
|
|
149
|
+
var areaLightSpecular: f32 = 0.0;
|
|
150
|
+
var areaLightSpecularCC: f32 = 0.0;
|
|
151
|
+
#endif
|
|
148
152
|
#ifdef LIT_DIFFUSE_TRANSMISSION
|
|
149
153
|
var transmissionAttenuation: f32 = 0.0;
|
|
150
154
|
#endif
|
|
@@ -153,12 +157,13 @@ fn evaluateLight(
|
|
|
153
157
|
#ifdef CLUSTER_AREALIGHTS
|
|
154
158
|
if (light.shape != {LIGHTSHAPE_PUNCTUAL}) {
|
|
155
159
|
let areaData: ClusterLightAreaData = decodeClusterLightAreaData(light.lightIndex);
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
} else if (light.shape == {LIGHTSHAPE_DISK}) {
|
|
159
|
-
calcDiskLightValues(light.position, areaData.halfWidth, areaData.halfHeight);
|
|
160
|
-
} else {
|
|
160
|
+
#ifdef CLUSTER_AREALIGHT_SPHERE
|
|
161
|
+
if (light.shape == {LIGHTSHAPE_SPHERE}) {
|
|
161
162
|
calcSphereLightValues(light.position, areaData.halfWidth, areaData.halfHeight);
|
|
163
|
+
} else
|
|
164
|
+
#endif
|
|
165
|
+
{
|
|
166
|
+
calcRectLightValues(light.position, areaData.halfWidth, areaData.halfHeight);
|
|
162
167
|
}
|
|
163
168
|
falloffAttenuation = getFalloffWindow(light.range, lightDirW);
|
|
164
169
|
} else
|
|
@@ -173,21 +178,59 @@ fn evaluateLight(
|
|
|
173
178
|
if (falloffAttenuation > 0.00001) {
|
|
174
179
|
#ifdef CLUSTER_AREALIGHTS
|
|
175
180
|
if (light.shape != {LIGHTSHAPE_PUNCTUAL}) {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
181
|
+
let areaIsRect: bool = light.shape == {LIGHTSHAPE_RECT};
|
|
182
|
+
let areaIsDisk: bool = light.shape == {LIGHTSHAPE_DISK};
|
|
183
|
+
let identityInvMat = mat3x3f(vec3f(1.0, 0.0, 0.0), vec3f(0.0, 1.0, 0.0), vec3f(0.0, 0.0, 1.0));
|
|
184
|
+
#ifdef LIT_SPECULAR
|
|
185
|
+
let areaSpecularInvMat: mat3x3f = getLTCLightInvMat(dLTCUV);
|
|
186
|
+
#endif
|
|
187
|
+
#ifdef LIT_CLEARCOAT
|
|
188
|
+
let areaClearcoatInvMat: mat3x3f = getLTCLightInvMat(ccLTCUV);
|
|
189
|
+
#endif
|
|
190
|
+
for (var lobe: i32 = 0; lobe < 4; lobe++) {
|
|
191
|
+
#ifndef LIT_DIFFUSE_TRANSMISSION
|
|
192
|
+
if (lobe == 1) { continue; }
|
|
180
193
|
#endif
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
#ifdef LIT_DIFFUSE_TRANSMISSION
|
|
184
|
-
transmissionAttenuation = getDiskLightDiffuse(-worldNormal, viewDir, lightDirW, lightDirNormW) * 16.0;
|
|
194
|
+
#ifndef LIT_SPECULAR
|
|
195
|
+
if (lobe == 2) { break; }
|
|
185
196
|
#endif
|
|
186
|
-
|
|
187
|
-
|
|
197
|
+
#ifndef LIT_CLEARCOAT
|
|
198
|
+
if (lobe == 3) { break; }
|
|
199
|
+
#endif
|
|
200
|
+
var lobeNormal: vec3f = select(worldNormal, -worldNormal, lobe == 1);
|
|
201
|
+
var lobeInvMat: mat3x3f = identityInvMat;
|
|
202
|
+
#ifdef LIT_SPECULAR
|
|
203
|
+
if (lobe >= 2) { lobeInvMat = areaSpecularInvMat; }
|
|
204
|
+
#endif
|
|
205
|
+
#ifdef LIT_CLEARCOAT
|
|
206
|
+
if (lobe == 3) {
|
|
207
|
+
lobeNormal = clearcoat_worldNormal;
|
|
208
|
+
lobeInvMat = areaClearcoatInvMat;
|
|
209
|
+
}
|
|
210
|
+
#endif
|
|
211
|
+
var lobeValue: f32 = 0.0;
|
|
212
|
+
#ifdef CLUSTER_AREALIGHT_RECT
|
|
213
|
+
if (areaIsRect) {
|
|
214
|
+
lobeValue = LTC_EvaluateRect(lobeNormal, viewDir, vPositionW, lobeInvMat, dLTCCoords);
|
|
215
|
+
} else
|
|
216
|
+
#endif
|
|
217
|
+
#if defined(CLUSTER_AREALIGHT_DISK) || defined(CLUSTER_AREALIGHT_SPHERE)
|
|
218
|
+
if (areaIsDisk || lobe >= 2) {
|
|
219
|
+
lobeValue = LTC_EvaluateDisk(lobeNormal, viewDir, vPositionW, lobeInvMat, dLTCCoords);
|
|
220
|
+
if (lobe < 2) { lobeValue = FixNan(lobeValue); }
|
|
221
|
+
} else
|
|
222
|
+
#endif
|
|
223
|
+
{
|
|
224
|
+
#ifdef CLUSTER_AREALIGHT_SPHERE
|
|
225
|
+
lobeValue = getSphereLightDiffuse(lobeNormal, viewDir, lightDirW, lightDirNormW);
|
|
226
|
+
#endif
|
|
227
|
+
}
|
|
228
|
+
if (lobe == 0) { diffuseAttenuation = lobeValue * 16.0; }
|
|
188
229
|
#ifdef LIT_DIFFUSE_TRANSMISSION
|
|
189
|
-
|
|
230
|
+
if (lobe == 1) { transmissionAttenuation = lobeValue * 16.0; }
|
|
190
231
|
#endif
|
|
232
|
+
if (lobe == 2) { areaLightSpecular = lobeValue; }
|
|
233
|
+
if (lobe == 3) { areaLightSpecularCC = lobeValue; }
|
|
191
234
|
}
|
|
192
235
|
#ifdef LIT_DIFFUSE_TRANSMISSION
|
|
193
236
|
transmissionAttenuation = transmissionAttenuation * falloffAttenuation;
|
|
@@ -293,24 +336,8 @@ fn evaluateLight(
|
|
|
293
336
|
#endif
|
|
294
337
|
}
|
|
295
338
|
#ifdef LIT_SPECULAR
|
|
296
|
-
var areaLightSpecular: f32;
|
|
297
|
-
if (light.shape == {LIGHTSHAPE_RECT}) {
|
|
298
|
-
areaLightSpecular = getRectLightSpecular(worldNormal, viewDir);
|
|
299
|
-
} else if (light.shape == {LIGHTSHAPE_DISK}) {
|
|
300
|
-
areaLightSpecular = getDiskLightSpecular(worldNormal, viewDir);
|
|
301
|
-
} else {
|
|
302
|
-
areaLightSpecular = getSphereLightSpecular(worldNormal, viewDir);
|
|
303
|
-
}
|
|
304
339
|
dSpecularLight = dSpecularLight + dLTCSpecFres * areaLightSpecular * falloffAttenuation * light.color * cookieAttenuation;
|
|
305
340
|
#ifdef LIT_CLEARCOAT
|
|
306
|
-
var areaLightSpecularCC: f32;
|
|
307
|
-
if (light.shape == {LIGHTSHAPE_RECT}) {
|
|
308
|
-
areaLightSpecularCC = getRectLightSpecular(clearcoat_worldNormal, viewDir);
|
|
309
|
-
} else if (light.shape == {LIGHTSHAPE_DISK}) {
|
|
310
|
-
areaLightSpecularCC = getDiskLightSpecular(clearcoat_worldNormal, viewDir);
|
|
311
|
-
} else {
|
|
312
|
-
areaLightSpecularCC = getSphereLightSpecular(clearcoat_worldNormal, viewDir);
|
|
313
|
-
}
|
|
314
341
|
ccSpecularLight = ccSpecularLight + ccLTCSpecFres * areaLightSpecularCC * falloffAttenuation * light.color * cookieAttenuation;
|
|
315
342
|
#endif
|
|
316
343
|
#endif
|
package/build/playcanvas/src/scene/shader-lib/wgsl/chunks/lit/frag/lighting/lightFunctionLight.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: "\n#if defined(LIGHT{i})\n\nfn evaluateLight{i}(\n #if defined(LIT_IRIDESCENCE)\n iridescenceFresnel: vec3f\n #endif\n) {\n // light color\n var lightColor: vec3f = uniform.light{i}_color;\n\n #if LIGHT{i}TYPE == DIRECTIONAL && !defined(LIT_SHADOW_CATCHER)\n // early return if the light color is black (used by shadow catcher - this way this light is very cheap)\n if (all(lightColor == vec3f(0.0, 0.0, 0.0))) {\n return;\n }\n #endif\n\n #if LIGHT{i}TYPE == DIRECTIONAL // directional light\n\n dLightDirNormW = uniform.light{i}_direction;\n dAtten = 1.0;\n\n #else // omni or spot light\n\n var lightDirW: vec3f = evalOmniLight(uniform.light{i}_position);\n dLightDirNormW = normalize(lightDirW);\n\n // cookie attenuation\n #if defined(LIGHT{i}COOKIE)\n\n #if LIGHT{i}TYPE == SPOT\n #ifdef LIGHT{i}COOKIE_FALLOFF\n #ifdef LIGHT{i}COOKIE_TRANSFORM\n var cookieAttenuation: vec3f = getCookie2DXform(uniform.light{i}_cookie, uniform.light{i}_shadowMatrix, uniform.light{i}_cookieIntensity, uniform.light{i}_cookieMatrix, uniform.light{i}_cookieOffset).{LIGHT{i}COOKIE_CHANNEL};\n #else\n var cookieAttenuation: vec3f = getCookie2D(uniform.light{i}_cookie, uniform.light{i}_shadowMatrix, uniform.light{i}_cookieIntensity).{LIGHT{i}COOKIE_CHANNEL};\n #endif\n #else\n #ifdef LIGHT{i}COOKIE_TRANSFORM\n var cookieAttenuation: vec3f = getCookie2DClipXform(uniform.light{i}_cookie, uniform.light{i}_shadowMatrix, uniform.light{i}_cookieIntensity, uniform.light{i}_cookieMatrix, uniform.light{i}_cookieOffset).{LIGHT{i}COOKIE_CHANNEL};\n #else\n var cookieAttenuation: vec3f = getCookie2DClip(uniform.light{i}_cookie, uniform.light{i}_shadowMatrix, uniform.light{i}_cookieIntensity).{LIGHT{i}COOKIE_CHANNEL};\n #endif\n #endif\n #endif\n\n #if LIGHT{i}TYPE == OMNI\n var cookieAttenuation: vec3f = getCookieCube(uniform.light{i}_cookie, uniform.light{i}_shadowMatrix, uniform.light{i}_cookieIntensity).{LIGHT{i}COOKIE_CHANNEL};\n #endif\n\n // multiply light color by the cookie attenuation\n lightColor = lightColor * cookieAttenuation;\n\n #endif\n\n // distance falloff\n #if LIGHT{i}SHAPE == PUNCTUAL\n #if LIGHT{i}FALLOFF == LINEAR\n dAtten = getFalloffLinear(uniform.light{i}_radius, lightDirW);\n #else\n dAtten = getFalloffInvSquared(uniform.light{i}_radius, lightDirW);\n #endif\n #else\n // non punctual lights only gets the range window here\n dAtten = getFalloffWindow(uniform.light{i}_radius, lightDirW);\n #endif\n\n // spot light angle falloff\n #if LIGHT{i}TYPE == SPOT\n #if !defined(LIGHT{i}COOKIE) || defined(LIGHT{i}COOKIE_FALLOFF)\n dAtten = dAtten * getSpotEffect(uniform.light{i}_direction, uniform.light{i}_innerConeAngle, uniform.light{i}_outerConeAngle, dLightDirNormW);\n #endif\n #endif\n #endif\n\n if (dAtten < 0.00001) {\n return;\n }\n\n // evaluate area light values\n #if LIGHT{i}SHAPE != PUNCTUAL\n #if LIGHT{i}SHAPE == RECT\n calcRectLightValues(uniform.light{i}_position, uniform.light{i}_halfWidth, uniform.light{i}_halfHeight);\n #elif LIGHT{i}SHAPE == DISK\n calcDiskLightValues(uniform.light{i}_position, uniform.light{i}_halfWidth, uniform.light{i}_halfHeight);\n #elif LIGHT{i}SHAPE == SPHERE\n calcSphereLightValues(uniform.light{i}_position, uniform.light{i}_halfWidth, uniform.light{i}_halfHeight);\n #endif\n #endif\n\n // diffuse transmission - the light arriving at the back of the surface, which is the diffuse\n // lighting of the flipped normal. It is evaluated before the diffuse lighting of a punctual\n // light multiplies into its attenuation\n #ifdef LIT_DIFFUSE_TRANSMISSION\n #if LIGHT{i}SHAPE == PUNCTUAL\n var attenTransmission: f32 = dAtten * getLightDiffuse(-litArgs_worldNormal, vec3(0.0), dLightDirNormW);\n #elif LIGHT{i}TYPE == DIRECTIONAL\n var attenTransmission: f32 = dAtten * getLightDiffuse(-litArgs_worldNormal, dViewDirW, dLightDirNormW);\n #elif LIGHT{i}SHAPE == RECT\n var attenTransmission: f32 = dAtten * getRectLightDiffuse(-litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == DISK\n var attenTransmission: f32 = dAtten * getDiskLightDiffuse(-litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == SPHERE\n var attenTransmission: f32 = dAtten * getSphereLightDiffuse(-litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #endif\n #endif\n\n // diffuse lighting - LTC lights do not mix diffuse lighting into attenuation that affects specular\n #if LIGHT{i}SHAPE != PUNCTUAL\n\n // attenDiffuse - separate diffuse attenuation for non-punctual light sources\n\n #if LIGHT{i}TYPE == DIRECTIONAL\n // NB: A better approximation perhaps using wrap lighting could be implemented here\n var attenDiffuse: f32 = getLightDiffuse(litArgs_worldNormal, dViewDirW, dLightDirNormW);\n #else\n // 16.0 is a constant that is in getFalloffInvSquared()\n #if LIGHT{i}SHAPE == RECT\n var attenDiffuse: f32 = getRectLightDiffuse(litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == DISK\n var attenDiffuse: f32 = getDiskLightDiffuse(litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == SPHERE\n var attenDiffuse: f32 = getSphereLightDiffuse(litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #endif\n #endif\n #else\n // one parameter is unused for punctual lights\n dAtten = dAtten * getLightDiffuse(litArgs_worldNormal, vec3(0.0), dLightDirNormW);\n #endif\n\n // apply the shadow attenuation\n #ifdef LIGHT{i}CASTSHADOW\n\n #if LIGHT{i}TYPE == DIRECTIONAL\n var shadow: f32 = getShadow{i}(vec3(0.0));\n #else\n var shadow: f32 = getShadow{i}(lightDirW);\n #endif\n\n #ifdef LIGHT{i}_SHADOW_CASCADE_BLEND\n // Fade to fully lit at the shadow distance without sampling another cascade.\n let shadowDistance: f32 = uniform.light{i}_shadowCascadeDistances.w;\n let shadowFade: f32 = smoothstep(uniform.light{i}_shadowCascadeBlend * shadowDistance, shadowDistance, 1.0 / pcPosition.w);\n shadow = mix(shadow, 1.0, shadowFade);\n #endif\n\n // Apply shadow intensity to the shadow value\n shadow = mix(1.0, shadow, uniform.light{i}_shadowIntensity);\n\n dAtten = dAtten * shadow;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n attenTransmission = attenTransmission * shadow;\n #endif\n\n #if defined(LIT_SHADOW_CATCHER) && LIGHT{i}TYPE == DIRECTIONAL\n // accumulate shadows for directional lights\n dShadowCatcher = dShadowCatcher * shadow;\n #endif \n\n #endif\n\n // Relief self shadowing, which is a property of the surface rather than of the light, so it\n // applies whether or not the light casts a shadow map. Directional lights only for now: they are\n // never clustered, so a pixel sees at most a handful of them and the cost stays bounded.\n #if defined(STD_PARALLAX_SELF_SHADOW) && LIGHT{i}TYPE == DIRECTIONAL\n dAtten = dAtten * getParallaxSelfShadow(dLightDirNormW);\n #endif\n\n #if LIGHT{i}SHAPE != PUNCTUAL\n // area light - they do not mix diffuse lighting into specular attenuation\n #ifdef LIT_SPECULAR\n dDiffuseLight = dDiffuseLight + (((attenDiffuse * dAtten) * lightColor) * (1.0 - dLTCSpecFres));\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight = dDiffuseTransmissionLight + ((attenTransmission * lightColor) * (1.0 - dLTCSpecFres));\n #endif\n #else\n dDiffuseLight = dDiffuseLight + ((attenDiffuse * dAtten) * lightColor);\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight = dDiffuseTransmissionLight + (attenTransmission * lightColor);\n #endif\n #endif \n #else\n // punctual light\n #if defined(AREA_LIGHTS) && defined(LIT_SPECULAR)\n dDiffuseLight = dDiffuseLight + ((dAtten * lightColor) * (1.0 - litArgs_specularity));\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight = dDiffuseTransmissionLight + ((attenTransmission * lightColor) * (1.0 - litArgs_specularity));\n #endif\n #else\n dDiffuseLight = dDiffuseLight + (dAtten * lightColor);\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight = dDiffuseTransmissionLight + (attenTransmission * lightColor);\n #endif\n #endif\n #endif\n\n // specular lighting\n #ifdef LIGHT{i}AFFECT_SPECULARITY\n\n #if LIGHT{i}SHAPE != PUNCTUAL // area light\n\n #ifdef LIT_CLEARCOAT\n #if LIGHT{i}SHAPE == RECT\n ccSpecularLight = ccSpecularLight + (ccLTCSpecFres * getRectLightSpecular(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor);\n #elif LIGHT{i}SHAPE == DISK\n ccSpecularLight = ccSpecularLight + (ccLTCSpecFres * getDiskLightSpecular(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor);\n #elif LIGHT{i}SHAPE == SPHERE\n ccSpecularLight = ccSpecularLight + (ccLTCSpecFres * getSphereLightSpecular(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor);\n #endif\n #endif\n\n #ifdef LIT_SPECULAR\n #if LIGHT{i}SHAPE == RECT\n dSpecularLight = dSpecularLight + (dLTCSpecFres * getRectLightSpecular(litArgs_worldNormal, dViewDirW) * dAtten * lightColor);\n #elif LIGHT{i}SHAPE == DISK\n dSpecularLight = dSpecularLight + (dLTCSpecFres * getDiskLightSpecular(litArgs_worldNormal, dViewDirW) * dAtten * lightColor);\n #elif LIGHT{i}SHAPE == SPHERE\n dSpecularLight = dSpecularLight + (dLTCSpecFres * getSphereLightSpecular(litArgs_worldNormal, dViewDirW) * dAtten * lightColor);\n #endif\n #endif\n\n #else // punctual light\n\n // is fresnel needed\n #if LIGHT{i}TYPE == DIRECTIONAL && LIT_FRESNEL_MODEL != NONE\n #define LIGHT{i}FRESNEL\n #endif\n\n #ifdef LIT_SPECULAR\n var halfDirW: vec3f = normalize(-dLightDirNormW + dViewDirW);\n #endif\n\n // if LTC lights are present, specular must be accumulated with specularity (specularity is pre multiplied by punctual light fresnel)\n #ifdef LIT_CLEARCOAT\n var lightspecularCC: vec3f = getLightSpecular(halfDirW, ccReflDirW, litArgs_clearcoat_worldNormal, dViewDirW, dLightDirNormW, litArgs_clearcoat_gloss, dTBN) * dAtten * lightColor;\n #ifdef LIGHT{i}FRESNEL\n lightspecularCC = lightspecularCC * getFresnelCC(dot(dViewDirW, halfDirW));\n #endif\n ccSpecularLight = ccSpecularLight + lightspecularCC;\n #endif\n\n #ifdef LIT_SHEEN\n sSpecularLight = sSpecularLight + (getLightSpecularSheen(halfDirW, litArgs_worldNormal, dViewDirW, dLightDirNormW, litArgs_sheen_gloss) * dAtten * lightColor);\n #endif\n\n #ifdef LIT_SPECULAR\n\n var lightSpecular: vec3f = getLightSpecular(halfDirW, dReflDirW, litArgs_worldNormal, dViewDirW, dLightDirNormW, litArgs_gloss, dTBN) * dAtten * lightColor;\n #ifdef LIGHT{i}FRESNEL\n\n #if defined(LIT_IRIDESCENCE)\n lightSpecular = lightSpecular * getFresnel(dot(dViewDirW, halfDirW), litArgs_gloss, litArgs_specularity, iridescenceFresnel, litArgs_iridescence_intensity);\n #else\n lightSpecular = lightSpecular * getFresnel(dot(dViewDirW, halfDirW), litArgs_gloss, litArgs_specularity);\n #endif\n\n #else\n lightSpecular = lightSpecular * litArgs_specularity;\n #endif\n \n dSpecularLight = dSpecularLight + lightSpecular;\n #endif\n #endif\n #endif\n}\n#endif\n";
|
|
1
|
+
declare const _default: "\n#if defined(LIGHT{i})\n\nfn evaluateLight{i}(\n #if defined(LIT_IRIDESCENCE)\n iridescenceFresnel: vec3f\n #endif\n) {\n // light color\n var lightColor: vec3f = uniform.light{i}_color;\n\n #if LIGHT{i}TYPE == DIRECTIONAL && !defined(LIT_SHADOW_CATCHER)\n // early return if the light color is black (used by shadow catcher - this way this light is very cheap)\n if (all(lightColor == vec3f(0.0, 0.0, 0.0))) {\n return;\n }\n #endif\n\n #if LIGHT{i}TYPE == DIRECTIONAL // directional light\n\n dLightDirNormW = uniform.light{i}_direction;\n dAtten = 1.0;\n\n #else // omni or spot light\n\n var lightDirW: vec3f = evalOmniLight(uniform.light{i}_position);\n dLightDirNormW = normalize(lightDirW);\n\n // cookie attenuation\n #if defined(LIGHT{i}COOKIE)\n\n #if LIGHT{i}TYPE == SPOT\n #ifdef LIGHT{i}COOKIE_FALLOFF\n #ifdef LIGHT{i}COOKIE_TRANSFORM\n var cookieAttenuation: vec3f = getCookie2DXform(uniform.light{i}_cookie, uniform.light{i}_shadowMatrix, uniform.light{i}_cookieIntensity, uniform.light{i}_cookieMatrix, uniform.light{i}_cookieOffset).{LIGHT{i}COOKIE_CHANNEL};\n #else\n var cookieAttenuation: vec3f = getCookie2D(uniform.light{i}_cookie, uniform.light{i}_shadowMatrix, uniform.light{i}_cookieIntensity).{LIGHT{i}COOKIE_CHANNEL};\n #endif\n #else\n #ifdef LIGHT{i}COOKIE_TRANSFORM\n var cookieAttenuation: vec3f = getCookie2DClipXform(uniform.light{i}_cookie, uniform.light{i}_shadowMatrix, uniform.light{i}_cookieIntensity, uniform.light{i}_cookieMatrix, uniform.light{i}_cookieOffset).{LIGHT{i}COOKIE_CHANNEL};\n #else\n var cookieAttenuation: vec3f = getCookie2DClip(uniform.light{i}_cookie, uniform.light{i}_shadowMatrix, uniform.light{i}_cookieIntensity).{LIGHT{i}COOKIE_CHANNEL};\n #endif\n #endif\n #endif\n\n #if LIGHT{i}TYPE == OMNI\n var cookieAttenuation: vec3f = getCookieCube(uniform.light{i}_cookie, uniform.light{i}_shadowMatrix, uniform.light{i}_cookieIntensity).{LIGHT{i}COOKIE_CHANNEL};\n #endif\n\n // multiply light color by the cookie attenuation\n lightColor = lightColor * cookieAttenuation;\n\n #endif\n\n // distance falloff\n #if LIGHT{i}SHAPE == PUNCTUAL\n #if LIGHT{i}FALLOFF == LINEAR\n dAtten = getFalloffLinear(uniform.light{i}_radius, lightDirW);\n #else\n dAtten = getFalloffInvSquared(uniform.light{i}_radius, lightDirW);\n #endif\n #else\n // non punctual lights only gets the range window here\n dAtten = getFalloffWindow(uniform.light{i}_radius, lightDirW);\n #endif\n\n // spot light angle falloff\n #if LIGHT{i}TYPE == SPOT\n #if !defined(LIGHT{i}COOKIE) || defined(LIGHT{i}COOKIE_FALLOFF)\n dAtten = dAtten * getSpotEffect(uniform.light{i}_direction, uniform.light{i}_innerConeAngle, uniform.light{i}_outerConeAngle, dLightDirNormW);\n #endif\n #endif\n #endif\n\n if (dAtten < 0.00001) {\n return;\n }\n\n // evaluate area light values\n #if LIGHT{i}SHAPE != PUNCTUAL\n #if LIGHT{i}SHAPE == RECT\n calcRectLightValues(uniform.light{i}_position, uniform.light{i}_halfWidth, uniform.light{i}_halfHeight);\n #elif LIGHT{i}SHAPE == DISK\n calcDiskLightValues(uniform.light{i}_position, uniform.light{i}_halfWidth, uniform.light{i}_halfHeight);\n #elif LIGHT{i}SHAPE == SPHERE\n calcSphereLightValues(uniform.light{i}_position, uniform.light{i}_halfWidth, uniform.light{i}_halfHeight);\n #endif\n #endif\n\n // diffuse transmission - the light arriving at the back of the surface, which is the diffuse\n // lighting of the flipped normal. It is evaluated before the diffuse lighting of a punctual\n // light multiplies into its attenuation\n #ifdef LIT_DIFFUSE_TRANSMISSION\n #if LIGHT{i}SHAPE == PUNCTUAL\n var attenTransmission: f32 = dAtten * getLightDiffuse(-litArgs_worldNormal, vec3(0.0), dLightDirNormW);\n #elif LIGHT{i}TYPE == DIRECTIONAL\n var attenTransmission: f32 = dAtten * getLightDiffuse(-litArgs_worldNormal, dViewDirW, dLightDirNormW);\n #elif LIGHT{i}SHAPE == RECT\n var attenTransmission: f32 = dAtten * getRectLightDiffuse(-litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == DISK\n var attenTransmission: f32 = dAtten * getDiskLightDiffuse(-litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == SPHERE\n var attenTransmission: f32 = dAtten * getSphereLightDiffuse(-litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #endif\n #endif\n\n // diffuse lighting - LTC lights do not mix diffuse lighting into attenuation that affects specular\n #if LIGHT{i}SHAPE != PUNCTUAL\n\n // attenDiffuse - separate diffuse attenuation for non-punctual light sources\n\n #if LIGHT{i}TYPE == DIRECTIONAL\n // NB: A better approximation perhaps using wrap lighting could be implemented here\n var attenDiffuse: f32 = getLightDiffuse(litArgs_worldNormal, dViewDirW, dLightDirNormW);\n #else\n // 16.0 is a constant that is in getFalloffInvSquared()\n #if LIGHT{i}SHAPE == RECT\n var attenDiffuse: f32 = getRectLightDiffuse(litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == DISK\n var attenDiffuse: f32 = getDiskLightDiffuse(litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == SPHERE\n var attenDiffuse: f32 = getSphereLightDiffuse(litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #endif\n #endif\n #else\n // one parameter is unused for punctual lights\n dAtten = dAtten * getLightDiffuse(litArgs_worldNormal, vec3(0.0), dLightDirNormW);\n #endif\n\n // apply the shadow attenuation\n #ifdef LIGHT{i}CASTSHADOW\n\n #if LIGHT{i}TYPE == DIRECTIONAL\n var shadow: f32 = getShadow{i}(vec3(0.0));\n #else\n var shadow: f32 = getShadow{i}(lightDirW);\n #endif\n\n #ifdef LIGHT{i}_SHADOW_CASCADE_BLEND\n // Fade to fully lit at the shadow distance without sampling another cascade.\n let shadowDistance: f32 = uniform.light{i}_shadowCascadeDistances.w;\n let shadowFade: f32 = smoothstep(uniform.light{i}_shadowCascadeBlend * shadowDistance, shadowDistance, 1.0 / pcPosition.w);\n shadow = mix(shadow, 1.0, shadowFade);\n #endif\n\n // Apply shadow intensity to the shadow value\n shadow = mix(1.0, shadow, uniform.light{i}_shadowIntensity);\n\n dAtten = dAtten * shadow;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n attenTransmission = attenTransmission * shadow;\n #endif\n\n #if defined(LIT_SHADOW_CATCHER) && LIGHT{i}TYPE == DIRECTIONAL\n // accumulate shadows for directional lights\n dShadowCatcher = dShadowCatcher * shadow;\n #endif \n\n #endif\n\n // Relief self shadowing, which is a property of the surface rather than of the light, so it\n // applies whether or not the light casts a shadow map. Directional lights only for now: they are\n // never clustered, so a pixel sees at most a handful of them and the cost stays bounded.\n #if defined(STD_PARALLAX_SELF_SHADOW) && LIGHT{i}TYPE == DIRECTIONAL\n dAtten = dAtten * getParallaxSelfShadow(dLightDirNormW);\n #endif\n\n #if LIGHT{i}SHAPE != PUNCTUAL\n // area light - they do not mix diffuse lighting into specular attenuation\n #ifdef LIT_SPECULAR\n dDiffuseLight = dDiffuseLight + (((attenDiffuse * dAtten) * lightColor) * (1.0 - dLTCSpecFres));\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight = dDiffuseTransmissionLight + ((attenTransmission * lightColor) * (1.0 - dLTCSpecFres));\n #endif\n #else\n dDiffuseLight = dDiffuseLight + ((attenDiffuse * dAtten) * lightColor);\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight = dDiffuseTransmissionLight + (attenTransmission * lightColor);\n #endif\n #endif \n #else\n // punctual light\n #if defined(AREA_LIGHTS) && defined(LIT_SPECULAR)\n dDiffuseLight = dDiffuseLight + ((dAtten * lightColor) * (1.0 - litArgs_specularity));\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight = dDiffuseTransmissionLight + ((attenTransmission * lightColor) * (1.0 - litArgs_specularity));\n #endif\n #else\n dDiffuseLight = dDiffuseLight + (dAtten * lightColor);\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight = dDiffuseTransmissionLight + (attenTransmission * lightColor);\n #endif\n #endif\n #endif\n\n // specular lighting\n #ifdef LIGHT{i}AFFECT_SPECULARITY\n\n #if LIGHT{i}SHAPE != PUNCTUAL // area light\n\n #ifdef LIT_CLEARCOAT\n // with the LTC matrix of the clear coat gloss (ccLTCUV)\n #if LIGHT{i}SHAPE == RECT\n ccSpecularLight = ccSpecularLight + (ccLTCSpecFres * getRectLightSpecularCC(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor);\n #elif LIGHT{i}SHAPE == DISK\n ccSpecularLight = ccSpecularLight + (ccLTCSpecFres * getDiskLightSpecularCC(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor);\n #elif LIGHT{i}SHAPE == SPHERE\n ccSpecularLight = ccSpecularLight + (ccLTCSpecFres * getSphereLightSpecularCC(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor);\n #endif\n #endif\n\n #ifdef LIT_SPECULAR\n #if LIGHT{i}SHAPE == RECT\n dSpecularLight = dSpecularLight + (dLTCSpecFres * getRectLightSpecular(litArgs_worldNormal, dViewDirW) * dAtten * lightColor);\n #elif LIGHT{i}SHAPE == DISK\n dSpecularLight = dSpecularLight + (dLTCSpecFres * getDiskLightSpecular(litArgs_worldNormal, dViewDirW) * dAtten * lightColor);\n #elif LIGHT{i}SHAPE == SPHERE\n dSpecularLight = dSpecularLight + (dLTCSpecFres * getSphereLightSpecular(litArgs_worldNormal, dViewDirW) * dAtten * lightColor);\n #endif\n #endif\n\n #else // punctual light\n\n // is fresnel needed\n #if LIGHT{i}TYPE == DIRECTIONAL && LIT_FRESNEL_MODEL != NONE\n #define LIGHT{i}FRESNEL\n #endif\n\n #ifdef LIT_SPECULAR\n var halfDirW: vec3f = normalize(-dLightDirNormW + dViewDirW);\n #endif\n\n // if LTC lights are present, specular must be accumulated with specularity (specularity is pre multiplied by punctual light fresnel)\n #ifdef LIT_CLEARCOAT\n var lightspecularCC: vec3f = getLightSpecular(halfDirW, ccReflDirW, litArgs_clearcoat_worldNormal, dViewDirW, dLightDirNormW, litArgs_clearcoat_gloss, dTBN) * dAtten * lightColor;\n #ifdef LIGHT{i}FRESNEL\n lightspecularCC = lightspecularCC * getFresnelCC(dot(dViewDirW, halfDirW));\n #endif\n ccSpecularLight = ccSpecularLight + lightspecularCC;\n #endif\n\n #ifdef LIT_SHEEN\n sSpecularLight = sSpecularLight + (getLightSpecularSheen(halfDirW, litArgs_worldNormal, dViewDirW, dLightDirNormW, litArgs_sheen_gloss) * dAtten * lightColor);\n #endif\n\n #ifdef LIT_SPECULAR\n\n var lightSpecular: vec3f = getLightSpecular(halfDirW, dReflDirW, litArgs_worldNormal, dViewDirW, dLightDirNormW, litArgs_gloss, dTBN) * dAtten * lightColor;\n #ifdef LIGHT{i}FRESNEL\n\n #if defined(LIT_IRIDESCENCE)\n lightSpecular = lightSpecular * getFresnel(dot(dViewDirW, halfDirW), litArgs_gloss, litArgs_specularity, iridescenceFresnel, litArgs_iridescence_intensity);\n #else\n lightSpecular = lightSpecular * getFresnel(dot(dViewDirW, halfDirW), litArgs_gloss, litArgs_specularity);\n #endif\n\n #else\n lightSpecular = lightSpecular * litArgs_specularity;\n #endif\n \n dSpecularLight = dSpecularLight + lightSpecular;\n #endif\n #endif\n #endif\n}\n#endif\n";
|
|
2
2
|
export default _default;
|
package/build/playcanvas/src/scene/shader-lib/wgsl/chunks/lit/frag/lighting/lightFunctionLight.js
CHANGED
|
@@ -145,11 +145,11 @@ fn evaluateLight{i}(
|
|
|
145
145
|
#if LIGHT{i}SHAPE != PUNCTUAL
|
|
146
146
|
#ifdef LIT_CLEARCOAT
|
|
147
147
|
#if LIGHT{i}SHAPE == RECT
|
|
148
|
-
ccSpecularLight = ccSpecularLight + (ccLTCSpecFres *
|
|
148
|
+
ccSpecularLight = ccSpecularLight + (ccLTCSpecFres * getRectLightSpecularCC(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor);
|
|
149
149
|
#elif LIGHT{i}SHAPE == DISK
|
|
150
|
-
ccSpecularLight = ccSpecularLight + (ccLTCSpecFres *
|
|
150
|
+
ccSpecularLight = ccSpecularLight + (ccLTCSpecFres * getDiskLightSpecularCC(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor);
|
|
151
151
|
#elif LIGHT{i}SHAPE == SPHERE
|
|
152
|
-
ccSpecularLight = ccSpecularLight + (ccLTCSpecFres *
|
|
152
|
+
ccSpecularLight = ccSpecularLight + (ccLTCSpecFres * getSphereLightSpecularCC(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor);
|
|
153
153
|
#endif
|
|
154
154
|
#endif
|
|
155
155
|
#ifdef LIT_SPECULAR
|