roamwright 0.61.20 → 0.61.22
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 +13 -12
- package/examples/scenes.html +54 -0
- package/examples/scenes.js +594 -0
- package/geometry/projectile-shapes.js +85 -0
- package/index.js +50 -0
- package/motion/fallen-mount.js +63 -0
- package/motion/melee-combat.js +328 -0
- package/motion/melee-cuts.js +395 -0
- package/motion/melee-duels.js +142 -0
- package/motion/melee-exchange-creatures.js +86 -0
- package/motion/melee-exchange.js +498 -0
- package/motion/melee-reactions.js +147 -0
- package/motion/prop-contact.js +87 -0
- package/package.json +1 -1
- package/rendering/blade-trails.js +128 -0
- package/rendering/scene-beams.js +486 -0
- package/rendering/scene-coverage.js +500 -0
- package/rendering/scene-markers.js +421 -0
- package/rendering/scene-projectiles.js +395 -0
- package/rendering/scene-props.js +383 -0
- package/rendering/scene-shots.js +199 -0
- package/runtime/foley-surface.js +79 -0
- package/runtime/scene-script.js +137 -0
- package/runtime/scene-zones.js +117 -0
- package/tools/README.md +34 -25
- package/tools/cpu-profile.cjs +117 -0
- package/tools/cpu-profile.test.cjs +31 -1
- package/tools/figure-inspect.cjs +475 -0
- package/tools/figure-inspect.test.cjs +153 -0
- package/tools/geometry-hash.cjs +361 -0
- package/tools/geometry-hash.test.cjs +146 -0
- package/tools/load-audit.cjs +199 -2
- package/tools/load-audit.test.cjs +68 -0
- package/tools/quiet-bench.cjs +202 -0
- package/tools/quiet-bench.test.cjs +91 -0
- package/tools/scene-detail.cjs +355 -0
- package/tools/scene-detail.test.cjs +162 -0
- package/tools/scrub-clear.cjs +191 -0
- package/tools/scrub-clear.test.cjs +100 -0
- package/tools/tree-inspect.cjs +271 -0
- package/tools/tree-inspect.test.cjs +123 -0
- package/tools/world-tour.cjs +223 -0
- package/tools/world-tour.test.cjs +77 -0
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scene prop registry and kinematics: lazy construction and pre-warming by name, show/hide/move/spin,
|
|
3
|
+
* single- and two-handed actor grips, vessel palm orientation, timed contact handoffs, catch and
|
|
4
|
+
* tumbling ballistic throws, tied multi-actor props, and camera sightline occlusion checks.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as THREE from 'three';
|
|
9
|
+
import { lerp, sstep } from '../math/scalar.js';
|
|
10
|
+
import { sampleBallisticArc } from '../motion/ballistic-arc.js';
|
|
11
|
+
import { createPropContact } from '../motion/prop-contact.js';
|
|
12
|
+
|
|
13
|
+
const SMALL_GRIP = 0.012, // default finger grip radius in metres
|
|
14
|
+
MOUTH = new THREE.Vector3(0, 0.1, 0.09), // mouth offset from head bone in figure units
|
|
15
|
+
BEFORE_PALM = 0.5,
|
|
16
|
+
UP = new THREE.Vector3(0, 1, 0);
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Creates a named scene prop registry bound to `scene` and actor hand/IK helpers.
|
|
20
|
+
* @param {object} options
|
|
21
|
+
* @param {THREE.Object3D} options.scene Scene receiving registered prop objects.
|
|
22
|
+
* @param {(x: number, z: number) => number} [options.groundAt] Ground height sampler for thrown-prop bounces and tabletop slides.
|
|
23
|
+
* @param {Record<string, () => THREE.Object3D>} [options.makers] Lazy prop builder table keyed by name.
|
|
24
|
+
* @param {Record<string, number>} [options.spins] Default spin speeds in rad/s per prop name.
|
|
25
|
+
* @param {Record<string, number>} [options.solidRadii] Occlusion radii in metres for standing props that block close-up sightlines.
|
|
26
|
+
* @param {() => Record<string, () => THREE.Object3D> | undefined} [options.getSpaceMakers] Returns the current space's optional prop-maker map.
|
|
27
|
+
* @param {(name: string) => Promise<(() => THREE.Object3D) | undefined>} [options.resolveAsyncMaker] Optional lazy async maker fallback (e.g. dynamic chunk of gift props).
|
|
28
|
+
* @param {(obj: THREE.Object3D) => Promise<void>} [options.warmShaders] Optional shader warm-up hook invoked before showing a newly built prop.
|
|
29
|
+
* @param {() => boolean} [options.anyFrameDrawn] Returns true once the renderer has drawn a frame (gates GPU upload hand-over).
|
|
30
|
+
* @param {(objs: THREE.Object3D[]) => Promise<void>} [options.handToCard] Optional GPU upload hand-over helper.
|
|
31
|
+
* @param {(actor: object, radius: number, snap: boolean, left?: boolean) => boolean} [options.closeHand] Closes an actor's fingers around a held prop.
|
|
32
|
+
* @param {(actor: object, left?: boolean) => void} [options.openHand] Opens an actor's hand when releasing a prop.
|
|
33
|
+
* @param {(actor: object, radius: number, out: THREE.Vector3, left?: boolean) => void} [options.inHand] Writes world grip position inside an actor's hand.
|
|
34
|
+
* @param {Function} [options.reachArm] Two-bone arm IK solver for timed handoffs and placements.
|
|
35
|
+
* @returns {object} Prop registry controller exposing `props`, `add`, `prepare`, `show`, `inHand`, `lay`, `throwProp`, `tie`, `hide`, `clear`, `moveTo`, `update`, and `inTheWay`.
|
|
36
|
+
*/
|
|
37
|
+
export function createSceneProps({
|
|
38
|
+
scene,
|
|
39
|
+
groundAt = () => 0,
|
|
40
|
+
makers = {},
|
|
41
|
+
spins = {},
|
|
42
|
+
solidRadii = {},
|
|
43
|
+
getSpaceMakers = () => undefined,
|
|
44
|
+
resolveAsyncMaker = null,
|
|
45
|
+
warmShaders = null,
|
|
46
|
+
anyFrameDrawn = () => false,
|
|
47
|
+
handToCard = null,
|
|
48
|
+
closeHand = () => true,
|
|
49
|
+
openHand = () => {},
|
|
50
|
+
inHand = (a, _r, out) => out.set(a.x, a.y + 1.1, a.z),
|
|
51
|
+
reachArm = () => {},
|
|
52
|
+
} = {}) {
|
|
53
|
+
const props = {},
|
|
54
|
+
propList = [],
|
|
55
|
+
propOrders = new Map(),
|
|
56
|
+
_mouth = new THREE.Vector3(),
|
|
57
|
+
_palm = new THREE.Vector3(),
|
|
58
|
+
_arm = new THREE.Vector3(),
|
|
59
|
+
_neck = new THREE.Vector3(),
|
|
60
|
+
_wide = new THREE.Vector3(),
|
|
61
|
+
_turn = new THREE.Matrix4(),
|
|
62
|
+
_catchPoint = new THREE.Vector3(),
|
|
63
|
+
_otherHand = new THREE.Vector3(),
|
|
64
|
+
_vecScratch = new THREE.Vector3(),
|
|
65
|
+
_quatScratch = new THREE.Quaternion(),
|
|
66
|
+
contactHelpers = { inHand, closeHand, openHand, reachArm, groundAt };
|
|
67
|
+
|
|
68
|
+
function add(name, obj, spin = spins[name] || 0) {
|
|
69
|
+
obj.visible = false;
|
|
70
|
+
if (!obj.name) obj.name = name;
|
|
71
|
+
scene?.add(obj);
|
|
72
|
+
props[name] = {
|
|
73
|
+
obj,
|
|
74
|
+
spin,
|
|
75
|
+
tw: null,
|
|
76
|
+
s: 1,
|
|
77
|
+
hand: null,
|
|
78
|
+
left: false,
|
|
79
|
+
taken: false,
|
|
80
|
+
warmed: false,
|
|
81
|
+
contact: null,
|
|
82
|
+
worn: null,
|
|
83
|
+
tick: obj.userData.tick || null,
|
|
84
|
+
vessel: obj.userData.vessel || null,
|
|
85
|
+
twoHanded: !!obj.userData.twoHanded,
|
|
86
|
+
grip: obj.userData.grip || obj.userData.vessel?.thin || SMALL_GRIP,
|
|
87
|
+
};
|
|
88
|
+
if (propList.length) propList.push(props[name]);
|
|
89
|
+
return props[name];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function syncCarryingProp(actor) {
|
|
93
|
+
if (!actor) return;
|
|
94
|
+
let held = false;
|
|
95
|
+
for (const k in props) {
|
|
96
|
+
const q = props[k];
|
|
97
|
+
if (q.obj.visible && q.hand === actor) {
|
|
98
|
+
held = true;
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
actor.carryingProp = held;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function letGo(p) {
|
|
106
|
+
p.contact = null;
|
|
107
|
+
const previous = p.hand;
|
|
108
|
+
if (previous) openHand(previous, p.left);
|
|
109
|
+
p.hand = null;
|
|
110
|
+
syncCarryingProp(previous);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function buildSceneProp(name, sp) {
|
|
114
|
+
if (props[name]?.warmed) return;
|
|
115
|
+
let obj = props[name]?.obj ?? sp?.props?.[name];
|
|
116
|
+
if (!obj) {
|
|
117
|
+
const make = sp?.propMakers?.[name] ?? makers[name] ?? (resolveAsyncMaker ? await resolveAsyncMaker(name) : null);
|
|
118
|
+
if (!make) throw new Error('Unknown scene prop ' + name);
|
|
119
|
+
obj = make();
|
|
120
|
+
}
|
|
121
|
+
if (warmShaders) await warmShaders(obj);
|
|
122
|
+
if (!props[name]) add(name, obj, spins[name] || 0);
|
|
123
|
+
props[name].warmed = true;
|
|
124
|
+
if (!anyFrameDrawn() || !handToCard) return;
|
|
125
|
+
const hidden = [];
|
|
126
|
+
obj.traverse(o => {
|
|
127
|
+
if (o !== obj && o.isMesh && o.visible) {
|
|
128
|
+
o.visible = false;
|
|
129
|
+
hidden.push(o);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
if (!obj.isMesh) obj.visible = true;
|
|
133
|
+
try {
|
|
134
|
+
await handToCard([obj]);
|
|
135
|
+
} finally {
|
|
136
|
+
for (const o of hidden) o.visible = true;
|
|
137
|
+
if (!props[name].hand && !props[name].tw) obj.visible = false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function prepare(ev, sp) {
|
|
142
|
+
let wait;
|
|
143
|
+
for (const step of ev.steps ?? []) {
|
|
144
|
+
const name = step[2];
|
|
145
|
+
if (step[1] !== 'prop' || step[3] === 'hide' || props[name]?.warmed) continue;
|
|
146
|
+
if (!propOrders.has(name)) {
|
|
147
|
+
propOrders.set(
|
|
148
|
+
name,
|
|
149
|
+
buildSceneProp(name, sp).finally(() => propOrders.delete(name)),
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
(wait ??= new Set()).add(propOrders.get(name));
|
|
153
|
+
}
|
|
154
|
+
return wait ? Promise.all(wait) : null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function propOf(name) {
|
|
158
|
+
if (!props[name]) {
|
|
159
|
+
const make = getSpaceMakers()?.[name] ?? makers[name];
|
|
160
|
+
if (make) add(name, make(), spins[name] || 0);
|
|
161
|
+
}
|
|
162
|
+
return props[name];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function show(name, x, y, z, s = 1) {
|
|
166
|
+
const p = propOf(name);
|
|
167
|
+
p.obj.position.set(x, y, z);
|
|
168
|
+
p.obj.scale.setScalar(s);
|
|
169
|
+
p.obj.visible = true;
|
|
170
|
+
p.tw = null;
|
|
171
|
+
p.s = s;
|
|
172
|
+
letGo(p);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function holdInHand(name, actor, s = 1, seconds = 0, how) {
|
|
176
|
+
const p = propOf(name);
|
|
177
|
+
if (seconds && p.obj.visible) {
|
|
178
|
+
if (how === 'catch' && p.hand) {
|
|
179
|
+
const start = p.obj.position.clone();
|
|
180
|
+
letGo(p);
|
|
181
|
+
p.tw = { start, catchBy: actor, t: 0, dur: seconds };
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
p.contact = createPropContact(
|
|
185
|
+
p,
|
|
186
|
+
seconds,
|
|
187
|
+
{
|
|
188
|
+
at: p.hand ? null : p.obj.position.clone(),
|
|
189
|
+
to: actor,
|
|
190
|
+
toLeft: how === 'left',
|
|
191
|
+
slideHeight: how?.slideHeight,
|
|
192
|
+
turn: p.obj.userData.blade ? p.obj.quaternion.clone() : null,
|
|
193
|
+
},
|
|
194
|
+
contactHelpers,
|
|
195
|
+
);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
p.obj.scale.setScalar(s);
|
|
199
|
+
p.obj.visible = true;
|
|
200
|
+
p.tw = null;
|
|
201
|
+
p.s = s;
|
|
202
|
+
letGo(p);
|
|
203
|
+
p.hand = actor;
|
|
204
|
+
p.left = how === 'left';
|
|
205
|
+
syncCarryingProp(actor);
|
|
206
|
+
if (p.obj.userData.worn === actor.def.look.item && p.obj.userData.worn && actor.sheath) {
|
|
207
|
+
p.worn = actor.sheath;
|
|
208
|
+
p.worn.scale.setScalar(0.001);
|
|
209
|
+
}
|
|
210
|
+
p.taken = closeHand(actor, p.grip * s, false, p.left);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function tie(name, list) {
|
|
214
|
+
const p = propOf(name);
|
|
215
|
+
p.obj.position.set(0, 0, 0);
|
|
216
|
+
p.obj.scale.setScalar(1);
|
|
217
|
+
p.obj.userData.tie(list);
|
|
218
|
+
p.obj.visible = true;
|
|
219
|
+
p.tw = null;
|
|
220
|
+
letGo(p);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function hide(name) {
|
|
224
|
+
const p = props[name];
|
|
225
|
+
if (!p) return;
|
|
226
|
+
p.obj.visible = false;
|
|
227
|
+
p.tw = null;
|
|
228
|
+
if (p.worn) {
|
|
229
|
+
p.worn.scale.setScalar(1);
|
|
230
|
+
p.worn = null;
|
|
231
|
+
}
|
|
232
|
+
letGo(p);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function clear() {
|
|
236
|
+
for (const k in props) hide(k);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function moveTo(name, x, y, z, s, dur) {
|
|
240
|
+
const p = propOf(name),
|
|
241
|
+
o = p.obj;
|
|
242
|
+
letGo(p);
|
|
243
|
+
p.tw = { x0: o.position.x, y0: o.position.y, z0: o.position.z, s0: o.scale.x, x, y, z, s, t: 0, dur };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function vesselInHand(p, a) {
|
|
247
|
+
const hand = a.handR.matrixWorld,
|
|
248
|
+
o = p.obj,
|
|
249
|
+
{ wide, tall, thin } = p.vessel;
|
|
250
|
+
_palm.setFromMatrixColumn(hand, 0).normalize().negate();
|
|
251
|
+
_arm.setFromMatrixColumn(hand, 1).normalize();
|
|
252
|
+
a.head.updateWorldMatrix(false, false);
|
|
253
|
+
a.head.localToWorld(_mouth.copy(MOUTH)).sub(o.position).normalize();
|
|
254
|
+
_neck.copy(_mouth).addScaledVector(_palm, -_mouth.dot(_palm));
|
|
255
|
+
const aside = _neck.length();
|
|
256
|
+
if (aside < BEFORE_PALM) _neck.addScaledVector(_wide.setFromMatrixColumn(hand, 2).normalize(), BEFORE_PALM - aside);
|
|
257
|
+
_neck.normalize();
|
|
258
|
+
_wide.crossVectors(_neck, _palm);
|
|
259
|
+
o.quaternion.setFromRotationMatrix(_turn.makeBasis(_wide, _neck, _palm));
|
|
260
|
+
const below = Math.hypot(tall * _neck.dot(_arm), wide * _wide.dot(_arm));
|
|
261
|
+
o.position.addScaledVector(_arm, (below - thin) * p.s);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function throwProp(name, x, y, z, dur = 0.6, dropToGround = true) {
|
|
265
|
+
const p = propOf(name);
|
|
266
|
+
p.obj.visible = true;
|
|
267
|
+
if (p.hand) inHand(p.hand, p.grip * p.s, p.obj.position, p.left);
|
|
268
|
+
const start = p.obj.position.clone(),
|
|
269
|
+
end = _vecScratch.set(x, y, z).clone();
|
|
270
|
+
letGo(p);
|
|
271
|
+
p.tw = { start, end, tumble: true, dropToGround, t: 0, dur: Math.max(0.1, dur) };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function lay(name, x, y, z, yaw, seconds) {
|
|
275
|
+
const p = propOf(name);
|
|
276
|
+
p.contact = createPropContact(
|
|
277
|
+
p,
|
|
278
|
+
seconds,
|
|
279
|
+
{
|
|
280
|
+
at: _vecScratch.set(x, y, z).clone(),
|
|
281
|
+
turn: _quatScratch.setFromAxisAngle(UP, yaw).clone(),
|
|
282
|
+
},
|
|
283
|
+
contactHelpers,
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function update(dt) {
|
|
288
|
+
if (propList.length === 0) for (const k in props) propList.push(props[k]);
|
|
289
|
+
for (let i = 0; i < propList.length; i++) {
|
|
290
|
+
const p = propList[i];
|
|
291
|
+
if (!p.obj.visible) continue;
|
|
292
|
+
if (p.spin) p.obj.rotation.y += p.spin * dt;
|
|
293
|
+
if (p.contact) {
|
|
294
|
+
const prevFrom = p.contact.from,
|
|
295
|
+
prevTo = p.contact.to;
|
|
296
|
+
if (p.contact.advance(dt)) p.contact = null;
|
|
297
|
+
syncCarryingProp(prevFrom);
|
|
298
|
+
syncCarryingProp(prevTo);
|
|
299
|
+
}
|
|
300
|
+
if (p.hand) {
|
|
301
|
+
if (!p.taken) p.taken = closeHand(p.hand, p.grip * p.s, true, p.left);
|
|
302
|
+
inHand(p.hand, p.grip * p.s, p.obj.position, p.left);
|
|
303
|
+
const wrist = p.left ? p.hand.handL : p.hand.handR;
|
|
304
|
+
if (p.twoHanded && p.hand.pose === 'carry' && p.hand.handL && p.hand.handR) {
|
|
305
|
+
inHand(p.hand, p.grip * p.s, _otherHand, !p.left);
|
|
306
|
+
p.obj.position.lerp(_otherHand, 0.5);
|
|
307
|
+
p.obj.position.y += 0.025;
|
|
308
|
+
p.obj.quaternion.setFromAxisAngle(UP, p.hand.yaw);
|
|
309
|
+
} else if (p.obj.userData.blade && wrist) {
|
|
310
|
+
wrist.getWorldQuaternion(p.obj.quaternion);
|
|
311
|
+
const c = p.contact;
|
|
312
|
+
if (c?.turn) {
|
|
313
|
+
const u = c.phase;
|
|
314
|
+
if (!c.to) p.obj.quaternion.slerp(c.turn, c.approach);
|
|
315
|
+
else if (p.hand === c.from) c.turn.copy(p.obj.quaternion);
|
|
316
|
+
else p.obj.quaternion.slerp(c.turn, 1 - sstep(0.55, 1, u));
|
|
317
|
+
}
|
|
318
|
+
} else if (p.vessel && p.hand.head) vesselInHand(p, p.hand);
|
|
319
|
+
else if (!p.spin) p.obj.quaternion.setFromAxisAngle(UP, p.hand.yaw);
|
|
320
|
+
} else if (p.tw) {
|
|
321
|
+
const w = p.tw;
|
|
322
|
+
w.t += dt;
|
|
323
|
+
const u = Math.min(1, w.t / w.dur),
|
|
324
|
+
e = u * u * (3 - 2 * u);
|
|
325
|
+
if (w.catchBy) {
|
|
326
|
+
inHand(w.catchBy, p.grip * p.s, _catchPoint);
|
|
327
|
+
sampleBallisticArc(p.obj.position, w.start, _catchPoint, u, w.dur);
|
|
328
|
+
if (u === 1) {
|
|
329
|
+
p.hand = w.catchBy;
|
|
330
|
+
p.left = false;
|
|
331
|
+
syncCarryingProp(p.hand);
|
|
332
|
+
p.taken = closeHand(p.hand, p.grip * p.s, false);
|
|
333
|
+
}
|
|
334
|
+
} else if (w.end) {
|
|
335
|
+
sampleBallisticArc(p.obj.position, w.start, w.end, u, w.dur);
|
|
336
|
+
const yaw = Math.atan2(w.end.x - w.start.x, w.end.z - w.start.z);
|
|
337
|
+
if (w.tumble) p.obj.rotation.set(u * 10.5, yaw, u * 3.8);
|
|
338
|
+
if (u >= 1 && w.dropToGround) {
|
|
339
|
+
const gx = w.end.x + Math.sin(yaw) * 0.34,
|
|
340
|
+
gz = w.end.z + Math.cos(yaw) * 0.34,
|
|
341
|
+
gy = groundAt(gx, gz) + 0.035;
|
|
342
|
+
p.tw = { start: w.end.clone(), end: _vecScratch.set(gx, gy, gz).clone(), tumble: false, dropToGround: false, t: 0, dur: 0.36 };
|
|
343
|
+
p.obj.rotation.set(0, yaw + 0.55, 0);
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
} else {
|
|
347
|
+
p.obj.position.set(lerp(w.x0, w.x, e), lerp(w.y0, w.y, e), lerp(w.z0, w.z, e));
|
|
348
|
+
p.obj.scale.setScalar(lerp(w.s0, w.s, e));
|
|
349
|
+
}
|
|
350
|
+
if (u >= 1) p.tw = null;
|
|
351
|
+
}
|
|
352
|
+
if (p.tick) p.tick(dt);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function inTheWay(p, at) {
|
|
357
|
+
const dx = at.x - p.x,
|
|
358
|
+
dz = at.z - p.z,
|
|
359
|
+
reach = Math.hypot(dx, dz);
|
|
360
|
+
if (reach < 0.5) return false;
|
|
361
|
+
const ux = dx / reach,
|
|
362
|
+
uz = dz / reach;
|
|
363
|
+
for (const name in solidRadii) {
|
|
364
|
+
const entry = props[name];
|
|
365
|
+
if (!entry?.obj.visible || entry.hand) continue;
|
|
366
|
+
const r = solidRadii[name] * entry.s,
|
|
367
|
+
ox = entry.obj.position.x - p.x,
|
|
368
|
+
oz = entry.obj.position.z - p.z,
|
|
369
|
+
along = ox * ux + oz * uz,
|
|
370
|
+
aside = Math.abs(ox * uz - oz * ux);
|
|
371
|
+
if (
|
|
372
|
+
along > -r * 0.4 &&
|
|
373
|
+
along < reach - 0.5 &&
|
|
374
|
+
aside < r &&
|
|
375
|
+
p.y + (at.y - p.y) * Math.max(0, along / reach) < entry.obj.position.y + 4 * entry.s
|
|
376
|
+
)
|
|
377
|
+
return true;
|
|
378
|
+
}
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
return { props, add, prepare, show, inHand: holdInHand, lay, throwProp, tie, hide, clear, moveTo, update, inTheWay };
|
|
383
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scene camera driver and shot helpers: samples scripted tracks (including actor-carried `camOn`
|
|
3
|
+
* origins), protects headroom on tight master lenses, computes continuous gallop ground tremor,
|
|
4
|
+
* applies narrow-screen pullback, drives stage lights, depth-of-field, shadow focus, and cut events.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { clamp, sstep } from '../math/scalar.js';
|
|
9
|
+
import { createCameraImpulse, createCameraTrackSampler } from './camera-track.js';
|
|
10
|
+
import { ensureHeadroom } from './stage-framing.js';
|
|
11
|
+
import { createNarrowFraming } from './stage-lights.js';
|
|
12
|
+
import { actorHeadTop as defaultHeadTop } from './scene-coverage.js';
|
|
13
|
+
|
|
14
|
+
const STAGE_ASPECT = 1.5,
|
|
15
|
+
SHADOW_SPAN = 2.5,
|
|
16
|
+
SHADOW_ROOM = 8,
|
|
17
|
+
SHADOW_LEAST = 24,
|
|
18
|
+
MASTER_HEADROOM_NEAR = 6.2,
|
|
19
|
+
MASTER_AIM_RADIUS = 1.3,
|
|
20
|
+
GALLOP_TREMOR_DIST = 11.5;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Computes continuous ground tremor in `[0, 1]` from mounted actors galloping past `lensPos`.
|
|
24
|
+
* @param {{ x: number, z: number }} lensPos Current camera world position.
|
|
25
|
+
* @param {object[]} actorList Live actor array.
|
|
26
|
+
* @param {number} [maxDist] Maximum audible/felt gallop distance in metres (default `11.5`).
|
|
27
|
+
* @param {number} [minSpeed] Minimum mount speed in m/s that causes tremor (default `4.2`).
|
|
28
|
+
* @param {number} [speedSpan] Speed range in m/s over which tremor saturates (default `5.5`).
|
|
29
|
+
* @returns {number} Peak tremor level in `[0, 1]`.
|
|
30
|
+
*/
|
|
31
|
+
export function gallopTremorLevel(lensPos, actorList, maxDist = GALLOP_TREMOR_DIST, minSpeed = 4.2, speedSpan = 5.5) {
|
|
32
|
+
let tremor = 0;
|
|
33
|
+
for (let i = 0; i < actorList.length; i++) {
|
|
34
|
+
const a = actorList[i];
|
|
35
|
+
if (!a.active || !a.mount || a.speed < minSpeed) continue;
|
|
36
|
+
const d = Math.hypot(a.x - lensPos.x, a.z - lensPos.z);
|
|
37
|
+
if (d < maxDist) {
|
|
38
|
+
tremor = Math.max(tremor, clamp((a.speed - minSpeed) / speedSpan, 0, 1) * (1 - d / maxDist));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return tremor;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Lifts aim `l.y` when needed so close master shots on narrowed lenses do not crop the top of
|
|
46
|
+
* an aimed character's head.
|
|
47
|
+
* @param {import('three').Vector3} p Camera world position.
|
|
48
|
+
* @param {import('three').Vector3} l Camera aim position (mutated in place).
|
|
49
|
+
* @param {number} fov Applied vertical field of view in degrees.
|
|
50
|
+
* @param {object[]} actorList Live actor array.
|
|
51
|
+
* @param {(actor: object) => number} [headTopOf] Head crown height helper.
|
|
52
|
+
* @param {string[]} [skipKinds] Actor kinds excluded from headroom adjustment (default `['dragon', 'raven']`).
|
|
53
|
+
*/
|
|
54
|
+
export function protectMasterHeadroom(p, l, fov, actorList, headTopOf = defaultHeadTop, skipKinds = ['dragon', 'raven']) {
|
|
55
|
+
if (p.distanceTo(l) > MASTER_HEADROOM_NEAR) return;
|
|
56
|
+
let highestTop = -Infinity;
|
|
57
|
+
for (let i = 0; i < actorList.length; i++) {
|
|
58
|
+
const a = actorList[i];
|
|
59
|
+
if (!a.active || !a.inScene || skipKinds.includes(a.kind)) continue;
|
|
60
|
+
if (Math.hypot(a.x - l.x, a.z - l.z) > MASTER_AIM_RADIUS || Math.abs(a.y + a.height * 0.5 - l.y) > a.height) continue;
|
|
61
|
+
const top = headTopOf(a);
|
|
62
|
+
if (top > highestTop) highestTop = top;
|
|
63
|
+
}
|
|
64
|
+
if (highestTop > -Infinity) ensureHeadroom(p, l, highestTop, fov, 0.09, 0.55);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Resolves the world origin for a scene's camera keys: either the scene anchor `anchor`, or the
|
|
69
|
+
* horizontal position of an active actor named by `ev.camOn` (for cameras carried aboard moving boats/carts).
|
|
70
|
+
* @param {object} ev Scene event record with optional `camOn` actor id.
|
|
71
|
+
* @param {{ x: number, y: number, z: number }} anchor Scene anchor.
|
|
72
|
+
* @param {Record<string, object>} actors Actor table by id.
|
|
73
|
+
* @param {{ x: number, y: number, z: number }} out Scratch origin object written when carried.
|
|
74
|
+
* @returns {{ x: number, y: number, z: number }} Effective track origin.
|
|
75
|
+
*/
|
|
76
|
+
export function trackOrigin(ev, anchor, actors, out) {
|
|
77
|
+
const a = ev.camOn && actors[ev.camOn];
|
|
78
|
+
if (!a || !a.active) return anchor;
|
|
79
|
+
out.x = a.x;
|
|
80
|
+
out.y = anchor.y;
|
|
81
|
+
out.z = a.z;
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Creates a complete per-frame scene camera and stage-light controller.
|
|
87
|
+
* @param {object} options
|
|
88
|
+
* @param {import('three').PerspectiveCamera} options.camera Active viewport camera.
|
|
89
|
+
* @param {object[]} options.actorList Live actor array.
|
|
90
|
+
* @param {Record<string, object>} options.actors Actor table by id.
|
|
91
|
+
* @param {() => number} options.getBaseFov Returns base vertical FOV in degrees.
|
|
92
|
+
* @param {number} [options.wideFov] Wide reference FOV for narrow-screen pullback (default `58`).
|
|
93
|
+
* @param {(fov: number) => void} options.setFov Applies vertical FOV to the camera/viewport.
|
|
94
|
+
* @param {() => object} options.getSpace Returns the active space (`id`, `indoors`, `lowLens`, `ground`).
|
|
95
|
+
* @param {() => number} options.getDay Returns daylight share in `[0, 1]`.
|
|
96
|
+
* @param {() => number} options.getElapsed Returns elapsed time in seconds.
|
|
97
|
+
* @param {() => number} options.getFrame Returns current frame counter.
|
|
98
|
+
* @param {(name: string, out: import('three').Vector3) => void} [options.resolveNamedTarget] Resolves named track targets (e.g. `'@…'`).
|
|
99
|
+
* @param {Function} [options.coverShot] Automatic closer-shot selector (`(ev, t, p, l, trackState) => fov`).
|
|
100
|
+
* @param {(x: number, y: number, z: number) => boolean} [options.lensBlocked] Blocked-lens check for narrow pullback.
|
|
101
|
+
* @param {(actor: object) => number} [options.actorHeadTop] Crown height sampler.
|
|
102
|
+
* @param {() => { update: Function }} [options.getStageLights] Returns the scene stage-lights rig.
|
|
103
|
+
* @param {{ update: Function }} [options.practicalFlame] Optional practical-flame tracker.
|
|
104
|
+
* @param {(reach: number, frame: number, opts: object) => void} [options.focusOn] Optional depth-of-field focus updater.
|
|
105
|
+
* @param {(x: number, z: number, radius: number, frame: number) => void} [options.setShadowFocus] Tightens the sun shadow box on closer shots.
|
|
106
|
+
* @param {() => void} [options.dropHistory] Clears temporal anti-aliasing history on camera cuts.
|
|
107
|
+
* @param {() => void} [options.onCut] Called once the camera lands on a new cut.
|
|
108
|
+
* @param {number} [options.stageAspect] Reference framing aspect ratio (default `1.5`).
|
|
109
|
+
* @returns {{ shoot: (director: object, ev: object, dt: number) => void, triggerImpulse: (event: object) => void, clearImpulse: () => void }}
|
|
110
|
+
*/
|
|
111
|
+
export function createSceneShots({
|
|
112
|
+
camera,
|
|
113
|
+
actorList = [],
|
|
114
|
+
actors = {},
|
|
115
|
+
getBaseFov = () => 58,
|
|
116
|
+
wideFov = 58,
|
|
117
|
+
setFov = () => {},
|
|
118
|
+
getSpace,
|
|
119
|
+
getDay = () => 1,
|
|
120
|
+
getElapsed = () => 0,
|
|
121
|
+
getFrame = () => 0,
|
|
122
|
+
resolveNamedTarget,
|
|
123
|
+
coverShot = () => 0,
|
|
124
|
+
lensBlocked = () => false,
|
|
125
|
+
actorHeadTop = defaultHeadTop,
|
|
126
|
+
getStageLights = null,
|
|
127
|
+
practicalFlame = null,
|
|
128
|
+
focusOn = null,
|
|
129
|
+
setShadowFocus = null,
|
|
130
|
+
dropHistory = null,
|
|
131
|
+
onCut = null,
|
|
132
|
+
stageAspect = STAGE_ASPECT,
|
|
133
|
+
}) {
|
|
134
|
+
const frameNarrow = createNarrowFraming({ blocked: lensBlocked, stageAspect }),
|
|
135
|
+
cameraImpulse = createCameraImpulse(),
|
|
136
|
+
sampleTrack = createCameraTrackSampler(resolveNamedTarget),
|
|
137
|
+
_trackState = { namedTarget: null, explicitLens: false, sinceCut: Infinity, speed: 0 },
|
|
138
|
+
_carried = { x: 0, y: 0, z: 0 };
|
|
139
|
+
|
|
140
|
+
function triggerImpulse(event) {
|
|
141
|
+
if (!event) return;
|
|
142
|
+
const dist = event.x !== undefined && event.z !== undefined ? Math.hypot(event.x - camera.position.x, event.z - camera.position.z) : 0;
|
|
143
|
+
cameraImpulse.trigger(event.strength ?? 0.55, dist);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function clearImpulse() {
|
|
147
|
+
cameraImpulse.clear();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function shoot(d, ev, dt) {
|
|
151
|
+
const sp = getSpace(),
|
|
152
|
+
baseFov = getBaseFov(),
|
|
153
|
+
keyLens = sampleTrack(ev.cam, d.t, trackOrigin(ev, d.A, actors, _carried), d.p, d.l, _trackState);
|
|
154
|
+
d.p.x += Math.sin(d.t * 0.31) * 0.16;
|
|
155
|
+
d.p.y += Math.sin(d.t * 0.23 + 1) * 0.1;
|
|
156
|
+
const longLens = keyLens ? baseFov - keyLens : (11 + 2.5 * Math.sin(d.t * 0.19)) * sstep(0, 3.5, d.t),
|
|
157
|
+
narrow = camera.aspect < stageAspect;
|
|
158
|
+
const closer = coverShot(ev, d.t, d.p, d.l, _trackState);
|
|
159
|
+
const appliedFov = closer || (narrow ? frameNarrow(d, longLens, dt, baseFov, wideFov, camera.aspect) : baseFov - longLens);
|
|
160
|
+
setFov(appliedFov);
|
|
161
|
+
if (!closer) {
|
|
162
|
+
if (!sp.lowLens) {
|
|
163
|
+
const floor = sp.ground(d.p.x, d.p.z) + 1.45;
|
|
164
|
+
if (d.p.y < floor) d.p.y = floor;
|
|
165
|
+
const aim = sp.ground(d.l.x, d.l.z) + 0.9;
|
|
166
|
+
if (d.l.y < aim && d.l.y > aim - 12) d.l.y = aim;
|
|
167
|
+
}
|
|
168
|
+
protectMasterHeadroom(d.p, d.l, appliedFov, actorList, actorHeadTop);
|
|
169
|
+
}
|
|
170
|
+
cameraImpulse.setTremor(gallopTremorLevel(d.p, actorList));
|
|
171
|
+
cameraImpulse.apply(dt, d.p, d.l);
|
|
172
|
+
d.rig.position.copy(d.p);
|
|
173
|
+
d.rig.lookAt(d.l);
|
|
174
|
+
const day = getDay(),
|
|
175
|
+
frame = getFrame();
|
|
176
|
+
practicalFlame?.update(d.l, sp.id, !!sp.indoors, day, getElapsed());
|
|
177
|
+
const jumped = getStageLights ? getStageLights().update(d.p, d.l, !!closer, day, dt) : false;
|
|
178
|
+
if (jumped) dropHistory?.();
|
|
179
|
+
const reach = d.p.distanceTo(d.l);
|
|
180
|
+
if (closer) {
|
|
181
|
+
focusOn?.(reach, frame, { dt, snap: jumped, strength: 1 });
|
|
182
|
+
setShadowFocus?.(d.l.x, d.l.z, Math.max(SHADOW_LEAST, reach * SHADOW_SPAN + SHADOW_ROOM), frame);
|
|
183
|
+
} else if (reach < MASTER_HEADROOM_NEAR) {
|
|
184
|
+
focusOn?.(reach, frame, { dt, snap: jumped, strength: 0.46 * sstep(MASTER_HEADROOM_NEAR, 3.0, reach) });
|
|
185
|
+
}
|
|
186
|
+
if (d.blend < 1) {
|
|
187
|
+
d.blend = Math.min(1, d.blend + dt / 1.9);
|
|
188
|
+
const e = d.blend * d.blend * (3 - 2 * d.blend);
|
|
189
|
+
camera.position.lerpVectors(d.fromPos, d.p, e);
|
|
190
|
+
camera.quaternion.slerpQuaternions(d.fromQuat, d.rig.quaternion, e);
|
|
191
|
+
} else {
|
|
192
|
+
camera.position.copy(d.p);
|
|
193
|
+
camera.quaternion.copy(d.rig.quaternion);
|
|
194
|
+
}
|
|
195
|
+
if (jumped) onCut?.();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return { shoot, triggerImpulse, clearImpulse };
|
|
199
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-allocation underfoot surface classifier for stride foley, scuffs, and body-fall effects.
|
|
3
|
+
* Distinguishes wood decks, shallow water, stone, ash, mud, sand, and earth from caller-provided
|
|
4
|
+
* world probes without allocating per call.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates a reusable surface classifier returning a shared `{ kind, audioKind, waterY }` record.
|
|
10
|
+
* @param {object} options
|
|
11
|
+
* @param {(x: number, y: number, z: number) => boolean} [options.isWood] Returns true on wooden decks or timber floors.
|
|
12
|
+
* @param {(x: number, y: number, z: number, out: { waterY: number, nearBank: boolean }) => void} [options.waterAt] Populates water surface elevation (`-Infinity` if dry) and whether `(x, z)` lies on a wet bank.
|
|
13
|
+
* @param {(x: number, z: number) => boolean} [options.isStone] Returns true on paved stone or rock.
|
|
14
|
+
* @param {(x: number, z: number) => boolean} [options.isAsh] Returns true on ash-covered ground.
|
|
15
|
+
* @param {(x: number, z: number) => boolean} [options.isWet] Returns true when weather/moisture makes soil muddy.
|
|
16
|
+
* @param {(x: number, z: number) => boolean} [options.isSand] Returns true on sandy/desert terrain.
|
|
17
|
+
* @param {number} [options.waterMinDepth] Minimum water depth in metres classified as `'water'` (default `0.02`).
|
|
18
|
+
* @param {number} [options.waterMaxDepth] Maximum shallow water depth in metres classified as `'water'` (default `1.15`).
|
|
19
|
+
* @returns {(x: number, y: number, z: number) => { kind: string, audioKind: string, waterY: number }} Classifier writing into a single reused record.
|
|
20
|
+
*/
|
|
21
|
+
export function createSurfaceClassifier({
|
|
22
|
+
isWood = () => false,
|
|
23
|
+
waterAt = null,
|
|
24
|
+
isStone = () => false,
|
|
25
|
+
isAsh = () => false,
|
|
26
|
+
isWet = () => false,
|
|
27
|
+
isSand = () => false,
|
|
28
|
+
waterMinDepth = 0.02,
|
|
29
|
+
waterMaxDepth = 1.15,
|
|
30
|
+
} = {}) {
|
|
31
|
+
const surface = { kind: 'earth', audioKind: 'earth', waterY: -Infinity };
|
|
32
|
+
const waterProbe = { waterY: -Infinity, nearBank: false };
|
|
33
|
+
|
|
34
|
+
return function classifySurface(x, y, z) {
|
|
35
|
+
if (isWood(x, y, z)) {
|
|
36
|
+
surface.kind = 'wood';
|
|
37
|
+
surface.audioKind = 'wood';
|
|
38
|
+
surface.waterY = -Infinity;
|
|
39
|
+
return surface;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
waterProbe.waterY = -Infinity;
|
|
43
|
+
waterProbe.nearBank = false;
|
|
44
|
+
if (waterAt) waterAt(x, y, z, waterProbe);
|
|
45
|
+
|
|
46
|
+
const depth = waterProbe.waterY - y;
|
|
47
|
+
if (depth > waterMinDepth && depth < waterMaxDepth) {
|
|
48
|
+
surface.kind = 'water';
|
|
49
|
+
surface.audioKind = 'earth';
|
|
50
|
+
surface.waterY = waterProbe.waterY;
|
|
51
|
+
return surface;
|
|
52
|
+
}
|
|
53
|
+
surface.waterY = -Infinity;
|
|
54
|
+
|
|
55
|
+
if (isStone(x, z)) {
|
|
56
|
+
surface.kind = 'stone';
|
|
57
|
+
surface.audioKind = 'stone';
|
|
58
|
+
return surface;
|
|
59
|
+
}
|
|
60
|
+
if (isAsh(x, z)) {
|
|
61
|
+
surface.kind = 'ash';
|
|
62
|
+
surface.audioKind = 'sand';
|
|
63
|
+
return surface;
|
|
64
|
+
}
|
|
65
|
+
if (isWet(x, z) || waterProbe.nearBank) {
|
|
66
|
+
surface.kind = 'mud';
|
|
67
|
+
surface.audioKind = 'earth';
|
|
68
|
+
return surface;
|
|
69
|
+
}
|
|
70
|
+
if (isSand(x, z)) {
|
|
71
|
+
surface.kind = 'sand';
|
|
72
|
+
surface.audioKind = 'sand';
|
|
73
|
+
return surface;
|
|
74
|
+
}
|
|
75
|
+
surface.kind = 'earth';
|
|
76
|
+
surface.audioKind = 'earth';
|
|
77
|
+
return surface;
|
|
78
|
+
};
|
|
79
|
+
}
|