Skip to main content

Variable: opticalCaustics

const opticalCaustics: object

Defined in: modules/engine/src/materials/optical-caustics.ts:215

Portable raster approximation of focused light projected through nearby glass.

Type Declaration​

defaultUniforms​

readonly defaultUniforms: OpticalCausticsUniforms

dependencies​

readonly dependencies: [{ fs: "vec3 opticalLighting_faceNormal(vec3 normal, vec3 viewDirection) {\n return dot(normal, viewDirection) >= 0.0 ? normal : -normal;\n}\n\nfloat opticalLighting_getFresnel(float viewAlignment, float baseReflectance, float exponent) {\n return baseReflectance + (1.0 - baseReflectance) *\n pow(1.0 - clamp(viewAlignment, 0.0, 1.0), exponent);\n}\n\nfloat opticalLighting_getFilteredRoughness(vec3 normal, float roughness) {\n vec3 normalDerivativeX = dFdx(normal);\n vec3 normalDerivativeY = dFdy(normal);\n float normalVariance = dot(normalDerivativeX, normalDerivativeX) +\n dot(normalDerivativeY, normalDerivativeY);\n float kernelRoughnessSquared = min(2.0 * normalVariance, 1.0);\n return clamp(sqrt(roughness * roughness + kernelRoughnessSquared), 0.04, 1.0);\n}\n\nfloat opticalLighting_getMicrofacetSpecular(\n vec3 normal,\n vec3 viewDirection,\n vec3 lightDirection,\n float roughness\n) {\n vec3 halfDirection = normalize(viewDirection + lightDirection);\n float normalAlignment = max(dot(normal, viewDirection), 0.001);\n float lightAlignment = max(dot(normal, lightDirection), 0.0);\n float halfAlignment = max(dot(normal, halfDirection), 0.0);\n float alpha = max(roughness * roughness, 0.045);\n float alphaSquared = alpha * alpha;\n float distributionDenominator = halfAlignment * halfAlignment * (alphaSquared - 1.0) + 1.0;\n float distribution = alphaSquared /\n max(3.14159265 * distributionDenominator * distributionDenominator, 0.001);\n float visibilityFactor = pow(roughness + 1.0, 2.0) * 0.125;\n float viewVisibility = normalAlignment /\n (normalAlignment * (1.0 - visibilityFactor) + visibilityFactor);\n float lightVisibility = lightAlignment /\n (lightAlignment * (1.0 - visibilityFactor) + visibilityFactor);\n return distribution * viewVisibility * lightVisibility * lightAlignment /\n max(4.0 * normalAlignment * lightAlignment, 0.001);\n}\n\nvec3 opticalLighting_getKeyLight() {\n return normalize(vec3(-0.45, 0.82, 0.34));\n}\n\nvec3 opticalLighting_getFillLight() {\n return normalize(vec3(0.62, 0.35, -0.7));\n}\n\nvec3 opticalLighting_sampleEnvironment(\n vec3 reflectionDirection,\n vec3 shadowColor,\n vec3 highlightColor,\n float horizonStrength\n) {\n float horizon = pow(1.0 - abs(reflectionDirection.y), 4.0);\n return mix(\n shadowColor,\n highlightColor,\n clamp(reflectionDirection.y * 0.5 + 0.5 + horizon * horizonStrength, 0.0, 1.0)\n );\n}\n"; name: "opticalLighting"; source: "fn opticalLighting_faceNormal(normal: vec3<f32>, viewDirection: vec3<f32>) -> vec3<f32> {\n return select(-normal, normal, dot(normal, viewDirection) >= 0.0);\n}\n\nfn opticalLighting_getFresnel(\n viewAlignment: f32,\n baseReflectance: f32,\n exponent: f32\n) -> f32 {\n return baseReflectance + (1.0 - baseReflectance) *\n pow(1.0 - clamp(viewAlignment, 0.0, 1.0), exponent);\n}\n\nfn opticalLighting_getFilteredRoughness(normal: vec3<f32>, roughness: f32) -> f32 {\n let normalDerivativeX = dpdx(normal);\n let normalDerivativeY = dpdy(normal);\n let normalVariance = dot(normalDerivativeX, normalDerivativeX) +\n dot(normalDerivativeY, normalDerivativeY);\n let kernelRoughnessSquared = min(2.0 * normalVariance, 1.0);\n return clamp(sqrt(roughness * roughness + kernelRoughnessSquared), 0.04, 1.0);\n}\n\nfn opticalLighting_getMicrofacetSpecular(\n normal: vec3<f32>,\n viewDirection: vec3<f32>,\n lightDirection: vec3<f32>,\n roughness: f32\n) -> f32 {\n let halfDirection = normalize(viewDirection + lightDirection);\n let normalAlignment = max(dot(normal, viewDirection), 0.001);\n let lightAlignment = max(dot(normal, lightDirection), 0.0);\n let halfAlignment = max(dot(normal, halfDirection), 0.0);\n let alpha = max(roughness * roughness, 0.045);\n let alphaSquared = alpha * alpha;\n let distributionDenominator = halfAlignment * halfAlignment * (alphaSquared - 1.0) + 1.0;\n let distribution = alphaSquared /\n max(3.14159265 * distributionDenominator * distributionDenominator, 0.001);\n let visibilityFactor = pow(roughness + 1.0, 2.0) * 0.125;\n let viewVisibility = normalAlignment /\n (normalAlignment * (1.0 - visibilityFactor) + visibilityFactor);\n let lightVisibility = lightAlignment /\n (lightAlignment * (1.0 - visibilityFactor) + visibilityFactor);\n return distribution * viewVisibility * lightVisibility * lightAlignment /\n max(4.0 * normalAlignment * lightAlignment, 0.001);\n}\n\nfn opticalLighting_getKeyLight() -> vec3<f32> {\n return normalize(vec3<f32>(-0.45, 0.82, 0.34));\n}\n\nfn opticalLighting_getFillLight() -> vec3<f32> {\n return normalize(vec3<f32>(0.62, 0.35, -0.7));\n}\n\nfn opticalLighting_sampleEnvironment(\n reflectionDirection: vec3<f32>,\n shadowColor: vec3<f32>,\n highlightColor: vec3<f32>,\n horizonStrength: f32\n) -> vec3<f32> {\n let horizon = pow(1.0 - abs(reflectionDirection.y), 4.0);\n return mix(\n shadowColor,\n highlightColor,\n clamp(reflectionDirection.y * 0.5 + 0.5 + horizon * horizonStrength, 0.0, 1.0)\n );\n}\n"; }]

