@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,634 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* three's math values on the browser back end (PRD-532, PRD-573, PRD-803): `new Vector3()` is JS and
|
|
3
|
+
* holds its own lanes, an engine value (`mesh.position`) reads and writes them in the engine's
|
|
4
|
+
* memory, and a value that crosses into an engine call is borrowed or promoted. Each engine keeps
|
|
5
|
+
* its own pools and prototypes, so the back end builds them per engine.
|
|
6
|
+
*/
|
|
7
|
+
import type { EngineValue, IBrowserRuntime, IEngineRef } from "./browser-abi-types.js";
|
|
8
|
+
import type { IRegistryClass, IRegistryDump, IWrapped } from "./browser-backend.js";
|
|
9
|
+
import { REF, SCALARS, TYPED, type TypedArray, isPlainObject } from "./browser-marshal.js";
|
|
10
|
+
import { UNWATCH } from "./tsl-uniforms.js";
|
|
11
|
+
|
|
12
|
+
// three's math values are JS (browser-math.ts): `new Vector3()` holds its own lanes. The engine's
|
|
13
|
+
// vectors (`mesh.position`) share that prototype through `engine`, whose lanes are its memory.
|
|
14
|
+
// An engine call takes a JS value by value: a method borrows a pooled engine object and copies
|
|
15
|
+
// the lanes back after the call (an output argument); a constructor or setter, which may keep the
|
|
16
|
+
// object, gets the value itself turned into an engine object in place, so identity survives.
|
|
17
|
+
//
|
|
18
|
+
// A class may store its lanes under three's private names (`Quaternion._x`, `Matrix4.elements`) or
|
|
19
|
+
// hold them in member vectors (`Box3.min`): its plan reads and writes those, and says which own
|
|
20
|
+
// properties a promoted value must drop so the engine accessors take over.
|
|
21
|
+
/** A value class's JS storage: its public lanes, three's private names, and member vectors. */
|
|
22
|
+
export interface IValueLanes {
|
|
23
|
+
[key: string]: unknown;
|
|
24
|
+
_x?: number;
|
|
25
|
+
_y?: number;
|
|
26
|
+
_z?: number;
|
|
27
|
+
_w?: number;
|
|
28
|
+
_order?: string;
|
|
29
|
+
elements?: number[];
|
|
30
|
+
radius?: number;
|
|
31
|
+
min?: { x: number; y: number; z: number; set(x: number, y: number, z: number): unknown };
|
|
32
|
+
max?: { x: number; y: number; z: number; set(x: number, y: number, z: number): unknown };
|
|
33
|
+
center?: { x: number; y: number; z: number; set(x: number, y: number, z: number): unknown };
|
|
34
|
+
}
|
|
35
|
+
export interface IValuePlan {
|
|
36
|
+
/** The engine's lane names, in the order its constructor takes them. */
|
|
37
|
+
lanes: readonly string[];
|
|
38
|
+
/** Own JS properties the class keeps its storage in; a promoted value drops them. */
|
|
39
|
+
drop: readonly string[];
|
|
40
|
+
/** The lanes of a JS value, in `lanes` order (Euler appends its order). */
|
|
41
|
+
read(value: IValueLanes): number[];
|
|
42
|
+
/** Writes engine lanes back into a JS value, as `lend` copies them back. */
|
|
43
|
+
write(value: IValueLanes, values: readonly number[]): void;
|
|
44
|
+
/** Engine constructor arguments; the lanes themselves by default. */
|
|
45
|
+
args?(values: number[], make: (name: string, values: number[]) => IEngineRef): EngineValue[];
|
|
46
|
+
/** A method argument becomes an engine object in place: whole-object lanes and output targets. */
|
|
47
|
+
promote?: boolean;
|
|
48
|
+
/** Copies the lanes from one value to another with no list between: a loan's two copies. */
|
|
49
|
+
copy?(target: IValueLanes, source: IValueLanes): void;
|
|
50
|
+
}
|
|
51
|
+
export interface IValueClass {
|
|
52
|
+
name: string;
|
|
53
|
+
engine: object;
|
|
54
|
+
pool: object[];
|
|
55
|
+
top: number;
|
|
56
|
+
plan: IValuePlan;
|
|
57
|
+
}
|
|
58
|
+
const eulerOrders = ["XYZ", "YXZ", "ZXY", "ZYX", "YZX", "XZY"] as const;
|
|
59
|
+
export const defaultPlan = (lanes: readonly string[]): IValuePlan => ({
|
|
60
|
+
lanes,
|
|
61
|
+
drop: lanes,
|
|
62
|
+
copy: (target, source) => {
|
|
63
|
+
for (const lane of lanes) target[lane] = source[lane];
|
|
64
|
+
},
|
|
65
|
+
read: (value) => lanes.map((lane) => value[lane] as number),
|
|
66
|
+
write: (value, values) => {
|
|
67
|
+
lanes.forEach((lane, index) => {
|
|
68
|
+
value[lane] = values[index] as number;
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
/** A value's member vector (`Box3.min`, `Sphere.center`): present on every real instance. */
|
|
73
|
+
const memberVector = (vector: IValueLanes["min"]): NonNullable<IValueLanes["min"]> => {
|
|
74
|
+
if (vector === undefined) throw new TypeError("TN_BROWSER_VALUE_MEMBER: the value has no vector");
|
|
75
|
+
return vector;
|
|
76
|
+
};
|
|
77
|
+
// three's private storage names, and the classes whose lanes are not plain fields.
|
|
78
|
+
export const valuePlans: Record<string, IValuePlan> = {
|
|
79
|
+
// quality-allow: the plan table is keyed by three's PascalCase class names, so the name rule cannot apply.
|
|
80
|
+
// biome-ignore lint/style/useNamingConvention: keyed by three's class name.
|
|
81
|
+
Color: defaultPlan(["r", "g", "b"]),
|
|
82
|
+
// quality-allow: the plan table is keyed by three's PascalCase class names, so the name rule cannot apply.
|
|
83
|
+
// biome-ignore lint/style/useNamingConvention: keyed by three's class name.
|
|
84
|
+
Vector4: defaultPlan(["x", "y", "z", "w"]),
|
|
85
|
+
// quality-allow: the plan table is keyed by three's PascalCase class names, so the name rule cannot apply.
|
|
86
|
+
// biome-ignore lint/style/useNamingConvention: keyed by three's class name.
|
|
87
|
+
Quaternion: {
|
|
88
|
+
lanes: ["x", "y", "z", "w"],
|
|
89
|
+
drop: ["_x", "_y", "_z", "_w"],
|
|
90
|
+
read: (value) => [value._x, value._y, value._z, value._w] as number[],
|
|
91
|
+
write: (value, values) => {
|
|
92
|
+
value._x = values[0];
|
|
93
|
+
value._y = values[1];
|
|
94
|
+
value._z = values[2];
|
|
95
|
+
value._w = values[3];
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
// quality-allow: the plan table is keyed by three's PascalCase class names, so the name rule cannot apply.
|
|
99
|
+
// biome-ignore lint/style/useNamingConvention: keyed by three's class name.
|
|
100
|
+
Euler: {
|
|
101
|
+
lanes: ["x", "y", "z"],
|
|
102
|
+
drop: ["_x", "_y", "_z", "_order"],
|
|
103
|
+
read: (value) =>
|
|
104
|
+
[
|
|
105
|
+
value._x,
|
|
106
|
+
value._y,
|
|
107
|
+
value._z,
|
|
108
|
+
eulerOrders.indexOf(value._order as (typeof eulerOrders)[number]),
|
|
109
|
+
] as number[],
|
|
110
|
+
write: (value, values) => {
|
|
111
|
+
value._x = values[0];
|
|
112
|
+
value._y = values[1];
|
|
113
|
+
value._z = values[2];
|
|
114
|
+
value._order = eulerOrders[values[3] as number] ?? "XYZ";
|
|
115
|
+
},
|
|
116
|
+
args: (values) => [values[0], values[1], values[2], eulerOrders[values[3] as number] ?? "XYZ"],
|
|
117
|
+
},
|
|
118
|
+
// quality-allow: the plan table is keyed by three's PascalCase class names, so the name rule cannot apply.
|
|
119
|
+
// biome-ignore lint/style/useNamingConvention: keyed by three's class name.
|
|
120
|
+
Matrix3: {
|
|
121
|
+
promote: true,
|
|
122
|
+
lanes: ["elements"],
|
|
123
|
+
drop: ["elements"],
|
|
124
|
+
read: (value) => Array.from(value.elements as number[]),
|
|
125
|
+
write: (value, values) => {
|
|
126
|
+
value.elements = Array.from(values);
|
|
127
|
+
},
|
|
128
|
+
// `elements` is column-major; the engine constructor takes three's row-major set order.
|
|
129
|
+
args: (values) => [0, 3, 6, 1, 4, 7, 2, 5, 8].map((index) => values[index] as number),
|
|
130
|
+
},
|
|
131
|
+
// quality-allow: the plan table is keyed by three's PascalCase class names, so the name rule cannot apply.
|
|
132
|
+
// biome-ignore lint/style/useNamingConvention: keyed by three's class name.
|
|
133
|
+
Matrix4: {
|
|
134
|
+
promote: true,
|
|
135
|
+
lanes: ["elements"],
|
|
136
|
+
drop: ["elements"],
|
|
137
|
+
read: (value) => Array.from(value.elements as number[]),
|
|
138
|
+
write: (value, values) => {
|
|
139
|
+
value.elements = Array.from(values);
|
|
140
|
+
},
|
|
141
|
+
// `elements` is column-major; the engine constructor takes three's row-major set order.
|
|
142
|
+
args: (values) =>
|
|
143
|
+
[0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15].map(
|
|
144
|
+
(index) => values[index] as number,
|
|
145
|
+
),
|
|
146
|
+
},
|
|
147
|
+
// quality-allow: the plan table is keyed by three's PascalCase class names, so the name rule cannot apply.
|
|
148
|
+
// biome-ignore lint/style/useNamingConvention: keyed by three's class name.
|
|
149
|
+
Box3: {
|
|
150
|
+
promote: true,
|
|
151
|
+
lanes: [],
|
|
152
|
+
drop: ["min", "max"],
|
|
153
|
+
read: (value) => {
|
|
154
|
+
const min = memberVector(value.min);
|
|
155
|
+
const max = memberVector(value.max);
|
|
156
|
+
return [min.x, min.y, min.z, max.x, max.y, max.z];
|
|
157
|
+
},
|
|
158
|
+
write: (value, values) => {
|
|
159
|
+
memberVector(value.min).set(values[0] as number, values[1] as number, values[2] as number);
|
|
160
|
+
memberVector(value.max).set(values[3] as number, values[4] as number, values[5] as number);
|
|
161
|
+
},
|
|
162
|
+
args: (values, make) => [
|
|
163
|
+
make("Vector3", values.slice(0, 3).map(Number)),
|
|
164
|
+
make("Vector3", values.slice(3, 6).map(Number)),
|
|
165
|
+
],
|
|
166
|
+
},
|
|
167
|
+
// quality-allow: the plan table is keyed by three's PascalCase class names, so the name rule cannot apply.
|
|
168
|
+
// biome-ignore lint/style/useNamingConvention: keyed by three's class name.
|
|
169
|
+
Sphere: {
|
|
170
|
+
promote: true,
|
|
171
|
+
lanes: ["radius"],
|
|
172
|
+
drop: ["center", "radius"],
|
|
173
|
+
read: (value) => {
|
|
174
|
+
const center = memberVector(value.center);
|
|
175
|
+
return [center.x, center.y, center.z, value.radius as number];
|
|
176
|
+
},
|
|
177
|
+
write: (value, values) => {
|
|
178
|
+
memberVector(value.center).set(values[0] as number, values[1] as number, values[2] as number);
|
|
179
|
+
value.radius = values[3] as number;
|
|
180
|
+
},
|
|
181
|
+
args: (values, make) => [make("Vector3", values.slice(0, 3).map(Number)), values[3]],
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
/** What a value class's engine side needs from the back end whose objects it belongs to. */
|
|
186
|
+
export interface IValueViewHost {
|
|
187
|
+
readonly runtime: IBrowserRuntime;
|
|
188
|
+
readonly registry: IRegistryDump;
|
|
189
|
+
refOf(self: unknown): IEngineRef;
|
|
190
|
+
adopt(target: object, ref: IEngineRef): object;
|
|
191
|
+
wrap(ref: IEngineRef): object;
|
|
192
|
+
hold(wrapper: object): void;
|
|
193
|
+
addressOf(self: object): number;
|
|
194
|
+
readonly rotations: WeakMap<object, object>;
|
|
195
|
+
readonly quaternions: WeakMap<object, object>;
|
|
196
|
+
readonly sharedTransformWrite?: (self: object, address: number, value: number) => void;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** The value marshalling one engine keeps: its classes, its loan pool, and both directions. */
|
|
200
|
+
export interface IValueViews {
|
|
201
|
+
toEngine(value: unknown): EngineValue;
|
|
202
|
+
/** JS prototype -> its engine side. */
|
|
203
|
+
readonly valueClasses: Map<object, IValueClass>;
|
|
204
|
+
/** The loans not yet copied back, from `lend` to `repay`. */
|
|
205
|
+
readonly loans: [IValueLanes, IValueLanes, IValueClass][];
|
|
206
|
+
repay(mark: number, returned: unknown): unknown;
|
|
207
|
+
toEngineArgs(args: readonly unknown[]): EngineValue[];
|
|
208
|
+
defineValueView(
|
|
209
|
+
name: string,
|
|
210
|
+
accessors: Record<string, unknown>,
|
|
211
|
+
binding: IRegistryClass,
|
|
212
|
+
skip: Set<string>,
|
|
213
|
+
): void;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function defineValueViews(host: IValueViewHost): IValueViews {
|
|
217
|
+
const {
|
|
218
|
+
runtime,
|
|
219
|
+
registry,
|
|
220
|
+
refOf,
|
|
221
|
+
adopt,
|
|
222
|
+
wrap,
|
|
223
|
+
hold,
|
|
224
|
+
addressOf,
|
|
225
|
+
rotations,
|
|
226
|
+
quaternions,
|
|
227
|
+
sharedTransformWrite,
|
|
228
|
+
} = host;
|
|
229
|
+
const valueClasses = new Map<object, IValueClass>(); // JS prototype -> its engine side
|
|
230
|
+
const loans: [IValueLanes, IValueLanes, IValueClass][] = [];
|
|
231
|
+
let borrowing = false;
|
|
232
|
+
const toEngineArgs = (args: readonly unknown[]): EngineValue[] => {
|
|
233
|
+
borrowing = true;
|
|
234
|
+
try {
|
|
235
|
+
return args.map(toEngine);
|
|
236
|
+
} finally {
|
|
237
|
+
borrowing = false;
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
// A plan's member-vector arguments (Box3's two, Sphere's one) are native temporaries: the engine
|
|
241
|
+
// copies them, so they are released as soon as the value that owns the lanes is constructed.
|
|
242
|
+
const engineArgs = (
|
|
243
|
+
info: IValueClass,
|
|
244
|
+
values: number[],
|
|
245
|
+
): { args: EngineValue[]; temporaries: IEngineRef[] } => {
|
|
246
|
+
if (info.plan.args === undefined) return { args: values, temporaries: [] };
|
|
247
|
+
const temporaries: IEngineRef[] = [];
|
|
248
|
+
const args = info.plan.args(values, (name, lanes) => {
|
|
249
|
+
const ref = runtime.construct(name, lanes);
|
|
250
|
+
temporaries.push(ref);
|
|
251
|
+
return ref;
|
|
252
|
+
});
|
|
253
|
+
return { args, temporaries };
|
|
254
|
+
};
|
|
255
|
+
const lend = (value: IValueLanes, info: IValueClass): IEngineRef => {
|
|
256
|
+
let scratch = info.pool[info.top];
|
|
257
|
+
if (scratch === undefined) {
|
|
258
|
+
const built = engineArgs(info, info.plan.read(value));
|
|
259
|
+
try {
|
|
260
|
+
scratch = wrap(runtime.construct(info.name, built.args));
|
|
261
|
+
} finally {
|
|
262
|
+
for (const temporary of built.temporaries) runtime.release(temporary);
|
|
263
|
+
}
|
|
264
|
+
info.pool.push(scratch);
|
|
265
|
+
}
|
|
266
|
+
info.top++;
|
|
267
|
+
const lanes = scratch as IValueLanes;
|
|
268
|
+
if (info.plan.copy !== undefined) info.plan.copy(lanes, value);
|
|
269
|
+
else info.plan.write(lanes, info.plan.read(value));
|
|
270
|
+
loans.push([value, lanes, info]);
|
|
271
|
+
return refOf(scratch);
|
|
272
|
+
};
|
|
273
|
+
// Copies each borrowed object's lanes back to its JS value; the result answers the value itself.
|
|
274
|
+
const repay = (mark: number, returned: unknown): unknown => {
|
|
275
|
+
let result = returned;
|
|
276
|
+
for (let i = loans.length - 1; i >= mark; i--) {
|
|
277
|
+
const [value, scratch, info] = loans[i] as (typeof loans)[number];
|
|
278
|
+
if (info.plan.copy !== undefined) info.plan.copy(value, scratch);
|
|
279
|
+
else info.plan.write(value, info.plan.read(scratch));
|
|
280
|
+
info.top--;
|
|
281
|
+
if (result === scratch) result = value;
|
|
282
|
+
}
|
|
283
|
+
loans.length = mark;
|
|
284
|
+
return result;
|
|
285
|
+
};
|
|
286
|
+
const promote = (value: IValueLanes, info: IValueClass): void => {
|
|
287
|
+
const values = info.plan.read(value);
|
|
288
|
+
(value as { [UNWATCH]?: () => void })[UNWATCH]?.(); // a uniform's watched lanes go: scan it instead
|
|
289
|
+
for (const key of info.plan.drop) delete value[key];
|
|
290
|
+
Object.setPrototypeOf(value, info.engine);
|
|
291
|
+
const built = engineArgs(info, values);
|
|
292
|
+
try {
|
|
293
|
+
adopt(value, runtime.construct(info.name, built.args));
|
|
294
|
+
} finally {
|
|
295
|
+
for (const temporary of built.temporaries) runtime.release(temporary);
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
// An engine-owned value reads and writes its lanes in the engine's memory through accessors named
|
|
299
|
+
// for the storage its class actually uses. Quaternion and Euler store `_x`, Matrix stores
|
|
300
|
+
// `elements`, and an Euler's order is a string over the engine's byte enum; Box3 and Sphere reach
|
|
301
|
+
// their vectors through the registry's fixed members, so they need nothing here.
|
|
302
|
+
const defineValueView = (
|
|
303
|
+
name: string,
|
|
304
|
+
accessors: Record<string, unknown>,
|
|
305
|
+
binding: IRegistryClass,
|
|
306
|
+
skip: Set<string>,
|
|
307
|
+
): void => {
|
|
308
|
+
const read = runtime.readDoubles;
|
|
309
|
+
const write = runtime.writeDouble;
|
|
310
|
+
const peek = runtime.readByte;
|
|
311
|
+
if (read === undefined || write === undefined) return;
|
|
312
|
+
const heap = runtime.heapF64;
|
|
313
|
+
const lane = heap
|
|
314
|
+
? (address: number): number => heap()[address / 8] as number
|
|
315
|
+
: (address: number): number => read(address, 1) as number;
|
|
316
|
+
if (name === "Quaternion" && binding.fields !== undefined) {
|
|
317
|
+
const offsets = ["x", "y", "z", "w"].map((key) => binding.fields?.[key]?.[0] ?? 0);
|
|
318
|
+
["_x", "_y", "_z", "_w"].forEach((key, index) => {
|
|
319
|
+
const offset = offsets[index] as number;
|
|
320
|
+
Object.defineProperty(accessors, key, {
|
|
321
|
+
configurable: true,
|
|
322
|
+
get(this: object): number {
|
|
323
|
+
return lane(addressOf(this) + offset);
|
|
324
|
+
},
|
|
325
|
+
set(this: object, value: number): void {
|
|
326
|
+
const address = addressOf(this) + offset;
|
|
327
|
+
const next = Number(value);
|
|
328
|
+
sharedTransformWrite?.(this, address, next);
|
|
329
|
+
write(address, next);
|
|
330
|
+
},
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
skip.add("x").add("y").add("z").add("w");
|
|
334
|
+
// three syncs an Object3D's rotation from its quaternion. Physics interpolation sets every body's
|
|
335
|
+
// quaternion every frame, so a call per sync cost 1,732 crossings a frame on the stress lab. This is
|
|
336
|
+
// Euler::setFromQuaternion with update off (Euler.cpp), written into the rotation's lanes: the same
|
|
337
|
+
// formulas, and Math is V8's fdlibm, which the engine's ieee754 functions are, so the bits match.
|
|
338
|
+
const eulerFields = registry.classes.Euler?.fields;
|
|
339
|
+
const angles = ["x", "y", "z"].map((key) => eulerFields?.[key]?.[0]);
|
|
340
|
+
const orderAt = eulerFields?.__order?.[0];
|
|
341
|
+
const local =
|
|
342
|
+
peek !== undefined && orderAt !== undefined && angles.every((at) => at !== undefined);
|
|
343
|
+
const [ax, ay, az] = angles as [number, number, number];
|
|
344
|
+
Object.defineProperty(accessors, "_onChangeCallback", {
|
|
345
|
+
configurable: true,
|
|
346
|
+
writable: true,
|
|
347
|
+
value(this: object): void {
|
|
348
|
+
const owner = quaternions.get(this) as
|
|
349
|
+
| { rotation?: object & { _order?: string } }
|
|
350
|
+
| undefined;
|
|
351
|
+
if (owner?.rotation === undefined) return;
|
|
352
|
+
if (!local) {
|
|
353
|
+
// The rotation's own order; the engine refuses a missing one on this call.
|
|
354
|
+
runtime.invoke(refOf(owner.rotation), "setFromQuaternion", [
|
|
355
|
+
refOf(this),
|
|
356
|
+
owner.rotation._order ?? "XYZ",
|
|
357
|
+
]);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
const q = addressOf(this);
|
|
361
|
+
const e = addressOf(owner.rotation);
|
|
362
|
+
const [ex, ey, ez] = eulerFromQuaternion(
|
|
363
|
+
lane(q + (offsets[0] as number)),
|
|
364
|
+
lane(q + (offsets[1] as number)),
|
|
365
|
+
lane(q + (offsets[2] as number)),
|
|
366
|
+
lane(q + (offsets[3] as number)),
|
|
367
|
+
eulerOrders[(peek as (address: number) => number)(e + (orderAt as number))] ?? "XYZ",
|
|
368
|
+
);
|
|
369
|
+
write(e + ax, ex);
|
|
370
|
+
write(e + ay, ey);
|
|
371
|
+
write(e + az, ez);
|
|
372
|
+
},
|
|
373
|
+
});
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
if (name === "Euler" && binding.fields !== undefined && peek !== undefined) {
|
|
377
|
+
const offsets = ["x", "y", "z"].map((key) => binding.fields?.[key]?.[0] ?? 0) as number[];
|
|
378
|
+
const orderAt = binding.fields.__order?.[0] ?? 0;
|
|
379
|
+
const quaternionFields = registry.classes.Quaternion?.fields;
|
|
380
|
+
const targets = ["x", "y", "z", "w"].map((key) => quaternionFields?.[key]?.[0] ?? 0);
|
|
381
|
+
// three's setFromEuler: each order only signs the second terms, in the engine enum's order.
|
|
382
|
+
const signs = [
|
|
383
|
+
[1, -1, 1, -1],
|
|
384
|
+
[1, -1, -1, 1],
|
|
385
|
+
[-1, 1, 1, -1],
|
|
386
|
+
[-1, 1, -1, 1],
|
|
387
|
+
[1, 1, -1, -1],
|
|
388
|
+
[-1, -1, 1, 1],
|
|
389
|
+
];
|
|
390
|
+
const sync = (euler: object): void => {
|
|
391
|
+
const owner = rotations.get(euler) as { quaternion?: object } | undefined;
|
|
392
|
+
if (owner?.quaternion === undefined) return;
|
|
393
|
+
const at = addressOf(euler);
|
|
394
|
+
const q = addressOf(owner.quaternion);
|
|
395
|
+
const x = lane(at + (offsets[0] as number)) / 2;
|
|
396
|
+
const y = lane(at + (offsets[1] as number)) / 2;
|
|
397
|
+
const z = lane(at + (offsets[2] as number)) / 2;
|
|
398
|
+
const sign = signs[peek(at + orderAt)] ?? (signs[0] as number[]);
|
|
399
|
+
const c1 = Math.cos(x);
|
|
400
|
+
const c2 = Math.cos(y);
|
|
401
|
+
const c3 = Math.cos(z);
|
|
402
|
+
const s1 = Math.sin(x);
|
|
403
|
+
const s2 = Math.sin(y);
|
|
404
|
+
const s3 = Math.sin(z);
|
|
405
|
+
write(q + (targets[0] as number), s1 * c2 * c3 + (sign[0] as number) * c1 * s2 * s3);
|
|
406
|
+
write(q + (targets[1] as number), c1 * s2 * c3 + (sign[1] as number) * s1 * c2 * s3);
|
|
407
|
+
write(q + (targets[2] as number), c1 * c2 * s3 + (sign[2] as number) * s1 * s2 * c3);
|
|
408
|
+
write(q + (targets[3] as number), c1 * c2 * c3 + (sign[3] as number) * s1 * s2 * s3);
|
|
409
|
+
};
|
|
410
|
+
["_x", "_y", "_z"].forEach((key, index) => {
|
|
411
|
+
const offset = offsets[index] as number;
|
|
412
|
+
Object.defineProperty(accessors, key, {
|
|
413
|
+
configurable: true,
|
|
414
|
+
get(this: object): number {
|
|
415
|
+
return lane(addressOf(this) + offset);
|
|
416
|
+
},
|
|
417
|
+
set(this: object, value: number): void {
|
|
418
|
+
const address = addressOf(this) + offset;
|
|
419
|
+
const next = Number(value);
|
|
420
|
+
sharedTransformWrite?.(this, address, next);
|
|
421
|
+
write(address, next);
|
|
422
|
+
},
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
Object.defineProperty(accessors, "_order", {
|
|
426
|
+
configurable: true,
|
|
427
|
+
get(this: object): string {
|
|
428
|
+
return eulerOrders[peek(addressOf(this) + orderAt)] ?? "XYZ";
|
|
429
|
+
},
|
|
430
|
+
set(this: object, value: unknown): void {
|
|
431
|
+
const index = eulerOrders.indexOf(value as (typeof eulerOrders)[number]);
|
|
432
|
+
if (index < 0)
|
|
433
|
+
throw new TypeError(
|
|
434
|
+
`Euler order is not one of ${eulerOrders.join(", ")}: ${String(value)}`,
|
|
435
|
+
);
|
|
436
|
+
// The order is the engine's byte enum: its setter reorders and syncs, so only a change
|
|
437
|
+
// crosses (four numbers are not worth a call on every `rotation.set`).
|
|
438
|
+
if (index !== peek(addressOf(this) + orderAt))
|
|
439
|
+
runtime.set(refOf(this), "order", value as string);
|
|
440
|
+
},
|
|
441
|
+
});
|
|
442
|
+
Object.defineProperty(accessors, "_onChangeCallback", {
|
|
443
|
+
configurable: true,
|
|
444
|
+
writable: true,
|
|
445
|
+
value(this: object): void {
|
|
446
|
+
sync(this);
|
|
447
|
+
},
|
|
448
|
+
});
|
|
449
|
+
skip.add("x").add("y").add("z").add("order");
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
if ((name === "Matrix3" || name === "Matrix4") && binding.fields?.elements !== undefined) {
|
|
453
|
+
const [offset = 0, count = 0] = binding.fields.elements;
|
|
454
|
+
// three keeps one `elements` array; this keeps one proxy per matrix over a mirrored array and
|
|
455
|
+
// writes every element it sets straight to engine memory, so the array never detaches.
|
|
456
|
+
const mirrors = new WeakMap<object, { values: number[]; view: number[] }>();
|
|
457
|
+
// Off the heap view when there is one: a game reads `matrixWorld.elements` hundreds of times
|
|
458
|
+
// a frame, and a copy call allocates two arrays a read.
|
|
459
|
+
const refresh = heap
|
|
460
|
+
? (address: number, values: number[]): void => {
|
|
461
|
+
const memory = heap();
|
|
462
|
+
for (let i = 0, at = address / 8; i < count; i++) values[i] = memory[at + i] as number;
|
|
463
|
+
}
|
|
464
|
+
: (address: number, values: number[]): void => {
|
|
465
|
+
const memory = read(address, count) as number[];
|
|
466
|
+
for (let i = 0; i < count; i++) values[i] = memory[i] as number;
|
|
467
|
+
};
|
|
468
|
+
// Shared memory (the threads build) never detaches when it grows, so a view onto it is the
|
|
469
|
+
// matrix itself: three's compose wrote 16 trapped elements a call, 49 ms of a laptop load (PRD-860).
|
|
470
|
+
const shared = new WeakMap<object, Float64Array>();
|
|
471
|
+
Object.defineProperty(accessors, "elements", {
|
|
472
|
+
configurable: true,
|
|
473
|
+
get(this: object): number[] {
|
|
474
|
+
const address = addressOf(this) + offset;
|
|
475
|
+
const memory = heap?.();
|
|
476
|
+
if (
|
|
477
|
+
typeof SharedArrayBuffer !== "undefined" &&
|
|
478
|
+
memory?.buffer instanceof SharedArrayBuffer
|
|
479
|
+
) {
|
|
480
|
+
let view = shared.get(this);
|
|
481
|
+
if (view === undefined) {
|
|
482
|
+
view = memory.subarray(address / 8, address / 8 + count);
|
|
483
|
+
shared.set(this, view);
|
|
484
|
+
}
|
|
485
|
+
// quality-allow: three's elements are an indexable number list; a Float64Array is one.
|
|
486
|
+
return view as unknown as number[];
|
|
487
|
+
}
|
|
488
|
+
let mirror = mirrors.get(this);
|
|
489
|
+
if (mirror === undefined) {
|
|
490
|
+
const values = new Array<number>(count);
|
|
491
|
+
const view = new Proxy(values, {
|
|
492
|
+
set: (target, key, value): boolean => {
|
|
493
|
+
const index = typeof key === "string" ? Number(key) : Number.NaN;
|
|
494
|
+
if (Number.isInteger(index) && index >= 0 && index < count)
|
|
495
|
+
write(address + index * 8, Number(value));
|
|
496
|
+
target[key as never] = value as never;
|
|
497
|
+
return true;
|
|
498
|
+
},
|
|
499
|
+
});
|
|
500
|
+
mirror = { values, view };
|
|
501
|
+
mirrors.set(this, mirror);
|
|
502
|
+
}
|
|
503
|
+
refresh(address, mirror.values);
|
|
504
|
+
return mirror.view;
|
|
505
|
+
},
|
|
506
|
+
set(this: object, value: ArrayLike<number>): void {
|
|
507
|
+
const address = addressOf(this) + offset;
|
|
508
|
+
for (let i = 0; i < count; i++) write(address + i * 8, Number(value[i]));
|
|
509
|
+
},
|
|
510
|
+
});
|
|
511
|
+
skip.add("elements");
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
const toEngine = (value: unknown): EngineValue => {
|
|
515
|
+
if (value === null || value === undefined) return null;
|
|
516
|
+
if (typeof value === "number" || typeof value === "boolean" || typeof value === "string")
|
|
517
|
+
return value;
|
|
518
|
+
if (ArrayBuffer.isView(value) && !(value instanceof DataView)) {
|
|
519
|
+
// An image's `getImageData` pixels are a Uint8ClampedArray: boxed into a list, a 4096x2048 sky took 1.4 s.
|
|
520
|
+
if (
|
|
521
|
+
runtime.typedArrays === true &&
|
|
522
|
+
(value instanceof Uint8ClampedArray || SCALARS.some((scalar) => value instanceof scalar))
|
|
523
|
+
)
|
|
524
|
+
return value as TypedArray;
|
|
525
|
+
// quality-allow: a typed-array view read as its numeric index signature, which ArrayBufferView does not declare.
|
|
526
|
+
const list = Array.from(value as unknown as ArrayLike<number>);
|
|
527
|
+
Object.defineProperty(list, TYPED, { value: value.constructor.name });
|
|
528
|
+
return list;
|
|
529
|
+
}
|
|
530
|
+
if (Array.isArray(value)) return Array.from(value as ArrayLike<unknown>, toEngine);
|
|
531
|
+
const info = valueClasses.get(Object.getPrototypeOf(value) as object);
|
|
532
|
+
if (info !== undefined) {
|
|
533
|
+
if (borrowing && info.plan.promote !== true) return lend(value as IValueLanes, info);
|
|
534
|
+
promote(value as IValueLanes, info);
|
|
535
|
+
}
|
|
536
|
+
if (typeof value === "object" && REF in value) {
|
|
537
|
+
// A wrapper passed into the engine is held until the next safe point decides (collect()).
|
|
538
|
+
hold(value);
|
|
539
|
+
return (value as IWrapped)[REF];
|
|
540
|
+
}
|
|
541
|
+
// An options object (`new ExtrudeGeometry(shape, { depth })`) crosses as a record of scalars and
|
|
542
|
+
// engine objects, as the V8 adapter passes it; an undefined value is left out, as three reads it.
|
|
543
|
+
if (isPlainObject(value)) {
|
|
544
|
+
const fields: Record<string, EngineValue> = {};
|
|
545
|
+
for (const [key, item] of Object.entries(value as object)) {
|
|
546
|
+
if (item === undefined) continue;
|
|
547
|
+
if (
|
|
548
|
+
item !== null &&
|
|
549
|
+
typeof item === "object" &&
|
|
550
|
+
!(REF in item) &&
|
|
551
|
+
!valueClasses.has(Object.getPrototypeOf(item) as object)
|
|
552
|
+
)
|
|
553
|
+
throw new TypeError(
|
|
554
|
+
`TN_BROWSER_ARGUMENT_UNSUPPORTED: option ${key} cannot cross to the engine`,
|
|
555
|
+
);
|
|
556
|
+
fields[key] = toEngine(item);
|
|
557
|
+
}
|
|
558
|
+
return fields;
|
|
559
|
+
}
|
|
560
|
+
throw new TypeError(
|
|
561
|
+
`TN_BROWSER_ARGUMENT_UNSUPPORTED: ${typeof value} cannot cross to the engine`,
|
|
562
|
+
);
|
|
563
|
+
};
|
|
564
|
+
return {
|
|
565
|
+
toEngine,
|
|
566
|
+
valueClasses,
|
|
567
|
+
loans,
|
|
568
|
+
repay,
|
|
569
|
+
toEngineArgs,
|
|
570
|
+
defineValueView,
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* three's `Euler.setFromQuaternion` (and the engine's Euler.cpp, whose ieee754 functions are V8's fdlibm, so the
|
|
576
|
+
* bits match): the rotation matrix of a unit quaternion, then the angles of the given order.
|
|
577
|
+
*/
|
|
578
|
+
export function eulerFromQuaternion(
|
|
579
|
+
x: number,
|
|
580
|
+
y: number,
|
|
581
|
+
z: number,
|
|
582
|
+
w: number,
|
|
583
|
+
order: string,
|
|
584
|
+
): [number, number, number] {
|
|
585
|
+
const x2 = x + x;
|
|
586
|
+
const y2 = y + y;
|
|
587
|
+
const z2 = z + z;
|
|
588
|
+
const xx = x * x2;
|
|
589
|
+
const xy = x * y2;
|
|
590
|
+
const xz = x * z2;
|
|
591
|
+
const yy = y * y2;
|
|
592
|
+
const yz = y * z2;
|
|
593
|
+
const zz = z * z2;
|
|
594
|
+
const wx = w * x2;
|
|
595
|
+
const wy = w * y2;
|
|
596
|
+
const wz = w * z2;
|
|
597
|
+
const m11 = 1 - (yy + zz);
|
|
598
|
+
const m12 = xy - wz;
|
|
599
|
+
const m13 = xz + wy;
|
|
600
|
+
const m21 = xy + wz;
|
|
601
|
+
const m22 = 1 - (xx + zz);
|
|
602
|
+
const m23 = yz - wx;
|
|
603
|
+
const m31 = xz - wy;
|
|
604
|
+
const m32 = yz + wx;
|
|
605
|
+
const m33 = 1 - (xx + yy);
|
|
606
|
+
const asin = (value: number): number => Math.asin(Math.max(-1, Math.min(1, value)));
|
|
607
|
+
const free = (value: number): boolean => Math.abs(value) < 0.9999999;
|
|
608
|
+
switch (order) {
|
|
609
|
+
case "YXZ":
|
|
610
|
+
return free(m23)
|
|
611
|
+
? [-asin(m23), Math.atan2(m13, m33), Math.atan2(m21, m22)]
|
|
612
|
+
: [-asin(m23), Math.atan2(-m31, m11), 0];
|
|
613
|
+
case "ZXY":
|
|
614
|
+
return free(m32)
|
|
615
|
+
? [asin(m32), Math.atan2(-m31, m33), Math.atan2(-m12, m22)]
|
|
616
|
+
: [asin(m32), 0, Math.atan2(m21, m11)];
|
|
617
|
+
case "ZYX":
|
|
618
|
+
return free(m31)
|
|
619
|
+
? [Math.atan2(m32, m33), -asin(m31), Math.atan2(m21, m11)]
|
|
620
|
+
: [0, -asin(m31), Math.atan2(-m12, m22)];
|
|
621
|
+
case "YZX":
|
|
622
|
+
return free(m21)
|
|
623
|
+
? [Math.atan2(-m23, m22), Math.atan2(-m31, m11), asin(m21)]
|
|
624
|
+
: [0, Math.atan2(m13, m33), asin(m21)];
|
|
625
|
+
case "XZY":
|
|
626
|
+
return free(m12)
|
|
627
|
+
? [Math.atan2(m32, m22), Math.atan2(m13, m11), -asin(m12)]
|
|
628
|
+
: [Math.atan2(-m23, m33), 0, -asin(m12)];
|
|
629
|
+
default:
|
|
630
|
+
return free(m13)
|
|
631
|
+
? [Math.atan2(-m23, m33), asin(m13), Math.atan2(-m12, m11)]
|
|
632
|
+
: [Math.atan2(m32, m22), asin(m13), 0];
|
|
633
|
+
}
|
|
634
|
+
}
|