/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{addNearFar as e}from"../views/3d/webgl-engine/core/shaderLibrary/ForwardLinearDepthToWriteShadowMap.glsl.js";import{Offset as r}from"../views/3d/webgl-engine/core/shaderLibrary/Offset.glsl.js";import{isColor as i}from"../views/3d/webgl-engine/core/shaderLibrary/ShaderOutput.js";import{SliceDraw as a}from"../views/3d/webgl-engine/core/shaderLibrary/Slice.glsl.js";import{Transform as o}from"../views/3d/webgl-engine/core/shaderLibrary/Transform.glsl.js";import{ObjectAndLayerIdColor as n}from"../views/3d/webgl-engine/core/shaderLibrary/attributes/ObjectAndLayerIdColor.glsl.js";import{PathVertexPosition as l}from"../views/3d/webgl-engine/core/shaderLibrary/attributes/PathVertexPosition.glsl.js";import{OutputDepth as d}from"../views/3d/webgl-engine/core/shaderLibrary/output/OutputDepth.glsl.js";import{OutputHighlight as s}from"../views/3d/webgl-engine/core/shaderLibrary/output/OutputHighlight.glsl.js";import{EvaluateAmbientOcclusion as t}from"../views/3d/webgl-engine/core/shaderLibrary/shading/EvaluateAmbientOcclusion.glsl.js";import{EvaluateSceneLighting as c,addAmbientBoostFactor as m,addLightingGlobalFactor as g}from"../views/3d/webgl-engine/core/shaderLibrary/shading/EvaluateSceneLighting.glsl.js";import{addMainLightIntensity as u}from"../views/3d/webgl-engine/core/shaderLibrary/shading/MainLighting.glsl.js";import{Normals as v}from"../views/3d/webgl-engine/core/shaderLibrary/shading/Normals.glsl.js";import{NormalUtils as p}from"../views/3d/webgl-engine/core/shaderLibrary/shading/NormalUtils.glsl.js";import{PhysicallyBasedRenderingParameters as w}from"../views/3d/webgl-engine/core/shaderLibrary/shading/PhysicallyBasedRenderingParameters.glsl.js";import{ReadShadowMapDrawFragment as b}from"../views/3d/webgl-engine/core/shaderLibrary/shading/ReadShadowMap.glsl.js";import{ColorConversion as h}from"../views/3d/webgl-engine/core/shaderLibrary/util/ColorConversion.glsl.js";import{addProjViewLocalOrigin as f,addCameraPosition as x,addViewNormal as y}from"../views/3d/webgl-engine/core/shaderLibrary/util/View.glsl.js";import{Float3PassUniform as L}from"../views/3d/webgl-engine/core/shaderModules/Float3PassUniform.js";import{FloatPassUniform as j}from"../views/3d/webgl-engine/core/shaderModules/FloatPassUniform.js";import{If as P,glsl as V}from"../views/3d/webgl-engine/core/shaderModules/glsl.js";import{SnowCover as S}from"../views/3d/webgl-engine/effects/snowcover/SnowCover.glsl.js";import{outputColorHighlightOLID as O}from"../views/3d/webgl-engine/shaders/OutputColorHighlightOLID.glsl.js";import{ShaderBuilder as C}from"../views/webgl/ShaderBuilder.js";function A(A){const B=new C,{vertex:_,fragment:I,varyings:D}=B;f(_,A),D.add("vpos","vec3",{invariant:!0}),B.include(l,A);const{output:M,spherical:F,pbrMode:k,snowCover:z,offsetBackfaces:E}=A;switch((i(M)||11===M)&&(B.include(o),B.include(b,A),B.include(n,A),E&&(x(_,A),B.include(r)),D.add("vnormal","vec3"),D.add("vcolor","vec4"),_.main.add(V`
      ExtrudedVertex extrudedVertex = evaluateVertex();
      vpos = calculateVPos(extrudedVertex);
      vnormal = normalize(localNormal(extrudedVertex));
      gl_Position = transformPosition(proj, view, vpos);
      ${P(E,"gl_Position = offsetBackfacingClipPosition(gl_Position, vpos, vnormal, cameraPosition);")}

      forwardObjectAndLayerIdColor();
      vcolor = getColor();
      forwardLinearDepthToReadShadowMap();`)),M){case 0:case 1:case 2:B.include(w,A),I.include(c,A),I.include(t,A),B.include(v,A),I.include(a,A),B.include(O,A),x(I,A),m(I),g(I),I.uniforms.add(_.uniforms.get("localOrigin"),new L("ambient",e=>e.ambient),new L("diffuse",e=>e.diffuse),new j("opacity",e=>e.opacity)),I.include(h),I.include(S,A),u(I),I.main.add(V`
        discardBySlice(vpos);

        shadingParams.viewDirection = normalize(vpos - cameraPosition);
        shadingParams.normalView = vnormal;
        vec3 normal = shadingNormal(shadingParams);
        float ssao = evaluateAmbientOcclusionInverse();

        vec3 posWorld = vpos + localOrigin;
        vec3 normalGround = ${F?"normalize(posWorld);":"vec3(0.0, 0.0, 1.0);"}

        vec3 albedo = vcolor.rgb * max(ambient, diffuse); // combine the old material parameters into a single one
        float combinedOpacity = vcolor.a * opacity;

        ${P(z,V`float snow = getSnow(normal, normalGround);
                 albedo = mix(albedo, vec3(1), snow);
                 ssao = mix(ssao, 1.0, snow);`)}

        float additionalAmbientScale = additionalDirectedAmbientLight(posWorld);
        vec3 additionalLight = ssao * mainLightIntensity * additionalAmbientScale * ambientBoostFactor * lightingGlobalFactor;
        float shadow = readShadow(additionalAmbientScale, vpos);

        ${P(2===k,`float additionalAmbientIrradiance = additionalAmbientIrradianceFactor * mainLightIntensity[2];\n           ${P(z,"mrr = applySnowToMRR(mrr, snow);")}`)}

        vec3 shadedColor = ${2===k?"evaluateSceneLightingPBR(normal, albedo, shadow, 1.0 - ssao, additionalLight, shadingParams.viewDirection, normalGround, mrr, additionalAmbientIrradiance);":"evaluateSceneLighting(normal, albedo, shadow, 1.0 - ssao, additionalLight);"}
        vec4 finalColor = vec4(shadedColor, combinedOpacity);
        outputColorHighlightOLID(applySlice(finalColor, vpos), albedo ${P(z,", snow")});`);break;case 3:B.include(o),_.main.add(V`ExtrudedVertex extrudedVertex = evaluateVertex();
vpos = calculateVPos(extrudedVertex);
gl_Position = transformPosition(proj, view, vpos);`),B.fragment.include(a,A),I.main.add(V`discardBySlice(vpos);`);break;case 5:case 6:case 7:case 8:B.include(o),e(B),D.add("depth","float"),_.main.add(V`ExtrudedVertex extrudedVertex = evaluateVertex();
vpos = calculateVPos(extrudedVertex);
gl_Position = transformPositionWithDepth(proj, view, vpos, nearFar, depth);`),B.fragment.include(a,A),B.include(d,A),I.main.add(V`discardBySlice(vpos);
outputDepth(depth);`);break;case 11:B.fragment.include(a,A),I.main.add(V`discardBySlice(vpos);
outputObjectAndLayerIdColor();`);break;case 4:B.include(o),B.include(p,A),y(_),D.add("vnormal","vec3"),_.main.add(V`ExtrudedVertex extrudedVertex = evaluateVertex();
vpos = calculateVPos(extrudedVertex);
vnormal = normalize((viewNormal * vec4(localNormal(extrudedVertex), 1.0)).xyz);
gl_Position = transformPosition(proj, view, vpos);`),B.fragment.include(a,A),I.main.add(V`discardBySlice(vpos);
vec3 normal = normalize(vnormal);
if (gl_FrontFacing == false) normal = -normal;
fragColor = vec4(vec3(0.5) + 0.5 * normal, 1.0);`);break;case 10:B.include(o),B.include(p,A),D.add("vnormal","vec3"),_.main.add(V`ExtrudedVertex extrudedVertex = evaluateVertex();
vpos = calculateVPos(extrudedVertex);
gl_Position = transformPosition(proj, view, vpos);`),B.fragment.include(a,A),B.include(s,A),I.main.add(V`discardBySlice(vpos);
calculateOcclusionAndOutputHighlight();`)}return B}const B=Object.freeze(Object.defineProperty({__proto__:null,build:A},Symbol.toStringTag,{value:"Module"}));export{B as P,A as b};
