/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","./vec4f64","../views/3d/webgl-engine/core/shaderLibrary/ScreenSizeScaling.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/shading/MultipassTerrainTest.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/interfaces","../views/3d/webgl-engine/core/shaderModules/Matrix4PassUniform","../views/3d/webgl-engine/core/shaderModules/ShaderBuilder","../views/3d/webgl-engine/lib/basicInterfaces","../views/3d/webgl-engine/lib/VertexAttribute"],(function(e,r,i,o,a,l,n,d,s,t,g,c,u,v,w,h,b){"use strict";function f(e){const r=new w.ShaderBuilder,f=e.hasMultipassTerrain&&(e.output===o.ShaderOutput.Color||e.output===o.ShaderOutput.Alpha);r.include(l.Transform),r.include(i.ScreenSizeScaling,e),r.include(a.SliceDraw,e);const{vertex:p,fragment:S}=r;return S.include(s.ColorConversion),t.addProjViewLocalOrigin(r,e),S.uniforms.add(new c.Float4PassUniform("uColor",(e=>e.color))),r.attributes.add(b.VertexAttribute.POSITION,"vec3"),r.varyings.add("vWorldPosition","vec3"),f&&r.varyings.add("depth","float"),e.screenSizeEnabled&&r.attributes.add(b.VertexAttribute.OFFSET,"vec3"),e.shadingEnabled&&(p.uniforms.add(new v.Matrix4PassUniform("viewNormal",((e,r)=>r.camera.viewInverseTransposeMatrix))),r.attributes.add(b.VertexAttribute.NORMAL,"vec3"),r.varyings.add("vViewNormal","vec3")),p.code.add(u.glsl`
    void main(void) {
      vWorldPosition = ${e.screenSizeEnabled?"screenSizeScaling(offset, position)":"position"};
  `),e.shadingEnabled&&p.code.add(u.glsl`vec3 worldNormal = normal;
vViewNormal = (viewNormal * vec4(worldNormal, 1)).xyz;`),p.code.add(u.glsl`
    ${f?"depth = (view * vec4(vWorldPosition, 1.0)).z;":""}
    gl_Position = transformPosition(proj, view, vWorldPosition);
  }
  `),f&&r.include(n.multipassTerrainTest,e),S.code.add(u.glsl`
    void main() {
      discardBySlice(vWorldPosition);
      ${f?"terrainDepthTest(gl_FragCoord, depth);":""}
    `),e.shadingEnabled?(S.uniforms.add(new g.Float3PassUniform("shadingDirection",(e=>e.shadingDirection))),S.uniforms.add(new c.Float4PassUniform("shadedColor",(e=>m(e.shadingTint,e.color)))),S.code.add(u.glsl`vec3 viewNormalNorm = normalize(vViewNormal);
float shadingFactor = 1.0 - clamp(-dot(viewNormalNorm, shadingDirection), 0.0, 1.0);
vec4 finalColor = mix(uColor, shadedColor, shadingFactor);`)):S.code.add(u.glsl`vec4 finalColor = uColor;`),S.code.add(u.glsl`
      if (finalColor.a < ${u.glsl.float(d.symbolAlphaCutoff)}) {
        discard;
      }
      ${e.output===o.ShaderOutput.Alpha?u.glsl`gl_FragColor = vec4(finalColor.a);`:""}

      ${e.output===o.ShaderOutput.Color?u.glsl`gl_FragColor = highlightSlice(finalColor, vWorldPosition); ${e.transparencyPassType===h.TransparencyPassType.Color?"gl_FragColor = premultiplyAlpha(gl_FragColor);":""}`:""}
    }
    `),r}function m(e,r){const i=1-e[3],o=e[3]+r[3]*i;return 0===o?(p[3]=o,p):(p[0]=(e[0]*e[3]+r[0]*r[3]*i)/o,p[1]=(e[1]*e[3]+r[1]*r[3]*i)/o,p[2]=(e[2]*e[3]+r[2]*r[3]*i)/o,p[3]=r[3],p)}const p=r.create(),S=Object.freeze(Object.defineProperty({__proto__:null,build:f},Symbol.toStringTag,{value:"Module"}));e.ShadedColorMaterialShader=S,e.build=f}));
