@threenative/three-native 0.3.11 → 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 +2 -2
- package/src/addons/ccd-ik-solver.ts +170 -0
- package/src/addons/gltf-loader-web.ts +132 -26
- package/src/addons/screen-space.ts +61 -24
- package/src/animation-utils.ts +366 -0
- package/src/box2.ts +172 -0
- package/src/bridge-meter.ts +33 -12
- package/src/browser-abi-types.ts +621 -0
- package/src/browser-attribute-sync.ts +249 -0
- package/src/browser-backend.ts +266 -1650
- package/src/browser-entry.ts +79 -22
- package/src/browser-loaders.ts +12 -0
- package/src/browser-marshal.ts +79 -0
- package/src/browser-renderer.ts +297 -87
- package/src/browser-tsl.ts +146 -23
- package/src/browser-value-views.ts +634 -0
- package/src/browser-wasm-runtime.ts +525 -0
- package/src/buffer-attributes.ts +19 -0
- package/src/compressed-texture.ts +116 -0
- package/src/debug-telemetry.ts +23 -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 +47 -6
- package/src/pass-node.ts +354 -10
- package/src/render-target.ts +515 -16
- package/src/shadow-node.ts +24 -1
- package/src/storage.ts +16 -5
- package/src/texture-sources.ts +80 -23
- package/src/tsl-extras.ts +50 -0
- package/src/tsl-uniforms.ts +294 -63
- package/src/update-ranges.ts +50 -1
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
// three's AnimationUtils, ported from three r187 src/animation/AnimationUtils.js (MIT), with
|
|
2
|
+
// Quaternion.slerpFlat, Quaternion.multiplyQuaternionsFlat and LinearInterpolant written over plain arrays.
|
|
3
|
+
//
|
|
4
|
+
// The engine's keyframe tracks answer `times` and `values` as copies, and an AnimationClip holds copies
|
|
5
|
+
// of the tracks it is handed. So a write into `track.values[k]` is lost, and three's in-place edits
|
|
6
|
+
// cannot work. subclip and makeClipAdditive here build NEW tracks from adjusted arrays through each
|
|
7
|
+
// track's own constructor and return a NEW clip of them. Use the returned clip: the input is unchanged.
|
|
8
|
+
// The same code runs on three's own classes, where it gives three's values (see its spec).
|
|
9
|
+
|
|
10
|
+
type Values = ArrayLike<number> & { readonly constructor: unknown };
|
|
11
|
+
|
|
12
|
+
// three's property name; Record keeps it out of the naming rule.
|
|
13
|
+
type ITrack = Readonly<Record<"ValueTypeName", string>> & {
|
|
14
|
+
readonly name: string;
|
|
15
|
+
readonly times: Values;
|
|
16
|
+
readonly values: Values;
|
|
17
|
+
readonly constructor: unknown;
|
|
18
|
+
getValueSize?(): number;
|
|
19
|
+
getInterpolation?(): number;
|
|
20
|
+
createInterpolant?: (() => { evaluate(time: number): ArrayLike<number> }) & {
|
|
21
|
+
isInterpolantFactoryMethodGLTFCubicSpline?: boolean;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
interface IClip {
|
|
26
|
+
readonly name: string;
|
|
27
|
+
readonly duration: number;
|
|
28
|
+
readonly tracks: readonly ITrack[];
|
|
29
|
+
readonly blendMode?: number;
|
|
30
|
+
readonly constructor: unknown;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type TrackClass = new (
|
|
34
|
+
name: string,
|
|
35
|
+
times: ArrayLike<number>,
|
|
36
|
+
values: ArrayLike<number>,
|
|
37
|
+
interpolation?: number,
|
|
38
|
+
) => ITrack;
|
|
39
|
+
type ClipClass = new (
|
|
40
|
+
name: string,
|
|
41
|
+
duration: number,
|
|
42
|
+
tracks: ITrack[],
|
|
43
|
+
blendMode?: number,
|
|
44
|
+
) => IClip;
|
|
45
|
+
|
|
46
|
+
const ADDITIVE_ANIMATION_BLEND_MODE = 2501;
|
|
47
|
+
|
|
48
|
+
// The engine track binds no getValueSize; three's is this same ratio.
|
|
49
|
+
const valueSize = (track: ITrack) =>
|
|
50
|
+
track.getValueSize?.() ?? track.values.length / track.times.length;
|
|
51
|
+
|
|
52
|
+
// The V8 engine's constructors refuse an explicit undefined argument, so an absent optional one is left out.
|
|
53
|
+
const defined = (value: number | undefined) => (value === undefined ? [] : [value]);
|
|
54
|
+
|
|
55
|
+
// A copy of `track` with new keys; the interpolation carries over where the track answers it.
|
|
56
|
+
const rebuild = (track: ITrack, times: ArrayLike<number>, values: ArrayLike<number>) =>
|
|
57
|
+
new (track.constructor as TrackClass)(
|
|
58
|
+
track.name,
|
|
59
|
+
times,
|
|
60
|
+
values,
|
|
61
|
+
...defined(track.getInterpolation?.()),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
type ArrayType = Partial<Record<"BYTES_PER_ELEMENT", number>> & (new (array: never) => unknown);
|
|
65
|
+
|
|
66
|
+
export function convertArray(array: unknown, type: ArrayType): unknown {
|
|
67
|
+
if (!array || (array as { constructor?: unknown }).constructor === type) return array;
|
|
68
|
+
if (typeof type.BYTES_PER_ELEMENT === "number") return new type(array as never);
|
|
69
|
+
return Array.prototype.slice.call(array);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function isTypedArray(object: unknown): boolean {
|
|
73
|
+
return ArrayBuffer.isView(object) && !(object instanceof DataView);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function hasTangents(settings?: { inTangents?: unknown; outTangents?: unknown }): boolean {
|
|
77
|
+
return (
|
|
78
|
+
settings !== undefined &&
|
|
79
|
+
settings.inTangents !== undefined &&
|
|
80
|
+
settings.outTangents !== undefined
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function getKeyframeOrder(times: ArrayLike<number>): number[] {
|
|
85
|
+
const result = Array.from({ length: times.length }, (_, i) => i);
|
|
86
|
+
result.sort((i, j) => (times[i] as number) - (times[j] as number));
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function sortedArray<T extends ArrayLike<number> & { constructor: unknown }>(
|
|
91
|
+
values: T,
|
|
92
|
+
stride: number,
|
|
93
|
+
order: readonly number[],
|
|
94
|
+
): T {
|
|
95
|
+
const result = new (values.constructor as new (length: number) => T & number[])(values.length);
|
|
96
|
+
for (let i = 0, dst = 0; dst !== values.length; ++i) {
|
|
97
|
+
const src = (order[i] as number) * stride;
|
|
98
|
+
for (let j = 0; j !== stride; ++j) result[dst++] = values[src + j] as number;
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
type Key = Record<string, unknown> & { time: number };
|
|
104
|
+
|
|
105
|
+
export function flattenJSON(
|
|
106
|
+
jsonKeys: readonly Key[],
|
|
107
|
+
times: number[],
|
|
108
|
+
values: unknown[],
|
|
109
|
+
valuePropertyName: string,
|
|
110
|
+
): void {
|
|
111
|
+
let i = 1;
|
|
112
|
+
let key = jsonKeys[0];
|
|
113
|
+
while (key !== undefined && key[valuePropertyName] === undefined) key = jsonKeys[i++];
|
|
114
|
+
if (key === undefined) return;
|
|
115
|
+
const first = key[valuePropertyName];
|
|
116
|
+
if (first === undefined) return;
|
|
117
|
+
do {
|
|
118
|
+
const value = key[valuePropertyName];
|
|
119
|
+
if (value !== undefined) {
|
|
120
|
+
times.push(key.time);
|
|
121
|
+
if (Array.isArray(first)) values.push(...(value as unknown[]));
|
|
122
|
+
else if ((first as { toArray?: unknown }).toArray !== undefined)
|
|
123
|
+
(value as { toArray(array: unknown[], offset: number): void }).toArray(
|
|
124
|
+
values,
|
|
125
|
+
values.length,
|
|
126
|
+
);
|
|
127
|
+
else values.push(value);
|
|
128
|
+
}
|
|
129
|
+
key = jsonKeys[i++];
|
|
130
|
+
} while (key !== undefined);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function subclip(
|
|
134
|
+
sourceClip: IClip,
|
|
135
|
+
name: string,
|
|
136
|
+
startFrame: number,
|
|
137
|
+
endFrame: number,
|
|
138
|
+
fps = 30,
|
|
139
|
+
): IClip {
|
|
140
|
+
const kept: { track: ITrack; times: number[]; values: number[] }[] = [];
|
|
141
|
+
for (const track of sourceClip.tracks) {
|
|
142
|
+
const size = valueSize(track);
|
|
143
|
+
const times: number[] = [];
|
|
144
|
+
const values: number[] = [];
|
|
145
|
+
const sourceTimes = track.times;
|
|
146
|
+
const sourceValues = track.values;
|
|
147
|
+
for (let j = 0; j < sourceTimes.length; ++j) {
|
|
148
|
+
const frame = (sourceTimes[j] as number) * fps;
|
|
149
|
+
if (frame < startFrame || frame >= endFrame) continue;
|
|
150
|
+
times.push(sourceTimes[j] as number);
|
|
151
|
+
for (let k = 0; k < size; ++k) values.push(sourceValues[j * size + k] as number);
|
|
152
|
+
}
|
|
153
|
+
if (times.length > 0) kept.push({ track, times, values });
|
|
154
|
+
}
|
|
155
|
+
// Shift every track so the clip begins at t = 0, then take the duration from the last key.
|
|
156
|
+
const type = (track: ITrack, field: "times" | "values") => track[field].constructor as ArrayType;
|
|
157
|
+
const shifted = kept.map(
|
|
158
|
+
({ track, times }) => convertArray(times, type(track, "times")) as number[],
|
|
159
|
+
);
|
|
160
|
+
const start = Math.min(...shifted.map((times) => times[0] as number));
|
|
161
|
+
let duration = 0;
|
|
162
|
+
const tracks = kept.map(({ track, values }, i) => {
|
|
163
|
+
const times = shifted[i] as number[];
|
|
164
|
+
for (let j = 0; j < times.length; j++) times[j] = (times[j] as number) - start;
|
|
165
|
+
duration = Math.max(duration, times[times.length - 1] as number);
|
|
166
|
+
return rebuild(track, times, convertArray(values, type(track, "values")) as number[]);
|
|
167
|
+
});
|
|
168
|
+
return new (sourceClip.constructor as ClipClass)(
|
|
169
|
+
name,
|
|
170
|
+
duration,
|
|
171
|
+
tracks,
|
|
172
|
+
...defined(sourceClip.blendMode),
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// three's Quaternion.slerpFlat over plain arrays.
|
|
177
|
+
function slerpFlat(a: ArrayLike<number>, b: ArrayLike<number>, t: number): number[] {
|
|
178
|
+
let [x0, y0, z0, w0] = [a[0] as number, a[1] as number, a[2] as number, a[3] as number];
|
|
179
|
+
let [x1, y1, z1, w1] = [b[0] as number, b[1] as number, b[2] as number, b[3] as number];
|
|
180
|
+
if (w0 !== w1 || x0 !== x1 || y0 !== y1 || z0 !== z1) {
|
|
181
|
+
let dot = x0 * x1 + y0 * y1 + z0 * z1 + w0 * w1;
|
|
182
|
+
if (dot < 0) {
|
|
183
|
+
[x1, y1, z1, w1, dot] = [-x1, -y1, -z1, -w1, -dot];
|
|
184
|
+
}
|
|
185
|
+
let s = 1 - t;
|
|
186
|
+
let u = t;
|
|
187
|
+
if (dot < 0.9995) {
|
|
188
|
+
const theta = Math.acos(dot);
|
|
189
|
+
const sin = Math.sin(theta);
|
|
190
|
+
s = Math.sin(s * theta) / sin;
|
|
191
|
+
u = Math.sin(u * theta) / sin;
|
|
192
|
+
}
|
|
193
|
+
[x0, y0, z0, w0] = [x0 * s + x1 * u, y0 * s + y1 * u, z0 * s + z1 * u, w0 * s + w1 * u];
|
|
194
|
+
if (dot >= 0.9995) {
|
|
195
|
+
const f = 1 / Math.sqrt(x0 * x0 + y0 * y0 + z0 * z0 + w0 * w0);
|
|
196
|
+
[x0, y0, z0, w0] = [x0 * f, y0 * f, z0 * f, w0 * f];
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return [x0, y0, z0, w0];
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// The track's value at `time`, strictly between its first and last keys. three's interpolant when the
|
|
203
|
+
// track has one; else three's linear interpolant (slerp for a quaternion).
|
|
204
|
+
// ponytail: the engine track binds no createInterpolant or getInterpolation, so a discrete or smooth
|
|
205
|
+
// engine track samples linearly here; bind getInterpolation when an additive layer needs those.
|
|
206
|
+
function sample(track: ITrack, time: number, size: number): number[] {
|
|
207
|
+
if (track.createInterpolant) return Array.from(track.createInterpolant().evaluate(time));
|
|
208
|
+
const { times, values } = track;
|
|
209
|
+
let i = 1;
|
|
210
|
+
while ((times[i] as number) < time) i++;
|
|
211
|
+
const t0 = times[i - 1] as number;
|
|
212
|
+
const weight = (time - t0) / ((times[i] as number) - t0);
|
|
213
|
+
const v0 = Array.from({ length: size }, (_, k) => values[(i - 1) * size + k] as number);
|
|
214
|
+
const v1 = Array.from({ length: size }, (_, k) => values[i * size + k] as number);
|
|
215
|
+
if (track.ValueTypeName === "quaternion") return slerpFlat(v0, v1, weight);
|
|
216
|
+
return v0.map((value, k) => value * (1 - weight) + (v1[k] as number) * weight);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// The glTF cubic-spline layout stores in-tangent, value, out-tangent per key.
|
|
220
|
+
const splineOffset = (track: ITrack, size: number) =>
|
|
221
|
+
track.createInterpolant?.isInterpolantFactoryMethodGLTFCubicSpline ? size / 3 : 0;
|
|
222
|
+
|
|
223
|
+
// The reference track's value at `time`; a quaternion comes back normalized and conjugated.
|
|
224
|
+
function referenceValue(track: ITrack, time: number): number[] {
|
|
225
|
+
const size = valueSize(track);
|
|
226
|
+
const offset = splineOffset(track, size);
|
|
227
|
+
const { times } = track;
|
|
228
|
+
const last = times.length - 1;
|
|
229
|
+
let value: number[];
|
|
230
|
+
// Before the first key or after the last one, the reference value is that key.
|
|
231
|
+
if (time <= (times[0] as number)) value = Array.from(track.values).slice(offset, size - offset);
|
|
232
|
+
else if (time >= (times[last] as number)) {
|
|
233
|
+
const start = last * size + offset;
|
|
234
|
+
value = Array.from(track.values).slice(start, start + size - offset);
|
|
235
|
+
} else value = sample(track, time, size);
|
|
236
|
+
if (track.ValueTypeName !== "quaternion") return value;
|
|
237
|
+
// Quaternion.normalize().conjugate().
|
|
238
|
+
const [x, y, z, w] = value as [number, number, number, number];
|
|
239
|
+
const length = Math.sqrt(x * x + y * y + z * z + w * w);
|
|
240
|
+
if (length === 0) return [-0, -0, -0, 1];
|
|
241
|
+
return [-(x / length), -(y / length), -(z / length), w / length];
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Subtracts `reference` from every key of `values`; a quaternion key becomes reference * key, where
|
|
245
|
+
// the reference is already conjugated.
|
|
246
|
+
function subtract(track: ITrack, values: number[], reference: number[]): void {
|
|
247
|
+
const size = valueSize(track);
|
|
248
|
+
const offset = splineOffset(track, size);
|
|
249
|
+
const [x0, y0, z0, w0] = reference as [number, number, number, number];
|
|
250
|
+
for (let j = 0; j < track.times.length; ++j) {
|
|
251
|
+
const at = j * size + offset;
|
|
252
|
+
if (track.ValueTypeName === "quaternion") {
|
|
253
|
+
// Quaternion.multiplyQuaternionsFlat(values, at, reference, 0, values, at).
|
|
254
|
+
const [x1, y1, z1, w1] = values.slice(at, at + 4) as [number, number, number, number];
|
|
255
|
+
values[at] = x0 * w1 + w0 * x1 + y0 * z1 - z0 * y1;
|
|
256
|
+
values[at + 1] = y0 * w1 + w0 * y1 + z0 * x1 - x0 * z1;
|
|
257
|
+
values[at + 2] = z0 * w1 + w0 * z1 + x0 * y1 - y0 * x1;
|
|
258
|
+
values[at + 3] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1;
|
|
259
|
+
} else
|
|
260
|
+
for (let k = 0; k < size - offset * 2; ++k)
|
|
261
|
+
values[at + k] = (values[at + k] as number) - (reference[k] as number);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function makeClipAdditive(
|
|
266
|
+
targetClip: IClip,
|
|
267
|
+
referenceFrame = 0,
|
|
268
|
+
referenceClip: IClip = targetClip,
|
|
269
|
+
fps = 30,
|
|
270
|
+
): IClip {
|
|
271
|
+
const referenceTime = referenceFrame / (fps <= 0 ? 30 : fps);
|
|
272
|
+
const targetTracks = targetClip.tracks;
|
|
273
|
+
// Each changed target track's working values, rebuilt into a new track at the end.
|
|
274
|
+
const edited = new Map<ITrack, number[]>();
|
|
275
|
+
for (const referenceTrack of referenceClip.tracks) {
|
|
276
|
+
const type = referenceTrack.ValueTypeName;
|
|
277
|
+
// Skip non-numeric tracks.
|
|
278
|
+
if (type === "bool" || type === "string") continue;
|
|
279
|
+
const targetTrack = targetTracks.find(
|
|
280
|
+
(track) => track.name === referenceTrack.name && track.ValueTypeName === type,
|
|
281
|
+
);
|
|
282
|
+
if (targetTrack === undefined) continue;
|
|
283
|
+
const values = edited.get(targetTrack) ?? Array.from(targetTrack.values);
|
|
284
|
+
subtract(targetTrack, values, referenceValue(referenceTrack, referenceTime));
|
|
285
|
+
edited.set(targetTrack, values);
|
|
286
|
+
}
|
|
287
|
+
const tracks = targetTracks.map((track) => {
|
|
288
|
+
const values = edited.get(track);
|
|
289
|
+
if (values === undefined) return track;
|
|
290
|
+
return rebuild(
|
|
291
|
+
track,
|
|
292
|
+
track.times,
|
|
293
|
+
convertArray(values, track.values.constructor as ArrayType) as number[],
|
|
294
|
+
);
|
|
295
|
+
});
|
|
296
|
+
return new (targetClip.constructor as ClipClass)(
|
|
297
|
+
targetClip.name,
|
|
298
|
+
targetClip.duration,
|
|
299
|
+
tracks,
|
|
300
|
+
ADDITIVE_ANIMATION_BLEND_MODE,
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const INTERPOLATE_DISCRETE = 2300;
|
|
305
|
+
/** three's property name, spelled through a constant so the naming rule does not read it as a game field. */
|
|
306
|
+
const VALUE_TYPE_NAME = "ValueTypeName";
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* three's `track.createInterpolant(result)` on the engine's track classes, which do not bind it (core's root
|
|
310
|
+
* motion samples with it). The interpolant reads a snapshot of the keys, holds the first and last key outside
|
|
311
|
+
* them as three's Interpolant does, and writes each value into `result`.
|
|
312
|
+
* ponytail: a smooth track evaluates linearly; port CubicInterpolant when a game samples one.
|
|
313
|
+
*/
|
|
314
|
+
export function defineCreateInterpolant(classes: Readonly<Record<string, unknown>>): void {
|
|
315
|
+
for (const name of ["NumberKeyframeTrack", "VectorKeyframeTrack", "QuaternionKeyframeTrack"]) {
|
|
316
|
+
const prototype = (classes[name] as { prototype?: object } | undefined)?.prototype;
|
|
317
|
+
if (prototype === undefined || "createInterpolant" in prototype) continue;
|
|
318
|
+
Object.defineProperty(prototype, "createInterpolant", {
|
|
319
|
+
configurable: true,
|
|
320
|
+
writable: true,
|
|
321
|
+
value(this: ITrack, result?: Float32Array) {
|
|
322
|
+
const size = valueSize(this);
|
|
323
|
+
const times = Float32Array.from(this.times);
|
|
324
|
+
const keys = Object.assign(
|
|
325
|
+
{ [VALUE_TYPE_NAME]: this.ValueTypeName },
|
|
326
|
+
{
|
|
327
|
+
name: this.name,
|
|
328
|
+
times,
|
|
329
|
+
values: Float32Array.from(this.values),
|
|
330
|
+
constructor: undefined,
|
|
331
|
+
},
|
|
332
|
+
) as ITrack;
|
|
333
|
+
const discrete = this.getInterpolation?.() === INTERPOLATE_DISCRETE;
|
|
334
|
+
const resultBuffer = result ?? new Float32Array(size);
|
|
335
|
+
const last = times.length - 1;
|
|
336
|
+
const keyValue = (key: number) =>
|
|
337
|
+
(keys.values as Float32Array).subarray(key * size, key * size + size);
|
|
338
|
+
return {
|
|
339
|
+
resultBuffer,
|
|
340
|
+
evaluate(time: number) {
|
|
341
|
+
if (time <= (times[0] as number)) resultBuffer.set(keyValue(0));
|
|
342
|
+
else if (time >= (times[last] as number)) resultBuffer.set(keyValue(last));
|
|
343
|
+
else if (discrete) {
|
|
344
|
+
let i = 1;
|
|
345
|
+
while ((times[i] as number) <= time) i++;
|
|
346
|
+
resultBuffer.set(keyValue(i - 1));
|
|
347
|
+
} else resultBuffer.set(sample(keys, time, size));
|
|
348
|
+
return resultBuffer;
|
|
349
|
+
},
|
|
350
|
+
};
|
|
351
|
+
},
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** three's static `AnimationUtils` namespace. */
|
|
357
|
+
export const AnimationUtils = {
|
|
358
|
+
convertArray,
|
|
359
|
+
isTypedArray,
|
|
360
|
+
hasTangents,
|
|
361
|
+
getKeyframeOrder,
|
|
362
|
+
sortedArray,
|
|
363
|
+
flattenJSON,
|
|
364
|
+
subclip,
|
|
365
|
+
makeClipAdditive,
|
|
366
|
+
} as const;
|
package/src/box2.ts
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// three's Box2 over the engine's Vector2, ported from three r187 src/math/Box2.js (MIT).
|
|
2
|
+
// Box2 is pure math over two Vector2s, so it is plain JS here: no engine class of its own.
|
|
3
|
+
|
|
4
|
+
interface IVector2 {
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
set(x: number, y: number): IVector2;
|
|
8
|
+
copy(v: IVector2): IVector2;
|
|
9
|
+
add(v: IVector2): IVector2;
|
|
10
|
+
sub(v: IVector2): IVector2;
|
|
11
|
+
addScalar(s: number): IVector2;
|
|
12
|
+
addVectors(a: IVector2, b: IVector2): IVector2;
|
|
13
|
+
subVectors(a: IVector2, b: IVector2): IVector2;
|
|
14
|
+
multiplyScalar(s: number): IVector2;
|
|
15
|
+
min(v: IVector2): IVector2;
|
|
16
|
+
max(v: IVector2): IVector2;
|
|
17
|
+
clamp(min: IVector2, max: IVector2): IVector2;
|
|
18
|
+
distanceTo(v: IVector2): number;
|
|
19
|
+
equals(v: IVector2): boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function defineBox2(
|
|
23
|
+
classes: Readonly<Record<"Vector2", new (x?: number, y?: number) => object>>,
|
|
24
|
+
) {
|
|
25
|
+
const Vector2 = classes.Vector2 as new (x?: number, y?: number) => IVector2;
|
|
26
|
+
const scratch = new Vector2();
|
|
27
|
+
return class Box2 {
|
|
28
|
+
readonly isBox2 = true;
|
|
29
|
+
min: IVector2;
|
|
30
|
+
max: IVector2;
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
min: IVector2 = new Vector2(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY),
|
|
34
|
+
max: IVector2 = new Vector2(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY),
|
|
35
|
+
) {
|
|
36
|
+
this.min = min;
|
|
37
|
+
this.max = max;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
set(min: IVector2, max: IVector2): this {
|
|
41
|
+
this.min.copy(min);
|
|
42
|
+
this.max.copy(max);
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
setFromPoints(points: readonly IVector2[]): this {
|
|
47
|
+
this.makeEmpty();
|
|
48
|
+
for (const point of points) this.expandByPoint(point);
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
setFromCenterAndSize(center: IVector2, size: IVector2): this {
|
|
53
|
+
const halfSize = scratch.copy(size).multiplyScalar(0.5);
|
|
54
|
+
this.min.copy(center).sub(halfSize);
|
|
55
|
+
this.max.copy(center).add(halfSize);
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
clone(): Box2 {
|
|
60
|
+
return new Box2().copy(this);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
copy(box: Box2): this {
|
|
64
|
+
this.min.copy(box.min);
|
|
65
|
+
this.max.copy(box.max);
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
makeEmpty(): this {
|
|
70
|
+
this.min.x = this.min.y = Number.POSITIVE_INFINITY;
|
|
71
|
+
this.max.x = this.max.y = Number.NEGATIVE_INFINITY;
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
isEmpty(): boolean {
|
|
76
|
+
return this.max.x < this.min.x || this.max.y < this.min.y;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
getCenter(target: IVector2): IVector2 {
|
|
80
|
+
return this.isEmpty()
|
|
81
|
+
? target.set(0, 0)
|
|
82
|
+
: target.addVectors(this.min, this.max).multiplyScalar(0.5);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
getSize(target: IVector2): IVector2 {
|
|
86
|
+
return this.isEmpty() ? target.set(0, 0) : target.subVectors(this.max, this.min);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
expandByPoint(point: IVector2): this {
|
|
90
|
+
this.min.min(point);
|
|
91
|
+
this.max.max(point);
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
expandByVector(vector: IVector2): this {
|
|
96
|
+
this.min.sub(vector);
|
|
97
|
+
this.max.add(vector);
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
expandByScalar(scalar: number): this {
|
|
102
|
+
this.min.addScalar(-scalar);
|
|
103
|
+
this.max.addScalar(scalar);
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
containsPoint(point: IVector2): boolean {
|
|
108
|
+
return (
|
|
109
|
+
point.x >= this.min.x &&
|
|
110
|
+
point.x <= this.max.x &&
|
|
111
|
+
point.y >= this.min.y &&
|
|
112
|
+
point.y <= this.max.y
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
containsBox(box: Box2): boolean {
|
|
117
|
+
return (
|
|
118
|
+
this.min.x <= box.min.x &&
|
|
119
|
+
box.max.x <= this.max.x &&
|
|
120
|
+
this.min.y <= box.min.y &&
|
|
121
|
+
box.max.y <= this.max.y
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
getParameter(point: IVector2, target: IVector2): IVector2 {
|
|
126
|
+
return target.set(
|
|
127
|
+
(point.x - this.min.x) / (this.max.x - this.min.x),
|
|
128
|
+
(point.y - this.min.y) / (this.max.y - this.min.y),
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
intersectsBox(box: Box2): boolean {
|
|
133
|
+
return (
|
|
134
|
+
box.max.x >= this.min.x &&
|
|
135
|
+
box.min.x <= this.max.x &&
|
|
136
|
+
box.max.y >= this.min.y &&
|
|
137
|
+
box.min.y <= this.max.y
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
clampPoint(point: IVector2, target: IVector2): IVector2 {
|
|
142
|
+
return target.copy(point).clamp(this.min, this.max);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
distanceToPoint(point: IVector2): number {
|
|
146
|
+
return this.clampPoint(point, scratch).distanceTo(point);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
intersect(box: Box2): this {
|
|
150
|
+
this.min.max(box.min);
|
|
151
|
+
this.max.min(box.max);
|
|
152
|
+
if (this.isEmpty()) this.makeEmpty();
|
|
153
|
+
return this;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
union(box: Box2): this {
|
|
157
|
+
this.min.min(box.min);
|
|
158
|
+
this.max.max(box.max);
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
translate(offset: IVector2): this {
|
|
163
|
+
this.min.add(offset);
|
|
164
|
+
this.max.add(offset);
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
equals(box: Box2): boolean {
|
|
169
|
+
return box.min.equals(this.min) && box.max.equals(this.max);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
}
|
package/src/bridge-meter.ts
CHANGED
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
import { debugRequested } from "./debug-telemetry.js";
|
|
25
|
+
import { CONSOLE_MARKER } from "./generated/console-markers.js";
|
|
25
26
|
|
|
26
27
|
export const BRIDGE_WARNING_MARKER = "TN_BRIDGE_WARNING";
|
|
27
28
|
/**
|
|
@@ -34,7 +35,12 @@ export const BRIDGE_WARNING_MARKER = "TN_BRIDGE_WARNING";
|
|
|
34
35
|
* crossed. The counters themselves ride TN_FRAME_BUDGET.counters.engine.
|
|
35
36
|
*/
|
|
36
37
|
export const ENGINE_COST_KNOB = "__THREENATIVE_ENGINE_COST__";
|
|
37
|
-
export const BRIDGE_CENSUS_MARKER =
|
|
38
|
+
export const BRIDGE_CENSUS_MARKER = CONSOLE_MARKER.bridgeCensus;
|
|
39
|
+
/**
|
|
40
|
+
* How many crossing classes one census line names. Ten is what a measurement sweep reads: the top five
|
|
41
|
+
* of a scene usually share one caller, and a lever that moves the sixth is invisible at five (PRD-828).
|
|
42
|
+
*/
|
|
43
|
+
export const CENSUS_TOP = 10;
|
|
38
44
|
|
|
39
45
|
export const PLAYTEST_OBSERVING_GLOBAL = "__tnPlaytestObserving";
|
|
40
46
|
|
|
@@ -73,10 +79,19 @@ export interface IBridgeMeter {
|
|
|
73
79
|
frame(): void;
|
|
74
80
|
}
|
|
75
81
|
|
|
82
|
+
/**
|
|
83
|
+
* The files a caller search looks through rather than names: the back end's own (`browser-backend.ts`
|
|
84
|
+
* and the modules it was split into, PRD-803) and this meter's two.
|
|
85
|
+
*/
|
|
86
|
+
const BACK_END_FILE =
|
|
87
|
+
/(?:browser-backend|browser-abi-types|browser-marshal|browser-wasm-runtime|browser-value-views|browser-attribute-sync|debug-telemetry|bridge-meter)\.[jt]s(?:[:?]|$)/u;
|
|
88
|
+
|
|
76
89
|
/**
|
|
77
90
|
* The first frame of `stack` outside the back end and this meter (the files of its first two frames:
|
|
78
91
|
* the stack call, then `record`), and the two after it: `fn (file:line) < fn (file:line)`. A bundle
|
|
79
92
|
* that puts the game in the back end's file falls back to the fourth frame, the one above the accessor.
|
|
93
|
+
* Every file the back end was split into (PRD-803) belongs here: a crossing it makes for a game's call
|
|
94
|
+
* is attributed to the game, never to the back end.
|
|
80
95
|
*/
|
|
81
96
|
export function callerOf(stack: string | undefined): string {
|
|
82
97
|
const frames = (stack ?? "")
|
|
@@ -87,9 +102,7 @@ export function callerOf(stack: string | undefined): string {
|
|
|
87
102
|
const fileOf = (frame: string) => /\(?([^()\s]+?)(?::\d+){1,2}\)?$/u.exec(frame)?.[1] ?? frame;
|
|
88
103
|
const own = new Set(frames.slice(0, 2).map(fileOf));
|
|
89
104
|
let at = frames.findIndex(
|
|
90
|
-
(frame) =>
|
|
91
|
-
!own.has(fileOf(frame)) &&
|
|
92
|
-
!/(?:browser-backend|debug-telemetry|bridge-meter)\.[jt]s(?:[:?]|$)/u.test(fileOf(frame)),
|
|
105
|
+
(frame) => !own.has(fileOf(frame)) && !BACK_END_FILE.test(fileOf(frame)),
|
|
93
106
|
);
|
|
94
107
|
if (at < 0) at = Math.min(3, frames.length - 1);
|
|
95
108
|
const short = (frame: string) =>
|
|
@@ -105,6 +118,21 @@ export function callerOf(stack: string | undefined): string {
|
|
|
105
118
|
);
|
|
106
119
|
}
|
|
107
120
|
|
|
121
|
+
/**
|
|
122
|
+
* The busiest crossing classes of one census frame, highest first. The first tie keeps the order the
|
|
123
|
+
* crossings were first seen in, so two runs of the same frame print the same list.
|
|
124
|
+
*/
|
|
125
|
+
export function topCrossings(
|
|
126
|
+
byName: ReadonlyMap<string, number>,
|
|
127
|
+
callers: ReadonlyMap<string, string>,
|
|
128
|
+
limit: number,
|
|
129
|
+
): { name: string; perFrame: number; caller: string }[] {
|
|
130
|
+
return [...byName]
|
|
131
|
+
.sort((a, b) => b[1] - a[1])
|
|
132
|
+
.slice(0, limit)
|
|
133
|
+
.map(([name, perFrame]) => ({ name, perFrame, caller: callers.get(name) ?? "unknown" }));
|
|
134
|
+
}
|
|
135
|
+
|
|
108
136
|
export function createBridgeMeter(host: IBridgeMeterHost): IBridgeMeter {
|
|
109
137
|
let frames = 0;
|
|
110
138
|
let last: number | undefined;
|
|
@@ -132,14 +160,7 @@ export function createBridgeMeter(host: IBridgeMeterHost): IBridgeMeter {
|
|
|
132
160
|
if (done.printed) {
|
|
133
161
|
let crossed = 0;
|
|
134
162
|
for (const count of done.byName.values()) crossed += count;
|
|
135
|
-
const top =
|
|
136
|
-
.sort((a, b) => b[1] - a[1])
|
|
137
|
-
.slice(0, 5)
|
|
138
|
-
.map(([name, perFrame]) => ({
|
|
139
|
-
name,
|
|
140
|
-
perFrame,
|
|
141
|
-
caller: done.callers.get(name) ?? "unknown",
|
|
142
|
-
}));
|
|
163
|
+
const top = topCrossings(done.byName, done.callers, CENSUS_TOP);
|
|
143
164
|
// The debug path's own observation: what every crossing of the frame cost, at this host's
|
|
144
165
|
// calibration. It is not a warning — a game with no repeated read has nothing to fix — so it
|
|
145
166
|
// rides the census a debug run or a playtest bound already asks for.
|