ChapmanAtmosphere.glsl.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.25/esri/copyright.txt for details.
  4. */
  5. import{a as e}from"./mat4.js";import{c as t}from"./mat4f64.js";import{f as a}from"./vec3f64.js";import{rayLeighScaleHeight as r,atmosphereHeight as i}from"../views/3d/environment/atmosphereUtils.js";import{TextureCoordinateAttribute as o,TextureCoordinateAttributeType as s}from"../views/3d/webgl-engine/core/shaderLibrary/attributes/TextureCoordinateAttribute.glsl.js";import{ReadLinearDepth as n}from"../views/3d/webgl-engine/core/shaderLibrary/output/ReadLinearDepth.glsl.js";import{Gamma as l}from"../views/3d/webgl-engine/core/shaderLibrary/shading/Gamma.glsl.js";import{addMainLightDirection as c}from"../views/3d/webgl-engine/core/shaderLibrary/shading/MainLighting.glsl.js";import{Float2PassUniform as d}from"../views/3d/webgl-engine/core/shaderModules/Float2PassUniform.js";import{Float3PassUniform as m}from"../views/3d/webgl-engine/core/shaderModules/Float3PassUniform.js";import{Float4PassUniform as h}from"../views/3d/webgl-engine/core/shaderModules/Float4PassUniform.js";import{FloatPassUniform as p}from"../views/3d/webgl-engine/core/shaderModules/FloatPassUniform.js";import{glsl as g}from"../views/3d/webgl-engine/core/shaderModules/interfaces.js";import{Matrix4PassUniform as f}from"../views/3d/webgl-engine/core/shaderModules/Matrix4PassUniform.js";import{ShaderBuilder as u}from"../views/3d/webgl-engine/core/shaderModules/ShaderBuilder.js";import{Texture2DPassUniform as v}from"../views/3d/webgl-engine/core/shaderModules/Texture2DPassUniform.js";import{VertexAttribute as y}from"../views/3d/webgl-engine/lib/VertexAttribute.js";const x=a(parseFloat(Number(5802e-9).toFixed(6)),parseFloat(Number(13558e-9).toFixed(6)),parseFloat(Number(331e-7).toFixed(6))),b=3,D=a(b*parseFloat(Number(65e-8).toFixed(6)),b*parseFloat(Number(1881e-9).toFixed(6)),b*parseFloat(Number(85e-9).toFixed(6))),P=3996e-9,w=a(parseFloat(Number(x[0]+D[0]).toFixed(6)),parseFloat(Number(x[1]+D[1]).toFixed(6)),parseFloat(Number(x[2]+D[2]).toFixed(6)));function F(t){const a=new u;a.attributes.add(y.POSITION,"vec2"),a.include(o,{textureCoordinateType:s.Default}),a.varyings.add("worldRay","vec3"),a.varyings.add("eyeDir","vec3");const{vertex:b,fragment:D}=a;return b.uniforms.add([new f("inverseProjectionMatrix",((e,t)=>t.camera.inverseProjectionMatrix)),new f("inverseViewMatrix",((t,a)=>e(S,a.camera.viewMatrix)))]),b.code.add(g`void main(void) {
  6. vec3 posViewNear = (inverseProjectionMatrix * vec4(position, -1, 1)).xyz;
  7. eyeDir = posViewNear;
  8. worldRay = (inverseViewMatrix * vec4(posViewNear, 0)).xyz;
  9. forwardTextureCoordinates();
  10. gl_Position = vec4(position, 1, 1);
  11. }`),D.uniforms.add([new d("radii",(e=>e.radii)),new m("cameraPosition",((e,t)=>t.camera.eye)),new h("heightParameters",(e=>e.heightParameters)),new p("innerFadeDistance",(e=>e.innerFadeDistance)),new p("altitudeFade",(e=>e.altitudeFade)),new v("depthTex",(e=>e.depthTex)),new p("hazeStrength",(e=>e.hazeStrength))]),D.constants.add("betaRayleigh","vec3",x),D.constants.add("betaCombined","vec3",w),D.constants.add("betaMie","float",P),D.constants.add("scaleHeight","float",r*i),c(D),a.include(l),t.haze&&(D.include(n),D.uniforms.add(new d("nearFar",((e,t)=>t.camera.nearFar)))),D.code.add(g`vec2 sphereIntersect(vec3 start, vec3 dir, float radius, bool planet) {
  12. float a = dot(dir, dir);
  13. float b = 2.0 * dot(dir, start);
  14. float c = planet ? heightParameters[1] - radius * radius : heightParameters[2];
  15. float d = (b * b) - 4.0 * a * c;
  16. if (d < 0.0) {
  17. return vec2(1e5, -1e5);
  18. }
  19. return vec2((-b - sqrt(d)) / (2.0 * a), (-b + sqrt(d)) / (2.0 * a));
  20. }`),D.code.add(g`float chapmanApproximation(float X, float h, float cosZenith) {
  21. float c = sqrt(X + h);
  22. float cExpH = c * exp(-h);
  23. if (cosZenith >= 0.0) {
  24. return cExpH / (c * cosZenith + 1.0);
  25. } else {
  26. float x0 = sqrt(1.0 - cosZenith * cosZenith) * (X + h);
  27. float c0 = sqrt(x0);
  28. return 2.0 * c0 * exp(X - x0) - cExpH / (1.0 - c * cosZenith);
  29. }
  30. }`),D.code.add(g`float getOpticalDepth(vec3 position, vec3 dir, float h) {
  31. return scaleHeight * chapmanApproximation(radii[0] / scaleHeight, h, dot(normalize(position), dir));
  32. }`),D.code.add(g`
  33. const int STEPS = 6;
  34. float getGlow(float dist, float radius, float intensity) {
  35. return pow(radius / max(dist, 1e-6), intensity);
  36. }
  37. vec3 getAtmosphereColour(vec3 cameraPos, vec3 rayDir, vec3 lightDir, float terrainDepth) {
  38. float reducedPlanetRadius = radii[0] - 20000.0;
  39. vec2 rayPlanetIntersect = sphereIntersect(cameraPos, rayDir, reducedPlanetRadius, true);
  40. vec2 rayAtmosphereIntersect = sphereIntersect(cameraPos, rayDir, radii[1], false);
  41. bool hitsAtmosphere = (rayAtmosphereIntersect.x <= rayAtmosphereIntersect.y) && rayAtmosphereIntersect.x > 0.0;
  42. bool insideAtmosphere = heightParameters[0] < radii[1];
  43. if (!(hitsAtmosphere || insideAtmosphere)) {
  44. return vec3(0);
  45. }
  46. bool hitsPlanet = (rayPlanetIntersect.x <= rayPlanetIntersect.y) && rayPlanetIntersect.x > 0.0;
  47. float start = insideAtmosphere ? 0.0 : rayAtmosphereIntersect.x;
  48. if (heightParameters[0] < reducedPlanetRadius) {
  49. // Long light rays from the night side of the planet lead to numerical instability
  50. // Do not render the atmosphere in such cases
  51. if (dot(rayDir, normalize(cameraPos)) < -0.025) {
  52. return vec3(0);
  53. }
  54. start = rayPlanetIntersect.y;
  55. }
  56. float end = hitsPlanet ? rayPlanetIntersect.x : rayAtmosphereIntersect.y;
  57. float maxEnd = end;
  58. ${t.haze?g`if (terrainDepth != -1.0) { end = terrainDepth; }`:""}
  59. vec3 samplePoint = cameraPos + rayDir * end;
  60. float multiplier = hitsPlanet ? -1.0 : 1.0;
  61. vec3 scattering = vec3(0);
  62. float scaleFract = (length(samplePoint) - radii[0]) / scaleHeight;
  63. float lastOpticalDepth = getOpticalDepth(samplePoint, rayDir, scaleFract);
  64. float stepSize = (end - start) / float(STEPS);
  65. for (int i = 0; i < STEPS; i++) {
  66. samplePoint -= stepSize * rayDir;
  67. scaleFract = (length(samplePoint) - radii[0]) / scaleHeight;
  68. float opticalDepth = multiplier * getOpticalDepth(samplePoint, rayDir * multiplier, scaleFract);
  69. if (i > 0) {
  70. scattering *= ${t.haze?g``:" mix(2.5, 1.0, clamp((length(cameraPos) - radii[0]) / 50e3, 0.0, 1.0)) * "} exp(-(mix(betaCombined, betaRayleigh, 0.5) + betaMie) * max(0.0, (opticalDepth - lastOpticalDepth)));
  71. }
  72. if (dot(normalize(samplePoint), lightDir) > -0.3) {
  73. float scale = exp(-scaleFract);
  74. float lightDepth = getOpticalDepth(samplePoint, lightDir, scaleFract);
  75. scattering += scale * exp(-(betaCombined + betaMie) * lightDepth);
  76. ${t.haze?"":g`scattering += scale * exp(-(0.25 * betaCombined ) * lightDepth);`}
  77. }
  78. lastOpticalDepth = opticalDepth;
  79. }
  80. float mu = dot(rayDir, lightDir);
  81. float mumu = 1.0 + mu * mu;
  82. float phaseRayleigh = 0.0596831 * mumu;
  83. ${t.haze?g`return 3.0 * scattering * stepSize * phaseRayleigh * betaRayleigh;`:g`
  84. const float g = 0.8;
  85. const float gg = g * g;
  86. float phaseMie = end == maxEnd ? 0.1193662 * ((1.0 - gg) * mumu) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg)) : 0.0;
  87. phaseMie += getGlow(1.0 - mu, 5e-5, 3.0) * smoothstep(0.01, 0.1, length(scattering));
  88. phaseMie = clamp(phaseMie, 0.0, 128.0);
  89. return 3.0 * scattering * stepSize * (phaseRayleigh * betaRayleigh + 0.025 * phaseMie * betaMie);`}
  90. }
  91. vec3 tonemapACES(vec3 x) {
  92. return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0);
  93. }
  94. vec4 applyUndergroundAtmosphere(vec3 rayDir, vec3 lightDirection, vec4 fragColor) {
  95. vec2 rayPlanetIntersect = sphereIntersect(cameraPosition, rayDir, radii[0], true);
  96. if (!((rayPlanetIntersect.x <= rayPlanetIntersect.y) && rayPlanetIntersect.y > 0.0)) {
  97. return fragColor;
  98. }
  99. float lightAngle = dot(lightDirection, normalize(cameraPosition + rayDir * max(0.0, rayPlanetIntersect.x)));
  100. vec4 surfaceColor = vec4(vec3(max(0.0, (smoothstep(-1.0, 0.8, 2.0 * lightAngle)))), 1.0 - altitudeFade);
  101. float relDist = (rayPlanetIntersect.y - max(0.0, rayPlanetIntersect.x)) / innerFadeDistance;
  102. if (relDist > 1.0) {
  103. return surfaceColor;
  104. }
  105. return mix(gl_FragColor, surfaceColor, smoothstep(0.0, 1.0, relDist * relDist));
  106. }
  107. void main() {
  108. vec3 rayDir = normalize(worldRay);
  109. float terrainDepth = -1.0;
  110. ${t.haze?g`
  111. vec4 depthSample = texture2D(depthTex, vuv0).rgba;
  112. if (depthSample != vec4(0)) {
  113. vec3 cameraSpaceRay = normalize(eyeDir);
  114. cameraSpaceRay /= cameraSpaceRay.z;
  115. cameraSpaceRay *= -linearDepthFromTexture(depthTex, vuv0, nearFar);
  116. terrainDepth = max(0.0, length(cameraSpaceRay));
  117. }`:g`
  118. float depthSample = texture2D(depthTex, vuv0).r;
  119. if (depthSample != 1.0) {
  120. gl_FragColor = vec4(0);
  121. return;
  122. }`}
  123. ${t.haze?g`
  124. vec3 col = vec3(0);
  125. float fadeOut = smoothstep(-10000.0, -15000.0, heightParameters[0] - radii[0]);
  126. if(depthSample != vec4(0)){
  127. col = (1.0 - fadeOut) * hazeStrength * getAtmosphereColour(cameraPosition, rayDir, mainLightDirection, terrainDepth);
  128. }
  129. float alpha = 1.0 - fadeOut;`:g`
  130. vec3 col = getAtmosphereColour(cameraPosition, rayDir, mainLightDirection, terrainDepth);;
  131. float alpha = smoothstep(0.0, mix(0.15, 0.01, heightParameters[3]), length(col));`}
  132. col = tonemapACES(col);
  133. gl_FragColor = delinearizeGamma(vec4(col, alpha));
  134. ${t.haze?"":g`
  135. if (depthSample == 1.0) {
  136. gl_FragColor = applyUndergroundAtmosphere(rayDir, mainLightDirection, gl_FragColor);
  137. }`}
  138. }
  139. `),a}const S=t(),z=Object.freeze(Object.defineProperty({__proto__:null,betaRayleigh:x,build:F},Symbol.toStringTag,{value:"Module"}));export{z as C,F as a,x as b};