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
@@ -0,0 +1,20 @@
1
+ import * as THREE from "three/webgpu";
2
+ import { type Atmosphere } from "webgpu-trees";
3
+ import { type SubstrateGrid, type TerrainShape } from "./substrate.js";
4
+ import { type WaterConfig } from "./water.js";
5
+ export interface BiomeHorizon {
6
+ readonly object: THREE.Group;
7
+ readonly treeCount: number;
8
+ update(sun: THREE.Vector3, sunTint: THREE.Vector3, shadeTint: THREE.Vector3, snow: number, autumnTurn: number): void;
9
+ dispose(): void;
10
+ }
11
+ /** Build the 5.5 km distant landform & ocean horizon extension around a hero biome grid. */
12
+ export declare function createBiomeHorizon(opts: {
13
+ grid: SubstrateGrid;
14
+ shape: TerrainShape;
15
+ atmosphere: Atmosphere;
16
+ noiseTex: THREE.DataTexture;
17
+ waterConfig?: WaterConfig;
18
+ outerRadius?: number;
19
+ }): BiomeHorizon;
20
+ //# sourceMappingURL=horizon.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"horizon.d.ts","sourceRoot":"","sources":["../src/horizon.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AA0BtC,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,YAAY,EAGlB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,KAAK,CAAC;IAC7B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,MAAM,CACJ,GAAG,EAAE,KAAK,CAAC,OAAO,EAClB,OAAO,EAAE,KAAK,CAAC,OAAO,EACtB,SAAS,EAAE,KAAK,CAAC,OAAO,EACxB,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,GACjB,IAAI,CAAC;IACR,OAAO,IAAI,IAAI,CAAC;CACjB;AA4QD,4FAA4F;AAC5F,wBAAgB,kBAAkB,CAAC,IAAI,EAAE;IACvC,IAAI,EAAE,aAAa,CAAC;IACpB,KAAK,EAAE,YAAY,CAAC;IACpB,UAAU,EAAE,UAAU,CAAC;IACvB,QAAQ,EAAE,KAAK,CAAC,WAAW,CAAC;IAC5B,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,GAAG,YAAY,CA8Xf"}
package/lib/horizon.js ADDED
@@ -0,0 +1,625 @@
1
+ // Seamless 5.5 km distant horizon terrain skirt + outer sea continuation ring.
2
+ // Inner perimeter ring stitches identically to `sampleSubstrateCpu` at `±half`, then expands
3
+ // out to `5500 m` via `evalHorizonTerrainHeight` with prebaked ridge AO, snowline, and aerial perspective.
4
+ import * as THREE from "three/webgpu";
5
+ import { Discard, Fn, attribute, cameraPosition, clamp, cos, dot, float, length, max, mix, normalWorld, normalize, positionWorld, pow, sin, smoothstep, texture, uniform, varying, vec2, vec3, vec4, } from "three/tsl";
6
+ import { evalHorizonTerrainHeight, sampleSubstrateCpu, } from "./substrate.js";
7
+ const SEA_SHAPES = new Set(["coastal-dunes", "palm-lagoon", "karst-gorge"]);
8
+ function hashH(seed) {
9
+ let x = (Math.imul(seed | 0, 1597334677) ^ 0x9e3779b9) >>> 0;
10
+ x = Math.imul(x ^ (x >>> 15), 2246822519) >>> 0;
11
+ x = Math.imul(x ^ (x >>> 13), 3266489917) >>> 0;
12
+ return ((x ^ (x >>> 16)) >>> 0) / 4294967296;
13
+ }
14
+ /**
15
+ * Procedural 512x128 RGBA8 horizon forest-clump atlas (4 habits x 128px with 10-texel transparent
16
+ * horizontal guard borders to prevent mipmap column bleed):
17
+ * 0 = multi-stem conifer grove, 1 = wide pine/umbrella clump with understory skirt,
18
+ * 2 = billowing multi-crown broadleaf copse, 3 = coastal palm grove cluster.
19
+ */
20
+ function makeHorizonTreeAtlas() {
21
+ const W = 512;
22
+ const H = 128;
23
+ const cell = 128;
24
+ const data = new Uint8Array(W * H * 4);
25
+ const blobCov = (u, v, cx, cy, rx, ry, seed) => {
26
+ const dx = (u - cx) / rx;
27
+ const dy = (v - cy) / ry;
28
+ const scallop = 0.11 * Math.sin(Math.atan2(dy, dx) * 5.0 + seed) +
29
+ 0.06 * Math.cos(u * 21 + v * 19 + seed * 1.7);
30
+ const d = Math.hypot(dx, dy) - scallop;
31
+ return d < 1.0 ? Math.min(1, (1 - d) * 8) : 0;
32
+ };
33
+ for (let py = 0; py < H; py++) {
34
+ const v = 1 - (py + 0.5) / H; // 0 at base, 1 at top
35
+ for (let px = 0; px < W; px++) {
36
+ const col = Math.floor(px / cell);
37
+ const u = ((px % cell) + 0.5) / cell * 2 - 1; // -1..1
38
+ let cov = 0;
39
+ let shade = 0.62;
40
+ let isTrunk = 0;
41
+ // Hard horizontal transparent padding so linear/mip filtering never bleeds neighbour columns.
42
+ if (Math.abs(u) <= 0.84) {
43
+ if (col === 0) {
44
+ // Layered multi-spire conifer grove (broad overlapping skirts down to ground).
45
+ const spires = [
46
+ [0.0, 0.08, 0.96, 0.56],
47
+ [-0.4, 0.04, 0.8, 0.44],
48
+ [0.42, 0.04, 0.76, 0.42],
49
+ ];
50
+ for (const [cx, y0, y1, hw] of spires) {
51
+ if (v >= y0 && v <= y1) {
52
+ const t = (v - y0) / (y1 - y0);
53
+ const tierWave = 0.78 + 0.22 * Math.sin(t * 22.0 + cx * 5);
54
+ const coneW = (1 - t * 0.88) * hw * tierWave;
55
+ const n = 0.09 * Math.sin(u * 24 + v * 21) * Math.cos(v * 33 - u * 15);
56
+ const edge = coneW + n - Math.abs(u - cx);
57
+ if (edge > 0) {
58
+ const c = Math.min(1, edge * 14);
59
+ if (c > cov) {
60
+ cov = c;
61
+ shade = 0.36 + 0.64 * clamp01(0.25 + t * 0.68 + (u - cx) * 0.14);
62
+ }
63
+ }
64
+ }
65
+ }
66
+ }
67
+ else if (col === 1) {
68
+ // Wide umbrella / shelterbelt pine stand with low shrubby skirt (never tall naked poles).
69
+ const domes = [
70
+ [-0.36, 0.48, 0.42, 0.24, 1.1],
71
+ [0.34, 0.52, 0.42, 0.25, 2.3],
72
+ [0.0, 0.66, 0.48, 0.28, 0.4],
73
+ [0.0, 0.24, 0.72, 0.24, 3.2],
74
+ ];
75
+ for (const [cx, cy, rx, ry, seed] of domes) {
76
+ const c = blobCov(u, v, cx, cy, rx, ry, seed);
77
+ if (c > cov) {
78
+ cov = c;
79
+ shade = 0.38 + 0.62 * clamp01(0.32 + (v - (cy - ry)) / (2 * ry) * 0.85 - (u - cx) * 0.12);
80
+ }
81
+ }
82
+ if (v < 0.55 && v > 0.06 && (Math.abs(u - 0.06) < 0.075 || Math.abs(u + 0.24) < 0.07)) {
83
+ cov = Math.max(cov, 0.9);
84
+ if (cov < 0.92) {
85
+ shade = 0.34;
86
+ isTrunk = 1;
87
+ }
88
+ }
89
+ }
90
+ else if (col === 2) {
91
+ // Billowing multi-crown broadleaf copse grounded to v = 0.05.
92
+ const crowns = [
93
+ [-0.38, 0.36, 0.4, 0.32, 0.7],
94
+ [0.38, 0.38, 0.4, 0.32, 2.1],
95
+ [0.0, 0.54, 0.52, 0.4, 3.5],
96
+ ];
97
+ for (const [cx, cy, rx, ry, seed] of crowns) {
98
+ const c = blobCov(u, v, cx, cy, rx, ry, seed);
99
+ if (c > cov) {
100
+ cov = c;
101
+ shade = 0.38 + 0.62 * clamp01(0.28 + (v - (cy - ry)) / (2 * ry) * 0.88 + (u - cx) * 0.12);
102
+ }
103
+ }
104
+ }
105
+ else {
106
+ // Multi-palm coastal cluster (tall crown + shorter companion palm + lush frond base).
107
+ const palms = [
108
+ [-0.12, 0.76, 0.48, 0.05],
109
+ [0.32, 0.56, 0.38, -0.04],
110
+ ];
111
+ for (const [px0, pyTop, rFrond, bend] of palms) {
112
+ const stemX = px0 + Math.sin(v * 2.5) * bend;
113
+ if (v < pyTop && Math.abs(u - stemX) < 0.045) {
114
+ cov = Math.max(cov, 0.95);
115
+ shade = 0.36;
116
+ isTrunk = 1;
117
+ }
118
+ const rx = u - (px0 + Math.sin(pyTop * 2.5) * bend);
119
+ const ry = v - (pyTop - 0.02);
120
+ const ang = Math.atan2(ry, rx);
121
+ const rad = Math.hypot(rx, ry * 1.15);
122
+ const rays = 0.55 + 0.38 * Math.pow(Math.max(0, Math.cos(ang * 4.5 + 0.4)), 1.5);
123
+ if (rad < rays * rFrond && v > pyTop - 0.28) {
124
+ const tipFade = Math.max(0, 1 - rad / (rays * rFrond));
125
+ const c = Math.min(1, tipFade * 5.5);
126
+ if (c > cov) {
127
+ cov = c;
128
+ shade = 0.52 + 0.5 * clamp01(0.4 + (v - (pyTop - 0.2)) * 2.2);
129
+ isTrunk = 0;
130
+ }
131
+ }
132
+ }
133
+ const baseScrub = blobCov(u, v, 0.0, 0.16, 0.65, 0.16, 4.1);
134
+ if (baseScrub > cov) {
135
+ cov = baseScrub;
136
+ shade = 0.38 + 0.35 * v;
137
+ isTrunk = 0;
138
+ }
139
+ }
140
+ }
141
+ const off = (py * W + px) * 4;
142
+ const tone = Math.round(clamp01(shade) * 255);
143
+ data[off] = tone;
144
+ data[off + 1] = isTrunk ? Math.round(tone * 0.75) : tone;
145
+ data[off + 2] = isTrunk ? Math.round(tone * 0.58) : tone;
146
+ data[off + 3] = Math.round(clamp01(cov) * 255);
147
+ }
148
+ }
149
+ const tex = new THREE.DataTexture(data, W, H, THREE.RGBAFormat, THREE.UnsignedByteType);
150
+ tex.colorSpace = THREE.NoColorSpace;
151
+ tex.magFilter = THREE.LinearFilter;
152
+ tex.minFilter = THREE.LinearMipmapLinearFilter;
153
+ tex.generateMipmaps = true;
154
+ tex.wrapS = THREE.ClampToEdgeWrapping;
155
+ tex.wrapT = THREE.ClampToEdgeWrapping;
156
+ tex.needsUpdate = true;
157
+ return tex;
158
+ }
159
+ function clamp01(v) {
160
+ return v < 0 ? 0 : v > 1 ? 1 : v;
161
+ }
162
+ function buildSquareSkirtGeometry(rings, segsPerSide, half, outerRadius, evalY) {
163
+ const ringVerts = segsPerSide * 4;
164
+ const totalVerts = (rings + 1) * ringVerts;
165
+ const positions = new Float32Array(totalVerts * 3);
166
+ const normals = new Float32Array(totalVerts * 3);
167
+ const uvArr = new Float32Array(totalVerts * 2);
168
+ const metaArr = new Float32Array(totalVerts * 4);
169
+ const sideUnit = (idx) => {
170
+ const side = Math.floor(idx / segsPerSide);
171
+ const t = (idx % segsPerSide) / segsPerSide;
172
+ const s = -1 + 2 * t;
173
+ if (side === 0)
174
+ return [s, -1]; // North edge (-z): -1 -> +1
175
+ if (side === 1)
176
+ return [1, s]; // East edge (+x): -1 -> +1
177
+ if (side === 2)
178
+ return [-s, 1]; // South edge (+z): +1 -> -1
179
+ return [-1, -s]; // West edge (-x): +1 -> -1
180
+ };
181
+ for (let r = 0; r <= rings; r++) {
182
+ const f = r / rings;
183
+ // Exponential radial stretch keeps inner transition dense (~6 m) while reaching 5.5 km.
184
+ const distScale = half + (outerRadius - half) * Math.pow(f, 2.55);
185
+ const circularMix = clamp01((f - 0.15) / 0.85);
186
+ for (let k = 0; k < ringVerts; k++) {
187
+ const [sx, sz] = sideUnit(k);
188
+ const len = Math.hypot(sx, sz) || 1;
189
+ const cx = sx / len;
190
+ const cz = sz / len;
191
+ const rx = sx + (cx - sx) * circularMix;
192
+ const rz = sz + (cz - sz) * circularMix;
193
+ const x = rx * distScale;
194
+ const z = rz * distScale;
195
+ const y = evalY(x, z, r);
196
+ const vIdx = r * ringVerts + k;
197
+ positions[vIdx * 3] = x;
198
+ positions[vIdx * 3 + 1] = y;
199
+ positions[vIdx * 3 + 2] = z;
200
+ uvArr[vIdx * 2] = x * 0.002;
201
+ uvArr[vIdx * 2 + 1] = z * 0.002;
202
+ metaArr[vIdx * 4] = y;
203
+ metaArr[vIdx * 4 + 1] = clamp01(f);
204
+ metaArr[vIdx * 4 + 2] = 0;
205
+ metaArr[vIdx * 4 + 3] = 0;
206
+ }
207
+ }
208
+ // Compute finite-difference surface normals & gentle valley AO.
209
+ const eps = 12;
210
+ for (let r = 0; r <= rings; r++) {
211
+ for (let k = 0; k < ringVerts; k++) {
212
+ const vIdx = r * ringVerts + k;
213
+ const x = positions[vIdx * 3];
214
+ const y = positions[vIdx * 3 + 1];
215
+ const z = positions[vIdx * 3 + 2];
216
+ if (r === 0) {
217
+ normals[vIdx * 3] = 0;
218
+ normals[vIdx * 3 + 1] = 1;
219
+ normals[vIdx * 3 + 2] = 0;
220
+ metaArr[vIdx * 4 + 2] = 1;
221
+ }
222
+ else {
223
+ const dx = (evalY(x + eps, z, r) - evalY(x - eps, z, r)) / (2 * eps);
224
+ const dz = (evalY(x, z + eps, r) - evalY(x, z - eps, r)) / (2 * eps);
225
+ const nLen = Math.hypot(dx, 1, dz) || 1;
226
+ normals[vIdx * 3] = -dx / nLen;
227
+ normals[vIdx * 3 + 1] = 1 / nLen;
228
+ normals[vIdx * 3 + 2] = -dz / nLen;
229
+ const avgSurr = 0.25 * (evalY(x + 65, z, r) + evalY(x - 65, z, r) + evalY(x, z + 65, r) + evalY(x, z - 65, r));
230
+ metaArr[vIdx * 4 + 2] = clamp01(0.88 - Math.max(0, avgSurr - y) * 0.015);
231
+ }
232
+ }
233
+ }
234
+ const indices = [];
235
+ for (let r = 0; r < rings; r++) {
236
+ const rowA = r * ringVerts;
237
+ const rowB = (r + 1) * ringVerts;
238
+ for (let k = 0; k < ringVerts; k++) {
239
+ const kNext = (k + 1) % ringVerts;
240
+ const a = rowA + k;
241
+ const b = rowA + kNext;
242
+ const c = rowB + k;
243
+ const d = rowB + kNext;
244
+ indices.push(a, b, c, b, d, c);
245
+ }
246
+ }
247
+ const geo = new THREE.BufferGeometry();
248
+ geo.setAttribute("position", new THREE.BufferAttribute(positions, 3));
249
+ geo.setAttribute("normal", new THREE.BufferAttribute(normals, 3));
250
+ geo.setAttribute("uv", new THREE.BufferAttribute(uvArr, 2));
251
+ geo.setAttribute("aHorizonMeta", new THREE.BufferAttribute(metaArr, 4));
252
+ geo.setIndex(indices);
253
+ geo.computeBoundingSphere();
254
+ return geo;
255
+ }
256
+ /** Build the 5.5 km distant landform & ocean horizon extension around a hero biome grid. */
257
+ export function createBiomeHorizon(opts) {
258
+ const { grid, shape, atmosphere, noiseTex, waterConfig, outerRadius = 5500 } = opts;
259
+ const half = grid.width * 0.5;
260
+ const group = new THREE.Group();
261
+ group.name = "biome-horizon";
262
+ const uSun = uniform(new THREE.Vector3(0.4, 0.7, 0.4).normalize());
263
+ const uSunTint = uniform(new THREE.Vector3(1.14, 1.04, 0.86));
264
+ const uShadeTint = uniform(new THREE.Vector3(0.6, 0.7, 0.88));
265
+ /** `(snow, autumnTurn, waterLevel, hasSea)` */
266
+ const uEnv = uniform(new THREE.Vector4(0, 0, waterConfig?.level ?? -999, waterConfig ? 1 : 0));
267
+ const landRings = 56;
268
+ const landSegs = 96;
269
+ const landGeo = buildSquareSkirtGeometry(landRings, landSegs, half, outerRadius, (x, z, r) => {
270
+ if (r === 0)
271
+ return sampleSubstrateCpu(grid, x, z).height;
272
+ return evalHorizonTerrainHeight(shape, x, z, half, waterConfig?.level);
273
+ });
274
+ // Sample the exact interpolated triangle height from `landGeo` rings so horizon trees never float on chords.
275
+ const landPos = landGeo.getAttribute("position");
276
+ const landRingVerts = landSegs * 4;
277
+ const sampleSkirtY = (tx, tz) => {
278
+ const ax = Math.abs(tx);
279
+ const az = Math.abs(tz);
280
+ let side = 0;
281
+ let s = 0;
282
+ if (az >= ax) {
283
+ if (tz <= 0) {
284
+ side = 0;
285
+ s = tx / Math.max(1e-4, -tz);
286
+ }
287
+ else {
288
+ side = 2;
289
+ s = -tx / Math.max(1e-4, tz);
290
+ }
291
+ }
292
+ else if (tx >= 0) {
293
+ side = 1;
294
+ s = tz / Math.max(1e-4, tx);
295
+ }
296
+ else {
297
+ side = 3;
298
+ s = -tz / Math.max(1e-4, -tx);
299
+ }
300
+ const uSeg = clamp01((s + 1) * 0.5) * landSegs;
301
+ const s0 = Math.min(landSegs - 1, Math.floor(uSeg));
302
+ const st = uSeg - s0;
303
+ const k0 = side * landSegs + s0;
304
+ const k1 = (k0 + 1) % landRingVerts;
305
+ const rHoriz = Math.hypot(tx, tz);
306
+ const edgeLen = Math.hypot(1, Math.min(1, Math.abs(s)));
307
+ const ringRadiusAt = (rIdx) => {
308
+ const f = rIdx / landRings;
309
+ const distScale = half + (outerRadius - half) * Math.pow(f, 2.55);
310
+ const circularMix = clamp01((f - 0.15) / 0.85);
311
+ return distScale * (edgeLen + (1 - edgeLen) * circularMix);
312
+ };
313
+ let lo = 0;
314
+ let hi = landRings - 1;
315
+ while (lo < hi) {
316
+ const mid = (lo + hi + 1) >> 1;
317
+ if (ringRadiusAt(mid) <= rHoriz)
318
+ lo = mid;
319
+ else
320
+ hi = mid - 1;
321
+ }
322
+ const r0 = lo;
323
+ const r1 = Math.min(landRings, r0 + 1);
324
+ const d0 = ringRadiusAt(r0);
325
+ const d1 = ringRadiusAt(r1);
326
+ const rt = clamp01((rHoriz - d0) / Math.max(1e-4, d1 - d0));
327
+ const y00 = landPos.getY(r0 * landRingVerts + k0);
328
+ const y01 = landPos.getY(r0 * landRingVerts + k1);
329
+ const y10 = landPos.getY(r1 * landRingVerts + k0);
330
+ const y11 = landPos.getY(r1 * landRingVerts + k1);
331
+ return (y00 * (1 - st) + y01 * st) * (1 - rt) + (y10 * (1 - st) + y11 * st) * rt;
332
+ };
333
+ // Match biome palette across 1..5.5 km ridges.
334
+ const isMontane = shape === "montane-ridge";
335
+ const isKarst = shape === "karst-gorge";
336
+ const isDunes = shape === "coastal-dunes";
337
+ const isPalm = shape === "palm-lagoon";
338
+ const isTaiga = shape === "glacial-tarn";
339
+ const grassBase = isDunes
340
+ ? new THREE.Vector3(0.11, 0.135, 0.065)
341
+ : isKarst
342
+ ? new THREE.Vector3(0.13, 0.145, 0.075)
343
+ : isPalm
344
+ ? new THREE.Vector3(0.1, 0.175, 0.065)
345
+ : isTaiga
346
+ ? new THREE.Vector3(0.065, 0.105, 0.048)
347
+ : new THREE.Vector3(0.085, 0.145, 0.052);
348
+ const canopyBase = isMontane
349
+ ? new THREE.Vector3(0.048, 0.088, 0.042)
350
+ : isTaiga
351
+ ? new THREE.Vector3(0.036, 0.072, 0.038)
352
+ : isKarst
353
+ ? new THREE.Vector3(0.065, 0.105, 0.05)
354
+ : new THREE.Vector3(0.055, 0.11, 0.042);
355
+ const rockBase = isKarst
356
+ ? new THREE.Vector3(0.32, 0.3, 0.26)
357
+ : isMontane
358
+ ? new THREE.Vector3(0.17, 0.175, 0.185)
359
+ : isDunes
360
+ ? new THREE.Vector3(0.38, 0.33, 0.22)
361
+ : new THREE.Vector3(0.16, 0.155, 0.145);
362
+ const uGrassCol = uniform(grassBase);
363
+ const uForestCol = uniform(canopyBase);
364
+ const uRockCol = uniform(rockBase);
365
+ const landMat = new THREE.MeshBasicNodeMaterial({ fog: false });
366
+ landMat.colorNode = Fn(() => {
367
+ const posW = positionWorld;
368
+ const n = normalize(normalWorld);
369
+ const meta = attribute("aHorizonMeta", "vec4");
370
+ const nMacro = texture(noiseTex, posW.xz.mul(0.0009)).r;
371
+ const nDetail = texture(noiseTex, posW.xz.mul(0.0045)).g;
372
+ // Distant woodland canopy belts below alpine treeline + rock crags on steep mountain slopes.
373
+ const autumnMix = isMontane || isTaiga ? uEnv.y.mul(0.32) : uEnv.y.mul(0.14);
374
+ const forestSeasonal = mix(uForestCol, vec3(0.24, 0.14, 0.045), autumnMix);
375
+ const coverPattern = smoothstep(float(0.32), float(0.68), nMacro.mul(0.7).add(nDetail.mul(0.3)));
376
+ let alb = mix(uGrassCol, forestSeasonal, coverPattern.mul(isDunes ? 0.55 : 0.85));
377
+ const steepRock = smoothstep(float(0.86), float(0.68), n.y);
378
+ const altRock = isMontane ? smoothstep(float(28), float(95), posW.y) : float(0);
379
+ alb = mix(alb, uRockCol.mul(float(0.82).add(nDetail.mul(0.36))), clamp(max(steepRock, altRock.mul(0.85)), float(0), float(1)));
380
+ // Alpine peak snowline (even in Autumn on high 90m+ peaks) + winter landscape snow.
381
+ const alpineLine = isMontane ? smoothstep(float(72), float(135), posW.y).mul(smoothstep(float(0.45), float(0.75), n.y)) : float(0);
382
+ const winterMask = clamp(uEnv.x.mul(smoothstep(float(0.55), float(0.78), n.y)).mul(float(0.72).add(nMacro.mul(0.4))), float(0), float(0.95));
383
+ const snowMask = clamp(max(winterMask, alpineLine.mul(0.85)), float(0), float(0.95));
384
+ alb = mix(alb, vec3(0.86, 0.89, 0.95), snowMask);
385
+ // Coastal wet shoreline darkening & submerged seabed tint (eliminates bright land slivers along distant bays).
386
+ const shoreWet = smoothstep(float(0.8), float(-0.4), posW.y.sub(uEnv.z)).mul(uEnv.w);
387
+ alb = mix(alb, uRockCol.mul(0.55), shoreWet.mul(0.55));
388
+ const subWater = smoothstep(float(0.05), float(-0.6), posW.y.sub(uEnv.z)).mul(uEnv.w);
389
+ alb = mix(alb, vec3(...(waterConfig?.deepColor ?? [0.02, 0.06, 0.09])), subWater.mul(0.88));
390
+ const ndl = max(dot(n, uSun), float(0));
391
+ const skyVis = clamp(meta.z, float(0.35), float(1.0));
392
+ const lit = alb.mul(mix(uShadeTint.mul(skyVis), uSunTint, ndl.mul(0.88)));
393
+ return vec4(atmosphere.air(lit, posW), 1);
394
+ })();
395
+ const landMesh = new THREE.Mesh(landGeo, landMat);
396
+ landMesh.name = "biome-horizon-land";
397
+ landMesh.frustumCulled = false;
398
+ group.add(landMesh);
399
+ const maxHorizonTrees = 4200;
400
+ const hPosArr = new Float32Array(maxHorizonTrees * 4); // (x, y, z, colIndex 0..3)
401
+ const hMetaArr = new Float32Array(maxHorizonTrees * 4); // (width, height, autumnWeight, tintSeed)
402
+ let hTreeCount = 0;
403
+ const waterLvl = waterConfig?.level ?? -999;
404
+ const epsS = 14;
405
+ const minHRadius = Math.max(half + 52, 212);
406
+ for (let i = 0; i < 16000 && hTreeCount < maxHorizonTrees; i++) {
407
+ const rU = hashH(i * 7 + 11);
408
+ const radius = minHRadius + Math.pow(rU, 1.35) * 2400;
409
+ const ang = hashH(i * 13 + 29) * Math.PI * 2;
410
+ const tx = Math.cos(ang) * radius;
411
+ const tz = Math.sin(ang) * radius;
412
+ // Keep trees cleanly outside hero terrain square (+/-half) and inside 2.65 km forest belt.
413
+ if (Math.max(Math.abs(tx), Math.abs(tz)) < half + 36)
414
+ continue;
415
+ const ty = evalHorizonTerrainHeight(shape, tx, tz, half, waterConfig?.level);
416
+ if (waterConfig && ty < waterLvl + 0.65)
417
+ continue;
418
+ // Coastal dune seaward beaches stay open sand & scrub; pine clumps grow only on sheltered inland dunes.
419
+ if (isDunes && (tz < 14 || ty < 4.2))
420
+ continue;
421
+ // Alpine treeline on montane ridges fades organically into exposed rock crags.
422
+ const treeline = 46 + 9 * Math.sin(tx * 0.008 + tz * 0.006);
423
+ if (isMontane && ty > treeline)
424
+ continue;
425
+ if (isMontane && ty > treeline - 9 && hashH(i * 19 + 3) > (treeline - ty) / 9)
426
+ continue;
427
+ const dx = (evalHorizonTerrainHeight(shape, tx + epsS, tz, half, waterConfig?.level) - evalHorizonTerrainHeight(shape, tx - epsS, tz, half, waterConfig?.level)) / (2 * epsS);
428
+ const dz = (evalHorizonTerrainHeight(shape, tx, tz + epsS, half, waterConfig?.level) - evalHorizonTerrainHeight(shape, tx, tz - epsS, half, waterConfig?.level)) / (2 * epsS);
429
+ const ny = 1 / Math.hypot(dx, 1, dz);
430
+ if (ny < 0.87)
431
+ continue; // >~29.5 deg rocky crag slope
432
+ // Clumped woodland belt density so meadows & heaths have natural distant glades rather than uniform fuzz.
433
+ const standWave = 0.5 +
434
+ 0.32 * Math.sin(tx * 0.0045 + tz * 0.0037) +
435
+ 0.18 * Math.cos(tx * 0.011 - tz * 0.009);
436
+ const minDense = isDunes ? 0.56 : isKarst ? 0.52 : isPalm ? 0.5 : 0.34;
437
+ if (standWave < minDense)
438
+ continue;
439
+ const spU = hashH(i * 23 + 47);
440
+ let colIdx = 2;
441
+ let autumnW = 0.25;
442
+ let width = 10 + hashH(i * 31 + 5) * 7;
443
+ let height = 12 + hashH(i * 37 + 7) * 11;
444
+ if (isTaiga) {
445
+ colIdx = spU < 0.6 ? 0 : spU < 0.82 ? 1 : 2;
446
+ autumnW = colIdx === 2 ? 0.92 : spU > 0.72 && spU < 0.82 ? 0.75 : 0.02;
447
+ height = 11 + hashH(i * 37 + 7) * 12;
448
+ width = colIdx === 0 ? height * 0.52 : height * 0.68;
449
+ }
450
+ else if (isMontane) {
451
+ colIdx = ty > 18 ? (spU < 0.75 ? 0 : 1) : spU < 0.52 ? 0 : spU < 0.78 ? 2 : 1;
452
+ autumnW = colIdx === 2 ? 0.95 : 0.05;
453
+ height = 11 + hashH(i * 37 + 7) * 13;
454
+ width = colIdx === 0 ? height * 0.52 : height * 0.66;
455
+ }
456
+ else if (isKarst) {
457
+ colIdx = spU < 0.62 ? 1 : spU < 0.84 ? 2 : 0;
458
+ autumnW = 0.08;
459
+ height = 10 + hashH(i * 37 + 7) * 9;
460
+ width = colIdx === 1 ? height * 0.95 : colIdx === 0 ? height * 0.32 : height * 0.82;
461
+ }
462
+ else if (isDunes) {
463
+ colIdx = spU < 0.72 ? 1 : 2;
464
+ autumnW = colIdx === 2 ? 0.55 : 0.04;
465
+ height = 5.5 + hashH(i * 37 + 7) * 5.0;
466
+ width = height * 1.45;
467
+ }
468
+ else if (isPalm) {
469
+ colIdx = spU < 0.62 ? 3 : spU < 0.86 ? 1 : 2;
470
+ autumnW = 0.0;
471
+ height = colIdx === 3 ? 12 + hashH(i * 37 + 7) * 10 : 10 + hashH(i * 37 + 7) * 9;
472
+ width = colIdx === 3 ? height * 0.86 : height * 1.12;
473
+ }
474
+ else {
475
+ // woodlandMeadow: broad oak/beech woods + scattered Scots pine
476
+ colIdx = spU < 0.74 ? 2 : spU < 0.9 ? 1 : 0;
477
+ autumnW = colIdx === 2 ? 0.72 : 0.05;
478
+ height = 10 + hashH(i * 37 + 7) * 10;
479
+ width = height * 1.05;
480
+ }
481
+ const nearTaper = 0.56 + 0.44 * Math.min(1, Math.max(0, (radius - minHRadius) / 190));
482
+ const idx4 = hTreeCount * 4;
483
+ const meshY = sampleSkirtY(tx, tz);
484
+ hPosArr[idx4] = tx;
485
+ hPosArr[idx4 + 1] = Math.min(ty, meshY) - 1.15;
486
+ hPosArr[idx4 + 2] = tz;
487
+ hPosArr[idx4 + 3] = colIdx;
488
+ hMetaArr[idx4] = width * nearTaper;
489
+ hMetaArr[idx4 + 1] = height * nearTaper;
490
+ hMetaArr[idx4 + 2] = autumnW;
491
+ hMetaArr[idx4 + 3] = hashH(i * 43 + 19);
492
+ hTreeCount++;
493
+ }
494
+ // Two crossed upright billboard quads per distant horizon tree (`0` storage buffers, `1` texture slot).
495
+ const crossGeo = new THREE.InstancedBufferGeometry();
496
+ const basePos = new Float32Array([
497
+ -0.5, 0, 0, 0.5, 0, 0, 0.5, 1, 0, -0.5, 1, 0,
498
+ 0, 0, -0.5, 0, 0, 0.5, 0, 1, 0.5, 0, 1, -0.5,
499
+ ]);
500
+ const baseUv = new Float32Array([
501
+ 0, 0, 1, 0, 1, 1, 0, 1,
502
+ 0, 0, 1, 0, 1, 1, 0, 1,
503
+ ]);
504
+ const baseNorm = new Float32Array([
505
+ 0, 0.3, 0.95, 0, 0.3, 0.95, 0, 0.7, 0.7, 0, 0.7, 0.7,
506
+ 0.95, 0.3, 0, 0.95, 0.3, 0, 0.7, 0.7, 0, 0.7, 0.7, 0,
507
+ ]);
508
+ crossGeo.setAttribute("position", new THREE.BufferAttribute(basePos, 3));
509
+ crossGeo.setAttribute("uv", new THREE.BufferAttribute(baseUv, 2));
510
+ crossGeo.setAttribute("normal", new THREE.BufferAttribute(baseNorm, 3));
511
+ crossGeo.setIndex([0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7]);
512
+ const hPosAttr = new THREE.InstancedBufferAttribute(hPosArr.subarray(0, Math.max(4, hTreeCount * 4)), 4);
513
+ const hMetaAttr = new THREE.InstancedBufferAttribute(hMetaArr.subarray(0, Math.max(4, hTreeCount * 4)), 4);
514
+ crossGeo.setAttribute("aHTreePos", hPosAttr);
515
+ crossGeo.setAttribute("aHTreeMeta", hMetaAttr);
516
+ crossGeo.instanceCount = hTreeCount;
517
+ const horizonTreeAtlas = makeHorizonTreeAtlas();
518
+ const hTreeMat = new THREE.MeshBasicNodeMaterial({
519
+ side: THREE.DoubleSide,
520
+ alphaToCoverage: true,
521
+ fog: false,
522
+ });
523
+ const aHTreePos = attribute("aHTreePos", "vec4");
524
+ const aHTreeMeta = attribute("aHTreeMeta", "vec4");
525
+ const pLocal = attribute("position", "vec3");
526
+ const uvLocal = attribute("uv", "vec2");
527
+ const hYaw = aHTreeMeta.w.mul(6.28318);
528
+ const hCy = cos(hYaw);
529
+ const hSy = sin(hYaw);
530
+ const sX = pLocal.x.mul(aHTreeMeta.x);
531
+ const sY = pLocal.y.mul(aHTreeMeta.y);
532
+ const sZ = pLocal.z.mul(aHTreeMeta.x);
533
+ const rX = sX.mul(hCy).sub(sZ.mul(hSy));
534
+ const rZ = sX.mul(hSy).add(sZ.mul(hCy));
535
+ const worldTreePos = vec3(aHTreePos.x.add(rX), aHTreePos.y.add(sY), aHTreePos.z.add(rZ));
536
+ hTreeMat.positionNode = worldTreePos;
537
+ const vTreeUv = varying(vec2(aHTreePos.w.mul(0.25).add(0.015).add(uvLocal.x.mul(0.22)), uvLocal.y), "vHTreeUv");
538
+ const vTreeMeta = varying(vec4(uvLocal.y, aHTreeMeta.z, aHTreeMeta.w, 1), "vHTreeMeta");
539
+ const vTreeWPos = varying(worldTreePos, "vHTreeWPos");
540
+ hTreeMat.colorNode = Fn(() => {
541
+ const tex = texture(horizonTreeAtlas, vTreeUv);
542
+ Discard(tex.a.lessThan(0.42));
543
+ const hFrac = vTreeMeta.x;
544
+ const autumnW = vTreeMeta.y;
545
+ const seedJit = vTreeMeta.z.sub(0.5).mul(0.24);
546
+ const baseEver = uForestCol.mul(float(1).add(seedJit));
547
+ const goldFol = vec3(0.26, 0.18, 0.065).mul(float(0.85).add(vTreeMeta.z.mul(0.3)));
548
+ const leafCol = mix(baseEver, goldFol, clamp(uEnv.y.mul(autumnW), float(0), float(1)));
549
+ const crownTint = leafCol.mul(tex.rgb).mul(mix(float(0.65), float(1.18), hFrac));
550
+ const crownSnow = clamp(uEnv.x.mul(smoothstep(float(0.32), float(0.78), hFrac)).mul(0.82), float(0), float(0.85));
551
+ const alb = mix(crownTint, vec3(0.82, 0.86, 0.93), crownSnow);
552
+ const sunUp = smoothstep(float(0.02), float(0.45), uSun.y);
553
+ const topLit = mix(float(0.48), float(1.0), hFrac).mul(sunUp);
554
+ const lit = alb.mul(mix(uShadeTint.mul(0.85), uSunTint, topLit));
555
+ const dCam = length(cameraPosition.sub(vTreeWPos));
556
+ const edgeAlpha = clamp(tex.a.mul(float(1).sub(smoothstep(float(2250), float(2750), dCam))), float(0), float(1));
557
+ return vec4(atmosphere.air(lit, vTreeWPos), edgeAlpha);
558
+ })();
559
+ const hTreeMesh = new THREE.Mesh(crossGeo, hTreeMat);
560
+ hTreeMesh.name = "biome-horizon-trees";
561
+ hTreeMesh.frustumCulled = false;
562
+ if (hTreeCount > 0)
563
+ group.add(hTreeMesh);
564
+ let seaGeo = null;
565
+ let seaMat = null;
566
+ if (waterConfig && SEA_SHAPES.has(shape)) {
567
+ const seaY = waterConfig.level - 0.015;
568
+ seaGeo = buildSquareSkirtGeometry(18, 48, Math.max(4, half - 28), outerRadius * 1.1, () => seaY);
569
+ const uDeep = uniform(new THREE.Vector3(...waterConfig.deepColor));
570
+ seaMat = new THREE.MeshBasicNodeMaterial({ fog: false });
571
+ seaMat.colorNode = Fn(() => {
572
+ const posW = positionWorld;
573
+ const toCam = cameraPosition.sub(posW);
574
+ const camDist = length(toCam);
575
+ const viewDir = toCam.div(max(camDist, float(1e-4)));
576
+ const waveFade = float(1).sub(smoothstep(float(110), float(380), camDist));
577
+ const pWarpX = sin(posW.z.mul(0.037).add(posW.x.mul(0.019))).mul(14.0);
578
+ const pWarpZ = cos(posW.x.mul(0.031).sub(posW.z.mul(0.023))).mul(12.0);
579
+ const rx = sin(posW.x.mul(0.17).add(posW.z.mul(0.11)).add(pWarpX))
580
+ .add(cos(posW.x.mul(0.31).sub(posW.z.mul(0.24))).mul(0.55))
581
+ .mul(0.012)
582
+ .mul(waveFade);
583
+ const rz = sin(posW.z.mul(0.19).sub(posW.x.mul(0.13)).add(pWarpZ))
584
+ .add(cos(posW.z.mul(0.29).add(posW.x.mul(0.22))).mul(0.55))
585
+ .mul(0.012)
586
+ .mul(waveFade);
587
+ const nWave = normalize(vec3(rx, float(1), rz));
588
+ const ndv = clamp(dot(nWave, viewDir), float(0), float(1));
589
+ const fresnel = float(0.02).add(float(0.98).mul(pow(float(1).sub(ndv), float(5))));
590
+ const bodyCol = uDeep.mul(mix(uShadeTint, uSunTint, float(0.45)));
591
+ const reflDir = normalize(vec3(viewDir.x.negate(), max(viewDir.y, float(0.03)), viewDir.z.negate()));
592
+ const skyRefl = mix(uShadeTint.mul(0.74).add(uSunTint.mul(0.14)), atmosphere.skyColor(reflDir).mul(0.92), float(0.65));
593
+ const halfVec = normalize(uSun.add(viewDir));
594
+ const sunGlint = pow(max(dot(nWave, halfVec), float(0)), float(180)).mul(1.4);
595
+ const waterCol = mix(bodyCol, skyRefl, fresnel.mul(0.9)).add(uSunTint.mul(sunGlint));
596
+ const iceAlb = vec3(0.76, 0.84, 0.92).mul(mix(uShadeTint, uSunTint, float(0.55)));
597
+ const col = mix(waterCol, iceAlb, smoothstep(float(0.45), float(0.85), uEnv.x));
598
+ return vec4(atmosphere.air(col, posW), 1);
599
+ })();
600
+ const seaMesh = new THREE.Mesh(seaGeo, seaMat);
601
+ seaMesh.name = "biome-horizon-sea";
602
+ seaMesh.frustumCulled = false;
603
+ group.add(seaMesh);
604
+ }
605
+ return {
606
+ object: group,
607
+ treeCount: hTreeCount,
608
+ update(sun, sunTint, shadeTint, snow, autumnTurn) {
609
+ uSun.value.copy(sun).normalize();
610
+ uSunTint.value.copy(sunTint);
611
+ uShadeTint.value.copy(shadeTint);
612
+ uEnv.value.set(clamp01(snow), clamp01(autumnTurn), waterConfig?.level ?? -999, waterConfig ? 1 : 0);
613
+ },
614
+ dispose() {
615
+ landGeo.dispose();
616
+ landMat.dispose();
617
+ crossGeo.dispose();
618
+ hTreeMat.dispose();
619
+ horizonTreeAtlas.dispose();
620
+ seaGeo?.dispose();
621
+ seaMat?.dispose();
622
+ },
623
+ };
624
+ }
625
+ //# sourceMappingURL=horizon.js.map