/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../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/Transform.glsl","../views/3d/webgl-engine/core/shaderLibrary/output/OutputHighlight.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/EvaluateAmbientLighting.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/MainLighting.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/MultipassTerrainTest.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/NormalUtils.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/PhysicallyBasedRenderingParameters.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/ReadShadowMap.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/Water.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/WaterDistortion.glsl","../views/3d/webgl-engine/core/shaderLibrary/util/AlphaCutoff","../views/3d/webgl-engine/core/shaderLibrary/util/ColorConversion.glsl","../views/3d/webgl-engine/core/shaderLibrary/util/View.glsl","../views/3d/webgl-engine/core/shaderModules/Float3PassUniform","../views/3d/webgl-engine/core/shaderModules/Float4PassUniform","../views/3d/webgl-engine/core/shaderModules/Float4Uniform","../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/lib/basicInterfaces","../views/3d/webgl-engine/lib/VertexAttribute"],(function(e,i,a,r,o,l,t,n,s,d,g,v,c,u,h,p,w,m,b,f,y,C,F,S,M,P){"use strict";function L(e){const L=new S.ShaderBuilder,{vertex:D,fragment:_}=L,x=w.addProjViewLocalOrigin(L,e);return L.include(o.Transform),L.attributes.add(P.VertexAttribute.POSITION,"vec3"),L.attributes.add(P.VertexAttribute.UV0,"vec2"),D.uniforms.add(new b.Float4PassUniform("waterColor",(e=>e.color))),e.output!==a.ShaderOutput.Color&&e.output!==a.ShaderOutput.Alpha||(L.include(d.NormalUtils,e),L.include(i.ForwardLinearDepth,e),L.varyings.add("vuv","vec2"),L.varyings.add("vpos","vec3"),L.varyings.add("vnormal","vec3"),L.varyings.add("vtbnMatrix","mat3"),_.uniforms.add(x),e.hasMultipassTerrain&&L.varyings.add("depth","float"),D.code.add(C.glsl`
      void main(void) {
        if (waterColor.a < ${C.glsl.float(h.symbolAlphaCutoff)}) {
          // Discard this vertex
          gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
          return;
        }

        vuv = uv0;
        vpos = position;

        vnormal = getLocalUp(vpos, localOrigin);
        vtbnMatrix = getTBNMatrix(vnormal);

        ${e.hasMultipassTerrain?"depth = (view * vec4(vpos, 1.0)).z;":""}

        gl_Position = transformPosition(proj, view, vpos);
        ${e.output===a.ShaderOutput.Color?"forwardLinearDepth();":""}
      }
    `)),L.include(s.multipassTerrainTest,e),e.output===a.ShaderOutput.Alpha&&(L.include(r.SliceDraw,e),_.uniforms.add(new f.Float4Uniform("waterColor")),_.code.add(C.glsl`
        void main() {
          discardBySlice(vpos);
          ${e.hasMultipassTerrain?"terrainDepthTest(gl_FragCoord, depth);":""}

          gl_FragColor = vec4(waterColor.a);
        }
      `)),e.output===a.ShaderOutput.Color&&(L.include(n.MainLighting,{isGround:!1}),L.include(t.EvaluateAmbientLighting,{pbrMode:g.PBRMode.Disabled,lightingSphericalHarmonicsOrder:2}),L.include(u.WaterDistortion),L.include(r.SliceDraw,e),L.include(v.ReadShadowMapDraw,e),L.include(c.Water,e),_.uniforms.add([new f.Float4Uniform("waterColor"),new m.Float3PassUniform("lightingMainDirection",((e,i)=>i.lighting.lightingMainDirection)),new m.Float3PassUniform("lightingMainIntensity",((e,i)=>i.lighting.mainLight.intensity)),new y.FloatPassUniform("timeElapsed",(e=>e.timeElapsed)),new F.Matrix4Uniform("view")]),w.addCameraPosition(_,e),_.include(p.ColorConversion),_.code.add(C.glsl`
      void main() {
        discardBySlice(vpos);
        ${e.hasMultipassTerrain?"terrainDepthTest(gl_FragCoord, depth);":""}
        vec3 localUp = vnormal;
        // the created normal is in tangent space
        vec4 tangentNormalFoam = getSurfaceNormalAndFoam(vuv, timeElapsed);

        // we rotate the normal according to the tangent-bitangent-normal-Matrix
        vec3 n = normalize(vtbnMatrix * tangentNormalFoam.xyz);
        vec3 v = -normalize(vpos - cameraPosition);
        float shadow = ${e.receiveShadows?C.glsl`1.0 - readShadowMap(vpos, linearDepth)`:"1.0"};
        vec4 vPosView = view*vec4(vpos, 1.0);
        vec4 final = vec4(getSeaColor(n, v, lightingMainDirection, waterColor.rgb, lightingMainIntensity, localUp, shadow, tangentNormalFoam.w, vPosView.xyz, vpos + localOrigin), waterColor.w);

        // gamma correction
        gl_FragColor = delinearizeGamma(final);
        gl_FragColor = highlightSlice(gl_FragColor, vpos);
        ${e.transparencyPassType===M.TransparencyPassType.Color?"gl_FragColor = premultiplyAlpha(gl_FragColor);":""}
      }
    `)),e.output===a.ShaderOutput.Normal&&(L.include(d.NormalUtils,e),L.include(u.WaterDistortion,e),L.include(r.SliceDraw,e),L.varyings.add("vpos","vec3"),L.varyings.add("vuv","vec2"),D.code.add(C.glsl`
        void main(void) {
          if (waterColor.a < ${C.glsl.float(h.symbolAlphaCutoff)}) {
            // Discard this vertex
            gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
            return;
          }

          vuv = uv0;
          vpos = position;

          gl_Position = transformPosition(proj, view, vpos);
        }
    `),_.uniforms.add(new y.FloatPassUniform("timeElapsed",(e=>e.timeElapsed))),_.code.add(C.glsl`void main() {
discardBySlice(vpos);
vec4 tangentNormalFoam = getSurfaceNormalAndFoam(vuv, timeElapsed);
tangentNormalFoam.xyz = normalize(tangentNormalFoam.xyz);
gl_FragColor = vec4((tangentNormalFoam.xyz + vec3(1.0)) * 0.5, tangentNormalFoam.w);
}`)),e.output===a.ShaderOutput.Draped&&(L.varyings.add("vpos","vec3"),D.code.add(C.glsl`
        void main(void) {
          if (waterColor.a < ${C.glsl.float(h.symbolAlphaCutoff)}) {
            // Discard this vertex
            gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
            return;
          }

          vpos = position;
          gl_Position = transformPosition(proj, view, vpos);
        }
    `),_.uniforms.add(new f.Float4Uniform("waterColor")),_.code.add(C.glsl`void main() {
gl_FragColor = waterColor;
}`)),e.output===a.ShaderOutput.Highlight&&(L.include(l.OutputHighlight),L.varyings.add("vpos","vec3"),D.code.add(C.glsl`
      void main(void) {
        if (waterColor.a < ${C.glsl.float(h.symbolAlphaCutoff)}) {
          // Discard this vertex
          gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
          return;
        }

        vpos = position;
        gl_Position = transformPosition(proj, view, vpos);
      }
    `),L.include(r.SliceDraw,e),_.code.add(C.glsl`void main() {
discardBySlice(vpos);
outputHighlight();
}`)),L}const D=Object.freeze(Object.defineProperty({__proto__:null,build:L},Symbol.toStringTag,{value:"Module"}));e.WaterSurface=D,e.build=L}));
