/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import e from"../core/Error.js";import{isColor as o}from"../views/3d/webgl-engine/core/shaderLibrary/ShaderOutput.js";import{SliceDraw as r}from"../views/3d/webgl-engine/core/shaderLibrary/Slice.glsl.js";import{Transform as i}from"../views/3d/webgl-engine/core/shaderLibrary/Transform.glsl.js";import{ObjectAndLayerIdColor as a}from"../views/3d/webgl-engine/core/shaderLibrary/attributes/ObjectAndLayerIdColor.glsl.js";import{OutputHighlight as t}from"../views/3d/webgl-engine/core/shaderLibrary/output/OutputHighlight.glsl.js";import{EvaluateAmbientLighting as n}from"../views/3d/webgl-engine/core/shaderLibrary/shading/EvaluateAmbientLighting.glsl.js";import{addMainLightDirection as s,addMainLightIntensity as l}from"../views/3d/webgl-engine/core/shaderLibrary/shading/MainLighting.glsl.js";import{NormalUtils as d}from"../views/3d/webgl-engine/core/shaderLibrary/shading/NormalUtils.glsl.js";import{ReadShadowMapDrawFragment as m}from"../views/3d/webgl-engine/core/shaderLibrary/shading/ReadShadowMap.glsl.js";import{WaterColor as c}from"../views/3d/webgl-engine/core/shaderLibrary/shading/WaterColor.glsl.js";import{WaterDistortion as g}from"../views/3d/webgl-engine/core/shaderLibrary/shading/WaterDistortion.glsl.js";import{ColorConversion as v}from"../views/3d/webgl-engine/core/shaderLibrary/util/ColorConversion.glsl.js";import{addProjViewLocalOrigin as p,addCameraPosition as u}from"../views/3d/webgl-engine/core/shaderLibrary/util/View.glsl.js";import{Float4PassUniform as w}from"../views/3d/webgl-engine/core/shaderModules/Float4PassUniform.js";import{FloatPassUniform as f}from"../views/3d/webgl-engine/core/shaderModules/FloatPassUniform.js";import{glsl as h}from"../views/3d/webgl-engine/core/shaderModules/glsl.js";import{outputColorHighlightOLID as b}from"../views/3d/webgl-engine/shaders/OutputColorHighlightOLID.glsl.js";import{ShaderBuilder as j}from"../views/webgl/ShaderBuilder.js";import{alphaCutoff as y}from"../webscene/support/AlphaCutoff.js";function C(C){const L=new j,{vertex:P,fragment:x,varyings:S}=L,{output:_,draped:O,receiveShadows:M}=C;p(P,C),L.include(i),L.attributes.add("position","vec3"),L.attributes.add("uv0","vec2");const D=new w("waterColor",e=>e.color);if(S.add("vpos","vec3",{invariant:!0}),P.uniforms.add(D),o(_)){if(O)return P.main.add(h`
      if (waterColor.a < ${h.float(y)}) {
        // Discard this vertex
        gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
        return;
      }

      vpos = position;
      gl_Position = transformPosition(proj, view, vpos);`),x.uniforms.add(D),x.main.add(h`fragColor = waterColor;`),L;L.include(d,C),S.add("vuv","vec2"),S.add("vnormal","vec3"),S.add("vtbnMatrix","mat3"),P.main.add(h`
      if (waterColor.a < ${h.float(y)}) {
        // Discard this vertex
        gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
        return;
      }

      vuv = uv0;
      vpos = position;

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

      gl_Position = transformPosition(proj, view, vpos);
      forwardLinearDepthToReadShadowMap();`)}switch(L.include(m,C),_){case 0:case 1:case 2:x.include(n,{pbrMode:0}),L.include(g),L.include(c,C),x.include(r,C),L.include(b,C),x.include(v),u(x,C),s(x),l(x),x.uniforms.add(D,new f("timeElapsed",({timeElapsed:e})=>e),P.uniforms.get("view"),P.uniforms.get("localOrigin")).main.add(h`
        discardBySlice(vpos);
        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 = ${M?h`1.0 - readShadowMap(vpos, linearDepth)`:"1.0"};
        vec4 vPosView = view * vec4(vpos, 1.0);
        vec4 final = vec4(getWaterColor(n, v, mainLightDirection, waterColor.rgb, mainLightIntensity, localUp, shadow, tangentNormalFoam.w, vPosView.xyz, vpos + localOrigin), waterColor.w);

        fragColor = delinearizeGamma(final);
        outputColorHighlightOLID(applySlice(fragColor, vpos), final.rgb);`);break;case 4:L.include(d,C),L.include(g,C),x.include(r,C),S.add("vuv","vec2"),P.main.add(h`
        if (waterColor.a < ${h.float(y)}) {
          // Discard this vertex
          gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
          return;
        }

        vuv = uv0;
        vpos = position;

        gl_Position = transformPosition(proj, view, vpos);`),x.uniforms.add(new f("timeElapsed",({timeElapsed:e})=>e)).main.add(h`discardBySlice(vpos);
vec4 tangentNormalFoam = getSurfaceNormalAndFoam(vuv, timeElapsed);
tangentNormalFoam.xyz = normalize(tangentNormalFoam.xyz);
fragColor = vec4((tangentNormalFoam.xyz + vec3(1.0)) * 0.5, tangentNormalFoam.w);`);break;case 3:P.main.add(h`
        if (waterColor.a < ${h.float(y)}) {
          // Discard this vertex
          gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
          return;
        }

        vpos = position;
        gl_Position = transformPosition(proj, view, vpos);`),x.include(r,C),x.main.add("discardBySlice(vpos);");break;case 10:L.include(t,C),P.main.add(h`
        if (waterColor.a < ${h.float(y)}) {
          // Discard this vertex
          gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
          return;
        }

        vpos = position;
        gl_Position = transformPosition(proj, view, vpos);`),x.include(r,C),x.main.add(h`discardBySlice(vpos);
calculateOcclusionAndOutputHighlight();`);break;case 11:L.include(a,C),P.main.add(h`
        if (waterColor.a < ${h.float(y)}) {
          // Discard this vertex
          gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
          return;
        }

        vpos = position;
        gl_Position = transformPosition(proj, view, vpos);
        forwardObjectAndLayerIdColor();`),x.include(r,C),x.main.add(h`discardBySlice(vpos);
outputObjectAndLayerIdColor();`);break;default:throw new e("shaderbuilder:missing-output",`Unimplemented shader output ${_} for WaterTechnique`)}return L}const L=Object.freeze(Object.defineProperty({__proto__:null,build:C},Symbol.toStringTag,{value:"Module"}));export{L as W,C as b};
