roamwright 0.54.0 → 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 +11 -1
- package/motion/cast-actors.js +379 -0
- package/motion/cast-manager.js +321 -0
- package/motion/figure-mount.js +1 -0
- package/package.json +20 -1
- package/rendering/cast-stage.js +299 -0
- package/rendering/crowd-system.js +239 -0
- package/rendering/daylight-sky.js +212 -0
- package/rendering/house-kit-meshes.js +65 -16
- package/rendering/nameplate-groups.js +84 -0
- package/rendering/nameplates.js +386 -0
- package/rendering/orbit-camera.js +216 -0
- package/rendering/water-simple.js +112 -0
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Distance-faded, collision-free floating nameplates for cast actors and named crowd groups:
|
|
3
|
+
* wraps long subtitles onto two lines, renders crisp canvas textures at a paced per-frame
|
|
4
|
+
* budget, defers `renderer.initTexture` uploads by one frame to avoid GPU stalls, avoids HUD
|
|
5
|
+
* regions and neighbouring labels (with shoulder fallback), and smoothly fades opacity.
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import * as THREE from 'three';
|
|
9
|
+
import { clamp, sstep } from '../math/scalar.js';
|
|
10
|
+
import { createLabelLayout } from './label-layout.js';
|
|
11
|
+
|
|
12
|
+
/** Default title and subtitle canvas font strings. */
|
|
13
|
+
export const DEFAULT_PLATE_FONTS = Object.freeze({
|
|
14
|
+
name: '600 44px "Iowan Old Style","Palatino Linotype",Palatino,"Book Antiqua",Georgia,serif',
|
|
15
|
+
sub: '500 36px system-ui,-apple-system,"Segoe UI",Roboto,sans-serif',
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
/** Default radial backdrop gradient stops behind plate lettering. */
|
|
19
|
+
export const DEFAULT_PLATE_BACKDROP = Object.freeze([
|
|
20
|
+
[0, 'rgba(4,9,11,.78)'],
|
|
21
|
+
[0.55, 'rgba(4,9,11,.44)'],
|
|
22
|
+
[1, 'rgba(4,9,11,0)'],
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
const DEFAULT_PLATES_PER_FRAME = 2;
|
|
26
|
+
const DEFAULT_PLATE_SCREEN = 0.031; // plate height as a fraction of viewport height
|
|
27
|
+
const DEFAULT_PLATE_MIN_PX = 36; // minimum plate height in CSS pixels for phone legibility
|
|
28
|
+
const DEFAULT_FIGURE_TO_PLATE = Object.freeze([0.9, 1.6]); // figure-height / plate-height fade range
|
|
29
|
+
const PLATE_CANVAS_WIDTH = 512;
|
|
30
|
+
const PLATE_CANVAS_WIDE = 4; // canvas width in plate-height units
|
|
31
|
+
const LETTER_MARGIN = 0.3; // horizontal padding around measured lettering, in plate heights
|
|
32
|
+
const TEXT_MOST = 3.9; // estimated text width in plate heights before canvas measurement
|
|
33
|
+
const DEFAULT_HEAD_CLEARANCE = 0.35; // metres above actor/group top
|
|
34
|
+
const DEFAULT_SHOULDER_RATIO = 0.72; // fraction of actor height for side-of-shoulder fallback
|
|
35
|
+
const _v = new THREE.Vector3(); // module scratch for synchronous screen projection
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Splits a long subtitle onto two balanced lines at the space closest to its midpoint,
|
|
39
|
+
* stripping a trailing or leading separator token (`'·'` by default) at the break point.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} subtitle - Subtitle string.
|
|
42
|
+
* @param {(text: string) => number} measureWidth - Returns pixel width of a line in the subtitle font.
|
|
43
|
+
* @param {number} [maxWidth=480] - Maximum single-line width (canvas pixels) before wrapping.
|
|
44
|
+
* @param {string} [separator='·'] - Inline separator stripped at line breaks when present.
|
|
45
|
+
* @returns {[string, string]} Primary line and optional second line (`''` when unwrapped).
|
|
46
|
+
*/
|
|
47
|
+
export function wrapSubtitle(subtitle, measureWidth, maxWidth = 480, separator = '·') {
|
|
48
|
+
let l1 = subtitle,
|
|
49
|
+
l2 = '';
|
|
50
|
+
if (measureWidth(subtitle) > maxWidth) {
|
|
51
|
+
let best = -1,
|
|
52
|
+
bestWidth = 1e9;
|
|
53
|
+
for (let i = subtitle.indexOf(' '); i > 0; i = subtitle.indexOf(' ', i + 1)) {
|
|
54
|
+
const w = Math.max(measureWidth(subtitle.slice(0, i)), measureWidth(subtitle.slice(i + 1)));
|
|
55
|
+
if (w < bestWidth) {
|
|
56
|
+
bestWidth = w;
|
|
57
|
+
best = i;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (best > 0) {
|
|
61
|
+
l1 = subtitle.slice(0, best);
|
|
62
|
+
l2 = subtitle.slice(best + 1);
|
|
63
|
+
if (separator) {
|
|
64
|
+
const trail = ' ' + separator;
|
|
65
|
+
const lead = separator + ' ';
|
|
66
|
+
if (l1.endsWith(trail)) l1 = l1.slice(0, -trail.length);
|
|
67
|
+
if (l2.startsWith(lead)) l2 = l2.slice(lead.length);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return [l1, l2];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Renders a two-tier nameplate (title and one- or two-line subtitle over a soft radial
|
|
76
|
+
* backdrop) onto a canvas and reports its aspect ratio and measured lettering width.
|
|
77
|
+
*
|
|
78
|
+
* @param {string} name - Primary title text.
|
|
79
|
+
* @param {string} subtitle - Secondary descriptor text.
|
|
80
|
+
* @param {string} subColor - CSS colour string for the subtitle line(s).
|
|
81
|
+
* @param {object} [options]
|
|
82
|
+
* @param {{ name: string, sub: string }} [options.fonts=DEFAULT_PLATE_FONTS] - Canvas font strings.
|
|
83
|
+
* @param {string} [options.nameColor='#f3ecd9'] - CSS colour string for the title text.
|
|
84
|
+
* @param {ReadonlyArray<[number, string]>} [options.backdropStops=DEFAULT_PLATE_BACKDROP] - Radial gradient colour stops.
|
|
85
|
+
* @param {string} [options.shadowColor='rgba(0,0,0,.95)'] - Text drop shadow colour.
|
|
86
|
+
* @param {number} [options.shadowBlur=7] - Text drop shadow blur radius (canvas pixels).
|
|
87
|
+
* @param {string} [options.separator='·'] - Separator stripped at wrap boundaries.
|
|
88
|
+
* @param {() => HTMLCanvasElement} [options.createCanvas] - Canvas factory (defaults to `document.createElement('canvas')`).
|
|
89
|
+
* @returns {{ canvas: HTMLCanvasElement, plateH: number, plateWide: number }}
|
|
90
|
+
*/
|
|
91
|
+
export function renderNameplateCanvas(
|
|
92
|
+
name,
|
|
93
|
+
subtitle,
|
|
94
|
+
subColor,
|
|
95
|
+
{
|
|
96
|
+
fonts = DEFAULT_PLATE_FONTS,
|
|
97
|
+
nameColor = '#f3ecd9',
|
|
98
|
+
backdropStops = DEFAULT_PLATE_BACKDROP,
|
|
99
|
+
shadowColor = 'rgba(0,0,0,.95)',
|
|
100
|
+
shadowBlur = 7,
|
|
101
|
+
separator = '·',
|
|
102
|
+
createCanvas = () => document.createElement('canvas'),
|
|
103
|
+
} = {},
|
|
104
|
+
) {
|
|
105
|
+
const canvas = createCanvas(),
|
|
106
|
+
g = canvas.getContext('2d');
|
|
107
|
+
g.font = fonts.sub;
|
|
108
|
+
const [l1, l2] = wrapSubtitle(subtitle, text => g.measureText(text).width, 480, separator);
|
|
109
|
+
const heightPx = l2 ? 164 : 128;
|
|
110
|
+
canvas.width = PLATE_CANVAS_WIDTH;
|
|
111
|
+
canvas.height = heightPx;
|
|
112
|
+
const grd = g.createRadialGradient(256, heightPx / 2, 8, 256, heightPx / 2, 250);
|
|
113
|
+
for (let i = 0; i < backdropStops.length; i++) grd.addColorStop(backdropStops[i][0], backdropStops[i][1]);
|
|
114
|
+
g.save();
|
|
115
|
+
g.translate(0, heightPx / 2);
|
|
116
|
+
g.scale(1, heightPx / PLATE_CANVAS_WIDTH);
|
|
117
|
+
g.translate(0, -heightPx / 2);
|
|
118
|
+
g.fillStyle = grd;
|
|
119
|
+
g.fillRect(0, heightPx / 2 - 256, PLATE_CANVAS_WIDTH, PLATE_CANVAS_WIDTH);
|
|
120
|
+
g.restore();
|
|
121
|
+
g.textAlign = 'center';
|
|
122
|
+
g.textBaseline = 'middle';
|
|
123
|
+
g.shadowColor = shadowColor;
|
|
124
|
+
g.shadowBlur = shadowBlur;
|
|
125
|
+
let widest = 0;
|
|
126
|
+
const fit = (text, font, y, color, max) => {
|
|
127
|
+
g.font = font;
|
|
128
|
+
const w = g.measureText(text).width;
|
|
129
|
+
widest = Math.max(widest, Math.min(w, max));
|
|
130
|
+
g.save();
|
|
131
|
+
g.translate(256, y);
|
|
132
|
+
if (w > max) g.scale(max / w, 1);
|
|
133
|
+
g.fillStyle = color;
|
|
134
|
+
g.fillText(text, 0, 0);
|
|
135
|
+
g.fillText(text, 0, 0);
|
|
136
|
+
g.restore();
|
|
137
|
+
};
|
|
138
|
+
fit(name, fonts.name, 42, nameColor, 490);
|
|
139
|
+
fit(l1, fonts.sub, 95, subColor, 496);
|
|
140
|
+
if (l2) fit(l2, fonts.sub, 131, subColor, 496);
|
|
141
|
+
return {
|
|
142
|
+
canvas,
|
|
143
|
+
plateH: heightPx / 128,
|
|
144
|
+
plateWide: (widest / canvas.width) * PLATE_CANVAS_WIDE,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Creates a zero-allocation per-frame nameplate controller managing sprite creation,
|
|
150
|
+
* deferred GPU texture initialization, screen-space deconfliction, and smooth opacity fading
|
|
151
|
+
* for cast actors and shared group anchors.
|
|
152
|
+
*
|
|
153
|
+
* @param {object} config
|
|
154
|
+
* @param {THREE.PerspectiveCamera} config.camera - Active camera.
|
|
155
|
+
* @param {{ initTexture: (texture: THREE.Texture) => void }} config.renderer - Renderer used to upload canvas textures on the following frame.
|
|
156
|
+
* @param {{ far: number, opacity: (distance: number, limit?: number) => number }} config.labelRange - Label distance fade curve (`createLabelRange`).
|
|
157
|
+
* @param {object[]} config.actors - Dense cast actor array.
|
|
158
|
+
* @param {object[]} [config.groups=[]] - Dense group nameplate anchor array (`createNameplateGroups().groupList`).
|
|
159
|
+
* @param {() => number} config.getViewportWidth - Returns viewport width in CSS pixels.
|
|
160
|
+
* @param {() => number} config.getViewportHeight - Returns viewport height in CSS pixels.
|
|
161
|
+
* @param {() => number} config.getFrame - Returns current frame counter.
|
|
162
|
+
* @param {() => boolean} [config.isActive] - Returns true when nameplates should be shown (e.g. not on title screen).
|
|
163
|
+
* @param {(left: number, top: number, right: number, bottom: number) => boolean} [config.blocked] - Returns true when a screen box overlaps HUD controls.
|
|
164
|
+
* @param {(sprite: THREE.Sprite) => void} [config.onSpriteCreated] - Called once per newly created nameplate sprite (e.g. to place it in a full-res overlay pass).
|
|
165
|
+
* @param {(actor: object) => any} [config.getActorKey] - Deduplication key for actors sharing a name.
|
|
166
|
+
* @param {(actor: object) => [string, string, string]} [config.getActorPlate] - Returns `[name, subtitle, subColor]` when creating an actor's plate canvas.
|
|
167
|
+
* @param {(actor: object) => boolean} [config.isActorEligible] - Returns true when an actor may carry a plate this frame.
|
|
168
|
+
* @param {(group: object) => any} [config.getGroupKey] - Deduplication key for shared group anchors.
|
|
169
|
+
* @param {(group: object) => [string, string, string]} [config.getGroupPlate] - Returns `[name, subtitle, subColor]` when creating a group's plate canvas.
|
|
170
|
+
* @param {(group: object) => number} [config.getGroupScale] - Scale multiplier for a group's plate height.
|
|
171
|
+
* @param {{ name: string, sub: string }} [config.fonts=DEFAULT_PLATE_FONTS] - Canvas font strings.
|
|
172
|
+
* @param {string} [config.nameColor='#f3ecd9'] - Title CSS colour.
|
|
173
|
+
* @param {ReadonlyArray<[number, string]>} [config.backdropStops=DEFAULT_PLATE_BACKDROP] - Backdrop gradient stops.
|
|
174
|
+
* @param {string} [config.separator='·'] - Subtitle wrap separator.
|
|
175
|
+
* @param {number} [config.platesPerFrame=2] - Maximum new plate canvases rendered in one frame.
|
|
176
|
+
* @param {number} [config.capacity=64] - Maximum simultaneous placed plates in layout.
|
|
177
|
+
* @param {number} [config.margin=6] - Viewport edge margin (CSS pixels).
|
|
178
|
+
* @param {number} [config.screenShare=0.031] - Plate height as a fraction of viewport height.
|
|
179
|
+
* @param {number} [config.minPx=36] - Minimum plate height (CSS pixels).
|
|
180
|
+
* @param {readonly [number, number]} [config.figureToPlate=[0.9, 1.6]] - Figure-to-plate height ratio fade band.
|
|
181
|
+
* @param {number} [config.headClearance=0.35] - Vertical offset above head (metres).
|
|
182
|
+
* @param {number} [config.shoulderRatio=0.72] - Vertical fraction of actor height for side-shoulder fallback.
|
|
183
|
+
* @param {number} [config.renderOrder=20] - Sprite `renderOrder`.
|
|
184
|
+
* @param {number} [config.anisotropy=4] - Canvas texture anisotropy.
|
|
185
|
+
* @param {number} [config.fadeRate=8] - Opacity ease rate (1/s).
|
|
186
|
+
* @param {() => HTMLCanvasElement} [config.createCanvas] - Optional canvas factory for headless tests.
|
|
187
|
+
* @returns {{ onPlates: (left: number, top: number, right: number, bottom: number) => boolean, updatePlates: (dt: number) => void }}
|
|
188
|
+
*/
|
|
189
|
+
export function createNameplates({
|
|
190
|
+
camera,
|
|
191
|
+
renderer,
|
|
192
|
+
labelRange,
|
|
193
|
+
actors,
|
|
194
|
+
groups = [],
|
|
195
|
+
getViewportWidth,
|
|
196
|
+
getViewportHeight,
|
|
197
|
+
getFrame,
|
|
198
|
+
isActive = () => true,
|
|
199
|
+
blocked = () => false,
|
|
200
|
+
onSpriteCreated = null,
|
|
201
|
+
getActorKey = a => a.def.name,
|
|
202
|
+
getActorPlate = a => [a.def.name, a.def.sub || '', '#d8c8a8'],
|
|
203
|
+
isActorEligible = a => a.group.visible && a.pose !== 'dead',
|
|
204
|
+
getGroupKey = b => b.bodyDef,
|
|
205
|
+
getGroupPlate = b => [b.bodyDef.name, b.bodyDef.sub, b.bodyDef.color],
|
|
206
|
+
getGroupScale = b => b.bodyDef.plateScale ?? 1,
|
|
207
|
+
fonts = DEFAULT_PLATE_FONTS,
|
|
208
|
+
nameColor = '#f3ecd9',
|
|
209
|
+
backdropStops = DEFAULT_PLATE_BACKDROP,
|
|
210
|
+
separator = '·',
|
|
211
|
+
platesPerFrame = DEFAULT_PLATES_PER_FRAME,
|
|
212
|
+
capacity = 64,
|
|
213
|
+
margin = 6,
|
|
214
|
+
screenShare = DEFAULT_PLATE_SCREEN,
|
|
215
|
+
minPx = DEFAULT_PLATE_MIN_PX,
|
|
216
|
+
figureToPlate = DEFAULT_FIGURE_TO_PLATE,
|
|
217
|
+
headClearance = DEFAULT_HEAD_CLEARANCE,
|
|
218
|
+
shoulderRatio = DEFAULT_SHOULDER_RATIO,
|
|
219
|
+
renderOrder = 20,
|
|
220
|
+
anisotropy = 4,
|
|
221
|
+
fadeRate = 8,
|
|
222
|
+
createCanvas = () => document.createElement('canvas'),
|
|
223
|
+
}) {
|
|
224
|
+
const drawnLast = [];
|
|
225
|
+
const placements = createLabelLayout({ capacity, margin, blocked });
|
|
226
|
+
const plateOrder = [];
|
|
227
|
+
const named = new Set();
|
|
228
|
+
const view = { on: false, H: 0, W: 0, proj: 0, platePx: 0 };
|
|
229
|
+
const canvasStyle = { fonts, nameColor, backdropStops, separator, createCanvas };
|
|
230
|
+
|
|
231
|
+
const aboveHead = a => a.y + a.height + headClearance;
|
|
232
|
+
const shoulderHeight = a => a.y + a.height * shoulderRatio;
|
|
233
|
+
|
|
234
|
+
function makePlate(a, name, sub, color) {
|
|
235
|
+
const { canvas, plateH, plateWide } = renderNameplateCanvas(name, sub, color, canvasStyle);
|
|
236
|
+
a.plateH = plateH;
|
|
237
|
+
a.plateWide = plateWide;
|
|
238
|
+
const tex = new THREE.CanvasTexture(canvas);
|
|
239
|
+
tex.colorSpace = THREE.SRGBColorSpace;
|
|
240
|
+
tex.anisotropy = anisotropy;
|
|
241
|
+
const sp = new THREE.Sprite(
|
|
242
|
+
new THREE.SpriteMaterial({ map: tex, transparent: true, depthWrite: false, depthTest: true, fog: false, opacity: 0 }),
|
|
243
|
+
);
|
|
244
|
+
sp.renderOrder = renderOrder;
|
|
245
|
+
sp.visible = false;
|
|
246
|
+
if (onSpriteCreated) onSpriteCreated(sp);
|
|
247
|
+
a.plate = sp;
|
|
248
|
+
drawnLast.push(a);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function toCard() {
|
|
252
|
+
for (let i = 0; i < drawnLast.length; i++) {
|
|
253
|
+
const a = drawnLast[i];
|
|
254
|
+
renderer.initTexture(a.plate.material.map);
|
|
255
|
+
a.plateOnCard = true;
|
|
256
|
+
}
|
|
257
|
+
drawnLast.length = 0;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function onScreen(a, y) {
|
|
261
|
+
a.deep = Math.max(-_v.set(a.x, y, a.z).applyMatrix4(camera.matrixWorldInverse).z, 0.01);
|
|
262
|
+
_v.set(a.x, y, a.z).project(camera);
|
|
263
|
+
if (!(_v.z > -1 && _v.z < 1 && _v.x > -1.15 && _v.x < 1.15 && _v.y > -1.1 && _v.y < 1.2)) return false;
|
|
264
|
+
a.sx = (_v.x * 0.5 + 0.5) * view.W;
|
|
265
|
+
a.sy = (-_v.y * 0.5 + 0.5) * view.H;
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function wantActors() {
|
|
270
|
+
const { platePx, proj } = view;
|
|
271
|
+
for (let i = 0; i < actors.length; i++) {
|
|
272
|
+
const a = actors[i];
|
|
273
|
+
let want = 0;
|
|
274
|
+
if (view.on && isActorEligible(a) && a.dist < a.range && (onScreen(a, aboveHead(a)) || onScreen(a, shoulderHeight(a)))) {
|
|
275
|
+
const figurePx = (a.height / a.dist) * proj;
|
|
276
|
+
a.ph = clamp((a.deep * platePx) / proj, 0.02, 3.6);
|
|
277
|
+
want = labelRange.opacity(a.dist, a.range) * sstep(figureToPlate[0], figureToPlate[1], figurePx / platePx);
|
|
278
|
+
if (want > 0.01) plateOrder.push(a);
|
|
279
|
+
}
|
|
280
|
+
a.want = want;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function wantGroups() {
|
|
285
|
+
const frame = getFrame();
|
|
286
|
+
for (let i = 0; i < groups.length; i++) {
|
|
287
|
+
const b = groups[i];
|
|
288
|
+
b.dist = b.selection.distance(frame);
|
|
289
|
+
b.want = 0;
|
|
290
|
+
if (!view.on || b.dist >= b.range || !onScreen(b, aboveHead(b))) continue;
|
|
291
|
+
b.ph = ((b.deep * view.platePx) / view.proj) * getGroupScale(b);
|
|
292
|
+
b.want = labelRange.opacity(b.dist);
|
|
293
|
+
if (b.want > 0.01) plateOrder.push(b);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function placePlate(a, isGroup) {
|
|
298
|
+
const hpx = (a.ph / a.deep) * view.proj,
|
|
299
|
+
tall = hpx * (a.plateH || 1),
|
|
300
|
+
width = hpx * ((a.plateWide ?? TEXT_MOST) + LETTER_MARGIN);
|
|
301
|
+
let dx = 0,
|
|
302
|
+
beside = false,
|
|
303
|
+
bottom = NaN;
|
|
304
|
+
if (onScreen(a, aboveHead(a))) bottom = placements.place(a.sx, a.sy, width, tall, tall * 2, hpx * 0.12);
|
|
305
|
+
if (!Number.isFinite(bottom)) {
|
|
306
|
+
if (isGroup || !onScreen(a, shoulderHeight(a))) return false;
|
|
307
|
+
beside = true;
|
|
308
|
+
const clearance = Math.max(8, (a.height * 0.18 * view.proj) / a.deep);
|
|
309
|
+
dx = width / 2 + clearance;
|
|
310
|
+
const foot = a.sy + tall / 2;
|
|
311
|
+
bottom = placements.place(a.sx + dx, foot, width, tall);
|
|
312
|
+
if (!Number.isFinite(bottom)) {
|
|
313
|
+
dx = -dx;
|
|
314
|
+
bottom = placements.place(a.sx + dx, foot, width, tall);
|
|
315
|
+
}
|
|
316
|
+
if (!Number.isFinite(bottom)) return false;
|
|
317
|
+
}
|
|
318
|
+
a.plateBeside = beside;
|
|
319
|
+
a.ph = (hpx * a.deep) / view.proj;
|
|
320
|
+
if (a.plate) a.plate.center.set(0.5 - dx / (hpx * PLATE_CANVAS_WIDE), (bottom - a.sy) / tall);
|
|
321
|
+
return true;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function showPlate(a, k) {
|
|
325
|
+
if (!a.plate) return;
|
|
326
|
+
a.plateA = a.dist >= a.range ? 0 : a.plateA + ((a.want || 0) - a.plateA) * k;
|
|
327
|
+
if (a.plateA < 0.015) {
|
|
328
|
+
if (a.plate.visible) a.plate.visible = false;
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
const sp = a.plate,
|
|
332
|
+
hk = a.plateH || 1;
|
|
333
|
+
sp.visible = true;
|
|
334
|
+
sp.material.opacity = a.plateA;
|
|
335
|
+
sp.position.set(a.x, a.plateBeside ? shoulderHeight(a) : aboveHead(a), a.z);
|
|
336
|
+
sp.scale.set(a.ph * PLATE_CANVAS_WIDE, a.ph * hk, 1);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function updatePlates(dt) {
|
|
340
|
+
const H = getViewportHeight();
|
|
341
|
+
view.on = isActive();
|
|
342
|
+
view.H = H;
|
|
343
|
+
view.W = getViewportWidth();
|
|
344
|
+
view.proj = H / (2 * Math.tan((camera.fov * Math.PI) / 360));
|
|
345
|
+
view.platePx = Math.max(screenShare * H, minPx);
|
|
346
|
+
placements.reset(view.W, H);
|
|
347
|
+
toCard();
|
|
348
|
+
plateOrder.length = 0;
|
|
349
|
+
wantActors();
|
|
350
|
+
wantGroups();
|
|
351
|
+
for (let i = 1; i < plateOrder.length; i++) {
|
|
352
|
+
const a = plateOrder[i];
|
|
353
|
+
let j = i - 1;
|
|
354
|
+
while (j >= 0 && plateOrder[j].dist > a.dist) {
|
|
355
|
+
plateOrder[j + 1] = plateOrder[j];
|
|
356
|
+
j--;
|
|
357
|
+
}
|
|
358
|
+
plateOrder[j + 1] = a;
|
|
359
|
+
}
|
|
360
|
+
let made = 0;
|
|
361
|
+
named.clear();
|
|
362
|
+
for (let i = 0; i < plateOrder.length; i++) {
|
|
363
|
+
const a = plateOrder[i],
|
|
364
|
+
isGroup = !!a.bodyDef,
|
|
365
|
+
key = isGroup ? getGroupKey(a) : getActorKey(a);
|
|
366
|
+
if (named.has(key)) {
|
|
367
|
+
a.want = 0;
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
if (!a.plate && made < platesPerFrame) {
|
|
371
|
+
const [name, sub, color] = isGroup ? getGroupPlate(a) : getActorPlate(a);
|
|
372
|
+
makePlate(a, name, sub, color);
|
|
373
|
+
made++;
|
|
374
|
+
}
|
|
375
|
+
if (placePlate(a, isGroup)) {
|
|
376
|
+
named.add(key);
|
|
377
|
+
if (!a.plateOnCard) a.want = 0;
|
|
378
|
+
} else a.want = 0;
|
|
379
|
+
}
|
|
380
|
+
const k = Math.min(1, dt * fadeRate);
|
|
381
|
+
for (let i = 0; i < actors.length; i++) showPlate(actors[i], k);
|
|
382
|
+
for (let i = 0; i < groups.length; i++) showPlate(groups[i], k);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
return { onPlates: placements.overlaps, updatePlates };
|
|
386
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reusable orbit and showcase camera controller with ground clearance clamping, adaptive portrait
|
|
3
|
+
* field of view, keyboard orbit/zoom bindings, and named viewpoint switching.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as THREE from 'three';
|
|
8
|
+
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Attaches keyboard arrow-key orbit and `+`/`=`/`-` zoom controls to `canvas` matching pointer
|
|
12
|
+
* `OrbitControls`, returning an unbind callback that removes the listener.
|
|
13
|
+
*
|
|
14
|
+
* @param {object} config
|
|
15
|
+
* @param {EventTarget} config.canvas Focusable canvas element receiving `keydown` events.
|
|
16
|
+
* @param {THREE.Camera} config.camera Camera driven by `controls`.
|
|
17
|
+
* @param {{ target: THREE.Vector3, update: () => void }} config.controls Orbit controls instance.
|
|
18
|
+
* @param {number} [config.minDistance=0] Minimum orbit distance from target (metres).
|
|
19
|
+
* @param {number} [config.maxDistance=Infinity] Maximum orbit distance from target (metres).
|
|
20
|
+
* @param {number} [config.yawStep=0.1] Azimuth increment per left/right key press (radians).
|
|
21
|
+
* @param {number} [config.pitchStep=0.08] Polar angle increment per up/down key press (radians).
|
|
22
|
+
* @param {number} [config.zoomFactor=0.88] Radius multiplier per `+` zoom step (`1 / zoomFactor` for `-`).
|
|
23
|
+
* @returns {() => void} Cleanup function removing the `keydown` listener.
|
|
24
|
+
*/
|
|
25
|
+
export function bindKeyboardOrbit({
|
|
26
|
+
canvas,
|
|
27
|
+
camera,
|
|
28
|
+
controls,
|
|
29
|
+
minDistance = 0,
|
|
30
|
+
maxDistance = Infinity,
|
|
31
|
+
yawStep = 0.1,
|
|
32
|
+
pitchStep = 0.08,
|
|
33
|
+
zoomFactor = 0.88,
|
|
34
|
+
}) {
|
|
35
|
+
const offset = new THREE.Vector3();
|
|
36
|
+
const orbit = new THREE.Spherical();
|
|
37
|
+
|
|
38
|
+
function onKeyDown(event) {
|
|
39
|
+
offset.copy(camera.position).sub(controls.target);
|
|
40
|
+
orbit.setFromVector3(offset);
|
|
41
|
+
switch (event.key) {
|
|
42
|
+
case 'ArrowLeft':
|
|
43
|
+
orbit.theta -= yawStep;
|
|
44
|
+
break;
|
|
45
|
+
case 'ArrowRight':
|
|
46
|
+
orbit.theta += yawStep;
|
|
47
|
+
break;
|
|
48
|
+
case 'ArrowUp':
|
|
49
|
+
orbit.phi -= pitchStep;
|
|
50
|
+
break;
|
|
51
|
+
case 'ArrowDown':
|
|
52
|
+
orbit.phi += pitchStep;
|
|
53
|
+
break;
|
|
54
|
+
case '+':
|
|
55
|
+
case '=':
|
|
56
|
+
orbit.radius = Math.max(minDistance, orbit.radius * zoomFactor);
|
|
57
|
+
break;
|
|
58
|
+
case '-':
|
|
59
|
+
orbit.radius = Math.min(maxDistance, orbit.radius / zoomFactor);
|
|
60
|
+
break;
|
|
61
|
+
default:
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
event.preventDefault();
|
|
65
|
+
camera.position.setFromSpherical(orbit.makeSafe()).add(controls.target);
|
|
66
|
+
controls.update();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
canvas.addEventListener('keydown', onKeyDown);
|
|
70
|
+
return () => canvas.removeEventListener('keydown', onKeyDown);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Sets up `OrbitControls` with ground-clearance clamping, adaptive portrait FOV on resize,
|
|
75
|
+
* keyboard orbit/zoom, and optional named viewpoint buttons (`[data-view]`).
|
|
76
|
+
*
|
|
77
|
+
* @param {object} config
|
|
78
|
+
* @param {THREE.PerspectiveCamera} config.camera Perspective camera to control.
|
|
79
|
+
* @param {HTMLCanvasElement} config.canvas Target rendering canvas.
|
|
80
|
+
* @param {THREE.WebGLRenderer} config.renderer Renderer resized with the canvas.
|
|
81
|
+
* @param {THREE.Scene} config.scene Scene drawn on view changes when `onDraw` is omitted.
|
|
82
|
+
* @param {Record<string, { eye: [number, number, number], target: [number, number, number] }>} [config.views={}] Named viewpoints.
|
|
83
|
+
* @param {string} [config.initialView] Initial viewpoint key; defaults to the first key of `views`.
|
|
84
|
+
* @param {(x: number, z: number) => number} [config.groundAt] Ground height sampler (metres) for clearance clamping.
|
|
85
|
+
* @param {((camera: THREE.Camera) => void)|null} [config.guardCamera=null] Custom camera clamp overriding `groundAt` + `minClearance`.
|
|
86
|
+
* @param {number} [config.minClearance=1.0] Minimum height above `groundAt(x, z)` (metres).
|
|
87
|
+
* @param {number} [config.minDistance=4] Minimum orbit distance (metres).
|
|
88
|
+
* @param {number} [config.maxDistance=180] Maximum orbit distance (metres).
|
|
89
|
+
* @param {number} [config.baseFov=42] Landscape vertical FOV in degrees.
|
|
90
|
+
* @param {number} [config.maxFov=68] Maximum vertical FOV on narrow portrait viewports in degrees.
|
|
91
|
+
* @param {number} [config.maxPolarAngle=Math.PI * 0.485] Maximum polar angle from zenith (radians).
|
|
92
|
+
* @param {number} [config.far=2500] Camera far clipping plane (metres).
|
|
93
|
+
* @param {number} [config.maxPixelRatio=2] Upper bound on renderer pixel ratio.
|
|
94
|
+
* @param {boolean} [config.enableDamping=false] Enables OrbitControls damping.
|
|
95
|
+
* @param {boolean} [config.enablePan=false] Enables OrbitControls panning.
|
|
96
|
+
* @param {string|Iterable<Element>|false} [config.viewButtons='[data-view]'] CSS selector or element list for viewpoint buttons, or `false` to skip DOM binding.
|
|
97
|
+
* @param {(() => void)|null} [config.onDraw=null] Custom render callback (pass `() => {}` when an animation loop drives rendering).
|
|
98
|
+
* @returns {{ controls: OrbitControls, selectView: (name: string) => void, resize: () => void, clampCamera: () => void, dispose: () => void }}
|
|
99
|
+
*/
|
|
100
|
+
export function createOrbitCamera({
|
|
101
|
+
camera,
|
|
102
|
+
canvas,
|
|
103
|
+
renderer,
|
|
104
|
+
scene,
|
|
105
|
+
views = {},
|
|
106
|
+
initialView,
|
|
107
|
+
groundAt = () => 0,
|
|
108
|
+
guardCamera = null,
|
|
109
|
+
minClearance = 1.0,
|
|
110
|
+
minDistance = 4,
|
|
111
|
+
maxDistance = 180,
|
|
112
|
+
baseFov = 42,
|
|
113
|
+
maxFov = 68,
|
|
114
|
+
maxPolarAngle = Math.PI * 0.485,
|
|
115
|
+
far = 2500,
|
|
116
|
+
maxPixelRatio = 2,
|
|
117
|
+
enableDamping = false,
|
|
118
|
+
enablePan = false,
|
|
119
|
+
viewButtons = '[data-view]',
|
|
120
|
+
onDraw = null,
|
|
121
|
+
}) {
|
|
122
|
+
const controls = new OrbitControls(camera, canvas);
|
|
123
|
+
controls.enableDamping = enableDamping;
|
|
124
|
+
controls.enablePan = enablePan;
|
|
125
|
+
controls.maxPolarAngle = maxPolarAngle;
|
|
126
|
+
controls.minDistance = minDistance;
|
|
127
|
+
controls.maxDistance = maxDistance;
|
|
128
|
+
camera.far = far;
|
|
129
|
+
|
|
130
|
+
const buttons =
|
|
131
|
+
viewButtons === false || viewButtons === null
|
|
132
|
+
? []
|
|
133
|
+
: typeof viewButtons === 'string'
|
|
134
|
+
? typeof document !== 'undefined'
|
|
135
|
+
? Array.from(document.querySelectorAll(viewButtons))
|
|
136
|
+
: []
|
|
137
|
+
: Array.from(viewButtons);
|
|
138
|
+
|
|
139
|
+
function clampCamera() {
|
|
140
|
+
if (guardCamera) {
|
|
141
|
+
guardCamera(camera);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const floor = groundAt(camera.position.x, camera.position.z);
|
|
145
|
+
if (Number.isFinite(floor) && camera.position.y < floor + minClearance) {
|
|
146
|
+
camera.position.y = floor + minClearance;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function render() {
|
|
151
|
+
clampCamera();
|
|
152
|
+
camera.lookAt(controls.target);
|
|
153
|
+
if (onDraw) onDraw();
|
|
154
|
+
else renderer.render(scene, camera);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function onControlsChange() {
|
|
158
|
+
clampCamera();
|
|
159
|
+
camera.lookAt(controls.target);
|
|
160
|
+
if (!onDraw) renderer.render(scene, camera);
|
|
161
|
+
}
|
|
162
|
+
controls.addEventListener('change', onControlsChange);
|
|
163
|
+
|
|
164
|
+
function selectView(name) {
|
|
165
|
+
const spec = views[name];
|
|
166
|
+
if (!spec) return;
|
|
167
|
+
controls.target.fromArray(spec.target);
|
|
168
|
+
camera.position.fromArray(spec.eye);
|
|
169
|
+
clampCamera();
|
|
170
|
+
controls.update();
|
|
171
|
+
camera.lookAt(controls.target);
|
|
172
|
+
for (const btn of buttons) {
|
|
173
|
+
if (btn.dataset?.view in views) {
|
|
174
|
+
btn.setAttribute('aria-pressed', String(btn.dataset.view === name));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (!onDraw) renderer.render(scene, camera);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const buttonListeners = [];
|
|
181
|
+
for (const btn of buttons) {
|
|
182
|
+
if (btn.dataset?.view in views) {
|
|
183
|
+
const onClick = () => selectView(btn.dataset.view);
|
|
184
|
+
btn.addEventListener('click', onClick);
|
|
185
|
+
buttonListeners.push([btn, onClick]);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const unbindKeys = bindKeyboardOrbit({ canvas, camera, controls, minDistance, maxDistance });
|
|
190
|
+
|
|
191
|
+
function resize() {
|
|
192
|
+
const dpr = typeof devicePixelRatio === 'number' ? devicePixelRatio : 1;
|
|
193
|
+
renderer.setPixelRatio(Math.min(dpr, maxPixelRatio));
|
|
194
|
+
renderer.setSize(canvas.clientWidth, canvas.clientHeight, false);
|
|
195
|
+
camera.aspect = canvas.clientWidth / Math.max(1, canvas.clientHeight);
|
|
196
|
+
camera.fov = THREE.MathUtils.clamp(baseFov / Math.min(camera.aspect, 1), baseFov, maxFov);
|
|
197
|
+
camera.updateProjectionMatrix();
|
|
198
|
+
render();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const hasWindow = typeof window !== 'undefined' && typeof window.addEventListener === 'function';
|
|
202
|
+
if (hasWindow) window.addEventListener('resize', resize);
|
|
203
|
+
const first = initialView ?? Object.keys(views)[0];
|
|
204
|
+
if (first) selectView(first);
|
|
205
|
+
resize();
|
|
206
|
+
|
|
207
|
+
function dispose() {
|
|
208
|
+
if (hasWindow) window.removeEventListener('resize', resize);
|
|
209
|
+
controls.removeEventListener('change', onControlsChange);
|
|
210
|
+
for (const [btn, onClick] of buttonListeners) btn.removeEventListener('click', onClick);
|
|
211
|
+
unbindKeys();
|
|
212
|
+
controls.dispose();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return { controls, selectView, resize, clampCamera, dispose };
|
|
216
|
+
}
|