playcanvas 2.24.0-beta.34 → 2.24.0-beta.36
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 +5 -2
- package/build/playcanvas/src/scene/materials/lit-material.js +1 -0
- package/build/playcanvas/src/scene/materials/standard-material-options-builder.d.ts +0 -19
- package/build/playcanvas/src/scene/materials/standard-material-options-builder.js +11 -15
- package/build/playcanvas/src/scene/materials/standard-material-parameters.d.ts +1 -1
- package/build/playcanvas/src/scene/materials/standard-material-parameters.js +1 -1
- package/build/playcanvas/src/scene/materials/standard-material.d.ts +3 -11
- package/build/playcanvas/src/scene/materials/standard-material.js +8 -32
- 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 +76 -31
- 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 +5 -2
- package/build/playcanvas.dbg/src/scene/materials/lit-material.js +1 -0
- package/build/playcanvas.dbg/src/scene/materials/standard-material-options-builder.js +13 -38
- package/build/playcanvas.dbg/src/scene/materials/standard-material-parameters.js +1 -1
- package/build/playcanvas.dbg/src/scene/materials/standard-material.js +8 -32
- 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 +357 -179
- package/build/playcanvas.dbg.mjs +357 -179
- package/build/playcanvas.js +244 -144
- package/build/playcanvas.min.js +923 -844
- package/build/playcanvas.min.mjs +918 -839
- package/build/playcanvas.mjs +244 -143
- 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 +5 -2
- package/build/playcanvas.prf/src/scene/materials/lit-material.js +1 -0
- package/build/playcanvas.prf/src/scene/materials/standard-material-options-builder.js +11 -15
- package/build/playcanvas.prf/src/scene/materials/standard-material-parameters.js +1 -1
- package/build/playcanvas.prf/src/scene/materials/standard-material.js +8 -32
- 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 +244 -144
- package/build/playcanvas.prf.mjs +244 -143
- package/package.json +1 -1
|
@@ -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\nuniform highp usampler2D clusterWorldTexture;\nuniform highp sampler2D lightsTexture;\n\n#ifdef CLUSTER_SHADOWS\n // TODO: when VSM shadow is supported, it needs to use sampler2D in webgl2\n uniform sampler2DShadow shadowAtlasTexture;\n#endif\n\n#ifdef CLUSTER_COOKIES\n uniform sampler2D cookieAtlasTexture;\n#endif\n\nuniform int clusterMaxCells;\n\n// number of lights in the cluster structure\nuniform int numClusteredLights;\n\n// width of the cluster texture\nuniform int clusterTextureWidth;\n\nuniform vec3 clusterCellsCountByBoundsSize;\nuniform vec3 clusterBoundsMin;\nuniform vec3 clusterBoundsDelta;\nuniform ivec3 clusterCellsDot;\nuniform ivec3 clusterCellsMax;\nuniform vec2 shadowAtlasParams;\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 vec3 position;\n\n // light index in the lights texture\n int lightIndex;\n\n // world space direction (spot light only)\n vec3 direction;\n\n // area light shape\n uint shape;\n\n // color\n vec3 color;\n\n // 0.0 if the light doesn't cast shadows\n float shadowIntensity;\n\n // range of the light\n float range;\n\n // compressed biases, two half-floats stored in a float\n float biasesData;\n\n // intensity of the cookie\n float cookieIntensity;\n\n // true for spot lights\n bool isSpot;\n\n // light follow mode\n bool falloffModeLinear;\n\n // light mask (mutually exclusive)\n bool isDynamic;\n bool isLightmapped;\n};\n\n// Spot light cone angles, decoded on demand only when the light is a spot light.\nstruct ClusterLightSpotData {\n float innerConeAngleCos;\n float outerConeAngleCos;\n};\n\n// Area light dimensions and orientation, decoded on demand only for non-punctual lights.\nstruct ClusterLightAreaData {\n vec3 halfWidth;\n vec3 halfHeight;\n};\n\n// Shadow bias parameters, decoded on demand only when the light casts shadows.\nstruct ClusterLightShadowData {\n float shadowBias;\n float shadowNormalBias;\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. See https://github.com/KhronosGroup/WebGL/issues/3351\n\n// shadow (spot light only) / cookie projection matrix\nmat4 lightProjectionMatrix;\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.\nuint clusterLightData_flags; // 32bit of flags\nfloat clusterLightData_anglesData; // compressed angles, two half-floats stored in a float\nuint clusterLightData_colorBFlagsData; // blue color component and angle flags (as uint for efficient bit operations)\n\nvec4 sampleLightTextureF(int lightIndex, int index) {\n return texelFetch(lightsTexture, ivec2(index, lightIndex), 0);\n}\n\nClusterLightData decodeClusterLightCore(int lightIndex) {\n ClusterLightData clusterLightData;\n\n // light index\n clusterLightData.lightIndex = lightIndex;\n\n // sample data encoding half-float values into 32bit uints\n vec4 halfData = 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 = floatBitsToUint(halfData.y);\n\n // decompress color half-floats\n vec2 colorRG = unpackHalf2x16(floatBitsToUint(halfData.x));\n vec2 colorB_flags = unpackHalf2x16(clusterLightData_colorBFlagsData);\n clusterLightData.color = vec3(colorRG, colorB_flags.x) * {LIGHT_COLOR_DIVIDER};\n\n // position and range, full floats\n vec4 lightPosRange = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_POSITION_RANGE});\n clusterLightData.position = lightPosRange.xyz;\n clusterLightData.range = lightPosRange.w;\n\n // spot direction & flags data\n vec4 lightDir_Flags = 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 = floatBitsToUint(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 = float((clusterLightData_flags >> 0u) & 0xFFu) / 255.0;\n clusterLightData.cookieIntensity = float((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\nClusterLightSpotData decodeClusterLightSpot() {\n // decompress spot light angles\n uint angleFlags = (clusterLightData_colorBFlagsData >> 16u) & 0xFFFFu; // Extract upper 16 bits as integer\n\n vec2 angleValues = unpackHalf2x16(floatBitsToUint(clusterLightData_anglesData));\n float innerVal = angleValues.x;\n float outerVal = angleValues.y;\n\n // decode based on flags (branch-free)\n float innerIsVersine = float(angleFlags & 1u); // bit 0: inner angle format\n float outerIsVersine = float((angleFlags >> 1u) & 1u); // bit 1: outer angle format\n\n return ClusterLightSpotData(\n mix(innerVal, 1.0 - innerVal, innerIsVersine),\n mix(outerVal, 1.0 - outerVal, outerIsVersine)\n );\n}\n\nvec3 decodeClusterLightOmniAtlasViewport(int lightIndex) {\n return sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_0}).xyz;\n}\n\nClusterLightAreaData decodeClusterLightAreaData(int lightIndex) {\n return ClusterLightAreaData(\n sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_AREA_DATA_WIDTH}).xyz,\n sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_AREA_DATA_HEIGHT}).xyz\n );\n}\n\nmat4 decodeClusterLightProjectionMatrixData(int lightIndex) {\n // shadow matrix\n vec4 m0 = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_0});\n vec4 m1 = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_1});\n vec4 m2 = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_2});\n vec4 m3 = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_3});\n return mat4(m0, m1, m2, m3);\n}\n\nClusterLightShadowData decodeClusterLightShadowData(float biasesData) {\n // shadow biases\n vec2 biases = unpackHalf2x16(floatBitsToUint(biasesData));\n return ClusterLightShadowData(biases.x, biases.y);\n}\n\nvec4 decodeClusterLightCookieData() {\n // extract channel mask from flags\n uint cookieFlags = (clusterLightData_flags >> 23u) & 0x0Fu; // 4bits, each bit enables a channel\n vec4 mask = vec4(uvec4(cookieFlags) & uvec4(1u, 2u, 4u, 8u));\n return step(1.0, mask); // Normalize to 0.0 or 1.0\n}\n\nvoid evaluateLight(\n ClusterLightData light, \n vec3 worldNormal, \n vec3 viewDir, \n vec3 reflectionDir,\n#if defined(LIT_CLEARCOAT)\n vec3 clearcoatReflectionDir,\n#endif\n float gloss, \n vec3 specularity, \n vec3 geometricNormal, \n mat3 tbn, \n#if defined(LIT_IRIDESCENCE)\n vec3 iridescenceFresnel,\n#endif\n vec3 clearcoat_worldNormal,\n float clearcoat_gloss,\n float sheen_gloss,\n float iridescence_intensity\n) {\n\n vec3 cookieAttenuation = vec3(1.0);\n float diffuseAttenuation = 1.0;\n float falloffAttenuation = 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 float transmissionAttenuation = 0.0;\n #endif\n\n // evaluate omni part of the light\n vec3 lightDirW = evalOmniLight(light.position);\n vec3 lightDirNormW = normalize(lightDirW);\n\n #ifdef CLUSTER_AREALIGHTS\n\n // distance attenuation\n if (light.shape != {LIGHTSHAPE_PUNCTUAL}) { // area light\n\n // area lights\n ClusterLightAreaData areaData = 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 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 *= 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 *= getLightDiffuse(worldNormal, viewDir, lightDirNormW);\n }\n\n // spot light falloff\n if (light.isSpot) {\n ClusterLightSpotData spotData = decodeClusterLightSpot();\n float spotEffect = getSpotEffect(light.direction, spotData.innerConeAngleCos, spotData.outerConeAngleCos, lightDirNormW);\n falloffAttenuation *= spotEffect;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n 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 vec3 omniAtlasViewport = vec3(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 float shadowTextureResolution = shadowAtlasParams.x;\n float shadowEdgePixels = shadowAtlasParams.y;\n\n #ifdef CLUSTER_COOKIES\n\n // cookie\n if (light.cookieIntensity > 0.0) {\n vec4 cookieChannelMask = decodeClusterLightCookieData();\n\n if (light.isSpot) {\n cookieAttenuation = getCookie2DClustered(TEXTURE_PASS(cookieAtlasTexture), lightProjectionMatrix, vPositionW, light.cookieIntensity, cookieChannelMask);\n } else {\n cookieAttenuation = getCookieCubeClustered(TEXTURE_PASS(cookieAtlasTexture), 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 ClusterLightShadowData shadowData = decodeClusterLightShadowData(light.biasesData);\n\n vec4 shadowParams = vec4(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 vec3 shadowNormal = faceforward(geometricNormal, lightDirNormW, geometricNormal);\n #else\n vec3 shadowNormal = geometricNormal;\n #endif\n\n if (light.isSpot) {\n\n // spot shadow\n vec3 shadowCoord = getShadowCoordPerspZbufferNormalOffset(lightProjectionMatrix, shadowParams, shadowNormal);\n\n #if defined(CLUSTER_SHADOW_TYPE_PCF1)\n float shadow = getShadowSpotClusteredPCF1(SHADOWMAP_PASS(shadowAtlasTexture), shadowCoord, shadowParams);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF3)\n float shadow = getShadowSpotClusteredPCF3(SHADOWMAP_PASS(shadowAtlasTexture), shadowCoord, shadowParams);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF5)\n float shadow = getShadowSpotClusteredPCF5(SHADOWMAP_PASS(shadowAtlasTexture), shadowCoord, shadowParams);\n #elif defined(CLUSTER_SHADOW_TYPE_PCSS)\n float shadow = getShadowSpotClusteredPCSS(SHADOWMAP_PASS(shadowAtlasTexture), shadowCoord, shadowParams);\n #endif\n falloffAttenuation *= mix(1.0, shadow, light.shadowIntensity);\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n transmissionAttenuation *= mix(1.0, shadow, light.shadowIntensity);\n #endif\n\n } else {\n\n // omni shadow\n vec3 dir = normalOffsetPointShadow(shadowParams, light.position, lightDirW, lightDirNormW, shadowNormal); // normalBias adjusted for distance\n\n #if defined(CLUSTER_SHADOW_TYPE_PCF1)\n float shadow = getShadowOmniClusteredPCF1(SHADOWMAP_PASS(shadowAtlasTexture), shadowParams, omniAtlasViewport, shadowEdgePixels, dir);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF3)\n float shadow = getShadowOmniClusteredPCF3(SHADOWMAP_PASS(shadowAtlasTexture), shadowParams, omniAtlasViewport, shadowEdgePixels, dir);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF5)\n float shadow = getShadowOmniClusteredPCF5(SHADOWMAP_PASS(shadowAtlasTexture), shadowParams, omniAtlasViewport, shadowEdgePixels, dir);\n #endif\n falloffAttenuation *= mix(1.0, shadow, light.shadowIntensity);\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n 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 vec3 areaDiffuse = (diffuseAttenuation * falloffAttenuation) * light.color * cookieAttenuation;\n\n #if defined(LIT_SPECULAR)\n areaDiffuse = mix(areaDiffuse, vec3(0), dLTCSpecFres);\n #endif\n\n // area light diffuse - it does not mix diffuse lighting into specular attenuation\n dDiffuseLight += areaDiffuse;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n vec3 areaTransmission = transmissionAttenuation * light.color * cookieAttenuation;\n\n #if defined(LIT_SPECULAR)\n areaTransmission = mix(areaTransmission, vec3(0), dLTCSpecFres);\n #endif\n\n 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 float areaLightSpecular;\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 += dLTCSpecFres * areaLightSpecular * falloffAttenuation * light.color * cookieAttenuation;\n\n #ifdef LIT_CLEARCOAT\n\n // area light specular clear coat\n float areaLightSpecularCC;\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 += 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 vec3 punctualDiffuse = falloffAttenuation * light.color * cookieAttenuation;\n\n #if defined(CLUSTER_AREALIGHTS)\n #if defined(LIT_SPECULAR)\n punctualDiffuse = mix(punctualDiffuse, vec3(0), specularity);\n #endif\n #endif\n\n dDiffuseLight += punctualDiffuse;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n vec3 punctualTransmission = transmissionAttenuation * light.color * cookieAttenuation;\n\n #if defined(CLUSTER_AREALIGHTS)\n #if defined(LIT_SPECULAR)\n punctualTransmission = mix(punctualTransmission, vec3(0), specularity);\n #endif\n #endif\n\n 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 vec3 halfDir = normalize(-lightDirNormW + viewDir);\n \n // specular\n #ifdef LIT_SPECULAR_FRESNEL\n 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 += 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 += getLightSpecular(halfDir, clearcoatReflectionDir, clearcoat_worldNormal, viewDir, lightDirNormW, clearcoat_gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation * getFresnelCC(dot(viewDir, halfDir));\n #else\n 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 += 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\nvoid evaluateClusterLight(\n int lightIndex, \n vec3 worldNormal, \n vec3 viewDir, \n vec3 reflectionDir, \n#if defined(LIT_CLEARCOAT)\n vec3 clearcoatReflectionDir,\n#endif\n float gloss, \n vec3 specularity, \n vec3 geometricNormal, \n mat3 tbn, \n#if defined(LIT_IRIDESCENCE)\n vec3 iridescenceFresnel,\n#endif\n vec3 clearcoat_worldNormal,\n float clearcoat_gloss,\n float sheen_gloss,\n float iridescence_intensity\n) {\n\n // decode core light data from textures\n ClusterLightData clusterLightData = decodeClusterLightCore(lightIndex);\n\n // evaluate light if it uses accepted light mask\n #ifdef CLUSTER_MESH_DYNAMIC_LIGHTS\n bool acceptLightMask = clusterLightData.isDynamic;\n #else\n bool acceptLightMask = 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\nvoid addClusteredLights(\n vec3 worldNormal, \n vec3 viewDir, \n vec3 reflectionDir, \n#if defined(LIT_CLEARCOAT)\n vec3 clearcoatReflectionDir,\n#endif\n float gloss, \n vec3 specularity, \n vec3 geometricNormal, \n mat3 tbn, \n#if defined(LIT_IRIDESCENCE)\n vec3 iridescenceFresnel,\n#endif\n vec3 clearcoat_worldNormal,\n float clearcoat_gloss,\n float sheen_gloss,\n float iridescence_intensity\n) {\n\n // skip if no lights (index 0 is reserved for 'no light')\n if (numClusteredLights <= 1)\n return;\n\n // world space position to 3d integer cell coordinates in the cluster structure\n ivec3 cellCoords = ivec3(floor((vPositionW - clusterBoundsMin) * clusterCellsCountByBoundsSize));\n\n // no lighting when cell coordinate is out of range\n if (!(any(lessThan(cellCoords, ivec3(0))) || any(greaterThanEqual(cellCoords, clusterCellsMax)))) {\n\n // cell index (mapping from 3d cell coordinates to linear memory)\n int cellIndex = cellCoords.x * clusterCellsDot.x + cellCoords.y * clusterCellsDot.y + cellCoords.z * clusterCellsDot.z;\n\n // convert cell index to uv coordinates\n int clusterV = cellIndex / clusterTextureWidth;\n int clusterU = cellIndex - clusterV * clusterTextureWidth;\n\n // loop over maximum number of light cells\n for (int lightCellIndex = 0; lightCellIndex < clusterMaxCells; lightCellIndex++) {\n\n // using a single channel texture with data in red channel\n uint lightIndex = texelFetch(clusterWorldTexture, ivec2(clusterU + lightCellIndex, clusterV), 0).x;\n\n if (lightIndex == 0u)\n break;\n\n evaluateClusterLight(\n int(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}\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\nuniform highp usampler2D clusterWorldTexture;\nuniform highp sampler2D lightsTexture;\n\n#ifdef CLUSTER_SHADOWS\n // TODO: when VSM shadow is supported, it needs to use sampler2D in webgl2\n uniform sampler2DShadow shadowAtlasTexture;\n#endif\n\n#ifdef CLUSTER_COOKIES\n uniform sampler2D cookieAtlasTexture;\n#endif\n\nuniform int clusterMaxCells;\n\n// number of lights in the cluster structure\nuniform int numClusteredLights;\n\n// width of the cluster texture\nuniform int clusterTextureWidth;\n\nuniform vec3 clusterCellsCountByBoundsSize;\nuniform vec3 clusterBoundsMin;\nuniform vec3 clusterBoundsDelta;\nuniform ivec3 clusterCellsDot;\nuniform ivec3 clusterCellsMax;\nuniform vec2 shadowAtlasParams;\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 vec3 position;\n\n // light index in the lights texture\n int lightIndex;\n\n // world space direction (spot light only)\n vec3 direction;\n\n // area light shape\n uint shape;\n\n // color\n vec3 color;\n\n // 0.0 if the light doesn't cast shadows\n float shadowIntensity;\n\n // range of the light\n float range;\n\n // compressed biases, two half-floats stored in a float\n float biasesData;\n\n // intensity of the cookie\n float cookieIntensity;\n\n // true for spot lights\n bool isSpot;\n\n // light follow mode\n bool falloffModeLinear;\n\n // light mask (mutually exclusive)\n bool isDynamic;\n bool isLightmapped;\n};\n\n// Spot light cone angles, decoded on demand only when the light is a spot light.\nstruct ClusterLightSpotData {\n float innerConeAngleCos;\n float outerConeAngleCos;\n};\n\n// Area light dimensions and orientation, decoded on demand only for non-punctual lights.\nstruct ClusterLightAreaData {\n vec3 halfWidth;\n vec3 halfHeight;\n};\n\n// Shadow bias parameters, decoded on demand only when the light casts shadows.\nstruct ClusterLightShadowData {\n float shadowBias;\n float shadowNormalBias;\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. See https://github.com/KhronosGroup/WebGL/issues/3351\n\n// shadow (spot light only) / cookie projection matrix\nmat4 lightProjectionMatrix;\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.\nuint clusterLightData_flags; // 32bit of flags\nfloat clusterLightData_anglesData; // compressed angles, two half-floats stored in a float\nuint clusterLightData_colorBFlagsData; // blue color component and angle flags (as uint for efficient bit operations)\n\nvec4 sampleLightTextureF(int lightIndex, int index) {\n return texelFetch(lightsTexture, ivec2(index, lightIndex), 0);\n}\n\nClusterLightData decodeClusterLightCore(int lightIndex) {\n ClusterLightData clusterLightData;\n\n // light index\n clusterLightData.lightIndex = lightIndex;\n\n // sample data encoding half-float values into 32bit uints\n vec4 halfData = 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 = floatBitsToUint(halfData.y);\n\n // decompress color half-floats\n vec2 colorRG = unpackHalf2x16(floatBitsToUint(halfData.x));\n vec2 colorB_flags = unpackHalf2x16(clusterLightData_colorBFlagsData);\n clusterLightData.color = vec3(colorRG, colorB_flags.x) * {LIGHT_COLOR_DIVIDER};\n\n // position and range, full floats\n vec4 lightPosRange = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_POSITION_RANGE});\n clusterLightData.position = lightPosRange.xyz;\n clusterLightData.range = lightPosRange.w;\n\n // spot direction & flags data\n vec4 lightDir_Flags = 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 = floatBitsToUint(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 = float((clusterLightData_flags >> 0u) & 0xFFu) / 255.0;\n clusterLightData.cookieIntensity = float((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\nClusterLightSpotData decodeClusterLightSpot() {\n // decompress spot light angles\n uint angleFlags = (clusterLightData_colorBFlagsData >> 16u) & 0xFFFFu; // Extract upper 16 bits as integer\n\n vec2 angleValues = unpackHalf2x16(floatBitsToUint(clusterLightData_anglesData));\n float innerVal = angleValues.x;\n float outerVal = angleValues.y;\n\n // decode based on flags (branch-free)\n float innerIsVersine = float(angleFlags & 1u); // bit 0: inner angle format\n float outerIsVersine = float((angleFlags >> 1u) & 1u); // bit 1: outer angle format\n\n return ClusterLightSpotData(\n mix(innerVal, 1.0 - innerVal, innerIsVersine),\n mix(outerVal, 1.0 - outerVal, outerIsVersine)\n );\n}\n\nvec3 decodeClusterLightOmniAtlasViewport(int lightIndex) {\n return sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_0}).xyz;\n}\n\nClusterLightAreaData decodeClusterLightAreaData(int lightIndex) {\n return ClusterLightAreaData(\n sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_AREA_DATA_WIDTH}).xyz,\n sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_AREA_DATA_HEIGHT}).xyz\n );\n}\n\nmat4 decodeClusterLightProjectionMatrixData(int lightIndex) {\n // shadow matrix\n vec4 m0 = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_0});\n vec4 m1 = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_1});\n vec4 m2 = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_2});\n vec4 m3 = sampleLightTextureF(lightIndex, {CLUSTER_TEXTURE_PROJ_MAT_3});\n return mat4(m0, m1, m2, m3);\n}\n\nClusterLightShadowData decodeClusterLightShadowData(float biasesData) {\n // shadow biases\n vec2 biases = unpackHalf2x16(floatBitsToUint(biasesData));\n return ClusterLightShadowData(biases.x, biases.y);\n}\n\nvec4 decodeClusterLightCookieData() {\n // extract channel mask from flags\n uint cookieFlags = (clusterLightData_flags >> 23u) & 0x0Fu; // 4bits, each bit enables a channel\n vec4 mask = vec4(uvec4(cookieFlags) & uvec4(1u, 2u, 4u, 8u));\n return step(1.0, mask); // Normalize to 0.0 or 1.0\n}\n\nvoid evaluateLight(\n ClusterLightData light, \n vec3 worldNormal, \n vec3 viewDir, \n vec3 reflectionDir,\n#if defined(LIT_CLEARCOAT)\n vec3 clearcoatReflectionDir,\n#endif\n float gloss, \n vec3 specularity, \n vec3 geometricNormal, \n mat3 tbn, \n#if defined(LIT_IRIDESCENCE)\n vec3 iridescenceFresnel,\n#endif\n vec3 clearcoat_worldNormal,\n float clearcoat_gloss,\n float sheen_gloss,\n float iridescence_intensity\n) {\n\n vec3 cookieAttenuation = vec3(1.0);\n float diffuseAttenuation = 1.0;\n float falloffAttenuation = 1.0;\n\n #ifdef CLUSTER_AREALIGHTS\n // LTC specular terms of an area light, evaluated together with its diffuse term\n float areaLightSpecular = 0.0;\n float areaLightSpecularCC = 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 float transmissionAttenuation = 0.0;\n #endif\n\n // evaluate omni part of the light\n vec3 lightDirW = evalOmniLight(light.position);\n vec3 lightDirNormW = normalize(lightDirW);\n\n #ifdef CLUSTER_AREALIGHTS\n\n // distance attenuation\n if (light.shape != {LIGHTSHAPE_PUNCTUAL}) { // area light\n\n // area lights\n ClusterLightAreaData areaData = 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 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 bool areaIsRect = light.shape == {LIGHTSHAPE_RECT};\n bool areaIsDisk = light.shape == {LIGHTSHAPE_DISK};\n\n // LTC matrices of the specular and the clear coat specular, from their own gloss and normal\n #ifdef LIT_SPECULAR\n mat3 areaSpecularInvMat = getLTCLightInvMat(dLTCUV);\n #endif\n #ifdef LIT_CLEARCOAT\n mat3 areaClearcoatInvMat = getLTCLightInvMat(ccLTCUV);\n #endif\n\n for (int lobe = 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 vec3 lobeNormal = lobe == 1 ? -worldNormal : worldNormal;\n mat3 lobeInvMat = mat3(1.0);\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 float lobeValue = 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 *= 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 *= getLightDiffuse(worldNormal, viewDir, lightDirNormW);\n }\n\n // spot light falloff\n if (light.isSpot) {\n ClusterLightSpotData spotData = decodeClusterLightSpot();\n float spotEffect = getSpotEffect(light.direction, spotData.innerConeAngleCos, spotData.outerConeAngleCos, lightDirNormW);\n falloffAttenuation *= spotEffect;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n 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 vec3 omniAtlasViewport = vec3(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 float shadowTextureResolution = shadowAtlasParams.x;\n float shadowEdgePixels = shadowAtlasParams.y;\n\n #ifdef CLUSTER_COOKIES\n\n // cookie\n if (light.cookieIntensity > 0.0) {\n vec4 cookieChannelMask = decodeClusterLightCookieData();\n\n if (light.isSpot) {\n cookieAttenuation = getCookie2DClustered(TEXTURE_PASS(cookieAtlasTexture), lightProjectionMatrix, vPositionW, light.cookieIntensity, cookieChannelMask);\n } else {\n cookieAttenuation = getCookieCubeClustered(TEXTURE_PASS(cookieAtlasTexture), 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 ClusterLightShadowData shadowData = decodeClusterLightShadowData(light.biasesData);\n\n vec4 shadowParams = vec4(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 vec3 shadowNormal = faceforward(geometricNormal, lightDirNormW, geometricNormal);\n #else\n vec3 shadowNormal = geometricNormal;\n #endif\n\n if (light.isSpot) {\n\n // spot shadow\n vec3 shadowCoord = getShadowCoordPerspZbufferNormalOffset(lightProjectionMatrix, shadowParams, shadowNormal);\n\n #if defined(CLUSTER_SHADOW_TYPE_PCF1)\n float shadow = getShadowSpotClusteredPCF1(SHADOWMAP_PASS(shadowAtlasTexture), shadowCoord, shadowParams);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF3)\n float shadow = getShadowSpotClusteredPCF3(SHADOWMAP_PASS(shadowAtlasTexture), shadowCoord, shadowParams);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF5)\n float shadow = getShadowSpotClusteredPCF5(SHADOWMAP_PASS(shadowAtlasTexture), shadowCoord, shadowParams);\n #elif defined(CLUSTER_SHADOW_TYPE_PCSS)\n float shadow = getShadowSpotClusteredPCSS(SHADOWMAP_PASS(shadowAtlasTexture), shadowCoord, shadowParams);\n #endif\n falloffAttenuation *= mix(1.0, shadow, light.shadowIntensity);\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n transmissionAttenuation *= mix(1.0, shadow, light.shadowIntensity);\n #endif\n\n } else {\n\n // omni shadow\n vec3 dir = normalOffsetPointShadow(shadowParams, light.position, lightDirW, lightDirNormW, shadowNormal); // normalBias adjusted for distance\n\n #if defined(CLUSTER_SHADOW_TYPE_PCF1)\n float shadow = getShadowOmniClusteredPCF1(SHADOWMAP_PASS(shadowAtlasTexture), shadowParams, omniAtlasViewport, shadowEdgePixels, dir);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF3)\n float shadow = getShadowOmniClusteredPCF3(SHADOWMAP_PASS(shadowAtlasTexture), shadowParams, omniAtlasViewport, shadowEdgePixels, dir);\n #elif defined(CLUSTER_SHADOW_TYPE_PCF5)\n float shadow = getShadowOmniClusteredPCF5(SHADOWMAP_PASS(shadowAtlasTexture), shadowParams, omniAtlasViewport, shadowEdgePixels, dir);\n #endif\n falloffAttenuation *= mix(1.0, shadow, light.shadowIntensity);\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n 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 vec3 areaDiffuse = (diffuseAttenuation * falloffAttenuation) * light.color * cookieAttenuation;\n\n #if defined(LIT_SPECULAR)\n areaDiffuse = mix(areaDiffuse, vec3(0), dLTCSpecFres);\n #endif\n\n // area light diffuse - it does not mix diffuse lighting into specular attenuation\n dDiffuseLight += areaDiffuse;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n vec3 areaTransmission = transmissionAttenuation * light.color * cookieAttenuation;\n\n #if defined(LIT_SPECULAR)\n areaTransmission = mix(areaTransmission, vec3(0), dLTCSpecFres);\n #endif\n\n 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 += dLTCSpecFres * areaLightSpecular * falloffAttenuation * light.color * cookieAttenuation;\n\n #ifdef LIT_CLEARCOAT\n\n // area light specular clear coat\n 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 vec3 punctualDiffuse = falloffAttenuation * light.color * cookieAttenuation;\n\n #if defined(CLUSTER_AREALIGHTS)\n #if defined(LIT_SPECULAR)\n punctualDiffuse = mix(punctualDiffuse, vec3(0), specularity);\n #endif\n #endif\n\n dDiffuseLight += punctualDiffuse;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n vec3 punctualTransmission = transmissionAttenuation * light.color * cookieAttenuation;\n\n #if defined(CLUSTER_AREALIGHTS)\n #if defined(LIT_SPECULAR)\n punctualTransmission = mix(punctualTransmission, vec3(0), specularity);\n #endif\n #endif\n\n 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 vec3 halfDir = normalize(-lightDirNormW + viewDir);\n \n // specular\n #ifdef LIT_SPECULAR_FRESNEL\n 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 += 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 += getLightSpecular(halfDir, clearcoatReflectionDir, clearcoat_worldNormal, viewDir, lightDirNormW, clearcoat_gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation * getFresnelCC(dot(viewDir, halfDir));\n #else\n 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 += 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\nvoid evaluateClusterLight(\n int lightIndex, \n vec3 worldNormal, \n vec3 viewDir, \n vec3 reflectionDir, \n#if defined(LIT_CLEARCOAT)\n vec3 clearcoatReflectionDir,\n#endif\n float gloss, \n vec3 specularity, \n vec3 geometricNormal, \n mat3 tbn, \n#if defined(LIT_IRIDESCENCE)\n vec3 iridescenceFresnel,\n#endif\n vec3 clearcoat_worldNormal,\n float clearcoat_gloss,\n float sheen_gloss,\n float iridescence_intensity\n) {\n\n // decode core light data from textures\n ClusterLightData clusterLightData = decodeClusterLightCore(lightIndex);\n\n // evaluate light if it uses accepted light mask\n #ifdef CLUSTER_MESH_DYNAMIC_LIGHTS\n bool acceptLightMask = clusterLightData.isDynamic;\n #else\n bool acceptLightMask = 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\nvoid addClusteredLights(\n vec3 worldNormal, \n vec3 viewDir, \n vec3 reflectionDir, \n#if defined(LIT_CLEARCOAT)\n vec3 clearcoatReflectionDir,\n#endif\n float gloss, \n vec3 specularity, \n vec3 geometricNormal, \n mat3 tbn, \n#if defined(LIT_IRIDESCENCE)\n vec3 iridescenceFresnel,\n#endif\n vec3 clearcoat_worldNormal,\n float clearcoat_gloss,\n float sheen_gloss,\n float iridescence_intensity\n) {\n\n // skip if no lights (index 0 is reserved for 'no light')\n if (numClusteredLights <= 1)\n return;\n\n // world space position to 3d integer cell coordinates in the cluster structure\n ivec3 cellCoords = ivec3(floor((vPositionW - clusterBoundsMin) * clusterCellsCountByBoundsSize));\n\n // no lighting when cell coordinate is out of range\n if (!(any(lessThan(cellCoords, ivec3(0))) || any(greaterThanEqual(cellCoords, clusterCellsMax)))) {\n\n // cell index (mapping from 3d cell coordinates to linear memory)\n int cellIndex = cellCoords.x * clusterCellsDot.x + cellCoords.y * clusterCellsDot.y + cellCoords.z * clusterCellsDot.z;\n\n // convert cell index to uv coordinates\n int clusterV = cellIndex / clusterTextureWidth;\n int clusterU = cellIndex - clusterV * clusterTextureWidth;\n\n // loop over maximum number of light cells\n for (int lightCellIndex = 0; lightCellIndex < clusterMaxCells; lightCellIndex++) {\n\n // using a single channel texture with data in red channel\n uint lightIndex = texelFetch(clusterWorldTexture, ivec2(clusterU + lightCellIndex, clusterV), 0).x;\n\n if (lightIndex == 0u)\n break;\n\n evaluateClusterLight(\n int(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}\n";
|
|
2
2
|
export default _default;
|
|
@@ -143,6 +143,10 @@ void evaluateLight(
|
|
|
143
143
|
vec3 cookieAttenuation = vec3(1.0);
|
|
144
144
|
float diffuseAttenuation = 1.0;
|
|
145
145
|
float falloffAttenuation = 1.0;
|
|
146
|
+
#ifdef CLUSTER_AREALIGHTS
|
|
147
|
+
float areaLightSpecular = 0.0;
|
|
148
|
+
float areaLightSpecularCC = 0.0;
|
|
149
|
+
#endif
|
|
146
150
|
#ifdef LIT_DIFFUSE_TRANSMISSION
|
|
147
151
|
float transmissionAttenuation = 0.0;
|
|
148
152
|
#endif
|
|
@@ -151,12 +155,13 @@ void evaluateLight(
|
|
|
151
155
|
#ifdef CLUSTER_AREALIGHTS
|
|
152
156
|
if (light.shape != {LIGHTSHAPE_PUNCTUAL}) {
|
|
153
157
|
ClusterLightAreaData areaData = decodeClusterLightAreaData(light.lightIndex);
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
} else if (light.shape == {LIGHTSHAPE_DISK}) {
|
|
157
|
-
calcDiskLightValues(light.position, areaData.halfWidth, areaData.halfHeight);
|
|
158
|
-
} else {
|
|
158
|
+
#ifdef CLUSTER_AREALIGHT_SPHERE
|
|
159
|
+
if (light.shape == {LIGHTSHAPE_SPHERE}) {
|
|
159
160
|
calcSphereLightValues(light.position, areaData.halfWidth, areaData.halfHeight);
|
|
161
|
+
} else
|
|
162
|
+
#endif
|
|
163
|
+
{
|
|
164
|
+
calcRectLightValues(light.position, areaData.halfWidth, areaData.halfHeight);
|
|
160
165
|
}
|
|
161
166
|
falloffAttenuation = getFalloffWindow(light.range, lightDirW);
|
|
162
167
|
} else
|
|
@@ -170,21 +175,58 @@ void evaluateLight(
|
|
|
170
175
|
if (falloffAttenuation > 0.00001) {
|
|
171
176
|
#ifdef CLUSTER_AREALIGHTS
|
|
172
177
|
if (light.shape != {LIGHTSHAPE_PUNCTUAL}) {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
178
|
+
bool areaIsRect = light.shape == {LIGHTSHAPE_RECT};
|
|
179
|
+
bool areaIsDisk = light.shape == {LIGHTSHAPE_DISK};
|
|
180
|
+
#ifdef LIT_SPECULAR
|
|
181
|
+
mat3 areaSpecularInvMat = getLTCLightInvMat(dLTCUV);
|
|
182
|
+
#endif
|
|
183
|
+
#ifdef LIT_CLEARCOAT
|
|
184
|
+
mat3 areaClearcoatInvMat = getLTCLightInvMat(ccLTCUV);
|
|
185
|
+
#endif
|
|
186
|
+
for (int lobe = 0; lobe < 4; lobe++) {
|
|
187
|
+
#ifndef LIT_DIFFUSE_TRANSMISSION
|
|
188
|
+
if (lobe == 1) continue;
|
|
177
189
|
#endif
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
#
|
|
181
|
-
|
|
190
|
+
#ifndef LIT_SPECULAR
|
|
191
|
+
if (lobe == 2) break;
|
|
192
|
+
#endif
|
|
193
|
+
#ifndef LIT_CLEARCOAT
|
|
194
|
+
if (lobe == 3) break;
|
|
182
195
|
#endif
|
|
183
|
-
|
|
184
|
-
|
|
196
|
+
vec3 lobeNormal = lobe == 1 ? -worldNormal : worldNormal;
|
|
197
|
+
mat3 lobeInvMat = mat3(1.0);
|
|
198
|
+
#ifdef LIT_SPECULAR
|
|
199
|
+
if (lobe >= 2) lobeInvMat = areaSpecularInvMat;
|
|
200
|
+
#endif
|
|
201
|
+
#ifdef LIT_CLEARCOAT
|
|
202
|
+
if (lobe == 3) {
|
|
203
|
+
lobeNormal = clearcoat_worldNormal;
|
|
204
|
+
lobeInvMat = areaClearcoatInvMat;
|
|
205
|
+
}
|
|
206
|
+
#endif
|
|
207
|
+
float lobeValue = 0.0;
|
|
208
|
+
#ifdef CLUSTER_AREALIGHT_RECT
|
|
209
|
+
if (areaIsRect) {
|
|
210
|
+
lobeValue = LTC_EvaluateRect(lobeNormal, viewDir, vPositionW, lobeInvMat, dLTCCoords);
|
|
211
|
+
} else
|
|
212
|
+
#endif
|
|
213
|
+
#if defined(CLUSTER_AREALIGHT_DISK) || defined(CLUSTER_AREALIGHT_SPHERE)
|
|
214
|
+
if (areaIsDisk || lobe >= 2) {
|
|
215
|
+
lobeValue = LTC_EvaluateDisk(lobeNormal, viewDir, vPositionW, lobeInvMat, dLTCCoords);
|
|
216
|
+
if (lobe < 2) lobeValue = FixNan(lobeValue);
|
|
217
|
+
} else
|
|
218
|
+
#endif
|
|
219
|
+
{
|
|
220
|
+
#ifdef CLUSTER_AREALIGHT_SPHERE
|
|
221
|
+
lobeValue = getSphereLightDiffuse(lobeNormal, viewDir, lightDirW, lightDirNormW);
|
|
222
|
+
#endif
|
|
223
|
+
}
|
|
224
|
+
if (lobe == 0) diffuseAttenuation = lobeValue * 16.0;
|
|
185
225
|
#ifdef LIT_DIFFUSE_TRANSMISSION
|
|
186
|
-
|
|
226
|
+
if (lobe == 1) transmissionAttenuation = lobeValue * 16.0;
|
|
187
227
|
#endif
|
|
228
|
+
if (lobe == 2) areaLightSpecular = lobeValue;
|
|
229
|
+
if (lobe == 3) areaLightSpecularCC = lobeValue;
|
|
188
230
|
}
|
|
189
231
|
#ifdef LIT_DIFFUSE_TRANSMISSION
|
|
190
232
|
transmissionAttenuation *= falloffAttenuation;
|
|
@@ -290,24 +332,8 @@ void evaluateLight(
|
|
|
290
332
|
#endif
|
|
291
333
|
}
|
|
292
334
|
#ifdef LIT_SPECULAR
|
|
293
|
-
float areaLightSpecular;
|
|
294
|
-
if (light.shape == {LIGHTSHAPE_RECT}) {
|
|
295
|
-
areaLightSpecular = getRectLightSpecular(worldNormal, viewDir);
|
|
296
|
-
} else if (light.shape == {LIGHTSHAPE_DISK}) {
|
|
297
|
-
areaLightSpecular = getDiskLightSpecular(worldNormal, viewDir);
|
|
298
|
-
} else {
|
|
299
|
-
areaLightSpecular = getSphereLightSpecular(worldNormal, viewDir);
|
|
300
|
-
}
|
|
301
335
|
dSpecularLight += dLTCSpecFres * areaLightSpecular * falloffAttenuation * light.color * cookieAttenuation;
|
|
302
336
|
#ifdef LIT_CLEARCOAT
|
|
303
|
-
float areaLightSpecularCC;
|
|
304
|
-
if (light.shape == {LIGHTSHAPE_RECT}) {
|
|
305
|
-
areaLightSpecularCC = getRectLightSpecular(clearcoat_worldNormal, viewDir);
|
|
306
|
-
} else if (light.shape == {LIGHTSHAPE_DISK}) {
|
|
307
|
-
areaLightSpecularCC = getDiskLightSpecular(clearcoat_worldNormal, viewDir);
|
|
308
|
-
} else {
|
|
309
|
-
areaLightSpecularCC = getSphereLightSpecular(clearcoat_worldNormal, viewDir);
|
|
310
|
-
}
|
|
311
337
|
ccSpecularLight += ccLTCSpecFres * areaLightSpecularCC * falloffAttenuation * light.color * cookieAttenuation;
|
|
312
338
|
#endif
|
|
313
339
|
#endif
|
package/build/playcanvas/src/scene/shader-lib/glsl/chunks/lit/frag/lighting/lightFunctionLight.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: "\n#if defined(LIGHT{i})\n\nvoid evaluateLight{i}(\n #if defined(LIT_IRIDESCENCE)\n vec3 iridescenceFresnel\n #endif\n) {\n\n // light color\n vec3 lightColor = 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(equal(lightColor, vec3(0.0)))) {\n return;\n }\n #endif\n\n #if LIGHT{i}TYPE == DIRECTIONAL // directional light\n\n dLightDirNormW = light{i}_direction;\n dAtten = 1.0;\n\n #else // omni or spot light\n \n vec3 lightDirW = evalOmniLight(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 vec3 cookieAttenuation = getCookie2DXform(light{i}_cookie, light{i}_shadowMatrix, light{i}_cookieIntensity, light{i}_cookieMatrix, light{i}_cookieOffset).{LIGHT{i}COOKIE_CHANNEL};\n #else\n vec3 cookieAttenuation = getCookie2D(light{i}_cookie, light{i}_shadowMatrix, light{i}_cookieIntensity).{LIGHT{i}COOKIE_CHANNEL};\n #endif\n #else\n #ifdef LIGHT{i}COOKIE_TRANSFORM\n vec3 cookieAttenuation = getCookie2DClipXform(light{i}_cookie, light{i}_shadowMatrix, light{i}_cookieIntensity, light{i}_cookieMatrix, light{i}_cookieOffset).{LIGHT{i}COOKIE_CHANNEL};\n #else\n vec3 cookieAttenuation = getCookie2DClip(light{i}_cookie, light{i}_shadowMatrix, light{i}_cookieIntensity).{LIGHT{i}COOKIE_CHANNEL};\n #endif\n #endif\n #endif\n\n #if LIGHT{i}TYPE == OMNI\n vec3 cookieAttenuation = getCookieCube(light{i}_cookie, light{i}_shadowMatrix, light{i}_cookieIntensity).{LIGHT{i}COOKIE_CHANNEL};\n #endif\n\n // multiply light color by the cookie attenuation\n lightColor *= cookieAttenuation;\n\n #endif\n\n // distance falloff\n #if LIGHT{i}SHAPE == PUNCTUAL\n #if LIGHT{i}FALLOFF == LINEAR\n dAtten = getFalloffLinear(light{i}_radius, lightDirW);\n #else\n dAtten = getFalloffInvSquared(light{i}_radius, lightDirW);\n #endif\n #else\n // non punctual lights only gets the range window here\n dAtten = getFalloffWindow(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 *= getSpotEffect(light{i}_direction, light{i}_innerConeAngle, 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(light{i}_position, light{i}_halfWidth, light{i}_halfHeight);\n #elif LIGHT{i}SHAPE == DISK\n calcDiskLightValues(light{i}_position, light{i}_halfWidth, light{i}_halfHeight);\n #elif LIGHT{i}SHAPE == SPHERE\n calcSphereLightValues(light{i}_position, light{i}_halfWidth, 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 float attenTransmission = dAtten * getLightDiffuse(-litArgs_worldNormal, vec3(0.0), dLightDirNormW);\n #elif LIGHT{i}TYPE == DIRECTIONAL\n float attenTransmission = dAtten * getLightDiffuse(-litArgs_worldNormal, dViewDirW, dLightDirNormW);\n #elif LIGHT{i}SHAPE == RECT\n float attenTransmission = dAtten * getRectLightDiffuse(-litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == DISK\n float attenTransmission = dAtten * getDiskLightDiffuse(-litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == SPHERE\n float attenTransmission = 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 float attenDiffuse = getLightDiffuse(litArgs_worldNormal, dViewDirW, dLightDirNormW);\n #else\n // 16.0 is a constant that is in getFalloffInvSquared()\n #if LIGHT{i}SHAPE == RECT\n float attenDiffuse = getRectLightDiffuse(litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == DISK\n float attenDiffuse = getDiskLightDiffuse(litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == SPHERE\n float attenDiffuse = getSphereLightDiffuse(litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #endif\n #endif\n #else\n // one parameter is unused for punctual lights\n 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 float shadow = getShadow{i}(vec3(0.0));\n #else\n float shadow = 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 float shadowDistance = light{i}_shadowCascadeDistances.w;\n float shadowFade = smoothstep(light{i}_shadowCascadeBlend * shadowDistance, shadowDistance, 1.0 / gl_FragCoord.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, light{i}_shadowIntensity);\n\n dAtten *= shadow;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n attenTransmission *= shadow;\n #endif\n\n #if defined(LIT_SHADOW_CATCHER) && LIGHT{i}TYPE == DIRECTIONAL\n // accumulate shadows for directional lights\n 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 *= 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 += ((attenDiffuse * dAtten) * lightColor) * (1.0 - dLTCSpecFres);\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight += (attenTransmission * lightColor) * (1.0 - dLTCSpecFres);\n #endif\n #else\n dDiffuseLight += (attenDiffuse * dAtten) * lightColor;\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight += attenTransmission * lightColor;\n #endif\n #endif \n #else\n // punctual light\n #if defined(AREA_LIGHTS) && defined(LIT_SPECULAR)\n dDiffuseLight += (dAtten * lightColor) * (1.0 - litArgs_specularity);\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight += (attenTransmission * lightColor) * (1.0 - litArgs_specularity);\n #endif\n #else\n dDiffuseLight += dAtten * lightColor;\n #ifdef LIT_DIFFUSE_TRANSMISSION\n 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 += ccLTCSpecFres * getRectLightSpecular(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor;\n #elif LIGHT{i}SHAPE == DISK\n ccSpecularLight += ccLTCSpecFres * getDiskLightSpecular(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor;\n #elif LIGHT{i}SHAPE == SPHERE\n 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 += dLTCSpecFres * getRectLightSpecular(litArgs_worldNormal, dViewDirW) * dAtten * lightColor;\n #elif LIGHT{i}SHAPE == DISK\n dSpecularLight += dLTCSpecFres * getDiskLightSpecular(litArgs_worldNormal, dViewDirW) * dAtten * lightColor;\n #elif LIGHT{i}SHAPE == SPHERE\n 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 vec3 halfDirW = 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 vec3 lightspecularCC = getLightSpecular(halfDirW, ccReflDirW, litArgs_clearcoat_worldNormal, dViewDirW, dLightDirNormW, litArgs_clearcoat_gloss, dTBN) * dAtten * lightColor;\n #ifdef LIGHT{i}FRESNEL\n lightspecularCC *= getFresnelCC(dot(dViewDirW, halfDirW));\n #endif\n ccSpecularLight += lightspecularCC;\n #endif\n\n #ifdef LIT_SHEEN\n sSpecularLight += getLightSpecularSheen(halfDirW, litArgs_worldNormal, dViewDirW, dLightDirNormW, litArgs_sheen_gloss) * dAtten * lightColor;\n #endif\n\n #ifdef LIT_SPECULAR\n\n vec3 lightSpecular = 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 *= getFresnel(dot(dViewDirW, halfDirW), litArgs_gloss, litArgs_specularity, iridescenceFresnel, litArgs_iridescence_intensity);\n #else\n lightSpecular *= getFresnel(dot(dViewDirW, halfDirW), litArgs_gloss, litArgs_specularity);\n #endif\n\n #else\n lightSpecular *= litArgs_specularity;\n #endif\n \n dSpecularLight += lightSpecular;\n #endif\n #endif\n #endif\n}\n#endif\n";
|
|
1
|
+
declare const _default: "\n#if defined(LIGHT{i})\n\nvoid evaluateLight{i}(\n #if defined(LIT_IRIDESCENCE)\n vec3 iridescenceFresnel\n #endif\n) {\n\n // light color\n vec3 lightColor = 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(equal(lightColor, vec3(0.0)))) {\n return;\n }\n #endif\n\n #if LIGHT{i}TYPE == DIRECTIONAL // directional light\n\n dLightDirNormW = light{i}_direction;\n dAtten = 1.0;\n\n #else // omni or spot light\n \n vec3 lightDirW = evalOmniLight(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 vec3 cookieAttenuation = getCookie2DXform(light{i}_cookie, light{i}_shadowMatrix, light{i}_cookieIntensity, light{i}_cookieMatrix, light{i}_cookieOffset).{LIGHT{i}COOKIE_CHANNEL};\n #else\n vec3 cookieAttenuation = getCookie2D(light{i}_cookie, light{i}_shadowMatrix, light{i}_cookieIntensity).{LIGHT{i}COOKIE_CHANNEL};\n #endif\n #else\n #ifdef LIGHT{i}COOKIE_TRANSFORM\n vec3 cookieAttenuation = getCookie2DClipXform(light{i}_cookie, light{i}_shadowMatrix, light{i}_cookieIntensity, light{i}_cookieMatrix, light{i}_cookieOffset).{LIGHT{i}COOKIE_CHANNEL};\n #else\n vec3 cookieAttenuation = getCookie2DClip(light{i}_cookie, light{i}_shadowMatrix, light{i}_cookieIntensity).{LIGHT{i}COOKIE_CHANNEL};\n #endif\n #endif\n #endif\n\n #if LIGHT{i}TYPE == OMNI\n vec3 cookieAttenuation = getCookieCube(light{i}_cookie, light{i}_shadowMatrix, light{i}_cookieIntensity).{LIGHT{i}COOKIE_CHANNEL};\n #endif\n\n // multiply light color by the cookie attenuation\n lightColor *= cookieAttenuation;\n\n #endif\n\n // distance falloff\n #if LIGHT{i}SHAPE == PUNCTUAL\n #if LIGHT{i}FALLOFF == LINEAR\n dAtten = getFalloffLinear(light{i}_radius, lightDirW);\n #else\n dAtten = getFalloffInvSquared(light{i}_radius, lightDirW);\n #endif\n #else\n // non punctual lights only gets the range window here\n dAtten = getFalloffWindow(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 *= getSpotEffect(light{i}_direction, light{i}_innerConeAngle, 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(light{i}_position, light{i}_halfWidth, light{i}_halfHeight);\n #elif LIGHT{i}SHAPE == DISK\n calcDiskLightValues(light{i}_position, light{i}_halfWidth, light{i}_halfHeight);\n #elif LIGHT{i}SHAPE == SPHERE\n calcSphereLightValues(light{i}_position, light{i}_halfWidth, 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 float attenTransmission = dAtten * getLightDiffuse(-litArgs_worldNormal, vec3(0.0), dLightDirNormW);\n #elif LIGHT{i}TYPE == DIRECTIONAL\n float attenTransmission = dAtten * getLightDiffuse(-litArgs_worldNormal, dViewDirW, dLightDirNormW);\n #elif LIGHT{i}SHAPE == RECT\n float attenTransmission = dAtten * getRectLightDiffuse(-litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == DISK\n float attenTransmission = dAtten * getDiskLightDiffuse(-litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == SPHERE\n float attenTransmission = 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 float attenDiffuse = getLightDiffuse(litArgs_worldNormal, dViewDirW, dLightDirNormW);\n #else\n // 16.0 is a constant that is in getFalloffInvSquared()\n #if LIGHT{i}SHAPE == RECT\n float attenDiffuse = getRectLightDiffuse(litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == DISK\n float attenDiffuse = getDiskLightDiffuse(litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #elif LIGHT{i}SHAPE == SPHERE\n float attenDiffuse = getSphereLightDiffuse(litArgs_worldNormal, dViewDirW, lightDirW, dLightDirNormW) * 16.0;\n #endif\n #endif\n #else\n // one parameter is unused for punctual lights\n 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 float shadow = getShadow{i}(vec3(0.0));\n #else\n float shadow = 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 float shadowDistance = light{i}_shadowCascadeDistances.w;\n float shadowFade = smoothstep(light{i}_shadowCascadeBlend * shadowDistance, shadowDistance, 1.0 / gl_FragCoord.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, light{i}_shadowIntensity);\n\n dAtten *= shadow;\n\n #ifdef LIT_DIFFUSE_TRANSMISSION\n attenTransmission *= shadow;\n #endif\n\n #if defined(LIT_SHADOW_CATCHER) && LIGHT{i}TYPE == DIRECTIONAL\n // accumulate shadows for directional lights\n 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 *= 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 += ((attenDiffuse * dAtten) * lightColor) * (1.0 - dLTCSpecFres);\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight += (attenTransmission * lightColor) * (1.0 - dLTCSpecFres);\n #endif\n #else\n dDiffuseLight += (attenDiffuse * dAtten) * lightColor;\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight += attenTransmission * lightColor;\n #endif\n #endif \n #else\n // punctual light\n #if defined(AREA_LIGHTS) && defined(LIT_SPECULAR)\n dDiffuseLight += (dAtten * lightColor) * (1.0 - litArgs_specularity);\n #ifdef LIT_DIFFUSE_TRANSMISSION\n dDiffuseTransmissionLight += (attenTransmission * lightColor) * (1.0 - litArgs_specularity);\n #endif\n #else\n dDiffuseLight += dAtten * lightColor;\n #ifdef LIT_DIFFUSE_TRANSMISSION\n 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 += ccLTCSpecFres * getRectLightSpecularCC(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor;\n #elif LIGHT{i}SHAPE == DISK\n ccSpecularLight += ccLTCSpecFres * getDiskLightSpecularCC(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor;\n #elif LIGHT{i}SHAPE == SPHERE\n 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 += dLTCSpecFres * getRectLightSpecular(litArgs_worldNormal, dViewDirW) * dAtten * lightColor;\n #elif LIGHT{i}SHAPE == DISK\n dSpecularLight += dLTCSpecFres * getDiskLightSpecular(litArgs_worldNormal, dViewDirW) * dAtten * lightColor;\n #elif LIGHT{i}SHAPE == SPHERE\n 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 vec3 halfDirW = 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 vec3 lightspecularCC = getLightSpecular(halfDirW, ccReflDirW, litArgs_clearcoat_worldNormal, dViewDirW, dLightDirNormW, litArgs_clearcoat_gloss, dTBN) * dAtten * lightColor;\n #ifdef LIGHT{i}FRESNEL\n lightspecularCC *= getFresnelCC(dot(dViewDirW, halfDirW));\n #endif\n ccSpecularLight += lightspecularCC;\n #endif\n\n #ifdef LIT_SHEEN\n sSpecularLight += getLightSpecularSheen(halfDirW, litArgs_worldNormal, dViewDirW, dLightDirNormW, litArgs_sheen_gloss) * dAtten * lightColor;\n #endif\n\n #ifdef LIT_SPECULAR\n\n vec3 lightSpecular = 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 *= getFresnel(dot(dViewDirW, halfDirW), litArgs_gloss, litArgs_specularity, iridescenceFresnel, litArgs_iridescence_intensity);\n #else\n lightSpecular *= getFresnel(dot(dViewDirW, halfDirW), litArgs_gloss, litArgs_specularity);\n #endif\n\n #else\n lightSpecular *= litArgs_specularity;\n #endif\n \n dSpecularLight += lightSpecular;\n #endif\n #endif\n #endif\n}\n#endif\n";
|
|
2
2
|
export default _default;
|
package/build/playcanvas/src/scene/shader-lib/glsl/chunks/lit/frag/lighting/lightFunctionLight.js
CHANGED
|
@@ -146,11 +146,11 @@ void evaluateLight{i}(
|
|
|
146
146
|
#if LIGHT{i}SHAPE != PUNCTUAL
|
|
147
147
|
#ifdef LIT_CLEARCOAT
|
|
148
148
|
#if LIGHT{i}SHAPE == RECT
|
|
149
|
-
ccSpecularLight += ccLTCSpecFres *
|
|
149
|
+
ccSpecularLight += ccLTCSpecFres * getRectLightSpecularCC(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor;
|
|
150
150
|
#elif LIGHT{i}SHAPE == DISK
|
|
151
|
-
ccSpecularLight += ccLTCSpecFres *
|
|
151
|
+
ccSpecularLight += ccLTCSpecFres * getDiskLightSpecularCC(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor;
|
|
152
152
|
#elif LIGHT{i}SHAPE == SPHERE
|
|
153
|
-
ccSpecularLight += ccLTCSpecFres *
|
|
153
|
+
ccSpecularLight += ccLTCSpecFres * getSphereLightSpecularCC(litArgs_clearcoat_worldNormal, dViewDirW) * dAtten * lightColor;
|
|
154
154
|
#endif
|
|
155
155
|
#endif
|
|
156
156
|
#ifdef LIT_SPECULAR
|
|
@@ -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);
|