@threenative/terrain 0.1.9 → 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 +40 -59
- package/THIRD_PARTY_NOTICES.md +2 -1
- package/dist/bake-DJ5cxg0C.d.ts +82 -0
- package/dist/editor/index.d.ts +4 -4
- package/dist/editor/index.js +130 -128
- package/dist/editor/server.d.ts +2 -2
- package/dist/editor/server.js +38 -35
- package/dist/editor/worker.d.ts +1 -1
- package/dist/editor/worker.js +101 -101
- package/dist/export.d.ts +1 -1
- package/dist/index.d.ts +156 -273
- package/dist/index.js +562 -378
- 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 +337 -223
- package/dist/mcp/cook-child.js +1963 -1543
- package/dist/mcp/server.js +716 -429
- 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 +1142 -7
- package/dist/starter.js +2102 -120
- package/dist/{terrain-CAEaW0mQ.d.ts → terrain-DYCyAOCJ.d.ts} +1 -1
- package/dist/three.d.ts +1 -1
- package/dist/three.js +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/editor.md +73 -0
- package/docs/authoring/new-environment.md +97 -0
- package/docs/authoring/recipes.md +97 -0
- package/docs/authoring/world-export.md +66 -0
- package/package.json +24 -8
- package/starter/alpine/README.md +22 -12
- 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/arbor/wind.ts +84 -0
- package/starter/coastal/README.md +26 -25
- 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 -23
- 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 +59 -116
- package/starter/forest/assets.json +14 -14
- package/starter/forest/bake.mjs +26 -4
- package/starter/forest/playerRig.ts +89 -0
- package/starter/forest/recipe.json +233 -43
- package/starter/forest/render/bakedFile.ts +17 -0
- package/starter/forest/render/benchmark.ts +77 -0
- package/starter/forest/render/biomeData.ts +14 -0
- package/starter/forest/render/biomes.ts +637 -0
- package/starter/forest/render/ground/cover.ts +565 -0
- package/starter/forest/render/ground/horizon.ts +219 -0
- package/starter/forest/render/ground/palette.d.ts +1 -0
- package/starter/forest/render/ground/palette.js +11 -0
- package/starter/forest/render/ground/scatter.ts +104 -0
- package/starter/forest/render/ground/terrain.ts +1796 -0
- package/starter/forest/render/kitAssets.ts +28 -0
- package/starter/forest/render/loading.ts +354 -0
- package/starter/forest/render/lookKnobs.ts +37 -0
- package/starter/forest/render/orbitCamera.ts +155 -0
- package/starter/forest/render/props/farForest.ts +94 -0
- package/starter/forest/render/props/pack.ts +1078 -0
- package/starter/forest/render/props/prepared.ts +226 -0
- package/starter/forest/render/props/propMaterials.ts +1247 -0
- package/starter/forest/render/props/propProgress.ts +2 -0
- package/starter/forest/render/props/propWorld.ts +105 -0
- package/starter/forest/render/props/props.ts +624 -0
- package/starter/forest/render/props/spruce.ts +440 -0
- package/starter/forest/render/sky/atmosphere.ts +329 -0
- package/starter/forest/render/sky/sky.ts +382 -0
- package/starter/forest/render/texturePreparation.ts +135 -0
- package/starter/forest/render/water/river.ts +1132 -0
- package/starter/forest/render/worldNames.ts +6 -0
- package/starter/forest/scene.ts +715 -0
- package/starter/forest/sceneProps.ts +559 -0
- package/starter/forest/sceneState.ts +33 -0
- package/starter/forest/surface.json +2 -2
- package/starter/forest/world/terrainAssets.ts +158 -0
- package/starter/forest/world/walkBand.ts +47 -0
- package/starter/forest/world/water.ts +74 -0
- package/starter/forest/worlds.ts +13 -0
- package/starter/tundra/README.md +35 -14
- 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 +901 -15
- package/AGENT_GUIDE.md +0 -209
- package/starter/forest/sky.ts +0 -38
- package/starter/forest/water.ts +0 -257
- package/starter/forest/world.ts +0 -182
|
@@ -0,0 +1,1829 @@
|
|
|
1
|
+
import type { IAssetLoader } from "@threenative/core";
|
|
2
|
+
import {
|
|
3
|
+
Heightfield,
|
|
4
|
+
type HeightfieldCollar,
|
|
5
|
+
bakedFractalNoise,
|
|
6
|
+
bakedNoise,
|
|
7
|
+
loadTerrainSplat,
|
|
8
|
+
} from "@threenative/core/world";
|
|
9
|
+
import { waterlineRadius } from "@threenative/core/world";
|
|
10
|
+
import { createSegmentIndex, paintCanopyCover } from "@threenative/terrain";
|
|
11
|
+
import {
|
|
12
|
+
COLLAR_UNDER_TILE,
|
|
13
|
+
STREAM_TILE,
|
|
14
|
+
TILE_INSIDE_SQUARE,
|
|
15
|
+
collarMorph,
|
|
16
|
+
} from "@threenative/terrain/starter";
|
|
17
|
+
import {
|
|
18
|
+
ClampToEdgeWrapping,
|
|
19
|
+
DataTexture,
|
|
20
|
+
DataUtils,
|
|
21
|
+
Float32BufferAttribute,
|
|
22
|
+
HalfFloatType,
|
|
23
|
+
LinearFilter,
|
|
24
|
+
MathUtils,
|
|
25
|
+
Mesh,
|
|
26
|
+
MeshStandardMaterial,
|
|
27
|
+
RGBAFormat,
|
|
28
|
+
RepeatWrapping,
|
|
29
|
+
type Texture,
|
|
30
|
+
type Vector2,
|
|
31
|
+
Vector3,
|
|
32
|
+
} from "three";
|
|
33
|
+
import {
|
|
34
|
+
abs,
|
|
35
|
+
attribute,
|
|
36
|
+
clamp,
|
|
37
|
+
dot,
|
|
38
|
+
float,
|
|
39
|
+
fract,
|
|
40
|
+
fwidth,
|
|
41
|
+
max,
|
|
42
|
+
min,
|
|
43
|
+
mix,
|
|
44
|
+
mx_noise_float,
|
|
45
|
+
normalWorld,
|
|
46
|
+
normalWorldGeometry,
|
|
47
|
+
normalize,
|
|
48
|
+
oneMinus,
|
|
49
|
+
positionView,
|
|
50
|
+
positionWorld,
|
|
51
|
+
rotateUV,
|
|
52
|
+
sign,
|
|
53
|
+
smoothstep,
|
|
54
|
+
texture,
|
|
55
|
+
transformNormalToView,
|
|
56
|
+
vec2,
|
|
57
|
+
vec3,
|
|
58
|
+
vec4,
|
|
59
|
+
} from "three/tsl";
|
|
60
|
+
import type { Node, UniformNode } from "three/webgpu";
|
|
61
|
+
import { MeshStandardNodeMaterial } from "three/webgpu";
|
|
62
|
+
import { GROUND_TILE, type LayerKey, ROCKFACE_MAPS } from "../../world/terrainAssets.js";
|
|
63
|
+
import { alpineRockColor, alpineRockTap, desertRockColor } from "../biomes.js";
|
|
64
|
+
// The ground's look, and every number in it, lives in this game: which surface covers which
|
|
65
|
+
// height and slope, how many metres one texture tile spans, and how the blend edges break. The
|
|
66
|
+
// evaluator in `packages/terrain` produces heights and eight material channels; none of that is a
|
|
67
|
+
// picture, and this file is the only place that decides what one looks like.
|
|
68
|
+
//
|
|
69
|
+
// Layer weights are read from the baked heightfield where it is drawn — its own world height, its
|
|
70
|
+
// own geometric normal, and the palette the bake painted — so a cliff is rock because it is steep,
|
|
71
|
+
// a beach is sand because it sits near the sea level the bake recorded, and snow settles only on
|
|
72
|
+
// the high flat ground. Nothing here resamples the terrain or adds a mask texture that could
|
|
73
|
+
// disagree with the geometry.
|
|
74
|
+
//
|
|
75
|
+
// Texture budget: six albedos + four normals + curvature + two shadow levels × two = 15/16.
|
|
76
|
+
import {
|
|
77
|
+
BIOMES,
|
|
78
|
+
type IBiome,
|
|
79
|
+
type IWorldBiome,
|
|
80
|
+
biomeWeights,
|
|
81
|
+
elevationBand,
|
|
82
|
+
measureBiome,
|
|
83
|
+
} from "../biomes.js";
|
|
84
|
+
import { LOOK } from "../lookKnobs.js";
|
|
85
|
+
import { loadSkyLight, skyIrradiance } from "../props/pack.js";
|
|
86
|
+
import { SUN_VECTOR } from "../sky/sky.js";
|
|
87
|
+
import {
|
|
88
|
+
type IBakedLake,
|
|
89
|
+
type IBakedRiver,
|
|
90
|
+
REFLECTED_LAYER,
|
|
91
|
+
surfaceHeights,
|
|
92
|
+
} from "../water/river.js";
|
|
93
|
+
import { WORLD } from "../worldNames.js";
|
|
94
|
+
import {
|
|
95
|
+
COLLAR_CURVE,
|
|
96
|
+
type IPackedMassif,
|
|
97
|
+
collarColors,
|
|
98
|
+
createHorizonCollar,
|
|
99
|
+
horizonSurroundings,
|
|
100
|
+
} from "./horizon.js";
|
|
101
|
+
|
|
102
|
+
export interface IBakedWorld {
|
|
103
|
+
size: number;
|
|
104
|
+
resolution: number;
|
|
105
|
+
heights: number[];
|
|
106
|
+
colors: number[];
|
|
107
|
+
erosion?: { flow: number[]; sediment: number[]; deposition: number[]; talus: number[] };
|
|
108
|
+
waterLevel: number | null;
|
|
109
|
+
lakes?: readonly IBakedLake[];
|
|
110
|
+
rivers?: readonly IBakedRiver[];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** One transport mask shared by the terrain splat and rock placement. */
|
|
114
|
+
export function depositAtIndex(data: Pick<IBakedWorld, "erosion">, vertex: number): number {
|
|
115
|
+
const sediment = data.erosion?.sediment[vertex] ?? 0;
|
|
116
|
+
const hydraulic = (data.erosion?.deposition[vertex] ?? 0) / (1 + Math.sqrt(sediment));
|
|
117
|
+
return Math.min(1, hydraulic * 0.25 + (data.erosion?.talus[vertex] ?? 0) * 0.3);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The ground's own curvature, sampled from a height texture, as the material's curvature term.
|
|
122
|
+
*
|
|
123
|
+
* This is the one signal the ground material cannot get any other way, and it is what separates a
|
|
124
|
+
* hillside with hollows in it from a hillside with a colour painted on it. The heightfield's own
|
|
125
|
+
* samples give a Laplacian: positive where the ground is concave (a hollow, a gully, the crease
|
|
126
|
+
* where two slopes meet), negative on a nose or a rib. Both the moss and the rock below read it, so
|
|
127
|
+
* moss grows in the crease at the bottom of a slope and rock shows on the rib at the top, which is
|
|
128
|
+
* where each actually grows — rather than both keysing off slope alone and covering a whole face.
|
|
129
|
+
*
|
|
130
|
+
* Built once in `createTerrain` from the same heights the geometry and the collider were built from,
|
|
131
|
+
* so it cannot disagree with either.
|
|
132
|
+
*/
|
|
133
|
+
export interface IGroundCurvature {
|
|
134
|
+
/** +1 in a hollow, -1 on a rib, ~0 on flat ground. */
|
|
135
|
+
readonly node: Node<"float">;
|
|
136
|
+
readonly texture: DataTexture;
|
|
137
|
+
readonly wetBank: Node<"float">;
|
|
138
|
+
readonly flow: Node<"float">;
|
|
139
|
+
readonly deposits: Node<"float">;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
interface ILayerMaps {
|
|
143
|
+
diffuse: Texture;
|
|
144
|
+
normal?: Texture;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The stand-in for a ground layer the biome does not paint. White, because every consumer multiplies
|
|
149
|
+
* it by that layer's mask weight, and a layer with no mask row has zero weight everywhere.
|
|
150
|
+
*/
|
|
151
|
+
const NEUTRAL_GROUND = new DataTexture(new Uint8Array([255, 255, 255, 255]), 1, 1);
|
|
152
|
+
NEUTRAL_GROUND.needsUpdate = true;
|
|
153
|
+
|
|
154
|
+
/** Props draw at render order 0 and the water at 2: the ground draws between them. */
|
|
155
|
+
const GROUND_RENDER_ORDER = 1;
|
|
156
|
+
/**
|
|
157
|
+
* The beach band, in metres above the bake's own sea level.
|
|
158
|
+
*
|
|
159
|
+
* Sand Beach is a pocket beach under a 3 m berm, and the lowland behind it is marsh and spruce at
|
|
160
|
+
* 2-5 m: sand is full to 1 m above the sea and gone by 4 m, so the beach reads and the lowland does
|
|
161
|
+
* not turn into a dune field. The bake paints sand below 3.5 m for the same reason.
|
|
162
|
+
*/
|
|
163
|
+
const SHORE = { beach: 4, wet: 1 };
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The wet band, and what it does to the sand.
|
|
167
|
+
*
|
|
168
|
+
* Sand that the sea has just left is darker and more saturated than the sand above it, because the
|
|
169
|
+
* water is still in the pores and the grains are closer together for it. The band is a metre and a half
|
|
170
|
+
* of height above the bake's own sea level and it feathers: a hard line at the high-water mark is
|
|
171
|
+
* exactly the "straight-edged white shape" the foam work removed from the water.
|
|
172
|
+
*
|
|
173
|
+
* It is also the reason the beach reads as a *beach* rather than as a tan field, and it is a colour
|
|
174
|
+
* the sand layer cannot supply on its own because it is a fact about the water's last reach, not about
|
|
175
|
+
* the sand.
|
|
176
|
+
*/
|
|
177
|
+
const WET_SAND = {
|
|
178
|
+
/** Height above sea level over which the band fades out, in metres. */
|
|
179
|
+
band: 2.8,
|
|
180
|
+
/** How much darker and how much more saturated the wet sand is. */
|
|
181
|
+
darken: vec3(0.38, 0.4, 0.42),
|
|
182
|
+
/** The grain the band adds: wet sand is smooth and packed, so the ripples flatten in it. */
|
|
183
|
+
calm: 0.45,
|
|
184
|
+
} as const;
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* What makes the ground grass rather than the dry autumn meadow the CC0 set photographs.
|
|
188
|
+
*
|
|
189
|
+
* Leafy Grass is a real meadow in October: brown thatch with green sprigs. A temperate starter
|
|
190
|
+
* wants June, so this trades red for green on that one layer and leaves the texture's own detail,
|
|
191
|
+
* its normal map and every other surface alone.
|
|
192
|
+
*/
|
|
193
|
+
const MEADOW = vec3(0.47, 0.72, 0.4);
|
|
194
|
+
/** A spruce forest's meadow: dark olive, not lawn green (Project Nature's spruce forest frames). */
|
|
195
|
+
const FOREST_MEADOW = vec3(0.4, 0.58, 0.27);
|
|
196
|
+
/** How much of the vegetation's sky light the ground receives; the vegetation's own is 1.13. */
|
|
197
|
+
const GROUND_SKY_LIGHT = 0.6;
|
|
198
|
+
/** Multiplier that turns meadow turf into shaded needle litter under a stand. */
|
|
199
|
+
const FOREST_FLOOR = vec3(0.4, 0.4, 0.27);
|
|
200
|
+
/**
|
|
201
|
+
* Bare earth between the stands, on top of the litter tint: a spruce floor's humus reads L* 29-37 at
|
|
202
|
+
* the walk pose (PRD-855), and the litter tint alone left it at 53, brighter than the turf beside it.
|
|
203
|
+
*/
|
|
204
|
+
const BARE_SOIL = vec3(0.4, 0.38, 0.37);
|
|
205
|
+
/**
|
|
206
|
+
* Albedo of the backdrop's closed spruce cover, crowns and the shade between them averaged: the hue of
|
|
207
|
+
* the playable forest's own crowns in the overview (linear 0.74 : 1 : 0.28), a little bluer, so the
|
|
208
|
+
* far slopes and the near stands read as one forest.
|
|
209
|
+
*/
|
|
210
|
+
const BACKDROP_SPRUCE = vec3(0.019, 0.028, 0.013);
|
|
211
|
+
/** Albedo of the meadow gaps in the backdrop's cover: dull olive turf, not lawn. */
|
|
212
|
+
const BACKDROP_CLEARING = vec3(0.042, 0.052, 0.022);
|
|
213
|
+
/**
|
|
214
|
+
* The surveyed surroundings carry no stands (the bake plants only the playable square), so past the
|
|
215
|
+
* square's edge the forest hillside is an open sunlit meadow: lawn turf, no needle-soil patches, and
|
|
216
|
+
* the vista's ground cover (`meadowCover`).
|
|
217
|
+
*/
|
|
218
|
+
export const OPEN_MEADOW = true;
|
|
219
|
+
|
|
220
|
+
/** Continuous stochastic warp: no hard cell boundaries in colour or normals. */
|
|
221
|
+
export function tiledUV(
|
|
222
|
+
key: LayerKey,
|
|
223
|
+
scale = 1,
|
|
224
|
+
plane: Node<"vec2"> = positionWorld.xz,
|
|
225
|
+
): Node<"vec2"> {
|
|
226
|
+
const base = plane.div(GROUND_TILE[key] * scale);
|
|
227
|
+
const warp = vec2(
|
|
228
|
+
bakedNoise(positionWorld.mul(0.025)),
|
|
229
|
+
bakedNoise(positionWorld.mul(0.025).add(vec3(17, 0, 29))),
|
|
230
|
+
);
|
|
231
|
+
return rotateUV(base, float(scale === 1 ? 0.38 : -0.73), vec2(0)).add(warp.mul(0.35));
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Shared floor sampling: two rotated scales, with continuous world-space blend weights. */
|
|
235
|
+
export function groundLayer(
|
|
236
|
+
source: Texture,
|
|
237
|
+
key: LayerKey,
|
|
238
|
+
plane = positionWorld.xz,
|
|
239
|
+
): Node<"vec4"> {
|
|
240
|
+
const patch = smoothstep(-0.35, 0.35, bakedNoise(positionWorld.mul(0.025)));
|
|
241
|
+
const far = smoothstep(10, 70, positionView.length());
|
|
242
|
+
const mid = mix(
|
|
243
|
+
texture(source, tiledUV(key, 1, plane)),
|
|
244
|
+
texture(source, tiledUV(key, 2.35, plane)),
|
|
245
|
+
mix(patch.mul(0.65), patch.mul(0.5).add(0.25), far),
|
|
246
|
+
);
|
|
247
|
+
const near = mix(
|
|
248
|
+
mid,
|
|
249
|
+
texture(source, tiledUV(key, 0.23, plane)),
|
|
250
|
+
oneMinus(smoothstep(5, 24, positionView.length())).mul(0.2),
|
|
251
|
+
);
|
|
252
|
+
return mix(
|
|
253
|
+
near,
|
|
254
|
+
texture(source, tiledUV(key, 18, plane)),
|
|
255
|
+
smoothstep(100, 380, positionView.length()).mul(0.72),
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Turf colour shared by the landscape and soil banked against a rock. */
|
|
260
|
+
export function groundTurf(
|
|
261
|
+
sampled: Node<"vec3">,
|
|
262
|
+
biome?: IBiome,
|
|
263
|
+
hollow: Node<"float"> = float(0),
|
|
264
|
+
/** 0..1: how far a forest fragment is into open meadow, which takes the lawn turf instead. */
|
|
265
|
+
open: Node<"float"> = float(0),
|
|
266
|
+
): Node<"vec3"> {
|
|
267
|
+
const distance = positionView.length();
|
|
268
|
+
const broad = bakedFractalNoise(vec3(positionWorld.x, 0, positionWorld.z).mul(0.006), 2);
|
|
269
|
+
const dry = smoothstep(-0.45, 0.45, broad.sub(hollow.mul(0.06)));
|
|
270
|
+
const tufts = bakedNoise(positionWorld.mul(0.75))
|
|
271
|
+
.mul(0.22)
|
|
272
|
+
.mul(oneMinus(smoothstep(45, 140, distance)))
|
|
273
|
+
.add(1);
|
|
274
|
+
const temperate = !biome || biome.lawnGround;
|
|
275
|
+
if (!temperate) {
|
|
276
|
+
const tint = vec3(...biome.grassTint);
|
|
277
|
+
if (!biome.crestTurf) return sampled.mul(tint);
|
|
278
|
+
const cover = vec3(0.075, 0.12, 0.028).mul(mix(0.91, 1.08, dry));
|
|
279
|
+
return mix(sampled.mul(tint), cover, smoothstep(65, 180, distance).mul(0.85));
|
|
280
|
+
}
|
|
281
|
+
const olive = biome?.oliveMeadow ?? false;
|
|
282
|
+
const lawn = mix(vec3(0.042, 0.072, 0.018), vec3(0.062, 0.102, 0.029), dry);
|
|
283
|
+
const field = (
|
|
284
|
+
olive ? mix(mix(vec3(0.045, 0.062, 0.022), vec3(0.06, 0.082, 0.03), dry), lawn, open) : lawn
|
|
285
|
+
)
|
|
286
|
+
.mul(tufts)
|
|
287
|
+
.mul(mix(0.92, 1.08, smoothstep(0.6, -0.6, hollow)));
|
|
288
|
+
// Open meadow: sward patches metres across, sunlit and deep, so a slope past the grass draw reach
|
|
289
|
+
// still reads as grass rather than one flat tone.
|
|
290
|
+
const sward = mix(
|
|
291
|
+
0.5,
|
|
292
|
+
0.95,
|
|
293
|
+
smoothstep(
|
|
294
|
+
-0.45,
|
|
295
|
+
0.45,
|
|
296
|
+
bakedFractalNoise(vec3(positionWorld.x, 0, positionWorld.z).mul(0.09), 2),
|
|
297
|
+
),
|
|
298
|
+
);
|
|
299
|
+
const turf = mix(
|
|
300
|
+
sampled.mul(olive ? mix(FOREST_MEADOW, MEADOW, open) : MEADOW),
|
|
301
|
+
field,
|
|
302
|
+
// Open meadow: the photograph's thatch (albedo 0.15) only carries detail under a dark lawn.
|
|
303
|
+
smoothstep(60, 220, distance)
|
|
304
|
+
.mul(0.55)
|
|
305
|
+
.max(open.mul(0.6)),
|
|
306
|
+
).mul(mix(float(1), sward, open));
|
|
307
|
+
return turf;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function rockLayer(
|
|
311
|
+
source: Texture,
|
|
312
|
+
plane: Node<"vec2">,
|
|
313
|
+
biome?: IBiome,
|
|
314
|
+
relief = false,
|
|
315
|
+
): Node<"vec4"> {
|
|
316
|
+
const patch = smoothstep(-0.35, 0.35, bakedNoise(positionWorld.mul(0.025)));
|
|
317
|
+
const tap = (scale: number, angle: number, offset: Node<"vec2">) => {
|
|
318
|
+
const sampled = texture(source, rotateUV(plane.div(scale), float(angle), vec2(0)).add(offset));
|
|
319
|
+
if (!relief) return sampled;
|
|
320
|
+
const tangent = rotateUV(sampled.xy.mul(2).sub(1), float(-angle), vec2(0));
|
|
321
|
+
return vec4(tangent, sampled.z.mul(2).sub(1), 1);
|
|
322
|
+
};
|
|
323
|
+
return biome?.crestRock
|
|
324
|
+
? alpineRockTap(source, plane, relief)
|
|
325
|
+
: biome?.mesaRock
|
|
326
|
+
? mix(
|
|
327
|
+
tap(11, 0.035, vec2(mx_noise_float(positionWorld.xz.mul(0.009)).mul(0.9), 0)),
|
|
328
|
+
tap(27, -0.045, vec2(0.37, 0.61)),
|
|
329
|
+
patch.mul(0.5).add(0.25),
|
|
330
|
+
)
|
|
331
|
+
: mix(tap(6.7, 0.57, vec2(0)), tap(11.3, -0.83, vec2(0.37, 0.61)), patch);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** The bedrock projection shared by the landscape and a rock's contact band. */
|
|
335
|
+
export function stoneAlbedo(
|
|
336
|
+
source: Texture,
|
|
337
|
+
biome?: IBiome,
|
|
338
|
+
normal = normalWorldGeometry,
|
|
339
|
+
): Node<"vec3"> {
|
|
340
|
+
const axis = abs(normal).pow(4);
|
|
341
|
+
const share = axis.div(axis.x.add(axis.y).add(axis.z));
|
|
342
|
+
const other = biome !== undefined && !biome.lawnGround;
|
|
343
|
+
const tap = (plane: Node<"vec2">) =>
|
|
344
|
+
other ? rockLayer(source, plane, biome).rgb : texture(source, tiledUV("rock", 1, plane)).rgb;
|
|
345
|
+
return share.x
|
|
346
|
+
.mul(tap(positionWorld.zy))
|
|
347
|
+
.add(share.y.mul(tap(positionWorld.xz)))
|
|
348
|
+
.add(share.z.mul(tap(positionWorld.xy)));
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export function stoneColor(sampled: Node<"vec3">, biome?: IBiome): Node<"vec3"> {
|
|
352
|
+
const look = biome ?? BIOMES.coastal;
|
|
353
|
+
if (look.crestRock) return alpineRockColor(sampled);
|
|
354
|
+
if (look.mesaRock) return desertRockColor(sampled);
|
|
355
|
+
const grey = dot(sampled, vec3(0.2126, 0.7152, 0.0722));
|
|
356
|
+
return mix(sampled, vec3(grey), look.stoneGrey).mul(vec3(...look.stoneTint));
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export function stoneRelief(
|
|
360
|
+
source: Texture,
|
|
361
|
+
biome?: IBiome,
|
|
362
|
+
normal = normalWorldGeometry,
|
|
363
|
+
): Node<"vec3"> {
|
|
364
|
+
if (!biome || biome.lawnGround) return triplanarRelief(source, "rock", 1, normal).tilt;
|
|
365
|
+
const axis = abs(normal).pow(4);
|
|
366
|
+
const share = axis.div(axis.x.add(axis.y).add(axis.z));
|
|
367
|
+
const x = rockLayer(source, positionWorld.zy, biome, true);
|
|
368
|
+
const y = rockLayer(source, positionWorld.xz, biome, true);
|
|
369
|
+
const z = rockLayer(source, positionWorld.xy, biome, true);
|
|
370
|
+
return share.x
|
|
371
|
+
.mul(vec3(0, x.y, x.x.mul(sign(normal.x))))
|
|
372
|
+
.add(share.y.mul(vec3(y.x, 0, y.y)))
|
|
373
|
+
.add(share.z.mul(vec3(z.x.mul(sign(normal.z)), z.y, 0)));
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* One normal map's two answers on the ground plane: the tilt it asks for, and how much of its own
|
|
378
|
+
* up-facing it kept.
|
|
379
|
+
*
|
|
380
|
+
* Only the two tangential channels go into the tilt. The surface direction is already the
|
|
381
|
+
* heightfield's own normal, and folding a second "out of the surface" term in here would cancel the
|
|
382
|
+
* relief this map exists to add. On the ground plane the map's red runs along +x and its green
|
|
383
|
+
* along +z. The third channel is not thrown away: it is the crevice term below.
|
|
384
|
+
*/
|
|
385
|
+
interface IRelief {
|
|
386
|
+
/** How much this texel faces up, 0..1. A crevice is a texel that does not. */
|
|
387
|
+
readonly crevice: Node<"float">;
|
|
388
|
+
readonly tilt: Node<"vec3">;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function planarRelief(source: Texture, uv: Node<"vec2">, strength: number, angle = 0.38): IRelief {
|
|
392
|
+
const sample = texture(source, uv);
|
|
393
|
+
const tangent = rotateUV(sample.xy.mul(2).sub(1), float(-angle), vec2(0));
|
|
394
|
+
return {
|
|
395
|
+
crevice: sample.z,
|
|
396
|
+
tilt: vec3(tangent.x, 0, tangent.y).mul(strength),
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* The same relief projected on all three axes — this is what keeps a cliff from smearing.
|
|
402
|
+
*
|
|
403
|
+
* Each projection owns a different pair of world axes, so one map tilts the surface along a
|
|
404
|
+
* different direction per axis and the three are recombined by the surface's own blend weights. The
|
|
405
|
+
* crevice term is the one projection, because a crease is a crease whichever way the cliff faces.
|
|
406
|
+
*
|
|
407
|
+
* `roughnessScale` widens the sample spacing on the wall projections. A cliff nine metres of strata
|
|
408
|
+
* across reads a nine-metre map at a one-metre scale as an unreadable fine band along its own
|
|
409
|
+
* tangential direction, which is what a wall projection always does without this.
|
|
410
|
+
*/
|
|
411
|
+
function triplanarRelief(
|
|
412
|
+
source: Texture,
|
|
413
|
+
key: LayerKey,
|
|
414
|
+
roughnessScale = 1,
|
|
415
|
+
normal = normalWorldGeometry,
|
|
416
|
+
): IRelief {
|
|
417
|
+
const tap = (plane: Node<"vec2">) => {
|
|
418
|
+
const sampled = texture(source, tiledUV(key, roughnessScale, plane));
|
|
419
|
+
const tangent = rotateUV(
|
|
420
|
+
sampled.xy.mul(2).sub(1),
|
|
421
|
+
float(roughnessScale === 1 ? -0.38 : 0.73),
|
|
422
|
+
vec2(0),
|
|
423
|
+
);
|
|
424
|
+
return vec3(tangent, sampled.z.mul(2).sub(1));
|
|
425
|
+
};
|
|
426
|
+
const x = tap(positionWorld.zy);
|
|
427
|
+
const y = tap(positionWorld.xz);
|
|
428
|
+
const z = tap(positionWorld.xy);
|
|
429
|
+
const axis = abs(normal).pow(4);
|
|
430
|
+
const weight = axis.div(axis.x.add(axis.y).add(axis.z));
|
|
431
|
+
return {
|
|
432
|
+
crevice: float(1),
|
|
433
|
+
tilt: weight.x
|
|
434
|
+
.mul(vec3(0, x.y, x.x.mul(sign(normal.x))))
|
|
435
|
+
.add(weight.y.mul(vec3(y.x, 0, y.y)))
|
|
436
|
+
.add(weight.z.mul(vec3(z.x.mul(sign(normal.z)), z.y, 0))),
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* How steep a face has to be before a layer stops being projected onto the ground plane.
|
|
442
|
+
*
|
|
443
|
+
* A planar projection is exact on flat ground and wrong everywhere else, and it is *very* wrong on a
|
|
444
|
+
* slope: the map is stretched along the slope's own direction by the reciprocal of the cosine, so a
|
|
445
|
+
* thirty-degree hillside turns a 2.6 m grass tile into a five-metre smear. Thirty-five degrees is
|
|
446
|
+
* where that smear stops being invisible — below it the slope is gentle enough that the detail still
|
|
447
|
+
* reads, above it the triplanar takes over.
|
|
448
|
+
*/
|
|
449
|
+
// Slope is 1 − cos(angle): 0.18 is 35°, not the sine of 35°.
|
|
450
|
+
const TRIPLANAR_SLOPE = 0.18;
|
|
451
|
+
|
|
452
|
+
/** Wind ripples share one world-space phase; changing normals must not bend the phase. */
|
|
453
|
+
const RIPPLE = {
|
|
454
|
+
/** Crests per metre across the fall line. Real ripples are 5-20 cm apart; these read at 25 cm. */
|
|
455
|
+
frequency: 4,
|
|
456
|
+
/** How far the crests tilt the surface, and how much their spacing wanders. */
|
|
457
|
+
strength: 0.065,
|
|
458
|
+
wander: 1.4,
|
|
459
|
+
} as const;
|
|
460
|
+
|
|
461
|
+
function sandRipples(direction?: Node<"vec2">): Node<"vec3"> {
|
|
462
|
+
// A coherent wind direction avoids radial stripes where a beach normal changes.
|
|
463
|
+
const fall = direction ?? vec2(0.83, 0.56);
|
|
464
|
+
const downhill = fall.length().max(float(0.0001));
|
|
465
|
+
const across = vec2(fall.x.div(downhill), fall.y.div(downhill));
|
|
466
|
+
// Distance across the wind, in metres; slow noise breaks a perfect comb.
|
|
467
|
+
const phase = positionWorld.x
|
|
468
|
+
.mul(across.x)
|
|
469
|
+
.add(positionWorld.z.mul(across.y))
|
|
470
|
+
.mul(RIPPLE.frequency * Math.PI * 2);
|
|
471
|
+
const wobble = bakedNoise(positionWorld.mul(0.09)).mul(RIPPLE.wander);
|
|
472
|
+
const crest = phase.add(wobble).sin();
|
|
473
|
+
// Subtle relief disappears before the crests become smaller than a pixel.
|
|
474
|
+
return vec3(across.x, 0, across.y)
|
|
475
|
+
.mul(crest)
|
|
476
|
+
.mul(RIPPLE.strength)
|
|
477
|
+
.mul(oneMinus(smoothstep(8, 35, positionView.length())));
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* One albedo map projected on all three axes, recombined by the surface's own normal.
|
|
482
|
+
*
|
|
483
|
+
* This is the same idea as {@link triplanarRelief} on the colour side, and it is what a hillside
|
|
484
|
+
* needs: a planar projection stretches the map by the reciprocal of the cosine of the slope, so the
|
|
485
|
+
* further off flat the ground gets the more the detail smears along the fall line. Sampling the three
|
|
486
|
+
* world-axis planes and blending by `|n|` costs two extra taps and buys a hillside with detail on it
|
|
487
|
+
* at every angle.
|
|
488
|
+
*
|
|
489
|
+
* The wall projections sample at a coarser spacing than the floor one, because a vertical face has no
|
|
490
|
+
* floor projection to fall back on and at full density a nine-metre tile turns into fine stripes down
|
|
491
|
+
* its own length.
|
|
492
|
+
*/
|
|
493
|
+
function triplanarAlbedo(source: Texture, key: LayerKey, scale = 1): Node<"vec4"> {
|
|
494
|
+
const axis = abs(normalWorldGeometry).pow(4);
|
|
495
|
+
const weight = axis.div(axis.x.add(axis.y).add(axis.z));
|
|
496
|
+
const x = texture(source, tiledUV(key, scale, positionWorld.zy));
|
|
497
|
+
const y = texture(source, tiledUV(key, scale));
|
|
498
|
+
const z = texture(source, tiledUV(key, scale, positionWorld.xy));
|
|
499
|
+
return weight.x.mul(x).add(weight.y.mul(y)).add(weight.z.mul(z)) as Node<"vec4">;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* A third of a metre of ground grain, as a tilt in world space.
|
|
504
|
+
*
|
|
505
|
+
* The last scale of detail on the ground is not a texture: it is the difference between a surface
|
|
506
|
+
* that is *smooth* and one that is *smooth at a hundred metres*. One fractal noise field, differenced
|
|
507
|
+
* into a gradient, gives it for the price of four instructions and no sampler at all — and the ground
|
|
508
|
+
* material is already at eleven sampled textures, so a twelfth map is not available however much this
|
|
509
|
+
* detail wants it.
|
|
510
|
+
*
|
|
511
|
+
* It is deliberately faint. This is a third of a metre of clumping between the blades, not the blades
|
|
512
|
+
* themselves, and the capture that showed a speckled meadow is what happens when it is not: a normal
|
|
513
|
+
* perturbation this high-frequency reads as static underfoot and as noise at any distance, and it
|
|
514
|
+
* costs the eye the thing it was added for.
|
|
515
|
+
*/
|
|
516
|
+
function microGrain(): Node<"vec3"> {
|
|
517
|
+
const field = (p: Node<"vec3">) => bakedFractalNoise(p.mul(2.6), 2, 2, 0.5);
|
|
518
|
+
const here = field(positionWorld);
|
|
519
|
+
const step = float(0.22);
|
|
520
|
+
const dx = here.sub(field(positionWorld.add(vec3(step, 0, 0))));
|
|
521
|
+
const dz = here.sub(field(positionWorld.add(vec3(0, 0, step))));
|
|
522
|
+
return vec3(dx, 0, dz).div(step).mul(0.06);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/** Crown radius in metres per unit of placement scale, for the trees that drop needle litter. */
|
|
526
|
+
/**
|
|
527
|
+
* The crown radius that shades the ground under each tree, metres. The bake paints the canopy cover
|
|
528
|
+
* map from these rows; a species absent here shades nothing. Alpine crowns are smaller than the
|
|
529
|
+
* forest's spruce: the trees are shorter and the light is flatter.
|
|
530
|
+
*/
|
|
531
|
+
export const CANOPY_CROWN: Readonly<Record<string, number>> = {
|
|
532
|
+
spruce: 6,
|
|
533
|
+
scotspine: 5,
|
|
534
|
+
blackspruce: 4.5,
|
|
535
|
+
sapling: 2.5,
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Paint each placed tree's crown into the terrain's `canopy` vertex attribute, so the ground under a
|
|
540
|
+
* stand reads as forest floor rather than meadow turf. Vertices are row-major Z then X, centred at
|
|
541
|
+
* zero, at the heightfield's own spacing.
|
|
542
|
+
*/
|
|
543
|
+
export function paintCanopy(
|
|
544
|
+
mesh: Mesh,
|
|
545
|
+
data: Pick<IBakedWorld, "size" | "resolution">,
|
|
546
|
+
placements: readonly {
|
|
547
|
+
readonly asset: string;
|
|
548
|
+
readonly position: readonly number[];
|
|
549
|
+
readonly scale: number;
|
|
550
|
+
}[],
|
|
551
|
+
): void {
|
|
552
|
+
const cover = mesh.geometry.getAttribute("canopy");
|
|
553
|
+
if (!(cover instanceof Float32BufferAttribute) || data.resolution < 2) return;
|
|
554
|
+
paintCanopyCover(cover.array as Float32Array, data, placements, CANOPY_CROWN);
|
|
555
|
+
cover.needsUpdate = true;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* The ground material: one lit surface whose colour is a blend of six PBR layers, blended in weight
|
|
560
|
+
* order outwards from the base surface.
|
|
561
|
+
*/
|
|
562
|
+
export async function createGroundMaterial(
|
|
563
|
+
data: IBakedWorld,
|
|
564
|
+
assets: IAssetLoader,
|
|
565
|
+
curvature: IGroundCurvature,
|
|
566
|
+
biome?: IBiome,
|
|
567
|
+
/**
|
|
568
|
+
* A streamed tile carries no painted `canopy` or baked `sky`: it lies past the trees and keeps the
|
|
569
|
+
* whole sky, exactly as the collar's own attributes say.
|
|
570
|
+
*/
|
|
571
|
+
streamed = false,
|
|
572
|
+
): Promise<MeshStandardNodeMaterial> {
|
|
573
|
+
const canopyCover = (): Node<"float"> =>
|
|
574
|
+
streamed ? float(0) : attribute<"float">("canopy", "float");
|
|
575
|
+
const skyShare = (): Node<"float"> => (streamed ? float(1) : attribute<"float">("sky", "float"));
|
|
576
|
+
const measured =
|
|
577
|
+
biome && "heightBands" in biome
|
|
578
|
+
? (biome as IWorldBiome)
|
|
579
|
+
: measureBiome(biome ?? BIOMES.coastal, data.heights);
|
|
580
|
+
const look = biome ?? BIOMES.coastal;
|
|
581
|
+
const maps: Partial<Record<LayerKey, ILayerMaps>> = {};
|
|
582
|
+
const material = new MeshStandardNodeMaterial({ metalness: 0, roughness: 0.94 });
|
|
583
|
+
material.userData.biome = look.world;
|
|
584
|
+
material.userData.erosion = data.erosion !== undefined;
|
|
585
|
+
const held = new Set<Texture>([curvature.texture]);
|
|
586
|
+
// The forest's alpine backdrop shades from the unfiltered survey (owner, 2026-11-04: "mountain
|
|
587
|
+
// tops textures and heightmaps suck"); the sixteenth binding, so only this world takes it.
|
|
588
|
+
const collar = look.surveyCollar ? buildCollarRelief(look.world) : undefined;
|
|
589
|
+
if (collar) held.add(collar.texture);
|
|
590
|
+
// A biome carries the layers it paints, and this material knows the forest's eight. A world with
|
|
591
|
+
// fewer (the alpine kit writes rock, dirt and snow) has no mask weight anywhere for the rest, so a
|
|
592
|
+
// shared white tap stands in: one texture, no colour, and the world still draws.
|
|
593
|
+
const warned = new Set<LayerKey>();
|
|
594
|
+
const layer = (key: LayerKey): ILayerMaps => {
|
|
595
|
+
const found = maps[key];
|
|
596
|
+
if (found?.diffuse) return found;
|
|
597
|
+
if (!warned.has(key)) {
|
|
598
|
+
warned.add(key);
|
|
599
|
+
console.warn(
|
|
600
|
+
`TN_GROUND_MAP_MISSING Ground layer '${key}' has no diffuse texture; its mask weights are zero, so it draws as white`,
|
|
601
|
+
);
|
|
602
|
+
}
|
|
603
|
+
return { diffuse: NEUTRAL_GROUND };
|
|
604
|
+
};
|
|
605
|
+
|
|
606
|
+
const otherBiome = biome !== undefined && !biome.lawnGround;
|
|
607
|
+
const macro = bakedFractalNoise(vec3(positionWorld.x, 0, positionWorld.z).mul(0.014), 3);
|
|
608
|
+
const mottling = bakedNoise(vec3(positionWorld.x, 0, positionWorld.z).mul(0.045));
|
|
609
|
+
|
|
610
|
+
// --- where each surface sits -------------------------------------------------------------
|
|
611
|
+
const slope = normalWorldGeometry.y.abs().oneMinus();
|
|
612
|
+
const breakUp = bakedFractalNoise(positionWorld.mul(0.05), 3);
|
|
613
|
+
const steep = clamp(slope.add(breakUp.mul(0.05)), 0, 1);
|
|
614
|
+
// +1 in a hollow, -1 on a rib. The two ends of the ground's own shape, which slope cannot tell
|
|
615
|
+
// apart: a gully and a nose are both steep, and they are not the same surface.
|
|
616
|
+
const outside = max(abs(positionWorld.x), abs(positionWorld.z));
|
|
617
|
+
const hollow = curvature.node.mul(
|
|
618
|
+
oneMinus(smoothstep(data.size / 2, data.size / 2 + 28, outside)),
|
|
619
|
+
);
|
|
620
|
+
// Forest only: the open meadow past the square's edge (`OPEN_MEADOW`), eased in over 40 m.
|
|
621
|
+
const open =
|
|
622
|
+
OPEN_MEADOW && look.openMeadow
|
|
623
|
+
? smoothstep(data.size / 2, data.size / 2 + 40, outside)
|
|
624
|
+
: float(0);
|
|
625
|
+
// Material placement follows the actual landform, never the bake's brown blob palette.
|
|
626
|
+
const drainage = smoothstep(0.12, 0.65, curvature.flow);
|
|
627
|
+
const deposits = smoothstep(0.08, 0.6, curvature.deposits);
|
|
628
|
+
const scour = drainage.mul(smoothstep(0.035, 0.18, slope));
|
|
629
|
+
// The material's transport terms read the erosion bake through the curvature texture.
|
|
630
|
+
const flow = curvature.flow;
|
|
631
|
+
const sediment = deposits;
|
|
632
|
+
const rib = smoothstep(-0.02, -0.45, hollow);
|
|
633
|
+
const alpine = elevationBand(
|
|
634
|
+
measured,
|
|
635
|
+
measured.heightBands.alpine,
|
|
636
|
+
positionWorld.y.add(breakUp.mul(5)),
|
|
637
|
+
);
|
|
638
|
+
// No recorded sea level means an inland world, and an inland world has no beach.
|
|
639
|
+
const sand =
|
|
640
|
+
data.waterLevel === null
|
|
641
|
+
? float(0)
|
|
642
|
+
: smoothstep(float(SHORE.beach), float(SHORE.wet), positionWorld.y.sub(data.waterLevel)).mul(
|
|
643
|
+
smoothstep(0.34, 0.1, steep),
|
|
644
|
+
);
|
|
645
|
+
/**
|
|
646
|
+
* How wet the sand under this fragment is, 0..1: one at the sea surface, gone `WET_SAND.band`
|
|
647
|
+
* metres above it.
|
|
648
|
+
*
|
|
649
|
+
* The band is broken by the same slow noise the litter uses so the high-water mark is a ragged edge
|
|
650
|
+
* rather than a contour line — the same reason the foam's outer edge wanders in `ocean.ts`.
|
|
651
|
+
*/
|
|
652
|
+
const wetness = (): Node<"float"> => {
|
|
653
|
+
if (data.waterLevel === null) return float(0);
|
|
654
|
+
const above = positionWorld.y.sub(float(data.waterLevel));
|
|
655
|
+
const wander = bakedFractalNoise(positionWorld.mul(0.06), 3).mul(0.55).add(0.5);
|
|
656
|
+
return float(1).sub(
|
|
657
|
+
smoothstep(float(0), float(otherBiome ? 1.6 : WET_SAND.band), above.div(wander)),
|
|
658
|
+
);
|
|
659
|
+
};
|
|
660
|
+
const weights: Record<LayerKey, Node<"float">> = {
|
|
661
|
+
// The bake painted its beach in the same red-over-green as its dirt, so the height rule above
|
|
662
|
+
// has to be the one that speaks for the shore; painted dirt steps aside where it does.
|
|
663
|
+
dirt: max(
|
|
664
|
+
drainage.mul(0.18).max(sediment.mul(0.5)),
|
|
665
|
+
smoothstep(0.025, 0.18, steep)
|
|
666
|
+
.mul(smoothstep(-0.12, 0.32, breakUp))
|
|
667
|
+
.mul(0.48),
|
|
668
|
+
).mul(sand.oneMinus()),
|
|
669
|
+
grass: float(1),
|
|
670
|
+
moss: drainage.mul(smoothstep(0.02, 0.14, steep)).mul(0.24),
|
|
671
|
+
sand,
|
|
672
|
+
// Rock shows where the ground is steep *and* convex — the nose of a rib, the face of a scarp. On a
|
|
673
|
+
// concave slope the ground is covered by what has fallen into it, which is the moss above, so the
|
|
674
|
+
// steepness term is scaled down in a hollow rather than replaced: bare rock still shows at a
|
|
675
|
+
// scarp's foot where the ground is steep and the debris has not arrived yet.
|
|
676
|
+
// Soil holds on ordinary hills; only scarps expose bedrock (34–48 degrees).
|
|
677
|
+
rock: smoothstep(0.17, 0.33, steep.add(rib.mul(0.04)).add(breakUp.mul(0.025)))
|
|
678
|
+
.mul(mix(0.65, 1, elevationBand(measured, measured.heightBands.rock)))
|
|
679
|
+
.mul(oneMinus(smoothstep(0.15, 0.8, hollow).mul(0.25))),
|
|
680
|
+
snow: elevationBand(measured, measured.snowBand).mul(
|
|
681
|
+
smoothstep(measured.snowFractions[2], 0.06, steep),
|
|
682
|
+
),
|
|
683
|
+
};
|
|
684
|
+
|
|
685
|
+
Object.assign(weights, biomeWeights(measured, steep, hollow, breakUp));
|
|
686
|
+
// Needle litter and crown shade under the placed stands, as a multiplicative floor tint.
|
|
687
|
+
let forestFloor: Node<"float"> = float(0);
|
|
688
|
+
let bareSoil: Node<"float"> = float(0);
|
|
689
|
+
// Transport, slope and shelter choose the splat; noise only frays the material's edge.
|
|
690
|
+
weights.rock = max(weights.rock, scour.mul(0.9)).mul(oneMinus(deposits.mul(0.22)));
|
|
691
|
+
if (look.crestRock) weights.rock = max(weights.rock, smoothstep(0.08, 0.22, slope));
|
|
692
|
+
weights.dirt = max(weights.dirt.mul(0.45), deposits.mul(0.85))
|
|
693
|
+
.mul(oneMinus(smoothstep(0.22, 0.4, slope)))
|
|
694
|
+
.mul(sand.oneMinus());
|
|
695
|
+
if (look.mossGround) {
|
|
696
|
+
weights.moss = hollow
|
|
697
|
+
.max(0)
|
|
698
|
+
.mul(oneMinus(drainage))
|
|
699
|
+
.mul(smoothstep(0.12, 0.025, slope))
|
|
700
|
+
.mul(oneMinus(weights.snow))
|
|
701
|
+
.mul(0.7);
|
|
702
|
+
}
|
|
703
|
+
if (!otherBiome) {
|
|
704
|
+
// Ordinary forest hills carry turf; reserve exposed soil for drainage banks.
|
|
705
|
+
if (look.woodedGround)
|
|
706
|
+
weights.dirt = drainage.mul(0.18).max(sediment.mul(0.5)).mul(sand.oneMinus());
|
|
707
|
+
if (look.woodedGround) {
|
|
708
|
+
// Needle litter lies under the trees that were actually placed (`paintCanopy`), frayed by noise
|
|
709
|
+
// at its edge. The old sine-sum stand mask no longer matched the value-noise scatter, and its
|
|
710
|
+
// 38 m fade left every stand standing on meadow turf from any height.
|
|
711
|
+
const litter = smoothstep(0, 0.6, canopyCover())
|
|
712
|
+
.mul(mix(0.72, 1, smoothstep(-0.5, 0.5, bakedNoise(positionWorld.mul(2.2)))))
|
|
713
|
+
.mul(oneMinus(smoothstep(300, 600, positionView.length())))
|
|
714
|
+
.mul(0.85);
|
|
715
|
+
weights.dirt = weights.dirt.max(litter.mul(0.25));
|
|
716
|
+
forestFloor = litter;
|
|
717
|
+
// The bare patches are the wood's own: a ground drawn with no look keeps its litter only.
|
|
718
|
+
if (biome?.woodedGround) {
|
|
719
|
+
// Bare earth breaks the meadow in broad patches, as on a real spruce slope: broad noise
|
|
720
|
+
// picks them, finer noise frays their edge, a hollow keeps its turf. They fade out by 350 m
|
|
721
|
+
// so a far slope does not print the noise as camouflage.
|
|
722
|
+
const bare = smoothstep(
|
|
723
|
+
0.16,
|
|
724
|
+
0.5,
|
|
725
|
+
macro.add(mottling.mul(0.85)).sub(hollow.max(0).mul(0.2)),
|
|
726
|
+
)
|
|
727
|
+
.mul(oneMinus(smoothstep(150, 350, positionView.length())))
|
|
728
|
+
.mul(oneMinus(open));
|
|
729
|
+
weights.dirt = weights.dirt.max(bare.mul(0.8));
|
|
730
|
+
// The soil takes the litter's warm needle tint in full, not the dirt photograph's grey gravel.
|
|
731
|
+
forestFloor = forestFloor.max(bare);
|
|
732
|
+
bareSoil = bare;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
// Coastal soil patches remain; the forest meadow keeps turf between distant blade clusters.
|
|
736
|
+
if (look.shoreGround) {
|
|
737
|
+
const dry = smoothstep(0.14, 0.42, macro.add(mottling.mul(0.85)).sub(hollow.mul(0.12)));
|
|
738
|
+
weights.dirt = max(weights.dirt, dry.mul(0.7)).mul(sand.oneMinus());
|
|
739
|
+
}
|
|
740
|
+
weights.moss = hollow
|
|
741
|
+
.max(0)
|
|
742
|
+
.mul(oneMinus(drainage))
|
|
743
|
+
.mul(smoothstep(0.15, 0.025, slope))
|
|
744
|
+
.mul(0.5);
|
|
745
|
+
if (biome?.woodedGround) {
|
|
746
|
+
// A spruce floor is moss and needles, not turf and not bare sand: under the placed crowns and
|
|
747
|
+
// on the wood's bare patches the moss layer grows in broad cushions, frayed by a finer noise,
|
|
748
|
+
// and the needle soil shows between them. In the low sun the old floor drew as pale sand.
|
|
749
|
+
const cushions = smoothstep(
|
|
750
|
+
-0.25,
|
|
751
|
+
0.3,
|
|
752
|
+
bakedNoise(vec3(positionWorld.x, 0, positionWorld.z).mul(0.35)).add(mottling.mul(0.5)),
|
|
753
|
+
);
|
|
754
|
+
weights.moss = weights.moss.max(forestFloor.mul(cushions).mul(0.85));
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
// --- how each surface looks -------------------------------------------------------------
|
|
759
|
+
// Apply observed deposition after biome placement so dry/snow-world rules cannot erase it.
|
|
760
|
+
weights.dirt = weights.dirt.max(sediment.mul(0.5)).max(flow.mul(0.28)).mul(sand.oneMinus());
|
|
761
|
+
// The layers that cover most of a meadow take a second, larger scale faded in with distance: one
|
|
762
|
+
// tile under the player's feet, a coarser one near the horizon, and no single lattice for the eye
|
|
763
|
+
// to find anywhere between.
|
|
764
|
+
const far = smoothstep(float(10), float(70), positionView.length());
|
|
765
|
+
const patch = smoothstep(-0.35, 0.35, bakedNoise(positionWorld.mul(0.025)));
|
|
766
|
+
const tileBlend = mix(patch.mul(0.65), patch.mul(0.5).add(0.25), far);
|
|
767
|
+
// How much of a layer is projected onto the ground plane and how much onto the walls. Zero on the
|
|
768
|
+
// meadow, one on a cliff, and eased across the thirty-five degrees where a planar projection stops
|
|
769
|
+
// being an approximation and becomes a smear.
|
|
770
|
+
const planarShare = float(1).sub(
|
|
771
|
+
smoothstep(float(TRIPLANAR_SLOPE * 0.55), float(TRIPLANAR_SLOPE * 1.6), steep),
|
|
772
|
+
);
|
|
773
|
+
|
|
774
|
+
/**
|
|
775
|
+
* One layer's albedo: the ground plane near the camera, the walls on a slope, and a coarser tile
|
|
776
|
+
* faded in with distance so the horizon is not a field of half-resolved texels.
|
|
777
|
+
*
|
|
778
|
+
* The two planar scales are what the last captures' "flat green tint" was not: a single scale at
|
|
779
|
+
* 2.6 m is a dozen pixels wide from fifty metres away, and the middle distance goes to a wash. The
|
|
780
|
+
* two are blended rather than switched so the crossover has no seam to find.
|
|
781
|
+
*/
|
|
782
|
+
|
|
783
|
+
const albedoOf = (key: LayerKey): Node<"vec4"> => {
|
|
784
|
+
const { diffuse } = layer(key);
|
|
785
|
+
const flat = groundLayer(diffuse, key);
|
|
786
|
+
if (look.mossGround && (key === "grass" || key === "dirt" || key === "moss")) {
|
|
787
|
+
// The same rotated two-scale floor and walls continue across the resident edge.
|
|
788
|
+
const axis = abs(normalWorldGeometry).pow(4);
|
|
789
|
+
const share = axis.div(axis.x.add(axis.y).add(axis.z));
|
|
790
|
+
const tap = (plane: Node<"vec2">) => groundLayer(diffuse, key, plane);
|
|
791
|
+
const sampled = share.x
|
|
792
|
+
.mul(tap(positionWorld.zy))
|
|
793
|
+
.add(share.y.mul(flat))
|
|
794
|
+
.add(share.z.mul(tap(positionWorld.xy))) as Node<"vec4">;
|
|
795
|
+
return key === "dirt"
|
|
796
|
+
? vec4(sampled.rgb.mul(vec3(0.7, 0.58, 0.42)), sampled.a)
|
|
797
|
+
: key === "moss"
|
|
798
|
+
? vec4(sampled.rgb.mul(vec3(1.08, 1.07, 0.65)), sampled.a)
|
|
799
|
+
: sampled;
|
|
800
|
+
}
|
|
801
|
+
const walls =
|
|
802
|
+
key === "rock" ? vec4(stoneAlbedo(diffuse, biome), 1) : triplanarAlbedo(diffuse, key);
|
|
803
|
+
const blended = key === "rock" ? walls : mix(walls, flat, planarShare);
|
|
804
|
+
if (key === "snow" && biome) return vec4(blended.rgb.mul(vec3(...biome.snowTint)), blended.a);
|
|
805
|
+
// The wet band, applied to the sand only. It belongs here rather than in the layer blend below
|
|
806
|
+
// because it is a *height* fact about the water's last reach, not a weight another surface has.
|
|
807
|
+
if (key === "rock") {
|
|
808
|
+
const stone = stoneColor(blended.rgb, biome);
|
|
809
|
+
// Resolved stone underfoot; broad weathering once the photograph's repeats become visible.
|
|
810
|
+
const weathering = bakedFractalNoise(positionWorld.mul(vec3(0.005, 0.011, 0.005)), 2)
|
|
811
|
+
.mul(0.18)
|
|
812
|
+
.add(1);
|
|
813
|
+
const large = triplanarAlbedo(diffuse, key, 16).rgb;
|
|
814
|
+
const massif = look.crestRock
|
|
815
|
+
? alpineRockColor(large)
|
|
816
|
+
: look.mesaRock
|
|
817
|
+
? desertRockColor(large)
|
|
818
|
+
: large.mul(vec3(...look.stoneTint));
|
|
819
|
+
const distant = mix(
|
|
820
|
+
vec3(...(look.groundDistantStone ?? look.distantStone)),
|
|
821
|
+
massif,
|
|
822
|
+
0.65,
|
|
823
|
+
).mul(weathering);
|
|
824
|
+
return vec4(
|
|
825
|
+
mix(stone, distant, smoothstep(35, 220, positionView.length()).mul(look.distantStoneShare)),
|
|
826
|
+
blended.a,
|
|
827
|
+
);
|
|
828
|
+
}
|
|
829
|
+
if (key !== "sand" || data.waterLevel === null) return blended;
|
|
830
|
+
const wet = wetness();
|
|
831
|
+
const wrack = otherBiome
|
|
832
|
+
? float(0)
|
|
833
|
+
: oneMinus(
|
|
834
|
+
smoothstep(
|
|
835
|
+
0.07,
|
|
836
|
+
0.21,
|
|
837
|
+
positionWorld.y
|
|
838
|
+
.sub(2.1)
|
|
839
|
+
.sub(bakedNoise(positionWorld.mul(0.05)).mul(0.5))
|
|
840
|
+
.abs(),
|
|
841
|
+
),
|
|
842
|
+
)
|
|
843
|
+
.mul(smoothstep(-0.1, 0.15, bakedNoise(positionWorld.mul(0.65))))
|
|
844
|
+
// Subpixel wrack fragments read as brown loops from the overview.
|
|
845
|
+
.mul(oneMinus(smoothstep(60, 120, positionView.length())));
|
|
846
|
+
const sandColor = otherBiome
|
|
847
|
+
? blended.rgb
|
|
848
|
+
: mix(blended.rgb, vec3(dot(blended.rgb, vec3(0.2126, 0.7152, 0.0722))), 0.55).mul(
|
|
849
|
+
vec3(1.95, 1.82, 1.55),
|
|
850
|
+
);
|
|
851
|
+
return vec4(
|
|
852
|
+
sandColor
|
|
853
|
+
.mul(mix(vec3(1), vec3(0.45, 0.37, 0.24), wrack))
|
|
854
|
+
.mul(mix(float(1), otherBiome ? vec3(0.52, 0.5, 0.55) : WET_SAND.darken, wet)),
|
|
855
|
+
blended.a,
|
|
856
|
+
);
|
|
857
|
+
};
|
|
858
|
+
|
|
859
|
+
// The relief takes the same scales as the colour, or the ground keeps its detail underfoot and goes
|
|
860
|
+
// flat past twenty metres.
|
|
861
|
+
const reliefOf = (key: LayerKey, strength: number): IRelief => {
|
|
862
|
+
const source = layer(key).normal;
|
|
863
|
+
if (source === undefined)
|
|
864
|
+
return {
|
|
865
|
+
crevice: float(1),
|
|
866
|
+
tilt: key === "sand" ? sandRipples() : key === "snow" ? microGrain().mul(0.4) : vec3(0),
|
|
867
|
+
};
|
|
868
|
+
if (key === "rock") {
|
|
869
|
+
const walls = { crevice: float(1), tilt: stoneRelief(source, biome) };
|
|
870
|
+
return {
|
|
871
|
+
crevice: walls.crevice,
|
|
872
|
+
tilt: mix(
|
|
873
|
+
walls.tilt.mul(strength),
|
|
874
|
+
triplanarRelief(source, key, 12).tilt.mul(strength * 0.45),
|
|
875
|
+
smoothstep(80, 320, positionView.length()),
|
|
876
|
+
),
|
|
877
|
+
};
|
|
878
|
+
}
|
|
879
|
+
const near = planarRelief(source, tiledUV(key), strength);
|
|
880
|
+
const coarse = planarRelief(source, tiledUV(key, 2.35), strength * 0.6, -0.73);
|
|
881
|
+
const flat: IRelief = {
|
|
882
|
+
crevice: mix(near.crevice, coarse.crevice, tileBlend),
|
|
883
|
+
tilt: mix(near.tilt, coarse.tilt, tileBlend)
|
|
884
|
+
.add(
|
|
885
|
+
planarRelief(source, tiledUV(key, 0.18), strength * 0.42, -0.73).tilt.mul(
|
|
886
|
+
oneMinus(smoothstep(6, 28, positionView.length())),
|
|
887
|
+
),
|
|
888
|
+
)
|
|
889
|
+
.mul(mix(1, 0.35, smoothstep(60, 240, positionView.length()))),
|
|
890
|
+
};
|
|
891
|
+
// Rock's strata are metres across, so its wall projection samples at its own tile size; grass and
|
|
892
|
+
// dirt sample at theirs, which is why a cliff's grass fringe keeps the same grain as the meadow.
|
|
893
|
+
const walls = triplanarRelief(source, key);
|
|
894
|
+
const blended: IRelief = {
|
|
895
|
+
crevice: mix(walls.crevice, flat.crevice, planarShare),
|
|
896
|
+
tilt: mix(walls.tilt.mul(strength), flat.tilt, planarShare),
|
|
897
|
+
};
|
|
898
|
+
// Sand has no normal map at all, so its relief is entirely the ripple field below — and the
|
|
899
|
+
// ripples flatten in the wet band, because wet sand is packed and the sea has ironed them out.
|
|
900
|
+
return key === "sand"
|
|
901
|
+
? {
|
|
902
|
+
...blended,
|
|
903
|
+
tilt: blended.tilt.add(sandRipples().mul(float(1).sub(wetness().mul(WET_SAND.calm)))),
|
|
904
|
+
}
|
|
905
|
+
: blended;
|
|
906
|
+
};
|
|
907
|
+
|
|
908
|
+
let snowCover: Node<"float"> = float(0);
|
|
909
|
+
const samples: Partial<Record<LayerKey, { surface: Node<"vec4">; relief: IRelief }>> = {};
|
|
910
|
+
material.addEventListener("dispose", () => {
|
|
911
|
+
for (const source of held) source.dispose();
|
|
912
|
+
});
|
|
913
|
+
try {
|
|
914
|
+
// The cliffs take the rock face (`ROCKFACE_MAPS`) in place of the rock layer's own maps.
|
|
915
|
+
const rockPaths = otherBiome ? biome.maps.rock : ROCKFACE_MAPS;
|
|
916
|
+
if (!otherBiome) {
|
|
917
|
+
const diffuse = await get(assets, rockPaths.diffuse, false);
|
|
918
|
+
if (diffuse) {
|
|
919
|
+
held.add(diffuse);
|
|
920
|
+
const normal = rockPaths.normal ? await get(assets, rockPaths.normal, true) : undefined;
|
|
921
|
+
if (normal) held.add(normal);
|
|
922
|
+
maps.rock = { diffuse, normal };
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
return (await loadTerrainSplat({
|
|
926
|
+
assets,
|
|
927
|
+
url: `world/${biome?.world ?? WORLD}.world.json`,
|
|
928
|
+
layerSample: ({ layer: descriptor, diffuse, normal: normalMap }) => {
|
|
929
|
+
const key = descriptor.id as LayerKey;
|
|
930
|
+
maps[key] ??= { diffuse, normal: normalMap };
|
|
931
|
+
const surface = albedoOf(key);
|
|
932
|
+
const relief = reliefOf(
|
|
933
|
+
key,
|
|
934
|
+
key === "grass" ? 0.38 : key === "rock" ? 0.52 : key === "snow" ? 0.18 : 0.32,
|
|
935
|
+
);
|
|
936
|
+
samples[key] = { surface, relief };
|
|
937
|
+
if (key !== "grass") return { color: surface.rgb, normal: relief.tilt };
|
|
938
|
+
let normal = relief.tilt
|
|
939
|
+
.mul(weights.grass)
|
|
940
|
+
.mul(look.crestTurf ? mix(1, 0.05, smoothstep(45, 130, positionView.length())) : 1)
|
|
941
|
+
.add(microGrain().mul(oneMinus(smoothstep(12, 60, positionView.length()))));
|
|
942
|
+
if (look.sandRipples) normal = normal.add(sandRipples(vec2(0.53, 0.85)).mul(weights.grass));
|
|
943
|
+
const turf = groundTurf(surface.rgb, biome, hollow, open);
|
|
944
|
+
// Open turf in the wood takes the row's `turfTint`; under the crowns the needle litter
|
|
945
|
+
// (`FOREST_FLOOR`) already owns the tone, so the tint fades out with the canopy.
|
|
946
|
+
return {
|
|
947
|
+
color: look.woodedGround
|
|
948
|
+
? turf.mul(mix(LOOK.turf, vec3(1), smoothstep(0, 0.6, canopyCover())))
|
|
949
|
+
: turf,
|
|
950
|
+
normal,
|
|
951
|
+
};
|
|
952
|
+
},
|
|
953
|
+
// Geometry, transport and curvature choose placement; the packaged masks remain authored data.
|
|
954
|
+
layerWeight: (_masked, { layer: descriptor }) => {
|
|
955
|
+
const key = descriptor.id as LayerKey;
|
|
956
|
+
const weight = weights[key];
|
|
957
|
+
const sample = samples[key];
|
|
958
|
+
if (!sample) throw new RangeError(`Ground layer '${key}' was not sampled`);
|
|
959
|
+
const { surface, relief } = sample;
|
|
960
|
+
// No displacement maps in these starter sets: normal relief breaks the blend edge,
|
|
961
|
+
// while slope, elevation and curvature determine which surface belongs here.
|
|
962
|
+
const reliefHeight = relief.crevice.sub(0.8).mul(0.12).add(breakUp.mul(0.045));
|
|
963
|
+
// The photograph's relief and metre-scale breakup cut pockets into the soil/rock boundary.
|
|
964
|
+
const rockHeight = dot(surface.rgb, vec3(0.2126, 0.7152, 0.0722))
|
|
965
|
+
.sub(0.1)
|
|
966
|
+
.mul(0.7)
|
|
967
|
+
.add(bakedNoise(positionWorld.mul(0.65)).mul(0.16));
|
|
968
|
+
const over =
|
|
969
|
+
key === "snow" && otherBiome
|
|
970
|
+
? weight.mul(float(1).add(breakUp.mul(0.025))).clamp(0, 1)
|
|
971
|
+
: key === "rock"
|
|
972
|
+
? smoothstep(0.34, 0.51, weight.add(rockHeight))
|
|
973
|
+
: smoothstep(0.12, 0.82, weight.add(reliefHeight));
|
|
974
|
+
if (key === "snow") snowCover = over;
|
|
975
|
+
return over;
|
|
976
|
+
},
|
|
977
|
+
materialFactory: ({ color, normal }) => {
|
|
978
|
+
const albedo = vec4(color, 1);
|
|
979
|
+
// Concavity occludes sky bounce, leaving direct sunlight physically separate; the
|
|
980
|
+
// horizon the surrounding relief makes (`skyVisibility`) shades valleys and gorges too.
|
|
981
|
+
material.aoNode = mix(float(1), float(0.88), smoothstep(0.2, 0.85, hollow)).mul(skyShare());
|
|
982
|
+
|
|
983
|
+
// 50–200 metre vegetation tones survive texture mipmapping in the overview.
|
|
984
|
+
const dryness = otherBiome
|
|
985
|
+
? smoothstep(
|
|
986
|
+
-0.18,
|
|
987
|
+
0.22,
|
|
988
|
+
bakedFractalNoise(positionWorld.mul(0.006), 3)
|
|
989
|
+
.sub(hollow.mul(0.16))
|
|
990
|
+
.add(alpine.mul(0.06)),
|
|
991
|
+
)
|
|
992
|
+
: max(deposits.mul(0.7), drainage.mul(smoothstep(0.015, 0.12, slope)).mul(0.4));
|
|
993
|
+
const tone = otherBiome
|
|
994
|
+
? mix(vec3(0.92, 0.94, 0.91), vec3(1.08, 1.03, 0.95), dryness)
|
|
995
|
+
: mix(vec3(0.9, 0.95, 0.84), vec3(1.03, 1.04, 0.95), dryness);
|
|
996
|
+
const vegetation = oneMinus(max(max(weights.rock, weights.snow), weights.sand));
|
|
997
|
+
// Past the square the forest's hills are its wooded lower slopes: seen from 150 m and more
|
|
998
|
+
// they take the backdrop's spruce cover from 40 m inside the edge, while the meadow under the
|
|
999
|
+
// player's feet keeps the playable ground's own turf.
|
|
1000
|
+
const continuation =
|
|
1001
|
+
!otherBiome && !look.surveyedBackdrop
|
|
1002
|
+
? max(
|
|
1003
|
+
smoothstep(data.size / 2 + 60, data.size / 2 + 280, outside),
|
|
1004
|
+
smoothstep(data.size / 2 - 40, data.size / 2 + 20, outside).mul(
|
|
1005
|
+
smoothstep(150, 350, positionView.length()),
|
|
1006
|
+
),
|
|
1007
|
+
)
|
|
1008
|
+
: smoothstep(data.size / 2 + 60, data.size / 2 + 280, outside);
|
|
1009
|
+
const strata = bakedFractalNoise(positionWorld.mul(vec3(0.018, 0.035, 0.018)), 3);
|
|
1010
|
+
// The forest's backdrop is alpine (owner goal 2026-11-04); the coast keeps its survey's look.
|
|
1011
|
+
const crestBackdrop = !look.surveyedBackdrop;
|
|
1012
|
+
// Where the collar mesh blends to its prefiltered field (300 m past the edge and on), its
|
|
1013
|
+
// shading reads the unfiltered survey instead; the vista's near meadow keeps the mesh.
|
|
1014
|
+
const reliefWeight = collar
|
|
1015
|
+
? smoothstep(data.size / 2 + 300, data.size / 2 + 600, outside)
|
|
1016
|
+
: float(0);
|
|
1017
|
+
const surveyedNormal = collar
|
|
1018
|
+
? normalize(mix(normalWorldGeometry, collar.normal, reliefWeight))
|
|
1019
|
+
: normalWorldGeometry;
|
|
1020
|
+
const crestSteep = collar
|
|
1021
|
+
? clamp(oneMinus(surveyedNormal.y.abs()).add(breakUp.mul(0.05)), 0, 1)
|
|
1022
|
+
: steep;
|
|
1023
|
+
// Talus at the feet of cliff bands: the same rock, finer and paler.
|
|
1024
|
+
const scree = collar ? collar.scree.mul(reliefWeight) : float(0);
|
|
1025
|
+
// Bedding: strata on the steep rock, each a cliff under a ledge, as the goal frame's faces
|
|
1026
|
+
// break into bands (owner, 2026-11-04). Real beds dip about 20 degrees, thin and thicken, and
|
|
1027
|
+
// break along the strike; level, evenly spaced, unbroken rings read as artificial contour
|
|
1028
|
+
// lines (owner, 2026-11-07). The phase is antialiased by its own screen derivative, so a band
|
|
1029
|
+
// thinner than a pixel greys out instead of crawling. No noise feeds back into its own domain:
|
|
1030
|
+
// that warp curled the beds into marbled swirls (owner, 2026-11-09); beds bend only gently.
|
|
1031
|
+
const dipped = positionWorld.y
|
|
1032
|
+
.add(positionWorld.x.mul(0.3))
|
|
1033
|
+
.sub(positionWorld.z.mul(0.18))
|
|
1034
|
+
.add(bakedFractalNoise(positionWorld.mul(0.004), 2).mul(20));
|
|
1035
|
+
const bedding = dipped.div(34);
|
|
1036
|
+
const phase = fract(bedding);
|
|
1037
|
+
const blur = fwidth(bedding).min(0.25);
|
|
1038
|
+
const ledge = smoothstep(float(0.8).sub(blur), float(0.88).add(blur), phase).mul(
|
|
1039
|
+
oneMinus(smoothstep(float(0.95).sub(blur), float(1).add(blur), phase)),
|
|
1040
|
+
);
|
|
1041
|
+
const wall = smoothstep(float(0.08).sub(blur), float(0.2).add(blur), phase).mul(
|
|
1042
|
+
oneMinus(smoothstep(float(0.66).sub(blur), float(0.8).add(blur), phase)),
|
|
1043
|
+
);
|
|
1044
|
+
// A bed runs a few hundred metres along the face and pinches out under scree and couloirs.
|
|
1045
|
+
const runs = smoothstep(
|
|
1046
|
+
-0.05,
|
|
1047
|
+
0.3,
|
|
1048
|
+
bakedFractalNoise(positionWorld.mul(vec3(0.006, 0.03, 0.006)), 2),
|
|
1049
|
+
);
|
|
1050
|
+
// Not every face is bedded: kilometre patches carry bands, the rest stays massive rock.
|
|
1051
|
+
const bedded = collar
|
|
1052
|
+
? smoothstep(-0.15, 0.25, bakedFractalNoise(positionWorld.mul(0.0018), 2))
|
|
1053
|
+
.mul(runs)
|
|
1054
|
+
.mul(reliefWeight)
|
|
1055
|
+
: float(0);
|
|
1056
|
+
// Crag ribs that the prefiltered 10 m backdrop mesh cannot carry: 35 m across and 90 m down
|
|
1057
|
+
// the fall line, as couloirs and buttresses run. Their gradient tilts the normal and their
|
|
1058
|
+
// creases darken the rock, so cliff bands read at two kilometres.
|
|
1059
|
+
// The forest's massif (the only lawn world with a crest backdrop) takes three scales instead:
|
|
1060
|
+
// 350, 120 and 41 m across, each 2.3 times longer down the fall line, as a 110 m relief whose
|
|
1061
|
+
// every octave tilts the face by the same angle, so spurs and couloirs catch the low sun at
|
|
1062
|
+
// every range.
|
|
1063
|
+
const massif = !otherBiome && crestBackdrop;
|
|
1064
|
+
const facetAt = (x: number, y: number, z: number) =>
|
|
1065
|
+
massif
|
|
1066
|
+
? bakedFractalNoise(
|
|
1067
|
+
positionWorld.add(vec3(x, y, z)).mul(vec3(1 / 350, 1 / 800, 1 / 350)),
|
|
1068
|
+
3,
|
|
1069
|
+
2.9,
|
|
1070
|
+
0.33,
|
|
1071
|
+
)
|
|
1072
|
+
: bakedFractalNoise(positionWorld.add(vec3(x, y, z)).mul(vec3(0.028, 0.011, 0.028)), 2);
|
|
1073
|
+
const facet = facetAt(0, 0, 0);
|
|
1074
|
+
const facetTilt = vec3(
|
|
1075
|
+
facet.sub(facetAt(6, 0, 0)),
|
|
1076
|
+
facet.sub(facetAt(0, 6, 0)),
|
|
1077
|
+
facet.sub(facetAt(0, 0, 6)),
|
|
1078
|
+
).mul(massif ? 110 / 6 : 1.4);
|
|
1079
|
+
// Reuse the resident maps at massif scale: steep faces project onto their own axes.
|
|
1080
|
+
const turf = triplanarAlbedo(layer("grass").diffuse, "grass", 12).rgb;
|
|
1081
|
+
// The forest's lower slopes are a closed spruce cover drawn by material alone: 11 m crowns
|
|
1082
|
+
// with dark gaps, denser and sparser stands, darker in the hollows, opened by kilometre meadow clearings.
|
|
1083
|
+
// Past 1 km a crown is a few pixels: its contrast fades, so the cover does not scale over.
|
|
1084
|
+
// One baked read repeats every 8 noise units (88 m at this scale) and drew a checker under the
|
|
1085
|
+
// fog; a second read on a lattice turned 37 degrees and 1.53 times finer breaks the period.
|
|
1086
|
+
const crownField = bakedNoise(positionWorld.div(11))
|
|
1087
|
+
.add(
|
|
1088
|
+
bakedNoise(
|
|
1089
|
+
vec3(
|
|
1090
|
+
positionWorld.x.mul(0.8).add(positionWorld.z.mul(0.6)),
|
|
1091
|
+
positionWorld.y,
|
|
1092
|
+
positionWorld.z.mul(0.8).sub(positionWorld.x.mul(0.6)),
|
|
1093
|
+
).div(7.2),
|
|
1094
|
+
1,
|
|
1095
|
+
0,
|
|
1096
|
+
2,
|
|
1097
|
+
),
|
|
1098
|
+
)
|
|
1099
|
+
.mul(0.7);
|
|
1100
|
+
const crowns = mix(
|
|
1101
|
+
smoothstep(-0.3, 0.35, crownField),
|
|
1102
|
+
float(0.55),
|
|
1103
|
+
smoothstep(400, 1200, positionView.length()).mul(0.8),
|
|
1104
|
+
);
|
|
1105
|
+
const clumps = bakedFractalNoise(positionWorld.div(160), 2);
|
|
1106
|
+
const clearings = smoothstep(
|
|
1107
|
+
0.28,
|
|
1108
|
+
0.5,
|
|
1109
|
+
bakedFractalNoise(positionWorld.mul(0.008), 2).sub(crestSteep.mul(0.4)),
|
|
1110
|
+
);
|
|
1111
|
+
const cover = mix(
|
|
1112
|
+
BACKDROP_SPRUCE.mul(mix(0.4, 1.3, crowns))
|
|
1113
|
+
.mul(clumps.mul(0.15).add(1))
|
|
1114
|
+
.mul(
|
|
1115
|
+
mix(float(1), float(0.7), collar ? smoothstep(0.1, 0.5, collar.hollow) : float(0)),
|
|
1116
|
+
),
|
|
1117
|
+
BACKDROP_CLEARING.mul(mix(0.85, 1.1, crowns)),
|
|
1118
|
+
clearings,
|
|
1119
|
+
);
|
|
1120
|
+
// The coast's surveyed backdrop is an open lawn.
|
|
1121
|
+
const forest = crestBackdrop
|
|
1122
|
+
? cover
|
|
1123
|
+
: turf.mul(MEADOW).mul(mix(0.5, 0.85, breakUp.add(0.5)));
|
|
1124
|
+
// The massif reads at 1-3 km: the rock map tiles at 60 m and 220 m, blended by kilometre
|
|
1125
|
+
// noise, so no face repeats or smears at that range. Unwarped: the ground's 40 m UV warp
|
|
1126
|
+
// swirled a tile this large into marble. The scan is a red-brown boulder; under blue air it
|
|
1127
|
+
// read magenta, so the massif takes its luminance only.
|
|
1128
|
+
const rockAxis = abs(normalWorldGeometry).pow(4);
|
|
1129
|
+
const rockShare = rockAxis.div(rockAxis.x.add(rockAxis.y).add(rockAxis.z));
|
|
1130
|
+
const massifTap = (source: Texture, size: number, offset: number): Node<"vec3"> =>
|
|
1131
|
+
rockShare.x
|
|
1132
|
+
.mul(texture(source, positionWorld.zy.div(size).add(offset)).rgb)
|
|
1133
|
+
.add(rockShare.y.mul(texture(source, positionWorld.xz.div(size).add(offset)).rgb))
|
|
1134
|
+
.add(rockShare.z.mul(texture(source, positionWorld.xy.div(size).add(offset)).rgb));
|
|
1135
|
+
const massifRock = mix(
|
|
1136
|
+
massifTap(layer("rock").diffuse, 60, 0),
|
|
1137
|
+
massifTap(layer("rock").diffuse, 220, 0.37),
|
|
1138
|
+
smoothstep(-0.3, 0.3, bakedNoise(positionWorld.mul(0.0021))),
|
|
1139
|
+
);
|
|
1140
|
+
const crag = (
|
|
1141
|
+
look.surveyedBackdrop
|
|
1142
|
+
? triplanarAlbedo(layer("rock").diffuse, "rock", 6).rgb
|
|
1143
|
+
: // At half its contrast the scan's boulder seams stop drawing a crack net over the faces.
|
|
1144
|
+
vec3(
|
|
1145
|
+
dot(massifRock, vec3(0.2126, 0.7152, 0.0722))
|
|
1146
|
+
.sub(0.07)
|
|
1147
|
+
.mul(0.5)
|
|
1148
|
+
.add(0.07),
|
|
1149
|
+
)
|
|
1150
|
+
.mul(vec3(0.93, 0.99, 1.04))
|
|
1151
|
+
.mul(LOOK.rock)
|
|
1152
|
+
)
|
|
1153
|
+
.mul(crestBackdrop ? vec3(0.62) : vec3(0.62, 0.66, 0.67))
|
|
1154
|
+
.mul(strata.mul(crestBackdrop ? 0.15 : 0.35).add(1))
|
|
1155
|
+
.mul(crestBackdrop ? mix(0.6, 1.15, smoothstep(-0.4, 0.4, facet)) : float(1))
|
|
1156
|
+
// Kilometre-scale warm and cool rock, so whole faces do not read one flat grey.
|
|
1157
|
+
.mul(
|
|
1158
|
+
crestBackdrop
|
|
1159
|
+
? mix(
|
|
1160
|
+
vec3(0.95, 0.98, 1.03),
|
|
1161
|
+
vec3(1.05, 1.02, 0.97),
|
|
1162
|
+
smoothstep(-0.3, 0.3, bakedFractalNoise(positionWorld.mul(0.0025), 2)),
|
|
1163
|
+
)
|
|
1164
|
+
: vec3(1),
|
|
1165
|
+
);
|
|
1166
|
+
// Runoff and lichen stain steep rock in streaks down the fall line, metres wide and tens long.
|
|
1167
|
+
const streaks = smoothstep(
|
|
1168
|
+
-0.35,
|
|
1169
|
+
0.35,
|
|
1170
|
+
bakedFractalNoise(positionWorld.mul(vec3(0.045, 0.006, 0.045)), 3),
|
|
1171
|
+
);
|
|
1172
|
+
const talus = triplanarAlbedo(layer("rock").diffuse, "rock", 1.2).rgb;
|
|
1173
|
+
const rubble = (
|
|
1174
|
+
crestBackdrop
|
|
1175
|
+
? vec3(dot(talus, vec3(0.2126, 0.7152, 0.0722))).mul(vec3(0.66, 0.67, 0.69))
|
|
1176
|
+
: talus.mul(vec3(0.66, 0.65, 0.62))
|
|
1177
|
+
).mul(mix(0.85, 1.1, bakedNoise(positionWorld.mul(0.08)).add(0.5)));
|
|
1178
|
+
// A ragged treeline: spurs of forest climb 90 m higher or stop 90 m lower along the slope.
|
|
1179
|
+
const treeline = elevationBand(
|
|
1180
|
+
measured,
|
|
1181
|
+
measured.heightBands.treeline,
|
|
1182
|
+
positionWorld.y
|
|
1183
|
+
.add(breakUp.mul(24))
|
|
1184
|
+
.add(crestBackdrop ? bakedFractalNoise(positionWorld.mul(0.006), 2).mul(90) : float(0)),
|
|
1185
|
+
);
|
|
1186
|
+
// Rock from about 31 degrees. The wood's spruce stands to 36-46 degrees (its far-forest slope
|
|
1187
|
+
// row), so its cover holds to there: at 31 a surveyed massif's wooded flanks drew bare rock.
|
|
1188
|
+
const face = look.woodedGround
|
|
1189
|
+
? smoothstep(0.19, 0.31, crestSteep.add(strata.mul(0.055)))
|
|
1190
|
+
: smoothstep(0.14, 0.3, crestSteep.add(strata.mul(0.055)));
|
|
1191
|
+
const bands = bedded.mul(face);
|
|
1192
|
+
const snowline = elevationBand(
|
|
1193
|
+
measured,
|
|
1194
|
+
measured.heightBands.cap,
|
|
1195
|
+
positionWorld.y.add(breakUp.mul(28)),
|
|
1196
|
+
);
|
|
1197
|
+
// Snow fields, not ledge lines (owner, 2026-11-09): full cover above the snowline on faces
|
|
1198
|
+
// that point up, tongues 160 m further down the gullies, broken patches 150 m under the
|
|
1199
|
+
// line, and none on wall rock. The facets lift or drop each field's edge, so its rim follows
|
|
1200
|
+
// the ribs instead of a contour.
|
|
1201
|
+
const gully = collar ? smoothstep(0.05, 0.4, collar.hollow).mul(reliefWeight) : float(0);
|
|
1202
|
+
const lying = oneMinus(
|
|
1203
|
+
smoothstep(0.3, 0.48, crestSteep.sub(facet.mul(0.25)).sub(gully.mul(0.1))),
|
|
1204
|
+
);
|
|
1205
|
+
const fields = elevationBand(
|
|
1206
|
+
measured,
|
|
1207
|
+
measured.heightBands.cap,
|
|
1208
|
+
positionWorld.y.add(breakUp.mul(28)).add(gully.mul(160)),
|
|
1209
|
+
).mul(oneMinus(smoothstep(-0.25, -0.45, bakedFractalNoise(positionWorld.mul(0.006), 2))));
|
|
1210
|
+
const patches = smoothstep(
|
|
1211
|
+
measured.heightBands.cap[0] - 150,
|
|
1212
|
+
measured.heightBands.cap[0] - 20,
|
|
1213
|
+
positionWorld.y.add(breakUp.mul(28)),
|
|
1214
|
+
).mul(
|
|
1215
|
+
smoothstep(
|
|
1216
|
+
0.12,
|
|
1217
|
+
0.3,
|
|
1218
|
+
bakedFractalNoise(positionWorld.mul(0.011), 2).add(gully.mul(0.35)).add(facet.mul(0.2)),
|
|
1219
|
+
),
|
|
1220
|
+
);
|
|
1221
|
+
const cap = crestBackdrop
|
|
1222
|
+
? fields.max(patches).mul(lying)
|
|
1223
|
+
: snowline.mul(oneMinus(smoothstep(0.18, 0.42, steep)));
|
|
1224
|
+
// The massif's snow is unwarped too: the ground's UV warp at a 96 m tile swirled it.
|
|
1225
|
+
const snow = crestBackdrop
|
|
1226
|
+
? massifTap(layer("snow").diffuse, 60, 0.21)
|
|
1227
|
+
: triplanarAlbedo(layer("snow").diffuse, "snow", 8).rgb;
|
|
1228
|
+
// Each band is a darker wall under a paler ledge.
|
|
1229
|
+
const banded = crag
|
|
1230
|
+
.mul(mix(float(1), mix(float(1.08), float(0.8), wall), bands))
|
|
1231
|
+
.mul(mix(float(1), mix(float(0.78), float(1.04), streaks), face));
|
|
1232
|
+
const mountain = mix(
|
|
1233
|
+
mix(forest, mix(banded, rubble, scree), max(max(treeline, face), scree)),
|
|
1234
|
+
snow,
|
|
1235
|
+
cap,
|
|
1236
|
+
);
|
|
1237
|
+
material.colorNode = mix(
|
|
1238
|
+
albedo.rgb.mul(mix(vec3(1), tone, vegetation)),
|
|
1239
|
+
otherBiome ? albedo.rgb : mountain,
|
|
1240
|
+
continuation,
|
|
1241
|
+
)
|
|
1242
|
+
.mul(mix(vec3(1), vec3(0.42, 0.46, 0.42), curvature.wetBank))
|
|
1243
|
+
.mul(mix(vec3(1), FOREST_FLOOR, forestFloor))
|
|
1244
|
+
.mul(mix(vec3(1), BARE_SOIL, bareSoil));
|
|
1245
|
+
material.roughnessNode = mix(
|
|
1246
|
+
mix(
|
|
1247
|
+
otherBiome ? 0.94 : mix(0.88, 0.98, dryness),
|
|
1248
|
+
float(0.91).sub(
|
|
1249
|
+
smoothstep(0.48, 0.68, bakedNoise(positionWorld.mul(60)))
|
|
1250
|
+
.mul(oneMinus(smoothstep(3, 14, positionView.length())))
|
|
1251
|
+
.mul(0.5),
|
|
1252
|
+
),
|
|
1253
|
+
snowCover,
|
|
1254
|
+
),
|
|
1255
|
+
0.48,
|
|
1256
|
+
curvature.wetBank,
|
|
1257
|
+
);
|
|
1258
|
+
if (!otherBiome) {
|
|
1259
|
+
material.roughnessNode = mix(
|
|
1260
|
+
material.roughnessNode as Node<"float">,
|
|
1261
|
+
0.52,
|
|
1262
|
+
sand.mul(wetness()),
|
|
1263
|
+
);
|
|
1264
|
+
// A little albedo-coloured bounce keeps unlit rock faces legible without changing the sky rig.
|
|
1265
|
+
material.emissiveNode = (material.colorNode as Node<"vec3">).mul(
|
|
1266
|
+
weights.rock.max(continuation).mul(0.08),
|
|
1267
|
+
);
|
|
1268
|
+
}
|
|
1269
|
+
// Distant faces resolve broad rock strata, rather than subpixel meadow normals.
|
|
1270
|
+
const rockNormal = layer("rock").normal;
|
|
1271
|
+
// The forest's massif takes its relief from the facets alone: the rock normal map reads
|
|
1272
|
+
// through the ground's UV warp, which at a 30 m tile drew curled lines over every face.
|
|
1273
|
+
const mountainTilt = (
|
|
1274
|
+
rockNormal === undefined || massif
|
|
1275
|
+
? vec3(0)
|
|
1276
|
+
: triplanarRelief(rockNormal, "rock", 8).tilt.mul(face).mul(oneMinus(cap)).mul(0.32)
|
|
1277
|
+
).add(
|
|
1278
|
+
crestBackdrop
|
|
1279
|
+
? facetTilt.mul(mix(0.35, 1, max(treeline, face))).mul(oneMinus(cap.mul(0.6)))
|
|
1280
|
+
: vec3(0),
|
|
1281
|
+
);
|
|
1282
|
+
// Distant alpine rock previously discarded all normal relief; snow weights stay unchanged.
|
|
1283
|
+
const distantTilt = look.crestRock ? mountainTilt.mul(weights.rock) : mountainTilt;
|
|
1284
|
+
const tilt = look.continuationTilt ? mix(normal, distantTilt, continuation) : normal;
|
|
1285
|
+
// Detail is tangential; it must not rotate the whole hillside towards a fixed diagonal.
|
|
1286
|
+
const outward = normalize(
|
|
1287
|
+
vec3(surveyedNormal.x, 0, surveyedNormal.z).add(vec3(0.0001, 0, 0)),
|
|
1288
|
+
);
|
|
1289
|
+
const bandTilt = vec3(0, 1, 0)
|
|
1290
|
+
.sub(surveyedNormal)
|
|
1291
|
+
.mul(ledge.mul(0.7))
|
|
1292
|
+
.add(outward.sub(surveyedNormal).mul(wall.mul(0.35)))
|
|
1293
|
+
.mul(bands);
|
|
1294
|
+
const detail = tilt
|
|
1295
|
+
.add(bandTilt)
|
|
1296
|
+
.sub(surveyedNormal.mul(dot(surveyedNormal, tilt.add(bandTilt))));
|
|
1297
|
+
// A bump cannot light a face its own slope turns from the sun: the face's relief would
|
|
1298
|
+
// self-shadow it. Without this the low sun lit the massif's shaded walls in soft, cloud-like
|
|
1299
|
+
// wisps wherever the relief tilted toward it (owner, 2026-11-09). In shade the relief still
|
|
1300
|
+
// turns the face to more or less sky.
|
|
1301
|
+
const tangent = massif
|
|
1302
|
+
? detail.sub(
|
|
1303
|
+
SUN_VECTOR.mul(
|
|
1304
|
+
max(dot(detail, SUN_VECTOR), 0)
|
|
1305
|
+
.mul(oneMinus(smoothstep(-0.05, 0.25, dot(surveyedNormal, SUN_VECTOR))))
|
|
1306
|
+
.mul(continuation),
|
|
1307
|
+
),
|
|
1308
|
+
)
|
|
1309
|
+
: detail;
|
|
1310
|
+
const crystals = microGrain()
|
|
1311
|
+
.mul(snowCover)
|
|
1312
|
+
.mul(oneMinus(smoothstep(4, 22, positionView.length())));
|
|
1313
|
+
material.normalNode = transformNormalToView(
|
|
1314
|
+
normalize(surveyedNormal.add(tangent).add(crystals)),
|
|
1315
|
+
);
|
|
1316
|
+
return material;
|
|
1317
|
+
},
|
|
1318
|
+
})) as MeshStandardNodeMaterial;
|
|
1319
|
+
} catch (error) {
|
|
1320
|
+
material.dispose();
|
|
1321
|
+
throw error;
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
/**
|
|
1326
|
+
* The baked heightfield, its geometry, and the lit ground material once the PBR maps arrive.
|
|
1327
|
+
*
|
|
1328
|
+
* `enter` is synchronous and the physics collider is built from this very mesh, so the mesh exists
|
|
1329
|
+
* immediately and draws with flat vertex colours until the textures settle. A map that never loads
|
|
1330
|
+
* keeps that fallback rather than failing the world: the terrain still has to collide on a host
|
|
1331
|
+
* with no asset server.
|
|
1332
|
+
*/
|
|
1333
|
+
/**
|
|
1334
|
+
* The square around the player that streamed tiles draw (PRD-790): centre and half-width in metres.
|
|
1335
|
+
* The collar draws everywhere outside it, and the tiles draw only inside it and past the playable
|
|
1336
|
+
* square, so each point of ground is drawn once.
|
|
1337
|
+
*/
|
|
1338
|
+
export interface ITerrainStream {
|
|
1339
|
+
readonly centre: UniformNode<"vec2", Vector2>;
|
|
1340
|
+
readonly half: UniformNode<"float", number>;
|
|
1341
|
+
/** Receives the tiles' own ground material once the square's ground is in place. */
|
|
1342
|
+
readonly tiles: (material: MeshStandardNodeMaterial) => void;
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
export function createTerrain(
|
|
1346
|
+
data: IBakedWorld,
|
|
1347
|
+
assets?: IAssetLoader,
|
|
1348
|
+
biome?: IBiome,
|
|
1349
|
+
/** The baked `skyVisibility` per vertex; without it the ground keeps the whole sky. */
|
|
1350
|
+
sky?: Float32Array,
|
|
1351
|
+
stream?: ITerrainStream,
|
|
1352
|
+
): { collar: HeightfieldCollar; field: Heightfield; mesh: Mesh } {
|
|
1353
|
+
const field = new Heightfield({
|
|
1354
|
+
rows: data.resolution,
|
|
1355
|
+
columns: data.resolution,
|
|
1356
|
+
width: data.size,
|
|
1357
|
+
depth: data.size,
|
|
1358
|
+
origin: { x: 0, z: 0 },
|
|
1359
|
+
heights: new Float32Array(data.heights),
|
|
1360
|
+
});
|
|
1361
|
+
const geometry = field.toGeometry();
|
|
1362
|
+
if (data.colors.length !== geometry.getAttribute("position").count * 3)
|
|
1363
|
+
throw new RangeError("Baked terrain colours do not match the heightfield");
|
|
1364
|
+
geometry.setAttribute("color", new Float32BufferAttribute(data.colors, 3));
|
|
1365
|
+
// Crown cover per vertex, painted from the scattered trees by `paintCanopy`; zero until then.
|
|
1366
|
+
geometry.setAttribute(
|
|
1367
|
+
"canopy",
|
|
1368
|
+
new Float32BufferAttribute(new Float32Array(geometry.getAttribute("position").count), 1),
|
|
1369
|
+
);
|
|
1370
|
+
// The share of the sky each vertex sees past the relief around it, for the ground's ambient term.
|
|
1371
|
+
const vertices = geometry.getAttribute("position").count;
|
|
1372
|
+
if (sky && sky.length !== vertices)
|
|
1373
|
+
throw new RangeError(`Baked sky visibility has ${sky.length} values for ${vertices} vertices`);
|
|
1374
|
+
geometry.setAttribute(
|
|
1375
|
+
"sky",
|
|
1376
|
+
new Float32BufferAttribute(sky ?? new Float32Array(vertices).fill(1), 1),
|
|
1377
|
+
);
|
|
1378
|
+
const material = new MeshStandardMaterial({ vertexColors: true, roughness: 0.95 });
|
|
1379
|
+
const mesh: Mesh = new Mesh(geometry, material);
|
|
1380
|
+
mesh.name = "authored-terrain";
|
|
1381
|
+
// The ground is under everything and its material is the frame's dearest: drawn in sort order it
|
|
1382
|
+
// shaded every pixel a crown or a blade then covered. After the props, the depth test rejects those
|
|
1383
|
+
// first (forest player pose, RTX 2080, 913x881: 13.6 ms of GPU with the ground in sort order, 8.9 ms
|
|
1384
|
+
// after the props). The water keeps its place after the ground at render order 2.
|
|
1385
|
+
mesh.renderOrder = GROUND_RENDER_ORDER;
|
|
1386
|
+
mesh.layers.enable(REFLECTED_LAYER);
|
|
1387
|
+
mesh.receiveShadow = true;
|
|
1388
|
+
mesh.castShadow = biome?.terrainCastsShadow ?? false;
|
|
1389
|
+
const collar = createHorizonCollar(data, WORLD);
|
|
1390
|
+
const horizonGeometry = collar.geometry;
|
|
1391
|
+
horizonGeometry.setAttribute("color", new Float32BufferAttribute(collarColors(collar, data), 3));
|
|
1392
|
+
horizonGeometry.computeVertexNormals();
|
|
1393
|
+
const edgePositions = horizonGeometry.getAttribute("position");
|
|
1394
|
+
const edgeNormals = horizonGeometry.getAttribute("normal");
|
|
1395
|
+
const groundNormals = geometry.getAttribute("normal");
|
|
1396
|
+
let horizonSeamGap = 0;
|
|
1397
|
+
for (let vertex = 0; vertex < (data.resolution - 1) * 4; vertex++) {
|
|
1398
|
+
const column = Math.round(
|
|
1399
|
+
(edgePositions.getX(vertex) / data.size + 0.5) * (data.resolution - 1),
|
|
1400
|
+
);
|
|
1401
|
+
const row = Math.round((edgePositions.getZ(vertex) / data.size + 0.5) * (data.resolution - 1));
|
|
1402
|
+
const edge = row * data.resolution + column;
|
|
1403
|
+
horizonSeamGap = Math.max(
|
|
1404
|
+
horizonSeamGap,
|
|
1405
|
+
Math.abs(edgePositions.getY(vertex) - (data.heights[edge] as number)),
|
|
1406
|
+
);
|
|
1407
|
+
edgeNormals.setXYZ(
|
|
1408
|
+
vertex,
|
|
1409
|
+
groundNormals.getX(edge),
|
|
1410
|
+
groundNormals.getY(edge),
|
|
1411
|
+
groundNormals.getZ(edge),
|
|
1412
|
+
);
|
|
1413
|
+
}
|
|
1414
|
+
// The snow and mat masks read normals: blend the collar too, rather than only its boundary
|
|
1415
|
+
// vertices, and reach as far past the square as those masks do.
|
|
1416
|
+
const collarBand = biome?.groundCollarMeters ?? 0;
|
|
1417
|
+
if (collarBand > 0) {
|
|
1418
|
+
const perimeter = (data.resolution - 1) * 4;
|
|
1419
|
+
const normal = new Vector3();
|
|
1420
|
+
const boundary = new Vector3();
|
|
1421
|
+
for (let vertex = perimeter; vertex < edgePositions.count; vertex++) {
|
|
1422
|
+
const distance =
|
|
1423
|
+
Math.max(Math.abs(edgePositions.getX(vertex)), Math.abs(edgePositions.getZ(vertex))) -
|
|
1424
|
+
data.size / 2;
|
|
1425
|
+
if (distance >= collarBand) break;
|
|
1426
|
+
normal
|
|
1427
|
+
.fromBufferAttribute(edgeNormals, vertex)
|
|
1428
|
+
.lerp(
|
|
1429
|
+
boundary.fromBufferAttribute(edgeNormals, vertex % perimeter),
|
|
1430
|
+
1 - MathUtils.smoothstep(distance, 0, collarBand),
|
|
1431
|
+
)
|
|
1432
|
+
.normalize();
|
|
1433
|
+
edgeNormals.setXYZ(vertex, normal.x, normal.y, normal.z);
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
mesh.userData.horizonSeamGap = horizonSeamGap;
|
|
1437
|
+
mesh.userData.horizonSeamSamples = (data.resolution - 1) * 4;
|
|
1438
|
+
horizonGeometry.setAttribute(
|
|
1439
|
+
"canopy",
|
|
1440
|
+
new Float32BufferAttribute(new Float32Array(edgePositions.count), 1),
|
|
1441
|
+
);
|
|
1442
|
+
// The ground material reads `sky` too; the collar lies past the baked square and keeps the sky.
|
|
1443
|
+
horizonGeometry.setAttribute(
|
|
1444
|
+
"sky",
|
|
1445
|
+
new Float32BufferAttribute(new Float32Array(edgePositions.count).fill(1), 1),
|
|
1446
|
+
);
|
|
1447
|
+
const horizon: Mesh = new Mesh(horizonGeometry, material);
|
|
1448
|
+
horizon.name = "temperate-distant-ridges";
|
|
1449
|
+
horizon.renderOrder = GROUND_RENDER_ORDER;
|
|
1450
|
+
horizon.layers.enable(REFLECTED_LAYER);
|
|
1451
|
+
horizon.receiveShadow = true;
|
|
1452
|
+
// Scenery beyond the collider: no ray stands on it, and a picking tree over its 1.5M triangles
|
|
1453
|
+
// cost the first ground contact 1.1 s, which landed on the first rendered frame.
|
|
1454
|
+
horizon.raycast = () => undefined;
|
|
1455
|
+
mesh.add(horizon);
|
|
1456
|
+
geometry.addEventListener("dispose", () => horizon.geometry.dispose());
|
|
1457
|
+
|
|
1458
|
+
const curvature = buildCurvature(data, field, biome);
|
|
1459
|
+
let disposed = false;
|
|
1460
|
+
geometry.addEventListener("dispose", () => {
|
|
1461
|
+
disposed = true;
|
|
1462
|
+
});
|
|
1463
|
+
if (assets !== undefined)
|
|
1464
|
+
void Promise.all([
|
|
1465
|
+
createGroundMaterial(data, assets, curvature, biome),
|
|
1466
|
+
stream ? createGroundMaterial(data, assets, curvature, biome, true) : undefined,
|
|
1467
|
+
])
|
|
1468
|
+
.then(([ground, tile]) => {
|
|
1469
|
+
// Anything but the flat placeholder means the scene already moved on; a material nothing
|
|
1470
|
+
// draws holds GPU memory until its textures are released.
|
|
1471
|
+
if (disposed || mesh.material !== material) {
|
|
1472
|
+
tile?.dispose();
|
|
1473
|
+
return ground.dispose();
|
|
1474
|
+
}
|
|
1475
|
+
mesh.material = ground;
|
|
1476
|
+
horizon.material = ground;
|
|
1477
|
+
material.dispose();
|
|
1478
|
+
const surfaces = [ground];
|
|
1479
|
+
if (stream && tile) {
|
|
1480
|
+
// Chebyshev distance from the streamed square's centre, against its half-width.
|
|
1481
|
+
const fromCentre = max(
|
|
1482
|
+
abs(positionWorld.x.sub(stream.centre.x)),
|
|
1483
|
+
abs(positionWorld.z.sub(stream.centre.y)),
|
|
1484
|
+
);
|
|
1485
|
+
const far = ground.clone();
|
|
1486
|
+
// The collar draws under the block's edge too: the depth test picks the nearer surface there.
|
|
1487
|
+
far.maskNode = fromCentre.greaterThanEqual(stream.half.sub(COLLAR_UNDER_TILE));
|
|
1488
|
+
horizon.material = far;
|
|
1489
|
+
// The outermost streamed tile slides onto the collar's drawn height (PRD-790 box 2).
|
|
1490
|
+
tile.positionNode = collarMorph(collar, COLLAR_CURVE, stream, STREAM_TILE);
|
|
1491
|
+
tile.maskNode = fromCentre
|
|
1492
|
+
.lessThan(stream.half)
|
|
1493
|
+
// The skirt each tile hangs on the square's edge draws too, under the square's cracks.
|
|
1494
|
+
.and(
|
|
1495
|
+
max(abs(positionWorld.x), abs(positionWorld.z)).greaterThan(
|
|
1496
|
+
data.size / 2 - TILE_INSIDE_SQUARE,
|
|
1497
|
+
),
|
|
1498
|
+
);
|
|
1499
|
+
surfaces.push(far, tile);
|
|
1500
|
+
geometry.addEventListener("dispose", () => far.dispose());
|
|
1501
|
+
stream.tiles(tile);
|
|
1502
|
+
}
|
|
1503
|
+
// The ground took no image-based light, so terrain in shadow fell to near-black while every
|
|
1504
|
+
// plant on it read the sky. It reads the same clamped sky the vegetation does.
|
|
1505
|
+
if (biome === undefined || biome.lawnGround)
|
|
1506
|
+
void loadSkyLight(assets).then((sky) => {
|
|
1507
|
+
if (!sky || mesh.material !== ground || !ground.colorNode) return;
|
|
1508
|
+
// An envMap would be the ground's 17th sampler; the diffuse sky it carries is two colours.
|
|
1509
|
+
const { up, down } = skyIrradiance(sky);
|
|
1510
|
+
for (const surface of surfaces) {
|
|
1511
|
+
surface.emissiveNode = (surface.colorNode as Node<"vec3">)
|
|
1512
|
+
.mul(mix(vec3(...down), vec3(...up), normalWorld.y.mul(0.5).add(0.5)))
|
|
1513
|
+
.mul(GROUND_SKY_LIGHT);
|
|
1514
|
+
surface.needsUpdate = true;
|
|
1515
|
+
}
|
|
1516
|
+
});
|
|
1517
|
+
})
|
|
1518
|
+
// A map that never arrives, or a set this material cannot bind, leaves the ground on its
|
|
1519
|
+
// baked vertex colours. It must still collide and still draw, and the console says why.
|
|
1520
|
+
.catch((error) => {
|
|
1521
|
+
console.warn(`TN_GROUND_MAP_MISSING ${error instanceof Error ? error.message : error}`);
|
|
1522
|
+
// The ground itself is fine — it keeps its baked vertex colours — but the streamed block's
|
|
1523
|
+
// handshake must still land: the spawn gate waits on it, so a missing map would hold a player
|
|
1524
|
+
// behind the cover for the whole deadline. The placeholder draws the tiles until the map does.
|
|
1525
|
+
// The placeholder is a plain material, not a node surface; it is only here to land the join.
|
|
1526
|
+
stream?.tiles(material as unknown as MeshStandardNodeMaterial);
|
|
1527
|
+
});
|
|
1528
|
+
return { collar, field, mesh };
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1531
|
+
/**
|
|
1532
|
+
* Cook the heightfield's own curvature into a texture the ground material can read.
|
|
1533
|
+
*
|
|
1534
|
+
* A five-point Laplacian over the baked samples, evaluated once on the CPU and stored as half floats:
|
|
1535
|
+
* `h(-1) + h(+1) + h(-w) + h(+w) - 4h(0)`, which is positive in a hollow and negative on a rib. The
|
|
1536
|
+
* divisor is the sample spacing squared, so the number is in 1/m and does not change meaning when the
|
|
1537
|
+
* world's resolution does, and the gain is chosen so a valley a few metres deep reads near +1.
|
|
1538
|
+
*
|
|
1539
|
+
* Half float rather than byte because the range matters: a byte would quantise the sign of a
|
|
1540
|
+
* near-flat field into blocks, and the sign is the whole of the answer.
|
|
1541
|
+
*
|
|
1542
|
+
* One curvature binding plus six albedos, four normals and four shadow maps is fifteen.
|
|
1543
|
+
*/
|
|
1544
|
+
function buildCurvature(data: IBakedWorld, field: Heightfield, biome?: IBiome): IGroundCurvature {
|
|
1545
|
+
const resolution = data.resolution;
|
|
1546
|
+
const heights = data.heights;
|
|
1547
|
+
const spacing = data.size / (resolution - 1);
|
|
1548
|
+
const at = (row: number, column: number): number =>
|
|
1549
|
+
heights[
|
|
1550
|
+
Math.min(resolution - 1, Math.max(0, row)) * resolution +
|
|
1551
|
+
Math.min(resolution - 1, Math.max(0, column))
|
|
1552
|
+
] as number;
|
|
1553
|
+
const lakes = (data.lakes ?? []).map((lake) => ({
|
|
1554
|
+
...lake,
|
|
1555
|
+
reach: waterlineRadius(field, lake.at, lake.level),
|
|
1556
|
+
}));
|
|
1557
|
+
const rivers = (data.rivers ?? []).flatMap((river) => {
|
|
1558
|
+
const levels = surfaceHeights(field, river.points);
|
|
1559
|
+
return river.points.map(([x = 0, , z = 0], index) => ({
|
|
1560
|
+
x,
|
|
1561
|
+
z,
|
|
1562
|
+
level: levels[index] as number,
|
|
1563
|
+
}));
|
|
1564
|
+
});
|
|
1565
|
+
// River stations indexed at their 13 m wet margin. It is this ground shader's own number: the drawn
|
|
1566
|
+
// ribbon sizes itself from `WATER_RIBBON_REACH` in river.ts. A vertex tests only
|
|
1567
|
+
// the stations that can reach it rather than every station of every river.
|
|
1568
|
+
const RIVER_REACH = 13;
|
|
1569
|
+
const stations = createSegmentIndex(
|
|
1570
|
+
rivers.map((station) => ({
|
|
1571
|
+
a: [station.x, station.z] as const,
|
|
1572
|
+
b: [station.x, station.z] as const,
|
|
1573
|
+
reach: RIVER_REACH,
|
|
1574
|
+
data: station,
|
|
1575
|
+
})),
|
|
1576
|
+
RIVER_REACH,
|
|
1577
|
+
);
|
|
1578
|
+
// Both channels share one sampler: measured concavity and the water's actual wet margin.
|
|
1579
|
+
const transport = data.erosion;
|
|
1580
|
+
if (transport) {
|
|
1581
|
+
for (const key of ["flow", "sediment", "deposition", "talus"] as const) {
|
|
1582
|
+
const values = transport[key];
|
|
1583
|
+
if (
|
|
1584
|
+
!Array.isArray(values) ||
|
|
1585
|
+
values.length !== heights.length ||
|
|
1586
|
+
!values.every((value) => Number.isFinite(value) && value >= 0)
|
|
1587
|
+
)
|
|
1588
|
+
throw new RangeError(`Baked erosion '${key}' does not match the canonical heightfield`);
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
const sortedFlow = [...(transport?.flow ?? [])].sort((a, b) => a - b);
|
|
1592
|
+
const medianFlow = sortedFlow[Math.floor(sortedFlow.length * 0.5)] ?? 0;
|
|
1593
|
+
const channelFlow = sortedFlow[Math.floor(sortedFlow.length * 0.98)] ?? 1;
|
|
1594
|
+
const pixels = new Uint16Array(resolution * resolution * 4);
|
|
1595
|
+
for (let row = 0; row < resolution; row += 1) {
|
|
1596
|
+
for (let column = 0; column < resolution; column += 1) {
|
|
1597
|
+
const laplacian =
|
|
1598
|
+
at(row, column - 4) +
|
|
1599
|
+
at(row, column + 4) +
|
|
1600
|
+
at(row - 4, column) +
|
|
1601
|
+
at(row + 4, column) -
|
|
1602
|
+
4 * at(row, column);
|
|
1603
|
+
// An eight-metre neighbourhood picks out channels and hollows, suppressing tiny baked bumps.
|
|
1604
|
+
const vertex = row * resolution + column;
|
|
1605
|
+
const index = vertex * 4;
|
|
1606
|
+
pixels[index] = DataUtils.toHalfFloat(
|
|
1607
|
+
Math.max(-1, Math.min(1, (laplacian / (spacing * spacing * 16)) * 36)),
|
|
1608
|
+
);
|
|
1609
|
+
const x = column * spacing - data.size / 2;
|
|
1610
|
+
const z = row * spacing - data.size / 2;
|
|
1611
|
+
const height = at(row, column);
|
|
1612
|
+
const margin = (level: number): number =>
|
|
1613
|
+
Math.max(0, Math.min(1, (level + 1.1 - height) / 1.1));
|
|
1614
|
+
let wet = 0;
|
|
1615
|
+
for (const lake of lakes) {
|
|
1616
|
+
const distance = Math.hypot(x - (lake.at[0] ?? 0), z - (lake.at[1] ?? 0));
|
|
1617
|
+
if (distance <= lake.reach + 3) wet = Math.max(wet, margin(lake.level));
|
|
1618
|
+
}
|
|
1619
|
+
for (const station of stations.nearby(x, z)) {
|
|
1620
|
+
if (Math.hypot(x - station.x, z - station.z) <= RIVER_REACH)
|
|
1621
|
+
wet = Math.max(wet, margin(station.level));
|
|
1622
|
+
}
|
|
1623
|
+
pixels[index + 1] = DataUtils.toHalfFloat(wet);
|
|
1624
|
+
pixels[index + 2] = DataUtils.toHalfFloat(
|
|
1625
|
+
Math.max(
|
|
1626
|
+
0,
|
|
1627
|
+
Math.min(
|
|
1628
|
+
1,
|
|
1629
|
+
((transport?.flow[vertex] ?? 0) - medianFlow) / Math.max(1, channelFlow - medianFlow),
|
|
1630
|
+
),
|
|
1631
|
+
),
|
|
1632
|
+
);
|
|
1633
|
+
// Thermal deposits are actual cliff-foot transport. Hydraulic fines collect where water
|
|
1634
|
+
// carries a load and slows; normalise by carried load to avoid painting every rain visit.
|
|
1635
|
+
pixels[index + 3] = DataUtils.toHalfFloat(depositAtIndex(data, vertex));
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
const cooked = new DataTexture(pixels, resolution, resolution, RGBAFormat, HalfFloatType);
|
|
1639
|
+
cooked.minFilter = LinearFilter;
|
|
1640
|
+
cooked.magFilter = LinearFilter;
|
|
1641
|
+
cooked.wrapS = ClampToEdgeWrapping;
|
|
1642
|
+
cooked.wrapT = ClampToEdgeWrapping;
|
|
1643
|
+
cooked.needsUpdate = true;
|
|
1644
|
+
const uv = vec2(
|
|
1645
|
+
positionWorld.x.div(float(data.size)).add(0.5),
|
|
1646
|
+
positionWorld.z.div(float(data.size)).add(0.5),
|
|
1647
|
+
);
|
|
1648
|
+
const sampled = texture(cooked, uv);
|
|
1649
|
+
// A clamped wet boundary texel must not become a stripe across the continuation mesh.
|
|
1650
|
+
const outside = max(abs(positionWorld.x), abs(positionWorld.z));
|
|
1651
|
+
const collar = biome?.groundCollarMeters ?? 0;
|
|
1652
|
+
const resident = oneMinus(
|
|
1653
|
+
smoothstep(data.size / 2 - Math.max(collar, spacing * 2), data.size / 2 + collar, outside),
|
|
1654
|
+
);
|
|
1655
|
+
return {
|
|
1656
|
+
node: sampled.r,
|
|
1657
|
+
texture: cooked,
|
|
1658
|
+
wetBank: sampled.g.mul(resident),
|
|
1659
|
+
flow: sampled.b.mul(resident),
|
|
1660
|
+
deposits: sampled.a.mul(resident),
|
|
1661
|
+
};
|
|
1662
|
+
}
|
|
1663
|
+
|
|
1664
|
+
/** The collar's own relief at the survey's 9.8 m cells, for the pixels its prefiltered mesh smooths. */
|
|
1665
|
+
export interface ICollarRelief {
|
|
1666
|
+
readonly texture: DataTexture;
|
|
1667
|
+
/** World normal of the unfiltered surroundings. */
|
|
1668
|
+
readonly normal: Node<"vec3">;
|
|
1669
|
+
/** +1 in a gully or hollow, -1 on a rib. */
|
|
1670
|
+
readonly hollow: Node<"float">;
|
|
1671
|
+
/** 1 on talus at the foot of a cliff band. */
|
|
1672
|
+
readonly scree: Node<"float">;
|
|
1673
|
+
}
|
|
1674
|
+
|
|
1675
|
+
/**
|
|
1676
|
+
* Per-pixel relief for the collar, as UE shades a landscape from its normal map rather than the LOD
|
|
1677
|
+
* mesh (UE 5.8.3: Engine/Shaders/Private/LandscapeVertexFactory.ush:391-401). Past about 300 m the
|
|
1678
|
+
* collar draws a prefiltered field on a 14-18 m ring grid, so its cliffs, gullies and ledges exist
|
|
1679
|
+
* only in this texture. Half float like the curvature: the normal lanes are signed.
|
|
1680
|
+
*/
|
|
1681
|
+
export function buildCollarRelief(name: string): ICollarRelief | undefined {
|
|
1682
|
+
const source = horizonSurroundings(name);
|
|
1683
|
+
if (source === undefined) return undefined;
|
|
1684
|
+
const n = source.resolution;
|
|
1685
|
+
const massif = source.massif && decodeMassif(source.massif);
|
|
1686
|
+
// One atlas, one binding (the ground already samples sixteen): the collar's cells on the left,
|
|
1687
|
+
// a placed massif's own 4 m cells on the right, each read through its own clamped UV.
|
|
1688
|
+
const width = n + (massif?.columns ?? 0);
|
|
1689
|
+
const height = Math.max(n, massif?.rows ?? 0);
|
|
1690
|
+
const pixels = new Uint16Array(width * height * 4);
|
|
1691
|
+
writeRelief(pixels, width, 0, n, n, source.size / (n - 1), source.heights);
|
|
1692
|
+
if (massif)
|
|
1693
|
+
writeRelief(pixels, width, n, massif.columns, massif.rows, massif.spacing, massif.heights);
|
|
1694
|
+
const cooked = new DataTexture(pixels, width, height, RGBAFormat, HalfFloatType);
|
|
1695
|
+
cooked.minFilter = LinearFilter;
|
|
1696
|
+
cooked.magFilter = LinearFilter;
|
|
1697
|
+
cooked.wrapS = ClampToEdgeWrapping;
|
|
1698
|
+
cooked.wrapT = ClampToEdgeWrapping;
|
|
1699
|
+
cooked.needsUpdate = true;
|
|
1700
|
+
// Grid points sit on texel centres: column c of the collar is at (c + 0.5) / width.
|
|
1701
|
+
const collarUv = vec2(positionWorld.x, positionWorld.z)
|
|
1702
|
+
.div(source.size)
|
|
1703
|
+
.add(0.5)
|
|
1704
|
+
.mul(n - 1)
|
|
1705
|
+
.clamp(0, n - 1)
|
|
1706
|
+
.add(0.5)
|
|
1707
|
+
.div(vec2(width, height));
|
|
1708
|
+
let sampled: Node<"vec4"> = texture(cooked, collarUv);
|
|
1709
|
+
if (massif) {
|
|
1710
|
+
const cell = vec2(positionWorld.x, positionWorld.z)
|
|
1711
|
+
.sub(vec2(massif.origin[0], massif.origin[1]))
|
|
1712
|
+
.div(massif.spacing);
|
|
1713
|
+
const last = vec2(massif.columns - 1, massif.rows - 1);
|
|
1714
|
+
const inner = min(min(cell.x, cell.y), min(last.x.sub(cell.x), last.y.sub(cell.y)));
|
|
1715
|
+
const fine = texture(
|
|
1716
|
+
cooked,
|
|
1717
|
+
cell
|
|
1718
|
+
.clamp(vec2(0, 0), last)
|
|
1719
|
+
.add(vec2(n + 0.5, 0.5))
|
|
1720
|
+
.div(vec2(width, height)),
|
|
1721
|
+
);
|
|
1722
|
+
sampled = mix(sampled, fine, smoothstep(0, 3, inner));
|
|
1723
|
+
}
|
|
1724
|
+
return {
|
|
1725
|
+
texture: cooked,
|
|
1726
|
+
normal: vec3(
|
|
1727
|
+
sampled.r,
|
|
1728
|
+
float(1).sub(sampled.r.mul(sampled.r)).sub(sampled.g.mul(sampled.g)).max(0).sqrt(),
|
|
1729
|
+
sampled.g,
|
|
1730
|
+
),
|
|
1731
|
+
hollow: sampled.b,
|
|
1732
|
+
scree: sampled.a,
|
|
1733
|
+
};
|
|
1734
|
+
}
|
|
1735
|
+
|
|
1736
|
+
/** A massif's heights from `horizon.json`: int16 decimetres above `offset`, base64. */
|
|
1737
|
+
function decodeMassif(packed: IPackedMassif) {
|
|
1738
|
+
const bytes = Uint8Array.from(atob(packed.heights), (c) => c.charCodeAt(0));
|
|
1739
|
+
const raw = new Int16Array(bytes.buffer, 0, packed.columns * packed.rows);
|
|
1740
|
+
return { ...packed, heights: Float32Array.from(raw, (dm) => packed.offset + dm / 10) };
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1743
|
+
/**
|
|
1744
|
+
* Normal, hollow and talus texels for a `columns` x `rows` height grid at `cell` metres, written
|
|
1745
|
+
* into the atlas from column `x0`. The talus and hollow reach are in metres, so a 4 m grid reads
|
|
1746
|
+
* the same landforms as the 9.8 m collar, only sharper.
|
|
1747
|
+
*/
|
|
1748
|
+
function writeRelief(
|
|
1749
|
+
pixels: Uint16Array,
|
|
1750
|
+
width: number,
|
|
1751
|
+
x0: number,
|
|
1752
|
+
columns: number,
|
|
1753
|
+
rows: number,
|
|
1754
|
+
cell: number,
|
|
1755
|
+
heights: ArrayLike<number>,
|
|
1756
|
+
): void {
|
|
1757
|
+
const at = (row: number, column: number): number =>
|
|
1758
|
+
heights[
|
|
1759
|
+
Math.min(rows - 1, Math.max(0, row)) * columns + Math.min(columns - 1, Math.max(0, column))
|
|
1760
|
+
] as number;
|
|
1761
|
+
const slopes = new Float32Array(columns * rows);
|
|
1762
|
+
for (let row = 0; row < rows; row++)
|
|
1763
|
+
for (let column = 0; column < columns; column++)
|
|
1764
|
+
slopes[row * columns + column] = Math.atan(
|
|
1765
|
+
Math.hypot(
|
|
1766
|
+
(at(row, column + 1) - at(row, column - 1)) / (2 * cell),
|
|
1767
|
+
(at(row + 1, column) - at(row - 1, column)) / (2 * cell),
|
|
1768
|
+
),
|
|
1769
|
+
);
|
|
1770
|
+
const slopeAt = (row: number, column: number): number =>
|
|
1771
|
+
slopes[
|
|
1772
|
+
Math.min(rows - 1, Math.max(0, row)) * columns + Math.min(columns - 1, Math.max(0, column))
|
|
1773
|
+
] as number;
|
|
1774
|
+
const degrees = Math.PI / 180;
|
|
1775
|
+
const band = (edge0: number, edge1: number, value: number): number => {
|
|
1776
|
+
const t = Math.max(0, Math.min(1, (value - edge0) / (edge1 - edge0)));
|
|
1777
|
+
return MathUtils.smoothstep(t, 0, 1);
|
|
1778
|
+
};
|
|
1779
|
+
// The 9.8 m collar measured a hollow across 2 cells and talus within 3 cells; keep those metres.
|
|
1780
|
+
const span = Math.max(2, Math.round(19.6 / cell));
|
|
1781
|
+
const step = Math.max(1, Math.round(9.8 / cell));
|
|
1782
|
+
for (let row = 0; row < rows; row++)
|
|
1783
|
+
for (let column = 0; column < columns; column++) {
|
|
1784
|
+
const height = at(row, column);
|
|
1785
|
+
const dx = (at(row, column + 1) - at(row, column - 1)) / (2 * cell);
|
|
1786
|
+
const dz = (at(row + 1, column) - at(row - 1, column)) / (2 * cell);
|
|
1787
|
+
const length = Math.hypot(dx, 1, dz);
|
|
1788
|
+
// A gully 5 m deep across 20 m reads near +1.
|
|
1789
|
+
const laplacian =
|
|
1790
|
+
(at(row, column - span) +
|
|
1791
|
+
at(row, column + span) +
|
|
1792
|
+
at(row - span, column) +
|
|
1793
|
+
at(row + span, column) -
|
|
1794
|
+
4 * height) /
|
|
1795
|
+
(span * cell) ** 2;
|
|
1796
|
+
// Talus lies at 25-40 degrees under a face steeper than about 45 degrees within 30 m uphill.
|
|
1797
|
+
let cliffAbove = 0;
|
|
1798
|
+
for (let dr = -3; dr <= 3; dr++)
|
|
1799
|
+
for (let dc = -3; dc <= 3; dc++)
|
|
1800
|
+
if (at(row + dr * step, column + dc * step) > height + 8)
|
|
1801
|
+
cliffAbove = Math.max(
|
|
1802
|
+
cliffAbove,
|
|
1803
|
+
band(42 * degrees, 50 * degrees, slopeAt(row + dr * step, column + dc * step)),
|
|
1804
|
+
);
|
|
1805
|
+
const slope = slopes[row * columns + column] as number;
|
|
1806
|
+
const talus =
|
|
1807
|
+
band(22 * degrees, 28 * degrees, slope) * (1 - band(38 * degrees, 44 * degrees, slope));
|
|
1808
|
+
const texel = (row * width + x0 + column) * 4;
|
|
1809
|
+
pixels[texel] = DataUtils.toHalfFloat(-dx / length);
|
|
1810
|
+
pixels[texel + 1] = DataUtils.toHalfFloat(-dz / length);
|
|
1811
|
+
pixels[texel + 2] = DataUtils.toHalfFloat(Math.max(-1, Math.min(1, laplacian * 20)));
|
|
1812
|
+
pixels[texel + 3] = DataUtils.toHalfFloat(talus * cliffAbove);
|
|
1813
|
+
}
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
async function get(
|
|
1817
|
+
assets: IAssetLoader,
|
|
1818
|
+
path: string,
|
|
1819
|
+
data: boolean,
|
|
1820
|
+
): Promise<Texture | undefined> {
|
|
1821
|
+
// A clone without the licensed `local-assets/` draws the baked vertex colours; the warning names
|
|
1822
|
+
// the file, so a white ground on a machine that should have the art is one console line away.
|
|
1823
|
+
return assets.texture(path, { anisotropy: 8, data, wrap: RepeatWrapping }).catch((error) => {
|
|
1824
|
+
console.warn(
|
|
1825
|
+
`TN_GROUND_MAP_MISSING ${path}: ${error instanceof Error ? error.message : error}`,
|
|
1826
|
+
);
|
|
1827
|
+
return undefined;
|
|
1828
|
+
});
|
|
1829
|
+
}
|