@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
45 lines (44 loc) • 3.64 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.32/esri/copyright.txt for details.
*/
import{IDENTITY as e}from"../../../../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{create as s}from"../../../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{Rgba4FloatEncoding as a}from"../util/RgbaFloat16Encoding.glsl.js";import{Float4BindUniform as t}from"../../shaderModules/Float4BindUniform.js";import{glsl as o}from"../../shaderModules/glsl.js";import{IntegerBindUniform as i}from"../../shaderModules/IntegerBindUniform.js";import{Matrix4sDrawUniform as r}from"../../shaderModules/Matrix4sDrawUniform.js";import{Matrix4sPassUniform as d}from"../../shaderModules/Matrix4sPassUniform.js";import{Texture2DBindUniform as c}from"../../shaderModules/Texture2DBindUniform.js";import{NoParameters as p}from"../../../../../webgl/NoParameters.js";class n extends p{constructor(){super(...arguments),this.origin=s()}}class l extends n{}class v extends p{constructor(){super(...arguments),this.modelTransformation=e}}class h extends v{constructor(){super(...arguments),this.origin=s()}}function m(e,s){s.receiveShadows&&(e.fragment.uniforms.add(new d("shadowMapMatrix",((e,s)=>s.shadowMap.getShadowMapMatrices(e.origin)),4)),f(e))}function x(e,s){s.receiveShadows&&(e.fragment.uniforms.add(new r("shadowMapMatrix",((e,s)=>s.shadowMap.getShadowMapMatrices(e.origin)),4)),f(e))}function f(e){const s=e.fragment;s.include(a),s.uniforms.add(new c("shadowMap",(e=>e.shadowMap.depthTexture)),new i("numCascades",(e=>e.shadowMap.numCascades)),new t("cascadeDistances",(e=>e.shadowMap.cascadeDistances))).code.add(o`int chooseCascade(float depth, out mat4 mat) {
vec4 distance = cascadeDistances;
int i = depth < distance[1] ? 0 : depth < distance[2] ? 1 : depth < distance[3] ? 2 : 3;
mat = i == 0 ? shadowMapMatrix[0] : i == 1 ? shadowMapMatrix[1] : i == 2 ? shadowMapMatrix[2] : shadowMapMatrix[3];
return i;
}
vec3 lightSpacePosition(vec3 _vpos, mat4 mat) {
vec4 lv = mat * vec4(_vpos, 1.0);
lv.xy /= lv.w;
return 0.5 * lv.xyz + vec3(0.5);
}
vec2 cascadeCoordinates(int i, ivec2 textureSize, vec3 lvpos) {
float xScale = float(textureSize.y) / float(textureSize.x);
return vec2((float(i) + lvpos.x) * xScale, lvpos.y);
}
float readShadowMapDepth(ivec2 uv, sampler2D _depthTex) {
return rgba4ToFloat(texelFetch(_depthTex, uv, 0));
}
float posIsInShadow(ivec2 uv, vec3 lvpos, sampler2D _depthTex) {
return readShadowMapDepth(uv, _depthTex) < lvpos.z ? 1.0 : 0.0;
}
float filterShadow(vec2 uv, vec3 lvpos, ivec2 texSize, sampler2D _depthTex) {
vec2 st = fract(uv * vec2(texSize) + vec2(0.5));
ivec2 base = ivec2(uv * vec2(texSize) - vec2(0.5));
float s00 = posIsInShadow(ivec2(base.x, base.y), lvpos, _depthTex);
float s10 = posIsInShadow(ivec2(base.x + 1, base.y), lvpos, _depthTex);
float s11 = posIsInShadow(ivec2(base.x + 1, base.y + 1), lvpos, _depthTex);
float s01 = posIsInShadow(ivec2(base.x, base.y + 1), lvpos, _depthTex);
return mix(mix(s00, s10, st.x), mix(s01, s11, st.x), st.y);
}
float readShadowMap(const in vec3 _vpos, float _linearDepth) {
mat4 mat;
int i = chooseCascade(_linearDepth, mat);
if (i >= numCascades) { return 0.0; }
vec3 lvpos = lightSpacePosition(_vpos, mat);
if (lvpos.z >= 1.0 || lvpos.x < 0.0 || lvpos.x > 1.0 || lvpos.y < 0.0 || lvpos.y > 1.0) { return 0.0; }
ivec2 size = textureSize(shadowMap, 0);
vec2 uv = cascadeCoordinates(i, size, lvpos);
return filterShadow(uv, lvpos, size, shadowMap);
}`)}export{x as ReadShadowMapDraw,l as ReadShadowMapDrawParameters,v as ReadShadowMapParameters,m as ReadShadowMapPass,h as ReadShadowMapPassParameters};