roamwright 0.55.1 → 0.56.0
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/examples/animals.js +27 -15
- package/examples/crowds.js +28 -18
- package/examples/effects.js +13 -11
- package/examples/figure-motion.js +17 -5
- package/examples/figure.js +12 -8
- package/examples/fliers.js +16 -12
- package/examples/fortification.js +31 -18
- package/examples/house-kit.js +17 -10
- package/examples/island.js +25 -12
- package/examples/land.js +14 -8
- package/examples/props-and-plants.js +21 -32
- package/examples/rooms.js +2 -2
- package/examples/ships.js +33 -15
- package/examples/small-world-land.js +3 -3
- package/examples/small-world-life.js +13 -5
- package/examples/small-world-town.js +8 -11
- package/examples/small-world.js +26 -19
- package/examples/soundscape.js +18 -18
- package/examples/stage-kits.js +5 -133
- package/examples/stage.js +1 -324
- package/examples/towns.js +25 -18
- package/examples/viaduct-landscape.js +1 -1
- package/examples/viaduct.js +3 -4
- package/geometry/builder.js +8 -18
- package/geometry/plant-broadleaf.js +10 -4
- package/geometry/plant-trees.js +15 -9
- package/geometry/still-mesh.js +35 -0
- package/index.js +6 -1
- package/motion/figure-mount.js +1 -0
- package/package.json +1 -1
- package/rendering/crowd-system.js +239 -0
- package/rendering/daylight-sky.js +212 -0
- package/rendering/house-kit-meshes.js +65 -16
- package/rendering/orbit-camera.js +216 -0
- package/rendering/water-simple.js +112 -0
|
@@ -4,27 +4,76 @@ import * as THREE from 'three';
|
|
|
4
4
|
import { geometryOf } from '../geometry/builder.js';
|
|
5
5
|
import { KIT_KEYS } from '../geometry/house-kit-frame.js';
|
|
6
6
|
import { markStill } from './still-casters.js';
|
|
7
|
+
import { createHandOver } from '../runtime/hand-over.js';
|
|
8
|
+
|
|
9
|
+
const SYNC_HAND_OVER = createHandOver({ painted: () => Promise.resolve(), frameShare: () => Infinity });
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Creates a default vertex-coloured `MeshStandardMaterial` map for every house-kit part key
|
|
13
|
+
* (`KIT_KEYS`), suitable for immediate rendering without custom world shaders.
|
|
14
|
+
*
|
|
15
|
+
* @param {object} [options]
|
|
16
|
+
* @param {readonly string[]} [options.keys=KIT_KEYS] Kit material keys to populate.
|
|
17
|
+
* @param {boolean} [options.doubleSide=false] Draw both faces on solid kit parts.
|
|
18
|
+
* @param {number} [options.roughness=0.9] Surface roughness for matte kit parts.
|
|
19
|
+
* @param {number} [options.glassRoughness=0.25] Surface roughness for `glass` when single-sided.
|
|
20
|
+
* @param {number} [options.stainOpacity=0.25] Opacity for transparent ground-contact `stain` decals.
|
|
21
|
+
* @returns {Record<string, THREE.MeshStandardMaterial>}
|
|
22
|
+
*/
|
|
23
|
+
export function createHouseKitMaterials({
|
|
24
|
+
keys = KIT_KEYS,
|
|
25
|
+
doubleSide = false,
|
|
26
|
+
roughness = 0.9,
|
|
27
|
+
glassRoughness = 0.25,
|
|
28
|
+
stainOpacity = 0.25,
|
|
29
|
+
} = {}) {
|
|
30
|
+
const solid = doubleSide ? { vertexColors: true, roughness, side: THREE.DoubleSide } : { vertexColors: true, roughness };
|
|
31
|
+
const materials = {};
|
|
32
|
+
for (const key of keys) {
|
|
33
|
+
if (key === 'stain') {
|
|
34
|
+
materials[key] = new THREE.MeshStandardMaterial({
|
|
35
|
+
vertexColors: true,
|
|
36
|
+
transparent: true,
|
|
37
|
+
opacity: stainOpacity,
|
|
38
|
+
depthWrite: false,
|
|
39
|
+
});
|
|
40
|
+
} else if (key === 'glass' && !doubleSide) {
|
|
41
|
+
materials[key] = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: glassRoughness });
|
|
42
|
+
} else if (key === 'cloth') {
|
|
43
|
+
materials[key] = new THREE.MeshStandardMaterial({ vertexColors: true, roughness, side: THREE.DoubleSide });
|
|
44
|
+
} else {
|
|
45
|
+
materials[key] = new THREE.MeshStandardMaterial(solid);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return materials;
|
|
49
|
+
}
|
|
7
50
|
|
|
8
51
|
/**
|
|
9
52
|
* Binds a house kit's meshes to a world's materials and its renderer's batches.
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* @param {
|
|
53
|
+
* When `materials` or `handOver` are omitted, default vertex-coloured materials
|
|
54
|
+
* (`createHouseKitMaterials`) and a synchronous hand-over are used so small worlds and examples
|
|
55
|
+
* can build kit meshes with one call.
|
|
56
|
+
*
|
|
57
|
+
* @param {object} [o]
|
|
58
|
+
* @param {Object<string, import('three').Material>} [o.materials] A material for each of the kit's kinds (`KIT_KEYS`), read
|
|
59
|
+
* when a mesh is made; defaults to `createHouseKitMaterials()`.
|
|
60
|
+
* @param {Function|null} [o.PieceBatch=null] The renderer's batch class (`createPieceBatch`), required only for `cellPieces`.
|
|
61
|
+
* @param {(material: import('three').Material) => import('three').Material} [o.instancedMaterial] The material an
|
|
15
62
|
* instanced copy draws with (a twin of the mesh's that reads the instance's matrix); the mesh's own if not given.
|
|
16
|
-
* @param {() => boolean} [o.worldDrawn]
|
|
63
|
+
* @param {() => boolean} [o.worldDrawn] Whether the world has been drawn yet: until it has, steps written to the card
|
|
17
64
|
* run all at once (behind a loading bar) rather than paced.
|
|
18
|
-
* @param {object} o.handOver
|
|
19
|
-
* @returns {object} `kitStatic(kit, parent, materialOf)`,
|
|
20
|
-
*
|
|
21
|
-
* `
|
|
22
|
-
* `kitInstanced(kit, n, group)`; `cellPieces(cellsOfKinds, parent, far, detail, materialOf)`, a cut kit's cells as batches,
|
|
23
|
-
* a generator of steps each yielding the bytes it wrote (the small parts' cells noted in `detail` with where they stand and
|
|
24
|
-
* how far, `far` metres beyond their edge, they are drawn), returning the batches; `pieceSwitch(batch, id)`, a stand-in with
|
|
25
|
-
* `visible` for one piece of a batch; `toCard(steps, signal)`, runs such steps paced once the world is drawn.
|
|
65
|
+
* @param {object} [o.handOver] The renderer's hand-over (`createHandOver`): `paced`, `drain` and `heldBytes`; defaults to a synchronous hand-over.
|
|
66
|
+
* @returns {object} `materials` (the resolved material map), `kitStatic(kit, parent, materialOf)`, `kitMeshes(parts, parent, materialOf)`,
|
|
67
|
+
* `instancedOf(meshes, n, group)`, `kitInstanced(kit, n, group)`, `cellPieces(cellsOfKinds, parent, far, detail, materialOf)`,
|
|
68
|
+
* `pieceSwitch(batch, id)`, and `toCard(steps, signal)`.
|
|
26
69
|
*/
|
|
27
|
-
export function createHouseKitMeshes({
|
|
70
|
+
export function createHouseKitMeshes({
|
|
71
|
+
materials = createHouseKitMaterials(),
|
|
72
|
+
PieceBatch = null,
|
|
73
|
+
instancedMaterial = m => m,
|
|
74
|
+
worldDrawn = () => true,
|
|
75
|
+
handOver = SYNC_HAND_OVER,
|
|
76
|
+
} = {}) {
|
|
28
77
|
const { drain, heldBytes, paced } = handOver,
|
|
29
78
|
ownMaterial = k => materials[k];
|
|
30
79
|
|
|
@@ -120,5 +169,5 @@ export function createHouseKitMeshes({ materials, PieceBatch, instancedMaterial
|
|
|
120
169
|
};
|
|
121
170
|
}
|
|
122
171
|
|
|
123
|
-
return { cellPieces, instancedOf, kitInstanced, kitMeshes, kitStatic, pieceSwitch, toCard };
|
|
172
|
+
return { cellPieces, instancedOf, kitInstanced, kitMeshes, kitStatic, materials, pieceSwitch, toCard };
|
|
124
173
|
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reusable orbit and showcase camera controller with ground clearance clamping, adaptive portrait
|
|
3
|
+
* field of view, keyboard orbit/zoom bindings, and named viewpoint switching.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as THREE from 'three';
|
|
8
|
+
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Attaches keyboard arrow-key orbit and `+`/`=`/`-` zoom controls to `canvas` matching pointer
|
|
12
|
+
* `OrbitControls`, returning an unbind callback that removes the listener.
|
|
13
|
+
*
|
|
14
|
+
* @param {object} config
|
|
15
|
+
* @param {EventTarget} config.canvas Focusable canvas element receiving `keydown` events.
|
|
16
|
+
* @param {THREE.Camera} config.camera Camera driven by `controls`.
|
|
17
|
+
* @param {{ target: THREE.Vector3, update: () => void }} config.controls Orbit controls instance.
|
|
18
|
+
* @param {number} [config.minDistance=0] Minimum orbit distance from target (metres).
|
|
19
|
+
* @param {number} [config.maxDistance=Infinity] Maximum orbit distance from target (metres).
|
|
20
|
+
* @param {number} [config.yawStep=0.1] Azimuth increment per left/right key press (radians).
|
|
21
|
+
* @param {number} [config.pitchStep=0.08] Polar angle increment per up/down key press (radians).
|
|
22
|
+
* @param {number} [config.zoomFactor=0.88] Radius multiplier per `+` zoom step (`1 / zoomFactor` for `-`).
|
|
23
|
+
* @returns {() => void} Cleanup function removing the `keydown` listener.
|
|
24
|
+
*/
|
|
25
|
+
export function bindKeyboardOrbit({
|
|
26
|
+
canvas,
|
|
27
|
+
camera,
|
|
28
|
+
controls,
|
|
29
|
+
minDistance = 0,
|
|
30
|
+
maxDistance = Infinity,
|
|
31
|
+
yawStep = 0.1,
|
|
32
|
+
pitchStep = 0.08,
|
|
33
|
+
zoomFactor = 0.88,
|
|
34
|
+
}) {
|
|
35
|
+
const offset = new THREE.Vector3();
|
|
36
|
+
const orbit = new THREE.Spherical();
|
|
37
|
+
|
|
38
|
+
function onKeyDown(event) {
|
|
39
|
+
offset.copy(camera.position).sub(controls.target);
|
|
40
|
+
orbit.setFromVector3(offset);
|
|
41
|
+
switch (event.key) {
|
|
42
|
+
case 'ArrowLeft':
|
|
43
|
+
orbit.theta -= yawStep;
|
|
44
|
+
break;
|
|
45
|
+
case 'ArrowRight':
|
|
46
|
+
orbit.theta += yawStep;
|
|
47
|
+
break;
|
|
48
|
+
case 'ArrowUp':
|
|
49
|
+
orbit.phi -= pitchStep;
|
|
50
|
+
break;
|
|
51
|
+
case 'ArrowDown':
|
|
52
|
+
orbit.phi += pitchStep;
|
|
53
|
+
break;
|
|
54
|
+
case '+':
|
|
55
|
+
case '=':
|
|
56
|
+
orbit.radius = Math.max(minDistance, orbit.radius * zoomFactor);
|
|
57
|
+
break;
|
|
58
|
+
case '-':
|
|
59
|
+
orbit.radius = Math.min(maxDistance, orbit.radius / zoomFactor);
|
|
60
|
+
break;
|
|
61
|
+
default:
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
event.preventDefault();
|
|
65
|
+
camera.position.setFromSpherical(orbit.makeSafe()).add(controls.target);
|
|
66
|
+
controls.update();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
canvas.addEventListener('keydown', onKeyDown);
|
|
70
|
+
return () => canvas.removeEventListener('keydown', onKeyDown);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Sets up `OrbitControls` with ground-clearance clamping, adaptive portrait FOV on resize,
|
|
75
|
+
* keyboard orbit/zoom, and optional named viewpoint buttons (`[data-view]`).
|
|
76
|
+
*
|
|
77
|
+
* @param {object} config
|
|
78
|
+
* @param {THREE.PerspectiveCamera} config.camera Perspective camera to control.
|
|
79
|
+
* @param {HTMLCanvasElement} config.canvas Target rendering canvas.
|
|
80
|
+
* @param {THREE.WebGLRenderer} config.renderer Renderer resized with the canvas.
|
|
81
|
+
* @param {THREE.Scene} config.scene Scene drawn on view changes when `onDraw` is omitted.
|
|
82
|
+
* @param {Record<string, { eye: [number, number, number], target: [number, number, number] }>} [config.views={}] Named viewpoints.
|
|
83
|
+
* @param {string} [config.initialView] Initial viewpoint key; defaults to the first key of `views`.
|
|
84
|
+
* @param {(x: number, z: number) => number} [config.groundAt] Ground height sampler (metres) for clearance clamping.
|
|
85
|
+
* @param {((camera: THREE.Camera) => void)|null} [config.guardCamera=null] Custom camera clamp overriding `groundAt` + `minClearance`.
|
|
86
|
+
* @param {number} [config.minClearance=1.0] Minimum height above `groundAt(x, z)` (metres).
|
|
87
|
+
* @param {number} [config.minDistance=4] Minimum orbit distance (metres).
|
|
88
|
+
* @param {number} [config.maxDistance=180] Maximum orbit distance (metres).
|
|
89
|
+
* @param {number} [config.baseFov=42] Landscape vertical FOV in degrees.
|
|
90
|
+
* @param {number} [config.maxFov=68] Maximum vertical FOV on narrow portrait viewports in degrees.
|
|
91
|
+
* @param {number} [config.maxPolarAngle=Math.PI * 0.485] Maximum polar angle from zenith (radians).
|
|
92
|
+
* @param {number} [config.far=2500] Camera far clipping plane (metres).
|
|
93
|
+
* @param {number} [config.maxPixelRatio=2] Upper bound on renderer pixel ratio.
|
|
94
|
+
* @param {boolean} [config.enableDamping=false] Enables OrbitControls damping.
|
|
95
|
+
* @param {boolean} [config.enablePan=false] Enables OrbitControls panning.
|
|
96
|
+
* @param {string|Iterable<Element>|false} [config.viewButtons='[data-view]'] CSS selector or element list for viewpoint buttons, or `false` to skip DOM binding.
|
|
97
|
+
* @param {(() => void)|null} [config.onDraw=null] Custom render callback (pass `() => {}` when an animation loop drives rendering).
|
|
98
|
+
* @returns {{ controls: OrbitControls, selectView: (name: string) => void, resize: () => void, clampCamera: () => void, dispose: () => void }}
|
|
99
|
+
*/
|
|
100
|
+
export function createOrbitCamera({
|
|
101
|
+
camera,
|
|
102
|
+
canvas,
|
|
103
|
+
renderer,
|
|
104
|
+
scene,
|
|
105
|
+
views = {},
|
|
106
|
+
initialView,
|
|
107
|
+
groundAt = () => 0,
|
|
108
|
+
guardCamera = null,
|
|
109
|
+
minClearance = 1.0,
|
|
110
|
+
minDistance = 4,
|
|
111
|
+
maxDistance = 180,
|
|
112
|
+
baseFov = 42,
|
|
113
|
+
maxFov = 68,
|
|
114
|
+
maxPolarAngle = Math.PI * 0.485,
|
|
115
|
+
far = 2500,
|
|
116
|
+
maxPixelRatio = 2,
|
|
117
|
+
enableDamping = false,
|
|
118
|
+
enablePan = false,
|
|
119
|
+
viewButtons = '[data-view]',
|
|
120
|
+
onDraw = null,
|
|
121
|
+
}) {
|
|
122
|
+
const controls = new OrbitControls(camera, canvas);
|
|
123
|
+
controls.enableDamping = enableDamping;
|
|
124
|
+
controls.enablePan = enablePan;
|
|
125
|
+
controls.maxPolarAngle = maxPolarAngle;
|
|
126
|
+
controls.minDistance = minDistance;
|
|
127
|
+
controls.maxDistance = maxDistance;
|
|
128
|
+
camera.far = far;
|
|
129
|
+
|
|
130
|
+
const buttons =
|
|
131
|
+
viewButtons === false || viewButtons === null
|
|
132
|
+
? []
|
|
133
|
+
: typeof viewButtons === 'string'
|
|
134
|
+
? typeof document !== 'undefined'
|
|
135
|
+
? Array.from(document.querySelectorAll(viewButtons))
|
|
136
|
+
: []
|
|
137
|
+
: Array.from(viewButtons);
|
|
138
|
+
|
|
139
|
+
function clampCamera() {
|
|
140
|
+
if (guardCamera) {
|
|
141
|
+
guardCamera(camera);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const floor = groundAt(camera.position.x, camera.position.z);
|
|
145
|
+
if (Number.isFinite(floor) && camera.position.y < floor + minClearance) {
|
|
146
|
+
camera.position.y = floor + minClearance;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function render() {
|
|
151
|
+
clampCamera();
|
|
152
|
+
camera.lookAt(controls.target);
|
|
153
|
+
if (onDraw) onDraw();
|
|
154
|
+
else renderer.render(scene, camera);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function onControlsChange() {
|
|
158
|
+
clampCamera();
|
|
159
|
+
camera.lookAt(controls.target);
|
|
160
|
+
if (!onDraw) renderer.render(scene, camera);
|
|
161
|
+
}
|
|
162
|
+
controls.addEventListener('change', onControlsChange);
|
|
163
|
+
|
|
164
|
+
function selectView(name) {
|
|
165
|
+
const spec = views[name];
|
|
166
|
+
if (!spec) return;
|
|
167
|
+
controls.target.fromArray(spec.target);
|
|
168
|
+
camera.position.fromArray(spec.eye);
|
|
169
|
+
clampCamera();
|
|
170
|
+
controls.update();
|
|
171
|
+
camera.lookAt(controls.target);
|
|
172
|
+
for (const btn of buttons) {
|
|
173
|
+
if (btn.dataset?.view in views) {
|
|
174
|
+
btn.setAttribute('aria-pressed', String(btn.dataset.view === name));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (!onDraw) renderer.render(scene, camera);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const buttonListeners = [];
|
|
181
|
+
for (const btn of buttons) {
|
|
182
|
+
if (btn.dataset?.view in views) {
|
|
183
|
+
const onClick = () => selectView(btn.dataset.view);
|
|
184
|
+
btn.addEventListener('click', onClick);
|
|
185
|
+
buttonListeners.push([btn, onClick]);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const unbindKeys = bindKeyboardOrbit({ canvas, camera, controls, minDistance, maxDistance });
|
|
190
|
+
|
|
191
|
+
function resize() {
|
|
192
|
+
const dpr = typeof devicePixelRatio === 'number' ? devicePixelRatio : 1;
|
|
193
|
+
renderer.setPixelRatio(Math.min(dpr, maxPixelRatio));
|
|
194
|
+
renderer.setSize(canvas.clientWidth, canvas.clientHeight, false);
|
|
195
|
+
camera.aspect = canvas.clientWidth / Math.max(1, canvas.clientHeight);
|
|
196
|
+
camera.fov = THREE.MathUtils.clamp(baseFov / Math.min(camera.aspect, 1), baseFov, maxFov);
|
|
197
|
+
camera.updateProjectionMatrix();
|
|
198
|
+
render();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const hasWindow = typeof window !== 'undefined' && typeof window.addEventListener === 'function';
|
|
202
|
+
if (hasWindow) window.addEventListener('resize', resize);
|
|
203
|
+
const first = initialView ?? Object.keys(views)[0];
|
|
204
|
+
if (first) selectView(first);
|
|
205
|
+
resize();
|
|
206
|
+
|
|
207
|
+
function dispose() {
|
|
208
|
+
if (hasWindow) window.removeEventListener('resize', resize);
|
|
209
|
+
controls.removeEventListener('change', onControlsChange);
|
|
210
|
+
for (const [btn, onClick] of buttonListeners) btn.removeEventListener('click', onClick);
|
|
211
|
+
unbindKeys();
|
|
212
|
+
controls.dispose();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return { controls, selectView, resize, clampCamera, dispose };
|
|
216
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lightweight single-plane procedural water without planar reflections: vertex-coloured depth
|
|
3
|
+
* tinting from a caller-supplied bed depth function plus animated gentle surface wave shimmer.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as THREE from 'three';
|
|
8
|
+
import { glslVec3 } from './glsl-literal.js';
|
|
9
|
+
|
|
10
|
+
const WATER_VERTEX_PARS = /* glsl */ `varying vec2 vWaterXZ;
|
|
11
|
+
`;
|
|
12
|
+
const WATER_VERTEX_BEGIN = /* glsl */ `#include <begin_vertex>
|
|
13
|
+
vWaterXZ = position.xz;`;
|
|
14
|
+
|
|
15
|
+
const WATER_FRAGMENT_PARS = /* glsl */ `uniform float uWaterTime;
|
|
16
|
+
varying vec2 vWaterXZ;
|
|
17
|
+
`;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Builds a single horizontal water plane tinted by water depth with gentle animated wave glint,
|
|
21
|
+
* requiring no planar reflection pass or depth render target.
|
|
22
|
+
*
|
|
23
|
+
* @param {object} [options]
|
|
24
|
+
* @param {number} [options.width=800] Plane width along X (metres).
|
|
25
|
+
* @param {number} [options.depth=800] Plane extent along Z (metres).
|
|
26
|
+
* @param {number} [options.segs=128] Grid subdivisions along each axis.
|
|
27
|
+
* @param {number} [options.level=0] Water surface elevation Y (metres).
|
|
28
|
+
* @param {(x: number, z: number) => number} [options.depthAt] Water depth in metres at local `(x, z)`; defaults to constant `6`.
|
|
29
|
+
* @param {number} [options.shallowColor=0x6ea6a0] Shallow-water colour (sRGB hex).
|
|
30
|
+
* @param {number} [options.deepColor=0x245d6d] Deep-water colour (sRGB hex).
|
|
31
|
+
* @param {number} [options.shallowDepth=0.1] Depth (metres) where colour begins transitioning from shallow to deep.
|
|
32
|
+
* @param {number} [options.deepDepth=4.5] Depth (metres) where colour reaches full deep tint.
|
|
33
|
+
* @param {number} [options.roughness=0.24] Surface roughness for specular sun highlights.
|
|
34
|
+
* @param {number} [options.metalness=0.08] Surface metalness.
|
|
35
|
+
* @param {number} [options.opacity=0.9] Surface opacity (`< 1` enables alpha blending).
|
|
36
|
+
* @param {[number, number, number]} [options.glintColor=[0.08, 0.13, 0.14]] Additive RGB crest shimmer tint.
|
|
37
|
+
* @returns {{ mesh: THREE.Mesh, material: THREE.MeshStandardMaterial, timeUniform: { value: number }, update: (elapsed: number) => void, dispose: () => void }}
|
|
38
|
+
*/
|
|
39
|
+
export function createSimpleWater({
|
|
40
|
+
width = 800,
|
|
41
|
+
depth = 800,
|
|
42
|
+
segs = 128,
|
|
43
|
+
level = 0,
|
|
44
|
+
depthAt = () => 6,
|
|
45
|
+
shallowColor = 0x6ea6a0,
|
|
46
|
+
deepColor = 0x245d6d,
|
|
47
|
+
shallowDepth = 0.1,
|
|
48
|
+
deepDepth = 4.5,
|
|
49
|
+
roughness = 0.24,
|
|
50
|
+
metalness = 0.08,
|
|
51
|
+
opacity = 0.9,
|
|
52
|
+
glintColor = [0.08, 0.13, 0.14],
|
|
53
|
+
} = {}) {
|
|
54
|
+
const geometry = new THREE.PlaneGeometry(width, depth, segs, segs).rotateX(-Math.PI / 2);
|
|
55
|
+
const positions = geometry.attributes.position;
|
|
56
|
+
const colors = new Float32Array(positions.count * 3);
|
|
57
|
+
const shallow = new THREE.Color(shallowColor);
|
|
58
|
+
const deep = new THREE.Color(deepColor);
|
|
59
|
+
const tint = new THREE.Color();
|
|
60
|
+
for (let i = 0; i < positions.count; i++) {
|
|
61
|
+
const waterDepth = depthAt(positions.getX(i), positions.getZ(i));
|
|
62
|
+
const mix = THREE.MathUtils.smoothstep(waterDepth, shallowDepth, deepDepth);
|
|
63
|
+
tint
|
|
64
|
+
.copy(shallow)
|
|
65
|
+
.lerp(deep, mix)
|
|
66
|
+
.toArray(colors, i * 3);
|
|
67
|
+
}
|
|
68
|
+
geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3));
|
|
69
|
+
|
|
70
|
+
const material = new THREE.MeshStandardMaterial({
|
|
71
|
+
vertexColors: true,
|
|
72
|
+
roughness,
|
|
73
|
+
metalness,
|
|
74
|
+
transparent: opacity < 1,
|
|
75
|
+
opacity,
|
|
76
|
+
});
|
|
77
|
+
const timeUniform = { value: 0 };
|
|
78
|
+
const glintLiteral = glslVec3(glintColor);
|
|
79
|
+
const fragmentColor = /* glsl */ `#include <color_fragment>
|
|
80
|
+
vec2 wp = vWaterXZ;
|
|
81
|
+
float warp = sin(wp.x * 0.17 + wp.y * 0.13 + uWaterTime * 0.35) * 2.1
|
|
82
|
+
+ cos(wp.x * 0.09 - wp.y * 0.23 - uWaterTime * 0.28) * 1.6;
|
|
83
|
+
float swell = sin(wp.y * 0.85 + warp + uWaterTime * 1.05) * 0.52
|
|
84
|
+
+ sin(wp.y * 1.75 + wp.x * 0.19 - warp * 0.65 - uWaterTime * 1.35) * 0.33
|
|
85
|
+
+ sin(wp.y * 3.4 - wp.x * 0.27 + warp * 0.4 + uWaterTime * 1.8) * 0.15;
|
|
86
|
+
float shimmer = 0.45 + 0.55 * smoothstep(-0.3, 0.75, sin(wp.x * 0.29 + warp * 0.65));
|
|
87
|
+
float glint = smoothstep(0.54, 0.94, swell) * shimmer;
|
|
88
|
+
float vis = 1.0 - smoothstep(0.08, 0.5, fwidth(wp.y));
|
|
89
|
+
diffuseColor.rgb += ${glintLiteral} * glint * vis;`;
|
|
90
|
+
|
|
91
|
+
material.onBeforeCompile = shader => {
|
|
92
|
+
shader.uniforms.uWaterTime = timeUniform;
|
|
93
|
+
shader.vertexShader = WATER_VERTEX_PARS + shader.vertexShader.replace('#include <begin_vertex>', WATER_VERTEX_BEGIN);
|
|
94
|
+
shader.fragmentShader = WATER_FRAGMENT_PARS + shader.fragmentShader.replace('#include <color_fragment>', fragmentColor);
|
|
95
|
+
};
|
|
96
|
+
material.customProgramCacheKey = () => `roamwright-simple-water-v1:${glintLiteral}`;
|
|
97
|
+
|
|
98
|
+
const mesh = new THREE.Mesh(geometry, material);
|
|
99
|
+
mesh.position.y = level;
|
|
100
|
+
mesh.receiveShadow = true;
|
|
101
|
+
|
|
102
|
+
function update(elapsed) {
|
|
103
|
+
timeUniform.value = elapsed;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function dispose() {
|
|
107
|
+
geometry.dispose();
|
|
108
|
+
material.dispose();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return { mesh, material, timeUniform, update, dispose };
|
|
112
|
+
}
|