@threenative/three-native 0.3.12 → 0.3.13
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/package.json +1 -1
- package/src/addons/ccd-ik-solver.ts +170 -0
- package/src/addons/gltf-loader-web.ts +104 -17
- package/src/addons/screen-space.ts +20 -19
- package/src/animation-utils.ts +366 -0
- package/src/box2.ts +172 -0
- package/src/bridge-meter.ts +23 -9
- package/src/browser-abi-types.ts +232 -38
- package/src/browser-attribute-sync.ts +69 -0
- package/src/browser-backend.ts +71 -5
- package/src/browser-entry.ts +51 -12
- package/src/browser-loaders.ts +12 -0
- package/src/browser-renderer.ts +179 -112
- package/src/browser-tsl.ts +93 -16
- package/src/browser-value-views.ts +16 -0
- package/src/browser-wasm-runtime.ts +38 -13
- package/src/buffer-attributes.ts +19 -0
- package/src/compressed-texture.ts +116 -0
- package/src/debug-telemetry.ts +20 -6
- package/src/edges-geometry.ts +111 -0
- package/src/generated/console-markers.ts +13 -0
- package/src/generated/web-frame-fields.ts +84 -0
- package/src/gpu-stages.ts +17 -16
- package/src/grid-helper.ts +49 -0
- package/src/object-surface.ts +30 -6
- package/src/pass-node.ts +26 -6
- package/src/render-target.ts +478 -12
- package/src/shadow-node.ts +24 -1
- package/src/storage.ts +16 -5
- package/src/texture-sources.ts +12 -6
- package/src/tsl-uniforms.ts +146 -57
- package/src/update-ranges.ts +27 -1
package/src/browser-abi-types.ts
CHANGED
|
@@ -14,6 +14,18 @@ export interface IPackedRef extends IEngineRef {
|
|
|
14
14
|
w2: number;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* The ref's handle words. A ref the runtime made carries them; any other ref is parsed once and keeps them, so
|
|
19
|
+
* writing a handle is three stores and never a key parse per call.
|
|
20
|
+
*/
|
|
21
|
+
export function packedRef(ref: IEngineRef): IPackedRef {
|
|
22
|
+
const known = ref as Partial<IPackedRef>;
|
|
23
|
+
if (known.w0 !== undefined) return known as IPackedRef;
|
|
24
|
+
const [type = 0, context = 0, index = 0, generation = 0] = ref.key.split(":").map(Number);
|
|
25
|
+
const words = { w0: (type | (context << 16)) >>> 0, w1: index, w2: generation };
|
|
26
|
+
return Object.isExtensible(ref) ? Object.assign(ref, words) : { ...ref, ...words };
|
|
27
|
+
}
|
|
28
|
+
|
|
17
29
|
/** An engine object as the back end sees it: an opaque key and its catalog type id. */
|
|
18
30
|
export interface IEngineRef {
|
|
19
31
|
readonly key: string;
|
|
@@ -35,6 +47,21 @@ export type EngineValue =
|
|
|
35
47
|
| IEngineRef
|
|
36
48
|
| { readonly [key: string]: EngineValue };
|
|
37
49
|
|
|
50
|
+
/** PRD-828: one rig of an animation tick batch, as the backend takes it. */
|
|
51
|
+
export interface IAnimationTickBatch {
|
|
52
|
+
/** One entry per rig: the mixer, the rig's action (null when nothing plays), the body it strides from. */
|
|
53
|
+
readonly mixers: readonly IEngineRef[];
|
|
54
|
+
readonly actions: readonly (IEngineRef | null)[];
|
|
55
|
+
readonly roots: readonly (IEngineRef | null)[];
|
|
56
|
+
/**
|
|
57
|
+
* Five per rig: the stride interval, this tick's own time, the clip's ground speed, the rate this rule
|
|
58
|
+
* last applied (0 for none), and the flags word.
|
|
59
|
+
*/
|
|
60
|
+
readonly values: Float64Array;
|
|
61
|
+
/** Three per rig, written by the call: stride rate, measured ground speed, flags. */
|
|
62
|
+
readonly status: Float64Array;
|
|
63
|
+
}
|
|
64
|
+
|
|
38
65
|
/** What the classes call; the Wasm ABI implements it, and a surface-only runtime refuses every call. */
|
|
39
66
|
export interface IBrowserRuntime {
|
|
40
67
|
readonly debugTelemetry?: DebugTelemetry;
|
|
@@ -46,6 +73,11 @@ export interface IBrowserRuntime {
|
|
|
46
73
|
release(self: IEngineRef): void;
|
|
47
74
|
/** For each ref, how many references other engine objects hold to its object; one call a batch. */
|
|
48
75
|
engineReferences?(refs: readonly IEngineRef[]): ArrayLike<number>;
|
|
76
|
+
/**
|
|
77
|
+
* PRD-828: advances a batch of animation rigs in one crossing — the weight ramp, the stride rate and
|
|
78
|
+
* one mixer step each. A backend that cannot batch leaves it out and the JS loop keeps running.
|
|
79
|
+
*/
|
|
80
|
+
animTick?(batch: IAnimationTickBatch): void;
|
|
49
81
|
/** A numeric typed array argument crosses as is, no boxed copy; Wasm only. */
|
|
50
82
|
readonly typedArrays?: boolean;
|
|
51
83
|
/** Doubles at `address` in the engine's memory (one as a number, more as an array); Wasm only. */
|
|
@@ -85,7 +117,22 @@ export interface IBrowserRuntime {
|
|
|
85
117
|
loadGltf?(
|
|
86
118
|
bytes: Uint8Array,
|
|
87
119
|
files?: ReadonlyMap<string, Uint8Array>,
|
|
88
|
-
): {
|
|
120
|
+
): {
|
|
121
|
+
readonly scene: IEngineRef;
|
|
122
|
+
readonly animations: IEngineRef[];
|
|
123
|
+
/** Every scene in file order; the default scene is one of them. */
|
|
124
|
+
readonly scenes: IEngineRef[];
|
|
125
|
+
};
|
|
126
|
+
/** `loadGltf` on the engine's loader thread (PRD-860), when the module carries it. */
|
|
127
|
+
loadGltfAsync?(
|
|
128
|
+
bytes: Uint8Array,
|
|
129
|
+
files?: ReadonlyMap<string, Uint8Array>,
|
|
130
|
+
): Promise<{
|
|
131
|
+
readonly scene: IEngineRef;
|
|
132
|
+
readonly animations: IEngineRef[];
|
|
133
|
+
/** Every scene in file order; the default scene is one of them. */
|
|
134
|
+
readonly scenes: IEngineRef[];
|
|
135
|
+
}>;
|
|
89
136
|
}
|
|
90
137
|
|
|
91
138
|
/**
|
|
@@ -105,7 +152,8 @@ type AbiCall =
|
|
|
105
152
|
| "_tn_get"
|
|
106
153
|
| "_tn_set"
|
|
107
154
|
| "_tn_set_callback"
|
|
108
|
-
| "_tn_diagnostic_release"
|
|
155
|
+
| "_tn_diagnostic_release"
|
|
156
|
+
| "_tn_anim_tick";
|
|
109
157
|
export type TnAbiModule = Record<AbiCall, (...args: number[]) => number> &
|
|
110
158
|
Partial<
|
|
111
159
|
Record<"_tnw_attribute_view" | "_tnw_attribute_view_release", (...args: number[]) => number>
|
|
@@ -157,6 +205,8 @@ type TslCall =
|
|
|
157
205
|
| "_tn_tsl_release"
|
|
158
206
|
| "_tn_tsl_set"
|
|
159
207
|
| "_tn_tsl_set_uniform"
|
|
208
|
+
| "_tn_tsl_set_uniforms"
|
|
209
|
+
| "_tn_tsl_reads"
|
|
160
210
|
| "_tn_tsl_effect_parameter"
|
|
161
211
|
| "_tnw_web_set_post"
|
|
162
212
|
| "_tnw_web_compute"
|
|
@@ -179,16 +229,29 @@ export interface IAbiHelpers {
|
|
|
179
229
|
view(): DataView;
|
|
180
230
|
}
|
|
181
231
|
|
|
232
|
+
/** `tnw_web_load_gltf`'s handles: the default scene, the clips, then every scene in file order. */
|
|
233
|
+
function splitModel(refs: IEngineRef[], scenes: number) {
|
|
234
|
+
const clipsEnd = refs.length - scenes;
|
|
235
|
+
return {
|
|
236
|
+
scene: refs[0] as IEngineRef,
|
|
237
|
+
animations: refs.slice(1, clipsEnd),
|
|
238
|
+
scenes: refs.slice(clipsEnd),
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
182
242
|
/** The engine glTF loader over the product web host's `tnw_web_load_gltf`, when the module has it. */
|
|
183
243
|
export function gltfOf(
|
|
184
244
|
abi: TnAbiModule,
|
|
185
245
|
context: number,
|
|
186
246
|
h: Pick<IAbiHelpers, "scoped" | "alloc"> & { keyOf(pointer: number): IEngineRef },
|
|
187
|
-
): Pick<IBrowserRuntime, "loadGltf"> {
|
|
247
|
+
): Pick<IBrowserRuntime, "loadGltf" | "loadGltfAsync"> {
|
|
188
248
|
// quality-allow: the Emscripten module is read through the exports this call site names, not its full surface.
|
|
189
249
|
const host = abi as unknown as Partial<
|
|
190
250
|
Record<
|
|
191
251
|
| "_tnw_web_load_gltf"
|
|
252
|
+
| "_tnw_web_load_gltf_begin"
|
|
253
|
+
| "_tnw_web_load_gltf_done"
|
|
254
|
+
| "_tnw_web_load_gltf_finish"
|
|
192
255
|
| "_tnw_web_load_error"
|
|
193
256
|
| "_tnw_web_gltf_file"
|
|
194
257
|
| "_tnw_web_gltf_files_clear",
|
|
@@ -197,56 +260,99 @@ export function gltfOf(
|
|
|
197
260
|
>;
|
|
198
261
|
const {
|
|
199
262
|
_tnw_web_load_gltf: load,
|
|
263
|
+
_tnw_web_load_gltf_begin: begin,
|
|
264
|
+
_tnw_web_load_gltf_done: done,
|
|
265
|
+
_tnw_web_load_gltf_finish: finish,
|
|
200
266
|
_tnw_web_load_error: error,
|
|
201
267
|
_tnw_web_gltf_file: file,
|
|
202
268
|
_tnw_web_gltf_files_clear: clearFiles,
|
|
203
269
|
} = host;
|
|
204
270
|
if (load === undefined || error === undefined) return {};
|
|
205
|
-
|
|
206
|
-
|
|
271
|
+
/** Files the model names by relative uri (shared images), kept for the engine loader's next load. */
|
|
272
|
+
const stageFiles = (files: ReadonlyMap<string, Uint8Array> | undefined): void => {
|
|
273
|
+
if (files === undefined || files.size === 0) return;
|
|
274
|
+
if (file === undefined || clearFiles === undefined)
|
|
275
|
+
throw new Error(
|
|
276
|
+
"TN_WASM_GLTF_FILES_UNAVAILABLE: this web engine cannot read a model's image files",
|
|
277
|
+
);
|
|
278
|
+
for (const [uri, content] of files) {
|
|
279
|
+
// tnw_web_gltf_file keeps its own copy, so this scratch is dead once it returns. One scope
|
|
280
|
+
// per file frees it at once: the peak is one file, not the sum of all of them.
|
|
207
281
|
h.scoped(() => {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
282
|
+
const name = h.alloc(abi.lengthBytesUTF8(uri) + 1);
|
|
283
|
+
abi.stringToUTF8(uri, name, abi.lengthBytesUTF8(uri) + 1);
|
|
284
|
+
const copy = h.alloc(Math.max(1, content.byteLength));
|
|
285
|
+
abi.HEAPU8.set(content, copy);
|
|
286
|
+
file(name, copy, content.byteLength);
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
/**
|
|
291
|
+
* The handles `share(out, capacity, count, scenes)` writes, retried once at the exact size for a
|
|
292
|
+
* model with many clips.
|
|
293
|
+
*/
|
|
294
|
+
const handles = (
|
|
295
|
+
share: (out: number, capacity: number, count: number, scenes: number) => number,
|
|
296
|
+
) =>
|
|
297
|
+
h.scoped(() => {
|
|
298
|
+
const count = h.alloc(4);
|
|
299
|
+
const sceneCount = h.alloc(4);
|
|
300
|
+
for (let capacity = 64; ; ) {
|
|
301
|
+
const out = h.alloc(HANDLE * capacity);
|
|
302
|
+
const status = share(out, capacity, count, sceneCount);
|
|
303
|
+
const needed = new DataView(abi.HEAPU8.buffer).getUint32(count, true);
|
|
304
|
+
if (status === 0)
|
|
305
|
+
return splitModel(
|
|
306
|
+
Array.from({ length: needed }, (_, i) => h.keyOf(out + i * HANDLE)),
|
|
307
|
+
new DataView(abi.HEAPU8.buffer).getUint32(sceneCount, true),
|
|
308
|
+
);
|
|
309
|
+
const reason = abi.UTF8ToString(error());
|
|
310
|
+
if (reason !== "TN_WASM_GLTF_CAPACITY" || needed <= capacity) throw new Error(reason);
|
|
311
|
+
capacity = needed;
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
const loadGltf: NonNullable<IBrowserRuntime["loadGltf"]> = (bytes, files) =>
|
|
315
|
+
h.scoped(() => {
|
|
316
|
+
try {
|
|
317
|
+
stageFiles(files);
|
|
318
|
+
const data = h.alloc(Math.max(1, bytes.byteLength));
|
|
319
|
+
abi.HEAPU8.set(bytes, data);
|
|
320
|
+
return handles((out, capacity, count, scenes) =>
|
|
321
|
+
load(context, data, bytes.byteLength, out, capacity, count, scenes),
|
|
322
|
+
);
|
|
323
|
+
} finally {
|
|
324
|
+
clearFiles?.();
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
if (begin === undefined || done === undefined || finish === undefined) return { loadGltf };
|
|
328
|
+
return {
|
|
329
|
+
loadGltf,
|
|
330
|
+
// PRD-860: the engine parses on its loader thread, so a model that arrives during play costs the
|
|
331
|
+
// main thread a poll and the hand-over, not the 100-500 ms parse Midway's late fleet froze on.
|
|
332
|
+
loadGltfAsync: async (bytes, files) => {
|
|
333
|
+
const ticket = h.scoped(() => {
|
|
226
334
|
try {
|
|
335
|
+
stageFiles(files);
|
|
227
336
|
const data = h.alloc(Math.max(1, bytes.byteLength));
|
|
228
337
|
abi.HEAPU8.set(bytes, data);
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
for (let capacity = 64; ; ) {
|
|
232
|
-
const out = h.alloc(HANDLE * capacity);
|
|
233
|
-
const status = load(context, data, bytes.byteLength, out, capacity, count);
|
|
234
|
-
const needed = new DataView(abi.HEAPU8.buffer).getUint32(count, true);
|
|
235
|
-
if (status === 0) {
|
|
236
|
-
const refs = Array.from({ length: needed }, (_, i) => h.keyOf(out + i * HANDLE));
|
|
237
|
-
return { scene: refs[0] as IEngineRef, animations: refs.slice(1) };
|
|
238
|
-
}
|
|
239
|
-
const reason = abi.UTF8ToString(error());
|
|
240
|
-
if (reason !== "TN_WASM_GLTF_CAPACITY" || needed <= capacity) throw new Error(reason);
|
|
241
|
-
capacity = needed;
|
|
242
|
-
}
|
|
338
|
+
// begin copies the bytes and takes the staged files with it.
|
|
339
|
+
return begin(data, bytes.byteLength);
|
|
243
340
|
} finally {
|
|
244
341
|
clearFiles?.();
|
|
245
342
|
}
|
|
246
|
-
})
|
|
343
|
+
});
|
|
344
|
+
if (ticket === 0) throw new Error("TN_WASM_GLTF: the engine refused the load");
|
|
345
|
+
while (done(ticket) === 0) await new Promise((resolve) => setTimeout(resolve, GLTF_POLL_MS));
|
|
346
|
+
return handles((out, capacity, count, scenes) =>
|
|
347
|
+
finish(context, ticket, out, capacity, count, scenes),
|
|
348
|
+
);
|
|
349
|
+
},
|
|
247
350
|
};
|
|
248
351
|
}
|
|
249
352
|
|
|
353
|
+
/** How often a pending engine glTF load is polled: a 4 ms timer costs nothing beside a 100 ms parse. */
|
|
354
|
+
const GLTF_POLL_MS = 4;
|
|
355
|
+
|
|
250
356
|
/** TSL by name over `tn_tsl_call`, when the module exports it (the product web host does). */
|
|
251
357
|
export function tslOf(
|
|
252
358
|
abi: TnAbiModule,
|
|
@@ -263,6 +369,8 @@ export function tslOf(
|
|
|
263
369
|
_tn_tsl_release: release,
|
|
264
370
|
_tn_tsl_set: set,
|
|
265
371
|
_tn_tsl_set_uniform: setUniform,
|
|
372
|
+
_tn_tsl_set_uniforms: setUniforms,
|
|
373
|
+
_tn_tsl_reads: reads,
|
|
266
374
|
_tn_tsl_effect_parameter: effectParameter,
|
|
267
375
|
_tnw_web_set_post: setPost,
|
|
268
376
|
_tnw_web_compute: computeNode,
|
|
@@ -282,6 +390,11 @@ export function tslOf(
|
|
|
282
390
|
effectParameter === undefined
|
|
283
391
|
)
|
|
284
392
|
return undefined;
|
|
393
|
+
// PRD-878: the hooked uniform ids `reads` checks, kept in engine memory: the facade only appends to its list, so a
|
|
394
|
+
// query writes just the ids added since the last one.
|
|
395
|
+
let candidateAt = 0;
|
|
396
|
+
let candidateRoom = 0;
|
|
397
|
+
let candidatesWritten = 0;
|
|
285
398
|
// Writes tn_tsl_arg_t values; text is allocated first, since an allocation can grow the memory.
|
|
286
399
|
const writeArgs = (args: readonly TslArgValue[]): number => {
|
|
287
400
|
const pointer = h.alloc(Math.max(TSL_ARG, args.length * TSL_ARG));
|
|
@@ -422,6 +535,87 @@ export function tslOf(
|
|
|
422
535
|
const diag = h.diagnostic();
|
|
423
536
|
h.check(setUniform(context, pointer, values, lanes.length, diag), diag, "uniform.value");
|
|
424
537
|
}),
|
|
538
|
+
...(reads === undefined
|
|
539
|
+
? {}
|
|
540
|
+
: {
|
|
541
|
+
reads: (
|
|
542
|
+
root: IEngineRef | undefined,
|
|
543
|
+
kernels: readonly number[],
|
|
544
|
+
candidates: readonly number[],
|
|
545
|
+
): number[] => {
|
|
546
|
+
if (candidates.length > candidateRoom) {
|
|
547
|
+
if (candidateAt !== 0) abi._free(candidateAt);
|
|
548
|
+
candidateRoom = Math.max(16, candidates.length * 2);
|
|
549
|
+
// The ids, then room for as many u32 answers.
|
|
550
|
+
candidateAt = abi._malloc(candidateRoom * 12) >>> 0;
|
|
551
|
+
candidatesWritten = 0;
|
|
552
|
+
}
|
|
553
|
+
const ids = h.view();
|
|
554
|
+
for (; candidatesWritten < candidates.length; candidatesWritten += 1)
|
|
555
|
+
ids.setBigUint64(
|
|
556
|
+
candidateAt + candidatesWritten * 8,
|
|
557
|
+
BigInt(candidates[candidatesWritten] as number),
|
|
558
|
+
true,
|
|
559
|
+
);
|
|
560
|
+
return h.scoped(() => {
|
|
561
|
+
const rootAt = root === undefined ? 0 : h.alloc(HANDLE);
|
|
562
|
+
if (root !== undefined) h.writeHandle(rootAt, root);
|
|
563
|
+
const kernelAt = h.alloc(Math.max(8, kernels.length * 8));
|
|
564
|
+
const countAt = h.alloc(4);
|
|
565
|
+
const v = h.view();
|
|
566
|
+
kernels.forEach((kernel, i) =>
|
|
567
|
+
v.setBigUint64(kernelAt + i * 8, BigInt(kernel), true),
|
|
568
|
+
);
|
|
569
|
+
const out = candidateAt + candidateRoom * 8;
|
|
570
|
+
const diag = h.diagnostic();
|
|
571
|
+
h.check(
|
|
572
|
+
reads(
|
|
573
|
+
context,
|
|
574
|
+
rootAt,
|
|
575
|
+
kernelAt,
|
|
576
|
+
kernels.length,
|
|
577
|
+
candidateAt,
|
|
578
|
+
candidates.length,
|
|
579
|
+
out,
|
|
580
|
+
countAt,
|
|
581
|
+
diag,
|
|
582
|
+
),
|
|
583
|
+
diag,
|
|
584
|
+
"uniform reads",
|
|
585
|
+
);
|
|
586
|
+
const after = h.view();
|
|
587
|
+
const picked: number[] = [];
|
|
588
|
+
for (let i = 0, count = after.getUint32(countAt, true); i < count; i += 1)
|
|
589
|
+
picked.push(after.getUint32(out + i * 4, true));
|
|
590
|
+
return picked;
|
|
591
|
+
});
|
|
592
|
+
},
|
|
593
|
+
}),
|
|
594
|
+
// PRD-828: a sync's changed uniforms as one `tn_tsl_set_uniforms` span; absent on an older module.
|
|
595
|
+
...(setUniforms === undefined
|
|
596
|
+
? {}
|
|
597
|
+
: {
|
|
598
|
+
setUniforms: (
|
|
599
|
+
nodes: readonly number[],
|
|
600
|
+
counts: readonly number[],
|
|
601
|
+
lanes: readonly number[],
|
|
602
|
+
) =>
|
|
603
|
+
h.scoped(() => {
|
|
604
|
+
const ids = h.alloc(Math.max(8, nodes.length * 8));
|
|
605
|
+
const widths = h.alloc(Math.max(4, counts.length * 4));
|
|
606
|
+
const values = h.alloc(Math.max(8, lanes.length * 8));
|
|
607
|
+
const v = h.view();
|
|
608
|
+
nodes.forEach((node, i) => v.setBigUint64(ids + i * 8, BigInt(node), true));
|
|
609
|
+
counts.forEach((count, i) => v.setUint32(widths + i * 4, count, true));
|
|
610
|
+
lanes.forEach((lane, i) => v.setFloat64(values + i * 8, lane, true));
|
|
611
|
+
const diag = h.diagnostic();
|
|
612
|
+
h.check(
|
|
613
|
+
setUniforms(context, ids, widths, values, nodes.length, diag),
|
|
614
|
+
diag,
|
|
615
|
+
"uniform.value",
|
|
616
|
+
);
|
|
617
|
+
}),
|
|
618
|
+
}),
|
|
425
619
|
},
|
|
426
620
|
};
|
|
427
621
|
}
|
|
@@ -42,6 +42,8 @@ export interface IAttributeSync {
|
|
|
42
42
|
/** Shape survives data uploads and transform calls; only copy/replacement changes it. */
|
|
43
43
|
readonly shapes: WeakMap<object, Map<string, unknown>>;
|
|
44
44
|
readonly pending: Set<object>;
|
|
45
|
+
/** Item ranges a mesh-level write put in an attribute's array, sent with `pending` (PRD-860). */
|
|
46
|
+
readonly dirty: Map<object, { from: number; to: number }>;
|
|
45
47
|
writeBack(): void;
|
|
46
48
|
itemSizeOf(attribute: object): number;
|
|
47
49
|
rangeOf(method: string, args: readonly unknown[], itemSize: number): readonly [number, number];
|
|
@@ -53,6 +55,12 @@ export interface IAttributeSync {
|
|
|
53
55
|
index: unknown,
|
|
54
56
|
argument: unknown,
|
|
55
57
|
): { array: TypedArray; at: number; values: ArrayLike<number> } | undefined;
|
|
58
|
+
writeItemAt(
|
|
59
|
+
mesh: object,
|
|
60
|
+
name: "instanceMatrix" | "instanceColor",
|
|
61
|
+
index: unknown,
|
|
62
|
+
value: unknown,
|
|
63
|
+
): boolean;
|
|
56
64
|
}
|
|
57
65
|
|
|
58
66
|
export function defineAttributeSync(host: IAttributeSyncHost): IAttributeSync {
|
|
@@ -70,7 +78,14 @@ export function defineAttributeSync(host: IAttributeSyncHost): IAttributeSync {
|
|
|
70
78
|
// Shape survives data uploads and transform calls; only copy/replacement changes it.
|
|
71
79
|
const shapes = new WeakMap<object, Map<string, unknown>>();
|
|
72
80
|
const pending = new Set<object>();
|
|
81
|
+
const dirty = new Map<object, { from: number; to: number }>();
|
|
73
82
|
const writeBack = (): void => {
|
|
83
|
+
for (const [attribute, { from, to }] of dirty) {
|
|
84
|
+
const entry = arrays.get(attribute);
|
|
85
|
+
if (entry !== undefined && !pending.has(attribute))
|
|
86
|
+
attributeWrite?.call(runtime, refOf(attribute), entry.array.subarray(from, to), from);
|
|
87
|
+
}
|
|
88
|
+
dirty.clear();
|
|
74
89
|
for (const attribute of pending) {
|
|
75
90
|
const entry = arrays.get(attribute);
|
|
76
91
|
if (entry !== undefined) attributeWrite?.call(runtime, refOf(attribute), entry.array);
|
|
@@ -163,6 +178,58 @@ export function defineAttributeSync(host: IAttributeSyncHost): IAttributeSync {
|
|
|
163
178
|
return undefined;
|
|
164
179
|
return { array: entry.array, at, values };
|
|
165
180
|
};
|
|
181
|
+
// three's `setMatrixAt` and `setColorAt` write the item into the attribute's array, and so does
|
|
182
|
+
// this: one engine call an instance made a meadow of a few thousand tufts cost 60 ms of a laptop
|
|
183
|
+
// load (PRD-860). The written range goes to the engine once, before the next engine call, which is
|
|
184
|
+
// the first thing that can read it. Anything three would not write (a bad index, no value) and the
|
|
185
|
+
// first colour (the engine creates `instanceColor` then) take the engine's call.
|
|
186
|
+
const writeItemAt = (
|
|
187
|
+
mesh: object,
|
|
188
|
+
name: "instanceMatrix" | "instanceColor",
|
|
189
|
+
index: unknown,
|
|
190
|
+
value: unknown,
|
|
191
|
+
): boolean => {
|
|
192
|
+
const source = value as {
|
|
193
|
+
elements?: ArrayLike<number>;
|
|
194
|
+
r?: unknown;
|
|
195
|
+
g?: unknown;
|
|
196
|
+
b?: unknown;
|
|
197
|
+
} | null;
|
|
198
|
+
const size = name === "instanceMatrix" ? 16 : 3;
|
|
199
|
+
const values =
|
|
200
|
+
name === "instanceMatrix"
|
|
201
|
+
? source?.elements
|
|
202
|
+
: typeof source?.r === "number" &&
|
|
203
|
+
typeof source.g === "number" &&
|
|
204
|
+
typeof source.b === "number"
|
|
205
|
+
? [source.r, source.g, source.b]
|
|
206
|
+
: undefined;
|
|
207
|
+
const attribute = (mesh as Record<string, unknown>)[name];
|
|
208
|
+
const at = Number(index) * size;
|
|
209
|
+
if (
|
|
210
|
+
attributeArray === undefined ||
|
|
211
|
+
values?.length !== size ||
|
|
212
|
+
typeof attribute !== "object" ||
|
|
213
|
+
attribute === null ||
|
|
214
|
+
!Number.isSafeInteger(at) ||
|
|
215
|
+
at < 0
|
|
216
|
+
)
|
|
217
|
+
return false;
|
|
218
|
+
let entry = arrays.get(attribute);
|
|
219
|
+
if (entry === undefined) {
|
|
220
|
+
entry = { array: attributeArray.call(runtime, refOf(attribute)) };
|
|
221
|
+
arrays.set(attribute, entry);
|
|
222
|
+
}
|
|
223
|
+
if (at + size > entry.array.length) return false;
|
|
224
|
+
entry.array.set(values, at);
|
|
225
|
+
const range = dirty.get(attribute);
|
|
226
|
+
if (range === undefined) dirty.set(attribute, { from: at, to: at + size });
|
|
227
|
+
else {
|
|
228
|
+
range.from = Math.min(range.from, at);
|
|
229
|
+
range.to = Math.max(range.to, at + size);
|
|
230
|
+
}
|
|
231
|
+
return true;
|
|
232
|
+
};
|
|
166
233
|
return {
|
|
167
234
|
attributeArray,
|
|
168
235
|
attributeWrite,
|
|
@@ -170,11 +237,13 @@ export function defineAttributeSync(host: IAttributeSyncHost): IAttributeSync {
|
|
|
170
237
|
itemSizes,
|
|
171
238
|
shapes,
|
|
172
239
|
pending,
|
|
240
|
+
dirty,
|
|
173
241
|
writeBack,
|
|
174
242
|
itemSizeOf,
|
|
175
243
|
rangeOf,
|
|
176
244
|
syncRange,
|
|
177
245
|
syncFull,
|
|
178
246
|
instanceItem,
|
|
247
|
+
writeItemAt,
|
|
179
248
|
};
|
|
180
249
|
}
|
package/src/browser-backend.ts
CHANGED
|
@@ -264,6 +264,11 @@ function defineTraversal(
|
|
|
264
264
|
* Defines every registry class over `runtime`. With `catalog`, classes chain as three's do
|
|
265
265
|
* (`mesh instanceof Object3D`) and carry three's `is*` flags.
|
|
266
266
|
*/
|
|
267
|
+
/** A catalog field that is one of three's `is<Class>` type flags, which the back end sets to true. */
|
|
268
|
+
export const isTypeFlagField = (field: { name: string; type: string; mutable?: boolean }) =>
|
|
269
|
+
/^is[A-Z]/u.test(field.name) &&
|
|
270
|
+
(field.type === "true" || (field.type === "boolean" && field.mutable === false));
|
|
271
|
+
|
|
267
272
|
export function defineBrowserClasses(
|
|
268
273
|
registry: IRegistryDump,
|
|
269
274
|
source: IBrowserRuntime,
|
|
@@ -683,12 +688,14 @@ export function defineBrowserClasses(
|
|
|
683
688
|
itemSizes,
|
|
684
689
|
shapes,
|
|
685
690
|
pending,
|
|
691
|
+
dirty,
|
|
686
692
|
writeBack,
|
|
687
693
|
itemSizeOf,
|
|
688
694
|
rangeOf,
|
|
689
695
|
syncRange,
|
|
690
696
|
syncFull,
|
|
691
697
|
instanceItem,
|
|
698
|
+
writeItemAt,
|
|
692
699
|
} = defineAttributeSync({ runtime, refOf });
|
|
693
700
|
|
|
694
701
|
const fromEngine = (value: EngineValue): unknown => {
|
|
@@ -898,6 +905,12 @@ export function defineBrowserClasses(
|
|
|
898
905
|
writable: true,
|
|
899
906
|
value(this: object, ...args: unknown[]) {
|
|
900
907
|
if (glueCounting) methodCalls += 1;
|
|
908
|
+
// An instance's matrix or colour is an array write, as in three, sent before the next engine call.
|
|
909
|
+
if (instanceWrite !== undefined && writeItemAt(this, instanceWrite, args[0], args[1])) {
|
|
910
|
+
labels.delete(this);
|
|
911
|
+
animated.delete(this);
|
|
912
|
+
return this;
|
|
913
|
+
}
|
|
901
914
|
// three's toArray(array, offset) writes the lanes into the array it is given and answers that array; the
|
|
902
915
|
// engine answers a new one, since a plain array crosses by value.
|
|
903
916
|
if (method === "toArray" && args[0] !== undefined) {
|
|
@@ -919,7 +932,7 @@ export function defineBrowserClasses(
|
|
|
919
932
|
} else if (schedule === "all") scheduling = new WeakMap();
|
|
920
933
|
// A per-vertex setter writes its own range and reads no kept array, so a whole-shadow flush
|
|
921
934
|
// would cost O(N) a call; it syncs just its range back below. Anything else flushes edits.
|
|
922
|
-
if (pending.size > 0 && !rangeMethod && !bookkeeping) writeBack();
|
|
935
|
+
if ((pending.size > 0 || dirty.size > 0) && !rangeMethod && !bookkeeping) writeBack();
|
|
923
936
|
// A full-array attribute transform reads its own array: send the game's shadow first.
|
|
924
937
|
if (transformMethod) {
|
|
925
938
|
const entry = arrays.get(this);
|
|
@@ -1715,15 +1728,13 @@ export function defineBrowserClasses(
|
|
|
1715
1728
|
);
|
|
1716
1729
|
}
|
|
1717
1730
|
pending.delete(this);
|
|
1731
|
+
dirty.delete(this);
|
|
1718
1732
|
}
|
|
1719
1733
|
needsUpdate.set?.call(this, value);
|
|
1720
1734
|
},
|
|
1721
1735
|
});
|
|
1722
1736
|
}
|
|
1723
1737
|
}
|
|
1724
|
-
const isFlag = (field: { name: string; type: string; mutable?: boolean }) =>
|
|
1725
|
-
/^is[A-Z]/u.test(field.name) &&
|
|
1726
|
-
(field.type === "true" || (field.type === "boolean" && field.mutable === false));
|
|
1727
1738
|
for (const [name, cls] of Object.entries(classes)) {
|
|
1728
1739
|
const entry = entries.get(name);
|
|
1729
1740
|
const parent = entry?.extends ? classes[entry.extends] : undefined;
|
|
@@ -1735,13 +1746,68 @@ export function defineBrowserClasses(
|
|
|
1735
1746
|
link = link.extends ? entries.get(link.extends) : undefined
|
|
1736
1747
|
)
|
|
1737
1748
|
// Writable: three's own value classes assign `this.isColor = true` in their constructors.
|
|
1738
|
-
for (const field of (link.fields ?? []).filter(
|
|
1749
|
+
for (const field of (link.fields ?? []).filter(isTypeFlagField))
|
|
1739
1750
|
Object.defineProperty(cls.prototype, field.name, {
|
|
1740
1751
|
configurable: true,
|
|
1741
1752
|
writable: true,
|
|
1742
1753
|
value: true,
|
|
1743
1754
|
});
|
|
1744
1755
|
}
|
|
1756
|
+
// PRD-828: the engine advances a whole frame's animation rigs in one crossing. Core finds it by this
|
|
1757
|
+
// name and keeps its own per-rig loop when a backend installs nothing, so this cannot change a game
|
|
1758
|
+
// that runs on a backend without it.
|
|
1759
|
+
if (source.animTick !== undefined) {
|
|
1760
|
+
(globalThis as { __tnAnimationTick?: unknown }).__tnAnimationTick = (
|
|
1761
|
+
rigs: readonly {
|
|
1762
|
+
readonly mixer: object;
|
|
1763
|
+
readonly action: object | null;
|
|
1764
|
+
readonly strideRoot: object | null;
|
|
1765
|
+
readonly dt: number;
|
|
1766
|
+
readonly elapsed: number;
|
|
1767
|
+
readonly step: boolean;
|
|
1768
|
+
readonly reset: boolean;
|
|
1769
|
+
readonly lastRate: number;
|
|
1770
|
+
readonly clipGroundSpeed: number;
|
|
1771
|
+
readonly strideSync: boolean;
|
|
1772
|
+
}[],
|
|
1773
|
+
status: Float64Array,
|
|
1774
|
+
): void => {
|
|
1775
|
+
const count = rigs.length;
|
|
1776
|
+
if (count === 0) return;
|
|
1777
|
+
const mixers: ReturnType<typeof refOf>[] = [];
|
|
1778
|
+
const actions: (ReturnType<typeof refOf> | null)[] = [];
|
|
1779
|
+
const roots: (ReturnType<typeof refOf> | null)[] = [];
|
|
1780
|
+
const values = new Float64Array(count * 5);
|
|
1781
|
+
for (let i = 0; i < count; i += 1) {
|
|
1782
|
+
const rig = rigs[i];
|
|
1783
|
+
if (rig === undefined) continue;
|
|
1784
|
+
mixers.push(refOf(rig.mixer));
|
|
1785
|
+
actions.push(rig.action === null ? null : refOf(rig.action));
|
|
1786
|
+
roots.push(rig.strideRoot === null ? null : refOf(rig.strideRoot));
|
|
1787
|
+
values[i * 5] = rig.dt;
|
|
1788
|
+
values[i * 5 + 1] = rig.elapsed;
|
|
1789
|
+
values[i * 5 + 2] = rig.clipGroundSpeed;
|
|
1790
|
+
values[i * 5 + 3] = rig.lastRate;
|
|
1791
|
+
// bit 0: the stride convention applies; bit 1: the action is real; bit 2: the body is real;
|
|
1792
|
+
// bit 3: this tick poses the rig; bit 4: the player spent ticks this batch did not.
|
|
1793
|
+
values[i * 5 + 4] =
|
|
1794
|
+
(rig.strideSync ? 1 : 0) |
|
|
1795
|
+
(rig.action === null ? 0 : 2) |
|
|
1796
|
+
(rig.strideRoot === null ? 0 : 4) |
|
|
1797
|
+
(rig.step ? 8 : 0) |
|
|
1798
|
+
(rig.reset ? 16 : 0);
|
|
1799
|
+
}
|
|
1800
|
+
crossings += 1;
|
|
1801
|
+
const first = mixers[0];
|
|
1802
|
+
if (meter.counting && first !== undefined) meter.record("call", first, "tn_anim_tick");
|
|
1803
|
+
source.animTick?.({ mixers, actions, roots, values, status });
|
|
1804
|
+
// Nothing to invalidate: every field this call writes — the mixer's `time`, the action's `time`,
|
|
1805
|
+
// `timeScale` and `weight` — is an engine-memory field on this back end (`fastGetter` over
|
|
1806
|
+
// `readDoubles` in the registry's `fields`), not a JS mirror, so a read after it is the engine's
|
|
1807
|
+
// answer. `bridge-mirror.spec.ts` pins exactly that.
|
|
1808
|
+
};
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1745
1811
|
return {
|
|
1746
1812
|
classes,
|
|
1747
1813
|
wrap,
|