@threenative/terrain 0.1.11 → 0.1.13

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 (218) hide show
  1. package/README.md +1 -0
  2. package/dist/bake-DJ5cxg0C.d.ts +82 -0
  3. package/dist/editor/index.d.ts +4 -4
  4. package/dist/editor/index.js +1 -1
  5. package/dist/editor/server.d.ts +2 -2
  6. package/dist/editor/worker.d.ts +1 -1
  7. package/dist/editor/worker.js +1 -1
  8. package/dist/export.d.ts +1 -1
  9. package/dist/index.d.ts +51 -222
  10. package/dist/index.js +111 -2
  11. package/dist/kit-world.d.ts +107 -0
  12. package/dist/kit-world.js +5134 -0
  13. package/dist/kit.d.ts +21 -4
  14. package/dist/kit.js +19 -4
  15. package/dist/mcp/cook-child.js +9 -6
  16. package/dist/mcp/server.js +14 -4
  17. package/dist/prop-package-Dwz0EWoB.d.ts +141 -0
  18. package/dist/{server-B5RdvEAC.d.ts → server-LnFwY8sr.d.ts} +1 -1
  19. package/dist/starter.d.ts +1141 -6
  20. package/dist/starter.js +1999 -17
  21. package/dist/{terrain-CAEaW0mQ.d.ts → terrain-DYCyAOCJ.d.ts} +1 -1
  22. package/dist/three.d.ts +1 -1
  23. package/dist/{types-BpH00qsx.d.ts → types-kpI0C4hW.d.ts} +1 -1
  24. package/dist/{world-package-B8QgHjAQ.d.ts → world-package-DqJC3-Nn.d.ts} +2 -2
  25. package/docs/authoring/new-environment.md +97 -0
  26. package/package.json +8 -8
  27. package/starter/alpine/README.md +22 -14
  28. package/starter/alpine/bake.mjs +14 -1
  29. package/starter/alpine/biome.data.json +187 -25
  30. package/starter/alpine/heightmap.r16 +0 -0
  31. package/starter/alpine/playerRig.ts +89 -0
  32. package/starter/alpine/recipe.json +169 -15
  33. package/starter/alpine/render/bakedFile.ts +17 -0
  34. package/starter/alpine/render/benchmark.ts +77 -0
  35. package/starter/alpine/render/biomeData.ts +14 -0
  36. package/starter/alpine/render/biomes.ts +687 -0
  37. package/starter/alpine/render/ground/cover.ts +565 -0
  38. package/starter/alpine/render/ground/horizon.ts +219 -0
  39. package/starter/alpine/render/ground/palette.d.ts +1 -0
  40. package/starter/alpine/render/ground/palette.js +11 -0
  41. package/starter/alpine/render/ground/scatter.ts +104 -0
  42. package/starter/alpine/render/ground/terrain.ts +1860 -0
  43. package/starter/alpine/render/kitAssets.ts +28 -0
  44. package/starter/alpine/render/loading.ts +354 -0
  45. package/starter/alpine/render/lookKnobs.ts +37 -0
  46. package/starter/alpine/render/orbitCamera.ts +155 -0
  47. package/starter/alpine/render/props/farForest.ts +94 -0
  48. package/starter/alpine/render/props/pack.ts +1114 -0
  49. package/starter/alpine/render/props/prepared.ts +225 -0
  50. package/starter/alpine/render/props/propMaterials.ts +1239 -0
  51. package/starter/alpine/render/props/propProgress.ts +2 -0
  52. package/starter/alpine/render/props/propWorld.ts +105 -0
  53. package/starter/alpine/render/props/props.ts +633 -0
  54. package/starter/alpine/render/props/spruce.ts +440 -0
  55. package/starter/alpine/render/sky/atmosphere.ts +329 -0
  56. package/starter/alpine/render/sky/sky.ts +382 -0
  57. package/starter/alpine/render/texturePreparation.ts +135 -0
  58. package/starter/alpine/render/water/river.ts +1132 -0
  59. package/starter/alpine/render/worldNames.ts +6 -0
  60. package/starter/alpine/scene.ts +715 -0
  61. package/starter/alpine/sceneProps.ts +561 -0
  62. package/starter/alpine/sceneState.ts +33 -0
  63. package/starter/alpine/world/terrainAssets.ts +145 -0
  64. package/starter/alpine/world/walkBand.ts +47 -0
  65. package/starter/alpine/world/water.ts +74 -0
  66. package/starter/alpine/worlds.ts +13 -0
  67. package/starter/coastal/README.md +26 -27
  68. package/starter/coastal/assets.json +10 -0
  69. package/starter/coastal/bake.mjs +14 -1
  70. package/starter/coastal/biome.data.json +18 -3
  71. package/starter/coastal/playerRig.ts +89 -0
  72. package/starter/coastal/recipe.json +220 -18
  73. package/starter/coastal/render/bakedFile.ts +17 -0
  74. package/starter/coastal/render/benchmark.ts +81 -0
  75. package/starter/coastal/render/biomeData.ts +14 -0
  76. package/starter/coastal/render/biomes.ts +678 -0
  77. package/starter/coastal/render/ground/cover.ts +565 -0
  78. package/starter/coastal/render/ground/horizon.ts +219 -0
  79. package/starter/coastal/render/ground/palette.d.ts +1 -0
  80. package/starter/coastal/render/ground/palette.js +11 -0
  81. package/starter/coastal/render/ground/scatter.ts +104 -0
  82. package/starter/coastal/render/ground/terrain.ts +1829 -0
  83. package/starter/coastal/render/kitAssets.ts +28 -0
  84. package/starter/coastal/render/loading.ts +354 -0
  85. package/starter/coastal/render/lookKnobs.ts +37 -0
  86. package/starter/coastal/render/orbitCamera.ts +155 -0
  87. package/starter/coastal/render/props/farForest.ts +94 -0
  88. package/starter/coastal/render/props/pack.ts +1109 -0
  89. package/starter/coastal/render/props/prepared.ts +225 -0
  90. package/starter/coastal/render/props/propMaterials.ts +1239 -0
  91. package/starter/coastal/render/props/propProgress.ts +2 -0
  92. package/starter/coastal/render/props/propWorld.ts +105 -0
  93. package/starter/coastal/render/props/props.ts +633 -0
  94. package/starter/coastal/render/props/spruce.ts +440 -0
  95. package/starter/coastal/render/sky/atmosphere.ts +329 -0
  96. package/starter/coastal/render/sky/sky.ts +382 -0
  97. package/starter/coastal/render/texturePreparation.ts +135 -0
  98. package/starter/coastal/render/water/ocean.ts +284 -0
  99. package/starter/coastal/render/water/river.ts +1132 -0
  100. package/starter/coastal/render/worldNames.ts +6 -0
  101. package/starter/coastal/scene.ts +740 -0
  102. package/starter/coastal/sceneProps.ts +559 -0
  103. package/starter/coastal/sceneState.ts +33 -0
  104. package/starter/coastal/world/terrainAssets.ts +151 -0
  105. package/starter/coastal/world/walkBand.ts +47 -0
  106. package/starter/coastal/world/water.ts +74 -0
  107. package/starter/coastal/worlds.ts +13 -0
  108. package/starter/desert/README.md +51 -25
  109. package/starter/desert/bake-flora.mjs +142 -0
  110. package/starter/desert/bake-rocks.mjs +291 -0
  111. package/starter/desert/bake.mjs +14 -1
  112. package/starter/desert/biome.data.json +24 -12
  113. package/starter/desert/playerRig.ts +89 -0
  114. package/starter/desert/recipe.json +63 -16
  115. package/starter/desert/render/bakedFile.ts +17 -0
  116. package/starter/desert/render/benchmark.ts +79 -0
  117. package/starter/desert/render/biomeData.ts +14 -0
  118. package/starter/desert/render/biomes.ts +660 -0
  119. package/starter/desert/render/ground/cover.ts +565 -0
  120. package/starter/desert/render/ground/debris.ts +136 -0
  121. package/starter/desert/render/ground/horizon.ts +219 -0
  122. package/starter/desert/render/ground/palette.d.ts +1 -0
  123. package/starter/desert/render/ground/palette.js +11 -0
  124. package/starter/desert/render/ground/scatter.ts +104 -0
  125. package/starter/desert/render/ground/terrain.ts +1854 -0
  126. package/starter/desert/render/kitAssets.ts +28 -0
  127. package/starter/desert/render/loading.ts +354 -0
  128. package/starter/desert/render/lookKnobs.ts +37 -0
  129. package/starter/desert/render/orbitCamera.ts +155 -0
  130. package/starter/desert/render/props/farForest.ts +96 -0
  131. package/starter/desert/render/props/formations.ts +630 -0
  132. package/starter/desert/render/props/pack.ts +1195 -0
  133. package/starter/desert/render/props/prepared.ts +225 -0
  134. package/starter/desert/render/props/propMaterials.ts +1255 -0
  135. package/starter/desert/render/props/propProgress.ts +2 -0
  136. package/starter/desert/render/props/propWorld.ts +105 -0
  137. package/starter/desert/render/props/props.ts +624 -0
  138. package/starter/desert/render/props/spruce.ts +440 -0
  139. package/starter/desert/render/sand.ts +581 -0
  140. package/starter/desert/render/sky/atmosphere.ts +329 -0
  141. package/starter/desert/render/sky/sky.ts +382 -0
  142. package/starter/desert/render/texturePreparation.ts +135 -0
  143. package/starter/desert/render/water/river.ts +1132 -0
  144. package/starter/desert/render/worldNames.ts +6 -0
  145. package/starter/desert/scene.ts +748 -0
  146. package/starter/desert/sceneProps.ts +562 -0
  147. package/starter/desert/sceneState.ts +33 -0
  148. package/starter/desert/world/terrainAssets.ts +162 -0
  149. package/starter/desert/world/walkBand.ts +47 -0
  150. package/starter/desert/world/water.ts +74 -0
  151. package/starter/desert/worlds.ts +13 -0
  152. package/starter/forest/README.md +8 -5
  153. package/starter/forest/bake.mjs +21 -413
  154. package/starter/forest/playerRig.ts +37 -171
  155. package/starter/forest/recipe.json +237 -47
  156. package/starter/forest/render/biomes.ts +2 -1
  157. package/starter/forest/render/ground/horizon.ts +4 -302
  158. package/starter/forest/render/ground/terrain.ts +14 -32
  159. package/starter/forest/render/kitAssets.ts +3 -2
  160. package/starter/forest/render/props/farForest.ts +2 -1
  161. package/starter/forest/render/props/pack.ts +4 -2
  162. package/starter/forest/render/props/prepared.ts +2 -1
  163. package/starter/forest/render/props/propProgress.ts +1 -11
  164. package/starter/forest/render/props/propWorld.ts +48 -662
  165. package/starter/forest/render/props/props.ts +64 -248
  166. package/starter/forest/render/sky/sky.ts +2 -1
  167. package/starter/forest/render/worldNames.ts +6 -2
  168. package/starter/forest/scene.ts +34 -25
  169. package/starter/forest/sceneProps.ts +2 -2
  170. package/starter/forest/sceneState.ts +5 -4
  171. package/starter/forest/surface.json +2 -2
  172. package/starter/forest/worlds.ts +3 -2
  173. package/starter/tundra/README.md +35 -16
  174. package/starter/tundra/bake.mjs +14 -1
  175. package/starter/tundra/biome.data.json +6 -6
  176. package/starter/tundra/playerRig.ts +89 -0
  177. package/starter/tundra/recipe.json +111 -110
  178. package/starter/tundra/render/bakedFile.ts +17 -0
  179. package/starter/tundra/render/benchmark.ts +79 -0
  180. package/starter/tundra/render/biomeData.ts +14 -0
  181. package/starter/tundra/render/biomes.ts +714 -0
  182. package/starter/tundra/render/ground/cover.ts +565 -0
  183. package/starter/tundra/render/ground/horizon.ts +219 -0
  184. package/starter/tundra/render/ground/palette.d.ts +1 -0
  185. package/starter/tundra/render/ground/palette.js +11 -0
  186. package/starter/tundra/render/ground/scatter.ts +104 -0
  187. package/starter/tundra/render/ground/terrain.ts +1874 -0
  188. package/starter/tundra/render/kitAssets.ts +28 -0
  189. package/starter/tundra/render/loading.ts +354 -0
  190. package/starter/tundra/render/lookKnobs.ts +37 -0
  191. package/starter/tundra/render/orbitCamera.ts +155 -0
  192. package/starter/tundra/render/props/farForest.ts +96 -0
  193. package/starter/tundra/render/props/pack.ts +1088 -0
  194. package/starter/tundra/render/props/prepared.ts +225 -0
  195. package/starter/tundra/render/props/propMaterials.ts +1247 -0
  196. package/starter/tundra/render/props/propProgress.ts +2 -0
  197. package/starter/tundra/render/props/propWorld.ts +105 -0
  198. package/starter/tundra/render/props/props.ts +624 -0
  199. package/starter/tundra/render/props/spruce.ts +440 -0
  200. package/starter/tundra/render/sky/atmosphere.ts +329 -0
  201. package/starter/tundra/render/sky/sky.ts +382 -0
  202. package/starter/tundra/render/texturePreparation.ts +135 -0
  203. package/starter/tundra/render/water/river.ts +1132 -0
  204. package/starter/tundra/render/worldNames.ts +6 -0
  205. package/starter/tundra/scene.ts +715 -0
  206. package/starter/tundra/sceneProps.ts +559 -0
  207. package/starter/tundra/sceneState.ts +33 -0
  208. package/starter/tundra/world/terrainAssets.ts +159 -0
  209. package/starter/tundra/world/walkBand.ts +47 -0
  210. package/starter/tundra/world/water.ts +74 -0
  211. package/starter/tundra/worlds.ts +13 -0
  212. package/starter-assets.json +4 -4
  213. package/starter-credits.json +613 -10
  214. package/starter/forest/diagnostics.ts +0 -1020
  215. package/starter/forest/render/sky/skyVisibility.ts +0 -121
  216. package/starter/forest/spawnGate.ts +0 -183
  217. package/starter/forest/viewStatistics.ts +0 -47
  218. package/starter/forest/world/streamedGround.ts +0 -139
