FogHaze.glsl.js 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 r}from"./mat4f64.js";import{c as a}from"./vec3f64.js";import{TextureCoordinateAttribute as o,TextureCoordinateAttributeType as t}from"../views/3d/webgl-engine/core/shaderLibrary/attributes/TextureCoordinateAttribute.glsl.js";import{ReadLinearDepth as i}from"../views/3d/webgl-engine/core/shaderLibrary/output/ReadLinearDepth.glsl.js";import{Gamma as n}from"../views/3d/webgl-engine/core/shaderLibrary/shading/Gamma.glsl.js";import{Float2PassUniform as s}from"../views/3d/webgl-engine/core/shaderModules/Float2PassUniform.js";import{Float3PassUniform as d}from"../views/3d/webgl-engine/core/shaderModules/Float3PassUniform.js";import{FloatPassUniform as m}from"../views/3d/webgl-engine/core/shaderModules/FloatPassUniform.js";import{NoParameters as c,glsl as l}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 g}from"../views/3d/webgl-engine/core/shaderModules/ShaderBuilder.js";import{Texture2DPassUniform as p}from"../views/3d/webgl-engine/core/shaderModules/Texture2DPassUniform.js";import{VertexAttribute as h}from"../views/3d/webgl-engine/lib/VertexAttribute.js";class u extends c{constructor(){super(...arguments),this.fogColor=a(),this.hazeColor=a(),this.fogStrength=4e-6,this.hazeStrength=4e-6,this.atmosphereC=1}}function v(r){const a=new g;a.attributes.add(h.POSITION,"vec2"),a.include(o,{textureCoordinateType:t.Default}),a.varyings.add("worldRay","vec3"),a.varyings.add("eyeDir","vec3");const{vertex:c,fragment:u}=a;return c.uniforms.add([new f("inverseProjectionMatrix",((e,r)=>r.camera.inverseProjectionMatrix)),new f("inverseViewMatrix",((r,a)=>e(w,a.camera.viewMatrix)))]),c.code.add(l`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. }`),u.uniforms.add(new m("atmosphereC",(e=>e.atmosphereC))),u.uniforms.add(new d("cameraPosition",((e,r)=>r.camera.eye))),u.uniforms.add(new s("nearFar",((e,r)=>r.camera.nearFar))),u.uniforms.add(new p("depthTex",(e=>e.depthTexture))),u.uniforms.add(new m("fogStrength",(e=>r.haze?e.hazeStrength:e.fogStrength))),u.uniforms.add(new m("fogAmount",(e=>r.haze?e.hazeAmount:e.fogAmount))),u.uniforms.add(new d("fogColor",(e=>r.haze?e.hazeColor:e.fogColor))),a.include(n),u.include(i),u.code.add(l`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. }`),u.code.add(l`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. }`),u.code.add(l`
  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?l`
  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. `),a}const w=r(),y=Object.freeze(Object.defineProperty({__proto__:null,FogHazePassParameters:u,build:v},Symbol.toStringTag,{value:"Module"}));export{u as F,y as H,v as b};