/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{normalize as e,transformMat4 as i}from"../core/libs/gl-matrix-2/math/vec3.js";import{create as r}from"../core/libs/gl-matrix-2/factories/vec3f64.js";import{NormalFromDepth as o}from"../views/3d/webgl-engine/core/shaderLibrary/NormalFromDepth.glsl.js";import{ScreenSpacePass as t}from"../views/3d/webgl-engine/core/shaderLibrary/ScreenSpacePass.glsl.js";import{calculateUVZShadowFromDepthPass as a}from"../views/3d/webgl-engine/core/shaderLibrary/shading/calculateUVZShadowFromDepth.glsl.js";import{ShadowmapFiltering as l}from"../views/3d/webgl-engine/core/shaderLibrary/shading/ShadowmapFiltering.glsl.js";import{Float3BindUniform as h}from"../views/3d/webgl-engine/core/shaderModules/Float3BindUniform.js";import{Float4PassUniform as g}from"../views/3d/webgl-engine/core/shaderModules/Float4PassUniform.js";import{FloatPassUniform as n}from"../views/3d/webgl-engine/core/shaderModules/FloatPassUniform.js";import{glsl as d}from"../views/3d/webgl-engine/core/shaderModules/glsl.js";import{Texture2DBindUniform as s}from"../views/3d/webgl-engine/core/shaderModules/Texture2DBindUniform.js";import{Texture2DShadowBindUniform as c}from"../views/3d/webgl-engine/core/shaderModules/Texture2DShadowBindUniform.js";import{Texture2DUintPassUniform as u}from"../views/3d/webgl-engine/core/shaderModules/Texture2DUintPassUniform.js";import{ShaderBuilder as m}from"../views/webgl/ShaderBuilder.js";const w=.025;function p(r){const p=new m;p.include(a),p.fragment.include(l),p.include(t),p.include(o);const v=p.fragment;return v.uniforms.add(new c("shadowMapExcludingHighlight",({shadowMap:e})=>e.getOutput(7)),new s("shadowHighlight",({shadowHighlight:e})=>e?.getTexture()),new s("depthMap",e=>e.mainDepth),new u("highlightTexture",e=>e.highlightTexture),new g("uColor",e=>e.shadowColor),new n("opacity",e=>e.shadowOpacity),new n("occludedOpacity",e=>e.occludedShadowOpacity),new n("terminationFactor",e=>e.opacityElevation*e.dayNightTerminator),new h("lightingMainDirectionView",({lighting:r,camera:o})=>e(f,i(f,r.mainLight.direction,o.viewInverseTransposeMatrix)))),v.main.add(d`
    ivec2 highlightTextureSize = textureSize(highlightTexture, 0);
    ivec2 highlightIUV = ivec2(uv * vec2(highlightTextureSize));
    uvec2 highlightInfo = texelFetch(highlightTexture, highlightIUV, 0).rg;

    fragColor = vec4(0.0);

    // Calculate bit mask to check if pixel is highlit unoccluded at any level
    uint ored = (highlightInfo.r << 0) | (highlightInfo.g << 8);

    bool visiblyHighlighted = ((ored & ~(ored >> 1)) & (1u+4u+16u+64u)) != 0u;
    if (visiblyHighlighted) {
      return;
    }

    // shadowHighlight is rendered as a full-resolution screen-space buffer, so the current
    // framebuffer pixel maps directly to the corresponding texel in the screen-space highlight texture.
    float shadowHighlightFactor = texelFetch(shadowHighlight, ivec2(gl_FragCoord.xy), 0).r;
    if (shadowHighlightFactor == 0.0) {
      return;
    }

    vec4 currentPixelPos;
    vec3 uvzShadow = calculateUVZShadowAndPixelPosFromDepth(
      uv,
      textureSize(shadowMapExcludingHighlight, 0),
      depthMap,
      currentPixelPos
    );
    if (uvzShadow.z < 0.0) {
      return;
    }

    float shadowExcludingHighlightFactor = readShadowMapUVZ(uvzShadow, shadowMapExcludingHighlight);

    vec3 normal = normalFromDepth(depthMap, currentPixelPos.xyz, gl_FragCoord.xy, uv);
    bool shaded = dot(normal, lightingMainDirectionView) < ${d.float(w)};

    float occludedFactor = max(shadowExcludingHighlightFactor, shaded ? 1.0 : 0.0);
    float fragOpacity = mix(opacity, occludedOpacity, occludedFactor);
    fragColor = vec4(uColor.rgb, uColor.a * fragOpacity * terminationFactor);
  `),p}const f=r(),v=Object.freeze(Object.defineProperty({__proto__:null,build:p},Symbol.toStringTag,{value:"Module"}));export{v as S,p as b};