@@ -4,27 +4,10 @@ import {
4
4
  HeightfieldCollar,
5
5
  type IHeightfieldCollarSite,
6
6
  } from "@threenative/core/world";
7
- import { DataTexture, FloatType, MathUtils, NearestFilter, RedFormat } from "three";
8
- import {
9
- abs,
10
- float,
11
- floor,
12
- max,
13
- min,
14
- modelWorldMatrix,
15
- positionGeometry,
16
- positionLocal,
17
- select,
18
- smoothstep,
19
- texture,
20
- vec2,
21
- vec3,
22
- vec4,
23
- } from "three/tsl";
24
- import type { Node } from "three/webgpu";
7
+ import { MathUtils } from "three";
25
8
  import { fetchBakedFile } from "../bakedFile.js";
26
9
  import type { WorldName } from "../biomes.js";
27
- import type { IBakedWorld, ITerrainStream } from "./terrain.js";
10
+ import type { IBakedWorld } from "./terrain.js";
28
11
 
29
12
  /** `world/horizon.json`: each world's same-site USGS surroundings, as `scripts/bake/bake.mjs` writes them. */
30
13
  export type HorizonSurroundings = Record<
@@ -129,22 +112,10 @@ export const HORIZON_WALK_BAND = 200;
129
112
  /** Rings of the far collar, and the radius they reach: the survey's own rim, at 2.1 km. */
