Variable: shadow
constshadow:object
Defined in: modules/lighting/shadow/shadow.ts:389
Backend-aware shadow sampling module. Applications explicitly apply factors to direct-light terms.
Type Declaration
bindingLayout
readonlybindingLayout: readonly [{group:2;name:"shadow"; }, {group:2;name:"directionalShadowTexture"; }, {group:2;name:"spotShadowTexture"; }, {group:2;name:"pointShadowTexture"; }, {group:2;name:"shadowComparisonSampler"; }]
bindings
readonlybindings:ShadowBindings<unknown,unknown>
fs
readonlyfs: "\nprecision highp sampler2DArray;\nstruct DirectionalShadowLightUniform {\n vec3 direction;\n float strength;\n float normalBias;\n float sourceAngularRadius;\n float cascadeBlendFraction;\n float farFadeFraction;\n float shadowDistance;\n};\nstruct SpotShadowLightUniform {\n vec3 position;\n float range;\n vec3 direction;\n float outerConeCos;\n float sourceRadius;\n float normalBias;\n float strength;\n float nearPlane;\n};\nstruct PointShadowLightUniform {\n vec3 position;\n float range;\n float sourceRadius;\n float normalBias;\n float strength;\n float nearPlane;\n};\nlayout(std140) uniform shadowUniforms {\n int directionalLightCount;\n int spotLightCount;\n int pointLightCount;\n int cascadeCount;\n int blockerSampleCount;\n int filterSampleCount;\n vec4 directionalCascadeSplits[4];\n mat4 directionalViewProjectionMatrices[16];\n mat4 spotViewProjectionMatrices[4];\n mat4 pointViewProjectionMatrices[24];\n DirectionalShadowLightUniform directionalLights[4];\n SpotShadowLightUniform spotLights[4];\n PointShadowLightUniform pointLights[4];\n} shadow;\nuniform sampler2DArray directionalShadowTexture;\nuniform sampler2DArray spotShadowTexture;\nuniform sampler2DArray pointShadowTexture;\nconst float SHADOW_PI = 3.141592653589793;\nfloat shadow_hash(vec3 position) {\n return fract(sin(dot(position, vec3(12.9898, 78.233, 37.719))) * 43758.5453);\n}\nvec2 shadow_diskSample(int index, int count, float rotation) {\n float fraction = (float(index) + 0.5) / max(float(count), 1.0);\n float angle = float(index) * 2.39996323 + rotation;\n return sqrt(fraction) * vec2(cos(angle), sin(angle));\n}\nvec3 shadow_project(mat4 matrix, vec3 worldPosition) {\n vec4 clip = matrix * vec4(worldPosition, 1.0);\n vec3 projected = clip.xyz / max(abs(clip.w), 0.00001);\n // Shared matrices retain [0,1] depth; WebGL texture rows start at the bottom.\n return vec3(projected.xy * 0.5 + 0.5, projected.z);\n}\nbool shadow_validProjection(vec3 projected) {\n return all(greaterThanEqual(projected, vec3(0.0))) && all(lessThanEqual(projected, vec3(1.0)));\n}\n// Bilinear interpolation of four depth comparisons matches a hardware 2x2 PCF lookup.\nfloat shadow_compare(sampler2DArray shadowMap, vec2 coordinate, int layer, float reference) {\n ivec2 dimensions = textureSize(shadowMap, 0).xy;\n vec2 position = coordinate * vec2(dimensions) - 0.5;\n ivec2 base = ivec2(floor(position));\n vec2 fraction = fract(position);\n ivec2 maximum = dimensions - 1;\n float lowerLeft = step(reference, texelFetch(shadowMap, ivec3(clamp(base, ivec2(0), maximum), layer), 0).r);\n float lowerRight = step(reference, texelFetch(shadowMap, ivec3(clamp(base + ivec2(1,0), ivec2(0), maximum), layer), 0).r);\n float upperLeft = step(reference, texelFetch(shadowMap, ivec3(clamp(base + ivec2(0,1), ivec2(0), maximum), layer), 0).r);\n float upperRight = step(reference, texelFetch(shadowMap, ivec3(clamp(base + ivec2(1,1), ivec2(0), maximum), layer), 0).r);\n return mix(mix(lowerLeft, lowerRight, fraction.x), mix(upperLeft, upperRight, fraction.x), fraction.y);\n}\nfloat shadow_arrayPCSS(sampler2DArray shadowMap, int layer, vec3 projected, float searchRadius, float rotation, float penumbraScale) {\n if (!shadow_validProjection(projected)) return 1.0;\n float texel = 1.0 / float(textureSize(shadowMap, 0).x);\n searchRadius = max(texel, searchRadius);\n float blockerDepth = 0.0;\n float blockerCount = 0.0;\n for (int index = 0; index < 24; index++) {\n if (index >= shadow.blockerSampleCount) break;\n vec2 coordinate = clamp(projected.xy + shadow_diskSample(index, shadow.blockerSampleCount, rotation) * searchRadius, vec2(0.0), vec2(1.0));\n float depth = textureLod(shadowMap, vec3(coordinate, float(layer)), 0.0).r;\n if (depth < projected.z) { blockerDepth += depth; blockerCount += 1.0; }\n }\n if (blockerCount == 0.0) return 1.0;\n float averageBlocker = blockerDepth / blockerCount;\n float penumbra = clamp((projected.z - averageBlocker) / max(abs(averageBlocker), 0.001), 0.0, 1.0);\n float radius = max(texel, searchRadius * (1.0 + penumbra * penumbraScale));\n float visibility = 0.0;\n for (int index = 0; index < 48; index++) {\n if (index >= shadow.filterSampleCount) break;\n vec2 coordinate = clamp(projected.xy + shadow_diskSample(index, shadow.filterSampleCount, rotation) * radius, vec2(0.0), vec2(1.0));\n visibility += shadow_compare(shadowMap, coordinate, layer, projected.z);\n }\n return visibility / max(float(shadow.filterSampleCount), 1.0);\n}\nint shadow_directionalCascadeIndex(int lightIndex, float viewDepth) {\n int cascadeIndex = 0;\n for (int index = 0; index < 4; index++) {\n if (index >= shadow.cascadeCount) break;\n cascadeIndex = index;\n if (viewDepth <= shadow.directionalCascadeSplits[lightIndex][index]) break;\n }\n return cascadeIndex;\n}\nint shadow_getDirectionalCascadeIndex(float viewDepth) {\n return shadow.directionalLightCount == 0 ? -1 : shadow_directionalCascadeIndex(0, viewDepth);\n}\nfloat shadow_directionalPCSS(int lightIndex, int cascadeIndex, vec3 worldPosition, float viewDepth) {\n DirectionalShadowLightUniform light = shadow.directionalLights[lightIndex];\n int layerIndex = lightIndex * shadow.cascadeCount + cascadeIndex;\n return shadow_arrayPCSS(directionalShadowTexture, layerIndex,\n shadow_project(shadow.directionalViewProjectionMatrices[layerIndex], worldPosition),\n light.sourceAngularRadius * viewDepth / max(light.shadowDistance, 0.001),\n shadow_hash(worldPosition) * 2.0 * SHADOW_PI, 18.0);\n}\nfloat shadow_getDirectionalLightFactor(int lightIndex, vec3 worldPosition, vec3 worldNormal, float viewDepth) {\n if (lightIndex < 0 || lightIndex >= shadow.directionalLightCount || viewDepth <= 0.0) return 1.0;\n DirectionalShadowLightUniform light = shadow.directionalLights[lightIndex];\n if (viewDepth >= light.shadowDistance) return 1.0;\n vec3 position = worldPosition + normalize(worldNormal) * light.normalBias;\n int cascadeIndex = shadow_directionalCascadeIndex(lightIndex, viewDepth);\n float visibility = shadow_directionalPCSS(lightIndex, cascadeIndex, position, viewDepth);\n if (cascadeIndex + 1 < shadow.cascadeCount) {\n float cascadeStart = cascadeIndex > 0 ? shadow.directionalCascadeSplits[lightIndex][cascadeIndex - 1] : 0.0;\n float cascadeEnd = shadow.directionalCascadeSplits[lightIndex][cascadeIndex];\n float blendWidth = max((cascadeEnd - cascadeStart) * light.cascadeBlendFraction, 0.0001);\n float blend = smoothstep(cascadeEnd - blendWidth, cascadeEnd, viewDepth);\n if (blend > 0.0) visibility = mix(visibility, shadow_directionalPCSS(lightIndex, cascadeIndex + 1, position, viewDepth), blend);\n }\n float fadeWidth = max(light.shadowDistance * light.farFadeFraction, 0.0001);\n visibility = mix(visibility, 1.0, smoothstep(light.shadowDistance - fadeWidth, light.shadowDistance, viewDepth));\n return mix(1.0, visibility, light.strength);\n}\n// Preserve the first-light helper used by existing shader consumers.\nfloat shadow_getDirectionalFactor(vec3 worldPosition, vec3 worldNormal, float viewDepth) {\n return shadow_getDirectionalLightFactor(0, worldPosition, worldNormal, viewDepth);\n}\nfloat shadow_getSpotFactor(int lightIndex, vec3 worldPosition, vec3 worldNormal) {\n if (lightIndex < 0 || lightIndex >= shadow.spotLightCount) return 1.0;\n SpotShadowLightUniform light = shadow.spotLights[lightIndex];\n vec3 fromLight = worldPosition - light.position;\n float distanceToLight = length(fromLight);\n if (distanceToLight >= light.range || dot(normalize(fromLight), normalize(light.direction)) < light.outerConeCos) return 1.0;\n vec3 position = worldPosition + normalize(worldNormal) * light.normalBias;\n float visibility = shadow_arrayPCSS(spotShadowTexture, lightIndex,\n shadow_project(shadow.spotViewProjectionMatrices[lightIndex], position),\n light.sourceRadius / max(distanceToLight, 0.001) * 0.5,\n shadow_hash(worldPosition) * 2.0 * SHADOW_PI, 16.0);\n return mix(1.0, visibility, light.strength);\n}\nfloat shadow_pointReferenceDepth(float majorDistance, float nearPlane, float farPlane) {\n return farPlane / (farPlane - nearPlane) - (farPlane * nearPlane) / ((farPlane - nearPlane) * majorDistance);\n}\nvec3 shadow_pointCoordinate(int lightIndex, vec3 direction) {\n vec3 magnitude = abs(direction);\n int face = magnitude.x >= magnitude.y && magnitude.x >= magnitude.z ? (direction.x >= 0.0 ? 0 : 1)\n : magnitude.y >= magnitude.z ? (direction.y >= 0.0 ? 2 : 3) : (direction.z >= 0.0 ? 4 : 5);\n PointShadowLightUniform light = shadow.pointLights[lightIndex];\n vec3 projected = shadow_project(shadow.pointViewProjectionMatrices[lightIndex * 6 + face], light.position + direction * light.range * 0.5);\n return vec3(projected.xy, float(lightIndex * 6 + face));\n}\nfloat shadow_getPointFactor(int lightIndex, vec3 worldPosition, vec3 worldNormal) {\n if (lightIndex < 0 || lightIndex >= shadow.pointLightCount) return 1.0;\n PointShadowLightUniform light = shadow.pointLights[lightIndex];\n vec3 fromLight = worldPosition + normalize(worldNormal) * light.normalBias - light.position;\n float distanceToLight = length(fromLight);\n if (distanceToLight >= light.range || distanceToLight <= light.nearPlane) return 1.0;\n vec3 direction = normalize(fromLight);\n float referenceDepth = shadow_pointReferenceDepth(max(max(abs(fromLight.x), abs(fromLight.y)), abs(fromLight.z)), light.nearPlane, light.range);\n vec3 reference = abs(direction.y) > 0.99 ? vec3(0,0,1) : vec3(0,1,0);\n vec3 tangent = normalize(cross(direction, reference));\n vec3 bitangent = normalize(cross(direction, tangent));\n float searchRadius = light.sourceRadius / max(distanceToLight, 0.001);\n float rotation = shadow_hash(worldPosition) * 2.0 * SHADOW_PI;\n float blockerDepth = 0.0;\n float blockerCount = 0.0;\n for (int index = 0; index < 24; index++) {\n if (index >= shadow.blockerSampleCount) break;\n vec2 disk = shadow_diskSample(index, shadow.blockerSampleCount, rotation) * searchRadius;\n vec3 coordinate = shadow_pointCoordinate(lightIndex, normalize(direction + tangent * disk.x + bitangent * disk.y));\n float depth = textureLod(pointShadowTexture, coordinate, 0.0).r;\n if (depth < referenceDepth) { blockerDepth += depth; blockerCount += 1.0; }\n }\n if (blockerCount == 0.0) return 1.0;\n float averageBlocker = blockerDepth / blockerCount;\n float penumbra = clamp((referenceDepth - averageBlocker) / max(abs(averageBlocker), 0.001), 0.0, 1.0);\n float radius = searchRadius * (1.0 + penumbra * 12.0);\n float visibility = 0.0;\n for (int index = 0; index < 48; index++) {\n if (index >= shadow.filterSampleCount) break;\n vec2 disk = shadow_diskSample(index, shadow.filterSampleCount, rotation) * radius;\n vec3 coordinate = shadow_pointCoordinate(lightIndex, normalize(direction + tangent * disk.x + bitangent * disk.y));\n visibility += shadow_compare(pointShadowTexture, coordinate.xy, int(coordinate.z), referenceDepth);\n }\n return mix(1.0, visibility / max(float(shadow.filterSampleCount), 1.0), light.strength);\n}\n" =SHADOW_GLSL
name
readonlyname:"shadow"='shadow'
props
readonlyprops:ShadowProps
source
readonlysource: "const SHADOW_PI: f32 = 3.141592653589793;\n\nstruct DirectionalShadowLightUniform {\n direction: vec3f,\n strength: f32,\n normalBias: f32,\n sourceAngularRadius: f32,\n cascadeBlendFraction: f32,\n farFadeFraction: f32,\n shadowDistance: f32,\n};\n\nstruct SpotShadowLightUniform {\n position: vec3f,\n range: f32,\n direction: vec3f,\n outerConeCos: f32,\n sourceRadius: f32,\n normalBias: f32,\n strength: f32,\n nearPlane: f32,\n};\n\nstruct PointShadowLightUniform {\n position: vec3f,\n range: f32,\n sourceRadius: f32,\n normalBias: f32,\n strength: f32,\n nearPlane: f32,\n};\n\nstruct ShadowUniforms {\n directionalLightCount: i32,\n spotLightCount: i32,\n pointLightCount: i32,\n cascadeCount: i32,\n blockerSampleCount: i32,\n filterSampleCount: i32,\n directionalCascadeSplits: array<vec4f, 4>,\n directionalViewProjectionMatrices: array<mat4x4f, 16>,\n spotViewProjectionMatrices: array<mat4x4f, 4>,\n pointViewProjectionMatrices: array<mat4x4f, 24>,\n directionalLights: array<DirectionalShadowLightUniform, 4>,\n spotLights: array<SpotShadowLightUniform, 4>,\n pointLights: array<PointShadowLightUniform, 4>,\n};\n\n@group(2) @binding(auto) var<uniform> shadow: ShadowUniforms;\n@group(2) @binding(auto) var directionalShadowTexture: texture_depth_2d_array;\n@group(2) @binding(auto) var spotShadowTexture: texture_depth_2d_array;\n@group(2) @binding(auto) var pointShadowTexture: texture_depth_cube_array;\n@group(2) @binding(auto) var directionalShadowTextureSampler: sampler;\n@group(2) @binding(auto) var shadowComparisonSampler: sampler_comparison;\n\nfn shadow_hash(position: vec3f) -> f32 {\n return fract(sin(dot(position, vec3f(12.9898, 78.233, 37.719))) * 43758.5453);\n}\n\nfn shadow_diskSample(index: i32, count: i32, rotation: f32) -> vec2f {\n let fraction = (f32(index) + 0.5) / max(f32(count), 1.0);\n let angle = f32(index) * 2.39996323 + rotation;\n return sqrt(fraction) * vec2f(cos(angle), sin(angle));\n}\n\nfn shadow_project(matrix: mat4x4f, worldPosition: vec3f) -> vec3f {\n let clip = matrix * vec4f(worldPosition, 1.0);\n let ndc = clip.xyz / max(abs(clip.w), 0.00001);\n return vec3f(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5, ndc.z);\n}\n\nfn shadow_validProjection(projected: vec3f) -> bool {\n return all(projected.xy >= vec2f(0.0)) && all(projected.xy <= vec2f(1.0)) &&\n projected.z >= 0.0 && projected.z <= 1.0;\n}\n\nfn shadow_directionalCascadeIndex(lightIndex: i32, viewDepth: f32) -> i32 {\n var cascadeIndex = 0;\n for (var index: i32 = 0; index < 4; index++) {\n if (index >= shadow.cascadeCount) { break; }\n cascadeIndex = index;\n if (viewDepth <= shadow.directionalCascadeSplits[lightIndex][index]) { break; }\n }\n return cascadeIndex;\n}\n\nfn shadow_getDirectionalCascadeIndex(viewDepth: f32) -> i32 {\n if (shadow.directionalLightCount == 0) { return -1; }\n return shadow_directionalCascadeIndex(0, viewDepth);\n}\n\nfn shadow_directionalPCSS(lightIndex: i32, cascadeIndex: i32, worldPosition: vec3f, viewDepth: f32) -> f32 {\n let layerIndex = lightIndex * shadow.cascadeCount + cascadeIndex;\n let projected = shadow_project(shadow.directionalViewProjectionMatrices[layerIndex], worldPosition);\n if (!shadow_validProjection(projected)) { return 1.0; }\n let dimensions = vec2f(textureDimensions(directionalShadowTexture));\n let texel = 1.0 / dimensions;\n let light = shadow.directionalLights[lightIndex];\n let rotation = shadow_hash(worldPosition) * 2.0 * SHADOW_PI;\n let searchRadius = max(texel.x, light.sourceAngularRadius * viewDepth / max(light.shadowDistance, 0.001));\n var blockerDepth = 0.0;\n var blockerCount = 0.0;\n for (var index: i32 = 0; index < 24; index++) {\n if (index >= shadow.blockerSampleCount) { break; }\n let offset = shadow_diskSample(index, shadow.blockerSampleCount, rotation) * searchRadius;\n let sampleDepth = textureSampleLevel(\n directionalShadowTexture,\n directionalShadowTextureSampler,\n clamp(projected.xy + offset, vec2f(0.0), vec2f(1.0)),\n layerIndex,\n 0\n );\n if (sampleDepth < projected.z) {\n blockerDepth += sampleDepth;\n blockerCount += 1.0;\n }\n }\n if (blockerCount == 0.0) { return 1.0; }\n let averageBlocker = blockerDepth / blockerCount;\n let penumbra = clamp((projected.z - averageBlocker) / max(abs(averageBlocker), 0.001), 0.0, 1.0);\n let filterRadius = max(texel.x, searchRadius * (1.0 + penumbra * 18.0));\n var visibility = 0.0;\n for (var index: i32 = 0; index < 48; index++) {\n if (index >= shadow.filterSampleCount) { break; }\n let offset = shadow_diskSample(index, shadow.filterSampleCount, rotation) * filterRadius;\n visibility += textureSampleCompareLevel(\n directionalShadowTexture,\n shadowComparisonSampler,\n clamp(projected.xy + offset, vec2f(0.0), vec2f(1.0)),\n layerIndex,\n projected.z\n );\n }\n return visibility / max(f32(shadow.filterSampleCount), 1.0);\n}\n\nfn shadow_getDirectionalLightFactor(lightIndex: i32, worldPosition: vec3f, worldNormal: vec3f, viewDepth: f32) -> f32 {\n if (lightIndex < 0 || lightIndex >= shadow.directionalLightCount || viewDepth <= 0.0) { return 1.0; }\n let light = shadow.directionalLights[lightIndex];\n if (viewDepth >= light.shadowDistance) { return 1.0; }\n let biasedPosition = worldPosition + normalize(worldNormal) * light.normalBias;\n let cascadeIndex = shadow_directionalCascadeIndex(lightIndex, viewDepth);\n var visibility = shadow_directionalPCSS(lightIndex, cascadeIndex, biasedPosition, viewDepth);\n if (cascadeIndex + 1 < shadow.cascadeCount) {\n var cascadeStart = 0.0;\n if (cascadeIndex > 0) { cascadeStart = shadow.directionalCascadeSplits[lightIndex][cascadeIndex - 1]; }\n let cascadeEnd = shadow.directionalCascadeSplits[lightIndex][cascadeIndex];\n let blendWidth = max((cascadeEnd - cascadeStart) * light.cascadeBlendFraction, 0.0001);\n let blend = smoothstep(cascadeEnd - blendWidth, cascadeEnd, viewDepth);\n if (blend > 0.0) {\n visibility = mix(\n visibility,\n shadow_directionalPCSS(lightIndex, cascadeIndex + 1, biasedPosition, viewDepth),\n blend\n );\n }\n }\n let fadeWidth = max(light.shadowDistance * light.farFadeFraction, 0.0001);\n let farFade = smoothstep(light.shadowDistance - fadeWidth, light.shadowDistance, viewDepth);\n visibility = mix(visibility, 1.0, farFade);\n return mix(1.0, visibility, light.strength);\n}\n\n// Preserve the first-light helper used by existing shader consumers.\nfn shadow_getDirectionalFactor(worldPosition: vec3f, worldNormal: vec3f, viewDepth: f32) -> f32 {\n return shadow_getDirectionalLightFactor(0, worldPosition, worldNormal, viewDepth);\n}\n\nfn shadow_spotPCSS(lightIndex: i32, worldPosition: vec3f) -> f32 {\n let projected = shadow_project(shadow.spotViewProjectionMatrices[lightIndex], worldPosition);\n if (!shadow_validProjection(projected)) { return 1.0; }\n let light = shadow.spotLights[lightIndex];\n let dimensions = vec2f(textureDimensions(spotShadowTexture));\n let texel = 1.0 / dimensions;\n let distanceToLight = length(worldPosition - light.position);\n let searchRadius = max(texel.x, light.sourceRadius / max(distanceToLight, 0.001) * 0.5);\n let rotation = shadow_hash(worldPosition) * 2.0 * SHADOW_PI;\n var blockerDepth = 0.0;\n var blockerCount = 0.0;\n for (var index: i32 = 0; index < 24; index++) {\n if (index >= shadow.blockerSampleCount) { break; }\n let offset = shadow_diskSample(index, shadow.blockerSampleCount, rotation) * searchRadius;\n let sampleDepth = textureSampleLevel(\n spotShadowTexture,\n directionalShadowTextureSampler,\n clamp(projected.xy + offset, vec2f(0.0), vec2f(1.0)),\n lightIndex,\n 0\n );\n if (sampleDepth < projected.z) { blockerDepth += sampleDepth; blockerCount += 1.0; }\n }\n if (blockerCount == 0.0) { return 1.0; }\n let averageBlocker = blockerDepth / blockerCount;\n let penumbra = clamp((projected.z - averageBlocker) / max(abs(averageBlocker), 0.001), 0.0, 1.0);\n let filterRadius = max(texel.x, searchRadius * (1.0 + penumbra * 16.0));\n var visibility = 0.0;\n for (var index: i32 = 0; index < 48; index++) {\n if (index >= shadow.filterSampleCount) { break; }\n let offset = shadow_diskSample(index, shadow.filterSampleCount, rotation) * filterRadius;\n visibility += textureSampleCompareLevel(\n spotShadowTexture,\n shadowComparisonSampler,\n clamp(projected.xy + offset, vec2f(0.0), vec2f(1.0)),\n lightIndex,\n projected.z\n );\n }\n return visibility / max(f32(shadow.filterSampleCount), 1.0);\n}\n\nfn shadow_getSpotFactor(lightIndex: i32, worldPosition: vec3f, worldNormal: vec3f) -> f32 {\n if (lightIndex < 0 || lightIndex >= shadow.spotLightCount) { return 1.0; }\n let light = shadow.spotLights[lightIndex];\n let fromLight = worldPosition - light.position;\n let distanceToLight = length(fromLight);\n let coneCos = dot(normalize(fromLight), normalize(light.direction));\n if (distanceToLight >= light.range || coneCos < light.outerConeCos) { return 1.0; }\n let biasedPosition = worldPosition + normalize(worldNormal) * light.normalBias;\n return mix(1.0, shadow_spotPCSS(lightIndex, biasedPosition), light.strength);\n}\n\nfn shadow_pointReferenceDepth(majorDistance: f32, nearPlane: f32, farPlane: f32) -> f32 {\n // Match the cube-face projection and stored WebGPU depth in [0, 1].\n return farPlane / (farPlane - nearPlane) -\n (farPlane * nearPlane) / ((farPlane - nearPlane) * majorDistance);\n}\n\nfn shadow_pointBasis(direction: vec3f) -> mat2x3f {\n let reference = select(vec3f(0.0, 1.0, 0.0), vec3f(0.0, 0.0, 1.0), abs(direction.y) > 0.99);\n let tangent = normalize(cross(direction, reference));\n return mat2x3f(tangent, normalize(cross(direction, tangent)));\n}\n\nfn shadow_getPointFactor(lightIndex: i32, worldPosition: vec3f, worldNormal: vec3f) -> f32 {\n if (lightIndex < 0 || lightIndex >= shadow.pointLightCount) { return 1.0; }\n let light = shadow.pointLights[lightIndex];\n let biasedPosition = worldPosition + normalize(worldNormal) * light.normalBias;\n let fromLight = biasedPosition - light.position;\n let distanceToLight = length(fromLight);\n if (distanceToLight >= light.range || distanceToLight <= light.nearPlane) { return 1.0; }\n let direction = normalize(fromLight);\n let majorDistance = max(max(abs(fromLight.x), abs(fromLight.y)), abs(fromLight.z));\n let referenceDepth = shadow_pointReferenceDepth(majorDistance, light.nearPlane, light.range);\n let searchRadius = light.sourceRadius / max(distanceToLight, 0.001);\n let rotation = shadow_hash(worldPosition) * 2.0 * SHADOW_PI;\n let basis = shadow_pointBasis(direction);\n var blockerDepth = 0.0;\n var blockerCount = 0.0;\n for (var index: i32 = 0; index < 24; index++) {\n if (index >= shadow.blockerSampleCount) { break; }\n let disk = shadow_diskSample(index, shadow.blockerSampleCount, rotation) * searchRadius;\n let sampleDirection = normalize(direction + basis[0] * disk.x + basis[1] * disk.y);\n let sampleDepth = textureSampleLevel(\n pointShadowTexture,\n directionalShadowTextureSampler,\n sampleDirection,\n lightIndex,\n 0\n );\n if (sampleDepth < referenceDepth) { blockerDepth += sampleDepth; blockerCount += 1.0; }\n }\n if (blockerCount == 0.0) { return 1.0; }\n let averageBlocker = blockerDepth / blockerCount;\n let penumbra = clamp((referenceDepth - averageBlocker) / max(abs(averageBlocker), 0.001), 0.0, 1.0);\n let filterRadius = searchRadius * (1.0 + penumbra * 12.0);\n var visibility = 0.0;\n for (var index: i32 = 0; index < 48; index++) {\n if (index >= shadow.filterSampleCount) { break; }\n let disk = shadow_diskSample(index, shadow.filterSampleCount, rotation) * filterRadius;\n let sampleDirection = normalize(direction + basis[0] * disk.x + basis[1] * disk.y);\n visibility += textureSampleCompareLevel(\n pointShadowTexture,\n shadowComparisonSampler,\n sampleDirection,\n lightIndex,\n referenceDepth\n );\n }\n visibility /= max(f32(shadow.filterSampleCount), 1.0);\n return mix(1.0, visibility, light.strength);\n}\n" =SHADOW_WGSL
uniforms
readonlyuniforms:ShadowUniforms
uniformTypes
readonlyuniformTypes:object
uniformTypes.blockerSampleCount
readonlyblockerSampleCount:"i32"='i32'
uniformTypes.cascadeCount
readonlycascadeCount:"i32"='i32'
uniformTypes.directionalCascadeSplits
readonlydirectionalCascadeSplits: readonly ["vec4<f32>",4]
uniformTypes.directionalLightCount
readonlydirectionalLightCount:"i32"='i32'
uniformTypes.directionalLights
readonlydirectionalLights: readonly [{cascadeBlendFraction:"f32";direction:"vec3<f32>";farFadeFraction:"f32";normalBias:"f32";shadowDistance:"f32";sourceAngularRadius:"f32";strength:"f32"; },4]
uniformTypes.directionalViewProjectionMatrices
readonlydirectionalViewProjectionMatrices: readonly ["mat4x4<f32>",number]
uniformTypes.filterSampleCount
readonlyfilterSampleCount:"i32"='i32'
uniformTypes.pointLightCount
readonlypointLightCount:"i32"='i32'
uniformTypes.pointLights
readonlypointLights: readonly [{nearPlane:"f32";normalBias:"f32";position:"vec3<f32>";range:"f32";sourceRadius:"f32";strength:"f32"; },4]
uniformTypes.pointViewProjectionMatrices
readonlypointViewProjectionMatrices: readonly ["mat4x4<f32>",24]
uniformTypes.spotLightCount
readonlyspotLightCount:"i32"='i32'
uniformTypes.spotLights
readonlyspotLights: readonly [{direction:"vec3<f32>";nearPlane:"f32";normalBias:"f32";outerConeCos:"f32";position:"vec3<f32>";range:"f32";sourceRadius:"f32";strength:"f32"; },4]
uniformTypes.spotViewProjectionMatrices
readonlyspotViewProjectionMatrices: readonly ["mat4x4<f32>",4]
getUniforms()
readonlygetUniforms(props):Partial<ShadowUniforms&ShadowBindings>
Parameters
props
Partial<ShadowProps>
Returns
Partial<ShadowUniforms & ShadowBindings>