/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{ReadDepth as e}from"../views/3d/webgl-engine/core/shaderLibrary/output/ReadDepth.glsl.js";import{Gamma as o}from"../views/3d/webgl-engine/core/shaderLibrary/shading/Gamma.glsl.js";import{FloatPassUniform as r}from"../views/3d/webgl-engine/core/shaderModules/FloatPassUniform.js";import{FloatsPassUniform as i}from"../views/3d/webgl-engine/core/shaderModules/FloatsPassUniform.js";import{If as s,glsl as l}from"../views/3d/webgl-engine/core/shaderModules/glsl.js";import{IntegerPassUniform as t}from"../views/3d/webgl-engine/core/shaderModules/IntegerPassUniform.js";import{Texture2DBindUniform as a}from"../views/3d/webgl-engine/core/shaderModules/Texture2DBindUniform.js";import{Texture2DPassUniform as n}from"../views/3d/webgl-engine/core/shaderModules/Texture2DPassUniform.js";import{RayDistanceFalloffPassParameters as d,RayDistanceFalloff as m}from"../views/3d/webgl-engine/effects/raymarching/RayDistanceFalloff.glsl.js";import{ScreenSpacePassAtmosphere as c}from"../views/3d/webgl-engine/shaders/ScreenSpacePassAtmosphere.glsl.js";import{ToneMapping as p}from"../views/3d/webgl-engine/shaders/ToneMapping.glsl.js";import{ShaderBuilder as u}from"../views/webgl/ShaderBuilder.js";class g extends d{constructor(){super(...arguments),this.glowLod=-1,this.dispersionWeight=1,this.distanceModifier=1e-4}}function x(d){const g=new u,x=g.fragment,{blurEnabled:w,tonemappingEnabled:f}=d;return g.include(c,{needUVs:!0,needEyeDirection:!0}),x.include(o),x.include(m),x.include(e),g.outputs.add("fragColor","vec4",0),g.outputs.add("fragEmission","vec3",1),x.include(p),x.uniforms.add(new n("colorTexture",e=>e.color),new n("emissionTexture",e=>e.emission)),w?(x.uniforms.add(new a("depthTexture",e=>e.mainDepth),new r("distanceModifier",e=>e.distanceModifier),new n("lodTexture0",e=>e.lodTexture0),new n("lodTexture1",e=>e.lodTexture1),new n("lodTexture2",e=>e.lodTexture2),new n("lodTexture3",e=>e.lodTexture3),new n("lodTexture4",e=>e.lodTexture4),new t("glowLod",e=>e.glowLod),new i("minDisperse",6,e=>e.minDisperse.presets),new i("maxDisperse",6,e=>e.maxDisperse.presets),new r("dispersionWeight",e=>e.dispersionWeight)).main.add(l`
    vec4 color = texture(colorTexture, uv);
    color = vec4(linearizeGamma(color.rgb), color.a);

    vec3 lod0 = texture(emissionTexture, uv).rgb;
    vec3 lod1 = texture(lodTexture0, uv).rgb;
    vec3 lod2 = texture(lodTexture1, uv).rgb;
    vec3 lod3 = texture(lodTexture2, uv).rgb;
    vec3 lod4 = texture(lodTexture3, uv).rgb;
    vec3 lod5 = texture(lodTexture4, uv).rgb;

    float terrainDepth = -1.0;
    float depthSample = depthFromTexture(depthTexture, uv);
    if(depthSample < 1.0 && depthSample > 0.0){
      vec3 cameraSpaceRay = normalize(eyeDir);
      cameraSpaceRay /= cameraSpaceRay.z;
      cameraSpaceRay *= linearizeDepth(depthSample);
      terrainDepth = max(0.0, length(cameraSpaceRay));
    }
    vec3 rayDir = normalize(worldRay);
    float dispersionByDistance = getDistanceFalloff(terrainDepth, rayDir, distanceModifier);
    float dispersionPerPixel = dispersionWeight * dispersionByDistance;

    float lodFactor0 = mix(minDisperse[0], maxDisperse[0], dispersionPerPixel);
    float lodFactor1 = mix(minDisperse[1], maxDisperse[1], dispersionPerPixel);
    float lodFactor2 = mix(minDisperse[2], maxDisperse[2], dispersionPerPixel);
    float lodFactor3 = mix(minDisperse[3], maxDisperse[3], dispersionPerPixel);
    float lodFactor4 = mix(minDisperse[4], maxDisperse[4], dispersionPerPixel);
    float lodFactor5 = mix(minDisperse[5], maxDisperse[5], dispersionPerPixel);

    vec3 emission = lodFactor0 * lod0;
    emission += lodFactor1 * lod1;
    emission += lodFactor2 * lod2;
    emission += lodFactor3 * lod3;
    emission += lodFactor4 * lod4;
    emission += lodFactor5 * lod5;

    // only for glow editor lod debugging
    emission = glowLod == 0 ? lodFactor0 * lod0 : glowLod == 1 ? lodFactor1 * lod1 : glowLod == 2 ? lodFactor2 * lod2 : glowLod == 3 ? lodFactor3 * lod3 : glowLod == 4 ? lodFactor4 * lod4 : glowLod == 5 ? lodFactor5 * lod5 : emission;

    fragEmission = emission;
    // tonemapping is only applied to the emissive part since main color values are not in HDR.
    ${s(f,"emission = tonemapKhronosNeutral(emission, 1.0);")}

    fragColor = delinearizeGamma(vec4(color.rgb + emission.rgb, color.w));
  `),g):(x.main.add(l`
      vec4 color = texture(colorTexture, uv);
      color = vec4(linearizeGamma(color.rgb), color.a);

      // emission is already in linear color space here.
      vec3 emission = texture(emissionTexture, uv).rgb;

      ${s(f,"emission = tonemapKhronosNeutral(emission, 1.0);")}
      fragColor = delinearizeGamma(vec4(color.rgb + emission, color.w));
    `),g)}class w{constructor(e,o=!0){this.presets=e,this.presets=f(e,o)}}function f(e,o,r=1){const i=e[0]+e[1]+e[2]+e[3]+e[4]+e[5];return!o||i<=r?e:[e[0]/i,e[1]/i,e[2]/i,e[3]/i,e[4]/i,e[5]/i]}function v(e,o,r=1){return[e[0]*(1-r)+o[0]*r,e[1]*(1-r)+o[1]*r,e[2]*(1-r)+o[2]*r,e[3]*(1-r)+o[3]*r,e[4]*(1-r)+o[4]*r,e[5]*(1-r)+o[5]*r]}const h=Object.freeze(Object.defineProperty({__proto__:null,GlowCompositionPassParameters:g,GlowLodFactors:w,build:x,mixPreset:v},Symbol.toStringTag,{value:"Module"}));export{g as G,w as a,h as b,x as c,v as m};