fs​

readonly fs: "struct OpticalCausticLensUniform {\n vec3 position;\n float radius;\n vec3 color;\n float intensity;\n};\n\nlayout(std140) uniform opticalCausticsUniforms {\n int lensCount;\n float intensity;\n float focus;\n OpticalCausticLensUniform lenses[8];\n} opticalCaustics;\n\nvec3 opticalCaustics_getColor(\n vec3 normal,\n vec3 worldPosition,\n vec3 cameraPosition\n) {\n vec3 viewDirection = normalize(cameraPosition - worldPosition);\n vec3 normalFacingCamera = opticalLighting_faceNormal(normalize(normal), viewDirection);\n vec3 accumulatedColor = vec3(0.0);\n\n for (int lensIndex = 0; lensIndex < 8; lensIndex++) {\n if (lensIndex >= opticalCaustics.lensCount) {\n break;\n }\n\n OpticalCausticLensUniform lens = opticalCaustics.lenses[lensIndex];\n vec3 lensOffset = worldPosition - lens.position;\n float distanceSquared = dot(lensOffset, lensOffset);\n float radius = max(lens.radius, 0.0001);\n float distance = sqrt(distanceSquared);\n float normalizedDistance = clamp(distance / (radius * 4.2), 0.0, 1.0);\n float attenuation = pow(1.0 - normalizedDistance, 2.0);\n float projectedDistance = max(-lensOffset.y, 0.0);\n float spread = radius * (0.82 + projectedDistance * 0.38);\n float radialDistance = length(lensOffset.xz) / max(spread, 0.0001);\n float concentration = max(opticalCaustics.focus, 0.0);\n float focusedCore = exp(-radialDistance * radialDistance * (2.4 + concentration * 3.0));\n float focusedRing = exp(-pow(radialDistance - 0.58, 2.0) *\n (11.0 + concentration * 15.0));\n float angularDetail = 0.78 + 0.22 * cos(\n atan(lensOffset.z, lensOffset.x) * 3.0 + distance * 7.0\n );\n vec3 lensDirection = -lensOffset * inversesqrt(max(distanceSquared, 0.00001));\n float surfaceResponse = 0.18 +\n max(dot(normalFacingCamera, lensDirection), 0.0) * 0.82;\n float causticPattern = focusedCore * 0.58 + focusedRing * angularDetail * 0.42;\n accumulatedColor += lens.color * lens.intensity * attenuation *\n causticPattern * surfaceResponse;\n }\n\n return accumulatedColor * opticalCaustics.intensity;\n}\n" = OPTICAL_CAUSTICS_GLSL

