webgpu-biome 0.0.0-stage → 0.1.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.
Files changed (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +89 -2
  3. package/lib/canopy.d.ts +75 -0
  4. package/lib/canopy.d.ts.map +1 -0
  5. package/lib/canopy.js +295 -0
  6. package/lib/canopy.js.map +1 -0
  7. package/lib/climate.d.ts +68 -0
  8. package/lib/climate.d.ts.map +1 -0
  9. package/lib/climate.js +105 -0
  10. package/lib/climate.js.map +1 -0
  11. package/lib/detail.d.ts +55 -0
  12. package/lib/detail.d.ts.map +1 -0
  13. package/lib/detail.js +225 -0
  14. package/lib/detail.js.map +1 -0
  15. package/lib/ecotone.d.ts +166 -0
  16. package/lib/ecotone.d.ts.map +1 -0
  17. package/lib/ecotone.js +1007 -0
  18. package/lib/ecotone.js.map +1 -0
  19. package/lib/engine.d.ts +102 -0
  20. package/lib/engine.d.ts.map +1 -0
  21. package/lib/engine.js +773 -0
  22. package/lib/engine.js.map +1 -0
  23. package/lib/epiphytes.d.ts +19 -0
  24. package/lib/epiphytes.d.ts.map +1 -0
  25. package/lib/epiphytes.js +180 -0
  26. package/lib/epiphytes.js.map +1 -0
  27. package/lib/ground.d.ts +43 -0
  28. package/lib/ground.d.ts.map +1 -0
  29. package/lib/ground.js +450 -0
  30. package/lib/ground.js.map +1 -0
  31. package/lib/horizon.d.ts +20 -0
  32. package/lib/horizon.d.ts.map +1 -0
  33. package/lib/horizon.js +625 -0
  34. package/lib/horizon.js.map +1 -0
  35. package/lib/index.d.ts +21 -0
  36. package/lib/index.d.ts.map +1 -0
  37. package/lib/index.js +18 -0
  38. package/lib/index.js.map +1 -0
  39. package/lib/particles.d.ts +12 -0
  40. package/lib/particles.d.ts.map +1 -0
  41. package/lib/particles.js +124 -0
  42. package/lib/particles.js.map +1 -0
  43. package/lib/post.d.ts +23 -0
  44. package/lib/post.d.ts.map +1 -0
  45. package/lib/post.js +225 -0
  46. package/lib/post.js.map +1 -0
  47. package/lib/recipe-specs.d.ts +51 -0
  48. package/lib/recipe-specs.d.ts.map +1 -0
  49. package/lib/recipe-specs.js +562 -0
  50. package/lib/recipe-specs.js.map +1 -0
  51. package/lib/recipes.d.ts +9 -0
  52. package/lib/recipes.d.ts.map +1 -0
  53. package/lib/recipes.js +279 -0
  54. package/lib/recipes.js.map +1 -0
  55. package/lib/sandbox.d.ts +183 -0
  56. package/lib/sandbox.d.ts.map +1 -0
  57. package/lib/sandbox.js +1060 -0
  58. package/lib/sandbox.js.map +1 -0
  59. package/lib/shadows.d.ts +71 -0
  60. package/lib/shadows.d.ts.map +1 -0
  61. package/lib/shadows.js +532 -0
  62. package/lib/shadows.js.map +1 -0
  63. package/lib/sky.d.ts +16 -0
  64. package/lib/sky.d.ts.map +1 -0
  65. package/lib/sky.js +132 -0
  66. package/lib/sky.js.map +1 -0
  67. package/lib/substrate.d.ts +124 -0
  68. package/lib/substrate.d.ts.map +1 -0
  69. package/lib/substrate.js +988 -0
  70. package/lib/substrate.js.map +1 -0
  71. package/lib/turf.d.ts +60 -0
  72. package/lib/turf.d.ts.map +1 -0
  73. package/lib/turf.js +278 -0
  74. package/lib/turf.js.map +1 -0
  75. package/lib/water.d.ts +41 -0
  76. package/lib/water.d.ts.map +1 -0
  77. package/lib/water.js +210 -0
  78. package/lib/water.js.map +1 -0
  79. package/package.json +63 -3
package/lib/shadows.js ADDED
@@ -0,0 +1,532 @@
1
+ // Multi-scale world shadows & obstacle map packed into a single filterable RGBA8 DataTexture:
2
+ // R: tree crown (LightVolume) + analytic trunk cylinder shadow stripes
3
+ // G: heightfield ray-marched mountain/dune/karst shadows + solid rock (cliff & erratic) cast shadows
4
+ // B: encoded upwind blocker height hBlocker for 3D ridge/cliff shadows on tree trunks & crowns
5
+ // A: solid rock & root-collar proximity mask for waterline intersection foam
6
+ import * as THREE from "three/webgpu";
7
+ import { abs, clamp, cos, floor, float, fract, int, ivec2, length, max, mix, select, sin, smoothstep, step, texture, textureLoad, uniform, vec2, vec3, vec4 } from "three/tsl";
8
+ import { TREE_STRIDE } from "webgpu-trees";
9
+ import { ROCK_STRIDE } from "webgpu-rocks";
10
+ import { evalHorizonTerrainHeight } from "./substrate.js";
11
+ const CASCADE_RES = 1536;
12
+ const CASCADE_HALF_M = 48;
13
+ function clamp01(v) {
14
+ return v < 0 ? 0 : v > 1 ? 1 : v;
15
+ }
16
+ function fastBilinearHeight(grid, x, z) {
17
+ const { nx, nz, x0, z0, cell, heights } = grid;
18
+ const fx = (x - x0) / cell;
19
+ const fz = (z - z0) / cell;
20
+ const i0 = Math.max(0, Math.min(nx - 2, fx | 0));
21
+ const j0 = Math.max(0, Math.min(nz - 2, fz | 0));
22
+ const tx = fx - i0;
23
+ const tz = fz - j0;
24
+ const idx = j0 * nx + i0;
25
+ const a = heights[idx];
26
+ const b = heights[idx + 1];
27
+ const c = heights[idx + nx];
28
+ const d = heights[idx + nx + 1];
29
+ return (a + (b - a) * tx) * (1 - tz) + (c + (d - c) * tx) * tz;
30
+ }
31
+ /**
32
+ * Pure-CPU heightfield shadow ray-march toward `sun` across both hero `grid` and outer 5.5 km horizon terrain.
33
+ */
34
+ export function evalHeightfieldRayShadow(opts) {
35
+ const { x, y, z, sun, grid, shape, half, waterLevel } = opts;
36
+ const sLen = Math.hypot(sun[0], sun[1], sun[2]) || 1;
37
+ const sx = sun[0] / sLen;
38
+ const sy = sun[1] / sLen;
39
+ const sz = sun[2] / sLen;
40
+ if (sy <= 0.01) {
41
+ return { terrainSun: 0.04, blockerHeight: y + 40 };
42
+ }
43
+ const maxDist = opts.maxDist ?? 260;
44
+ const steps = Math.max(6, opts.steps ?? 24);
45
+ const { x0, z0, width, depth } = grid;
46
+ const x1 = x0 + width;
47
+ const z1 = z0 + depth;
48
+ const startY = y + 0.15;
49
+ const invHorizon = 1 / Math.max(0.02, Math.hypot(sx, sz));
50
+ let maxSlopeOver = -1;
51
+ let blockerHeight = y - 10;
52
+ for (let s = 1; s <= steps; s++) {
53
+ const u = s / steps;
54
+ const t = u * u * 0.85 * maxDist + u * 0.15 * maxDist;
55
+ const rx = x + sx * t;
56
+ const rz = z + sz * t;
57
+ const rayY = startY + sy * t;
58
+ const hSample = rx >= x0 && rx <= x1 && rz >= z0 && rz <= z1
59
+ ? fastBilinearHeight(grid, rx, rz)
60
+ : evalHorizonTerrainHeight(shape, rx, rz, half, waterLevel);
61
+ const diff = hSample - rayY;
62
+ const horiz = t * Math.hypot(sx, sz);
63
+ const localBlocker = hSample - sy * horiz * invHorizon;
64
+ if (localBlocker > blockerHeight)
65
+ blockerHeight = localBlocker;
66
+ const slopeOver = diff / Math.max(2.0, t);
67
+ if (slopeOver > maxSlopeOver)
68
+ maxSlopeOver = slopeOver;
69
+ }
70
+ if (maxSlopeOver <= 0) {
71
+ return { terrainSun: 1, blockerHeight };
72
+ }
73
+ const terrainSun = clamp01(1 - maxSlopeOver * 11.5);
74
+ return { terrainSun: 0.06 + 0.94 * terrainSun, blockerHeight };
75
+ }
76
+ /**
77
+ * Compute unified heightfield + cliff/erratic + trunk + crown shadow map paired with TSL samplers.
78
+ */
79
+ export function createBiomeShadows(opts) {
80
+ const { grid, substrate, shape, waterLevel, treeSpecies, rockSpecies } = opts;
81
+ const bounds = substrate.bounds;
82
+ const res = Math.max(128, Math.min(384, opts.resolution ?? Math.round(bounds.width)));
83
+ const half = bounds.width * 0.5;
84
+ let minH = Infinity;
85
+ let maxH = -Infinity;
86
+ for (let k = 0; k < grid.heights.length; k++) {
87
+ const h = grid.heights[k];
88
+ if (h < minH)
89
+ minH = h;
90
+ if (h > maxH)
91
+ maxH = h;
92
+ }
93
+ const hMin = minH - 8;
94
+ const hSpan = Math.max(24, maxH - minH + 64);
95
+ const data = new Uint8Array(res * res * 4);
96
+ data.fill(255);
97
+ for (let k = 0; k < res * res; k++) {
98
+ data[k * 4 + 2] = 0; // blocker height
99
+ data[k * 4 + 3] = 0; // obstacle proximity
100
+ }
101
+ const shadowTex = new THREE.DataTexture(data, res, res, THREE.RGBAFormat, THREE.UnsignedByteType);
102
+ shadowTex.colorSpace = THREE.NoColorSpace;
103
+ shadowTex.magFilter = shadowTex.minFilter = THREE.LinearFilter;
104
+ shadowTex.wrapS = shadowTex.wrapT = THREE.ClampToEdgeWrapping;
105
+ shadowTex.generateMipmaps = false;
106
+ shadowTex.needsUpdate = true;
107
+ const shadowNode = texture(shadowTex);
108
+ const cascadeRenderDepthTex = new THREE.DepthTexture(CASCADE_RES, CASCADE_RES);
109
+ cascadeRenderDepthTex.type = THREE.FloatType;
110
+ cascadeRenderDepthTex.generateMipmaps = false;
111
+ const cascadeRt = new THREE.RenderTarget(CASCADE_RES, CASCADE_RES, {
112
+ samples: 0,
113
+ depthBuffer: true,
114
+ depthTexture: cascadeRenderDepthTex,
115
+ });
116
+ const cascadeSampleDepthTex = new THREE.DepthTexture(CASCADE_RES, CASCADE_RES);
117
+ cascadeSampleDepthTex.type = THREE.FloatType;
118
+ cascadeSampleDepthTex.generateMipmaps = false;
119
+ const cascadeSampleRt = new THREE.RenderTarget(CASCADE_RES, CASCADE_RES, {
120
+ samples: 0,
121
+ depthBuffer: true,
122
+ depthTexture: cascadeSampleDepthTex,
123
+ });
124
+ const cascadeDepthNode = texture(cascadeSampleDepthTex);
125
+ const cascadeCamera = new THREE.OrthographicCamera(-CASCADE_HALF_M, CASCADE_HALF_M, CASCADE_HALF_M, -CASCADE_HALF_M, 1, 190);
126
+ const cascadeCullCamera = new THREE.PerspectiveCamera(72, 1, 1, 165);
127
+ const uCascadeVP = uniform(new THREE.Matrix4());
128
+ /** `(enabled, reversedDepth, invRes, halfExtent)` */
129
+ const uCascadeInfo = uniform(new THREE.Vector4(0, 1, 1 / CASCADE_RES, CASCADE_HALF_M));
130
+ const uSunY = uniform(0.5);
131
+ const uArea = uniform(new THREE.Vector4(bounds.x0, bounds.z0, 1 / Math.max(1e-4, bounds.width), 1 / Math.max(1e-4, bounds.depth)));
132
+ const uHeightRange = uniform(new THREE.Vector2(hMin, hSpan));
133
+ const uWind = uniform(new THREE.Vector3(1, 0, 2));
134
+ const uTime = uniform(0);
135
+ const _camPosTmp = new THREE.Vector3();
136
+ const _fwdTmp = new THREE.Vector3();
137
+ const _focusTmp = new THREE.Vector3();
138
+ const _lightPosTmp = new THREE.Vector3();
139
+ const _rightTmp = new THREE.Vector3();
140
+ const _upTmp = new THREE.Vector3();
141
+ const _sunTmp = new THREE.Vector3();
142
+ let curTrees = opts.trees;
143
+ let curRocks = opts.rocks;
144
+ const obstacleA = new Uint8Array(res * res);
145
+ let obstaclesDirty = true;
146
+ let trunkSegs = [];
147
+ let solidRocks = [];
148
+ const terrainY = new Float32Array(res * res);
149
+ const cellW = bounds.width / res;
150
+ const cellD = bounds.depth / res;
151
+ for (let j = 0; j < res; j++) {
152
+ const gz = bounds.z0 + (j + 0.5) * cellD;
153
+ for (let i = 0; i < res; i++) {
154
+ const gx = bounds.x0 + (i + 0.5) * cellW;
155
+ terrainY[j * res + i] = fastBilinearHeight(grid, gx, gz);
156
+ }
157
+ }
158
+ const rebuildObstacles = () => {
159
+ obstaclesDirty = false;
160
+ obstacleA.fill(0);
161
+ trunkSegs = [];
162
+ solidRocks = [];
163
+ const treeCount = Math.floor(curTrees.length / TREE_STRIDE);
164
+ for (let t = 0; t < treeCount; t++) {
165
+ const o = t * TREE_STRIDE;
166
+ const tx = curTrees[o];
167
+ const ty = curTrees[o + 1];
168
+ const tz = curTrees[o + 2];
169
+ const sp = treeSpecies[Math.round(curTrees[o + 3])];
170
+ if (!sp)
171
+ continue;
172
+ const h = Number.isFinite(curTrees[o + 4]) && curTrees[o + 4] > 0 ? curTrees[o + 4] : sp.height;
173
+ const cb = Number.isFinite(curTrees[o + 6]) && curTrees[o + 6] >= 0 ? curTrees[o + 6] : sp.crownBase;
174
+ const tr = Number.isFinite(curTrees[o + 7]) && curTrees[o + 7] > 0 ? curTrees[o + 7] : sp.trunkRadius;
175
+ trunkSegs.push({ x: tx, y: ty, z: tz, top: ty + Math.max(0.8, Math.min(h * 0.85, cb + 1.2)), rad: Math.max(0.08, tr * 1.3) });
176
+ // Stamp subtle root-collar foam footprint only if trunk pierces the waterline (or when waterLevel is unset for tests).
177
+ if (waterLevel === undefined || (ty <= waterLevel + 0.18 && ty >= waterLevel - 0.8)) {
178
+ const colR = Math.max(0.6, tr * 2.4);
179
+ const i0 = Math.max(0, Math.floor((tx - colR - bounds.x0) / cellW));
180
+ const i1 = Math.min(res - 1, Math.ceil((tx + colR - bounds.x0) / cellW));
181
+ const j0 = Math.max(0, Math.floor((tz - colR - bounds.z0) / cellD));
182
+ const j1 = Math.min(res - 1, Math.ceil((tz + colR - bounds.z0) / cellD));
183
+ for (let j = j0; j <= j1; j++) {
184
+ const gz = bounds.z0 + (j + 0.5) * cellD;
185
+ for (let i = i0; i <= i1; i++) {
186
+ const gx = bounds.x0 + (i + 0.5) * cellW;
187
+ const d = Math.hypot(gx - tx, gz - tz);
188
+ if (d < colR) {
189
+ const v = Math.round(clamp01(1 - d / colR) * 200);
190
+ const k = j * res + i;
191
+ if (v > obstacleA[k])
192
+ obstacleA[k] = v;
193
+ }
194
+ }
195
+ }
196
+ }
197
+ }
198
+ const rockCount = Math.floor(curRocks.length / ROCK_STRIDE);
199
+ for (let r = 0; r < rockCount; r++) {
200
+ const o = r * ROCK_STRIDE;
201
+ const rx = curRocks[o];
202
+ const ry = curRocks[o + 1];
203
+ const rz = curRocks[o + 2];
204
+ const sp = rockSpecies[Math.round(curRocks[o + 3])];
205
+ if (!sp)
206
+ continue;
207
+ if (sp.archetype === "cliff" || sp.archetype === "erratic") {
208
+ const rh = Number.isFinite(curRocks[o + 4]) ? curRocks[o + 4] : sp.height;
209
+ const rr = Number.isFinite(curRocks[o + 5]) ? curRocks[o + 5] : sp.radius;
210
+ const baseY = ry - rh * sp.embed;
211
+ const topY = baseY + rh;
212
+ solidRocks.push({ x: rx, y: baseY, z: rz, h: rh, r: rr, topY });
213
+ // Only emergent cliffs & boulders breaking the waterline stamp obstacleA foam halos (never submerged cobbles/talus).
214
+ if (waterLevel === undefined || (baseY < waterLevel + 0.12 && topY > waterLevel + 0.06)) {
215
+ const foamR = rr * 1.08 + 0.18;
216
+ const i0 = Math.max(0, Math.floor((rx - foamR - bounds.x0) / cellW));
217
+ const i1 = Math.min(res - 1, Math.ceil((rx + foamR - bounds.x0) / cellW));
218
+ const j0 = Math.max(0, Math.floor((rz - foamR - bounds.z0) / cellD));
219
+ const j1 = Math.min(res - 1, Math.ceil((rz + foamR - bounds.z0) / cellD));
220
+ for (let j = j0; j <= j1; j++) {
221
+ const gz = bounds.z0 + (j + 0.5) * cellD;
222
+ for (let i = i0; i <= i1; i++) {
223
+ const gx = bounds.x0 + (i + 0.5) * cellW;
224
+ const d = Math.hypot(gx - rx, gz - rz);
225
+ if (d < foamR) {
226
+ const v = Math.round(clamp01((1 - d / foamR) * 1.4) * 255);
227
+ const k = j * res + i;
228
+ if (v > obstacleA[k])
229
+ obstacleA[k] = v;
230
+ }
231
+ }
232
+ }
233
+ }
234
+ }
235
+ }
236
+ };
237
+ const lastSun = new THREE.Vector3(0, -1, 0);
238
+ let lastRetention = -1;
239
+ const bakeAll = (sunVec, treesObj, foliageRetention) => {
240
+ if (obstaclesDirty)
241
+ rebuildObstacles();
242
+ const sun = sunVec.clone().normalize();
243
+ const sArr = [sun.x, sun.y, sun.z];
244
+ // 1. Bake LightVolume canopy transmittance using `treesObj.bakeShade`.
245
+ treesObj.bakeShade({
246
+ x0: bounds.x0,
247
+ z0: bounds.z0,
248
+ width: bounds.width,
249
+ depth: bounds.depth,
250
+ cell: bounds.width / res,
251
+ });
252
+ const treeShadeImg = treesObj.shadeTexture.image;
253
+ const tW = Math.max(1, treeShadeImg.width);
254
+ const tH = Math.max(1, treeShadeImg.height);
255
+ const tData = treeShadeImg.data;
256
+ // Seed R with canopy transmittance, G with heightfield ray-marched terrain shadows, B with blocker height, A with obstacles.
257
+ const rChannel = new Float32Array(res * res);
258
+ const gChannel = new Float32Array(res * res);
259
+ const blockers = new Float32Array(res * res);
260
+ for (let j = 0; j < res; j++) {
261
+ const gz = bounds.z0 + (j + 0.5) * cellD;
262
+ const tj = Math.min(tH - 1, Math.floor((j / res) * tH));
263
+ for (let i = 0; i < res; i++) {
264
+ const gx = bounds.x0 + (i + 0.5) * cellW;
265
+ const ti = Math.min(tW - 1, Math.floor((i / res) * tW));
266
+ const k = j * res + i;
267
+ rChannel[k] = (tData[(tj * tW + ti) * 4] ?? 255) / 255;
268
+ const hr = evalHeightfieldRayShadow({
269
+ x: gx,
270
+ y: terrainY[k],
271
+ z: gz,
272
+ sun: sArr,
273
+ grid,
274
+ shape,
275
+ half,
276
+ waterLevel,
277
+ maxDist: 220,
278
+ steps: 20,
279
+ });
280
+ gChannel[k] = hr.terrainSun;
281
+ blockers[k] = hr.blockerHeight;
282
+ }
283
+ }
284
+ // 2. Analytic trunk shadow stripes + solid rock (cliff & erratic) cast shadows & 3D blocker height.
285
+ if (sun.y > 0.03) {
286
+ const slopeH = Math.hypot(sun.x, sun.z) / Math.max(0.05, sun.y);
287
+ const sxH = sun.x / Math.max(0.05, sun.y);
288
+ const szH = sun.z / Math.max(0.05, sun.y);
289
+ for (const tr of trunkSegs) {
290
+ const segH = tr.top - tr.y;
291
+ const dx = -sxH * segH;
292
+ const dz = -szH * segH;
293
+ const minX = Math.min(tr.x, tr.x + dx) - tr.rad * 2.5;
294
+ const maxX = Math.max(tr.x, tr.x + dx) + tr.rad * 2.5;
295
+ const minZ = Math.min(tr.z, tr.z + dz) - tr.rad * 2.5;
296
+ const maxZ = Math.max(tr.z, tr.z + dz) + tr.rad * 2.5;
297
+ const i0 = Math.max(0, Math.floor((minX - bounds.x0) / cellW));
298
+ const i1 = Math.min(res - 1, Math.ceil((maxX - bounds.x0) / cellW));
299
+ const j0 = Math.max(0, Math.floor((minZ - bounds.z0) / cellD));
300
+ const j1 = Math.min(res - 1, Math.ceil((maxZ - bounds.z0) / cellD));
301
+ const segLen2 = dx * dx + dz * dz;
302
+ if (segLen2 < 1e-4)
303
+ continue;
304
+ for (let j = j0; j <= j1; j++) {
305
+ const gz = bounds.z0 + (j + 0.5) * cellD;
306
+ for (let i = i0; i <= i1; i++) {
307
+ const gx = bounds.x0 + (i + 0.5) * cellW;
308
+ const px = gx - tr.x;
309
+ const pz = gz - tr.z;
310
+ const tProj = Math.min(1, Math.max(0, (px * dx + pz * dz) / segLen2));
311
+ const cx = tr.x + dx * tProj;
312
+ const cz = tr.z + dz * tProj;
313
+ const dist = Math.hypot(gx - cx, gz - cz);
314
+ const penumbra = tr.rad * (0.85 + tProj * 0.95);
315
+ if (dist < penumbra * 1.45) {
316
+ const shade = 0.22 + 0.78 * clamp01((dist - penumbra * 0.45) / (penumbra * 1.0));
317
+ const k = j * res + i;
318
+ rChannel[k] = Math.min(rChannel[k], rChannel[k] * shade + 0.08 * (1 - shade));
319
+ }
320
+ }
321
+ }
322
+ }
323
+ for (const rk of solidRocks) {
324
+ const dropH = rk.h * 1.15;
325
+ const dx = -sxH * dropH;
326
+ const dz = -szH * dropH;
327
+ const rPad = rk.r * 1.35;
328
+ const minX = Math.min(rk.x, rk.x + dx) - rPad;
329
+ const maxX = Math.max(rk.x, rk.x + dx) + rPad;
330
+ const minZ = Math.min(rk.z, rk.z + dz) - rPad;
331
+ const maxZ = Math.max(rk.z, rk.z + dz) + rPad;
332
+ const i0 = Math.max(0, Math.floor((minX - bounds.x0) / cellW));
333
+ const i1 = Math.min(res - 1, Math.ceil((maxX - bounds.x0) / cellW));
334
+ const j0 = Math.max(0, Math.floor((minZ - bounds.z0) / cellD));
335
+ const j1 = Math.min(res - 1, Math.ceil((maxZ - bounds.z0) / cellD));
336
+ const lenXZ = Math.hypot(dx, dz);
337
+ const ux = lenXZ > 1e-4 ? dx / lenXZ : 0;
338
+ const uz = lenXZ > 1e-4 ? dz / lenXZ : 0;
339
+ for (let j = j0; j <= j1; j++) {
340
+ const gz = bounds.z0 + (j + 0.5) * cellD;
341
+ for (let i = i0; i <= i1; i++) {
342
+ const gx = bounds.x0 + (i + 0.5) * cellW;
343
+ const px = gx - rk.x;
344
+ const pz = gz - rk.z;
345
+ const along = px * ux + pz * uz;
346
+ if (along < -rk.r * 0.65 || along > lenXZ + rk.r * 0.8)
347
+ continue;
348
+ const perp = Math.abs(-px * uz + pz * ux);
349
+ const tRatio = clamp01(along / Math.max(0.5, lenXZ));
350
+ const widthAt = rk.r * (0.95 - tRatio * 0.32);
351
+ if (perp < widthAt) {
352
+ const edge = clamp01((widthAt - perp) / Math.max(0.35, widthAt * 0.45));
353
+ const endFade = clamp01(1 - Math.max(0, along - lenXZ * 0.72) / Math.max(0.4, rk.r * 0.85));
354
+ const shadowFactor = 1 - 0.84 * edge * endFade;
355
+ const k = j * res + i;
356
+ gChannel[k] = Math.min(gChannel[k], gChannel[k] * shadowFactor + 0.08 * (1 - shadowFactor));
357
+ const rockBlockY = rk.topY - Math.max(0, along) / Math.max(0.05, slopeH);
358
+ if (rockBlockY > blockers[k])
359
+ blockers[k] = rockBlockY;
360
+ }
361
+ }
362
+ }
363
+ }
364
+ }
365
+ for (let k = 0; k < res * res; k++) {
366
+ data[k * 4] = Math.round(clamp01(rChannel[k]) * 255);
367
+ data[k * 4 + 1] = Math.round(clamp01(gChannel[k]) * 255);
368
+ data[k * 4 + 2] = Math.round(clamp01((blockers[k] - hMin) / hSpan) * 255);
369
+ data[k * 4 + 3] = obstacleA[k];
370
+ }
371
+ shadowTex.needsUpdate = true;
372
+ lastSun.copy(sun);
373
+ lastRetention = foliageRetention;
374
+ };
375
+ const sampleCascadeTap = (posW, extraBias) => {
376
+ const clip = uCascadeVP.mul(vec4(posW, 1));
377
+ const uvC = vec2(clip.x.mul(0.5).add(0.5), float(0.5).sub(clip.y.mul(0.5)));
378
+ const zRec = clip.z;
379
+ const boxDist = max(abs(clip.x), abs(clip.y)).mul(uCascadeInfo.w);
380
+ const inRange = float(1)
381
+ .sub(smoothstep(float(38.0), float(46.5), boxDist))
382
+ .mul(uCascadeInfo.x)
383
+ .mul(step(float(0.002), zRec))
384
+ .mul(step(zRec, float(0.998)));
385
+ const bias = extraBias.add(float(0.0028).mul(float(1).sub(clamp(uSunY, float(0.1), float(1)))));
386
+ // Deterministic, zero-noise C1 quadratic B-spline 3x3 PCF filter using textureLoad on the 2048x2048 DepthTexture.
387
+ const st = uvC.mul(float(CASCADE_RES));
388
+ const ic = floor(st);
389
+ const f = fract(st);
390
+ const wx0 = float(1).sub(f.x).mul(float(1).sub(f.x)).mul(0.5);
391
+ const wx1 = float(0.5).add(f.x.mul(float(1).sub(f.x)));
392
+ const wx2 = f.x.mul(f.x).mul(0.5);
393
+ const wy0 = float(1).sub(f.y).mul(float(1).sub(f.y)).mul(0.5);
394
+ const wy1 = float(0.5).add(f.y.mul(float(1).sub(f.y)));
395
+ const wy2 = f.y.mul(f.y).mul(0.5);
396
+ const rev = uCascadeInfo.y.greaterThan(0.5);
397
+ const tapAt = (ox, oy) => {
398
+ const clampedPx = clamp(ic.add(vec2(ox, oy)), vec2(0), vec2(CASCADE_RES - 1));
399
+ const zTex = textureLoad(cascadeDepthNode, ivec2(clampedPx), int(0)).r;
400
+ return select(rev, step(zTex, zRec.add(bias)), step(zRec.sub(bias), zTex));
401
+ };
402
+ const row0 = tapAt(-1, -1).mul(wx0).add(tapAt(0, -1).mul(wx1)).add(tapAt(1, -1).mul(wx2));
403
+ const row1 = tapAt(-1, 0).mul(wx0).add(tapAt(0, 0).mul(wx1)).add(tapAt(1, 0).mul(wx2));
404
+ const row2 = tapAt(-1, 1).mul(wx0).add(tapAt(0, 1).mul(wx1)).add(tapAt(1, 1).mul(wx2));
405
+ const pcf = row0.mul(wy0).add(row1.mul(wy1)).add(row2.mul(wy2));
406
+ return vec2(pcf, inRange);
407
+ };
408
+ const sampleShadowTex = (xz) => {
409
+ const uv = clamp(vec2(xz.x.sub(uArea.x).mul(uArea.z), xz.y.sub(uArea.y).mul(uArea.w)), vec2(0.001), vec2(0.999));
410
+ return shadowNode.sample(uv).level(float(0));
411
+ };
412
+ const groundSunAt = (xz, yWorld) => {
413
+ const uv = clamp(vec2(xz.x.sub(uArea.x).mul(uArea.z), xz.y.sub(uArea.y).mul(uArea.w)), vec2(0.001), vec2(0.999));
414
+ const inside = step(float(0), uv.x).mul(step(float(0), uv.y)).mul(step(uv.x, float(1))).mul(step(uv.y, float(1)));
415
+ const s = shadowNode.sample(uv).level(float(0));
416
+ const sBase = s.r;
417
+ const penumbra = clamp(sBase.mul(float(1).sub(sBase)).mul(4), float(0), float(1));
418
+ const wLen = max(length(uWind.xy), float(1e-4));
419
+ const wDir = uWind.xy.div(wLen);
420
+ // Gentle stationary-anchored canopy breeze tremor (no scrolling 2 m wave ripples across sunlit sand).
421
+ const swayAmp = clamp(uWind.z.div(10), float(0.02), float(0.055));
422
+ const gustPhase = xz.x.mul(0.31).add(xz.y.mul(0.23)).sub(uTime.mul(max(uWind.z, float(0.8))).mul(0.35));
423
+ const swayWorld = wDir
424
+ .mul(sin(gustPhase).mul(swayAmp))
425
+ .add(vec2(wDir.y.negate(), wDir.x).mul(cos(gustPhase.mul(1.6)).mul(0.5).mul(swayAmp)));
426
+ const uvSway = clamp(uv.add(vec2(swayWorld.x.mul(uArea.z), swayWorld.y.mul(uArea.w))), vec2(0.001), vec2(0.999));
427
+ const sSway = shadowNode.sample(uvSway).level(float(0)).r;
428
+ const canopySun = clamp(mix(sBase, sSway, penumbra.mul(0.65)), float(0), float(1));
429
+ const farSun = canopySun.mul(s.g);
430
+ const worldY = yWorld ?? substrate.sampleHeightTex(xz);
431
+ const casc = sampleCascadeTap(vec3(xz.x, worldY, xz.y), float(0.0018));
432
+ const nearSun = casc.x.mul(s.g);
433
+ return mix(float(1), mix(farSun, nearSun, casc.y), inside);
434
+ };
435
+ const treeSunAt3d = (at) => {
436
+ const xz = vec2(at.x, at.z);
437
+ const uv = clamp(vec2(xz.x.sub(uArea.x).mul(uArea.z), xz.y.sub(uArea.y).mul(uArea.w)), vec2(0.001), vec2(0.999));
438
+ const inside = step(float(0), uv.x).mul(step(float(0), uv.y)).mul(step(uv.x, float(1))).mul(step(uv.y, float(1)));
439
+ const s = sampleShadowTex(xz);
440
+ const hBlocker = uHeightRange.x.add(s.b.mul(uHeightRange.y));
441
+ const ridgeVis = smoothstep(hBlocker.sub(2.2), hBlocker.add(1.8), at.y);
442
+ return mix(float(1), ridgeVis, inside);
443
+ };
444
+ const woodSunAt3d = (at, normal) => {
445
+ const casc = sampleCascadeTap(at.add(normal.mul(0.14)), float(0.0022));
446
+ return mix(float(1), casc.x, casc.y);
447
+ };
448
+ const obstacleAt = (xz) => sampleShadowTex(xz).a;
449
+ const worldPerTexel = (CASCADE_HALF_M * 2) / CASCADE_RES;
450
+ return {
451
+ shadowTex,
452
+ cascadeRt,
453
+ cascadeSampleRt,
454
+ cascadeRenderDepthTex,
455
+ cascadeSampleDepthTex,
456
+ cascadeCamera,
457
+ cascadeCullCamera,
458
+ resolution: res,
459
+ groundSunAt,
460
+ treeSunAt3d,
461
+ woodSunAt3d,
462
+ obstacleAt,
463
+ updateCascadeCamera(camera, sun, reversedDepth) {
464
+ _sunTmp.copy(sun).normalize();
465
+ camera.getWorldDirection(_fwdTmp);
466
+ // World position: the camera may sit inside a host's moving rig.
467
+ camera.getWorldPosition(_camPosTmp);
468
+ const fwdFlatLen = Math.hypot(_fwdTmp.x, _fwdTmp.z) || 1;
469
+ const fx = _camPosTmp.x + (_fwdTmp.x / fwdFlatLen) * 18;
470
+ const fz = _camPosTmp.z + (_fwdTmp.z / fwdFlatLen) * 18;
471
+ const clampedX = Math.max(bounds.x0 + 10, Math.min(bounds.x0 + bounds.width - 10, fx));
472
+ const clampedZ = Math.max(bounds.z0 + 10, Math.min(bounds.z0 + bounds.depth - 10, fz));
473
+ const groundH = fastBilinearHeight(grid, clampedX, clampedZ);
474
+ _focusTmp.set(clampedX, groundH + 4, clampedZ);
475
+ // Build light basis and snap focus to exact texel increments to prevent shadow swimming.
476
+ _lightPosTmp.copy(_focusTmp).addScaledVector(_sunTmp, 92);
477
+ cascadeCamera.position.copy(_lightPosTmp);
478
+ cascadeCamera.up.set(0, 1, 0);
479
+ if (Math.abs(_sunTmp.y) > 0.96)
480
+ cascadeCamera.up.set(0, 0, -1);
481
+ cascadeCamera.lookAt(_focusTmp);
482
+ cascadeCamera.updateMatrixWorld(true);
483
+ cascadeCamera.matrixWorld.extractBasis(_rightTmp, _upTmp, _fwdTmp);
484
+ const projX = _focusTmp.dot(_rightTmp);
485
+ const projY = _focusTmp.dot(_upTmp);
486
+ const snapX = Math.round(projX / worldPerTexel) * worldPerTexel - projX;
487
+ const snapY = Math.round(projY / worldPerTexel) * worldPerTexel - projY;
488
+ _focusTmp.addScaledVector(_rightTmp, snapX).addScaledVector(_upTmp, snapY);
489
+ _lightPosTmp.copy(_focusTmp).addScaledVector(_sunTmp, 92);
490
+ cascadeCamera.position.copy(_lightPosTmp);
491
+ cascadeCamera.lookAt(_focusTmp);
492
+ cascadeCamera.coordinateSystem = THREE.WebGPUCoordinateSystem;
493
+ cascadeCamera._reversedDepth = reversedDepth;
494
+ cascadeCamera.updateProjectionMatrix();
495
+ cascadeCamera.updateMatrixWorld(true);
496
+ cascadeCullCamera.position.copy(_focusTmp).addScaledVector(_sunTmp, 96);
497
+ cascadeCullCamera.up.copy(cascadeCamera.up);
498
+ cascadeCullCamera.lookAt(_focusTmp);
499
+ cascadeCullCamera.updateMatrixWorld(true);
500
+ cascadeCullCamera.updateProjectionMatrix();
501
+ uCascadeVP.value.multiplyMatrices(cascadeCamera.projectionMatrix, cascadeCamera.matrixWorldInverse);
502
+ uCascadeInfo.value.set(1, reversedDepth ? 1 : 0, 1 / CASCADE_RES, CASCADE_HALF_M);
503
+ uSunY.value = _sunTmp.y;
504
+ },
505
+ disableCascade() {
506
+ uCascadeInfo.value.x = 0;
507
+ },
508
+ setObjects(treesArr, rocksArr) {
509
+ curTrees = treesArr;
510
+ curRocks = rocksArr;
511
+ obstaclesDirty = true;
512
+ lastSun.set(0, -1, 0);
513
+ },
514
+ update(sun, treesObj, foliageRetention, wind, timeS) {
515
+ uWind.value.set(wind[0], wind[1], wind[2]);
516
+ uTime.value = timeS;
517
+ if (lastSun.y < -0.5 ||
518
+ lastSun.distanceTo(sun) > 0.012 ||
519
+ Math.abs(foliageRetention - lastRetention) > 0.08) {
520
+ bakeAll(sun, treesObj, foliageRetention);
521
+ }
522
+ },
523
+ dispose() {
524
+ cascadeRt.dispose();
525
+ cascadeSampleRt.dispose();
526
+ cascadeRenderDepthTex.dispose();
527
+ cascadeSampleDepthTex.dispose();
528
+ shadowTex.dispose();
529
+ },
530
+ };
531
+ }
532
+ //# sourceMappingURL=shadows.js.map