/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../core/maybe","./mat4f64","../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/VertexNormal.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/ComputeNormalTexture.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/Normals.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/HeaderComment.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/Matrix4PassUniform","../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,r,o,i,l,t,s,n,d,c,g,u,m,v,h,b,p,w,x,y,f,C,P,T,M,A,L,O,S,N,D,F,E,$,V,B,_,U,I,R){"use strict";function j(e){const j=new B.ShaderBuilder,z=j.vertex.code,G=j.fragment.code;j.include(O.HeaderComment,{name:"Default Material Shader",output:e.output});const k=N.addProjViewLocalOrigin(j,e);return j.include(c.PositionAttribute),j.varyings.add("vpos","vec3"),j.include(M.VisualVariables,e),j.include(n.InstancedDoublePrecision,e),j.include(h.VerticalOffset,e),e.output!==l.ShaderOutput.Color&&e.output!==l.ShaderOutput.Alpha||(N.addCameraPosition(j.vertex,e),j.include(d.NormalAttribute,e),j.include(s.Transform,{linearDepth:!1,hasModelTransformation:e.hasModelTransformation}),e.normalType===d.NormalAttributeType.Attribute&&e.offsetBackfaces&&j.include(i.Offset),j.include(p.ComputeNormalTexture,e),j.include(v.VertexNormal,e),e.instancedColor&&j.attributes.add(I.VertexAttribute.INSTANCECOLOR,"vec4"),j.varyings.add("localvpos","vec3"),j.include(u.TextureCoordinateAttribute,e),j.include(o.ForwardLinearDepth,e),j.include(g.SymbolColor,e),j.include(m.VertexColor,e),j.vertex.uniforms.add(new F.Float4PassUniform("externalColor",(e=>e.externalColor))),j.varyings.add("vcolorExt","vec4"),e.hasMultipassTerrain&&j.varyings.add("depth","float"),e.hasModelTransformation&&j.vertex.uniforms.add(new V.Matrix4PassUniform("model",(e=>a.isSome(e.modelTransformation)?e.modelTransformation:r.IDENTITY))),z.add($.glsl`
      void main(void) {
        forwardNormalizedVertexColor();
        vcolorExt = externalColor;
        ${e.instancedColor?"vcolorExt *= instanceColor;":""}
        vcolorExt *= vvColor();
        vcolorExt *= getSymbolColor();
        forwardColorMixMode();

        if (vcolorExt.a < ${$.glsl.float(A.symbolAlphaCutoff)}) {
          gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
        } else {
          vpos = calculateVPos();
          localvpos = vpos - view[3].xyz;
          vpos = subtractOrigin(vpos);
          ${e.normalType===d.NormalAttributeType.Attribute?$.glsl`vNormalWorld = dpNormal(vvLocalNormal(normalModel()));`:""}
          vpos = addVerticalOffset(vpos, localOrigin);
          ${e.hasVertexTangents?"vTangent = dpTransformVertexTangent(tangent);":""}
          gl_Position = transformPosition(proj, view, ${e.hasModelTransformation?"model,":""} vpos);
          ${e.normalType===d.NormalAttributeType.Attribute&&e.offsetBackfaces?"gl_Position = offsetBackfacingClipPosition(gl_Position, vpos, vNormalWorld, cameraPosition);":""}
        }

        ${e.hasMultipassTerrain?"depth = (view * vec4(vpos, 1.0)).z;":""}
        forwardLinearDepth();
        forwardTextureCoordinates();
      }
    `)),e.output===l.ShaderOutput.Alpha&&(j.include(t.SliceDraw,e),j.include(L.DiscardOrAdjustAlphaPass,e),j.include(y.multipassTerrainTest,e),j.fragment.uniforms.add([new E.FloatPassUniform("opacity",(e=>e.opacity)),new E.FloatPassUniform("layerOpacity",(e=>e.layerOpacity))]),e.hasColorTexture&&j.fragment.uniforms.add(new _.Texture2DPassUniform("tex",(e=>e.texture))),j.fragment.include(S.MixExternalColor),G.add($.glsl`
      void main() {
        discardBySlice(vpos);
        ${e.hasMultipassTerrain?"terrainDepthTest(gl_FragCoord, depth);":""}
        ${e.hasColorTexture?$.glsl`
                vec4 texColor = texture2D(tex, vuv0);
                ${e.textureAlphaPremultiplied?"texColor.rgb /= texColor.a;":""}
                discardOrAdjustAlpha(texColor);`:$.glsl`vec4 texColor = vec4(1.0);`}
        ${e.hasVertexColors?$.glsl`float opacity_ = layerOpacity * mixExternalOpacity(vColor.a * opacity, texColor.a, vcolorExt.a, int(colorMixMode));`:$.glsl`float opacity_ = layerOpacity * mixExternalOpacity(opacity, texColor.a, vcolorExt.a, int(colorMixMode));`}
        gl_FragColor = vec4(opacity_);
      }
    `)),e.output===l.ShaderOutput.Color&&(j.include(t.SliceDraw,e),j.include(x.EvaluateSceneLighting,e),j.include(w.EvaluateAmbientOcclusion,e),j.include(L.DiscardOrAdjustAlphaPass,e),j.include(e.instancedDoublePrecision?T.ReadShadowMapPass:T.ReadShadowMapDraw,e),j.include(y.multipassTerrainTest,e),N.addCameraPosition(j.fragment,e),j.fragment.uniforms.add([k,new D.Float3PassUniform("ambient",(e=>e.ambient)),new D.Float3PassUniform("diffuse",(e=>e.diffuse)),new E.FloatPassUniform("opacity",(e=>e.opacity)),new E.FloatPassUniform("layerOpacity",(e=>e.layerOpacity)),new E.FloatPassUniform("lightingGlobalFactor",((e,a)=>a.lighting.globalFactor)),new D.Float3PassUniform("lightingMainIntensity",((e,a)=>a.lighting.mainLight.intensity))]),j.fragment.constants.add("ambientBoostFactor","float",R.ambientBoost),e.hasColorTexture&&j.fragment.uniforms.add(new _.Texture2DPassUniform("tex",(e=>e.texture))),j.include(P.PhysicallyBasedRenderingParameters,e),j.include(C.PhysicallyBasedRendering,e),j.fragment.include(S.MixExternalColor),j.include(f.Normals,e),G.add($.glsl`
      void main() {
        discardBySlice(vpos);
        ${e.hasMultipassTerrain?"terrainDepthTest(gl_FragCoord, depth);":""}
        ${e.hasColorTexture?$.glsl`
                vec4 texColor = texture2D(tex, vuv0);
                ${e.textureAlphaPremultiplied?"texColor.rgb /= texColor.a;":""}
                discardOrAdjustAlpha(texColor);`:$.glsl`vec4 texColor = vec4(1.0);`}
        shadingParams.viewDirection = normalize(vpos - cameraPosition);
        ${e.normalType===d.NormalAttributeType.ScreenDerivative?$.glsl`
                vec3 normal = screenDerivativeNormal(localvpos);`:$.glsl`
                shadingParams.normalView = vNormalWorld;
                vec3 normal = shadingNormal(shadingParams);`}
        ${e.pbrMode===P.PBRMode.Normal?"applyPBRFactors();":""}
        float ssao = evaluateAmbientOcclusionInverse();
        ssao *= getBakedOcclusion();

        float additionalAmbientScale = additionalDirectedAmbientLight(vpos + localOrigin);
        ${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?$.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));`:$.glsl`
                vec3 albedo = mixExternalColor(matColor, texColor.rgb, vcolorExt.rgb, int(colorMixMode));
                float opacity_ = layerOpacity * mixExternalOpacity(opacity, texColor.a, vcolorExt.a, int(colorMixMode));`}
        ${e.hasNormalTexture?$.glsl`
                mat3 tangentSpace = ${e.hasVertexTangents?"computeTangentSpace(normal);":"computeTangentSpace(normal, vpos, vuv0);"}
                vec3 shadingNormal = computeTextureNormal(tangentSpace, vuv0);`:$.glsl`vec3 shadingNormal = normal;`}
        vec3 normalGround = ${e.spherical?$.glsl`normalize(vpos + localOrigin);`:$.glsl`vec3(0.0, 0.0, 1.0);`}

        ${e.snowCover?$.glsl`
                float snow = smoothstep(0.5, 0.55, dot(normal, normalGround));
                albedo = mix(albedo, vec3(1), snow);
                shadingNormal = mix(shadingNormal, normal, snow);
                ssao = mix(ssao, 1.0, snow);`:""}

        vec3 additionalLight = ssao * lightingMainIntensity * additionalAmbientScale * ambientBoostFactor * lightingGlobalFactor;

        ${e.pbrMode===P.PBRMode.Normal||e.pbrMode===P.PBRMode.Schematic?$.glsl`
                float additionalAmbientIrradiance = additionalAmbientIrradianceFactor * lightingMainIntensity[2];
                ${e.snowCover?$.glsl`
                        mrr = mix(mrr, vec3(0.0, 1.0, 0.04), snow);
                        emission = mix(emission, vec3(0.0), snow);`:""}

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