/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{ScreenSpacePass as e}from"../views/3d/webgl-engine/core/shaderLibrary/ScreenSpacePass.glsl.js";import{If as r,glsl as o}from"../views/3d/webgl-engine/core/shaderModules/glsl.js";import{Texture2DPassUniform as s}from"../views/3d/webgl-engine/core/shaderModules/Texture2DPassUniform.js";import{O as t}from"./OITDim.glsl.js";import{ShaderBuilder as a}from"../views/webgl/ShaderBuilder.js";class n extends t{}function i(t){const n=new a;n.include(e);const{hasEmitters:i,frontFaceMask:c,computeHudOcclusion:u}=t,l=n.fragment;l.uniforms.add(new s("colorTexture",e=>e.colorTexture),new s("alphaTexture",e=>e.alphaTexture));let m=0;return n.outputs.add("fragColor","vec4",m++),i&&n.outputs.add("fragEmission","vec4",m++),u&&n.outputs.add("fragOcclusion","vec4",m++),l.uniforms.add(new s("frontFaceTexture",e=>e.frontFaceTexture)),i&&(l.uniforms.add(new s("emissionTexture",e=>e.emissionTexture)),l.uniforms.add(new s("emissionFrontFaceTexture",e=>e.emissionFrontFaceTexture))),l.main.add(o`
    float srcAlpha = texture(alphaTexture, uv).r;
    ${r(u,"fragOcclusion = vec4(1.0, 0.0, 0.0, 1.0);")}

    if(srcAlpha == 0.0) {
      fragColor = vec4(0.0);
      ${r(i,"fragEmission = vec4(0.0);")}
      return;
    }
    vec4 frontFace = texture(frontFaceTexture, uv);

    ${r(c,o`
    if (frontFace.a == 0.0) {
      fragColor = vec4(0.0);
      ${r(i,"fragEmission = vec4(0.0);")}
      return;
    }`)}

    vec4 srcColor = texture(colorTexture, uv);
    fragColor = vec4(mix(srcColor.rgb / srcAlpha, frontFace.rgb, frontFace.a), 1.0 - srcColor.a);

    ${r(i,"vec4 emission = texture(emissionTexture, uv);\n       vec4 emissionFrontFace = texture(emissionFrontFaceTexture, uv);\n\n       // Modulate transparent emitter by front faces. This case is important for surfaces which contain emitter and\n       // non-emitter at the same time. Non-emitter surface parts need to modulate emissions as well.\n       emission.rgb = emission.rgb * (1.0 - frontFace.a);\n\n       fragEmission = vec4(mix(emission.rgb / srcAlpha, emissionFrontFace.rgb, emissionFrontFace.a), 1.0 - srcColor.a);\n      ")}

    ${r(u,"fragOcclusion.r = pow(srcColor.a, 0.5);")}
  `),n}const c=Object.freeze(Object.defineProperty({__proto__:null,OITBlendPassParameters:n,build:i},Symbol.toStringTag,{value:"Module"}));export{n as O,c as a,i as b};
