/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{getHeightParameters as e}from"../views/3d/environment/ChapmanApproximation.glsl.js";import{ChapmanRaymarching as r}from"../views/3d/environment/ChapmanRaymarching.glsl.js";import{ReadDepth as t}from"../views/3d/webgl-engine/core/shaderLibrary/output/ReadDepth.glsl.js";import{Gamma as a}from"../views/3d/webgl-engine/core/shaderLibrary/shading/Gamma.glsl.js";import{addMainLightDirection as i}from"../views/3d/webgl-engine/core/shaderLibrary/shading/MainLighting.glsl.js";import{FloatPassUniform as o}from"../views/3d/webgl-engine/core/shaderModules/FloatPassUniform.js";import{FloatsPassUniform as n}from"../views/3d/webgl-engine/core/shaderModules/FloatsPassUniform.js";import{glsl as d,If as s}from"../views/3d/webgl-engine/core/shaderModules/glsl.js";import{Texture2DBindUniform as l}from"../views/3d/webgl-engine/core/shaderModules/Texture2DBindUniform.js";import{ScreenSpacePassAtmosphere as m}from"../views/3d/webgl-engine/shaders/ScreenSpacePassAtmosphere.glsl.js";import{SphereIntersect as p}from"../views/3d/webgl-engine/shaders/SphereIntersect.glsl.js";import{ToneMapping as h}from"../views/3d/webgl-engine/shaders/ToneMapping.glsl.js";import{ShaderBuilder as c}from"../views/webgl/ShaderBuilder.js";function g(g){const u=new c,{fragment:f}=u;u.include(m),i(f),f.include(a),f.include(t),f.include(p),f.include(h),f.include(r,!0),f.uniforms.add(new l("depthTexture",e=>e.mainDepth));const{reduced:v}=g;return v&&f.code.add(d`float getDepth(vec2 uv){
return linearDepthFromTexture(depthTexture, uv);
}
float textureBilinear(vec2 uv) {
vec2 depthTextureSize = vec2(textureSize(depthTexture, 0));
vec2 texelSize = 1.0 / depthTextureSize;
vec2 depthUV = (uv * depthTextureSize) - vec2(0.5);
vec2 f = fract(depthUV);
vec2 snapUV = (floor(depthUV) + vec2(0.5)) / depthTextureSize;
float d0 = getDepth(snapUV);
float d1 = getDepth(snapUV + vec2(texelSize.x, 0.0));
float d2 = getDepth(snapUV + vec2(0.0, texelSize.y));
float d3 = getDepth(snapUV + texelSize);
return mix(mix(d0, d1, f.x), mix(d2, d3, f.x), f.y);
}`),f.uniforms.add(new o("hazeStrength",e=>e.hazeStrength),new n("heightParameters",5,(r,t)=>e(t,r))).main.add(d`
    float depthSample = depthFromTexture(depthTexture, uv);
    if (depthSample == 1.0) {
      discard;
    }

    vec3 rayDir = normalize(worldRay);
    vec3 cameraSpaceRay = normalize(eyeDir);
    cameraSpaceRay /= cameraSpaceRay.z;

    cameraSpaceRay *= ${s(v,"-textureBilinear(uv)","-linearizeDepth(depthSample)")};
    float terrainDepth = max(0.0, length(cameraSpaceRay));

    // Alpha is ignored for haze blending
    float fadeOut = smoothstep(-10000.0, -15000.0, heightParameters[0] - radii[0]);
    vec3 color = (1.0 - fadeOut) * hazeStrength * raymarchAtmosphere(rayDir, mainLightDirection, terrainDepth);

    color = tonemapACES(color);
    fragColor = delinearizeGamma(vec4(color, 1.0));
  `),u}const u=Object.freeze(Object.defineProperty({__proto__:null,build:g},Symbol.toStringTag,{value:"Module"}));export{u as H,g as b};
