/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{ReadDepth as e}from"../views/3d/webgl-engine/core/shaderLibrary/output/ReadDepth.glsl.js";import{Gamma as r}from"../views/3d/webgl-engine/core/shaderLibrary/shading/Gamma.glsl.js";import{addMainLightDirection as t}from"../views/3d/webgl-engine/core/shaderLibrary/shading/MainLighting.glsl.js";import{FloatPassUniform as a}from"../views/3d/webgl-engine/core/shaderModules/FloatPassUniform.js";import{FloatsPassUniform as i}from"../views/3d/webgl-engine/core/shaderModules/FloatsPassUniform.js";import{glsl as o,If as n}from"../views/3d/webgl-engine/core/shaderModules/glsl.js";import{Texture2DBindUniform as s}from"../views/3d/webgl-engine/core/shaderModules/Texture2DBindUniform.js";import{getHeightParameters as d}from"../views/3d/webgl-engine/effects/atmosphere/ChapmanApproximation.glsl.js";import{ChapmanRaymarching as l}from"../views/3d/webgl-engine/effects/atmosphere/ChapmanRaymarching.glsl.js";import{ScreenSpacePassAtmosphere as p}from"../views/3d/webgl-engine/shaders/ScreenSpacePassAtmosphere.glsl.js";import{SphereIntersect as m}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 f=new c,{fragment:u}=f;f.include(p),t(u),u.include(r),u.include(e),u.include(m),u.include(h),u.include(l,!0),u.uniforms.add(new s("depthTexture",e=>e.mainDepth));const{reduced:v}=g;return v&&u.code.add(o`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);
}`),u.uniforms.add(new a("hazeStrength",e=>e.hazeStrength),new i("heightParameters",5,(e,r)=>d(r,e))).main.add(o`
    float depthSample = depthFromTexture(depthTexture, uv);
    if (depthSample == 1.0) {
      discard;
    }

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

    cameraSpaceRay *= ${n(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));
  `),f}const f=Object.freeze(Object.defineProperty({__proto__:null,build:g},Symbol.toStringTag,{value:"Module"}));export{f as H,g as b};
