Variable: opticalLighting
constopticalLighting:object
Defined in: modules/engine/src/materials/optical-lighting.ts:144
Shared portable Fresnel, normal-facing, lighting, and environment-reflection helpers.
Type Declaration
fs
readonlyfs: "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" =OPTICAL_LIGHTING_GLSL
name
readonlyname:"opticalLighting"='opticalLighting'
source
readonlysource: "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" =OPTICAL_LIGHTING_WGSL