@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.
- 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,1195 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type IAssetLoader,
|
|
3
|
+
baseGeometryOf,
|
|
4
|
+
lodChainOf,
|
|
5
|
+
normaliseToMetres,
|
|
6
|
+
thinCards,
|
|
7
|
+
} from "@threenative/core";
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
type IMeshSection,
|
|
11
|
+
bakedNoise,
|
|
12
|
+
instanceRandom,
|
|
13
|
+
meshSections,
|
|
14
|
+
repairZeroNormals,
|
|
15
|
+
sectionGeometry,
|
|
16
|
+
} from "@threenative/core/world";
|
|
17
|
+
import {
|
|
18
|
+
Box3,
|
|
19
|
+
BufferAttribute,
|
|
20
|
+
type BufferGeometry,
|
|
21
|
+
DataTexture,
|
|
22
|
+
DataUtils,
|
|
23
|
+
DoubleSide,
|
|
24
|
+
EquirectangularReflectionMapping,
|
|
25
|
+
FrontSide,
|
|
26
|
+
type Group,
|
|
27
|
+
LinearFilter,
|
|
28
|
+
type Material,
|
|
29
|
+
type Mesh,
|
|
30
|
+
type MeshStandardMaterial,
|
|
31
|
+
RedFormat,
|
|
32
|
+
RepeatWrapping,
|
|
33
|
+
type Texture,
|
|
34
|
+
Vector3,
|
|
35
|
+
} from "three";
|
|
36
|
+
import { HDRLoader } from "three/addons/loaders/HDRLoader.js";
|
|
37
|
+
import {
|
|
38
|
+
attribute,
|
|
39
|
+
cameraProjectionMatrix,
|
|
40
|
+
cameraViewMatrix,
|
|
41
|
+
dFdx,
|
|
42
|
+
dFdy,
|
|
43
|
+
dot,
|
|
44
|
+
float,
|
|
45
|
+
length,
|
|
46
|
+
log2,
|
|
47
|
+
max,
|
|
48
|
+
mix,
|
|
49
|
+
normalMap,
|
|
50
|
+
normalViewGeometry,
|
|
51
|
+
normalWorldGeometry,
|
|
52
|
+
positionGeometry,
|
|
53
|
+
positionLocal,
|
|
54
|
+
positionView,
|
|
55
|
+
positionWorld,
|
|
56
|
+
sin,
|
|
57
|
+
smoothstep,
|
|
58
|
+
texture,
|
|
59
|
+
time,
|
|
60
|
+
uniform,
|
|
61
|
+
uv,
|
|
62
|
+
vec2,
|
|
63
|
+
vec3,
|
|
64
|
+
vec4,
|
|
65
|
+
} from "three/tsl";
|
|
66
|
+
import { MeshPhysicalNodeMaterial, type Node } from "three/webgpu";
|
|
67
|
+
import { BROADLEAF_TREES, GROUND_MAPS, ROCKFACE_MAPS } from "../../world/terrainAssets.js";
|
|
68
|
+
import { biomeSpec } from "../biomeData.js";
|
|
69
|
+
import {
|
|
70
|
+
BIOMES,
|
|
71
|
+
type IBiome,
|
|
72
|
+
type IWorldBiome,
|
|
73
|
+
type WorldName,
|
|
74
|
+
alpineRockAlbedo,
|
|
75
|
+
alpineRockColor,
|
|
76
|
+
alpineSnowCover,
|
|
77
|
+
biomeWeights,
|
|
78
|
+
desertRockColor,
|
|
79
|
+
measureBiome,
|
|
80
|
+
} from "../biomes.js";
|
|
81
|
+
import { LOOK } from "../lookKnobs.js";
|
|
82
|
+
import { WORLD } from "../worldNames.js";
|
|
83
|
+
import {
|
|
84
|
+
type IPropGround,
|
|
85
|
+
createRockGround,
|
|
86
|
+
lightNeedles,
|
|
87
|
+
skyEnvironment,
|
|
88
|
+
} from "./propMaterials.js";
|
|
89
|
+
import type { IPropPart, PropRole } from "./props.js";
|
|
90
|
+
|
|
91
|
+
export interface IPackSpecies {
|
|
92
|
+
readonly asset: string;
|
|
93
|
+
readonly variant: number;
|
|
94
|
+
readonly path: string;
|
|
95
|
+
readonly metres: number;
|
|
96
|
+
readonly level?: number;
|
|
97
|
+
}
|
|
98
|
+
/** Independent same-asset probes; omitted fields keep the current forest appearance. */
|
|
99
|
+
export interface ICanopyComparison {
|
|
100
|
+
/** Forest crown normals; other worlds already retain their imported geometry normals. */
|
|
101
|
+
readonly normals?: "radial" | "authored";
|
|
102
|
+
readonly specular?: "disabled" | "standard";
|
|
103
|
+
}
|
|
104
|
+
/** Fail closed so a labelled comparison cannot silently capture the current appearance. */
|
|
105
|
+
export function canopyComparison(
|
|
106
|
+
normals: string | null,
|
|
107
|
+
specular: string | null,
|
|
108
|
+
): ICanopyComparison {
|
|
109
|
+
if (normals !== null && normals !== "radial" && normals !== "authored")
|
|
110
|
+
throw new Error(`Unsupported canopyNormals '${normals}'`);
|
|
111
|
+
if (specular !== null && specular !== "disabled" && specular !== "standard")
|
|
112
|
+
throw new Error(`Unsupported canopySpecular '${specular}'`);
|
|
113
|
+
return { normals: normals ?? "authored", specular: specular ?? "disabled" };
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The forest's canopy: Poly Haven's CC0 broadleaf scan (`BROADLEAF_TREES`), two silhouettes at five
|
|
117
|
+
* heights (metres, crown top), so a stand reads as a European broadleaf wood rather than a spruce plantation (owner,
|
|
118
|
+
* 2026-11-14: "replace this trees with scanned trees from poly haven"). Each variant is a near GLB
|
|
119
|
+
* and a mid GLB of leaf cards from `scripts/prep/prep-polyhaven-broadleaf.py`.
|
|
120
|
+
*/
|
|
121
|
+
const BROADLEAF_METRES = [12, 15, 14, 17, 19];
|
|
122
|
+
const species: IPackSpecies[] = [];
|
|
123
|
+
for (let i = 0; i < 5; i++) {
|
|
124
|
+
const tree = BROADLEAF_TREES[i % BROADLEAF_TREES.length];
|
|
125
|
+
const metres = BROADLEAF_METRES[i] ?? 15;
|
|
126
|
+
species.push({ asset: "spruce", variant: i, path: `${tree}-near.glb`, metres });
|
|
127
|
+
species.push({ asset: "spruce", variant: i, path: `${tree}-mid.glb`, metres, level: 1 });
|
|
128
|
+
}
|
|
129
|
+
for (const [asset, heights] of Object.entries({
|
|
130
|
+
sapling: [1.1, 2.1, 3.0, 1.6],
|
|
131
|
+
grass: [0.3, 0.55, 0.75, 0.45],
|
|
132
|
+
/** The meadow's dense base layer (`meadowCover`): Open World Demo field grass and a low mat. */
|
|
133
|
+
meadow: [0.42, 0.3],
|
|
134
|
+
scrub: [0.085, 0.065, 0.018],
|
|
135
|
+
poppy: [0.38, 0.5, 0.42, 0.4],
|
|
136
|
+
bloom: [0.35, 0.5, 0.4, 0.45],
|
|
137
|
+
fern: [0.7, 0.6, 0.85],
|
|
138
|
+
bush: [0.9, 0.55],
|
|
139
|
+
litter: [0.42, 0.55],
|
|
140
|
+
boulder: [2.1, 5.2, 2.5],
|
|
141
|
+
riverrock: [0.8],
|
|
142
|
+
scree: [5.8],
|
|
143
|
+
cliff: [18],
|
|
144
|
+
/** Fallen wood (`cover.understorey.deadfall`): its scans are `POLYHAVEN_FLOOR.deadfall`. */
|
|
145
|
+
deadfall: [3.1, 4.0, 1.6, 1.1],
|
|
146
|
+
mountain: [24, 24],
|
|
147
|
+
})) {
|
|
148
|
+
heights.forEach((metres, variant) =>
|
|
149
|
+
species.push({ asset, variant, metres, path: `${asset}/${variant}` }),
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
// The desert's plants and stones. Valley of the Gods is Colorado Plateau desert: Utah juniper, sage,
|
|
153
|
+
// blackbrush, Utah yucca, desert marigold and bunchgrass between sandstone buttes, with no saguaro,
|
|
154
|
+
// joshua tree or acacia. The plants are `@threenative/procedural-environment` bakes
|
|
155
|
+
// (`starter-assets/desert_flora/`): `olive` draws the gnarled grey juniper, `creosote` the sage,
|
|
156
|
+
// `thornbush` the blackbrush, `agave` the yucca. The stones are Outcrop sandstone bakes.
|
|
157
|
+
// Each row is [GLB, metres]; a second GLB is the far level. These rows replace the generator's own
|
|
158
|
+
// procedural parts for the same ids, so the ordered stream's placements keep their positions.
|
|
159
|
+
const DESERT_FLORA: Record<string, readonly (readonly [string, number, string?])[]> = {
|
|
160
|
+
// Juniper and saguaro share the tree slot, so cacti stand among the juniper on the flats.
|
|
161
|
+
sapling: [
|
|
162
|
+
["desert_flora/juniper-a.glb", 3.6, "desert_flora/juniper-a-far.glb"],
|
|
163
|
+
["desert_flora/saguaro-a.glb", 5.5, "desert_flora/saguaro-a-far.glb"],
|
|
164
|
+
["desert_flora/juniper-b.glb", 4.6, "desert_flora/juniper-b-far.glb"],
|
|
165
|
+
["desert_flora/saguaro-b.glb", 3.8, "desert_flora/saguaro-b-far.glb"],
|
|
166
|
+
],
|
|
167
|
+
// The ordered stream's cover is mostly `grass` and `scrub`, with a `bush` only every fiftieth
|
|
168
|
+
// cell, so the shrubs that cover the plateau ride `scrub` as well as `bush`.
|
|
169
|
+
grass: [
|
|
170
|
+
["desert_flora/bunchgrass-a.glb", 0.45],
|
|
171
|
+
["desert_flora/bunchgrass-b.glb", 0.6],
|
|
172
|
+
["desert_flora/drygrass.glb", 0.35],
|
|
173
|
+
["desert_flora/marigold.glb", 0.3],
|
|
174
|
+
],
|
|
175
|
+
scrub: [
|
|
176
|
+
["desert_flora/sage.glb", 0.9],
|
|
177
|
+
["desert_flora/blackbrush.glb", 0.8],
|
|
178
|
+
["desert_flora/yucca.glb", 0.55],
|
|
179
|
+
["desert_flora/creosote-b.glb", 1.1],
|
|
180
|
+
["desert_flora/thornbush-b.glb", 0.7],
|
|
181
|
+
["desert_flora/agave-b.glb", 0.6],
|
|
182
|
+
],
|
|
183
|
+
bush: [
|
|
184
|
+
["desert_flora/sage.glb", 1.3],
|
|
185
|
+
["desert_flora/blackbrush.glb", 1.1],
|
|
186
|
+
["desert_flora/creosote-b.glb", 1.6],
|
|
187
|
+
],
|
|
188
|
+
// Dead juniper wood and twigs on the sand (`render/ground/debris.ts`), by their longest side.
|
|
189
|
+
deadfall: [
|
|
190
|
+
["desert_flora/branch.glb", 1.6],
|
|
191
|
+
["desert_flora/branch-b.glb", 1.1],
|
|
192
|
+
["desert_flora/twig.glb", 0.35],
|
|
193
|
+
["desert_flora/branch-b.glb", 2.2],
|
|
194
|
+
],
|
|
195
|
+
// Stone, by its longest side: weathered red-brown sandstone from Outcrop (`bake-rocks.mjs`,
|
|
196
|
+
// `starter-assets/desert_rocks/`): chipped, jointed blocks darker than the sand they lie on. A
|
|
197
|
+
// second GLB is the half-density level past `LOD_BANDS.mid`.
|
|
198
|
+
boulder: [
|
|
199
|
+
["desert_rocks/boulder-a.glb", 1.5, "desert_rocks/boulder-a-far.glb"],
|
|
200
|
+
["desert_rocks/block-a.glb", 2.2, "desert_rocks/block-a-far.glb"],
|
|
201
|
+
["desert_rocks/boulder-b.glb", 1.1, "desert_rocks/boulder-b-far.glb"],
|
|
202
|
+
],
|
|
203
|
+
riverrock: [["desert_rocks/cobble.glb", 0.8]],
|
|
204
|
+
scree: [["desert_rocks/boulder-c.glb", 1.2, "desert_rocks/boulder-c-far.glb"]],
|
|
205
|
+
// Gravel and pebbles, 0.1-0.3 m across at their placed scale: a few hundred triangles each.
|
|
206
|
+
pebble: [
|
|
207
|
+
["desert_rocks/pebble.glb", 1],
|
|
208
|
+
["desert_rocks/pebble-b.glb", 0.7],
|
|
209
|
+
["desert_rocks/cobble.glb", 1.3],
|
|
210
|
+
],
|
|
211
|
+
// Fallen blocks: the bake's foot and rim generators place these at about their row size, so a
|
|
212
|
+
// 10 m row drew domes taller than the player's view; real blocks under a butte are 2-4 m (the
|
|
213
|
+
// debris layer scales them down further for talus).
|
|
214
|
+
volcanic: [
|
|
215
|
+
["desert_rocks/block-a.glb", 2.6, "desert_rocks/block-a-far.glb"],
|
|
216
|
+
["desert_rocks/boulder-c.glb", 2.2, "desert_rocks/boulder-c-far.glb"],
|
|
217
|
+
["desert_rocks/crag-b.glb", 2.2, "desert_rocks/crag-b-far.glb"],
|
|
218
|
+
["desert_rocks/boulder-a.glb", 2.4, "desert_rocks/boulder-a-far.glb"],
|
|
219
|
+
],
|
|
220
|
+
// The larger outcrops a slope reveals: two jointed crags standing out of the sand.
|
|
221
|
+
// The bake stands them at about four times their row, so these are 6 m crags, not 20 m domes.
|
|
222
|
+
reveal: [
|
|
223
|
+
["desert_rocks/crag-b.glb", 1.6, "desert_rocks/crag-b-far.glb"],
|
|
224
|
+
["desert_rocks/crag-a.glb", 1.4, "desert_rocks/crag-a-far.glb"],
|
|
225
|
+
],
|
|
226
|
+
// The bake's exposed-rock rows (a 2-20 m wall at row size 24 m) drew the temperate kit's boulder
|
|
227
|
+
// scan as smooth domes 20 m across. A desert outcrop is a jointed crag a few metres high.
|
|
228
|
+
mountain: [
|
|
229
|
+
["desert_rocks/crag-a.glb", 7, "desert_rocks/crag-a-far.glb"],
|
|
230
|
+
["desert_rocks/crag-b.glb", 6, "desert_rocks/crag-b-far.glb"],
|
|
231
|
+
],
|
|
232
|
+
};
|
|
233
|
+
for (const [asset, rows] of Object.entries(DESERT_FLORA))
|
|
234
|
+
rows.forEach(([path, metres, far], variant) => {
|
|
235
|
+
species.push({ asset, variant, path, metres });
|
|
236
|
+
if (far) species.push({ asset, variant, path: far, metres, level: 1 });
|
|
237
|
+
});
|
|
238
|
+
/**
|
|
239
|
+
* The forest floor's Poly Haven CC0 scans (`packages/terrain/starter-assets`), as [GLB, metres] per
|
|
240
|
+
* variant: dead trunks, a stump and an upturned root plate for the deadfall, dry branches for the
|
|
241
|
+
* litter, weeds and moss for the ground mounds, shrubs and a nettle where the spruce saplings stood,
|
|
242
|
+
* celandine, dandelion and periwinkle where the meadow flowers stood, and the kit's own fern
|
|
243
|
+
* (owner, 2026-11-14: "not enough ground/debris/trunks vegetation on the floor"). Metres are each scan's own size, near enough.
|
|
244
|
+
*/
|
|
245
|
+
const POLYHAVEN_FLOOR: Record<string, readonly (readonly [string, number])[]> = {
|
|
246
|
+
deadfall: [
|
|
247
|
+
["dead_tree_trunk/dead_tree_trunk.glb", 3.1],
|
|
248
|
+
// dead_tree_trunk_02 is not drawn: it was scanned propped on a branch, so it floats on flat ground.
|
|
249
|
+
["dead_tree_trunk/dead_tree_trunk.glb", 4.0],
|
|
250
|
+
["tree_stump_01/tree_stump_01.glb", 1.6],
|
|
251
|
+
// root_cluster_01 is not drawn: its 4.1 x 2.7 m root plate cut through trunks and lay on slopes as a slab.
|
|
252
|
+
["tree_stump_01/tree_stump_01.glb", 1.1],
|
|
253
|
+
],
|
|
254
|
+
litter: [
|
|
255
|
+
["dry_branches_medium_01/dry_branches_medium_01_a.glb", 0.34],
|
|
256
|
+
["dry_branches_medium_01/dry_branches_medium_01_b.glb", 0.14],
|
|
257
|
+
],
|
|
258
|
+
scrub: [
|
|
259
|
+
["weed_plant_02/weed_plant_02_a.glb", 0.07],
|
|
260
|
+
["weed_plant_02/weed_plant_02_b.glb", 0.07],
|
|
261
|
+
["moss_01/moss_01_tall_b.glb", 0.04],
|
|
262
|
+
],
|
|
263
|
+
bush: [
|
|
264
|
+
["shrub_02/shrub_02_d.glb", 1.2],
|
|
265
|
+
["shrub_04/shrub_04.glb", 0.3],
|
|
266
|
+
],
|
|
267
|
+
sapling: [
|
|
268
|
+
["shrub_02/shrub_02_a.glb", 1.7],
|
|
269
|
+
["shrub_02/shrub_02_b.glb", 1.5],
|
|
270
|
+
["shrub_02/shrub_02_c.glb", 1.3],
|
|
271
|
+
["nettle_plant/nettle_plant_tall_a.glb", 0.5],
|
|
272
|
+
],
|
|
273
|
+
poppy: [
|
|
274
|
+
["celandine_01/celandine_01_a.glb", 0.25],
|
|
275
|
+
["celandine_01/celandine_01_b.glb", 0.2],
|
|
276
|
+
["dandelion_01/dandelion_01_a.glb", 0.22],
|
|
277
|
+
["celandine_01/celandine_01_e.glb", 0.18],
|
|
278
|
+
],
|
|
279
|
+
bloom: [
|
|
280
|
+
["periwinkle_plant/periwinkle_plant_02.glb", 0.4],
|
|
281
|
+
["dandelion_01/dandelion_01_b.glb", 0.16],
|
|
282
|
+
["celandine_01/celandine_01_d.glb", 0.2],
|
|
283
|
+
["periwinkle_plant/periwinkle_plant_05.glb", 0.22],
|
|
284
|
+
],
|
|
285
|
+
// Stone: the kit's Poly Haven `boulder_01` (its authored LOD1, LOD2 where it is scaled past a
|
|
286
|
+
// boulder or seen small), by its longest side.
|
|
287
|
+
boulder: [
|
|
288
|
+
["boulder_01/boulder-near.glb", 2.1],
|
|
289
|
+
["boulder_01/boulder-mid.glb", 5.2],
|
|
290
|
+
["boulder_01/boulder-near.glb", 2.5],
|
|
291
|
+
],
|
|
292
|
+
riverrock: [["boulder_01/boulder-mid.glb", 0.8]],
|
|
293
|
+
scree: [["boulder_01/boulder-mid.glb", 5.8]],
|
|
294
|
+
cliff: [["boulder_01/boulder-near.glb", 18]],
|
|
295
|
+
mountain: [
|
|
296
|
+
["boulder_01/boulder-near.glb", 24],
|
|
297
|
+
["boulder_01/boulder-near.glb", 24],
|
|
298
|
+
],
|
|
299
|
+
fern: [
|
|
300
|
+
["fern_02/fern-a.glb", 0.55],
|
|
301
|
+
["fern_02/fern-b.glb", 0.5],
|
|
302
|
+
["fern_02/fern-a.glb", 0.75],
|
|
303
|
+
],
|
|
304
|
+
};
|
|
305
|
+
/**
|
|
306
|
+
* The grass: Poly Haven CC0 grass_medium_01/02 baked to crossed cards on one atlas
|
|
307
|
+
* (`scripts/prep/prep-polyhaven-grass.py`), as [GLB, metres tall] per variant at the licensed
|
|
308
|
+
* rows' heights: a field patch for the field grass, tufts and a reed for the Grass Library tufts, and
|
|
309
|
+
* a low clump for the mat.
|
|
310
|
+
*/
|
|
311
|
+
const POLYHAVEN_GRASS: Record<string, readonly (readonly [string, number])[]> = {
|
|
312
|
+
grass: [
|
|
313
|
+
["grass_medium/field.glb", 0.3],
|
|
314
|
+
["grass_medium/tuft.glb", 0.55],
|
|
315
|
+
["grass_medium/reed.glb", 0.75],
|
|
316
|
+
["grass_medium/tuft.glb", 0.45],
|
|
317
|
+
],
|
|
318
|
+
meadow: [
|
|
319
|
+
["grass_medium/field.glb", 0.42],
|
|
320
|
+
["grass_medium/mat.glb", 0.3],
|
|
321
|
+
],
|
|
322
|
+
};
|
|
323
|
+
/** The species drawn as stone: they stand on their lowest point and take a hull collider. */
|
|
324
|
+
export const STONE = new Set([
|
|
325
|
+
"boulder",
|
|
326
|
+
"riverrock",
|
|
327
|
+
"scree",
|
|
328
|
+
"cliff",
|
|
329
|
+
"mountain",
|
|
330
|
+
"volcanic",
|
|
331
|
+
"reveal",
|
|
332
|
+
"pebble",
|
|
333
|
+
]);
|
|
334
|
+
/** Meadow Flowers species: photographed petals kept, stems lifted, no foliage tint. */
|
|
335
|
+
const FLOWERS = new Set(["poppy", "bloom"]);
|
|
336
|
+
/** Needle-card triangles one far spruce draws past LOD_BANDS.far (its middle level draws 0.8-4.7k). */
|
|
337
|
+
const FAR_NEEDLE_TRIANGLES = 1500;
|
|
338
|
+
|
|
339
|
+
// Per tree, not per draw slot: WorldCells repacks instances as the player walks.
|
|
340
|
+
const phase = instanceRandom().mul(6.2831);
|
|
341
|
+
const gust = sin(time.mul(0.1).add(phase));
|
|
342
|
+
|
|
343
|
+
/** What one imported section is, before any appearance decision: its art and its niche. */
|
|
344
|
+
interface ISurfaceContext {
|
|
345
|
+
readonly source: MeshStandardMaterial;
|
|
346
|
+
readonly asset: string;
|
|
347
|
+
readonly world: WorldName;
|
|
348
|
+
readonly look: IBiome;
|
|
349
|
+
readonly biome?: IWorldBiome;
|
|
350
|
+
readonly stone: boolean;
|
|
351
|
+
readonly cutout: boolean;
|
|
352
|
+
readonly canopy: boolean;
|
|
353
|
+
readonly fieldGrass: boolean;
|
|
354
|
+
readonly otherBiome: boolean;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** Imported foliage draws as imported: its albedo, its normal map, its own cutoff, both faces. */
|
|
358
|
+
/**
|
|
359
|
+
* Imported normal maps whose green channel is DirectX (Unreal) rather than glTF's OpenGL, measured
|
|
360
|
+
* per material by a curl test on the staged maps (2026-10-08). Every other imported map is OpenGL.
|
|
361
|
+
*/
|
|
362
|
+
const DIRECTX_NORMALS =
|
|
363
|
+
/^(branch|MI_LargePlainsBoulder002|MI_Large_VolcanicRock_002|ScotsPine_01_.*)$/u;
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* GLTFLoader flips `normalScale.y` for a mesh without tangents, which none of the cooked meshes
|
|
367
|
+
* carry; a rebuilt material that copies only `normalMap` loses that flip and lights every relief
|
|
368
|
+
* from the mirrored side. Keep it, and undo it for the DirectX-authored maps.
|
|
369
|
+
*/
|
|
370
|
+
function copyNormalScale(material: MeshPhysicalNodeMaterial, source: MeshStandardMaterial): void {
|
|
371
|
+
material.normalScale.copy(source.normalScale);
|
|
372
|
+
if (DIRECTX_NORMALS.test(source.name)) material.normalScale.y *= -1;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function importedFoliage(
|
|
376
|
+
material: MeshPhysicalNodeMaterial,
|
|
377
|
+
source: MeshStandardMaterial,
|
|
378
|
+
map: Texture,
|
|
379
|
+
): void {
|
|
380
|
+
const sampled = texture(map, uv());
|
|
381
|
+
map.anisotropy = 8;
|
|
382
|
+
material.normalMap = source.normalMap;
|
|
383
|
+
copyNormalScale(material, source);
|
|
384
|
+
material.roughness = 0.92;
|
|
385
|
+
// The Kite field grass is authored dark for Unreal's exposure (Wildwood lifts such packs ~3x); drawn raw
|
|
386
|
+
// it read as a blue-black carpet.
|
|
387
|
+
material.colorNode = sampled.rgb.mul(source.name === "open-world-demo" ? 3 : 1);
|
|
388
|
+
material.alphaTest = source.alphaTest;
|
|
389
|
+
material.alphaTestNode = float(source.alphaTest);
|
|
390
|
+
material.opacityNode = sampled.a;
|
|
391
|
+
material.side = DoubleSide;
|
|
392
|
+
material.shadowSide = DoubleSide;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Moss, snow and the upward-face mask a stone section wears over its own albedo.
|
|
397
|
+
*
|
|
398
|
+
* Alpine keeps the heightfield's upward-face mask across mesh seams; tundra uses its ground rule.
|
|
399
|
+
*/
|
|
400
|
+
function stoneSurface(
|
|
401
|
+
material: MeshPhysicalNodeMaterial,
|
|
402
|
+
context: ISurfaceContext,
|
|
403
|
+
snowMap?: Texture,
|
|
404
|
+
): void {
|
|
405
|
+
const { source, look } = context;
|
|
406
|
+
if (look.stoneFrontFace) material.shadowSide = FrontSide;
|
|
407
|
+
const growth = smoothstep(-0.15, 0.3, bakedNoise(positionWorld.mul(2.1)));
|
|
408
|
+
const moss = normalWorldGeometry.y.max(0).mul(growth).mul(look.stoneMoss);
|
|
409
|
+
material.colorNode = mix(material.colorNode as Node<"vec3">, vec3(...look.stoneMossTint), moss);
|
|
410
|
+
if (!look.stoneSnow) return;
|
|
411
|
+
const biome = context.biome;
|
|
412
|
+
if (!biome) throw new RangeError("Stone snow requires measured terrain elevations");
|
|
413
|
+
const snow = look.crestRock
|
|
414
|
+
? smoothstep(0.12, 0.82, alpineSnowCover(biome))
|
|
415
|
+
: (biomeWeights(biome, float(1).sub(normalWorldGeometry.y), float(0), growth)
|
|
416
|
+
.snow as Node<"float">);
|
|
417
|
+
material.colorNode = mix(
|
|
418
|
+
material.colorNode as Node<"vec3">,
|
|
419
|
+
look.crestRock
|
|
420
|
+
? (snowMap ? texture(snowMap, positionWorld.xz.div(12)).rgb : vec3(0.82, 0.86, 0.9)).mul(
|
|
421
|
+
vec3(...biome.snowTint),
|
|
422
|
+
)
|
|
423
|
+
: vec3(...look.stoneSnowTint),
|
|
424
|
+
snow,
|
|
425
|
+
);
|
|
426
|
+
material.roughnessNode = mix(0.96, 0.82, snow);
|
|
427
|
+
if (source.normalMap)
|
|
428
|
+
material.normalNode = normalMap(texture(source.normalMap), vec2(mix(1, 0.2, snow)));
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** Needle pixels nearer the lens than this are cut, in metres. */
|
|
432
|
+
const CANOPY_NEAR = 1.2;
|
|
433
|
+
/** Bark is cut only at the lens: a limb sliced further out shows a hollow, sawn end. */
|
|
434
|
+
const BARK_NEAR = 0.4;
|
|
435
|
+
/** Radius of the sight line kept clear of tree pixels from the lens to the hero, in metres. */
|
|
436
|
+
const SIGHT_RADIUS = 1.2;
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* The hero the orbit camera aims at, in world space. `game.ts` writes `aim` each frame and sets
|
|
440
|
+
* `radius` to {@link SIGHT_RADIUS} while the player view is live, 0 for the fixed views.
|
|
441
|
+
*/
|
|
442
|
+
export const sightLine = { aim: uniform(new Vector3()), radius: uniform(0) };
|
|
443
|
+
/** The waterline rocks stand in the river bed. */
|
|
444
|
+
export const RIVER_BED = true;
|
|
445
|
+
export { SIGHT_RADIUS };
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* The orbit camera swings through crowns: a branch a metre from the lens fills the screen, and a
|
|
449
|
+
* stand between the lens and the hero hides the hero. Needles near the lens, and on the line from
|
|
450
|
+
* the lens to the hero, are eroded, as a third-person camera fades the occluders of its character
|
|
451
|
+
* (UE's CameraDepthFade into the opacity mask). The fade scales the needle alpha before the 4x MSAA
|
|
452
|
+
* alpha-to-coverage test, so cards thin from their tips inward over a metre: a hard cut drew a
|
|
453
|
+
* sharp circle, and slicing branches with it drew their hollow ends as rings round the hero.
|
|
454
|
+
* Bark takes only a short cut at the lens (a sliced limb reads hollow); branches stay whole.
|
|
455
|
+
* An orthographic view (P[3][3] = 1) is a shadow page or the impostor baker, so it keeps the tree.
|
|
456
|
+
*/
|
|
457
|
+
export function nearCameraFade(material: MeshPhysicalNodeMaterial, needles: boolean): void {
|
|
458
|
+
const ortho = cameraProjectionMatrix.mul(vec4(0, 0, 0, 1)).w.greaterThan(0.5);
|
|
459
|
+
if (!needles) {
|
|
460
|
+
material.maskNode = ortho.or(length(positionView).greaterThanEqual(BARK_NEAR));
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
const aim = cameraViewMatrix.mul(vec4(sightLine.aim, 1)).xyz;
|
|
464
|
+
const along = dot(positionView, aim).div(dot(aim, aim).max(1e-4));
|
|
465
|
+
const off = length(positionView.sub(aim.mul(along.clamp(0, 1))));
|
|
466
|
+
// 1 keeps the needle; radius 0 (a fixed view) keeps every needle off the line.
|
|
467
|
+
const sight = smoothstep(0.8, 1.7, off.div(sightLine.radius.max(1e-3))).max(
|
|
468
|
+
smoothstep(0.9, 1.05, along),
|
|
469
|
+
);
|
|
470
|
+
const lens = smoothstep(CANOPY_NEAR * 0.5, CANOPY_NEAR * 1.5, length(positionView));
|
|
471
|
+
const keep = sight.min(lens).max(ortho.select(float(1), float(0)));
|
|
472
|
+
material.opacityNode = ((material.opacityNode as Node<"float"> | null) ?? float(1)).mul(keep);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/** The coverage, interior depth and normal a cut-out section is drawn with. */
|
|
476
|
+
function cutoutSurface(
|
|
477
|
+
material: MeshPhysicalNodeMaterial,
|
|
478
|
+
context: ISurfaceContext,
|
|
479
|
+
sampled: Node<"vec4">,
|
|
480
|
+
): void {
|
|
481
|
+
const { source, asset, look, canopy, otherBiome } = context;
|
|
482
|
+
// Shadow overrides and VirtualShadowNode classify cutouts by this scalar, not the node.
|
|
483
|
+
material.alphaTest = source.alphaTest;
|
|
484
|
+
// The scene pass is 4x MSAA: coverage from alpha smooths card edges that a hard test speckles.
|
|
485
|
+
material.alphaToCoverage = true;
|
|
486
|
+
material.side = DoubleSide;
|
|
487
|
+
material.shadowSide = DoubleSide;
|
|
488
|
+
if (canopy) {
|
|
489
|
+
const inner = attribute<"float">("inner", "float");
|
|
490
|
+
const shape = look.canopyLinearInner ? inner : inner.pow(2);
|
|
491
|
+
material.colorNode = (material.colorNode as Node<"vec3">).mul(
|
|
492
|
+
mix(look.canopyInnerShade, 1, shape),
|
|
493
|
+
);
|
|
494
|
+
material.aoNode = mix(look.canopyAmbientOcclusion[0], look.canopyAmbientOcclusion[1], shape);
|
|
495
|
+
// The bent crown normal is the geometry's own (radial, from `addRadialCoverage`); the imported
|
|
496
|
+
// normal map perturbs that same geometry normal. Installing the bare geometry normal as
|
|
497
|
+
// `normalNode` replaced the normal node and discarded the map, so the crown shaded as one flat
|
|
498
|
+
// card. The custom path only fills in where the art carries no map — an imported surface sets
|
|
499
|
+
// `normalMap` and leaves the node to Three, which also applies the texture's own packing.
|
|
500
|
+
if (look.crownGeometryNormals && !source.normalMap) material.normalNode = normalViewGeometry;
|
|
501
|
+
} else if (FLOWERS.has(asset)) {
|
|
502
|
+
// Keep the photographed red petals; lift only the nearly black stems/seed pods.
|
|
503
|
+
const dark = smoothstep(0.045, 0.008, sampled.r.max(sampled.g).max(sampled.b));
|
|
504
|
+
material.colorNode = mix(
|
|
505
|
+
material.colorNode as Node<"vec3">,
|
|
506
|
+
vec3(0.028, 0.055, 0.009),
|
|
507
|
+
dark.mul(0.75),
|
|
508
|
+
);
|
|
509
|
+
material.emissiveNode = (material.colorNode as Node<"vec3">).mul(0.045);
|
|
510
|
+
} else if (source.normalMap && !look.mats) {
|
|
511
|
+
// Ground foliage keeps photographed relief around its bent, upward leaf normal.
|
|
512
|
+
const relief = texture(source.normalMap, uv()).xy.mul(2).sub(1).mul(0.45);
|
|
513
|
+
material.normalNode = vec3(relief.x, 1, relief.y)
|
|
514
|
+
.normalize()
|
|
515
|
+
.transformDirection(cameraViewMatrix);
|
|
516
|
+
}
|
|
517
|
+
const image = source.map?.image as { width?: number; height?: number } | undefined;
|
|
518
|
+
const size = vec2(image?.width ?? 2048, image?.height ?? 2048);
|
|
519
|
+
const mip = max(
|
|
520
|
+
log2(max(length(dFdx(uv()).mul(size)), length(dFdy(uv()).mul(size))).max(1)),
|
|
521
|
+
float(0),
|
|
522
|
+
);
|
|
523
|
+
// Low tundra mats must reject blurred photographic background in alpha mips.
|
|
524
|
+
material.alphaTestNode =
|
|
525
|
+
look.mats && !canopy
|
|
526
|
+
? float(0.5)
|
|
527
|
+
: float(look.authoredAlphaTest ? source.alphaTest : 0.42).div(float(1).add(mip.mul(0.25)));
|
|
528
|
+
material.opacityNode = sampled.a;
|
|
529
|
+
material.maskShadowNode = sampled.a.greaterThan(material.alphaTestNode as Node<"float">);
|
|
530
|
+
// Keep the authored scalar for cutout classification and impostor capture.
|
|
531
|
+
material.userData.tnImpostorAlphaTest = source.alphaTest;
|
|
532
|
+
if (canopy) {
|
|
533
|
+
lightNeedles(material, material.aoNode as Node<"float">);
|
|
534
|
+
if (!otherBiome) material.emissiveNode = (material.emissiveNode as Node<"vec3">).mul(0.18);
|
|
535
|
+
if (look.canopyEmissiveScale !== null && material.emissiveNode)
|
|
536
|
+
material.emissiveNode = (material.emissiveNode as Node<"vec3">).mul(look.canopyEmissiveScale);
|
|
537
|
+
} else if (!FLOWERS.has(asset))
|
|
538
|
+
material.emissiveNode = (material.colorNode as Node<"vec3">).mul(0.025);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/** Imported albedo graded per niche, then stone, cutout and coverage. */
|
|
542
|
+
function tintedFoliage(
|
|
543
|
+
material: MeshPhysicalNodeMaterial,
|
|
544
|
+
context: ISurfaceContext,
|
|
545
|
+
snowMap?: Texture,
|
|
546
|
+
): void {
|
|
547
|
+
const { source, asset, look, stone, cutout, canopy, fieldGrass, otherBiome } = context;
|
|
548
|
+
const map = source.map as Texture;
|
|
549
|
+
map.anisotropy = 8;
|
|
550
|
+
const sampled = texture(map, uv());
|
|
551
|
+
// Cooked albedo is already sRGB (KTX2 DFD transfer=2); never apply a second decode or lift.
|
|
552
|
+
const tint =
|
|
553
|
+
cutout && canopy
|
|
554
|
+
? look.canopyTint
|
|
555
|
+
: cutout && !FLOWERS.has(asset)
|
|
556
|
+
? look.bladeTint
|
|
557
|
+
: FLOWERS.has(asset)
|
|
558
|
+
? ([1, 1, 0.85] as const)
|
|
559
|
+
: canopy && source.name === "branch"
|
|
560
|
+
? ([0.26, 0.21, 0.15] as const)
|
|
561
|
+
: ([1, 1, 1] as const);
|
|
562
|
+
|
|
563
|
+
material.colorNode = sampled.rgb.mul(vec3(...tint));
|
|
564
|
+
if (!otherBiome && canopy && !cutout)
|
|
565
|
+
// A limb is photographed pale and smooth: lit by the low sun it drew as a tan tube beside the
|
|
566
|
+
// dark trunk it grows from, so it takes about half the trunk's value.
|
|
567
|
+
material.colorNode = sampled.rgb.mul(
|
|
568
|
+
source.name === "branch"
|
|
569
|
+
? vec3(0.11, 0.1, 0.085).mul(LOOK.limb)
|
|
570
|
+
: vec3(0.2, 0.18, 0.15).mul(LOOK.bark),
|
|
571
|
+
);
|
|
572
|
+
if (!otherBiome && (asset === "grass" || asset === "meadow" || asset === "scrub")) {
|
|
573
|
+
const tip = smoothstep(0.008, asset === "scrub" ? 0.065 : 0.34, positionGeometry.y);
|
|
574
|
+
const dry = smoothstep(0.65, 0.92, sin(phase).mul(0.5).add(0.5));
|
|
575
|
+
const dune = look.shoreGround ? smoothstep(7.5, 2.2, positionWorld.y) : float(0);
|
|
576
|
+
const green = mix(vec3(0.32, 0.48, 0.18), vec3(0.84, 0.92, 0.46), tip);
|
|
577
|
+
const straw = mix(vec3(0.4, 0.29, 0.12), vec3(1.18, 0.94, 0.48), tip);
|
|
578
|
+
const grain = dot(sampled.rgb, vec3(0.2126, 0.7152, 0.0722));
|
|
579
|
+
material.colorNode = mix(sampled.rgb.mul(green), straw.mul(grain), dry.max(dune));
|
|
580
|
+
if (fieldGrass)
|
|
581
|
+
material.colorNode = mix(vec3(0.06, 0.1, 0.025), vec3(0.2, 0.31, 0.065), tip).mul(
|
|
582
|
+
grain.mul(24).clamp(0.4, 1.4),
|
|
583
|
+
);
|
|
584
|
+
material.aoNode = mix(fieldGrass ? 0.55 : 0.35, 0.95, tip);
|
|
585
|
+
}
|
|
586
|
+
if (otherBiome && stone) material.colorNode = sampled.rgb.mul(vec3(...look.stoneSectionTint));
|
|
587
|
+
if (look.mesaRock && stone) material.colorNode = desertRockColor(sampled.rgb);
|
|
588
|
+
if (look.crestRock && stone) material.colorNode = alpineRockColor(alpineRockAlbedo(map));
|
|
589
|
+
if (otherBiome && cutout && !canopy)
|
|
590
|
+
material.colorNode = sampled.rgb.mul(vec3(...look.cutoutTint));
|
|
591
|
+
if (look.strawBlades && asset === "grass")
|
|
592
|
+
material.colorNode = vec3(1.28, 0.94, 0.52).mul(dot(sampled.rgb, vec3(0.2126, 0.7152, 0.0722)));
|
|
593
|
+
if (look.mats && cutout && !canopy) {
|
|
594
|
+
const root = smoothstep(0.015, 0.14, positionGeometry.y);
|
|
595
|
+
material.colorNode = mix(vec3(0.025, 0.035, 0.013), material.colorNode, root);
|
|
596
|
+
material.aoNode = mix(0.15, 0.8, root);
|
|
597
|
+
}
|
|
598
|
+
if (stone) stoneSurface(material, context, snowMap);
|
|
599
|
+
if (cutout) cutoutSurface(material, context, sampled);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/** The wind a non-stone section bends with, per instance, from the geometry's own `sway` weights. */
|
|
603
|
+
function applyWind(material: MeshPhysicalNodeMaterial, context: ISurfaceContext): void {
|
|
604
|
+
if (context.stone) return;
|
|
605
|
+
const bend = gust.mul(positionGeometry.y.max(0).pow(1.5)).mul(windStiffness(context.asset));
|
|
606
|
+
const offset = vec3(
|
|
607
|
+
positionLocal.x.add(bend),
|
|
608
|
+
positionLocal.y,
|
|
609
|
+
positionLocal.z.add(bend.mul(0.55)),
|
|
610
|
+
);
|
|
611
|
+
material.positionNode = offset;
|
|
612
|
+
// Cached shadow pages cast the rest pose, so a page refresh redraws the same shadow (see propMaterials sway).
|
|
613
|
+
material.castShadowPositionNode = positionLocal;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
function windStiffness(asset: string): number {
|
|
617
|
+
return asset === "spruce" ? 0.008 : 0.025;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* engine-override: the farthest applyWind moves a vertex of this section, its top bent by a full gust
|
|
622
|
+
* along (1, 0, 0.55). The owned engine grows each instance's shadow box by it, so a virtual shadow page
|
|
623
|
+
* draws only the trees that can reach it instead of the whole forest.
|
|
624
|
+
*/
|
|
625
|
+
function declareWindReach(
|
|
626
|
+
material: MeshPhysicalNodeMaterial,
|
|
627
|
+
asset: string,
|
|
628
|
+
geometry: BufferGeometry,
|
|
629
|
+
): void {
|
|
630
|
+
geometry.computeBoundingBox();
|
|
631
|
+
const top = Math.max(0, geometry.boundingBox?.max.y ?? 0);
|
|
632
|
+
const reach = top ** 1.5 * windStiffness(asset) * Math.hypot(1, 0.55);
|
|
633
|
+
const swaying = material as MeshPhysicalNodeMaterial & { maxPositionOffset?: number };
|
|
634
|
+
swaying.maxPositionOffset = Math.max(swaying.maxPositionOffset ?? 0, reach);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function surface(
|
|
638
|
+
source: MeshStandardMaterial,
|
|
639
|
+
asset: string,
|
|
640
|
+
world: WorldName,
|
|
641
|
+
snowMap?: Texture,
|
|
642
|
+
skyLight?: Texture,
|
|
643
|
+
comparison?: ICanopyComparison,
|
|
644
|
+
biome?: IWorldBiome,
|
|
645
|
+
canopyCover?: Node<"float">,
|
|
646
|
+
waxy = false,
|
|
647
|
+
): MeshPhysicalNodeMaterial {
|
|
648
|
+
const stone = STONE.has(asset);
|
|
649
|
+
const cutout = !stone && source.alphaTest > 0;
|
|
650
|
+
const canopy = asset === "spruce" || asset === "sapling";
|
|
651
|
+
const fieldGrass = (asset === "grass" || asset === "meadow") && source.name === "open-world-demo";
|
|
652
|
+
const look = BIOMES[world];
|
|
653
|
+
const otherBiome = !look.lawnGround;
|
|
654
|
+
const context: ISurfaceContext = {
|
|
655
|
+
source,
|
|
656
|
+
asset,
|
|
657
|
+
world,
|
|
658
|
+
look,
|
|
659
|
+
biome,
|
|
660
|
+
stone,
|
|
661
|
+
cutout,
|
|
662
|
+
canopy,
|
|
663
|
+
fieldGrass,
|
|
664
|
+
otherBiome,
|
|
665
|
+
};
|
|
666
|
+
const material = new MeshPhysicalNodeMaterial({
|
|
667
|
+
map: source.map,
|
|
668
|
+
color: source.color,
|
|
669
|
+
normalMap: source.normalMap,
|
|
670
|
+
roughness: stone ? 0.96 : 1,
|
|
671
|
+
specularIntensity: canopy
|
|
672
|
+
? comparison?.specular === "standard"
|
|
673
|
+
? 1
|
|
674
|
+
: 0
|
|
675
|
+
: look.mats && cutout
|
|
676
|
+
? 0
|
|
677
|
+
: cutout
|
|
678
|
+
? 0.02
|
|
679
|
+
: 0.3,
|
|
680
|
+
metalness: 0,
|
|
681
|
+
});
|
|
682
|
+
// A saguaro's stem is a wax layer over wet flesh, and it is the one plant here that answers the
|
|
683
|
+
// sun with a highlight instead of the bone-dry matte every other imported prop wears. The wax is
|
|
684
|
+
// the clearcoat: a broad, low-gloss lobe, not a wet sheen, over a slightly less matte base.
|
|
685
|
+
if (waxy) {
|
|
686
|
+
material.roughness = 0.62;
|
|
687
|
+
material.clearcoat = 0.35;
|
|
688
|
+
material.clearcoatRoughness = 0.5;
|
|
689
|
+
material.specularIntensity = 0.45;
|
|
690
|
+
}
|
|
691
|
+
copyNormalScale(material, source);
|
|
692
|
+
if (!otherBiome && canopy && !cutout) material.normalMap = source.normalMap;
|
|
693
|
+
// Forest needle crowns need their authored interior shading and filtered alpha coverage.
|
|
694
|
+
// A plain imported PBR card erased canopy depth at the same geometry and camera.
|
|
695
|
+
if (cutout && source.map && !(look.woodCanopy && canopy)) {
|
|
696
|
+
importedFoliage(material, source, source.map);
|
|
697
|
+
if (look.woodCanopy && (asset === "grass" || asset === "meadow")) {
|
|
698
|
+
// One photograph per species reads as astroturf: each clump takes its own value about 1 and
|
|
699
|
+
// a green-to-dry hue from its instance hash, and its root sinks into the soil's shade.
|
|
700
|
+
const clump = sin(phase.mul(3.7)).mul(0.5).add(0.5);
|
|
701
|
+
const shade = mix(vec3(0.62, 0.86, 0.5), vec3(0.98, 0.96, 0.52), clump.mul(clump));
|
|
702
|
+
// Drifts metres across: a meadow is darker in its damp patches and paler on its dry ones.
|
|
703
|
+
const drift = mix(0.78, 1.08, smoothstep(-0.4, 0.4, bakedNoise(positionWorld.mul(0.11))));
|
|
704
|
+
// Part of a meadow has gone to seed: straw patches tens of metres across break the one green,
|
|
705
|
+
// on a slower field than the drift so the two do not line up.
|
|
706
|
+
const straw = smoothstep(
|
|
707
|
+
0.2,
|
|
708
|
+
0.6,
|
|
709
|
+
bakedNoise(
|
|
710
|
+
vec3(positionWorld.x, 0, positionWorld.z)
|
|
711
|
+
.mul(0.027)
|
|
712
|
+
.add(vec3(31, 0, 17)),
|
|
713
|
+
),
|
|
714
|
+
);
|
|
715
|
+
const seeded = mix(vec3(1, 1, 1), vec3(1.32, 1.08, 0.6), straw.mul(0.6));
|
|
716
|
+
// A blade is dark where it leaves the thatch and brightest at its tip.
|
|
717
|
+
// A tuft stands three times as tall as the field grass, so the same 0.32 m ramp left nearly
|
|
718
|
+
// all of it at full value above a field that sits inside its own root shade: its ramp runs
|
|
719
|
+
// the tuft's own height.
|
|
720
|
+
const root = mix(0.34, 1, smoothstep(0.0, fieldGrass ? 0.32 : 0.8, positionGeometry.y));
|
|
721
|
+
// The tall tufts are photographed far paler than the field grass they stand in: bring them
|
|
722
|
+
// to the same sward so a tuft reads as taller grass, not as a lit paper cut-out.
|
|
723
|
+
const sward = fieldGrass ? vec3(1, 1, 1) : vec3(0.5, 0.62, 0.4);
|
|
724
|
+
material.colorNode = (material.colorNode as Node<"vec3">)
|
|
725
|
+
.mul(shade)
|
|
726
|
+
.mul(drift)
|
|
727
|
+
.mul(root)
|
|
728
|
+
.mul(sward)
|
|
729
|
+
.mul(seeded);
|
|
730
|
+
material.aoNode = mix(0.5, 1, smoothstep(0.0, 0.25, positionGeometry.y));
|
|
731
|
+
// A blade card stands upright, so its own normal faces the horizon and half the cards face
|
|
732
|
+
// away from the sun: those drew black beside sunlit ones. A meadow shades as the ground it
|
|
733
|
+
// grows from, so every blade takes the up normal and the sun's shadow still falls across it.
|
|
734
|
+
material.normalNode = vec3(0, 1, 0).transformDirection(cameraViewMatrix);
|
|
735
|
+
}
|
|
736
|
+
if (look.woodCanopy && asset === "fern") {
|
|
737
|
+
// A frond drew lime beside the sward it grows in: it takes a deeper green, each plant its own
|
|
738
|
+
// value, and the heart of the plant sinks into the shade of its own fronds.
|
|
739
|
+
const each = mix(0.85, 1.08, sin(phase.mul(2.3)).mul(0.5).add(0.5));
|
|
740
|
+
const heart = mix(0.5, 1, smoothstep(0.0, 0.4, positionGeometry.y));
|
|
741
|
+
material.colorNode = (material.colorNode as Node<"vec3">)
|
|
742
|
+
.mul(vec3(0.5, 0.66, 0.38))
|
|
743
|
+
.mul(each)
|
|
744
|
+
.mul(heart);
|
|
745
|
+
material.aoNode = mix(0.5, 1, smoothstep(0.0, 0.3, positionGeometry.y));
|
|
746
|
+
}
|
|
747
|
+
if (look.woodCanopy && FLOWERS.has(asset)) {
|
|
748
|
+
// A flower model carries its own leaves, photographed pale mint: beside the sward they drew
|
|
749
|
+
// as white paper rosettes (paint-by-species capture, 2026-11-09). The green texels take the
|
|
750
|
+
// sward's green and sink at the plant's heart; a petal, redder than it is green, keeps its
|
|
751
|
+
// photograph.
|
|
752
|
+
const sampled = material.colorNode as Node<"vec3">;
|
|
753
|
+
const leaf = smoothstep(0.0, 0.06, sampled.g.sub(sampled.r));
|
|
754
|
+
const heart = mix(0.45, 1, smoothstep(0.0, 0.3, positionGeometry.y));
|
|
755
|
+
material.colorNode = mix(sampled, sampled.mul(vec3(0.5, 0.66, 0.4)).mul(heart), leaf);
|
|
756
|
+
}
|
|
757
|
+
if (look.swardTint && !FLOWERS.has(asset))
|
|
758
|
+
material.colorNode = (material.colorNode as Node<"vec3">).mul(LOOK.sward);
|
|
759
|
+
if (look.woodCanopy) {
|
|
760
|
+
// Blades and fronds a cell thick pass the sun through; without it a meadow seen against the
|
|
761
|
+
// sun is lit almost only by the sky. Same shadowed term as the needles, without their fill.
|
|
762
|
+
// Roots and the gaps between blades stay dark; the upper blade carries the glow.
|
|
763
|
+
lightNeedles(material, smoothstep(0.02, 0.4, positionGeometry.y), 0.4);
|
|
764
|
+
material.emissiveNode = null;
|
|
765
|
+
}
|
|
766
|
+
} else if (source.map) tintedFoliage(material, context, snowMap);
|
|
767
|
+
// An Outcrop rock carries no map: its bedding, varnish and drifted sand are vertex colours.
|
|
768
|
+
else if (stone && source.vertexColors) {
|
|
769
|
+
material.vertexColors = true;
|
|
770
|
+
material.color.setRGB(...(look.propStoneTint ?? ([1, 1, 1] as const)));
|
|
771
|
+
}
|
|
772
|
+
// River rock lies on the waterline, and wet stone is darker and glossier: water fills the pores
|
|
773
|
+
// (albedo about halves) and films the surface (roughness falls). `?riverbed=off`: dry, as before.
|
|
774
|
+
if (asset === "riverrock" && RIVER_BED && material.colorNode) {
|
|
775
|
+
material.colorNode = (material.colorNode as Node<"vec3">).mul(0.45);
|
|
776
|
+
material.roughness = 0.32;
|
|
777
|
+
}
|
|
778
|
+
applyWind(material, context);
|
|
779
|
+
if (canopy) nearCameraFade(material, cutout);
|
|
780
|
+
// Image-based sky light, as Wildwood lights the same Fab packs (Kloofendal at 1.8 × 0.629).
|
|
781
|
+
// Without it a shaded needle card gets only the hemisphere fill and falls to one flat dark value.
|
|
782
|
+
// Per material, not `scene.environment`: the ground is tuned to the fill alone and cannot opt out.
|
|
783
|
+
if (skyLight && !stone) skyEnvironment(material, skyLight);
|
|
784
|
+
// At full sky light a forest crown is ~85% lit by sky and emissive, so it shows no sun side.
|
|
785
|
+
if (skyLight && look.woodCanopy && canopy) material.envMapIntensity = 0.35;
|
|
786
|
+
// A plant under the crowns sees the sky through a few gaps: its ambient and sky-image light fall
|
|
787
|
+
// to the row's share where the baked canopy closes over it, and the sun's own light is untouched,
|
|
788
|
+
// so a sunlit meadow keeps its full sky and a fern in deep shade stops glowing.
|
|
789
|
+
if (canopyCover && look.understoreySkyLight !== undefined && !canopy && !stone)
|
|
790
|
+
material.aoNode = ((material.aoNode as Node<"float"> | null) ?? float(1)).mul(
|
|
791
|
+
mix(float(1), LOOK.plantShade, smoothstep(0.05, 0.6, canopyCover)),
|
|
792
|
+
);
|
|
793
|
+
return material;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
/**
|
|
797
|
+
* The photograph's sun disc (peak luminance ~72,559, 81° from the scene sun) gave the meadow 44% of
|
|
798
|
+
* its light from a second, unshadowed key light. The scene's own sun is the key; the image keeps only
|
|
799
|
+
* its sky, so every texel is capped at a luminance of 30.
|
|
800
|
+
*/
|
|
801
|
+
const SKY_LUMINANCE_CAP = 30;
|
|
802
|
+
function clampSkySun(sky: Texture): void {
|
|
803
|
+
const image = sky.image as { data: Uint16Array | Float32Array; width: number; height: number };
|
|
804
|
+
const data = image.data;
|
|
805
|
+
const half = data instanceof Uint16Array;
|
|
806
|
+
const read = (i: number) =>
|
|
807
|
+
half ? DataUtils.fromHalfFloat(data[i] as number) : (data[i] as number);
|
|
808
|
+
const write = (i: number, v: number) => {
|
|
809
|
+
data[i] = half ? DataUtils.toHalfFloat(v) : v;
|
|
810
|
+
};
|
|
811
|
+
const channels = data.length / (image.width * image.height);
|
|
812
|
+
for (let i = 0; i < data.length; i += channels) {
|
|
813
|
+
const r = read(i);
|
|
814
|
+
const g = read(i + 1);
|
|
815
|
+
const b = read(i + 2);
|
|
816
|
+
const luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
817
|
+
if (luminance <= SKY_LUMINANCE_CAP) continue;
|
|
818
|
+
const scale = SKY_LUMINANCE_CAP / luminance;
|
|
819
|
+
write(i, r * scale);
|
|
820
|
+
write(i + 1, g * scale);
|
|
821
|
+
write(i + 2, b * scale);
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* The sky's diffuse light as two colours: the cosine-weighted mean radiance of the upper hemisphere
|
|
827
|
+
* (what a level surface receives) and of the lower one (the bounce a surface facing down receives).
|
|
828
|
+
* A rough surface shows little more of image-based light than this, and two colours cost no sampler.
|
|
829
|
+
*/
|
|
830
|
+
export function skyIrradiance(sky: Texture): {
|
|
831
|
+
up: [number, number, number];
|
|
832
|
+
down: [number, number, number];
|
|
833
|
+
} {
|
|
834
|
+
const image = sky.image as { data: Uint16Array | Float32Array; width: number; height: number };
|
|
835
|
+
const data = image.data;
|
|
836
|
+
const half = data instanceof Uint16Array;
|
|
837
|
+
const read = (i: number) =>
|
|
838
|
+
half ? DataUtils.fromHalfFloat(data[i] as number) : (data[i] as number);
|
|
839
|
+
const channels = data.length / (image.width * image.height);
|
|
840
|
+
const up = [0, 0, 0];
|
|
841
|
+
const down = [0, 0, 0];
|
|
842
|
+
let upWeight = 0;
|
|
843
|
+
let downWeight = 0;
|
|
844
|
+
for (let row = 0; row < image.height; row += 2) {
|
|
845
|
+
// Equirectangular rows run from the zenith (row 0) to the nadir.
|
|
846
|
+
const theta = ((row + 0.5) / image.height) * Math.PI;
|
|
847
|
+
const y = Math.cos(theta);
|
|
848
|
+
const solid = Math.sin(theta);
|
|
849
|
+
for (let column = 0; column < image.width; column += 2) {
|
|
850
|
+
const i = (row * image.width + column) * channels;
|
|
851
|
+
const weight = Math.abs(y) * solid;
|
|
852
|
+
const target = y >= 0 ? up : down;
|
|
853
|
+
target[0] = (target[0] ?? 0) + read(i) * weight;
|
|
854
|
+
target[1] = (target[1] ?? 0) + read(i + 1) * weight;
|
|
855
|
+
target[2] = (target[2] ?? 0) + read(i + 2) * weight;
|
|
856
|
+
if (y >= 0) upWeight += weight;
|
|
857
|
+
else downWeight += weight;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
const mean = (c: number[], w: number): [number, number, number] => [
|
|
861
|
+
(c[0] ?? 0) / (w || 1),
|
|
862
|
+
(c[1] ?? 0) / (w || 1),
|
|
863
|
+
(c[2] ?? 0) / (w || 1),
|
|
864
|
+
];
|
|
865
|
+
return { up: mean(up, upWeight), down: mean(down, downWeight) };
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
let skyLight: Promise<Texture | undefined> | undefined;
|
|
869
|
+
/** The CC0 sky photograph, loaded once; absent locally, vegetation keeps the fill alone. */
|
|
870
|
+
export function loadSkyLight(assets: IAssetLoader): Promise<Texture | undefined> {
|
|
871
|
+
skyLight ??= assets
|
|
872
|
+
.resolve("hdri/kloofendal_48d_partly_cloudy_2k.hdr")
|
|
873
|
+
.then(async ([url]) => {
|
|
874
|
+
if (!url) return undefined;
|
|
875
|
+
const sky = await new HDRLoader().loadAsync(url);
|
|
876
|
+
sky.mapping = EquirectangularReflectionMapping;
|
|
877
|
+
clampSkySun(sky);
|
|
878
|
+
return sky;
|
|
879
|
+
})
|
|
880
|
+
.catch(() => undefined);
|
|
881
|
+
return skyLight;
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
/** Per-world reshape for one asset, applied on top of the shared whole-model scale. */
|
|
885
|
+
const WORLD_ASSET_SCALES: Record<string, Record<string, readonly [number, number, number]>> = {
|
|
886
|
+
tundra: { grass: [1.35, 1, 1.35], bush: [0.18, 1, 0.18], scrub: [1.1, 2, 1.1] },
|
|
887
|
+
coastal: { grass: [0.55, 1.35, 0.55] },
|
|
888
|
+
forest: { poppy: [1, 1.4, 1] },
|
|
889
|
+
};
|
|
890
|
+
/** A reshape every world applies to one asset. */
|
|
891
|
+
const ASSET_SCALES: Record<string, readonly [number, number, number]> = {
|
|
892
|
+
spruce: [1.12, 1, 1.12],
|
|
893
|
+
};
|
|
894
|
+
|
|
895
|
+
/** Every step is a diagonal scale, so the order the reshapes run in cannot change the result. */
|
|
896
|
+
function scaleGeometry(
|
|
897
|
+
geometry: BufferGeometry,
|
|
898
|
+
asset: string,
|
|
899
|
+
world: WorldName,
|
|
900
|
+
size: Vector3,
|
|
901
|
+
): void {
|
|
902
|
+
const look = BIOMES[world];
|
|
903
|
+
const worldScale = WORLD_ASSET_SCALES[world]?.[asset];
|
|
904
|
+
if (worldScale) geometry.scale(worldScale[0], worldScale[1], worldScale[2]);
|
|
905
|
+
const assetScale = ASSET_SCALES[asset];
|
|
906
|
+
if (assetScale) geometry.scale(assetScale[0], assetScale[1], assetScale[2]);
|
|
907
|
+
// Alpine peaks are pinned to a 24 m longest axis after their mountain reshape.
|
|
908
|
+
if (look.pinnedCrags && asset === "mountain") {
|
|
909
|
+
geometry.scale(0.65, 1.45, 1.8);
|
|
910
|
+
const longest = Math.max(size.x * 0.65, size.y * 1.45, size.z * 1.8);
|
|
911
|
+
geometry.scale(24 / longest, 24 / longest, 24 / longest);
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
/** Radial coverage per height band: tips see sky, needles near the trunk do not. */
|
|
916
|
+
function addRadialCoverage(
|
|
917
|
+
geometry: BufferGeometry,
|
|
918
|
+
metres: number,
|
|
919
|
+
world: WorldName,
|
|
920
|
+
comparison?: ICanopyComparison,
|
|
921
|
+
): void {
|
|
922
|
+
const positions = geometry.getAttribute("position");
|
|
923
|
+
const radii = new Float32Array(16);
|
|
924
|
+
const band = (i: number) =>
|
|
925
|
+
Math.min(15, Math.max(0, Math.floor((positions.getY(i) / metres) * 16)));
|
|
926
|
+
for (let i = 0; i < positions.count; i++)
|
|
927
|
+
radii[band(i)] = Math.max(
|
|
928
|
+
radii[band(i)] ?? 0,
|
|
929
|
+
Math.hypot(positions.getX(i), positions.getZ(i)),
|
|
930
|
+
);
|
|
931
|
+
const inner = Float32Array.from({ length: positions.count }, (_, i) =>
|
|
932
|
+
Math.min(
|
|
933
|
+
1,
|
|
934
|
+
Math.hypot(positions.getX(i), positions.getZ(i)) / Math.max(0.1, radii[band(i)] ?? 0),
|
|
935
|
+
),
|
|
936
|
+
);
|
|
937
|
+
geometry.setAttribute("inner", new BufferAttribute(inner, 1));
|
|
938
|
+
if (!BIOMES[world].crownGeometryNormals || comparison?.normals !== "radial") return;
|
|
939
|
+
// Keep synthetic crown normals as an explicit same-asset probe; the authored normals and normal
|
|
940
|
+
// map preserve needle lighting by default. Only this owned clone changes, never the cached asset.
|
|
941
|
+
const crownNormals = new Float32Array(positions.count * 3);
|
|
942
|
+
const direction = new Vector3();
|
|
943
|
+
for (let i = 0; i < positions.count; i++) {
|
|
944
|
+
direction
|
|
945
|
+
.set(positions.getX(i), Math.max(0.1, radii[band(i)] ?? 0) * 0.45, positions.getZ(i))
|
|
946
|
+
.normalize();
|
|
947
|
+
direction.toArray(crownNormals, i * 3);
|
|
948
|
+
}
|
|
949
|
+
geometry.setAttribute("normal", new BufferAttribute(crownNormals, 3));
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
/** One whole-model base and scale for all sections: scaling each part separately detached crowns. */
|
|
953
|
+
/**
|
|
954
|
+
* The model height a prop's base stands on. A starter-assets `.glb` is a Poly Haven scan whose origin
|
|
955
|
+
* is the ground it was scanned on, so a branch or root below y = 0 stays buried.
|
|
956
|
+
*/
|
|
957
|
+
export function standingBase(box: Box3, path: string): number {
|
|
958
|
+
return path.endsWith(".glb") ? Math.max(box.min.y, 0) : box.min.y;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
function prepareGeometry(
|
|
962
|
+
mesh: Mesh,
|
|
963
|
+
one: IPackSpecies,
|
|
964
|
+
world: WorldName,
|
|
965
|
+
stone: boolean,
|
|
966
|
+
box: Box3,
|
|
967
|
+
size: Vector3,
|
|
968
|
+
section: IMeshSection,
|
|
969
|
+
): BufferGeometry {
|
|
970
|
+
// `spruce_full_03_low` ships zero normals; the repair runs after the reshape so they fit it.
|
|
971
|
+
const geometry = sectionGeometry(mesh, box, section, {
|
|
972
|
+
centreXZ: stone && BIOMES[world].lawnGround,
|
|
973
|
+
});
|
|
974
|
+
scaleGeometry(geometry, one.asset, world, size);
|
|
975
|
+
repairZeroNormals(geometry);
|
|
976
|
+
return geometry;
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
/**
|
|
980
|
+
* The species one world draws, read from its biome file: every cell row whose asset the file names,
|
|
981
|
+
* then the rocks of the west-coast scans it names, then a mat plain's own small bush.
|
|
982
|
+
*
|
|
983
|
+
* The file says which species live in the biome; this pack says what art each one is drawn with.
|
|
984
|
+
*/
|
|
985
|
+
export function speciesFor(world: WorldName): IPackSpecies[] {
|
|
986
|
+
const look = BIOMES[world];
|
|
987
|
+
const held = new Set(biomeSpec(world).species);
|
|
988
|
+
// A mat plain grows one small scrub, not the temperate thicket pair.
|
|
989
|
+
const rows = look.scrubBush ? species.filter((one) => one.asset !== "bush") : species;
|
|
990
|
+
const selected = rows.filter((one) => held.has(one.asset));
|
|
991
|
+
if (look.scrubBush && held.has("bush"))
|
|
992
|
+
selected.push({ asset: "bush", variant: 0, path: "scrub/0", metres: 0.45 });
|
|
993
|
+
return selected.map((one) => {
|
|
994
|
+
const grass = look.lawnGround ? POLYHAVEN_GRASS[one.asset]?.[one.variant] : undefined;
|
|
995
|
+
const scan = grass ?? (look.lawnGround ? POLYHAVEN_FLOOR[one.asset]?.[one.variant] : undefined);
|
|
996
|
+
return scan ? { ...one, path: scan[0], metres: scan[1] } : one;
|
|
997
|
+
});
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
/** The baked crown weight, 0 in the open and about 1 under a closed stand, at a world position. */
|
|
1001
|
+
function canopyCoverNode(ground: IPropGround, canopy: Float32Array): Node<"float"> {
|
|
1002
|
+
const map = new DataTexture(
|
|
1003
|
+
Uint8Array.from(canopy, (weight) => Math.round(Math.min(1, Math.max(0, weight)) * 255)),
|
|
1004
|
+
ground.resolution,
|
|
1005
|
+
ground.resolution,
|
|
1006
|
+
RedFormat,
|
|
1007
|
+
);
|
|
1008
|
+
map.minFilter = LinearFilter;
|
|
1009
|
+
map.magFilter = LinearFilter;
|
|
1010
|
+
map.needsUpdate = true;
|
|
1011
|
+
const at = positionWorld.xz
|
|
1012
|
+
.div(float(ground.size))
|
|
1013
|
+
.add(0.5)
|
|
1014
|
+
.mul((ground.resolution - 1) / ground.resolution)
|
|
1015
|
+
.add(0.5 / ground.resolution);
|
|
1016
|
+
return texture(map, at).r;
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
export interface IPackProps {
|
|
1020
|
+
readonly parts: Map<string, IPropPart[]>;
|
|
1021
|
+
readonly dispose: () => void;
|
|
1022
|
+
}
|
|
1023
|
+
export async function loadPack(
|
|
1024
|
+
assets?: IAssetLoader,
|
|
1025
|
+
world: WorldName = WORLD,
|
|
1026
|
+
ground?: IPropGround,
|
|
1027
|
+
comparison?: ICanopyComparison,
|
|
1028
|
+
biome?: IWorldBiome,
|
|
1029
|
+
/** The baked crown weight per ground vertex (`scatter.canopy`), row-major like the heights. */
|
|
1030
|
+
canopy?: Float32Array,
|
|
1031
|
+
): Promise<IPackProps> {
|
|
1032
|
+
const measured = biome ?? (ground ? measureBiome(BIOMES[world], ground.heights) : undefined);
|
|
1033
|
+
const look = BIOMES[world];
|
|
1034
|
+
if (!measured && look.stoneSnow)
|
|
1035
|
+
throw new RangeError("Stone snow requires measured terrain elevations");
|
|
1036
|
+
const parts = new Map<string, IPropPart[]>();
|
|
1037
|
+
const built: { geometry: BufferGeometry; material: Material }[] = [];
|
|
1038
|
+
const selected = speciesFor(world);
|
|
1039
|
+
const canopyCover = ground && canopy ? canopyCoverNode(ground, canopy) : undefined;
|
|
1040
|
+
const rockface =
|
|
1041
|
+
look.crestRock && assets
|
|
1042
|
+
? await Promise.all(
|
|
1043
|
+
[ROCKFACE_MAPS.diffuse, ROCKFACE_MAPS.normal, GROUND_MAPS.snow.diffuse].map(
|
|
1044
|
+
(path, index) =>
|
|
1045
|
+
path
|
|
1046
|
+
? assets
|
|
1047
|
+
.texture(path, { data: index === 1, wrap: RepeatWrapping })
|
|
1048
|
+
.catch(() => undefined)
|
|
1049
|
+
: undefined,
|
|
1050
|
+
),
|
|
1051
|
+
)
|
|
1052
|
+
: [];
|
|
1053
|
+
const rockGround = await createRockGround(assets, ground, measured);
|
|
1054
|
+
const sky = assets ? await loadSkyLight(assets) : undefined;
|
|
1055
|
+
const loaded = await Promise.all(
|
|
1056
|
+
selected.map(async (one) => {
|
|
1057
|
+
// A species with no scan keeps its procedural part (`buildPropVariants`).
|
|
1058
|
+
if (!assets || !one.path.endsWith(".glb")) return undefined;
|
|
1059
|
+
try {
|
|
1060
|
+
return await assets.model<{ scene?: Group }>(one.path);
|
|
1061
|
+
} catch {
|
|
1062
|
+
return undefined;
|
|
1063
|
+
}
|
|
1064
|
+
}),
|
|
1065
|
+
);
|
|
1066
|
+
loaded.forEach((gltf, index) => {
|
|
1067
|
+
const one = selected[index];
|
|
1068
|
+
const root = gltf?.scene;
|
|
1069
|
+
if (!root || !one) return;
|
|
1070
|
+
normaliseToMetres(root, {
|
|
1071
|
+
metres: one.metres,
|
|
1072
|
+
axis: STONE.has(one.asset) || one.asset === "deadfall" ? "longest" : "height",
|
|
1073
|
+
});
|
|
1074
|
+
const box = new Box3().setFromObject(root); // engine-override: center baked geometry after unit normalization
|
|
1075
|
+
const size = box.getSize(new Vector3());
|
|
1076
|
+
box.min.y = standingBase(box, one.path);
|
|
1077
|
+
const stone = STONE.has(one.asset);
|
|
1078
|
+
const entry = parts.get(`${one.asset}:${one.variant}`) ?? [];
|
|
1079
|
+
const horizon = new Map<IPropPart, BufferGeometry>();
|
|
1080
|
+
root.traverse((object) => {
|
|
1081
|
+
const mesh = object as Mesh;
|
|
1082
|
+
if (!mesh.isMesh) return;
|
|
1083
|
+
for (const section of meshSections(mesh)) {
|
|
1084
|
+
if (!section.material) continue;
|
|
1085
|
+
let source = section.material as MeshStandardMaterial;
|
|
1086
|
+
if (look.crestRock && stone && rockface[0]) {
|
|
1087
|
+
source = source.clone();
|
|
1088
|
+
source.map = rockface[0];
|
|
1089
|
+
source.normalMap = rockface[1] ?? null;
|
|
1090
|
+
}
|
|
1091
|
+
// The reduced Kite GLB keeps UVs but carries no textures. Reuse the matching near surface;
|
|
1092
|
+
// skipping those parts kept all 3,200 trees at full detail, even in the aerial view.
|
|
1093
|
+
const role: PropRole = stone ? "stone" : source.alphaTest > 0 ? "pine" : "bark";
|
|
1094
|
+
const inherited =
|
|
1095
|
+
one.asset === "spruce" && one.level
|
|
1096
|
+
? entry.find((part) => (part.level ?? 0) === 0 && part.role === role)?.material
|
|
1097
|
+
: undefined;
|
|
1098
|
+
// A far level with no texture and no vertex colour has nothing to draw with.
|
|
1099
|
+
if (
|
|
1100
|
+
!source.map &&
|
|
1101
|
+
!source.vertexColors &&
|
|
1102
|
+
one.level &&
|
|
1103
|
+
!(inherited instanceof MeshPhysicalNodeMaterial)
|
|
1104
|
+
)
|
|
1105
|
+
continue;
|
|
1106
|
+
const geometry = prepareGeometry(mesh, one, world, stone, box, size, section);
|
|
1107
|
+
if (source.alphaTest > 0 && (one.asset === "spruce" || one.asset === "sapling"))
|
|
1108
|
+
addRadialCoverage(geometry, one.metres, world, comparison);
|
|
1109
|
+
const material =
|
|
1110
|
+
inherited instanceof MeshPhysicalNodeMaterial
|
|
1111
|
+
? inherited
|
|
1112
|
+
: surface(
|
|
1113
|
+
source,
|
|
1114
|
+
one.asset,
|
|
1115
|
+
world,
|
|
1116
|
+
rockface[2],
|
|
1117
|
+
sky,
|
|
1118
|
+
comparison,
|
|
1119
|
+
measured,
|
|
1120
|
+
canopyCover,
|
|
1121
|
+
one.path.includes("saguaro"),
|
|
1122
|
+
);
|
|
1123
|
+
if (stone) rockGround?.apply(material);
|
|
1124
|
+
else declareWindReach(material, one.asset, geometry);
|
|
1125
|
+
if (source !== section.material) source.dispose();
|
|
1126
|
+
built.push({ geometry, material });
|
|
1127
|
+
const part = { geometry, material, role, level: one.level ?? 0, variant: one.variant };
|
|
1128
|
+
entry.push(part);
|
|
1129
|
+
const rung =
|
|
1130
|
+
one.asset === "spruce" && one.level === 1 && source.alphaTest === 0 && !section.group
|
|
1131
|
+
? lodChainOf(baseGeometryOf(mesh))?.levels.at(-1)?.index
|
|
1132
|
+
: undefined;
|
|
1133
|
+
if (rung) {
|
|
1134
|
+
const reduced = geometry.clone().setIndex(rung.clone());
|
|
1135
|
+
built.push({ geometry: reduced, material });
|
|
1136
|
+
horizon.set(part, reduced);
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
});
|
|
1140
|
+
// Past LOD_BANDS.far the far spruce draws its cook's terminal rung (TN_discrete_lod) of the opaque
|
|
1141
|
+
// trunk and branch meshes, which stayed full detail at every distance: 7-17k of each tree's
|
|
1142
|
+
// 12-21k far triangles. Needle cards, which no simplifier can reduce, keep FAR_NEEDLE_TRIANGLES
|
|
1143
|
+
// of their cards at the same total area: in a stand, 8.7 M far card triangles cost 5 ms of GPU.
|
|
1144
|
+
if (horizon.size > 0)
|
|
1145
|
+
for (const part of entry.filter((candidate) => candidate.level === 1)) {
|
|
1146
|
+
let geometry = horizon.get(part) ?? part.geometry;
|
|
1147
|
+
if (part.role === "pine" && geometry.index) {
|
|
1148
|
+
// A multi-material section draws a range of a shared index: its own triangles set the share.
|
|
1149
|
+
const { start, count } = geometry.drawRange;
|
|
1150
|
+
const drawn = (Math.min(geometry.index.count, start + count) - start) / 3;
|
|
1151
|
+
geometry = thinCards(geometry, Math.min(1, FAR_NEEDLE_TRIANGLES / drawn));
|
|
1152
|
+
if (geometry !== part.geometry && part.material)
|
|
1153
|
+
built.push({ geometry, material: part.material });
|
|
1154
|
+
}
|
|
1155
|
+
entry.push({ ...part, geometry, level: 2 });
|
|
1156
|
+
}
|
|
1157
|
+
// The meadow mat reaches 220 m from the eye, where a mat is a few pixels: past LOD_BANDS.mid it
|
|
1158
|
+
// keeps a third of its blades and past far a tenth, each grown to hold the mat's area. UE grass
|
|
1159
|
+
// steps down its static mesh's own LODs the same way (UE 5.8.3:
|
|
1160
|
+
// Engine/Source/Runtime/Landscape/Classes/LandscapeGrassType.h:59-79, cull distances and MinLOD).
|
|
1161
|
+
// The field grass and the ferns step down the same way at their own nearer `LOD_STEPS`, where a
|
|
1162
|
+
// tuft is still a dozen pixels tall: they keep half their blades, then a quarter.
|
|
1163
|
+
const thinned =
|
|
1164
|
+
one.asset === "meadow"
|
|
1165
|
+
? ([
|
|
1166
|
+
[1, 0.35],
|
|
1167
|
+
[2, 0.12],
|
|
1168
|
+
] as const)
|
|
1169
|
+
: one.asset === "grass" || one.asset === "fern"
|
|
1170
|
+
? ([
|
|
1171
|
+
[1, 0.5],
|
|
1172
|
+
[2, 0.25],
|
|
1173
|
+
] as const)
|
|
1174
|
+
: undefined;
|
|
1175
|
+
if (thinned)
|
|
1176
|
+
for (const part of entry.filter((candidate) => (candidate.level ?? 0) === 0))
|
|
1177
|
+
for (const [level, keep] of thinned) {
|
|
1178
|
+
if (!part.geometry.index || part.geometry.groups.length > 0 || !part.material) continue;
|
|
1179
|
+
const geometry = thinCards(part.geometry, keep);
|
|
1180
|
+
built.push({ geometry, material: part.material });
|
|
1181
|
+
entry.push({ ...part, geometry, level });
|
|
1182
|
+
}
|
|
1183
|
+
if (entry.some((part) => (part.level ?? 0) === 0))
|
|
1184
|
+
parts.set(`${one.asset}:${one.variant}`, entry);
|
|
1185
|
+
});
|
|
1186
|
+
return {
|
|
1187
|
+
parts,
|
|
1188
|
+
dispose: () => {
|
|
1189
|
+
rockGround?.dispose();
|
|
1190
|
+
for (const one of built) one.geometry.dispose();
|
|
1191
|
+
for (const material of new Set(built.map((one) => one.material))) material.dispose();
|
|
1192
|
+
parts.clear();
|
|
1193
|
+
},
|
|
1194
|
+
};
|
|
1195
|
+
}
|