roamwright 0.54.0 → 0.55.1
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/index.js +5 -0
- package/motion/cast-actors.js +379 -0
- package/motion/cast-manager.js +321 -0
- package/package.json +20 -1
- package/rendering/cast-stage.js +299 -0
- package/rendering/nameplate-groups.js +84 -0
- package/rendering/nameplates.js +386 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared nameplate anchors for crowds, formations, or flocks: each named group attaches
|
|
3
|
+
* one anchor record and offers its members each frame so the nearest visible standing
|
|
4
|
+
* member carries the group's single nameplate.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const DEFAULT_HEAD_CLEARANCE = 0.35; // metres above member top where the label attaches
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Creates a manager for group nameplate anchors bound to a label fade range, camera, and
|
|
12
|
+
* frame counter. Zero allocations per frame in `considerMember`.
|
|
13
|
+
*
|
|
14
|
+
* @param {object} config
|
|
15
|
+
* @param {{ far: number, selection: () => { accept: Function, distance: Function } }} config.labelRange - Label visibility range (`createLabelRange`).
|
|
16
|
+
* @param {() => THREE.Camera} config.getCamera - Returns the active camera.
|
|
17
|
+
* @param {() => number} config.getFrame - Returns the current frame counter.
|
|
18
|
+
* @param {number} [config.headClearance=0.35] - Vertical offset (metres) above a member's height for frustum testing.
|
|
19
|
+
* @returns {{ groupList: object[], attachGroup: (group: object, def: object) => object, considerMember: (anchor: object, x: number, y: number, z: number, height: number) => void, clearGroups: () => void }}
|
|
20
|
+
*/
|
|
21
|
+
export function createNameplateGroups({ labelRange, getCamera, getFrame, headClearance = DEFAULT_HEAD_CLEARANCE }) {
|
|
22
|
+
const groupList = [];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Attaches a shared nameplate anchor record to `group` using metadata `def` (`{ name, sub, color, plateScale? }`).
|
|
26
|
+
* Stores the anchor on `group.bodyPlate` and registers it in `groupList`.
|
|
27
|
+
*
|
|
28
|
+
* @param {object} group - Crowd or flock object receiving the anchor.
|
|
29
|
+
* @param {object} def - Group nameplate definition.
|
|
30
|
+
* @returns {object} The passed `group` for chaining.
|
|
31
|
+
*/
|
|
32
|
+
function attachGroup(group, def) {
|
|
33
|
+
// Keep one monomorphic shape matching cast actor nameplate fields so rendering/nameplates.js shares one hot path.
|
|
34
|
+
const anchor = {
|
|
35
|
+
bodyDef: def,
|
|
36
|
+
range: labelRange.far,
|
|
37
|
+
selection: labelRange.selection(),
|
|
38
|
+
x: 0,
|
|
39
|
+
y: 0,
|
|
40
|
+
z: 0,
|
|
41
|
+
height: 0,
|
|
42
|
+
dist: Infinity,
|
|
43
|
+
sx: 0,
|
|
44
|
+
sy: 0,
|
|
45
|
+
ph: 0,
|
|
46
|
+
want: 0,
|
|
47
|
+
plate: null,
|
|
48
|
+
plateH: 1,
|
|
49
|
+
plateOnCard: false,
|
|
50
|
+
plateA: 0,
|
|
51
|
+
plateBeside: false,
|
|
52
|
+
};
|
|
53
|
+
groupList.push(anchor);
|
|
54
|
+
group.bodyPlate = anchor;
|
|
55
|
+
return group;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Offers a member's resolved world position to its group's anchor; retains the nearest
|
|
60
|
+
* member whose label point falls inside the camera frustum for the current frame.
|
|
61
|
+
*
|
|
62
|
+
* @param {object} anchor - Group anchor (`group.bodyPlate`).
|
|
63
|
+
* @param {number} x - Member world X (metres).
|
|
64
|
+
* @param {number} y - Member base world Y (metres).
|
|
65
|
+
* @param {number} z - Member world Z (metres).
|
|
66
|
+
* @param {number} height - Member height (metres).
|
|
67
|
+
*/
|
|
68
|
+
function considerMember(anchor, x, y, z, height) {
|
|
69
|
+
if (!anchor.selection.accept(getFrame(), getCamera(), x, y + height * 0.5, z, y + height + headClearance)) return;
|
|
70
|
+
anchor.x = x;
|
|
71
|
+
anchor.y = y;
|
|
72
|
+
anchor.z = z;
|
|
73
|
+
anchor.height = height;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Removes all registered group anchors.
|
|
78
|
+
*/
|
|
79
|
+
function clearGroups() {
|
|
80
|
+
groupList.length = 0;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return { groupList, attachGroup, considerMember, clearGroups };
|
|
84
|
+
}
|
|
@@ -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
|
+
}
|