getUniforms​

readonly getUniforms: (props, previousUniforms?) => OpticalCausticsUniforms = getOpticalCausticsUniforms

Parameters​

props?​

Partial<OpticalCausticsProps> = {}

previousUniforms?​

OpticalCausticsUniforms

Returns​

OpticalCausticsUniforms

name​

readonly name: "opticalCaustics" = 'opticalCaustics'

source​

readonly source: "struct OpticalCausticLensUniform {\n position: vec3<f32>,\n radius: f32,\n color: vec3<f32>,\n intensity: f32,\n};\n\nstruct opticalCausticsUniforms {\n lensCount: i32,\n intensity: f32,\n focus: f32,\n lenses: array<OpticalCausticLensUniform, 8>,\n};\n\n@group(0) @binding(auto) var<uniform> opticalCaustics: opticalCausticsUniforms;\n\nfn opticalCaustics_getColor(\n normal: vec3<f32>,\n worldPosition: vec3<f32>,\n cameraPosition: vec3<f32>\n) -> vec3<f32> {\n let viewDirection = normalize(cameraPosition - worldPosition);\n let normalFacingCamera = opticalLighting_faceNormal(normalize(normal), viewDirection);\n var accumulatedColor = vec3<f32>(0.0);\n\n for (var lensIndex = 0; lensIndex < 8; lensIndex++) {\n if (lensIndex >= opticalCaustics.lensCount) {\n break;\n }\n\n let lens = opticalCaustics.lenses[lensIndex];\n let lensOffset = worldPosition - lens.position;\n let distanceSquared = dot(lensOffset, lensOffset);\n let radius = max(lens.radius, 0.0001);\n let distance = sqrt(distanceSquared);\n let normalizedDistance = clamp(distance / (radius * 4.2), 0.0, 1.0);\n let attenuation = pow(1.0 - normalizedDistance, 2.0);\n let projectedDistance = max(-lensOffset.y, 0.0);\n let spread = radius * (0.82 + projectedDistance * 0.38);\n let radialDistance = length(lensOffset.xz) / max(spread, 0.0001);\n let concentration = max(opticalCaustics.focus, 0.0);\n let focusedCore = exp(-radialDistance * radialDistance * (2.4 + concentration * 3.0));\n let focusedRing = exp(-pow(radialDistance - 0.58, 2.0) *\n (11.0 + concentration * 15.0));\n let angularDetail = 0.78 + 0.22 * cos(\n atan2(lensOffset.z, lensOffset.x) * 3.0 + distance * 7.0\n );\n let lensDirection = -lensOffset * inverseSqrt(max(distanceSquared, 0.00001));\n let surfaceResponse = 0.18 +\n max(dot(normalFacingCamera, lensDirection), 0.0) * 0.82;\n let causticPattern = focusedCore * 0.58 + focusedRing * angularDetail * 0.42;\n accumulatedColor += lens.color * lens.intensity * attenuation *\n causticPattern * surfaceResponse;\n }\n\n return accumulatedColor * opticalCaustics.intensity;\n}\n" = OPTICAL_CAUSTICS_WGSL

uniformTypes​

readonly uniformTypes: object

uniformTypes.focus​

readonly focus: "f32" = 'f32'

uniformTypes.intensity​

readonly intensity: "f32" = 'f32'

uniformTypes.lensCount​

readonly lensCount: "i32" = 'i32'

uniformTypes.lenses​

readonly lenses: readonly [{ color: "vec3<f32>"; intensity: "f32"; position: "vec3<f32>"; radius: "f32"; }, 8]