ShadowHighlight.glsl.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.25/esri/copyright.txt for details.
  4. */
  5. import{isSome as e}from"../core/maybe.js";import{a as o,v as r}from"./mat4.js";import{c as i}from"./mat4f64.js";import{s as t}from"./vec2.js";import{Z as a,a as l}from"./vec2f64.js";import{n as s,m as h}from"./vec3.js";import{c as d}from"./vec3f64.js";import{ScreenSpacePass as n}from"../views/3d/webgl-engine/core/shaderLibrary/ScreenSpacePass.js";import{unoccludedHighlightFlag as c}from"../views/3d/webgl-engine/core/shaderLibrary/output/OutputHighlight.glsl.js";import{ReadLinearDepth as p}from"../views/3d/webgl-engine/core/shaderLibrary/output/ReadLinearDepth.glsl.js";import{ReadShadowMapDraw as g}from"../views/3d/webgl-engine/core/shaderLibrary/shading/ReadShadowMap.glsl.js";import{CameraSpace as m}from"../views/3d/webgl-engine/core/shaderLibrary/util/CameraSpace.glsl.js";import{RgbaFloatEncoding as f}from"../views/3d/webgl-engine/core/shaderLibrary/util/RgbaFloatEncoding.glsl.js";import{Float2PassUniform as v}from"../views/3d/webgl-engine/core/shaderModules/Float2PassUniform.js";import{Float3PassUniform as u}from"../views/3d/webgl-engine/core/shaderModules/Float3PassUniform.js";import{Float4PassUniform as w}from"../views/3d/webgl-engine/core/shaderModules/Float4PassUniform.js";import{FloatPassUniform as x}from"../views/3d/webgl-engine/core/shaderModules/FloatPassUniform.js";import{glsl as P}from"../views/3d/webgl-engine/core/shaderModules/interfaces.js";import{Matrix4PassUniform as D}from"../views/3d/webgl-engine/core/shaderModules/Matrix4PassUniform.js";import{ShaderBuilder as b}from"../views/3d/webgl-engine/core/shaderModules/ShaderBuilder.js";import{Texture2DPassUniform as M}from"../views/3d/webgl-engine/core/shaderModules/Texture2DPassUniform.js";import{SnapshotSlot as F}from"../views/3d/webgl-engine/lib/ShadowMap.js";const S={highlightedThreshold:.99999,selfShadowThreshold:.025};function y(i){const l=new b;l.include(g,i);const d=l.fragment;return d.include(f),d.include(p),l.include(m),l.include(n),d.uniforms.add([new M("defaultDepthTex",((e,o)=>o.shadowMap.getSnapshot(F.Default))),new M("highlightDepthTex",((e,o)=>o.shadowMap.getSnapshot(F.Highlight))),new M("depthMap",((e,o)=>o.linearDepthTexture)),new M("highlightMap",((e,o)=>o.highlightColorTexture)),new w("uColor",(e=>e.shadowColor)),new v("nearFar",((e,o)=>o.camera.nearFar)),new x("opacity",(e=>e.shadowOpacity)),new x("occludedOpacity",(e=>e.occludedShadowOpacity)),new x("terminationFactor",(e=>e.opacityElevation*e.dayNightTerminator)),new u("lightingMainDirectionView",((e,o)=>s(T,h(T,o.lighting.mainLight.direction,o.camera.viewInverseTransposeMatrix)))),new v("texelSize",((o,r)=>e(r.linearDepthTexture)?t(j,1/r.linearDepthTexture.descriptor.width,1/r.linearDepthTexture.descriptor.height):a)),new D("inverseViewMatrix",((e,i)=>o(z,r(z,i.camera.viewMatrix,i.camera.center))))]),d.constants.add("unoccludedHighlightFlag","vec4",c).add("highlightedThreshold","float",S.highlightedThreshold).add("selfShadowThreshold","float",S.selfShadowThreshold),d.code.add(P`vec3 normalFromDepth(vec3 pixelPos, vec2 fragCoord, vec2 uv, vec2 texelSize, sampler2D depthMap, vec2 nearFar) {
  6. float leftPixelDepth = linearDepthFromTexture(depthMap, uv + vec2(-1.0, 0.0) * texelSize, nearFar);
  7. float rightPixelDepth = linearDepthFromTexture(depthMap, uv + vec2(1.0, 0.0) * texelSize, nearFar);
  8. float bottomPixelDepth = linearDepthFromTexture(depthMap, uv + vec2(0.0, -1.0) * texelSize, nearFar);
  9. float topPixelDepth = linearDepthFromTexture(depthMap, uv + vec2(0.0, 1.0) * texelSize, nearFar);
  10. bool pickLeft = abs(pixelPos.z - leftPixelDepth) < abs(pixelPos.z - rightPixelDepth);
  11. bool pickBottom = abs(pixelPos.z - bottomPixelDepth) < abs(pixelPos.z - topPixelDepth);
  12. vec3 fragCoordHorizontal = pickLeft
  13. ? vec3(fragCoord + vec2(-1.0, 0.0), leftPixelDepth)
  14. : vec3(fragCoord + vec2(1.0, 0.0), rightPixelDepth);
  15. vec3 fragCoordVertical = pickBottom
  16. ? vec3(fragCoord + vec2(0.0, -1.0), bottomPixelDepth)
  17. : vec3(fragCoord + vec2(0.0, 1.0), topPixelDepth);
  18. vec3 verticalPixelPos = reconstructPosition(fragCoordHorizontal.xy, fragCoordHorizontal.z);
  19. vec3 horizontalPixelPos = reconstructPosition(fragCoordVertical.xy, fragCoordVertical.z);
  20. vec3 normal = normalize(cross(verticalPixelPos - pixelPos, horizontalPixelPos - pixelPos));
  21. return pickLeft == pickBottom ? normal : -normal;
  22. }`),d.code.add(P`void main(void) {
  23. vec4 highlightInfo = texture2D(highlightMap, uv);
  24. float visiblyHighlighted = (1.0 - clamp(distance(unoccludedHighlightFlag, highlightInfo), 0.0, 1.0)) * highlightInfo.a;
  25. if (visiblyHighlighted > highlightedThreshold) {
  26. discard;
  27. }
  28. float depth = rgba2float(texture2D(depthMap, uv));
  29. if (depth == 0.0) {
  30. discard;
  31. }
  32. float currentPixelDepth = linearDepthFromFloat(depth, nearFar);
  33. if (-currentPixelDepth>nearFar.y || -currentPixelDepth<nearFar.x) {
  34. discard;
  35. }
  36. vec4 currentPixelPos = vec4(reconstructPosition(gl_FragCoord.xy, currentPixelDepth), 1.0);
  37. vec4 worldSpacePos = inverseViewMatrix * currentPixelPos;
  38. mat4 shadowMatrix;
  39. float linearDepth = -currentPixelDepth;
  40. int i = chooseCascade(linearDepth, shadowMatrix);
  41. if (i >= numCascades) {
  42. discard;
  43. }
  44. vec3 lvpos = lightSpacePosition(worldSpacePos.xyz, shadowMatrix);
  45. if (lvpos.z >= 1.0 || lvpos.x < 0.0 || lvpos.x > 1.0 || lvpos.y < 0.0 || lvpos.y > 1.0) {
  46. discard;
  47. }
  48. vec2 uvShadow = cascadeCoordinates(i, lvpos);
  49. float depthHighlight = readShadowMapDepth(uvShadow, highlightDepthTex);
  50. bool shadowHighlight = depthHighlight < lvpos.z;
  51. if (!shadowHighlight) {
  52. discard;
  53. }
  54. float depthDefault = readShadowMapDepth(uvShadow, defaultDepthTex);
  55. bool shadowDefault = depthDefault < lvpos.z;
  56. vec3 normal = normalFromDepth(currentPixelPos.xyz, gl_FragCoord.xy, uv, texelSize, depthMap, nearFar);
  57. bool shaded = dot(normal, lightingMainDirectionView) < selfShadowThreshold;
  58. float fragOpacity = (shadowDefault || shaded) ? occludedOpacity : opacity;
  59. gl_FragColor = vec4(uColor.rgb, uColor.a * fragOpacity * terminationFactor);
  60. }`),l}const z=i(),T=d(),j=l(),C=Object.freeze(Object.defineProperty({__proto__:null,build:y},Symbol.toStringTag,{value:"Module"}));export{C as S,y as b};