// luma.gl // SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors import type {Buffer, Texture} from '@luma.gl/core'; import type {ShaderPass, ShaderPassPipeline} from '@luma.gl/shadertools'; import type {NumberArray3, NumberArray16} from '@math.gl/core'; import {depthAwareBlur} from './depth-aware-blur'; /** Construction options for clustered participating-media integration. */ export type ClusteredVolumetricLightingShaderPassPipelineOptions = { /** Fractional integration, history, and denoising resolution. Defaults to full resolution. */ resolutionScale?: number; }; type ClusteredVolumetricTraceUniforms = { projectionMatrix: Readonly; inverseProjectionMatrix: Readonly; inverseViewMatrix: Readonly; directionalLightDirectionView: Readonly; directionalLightColor: Readonly; fogColor: Readonly; godRayPosition: readonly [number, number]; density: number; heightFalloff: number; fogHeight: number; anisotropy: number; directionalIntensity: number; pointLightIntensity: number; godRayIntensity: number; godRayDensity: number; godRayDecay: number; godRaySampleCount: number; godRaysOnly: number; maxDistance: number; sampleCount: number; shadowStrength: number; clusterCountX: number; clusterCountY: number; clusterCountZ: number; maxLightsPerCluster: number; pointLightCount: number; clusterNearPlane: number; clusterFarPlane: number; }; type ClusteredVolumetricTemporalUniforms = { inverseProjectionMatrix: Readonly; inverseViewProjectionMatrix: Readonly; previousViewProjectionMatrix: Readonly; historyWeight: number; depthThreshold: number; }; type ClusteredVolumetricDepthHistoryCopyUniforms = { inverseProjectionMatrix: Readonly; }; type ClusteredVolumetricCompositeUniforms = { strength: number; debugMode: number; }; type ClusteredVolumetricTraceBindings = { depthTexture?: Texture; pointLights?: Buffer; clusterLightCounts?: Buffer; clusterLightIndices?: Buffer; }; type ClusteredVolumetricTemporalBindings = { depthTexture?: Texture; velocityTexture?: Texture; historyTexture?: Texture; previousDepthTexture?: Texture; }; const IDENTITY_MATRIX: NumberArray16 = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]; /** Integrates real clustered point lights and occluded directional light through height fog. */ export const clusteredVolumetricTrace = { name: 'clusteredVolumetricTrace', source: /* wgsl */ `\ const CLUSTERED_VOLUMETRIC_PI: f32 = 3.141592653589793; const CLUSTERED_VOLUMETRIC_MAX_STEPS: u32 = 20u; const CLUSTERED_VOLUMETRIC_MAX_LIGHTS: u32 = 10u; const CLUSTERED_VOLUMETRIC_MAX_GOD_RAY_STEPS: u32 = 32u; struct ClusteredVolumetricTraceUniforms { projectionMatrix: mat4x4f, inverseProjectionMatrix: mat4x4f, inverseViewMatrix: mat4x4f, directionalLightDirectionView: vec3f, directionalLightColor: vec3f, fogColor: vec3f, godRayPosition: vec2f, density: f32, heightFalloff: f32, fogHeight: f32, anisotropy: f32, directionalIntensity: f32, pointLightIntensity: f32, godRayIntensity: f32, godRayDensity: f32, godRayDecay: f32, godRaySampleCount: f32, godRaysOnly: f32, maxDistance: f32, sampleCount: f32, shadowStrength: f32, clusterCountX: u32, clusterCountY: u32, clusterCountZ: u32, maxLightsPerCluster: u32, pointLightCount: u32, clusterNearPlane: f32, clusterFarPlane: f32, }; struct ClusteredVolumetricPointLight { positionRange: vec4f, colorIntensity: vec4f, }; @group(0) @binding(auto) var clusteredVolumetricTrace: ClusteredVolumetricTraceUniforms; @group(0) @binding(auto) var depthTexture: texture_depth_2d; @group(0) @binding(auto) var depthTextureSampler: sampler; @group(0) @binding(auto) var pointLights: array; @group(0) @binding(auto) var clusterLightCounts: array; @group(0) @binding(auto) var clusterLightIndices: array; fn clusteredVolumetricTrace_reconstructViewPosition(texCoord: vec2f, depth: f32) -> vec3f { let clip = vec4f(texCoord.x * 2.0 - 1.0, 1.0 - texCoord.y * 2.0, depth, 1.0); let viewPosition = clusteredVolumetricTrace.inverseProjectionMatrix * clip; return viewPosition.xyz / max(abs(viewPosition.w), 0.00001); } fn clusteredVolumetricTrace_projectViewPosition(viewPosition: vec3f) -> vec2f { let clip = clusteredVolumetricTrace.projectionMatrix * vec4f(viewPosition, 1.0); let normalizedDeviceCoordinate = clip.xy / max(clip.w, 0.00001); return normalizedDeviceCoordinate * vec2f(0.5, -0.5) + vec2f(0.5); } fn clusteredVolumetricTrace_phase(cosine: f32, anisotropy: f32) -> f32 { let squaredAnisotropy = anisotropy * anisotropy; let denominator = max(1.0 + squaredAnisotropy - 2.0 * anisotropy * cosine, 0.02); return (1.0 - squaredAnisotropy) / (4.0 * CLUSTERED_VOLUMETRIC_PI * pow(denominator, 1.5)); } fn clusteredVolumetricTrace_clusterIndex(texCoord: vec2f, viewDepth: f32) -> u32 { let tileX = min( u32(clamp(texCoord.x, 0.0, 0.999999) * f32(clusteredVolumetricTrace.clusterCountX)), clusteredVolumetricTrace.clusterCountX - 1u ); let tileY = min( u32(clamp(texCoord.y, 0.0, 0.999999) * f32(clusteredVolumetricTrace.clusterCountY)), clusteredVolumetricTrace.clusterCountY - 1u ); let normalizedDepth = clamp( log(max(viewDepth, clusteredVolumetricTrace.clusterNearPlane) / clusteredVolumetricTrace.clusterNearPlane) / log(clusteredVolumetricTrace.clusterFarPlane / clusteredVolumetricTrace.clusterNearPlane), 0.0, 0.999999 ); let tileZ = min( u32(normalizedDepth * f32(clusteredVolumetricTrace.clusterCountZ)), clusteredVolumetricTrace.clusterCountZ - 1u ); return tileX + clusteredVolumetricTrace.clusterCountX * (tileY + clusteredVolumetricTrace.clusterCountY * tileZ); } fn clusteredVolumetricTrace_directionalVisibility(viewPosition: vec3f) -> f32 { let lightDirection = normalize(clusteredVolumetricTrace.directionalLightDirectionView); for (var shadowStep: u32 = 1u; shadowStep <= 3u; shadowStep++) { let shadowPosition = viewPosition + lightDirection * (0.32 + f32(shadowStep) * 0.95); if (shadowPosition.z >= -0.04) { return 1.0; } let shadowCoord = clusteredVolumetricTrace_projectViewPosition(shadowPosition); if (any(shadowCoord <= vec2f(0.0)) || any(shadowCoord >= vec2f(1.0))) { return 1.0; } let occluderDepth = textureSampleLevel(depthTexture, depthTextureSampler, shadowCoord, 0); if (occluderDepth >= 0.99999) { continue; } let occluderPosition = clusteredVolumetricTrace_reconstructViewPosition( shadowCoord, occluderDepth ); let depthBehindOccluder = -shadowPosition.z - (-occluderPosition.z); if (depthBehindOccluder > 0.06 && depthBehindOccluder < 1.9) { return 1.0 - clusteredVolumetricTrace.shadowStrength; } } return 1.0; } fn clusteredVolumetricTrace_godRayVisibility(texCoord: vec2f) -> f32 { if (clusteredVolumetricTrace.godRayIntensity <= 0.0001) { return 0.0; } let sampleCount = clamp( u32(clusteredVolumetricTrace.godRaySampleCount), 3u, CLUSTERED_VOLUMETRIC_MAX_GOD_RAY_STEPS ); let sampleOffset = (texCoord - clusteredVolumetricTrace.godRayPosition) * clusteredVolumetricTrace.godRayDensity / f32(sampleCount); var sampleCoord = texCoord; var illumination = 0.0; var totalWeight = 0.0; var illuminationDecay = 1.0; for (var sampleIndex: u32 = 0u; sampleIndex < CLUSTERED_VOLUMETRIC_MAX_GOD_RAY_STEPS; sampleIndex++) { if (sampleIndex >= sampleCount) { break; } sampleCoord -= sampleOffset; if (any(sampleCoord < vec2f(0.0)) || any(sampleCoord > vec2f(1.0))) { break; } let occluderDepth = textureSampleLevel(depthTexture, depthTextureSampler, sampleCoord, 0); let visibility = smoothstep(0.9985, 0.99999, occluderDepth); illumination += visibility * illuminationDecay; totalWeight += illuminationDecay; illuminationDecay *= clusteredVolumetricTrace.godRayDecay; } let distanceFromSun = distance(texCoord, clusteredVolumetricTrace.godRayPosition); return illumination / max(totalWeight, 0.0001) * exp(-distanceFromSun * 1.6); } fn clusteredVolumetricTrace_pointRadiance( texCoord: vec2f, viewPosition: vec3f, viewDirection: vec3f ) -> vec3f { let clusterIndex = clusteredVolumetricTrace_clusterIndex(texCoord, -viewPosition.z); let clusterCount = clusterLightCounts[clusterIndex]; let firstCandidateIndex = clusterIndex * clusteredVolumetricTrace.maxLightsPerCluster; let clampedFirstCandidateIndex = min(firstCandidateIndex, arrayLength(&clusterLightIndices)); let storedCandidateCapacity = min( clusteredVolumetricTrace.maxLightsPerCluster, arrayLength(&clusterLightIndices) - clampedFirstCandidateIndex ); let candidateCount = min(clusterCount, storedCandidateCapacity); let activePointLightCount = min( clusteredVolumetricTrace.pointLightCount, arrayLength(&pointLights) ); var selectedLightIndices: array; var selectedLightScores: array; for (var slotIndex: u32 = 0u; slotIndex < CLUSTERED_VOLUMETRIC_MAX_LIGHTS; slotIndex++) { selectedLightIndices[slotIndex] = 0xffffffffu; selectedLightScores[slotIndex] = 1.0e20; } var selectedLightCount = 0u; var worstSlotIndex = 0u; var worstScore = -1.0; // Score every candidate retained by the cluster builder, including its complete overflow-safe // storage capacity, while keeping integration work independent of the global light count. for (var candidateIndex: u32 = 0u; candidateIndex < candidateCount; candidateIndex++) { let lightIndex = clusterLightIndices[firstCandidateIndex + candidateIndex]; if (lightIndex >= activePointLightCount) { continue; } let light = pointLights[lightIndex]; let toLight = light.positionRange.xyz - viewPosition; let distanceSquared = dot(toLight, toLight); let rangeSquared = light.positionRange.w * light.positionRange.w; if (distanceSquared >= rangeSquared || distanceSquared <= 0.0004) { continue; } let lightScore = distanceSquared / max(rangeSquared * max(light.colorIntensity.a, 0.05), 0.0001); if (selectedLightCount < CLUSTERED_VOLUMETRIC_MAX_LIGHTS) { selectedLightIndices[selectedLightCount] = lightIndex; selectedLightScores[selectedLightCount] = lightScore; if (lightScore > worstScore) { worstSlotIndex = selectedLightCount; worstScore = lightScore; } selectedLightCount += 1u; continue; } if (lightScore < worstScore) { selectedLightIndices[worstSlotIndex] = lightIndex; selectedLightScores[worstSlotIndex] = lightScore; worstSlotIndex = 0u; worstScore = selectedLightScores[0]; for (var slotIndex = 1u; slotIndex < CLUSTERED_VOLUMETRIC_MAX_LIGHTS; slotIndex++) { if (selectedLightScores[slotIndex] > worstScore) { worstSlotIndex = slotIndex; worstScore = selectedLightScores[slotIndex]; } } } } var radiance = vec3f(0.0); for (var slotIndex: u32 = 0u; slotIndex < CLUSTERED_VOLUMETRIC_MAX_LIGHTS; slotIndex++) { let lightIndex = selectedLightIndices[slotIndex]; if (lightIndex >= arrayLength(&pointLights)) { continue; } let light = pointLights[lightIndex]; let toLight = light.positionRange.xyz - viewPosition; let distanceToLight = length(toLight); if (distanceToLight >= light.positionRange.w || distanceToLight <= 0.02) { continue; } let lightDirection = toLight / distanceToLight; let rangeFade = pow(1.0 - distanceToLight / light.positionRange.w, 2.0); let attenuation = rangeFade / (1.0 + distanceToLight * distanceToLight * 0.1); let phase = clusteredVolumetricTrace_phase( dot(viewDirection, lightDirection), clusteredVolumetricTrace.anisotropy * 0.65 ); radiance += light.colorIntensity.rgb * light.colorIntensity.a * attenuation * phase * clusteredVolumetricTrace.pointLightIntensity; } return radiance; } fn clusteredVolumetricTrace_sampleColor( sourceTexture: texture_2d, sourceTextureSampler: sampler, texSize: vec2f, texCoord: vec2f ) -> vec4f { if (clusteredVolumetricTrace.density <= 0.0001) { return vec4f(0.0, 0.0, 0.0, 1.0); } let sceneDepth = textureSampleLevel(depthTexture, depthTextureSampler, texCoord, 0); let rayOrigin = clusteredVolumetricTrace_reconstructViewPosition(texCoord, 0.0); let farPosition = clusteredVolumetricTrace_reconstructViewPosition(texCoord, 0.9999); let viewDirection = normalize(farPosition - rayOrigin); let scenePosition = clusteredVolumetricTrace_reconstructViewPosition(texCoord, sceneDepth); let rayDistance = select( min( max(dot(scenePosition - rayOrigin, viewDirection), 0.0), clusteredVolumetricTrace.maxDistance ), clusteredVolumetricTrace.maxDistance, sceneDepth >= 0.99999 ); let stepCount = clamp(u32(clusteredVolumetricTrace.sampleCount), 3u, CLUSTERED_VOLUMETRIC_MAX_STEPS); let stepDistance = rayDistance / f32(stepCount); let pixelCoordinate = floor(texCoord * texSize); let rayJitter = fract(sin(dot(pixelCoordinate, vec2f(12.9898, 78.233))) * 43758.5453); let directionalPhase = clusteredVolumetricTrace_phase( dot(viewDirection, normalize(clusteredVolumetricTrace.directionalLightDirectionView)), clusteredVolumetricTrace.anisotropy ); let godRayVisibility = clusteredVolumetricTrace_godRayVisibility(texCoord); var opticalDepth = 0.0; var scattering = vec3f(0.0); for (var stepIndex: u32 = 0u; stepIndex < CLUSTERED_VOLUMETRIC_MAX_STEPS; stepIndex++) { if (stepIndex >= stepCount) { break; } let travel = (f32(stepIndex) + 0.28 + rayJitter * 0.52) * stepDistance; let viewPosition = rayOrigin + viewDirection * travel; let worldPosition = (clusteredVolumetricTrace.inverseViewMatrix * vec4f(viewPosition, 1.0)).xyz; let localDensity = clusteredVolumetricTrace.density * exp( -max(worldPosition.y - clusteredVolumetricTrace.fogHeight, 0.0) * clusteredVolumetricTrace.heightFalloff ); let extinction = localDensity * stepDistance; opticalDepth += extinction; let directionalVisibility = clusteredVolumetricTrace_directionalVisibility(viewPosition); let directionalRadiance = clusteredVolumetricTrace.directionalLightColor * clusteredVolumetricTrace.directionalIntensity * directionalPhase * directionalVisibility; let godRayRadiance = clusteredVolumetricTrace.directionalLightColor * clusteredVolumetricTrace.directionalIntensity * directionalPhase * godRayVisibility * clusteredVolumetricTrace.godRayIntensity * 2.4; let localRadiance = clusteredVolumetricTrace_pointRadiance( texCoord, viewPosition, viewDirection ); let incomingRadiance = select( clusteredVolumetricTrace.fogColor * 0.11 + directionalRadiance + localRadiance + godRayRadiance, godRayRadiance, clusteredVolumetricTrace.godRaysOnly > 0.5 ); scattering += incomingRadiance * extinction * exp(-opticalDepth); } return vec4f(scattering, clamp(exp(-opticalDepth), 0.0, 1.0)); }`, bindingLayout: [ {name: 'depthTexture', group: 0}, {name: 'pointLights', group: 0}, {name: 'clusterLightCounts', group: 0}, {name: 'clusterLightIndices', group: 0} ], props: {} as Partial & ClusteredVolumetricTraceBindings, uniforms: {} as ClusteredVolumetricTraceUniforms, bindings: {} as ClusteredVolumetricTraceBindings, uniformTypes: { projectionMatrix: 'mat4x4', inverseProjectionMatrix: 'mat4x4', inverseViewMatrix: 'mat4x4', directionalLightDirectionView: 'vec3', directionalLightColor: 'vec3', fogColor: 'vec3', godRayPosition: 'vec2', density: 'f32', heightFalloff: 'f32', fogHeight: 'f32', anisotropy: 'f32', directionalIntensity: 'f32', pointLightIntensity: 'f32', godRayIntensity: 'f32', godRayDensity: 'f32', godRayDecay: 'f32', godRaySampleCount: 'f32', godRaysOnly: 'f32', maxDistance: 'f32', sampleCount: 'f32', shadowStrength: 'f32', clusterCountX: 'u32', clusterCountY: 'u32', clusterCountZ: 'u32', maxLightsPerCluster: 'u32', pointLightCount: 'u32', clusterNearPlane: 'f32', clusterFarPlane: 'f32' }, propTypes: { projectionMatrix: {value: IDENTITY_MATRIX, private: true}, inverseProjectionMatrix: {value: IDENTITY_MATRIX, private: true}, inverseViewMatrix: {value: IDENTITY_MATRIX, private: true}, directionalLightDirectionView: {value: [0.35, 0.82, 0.38], private: true}, directionalLightColor: {value: [1, 0.85, 0.66], private: true}, fogColor: {value: [0.18, 0.3, 0.48]}, godRayPosition: {value: [0.72, 0.16]}, density: {value: 0.055, min: 0, softMax: 0.22}, heightFalloff: {value: 0.23, min: 0, softMax: 2}, fogHeight: {value: 0.2, min: -5, softMax: 5}, anisotropy: {value: 0.45, min: -0.8, max: 0.85}, directionalIntensity: {value: 2.2, min: 0, softMax: 8}, pointLightIntensity: {value: 1.8, min: 0, softMax: 6}, godRayIntensity: {value: 0, min: 0, softMax: 6}, godRayDensity: {value: 0.94, min: 0.1, max: 1.2}, godRayDecay: {value: 0.96, min: 0.7, max: 1}, godRaySampleCount: {value: 18, min: 3, max: 32}, godRaysOnly: {value: 0, min: 0, max: 1, private: true}, maxDistance: {value: 28, min: 1, softMax: 80}, sampleCount: {value: 10, min: 3, max: 20}, shadowStrength: {value: 0.74, min: 0, max: 1}, clusterCountX: {value: 1, private: true}, clusterCountY: {value: 1, private: true}, clusterCountZ: {value: 1, private: true}, maxLightsPerCluster: {value: 1, private: true}, pointLightCount: {value: 0, private: true}, clusterNearPlane: {value: 0.1, private: true}, clusterFarPlane: {value: 100, private: true} }, passes: [{sampler: true}] } as const satisfies ShaderPass< Partial & ClusteredVolumetricTraceBindings, ClusteredVolumetricTraceUniforms, ClusteredVolumetricTraceBindings >; /** Reprojects participating-media radiance and rejects invalid linear-depth history. */ export const clusteredVolumetricTemporal = { name: 'clusteredVolumetricTemporal', source: /* wgsl */ `\ struct ClusteredVolumetricTemporalUniforms { inverseProjectionMatrix: mat4x4f, inverseViewProjectionMatrix: mat4x4f, previousViewProjectionMatrix: mat4x4f, historyWeight: f32, depthThreshold: f32, }; @group(0) @binding(auto) var clusteredVolumetricTemporal: ClusteredVolumetricTemporalUniforms; @group(0) @binding(auto) var historyTexture: texture_2d; @group(0) @binding(auto) var historyTextureSampler: sampler; @group(0) @binding(auto) var velocityTexture: texture_2d; @group(0) @binding(auto) var velocityTextureSampler: sampler; @group(0) @binding(auto) var depthTexture: texture_depth_2d; @group(0) @binding(auto) var depthTextureSampler: sampler; @group(0) @binding(auto) var previousDepthTexture: texture_2d; fn clusteredVolumetricTemporal_viewDepth(texCoord: vec2f, depth: f32) -> f32 { let clip = vec4f(texCoord.x * 2.0 - 1.0, 1.0 - texCoord.y * 2.0, depth, 1.0); let viewPosition = clusteredVolumetricTemporal.inverseProjectionMatrix * clip; return abs(viewPosition.z / max(abs(viewPosition.w), 0.00001)); } fn clusteredVolumetricTemporal_cameraPreviousCoord(texCoord: vec2f, depth: f32) -> vec3f { let clip = vec4f(texCoord.x * 2.0 - 1.0, 1.0 - texCoord.y * 2.0, depth, 1.0); let worldPosition = clusteredVolumetricTemporal.inverseViewProjectionMatrix * clip; let world = worldPosition.xyz / max(abs(worldPosition.w), 0.00001); let previousClip = clusteredVolumetricTemporal.previousViewProjectionMatrix * vec4f(world, 1.0); let previousNdc = previousClip.xy / max(abs(previousClip.w), 0.00001); let previousCoord = previousNdc * vec2f(0.5, -0.5) + vec2f(0.5); return vec3f(previousCoord, select(0.0, 1.0, previousClip.w > 0.00001)); } fn clusteredVolumetricTemporal_sampleColor( sourceTexture: texture_2d, sourceTextureSampler: sampler, texSize: vec2f, texCoord: vec2f ) -> vec4f { let current = textureSampleLevel(sourceTexture, sourceTextureSampler, texCoord, 0); let currentDepth = textureSampleLevel(depthTexture, depthTextureSampler, texCoord, 0); let velocity = textureSampleLevel(velocityTexture, velocityTextureSampler, texCoord, 0).xy; let cameraReprojection = clusteredVolumetricTemporal_cameraPreviousCoord(texCoord, currentDepth); let velocityIsFinite = all(velocity == velocity) && all(abs(velocity) <= vec2f(1.0)); let useObjectVelocity = currentDepth < 0.99999 && velocityIsFinite; let previousCoord = select(cameraReprojection.xy, texCoord - velocity, useObjectVelocity); let validCoordinate = all(previousCoord >= vec2f(0.0)) && all(previousCoord <= vec2f(1.0)) && (useObjectVelocity || cameraReprojection.z > 0.5); let clampedPreviousCoord = clamp(previousCoord, vec2f(0.0), vec2f(1.0)); let previousDepthDimensions = textureDimensions(previousDepthTexture); let previousDepthCoordinate = min( vec2u(clampedPreviousCoord * vec2f(previousDepthDimensions)), previousDepthDimensions - vec2u(1u) ); let currentViewDepth = clusteredVolumetricTemporal_viewDepth(texCoord, currentDepth); let previousViewDepth = textureLoad( previousDepthTexture, vec2i(previousDepthCoordinate), 0 ).r; let relativeDepthDifference = abs(previousViewDepth - currentViewDepth) / max(currentViewDepth, 0.0001); let validDepth = previousViewDepth > 0.0 && relativeDepthDifference < clusteredVolumetricTemporal.depthThreshold; let texel = 1.0 / vec2f(textureDimensions(sourceTexture)); var minimumScattering = current; var maximumScattering = current; for (var sampleY: i32 = -1; sampleY <= 1; sampleY++) { for (var sampleX: i32 = -1; sampleX <= 1; sampleX++) { let sampleCoord = clamp( texCoord + vec2f(f32(sampleX), f32(sampleY)) * texel, vec2f(0.0), vec2f(1.0) ); let sampleScattering = textureSampleLevel( sourceTexture, sourceTextureSampler, sampleCoord, 0 ); minimumScattering = min(minimumScattering, sampleScattering); maximumScattering = max(maximumScattering, sampleScattering); } } let history = clamp( textureSampleLevel(historyTexture, historyTextureSampler, clampedPreviousCoord, 0), minimumScattering, maximumScattering ); let weight = select( 0.0, clusteredVolumetricTemporal.historyWeight, validCoordinate && validDepth ); return mix(current, history, weight); }`, bindingLayout: [ {name: 'historyTexture', group: 0}, {name: 'velocityTexture', group: 0}, {name: 'depthTexture', group: 0}, {name: 'previousDepthTexture', group: 0} ], props: {} as Partial & ClusteredVolumetricTemporalBindings, uniforms: {} as ClusteredVolumetricTemporalUniforms, bindings: {} as ClusteredVolumetricTemporalBindings, uniformTypes: { inverseProjectionMatrix: 'mat4x4', inverseViewProjectionMatrix: 'mat4x4', previousViewProjectionMatrix: 'mat4x4', historyWeight: 'f32', depthThreshold: 'f32' }, propTypes: { inverseProjectionMatrix: {value: IDENTITY_MATRIX, private: true}, inverseViewProjectionMatrix: {value: IDENTITY_MATRIX, private: true}, previousViewProjectionMatrix: {value: IDENTITY_MATRIX, private: true}, historyWeight: {value: 0.88, min: 0, max: 0.97}, depthThreshold: {value: 0.035, min: 0.0001, softMax: 0.2} }, passes: [{sampler: true}] } as const satisfies ShaderPass< Partial & ClusteredVolumetricTemporalBindings, ClusteredVolumetricTemporalUniforms, ClusteredVolumetricTemporalBindings >; /** Captures current scene depth for atmospheric temporal disocclusion rejection. */ export const clusteredVolumetricDepthHistoryCopy = { name: 'clusteredVolumetricDepthHistoryCopy', source: /* wgsl */ `\ struct ClusteredVolumetricDepthHistoryCopyUniforms { inverseProjectionMatrix: mat4x4f, }; @group(0) @binding(auto) var clusteredVolumetricDepthHistoryCopy: ClusteredVolumetricDepthHistoryCopyUniforms; @group(0) @binding(auto) var depthTexture: texture_depth_2d; @group(0) @binding(auto) var depthTextureSampler: sampler; fn clusteredVolumetricDepthHistoryCopy_sampleColor( sourceTexture: texture_2d, sourceTextureSampler: sampler, texSize: vec2f, texCoord: vec2f ) -> vec4f { let depth = textureSampleLevel(depthTexture, depthTextureSampler, texCoord, 0); let clip = vec4f(texCoord.x * 2.0 - 1.0, 1.0 - texCoord.y * 2.0, depth, 1.0); let viewPosition = clusteredVolumetricDepthHistoryCopy.inverseProjectionMatrix * clip; let linearDepth = abs(viewPosition.z / max(abs(viewPosition.w), 0.00001)); return vec4f(linearDepth, 0.0, 0.0, 1.0); }`, bindingLayout: [{name: 'depthTexture', group: 0}], props: {} as Partial, uniforms: {} as ClusteredVolumetricDepthHistoryCopyUniforms, uniformTypes: {inverseProjectionMatrix: 'mat4x4'}, propTypes: {inverseProjectionMatrix: {value: IDENTITY_MATRIX, private: true}}, passes: [{sampler: true}] } as const satisfies ShaderPass< Partial, ClusteredVolumetricDepthHistoryCopyUniforms >; /** Applies atmospheric extinction and in-scattering, or exposes direct volume diagnostics. */ export const clusteredVolumetricComposite = { name: 'clusteredVolumetricComposite', source: /* wgsl */ `\ struct ClusteredVolumetricCompositeUniforms { strength: f32, debugMode: f32, }; @group(0) @binding(auto) var clusteredVolumetricComposite: ClusteredVolumetricCompositeUniforms; @group(0) @binding(auto) var scatteringTexture: texture_2d; @group(0) @binding(auto) var scatteringTextureSampler: sampler; fn clusteredVolumetricComposite_sampleColor( sourceTexture: texture_2d, sourceTextureSampler: sampler, texSize: vec2f, texCoord: vec2f ) -> vec4f { let source = textureSampleLevel(sourceTexture, sourceTextureSampler, texCoord, 0); let scattering = textureSampleLevel(scatteringTexture, scatteringTextureSampler, texCoord, 0); if (clusteredVolumetricComposite.debugMode > 1.5) { return vec4f(vec3f(clamp(scattering.a, 0.0, 1.0)), 1.0); } if (clusteredVolumetricComposite.debugMode > 0.5) { return vec4f(scattering.rgb * 2.4, 1.0); } let strength = clamp(clusteredVolumetricComposite.strength, 0.0, 2.0); let transmittance = mix(1.0, clamp(scattering.a, 0.0, 1.0), min(strength, 1.0)); return vec4f(source.rgb * transmittance + scattering.rgb * strength, source.a); }`, bindingLayout: [{name: 'scatteringTexture', group: 0}], props: {} as Partial, uniforms: {} as ClusteredVolumetricCompositeUniforms, uniformTypes: {strength: 'f32', debugMode: 'f32'}, propTypes: { strength: {value: 0.9, min: 0, softMax: 2}, debugMode: {value: 0, min: 0, max: 2, private: true} }, passes: [{sampler: true}] } as const satisfies ShaderPass< Partial, ClusteredVolumetricCompositeUniforms >; /** Creates clustered participating-media integration, temporal stabilization, and composition. */ export function createClusteredVolumetricLightingShaderPassPipeline( options: ClusteredVolumetricLightingShaderPassPipelineOptions = {} ): ShaderPassPipeline< | 'clusteredVolumeRaw' | 'clusteredVolumeHistory' | 'clusteredVolumeDepthHistory' | 'clusteredVolumeScratch' | 'clusteredVolumeScattering' > { const scale = options.resolutionScale ?? 1; return { name: 'clusteredVolumetricLightingShaderPassPipeline', renderTargets: { clusteredVolumeRaw: {scale: [scale, scale], format: 'rgba16float'}, clusteredVolumeHistory: { scale: [scale, scale], format: 'rgba16float', lifetime: 'history', initialize: {clearColor: [0, 0, 0, 1]} }, clusteredVolumeDepthHistory: { scale: [scale, scale], // Keep the same four-byte footprint as r32float while remaining filterable on core WebGPU. // The temporal shader uses textureLoad(), but the inferred texture_2d binding still // requires a filterable sample type when the surrounding pass layout is assembled. format: 'rg16float', lifetime: 'history', initialize: {clearColor: [0, 0, 0, 1]} }, clusteredVolumeScratch: {scale: [scale, scale], format: 'rgba16float'}, clusteredVolumeScattering: {scale: [scale, scale], format: 'rgba16float'} }, steps: [ { shaderPass: clusteredVolumetricTrace, inputs: {sourceTexture: 'previous'}, output: 'clusteredVolumeRaw' }, { shaderPass: clusteredVolumetricTemporal, inputs: { sourceTexture: 'clusteredVolumeRaw', historyTexture: 'clusteredVolumeHistory', previousDepthTexture: 'clusteredVolumeDepthHistory' }, output: 'clusteredVolumeHistory' }, { shaderPass: clusteredVolumetricDepthHistoryCopy, inputs: {sourceTexture: 'previous'}, output: 'clusteredVolumeDepthHistory' }, { shaderPass: depthAwareBlur, inputs: {sourceTexture: 'clusteredVolumeHistory'}, output: 'clusteredVolumeScratch', uniforms: {direction: [1, 0], radius: 3, spatialSigma: 2.2, depthSigma: 0.018} }, { shaderPass: depthAwareBlur, inputs: {sourceTexture: 'clusteredVolumeScratch'}, output: 'clusteredVolumeScattering', uniforms: {direction: [0, 1], radius: 3, spatialSigma: 2.2, depthSigma: 0.018} }, { shaderPass: clusteredVolumetricComposite, inputs: {sourceTexture: 'previous', scatteringTexture: 'clusteredVolumeScattering'}, output: 'previous' } ] }; }