UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

88 lines (73 loc) 4.92 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import{set as e}from"../core/libs/gl-matrix-2/math/vec2.js";import{create as r}from"../core/libs/gl-matrix-2/factories/vec2f64.js";import{ScreenSpacePass as t}from"../views/3d/webgl-engine/core/shaderLibrary/ScreenSpacePass.glsl.js";import{ReadDepth as o}from"../views/3d/webgl-engine/core/shaderLibrary/output/ReadDepth.glsl.js";import{CameraSpace as a}from"../views/3d/webgl-engine/core/shaderLibrary/util/CameraSpace.glsl.js";import{Float2BindUniform as i}from"../views/3d/webgl-engine/core/shaderModules/Float2BindUniform.js";import{Float2PassUniform as n}from"../views/3d/webgl-engine/core/shaderModules/Float2PassUniform.js";import{FloatBindUniform as s}from"../views/3d/webgl-engine/core/shaderModules/FloatBindUniform.js";import{FloatPassUniform as c}from"../views/3d/webgl-engine/core/shaderModules/FloatPassUniform.js";import{glsl as l}from"../views/3d/webgl-engine/core/shaderModules/glsl.js";import{Texture2DPassUniform as u}from"../views/3d/webgl-engine/core/shaderModules/Texture2DPassUniform.js";import{ShaderBuilder as f}from"../views/webgl/ShaderBuilder.js";const d=16;function m(){const r=new f,m=r.fragment;return r.include(t),r.include(a),m.include(o),m.uniforms.add(new s("radius",(e=>v(e.camera)))).code.add(l`vec3 sphere[16] = vec3[16]( vec3(0.186937, 0.0, 0.0), vec3(0.700542, 0.0, 0.0), vec3(-0.864858, -0.481795, -0.111713), vec3(-0.624773, 0.102853, -0.730153), vec3(-0.387172, 0.260319, 0.007229), vec3(-0.222367, -0.642631, -0.707697), vec3(-0.01336, -0.014956, 0.169662), vec3(0.122575, 0.1544, -0.456944), vec3(-0.177141, 0.85997, -0.42346), vec3(-0.131631, 0.814545, 0.524355), vec3(-0.779469, 0.007991, 0.624833), vec3(0.308092, 0.209288,0.35969), vec3(0.359331, -0.184533, -0.377458), vec3(0.192633, -0.482999, -0.065284), vec3(0.233538, 0.293706, -0.055139), vec3(0.417709, -0.386701, 0.442449) ); float fallOffFunction(float vv, float vn, float bias) { float f = max(radius * radius - vv, 0.0); return f * f * f * max(vn - bias, 0.0); }`),m.code.add(l`float aoValueFromPositionsAndNormal(vec3 C, vec3 n_C, vec3 Q) { vec3 v = Q - C; float vv = dot(v, v); float vn = dot(normalize(v), n_C); return fallOffFunction(vv, vn, 0.1); }`),r.outputs.add("fragOcclusion","float"),m.uniforms.add(new u("normalMap",(e=>e.normalTexture)),new u("depthMap",(e=>e.depthTexture)),new c("projScale",(e=>e.projScale)),new u("rnm",(e=>e.noiseTexture)),new n("rnmScale",((r,t)=>e(p,t.camera.fullWidth/r.noiseTexture.descriptor.width,t.camera.fullHeight/r.noiseTexture.descriptor.height))),new c("intensity",(e=>e.intensity)),new i("screenSize",(r=>e(p,r.camera.fullWidth,r.camera.fullHeight)))).main.add(l` float depth = depthFromTexture(depthMap, uv); // Early out if depth is out of range, such as in the sky if (depth >= 1.0 || depth <= 0.0) { fragOcclusion = 1.0; return; } // get the normal of current fragment ivec2 iuv = ivec2(uv * vec2(textureSize(normalMap, 0))); vec4 norm4 = texelFetch(normalMap, iuv, 0); if(norm4.a != 1.0) { fragOcclusion = 1.0; return; } vec3 norm = normalize(norm4.xyz * 2.0 - 1.0); float currentPixelDepth = linearizeDepth(depth); vec3 currentPixelPos = reconstructPosition(gl_FragCoord.xy, currentPixelDepth); float sum = 0.0; vec3 tapPixelPos; vec3 fres = normalize(2.0 * texture(rnm, uv * rnmScale).xyz - 1.0); // note: the factor 2.0 should not be necessary, but makes ssao much nicer. // bug or deviation from CE somewhere else? float ps = projScale / (2.0 * currentPixelPos.z * zScale.x + zScale.y); for(int i = 0; i < ${l.int(d)}; ++i) { vec2 unitOffset = reflect(sphere[i], fres).xy; vec2 offset = vec2(-unitOffset * radius * ps); // don't use current or very nearby samples if( abs(offset.x) < 2.0 || abs(offset.y) < 2.0){ continue; } vec2 tc = vec2(gl_FragCoord.xy + offset); if (tc.x < 0.0 || tc.y < 0.0 || tc.x > screenSize.x || tc.y > screenSize.y) continue; vec2 tcTap = tc / screenSize; float occluderFragmentDepth = linearDepthFromTexture(depthMap, tcTap); tapPixelPos = reconstructPosition(tc, occluderFragmentDepth); sum += aoValueFromPositionsAndNormal(currentPixelPos, norm, tapPixelPos); } // output the result float A = max(1.0 - sum * intensity / float(${l.int(d)}), 0.0); // Anti-tone map to reduce contrast and drag dark region farther: (x^0.2 + 1.2 * x^4) / 2.2 A = (pow(A, 0.2) + 1.2 * A * A * A * A) / 2.2; fragOcclusion = A; `),r}function v(e){return Math.max(10,20*e.computeScreenPixelSizeAtDist(Math.abs(4*e.relativeElevation)))}const p=r(),g=Object.freeze(Object.defineProperty({__proto__:null,build:m,getRadius:v},Symbol.toStringTag,{value:"Module"}));export{g as S,m as b,v as g};