130
113
  const HORIZON_RINGS = 384;
131
114
  export const HORIZON_REACH = 2100;
132
- /** Streamed tile width in metres; the outermost tile of the drawn square is the morph band. */
133
- export const STREAM_TILE = 64;
134
- /**
135
- * Metres the collar also draws under the streamed block's edge. The tile and the collar cut their
136
- * triangles differently, so at the split their surfaces part by up to the fold `collarSeamGap`
137
- * measures; cut at one line, a view across that step saw the sky through it as a dotted white seam.
138
- */
139
- export const COLLAR_UNDER_TILE = STREAM_TILE / 4;
140
- /**
141
- * Metres inside the square's edge where the streamed tiles still draw. Each tile hangs a skirt on
142
- * that edge; a mask cut on the edge itself dropped it, and the rounding cracks where the square
143
- * and the tiles meet showed the sky as a dotted white line along low-view crests.
144
- */
145
- export const TILE_INSIDE_SQUARE = 0.5;
146
115
  /** How the rings spread: ring `k` stands `(k / rings) ** 1.65` of the reach past the edge. */
147
116
  const HORIZON_POWER = 1.65;
117
+ /** The ring curve `createHorizonCollar` builds and the package's collar morph inverts. */
118
+ export const COLLAR_CURVE = { power: HORIZON_POWER, reach: HORIZON_REACH } as const;
148
119
 
