/*
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/Offset.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/attributes/InstancedDoublePrecision.glsl","../views/3d/webgl-engine/core/shaderLibrary/attributes/NormalAttribute.glsl","../views/3d/webgl-engine/core/shaderLibrary/attributes/PositionAttribute.glsl","../views/3d/webgl-engine/core/shaderLibrary/attributes/SymbolColor.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/VerticalOffset.glsl","../views/3d/webgl-engine/core/shaderLibrary/default/DefaultMaterialAuxiliaryPasses.glsl","../views/3d/webgl-engine/core/shaderLibrary/shading/EvaluateAmbientOcclusion.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/PhysicallyBasedRendering.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/VisualVariables.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/MixExternalColor.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/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","../views/3d/webgl-engine/lighting/SceneLighting"],(function(e,a,i,r,o,l,t,s,n,d,c,g,v,u,b,h,w,m,p,f,x,y,C,M,P,L,O,A,F,S,T,E,D,_,B){"use strict";function $(e){const $=new T.ShaderBuilder,V=$.vertex.code,N=$.fragment.code,R=P.addProjViewLocalOrigin($,e);return $.include(n.PositionAttribute),$.varyings.add("vpos","vec3"),$.include(x.VisualVariables,e),$.include(t.InstancedDoublePrecision,e),$.include(v.VerticalOffset,e),e.output!==r.ShaderOutput.Color&&e.output!==r.ShaderOutput.Alpha||(P.addCameraPosition($.vertex,e),$.include(s.NormalAttribute,e),$.include(l.Transform),e.offsetBackfaces&&$.include(i.Offset),e.instancedColor&&$.attributes.add(_.VertexAttribute.INSTANCECOLOR,"vec4"),$.varyings.add("vNormalWorld","vec3"),$.varyings.add("localvpos","vec3"),e.hasMultipassTerrain&&$.varyings.add("depth","float"),$.include(c.TextureCoordinateAttribute,e),$.include(a.ForwardLinearDepth,e),$.include(d.SymbolColor,e),$.include(g.VertexColor,e),$.vertex.uniforms.add(new O.Float4PassUniform("externalColor",(e=>e.externalColor))),$.varyings.add("vcolorExt","vec4"),V.add(F.glsl`
        void main(void) {
          forwardNormalizedVertexColor();
          vcolorExt = externalColor;
          ${e.instancedColor?"vcolorExt *= instanceColor;":""}
          vcolorExt *= vvColor();
          vcolorExt *= getSymbolColor();
          forwardColorMixMode();

          if (vcolorExt.a < ${F.glsl.float(y.symbolAlphaCutoff)}) {
            gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
          } else {
            vpos = calculateVPos();
            localvpos = vpos - view[3].xyz;
            vpos = subtractOrigin(vpos);
            vNormalWorld = dpNormal(vvLocalNormal(normalModel()));
            vpos = addVerticalOffset(vpos, localOrigin);
            gl_Position = transformPosition(proj, view, vpos);
            ${e.offsetBackfaces?"gl_Position = offsetBackfacingClipPosition(gl_Position, vpos, vNormalWorld, cameraPosition);":""}
          }
          ${e.hasMultipassTerrain?F.glsl`depth = (view * vec4(vpos, 1.0)).z;`:""}
          forwardLinearDepth();
          forwardTextureCoordinates();
        }
      `)),e.output===r.ShaderOutput.Alpha&&($.include(o.SliceDraw,e),$.include(C.DiscardOrAdjustAlphaPass,e),$.include(w.multipassTerrainTest,e),$.fragment.uniforms.add([new A.FloatPassUniform("opacity",(e=>e.opacity)),new A.FloatPassUniform("layerOpacity",(e=>e.layerOpacity)),new S.Matrix4Uniform("view")]),e.hasColorTexture&&$.fragment.uniforms.add(new E.Texture2DPassUniform("tex",(e=>e.texture))),$.fragment.include(M.MixExternalColor),N.add(F.glsl`
      void main() {
        discardBySlice(vpos);
        ${e.hasMultipassTerrain?F.glsl`terrainDepthTest(gl_FragCoord, depth);`:""}
        ${e.hasColorTexture?F.glsl`
                vec4 texColor = texture2D(tex, vuv0);
                ${e.textureAlphaPremultiplied?"texColor.rgb /= texColor.a;":""}
                discardOrAdjustAlpha(texColor);`:F.glsl`vec4 texColor = vec4(1.0);`}
        ${e.hasVertexColors?F.glsl`float opacity_ = layerOpacity * mixExternalOpacity(vColor.a * opacity, texColor.a, vcolorExt.a, int(colorMixMode));`:F.glsl`float opacity_ = layerOpacity * mixExternalOpacity(opacity, texColor.a, vcolorExt.a, int(colorMixMode));`}

        gl_FragColor = vec4(opacity_);
      }
    `)),e.output===r.ShaderOutput.Color&&($.include(o.SliceDraw,e),$.include(h.EvaluateSceneLighting,e),$.include(b.EvaluateAmbientOcclusion,e),$.include(C.DiscardOrAdjustAlphaPass,e),$.include(e.instancedDoublePrecision?f.ReadShadowMapPass:f.ReadShadowMapDraw,e),$.include(w.multipassTerrainTest,e),P.addCameraPosition($.fragment,e),$.fragment.uniforms.add([R,new L.Float3PassUniform("ambient",(e=>e.ambient)),new L.Float3PassUniform("diffuse",(e=>e.diffuse)),new A.FloatPassUniform("opacity",(e=>e.opacity)),new A.FloatPassUniform("layerOpacity",(e=>e.layerOpacity)),new S.Matrix4Uniform("view"),new A.FloatPassUniform("lightingGlobalFactor",((e,a)=>a.lighting.globalFactor)),new L.Float3PassUniform("lightingMainIntensity",((e,a)=>a.lighting.mainLight.intensity))]),$.fragment.constants.add("ambientBoostFactor","float",B.ambientBoost),e.hasColorTexture&&$.fragment.uniforms.add(new E.Texture2DPassUniform("tex",(e=>e.texture))),$.include(p.PhysicallyBasedRenderingParameters,e),$.include(m.PhysicallyBasedRendering,e),$.fragment.include(M.MixExternalColor),$.extensions.add("GL_OES_standard_derivatives"),N.add(F.glsl`
      void main() {
        discardBySlice(vpos);
        ${e.hasMultipassTerrain?F.glsl`terrainDepthTest(gl_FragCoord, depth);`:""}
        ${e.hasColorTexture?F.glsl`
                vec4 texColor = texture2D(tex, vuv0);
                ${e.textureAlphaPremultiplied?"texColor.rgb /= texColor.a;":""}
                discardOrAdjustAlpha(texColor);`:F.glsl`vec4 texColor = vec4(1.0);`}
        vec3 viewDirection = normalize(vpos - cameraPosition);
        ${e.pbrMode===p.PBRMode.Normal?"applyPBRFactors();":""}
        float ssao = evaluateAmbientOcclusionInverse();
        ssao *= getBakedOcclusion();

        float additionalAmbientScale = additionalDirectedAmbientLight(vpos + localOrigin);
        vec3 additionalLight = ssao * lightingMainIntensity * additionalAmbientScale * ambientBoostFactor * lightingGlobalFactor;
        ${e.receiveShadows?"float shadow = readShadowMap(vpos, linearDepth);":e.spherical?"float shadow = lightingGlobalFactor * (1.0 - additionalAmbientScale);":"float shadow = 0.0;"}
        vec3 matColor = max(ambient, diffuse);
        ${e.hasVertexColors?F.glsl`
                vec3 albedo = mixExternalColor(vColor.rgb * matColor, texColor.rgb, vcolorExt.rgb, int(colorMixMode));
                float opacity_ = layerOpacity * mixExternalOpacity(vColor.a * opacity, texColor.a, vcolorExt.a, int(colorMixMode));`:F.glsl`
                vec3 albedo = mixExternalColor(matColor, texColor.rgb, vcolorExt.rgb, int(colorMixMode));
                float opacity_ = layerOpacity * mixExternalOpacity(opacity, texColor.a, vcolorExt.a, int(colorMixMode));`}
        ${e.snowCover?F.glsl`albedo = mix(albedo, vec3(1), 0.9);`:F.glsl``}
        ${F.glsl`
            vec3 shadingNormal = normalize(vNormalWorld);
            albedo *= 1.2;
            vec3 viewForward = vec3(view[0][2], view[1][2], view[2][2]);
            float alignmentLightView = clamp(dot(viewForward, -lightingMainDirection), 0.0, 1.0);
            float transmittance = 1.0 - clamp(dot(viewForward, shadingNormal), 0.0, 1.0);
            float treeRadialFalloff = vColor.r;
            float backLightFactor = 0.5 * treeRadialFalloff * alignmentLightView * transmittance * (1.0 - shadow);
            additionalLight += backLightFactor * lightingMainIntensity;`}
        ${e.pbrMode===p.PBRMode.Normal||e.pbrMode===p.PBRMode.Schematic?e.spherical?F.glsl`vec3 normalGround = normalize(vpos + localOrigin);`:F.glsl`vec3 normalGround = vec3(0.0, 0.0, 1.0);`:F.glsl``}
        ${e.pbrMode===p.PBRMode.Normal||e.pbrMode===p.PBRMode.Schematic?F.glsl`
                float additionalAmbientIrradiance = additionalAmbientIrradianceFactor * lightingMainIntensity[2];
                ${e.snowCover?F.glsl`
                        mrr = vec3(0.0, 1.0, 0.04);
                        emission = vec3(0.0);`:""}

                vec3 shadedColor = evaluateSceneLightingPBR(shadingNormal, albedo, shadow, 1.0 - ssao, additionalLight, viewDirection, normalGround, mrr, emission, additionalAmbientIrradiance);`:F.glsl`vec3 shadedColor = evaluateSceneLighting(shadingNormal, albedo, shadow, 1.0 - ssao, additionalLight);`}
        gl_FragColor = highlightSlice(vec4(shadedColor, opacity_), vpos);
        ${e.transparencyPassType===D.TransparencyPassType.Color?F.glsl`gl_FragColor = premultiplyAlpha(gl_FragColor);`:F.glsl``}
      }
    `)),$.include(u.DefaultMaterialAuxiliaryPasses,e),$}const V=Object.freeze(Object.defineProperty({__proto__:null,build:$},Symbol.toStringTag,{value:"Module"}));e.RealisticTree=V,e.build=$}));
