@navaramap/three-csm 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +8 -0
- package/README.md +5 -0
- package/dist/index.d.ts +197 -0
- package/dist/index.js +483 -0
- package/dist/index.umd.cjs +306 -0
- package/package.json +38 -0
- package/src/CSMHelper.ts +199 -0
- package/src/CascadedDirectionalLights.ts +80 -0
- package/src/CascadedShadowMaps.ts +475 -0
- package/src/FrustumCorners.ts +111 -0
- package/src/MaterialStates.ts +154 -0
- package/src/createFragmentShader.ts +92 -0
- package/src/index.ts +5 -0
- package/src/shaders/cascadedFadedLights.glsl +102 -0
- package/src/shaders/cascadedLights.glsl +81 -0
- package/src/shaders/cascadedLightsWithoutShadows.glsl +35 -0
- package/src/shaders/defaultLights.glsl +36 -0
- package/src/shaders/nonShadowCastingLights.glsl +18 -0
- package/src/splitFrustum.ts +59 -0
- package/src/vite-env.d.ts +1 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
import { Box3 as e, Box3Helper as t, BufferAttribute as n, BufferGeometry as r, Color as i, DirectionalLight as a, DoubleSide as o, Group as s, LineBasicMaterial as c, LineSegments as l, Matrix4 as u, Mesh as d, MeshBasicMaterial as f, Object3D as p, PlaneGeometry as m, ShaderChunk as h, Vector2 as g, Vector3 as _ } from "three";
|
|
2
|
+
//#region src/CascadedDirectionalLights.ts
|
|
3
|
+
var v = new i(16777215), y = class extends p {
|
|
4
|
+
mainLight;
|
|
5
|
+
cascadedLights;
|
|
6
|
+
direction = new _(-1, -1, -1);
|
|
7
|
+
constructor(e, t) {
|
|
8
|
+
super();
|
|
9
|
+
let n = new a(e, t);
|
|
10
|
+
n.castShadow = !0, this.mainLight = n, this.cascadedLights = [n], this.add(n), this.add(n.target);
|
|
11
|
+
}
|
|
12
|
+
setCount(e) {
|
|
13
|
+
e = Math.max(1, e);
|
|
14
|
+
let t = this.cascadedLights.length;
|
|
15
|
+
if (e === t) return this;
|
|
16
|
+
if (e > t) {
|
|
17
|
+
let n = this.mainLight;
|
|
18
|
+
for (let r = t; r < e; ++r) {
|
|
19
|
+
let e = n.clone();
|
|
20
|
+
e.color.copy(v), this.add(e), this.add(e.target), this.cascadedLights[r] = e;
|
|
21
|
+
}
|
|
22
|
+
} else for (let n = e; n < t; ++n) {
|
|
23
|
+
let e = this.cascadedLights[n];
|
|
24
|
+
e.dispose(), this.remove(e.target), this.remove(e);
|
|
25
|
+
}
|
|
26
|
+
return this.cascadedLights.length = e, this;
|
|
27
|
+
}
|
|
28
|
+
dispose() {
|
|
29
|
+
for (let e = 0; e < this.cascadedLights.length; ++e) this.cascadedLights[e].dispose();
|
|
30
|
+
}
|
|
31
|
+
copy(e, t) {
|
|
32
|
+
super.copy(e, !1), this.cascadedLights.length = 0, this.clear();
|
|
33
|
+
for (let t of e.cascadedLights) {
|
|
34
|
+
let e = t.clone();
|
|
35
|
+
this.add(e), this.add(e.target), this.cascadedLights.push(e);
|
|
36
|
+
}
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
}, b = "Invariant failed";
|
|
40
|
+
function x(e, t) {
|
|
41
|
+
if (!e) throw Error(b);
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/FrustumCorners.ts
|
|
45
|
+
var S = class e {
|
|
46
|
+
near = [
|
|
47
|
+
new _(),
|
|
48
|
+
new _(),
|
|
49
|
+
new _(),
|
|
50
|
+
new _()
|
|
51
|
+
];
|
|
52
|
+
far = [
|
|
53
|
+
new _(),
|
|
54
|
+
new _(),
|
|
55
|
+
new _(),
|
|
56
|
+
new _()
|
|
57
|
+
];
|
|
58
|
+
constructor(e, t) {
|
|
59
|
+
e != null && t != null && this.setFromCamera(e, t);
|
|
60
|
+
}
|
|
61
|
+
clone() {
|
|
62
|
+
return new e().copy(this);
|
|
63
|
+
}
|
|
64
|
+
copy(e) {
|
|
65
|
+
for (let t = 0; t < 4; ++t) this.near[t].copy(e.near[t]), this.far[t].copy(e.far[t]);
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
setFromCamera(e, t) {
|
|
69
|
+
let n = e.isOrthographicCamera === !0, r = e.projectionMatrixInverse;
|
|
70
|
+
this.near[0].set(1, 1, -1), this.near[1].set(1, -1, -1), this.near[2].set(-1, -1, -1), this.near[3].set(-1, 1, -1);
|
|
71
|
+
for (let e = 0; e < 4; ++e) this.near[e].applyMatrix4(r);
|
|
72
|
+
this.far[0].set(1, 1, 1), this.far[1].set(1, -1, 1), this.far[2].set(-1, -1, 1), this.far[3].set(-1, 1, 1);
|
|
73
|
+
for (let e = 0; e < 4; ++e) {
|
|
74
|
+
let i = this.far[e];
|
|
75
|
+
i.applyMatrix4(r);
|
|
76
|
+
let a = Math.abs(i.z);
|
|
77
|
+
n ? i.z *= Math.min(t / a, 1) : i.multiplyScalar(Math.min(t / a, 1));
|
|
78
|
+
}
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
split(t, n = []) {
|
|
82
|
+
for (let r = 0; r < t.length; ++r) {
|
|
83
|
+
let i = n[r] ??= new e();
|
|
84
|
+
if (r === 0) for (let e = 0; e < 4; ++e) i.near[e].copy(this.near[e]);
|
|
85
|
+
else for (let e = 0; e < 4; ++e) i.near[e].lerpVectors(this.near[e], this.far[e], t[r - 1]);
|
|
86
|
+
if (r === t.length - 1) for (let e = 0; e < 4; ++e) i.far[e].copy(this.far[e]);
|
|
87
|
+
else for (let e = 0; e < 4; ++e) i.far[e].lerpVectors(this.near[e], this.far[e], t[r]);
|
|
88
|
+
}
|
|
89
|
+
return n.length = t.length, n;
|
|
90
|
+
}
|
|
91
|
+
applyMatrix4(e) {
|
|
92
|
+
for (let t = 0; t < 4; ++t) this.near[t].applyMatrix4(e), this.far[t].applyMatrix4(e);
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
}, C = "// Based on the following work:\n// https://github.com/StrandedKitty/three-csm/tree/master/src\n// https://github.com/mrdoob/three.js/tree/r169/examples/jsm/csm\n\nvoid cascadedFadedLights() {\n directionalLight = directionalLights[0];\n getDirectionalLightInfo(directionalLight, directLight);\n\n float clipDepth = vViewPosition.z / (csmFar - csmNear);\n bool lastCascade;\n vec2 cascade;\n float cascadeCenter;\n float closestEdge;\n float margin;\n float shadow = 1.0;\n float nextShadow = 1.0;\n\n #pragma unroll_loop_start\n for (int i = 0; i < NUM_DIR_LIGHTS; i++) {\n #if UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS\n #if UNROLLED_LOOP_INDEX < CSM_CASCADE_COUNT\n\n lastCascade = UNROLLED_LOOP_INDEX == CSM_CASCADE_COUNT - 1;\n cascade = csmCascades[i];\n cascadeCenter = (cascade.x + cascade.y) * 0.5;\n closestEdge = clipDepth < cascadeCenter ? cascade.x : cascade.y;\n margin = 0.25 * pow(closestEdge, 2.0);\n cascade += margin * vec2(-0.5, 0.5);\n\n if (clipDepth >= cascade.x && (clipDepth < cascade.y || lastCascade)) {\n directionalLightShadow = directionalLightShadows[i];\n float prevShadow = nextShadow;\n nextShadow =\n directLight.visible && receiveShadow\n ? getShadow(\n directionalShadowMap[i],\n directionalLightShadow.shadowMapSize,\n directionalLightShadow.shadowIntensity,\n directionalLightShadow.shadowBias,\n directionalLightShadow.shadowRadius,\n vDirectionalShadowCoord[i]\n )\n : 1.0;\n\n shadow = mix(\n prevShadow,\n nextShadow,\n saturate(min(clipDepth - cascade.x, cascade.y - clipDepth) / margin)\n );\n }\n #endif // UNROLLED_LOOP_INDEX < CSM_CASCADE_COUNT\n #endif // UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS\n }\n #pragma unroll_loop_end\n\n // No loop here: all cascaded lights are in fact one light only.\n directLight.color *= shadow;\n RE_Direct(\n directLight,\n geometryPosition,\n geometryNormal,\n geometryViewDir,\n geometryClearcoatNormal,\n material,\n reflectedLight\n );\n\n // The rest of directional lights.\n #pragma unroll_loop_start\n for (int i = 0; i < NUM_DIR_LIGHTS; i++) {\n #if UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS\n #if UNROLLED_LOOP_INDEX >= CSM_CASCADE_COUNT\n directionalLight = directionalLights[i];\n getDirectionalLightInfo(directionalLight, directLight);\n\n directionalLightShadow = directionalLightShadows[i];\n directLight.color *=\n directLight.visible && receiveShadow\n ? getShadow(\n directionalShadowMap[i],\n directionalLightShadow.shadowMapSize,\n directionalLightShadow.shadowIntensity,\n directionalLightShadow.shadowBias,\n directionalLightShadow.shadowRadius,\n vDirectionalShadowCoord[i]\n )\n : 1.0;\n\n RE_Direct(\n directLight,\n geometryPosition,\n geometryNormal,\n geometryViewDir,\n geometryClearcoatNormal,\n material,\n reflectedLight\n );\n #endif // UNROLLED_LOOP_INDEX >= CSM_CASCADE_COUNT\n #endif // UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS\n }\n #pragma unroll_loop_end\n}\n", w = "// Based on the following work:\n// https://github.com/StrandedKitty/three-csm/tree/master/src\n// https://github.com/mrdoob/three.js/tree/r169/examples/jsm/csm\n\nvoid cascadedLights() {\n directionalLight = directionalLights[0];\n getDirectionalLightInfo(directionalLight, directLight);\n\n float clipDepth = vViewPosition.z / (csmFar - csmNear);\n vec2 cascade;\n\n #pragma unroll_loop_start\n for (int i = 0; i < NUM_DIR_LIGHTS; i++) {\n #if UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS\n #if UNROLLED_LOOP_INDEX < CSM_CASCADE_COUNT\n cascade = csmCascades[i];\n directionalLightShadow = directionalLightShadows[i];\n if (clipDepth >= cascade.x && clipDepth < cascade.y) {\n directLight.color *=\n directLight.visible && receiveShadow\n ? getShadow(\n directionalShadowMap[i],\n directionalLightShadow.shadowMapSize,\n directionalLightShadow.shadowIntensity,\n directionalLightShadow.shadowBias,\n directionalLightShadow.shadowRadius,\n vDirectionalShadowCoord[i]\n )\n : 1.0;\n }\n #endif // UNROLLED_LOOP_INDEX < CSM_CASCADE_COUNT\n #endif // UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS\n }\n #pragma unroll_loop_end\n\n // No loop here: all cascaded lights are in fact one light only.\n RE_Direct(\n directLight,\n geometryPosition,\n geometryNormal,\n geometryViewDir,\n geometryClearcoatNormal,\n material,\n reflectedLight\n );\n\n // The rest of directional lights.\n #pragma unroll_loop_start\n for (int i = 0; i < NUM_DIR_LIGHTS; i++) {\n #if UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS\n #if UNROLLED_LOOP_INDEX >= CSM_CASCADE_COUNT\n directionalLight = directionalLights[i];\n getDirectionalLightInfo(directionalLight, directLight);\n\n directionalLightShadow = directionalLightShadows[i];\n directLight.color *=\n directLight.visible && receiveShadow\n ? getShadow(\n directionalShadowMap[i],\n directionalLightShadow.shadowMapSize,\n directionalLightShadow.shadowIntensity,\n directionalLightShadow.shadowBias,\n directionalLightShadow.shadowRadius,\n vDirectionalShadowCoord[i]\n )\n : 1.0;\n\n RE_Direct(\n directLight,\n geometryPosition,\n geometryNormal,\n geometryViewDir,\n geometryClearcoatNormal,\n material,\n reflectedLight\n );\n #endif // UNROLLED_LOOP_INDEX >= CSM_CASCADE_COUNT\n #endif // UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS\n }\n #pragma unroll_loop_end\n}\n", T = "// Based on the following work:\n// https://github.com/StrandedKitty/three-csm/tree/master/src\n// https://github.com/mrdoob/three.js/tree/r169/examples/jsm/csm\n\nvoid cascadedLightsWithoutShadows() {\n // No loop here: all cascaded lights are in fact one light only.\n getDirectionalLightInfo(directionalLights[0], directLight);\n RE_Direct(\n directLight,\n geometryPosition,\n geometryNormal,\n geometryViewDir,\n geometryClearcoatNormal,\n material,\n reflectedLight\n );\n\n // The rest of directional lights.\n #pragma unroll_loop_start\n for (int i = CSM_CASCADE_COUNT; i < NUM_DIR_LIGHTS; i++) {\n directionalLight = directionalLights[i];\n getDirectionalLightInfo(directionalLight, directLight);\n\n RE_Direct(\n directLight,\n geometryPosition,\n geometryNormal,\n geometryViewDir,\n geometryClearcoatNormal,\n material,\n reflectedLight\n );\n }\n #pragma unroll_loop_end\n}\n", E = "// Default shader.\n// See: https://github.com/mrdoob/three.js/blob/r169/src/renderers/shaders/ShaderChunk/lights_fragment_begin.glsl.js#L136\n\nvoid defaultLights() {\n #pragma unroll_loop_start\n for (int i = 0; i < NUM_DIR_LIGHTS; i++) {\n directionalLight = directionalLights[i];\n getDirectionalLightInfo(directionalLight, directLight);\n\n #if defined(USE_SHADOWMAP) && UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS\n directionalLightShadow = directionalLightShadows[i];\n directLight.color *=\n directLight.visible && receiveShadow\n ? getShadow(\n directionalShadowMap[i],\n directionalLightShadow.shadowMapSize,\n directionalLightShadow.shadowIntensity,\n directionalLightShadow.shadowBias,\n directionalLightShadow.shadowRadius,\n vDirectionalShadowCoord[i]\n )\n : 1.0;\n #endif\n\n RE_Direct(\n directLight,\n geometryPosition,\n geometryNormal,\n geometryViewDir,\n geometryClearcoatNormal,\n material,\n reflectedLight\n );\n }\n #pragma unroll_loop_end\n}\n", D = "void nonShadowCastingLights() {\n #pragma unroll_loop_start\n for (int i = NUM_DIR_LIGHT_SHADOWS; i < NUM_DIR_LIGHTS; i++) {\n directionalLight = directionalLights[i];\n getDirectionalLightInfo(directionalLight, directLight);\n\n RE_Direct(\n directLight,\n geometryPosition,\n geometryNormal,\n geometryViewDir,\n geometryClearcoatNormal,\n material,\n reflectedLight\n );\n }\n #pragma unroll_loop_end\n}\n", O = "\n #include <lights_pars_begin>\n\n #ifdef CSM\n uniform vec2 csmCascades[CSM_CASCADE_COUNT];\n uniform float csmNear;\n uniform float csmFar;\n #endif // CSM\n";
|
|
96
|
+
function k(e) {
|
|
97
|
+
return e.replace(/^\s*void\s+\w+\s*\(\s*\)\s*/m, "");
|
|
98
|
+
}
|
|
99
|
+
var A = `
|
|
100
|
+
#if NUM_DIR_LIGHTS > 0 && defined(RE_Direct)
|
|
101
|
+
DirectionalLight directionalLight;
|
|
102
|
+
#if defined(USE_SHADOWMAP) && NUM_DIR_LIGHT_SHADOWS > 0
|
|
103
|
+
DirectionalLightShadow directionalLightShadow;
|
|
104
|
+
#endif
|
|
105
|
+
|
|
106
|
+
#ifdef CSM
|
|
107
|
+
#ifdef USE_SHADOWMAP
|
|
108
|
+
#ifdef CSM_FADE
|
|
109
|
+
${k(C)}
|
|
110
|
+
#else
|
|
111
|
+
${k(w)}
|
|
112
|
+
#endif // CSM_FADE
|
|
113
|
+
#elif NUM_DIR_LIGHT_SHADOWS > 0
|
|
114
|
+
${k(T)}
|
|
115
|
+
#endif // USE_SHADOWMAP
|
|
116
|
+
|
|
117
|
+
#if NUM_DIR_LIGHTS > NUM_DIR_LIGHT_SHADOWS
|
|
118
|
+
${k(D)}
|
|
119
|
+
#endif
|
|
120
|
+
#else
|
|
121
|
+
${k(E)}
|
|
122
|
+
#endif // CSM
|
|
123
|
+
#endif // NUM_DIR_LIGHTS > 0 && defined(RE_Direct)
|
|
124
|
+
`;
|
|
125
|
+
function j({ source: e, startLine: t, endLine: n, replacement: r }) {
|
|
126
|
+
let i = `\n${t}\n`, a = e.indexOf(i);
|
|
127
|
+
if (a < 0) return e;
|
|
128
|
+
let o = `\n${n}\n`, s = e.indexOf(o, a + i.length);
|
|
129
|
+
return s < 0 ? e : e.slice(0, a + 1) + r + e.slice(s + o.length - 1);
|
|
130
|
+
}
|
|
131
|
+
function M(e) {
|
|
132
|
+
return e.replace("#include <lights_pars_begin>", O).replace("#include <lights_fragment_begin>", j({
|
|
133
|
+
source: h.lights_fragment_begin,
|
|
134
|
+
startLine: "#if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct )",
|
|
135
|
+
endLine: "#endif",
|
|
136
|
+
replacement: A
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
//#endregion
|
|
140
|
+
//#region src/MaterialStates.ts
|
|
141
|
+
var N = class {
|
|
142
|
+
map = /* @__PURE__ */ new Map();
|
|
143
|
+
setup(e, t) {
|
|
144
|
+
if (this.map.has(e)) return e;
|
|
145
|
+
e.userData.defines ??= {}, e.userData.defines.CSM = 1, e.userData.defines.CSM_CASCADE_COUNT = t.cascadeCount, t.fade && (e.userData.defines.CSM_FADE = 1);
|
|
146
|
+
let n = {};
|
|
147
|
+
this.map.set(e, n);
|
|
148
|
+
let r = (e) => {
|
|
149
|
+
n.parameters = e;
|
|
150
|
+
let r = t.cascades, i = t.mainCamera.near, a = Math.min(t.mainCamera.far, t.far), o = e.uniforms;
|
|
151
|
+
o.csmCascades = { value: r }, o.csmNear = { value: i }, o.csmFar = { value: a }, e.fragmentShader = M(e.fragmentShader);
|
|
152
|
+
}, i = e.onBeforeCompile.bind(e), a = e.customProgramCacheKey.bind(e);
|
|
153
|
+
Object.prototype.hasOwnProperty.call(e, "hasOwnProperty") && (n.originalHandler = i), e.customProgramCacheKey = () => i.toString() + a(), e.onBeforeCompile = (...t) => {
|
|
154
|
+
let [n] = t;
|
|
155
|
+
n.defines ??= {}, Object.assign(n.defines, e.userData.defines), i.apply(e, t), r(...t);
|
|
156
|
+
};
|
|
157
|
+
let o = () => {
|
|
158
|
+
this.map.delete(e), e.removeEventListener("dispose", o);
|
|
159
|
+
};
|
|
160
|
+
return e.addEventListener("dispose", o), e;
|
|
161
|
+
}
|
|
162
|
+
update(e) {
|
|
163
|
+
let t = e.cascadeCount, n = e.fade, r = e.cascades, i = e.mainCamera.near, a = Math.min(e.mainCamera.far, e.far);
|
|
164
|
+
for (let [e, o] of this.map.entries()) {
|
|
165
|
+
let s = e.userData.defines;
|
|
166
|
+
if (s != null) {
|
|
167
|
+
let r = !1;
|
|
168
|
+
s.CSM_FADE != null !== n && (n ? s.CSM_FADE = 1 : delete s.CSM_FADE, r = !0), s.CSM_CASCADE_COUNT !== t && (s.CSM_CASCADE_COUNT = t, r = !0), r && (e.needsUpdate = !0);
|
|
169
|
+
}
|
|
170
|
+
let c = o.parameters?.uniforms;
|
|
171
|
+
c != null && (c.csmCascades.value = r, c.csmNear.value = i, c.csmFar.value = a);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
rollback(e) {
|
|
175
|
+
let t = this.map.get(e);
|
|
176
|
+
return t == null ? e : this.rollbackToState(e, t);
|
|
177
|
+
}
|
|
178
|
+
rollbackToState(e, { parameters: t, originalHandler: n }) {
|
|
179
|
+
return t != null && (delete t.uniforms.csmCascades, delete t.uniforms.csmNear, delete t.uniforms.csmFar), n == null ? delete e.onBeforeCompile : e.onBeforeCompile = n, e.userData.defines != null && (delete e.userData.defines.CSM, delete e.userData.defines.CSM_CASCADE_COUNT, delete e.userData.defines.CSM_FADE), e.needsUpdate = !0, this.map.delete(e), e;
|
|
180
|
+
}
|
|
181
|
+
dispose() {
|
|
182
|
+
for (let [e, t] of this.map) this.rollbackToState(e, t);
|
|
183
|
+
this.map.clear();
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
Math.PI / 180, 180 / Math.PI;
|
|
187
|
+
function P(e, t, n) {
|
|
188
|
+
return (1 - n) * e + n * t;
|
|
189
|
+
}
|
|
190
|
+
//#endregion
|
|
191
|
+
//#region src/splitFrustum.ts
|
|
192
|
+
var F = {
|
|
193
|
+
uniform: (e, t, n, r, i = []) => {
|
|
194
|
+
for (let r = 0; r < e; ++r) i[r] = (t + (n - t) * (r + 1) / e) / n;
|
|
195
|
+
return i.length = e, i;
|
|
196
|
+
},
|
|
197
|
+
logarithmic: (e, t, n, r, i = []) => {
|
|
198
|
+
for (let r = 0; r < e; ++r) i[r] = t * (n / t) ** ((r + 1) / e) / n;
|
|
199
|
+
return i.length = e, i;
|
|
200
|
+
},
|
|
201
|
+
practical: (e, t, n, r = .5, i = []) => {
|
|
202
|
+
for (let a = 0; a < e; ++a) i[a] = P((t + (n - t) * (a + 1) / e) / n, t * (n / t) ** ((a + 1) / e) / n, r);
|
|
203
|
+
return i.length = e, i;
|
|
204
|
+
}
|
|
205
|
+
}, I = F;
|
|
206
|
+
function L(e, t, n, r, i, a = []) {
|
|
207
|
+
return F[e](t, n, r, i, a);
|
|
208
|
+
}
|
|
209
|
+
//#endregion
|
|
210
|
+
//#region src/CascadedShadowMaps.ts
|
|
211
|
+
var R = /*#__PURE__*/ new _(), z = /*#__PURE__*/ new _(), B = /*#__PURE__*/ new u(), V = /*#__PURE__*/ new u(), H = /*#__PURE__*/ new S(), U = /*#__PURE__*/ new e(), W = {
|
|
212
|
+
cascadeCount: 4,
|
|
213
|
+
mapSize: 2048,
|
|
214
|
+
far: 5e3,
|
|
215
|
+
mode: "practical",
|
|
216
|
+
lambda: .5,
|
|
217
|
+
margin: 200,
|
|
218
|
+
fade: !0,
|
|
219
|
+
disableLastCascadeCutoff: !1
|
|
220
|
+
}, G = class {
|
|
221
|
+
mainCamera;
|
|
222
|
+
needsUpdateFrusta = !0;
|
|
223
|
+
directionalLights = new y();
|
|
224
|
+
materialStates = new N();
|
|
225
|
+
mainFrustum = new S();
|
|
226
|
+
cascadedFrusta = [];
|
|
227
|
+
splits = [];
|
|
228
|
+
cascades = [];
|
|
229
|
+
_far;
|
|
230
|
+
_mode;
|
|
231
|
+
_lambda;
|
|
232
|
+
_margin;
|
|
233
|
+
_fade;
|
|
234
|
+
_disableLastCascadeCutoff;
|
|
235
|
+
constructor(e, t) {
|
|
236
|
+
this.mainCamera = e;
|
|
237
|
+
let { cascadeCount: n, mapSize: r, far: i, mode: a, lambda: o, margin: s, fade: c, disableLastCascadeCutoff: l } = {
|
|
238
|
+
...W,
|
|
239
|
+
...t
|
|
240
|
+
};
|
|
241
|
+
this._far = i, this._mode = a, this._lambda = o, this._margin = s, this._fade = c, this._disableLastCascadeCutoff = l, this.cascadeCount = n, this.mapSize = r;
|
|
242
|
+
}
|
|
243
|
+
dispose() {
|
|
244
|
+
this.materialStates.dispose(), this.directionalLights.dispose();
|
|
245
|
+
}
|
|
246
|
+
setupMaterial(e) {
|
|
247
|
+
return this.materialStates.setup(e, this);
|
|
248
|
+
}
|
|
249
|
+
rollbackMaterial(e) {
|
|
250
|
+
return this.materialStates.rollback(e);
|
|
251
|
+
}
|
|
252
|
+
setupMaterials(e) {
|
|
253
|
+
for (let t = 0; t < e.length; ++t) this.materialStates.setup(e[t], this);
|
|
254
|
+
return () => {
|
|
255
|
+
for (let t = 0; t < e.length; ++t) this.materialStates.rollback(e[t]);
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
updateCascades() {
|
|
259
|
+
let e = this.cascadeCount, t = this.splits;
|
|
260
|
+
L(this.mode, e, this.mainCamera.near, Math.min(this.mainCamera.far, this.far), this.lambda, t), this.mainFrustum.setFromCamera(this.mainCamera, this.far), this.mainFrustum.split(t, this.cascadedFrusta);
|
|
261
|
+
let n = this.cascades;
|
|
262
|
+
for (let r = 0; r < e; ++r) (n[r] ?? (n[r] = new g())).set(t[r - 1] ?? 0, t[r] ?? 0);
|
|
263
|
+
this.disableLastCascadeCutoff && (n[e - 1].y = Infinity), n.length = e;
|
|
264
|
+
}
|
|
265
|
+
getFrustumRadius(e) {
|
|
266
|
+
let t = e.near, n = e.far, r = Math.max(n[0].distanceTo(n[2]), n[0].distanceTo(t[2]));
|
|
267
|
+
if (this.fade) {
|
|
268
|
+
let e = this.mainCamera, t = e.near, i = Math.min(e.far, this.far), a = n[0].z / (i - t);
|
|
269
|
+
r += .25 * a ** 2 * (i - t);
|
|
270
|
+
}
|
|
271
|
+
return r * .5;
|
|
272
|
+
}
|
|
273
|
+
updateShadowBounds() {
|
|
274
|
+
let e = this.cascadedFrusta, t = this.directionalLights.cascadedLights;
|
|
275
|
+
x(e.length === t.length);
|
|
276
|
+
for (let n = 0; n < e.length; ++n) {
|
|
277
|
+
let e = this.getFrustumRadius(this.cascadedFrusta[n]), r = t[n].shadow.camera;
|
|
278
|
+
r.left = -e, r.right = e, r.top = e, r.bottom = -e, r.near = 0, r.far = e * 2 + this.margin, r.updateProjectionMatrix();
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
updateFrusta() {
|
|
282
|
+
this.updateCascades(), this.updateShadowBounds(), this.materialStates.update(this);
|
|
283
|
+
}
|
|
284
|
+
update() {
|
|
285
|
+
this.needsUpdateFrusta && (this.needsUpdateFrusta = !1, this.updateFrusta());
|
|
286
|
+
let e = this.directionalLights, t = R.copy(e.direction).normalize(), n = B.lookAt(new _(), t, p.DEFAULT_UP), r = V.multiplyMatrices(V.copy(n).invert(), this.mainCamera.matrixWorld), i = this.margin, a = this.mapSize, o = this.cascadedFrusta, s = e.cascadedLights;
|
|
287
|
+
for (let e = 0; e < o.length; ++e) {
|
|
288
|
+
let { near: c, far: l } = H.copy(o[e]).applyMatrix4(r), u = U.makeEmpty();
|
|
289
|
+
for (let e = 0; e < 4; e++) u.expandByPoint(c[e]), u.expandByPoint(l[e]);
|
|
290
|
+
let d = u.getCenter(z);
|
|
291
|
+
d.z = u.max.z + i;
|
|
292
|
+
let f = s[e], { left: p, right: m, top: h, bottom: g } = f.shadow.camera, _ = (m - p) / a, v = (h - g) / a;
|
|
293
|
+
d.x = Math.round(d.x / _) * _, d.y = Math.round(d.y / v) * v, d.applyMatrix4(n), f.position.copy(d), f.target.position.copy(d), f.target.position.add(t);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
get cascadeCount() {
|
|
297
|
+
return this.directionalLights.cascadedLights.length;
|
|
298
|
+
}
|
|
299
|
+
set cascadeCount(e) {
|
|
300
|
+
e !== this.cascadeCount && (this.directionalLights.setCount(e), this.needsUpdateFrusta = !0);
|
|
301
|
+
}
|
|
302
|
+
get mapSize() {
|
|
303
|
+
return this.directionalLights.mainLight.shadow.mapSize.width;
|
|
304
|
+
}
|
|
305
|
+
set mapSize(e) {
|
|
306
|
+
if (e !== this.mapSize) {
|
|
307
|
+
let t = this.directionalLights.cascadedLights;
|
|
308
|
+
for (let n = 0; n < t.length; ++n) {
|
|
309
|
+
let r = t[n].shadow;
|
|
310
|
+
r.mapSize.width = e, r.mapSize.height = e, r.map != null && (r.map.dispose(), r.map = null);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
get far() {
|
|
315
|
+
return this._far;
|
|
316
|
+
}
|
|
317
|
+
set far(e) {
|
|
318
|
+
e !== this._far && (this._far = e, this.needsUpdateFrusta = !0);
|
|
319
|
+
}
|
|
320
|
+
get mode() {
|
|
321
|
+
return this._mode;
|
|
322
|
+
}
|
|
323
|
+
set mode(e) {
|
|
324
|
+
e !== this._mode && (this._mode = e, this.needsUpdateFrusta = !0);
|
|
325
|
+
}
|
|
326
|
+
get lambda() {
|
|
327
|
+
return this._lambda;
|
|
328
|
+
}
|
|
329
|
+
set lambda(e) {
|
|
330
|
+
e !== this._lambda && (this._lambda = e, this.needsUpdateFrusta = !0);
|
|
331
|
+
}
|
|
332
|
+
get margin() {
|
|
333
|
+
return this._margin;
|
|
334
|
+
}
|
|
335
|
+
set margin(e) {
|
|
336
|
+
e !== this._margin && (this._margin = e, this.needsUpdateFrusta = !0);
|
|
337
|
+
}
|
|
338
|
+
get fade() {
|
|
339
|
+
return this._fade;
|
|
340
|
+
}
|
|
341
|
+
set fade(e) {
|
|
342
|
+
e !== this._fade && (this._fade = e, this.needsUpdateFrusta = !0);
|
|
343
|
+
}
|
|
344
|
+
get disableLastCascadeCutoff() {
|
|
345
|
+
return this._disableLastCascadeCutoff;
|
|
346
|
+
}
|
|
347
|
+
set disableLastCascadeCutoff(e) {
|
|
348
|
+
e !== this._disableLastCascadeCutoff && (this._disableLastCascadeCutoff = e, this.needsUpdateFrusta = !0);
|
|
349
|
+
}
|
|
350
|
+
get intensity() {
|
|
351
|
+
return this.directionalLights.mainLight.intensity;
|
|
352
|
+
}
|
|
353
|
+
set intensity(e) {
|
|
354
|
+
if (e !== this.intensity) {
|
|
355
|
+
let t = this.directionalLights.cascadedLights;
|
|
356
|
+
for (let n = 0; n < t.length; ++n) t[n].intensity = e;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
get shadowIntensity() {
|
|
360
|
+
return this.directionalLights.mainLight.shadow.intensity;
|
|
361
|
+
}
|
|
362
|
+
set shadowIntensity(e) {
|
|
363
|
+
if (e !== this.shadowIntensity) {
|
|
364
|
+
let t = this.directionalLights.cascadedLights;
|
|
365
|
+
for (let n = 0; n < t.length; ++n) t[n].shadow.intensity = e;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
get color() {
|
|
369
|
+
return this.directionalLights.mainLight.color;
|
|
370
|
+
}
|
|
371
|
+
set color(e) {
|
|
372
|
+
e !== this.color && this.directionalLights.mainLight.color.copy(e);
|
|
373
|
+
}
|
|
374
|
+
get bias() {
|
|
375
|
+
return this.directionalLights.mainLight.shadow.bias;
|
|
376
|
+
}
|
|
377
|
+
set bias(e) {
|
|
378
|
+
if (e !== this.bias) {
|
|
379
|
+
let t = this.directionalLights.cascadedLights;
|
|
380
|
+
for (let n = 0; n < t.length; ++n) t[n].shadow.bias = e;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
get normalBias() {
|
|
384
|
+
return this.directionalLights.mainLight.shadow.normalBias;
|
|
385
|
+
}
|
|
386
|
+
set normalBias(e) {
|
|
387
|
+
if (e !== this.normalBias) {
|
|
388
|
+
let t = this.directionalLights.cascadedLights;
|
|
389
|
+
for (let n = 0; n < t.length; ++n) t[n].shadow.normalBias = e;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
get radius() {
|
|
393
|
+
return this.directionalLights.mainLight.shadow.radius;
|
|
394
|
+
}
|
|
395
|
+
set radius(e) {
|
|
396
|
+
if (e !== this.radius) {
|
|
397
|
+
let t = this.directionalLights.cascadedLights;
|
|
398
|
+
for (let n = 0; n < t.length; ++n) t[n].shadow.radius = e;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
get blurSamples() {
|
|
402
|
+
return this.directionalLights.mainLight.shadow.blurSamples;
|
|
403
|
+
}
|
|
404
|
+
set blurSamples(e) {
|
|
405
|
+
if (e !== this.blurSamples) {
|
|
406
|
+
let t = this.directionalLights.cascadedLights;
|
|
407
|
+
for (let n = 0; n < t.length; ++n) t[n].shadow.blurSamples = e;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}, K = class extends s {
|
|
411
|
+
csm;
|
|
412
|
+
displayFrustum = !0;
|
|
413
|
+
displayPlanes = !0;
|
|
414
|
+
displayShadowBounds = !0;
|
|
415
|
+
frustumLines;
|
|
416
|
+
cascadeLines = [];
|
|
417
|
+
cascadePlanes = [];
|
|
418
|
+
shadowLines = [];
|
|
419
|
+
constructor(e) {
|
|
420
|
+
super(), this.csm = e;
|
|
421
|
+
let t = new Uint16Array([
|
|
422
|
+
0,
|
|
423
|
+
1,
|
|
424
|
+
1,
|
|
425
|
+
2,
|
|
426
|
+
2,
|
|
427
|
+
3,
|
|
428
|
+
3,
|
|
429
|
+
0,
|
|
430
|
+
4,
|
|
431
|
+
5,
|
|
432
|
+
5,
|
|
433
|
+
6,
|
|
434
|
+
6,
|
|
435
|
+
7,
|
|
436
|
+
7,
|
|
437
|
+
4,
|
|
438
|
+
0,
|
|
439
|
+
4,
|
|
440
|
+
1,
|
|
441
|
+
5,
|
|
442
|
+
2,
|
|
443
|
+
6,
|
|
444
|
+
3,
|
|
445
|
+
7
|
|
446
|
+
]), i = /* @__PURE__ */ new Float32Array(24), a = new r();
|
|
447
|
+
a.setIndex(new n(t, 1)), a.setAttribute("position", new n(i, 3, !1));
|
|
448
|
+
let o = new l(a, new c());
|
|
449
|
+
this.add(o), this.frustumLines = o;
|
|
450
|
+
}
|
|
451
|
+
updateVisibility() {
|
|
452
|
+
let e = this.displayFrustum, t = this.displayPlanes, n = this.displayShadowBounds, r = this.frustumLines, i = this.cascadeLines, a = this.cascadePlanes, o = this.shadowLines;
|
|
453
|
+
for (let r = 0, s = i.length; r < s; r++) {
|
|
454
|
+
let s = i[r], c = a[r], l = o[r];
|
|
455
|
+
s.visible = e, c.visible = e && t, l.visible = n;
|
|
456
|
+
}
|
|
457
|
+
r.visible = e;
|
|
458
|
+
}
|
|
459
|
+
update() {
|
|
460
|
+
let n = this.csm, r = n.mainCamera, a = n.cascadeCount, c = n.mainFrustum, l = n.cascadedFrusta, u = n.directionalLights.cascadedLights, p = this.frustumLines.geometry.getAttribute("position"), h = this.cascadeLines, g = this.cascadePlanes, _ = this.shadowLines;
|
|
461
|
+
for (this.position.copy(r.position), this.quaternion.copy(r.quaternion), this.scale.copy(r.scale), this.updateMatrixWorld(!0); h.length > a;) {
|
|
462
|
+
let e = h.pop(), t = g.pop(), n = _.pop();
|
|
463
|
+
e && this.remove(e), t && this.remove(t), n && this.remove(n);
|
|
464
|
+
}
|
|
465
|
+
for (; h.length < a;) {
|
|
466
|
+
let n = new t(new e(), new i(16777215)), r = new f({
|
|
467
|
+
transparent: !0,
|
|
468
|
+
opacity: .1,
|
|
469
|
+
depthWrite: !1,
|
|
470
|
+
side: o
|
|
471
|
+
}), a = new d(new m(), r), c = new s(), l = new t(new e(), new i(16776960));
|
|
472
|
+
c.add(l), this.add(n), this.add(a), this.add(c), h.push(n), g.push(a), _.push(c);
|
|
473
|
+
}
|
|
474
|
+
for (let e = 0; e < a; e++) {
|
|
475
|
+
let t = l[e], n = u[e].shadow.camera, r = t.far, i = h[e], a = g[e], o = _[e], s = o.children[0];
|
|
476
|
+
i.box.min.copy(r[2]), i.box.max.copy(r[0]), i.box.max.z += 1e-4, a.position.addVectors(r[0], r[2]), a.position.multiplyScalar(.5), a.scale.subVectors(r[0], r[2]), a.scale.z = 1e-4, this.remove(o), o.position.copy(n.position), o.quaternion.copy(n.quaternion), o.scale.copy(n.scale), o.updateMatrixWorld(!0), this.attach(o), s.box.min.set(n.bottom, n.left, -n.far), s.box.max.set(n.top, n.right, -n.near);
|
|
477
|
+
}
|
|
478
|
+
let v = c.near, y = c.far;
|
|
479
|
+
p.setXYZ(0, y[0].x, y[0].y, y[0].z), p.setXYZ(1, y[3].x, y[3].y, y[3].z), p.setXYZ(2, y[2].x, y[2].y, y[2].z), p.setXYZ(3, y[1].x, y[1].y, y[1].z), p.setXYZ(4, v[0].x, v[0].y, v[0].z), p.setXYZ(5, v[3].x, v[3].y, v[3].z), p.setXYZ(6, v[2].x, v[2].y, v[2].z), p.setXYZ(7, v[1].x, v[1].y, v[1].z), p.needsUpdate = !0;
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
//#endregion
|
|
483
|
+
export { K as CSMHelper, y as CascadedDirectionalLights, G as CascadedShadowMaps, W as cascadedShadowMapsOptionsDefaults, I as frustumSplitFunctions, M as overrideFragmentShaderForCSM, L as splitFrustum };
|