149
120
  /**
150
121
  * The far collar for one world: the square's edge continued onto that site's surveyed surroundings,
@@ -246,272 +217,3 @@ export function collarColors(collar: HeightfieldCollar, data: IBakedWorld): Floa
246
217
  }
247
218
  return colors;
248
219
  }
249
-
250
- /**
251
- * The collar triangle under `(x, z)` as it is drawn (not a bilinear blend of its quad): its three
252
- * vertex indices and the point's weights. Undefined where there is no collar.
253
- */
254
- function collarTriangle(
255
- collar: HeightfieldCollar,
256
- x: number,
257
- z: number,
258
- ): { indices: [number, number, number]; weights: [number, number, number] } | undefined {
259
- const quad = collar.locate(x, z);
260
- if (quad === undefined) return undefined;
261
- const position = collar.geometry.getAttribute("position");
262
- const a = collar.index(quad.lower, quad.from);
263
- const b = collar.index(quad.lower, quad.to);
264
- const c = collar.index(quad.upper, quad.from);
265
- const d = collar.index(quad.upper, quad.to);
266
- // The collar's two triangles per quad are (a, b, c) and (b, d, c); the point's barycentrics in
267
- // the plane pick the one it lies in.
268
- for (const [p, q, r] of [
269
- [a, b, c],
270
- [b, d, c],
271
- ] as const) {
272
- const [px, pz] = [position.getX(p), position.getZ(p)];
273
- const [qx, qz] = [position.getX(q), position.getZ(q)];
274
- const [rx, rz] = [position.getX(r), position.getZ(r)];
275
- const det = (qz - rz) * (px - rx) + (rx - qx) * (pz - rz);
276
- const u = ((qz - rz) * (x - rx) + (rx - qx) * (z - rz)) / det;
277
- const v = ((rz - pz) * (x - rx) + (px - rx) * (z - rz)) / det;
278
- if (u >= -1e-6 && v >= -1e-6 && u + v <= 1 + 1e-6)
279
- return { indices: [p, q, r], weights: [u, v, 1 - u - v] };
280
- }
281
- return undefined;
282
- }
283
-
284
- /** The collar's drawn height under `(x, z)`; null where there is no collar. */
285
- export function collarDrawnHeight(collar: HeightfieldCollar, x: number, z: number): number | null {
286
- const triangle = collarTriangle(collar, x, z);
287
- if (triangle === undefined) return null;
288
- const position = collar.geometry.getAttribute("position");
289
- const [p, q, r] = triangle.indices;
290
- const [u, v, w] = triangle.weights;
291
- return u * position.getY(p) + v * position.getY(q) + w * position.getY(r);
292
- }
293
-
294
- /**
295
- * PRD-790: where streamed tiles meet the collar. The tiles draw inside the square
296
- * `(centreX, centreZ, half)` and the collar outside it, so the seam is that square's edge, past the
297
- * playable square. `gap` is the largest step at the tiles' edge vertices (`spacing` apart), as core
298
- * measures a tile-to-tile seam; `fold` is the largest step between them, where a fold of the
299
- * collar's coarser triangles crosses a tile's straight edge.
300
- */
301
- export function collarSeamGap(
302
- collar: HeightfieldCollar,
303
- height: (x: number, z: number) => number,
304
- centreX: number,
305
- centreZ: number,
306
- half: number,
307
- spacing: number,
308
- step = 0.1,
309
- ): { fold: number; gap: number } {
310
- let gap = 0;
311
- let fold = 0;
312
- const cells = Math.round((2 * half) / spacing);
313
- // Each edge of the square: its first corner and the unit step along it.
314
- const edges = [
315
- [centreX - half, centreZ - half, 1, 0],
316
- [centreX - half, centreZ + half, 1, 0],
317
- [centreX - half, centreZ - half, 0, 1],
318
- [centreX + half, centreZ - half, 0, 1],
319
- ] as const;
320
- for (const [startX, startZ, dx, dz] of edges)
321
- for (let cell = 0; cell < cells; cell += 1) {
322
- const h0 = height(startX + dx * cell * spacing, startZ + dz * cell * spacing);
323
- const h1 = height(startX + dx * (cell + 1) * spacing, startZ + dz * (cell + 1) * spacing);
324
- for (let offset = 0; offset < spacing; offset += step) {
325
- const along = cell * spacing + offset;
326
- const drawn = collarDrawnHeight(collar, startX + dx * along, startZ + dz * along);
327
- if (drawn === null) continue;
328
- const rise = Math.abs(drawn - (h0 + ((h1 - h0) * offset) / spacing));
329
- if (offset === 0) gap = Math.max(gap, rise);
330
- else fold = Math.max(fold, rise);
331
- }
332
- }
333
- return { fold, gap };
334
- }
335
-
336
- /**
337
- * The collar triangle under `(x, z)` found by arithmetic alone, as the tile shader finds it: the
338
- * ring from the ring spacing, the perimeter step from the face the point projects onto, and the
339
- * triangle from the point's place in the quad. On one face the world point is an affine map of
340
- * `(scale, share * scale)`, so weights read there are the drawn triangle's own.
341
- */
342
- export function collarArithmeticHeight(collar: HeightfieldCollar, x: number, z: number): number {
343
- const position = collar.geometry.getAttribute("position");
344
- const at = (ring: number, vertex: number): number =>
345
- position.getY(ring * collar.perimeter + vertex);
346
- const q = collarQuad(collar.perimeter, collar.rings, collarHalf(collar), x, z);
347
- return q.first
348
- ? q.wa * at(q.lower, q.from) + q.wb * at(q.lower, q.to) + q.wc * at(q.lower + 1, q.from)
349
- : q.wb * at(q.lower, q.to) + q.wd * at(q.lower + 1, q.to) + q.wc * at(q.lower + 1, q.from);
350
- }
351
-
352
- /** Half the playable square, read off the collar's first ring. */
353
- function collarHalf(collar: HeightfieldCollar): number {
354
- const position = collar.geometry.getAttribute("position");
355
- return Math.abs(position.getZ(0));
356
- }
357
-
358
- function collarQuad(perimeter: number, rings: number, half: number, x: number, z: number) {
359
- const segments = perimeter / 4;
360
- const cell = (2 * half) / segments;
361
- const scaleOf = (ring: number): number =>
362
- 1 + ((ring / rings) ** HORIZON_POWER * HORIZON_REACH) / half;
363
- const chebyshev = Math.max(Math.abs(x), Math.abs(z));
364
- const scale = chebyshev / half;
365
- const past = Math.max(0, chebyshev - half);
366
- const lower = Math.min(
367
- rings - 1,
368
- Math.max(0, Math.floor(rings * (past / HORIZON_REACH) ** (1 / HORIZON_POWER))),
369
- );
370
- const low = scaleOf(lower);
371
- const high = scaleOf(lower + 1);
372
- const edgeX = x / scale;
373
- const edgeZ = z / scale;
374
- const along =
375
- Math.abs(z) >= Math.abs(x)
376
- ? z < 0
377
- ? (edgeX + half) / cell
378
- : 2 * segments + (half - edgeX) / cell
379
- : x > 0
380
- ? segments + (edgeZ + half) / cell
381
- : 3 * segments + (half - edgeZ) / cell;
382
- const from = Math.min(perimeter - 1, Math.max(0, Math.floor(along)));
383
- const share = along - from;
384
- const ring = (scale - low) / (high - low);
385
- const b = (share * scale) / low;
386
- const first = b + ring <= 1;
387
- const back = 1 - ring;
388
- const d = (share * scale - back * low) / high;
389
- return {
390
- first,
391
- from,
392
- lower,
393
- to: (from + 1) % perimeter,
394
- wa: 1 - b - ring,
395
- wb: first ? b : back,
396
- wc: first ? ring : 1 - back - d,
397
- wd: d,
398
- };
399
- }
400
-
401
- /**
402
- * PRD-790 box 2: the streamed tiles' vertex shift. Only the outermost tile of the square the tiles
403
- * draw slides onto the collar's drawn height, smoothly over that tile, so at the square's edge every
404
- * tile vertex stands on the collar; the ground inside keeps the survey's smooth surface. The
405
- * collar's heights go to the GPU once, as a float texture of rings by perimeter steps.
406
- */
407
- export function collarMorph(
408
- collar: HeightfieldCollar,
409
- stream: ITerrainStream,
410
- band: number,
411
- ): Node<"vec3"> {
412
- const { perimeter, rings } = collar;
413
- const half = collarHalf(collar);
414
- const segments = perimeter / 4;
415
- const cell = (2 * half) / segments;
416
- const position = collar.geometry.getAttribute("position");
417
- const heights = new Float32Array(perimeter * (rings + 1));
418
- for (let index = 0; index < heights.length; index += 1) heights[index] = position.getY(index);
419
- const map = new DataTexture(heights, perimeter, rings + 1, RedFormat, FloatType);
420
- map.minFilter = NearestFilter;
421
- map.magFilter = NearestFilter;
422
- map.needsUpdate = true;
423
- const at = (ring: Node<"float">, vertex: Node<"float">): Node<"float"> =>
424
- texture(map, vec2(vertex.add(0.5).div(perimeter), ring.add(0.5).div(rings + 1))).r;
425
-
426
- const world = modelWorldMatrix.mul(vec4(positionGeometry, 1));
427
- const x = world.x;
428
- const z = world.z;
429
- const chebyshev = max(abs(x), abs(z));
430
- const scale = chebyshev.div(half);
431
- const past = chebyshev.sub(half).max(0);
432
- const lower = min(
433
- float(rings - 1),
434
- floor(
435
- past
436
- .div(HORIZON_REACH)
437
- .pow(1 / HORIZON_POWER)
438
- .mul(rings),
439
- ),
440
- );
441
- const scaleOf = (ring: Node<"float">): Node<"float"> =>
442
- ring
443
- .div(rings)
444
- .pow(HORIZON_POWER)
445
- .mul(HORIZON_REACH / half)
446
- .add(1);
447
- const low = scaleOf(lower);
448
- const high = scaleOf(lower.add(1));
449
- const edgeX = x.div(scale);
450
- const edgeZ = z.div(scale);
451
- const along = select(
452
- abs(z).greaterThanEqual(abs(x)),
453
- select(
454
- z.lessThan(0),
455
- edgeX.add(half).div(cell),
456
- float(half)
457
- .sub(edgeX)
458
- .div(cell)
459
- .add(2 * segments),
460
- ),
461
- select(
462
- x.greaterThan(0),
463
- edgeZ.add(half).div(cell).add(segments),
464
- float(half)
465
- .sub(edgeZ)
466
- .div(cell)
467
- .add(3 * segments),
468
- ),
469
- );
470
- const from = min(float(perimeter - 1), floor(along).max(0));
471
- const share = along.sub(from);
472
- const to = select(from.equal(perimeter - 1), float(0), from.add(1));
473
- const ring = scale.sub(low).div(high.sub(low));
474
- const b = share.mul(scale).div(low);
475
- const back = float(1).sub(ring);
476
- const d = share.mul(scale).sub(back.mul(low)).div(high);
477
- const upper = lower.add(1);
478
- const first = at(lower, from)
479
- .mul(float(1).sub(b).sub(ring))
480
- .add(at(lower, to).mul(b))
481
- .add(at(upper, from).mul(ring));
482
- const second = at(lower, to)
483
- .mul(back)
484
- .add(at(upper, to).mul(d))
485
- .add(at(upper, from).mul(float(1).sub(back).sub(d)));
486
- const drawn = select(b.add(ring).lessThanEqual(1), first, second);
487
- // The outermost tile of the drawn square, and never inside the playable square.
488
- const fromCentre = max(abs(x.sub(stream.centre.x)), abs(z.sub(stream.centre.y)));
489
- const weight = smoothstep(stream.half.sub(band), stream.half, fromCentre).mul(
490
- past.greaterThan(0).select(float(1), float(0)),
491
- );
492
- return positionLocal.add(vec3(0, drawn.sub(world.y).mul(weight), 0));
493
- }
494
-
495
- /**
496
- * The height a tile vertex stands at once `collarMorph` moved it: the survey inside the band, the
497
- * collar's arithmetic height at the drawn square's edge. The seam measure reads this.
498
- */
499
- export function createMorphedHeight(
500
- collar: HeightfieldCollar,
501
- height: (x: number, z: number) => number,
502
- centreX: number,
503
- centreZ: number,
504
- half: number,
505
- band: number,
506
- ): (x: number, z: number) => number {
507
- const square = collarHalf(collar);
508
- return (x, z) => {
509
- const fine = height(x, z);
510
- const past = Math.max(Math.abs(x), Math.abs(z)) - square;
511
- if (past <= 0) return fine;
512
- const fromCentre = Math.max(Math.abs(x - centreX), Math.abs(z - centreZ));
513
- const t = Math.min(1, Math.max(0, (fromCentre - (half - band)) / band));
514
- const weight = MathUtils.smoothstep(t, 0, 1);
515
- return fine + (collarArithmeticHeight(collar, x, z) - fine) * weight;
516
- };
517
- }
@@ -7,7 +7,13 @@ import {
7
7
  loadTerrainSplat,
8
8
  } from "@threenative/core/world";
