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
package/examples/stage-kits.js
CHANGED
|
@@ -1,79 +1,13 @@
|
|
|
1
1
|
import * as THREE from 'three';
|
|
2
|
-
import {
|
|
3
|
-
createCityFortification,
|
|
4
|
-
createCrowdBatches,
|
|
5
|
-
createCrowdLookBuilder,
|
|
6
|
-
createCrowdPoser,
|
|
7
|
-
createCrowdUpdater,
|
|
8
|
-
createCrowdVisibility,
|
|
9
|
-
createFigureAssembler,
|
|
10
|
-
createFigureBuilder,
|
|
11
|
-
createGreatWalls,
|
|
12
|
-
createHerdRenderer,
|
|
13
|
-
createHerdSkins,
|
|
14
|
-
createHouseKitMeshes,
|
|
15
|
-
createStreetBeasts,
|
|
16
|
-
GeometryBuilder,
|
|
17
|
-
geometryOf,
|
|
18
|
-
mulberry32,
|
|
19
|
-
registerCrowd,
|
|
20
|
-
} from 'roamwright';
|
|
2
|
+
import { createCityFortification, createGreatWalls, GeometryBuilder, mulberry32 } from 'roamwright';
|
|
21
3
|
import { particleNoise } from '../math/particle-noise.js';
|
|
22
4
|
import { C } from '../geometry/primitives.js';
|
|
23
5
|
import { partsGeometry } from '../geometry/foliage.js';
|
|
24
|
-
import { createBroadleaf } from '../geometry/plant-broadleaf.js';
|
|
25
6
|
import { createPlantTints } from '../geometry/plant-tints.js';
|
|
26
7
|
import { createTrees } from '../geometry/plant-trees.js';
|
|
27
8
|
import { PLAIN_GRASS, tussockGeometry } from '../geometry/plant-tussock.js';
|
|
28
|
-
import { createCrowdAlarms } from '../motion/crowd-alarms.js';
|
|
29
9
|
|
|
30
|
-
// Shared
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Synchronous hand-over adapter for building showcase kit meshes on the main thread.
|
|
34
|
-
*/
|
|
35
|
-
export const SYNC_HAND_OVER = {
|
|
36
|
-
paced: steps => Promise.resolve(steps),
|
|
37
|
-
drain: steps => steps,
|
|
38
|
-
heldBytes: () => 0,
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Default vertex-colored material map for synchronous house-kit and prop-kit showcase meshes.
|
|
43
|
-
*/
|
|
44
|
-
export function createShowcaseHouseMaterials(kitKeys, { doubleSide = false, roughness = 0.9 } = {}) {
|
|
45
|
-
const solid = doubleSide ? { vertexColors: true, roughness, side: THREE.DoubleSide } : { vertexColors: true, roughness };
|
|
46
|
-
const materials = {};
|
|
47
|
-
for (const key of kitKeys) {
|
|
48
|
-
if (key === 'stain') {
|
|
49
|
-
materials[key] = new THREE.MeshStandardMaterial({ vertexColors: true, transparent: true, opacity: 0.25, depthWrite: false });
|
|
50
|
-
} else if (key === 'glass' && !doubleSide) {
|
|
51
|
-
materials[key] = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.25 });
|
|
52
|
-
} else if (key === 'cloth') {
|
|
53
|
-
materials[key] = new THREE.MeshStandardMaterial({ vertexColors: true, roughness, side: THREE.DoubleSide });
|
|
54
|
-
} else {
|
|
55
|
-
materials[key] = new THREE.MeshStandardMaterial(solid);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return materials;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Uploads an array of kit buffers into a shadow-casting group and attaches it to parent.
|
|
63
|
-
*/
|
|
64
|
-
export function buildKitGroup({ kitKeys, kit, kits, materials, parent, castShadow = true, receiveShadow = true }) {
|
|
65
|
-
const { kitMeshes } = createHouseKitMeshes({ materials, PieceBatch: null, handOver: SYNC_HAND_OVER });
|
|
66
|
-
const group = new THREE.Group();
|
|
67
|
-
kitMeshes(kits ?? kitKeys.map(k => kit[k].parts(true)), group);
|
|
68
|
-
group.traverse(o => {
|
|
69
|
-
if (o.isMesh) {
|
|
70
|
-
o.castShadow = castShadow;
|
|
71
|
-
o.receiveShadow = receiveShadow;
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
parent.add(group);
|
|
75
|
-
return group;
|
|
76
|
-
}
|
|
10
|
+
// Shared fortification, foliage scatter, paddock fence, and particle-noise helpers for showcase examples.
|
|
77
11
|
|
|
78
12
|
/**
|
|
79
13
|
* Binds createGreatWalls and createCityFortification to a houseKit and terrain sampler.
|
|
@@ -113,17 +47,13 @@ export function scatterTrees({
|
|
|
113
47
|
}) {
|
|
114
48
|
const tints = createPlantTints({ fbm: noise.fbm, vnoise: noise.vnoise, hash });
|
|
115
49
|
const treeKit = createTrees({ hash, ...tints, seed });
|
|
116
|
-
const { broadleaf } = createBroadleaf(tints);
|
|
117
50
|
const material = new THREE.MeshStandardMaterial({ vertexColors: true, roughness, side: THREE.DoubleSide });
|
|
118
51
|
const builders = {
|
|
119
|
-
broadleaf: () => broadleaf(),
|
|
52
|
+
broadleaf: () => treeKit.broadleaf(),
|
|
120
53
|
conifer: () => treeKit.conifer(),
|
|
121
54
|
cedar: () => treeKit.cedar(0),
|
|
122
55
|
};
|
|
123
|
-
const geos = species.map(name =>
|
|
124
|
-
const built = builders[name]();
|
|
125
|
-
return partsGeometry(built.near ?? built);
|
|
126
|
-
});
|
|
56
|
+
const geos = species.map(name => partsGeometry(builders[name]().near));
|
|
127
57
|
const pools = geos.map(geo => {
|
|
128
58
|
const mesh = new THREE.InstancedMesh(geo, material, maxPerSpecies);
|
|
129
59
|
mesh.castShadow = mesh.receiveShadow = true;
|
|
@@ -216,9 +146,7 @@ export function buildCircularFence({
|
|
|
216
146
|
const yaw = Math.atan2(x1 - x0, z1 - z0);
|
|
217
147
|
for (const h of railHeights) fence.box(mx, my + h, mz, 0.07, 0.1, span, C(0x7e654b, 0.08, random), yaw);
|
|
218
148
|
}
|
|
219
|
-
|
|
220
|
-
mesh.castShadow = mesh.receiveShadow = true;
|
|
221
|
-
return mesh;
|
|
149
|
+
return fence.mesh(new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.92 }), worldGroup);
|
|
222
150
|
}
|
|
223
151
|
|
|
224
152
|
/**
|
|
@@ -242,59 +170,3 @@ export function makeParticleNoiseTexture(noise, size = 128) {
|
|
|
242
170
|
texture.needsUpdate = true;
|
|
243
171
|
return texture;
|
|
244
172
|
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Wires figure builders, instanced crowd batches, herd skins, alarms, visibility and updater for showcase scenes.
|
|
248
|
-
*/
|
|
249
|
-
export function setupShowcaseCrowd({ worldGroup, groundAt, spaceKit, camera, renderer }) {
|
|
250
|
-
const { buildBody, kit } = createFigureBuilder();
|
|
251
|
-
const { assembleFigure } = createFigureAssembler({
|
|
252
|
-
material: () => new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.88, side: THREE.DoubleSide }),
|
|
253
|
-
holds: kit.holds,
|
|
254
|
-
});
|
|
255
|
-
const buildFigure = (look, lookSeed, _near, bodies) => {
|
|
256
|
-
const far = bodies?.[0] || buildBody(look, lookSeed, 1);
|
|
257
|
-
return assembleFigure(look, 'human', { ...far, geo: geometryOf(far.parts) }, null);
|
|
258
|
-
};
|
|
259
|
-
const crowdPose = createCrowdPoser();
|
|
260
|
-
const batches = createCrowdBatches({
|
|
261
|
-
scene: worldGroup,
|
|
262
|
-
buildFigure,
|
|
263
|
-
buildCrowdLook: createCrowdLookBuilder({ buildBody }),
|
|
264
|
-
crowdPose,
|
|
265
|
-
});
|
|
266
|
-
const herdSkins = createHerdSkins();
|
|
267
|
-
const herdRenderer = createHerdRenderer({
|
|
268
|
-
scene: worldGroup,
|
|
269
|
-
material: () => new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.9 }),
|
|
270
|
-
herdPoses: herdSkins.herdPoses,
|
|
271
|
-
});
|
|
272
|
-
const crowds = [];
|
|
273
|
-
const addCrowd = (list, tones, cx, cz, range, kind, beasts = null, sp = 'world') =>
|
|
274
|
-
registerCrowd(crowds, list, tones, cx, cz, range, kind, { beasts, space: sp, groundAt: (_s, x, z) => groundAt(x, z) });
|
|
275
|
-
const { streetBeasts, beastsAbout } = createStreetBeasts({ addCrowd, herdLook: herdSkins.herdLook });
|
|
276
|
-
const visibility = createCrowdVisibility();
|
|
277
|
-
const alarms = createCrowdAlarms({ steer: spaceKit.steer });
|
|
278
|
-
|
|
279
|
-
function createUpdater({ getTime, getFrame, getLightDir, shadowRange = 120, shadowFar = 320 }) {
|
|
280
|
-
return createCrowdUpdater({
|
|
281
|
-
camera,
|
|
282
|
-
renderer,
|
|
283
|
-
getTime,
|
|
284
|
-
getFrame,
|
|
285
|
-
getShadowsEnabled: () => true,
|
|
286
|
-
getLightDir,
|
|
287
|
-
getShadowRange: () => shadowRange,
|
|
288
|
-
getShadowFar: () => shadowFar,
|
|
289
|
-
getSpace: () => spaceKit.worldSpace,
|
|
290
|
-
crowds,
|
|
291
|
-
batches,
|
|
292
|
-
visibility,
|
|
293
|
-
alarms,
|
|
294
|
-
crowdPose,
|
|
295
|
-
herds: { ...herdRenderer, orderHerd: herdSkins.orderHerd },
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
return { crowds, addCrowd, herdSkins, streetBeasts, beastsAbout, alarms, batches, createUpdater };
|
|
300
|
-
}
|
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 };
|