@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,284 @@
|
|
|
1
|
+
import { type ISpectralOceanOptions, SpectralOcean } from "@threenative/core";
|
|
2
|
+
import { shoreField } from "@threenative/core/world";
|
|
3
|
+
import {
|
|
4
|
+
Color,
|
|
5
|
+
DataTexture,
|
|
6
|
+
DataUtils,
|
|
7
|
+
HalfFloatType,
|
|
8
|
+
LinearFilter,
|
|
9
|
+
Mesh,
|
|
10
|
+
PlaneGeometry,
|
|
11
|
+
RGBAFormat,
|
|
12
|
+
} from "three";
|
|
13
|
+
import {
|
|
14
|
+
cameraPosition,
|
|
15
|
+
clamp,
|
|
16
|
+
color,
|
|
17
|
+
float,
|
|
18
|
+
fwidth,
|
|
19
|
+
mix,
|
|
20
|
+
mx_fractal_noise_float,
|
|
21
|
+
mx_noise_float,
|
|
22
|
+
normalize,
|
|
23
|
+
positionLocal,
|
|
24
|
+
positionWorld,
|
|
25
|
+
smoothstep,
|
|
26
|
+
texture,
|
|
27
|
+
time,
|
|
28
|
+
transformNormalToView,
|
|
29
|
+
uniform,
|
|
30
|
+
vec2,
|
|
31
|
+
vec3,
|
|
32
|
+
} from "three/tsl";
|
|
33
|
+
import type { Node } from "three/webgpu";
|
|
34
|
+
import { MeshStandardNodeMaterial } from "three/webgpu";
|
|
35
|
+
import type { IBakedWorld } from "../ground/terrain.js";
|
|
36
|
+
|
|
37
|
+
/** The coast's spectral sea state; two non-overlapping wave bands. */
|
|
38
|
+
export const SEA = {
|
|
39
|
+
amplitude: 0.0018,
|
|
40
|
+
cascades: [{ patchSize: 190 }, { patchSize: 37 }],
|
|
41
|
+
choppiness: 1.2,
|
|
42
|
+
directionality: 2.6,
|
|
43
|
+
gravity: 9.81,
|
|
44
|
+
// Keep the scenario's throttled CPU wave observations.
|
|
45
|
+
readbackEveryFrames: 3,
|
|
46
|
+
readbackResolution: 32,
|
|
47
|
+
resolution: 128,
|
|
48
|
+
seed: 20_260_906,
|
|
49
|
+
smallWaveCutoff: 0.32,
|
|
50
|
+
windDirection: 0.55,
|
|
51
|
+
windSpeed: 10.5,
|
|
52
|
+
} satisfies ISpectralOceanOptions;
|
|
53
|
+
|
|
54
|
+
/** Dense inner grid; its outer vertices stretch to the distant horizon below. */
|
|
55
|
+
export const SURFACE = { segments: 512, size: 1024 } as const;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* How far one axis of the sea plane spreads: dense to 320 m either side, then squared outwards so the
|
|
59
|
+
* surface reaches the fogged horizon instead of ending at the survey's edge.
|
|
60
|
+
*/
|
|
61
|
+
function stretchMetres(value: number): number {
|
|
62
|
+
const distance = Math.abs(value);
|
|
63
|
+
return (
|
|
64
|
+
Math.sign(value) * (distance <= 320 ? distance : 320 + ((distance - 320) / 192) ** 2 * 3800)
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Metres from the origin the drawn sea surface reaches, either side of it. */
|
|
69
|
+
export const SEA_EXTENT = stretchMetres(SURFACE.size / 2);
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The sea's two body colours as live uniforms, so an editor can retint the water without rebuilding
|
|
73
|
+
* its material. The defaults are this game's look: nothing here changes it until a caller writes.
|
|
74
|
+
*/
|
|
75
|
+
export const OCEAN_LOOK = {
|
|
76
|
+
// The Gulf of Maine: cold, grey-green and opaque, not a tropical blue.
|
|
77
|
+
deep: uniform(new Color(0x10302f)),
|
|
78
|
+
shallow: uniform(new Color(0x2e5a52)),
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/** Thin breaking surf on exposed shores. */
|
|
82
|
+
const FOAM = 0xe9f4f6;
|
|
83
|
+
|
|
84
|
+
export function createOcean(): SpectralOcean {
|
|
85
|
+
return new SpectralOcean(SEA);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Bilinear periodic sampling; wrap twice because JavaScript/WGSL remainder can be negative. */
|
|
89
|
+
function cascadeAt(
|
|
90
|
+
ocean: SpectralOcean,
|
|
91
|
+
index: number,
|
|
92
|
+
x: Node<"float">,
|
|
93
|
+
z: Node<"float">,
|
|
94
|
+
): Node<"vec4"> {
|
|
95
|
+
const grid = float(ocean.resolution);
|
|
96
|
+
const patch = float(ocean.cascadePatchSize(index));
|
|
97
|
+
const buffer = ocean.cascadeDisplacement(index);
|
|
98
|
+
const u = x.div(patch).mul(grid);
|
|
99
|
+
const v = z.div(patch).mul(grid);
|
|
100
|
+
const u0 = u.floor();
|
|
101
|
+
const v0 = v.floor();
|
|
102
|
+
const wrap = (value: Node<"float">): Node<"float"> => value.mod(grid).add(grid).mod(grid);
|
|
103
|
+
const read = (cx: Node<"float">, cz: Node<"float">): Node<"vec4"> =>
|
|
104
|
+
buffer.element(wrap(cz).mul(grid).add(wrap(cx)).toUint()) as Node<"vec4">;
|
|
105
|
+
const near = mix(read(u0, v0), read(u0.add(1), v0), u.sub(u0));
|
|
106
|
+
const far = mix(read(u0, v0.add(1)), read(u0.add(1), v0.add(1)), u.sub(u0));
|
|
107
|
+
return mix(near, far, v.sub(v0)) as Node<"vec4">;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Summed displacement of both cascades at a world position. */
|
|
111
|
+
function displacementAt(ocean: SpectralOcean, x: Node<"float">, z: Node<"float">): Node<"vec3"> {
|
|
112
|
+
const broad = cascadeAt(ocean, 0, x, z);
|
|
113
|
+
const fine = cascadeAt(ocean, 1, x, z);
|
|
114
|
+
return vec3(broad.x.add(fine.x), broad.y.add(fine.y), broad.z.add(fine.z));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The sea surface: displaced by the simulation, and lit by the scene.
|
|
119
|
+
*
|
|
120
|
+
* The vertex stage reads the cascade buffers directly, so what is drawn is the same field the
|
|
121
|
+
* height query is copied from. If the two disagreed the ship would ride water nothing renders and
|
|
122
|
+
* every assertion in this template would still be green.
|
|
123
|
+
*/
|
|
124
|
+
export function createWaterMesh(ocean: SpectralOcean, data: IBakedWorld): Mesh {
|
|
125
|
+
const level = data.waterLevel;
|
|
126
|
+
if (level === null || !Number.isFinite(level))
|
|
127
|
+
throw new Error("TN_COASTAL_SEA: the coastal bake has no finite sea level");
|
|
128
|
+
// Distance to dry land and connection to deep open water, measured once from the bake.
|
|
129
|
+
// Flooding only through >2 m water leaves sheltered lagoons out of the breaking-surf mask.
|
|
130
|
+
const n = data.resolution;
|
|
131
|
+
const cell = data.size / (n - 1);
|
|
132
|
+
// The surveyed deepest bed sets the tint range and the open-sea continuation.
|
|
133
|
+
const deepDepth = data.heights.reduce(
|
|
134
|
+
(deepest, height) => Math.max(deepest, level - height),
|
|
135
|
+
cell,
|
|
136
|
+
);
|
|
137
|
+
const { distance, exposed } = shoreField(data.heights, n, data.size, level);
|
|
138
|
+
const pixels = new Uint16Array(n * n * 4);
|
|
139
|
+
for (let i = 0; i < n * n; i++) {
|
|
140
|
+
pixels[i * 4] = DataUtils.toHalfFloat(data.heights[i] as number);
|
|
141
|
+
pixels[i * 4 + 1] = DataUtils.toHalfFloat(distance[i] as number);
|
|
142
|
+
pixels[i * 4 + 2] = DataUtils.toHalfFloat(exposed[i] as number);
|
|
143
|
+
}
|
|
144
|
+
const heightTexture = new DataTexture(pixels, n, n, RGBAFormat, HalfFloatType);
|
|
145
|
+
heightTexture.minFilter = LinearFilter;
|
|
146
|
+
heightTexture.magFilter = LinearFilter;
|
|
147
|
+
heightTexture.needsUpdate = true;
|
|
148
|
+
const geometry = new PlaneGeometry(
|
|
149
|
+
SURFACE.size,
|
|
150
|
+
SURFACE.size,
|
|
151
|
+
SURFACE.segments,
|
|
152
|
+
SURFACE.segments,
|
|
153
|
+
);
|
|
154
|
+
geometry.rotateX(-Math.PI / 2);
|
|
155
|
+
// Dense near shore, sparse beyond it; the sea reaches the fogged horizon instead of ending at 512 m.
|
|
156
|
+
const vertices = geometry.getAttribute("position");
|
|
157
|
+
for (let i = 0; i < vertices.count; i++)
|
|
158
|
+
vertices.setXYZ(i, stretchMetres(vertices.getX(i)), 0, stretchMetres(vertices.getZ(i)));
|
|
159
|
+
geometry.computeBoundingSphere();
|
|
160
|
+
|
|
161
|
+
const material = new MeshStandardNodeMaterial({
|
|
162
|
+
metalness: 0,
|
|
163
|
+
transparent: true,
|
|
164
|
+
depthWrite: true,
|
|
165
|
+
roughness: 0.12,
|
|
166
|
+
});
|
|
167
|
+
const shoreUV = (x: Node<"float">, z: Node<"float">) =>
|
|
168
|
+
vec2(x.div(data.size).add(0.5), z.div(data.size).add(0.5))
|
|
169
|
+
.mul((n - 1) / n)
|
|
170
|
+
.add(0.5 / n);
|
|
171
|
+
// Outside the surveyed bed, continue its edge depth before easing into open sea.
|
|
172
|
+
const inside = (x: Node<"float">, z: Node<"float">) =>
|
|
173
|
+
float(1).sub(smoothstep(data.size / 2, data.size * 0.75, x.abs().max(z.abs())));
|
|
174
|
+
const vertexLand = texture(heightTexture, shoreUV(positionLocal.x, positionLocal.z)).r;
|
|
175
|
+
const vertexDepth = mix(
|
|
176
|
+
float(deepDepth),
|
|
177
|
+
float(level).sub(vertexLand),
|
|
178
|
+
inside(positionLocal.x, positionLocal.z),
|
|
179
|
+
);
|
|
180
|
+
const shoal = smoothstep(0.15, 3, vertexDepth);
|
|
181
|
+
const vertexDistance = positionLocal.xz.sub(cameraPosition.xz).length();
|
|
182
|
+
const swellFade = float(1).sub(smoothstep(240, 1600, vertexDistance));
|
|
183
|
+
material.positionNode = positionLocal.add(
|
|
184
|
+
displacementAt(ocean, positionLocal.x, positionLocal.z).mul(shoal).mul(swellFade),
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
const eyeDistance = positionWorld.sub(cameraPosition).length();
|
|
188
|
+
const slopeFade = float(1).sub(smoothstep(100, 950, eyeDistance));
|
|
189
|
+
const step = float(0.45).add(eyeDistance.mul(0.006));
|
|
190
|
+
const east = displacementAt(ocean, positionWorld.x.add(step), positionWorld.z);
|
|
191
|
+
const west = displacementAt(ocean, positionWorld.x.sub(step), positionWorld.z);
|
|
192
|
+
const north = displacementAt(ocean, positionWorld.x, positionWorld.z.add(step));
|
|
193
|
+
const south = displacementAt(ocean, positionWorld.x, positionWorld.z.sub(step));
|
|
194
|
+
// The river's noise-gradient detail has no directional period to read as corduroy.
|
|
195
|
+
const ripplePoint = vec3(
|
|
196
|
+
positionWorld.x.mul(0.85).sub(time.mul(0.22)),
|
|
197
|
+
positionWorld.z.mul(0.85).add(time.mul(0.11)),
|
|
198
|
+
time.mul(0.07),
|
|
199
|
+
);
|
|
200
|
+
const rippleHeight = (point: Node<"vec3">) =>
|
|
201
|
+
mx_fractal_noise_float(point, 3, 2.1, 0.48).mul(0.028);
|
|
202
|
+
const h = rippleHeight(ripplePoint);
|
|
203
|
+
const ripple = vec3(
|
|
204
|
+
h.sub(rippleHeight(ripplePoint.add(vec3(0.12, 0, 0)))).div(0.12),
|
|
205
|
+
1,
|
|
206
|
+
h.sub(rippleHeight(ripplePoint.add(vec3(0, 0.12, 0)))).div(0.12),
|
|
207
|
+
);
|
|
208
|
+
const shore = texture(heightTexture, shoreUV(positionWorld.x, positionWorld.z));
|
|
209
|
+
const local = inside(positionWorld.x, positionWorld.z);
|
|
210
|
+
const depth = mix(float(deepDepth), float(level).sub(shore.r), local);
|
|
211
|
+
const damp = smoothstep(0.1, 2.5, depth);
|
|
212
|
+
const wind = mx_noise_float(
|
|
213
|
+
vec3(positionWorld.x.mul(0.07).sub(time.mul(0.04)), positionWorld.z.mul(0.07), time.mul(0.015)),
|
|
214
|
+
)
|
|
215
|
+
.mul(0.6)
|
|
216
|
+
.add(0.6);
|
|
217
|
+
const fineFade = float(1)
|
|
218
|
+
.sub(smoothstep(18, 100, eyeDistance))
|
|
219
|
+
.mul(damp)
|
|
220
|
+
.mul(wind);
|
|
221
|
+
const normal = normalize(
|
|
222
|
+
vec3(
|
|
223
|
+
west.y.sub(east.y).div(step.mul(2)).mul(slopeFade).mul(damp).add(ripple.x.mul(fineFade)),
|
|
224
|
+
1,
|
|
225
|
+
south.y.sub(north.y).div(step.mul(2)).mul(slopeFade).mul(damp).add(ripple.z.mul(fineFade)),
|
|
226
|
+
),
|
|
227
|
+
);
|
|
228
|
+
material.normalNode = transformNormalToView(normal);
|
|
229
|
+
const view = normalize(cameraPosition.sub(positionWorld));
|
|
230
|
+
const bounced = view.negate().reflect(normal);
|
|
231
|
+
// The standard material supplies Fresnel and the actual sun's specular lobe; the sky supplies radiance.
|
|
232
|
+
material.envNode = mix(color(0xb6cbd5), color(0x568fbd), clamp(bounced.y, 0, 1)).mul(1.15);
|
|
233
|
+
const water = mix(
|
|
234
|
+
OCEAN_LOOK.shallow,
|
|
235
|
+
OCEAN_LOOK.deep,
|
|
236
|
+
smoothstep(deepDepth * 0.25, deepDepth, depth).pow(2),
|
|
237
|
+
);
|
|
238
|
+
const surfPoint = vec3(positionWorld.x, positionWorld.z, time.mul(0.45));
|
|
239
|
+
const broadSurf = mx_noise_float(surfPoint.mul(vec3(0.18, 0.18, 0.3)))
|
|
240
|
+
.mul(0.5)
|
|
241
|
+
.add(0.5);
|
|
242
|
+
const surf = mx_noise_float(surfPoint.mul(vec3(0.9, 0.9, 1)))
|
|
243
|
+
.mul(0.5)
|
|
244
|
+
.add(0.5);
|
|
245
|
+
// A pixel covering a bubble fades it out; derivative-wide thresholds soften the remaining edges.
|
|
246
|
+
const footprint = fwidth(positionWorld.xz).length().mul(0.9);
|
|
247
|
+
const antialias = fwidth(surf).max(0.025);
|
|
248
|
+
const bubbles = smoothstep(
|
|
249
|
+
float(0.5).sub(broadSurf.mul(0.16)).sub(antialias),
|
|
250
|
+
float(0.65).sub(broadSurf.mul(0.16)).add(antialias),
|
|
251
|
+
surf,
|
|
252
|
+
).mul(float(1).sub(smoothstep(0.35, 1, footprint)));
|
|
253
|
+
const shoreReach = float(1)
|
|
254
|
+
.sub(smoothstep(3, 12, shore.g))
|
|
255
|
+
.mul(shore.b.mul(0.65).add(0.35))
|
|
256
|
+
.mul(local);
|
|
257
|
+
const shoreFoam = float(1)
|
|
258
|
+
.sub(smoothstep(0.25, 1.8, depth.add(time.mul(0.85).sin().mul(0.18))))
|
|
259
|
+
.mul(smoothstep(0.015, 0.08, depth))
|
|
260
|
+
.mul(bubbles)
|
|
261
|
+
.mul(broadSurf.mul(0.6).add(0.25))
|
|
262
|
+
.mul(shoreReach);
|
|
263
|
+
const swash = float(1)
|
|
264
|
+
.sub(smoothstep(0.1, 2.5, depth))
|
|
265
|
+
.mul(smoothstep(0, 0.12, depth))
|
|
266
|
+
.mul(broadSurf.mul(0.5).add(0.5))
|
|
267
|
+
.mul(shoreReach)
|
|
268
|
+
.mul(0.12);
|
|
269
|
+
material.colorNode = mix(water, color(FOAM), shoreFoam.add(swash));
|
|
270
|
+
material.opacityNode = smoothstep(0, Math.min(2.5, deepDepth), depth).max(shoreFoam.mul(0.9));
|
|
271
|
+
material.roughnessNode = mix(
|
|
272
|
+
float(0.12).add(smoothstep(70, 700, eyeDistance).mul(0.07)),
|
|
273
|
+
float(0.7),
|
|
274
|
+
shoreFoam,
|
|
275
|
+
);
|
|
276
|
+
material.addEventListener("dispose", () => heightTexture.dispose());
|
|
277
|
+
|
|
278
|
+
const mesh = new Mesh(geometry, material);
|
|
279
|
+
mesh.position.y = level;
|
|
280
|
+
mesh.receiveShadow = true;
|
|
281
|
+
mesh.frustumCulled = false;
|
|
282
|
+
mesh.name = "sea-surface";
|
|
283
|
+
return mesh;
|
|
284
|
+
}
|