9
9
  import { waterlineRadius } from "@threenative/core/world";
10
- import { createSegmentIndex } from "@threenative/terrain";
10
+ import { createSegmentIndex, paintCanopyCover } from "@threenative/terrain";
11
+ import {
12
+ COLLAR_UNDER_TILE,
13
+ STREAM_TILE,
14
+ TILE_INSIDE_SQUARE,
15
+ collarMorph,
16
+ } from "@threenative/terrain/starter";
11
17
  import {
12
18
  ClampToEdgeWrapping,
13
19
  DataTexture,
@@ -84,13 +90,11 @@ import {
84
90
  REFLECTED_LAYER,
85
91
  surfaceHeights,
86
92
  } from "../water/river.js";
93
+ import { WORLD } from "../worldNames.js";
87
94
  import {
88
- COLLAR_UNDER_TILE,
95
+ COLLAR_CURVE,
89
96
  type IPackedMassif,
90
- STREAM_TILE,
91
- TILE_INSIDE_SQUARE,
92
97
  collarColors,
93
- collarMorph,
94
98
  createHorizonCollar,
95
99
  horizonSurroundings,
96
100
  } from "./horizon.js";
@@ -511,7 +515,7 @@ function microGrain(): Node<"vec3"> {
511
515
  }
512
516
 
513
517
  /** Crown radius in metres per unit of placement scale, for the trees that drop needle litter. */
514
- const CANOPY_CROWN: Readonly<Record<string, number>> = { spruce: 7, sapling: 2.5 };
518
+ export const CANOPY_CROWN: Readonly<Record<string, number>> = { spruce: 7, sapling: 2.5 };
515
519
 
516
520
  /**
517
521
  * Paint each placed tree's crown into the terrain's `canopy` vertex attribute, so the ground under a
@@ -529,29 +533,7 @@ export function paintCanopy(
529
533
  ): void {
530
534
  const cover = mesh.geometry.getAttribute("canopy");
531
535
  if (!(cover instanceof Float32BufferAttribute) || data.resolution < 2) return;
532
- const values = cover.array as Float32Array;
533
- const spacing = data.size / (data.resolution - 1);
534
- const half = data.size / 2;
535
- for (const placement of placements) {
536
- const crown = CANOPY_CROWN[placement.asset];
537
- if (crown === undefined) continue;
538
- const radius = Math.max(spacing, crown * placement.scale);
539
- const x = placement.position[0] ?? 0;
540
- const z = placement.position[2] ?? 0;
541
- const fromColumn = Math.max(0, Math.ceil((x - radius + half) / spacing));
542
- const toColumn = Math.min(data.resolution - 1, Math.floor((x + radius + half) / spacing));
543
- const fromRow = Math.max(0, Math.ceil((z - radius + half) / spacing));
544
- const toRow = Math.min(data.resolution - 1, Math.floor((z + radius + half) / spacing));
545
- for (let row = fromRow; row <= toRow; row++)
546
- for (let column = fromColumn; column <= toColumn; column++) {
547
- const dx = column * spacing - half - x;
548
- const dz = row * spacing - half - z;
549
- const reach = 1 - (dx * dx + dz * dz) / (radius * radius);
550
- if (reach <= 0) continue;
551
- const index = row * data.resolution + column;
552
- values[index] = Math.max(values[index] as number, reach);
553
- }
554
- }
536
+ paintCanopyCover(cover.array as Float32Array, data, placements, CANOPY_CROWN);
555
537
  cover.needsUpdate = true;
556
538
  }
557
539
 
@@ -914,7 +896,7 @@ export async function createGroundMaterial(
914
896
  }
915
897
  return (await loadTerrainSplat({
916
898
  assets,
917
- url: `world/${biome?.world ?? "forest"}.world.json`,
899
+ url: `world/${biome?.world ?? WORLD}.world.json`,
918
900
  layerSample: ({ layer: descriptor, diffuse, normal: normalMap }) => {
919
901
  const key = descriptor.id as LayerKey;
920
902
  maps[key] ??= { diffuse, normal: normalMap };
@@ -1376,7 +1358,7 @@ export function createTerrain(
1376
1358
  mesh.layers.enable(REFLECTED_LAYER);
1377
1359
  mesh.receiveShadow = true;
1378
1360
  mesh.castShadow = biome?.terrainCastsShadow ?? false;
1379
- const collar = createHorizonCollar(data, "forest");
1361
+ const collar = createHorizonCollar(data, WORLD);
1380
1362
  const horizonGeometry = collar.geometry;
1381
1363
  horizonGeometry.setAttribute("color", new Float32BufferAttribute(collarColors(collar, data), 3));
1382
1364
  horizonGeometry.computeVertexNormals();
@@ -1477,7 +1459,7 @@ export function createTerrain(
1477
1459
  far.maskNode = fromCentre.greaterThanEqual(stream.half.sub(COLLAR_UNDER_TILE));
1478
1460
  horizon.material = far;
1479
1461
  // The outermost streamed tile slides onto the collar's drawn height (PRD-790 box 2).
1480
- tile.positionNode = collarMorph(collar, stream, STREAM_TILE);
1462
+ tile.positionNode = collarMorph(collar, COLLAR_CURVE, stream, STREAM_TILE);
1481
1463
  tile.maskNode = fromCentre
1482
1464
  .lessThan(stream.half)
1483
1465
  // The skirt each tile hangs on the square's edge draws too, under the square's cracks.
@@ -1,7 +1,8 @@
1
1
  import type { IAssetLoader } from "@threenative/core";
2
+ import { WORLD } from "./worldNames.js";
2
3
 
3
- /** Where `bake.mjs --out assets/terrain/forest` writes the forest inside the game's asset tree. */
4
- export const FOREST_ROOT = "terrain/forest/";
4
+ /** Where `bake.mjs` (default `--out assets/terrain/<WORLD>`) writes the world in the game's asset tree. */
5
+ export const FOREST_ROOT = `terrain/${WORLD}/`;
5
6
 
6
7
  /**
7
8
  * The game's asset loader, with every relative path read under {@link FOREST_ROOT}: the forest's
@@ -21,6 +21,7 @@ import {
21
21
  import { biomeSpec } from "../biomeData.js";
22
22
  import type { WorldName } from "../biomes.js";
23
23
  import type { IBakedLake, IBakedRiver } from "../water/river.js";
24
+ import { WORLD } from "../worldNames.js";
24
25
 
25
26
  /**
26
27
  * The far trees plant the band of hills just past the square, where a bare lawn read as a bug
@@ -30,7 +31,7 @@ const NEAR_REACH = 700;
30
31
  export const FAR_FOREST = true;
31
32
 
32
33
  /** The worlds that plant the collar past the square: the forest's surveyed valley. */
33
- export const FAR_FOREST_WORLDS: readonly WorldName[] = ["forest"];
34
+ export const FAR_FOREST_WORLDS: readonly WorldName[] = [WORLD];
34
35
 
35
36
  /** The layer `streamedBucket` reads to put a placement in the far, shadowless bucket. */
36
37
  export const FAR_FOREST_LAYER = "far-forest";
@@ -79,6 +79,7 @@ import {
79
79
  measureBiome,
80
80
  } from "../biomes.js";
81
81
  import { LOOK } from "../lookKnobs.js";
82
+ import { WORLD } from "../worldNames.js";
82
83
  import {
83
84
  type IPropGround,
84
85
  createRockGround,
@@ -911,7 +912,7 @@ export interface IPackProps {
911
912
  }
912
913
  export async function loadPack(
913
914
  assets?: IAssetLoader,
914
- world: WorldName = "forest",
915
+ world: WorldName = WORLD,
915
916
  ground?: IPropGround,
916
917
  comparison?: ICanopyComparison,
917
918
  biome?: IWorldBiome,
@@ -946,7 +947,8 @@ export async function loadPack(
946
947
  // A species with no scan keeps its procedural part (`buildPropVariants`).
947
948
  if (!assets || !one.path.endsWith(".glb")) return undefined;
948
949
  try {
949
- return await assets.model<{ scene?: Group }>(one.path);
950
+ // The pack copies vertices, so the streamed coarse companion would never upgrade.
951
+ return await assets.model<{ scene?: Group }>(one.path, { whole: true });
950
952
  } catch {
951
953
  return undefined;
952
954
  }
@@ -167,7 +167,8 @@ export async function loadPreparedProps(assets?: IAssetLoader): Promise<IPrepare
167
167
  const loaded = await Promise.all(
168
168
  PREPARED.map(async (file) => {
169
169
  try {
170
- return { file, gltf: await assets.model<{ scene?: Group }>(file.path) };
170
+ // Copied into parts below: wait for the full file, not the streamed coarse companion.
171
+ return { file, gltf: await assets.model<{ scene?: Group }>(file.path, { whole: true }) };
171
172
  } catch {
172
173
  return { file, gltf: undefined };
173
174
  }
@@ -1,12 +1,2 @@
1
- import type { WorldCells } from "@threenative/core/world";
2
-
3
1
  /** Consumer preparation only; the worlds still own admission, residency and prewarm. */
4
- export interface IPropPreparationProgress {
5
- readonly phase: "grounding" | "models" | "world-load" | "complete";
6
- readonly bucket: string;
7
- readonly added: number;
8
- readonly total: number;
9
- readonly worlds: readonly WorldCells[];
10
- /** Live promise settlements, distinct from load completion and the last rendered counters. */
11
- readonly prewarmedWorlds: number;
12
- }
2
+ export type { IPropPreparationProgress } from "@threenative/terrain/starter";