/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../geometry/support/Ellipsoid","../views/3d/terrain/interfaces","../views/3d/webgl-engine/collections/Component/Material/ComponentTechniqueConfiguration","../views/3d/webgl-engine/collections/Component/Material/shader/ComponentData.glsl","../views/3d/webgl-engine/collections/Component/Material/shader/VertexDiscardByOpacity.glsl","../views/3d/webgl-engine/core/shaderLibrary/ForwardLinearDepth.glsl","../views/3d/webgl-engine/core/shaderLibrary/ShaderOutputOptions","../views/3d/webgl-engine/core/shaderLibrary/Slice.glsl","../views/3d/webgl-engine/core/shaderLibrary/attributes/NormalAttribute.glsl","../views/3d/webgl-engine/core/shaderLibrary/attributes/TextureCoordinateAttribute.glsl","../views/3d/webgl-engine/core/shaderLibrary/attributes/VertexColor.glsl","../views/3d/webgl-engine/core/shaderLibrary/attributes/VertexNormal.glsl","../views/3d/webgl-engine/core/shaderLibrary/attributes/VertexPosition.glsl","../views/3d/webgl-engine/core/shaderLibrary/output/OutputDepth.glsl","../views/3d/webgl-engine/core/shaderLibrary/output/OutputHighlight.glsl","../views/3d/webgl-engine/core/shaderLibrary/output/ReadLinearDepth.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/ComputeMaterialColor.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/ComputeNormalTexture.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/ComputeShadingNormal.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/EvaluateSceneLighting.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/MultipassTerrainTest.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/PhysicallyBasedRenderingParameters.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/ReadBaseColorTexture.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/ReadShadowMap.glsl","../views/3d/webgl-engine/core/shaderLibrary/terrain/Overlay.glsl","../views/3d/webgl-engine/core/shaderLibrary/util/AlphaCutoff","../views/3d/webgl-engine/core/shaderLibrary/util/AlphaDiscard.glsl","../views/3d/webgl-engine/core/shaderLibrary/util/EllipsoidMode","../views/3d/webgl-engine/core/shaderModules/Float3PassUniform","../views/3d/webgl-engine/core/shaderModules/FloatPassUniform","../views/3d/webgl-engine/core/shaderModules/interfaces","../views/3d/webgl-engine/core/shaderModules/Matrix4Uniform","../views/3d/webgl-engine/core/shaderModules/ShaderBuilder","../views/3d/webgl-engine/core/shaderModules/Texture2DPassUniform","../views/3d/webgl-engine/lib/basicInterfaces","../views/3d/webgl-engine/lib/VertexAttribute"],(function(e,r,o,a,l,t,i,d,n,s,g,c,u,v,h,m,C,p,w,b,x,y,M,f,T,O,S,L,N,P,A,R,D,B,W,V,E){"use strict";const _=new Map([[E.VertexAttribute.POSITION,0],[E.VertexAttribute.NORMAL,1],[E.VertexAttribute.NORMALCOMPRESSED,1],[E.VertexAttribute.COLOR,2],[E.VertexAttribute.UV0,3],[E.VertexAttribute.UVREGION,4],[E.VertexAttribute.COMPONENTINDEX,5]]);function F(e){const o=new B.ShaderBuilder;o.include(v.VertexPosition,e),o.include(u.VertexNormal,e),o.include(c.VertexColor,e),o.include(g.TextureCoordinateAttribute,e),o.include(i.ForwardLinearDepth,e),o.include(l.ComponentData,e),o.include(L.DiscardOrAdjustAlphaDraw,e),o.include(n.SlicePass,e),o.include(f.ReadBaseColorTexture,e),o.include(t.VertexDiscardByOpacity,e);const{vertex:E,fragment:_}=o;_.uniforms.add(new D.Matrix4Uniform("view")),e.pbrMode!==M.PBRMode.Normal&&e.pbrMode!==M.PBRMode.Schematic||(o.include(M.PhysicallyBasedRenderingParameters,e),e.hasNormalTexture&&o.include(w.ComputeNormalTexture,e)),e.output===d.ShaderOutput.Shadow&&e.componentData===l.ComponentDataType.Varying?E.code.add(R.glsl`#define discardShadows(castShadows) { if(!castShadows) { gl_Position = vec4(1e38, 1e38, 1e38, 1.0); return; } }`):E.code.add(R.glsl`#define discardShadows(castShadows) {}`);const F=e.integratedMeshMode===a.IntegratedMeshMode.ColorOverlay||e.integratedMeshMode===a.IntegratedMeshMode.ColorOverlayWithWater,z=F&&e.output===d.ShaderOutput.Color&&e.pbrMode===M.PBRMode.WaterOnIntegratedMesh;return F&&(o.include(x.EvaluateSceneLighting,e),o.include(O.OverlayIM,e),e.spherical?E.code.add(R.glsl`
      const float invEllipsoidRadius = ${R.glsl.float(1/(e.ellipsoidMode===N.EllipsoidMode.Earth?r.earth.radius:e.ellipsoidMode===N.EllipsoidMode.Mars?r.mars.radius:r.moon.radius))};
      vec2 projectOverlay(vec3 pos) {
        return pos.xy / (1.0 + invEllipsoidRadius * pos.z);
      }
      `):E.code.add(R.glsl`vec2 projectOverlay(vec3 pos) { return pos.xy; }`)),z&&(o.varyings.add("tbnTangent","vec3"),o.varyings.add("tbnBiTangent","vec3"),o.varyings.add("groundNormal","vec3")),E.code.add(R.glsl`
    void main() {
      bool castShadows;
      vec4 externalColor = forwardExternalColor(castShadows);
      discardShadows(castShadows);

      vertexDiscardByOpacity(externalColor.a);

      if (externalColor.a < ${R.glsl.float(S.symbolAlphaCutoff)}) {
        // Discard this vertex
        gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
        return;
      }

      forwardPosition(readElevationOffset());
      forwardNormal();
      forwardTextureCoordinates();
      forwardVertexColor();
      forwardLinearDepth();
      ${z?e.spherical?R.glsl`
                groundNormal = normalize(positionWorld());
                tbnTangent = normalize(cross(vec3(0.0, 0.0, 1.0), groundNormal));
                tbnBiTangent = normalize(cross(groundNormal, tbnTangent));`:R.glsl`
                groundNormal = vec3(0.0, 0.0, 1.0);
                tbnTangent = vec3(1.0, 0.0, 0.0);
                tbnBiTangent = vec3(0.0, 1.0, 0.0);`:""}
      ${F?R.glsl`setOverlayVTC(projectOverlay(position));`:""}
    }
  `),e.output===d.ShaderOutput.Alpha&&(_.include(C.ReadLinearDepth),o.include(y.multipassTerrainTest,e),o.include(p.ComputeMaterialColor,e),F&&_.uniforms.add(new W.Texture2DPassUniform("ovColorTex",((e,r)=>O.getColorTexture(e,r)))),_.code.add(R.glsl`
      void main() {
        discardBySlice(vPositionWorldCameraRelative);
        ${e.hasMultipassTerrain?R.glsl`terrainDepthTest(gl_FragCoord, vPosition_view.z);`:""}

        vec4 textureColor = readBaseColorTexture();
        discardOrAdjustAlpha(textureColor);

        vec4 externalColor;
        int externalColorMixMode;
        readExternalColor(externalColor, externalColorMixMode);

        vec4 materialColor = computeMaterialColor(
          textureColor,
          externalColor,
          externalColorMixMode
        );
        ${F?R.glsl`
                vec4 overlayColor = getOverlayColor(ovColorTex, vtcOverlay);
                materialColor = materialColor * (1.0 - overlayColor.a) + overlayColor;`:""}

        gl_FragColor = vec4(materialColor.a);
      }
    `)),e.output===d.ShaderOutput.Color&&(_.include(C.ReadLinearDepth),o.include(y.multipassTerrainTest,e),o.include(p.ComputeMaterialColor,e),o.include(b.ComputeShadingNormal,e),o.include(x.EvaluateSceneLighting,e),e.receiveShadows?(o.include(T.ReadShadowMapPass,e),_.code.add(R.glsl`float evaluateShadow() {
return readShadowMap(vPositionWorldCameraRelative, linearDepth);
}`)):_.code.add(R.glsl`float evaluateShadow() { return 0.0; }`),F&&_.uniforms.add(new W.Texture2DPassUniform("ovColorTex",((e,r)=>O.getColorTexture(e,r)))),_.code.add(R.glsl`
      void main() {
        discardBySlice(vPositionWorldCameraRelative);
        ${e.hasMultipassTerrain?R.glsl`terrainDepthTest(gl_FragCoord, vPosition_view.z);`:""}

        vec4 textureColor = readBaseColorTexture();
        discardOrAdjustAlpha(textureColor);

        vec4 externalColor;
        int externalColorMixMode;
        readExternalColor(externalColor, externalColorMixMode);

        vec4 materialColor = computeMaterialColor(
          textureColor,
          externalColor,
          externalColorMixMode
        );
        ${F?R.glsl`vec4 overlayColor = getOverlayColor(ovColorTex, vtcOverlay);`:""}
    `),e.pbrMode===M.PBRMode.Normal||e.pbrMode===M.PBRMode.Schematic?(_.uniforms.add(new P.Float3PassUniform("lightingMainIntensity",((e,r)=>r.lighting.mainLight.intensity))),_.code.add(R.glsl`
        ${e.pbrMode===M.PBRMode.Normal?R.glsl`
                applyPBRFactors();
                if (int(externalColorMixMode) == 3) {
                  mrr = vec3(0.0, 0.6, 0.2);
                }`:""}
        vec3 normalVertex = shadingNormalWorld();
        float additionalIrradiance = 0.02 * lightingMainIntensity[2];
      `),e.hasNormalTexture?_.code.add(R.glsl`mat3 tangentSpace = computeTangentSpace(normalVertex, vPositionWorldCameraRelative, vuv0);
vec3 shadingNormal = computeTextureNormal(tangentSpace, vuv0);`):_.code.add(R.glsl`vec3 shadingNormal = normalVertex;`),_.code.add(R.glsl`${e.spherical?R.glsl`vec3 normalGround = normalize(positionWorld());`:R.glsl`vec3 normalGround = vec3(0.0, 0.0, 1.0);`}
      `),_.code.add(R.glsl`
        vec3 viewDir = normalize(vPositionWorldCameraRelative);
        float ssao = 1.0 - occlusion * (1.0 - evaluateAmbientOcclusion());

        ${e.snowCover?R.glsl`
                vec3 surfaceNormal = normalize(shadingNormalWorld());
                float snow = smoothstep(0.5, 0.55, dot(surfaceNormal, normalize(positionWorld())));
                materialColor.rgb = mix(materialColor.rgb, vec3(1), snow);

                shadingNormal = mix(shadingNormal, surfaceNormal, snow);
                ssao = mix(ssao, 0.0, snow);
                mrr = mix(mrr, vec3(0.0, 1.0, 0.04), snow);
                emission = mix(emission, vec3(0.0), snow);`:""}

        ${F?R.glsl` materialColor = materialColor * (1.0 - overlayColor.a) + overlayColor;`:""}

        vec3 additionalLight = evaluateAdditionalLighting(ssao, positionWorld());
        vec4 shadedColor = vec4(evaluateSceneLightingPBR(shadingNormal, materialColor.rgb, evaluateShadow(), ssao, additionalLight, viewDir, normalGround, mrr, emission, additionalIrradiance), materialColor.a);
        `)):(e.receiveShadows?_.code.add(R.glsl`float shadow = evaluateShadow();`):e.spherical?(_.uniforms.add(new A.FloatPassUniform("lightingGlobalFactor",((e,r)=>r.lighting.globalFactor))),_.code.add(R.glsl`float additionalAmbientScale = additionalDirectedAmbientLight(positionWorld());
float shadow = lightingGlobalFactor * (1.0 - additionalAmbientScale);`)):_.code.add(R.glsl`float shadow = 0.0;`),z&&_.uniforms.add(new W.Texture2DPassUniform("ovNormalTex",((e,r)=>$(r)))),e.snowCover&&(o.extensions.add("GL_OES_standard_derivatives"),_.code.add(R.glsl`vec3 surfaceNormal = normalize(cross(dFdx(vPositionWorldCameraRelative), dFdy(vPositionWorldCameraRelative)));
float snow = smoothstep(0.5, 0.55, dot(surfaceNormal, normalize(positionWorld())));
materialColor.rgb = mix(materialColor.rgb, vec3(1), snow);`)),_.code.add(R.glsl`
        float ambientOcclusion = evaluateAmbientOcclusion();
        vec3 additionalLight = evaluateAdditionalLighting(ambientOcclusion, positionWorld());

        ${F?R.glsl` materialColor = materialColor * (1.0 - overlayColor.a) + overlayColor;`:""}

        vec4 shadedColor = vec4(evaluateSceneLighting(shadingNormalWorld(), materialColor.rgb, shadow, ambientOcclusion, additionalLight), materialColor.a);
      ${z?R.glsl`
              vec4 overlayWaterMask = getOverlayColor(ovNormalTex, vtcOverlay);
              float waterNormalLength = length(overlayWaterMask);
              if (waterNormalLength > 0.95) {
                mat3 tbnMatrix = mat3(tbnTangent, tbnBiTangent, groundNormal);
                vec4 waterColorLinear = getOverlayWaterColor(overlayWaterMask, overlayColor, -normalize(vPositionWorldCameraRelative), shadow, groundNormal, tbnMatrix, vPosition_view, positionWorld());
                vec4 waterColorNonLinear = delinearizeGamma(vec4(waterColorLinear.xyz, 1.0));
                // un-gamma the ground color to mix in linear space
                shadedColor = mix(shadedColor, waterColorNonLinear, waterColorLinear.w);
              }`:""}
      `)),_.code.add(R.glsl`
        gl_FragColor = highlightSlice(shadedColor, vPositionWorldCameraRelative);
        ${e.transparencyPassType===V.TransparencyPassType.Color?"gl_FragColor = premultiplyAlpha(gl_FragColor);":""}
      }
    `)),e.output!==d.ShaderOutput.Depth&&e.output!==d.ShaderOutput.Shadow||(o.include(h.OutputDepth,e),_.code.add(R.glsl`void main() {
discardBySlice(vPositionWorldCameraRelative);
vec4 textureColor = readBaseColorTexture();
discardOrAdjustAlpha(textureColor);
outputDepth(linearDepth);
}`)),e.output===d.ShaderOutput.Normal&&(o.include(b.ComputeShadingNormal,e),_.code.add(R.glsl`
      void main() {
        discardBySlice(vPositionWorldCameraRelative);

        vec4 textureColor = readBaseColorTexture();
        discardOrAdjustAlpha(textureColor);

        // note: the alpha component needs to be 1.0 in order for this material
        // to influence ambient occlusion, see the ssao fragment shader
        float alpha = ${e.normalType===s.NormalAttributeType.Ground?"0.0":"1.0"};
        gl_FragColor = vec4(vec3(.5) + .5 * shadingNormal_view(), alpha);
      }
    `)),e.output===d.ShaderOutput.Highlight&&(o.include(m.OutputHighlight),_.code.add(R.glsl`
      void main() {
        discardBySlice(vPositionWorldCameraRelative);

        vec4 textureColor = readBaseColorTexture();
        discardOrAdjustAlpha(textureColor);

        ${F?R.glsl`
                vec4 overlayColor = getCombinedOverlayColor();
                if (overlayColor.a == 0.0) {
                  gl_FragColor = vec4(0.0);
                  return;
                }`:""}

        outputHighlight();
      }
    `)),o}function $(e){return 0===e.overlays.length?null:e.overlays[o.OverlayIndex.INNER].getValidTexture(o.RenderTargetType.Water)}const z=Object.freeze(Object.defineProperty({__proto__:null,attributeLocations:_,build:F,getOverlayNormalTexture:$},Symbol.toStringTag,{value:"Module"}));e.ComponentShader=z,e.attributeLocations=_,e.build=F,e.getOverlayNormalTexture=$}));
