Fog.glsl.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.24/esri/copyright.txt for details.
  4. */
  5. import{a as e}from"./mat4.js";import{c as r}from"./mat4f64.js";import{TextureCoordinateAttribute as a,TextureCoordinateAttributeType as o}from"../views/3d/webgl-engine/core/shaderLibrary/attributes/TextureCoordinateAttribute.glsl.js";import{ReadLinearDepth as t}from"../views/3d/webgl-engine/core/shaderLibrary/output/ReadLinearDepth.glsl.js";import{Gamma as i}from"../views/3d/webgl-engine/core/shaderLibrary/shading/Gamma.glsl.js";import{Float2PassUniform as n}from"../views/3d/webgl-engine/core/shaderModules/Float2PassUniform.js";import{Float3Uniform as d}from"../views/3d/webgl-engine/core/shaderModules/Float3Uniform.js";import{FloatUniform as s}from"../views/3d/webgl-engine/core/shaderModules/FloatUniform.js";import{glsl as m}from"../views/3d/webgl-engine/core/shaderModules/interfaces.js";import{Matrix4PassUniform as c}from"../views/3d/webgl-engine/core/shaderModules/Matrix4PassUniform.js";import{ShaderBuilder as l}from"../views/3d/webgl-engine/core/shaderModules/ShaderBuilder.js";import{Texture2DUniform as f}from"../views/3d/webgl-engine/core/shaderModules/Texture2DUniform.js";import{VertexAttribute as p}from"../views/3d/webgl-engine/lib/VertexAttribute.js";function g(r){const g=new l;g.attributes.add(p.POSITION,"vec2"),g.include(a,{textureCoordinateType:o.Default}),g.varyings.add("worldRay","vec3"),g.varyings.add("eyeDir","vec3");const{vertex:u,fragment:w}=g;return u.uniforms.add([new c("inverseProjectionMatrix",((e,r)=>r.camera.inverseProjectionMatrix)),new c("inverseViewMatrix",((r,a)=>e(v,a.camera.viewMatrix)))]),u.code.add(m`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. }`),w.uniforms.add(new s("atmosphereC")),w.uniforms.add(new d("cameraPosition")),w.uniforms.add(new n("nearFar",((e,r)=>r.camera.nearFar))),w.uniforms.add(new f("depthTex")),w.uniforms.add(new s("fogStrength")),w.uniforms.add(new s("fogAmount")),w.uniforms.add(new d("fogColor")),g.include(i),w.include(t),w.code.add(m`vec2 sphereIntersect(vec3 start, vec3 dir) {
  12. float a = dot(dir, dir);
  13. float b = 2.0 * dot(dir, start);
  14. float d = (b * b) - 4.0 * a * atmosphereC;
  15. if (d < 0.0) {
  16. return vec2(1e5, -1e5);
  17. }
  18. return vec2((-b - sqrt(d)) / (2.0 * a), (-b + sqrt(d)) / (2.0 * a));
  19. }`),w.code.add(m`vec4 applyFog(float dist, vec3 rayDir){
  20. if(dist == -1.0){
  21. vec2 rayAtmosphereIntersect = sphereIntersect(cameraPosition, rayDir);
  22. dist = 0.055 * rayAtmosphereIntersect.y;
  23. }
  24. float fogAmount = fogAmount * (1.0 - exp(-dist * fogStrength));
  25. return vec4(fogAmount * fogColor, fogAmount);
  26. }`),w.code.add(m`
  27. vec3 tonemapACES(vec3 x) {
  28. return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0);
  29. }
  30. void main() {
  31. vec3 rayDir = normalize(worldRay);
  32. float terrainDepth = -1.0;
  33. float depthSample = texture2D(depthTex, vuv0).r;
  34. float zNorm = 2.0 * depthSample - 1.0;
  35. float linDepth = 2.0 * nearFar[0] * nearFar[1] / (nearFar[1] + nearFar[0] - zNorm * (nearFar[1] - nearFar[0]));
  36. if(depthSample < 1.0 && depthSample > 0.0){
  37. vec3 cameraSpaceRay = normalize(eyeDir);
  38. cameraSpaceRay /= cameraSpaceRay.z;
  39. cameraSpaceRay *= linDepth;
  40. terrainDepth = max(0.0, length(cameraSpaceRay));
  41. }
  42. ${r.haze?m`
  43. if(terrainDepth == -1.0){
  44. gl_FragColor = vec4(0);
  45. return;
  46. }`:""}
  47. vec4 fog = applyFog(terrainDepth, rayDir);
  48. gl_FragColor = delinearizeGamma(vec4(tonemapACES(fog.rgb), fog.a));
  49. }
  50. `),g}const v=r(),u=Object.freeze(Object.defineProperty({__proto__:null,build:g},Symbol.toStringTag,{value:"Module"}));export{u as F,g as b};