roamwright 0.55.1 → 0.57.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/README.md +2 -1
- 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/pacing.html +100 -0
- package/examples/pacing.js +305 -0
- 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 +19 -2
- 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/perf-box-worker.js +95 -0
- package/rendering/perf-box.js +94 -0
- package/rendering/perf-probe-passes.js +333 -0
- package/rendering/perf-probe.js +406 -0
- package/rendering/water-simple.js +112 -0
- package/runtime/device-quality.js +75 -4
- package/runtime/perf-box-text.js +50 -0
- package/runtime/perf-probe.js +145 -0
- package/runtime/perf-watch.js +158 -0
- package/runtime/quality-fit.js +25 -5
- package/runtime/quality-store.js +37 -9
package/examples/stage.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import * as THREE from 'three';
|
|
2
|
-
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
|
3
2
|
import { createNoise, createRenderContext, createUnitShapes, mulberry32 } from 'roamwright';
|
|
4
3
|
import { createHash } from '../math/random.js';
|
|
5
4
|
|
|
6
|
-
// Shared
|
|
5
|
+
// Shared seed reading, example context bootstrap, and rolling ground helper for the example pages.
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Read the integer `?seed=` query parameter and sync the header `#seed` input.
|
|
@@ -30,129 +29,6 @@ export function initExample({ fallbackSeed = 42, fov = 42 } = {}) {
|
|
|
30
29
|
return { canvas, ...ctx, seed, random, noise, hash, shapes };
|
|
31
30
|
}
|
|
32
31
|
|
|
33
|
-
/**
|
|
34
|
-
* Wraps an RGBA pixel array into an equirectangular sky DataTexture.
|
|
35
|
-
*/
|
|
36
|
-
export function createSkyTexture(pixels, width, height) {
|
|
37
|
-
const sky = new THREE.DataTexture(pixels, width, height);
|
|
38
|
-
sky.colorSpace = THREE.SRGBColorSpace;
|
|
39
|
-
sky.mapping = THREE.EquirectangularReflectionMapping;
|
|
40
|
-
sky.minFilter = sky.magFilter = THREE.LinearFilter;
|
|
41
|
-
sky.needsUpdate = true;
|
|
42
|
-
return sky;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Painted equirectangular sky texture with soft horizon warmth, wispy high clouds and optional dusk/night stars.
|
|
47
|
-
*/
|
|
48
|
-
export function makeSky(
|
|
49
|
-
noise,
|
|
50
|
-
{
|
|
51
|
-
zenith = [0.27, 0.49, 0.72],
|
|
52
|
-
horizon = [0.82, 0.86, 0.84],
|
|
53
|
-
ground = [0.55, 0.56, 0.5],
|
|
54
|
-
cloudColor = [0.99, 0.95, 0.87],
|
|
55
|
-
sunGlow = [1.0, 0.84, 0.58],
|
|
56
|
-
sunDir = [-0.72, 0.28, -0.63],
|
|
57
|
-
cloudCover = 0.53,
|
|
58
|
-
stars = 0,
|
|
59
|
-
} = {},
|
|
60
|
-
) {
|
|
61
|
-
const width = 512,
|
|
62
|
-
height = 256;
|
|
63
|
-
const pixels = new Uint8Array(width * height * 4);
|
|
64
|
-
const sun = new THREE.Vector3(...sunDir).normalize();
|
|
65
|
-
for (let y = 0; y < height; y++) {
|
|
66
|
-
const lat = Math.PI * ((y + 0.5) / height - 0.5);
|
|
67
|
-
const up = Math.sin(lat),
|
|
68
|
-
ring = Math.cos(lat);
|
|
69
|
-
const skyMix = THREE.MathUtils.smoothstep(up, -0.04, 0.74);
|
|
70
|
-
const belowMix = THREE.MathUtils.smoothstep(-up, 0.0, 0.35);
|
|
71
|
-
for (let x = 0; x < width; x++) {
|
|
72
|
-
const lon = 2 * Math.PI * ((x + 0.5) / width - 0.5);
|
|
73
|
-
const ex = ring * Math.cos(lon),
|
|
74
|
-
ez = ring * Math.sin(lon);
|
|
75
|
-
const air = Math.max(0.09, up);
|
|
76
|
-
const cNoise = noise.fbm((ex / air) * 1.25, (ez / air) * 2.4, 4, 71);
|
|
77
|
-
const veil = THREE.MathUtils.smoothstep(cNoise, cloudCover, cloudCover + 0.24) * THREE.MathUtils.smoothstep(up, 0.01, 0.16);
|
|
78
|
-
const sd = Math.max(0, ex * sun.x + up * sun.y + ez * sun.z);
|
|
79
|
-
const glow = Math.pow(sd, 18) * 0.26 + Math.pow(sd, 140) * 0.65;
|
|
80
|
-
let r = horizon[0] + (zenith[0] - horizon[0]) * skyMix;
|
|
81
|
-
let g = horizon[1] + (zenith[1] - horizon[1]) * skyMix;
|
|
82
|
-
let b = horizon[2] + (zenith[2] - horizon[2]) * skyMix;
|
|
83
|
-
if (up < 0) {
|
|
84
|
-
r += (ground[0] - r) * belowMix;
|
|
85
|
-
g += (ground[1] - g) * belowMix;
|
|
86
|
-
b += (ground[2] - b) * belowMix;
|
|
87
|
-
}
|
|
88
|
-
r = r * (1 - veil) + cloudColor[0] * veil + sunGlow[0] * glow;
|
|
89
|
-
g = g * (1 - veil) + cloudColor[1] * veil + sunGlow[1] * glow;
|
|
90
|
-
b = b * (1 - veil) + cloudColor[2] * veil + sunGlow[2] * glow;
|
|
91
|
-
if (stars > 0 && up > 0.05) {
|
|
92
|
-
const twinkle = noise.vnoise(ex * 95 + 11, ez * 95 + up * 73, 19);
|
|
93
|
-
if (twinkle > 0.945) {
|
|
94
|
-
const s = ((twinkle - 0.945) / 0.055) * stars * THREE.MathUtils.smoothstep(up, 0.05, 0.3) * (1 - veil);
|
|
95
|
-
r += s * 0.9;
|
|
96
|
-
g += s * 0.94;
|
|
97
|
-
b += s;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
const i = (y * width + x) * 4;
|
|
101
|
-
pixels[i] = Math.min(255, Math.max(0, Math.round(r * 255)));
|
|
102
|
-
pixels[i + 1] = Math.min(255, Math.max(0, Math.round(g * 255)));
|
|
103
|
-
pixels[i + 2] = Math.min(255, Math.max(0, Math.round(b * 255)));
|
|
104
|
-
pixels[i + 3] = 255;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return createSkyTexture(pixels, width, height);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Low-angle sunlight with crisp shadows and warm/cool hemisphere fill.
|
|
112
|
-
*/
|
|
113
|
-
export function setupSun(
|
|
114
|
-
scene,
|
|
115
|
-
renderer,
|
|
116
|
-
{
|
|
117
|
-
sunPosition = [-65, 38, 48],
|
|
118
|
-
sunColor = 0xffe2b8,
|
|
119
|
-
sunIntensity = 2.6,
|
|
120
|
-
skyColor = 0xddeaf5,
|
|
121
|
-
groundColor = 0x6b6752,
|
|
122
|
-
hemiIntensity = 1.25,
|
|
123
|
-
shadowRadius = 70,
|
|
124
|
-
shadowFar = 240,
|
|
125
|
-
shadowMapSize = 2048,
|
|
126
|
-
normalBias = 0.045,
|
|
127
|
-
staticShadows = false,
|
|
128
|
-
} = {},
|
|
129
|
-
) {
|
|
130
|
-
renderer.shadowMap.enabled = true;
|
|
131
|
-
renderer.shadowMap.type = THREE.PCFShadowMap;
|
|
132
|
-
const hemi = new THREE.HemisphereLight(skyColor, groundColor, hemiIntensity);
|
|
133
|
-
scene.add(hemi);
|
|
134
|
-
const sun = new THREE.DirectionalLight(sunColor, sunIntensity);
|
|
135
|
-
sun.position.set(...sunPosition);
|
|
136
|
-
sun.castShadow = true;
|
|
137
|
-
sun.shadow.mapSize.set(shadowMapSize, shadowMapSize);
|
|
138
|
-
Object.assign(sun.shadow.camera, {
|
|
139
|
-
left: -shadowRadius,
|
|
140
|
-
right: shadowRadius,
|
|
141
|
-
top: shadowRadius,
|
|
142
|
-
bottom: -shadowRadius,
|
|
143
|
-
near: 1,
|
|
144
|
-
far: shadowFar,
|
|
145
|
-
});
|
|
146
|
-
sun.shadow.camera.updateProjectionMatrix();
|
|
147
|
-
sun.shadow.normalBias = normalBias;
|
|
148
|
-
scene.add(sun);
|
|
149
|
-
if (staticShadows) {
|
|
150
|
-
renderer.shadowMap.autoUpdate = false;
|
|
151
|
-
renderer.shadowMap.needsUpdate = true;
|
|
152
|
-
}
|
|
153
|
-
return { sun, hemi };
|
|
154
|
-
}
|
|
155
|
-
|
|
156
32
|
/**
|
|
157
33
|
* Vertex-colored rolling terrain mesh with natural variation.
|
|
158
34
|
*/
|
|
@@ -173,202 +49,3 @@ export function makeRollingGround({ size = 400, segs = 160, heightAt, colorAt, r
|
|
|
173
49
|
mesh.receiveShadow = true;
|
|
174
50
|
return mesh;
|
|
175
51
|
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Shimmering procedural water mesh with soft ripples and depth tinting.
|
|
179
|
-
*/
|
|
180
|
-
export function makeWaterMesh({
|
|
181
|
-
width = 800,
|
|
182
|
-
depth = 800,
|
|
183
|
-
segs = 128,
|
|
184
|
-
level = 0,
|
|
185
|
-
depthAt = () => 6,
|
|
186
|
-
shallowColor = 0x6ea6a0,
|
|
187
|
-
deepColor = 0x245d6d,
|
|
188
|
-
opacity = 0.9,
|
|
189
|
-
} = {}) {
|
|
190
|
-
const geo = new THREE.PlaneGeometry(width, depth, segs, segs).rotateX(-Math.PI / 2);
|
|
191
|
-
const pos = geo.attributes.position;
|
|
192
|
-
const colors = new Float32Array(pos.count * 3);
|
|
193
|
-
const shallow = new THREE.Color(shallowColor),
|
|
194
|
-
deep = new THREE.Color(deepColor),
|
|
195
|
-
tint = new THREE.Color();
|
|
196
|
-
for (let i = 0; i < pos.count; i++) {
|
|
197
|
-
const d = depthAt(pos.getX(i), pos.getZ(i));
|
|
198
|
-
const mix = THREE.MathUtils.smoothstep(d, 0.1, 4.5);
|
|
199
|
-
tint
|
|
200
|
-
.copy(shallow)
|
|
201
|
-
.lerp(deep, mix)
|
|
202
|
-
.toArray(colors, i * 3);
|
|
203
|
-
}
|
|
204
|
-
geo.setAttribute('color', new THREE.BufferAttribute(colors, 3));
|
|
205
|
-
const mat = new THREE.MeshStandardMaterial({
|
|
206
|
-
vertexColors: true,
|
|
207
|
-
roughness: 0.24,
|
|
208
|
-
metalness: 0.08,
|
|
209
|
-
transparent: opacity < 1,
|
|
210
|
-
opacity,
|
|
211
|
-
});
|
|
212
|
-
const timeUniform = { value: 0 };
|
|
213
|
-
mat.onBeforeCompile = shader => {
|
|
214
|
-
shader.uniforms.uWaterTime = timeUniform;
|
|
215
|
-
shader.vertexShader =
|
|
216
|
-
'varying vec2 vWaterXZ;\n' +
|
|
217
|
-
shader.vertexShader.replace('#include <begin_vertex>', '#include <begin_vertex>\nvWaterXZ = position.xz;');
|
|
218
|
-
shader.fragmentShader =
|
|
219
|
-
'uniform float uWaterTime;\nvarying vec2 vWaterXZ;\n' +
|
|
220
|
-
shader.fragmentShader.replace(
|
|
221
|
-
'#include <color_fragment>',
|
|
222
|
-
`#include <color_fragment>
|
|
223
|
-
vec2 wp = vWaterXZ;
|
|
224
|
-
float warp = sin(wp.x * 0.17 + wp.y * 0.13 + uWaterTime * 0.35) * 2.1
|
|
225
|
-
+ cos(wp.x * 0.09 - wp.y * 0.23 - uWaterTime * 0.28) * 1.6;
|
|
226
|
-
float swell = sin(wp.y * 0.85 + warp + uWaterTime * 1.05) * 0.52
|
|
227
|
-
+ sin(wp.y * 1.75 + wp.x * 0.19 - warp * 0.65 - uWaterTime * 1.35) * 0.33
|
|
228
|
-
+ sin(wp.y * 3.4 - wp.x * 0.27 + warp * 0.4 + uWaterTime * 1.8) * 0.15;
|
|
229
|
-
float shimmer = 0.45 + 0.55 * smoothstep(-0.3, 0.75, sin(wp.x * 0.29 + warp * 0.65));
|
|
230
|
-
float glint = smoothstep(0.54, 0.94, swell) * shimmer;
|
|
231
|
-
float vis = 1.0 - smoothstep(0.08, 0.5, fwidth(wp.y));
|
|
232
|
-
diffuseColor.rgb += vec3(0.08, 0.13, 0.14) * glint * vis;`,
|
|
233
|
-
);
|
|
234
|
-
};
|
|
235
|
-
mat.customProgramCacheKey = () => 'showcase-water-v1';
|
|
236
|
-
const mesh = new THREE.Mesh(geo, mat);
|
|
237
|
-
mesh.position.y = level;
|
|
238
|
-
mesh.receiveShadow = true;
|
|
239
|
-
return { mesh, timeUniform };
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* Gives keyboard explorers arrow-key orbit and +/- zoom matching pointer OrbitControls.
|
|
244
|
-
*/
|
|
245
|
-
export function bindKeyboardOrbit({ canvas, camera, controls, minDistance = 0, maxDistance = Infinity }) {
|
|
246
|
-
const offset = new THREE.Vector3();
|
|
247
|
-
const orbit = new THREE.Spherical();
|
|
248
|
-
canvas.addEventListener('keydown', event => {
|
|
249
|
-
offset.copy(camera.position).sub(controls.target);
|
|
250
|
-
orbit.setFromVector3(offset);
|
|
251
|
-
switch (event.key) {
|
|
252
|
-
case 'ArrowLeft':
|
|
253
|
-
orbit.theta -= 0.1;
|
|
254
|
-
break;
|
|
255
|
-
case 'ArrowRight':
|
|
256
|
-
orbit.theta += 0.1;
|
|
257
|
-
break;
|
|
258
|
-
case 'ArrowUp':
|
|
259
|
-
orbit.phi -= 0.08;
|
|
260
|
-
break;
|
|
261
|
-
case 'ArrowDown':
|
|
262
|
-
orbit.phi += 0.08;
|
|
263
|
-
break;
|
|
264
|
-
case '+':
|
|
265
|
-
case '=':
|
|
266
|
-
orbit.radius = Math.max(minDistance, orbit.radius * 0.88);
|
|
267
|
-
break;
|
|
268
|
-
case '-':
|
|
269
|
-
orbit.radius = Math.min(maxDistance, orbit.radius / 0.88);
|
|
270
|
-
break;
|
|
271
|
-
default:
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
event.preventDefault();
|
|
275
|
-
camera.position.setFromSpherical(orbit.makeSafe()).add(controls.target);
|
|
276
|
-
controls.update();
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* OrbitControls with adaptive portrait FOV, ground clearance, keyboard orbit, and named [data-view] buttons.
|
|
282
|
-
*/
|
|
283
|
-
export function createOrbitRig({
|
|
284
|
-
camera,
|
|
285
|
-
canvas,
|
|
286
|
-
renderer,
|
|
287
|
-
scene,
|
|
288
|
-
views,
|
|
289
|
-
initialView,
|
|
290
|
-
groundAt = () => 0,
|
|
291
|
-
guardCamera = null,
|
|
292
|
-
minClearance = 1.0,
|
|
293
|
-
minDistance = 4,
|
|
294
|
-
maxDistance = 180,
|
|
295
|
-
baseFov = 42,
|
|
296
|
-
maxFov = 68,
|
|
297
|
-
maxPolarAngle = Math.PI * 0.485,
|
|
298
|
-
far = 2500,
|
|
299
|
-
onDraw = null,
|
|
300
|
-
}) {
|
|
301
|
-
const controls = new OrbitControls(camera, canvas);
|
|
302
|
-
controls.enableDamping = false;
|
|
303
|
-
controls.enablePan = false;
|
|
304
|
-
controls.maxPolarAngle = maxPolarAngle;
|
|
305
|
-
controls.minDistance = minDistance;
|
|
306
|
-
controls.maxDistance = maxDistance;
|
|
307
|
-
camera.far = far;
|
|
308
|
-
|
|
309
|
-
const viewButtons = Array.from(document.querySelectorAll('[data-view]'));
|
|
310
|
-
|
|
311
|
-
function clampCamera() {
|
|
312
|
-
if (guardCamera) {
|
|
313
|
-
guardCamera(camera);
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
const floor = groundAt(camera.position.x, camera.position.z);
|
|
317
|
-
if (Number.isFinite(floor) && camera.position.y < floor + minClearance) {
|
|
318
|
-
camera.position.y = floor + minClearance;
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
function render() {
|
|
323
|
-
clampCamera();
|
|
324
|
-
camera.lookAt(controls.target);
|
|
325
|
-
if (onDraw) onDraw();
|
|
326
|
-
else renderer.render(scene, camera);
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
controls.addEventListener('change', () => {
|
|
330
|
-
clampCamera();
|
|
331
|
-
camera.lookAt(controls.target);
|
|
332
|
-
if (!onDraw) renderer.render(scene, camera);
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
function selectView(name) {
|
|
336
|
-
const spec = views[name];
|
|
337
|
-
if (!spec) return;
|
|
338
|
-
controls.target.fromArray(spec.target);
|
|
339
|
-
camera.position.fromArray(spec.eye);
|
|
340
|
-
clampCamera();
|
|
341
|
-
controls.update();
|
|
342
|
-
camera.lookAt(controls.target);
|
|
343
|
-
for (const btn of viewButtons) {
|
|
344
|
-
if (btn.dataset.view in views) {
|
|
345
|
-
btn.setAttribute('aria-pressed', String(btn.dataset.view === name));
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
if (!onDraw) renderer.render(scene, camera);
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
for (const btn of viewButtons) {
|
|
352
|
-
if (btn.dataset.view in views) {
|
|
353
|
-
btn.addEventListener('click', () => selectView(btn.dataset.view));
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
bindKeyboardOrbit({ canvas, camera, controls, minDistance, maxDistance });
|
|
358
|
-
|
|
359
|
-
function resize() {
|
|
360
|
-
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
|
|
361
|
-
renderer.setSize(canvas.clientWidth, canvas.clientHeight, false);
|
|
362
|
-
camera.aspect = canvas.clientWidth / Math.max(1, canvas.clientHeight);
|
|
363
|
-
camera.fov = THREE.MathUtils.clamp(baseFov / Math.min(camera.aspect, 1), baseFov, maxFov);
|
|
364
|
-
camera.updateProjectionMatrix();
|
|
365
|
-
render();
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
window.addEventListener('resize', resize);
|
|
369
|
-
const first = initialView ?? Object.keys(views)[0];
|
|
370
|
-
if (first) selectView(first);
|
|
371
|
-
resize();
|
|
372
|
-
|
|
373
|
-
return { controls, selectView, resize, clampCamera };
|
|
374
|
-
}
|
package/examples/towns.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import * as THREE from 'three';
|
|
2
2
|
import {
|
|
3
|
+
createDaylightSky,
|
|
3
4
|
createHouseKit,
|
|
5
|
+
createHouseKitMaterials,
|
|
4
6
|
createHouseKitMeshes,
|
|
7
|
+
createOrbitCamera,
|
|
5
8
|
createQuarterStream,
|
|
6
9
|
createSettlementSwitch,
|
|
10
|
+
createSkyPanorama,
|
|
7
11
|
createStreetLamps,
|
|
8
12
|
createTowns,
|
|
9
13
|
createWallRuns,
|
|
@@ -15,8 +19,8 @@ import { createPieceBatch, TIGHT } from 'roamwright/rendering/piece-batch.js';
|
|
|
15
19
|
import { C, TRS } from 'roamwright/geometry/primitives.js';
|
|
16
20
|
import { litShare } from 'roamwright/math/lit-share.js';
|
|
17
21
|
import { sstep } from 'roamwright/math/scalar.js';
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
22
|
+
import { initExample, makeRollingGround } from './stage.js';
|
|
23
|
+
import { scatterTrees } from './stage-kits.js';
|
|
20
24
|
|
|
21
25
|
const { canvas, renderer, camera, scene, worldGroup, seed, random, noise, hash, shapes } = initExample({
|
|
22
26
|
fallbackSeed: 7,
|
|
@@ -95,7 +99,7 @@ const styles = {
|
|
|
95
99
|
},
|
|
96
100
|
};
|
|
97
101
|
const houseKit = createHouseKit({ shapes, styles });
|
|
98
|
-
const materials =
|
|
102
|
+
const materials = createHouseKitMaterials({ roughness: 0.9 });
|
|
99
103
|
const glass = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.2, emissive: 0xffb058, emissiveIntensity: 0 });
|
|
100
104
|
materials.glass = glass;
|
|
101
105
|
const far = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.95 });
|
|
@@ -154,11 +158,7 @@ layVillage({
|
|
|
154
158
|
});
|
|
155
159
|
town.build(worldGroup);
|
|
156
160
|
town.buildFar(worldGroup);
|
|
157
|
-
const { kitStatic } = createHouseKitMeshes({
|
|
158
|
-
materials,
|
|
159
|
-
PieceBatch,
|
|
160
|
-
handOver: SYNC_HAND_OVER,
|
|
161
|
-
});
|
|
161
|
+
const { kitStatic } = createHouseKitMeshes({ materials, PieceBatch });
|
|
162
162
|
kitStatic(builder, near);
|
|
163
163
|
|
|
164
164
|
// A stone-walled courtyard at the eastern end of the village.
|
|
@@ -209,8 +209,22 @@ const { update: updateSettlements } = createSettlementSwitch({
|
|
|
209
209
|
settlements: [{ x: 0, z: 0, range: 460, town, near: [near] }],
|
|
210
210
|
});
|
|
211
211
|
|
|
212
|
-
const
|
|
213
|
-
|
|
212
|
+
const {
|
|
213
|
+
sky: daySky,
|
|
214
|
+
sun,
|
|
215
|
+
hemi,
|
|
216
|
+
} = createDaylightSky({
|
|
217
|
+
scene,
|
|
218
|
+
renderer,
|
|
219
|
+
noise,
|
|
220
|
+
sky: { sunDir: [-0.72, 0.28, 0.63] },
|
|
221
|
+
sunPosition: [-95, 56, 72],
|
|
222
|
+
shadowRadius: 150,
|
|
223
|
+
shadowFar: 420,
|
|
224
|
+
fogColor: 0xd0dcd8,
|
|
225
|
+
fogDensity: 0.0014,
|
|
226
|
+
});
|
|
227
|
+
const nightSky = createSkyPanorama(noise, {
|
|
214
228
|
zenith: [0.04, 0.07, 0.15],
|
|
215
229
|
horizon: [0.1, 0.14, 0.24],
|
|
216
230
|
ground: [0.06, 0.08, 0.12],
|
|
@@ -221,13 +235,6 @@ const nightSky = makeSky(noise, {
|
|
|
221
235
|
stars: 0.85,
|
|
222
236
|
});
|
|
223
237
|
|
|
224
|
-
const { sun, hemi } = setupSun(scene, renderer, {
|
|
225
|
-
sunPosition: [-95, 56, 72],
|
|
226
|
-
shadowRadius: 150,
|
|
227
|
-
shadowFar: 420,
|
|
228
|
-
});
|
|
229
|
-
scene.fog = new THREE.FogExp2(0xd0dcd8, 0.0014);
|
|
230
|
-
|
|
231
238
|
const hourBtn = document.querySelector('#hour');
|
|
232
239
|
function applyHour() {
|
|
233
240
|
lightUniforms.night.value = night ? 1 : 0;
|
|
@@ -262,7 +269,7 @@ let flying = false,
|
|
|
262
269
|
const flyFrom = new THREE.Vector3(-280, 75, 190),
|
|
263
270
|
flyTo = new THREE.Vector3(-54, 17, 44);
|
|
264
271
|
|
|
265
|
-
const rig =
|
|
272
|
+
const rig = createOrbitCamera({
|
|
266
273
|
camera,
|
|
267
274
|
canvas,
|
|
268
275
|
renderer,
|
package/examples/viaduct.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as THREE from 'three';
|
|
2
|
-
import { GeometryBuilder } from 'roamwright';
|
|
2
|
+
import { createOrbitCamera, GeometryBuilder } from 'roamwright';
|
|
3
3
|
import { heightfieldSampler } from 'roamwright/geometry/heightfield.js';
|
|
4
4
|
import { createCameraGuard } from './viaduct-camera.js';
|
|
5
5
|
import { alpineRiver, branchWhorl, valleySky } from './viaduct-landscape.js';
|
|
6
|
-
import {
|
|
6
|
+
import { initExample } from './stage.js';
|
|
7
7
|
import { C, stick, TRS } from 'roamwright/geometry/primitives.js';
|
|
8
8
|
|
|
9
9
|
const { canvas, renderer, camera, scene, worldGroup, random, noise, shapes } = initExample({ fallbackSeed: 42, fov: 41 });
|
|
@@ -265,7 +265,7 @@ const viewpoints = {
|
|
|
265
265
|
trackside: { eye: [29, 25, 14], target: [1, 20.5, 0] },
|
|
266
266
|
riverbank: { eye: [13, 5.3, 38], target: [-2, 11.8, -6] },
|
|
267
267
|
};
|
|
268
|
-
|
|
268
|
+
createOrbitCamera({
|
|
269
269
|
camera,
|
|
270
270
|
canvas,
|
|
271
271
|
renderer,
|
|
@@ -290,7 +290,6 @@ function moveTrain(time) {
|
|
|
290
290
|
lastTime = time;
|
|
291
291
|
train.position.x += elapsed * 6;
|
|
292
292
|
if (train.position.x >= 80) train.position.x -= 160;
|
|
293
|
-
train.updateMatrix();
|
|
294
293
|
renderer.render(scene, camera);
|
|
295
294
|
frame = requestAnimationFrame(moveTrain);
|
|
296
295
|
}
|
package/geometry/builder.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import * as THREE from 'three';
|
|
7
|
-
import {
|
|
7
|
+
import { stillMesh } from './still-mesh.js';
|
|
8
8
|
import { box, TRS } from './primitives.js';
|
|
9
9
|
|
|
10
10
|
// ════════════════════════════════════════════════════════════════════════
|
|
@@ -84,19 +84,9 @@ function geometryOf(parts, sphere = null) {
|
|
|
84
84
|
return geometry;
|
|
85
85
|
}
|
|
86
86
|
// A mesh of a builder's arrays (GB.parts), as GB.mesh makes one, in `parent`
|
|
87
|
-
function meshOf(parts, material, parent, shadow = true) {
|
|
88
|
-
const mesh = stillMesh(geometryOf(parts), material, shadow);
|
|
89
|
-
parent.add(mesh);
|
|
90
|
-
return mesh;
|
|
91
|
-
}
|
|
92
|
-
// A static mesh with its local transform fixed and optional shadow casting.
|
|
93
|
-
function stillMesh(geometry, material, shadow) {
|
|
94
|
-
const mesh = new THREE.Mesh(geometry, material);
|
|
95
|
-
mesh.castShadow = shadow;
|
|
96
|
-
mesh.receiveShadow = true;
|
|
97
|
-
mesh.matrixAutoUpdate = false;
|
|
98
|
-
mesh.updateMatrix();
|
|
99
|
-
if (shadow) markStill(mesh);
|
|
87
|
+
function meshOf(parts, material, parent, shadow = true, options) {
|
|
88
|
+
const mesh = stillMesh(geometryOf(parts), material, shadow, options);
|
|
89
|
+
if (parent) parent.add(mesh);
|
|
100
90
|
return mesh;
|
|
101
91
|
}
|
|
102
92
|
// Share GPU buffers and bounds while giving each instanced mesh a geometry identity
|
|
@@ -554,13 +544,13 @@ class GB {
|
|
|
554
544
|
if (this.skw) this.skw = [];
|
|
555
545
|
this.spent = true;
|
|
556
546
|
}
|
|
557
|
-
/** Build a
|
|
558
|
-
mesh(material, parent, shadow = true) {
|
|
559
|
-
const mesh = stillMesh(this.build(), material, shadow);
|
|
547
|
+
/** Build a mesh, release the builder’s backing arrays and attach the mesh to the optional parent. */
|
|
548
|
+
mesh(material, parent, shadow = true, options) {
|
|
549
|
+
const mesh = stillMesh(this.build(), material, shadow, options);
|
|
560
550
|
this.release();
|
|
561
551
|
if (parent) parent.add(mesh);
|
|
562
552
|
return mesh;
|
|
563
553
|
}
|
|
564
554
|
}
|
|
565
555
|
|
|
566
|
-
export { GB, geometryOf, geometryView, meshOf, releaseArray, sphereOf };
|
|
556
|
+
export { GB, geometryOf, geometryView, meshOf, releaseArray, sphereOf, stillMesh };
|
|
@@ -11,7 +11,7 @@ import { between, direction, lifted, moved } from './plant-shrub.js';
|
|
|
11
11
|
* @param {Function} o.barkTint - The kit's bark tint (`createPlantTints`).
|
|
12
12
|
* @param {Function} o.crownBlob - The kit's crown lump (`createPlantTints`).
|
|
13
13
|
* @param {Function} o.crownTint - The kit's foliage tint (`createPlantTints`).
|
|
14
|
-
* @returns {{broadleaf: Function, broadleafArrays: Function}} `broadleaf(
|
|
14
|
+
* @returns {{broadleaf: Function, broadleafArrays: Function}} `broadleaf()`: `{ near, far }` parts for
|
|
15
15
|
* `partsGeometry`, its foot at the origin; `broadleafArrays()`: the near tree as transferable arrays (`partsArrays`).
|
|
16
16
|
*/
|
|
17
17
|
export function createBroadleaf({ barkTint, crownBlob, crownTint }) {
|
|
@@ -214,10 +214,10 @@ export function createBroadleaf({ barkTint, crownBlob, crownTint }) {
|
|
|
214
214
|
|
|
215
215
|
// A broadleaf: its near version, or with `far` its far one, which keeps the trunk and the boughs and gathers each bough's
|
|
216
216
|
// clumps into one, a fifth of the near tree's triangles.
|
|
217
|
-
function
|
|
217
|
+
function buildBroadleaf(far, plan = broadleafPlan()) {
|
|
218
218
|
const parts = shapeParts(),
|
|
219
219
|
bark = barkTint(BROAD_BARK),
|
|
220
|
-
{ limbs, clumps, sprays } =
|
|
220
|
+
{ limbs, clumps, sprays } = plan,
|
|
221
221
|
sides = far ? [5, 3, 0, 0] : [8, 6, 4, 3];
|
|
222
222
|
for (const [from, to, foot, tip, rank] of limbs) if (sides[rank]) addLimb(parts, from, to, foot, tip, sides[rank], bark);
|
|
223
223
|
const putClump = (at, size, flat, turn, hex, seed, detail) => {
|
|
@@ -261,8 +261,14 @@ export function createBroadleaf({ barkTint, crownBlob, crownTint }) {
|
|
|
261
261
|
return parts;
|
|
262
262
|
}
|
|
263
263
|
|
|
264
|
+
// Returns both detail levels from one shared scaffold plan, matching every other tree builder's `{ near, far }` shape.
|
|
265
|
+
function broadleaf() {
|
|
266
|
+
const plan = broadleafPlan();
|
|
267
|
+
return { near: buildBroadleaf(false, plan), far: buildBroadleaf(true, plan) };
|
|
268
|
+
}
|
|
269
|
+
|
|
264
270
|
// The broadleaf as the arrays of its geometry (foliage.js's partsArrays), for the pool's workers to hand over: the model is
|
|
265
271
|
// too large to build in one frame on a page that draws.
|
|
266
|
-
const broadleafArrays = () => partsArrays(
|
|
272
|
+
const broadleafArrays = () => partsArrays(buildBroadleaf(false));
|
|
267
273
|
return { broadleaf, broadleafArrays };
|
|
268
274
|
}
|
package/geometry/plant-trees.js
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
|
-
// Trees of the plant kit,
|
|
2
|
-
//
|
|
1
|
+
// Trees of the plant kit, each returning `{ near, far }` parts for `partsGeometry`, with its foot at the origin:
|
|
2
|
+
// the broadleaf, conifer, burnt tree, thorn tree, park acacia and cedar. Worker-safe.
|
|
3
3
|
import * as THREE from 'three';
|
|
4
4
|
import { lerp, TAU } from '../math/scalar.js';
|
|
5
5
|
import { mulberry32 } from '../math/random.js';
|
|
6
6
|
import { C, TRS } from './primitives.js';
|
|
7
7
|
import { addShape, shapeParts } from './foliage.js';
|
|
8
|
+
import { createBroadleaf } from './plant-broadleaf.js';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* The kit's trees bound to a world's hash, seed and the kit's tints.
|
|
12
|
+
* Every species builder returns `{ near, far }` parts suitable for `partsGeometry`.
|
|
13
|
+
*
|
|
11
14
|
* @param {object} o
|
|
12
15
|
* @param {Function} o.hash - A seeded integer hash (`createHash`), which mottles needles and char.
|
|
13
16
|
* @param {Function} o.barkTint - The kit's bark tint (`createPlantTints`).
|
|
14
17
|
* @param {Function} o.crownBlob - The kit's crown lump (`createPlantTints`).
|
|
15
18
|
* @param {Function} o.crownTint - The kit's foliage tint (`createPlantTints`).
|
|
16
19
|
* @param {number} [o.seed=0] - The world's seed: the burnt, thorn, acacia and cedar trees grow from streams counted from it.
|
|
17
|
-
* @returns {{
|
|
18
|
-
*
|
|
19
|
-
* parts each (about 10, 9.5 and 5 m tall); `thornTree()`, `acacia()` and `cedar(variant = 0)`: `{ near, far }`, the cedar
|
|
20
|
-
* about fourteen metres tall and each `variant` a tree of its own.
|
|
20
|
+
* @returns {{broadleaf: Function, conifer: Function, burntTree: Function, thornTree: Function, acacia: Function, cedar: Function}}
|
|
21
|
+
* Every tree builder returns `{ near, far }`; `cedar(variant = 0)` builds an upland cedar about fourteen metres tall where each `variant` is a tree of its own.
|
|
21
22
|
*/
|
|
22
23
|
export function createTrees({ hash: hash2, barkTint, crownBlob, crownTint, seed = 0 }) {
|
|
24
|
+
const { broadleaf } = createBroadleaf({ barkTint, crownBlob, crownTint });
|
|
23
25
|
const LIMB_NEAR = new THREE.CylinderGeometry(0.62, 1, 1, 7, 1, true).translate(0, 0.5, 0), // a limb of radius 1 at its foot, a metre long
|
|
24
26
|
LIMB_FAR = new THREE.CylinderGeometry(0.62, 1, 1, 4, 1, true).translate(0, 0.5, 0),
|
|
25
27
|
FAR_PLATE = new THREE.Vector3(1.3, 1, 1.3), // a far cedar's plates, one to a limb, stand for the near tree's three
|
|
@@ -36,7 +38,7 @@ export function createTrees({ hash: hash2, barkTint, crownBlob, crownTint, seed
|
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
// A conifer: a tapering trunk to the top, and whorl after whorl of separate drooping boughs, long at the base and short at the leader.
|
|
39
|
-
function
|
|
41
|
+
function nearConifer() {
|
|
40
42
|
const near = shapeParts();
|
|
41
43
|
const boughGeo = (() => {
|
|
42
44
|
// one bough along +z: a tapered spray, thicker above than below, its tip hanging
|
|
@@ -142,7 +144,7 @@ export function createTrees({ hash: hash2, barkTint, crownBlob, crownTint, seed
|
|
|
142
144
|
}
|
|
143
145
|
|
|
144
146
|
// A burnt tree: a fire-killed trunk, bent and blackened, its top snapped off; charred limbs forking into bare twigs, some burnt back to stubs.
|
|
145
|
-
function
|
|
147
|
+
function nearBurntTree() {
|
|
146
148
|
const near = shapeParts();
|
|
147
149
|
const R = mulberry32(seed + 611),
|
|
148
150
|
up = new THREE.Vector3(0, 1, 0),
|
|
@@ -449,5 +451,9 @@ export function createTrees({ hash: hash2, barkTint, crownBlob, crownTint, seed
|
|
|
449
451
|
}
|
|
450
452
|
return parts;
|
|
451
453
|
}
|
|
452
|
-
|
|
454
|
+
|
|
455
|
+
const conifer = () => ({ near: nearConifer(), far: farConifer() });
|
|
456
|
+
const burntTree = () => ({ near: nearBurntTree(), far: farBurntTree() });
|
|
457
|
+
|
|
458
|
+
return { broadleaf, conifer, burntTree, thornTree, acacia, cedar };
|
|
453
459
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper creating a mesh with shadow defaults, optional static-caster layer marking, and
|
|
3
|
+
* configurable `matrixAutoUpdate` (enabled by default so subsequent `position`/`rotation`/`scale`
|
|
4
|
+
* edits take effect without manual `updateMatrix()` calls).
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as THREE from 'three';
|
|
9
|
+
import { markStill } from '../rendering/still-casters.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Builds a `THREE.Mesh` configured for static or repositionable world geometry, enabling shadow
|
|
13
|
+
* reception, optional shadow casting and static-caster registration (`markStill`).
|
|
14
|
+
*
|
|
15
|
+
* `matrixAutoUpdate` defaults to `true` so callers can position or move the returned mesh without
|
|
16
|
+
* remembering a manual `updateMatrix()` call; pass `{ matrixAutoUpdate: false }` to freeze the
|
|
17
|
+
* local matrix immediately at the origin.
|
|
18
|
+
*
|
|
19
|
+
* @param {THREE.BufferGeometry} geometry Geometry to render.
|
|
20
|
+
* @param {THREE.Material} material Material applied to the mesh.
|
|
21
|
+
* @param {boolean} [shadow=true] Whether the mesh casts shadows (and joins the static-caster layer).
|
|
22
|
+
* @param {object} [options]
|
|
23
|
+
* @param {boolean} [options.matrixAutoUpdate=true] Keep Three.js automatic local-matrix updates enabled.
|
|
24
|
+
* @param {boolean} [options.receiveShadow=true] Whether the mesh receives shadows.
|
|
25
|
+
* @returns {THREE.Mesh}
|
|
26
|
+
*/
|
|
27
|
+
export function stillMesh(geometry, material, shadow = true, { matrixAutoUpdate = true, receiveShadow = true } = {}) {
|
|
28
|
+
const mesh = new THREE.Mesh(geometry, material);
|
|
29
|
+
mesh.castShadow = shadow;
|
|
30
|
+
mesh.receiveShadow = receiveShadow;
|
|
31
|
+
mesh.matrixAutoUpdate = matrixAutoUpdate;
|
|
32
|
+
if (!matrixAutoUpdate) mesh.updateMatrix();
|
|
33
|
+
if (shadow) markStill(mesh);
|
|
34
|
+
return mesh;
|
|
35
|
+
}
|