roamwright 0.61.31 → 0.61.33

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.
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Cracks splitting the ground outward from a point, drawn by the ground's own lighting: `groundCrackGeometry` binds the arrays of
3
+ * `geometry/ground-cracks.js`; `createGroundCrackMaterial` draws each crack only as far as it has run (`front`, metres from the
4
+ * point) and glows hot in its gash behind the front, cooling to dark (`heat`); `createGroundCracks` assembles a mesh with its two
5
+ * uniform handles.
6
+ * @module
7
+ */
8
+
9
+ import * as THREE from 'three';
10
+ import { groundCrackArrays } from '../geometry/ground-cracks.js';
11
+
12
+ /** Default program cache key of {@link createGroundCrackMaterial}. */
13
+ export const GROUND_CRACK_DEFAULTS = Object.freeze({
14
+ cacheKey: 'ground-cracks',
15
+ });
16
+
17
+ /**
18
+ * Bind the crack arrays to a `THREE.BufferGeometry` with flat normals.
19
+ *
20
+ * @param {Parameters<groundCrackArrays>[0]} [options]
21
+ * @returns {THREE.BufferGeometry} `position`, `color` and `aCrack` (metres from the point, 0 in the gash to 1 at a lip).
22
+ */
23
+ export function groundCrackGeometry(options = {}) {
24
+ const { position, color, crack } = groundCrackArrays(options);
25
+ const geo = new THREE.BufferGeometry();
26
+ geo.setAttribute('position', new THREE.BufferAttribute(position, 3));
27
+ geo.setAttribute('color', new THREE.BufferAttribute(color, 3));
28
+ geo.setAttribute('aCrack', new THREE.BufferAttribute(crack, 2));
29
+ geo.computeVertexNormals();
30
+ return geo;
31
+ }
32
+
33
+ /**
34
+ * Create the cracks' `MeshStandardMaterial`: lit as the ground is, transparent, and drawn only as far as `front` metres from the
35
+ * point; the gash glows hot (a warm orange, in the shader's text) times `heat`, brightest at the front and cooling behind it.
36
+ *
37
+ * @param {object} [options]
38
+ * @param {{ value: number }} [options.front={ value: 0 }] - How far the cracks have run, in metres from the point.
39
+ * @param {{ value: number }} [options.heat={ value: 0 }] - How hot the gash glows (`0..1`).
40
+ * @param {string} [options.cacheKey=GROUND_CRACK_DEFAULTS.cacheKey] - The program cache key.
41
+ * @returns {THREE.MeshStandardMaterial}
42
+ */
43
+ export function createGroundCrackMaterial({ front = { value: 0 }, heat = { value: 0 }, cacheKey = GROUND_CRACK_DEFAULTS.cacheKey } = {}) {
44
+ const mat = new THREE.MeshStandardMaterial({
45
+ vertexColors: true,
46
+ roughness: 0.95,
47
+ transparent: true,
48
+ depthWrite: false,
49
+ polygonOffset: true,
50
+ polygonOffsetFactor: -2,
51
+ polygonOffsetUnits: -2,
52
+ });
53
+ mat.onBeforeCompile = sh => {
54
+ sh.uniforms.uFront = front;
55
+ sh.uniforms.uHeat = heat;
56
+ sh.vertexShader = sh.vertexShader
57
+ .replace('#include <common>', '#include <common>\nattribute vec2 aCrack; varying vec2 vCrack;')
58
+ .replace('#include <begin_vertex>', '#include <begin_vertex>\nvCrack = aCrack;');
59
+ sh.fragmentShader = sh.fragmentShader
60
+ .replace('#include <common>', '#include <common>\nuniform float uFront, uHeat; varying vec2 vCrack;')
61
+ .replace('#include <clipping_planes_fragment>', '#include <clipping_planes_fragment>\nif (vCrack.x > uFront) discard;')
62
+ .replace('#include <color_fragment>', '#include <color_fragment>\ndiffuseColor.a *= 1.0 - smoothstep(0.3, 1.0, vCrack.y);')
63
+ .replace(
64
+ '#include <emissivemap_fragment>',
65
+ `#include <emissivemap_fragment>
66
+ float behind = uFront - vCrack.x, core = 1.0 - vCrack.y;
67
+ totalEmissiveRadiance += vec3(2.6, 1.3, 0.38) * uHeat * pow(core, 6.0) * (0.25 + 0.75 * exp(-behind * 0.15));`,
68
+ );
69
+ };
70
+ mat.customProgramCacheKey = () => cacheKey;
71
+ return mat;
72
+ }
73
+
74
+ /**
75
+ * A mesh of cracks splitting the ground outward from `centre`, hidden until shown, with the handles its material reads.
76
+ *
77
+ * @param {Parameters<groundCrackArrays>[0] & { parent?: THREE.Object3D | null, cacheKey?: string }} [options] - The cracks' shape,
78
+ * optional parent group and program cache key. The key defaults to `GROUND_CRACK_DEFAULTS.cacheKey`.
79
+ * @returns {{ mesh: THREE.Mesh, geometry: THREE.BufferGeometry, material: THREE.MeshStandardMaterial, front: { value: number }, heat: { value: number }, dispose: () => void }}
80
+ */
81
+ export function createGroundCracks({ parent = null, cacheKey = GROUND_CRACK_DEFAULTS.cacheKey, ...shape } = {}) {
82
+ const front = { value: 0 },
83
+ heat = { value: 0 };
84
+ const geometry = groundCrackGeometry(shape);
85
+ const material = createGroundCrackMaterial({ front, heat, cacheKey });
86
+ const mesh = new THREE.Mesh(geometry, material);
87
+ mesh.receiveShadow = true;
88
+ mesh.visible = false;
89
+ if (parent) parent.add(mesh);
90
+ return {
91
+ mesh,
92
+ geometry,
93
+ material,
94
+ front,
95
+ heat,
96
+ dispose() {
97
+ mesh.removeFromParent();
98
+ geometry.dispose();
99
+ material.dispose();
100
+ },
101
+ };
102
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * A chain of iron links hanging between points (`createHangingChain`): one instanced mesh of a link, drawn along every span
3
+ * between `begin()` and `finish()`, each link turned along the span's sagging curve and the alternate ones a quarter round, so
4
+ * the chain reads as links and not a rod. The caller writes a span's ends into `from` and `to` and calls `addSpan()`: nothing is
5
+ * made in a frame.
6
+ * @module
7
+ */
8
+
9
+ import * as THREE from 'three';
10
+
11
+ /** Default size and look of a hanging chain. */
12
+ export const HANGING_CHAIN_DEFAULTS = Object.freeze({
13
+ links: 16, // links to a span
14
+ radius: 0.045, // metres: a link's ring
15
+ tube: 0.012, // metres: a link's wire
16
+ stretch: 2.2, // how far each link is drawn along its axis: an oval link
17
+ sag: Object.freeze([0.12, 0.7, 0.15]), // metres: the least sag of a span, and how its sag falls with its length
18
+ color: 0x2a2826, // sRGB hex of the iron
19
+ metalness: 0.7,
20
+ roughness: 0.55,
21
+ wireSegments: 8, // segments round a link's ring
22
+ });
23
+
24
+ let sharedLink = null;
25
+ /** The default link geometry, made once and shared by every chain on a page. */
26
+ export const defaultLinkGeometry = () =>
27
+ (sharedLink ??= new THREE.TorusGeometry(
28
+ HANGING_CHAIN_DEFAULTS.radius,
29
+ HANGING_CHAIN_DEFAULTS.tube,
30
+ 4,
31
+ HANGING_CHAIN_DEFAULTS.wireSegments,
32
+ ));
33
+ let sharedMaterial = null;
34
+ /** The default iron material, made once and shared by every chain on a page: one program for them all. */
35
+ export const defaultChainMaterial = () =>
36
+ (sharedMaterial ??= new THREE.MeshStandardMaterial({
37
+ color: HANGING_CHAIN_DEFAULTS.color,
38
+ metalness: HANGING_CHAIN_DEFAULTS.metalness,
39
+ roughness: HANGING_CHAIN_DEFAULTS.roughness,
40
+ }));
41
+
42
+ /**
43
+ * Create a hanging chain of `spans` spans, each of `links` links, as one `THREE.InstancedMesh`.
44
+ *
45
+ * Each frame its owner calls `begin()`, then for every span writes its ends into `from` and `to` (world points, in the mesh's
46
+ * parent's frame) and calls `addSpan()`, then `finish()`. A span past the capacity is left out.
47
+ *
48
+ * @param {object} [options]
49
+ * @param {THREE.Object3D | null} [options.parent=null] - A group to add the chain to.
50
+ * @param {number} [options.spans=1] - How many spans the chain can hold at once.
51
+ * @param {number} [options.links=HANGING_CHAIN_DEFAULTS.links] - Links to a span.
52
+ * @param {number} [options.stretch=HANGING_CHAIN_DEFAULTS.stretch] - How far each link is drawn along its axis.
53
+ * @param {[number, number, number]} [options.sag=HANGING_CHAIN_DEFAULTS.sag] - Metres: the least sag of a span, and how its sag
54
+ * falls with its length (`least`, `at zero length`, `per metre`).
55
+ * @param {THREE.BufferGeometry | null} [options.geometry=null] - The link's geometry (the default link, shared, where none is given).
56
+ * @param {THREE.Material | null} [options.material=null] - The links' material (the default iron, shared, where none is given).
57
+ * @param {boolean} [options.castShadow=true] - Whether the links cast shadows.
58
+ * @returns {{ mesh: THREE.InstancedMesh, from: THREE.Vector3, to: THREE.Vector3, begin: () => void, addSpan: () => void, finish: () => void, dispose: () => void }}
59
+ */
60
+ export function createHangingChain({
61
+ parent = null,
62
+ spans = 1,
63
+ links = HANGING_CHAIN_DEFAULTS.links,
64
+ stretch = HANGING_CHAIN_DEFAULTS.stretch,
65
+ sag = HANGING_CHAIN_DEFAULTS.sag,
66
+ geometry = null,
67
+ material = null,
68
+ castShadow = true,
69
+ } = {}) {
70
+ const capacity = links * spans;
71
+ const mesh = new THREE.InstancedMesh(geometry ?? defaultLinkGeometry(), material ?? defaultChainMaterial(), capacity);
72
+ mesh.castShadow = castShadow;
73
+ mesh.frustumCulled = false; // the links follow whatever they join: bounds worked out once would lag behind them
74
+ mesh.count = 0; // nothing is drawn until the first finish()
75
+ if (parent) parent.add(mesh);
76
+ const from = new THREE.Vector3(),
77
+ to = new THREE.Vector3(),
78
+ point = new THREE.Vector3(),
79
+ turn = new THREE.Quaternion(),
80
+ quarter = new THREE.Quaternion(),
81
+ size = new THREE.Vector3(),
82
+ matrix = new THREE.Matrix4(),
83
+ along = new THREE.Vector3(),
84
+ up = new THREE.Vector3(0, 1, 0);
85
+ let count = 0;
86
+ return {
87
+ mesh,
88
+ from,
89
+ to,
90
+ begin() {
91
+ count = 0;
92
+ },
93
+ addSpan() {
94
+ if (count + links > capacity) return;
95
+ const length = from.distanceTo(to),
96
+ drop = Math.max(sag[0], sag[1] - length * sag[2]);
97
+ for (let k = 0; k < links; k++) {
98
+ const u = (k + 0.5) / links;
99
+ point.lerpVectors(from, to, u);
100
+ point.y -= drop * 4 * u * (1 - u);
101
+ along.subVectors(to, from).normalize();
102
+ along.y -= (drop * 4 * (1 - 2 * u)) / Math.max(length, 0.5);
103
+ along.normalize();
104
+ turn.setFromUnitVectors(up, along).multiply(quarter.setFromAxisAngle(up, k % 2 ? Math.PI / 2 : 0));
105
+ mesh.setMatrixAt(count++, matrix.compose(point, turn, size.set(1, stretch, 1)));
106
+ }
107
+ },
108
+ finish() {
109
+ mesh.count = count;
110
+ mesh.instanceMatrix.needsUpdate = true;
111
+ },
112
+ dispose() {
113
+ mesh.removeFromParent();
114
+ mesh.dispose(); // the instance matrices; the shared link and iron stay for the next chain
115
+ },
116
+ };
117
+ }
@@ -147,6 +147,10 @@ export {
147
147
  createFallingWaterSheets,
148
148
  dustCurtainGeometry,
149
149
  } from './curtain-sheets.js';
150
+ export { GROUND_CRACK_DEFAULTS, createGroundCrackMaterial, createGroundCracks, groundCrackGeometry } from './ground-cracks.js';
151
+ export { createStormSilhouetteMaterial } from './storm-silhouette.js';
152
+ export { CALM_DOME_DEFAULTS, createCalmDome } from './calm-dome.js';
153
+ export { HANGING_CHAIN_DEFAULTS, createHangingChain, defaultChainMaterial, defaultLinkGeometry } from './hanging-chain.js';
150
154
  export { FALLEN_BEAST_DEFAULTS, placeFallenBeasts } from './camp-layout.js';
151
155
  export { createPracticalFlame, createSceneLights } from './scene-lights.js';
152
156
  export { createPerfBox } from './perf-box.js';
@@ -0,0 +1,59 @@
1
+ /**
2
+ * A shape looming in a storm (`createStormSilhouetteMaterial`): the shape drawn as darker dust where it stands, torn by the dust
3
+ * blowing across it, its grazing edges and its feet lost in the air. Any geometry takes it: the world supplies the shape and
4
+ * drives `show` (how much of it is there), `time` and the `light` the dust holds.
5
+ * @module
6
+ */
7
+
8
+ import * as THREE from 'three';
9
+ import { VALUE_NOISE_3D_GLSL } from './curtain-sheets.js';
10
+
11
+ /**
12
+ * Create the `THREE.ShaderMaterial` of a shape looming in blown dust, fogged with the scene.
13
+ *
14
+ * @param {object} [options]
15
+ * @param {{ value: number }} [options.show={ value: 1 }] - How much of the shape is there (`0..1`).
16
+ * @param {{ value: number }} [options.time={ value: 0 }] - Elapsed time in seconds: the dust tears across the shape.
17
+ * @param {{ value: THREE.Color }} [options.light={ value: new THREE.Color(1, 0.84, 0.66) }] - The light the dust holds.
18
+ * @returns {THREE.ShaderMaterial}
19
+ */
20
+ export function createStormSilhouetteMaterial({
21
+ show = { value: 1 },
22
+ time = { value: 0 },
23
+ light = { value: new THREE.Color(1, 0.84, 0.66) },
24
+ } = {}) {
25
+ return new THREE.ShaderMaterial({
26
+ transparent: true,
27
+ depthWrite: false,
28
+ fog: true,
29
+ uniforms: { ...THREE.UniformsUtils.clone(THREE.UniformsLib.fog), uShow: show, uTime: time, uLight: light },
30
+ vertexShader: /* glsl */ `
31
+ #include <common>
32
+ #include <fog_pars_vertex>
33
+ varying vec3 vLocal, vN, vView;
34
+ void main(){
35
+ vLocal = position;
36
+ vN = normalize(normalMatrix * normal);
37
+ vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
38
+ vView = normalize(-mvPosition.xyz);
39
+ gl_Position = projectionMatrix * mvPosition;
40
+ #include <fog_vertex>
41
+ }`,
42
+ fragmentShader: /* glsl */ `
43
+ #include <common>
44
+ #include <fog_pars_fragment>
45
+ uniform float uShow, uTime; uniform vec3 uLight;
46
+ varying vec3 vLocal, vN, vView;
47
+ ${VALUE_NOISE_3D_GLSL}
48
+ void main(){
49
+ float face = abs(dot(normalize(vN), vView));
50
+ float tear = ffbm(vLocal * vec3(0.05, 0.07, 0.05) + vec3(uTime * 0.35, -uTime * 0.5, 0.0));
51
+ float a = uShow * smoothstep(0.05, 0.6, face) * smoothstep(0.15, 0.45, tear + 0.1) * (0.3 + 0.7 * smoothstep(4.0, 22.0, vLocal.y));
52
+ vec3 col = mix(vec3(0.05, 0.032, 0.02), vec3(0.22, 0.14, 0.08), tear) * uLight;
53
+ gl_FragColor = vec4(col, a * 0.92);
54
+ #include <tonemapping_fragment>
55
+ #include <colorspace_fragment>
56
+ #include <fog_fragment>
57
+ }`,
58
+ });
59
+ }
@@ -41,7 +41,8 @@ export function createVehicleTrains({ kitNew, kitAt, kitInstanced, propCart, pro
41
41
  // Vehicles outside the active shadow frustum (`casts = false`) use unshadowed clone meshes to avoid shadow-pass vertex work.
42
42
  function makeVehicles(group, n, kind, rng) {
43
43
  const { build, wheels } = KINDS[kind],
44
- wheelOffsets = wheels.map(at => TRS(at[0], at[1], at[2], 1, at[3], at[3])),
44
+ // TRS hands back one shared scratch matrix: each wheel keeps its own copy, or every wheel lands on the last one's place
45
+ wheelOffsets = wheels.map(at => TRS(at[0], at[1], at[2], 1, at[3], at[3]).clone()),
45
46
  body = kitNew(),
46
47
  wheel = kitNew();
47
48
  kitAt(body, 0, 0, 0, 0, false, build, rng);