@threenative/terrain 0.1.11 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/bake-DJ5cxg0C.d.ts +82 -0
- package/dist/editor/index.d.ts +4 -4
- package/dist/editor/index.js +1 -1
- package/dist/editor/server.d.ts +2 -2
- package/dist/editor/worker.d.ts +1 -1
- package/dist/editor/worker.js +1 -1
- package/dist/export.d.ts +1 -1
- package/dist/index.d.ts +51 -222
- package/dist/index.js +111 -2
- package/dist/kit-world.d.ts +107 -0
- package/dist/kit-world.js +5134 -0
- package/dist/kit.d.ts +21 -4
- package/dist/kit.js +19 -4
- package/dist/mcp/cook-child.js +9 -6
- package/dist/mcp/server.js +14 -4
- package/dist/prop-package-Dwz0EWoB.d.ts +141 -0
- package/dist/{server-B5RdvEAC.d.ts → server-LnFwY8sr.d.ts} +1 -1
- package/dist/starter.d.ts +1141 -6
- package/dist/starter.js +1999 -17
- package/dist/{terrain-CAEaW0mQ.d.ts → terrain-DYCyAOCJ.d.ts} +1 -1
- package/dist/three.d.ts +1 -1
- package/dist/{types-BpH00qsx.d.ts → types-kpI0C4hW.d.ts} +1 -1
- package/dist/{world-package-B8QgHjAQ.d.ts → world-package-DqJC3-Nn.d.ts} +2 -2
- package/docs/authoring/new-environment.md +97 -0
- package/package.json +8 -8
- package/starter/alpine/README.md +22 -14
- package/starter/alpine/bake.mjs +14 -1
- package/starter/alpine/biome.data.json +187 -25
- package/starter/alpine/heightmap.r16 +0 -0
- package/starter/alpine/playerRig.ts +89 -0
- package/starter/alpine/recipe.json +169 -15
- package/starter/alpine/render/bakedFile.ts +17 -0
- package/starter/alpine/render/benchmark.ts +77 -0
- package/starter/alpine/render/biomeData.ts +14 -0
- package/starter/alpine/render/biomes.ts +687 -0
- package/starter/alpine/render/ground/cover.ts +565 -0
- package/starter/alpine/render/ground/horizon.ts +219 -0
- package/starter/alpine/render/ground/palette.d.ts +1 -0
- package/starter/alpine/render/ground/palette.js +11 -0
- package/starter/alpine/render/ground/scatter.ts +104 -0
- package/starter/alpine/render/ground/terrain.ts +1860 -0
- package/starter/alpine/render/kitAssets.ts +28 -0
- package/starter/alpine/render/loading.ts +354 -0
- package/starter/alpine/render/lookKnobs.ts +37 -0
- package/starter/alpine/render/orbitCamera.ts +155 -0
- package/starter/alpine/render/props/farForest.ts +94 -0
- package/starter/alpine/render/props/pack.ts +1114 -0
- package/starter/alpine/render/props/prepared.ts +225 -0
- package/starter/alpine/render/props/propMaterials.ts +1239 -0
- package/starter/alpine/render/props/propProgress.ts +2 -0
- package/starter/alpine/render/props/propWorld.ts +105 -0
- package/starter/alpine/render/props/props.ts +633 -0
- package/starter/alpine/render/props/spruce.ts +440 -0
- package/starter/alpine/render/sky/atmosphere.ts +329 -0
- package/starter/alpine/render/sky/sky.ts +382 -0
- package/starter/alpine/render/texturePreparation.ts +135 -0
- package/starter/alpine/render/water/river.ts +1132 -0
- package/starter/alpine/render/worldNames.ts +6 -0
- package/starter/alpine/scene.ts +715 -0
- package/starter/alpine/sceneProps.ts +561 -0
- package/starter/alpine/sceneState.ts +33 -0
- package/starter/alpine/world/terrainAssets.ts +145 -0
- package/starter/alpine/world/walkBand.ts +47 -0
- package/starter/alpine/world/water.ts +74 -0
- package/starter/alpine/worlds.ts +13 -0
- package/starter/coastal/README.md +26 -27
- package/starter/coastal/assets.json +10 -0
- package/starter/coastal/bake.mjs +14 -1
- package/starter/coastal/biome.data.json +18 -3
- package/starter/coastal/playerRig.ts +89 -0
- package/starter/coastal/recipe.json +220 -18
- package/starter/coastal/render/bakedFile.ts +17 -0
- package/starter/coastal/render/benchmark.ts +81 -0
- package/starter/coastal/render/biomeData.ts +14 -0
- package/starter/coastal/render/biomes.ts +678 -0
- package/starter/coastal/render/ground/cover.ts +565 -0
- package/starter/coastal/render/ground/horizon.ts +219 -0
- package/starter/coastal/render/ground/palette.d.ts +1 -0
- package/starter/coastal/render/ground/palette.js +11 -0
- package/starter/coastal/render/ground/scatter.ts +104 -0
- package/starter/coastal/render/ground/terrain.ts +1829 -0
- package/starter/coastal/render/kitAssets.ts +28 -0
- package/starter/coastal/render/loading.ts +354 -0
- package/starter/coastal/render/lookKnobs.ts +37 -0
- package/starter/coastal/render/orbitCamera.ts +155 -0
- package/starter/coastal/render/props/farForest.ts +94 -0
- package/starter/coastal/render/props/pack.ts +1109 -0
- package/starter/coastal/render/props/prepared.ts +225 -0
- package/starter/coastal/render/props/propMaterials.ts +1239 -0
- package/starter/coastal/render/props/propProgress.ts +2 -0
- package/starter/coastal/render/props/propWorld.ts +105 -0
- package/starter/coastal/render/props/props.ts +633 -0
- package/starter/coastal/render/props/spruce.ts +440 -0
- package/starter/coastal/render/sky/atmosphere.ts +329 -0
- package/starter/coastal/render/sky/sky.ts +382 -0
- package/starter/coastal/render/texturePreparation.ts +135 -0
- package/starter/coastal/render/water/ocean.ts +284 -0
- package/starter/coastal/render/water/river.ts +1132 -0
- package/starter/coastal/render/worldNames.ts +6 -0
- package/starter/coastal/scene.ts +740 -0
- package/starter/coastal/sceneProps.ts +559 -0
- package/starter/coastal/sceneState.ts +33 -0
- package/starter/coastal/world/terrainAssets.ts +151 -0
- package/starter/coastal/world/walkBand.ts +47 -0
- package/starter/coastal/world/water.ts +74 -0
- package/starter/coastal/worlds.ts +13 -0
- package/starter/desert/README.md +51 -25
- package/starter/desert/bake-flora.mjs +142 -0
- package/starter/desert/bake-rocks.mjs +291 -0
- package/starter/desert/bake.mjs +14 -1
- package/starter/desert/biome.data.json +24 -12
- package/starter/desert/playerRig.ts +89 -0
- package/starter/desert/recipe.json +63 -16
- package/starter/desert/render/bakedFile.ts +17 -0
- package/starter/desert/render/benchmark.ts +79 -0
- package/starter/desert/render/biomeData.ts +14 -0
- package/starter/desert/render/biomes.ts +660 -0
- package/starter/desert/render/ground/cover.ts +565 -0
- package/starter/desert/render/ground/debris.ts +136 -0
- package/starter/desert/render/ground/horizon.ts +219 -0
- package/starter/desert/render/ground/palette.d.ts +1 -0
- package/starter/desert/render/ground/palette.js +11 -0
- package/starter/desert/render/ground/scatter.ts +104 -0
- package/starter/desert/render/ground/terrain.ts +1854 -0
- package/starter/desert/render/kitAssets.ts +28 -0
- package/starter/desert/render/loading.ts +354 -0
- package/starter/desert/render/lookKnobs.ts +37 -0
- package/starter/desert/render/orbitCamera.ts +155 -0
- package/starter/desert/render/props/farForest.ts +96 -0
- package/starter/desert/render/props/formations.ts +630 -0
- package/starter/desert/render/props/pack.ts +1195 -0
- package/starter/desert/render/props/prepared.ts +225 -0
- package/starter/desert/render/props/propMaterials.ts +1255 -0
- package/starter/desert/render/props/propProgress.ts +2 -0
- package/starter/desert/render/props/propWorld.ts +105 -0
- package/starter/desert/render/props/props.ts +624 -0
- package/starter/desert/render/props/spruce.ts +440 -0
- package/starter/desert/render/sand.ts +581 -0
- package/starter/desert/render/sky/atmosphere.ts +329 -0
- package/starter/desert/render/sky/sky.ts +382 -0
- package/starter/desert/render/texturePreparation.ts +135 -0
- package/starter/desert/render/water/river.ts +1132 -0
- package/starter/desert/render/worldNames.ts +6 -0
- package/starter/desert/scene.ts +748 -0
- package/starter/desert/sceneProps.ts +562 -0
- package/starter/desert/sceneState.ts +33 -0
- package/starter/desert/world/terrainAssets.ts +162 -0
- package/starter/desert/world/walkBand.ts +47 -0
- package/starter/desert/world/water.ts +74 -0
- package/starter/desert/worlds.ts +13 -0
- package/starter/forest/README.md +8 -5
- package/starter/forest/bake.mjs +21 -413
- package/starter/forest/playerRig.ts +37 -171
- package/starter/forest/recipe.json +237 -47
- package/starter/forest/render/biomes.ts +2 -1
- package/starter/forest/render/ground/horizon.ts +4 -302
- package/starter/forest/render/ground/terrain.ts +14 -32
- package/starter/forest/render/kitAssets.ts +3 -2
- package/starter/forest/render/props/farForest.ts +2 -1
- package/starter/forest/render/props/pack.ts +4 -2
- package/starter/forest/render/props/prepared.ts +2 -1
- package/starter/forest/render/props/propProgress.ts +1 -11
- package/starter/forest/render/props/propWorld.ts +48 -662
- package/starter/forest/render/props/props.ts +64 -248
- package/starter/forest/render/sky/sky.ts +2 -1
- package/starter/forest/render/worldNames.ts +6 -2
- package/starter/forest/scene.ts +34 -25
- package/starter/forest/sceneProps.ts +2 -2
- package/starter/forest/sceneState.ts +5 -4
- package/starter/forest/surface.json +2 -2
- package/starter/forest/worlds.ts +3 -2
- package/starter/tundra/README.md +35 -16
- package/starter/tundra/bake.mjs +14 -1
- package/starter/tundra/biome.data.json +6 -6
- package/starter/tundra/playerRig.ts +89 -0
- package/starter/tundra/recipe.json +111 -110
- package/starter/tundra/render/bakedFile.ts +17 -0
- package/starter/tundra/render/benchmark.ts +79 -0
- package/starter/tundra/render/biomeData.ts +14 -0
- package/starter/tundra/render/biomes.ts +714 -0
- package/starter/tundra/render/ground/cover.ts +565 -0
- package/starter/tundra/render/ground/horizon.ts +219 -0
- package/starter/tundra/render/ground/palette.d.ts +1 -0
- package/starter/tundra/render/ground/palette.js +11 -0
- package/starter/tundra/render/ground/scatter.ts +104 -0
- package/starter/tundra/render/ground/terrain.ts +1874 -0
- package/starter/tundra/render/kitAssets.ts +28 -0
- package/starter/tundra/render/loading.ts +354 -0
- package/starter/tundra/render/lookKnobs.ts +37 -0
- package/starter/tundra/render/orbitCamera.ts +155 -0
- package/starter/tundra/render/props/farForest.ts +96 -0
- package/starter/tundra/render/props/pack.ts +1088 -0
- package/starter/tundra/render/props/prepared.ts +225 -0
- package/starter/tundra/render/props/propMaterials.ts +1247 -0
- package/starter/tundra/render/props/propProgress.ts +2 -0
- package/starter/tundra/render/props/propWorld.ts +105 -0
- package/starter/tundra/render/props/props.ts +624 -0
- package/starter/tundra/render/props/spruce.ts +440 -0
- package/starter/tundra/render/sky/atmosphere.ts +329 -0
- package/starter/tundra/render/sky/sky.ts +382 -0
- package/starter/tundra/render/texturePreparation.ts +135 -0
- package/starter/tundra/render/water/river.ts +1132 -0
- package/starter/tundra/render/worldNames.ts +6 -0
- package/starter/tundra/scene.ts +715 -0
- package/starter/tundra/sceneProps.ts +559 -0
- package/starter/tundra/sceneState.ts +33 -0
- package/starter/tundra/world/terrainAssets.ts +159 -0
- package/starter/tundra/world/walkBand.ts +47 -0
- package/starter/tundra/world/water.ts +74 -0
- package/starter/tundra/worlds.ts +13 -0
- package/starter-assets.json +4 -4
- package/starter-credits.json +613 -10
- package/starter/forest/diagnostics.ts +0 -1020
- package/starter/forest/render/sky/skyVisibility.ts +0 -121
- package/starter/forest/spawnGate.ts +0 -183
- package/starter/forest/viewStatistics.ts +0 -47
- package/starter/forest/world/streamedGround.ts +0 -139
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import type { IAssetLoader } from "@threenative/core";
|
|
2
|
+
import { BufferAttribute, type BufferGeometry, type Group, type Mesh, type Object3D } from "three";
|
|
3
|
+
import { PREPARED } from "../../world/terrainAssets.js";
|
|
4
|
+
// The prepared props of the Temperate starter, loaded as models: the CC0 rocks and firs that ship
|
|
5
|
+
// with the terrain package, and the licensed Fab Scots pine this example prepared for itself.
|
|
6
|
+
//
|
|
7
|
+
// What this file is: the bridge between a GLB somebody else authored and the starter's prop
|
|
8
|
+
// contract. A placement resolves to a variant, a variant resolves to parts, and a part is a
|
|
9
|
+
// geometry plus the role that decides its material. This file turns the prepared files into
|
|
10
|
+
// exactly that, and nothing else:
|
|
11
|
+
//
|
|
12
|
+
// - the tree's trunk and branches are one role (`bark`) with the model's own unwrap, because the
|
|
13
|
+
// game's bark map is sampled in UV and a trunk it cannot sample is a grey pole;
|
|
14
|
+
// - the crown is one role (`pine`) cut against the prepared atlas;
|
|
15
|
+
// - the far band is one role (`impostor`) cut against a card baked from the *uncut* crown, which
|
|
16
|
+
// is the only version of this tree that survives being a few hundred pixels tall;
|
|
17
|
+
// - the rocks carry no UVs at all, because the starter's rock surface is triplanar and a
|
|
18
|
+
// photoscanned unwrap on a boulder is stripes;
|
|
19
|
+
// - every part carries the `sway` attribute the wind reads, as a share of the prop's own height,
|
|
20
|
+
// so the wind in `propMaterials.ts` moves a prepared tree exactly as it moves a procedural one.
|
|
21
|
+
//
|
|
22
|
+
// A file that is not there is not an error, and that is load-bearing rather than defensive. The
|
|
23
|
+
// licensed pine is prepared by `scripts/prep/prep-fab-pines.py` into this example's gitignored
|
|
24
|
+
// `local-assets/prepared/`, so the folder does not exist on CI, on a fresh clone, or in a review;
|
|
25
|
+
// the starter ships a procedural spruce and a procedural boulder for exactly this reason. A game
|
|
26
|
+
// with no prepared art still has a forest. An unregistered asset id still fails by name, in
|
|
27
|
+
// `props.ts`, because that is a mistake rather than a missing optional file.
|
|
28
|
+
import type { IPropPart, PropRole } from "./props.js";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Which role a prepared material name draws with.
|
|
32
|
+
*
|
|
33
|
+
* Three crowns, three atlases, so the names are read apart rather than lumped: Poly Haven's twig
|
|
34
|
+
* atlas is `needles`, the Fab pine's own leaf atlas is `pine`, and the far card is `impostor`. The
|
|
35
|
+
* pine's crown keeps the upstream material name because that atlas *is* that material's, and the
|
|
36
|
+
* upstream word for needles is `Leaves` — matched on both spellings, because `leaf` is not a
|
|
37
|
+
* substring of `leaves` and a crown that misses its role falls through to `stone` and comes out as
|
|
38
|
+
* a pine drawn in mossy rock.
|
|
39
|
+
*/
|
|
40
|
+
function roleFor(name: string): PropRole {
|
|
41
|
+
const lowered = name.toLowerCase();
|
|
42
|
+
if (lowered.includes("impostor")) return "impostor";
|
|
43
|
+
if (lowered.includes("bark") || lowered.includes("wood")) return "bark";
|
|
44
|
+
if (lowered.includes("twig")) return "needles";
|
|
45
|
+
// Both spellings, and deliberately: `leaf` is not a substring of `leaves`, and a
|
|
46
|
+
// crown that misses its role falls through to `stone` and comes out as a pine
|
|
47
|
+
// drawn in mossy rock. That is not a hypothetical — it is what two runs of this
|
|
48
|
+
// change measured before the condition was spelled out.
|
|
49
|
+
if (
|
|
50
|
+
lowered.includes("leaves") ||
|
|
51
|
+
lowered.includes("leaf") ||
|
|
52
|
+
lowered.includes("needl") ||
|
|
53
|
+
lowered.includes("crown")
|
|
54
|
+
)
|
|
55
|
+
return "pine";
|
|
56
|
+
return "stone";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Bake one loaded mesh's world transform into a geometry the starter can instance.
|
|
61
|
+
*
|
|
62
|
+
* `InstancedBatch` places a geometry, not an object, so the node transform the file was
|
|
63
|
+
* authored with has to be in the vertices. `clone().applyMatrix4` is the whole of it, and it
|
|
64
|
+
* is also why the origin of every prepared file is its own base: the game's grounding, not
|
|
65
|
+
* the file, decides where it meets the ground.
|
|
66
|
+
*/
|
|
67
|
+
function flatten(object: Object3D): BufferGeometry | undefined {
|
|
68
|
+
object.updateWorldMatrix(true, true);
|
|
69
|
+
const mesh = object as Mesh;
|
|
70
|
+
const source = mesh.geometry;
|
|
71
|
+
if (!source || source.index === null) return undefined;
|
|
72
|
+
const geometry = source.clone();
|
|
73
|
+
geometry.applyMatrix4(object.matrixWorld);
|
|
74
|
+
return geometry;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The wind weight, baked per vertex as a share of the prop's own height.
|
|
79
|
+
*
|
|
80
|
+
* The same envelope `spruce.ts` writes by hand, and for the same reason: an instanced tree's
|
|
81
|
+
* vertices are shared by every copy of it, so the weight has to be a share of the variant's
|
|
82
|
+
* own height rather than a world Y, or every tree in the world would bend by the same amount.
|
|
83
|
+
*/
|
|
84
|
+
function addSway(geometry: BufferGeometry, height: number): void {
|
|
85
|
+
const position = geometry.getAttribute("position");
|
|
86
|
+
const sway = new Float32Array(position.count);
|
|
87
|
+
for (let index = 0; index < position.count; index += 1) {
|
|
88
|
+
const share = Math.min(1, Math.max(0, position.getY(index) / Math.max(height, 1e-6)));
|
|
89
|
+
sway[index] = share * share;
|
|
90
|
+
}
|
|
91
|
+
geometry.setAttribute("sway", new BufferAttribute(sway, 1));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Both prepared-tree defects, measured on the geometries the starter will actually draw.
|
|
96
|
+
*
|
|
97
|
+
* The flattened mesh is the only place either is visible: `GroundSnap` reads the trunk a
|
|
98
|
+
* level's own geometry puts lowest, and the prep wrote a node translation per level that is
|
|
99
|
+
* exactly as easy to get wrong as a base offset. Neither is a diff you can read.
|
|
100
|
+
*/
|
|
101
|
+
function measureLevels(parts: ReadonlyMap<string, IPropPart[]>): {
|
|
102
|
+
lodBaseSpread: number;
|
|
103
|
+
levelsWithoutSolid: number;
|
|
104
|
+
} {
|
|
105
|
+
let lodBaseSpread = 0;
|
|
106
|
+
let levelsWithoutSolid = 0;
|
|
107
|
+
for (const chosen of parts.values()) {
|
|
108
|
+
const base = new Map<number, number>();
|
|
109
|
+
const solid = new Set<number>();
|
|
110
|
+
for (const part of chosen) {
|
|
111
|
+
const level = part.level ?? 0;
|
|
112
|
+
part.geometry.computeBoundingBox();
|
|
113
|
+
const min = part.geometry.boundingBox?.min.y ?? 0;
|
|
114
|
+
base.set(level, Math.min(base.get(level) ?? Number.POSITIVE_INFINITY, min));
|
|
115
|
+
if (part.role === "bark" || part.role === "stone") solid.add(level);
|
|
116
|
+
}
|
|
117
|
+
// The far card is one quad whose trunk is baked into its own pixels, so a level made only
|
|
118
|
+
// of impostor parts is not a headless tree.
|
|
119
|
+
const cards = new Set(
|
|
120
|
+
chosen.filter((part) => part.role === "impostor").map((part) => part.level ?? 0),
|
|
121
|
+
);
|
|
122
|
+
for (const [level, min] of base) {
|
|
123
|
+
lodBaseSpread = Math.max(lodBaseSpread, min - Math.min(...base.values()));
|
|
124
|
+
if (!solid.has(level) && !cards.has(level)) levelsWithoutSolid += 1;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return { lodBaseSpread, levelsWithoutSolid };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface IPreparedProps {
|
|
131
|
+
/** Parts keyed the same way `buildPropVariants` keys them: `asset:variant`. */
|
|
132
|
+
readonly parts: Map<string, IPropPart[]>;
|
|
133
|
+
/**
|
|
134
|
+
* The widest gap, in metres, between any two detail levels' own base Y in one variant.
|
|
135
|
+
*
|
|
136
|
+
* A prop's grounding puts the *lowest point of its trunk* on the terrain and every
|
|
137
|
+
* level is drawn with that one matrix, so a level authored a little off the ground is
|
|
138
|
+
* a tree whose crown floats above it while its neighbours stand correctly — and it is
|
|
139
|
+
* invisible in a diff and obvious in a capture. The gate is five centimetres, which is
|
|
140
|
+
* the width of the shadow a trunk casts at the near band and nothing more.
|
|
141
|
+
*/
|
|
142
|
+
readonly lodBaseSpread: number;
|
|
143
|
+
/**
|
|
144
|
+
* How many detail levels carry no solid part at all.
|
|
145
|
+
*
|
|
146
|
+
* A level with only cutout geometry is a crown with nothing holding it up, which is
|
|
147
|
+
* the same failure seen from the other side. The far cross-card is exempt: its trunk
|
|
148
|
+
* is baked into its own card, so there is no second mesh to carry one.
|
|
149
|
+
*/
|
|
150
|
+
readonly levelsWithoutSolid: number;
|
|
151
|
+
readonly dispose: () => void;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Load every prepared file, and hand back the parts that arrived.
|
|
156
|
+
*
|
|
157
|
+
* Loads in parallel and fails soft per file: a missing or broken prepared model leaves that
|
|
158
|
+
* variant to the procedural one, which is the difference between a starter with no forest and
|
|
159
|
+
* a starter whose forest is not the one the owner picked.
|
|
160
|
+
*/
|
|
161
|
+
export async function loadPreparedProps(assets?: IAssetLoader): Promise<IPreparedProps> {
|
|
162
|
+
const parts = new Map<string, IPropPart[]>();
|
|
163
|
+
const geometries: BufferGeometry[] = [];
|
|
164
|
+
if (assets === undefined)
|
|
165
|
+
return { parts, dispose: () => undefined, lodBaseSpread: 0, levelsWithoutSolid: 0 };
|
|
166
|
+
|
|
167
|
+
const loaded = await Promise.all(
|
|
168
|
+
PREPARED.map(async (file) => {
|
|
169
|
+
try {
|
|
170
|
+
return { file, gltf: await assets.model<{ scene?: Group }>(file.path) };
|
|
171
|
+
} catch {
|
|
172
|
+
return { file, gltf: undefined };
|
|
173
|
+
}
|
|
174
|
+
}),
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
for (const { file, gltf } of loaded) {
|
|
178
|
+
const root = gltf?.scene;
|
|
179
|
+
if (root === undefined) continue;
|
|
180
|
+
const meshes: Mesh[] = [];
|
|
181
|
+
root.traverse((object) => {
|
|
182
|
+
const mesh = object as Mesh;
|
|
183
|
+
if (mesh.isMesh) meshes.push(mesh);
|
|
184
|
+
});
|
|
185
|
+
// One height for the whole file, so the trunk and the crown of one tree agree on what a
|
|
186
|
+
// full sway weight is; each level of a variant measures the same way, so a tree does not
|
|
187
|
+
// change its wind envelope when the camera steps it down a level.
|
|
188
|
+
let height = 0;
|
|
189
|
+
for (const mesh of meshes) {
|
|
190
|
+
mesh.geometry.computeBoundingBox();
|
|
191
|
+
const box = mesh.geometry.boundingBox;
|
|
192
|
+
if (box) height = Math.max(height, box.max.y - box.min.y);
|
|
193
|
+
}
|
|
194
|
+
const key = `${file.asset}:${file.variant}`;
|
|
195
|
+
const entry = parts.get(key) ?? [];
|
|
196
|
+
for (const mesh of meshes) {
|
|
197
|
+
const material = mesh.material;
|
|
198
|
+
const name = Array.isArray(material)
|
|
199
|
+
? (material[0]?.name ?? "")
|
|
200
|
+
: ((material?.name ?? "") as string);
|
|
201
|
+
const geometry = flatten(mesh);
|
|
202
|
+
if (geometry === undefined) continue;
|
|
203
|
+
addSway(geometry, height);
|
|
204
|
+
geometries.push(geometry);
|
|
205
|
+
entry.push({ geometry, role: roleFor(name), level: file.level, variant: file.variant });
|
|
206
|
+
}
|
|
207
|
+
if (entry.length > 0) parts.set(key, entry);
|
|
208
|
+
// The loaded scene is only a carrier. Its own geometries are copied by `flatten`, and the
|
|
209
|
+
// loader caches by path and hands the same scene to the next caller, so nothing here may
|
|
210
|
+
// dispose anything the file brought with it.
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const measured = measureLevels(parts);
|
|
214
|
+
|
|
215
|
+
return {
|
|
216
|
+
parts,
|
|
217
|
+
lodBaseSpread: measured.lodBaseSpread,
|
|
218
|
+
levelsWithoutSolid: measured.levelsWithoutSolid,
|
|
219
|
+
dispose: () => {
|
|
220
|
+
for (const geometry of geometries) geometry.dispose();
|
|
221
|
+
geometries.length = 0;
|
|
222
|
+
parts.clear();
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
}
|