@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,630 @@
|
|
|
1
|
+
import { type IAssetLoader, createRandom } from "@threenative/core";
|
|
2
|
+
import { buildStaticColliders } from "@threenative/physics";
|
|
3
|
+
import {
|
|
4
|
+
type BufferGeometry,
|
|
5
|
+
Euler,
|
|
6
|
+
Group,
|
|
7
|
+
InstancedMesh,
|
|
8
|
+
Matrix4,
|
|
9
|
+
type Mesh,
|
|
10
|
+
type Object3D,
|
|
11
|
+
Quaternion,
|
|
12
|
+
RepeatWrapping,
|
|
13
|
+
type Texture,
|
|
14
|
+
Vector3,
|
|
15
|
+
} from "three";
|
|
16
|
+
import {
|
|
17
|
+
abs,
|
|
18
|
+
dot,
|
|
19
|
+
mix,
|
|
20
|
+
normalWorld,
|
|
21
|
+
normalize,
|
|
22
|
+
positionWorld,
|
|
23
|
+
texture,
|
|
24
|
+
transformNormalToView,
|
|
25
|
+
vec3,
|
|
26
|
+
} from "three/tsl";
|
|
27
|
+
import { MeshStandardNodeMaterial, type Node } from "three/webgpu";
|
|
28
|
+
import type { TerrainCtx } from "../../sceneState.js";
|
|
29
|
+
import { BIOMES, measureBiome } from "../biomes.js";
|
|
30
|
+
import { loadSkyLight, skyIrradiance } from "./pack.js";
|
|
31
|
+
import { type IPropGround, createRockGround } from "./propMaterials.js";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The desert's rock: Outcrop sandstone (`bake-rocks.mjs`, `desert_rocks/`), every form of it.
|
|
35
|
+
* Cliff walls run along the contours of the steep slopes, their backs and ends in the hill;
|
|
36
|
+
* fractured outcrops stand on the gentler ones with their talus below; fins and hoodoos stand in
|
|
37
|
+
* gardens away from the spawn, with one arch; boulders, slabs, cobbles and pebbles lie on the dunes.
|
|
38
|
+
* Placement is the game's: change a row to change what stands where.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/** [file, x, z, height in metres, yaw in degrees]: a landmark, its base sunk into the sand. */
|
|
42
|
+
const LANDMARKS: readonly (readonly [string, number, number, number, number])[] = [
|
|
43
|
+
// One distant accent: the surveyed horizon holds the real mesas, and a 30 m butte baked whole
|
|
44
|
+
// drew a smooth slab in the walker's path.
|
|
45
|
+
["spire", 40, -170, 20, 30],
|
|
46
|
+
];
|
|
47
|
+
/** A rock row: GLB, and the range of its longest side in metres. */
|
|
48
|
+
type Row = readonly [string, number, number];
|
|
49
|
+
/** Fallen blocks: under a landmark, round an outcrop and at the foot of a wall. */
|
|
50
|
+
const BLOCKS: readonly Row[] = [
|
|
51
|
+
["boulder-a", 0.8, 3],
|
|
52
|
+
["boulder-b", 0.7, 2.4],
|
|
53
|
+
["boulder-c", 0.8, 3],
|
|
54
|
+
["boulder-d", 1, 3.4],
|
|
55
|
+
["block-a", 1, 3.4],
|
|
56
|
+
["slab-b", 1.2, 4],
|
|
57
|
+
["slab-c", 1, 3],
|
|
58
|
+
];
|
|
59
|
+
/** Talus and the loose stone on the dunes; the cobbles repeat so most stones are small. */
|
|
60
|
+
const LOOSE: readonly Row[] = [
|
|
61
|
+
["boulder-a", 0.4, 1.5],
|
|
62
|
+
["boulder-b", 0.4, 1.3],
|
|
63
|
+
["boulder-c", 0.4, 1.6],
|
|
64
|
+
["boulder-d", 0.5, 1.8],
|
|
65
|
+
["block-a", 0.5, 1.8],
|
|
66
|
+
["slab-b", 0.6, 2.2],
|
|
67
|
+
["slab-c", 0.5, 1.7],
|
|
68
|
+
["cobble", 0.18, 0.5],
|
|
69
|
+
["cobble", 0.18, 0.5],
|
|
70
|
+
["cobble", 0.2, 0.6],
|
|
71
|
+
["cobble", 0.25, 0.7],
|
|
72
|
+
];
|
|
73
|
+
const COBBLE: Row = ["cobble", 0.18, 0.6];
|
|
74
|
+
const PEBBLES: readonly Row[] = [
|
|
75
|
+
["pebble", 0.05, 0.2],
|
|
76
|
+
["pebble-b", 0.06, 0.24],
|
|
77
|
+
];
|
|
78
|
+
/** The flagstones a wall's cap bed breaks into; they lie on its rim. */
|
|
79
|
+
const CAPS: readonly Row[] = [
|
|
80
|
+
["slab-b", 1.4, 3.6],
|
|
81
|
+
["slab-c", 1.2, 2.8],
|
|
82
|
+
["block-a", 1.2, 3],
|
|
83
|
+
];
|
|
84
|
+
/** The jointed core of an outcrop. */
|
|
85
|
+
const CORES: readonly Row[] = [
|
|
86
|
+
["crag-a", 2.6, 5.5],
|
|
87
|
+
["crag-b", 3, 6.5],
|
|
88
|
+
];
|
|
89
|
+
/** What a wall leaves standing: fins and hoodoos, by their height. */
|
|
90
|
+
const STANDING: readonly Row[] = [
|
|
91
|
+
["fin-a", 4, 9],
|
|
92
|
+
["fin-b", 3, 6.5],
|
|
93
|
+
["hoodoo-a", 3.5, 8],
|
|
94
|
+
["hoodoo-b", 2.5, 5.5],
|
|
95
|
+
];
|
|
96
|
+
/**
|
|
97
|
+
* A cliff wall: the modules of one Outcrop run (`<file>-<n>`), which join only in this order, and
|
|
98
|
+
* the range of its scale. Each module is `bake-rocks.mjs`'s size times the scale.
|
|
99
|
+
*/
|
|
100
|
+
const WALLS: readonly (readonly [string, number, number, number])[] = [
|
|
101
|
+
["wall-a", 4, 0.8, 1.2],
|
|
102
|
+
["wall-b", 3, 0.85, 1.35],
|
|
103
|
+
];
|
|
104
|
+
/**
|
|
105
|
+
* Where walls stand: the slope band in degrees, the grid step in metres, the share of sites kept,
|
|
106
|
+
* and how near two walls at one height may stand. The ends of a wall turn `turn` radians into the
|
|
107
|
+
* hill so the cut face of its last module is under the sand.
|
|
108
|
+
*/
|
|
109
|
+
const WALL = {
|
|
110
|
+
slope: [15, 52],
|
|
111
|
+
step: 16,
|
|
112
|
+
share: 0.8,
|
|
113
|
+
half: 250,
|
|
114
|
+
apart: 26,
|
|
115
|
+
turn: 0.5,
|
|
116
|
+
face: 0.9,
|
|
117
|
+
} as const;
|
|
118
|
+
/** Outcrops: the same for the gentler slopes. */
|
|
119
|
+
const OUTCROP = { slope: [5, 38], step: 27, share: 0.4, half: 250 } as const;
|
|
120
|
+
const SPAWN: readonly [number, number] = [180, 65];
|
|
121
|
+
/** Gardens of fins and hoodoos: how many, the ring round the spawn, and the steepest ground. */
|
|
122
|
+
const GARDENS = { count: 8, ring: [105, 240], slope: 13, radius: 9 } as const;
|
|
123
|
+
/** The arch: its width in metres and the ring round the spawn it stands in. */
|
|
124
|
+
const ARCH = { width: 19, ring: [95, 150], slope: 7 } as const;
|
|
125
|
+
/** Stone clusters on the dunes: how many, how far from the spawn, and the steepest ground kept. */
|
|
126
|
+
const CLUSTERS = { count: 90, reach: 170, slope: 14 } as const;
|
|
127
|
+
/** Single pebbles strewn near the spawn, thinning outward. */
|
|
128
|
+
const STREWN = { count: 2600, reach: 85 } as const;
|
|
129
|
+
/** A rock this near the spawn draws its full level, a farther one its `-far` level. */
|
|
130
|
+
const NEAR = 190;
|
|
131
|
+
/** Rocks draw in square cells this wide, so the ones behind the camera are culled as a group. */
|
|
132
|
+
const CELL = 128;
|
|
133
|
+
/** World height under which a rock can be walked into: above the highest ground of the square. */
|
|
134
|
+
const CEILING = 2000;
|
|
135
|
+
/** Stones under this longest side get no collider: the capsule steps over them. */
|
|
136
|
+
const SOLID_FROM = 0.6;
|
|
137
|
+
/** The ground's own sky-light weight (`render/ground/terrain.ts` GROUND_SKY_LIGHT). */
|
|
138
|
+
const SKY_LIGHT = 0.6;
|
|
139
|
+
/** The bakes are lighter than the sand they stand on; this takes them under it. */
|
|
140
|
+
const ROCK_TINT: readonly [number, number, number] = [0.62, 0.62, 0.7];
|
|
141
|
+
/** The mean linear vertex colour of the bakes, for the sky term (the engine has no colour node). */
|
|
142
|
+
const ROCK_MEAN: readonly [number, number, number] = [0.22, 0.085, 0.05];
|
|
143
|
+
/**
|
|
144
|
+
* The surface a bake's vertices cannot hold: Poly Haven's bedded cliff (CC0), its colour as grain
|
|
145
|
+
* and its normal map as relief, projected on the three world axes so the beds lie level on every
|
|
146
|
+
* face. Metres per tile, the scan's mean luminance, and how much of each the rock takes.
|
|
147
|
+
*/
|
|
148
|
+
const DETAIL = {
|
|
149
|
+
map: "cliff_side/cliff_side_diff_1k.jpg",
|
|
150
|
+
normal: "cliff_side/cliff_side_nor_gl_1k.jpg",
|
|
151
|
+
tile: 3.2,
|
|
152
|
+
mean: 0.15,
|
|
153
|
+
share: 0.75,
|
|
154
|
+
relief: 1.1,
|
|
155
|
+
} as const;
|
|
156
|
+
/** Each rock mesh is named this plus its GLB, so the collider filter reads the GLB back. */
|
|
157
|
+
const ROCK_NAME = "desert-rock:";
|
|
158
|
+
/** The bakes with no `-far` level: the landmarks, and the stones too small to need one. */
|
|
159
|
+
const ONE_LEVEL = new Set(["butte", "mesa", "spire", "cobble", "pebble", "pebble-b"]);
|
|
160
|
+
|
|
161
|
+
interface IRock {
|
|
162
|
+
readonly geometry: BufferGeometry;
|
|
163
|
+
/** Metres along x (a wall module's width), y and z (its depth, face to +z). */
|
|
164
|
+
readonly size: Vector3;
|
|
165
|
+
readonly longest: number;
|
|
166
|
+
/** Metres from the origin to the highest point: a wall module's rim. */
|
|
167
|
+
readonly top: number;
|
|
168
|
+
}
|
|
169
|
+
const REACH = 3;
|
|
170
|
+
type Ground = (x: number, z: number) => number;
|
|
171
|
+
type Point = readonly [number, number];
|
|
172
|
+
|
|
173
|
+
async function loadRock(assets: IAssetLoader, file: string): Promise<IRock> {
|
|
174
|
+
const gltf = await assets.model<{ scene: Object3D }>(`desert_rocks/${file}.glb`);
|
|
175
|
+
let geometry: BufferGeometry | undefined;
|
|
176
|
+
gltf.scene.traverse((object) => {
|
|
177
|
+
if ((object as Mesh).isMesh) geometry ??= (object as Mesh).geometry;
|
|
178
|
+
});
|
|
179
|
+
if (!geometry) throw new Error(`TN_DESERT_ROCK: desert_rocks/${file}.glb holds no mesh`);
|
|
180
|
+
geometry.computeBoundingBox();
|
|
181
|
+
const box = geometry.boundingBox;
|
|
182
|
+
if (!box) throw new Error(`TN_DESERT_ROCK: desert_rocks/${file}.glb has no bounds`);
|
|
183
|
+
const size = box.getSize(new Vector3());
|
|
184
|
+
return { geometry, size, longest: Math.max(size.x, size.y, size.z), top: box.max.y };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Outcrop's beds, cracks and drifted sand are vertex colours; this adds the scanned surface. */
|
|
188
|
+
function rockMaterial(
|
|
189
|
+
grain: Texture | undefined,
|
|
190
|
+
relief: Texture | undefined,
|
|
191
|
+
sky: Awaited<ReturnType<typeof loadSkyLight>>,
|
|
192
|
+
): MeshStandardNodeMaterial {
|
|
193
|
+
const material = new MeshStandardNodeMaterial({ vertexColors: true, roughness: 0.94 });
|
|
194
|
+
const axis = abs(normalWorld).pow(4);
|
|
195
|
+
const share = axis.div(axis.x.add(axis.y).add(axis.z));
|
|
196
|
+
const planes = [positionWorld.zy, positionWorld.xz, positionWorld.xy].map((plane) =>
|
|
197
|
+
plane.div(DETAIL.tile),
|
|
198
|
+
) as [Node<"vec2">, Node<"vec2">, Node<"vec2">];
|
|
199
|
+
let tint: Node<"vec3"> = vec3(...ROCK_TINT);
|
|
200
|
+
if (grain) {
|
|
201
|
+
const tap = (plane: Node<"vec2">) =>
|
|
202
|
+
dot(texture(grain, plane).rgb, vec3(0.2126, 0.7152, 0.0722));
|
|
203
|
+
const light = share.x
|
|
204
|
+
.mul(tap(planes[0]))
|
|
205
|
+
.add(share.y.mul(tap(planes[1])))
|
|
206
|
+
.add(share.z.mul(tap(planes[2])));
|
|
207
|
+
tint = tint.mul(mix(1, light.div(DETAIL.mean).clamp(0.35, 1.9), DETAIL.share));
|
|
208
|
+
}
|
|
209
|
+
material.colorNode = tint;
|
|
210
|
+
if (relief) {
|
|
211
|
+
// Each plane's tangent normal leans the surface normal along that plane's two axes.
|
|
212
|
+
const lean = (plane: Node<"vec2">) =>
|
|
213
|
+
texture(relief, plane).xy.mul(2).sub(1).mul(DETAIL.relief);
|
|
214
|
+
const [x, y, z] = planes.map(lean) as [Node<"vec2">, Node<"vec2">, Node<"vec2">];
|
|
215
|
+
material.normalNode = transformNormalToView(
|
|
216
|
+
normalize(
|
|
217
|
+
normalWorld
|
|
218
|
+
.add(vec3(0, x.y, x.x).mul(share.x))
|
|
219
|
+
.add(vec3(y.x, 0, y.y).mul(share.y))
|
|
220
|
+
.add(vec3(z.x, z.y, 0).mul(share.z)),
|
|
221
|
+
),
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
if (sky) {
|
|
225
|
+
// Shaded sandstone reads the sky and the sand's bounce, as the ground does, not black. The
|
|
226
|
+
// owned engine has no vertex-colour node, so the term takes the bakes' mean rock colour.
|
|
227
|
+
const { up, down } = skyIrradiance(sky);
|
|
228
|
+
material.emissiveNode = mix(vec3(...down), vec3(...up), normalWorld.y.mul(0.5).add(0.5))
|
|
229
|
+
.mul(vec3(...ROCK_MEAN))
|
|
230
|
+
.mul(tint)
|
|
231
|
+
.mul(SKY_LIGHT);
|
|
232
|
+
}
|
|
233
|
+
return material;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Metres of rise per metre along x and z, and the slope in degrees, over `REACH` metres each way:
|
|
238
|
+
* the dunes carry ripples a metre or two long, and a slope read across one of them is not the
|
|
239
|
+
* slope a wall five metres deep stands in.
|
|
240
|
+
*/
|
|
241
|
+
function gradientOf(heightAt: Ground, x: number, z: number) {
|
|
242
|
+
const dx = (heightAt(x + REACH, z) - heightAt(x - REACH, z)) / (2 * REACH);
|
|
243
|
+
const dz = (heightAt(x, z + REACH) - heightAt(x, z - REACH)) / (2 * REACH);
|
|
244
|
+
return { dx, dz, degrees: (Math.atan(Math.hypot(dx, dz)) * 180) / Math.PI };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The joints of a wall of `count` modules, each `width` metres, along the contour through
|
|
249
|
+
* (`x`, `z`): `count + 1` points, module n between point n and point n + 1 with its face downhill.
|
|
250
|
+
* Undefined where the ground leaves the slope band, which a wall cannot stand in.
|
|
251
|
+
*/
|
|
252
|
+
function traceWall(
|
|
253
|
+
heightAt: Ground,
|
|
254
|
+
x: number,
|
|
255
|
+
z: number,
|
|
256
|
+
width: number,
|
|
257
|
+
count: number,
|
|
258
|
+
): Point[] | undefined {
|
|
259
|
+
const level = heightAt(x, z);
|
|
260
|
+
const joints: Point[] = [[x, z]];
|
|
261
|
+
let [cx, cz] = [x, z];
|
|
262
|
+
for (let n = 0; n < count; n++) {
|
|
263
|
+
const here = gradientOf(heightAt, cx, cz);
|
|
264
|
+
if (here.degrees < WALL.slope[0] * 0.7 || here.degrees > WALL.slope[1]) return undefined;
|
|
265
|
+
const steep = Math.hypot(here.dx, here.dz);
|
|
266
|
+
// A module's +x with its face (+z) down the slope.
|
|
267
|
+
let nx = cx + (-here.dz / steep) * width;
|
|
268
|
+
let nz = cz + (here.dx / steep) * width;
|
|
269
|
+
// Back onto the contour, then exactly one width from the last joint so the modules meet.
|
|
270
|
+
for (let pass = 0; pass < 2; pass++) {
|
|
271
|
+
const there = gradientOf(heightAt, nx, nz);
|
|
272
|
+
const square = there.dx ** 2 + there.dz ** 2;
|
|
273
|
+
if (square < 1e-4) return undefined;
|
|
274
|
+
const lift = Math.max(-width / 2, Math.min(width / 2, level - heightAt(nx, nz)));
|
|
275
|
+
nx += (lift * there.dx) / square;
|
|
276
|
+
nz += (lift * there.dz) / square;
|
|
277
|
+
}
|
|
278
|
+
const span = Math.hypot(nx - cx, nz - cz);
|
|
279
|
+
if (span < width * 0.4) return undefined;
|
|
280
|
+
cx += ((nx - cx) / span) * width;
|
|
281
|
+
cz += ((nz - cz) / span) * width;
|
|
282
|
+
joints.push([cx, cz]);
|
|
283
|
+
}
|
|
284
|
+
return joints;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Turns the wall's end joint about its neighbour, `WALL.turn` radians up the slope. */
|
|
288
|
+
function turnIn(heightAt: Ground, end: Point, pivot: Point): Point {
|
|
289
|
+
const { dx, dz } = gradientOf(heightAt, end[0], end[1]);
|
|
290
|
+
const steep = Math.hypot(dx, dz) || 1;
|
|
291
|
+
const span = Math.hypot(end[0] - pivot[0], end[1] - pivot[1]);
|
|
292
|
+
const along = [(end[0] - pivot[0]) / span, (end[1] - pivot[1]) / span] as const;
|
|
293
|
+
const x = along[0] * Math.cos(WALL.turn) + (dx / steep) * Math.sin(WALL.turn);
|
|
294
|
+
const z = along[1] * Math.cos(WALL.turn) + (dz / steep) * Math.sin(WALL.turn);
|
|
295
|
+
const length = Math.hypot(x, z);
|
|
296
|
+
return [pivot[0] + (x / length) * span, pivot[1] + (z / length) * span];
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Stands the rock on the ground `heightAt` describes, makes the large stones solid on the player's
|
|
301
|
+
* collision layer, and returns their disposal.
|
|
302
|
+
*/
|
|
303
|
+
export async function addFormations(
|
|
304
|
+
ctx: TerrainCtx,
|
|
305
|
+
assets: IAssetLoader,
|
|
306
|
+
heightAt: Ground,
|
|
307
|
+
/** The baked heights the ground is drawn from: the rock's foot blends into the sand over them. */
|
|
308
|
+
ground: IPropGround,
|
|
309
|
+
): Promise<() => void> {
|
|
310
|
+
const names = new Set([
|
|
311
|
+
...[...LANDMARKS, ...BLOCKS, ...LOOSE, ...PEBBLES, ...CAPS, ...CORES, ...STANDING].map(
|
|
312
|
+
([file]) => file,
|
|
313
|
+
),
|
|
314
|
+
...WALLS.flatMap(([file, count]) => Array.from({ length: count }, (_, n) => `${file}-${n}`)),
|
|
315
|
+
"arch",
|
|
316
|
+
]);
|
|
317
|
+
const files = [...names].flatMap((file) =>
|
|
318
|
+
ONE_LEVEL.has(file) ? [file] : [file, `${file}-far`],
|
|
319
|
+
);
|
|
320
|
+
const map = (path: string, data: boolean) =>
|
|
321
|
+
// No scan is not fatal: the rock keeps its vertex colours.
|
|
322
|
+
assets
|
|
323
|
+
.texture(path, { anisotropy: 8, data, wrap: RepeatWrapping })
|
|
324
|
+
.catch(() => undefined);
|
|
325
|
+
const [rocks, sky, grain, relief, blend] = await Promise.all([
|
|
326
|
+
Promise.all(files.map(async (file) => [file, await loadRock(assets, file)] as const)),
|
|
327
|
+
loadSkyLight(assets),
|
|
328
|
+
map(DETAIL.map, false),
|
|
329
|
+
map(DETAIL.normal, true),
|
|
330
|
+
createRockGround(assets, ground, measureBiome(BIOMES.desert, ground.heights)),
|
|
331
|
+
]);
|
|
332
|
+
const rock = new Map(rocks);
|
|
333
|
+
const material = rockMaterial(grain, relief, sky);
|
|
334
|
+
// Sand banks against the rock's foot and dusts its lowest half metre, as the pack's stones do.
|
|
335
|
+
blend?.apply(material, ROCK_MEAN);
|
|
336
|
+
|
|
337
|
+
const next = createRandom(0x0de5e47);
|
|
338
|
+
const pick = <T>(rows: readonly T[]): T => rows[Math.floor(next() * rows.length)] as T;
|
|
339
|
+
const between = (low: number, high: number, power = 1) => low + (high - low) * next() ** power;
|
|
340
|
+
const slopeAt = (x: number, z: number) => gradientOf(heightAt, x, z).degrees;
|
|
341
|
+
const matrices = new Map<string, Matrix4[]>();
|
|
342
|
+
const solid = new Set<string>();
|
|
343
|
+
/** Puts one rock at `y`, `scale` times its baked size, at its full or far level by distance. */
|
|
344
|
+
const place = (name: string, x: number, y: number, z: number, scale: number, turn: Euler) => {
|
|
345
|
+
const far = !ONE_LEVEL.has(name) && Math.hypot(x - SPAWN[0], z - SPAWN[1]) > NEAR;
|
|
346
|
+
const key = `${far ? `${name}-far` : name}@${Math.floor(x / CELL)},${Math.floor(z / CELL)}`;
|
|
347
|
+
const list = matrices.get(key) ?? [];
|
|
348
|
+
list.push(
|
|
349
|
+
new Matrix4().compose(
|
|
350
|
+
new Vector3(x, y, z),
|
|
351
|
+
new Quaternion().setFromEuler(turn),
|
|
352
|
+
new Vector3(scale, scale, scale),
|
|
353
|
+
),
|
|
354
|
+
);
|
|
355
|
+
matrices.set(key, list);
|
|
356
|
+
if (scale * (rock.get(name) as IRock).longest >= SOLID_FROM) solid.add(key);
|
|
357
|
+
};
|
|
358
|
+
/** The lowest ground under a stone `reach` metres across. */
|
|
359
|
+
const floorAt = (x: number, z: number, reach: number) =>
|
|
360
|
+
Math.min(
|
|
361
|
+
heightAt(x, z),
|
|
362
|
+
heightAt(x + reach, z),
|
|
363
|
+
heightAt(x - reach, z),
|
|
364
|
+
heightAt(x, z + reach),
|
|
365
|
+
heightAt(x, z - reach),
|
|
366
|
+
);
|
|
367
|
+
/** A loose stone: on the lowest ground under it, sunk a share of its height, tipped a little. */
|
|
368
|
+
const drop = ([name, small, large]: Row, x: number, z: number, sink: number, power = 2) => {
|
|
369
|
+
const shape = rock.get(name) as IRock;
|
|
370
|
+
const scale = between(small, large, power) / shape.longest;
|
|
371
|
+
const tip = new Euler((next() - 0.5) * 0.5, next() * Math.PI * 2, (next() - 0.5) * 0.5);
|
|
372
|
+
const y = floorAt(x, z, (shape.longest * scale) / 2) - shape.size.y * scale * sink;
|
|
373
|
+
place(name, x, y, z, scale, tip);
|
|
374
|
+
};
|
|
375
|
+
/** A standing stone `height` metres tall: upright, its foot a little under the sand. */
|
|
376
|
+
const stand = (name: string, x: number, z: number, height: number, sink = 0.07) => {
|
|
377
|
+
const shape = rock.get(name) as IRock;
|
|
378
|
+
const scale = height / shape.size.y;
|
|
379
|
+
const lean = new Euler((next() - 0.5) * 0.1, next() * Math.PI * 2, (next() - 0.5) * 0.1);
|
|
380
|
+
place(name, x, floorAt(x, z, (shape.size.x * scale) / 2) - height * sink, z, scale, lean);
|
|
381
|
+
return (Math.max(shape.size.x, shape.size.z) * scale) / 2;
|
|
382
|
+
};
|
|
383
|
+
/** `count` stones from `rows` scattered `near` to `far` metres round a point. */
|
|
384
|
+
const strew = (
|
|
385
|
+
rows: readonly Row[],
|
|
386
|
+
count: number,
|
|
387
|
+
x: number,
|
|
388
|
+
z: number,
|
|
389
|
+
near: number,
|
|
390
|
+
far: number,
|
|
391
|
+
sink = 0.3,
|
|
392
|
+
) => {
|
|
393
|
+
for (let n = 0; n < count; n++) {
|
|
394
|
+
const turn = next() * Math.PI * 2;
|
|
395
|
+
const away = between(near, far, 1.6);
|
|
396
|
+
drop(pick(rows), x + Math.cos(turn) * away, z + Math.sin(turn) * away, sink);
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
for (const [name, x, z, height, yaw] of LANDMARKS) {
|
|
401
|
+
const shape = rock.get(name) as IRock;
|
|
402
|
+
const scale = height / shape.size.y;
|
|
403
|
+
place(
|
|
404
|
+
name,
|
|
405
|
+
x,
|
|
406
|
+
heightAt(x, z) - height * 0.08,
|
|
407
|
+
z,
|
|
408
|
+
scale,
|
|
409
|
+
new Euler(0, (yaw * Math.PI) / 180, 0),
|
|
410
|
+
);
|
|
411
|
+
// Most blocks lie close under the face they fell from; a few rolled out onto the sand.
|
|
412
|
+
const radius = (shape.longest * scale) / 2;
|
|
413
|
+
strew(BLOCKS, Math.round(height), x, z, radius * 0.75, radius * 2.3, 0.25);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// A cliff wall follows a contour of a steep slope. Its modules stand in the order they were
|
|
417
|
+
// baked in, so each joins the next; its two ends turn into the hill; its top is level with the
|
|
418
|
+
// ground behind it, so the flat back Outcrop gives a module is under the sand.
|
|
419
|
+
const walls: (readonly [number, number, number])[] = [];
|
|
420
|
+
const buildWall = (x: number, z: number) => {
|
|
421
|
+
const [file, count, small, large] = pick(WALLS);
|
|
422
|
+
const shape = rock.get(`${file}-0`) as IRock;
|
|
423
|
+
const scale = between(small, large);
|
|
424
|
+
// The rim: a module's flat top stands a little under its highest chip.
|
|
425
|
+
const [width, height, depth] = [
|
|
426
|
+
shape.size.x * scale,
|
|
427
|
+
shape.top * scale * 0.94,
|
|
428
|
+
shape.size.z * scale,
|
|
429
|
+
];
|
|
430
|
+
const level = heightAt(x, z);
|
|
431
|
+
if (
|
|
432
|
+
walls.some(
|
|
433
|
+
([wx, wz, wy]) => Math.hypot(wx - x, wz - z) < WALL.apart && Math.abs(wy - level) < height,
|
|
434
|
+
)
|
|
435
|
+
)
|
|
436
|
+
return;
|
|
437
|
+
const joints = traceWall(heightAt, x, z, width, count);
|
|
438
|
+
if (!joints) return;
|
|
439
|
+
joints[0] = turnIn(heightAt, joints[0] as Point, joints[1] as Point);
|
|
440
|
+
joints[count] = turnIn(heightAt, joints[count] as Point, joints[count - 1] as Point);
|
|
441
|
+
// Module n: its centre, the way its face looks, and the ground behind and before it.
|
|
442
|
+
const modules = Array.from({ length: count }, (_, n) => {
|
|
443
|
+
const [a, b] = [joints[n] as Point, joints[n + 1] as Point];
|
|
444
|
+
const along: Point = [(b[0] - a[0]) / width, (b[1] - a[1]) / width];
|
|
445
|
+
const face: Point = [-along[1], along[0]];
|
|
446
|
+
// The joints run along the middle of the face, which is 0.4 of the depth before the centre.
|
|
447
|
+
const centre: Point = [
|
|
448
|
+
(a[0] + b[0]) / 2 - face[0] * depth * 0.4,
|
|
449
|
+
(a[1] + b[1]) / 2 - face[1] * depth * 0.4,
|
|
450
|
+
];
|
|
451
|
+
const row = (out: number) =>
|
|
452
|
+
[-0.35, 0, 0.35].map((side) =>
|
|
453
|
+
heightAt(
|
|
454
|
+
centre[0] + face[0] * out + along[0] * width * side,
|
|
455
|
+
centre[1] + face[1] * out + along[1] * width * side,
|
|
456
|
+
),
|
|
457
|
+
);
|
|
458
|
+
return { along, centre, face, behind: row(-depth * 0.5), before: row(depth * 0.5) };
|
|
459
|
+
});
|
|
460
|
+
// The straight modules set the height; the two turned ends go under the slope by design.
|
|
461
|
+
const straight = modules.slice(1, -1);
|
|
462
|
+
const foot = Math.min(...modules.flatMap((module) => module.before));
|
|
463
|
+
const top = Math.min(...straight.flatMap((module) => module.behind), foot - 0.4 + height);
|
|
464
|
+
const shown = top - Math.max(...straight.flatMap((module) => module.before));
|
|
465
|
+
if (shown < WALL.face) return;
|
|
466
|
+
walls.push([x, z, level]);
|
|
467
|
+
for (const [n, { along, centre, face }] of modules.entries()) {
|
|
468
|
+
const yaw = Math.atan2(-along[1], along[0]);
|
|
469
|
+
place(`${file}-${n}`, centre[0], top - height, centre[1], scale, new Euler(0, yaw, 0));
|
|
470
|
+
const edge = n > 0 && n < count - 1;
|
|
471
|
+
// Flagstones on the rim, where the cap bed broke; talus at the foot of the face.
|
|
472
|
+
for (let k = edge ? Math.round(between(1, 3)) : 0; k > 0; k--) {
|
|
473
|
+
const [name, small, large] = pick(CAPS);
|
|
474
|
+
const cap = rock.get(name) as IRock;
|
|
475
|
+
const size = between(small, large) / cap.longest;
|
|
476
|
+
const side = (next() - 0.5) * width * 0.9;
|
|
477
|
+
const out = between(-0.25, 0.3) * depth;
|
|
478
|
+
place(
|
|
479
|
+
name,
|
|
480
|
+
centre[0] + along[0] * side + face[0] * out,
|
|
481
|
+
top - cap.size.y * size * 0.35,
|
|
482
|
+
centre[1] + along[1] * side + face[1] * out,
|
|
483
|
+
size,
|
|
484
|
+
new Euler((next() - 0.5) * 0.3, next() * Math.PI * 2, (next() - 0.5) * 0.3),
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
for (let k = Math.round(between(2, 6)); k > 0; k--) {
|
|
488
|
+
const fall = depth * 0.6 + next() ** 2 * 12;
|
|
489
|
+
const side = (next() - 0.5) * width * 1.1;
|
|
490
|
+
drop(
|
|
491
|
+
pick(next() < 0.35 ? BLOCKS : LOOSE),
|
|
492
|
+
centre[0] + face[0] * fall + along[0] * side,
|
|
493
|
+
centre[1] + face[1] * fall + along[1] * side,
|
|
494
|
+
0.3,
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
for (let gz = -WALL.half; gz < WALL.half; gz += WALL.step)
|
|
500
|
+
for (let gx = -WALL.half; gx < WALL.half; gx += WALL.step) {
|
|
501
|
+
const x = gx + (next() - 0.5) * WALL.step;
|
|
502
|
+
const z = gz + (next() - 0.5) * WALL.step;
|
|
503
|
+
const degrees = slopeAt(x, z);
|
|
504
|
+
if (degrees >= WALL.slope[0] && degrees <= WALL.slope[1] && next() < WALL.share)
|
|
505
|
+
buildWall(x, z);
|
|
506
|
+
}
|
|
507
|
+
// An outcrop on a gentler slope: a jointed crag in the sand, the blocks that split from it
|
|
508
|
+
// against its sides, a slab leaning on it, and its talus down the slope.
|
|
509
|
+
for (let gz = -OUTCROP.half; gz < OUTCROP.half; gz += OUTCROP.step)
|
|
510
|
+
for (let gx = -OUTCROP.half; gx < OUTCROP.half; gx += OUTCROP.step) {
|
|
511
|
+
const x = gx + (next() - 0.5) * OUTCROP.step;
|
|
512
|
+
const z = gz + (next() - 0.5) * OUTCROP.step;
|
|
513
|
+
const { dx, dz, degrees } = gradientOf(heightAt, x, z);
|
|
514
|
+
if (degrees < OUTCROP.slope[0] || degrees > OUTCROP.slope[1] || next() > OUTCROP.share)
|
|
515
|
+
continue;
|
|
516
|
+
if (walls.some(([wx, wz]) => Math.hypot(wx - x, wz - z) < 18)) continue;
|
|
517
|
+
const [core, small, large] = pick(CORES);
|
|
518
|
+
const shape = rock.get(core) as IRock;
|
|
519
|
+
const size = between(small, large);
|
|
520
|
+
const radius = (Math.max(shape.size.x, shape.size.z) / shape.longest) * size * 0.5;
|
|
521
|
+
drop([core, size, size], x, z, 0.3, 1);
|
|
522
|
+
for (let k = Math.round(between(3, 7)); k > 0; k--) {
|
|
523
|
+
const turn = next() * Math.PI * 2;
|
|
524
|
+
const away = radius * between(0.75, 1.25);
|
|
525
|
+
drop(pick(BLOCKS), x + Math.cos(turn) * away, z + Math.sin(turn) * away, 0.38, 1.3);
|
|
526
|
+
}
|
|
527
|
+
const steep = Math.hypot(dx, dz) || 1;
|
|
528
|
+
for (let k = Math.round(between(5, 13)); k > 0; k--) {
|
|
529
|
+
const fall = radius + next() ** 1.6 * 13;
|
|
530
|
+
const side = (next() - 0.5) * (radius * 2 + fall * 0.8);
|
|
531
|
+
drop(
|
|
532
|
+
pick(LOOSE),
|
|
533
|
+
x - (dx / steep) * fall + (dz / steep) * side,
|
|
534
|
+
z - (dz / steep) * fall - (dx / steep) * side,
|
|
535
|
+
0.3,
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
strew(PEBBLES, Math.round(between(10, 22)), x, z, radius * 0.6, radius + 6, 0.35);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// Fins and hoodoos stand in gardens, a few close together, away from the spawn: the reference
|
|
542
|
+
// keeps its foreground to dune, scrub and the rocky hillside.
|
|
543
|
+
const ring = ([near, far]: readonly [number, number], slope: number): Point | undefined => {
|
|
544
|
+
for (let attempt = 0; attempt < 40; attempt++) {
|
|
545
|
+
const angle = next() * Math.PI * 2;
|
|
546
|
+
const out = between(near, far);
|
|
547
|
+
const point: Point = [SPAWN[0] + Math.cos(angle) * out, SPAWN[1] + Math.sin(angle) * out];
|
|
548
|
+
if (slopeAt(point[0], point[1]) <= slope) return point;
|
|
549
|
+
}
|
|
550
|
+
return undefined;
|
|
551
|
+
};
|
|
552
|
+
for (let k = 0; k < GARDENS.count; k++) {
|
|
553
|
+
const site = ring(GARDENS.ring, GARDENS.slope);
|
|
554
|
+
if (!site) continue;
|
|
555
|
+
for (let n = Math.round(between(3, 7)); n > 0; n--) {
|
|
556
|
+
const turn = next() * Math.PI * 2;
|
|
557
|
+
const away = between(0, GARDENS.radius);
|
|
558
|
+
const [x, z] = [site[0] + Math.cos(turn) * away, site[1] + Math.sin(turn) * away];
|
|
559
|
+
const [name, low, high] = pick(STANDING);
|
|
560
|
+
const radius = stand(name, x, z, between(low, high));
|
|
561
|
+
strew(LOOSE, Math.round(between(2, 5)), x, z, radius * 0.8, radius + 3);
|
|
562
|
+
}
|
|
563
|
+
strew(BLOCKS, Math.round(between(4, 9)), site[0], site[1], 2, GARDENS.radius + 5, 0.3);
|
|
564
|
+
}
|
|
565
|
+
const span = ring(ARCH.ring, ARCH.slope);
|
|
566
|
+
if (span) {
|
|
567
|
+
const shape = rock.get("arch") as IRock;
|
|
568
|
+
const scale = ARCH.width / shape.size.x;
|
|
569
|
+
const y = floorAt(span[0], span[1], ARCH.width / 2) - shape.size.y * scale * 0.06;
|
|
570
|
+
place("arch", span[0], y, span[1], scale, new Euler(0, next() * Math.PI, 0));
|
|
571
|
+
strew(BLOCKS, 12, span[0], span[1], 2, ARCH.width * 0.8, 0.3);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// On the dunes stone lies in clusters: one boulder or slab, cobbles beside it, pebbles around.
|
|
575
|
+
for (let k = 0; k < CLUSTERS.count; k++) {
|
|
576
|
+
const angle = next() * Math.PI * 2;
|
|
577
|
+
const out = 6 + CLUSTERS.reach * Math.sqrt(next());
|
|
578
|
+
const x = SPAWN[0] + Math.cos(angle) * out;
|
|
579
|
+
const z = SPAWN[1] + Math.sin(angle) * out;
|
|
580
|
+
if (slopeAt(x, z) > CLUSTERS.slope) continue;
|
|
581
|
+
drop(pick(LOOSE), x, z, 0.3);
|
|
582
|
+
strew([COBBLE], Math.round(between(2, 6)), x, z, 0.4, 2.6);
|
|
583
|
+
strew(PEBBLES, Math.round(between(6, 16)), x, z, 0.3, 4, 0.35);
|
|
584
|
+
}
|
|
585
|
+
for (let k = 0; k < STREWN.count; k++) {
|
|
586
|
+
const angle = next() * Math.PI * 2;
|
|
587
|
+
const out = 2 + STREWN.reach * next() ** 0.75;
|
|
588
|
+
drop(
|
|
589
|
+
pick(PEBBLES),
|
|
590
|
+
SPAWN[0] + Math.cos(angle) * out,
|
|
591
|
+
SPAWN[1] + Math.sin(angle) * out,
|
|
592
|
+
0.35,
|
|
593
|
+
1.5,
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// The rocks live in one group, so the colliders scan exactly them.
|
|
598
|
+
const root = new Group();
|
|
599
|
+
root.name = "desert-rocks";
|
|
600
|
+
const meshes: InstancedMesh[] = [];
|
|
601
|
+
for (const [key, list] of matrices) {
|
|
602
|
+
const file = key.slice(0, key.indexOf("@"));
|
|
603
|
+
const mesh = new InstancedMesh((rock.get(file) as IRock).geometry, material, list.length);
|
|
604
|
+
mesh.name = `${ROCK_NAME}${key}`;
|
|
605
|
+
for (const [at, matrix] of list.entries()) mesh.setMatrixAt(at, matrix);
|
|
606
|
+
mesh.instanceMatrix.needsUpdate = true;
|
|
607
|
+
mesh.computeBoundingSphere();
|
|
608
|
+
mesh.castShadow = true;
|
|
609
|
+
mesh.receiveShadow = true;
|
|
610
|
+
root.add(mesh);
|
|
611
|
+
meshes.push(mesh);
|
|
612
|
+
}
|
|
613
|
+
ctx.add(root);
|
|
614
|
+
root.updateMatrixWorld(true);
|
|
615
|
+
const bodies = buildStaticColliders(ctx, root, {
|
|
616
|
+
predicate: (object) =>
|
|
617
|
+
object.name.startsWith(ROCK_NAME) && solid.has(object.name.slice(ROCK_NAME.length)),
|
|
618
|
+
collisionLayer: 4,
|
|
619
|
+
// The helper's 4.5 m default assumes a level on y = 0; these dunes stand tens of metres up,
|
|
620
|
+
// and a rock above the ceiling got no body at all.
|
|
621
|
+
reachableCeiling: CEILING,
|
|
622
|
+
});
|
|
623
|
+
return () => {
|
|
624
|
+
for (const body of bodies) body.dispose();
|
|
625
|
+
root.removeFromParent();
|
|
626
|
+
for (const mesh of meshes) mesh.dispose();
|
|
627
|
+
material.dispose();
|
|
628
|
+
blend?.dispose();
|
|
629
|
+
};
|
|
630
|
+
}
|