castle-web-cli 0.4.231 → 0.4.232
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/dist/serverBundle.js +5 -3
- package/kits/hub-game/CLAUDE.md +26 -4
- package/kits/hub-game/code/client/avatar.js +39 -0
- package/kits/hub-game/code/client/chat.js +12 -0
- package/kits/hub-game/code/client/chrome.js +194 -4
- package/kits/hub-game/code/client/grab.js +31 -7
- package/kits/hub-game/code/client/hats.js +26 -0
- package/kits/hub-game/code/client/hookLayer.js +30 -3
- package/kits/hub-game/code/client/hudClear.js +126 -0
- package/kits/hub-game/code/client/state.js +22 -0
- package/kits/hub-game/code/client/tiers.js +31 -6
- package/kits/hub-game/code/client/together.js +66 -1
- package/kits/hub-game/code/client/wear.js +96 -14
- package/kits/hub-game/code/server/actors.js +5 -0
- package/kits/hub-game/code/server/grab.js +296 -11
- package/kits/hub-game/code/server/hub.js +63 -2
- package/kits/hub-game/code/server/manifest.js +48 -3
- package/kits/hub-game/code/server/party.js +11 -0
- package/kits/hub-game/code/server/say.js +8 -2
- package/kits/hub-game/code/server/wear.js +116 -11
- package/kits/hub-game/code/systems/hub.js +253 -5
- package/kits/hub-game/docs/contract.md +204 -8
- package/kits/hub-game/package.json +1 -1
- package/kits/hub-game/scripts/test-chat.mjs +32 -0
- package/kits/hub-game/scripts/test-grab-client.mjs +186 -3
- package/kits/hub-game/scripts/test-grab.mjs +857 -3
- package/kits/hub-game/scripts/test-hats.mjs +229 -0
- package/kits/hub-game/scripts/test-hub-trip.mjs +513 -0
- package/kits/hub-game/scripts/test-hud-clear.mjs +676 -0
- package/kits/hub-game/scripts/test-manifest.mjs +75 -0
- package/kits/hub-game/scripts/test-party.mjs +22 -1
- package/kits/hub-game/scripts/test-say.mjs +59 -2
- package/kits/hub-game/scripts/test-tiers.mjs +190 -2
- package/kits/hub-game/scripts/test-try-on.mjs +940 -0
- package/kits/physics-2d/castle.json +1 -1
- package/kits/physics-2d/engine/scene.js +35 -0
- package/kits/physics-2d/scripts/test-skip-unchanged.mjs +44 -0
- package/kits/physics-3d/CLAUDE.md +252 -0
- package/kits/physics-3d/behaviors/Player.jsx +363 -2
- package/kits/physics-3d/castle.json +2 -2
- package/kits/physics-3d/docs/pxmodel-format.md +4 -0
- package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +32 -0
- package/kits/physics-3d/engine3d/optimize.js +49 -0
- package/kits/physics-3d/engine3d/orbitinput.js +277 -0
- package/kits/physics-3d/engine3d/world3d.js +81 -25
- package/kits/physics-3d/scripts/Player.before-orbit.mjs +310 -0
- package/kits/physics-3d/scripts/Scene3DPlayer.before-orbit.jsx +337 -0
- package/kits/physics-3d/scripts/test-camera-follow.mjs +12 -2
- package/kits/physics-3d/scripts/test-camera-orbit.mjs +2105 -0
- package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +787 -0
- package/kits/physics-3d/scripts/test-static-actors.mjs +6 -1
- package/kits/physics-3d/scripts/test-static-passes.mjs +1 -1
- package/kits/physics-3d/scripts/test-steady-hover.mjs +633 -0
- package/kits/physics-3d/systems/physics3d.js +91 -6
- package/package.json +1 -1
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// Pointer, wheel and right-button input for a scene that set `"engine": {
|
|
2
|
+
// "camera": { "follow": "rigid", "control": "orbit" } }` (the kit's CLAUDE.md
|
|
3
|
+
// has the terms): the player turns, tilts and zooms the Player's camera.
|
|
4
|
+
// Scene3DPlayer's `attachInput` makes this the first time a pointer goes down
|
|
5
|
+
// under the key or a Player runs under it (`runtime.orbitInput()`), hands
|
|
6
|
+
// each pointer that goes down under the key to it, and leaves every other one
|
|
7
|
+
// to the handlers it always had. A runtime that never meets the key never
|
|
8
|
+
// makes it.
|
|
9
|
+
//
|
|
10
|
+
// - touch and pen: a drag that starts in the left half of the canvas is the
|
|
11
|
+
// movement stick (`runtime.moveInput`, as without the key); one that starts
|
|
12
|
+
// in the right half turns and tilts the camera; two fingers that both
|
|
13
|
+
// landed in the right half are a pinch, which zooms. The left half is the
|
|
14
|
+
// stick's: a finger that lands there is never part of a pinch.
|
|
15
|
+
// - mouse: a drag with any button turns and tilts; the wheel zooms.
|
|
16
|
+
// - a short tap that did not move is a jump, as without the key (a mouse's
|
|
17
|
+
// left button only).
|
|
18
|
+
//
|
|
19
|
+
// What the camera is asked for is added up on `runtime.lookInput` until the
|
|
20
|
+
// Player takes it (`consume`), once a frame.
|
|
21
|
+
|
|
22
|
+
// Radians of turn or tilt per CSS pixel dragged.
|
|
23
|
+
export const LOOK_RAD_PER_PX = 0.006;
|
|
24
|
+
// One notch of a mouse wheel moves the camera this much nearer or farther.
|
|
25
|
+
export const WHEEL_ZOOM_PER_NOTCH = 1.1;
|
|
26
|
+
const WHEEL_NOTCH_PX = 100;
|
|
27
|
+
// A wheel that reports lines: a notch is three of them.
|
|
28
|
+
const WHEEL_LINE_PX = WHEEL_NOTCH_PX / 3;
|
|
29
|
+
// The most one wheel event zooms (a flung trackpad reports hundreds of px).
|
|
30
|
+
const WHEEL_ZOOM_MOST = 1.5;
|
|
31
|
+
// Fingers closer than this count as this far apart, so a pinch that closes
|
|
32
|
+
// to nothing does not zoom without bound.
|
|
33
|
+
const PINCH_NEAREST_PX = 24;
|
|
34
|
+
// What is asked for and not yet taken stays within this.
|
|
35
|
+
const ZOOM_PENDING_MOST = 16;
|
|
36
|
+
|
|
37
|
+
const within = (value, least, most) => Math.max(least, Math.min(most, value));
|
|
38
|
+
|
|
39
|
+
// The stick a drag of (dx, dy) pixels holds: that over `radius`, no longer
|
|
40
|
+
// than 1.
|
|
41
|
+
export function stickFrom(dx, dy, radius) {
|
|
42
|
+
const len = Math.hypot(dx, dy) / radius;
|
|
43
|
+
const scale = len > 1 ? 1 / (len * radius) : 1 / radius;
|
|
44
|
+
return { x: dx * scale, y: dy * scale };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// The pointers that are down and what each is doing (its role):
|
|
48
|
+
// 'tap' down, not yet moved past the slop
|
|
49
|
+
// 'stick' holds the movement stick
|
|
50
|
+
// 'look' turns and tilts the camera
|
|
51
|
+
// 'pinch' one of the two fingers zooming
|
|
52
|
+
// 'spent' nothing more until it lifts (the finger left over from a pinch,
|
|
53
|
+
// a second drag in the left half, a drag begun in the right half
|
|
54
|
+
// while two fingers pinch)
|
|
55
|
+
// No DOM here: `down`, `move`, `up` and `wheel` are handed plain numbers, so
|
|
56
|
+
// the roles can be tested. `stick` is `runtime.moveInput`'s object, `onTap`
|
|
57
|
+
// is called for a jump, `tuning` is attachInput's { stickRadius, tapSlop,
|
|
58
|
+
// tapMs }.
|
|
59
|
+
export function makeLookGestures(stick, onTap, tuning) {
|
|
60
|
+
const pointers = new Map();
|
|
61
|
+
let stickId = null;
|
|
62
|
+
let locked = false;
|
|
63
|
+
const asked = { yaw: 0, pitch: 0, zoom: 1 };
|
|
64
|
+
const taken = { yaw: 0, pitch: 0, zoom: 1, locked: false };
|
|
65
|
+
const holding = (role) => [...pointers.values()].filter((p) => p.role === role);
|
|
66
|
+
const letGoOfStick = () => {
|
|
67
|
+
stickId = null;
|
|
68
|
+
stick.active = false;
|
|
69
|
+
stick.x = 0;
|
|
70
|
+
stick.y = 0;
|
|
71
|
+
};
|
|
72
|
+
const drop = () => {
|
|
73
|
+
asked.yaw = 0;
|
|
74
|
+
asked.pitch = 0;
|
|
75
|
+
asked.zoom = 1;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// What `runtime.lookInput` is: the turn and tilt (radians) and the zoom (a
|
|
79
|
+
// factor on the camera's distance; under 1 is nearer) asked for since they
|
|
80
|
+
// were last taken.
|
|
81
|
+
const look = {
|
|
82
|
+
// Takes what was asked for. The object returned is the same one each
|
|
83
|
+
// time. After `lock` it takes nothing: what was asked is dropped, and
|
|
84
|
+
// `locked` says the camera is not the player's this frame.
|
|
85
|
+
consume() {
|
|
86
|
+
taken.locked = locked;
|
|
87
|
+
taken.yaw = locked ? 0 : asked.yaw;
|
|
88
|
+
taken.pitch = locked ? 0 : asked.pitch;
|
|
89
|
+
taken.zoom = locked ? 1 : asked.zoom;
|
|
90
|
+
locked = false;
|
|
91
|
+
drop();
|
|
92
|
+
return taken;
|
|
93
|
+
},
|
|
94
|
+
// The next `consume` takes nothing (the hub during a door trip: its
|
|
95
|
+
// camera, not the player's). Called again each frame it should hold.
|
|
96
|
+
lock() {
|
|
97
|
+
locked = true;
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// What a pointer that has just moved past the slop becomes.
|
|
102
|
+
const roleOnDrag = (p) => {
|
|
103
|
+
if (p.mouse) return 'look';
|
|
104
|
+
if (p.left) return stickId === null ? 'stick' : 'spent';
|
|
105
|
+
return holding('pinch').length > 0 ? 'spent' : 'look';
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const zoomBy = (factor) => {
|
|
109
|
+
asked.zoom = within(asked.zoom * factor, 1 / ZOOM_PENDING_MOST, ZOOM_PENDING_MOST);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const apart = (a, b) => Math.max(PINCH_NEAREST_PX, Math.hypot(a.x - b.x, a.y - b.y));
|
|
113
|
+
|
|
114
|
+
const gestures = {
|
|
115
|
+
// Whether any pointer is down here.
|
|
116
|
+
busy: false,
|
|
117
|
+
look,
|
|
118
|
+
roleOf: (id) => pointers.get(id)?.role ?? null,
|
|
119
|
+
|
|
120
|
+
// `at` is { x, y, time, mouse, button, left }: where (CSS px), when (ms),
|
|
121
|
+
// whether it is a mouse and which button, and whether it is in the left
|
|
122
|
+
// half of the canvas.
|
|
123
|
+
down(id, at) {
|
|
124
|
+
const p = { x0: at.x, y0: at.y, x: at.x, y: at.y, time: at.time, moved: false, role: 'tap', mouse: at.mouse === true, button: at.button ?? 0, left: at.left === true };
|
|
125
|
+
if (!p.mouse && !p.left && holding('pinch').length === 0) {
|
|
126
|
+
// A second finger in the right half, beside one that landed there
|
|
127
|
+
// and is still a tap or turning the camera: a pinch. A finger in the
|
|
128
|
+
// left half is the stick's and is left out of it, on either side.
|
|
129
|
+
const others = [...pointers.values()].filter((o) => !o.mouse && !o.left && (o.role === 'tap' || o.role === 'look'));
|
|
130
|
+
if (others.length === 1) {
|
|
131
|
+
others[0].role = 'pinch';
|
|
132
|
+
p.role = 'pinch';
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
pointers.set(id, p);
|
|
136
|
+
gestures.busy = true;
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
// Returns whether the pointer is one that went down here.
|
|
140
|
+
move(id, x, y) {
|
|
141
|
+
const p = pointers.get(id);
|
|
142
|
+
if (!p) return false;
|
|
143
|
+
const from = { x: p.x, y: p.y };
|
|
144
|
+
if (!p.moved && Math.hypot(x - p.x0, y - p.y0) > tuning.tapSlop) {
|
|
145
|
+
p.moved = true;
|
|
146
|
+
if (p.role === 'tap') p.role = roleOnDrag(p);
|
|
147
|
+
if (p.role === 'stick') stickId = id;
|
|
148
|
+
}
|
|
149
|
+
if (p.role === 'pinch') {
|
|
150
|
+
const other = holding('pinch').find((o) => o !== p);
|
|
151
|
+
const before = other ? apart(from, other) : 0;
|
|
152
|
+
p.x = x;
|
|
153
|
+
p.y = y;
|
|
154
|
+
// Fingers moving apart bring the camera nearer.
|
|
155
|
+
if (other) zoomBy(before / apart(p, other));
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
p.x = x;
|
|
159
|
+
p.y = y;
|
|
160
|
+
if (p.role === 'stick') {
|
|
161
|
+
Object.assign(stick, stickFrom(x - p.x0, y - p.y0, tuning.stickRadius));
|
|
162
|
+
stick.active = true;
|
|
163
|
+
} else if (p.role === 'look') {
|
|
164
|
+
// Dragging right swings the view right; dragging up looks up.
|
|
165
|
+
asked.yaw += (x - from.x) * LOOK_RAD_PER_PX;
|
|
166
|
+
asked.pitch += (y - from.y) * LOOK_RAD_PER_PX;
|
|
167
|
+
}
|
|
168
|
+
return true;
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
// A pointer lifted, or cancelled (no tap then). Returns whether it is one
|
|
172
|
+
// that went down here.
|
|
173
|
+
up(id, time, cancelled) {
|
|
174
|
+
const p = pointers.get(id);
|
|
175
|
+
if (!p) return false;
|
|
176
|
+
pointers.delete(id);
|
|
177
|
+
gestures.busy = pointers.size > 0;
|
|
178
|
+
if (stickId === id) letGoOfStick();
|
|
179
|
+
// The finger left from a pinch neither turns the camera nor walks.
|
|
180
|
+
if (p.role === 'pinch') for (const other of holding('pinch')) other.role = 'spent';
|
|
181
|
+
// Only a finger that is still a tap jumps: never one of a pinch, nor
|
|
182
|
+
// one left spent.
|
|
183
|
+
const tapped = !cancelled && p.role === 'tap' && !p.moved && time - p.time < tuning.tapMs;
|
|
184
|
+
if (tapped && (!p.mouse || p.button === 0)) onTap();
|
|
185
|
+
return true;
|
|
186
|
+
},
|
|
187
|
+
|
|
188
|
+
// Every pointer is let go of at once, with no tap: the stick if one of
|
|
189
|
+
// them held it, what was asked of the camera and not yet taken, and a
|
|
190
|
+
// lock. For a scene load.
|
|
191
|
+
cancel() {
|
|
192
|
+
pointers.clear();
|
|
193
|
+
gestures.busy = false;
|
|
194
|
+
if (stickId !== null) letGoOfStick();
|
|
195
|
+
locked = false;
|
|
196
|
+
drop();
|
|
197
|
+
},
|
|
198
|
+
|
|
199
|
+
// A wheel event's deltaY and deltaMode: away from the player is nearer.
|
|
200
|
+
wheel(deltaY, deltaMode) {
|
|
201
|
+
const px = deltaY * (deltaMode === 1 ? WHEEL_LINE_PX : deltaMode === 2 ? WHEEL_NOTCH_PX : 1);
|
|
202
|
+
if (!Number.isFinite(px) || px === 0) return;
|
|
203
|
+
zoomBy(within(WHEEL_ZOOM_PER_NOTCH ** (px / WHEEL_NOTCH_PX), 1 / WHEEL_ZOOM_MOST, WHEEL_ZOOM_MOST));
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
return gestures;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// The look inputs this file made, so the engine acts on its own input only
|
|
210
|
+
// (`isOrbitLook`): something else on `runtime.lookInput` is left alone.
|
|
211
|
+
const ours = new WeakSet();
|
|
212
|
+
export const isOrbitLook = (look) => ours.has(look);
|
|
213
|
+
|
|
214
|
+
// The gestures on a canvas; this sets `runtime.lookInput`. The wheel and
|
|
215
|
+
// context-menu listeners are added by `runtime.lookInput.listen()`, which the
|
|
216
|
+
// Player calls every frame it runs under the key, and taken off by
|
|
217
|
+
// `runtime.lookInput.reset()`, which the physics system calls on every scene
|
|
218
|
+
// load and on dispose (`Physics3DSystem.reset`), with every pointer let go of
|
|
219
|
+
// (`cancel`). A tap already handed to attachInput and not yet taken stays
|
|
220
|
+
// with it across the load, as it does without the key. So from the first
|
|
221
|
+
// frame of a scene without the key there is no
|
|
222
|
+
// pointer, listener or pending look here, and a pointer held across any load
|
|
223
|
+
// does nothing more until it lifts: the handlers attachInput always had do
|
|
224
|
+
// not know it either.
|
|
225
|
+
export function attachOrbitInput(canvas, runtime, stick, onTap, tuning) {
|
|
226
|
+
const gestures = makeLookGestures(stick, onTap, tuning);
|
|
227
|
+
let listening = false;
|
|
228
|
+
const onWheel = (event) => {
|
|
229
|
+
event.preventDefault();
|
|
230
|
+
gestures.wheel(event.deltaY, event.deltaMode);
|
|
231
|
+
};
|
|
232
|
+
const onContextMenu = (event) => event.preventDefault();
|
|
233
|
+
const unlisten = () => {
|
|
234
|
+
if (!listening) return;
|
|
235
|
+
listening = false;
|
|
236
|
+
canvas.removeEventListener('wheel', onWheel);
|
|
237
|
+
canvas.removeEventListener('contextmenu', onContextMenu);
|
|
238
|
+
};
|
|
239
|
+
gestures.look.listen = () => {
|
|
240
|
+
if (listening || runtime.cameraControl !== 'orbit') return;
|
|
241
|
+
listening = true;
|
|
242
|
+
canvas.addEventListener('wheel', onWheel, { passive: false });
|
|
243
|
+
canvas.addEventListener('contextmenu', onContextMenu);
|
|
244
|
+
};
|
|
245
|
+
gestures.look.reset = () => {
|
|
246
|
+
gestures.cancel();
|
|
247
|
+
unlisten();
|
|
248
|
+
};
|
|
249
|
+
ours.add(gestures.look);
|
|
250
|
+
runtime.lookInput = gestures.look;
|
|
251
|
+
return {
|
|
252
|
+
// Whether a pointer that went down under the key is still down: its
|
|
253
|
+
// moves and its lift are this file's.
|
|
254
|
+
get busy() {
|
|
255
|
+
return gestures.busy;
|
|
256
|
+
},
|
|
257
|
+
down(event) {
|
|
258
|
+
canvas.focus();
|
|
259
|
+
const rect = canvas.getBoundingClientRect();
|
|
260
|
+
gestures.down(event.pointerId, {
|
|
261
|
+
x: event.clientX,
|
|
262
|
+
y: event.clientY,
|
|
263
|
+
time: performance.now(),
|
|
264
|
+
mouse: event.pointerType === 'mouse',
|
|
265
|
+
button: event.button,
|
|
266
|
+
left: event.clientX - rect.left < rect.width / 2,
|
|
267
|
+
});
|
|
268
|
+
canvas.setPointerCapture(event.pointerId);
|
|
269
|
+
},
|
|
270
|
+
move: (event) => gestures.move(event.pointerId, event.clientX, event.clientY),
|
|
271
|
+
up: (event) => gestures.up(event.pointerId, performance.now(), event.type === 'pointercancel'),
|
|
272
|
+
detach: () => {
|
|
273
|
+
gestures.cancel();
|
|
274
|
+
unlisten();
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
}
|
|
@@ -27,6 +27,7 @@ import {
|
|
|
27
27
|
releaseModel,
|
|
28
28
|
copyModel,
|
|
29
29
|
isStaticModel,
|
|
30
|
+
dropEmptyGroups,
|
|
30
31
|
makeInstances,
|
|
31
32
|
drawInstances,
|
|
32
33
|
aimInstances,
|
|
@@ -199,9 +200,9 @@ function actorModelSig(actor, sprites, files) {
|
|
|
199
200
|
// Build / refresh the node for one actor. Rebuilds only when what it looks
|
|
200
201
|
// like (Shape / Model / their art) changes. A Model replaces the Shape's own
|
|
201
202
|
// mesh with the .pxmodel rig; the Shape still sizes the physics collider.
|
|
202
|
-
function ensureActorNode(world, actor, sprites, files) {
|
|
203
|
+
function ensureActorNode(world, actor, sprites, files, drops) {
|
|
203
204
|
const modelSig = actorModelSig(actor, sprites, files);
|
|
204
|
-
const sig = nodeSignature(actor, sprites, modelSig);
|
|
205
|
+
const sig = builtAs(drops, nodeSignature(actor, sprites, modelSig));
|
|
205
206
|
let entry = world.nodes.get(actor.id);
|
|
206
207
|
if (entry && entry.sig !== sig) {
|
|
207
208
|
releaseActorNode(world, entry);
|
|
@@ -218,6 +219,7 @@ function ensureActorNode(world, actor, sprites, files) {
|
|
|
218
219
|
addModelRig(world, entry, model, modelComponent, `${modelComponent.file}|${modelSig}`, {
|
|
219
220
|
sprites,
|
|
220
221
|
files,
|
|
222
|
+
drops,
|
|
221
223
|
});
|
|
222
224
|
} else if (shape) {
|
|
223
225
|
node.add(makeShapeMesh(shape, sprites));
|
|
@@ -230,12 +232,18 @@ function ensureActorNode(world, actor, sprites, files) {
|
|
|
230
232
|
|
|
231
233
|
// The actor's model, from the shared build of it: a copy of its own, or,
|
|
232
234
|
// for a static model in the player, a place among that model's instances.
|
|
233
|
-
function addModelRig(world, entry, model, modelComponent, key, { sprites, files }) {
|
|
235
|
+
function addModelRig(world, entry, model, modelComponent, key, { sprites, files, drops }) {
|
|
234
236
|
const file = modelComponent.file;
|
|
237
|
+
if (drops === true) key = `${key}|dropEmptyParts`;
|
|
235
238
|
entry.modelKey = key;
|
|
236
239
|
const template = takeModel(world.models, key, () =>
|
|
237
240
|
buildModelNode(model, sprites, files, file, [file], { optimize: true })
|
|
238
241
|
);
|
|
242
|
+
if (drops === true && withoutEmptyParts(template, model).children.length === 0) {
|
|
243
|
+
// A model that draws nothing has no rig; `modelParts` is there and empty.
|
|
244
|
+
entry.node.userData.modelParts = new Map();
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
239
247
|
const instanced = world.instanceModels && isStaticModel(template);
|
|
240
248
|
const rig = instanced ? new THREE.Group() : copyModel(template);
|
|
241
249
|
rig.position.y = modelComponent.yOffset ?? 0;
|
|
@@ -252,6 +260,51 @@ function addModelRig(world, entry, model, modelComponent, key, { sprites, files
|
|
|
252
260
|
}
|
|
253
261
|
}
|
|
254
262
|
|
|
263
|
+
//
|
|
264
|
+
// Scenes that set `"engine": { "dropEmptyParts": true }`
|
|
265
|
+
//
|
|
266
|
+
// A game build merges a model's meshes into a few draw calls and leaves the
|
|
267
|
+
// group of every part standing, most of them now empty; a town's scene is two
|
|
268
|
+
// thirds such groups, each visited by three.js and by the engine on every
|
|
269
|
+
// frame. For a scene that asks, a placed model is built without the groups
|
|
270
|
+
// that draw nothing (optimize.js `dropEmptyGroups` has what goes and what
|
|
271
|
+
// always stays), and an actor whose model draws nothing at all gets no rig
|
|
272
|
+
// under its node. CLAUDE.md has the terms.
|
|
273
|
+
//
|
|
274
|
+
// With the key absent nothing here runs but the tests of `drops`: a node's
|
|
275
|
+
// signature, a shared build's key and the build are what they were. With it,
|
|
276
|
+
// the signature and the key say so, so that a node or a build made for a
|
|
277
|
+
// scene that asked is never one for a scene that did not, in either
|
|
278
|
+
// direction: the nodes of a resident scene parked while another plays, a pack
|
|
279
|
+
// mounted in a town, a door trip, a scene file reloaded with the key changed.
|
|
280
|
+
//
|
|
281
|
+
// Whether a pass drops is the pass's own: `syncActors` reads it once from the
|
|
282
|
+
// runtime it was handed and passes it down (`drops`) to everything that makes
|
|
283
|
+
// a signature or a build. Nothing about it is kept on the world, so a pass
|
|
284
|
+
// entered from inside another (a callback that syncs another scene), one that
|
|
285
|
+
// throws, or a dispose, cannot change what the pass around it builds. What is
|
|
286
|
+
// kept says which way it was made and is only ever matched against the pass
|
|
287
|
+
// asking: a node's `sig`, a shared build's key (and so `world.instances`,
|
|
288
|
+
// keyed the same), and what a skipping pass wrote down (`builtFrom.drops`).
|
|
289
|
+
|
|
290
|
+
// The shared builds whose empty groups have been taken out.
|
|
291
|
+
const withoutEmpty = new WeakSet();
|
|
292
|
+
|
|
293
|
+
// `template`, a shared build made for such a scene, with its empty groups
|
|
294
|
+
// gone: taken out once, when the build is first placed.
|
|
295
|
+
function withoutEmptyParts(template, model) {
|
|
296
|
+
if (!withoutEmpty.has(template)) {
|
|
297
|
+
withoutEmpty.add(template);
|
|
298
|
+
dropEmptyGroups(template, model);
|
|
299
|
+
}
|
|
300
|
+
return template;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// A node's signature, saying which way it was built.
|
|
304
|
+
function builtAs(drops, sig) {
|
|
305
|
+
return drops === true ? `${sig}|dropEmptyParts` : sig;
|
|
306
|
+
}
|
|
307
|
+
|
|
255
308
|
// Removes an actor's node. A shared model build is given back rather than
|
|
256
309
|
// disposed, since other actors may still draw it.
|
|
257
310
|
function releaseActorNode(world, entry) {
|
|
@@ -278,8 +331,9 @@ const DEG = Math.PI / 180;
|
|
|
278
331
|
// `actor.runtime.quat`; everything else rotates by the Transform's euler
|
|
279
332
|
// degrees.
|
|
280
333
|
export function syncActors(world, runtime, sprites, dt = 0) {
|
|
334
|
+
const drops = runtime.dropsEmptyParts === true;
|
|
281
335
|
if (runtime.skipsUnchangedActors === true && world.disposed !== true) {
|
|
282
|
-
syncActorsSkipping(world, runtime, sprites, dt);
|
|
336
|
+
syncActorsSkipping(world, runtime, sprites, dt, drops);
|
|
283
337
|
return;
|
|
284
338
|
}
|
|
285
339
|
if (world.skipping.on) stopSkipping(world);
|
|
@@ -296,7 +350,7 @@ export function syncActors(world, runtime, sprites, dt = 0) {
|
|
|
296
350
|
const t = actor.components.Transform;
|
|
297
351
|
if (!t) continue;
|
|
298
352
|
seen.add(actor.id);
|
|
299
|
-
const entry = ensureActorNode(world, actor, sprites, runtime.files);
|
|
353
|
+
const entry = ensureActorNode(world, actor, sprites, runtime.files, drops);
|
|
300
354
|
const node = entry.node;
|
|
301
355
|
node.position.set(t.x, t.y, t.z);
|
|
302
356
|
const quat = actor.runtime?.quat;
|
|
@@ -560,7 +614,7 @@ function forgetEverything(world) {
|
|
|
560
614
|
for (const parked of world.parked.values()) parked.forEach(forget);
|
|
561
615
|
}
|
|
562
616
|
|
|
563
|
-
function syncActorsSkipping(world, runtime, sprites, dt) {
|
|
617
|
+
function syncActorsSkipping(world, runtime, sprites, dt, drops) {
|
|
564
618
|
// Entered while a pass is running: that pass is no longer alone.
|
|
565
619
|
if (world.syncDepth > 0) leavingTheEngine(world);
|
|
566
620
|
const outermost = world.syncDepth === 0;
|
|
@@ -569,7 +623,7 @@ function syncActorsSkipping(world, runtime, sprites, dt) {
|
|
|
569
623
|
world.syncDepth += 1;
|
|
570
624
|
let finished = false;
|
|
571
625
|
try {
|
|
572
|
-
skippingPass(world, runtime, sprites, dt, outermost ? started : -1);
|
|
626
|
+
skippingPass(world, runtime, sprites, dt, outermost ? started : -1, drops);
|
|
573
627
|
finished = true;
|
|
574
628
|
} finally {
|
|
575
629
|
world.syncDepth -= 1;
|
|
@@ -579,7 +633,7 @@ function syncActorsSkipping(world, runtime, sprites, dt) {
|
|
|
579
633
|
|
|
580
634
|
// One pass. `started` is the generation it may trust its state at; a pass
|
|
581
635
|
// entered inside another is given one that never matches.
|
|
582
|
-
function skippingPass(world, runtime, sprites, dt, started) {
|
|
636
|
+
function skippingPass(world, runtime, sprites, dt, started, drops) {
|
|
583
637
|
beginPass(world, runtime, sprites);
|
|
584
638
|
if (world.generation === started) {
|
|
585
639
|
world.syncCount += 1;
|
|
@@ -590,7 +644,7 @@ function skippingPass(world, runtime, sprites, dt, started) {
|
|
|
590
644
|
const t = actor.components.Transform;
|
|
591
645
|
if (!t) continue;
|
|
592
646
|
seen.add(actor.id);
|
|
593
|
-
const entry = placeActor(world, actor, t, sprites, runtime.files, started);
|
|
647
|
+
const entry = placeActor(world, actor, t, sprites, runtime.files, started, drops);
|
|
594
648
|
animateModelParts(entry, dt);
|
|
595
649
|
animateMotion(entry, dt, actor);
|
|
596
650
|
}
|
|
@@ -632,9 +686,9 @@ function groundIsDressed(world, setting, sprites) {
|
|
|
632
686
|
// One actor's node, found or built, put where its Transform says, and ready
|
|
633
687
|
// to be animated: the skipping way while the pass is alone, `syncActors`'s way
|
|
634
688
|
// once it is not.
|
|
635
|
-
function placeActor(world, actor, t, sprites, files, started) {
|
|
689
|
+
function placeActor(world, actor, t, sprites, files, started, drops) {
|
|
636
690
|
const alone = world.generation === started;
|
|
637
|
-
const entry = alone ? nodeFor(world, actor, sprites, files) : nodeAsMain(world, actor, sprites, files);
|
|
691
|
+
const entry = alone ? nodeFor(world, actor, sprites, files, drops) : nodeAsMain(world, actor, sprites, files, drops);
|
|
638
692
|
const node = entry.node;
|
|
639
693
|
node.position.set(t.x, t.y, t.z);
|
|
640
694
|
const quat = actor.runtime?.quat;
|
|
@@ -727,10 +781,11 @@ function artReadsTheSame(artReads, sprites) {
|
|
|
727
781
|
// Whether the actor's node signature would come out as it did when `entry`
|
|
728
782
|
// last built it: the Shape and Model components read the same field for
|
|
729
783
|
// field, the art the Shape names is the same objects in the same state, and
|
|
730
|
-
// the model file's signature is the same
|
|
731
|
-
|
|
784
|
+
// the model file's signature is the same; and it was written down by a pass
|
|
785
|
+
// that built the way this one does (`drops`).
|
|
786
|
+
function looksTheSame(world, entry, actor, sprites, files, drops) {
|
|
732
787
|
const built = world.snapshots.get(entry)?.builtFrom;
|
|
733
|
-
if (!built) return false;
|
|
788
|
+
if (!built || built.drops !== drops) return false;
|
|
734
789
|
const components = actor.components;
|
|
735
790
|
const model = components.Model;
|
|
736
791
|
return (
|
|
@@ -745,20 +800,21 @@ function looksTheSame(world, entry, actor, sprites, files) {
|
|
|
745
800
|
// same way, but only when `looksTheSame` cannot vouch for the node. A node
|
|
746
801
|
// that has to be built or released ends the pass's being alone first. What a
|
|
747
802
|
// kept node was built from is written down after the signature is compared.
|
|
748
|
-
function nodeFor(world, actor, sprites, files) {
|
|
803
|
+
function nodeFor(world, actor, sprites, files, drops) {
|
|
749
804
|
const entry = world.nodes.get(actor.id);
|
|
750
|
-
if (entry && looksTheSame(world, entry, actor, sprites, files)) return entry;
|
|
805
|
+
if (entry && looksTheSame(world, entry, actor, sprites, files, drops)) return entry;
|
|
751
806
|
const artReads = [];
|
|
752
807
|
const modelSig = modelSigOf(world, actor.components.Model?.file, sprites, files);
|
|
753
|
-
const sig = nodeSignature(actor, recordReads(sprites, artReads), modelSig);
|
|
808
|
+
const sig = builtAs(drops, nodeSignature(actor, recordReads(sprites, artReads), modelSig));
|
|
754
809
|
if (!entry || entry.sig !== sig) {
|
|
755
|
-
return buildNode(world, entry, actor, sig, modelSig, sprites, files);
|
|
810
|
+
return buildNode(world, entry, actor, sig, modelSig, sprites, files, drops);
|
|
756
811
|
}
|
|
757
812
|
snapshotOf(world, entry).builtFrom = {
|
|
758
813
|
shape: remember(actor.components.Shape),
|
|
759
814
|
model: remember(actor.components.Model),
|
|
760
815
|
artReads: withReadiness(artReads),
|
|
761
816
|
modelSig,
|
|
817
|
+
drops,
|
|
762
818
|
};
|
|
763
819
|
return entry;
|
|
764
820
|
}
|
|
@@ -766,12 +822,12 @@ function nodeFor(world, actor, sprites, files) {
|
|
|
766
822
|
// `ensureActorNode` as it is, for a pass that is not alone: the signature is
|
|
767
823
|
// built and compared, nothing is read from what was written down and nothing
|
|
768
824
|
// is written. The node is an ActorNode all the same.
|
|
769
|
-
function nodeAsMain(world, actor, sprites, files) {
|
|
825
|
+
function nodeAsMain(world, actor, sprites, files, drops) {
|
|
770
826
|
const modelSig = actorModelSig(actor, sprites, files);
|
|
771
|
-
const sig = nodeSignature(actor, sprites, modelSig);
|
|
827
|
+
const sig = builtAs(drops, nodeSignature(actor, sprites, modelSig));
|
|
772
828
|
const entry = world.nodes.get(actor.id);
|
|
773
829
|
if (entry && entry.sig === sig) return entry;
|
|
774
|
-
return buildNode(world, entry, actor, sig, modelSig, sprites, files);
|
|
830
|
+
return buildNode(world, entry, actor, sig, modelSig, sprites, files, drops);
|
|
775
831
|
}
|
|
776
832
|
|
|
777
833
|
// What is written down for an entry, made on first use.
|
|
@@ -784,22 +840,22 @@ function snapshotOf(world, entry) {
|
|
|
784
840
|
return snapshot;
|
|
785
841
|
}
|
|
786
842
|
|
|
787
|
-
function buildNode(world, old, actor, sig, modelSig, sprites, files) {
|
|
843
|
+
function buildNode(world, old, actor, sig, modelSig, sprites, files, drops) {
|
|
788
844
|
leavingTheEngine(world);
|
|
789
845
|
if (old) releaseNode(world, old);
|
|
790
846
|
const entry = { node: new ActorNode(world.skipping), sig };
|
|
791
847
|
entry.node.userData.actorId = actor.id;
|
|
792
|
-
fillNode(world, entry, actor, modelSig, sprites, files);
|
|
848
|
+
fillNode(world, entry, actor, modelSig, sprites, files, drops);
|
|
793
849
|
world.scene.add(entry.node);
|
|
794
850
|
world.nodes.set(actor.id, entry);
|
|
795
851
|
return entry;
|
|
796
852
|
}
|
|
797
853
|
|
|
798
|
-
function fillNode(world, entry, actor, modelSig, sprites, files) {
|
|
854
|
+
function fillNode(world, entry, actor, modelSig, sprites, files, drops) {
|
|
799
855
|
const { Model: placed, Shape: shape } = actor.components;
|
|
800
856
|
const model = placed?.file ? parseModelCached(files?.[placed.file]) : null;
|
|
801
857
|
if (model) {
|
|
802
|
-
addModelRig(world, entry, model, placed, `${placed.file}|${modelSig}`, { sprites, files });
|
|
858
|
+
addModelRig(world, entry, model, placed, `${placed.file}|${modelSig}`, { sprites, files, drops });
|
|
803
859
|
} else if (shape) {
|
|
804
860
|
entry.node.add(makeShapeMesh(shape, sprites));
|
|
805
861
|
}
|