UNPKG

@babylonjs/viewer

Version:

The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.

3 lines (2 loc) 5.16 kB
function e(e=[{lightIndex:0,shadowType:"esm"}]){const t=e.filter(e=>"csm"===e.shadowType);if(t.length>0)return null(t.map(e=>({lightIndex:e.lightIndex})));const o=function(e,t){const o=[],s=[],a=[],p=[],h=[];for(const e of t){const t=e.lightIndex,h=`_${t}`;o.push({_name:`vPosFromLight${h}`,_type:"vec4<f32>"},{_name:`vDepthMetric${h}`,_type:"f32"}),"pcf"===e.shadowType?s.push({_name:`shadowTex${h}`,_type:{_kind:"texture",_textureType:"texture_depth_2d",_sampleType:"depth"},_group:"shadow",_visibility:2},{_name:`shadowComp${h}`,_type:{_kind:"sampler",_samplerType:"sampler_comparison"},_group:"shadow",_visibility:2}):s.push({_name:`shadowTex${h}`,_type:{_kind:"texture",_textureType:"texture_2d<f32>"},_group:"shadow",_visibility:2},{_name:`shadowSamp${h}`,_type:{_kind:"sampler",_samplerType:"sampler"},_group:"shadow",_visibility:2}),s.push({_name:`shadowInfo${h}`,_type:{_kind:"uniform-buffer"},_group:"shadow",_visibility:3}),a.push(`out.vPosFromLight${h} = shadowInfo${h}.lightMatrix * worldPos4;`,`out.vDepthMetric${h} = (out.vPosFromLight${h}.z + shadowInfo${h}.depthValues.x) / shadowInfo${h}.depthValues.y;`),"pcf"===e.shadowType?p.push(`shadowFactors[${t}] = computeShadowPCF${h}(input.vPosFromLight${h}, input.vDepthMetric${h}, shadowInfo${h}.shadowsInfo.x, shadowInfo${h}.shadowsInfo.y, shadowInfo${h}.shadowsInfo.z);`):p.push(`shadowFactors[${t}] = computeShadowESM${h}(input.vPosFromLight${h}, input.vDepthMetric${h}, shadowInfo${h}.shadowsInfo.x, shadowInfo${h}.shadowsInfo.z, shadowInfo${h}.shadowsInfo.w);`)}for(const e of t){const t=`_${e.lightIndex}`;h.push(`struct shadowInfo${t}Uniforms { lightMatrix: mat4x4<f32>, depthValues: vec4<f32>, shadowsInfo: vec4<f32> };`),"pcf"===e.shadowType?h.push(`\nfn computeShadowPCF${t}(posFromLight: vec4<f32>, depthMetric: f32, darkness: f32, mapSz: f32, invMapSz: f32) -> f32 {\nlet clipSpace = posFromLight.xyz / posFromLight.w;\nlet uv = vec2<f32>(0.5 * clipSpace.x + 0.5, 0.5 - 0.5 * clipSpace.y);\nif (depthMetric < 0.0 || depthMetric > 1.0 || uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) { return 1.0; }\nlet depthRef = clamp(clipSpace.z, 0.0, 1.0);\nvar tc = uv * mapSz + 0.5;\nlet st = fract(tc);\nlet base = (floor(tc) - 0.5) * invMapSz;\nlet uvw0 = 4.0 - 3.0 * st;\nlet uvw1 = vec2<f32>(7.0);\nlet uvw2 = 1.0 + 3.0 * st;\nlet u = vec3<f32>((3.0 - 2.0 * st.x) / uvw0.x - 2.0, (3.0 + st.x) / uvw1.x, st.x / uvw2.x + 2.0) * invMapSz;\nlet v = vec3<f32>((3.0 - 2.0 * st.y) / uvw0.y - 2.0, (3.0 + st.y) / uvw1.y, st.y / uvw2.y + 2.0) * invMapSz;\nvar sh = 0.0;\nsh += uvw0.x * uvw0.y * textureSampleCompareLevel(shadowTex${t}, shadowComp${t}, base + vec2<f32>(u[0], v[0]), depthRef);\nsh += uvw1.x * uvw0.y * textureSampleCompareLevel(shadowTex${t}, shadowComp${t}, base + vec2<f32>(u[1], v[0]), depthRef);\nsh += uvw2.x * uvw0.y * textureSampleCompareLevel(shadowTex${t}, shadowComp${t}, base + vec2<f32>(u[2], v[0]), depthRef);\nsh += uvw0.x * uvw1.y * textureSampleCompareLevel(shadowTex${t}, shadowComp${t}, base + vec2<f32>(u[0], v[1]), depthRef);\nsh += uvw1.x * uvw1.y * textureSampleCompareLevel(shadowTex${t}, shadowComp${t}, base + vec2<f32>(u[1], v[1]), depthRef);\nsh += uvw2.x * uvw1.y * textureSampleCompareLevel(shadowTex${t}, shadowComp${t}, base + vec2<f32>(u[2], v[1]), depthRef);\nsh += uvw0.x * uvw2.y * textureSampleCompareLevel(shadowTex${t}, shadowComp${t}, base + vec2<f32>(u[0], v[2]), depthRef);\nsh += uvw1.x * uvw2.y * textureSampleCompareLevel(shadowTex${t}, shadowComp${t}, base + vec2<f32>(u[1], v[2]), depthRef);\nsh += uvw2.x * uvw2.y * textureSampleCompareLevel(shadowTex${t}, shadowComp${t}, base + vec2<f32>(u[2], v[2]), depthRef);\nsh /= 144.0;\nreturn mix(darkness, 1.0, sh);\n}`):h.push(`\nfn computeFallOff${t}(value: f32, clipSpace: vec2<f32>, frustumEdgeFalloff: f32) -> f32 {\nlet mask = smoothstep(1.0 - frustumEdgeFalloff, 1.00000012, clamp(dot(clipSpace, clipSpace), 0.0, 1.0));\nreturn mix(value, 1.0, mask);\n}\nfn computeShadowESM${t}(posFromLight: vec4<f32>, depthMetric: f32, darkness: f32, depthScale: f32, frustumEdgeFalloff: f32) -> f32 {\nlet clipSpace = posFromLight.xyz / posFromLight.w;\nlet uv = vec2<f32>(0.5 * clipSpace.x + 0.5, 0.5 - 0.5 * clipSpace.y);\nif (depthMetric < 0.0 || depthMetric > 1.0 || uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) { return 1.0; }\nlet shadowPixelDepth = clamp(depthMetric, 0.0, 1.0);\nlet shadowMapSample = textureSampleLevel(shadowTex${t}, shadowSamp${t}, uv, 0.0).x;\nlet esm = 1.0 - clamp(exp(min(87.0, depthScale * shadowPixelDepth)) * shadowMapSample, 0.0, 1.0 - darkness);\nreturn computeFallOff${t}(esm, clipSpace.xy, frustumEdgeFalloff);\n}`)}const n=[];for(const e of t){const t=`_${e.lightIndex}`;n.push(`struct shadowInfo${t}Uniforms { lightMatrix: mat4x4<f32>, depthValues: vec4<f32>, shadowsInfo: vec4<f32> };`)}return{_id:e,_varyings:o,_bindings:s,_helperFunctions:h.join("\n"),_vertexHelperFunctions:n.join("\n"),_vertexSlots:{VB:a.join("\n")},_fragmentSlots:{AD:p.join("\n")}}}("pbr-shadow",e),s=o._fragmentSlots?.AD;return{...o,_fragmentSlots:s?{AS:s}:void 0}}export{e as createPbrShadowFragment}; //# sourceMappingURL=pbr-shadow-fragment-CQQ_1Kow.esm.min.js.map