@threenative/three-native 0.3.11 → 0.3.12
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/gltf-loader-web.ts +29 -10
- package/src/addons/screen-space.ts +42 -6
- package/src/bridge-meter.ts +10 -3
- package/src/browser-abi-types.ts +427 -0
- package/src/browser-attribute-sync.ts +180 -0
- package/src/browser-backend.ts +196 -1646
- package/src/browser-entry.ts +31 -13
- package/src/browser-marshal.ts +79 -0
- package/src/browser-renderer.ts +152 -9
- package/src/browser-tsl.ts +57 -11
- package/src/browser-value-views.ts +618 -0
- package/src/browser-wasm-runtime.ts +500 -0
- package/src/debug-telemetry.ts +3 -0
- package/src/object-surface.ts +17 -0
- package/src/pass-node.ts +333 -9
- package/src/render-target.ts +43 -10
- package/src/texture-sources.ts +68 -17
- package/src/tsl-extras.ts +50 -0
- package/src/tsl-uniforms.ts +150 -8
- package/src/update-ranges.ts +23 -0
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* three's `attribute.array` and the engine's copy of it, on the browser back end (PRD-540, PRD-617,
|
|
3
|
+
* PRD-803): the array the constructor was handed or a copy of the engine's data on first read, and
|
|
4
|
+
* the ranges an engine mutator writes back. A view of Wasm memory would detach when it grows, so the
|
|
5
|
+
* shadow is a JS array; the two are kept in step, and `needsUpdate` sends the array to the engine.
|
|
6
|
+
* Each engine keeps its own shadows, so the back end builds them per engine.
|
|
7
|
+
*/
|
|
8
|
+
import type { IBrowserRuntime, IEngineRef } from "./browser-abi-types.js";
|
|
9
|
+
import type { TypedArray } from "./browser-marshal.js";
|
|
10
|
+
|
|
11
|
+
/** three's MathUtils.denormalize: a normalized integer lane to [-1, 1] or [0, 1]. */
|
|
12
|
+
export function denormalize(value: number, array: TypedArray): number {
|
|
13
|
+
if (array instanceof Float32Array || array instanceof Float64Array) return value;
|
|
14
|
+
if (array instanceof Uint32Array) return value / 4294967295;
|
|
15
|
+
if (array instanceof Uint16Array) return value / 65535;
|
|
16
|
+
if (array instanceof Uint8Array) return value / 255;
|
|
17
|
+
if (array instanceof Int32Array) return Math.max(value / 2147483647, -1);
|
|
18
|
+
if (array instanceof Int16Array) return Math.max(value / 32767, -1);
|
|
19
|
+
return Math.max(value / 127, -1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const ATTRIBUTE_CLASSES = new Set([
|
|
23
|
+
"BufferAttribute",
|
|
24
|
+
"Float32BufferAttribute",
|
|
25
|
+
"Uint16BufferAttribute",
|
|
26
|
+
"Uint32BufferAttribute",
|
|
27
|
+
"InstancedBufferAttribute",
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
/** What an attribute's shadow needs from the back end that owns the object. */
|
|
31
|
+
export interface IAttributeSyncHost {
|
|
32
|
+
readonly runtime: IBrowserRuntime;
|
|
33
|
+
refOf(self: unknown): IEngineRef;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The attribute shadows one engine keeps, and the range bookkeeping an engine mutator's call needs. */
|
|
37
|
+
export interface IAttributeSync {
|
|
38
|
+
readonly attributeArray: IBrowserRuntime["attributeArray"];
|
|
39
|
+
readonly attributeWrite: IBrowserRuntime["attributeWrite"];
|
|
40
|
+
readonly arrays: WeakMap<object, { array: TypedArray }>;
|
|
41
|
+
readonly itemSizes: WeakMap<object, number>;
|
|
42
|
+
/** Shape survives data uploads and transform calls; only copy/replacement changes it. */
|
|
43
|
+
readonly shapes: WeakMap<object, Map<string, unknown>>;
|
|
44
|
+
readonly pending: Set<object>;
|
|
45
|
+
writeBack(): void;
|
|
46
|
+
itemSizeOf(attribute: object): number;
|
|
47
|
+
rangeOf(method: string, args: readonly unknown[], itemSize: number): readonly [number, number];
|
|
48
|
+
syncRange(attribute: object, entry: { array: TypedArray }, offset: number, count: number): void;
|
|
49
|
+
syncFull(attribute: object, entry: { array: TypedArray }): void;
|
|
50
|
+
instanceItem(
|
|
51
|
+
mesh: object,
|
|
52
|
+
attribute: string,
|
|
53
|
+
index: unknown,
|
|
54
|
+
argument: unknown,
|
|
55
|
+
): { array: TypedArray; at: number; values: ArrayLike<number> } | undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function defineAttributeSync(host: IAttributeSyncHost): IAttributeSync {
|
|
59
|
+
const { runtime, refOf } = host;
|
|
60
|
+
// three's `attribute.array` is the attribute's own JS typed array (PRD-540): the array the
|
|
61
|
+
// constructor was handed, or a copy of the engine's data on first read. A view of Wasm memory
|
|
62
|
+
// would detach when the memory grows, so the shadow is a JS array. The shadow and the engine's
|
|
63
|
+
// store are kept in step (PRD-617): an engine mutator copies back ONLY the range it wrote, so a
|
|
64
|
+
// later game edit to the array wins, and `needsUpdate` sends the array to the engine. A read hands
|
|
65
|
+
// the game the array, so the attribute is `pending` until its array is written back before the next
|
|
66
|
+
// engine call (a per-vertex setter reads no kept array and flushes nothing).
|
|
67
|
+
const { attributeArray, attributeWrite } = runtime;
|
|
68
|
+
const arrays = new WeakMap<object, { array: TypedArray }>();
|
|
69
|
+
const itemSizes = new WeakMap<object, number>();
|
|
70
|
+
// Shape survives data uploads and transform calls; only copy/replacement changes it.
|
|
71
|
+
const shapes = new WeakMap<object, Map<string, unknown>>();
|
|
72
|
+
const pending = new Set<object>();
|
|
73
|
+
const writeBack = (): void => {
|
|
74
|
+
for (const attribute of pending) {
|
|
75
|
+
const entry = arrays.get(attribute);
|
|
76
|
+
if (entry !== undefined) attributeWrite?.call(runtime, refOf(attribute), entry.array);
|
|
77
|
+
}
|
|
78
|
+
pending.clear();
|
|
79
|
+
};
|
|
80
|
+
const itemSizeOf = (attribute: object): number => {
|
|
81
|
+
let size = itemSizes.get(attribute);
|
|
82
|
+
if (size === undefined) {
|
|
83
|
+
size = Number(runtime.get(refOf(attribute), "itemSize"));
|
|
84
|
+
itemSizes.set(attribute, size);
|
|
85
|
+
}
|
|
86
|
+
return size;
|
|
87
|
+
};
|
|
88
|
+
// The scalar range an attribute mutator wrote: derived from the method and its arguments, so the
|
|
89
|
+
// engine syncs back only those elements. A component setter writes one, setXY/XYZ/XYZW an item's
|
|
90
|
+
// lanes, copyAt a whole destination item.
|
|
91
|
+
const lanes: Record<string, number> = { setX: 0, setY: 1, setZ: 2, setW: 3 };
|
|
92
|
+
const rangeOf = (
|
|
93
|
+
method: string,
|
|
94
|
+
args: readonly unknown[],
|
|
95
|
+
itemSize: number,
|
|
96
|
+
): readonly [number, number] => {
|
|
97
|
+
const at = Number(args[0]) * itemSize;
|
|
98
|
+
if (method === "copyAt") return [at, itemSize];
|
|
99
|
+
const component = lanes[method];
|
|
100
|
+
if (component !== undefined) return [at + component, 1];
|
|
101
|
+
if (method === "setComponent") return [at + Number(args[1]), 1];
|
|
102
|
+
return [at, method === "setXY" ? 2 : method === "setXYZ" ? 3 : 4];
|
|
103
|
+
};
|
|
104
|
+
const syncRange = (
|
|
105
|
+
attribute: object,
|
|
106
|
+
entry: { array: TypedArray },
|
|
107
|
+
offset: number,
|
|
108
|
+
count: number,
|
|
109
|
+
): void => {
|
|
110
|
+
// A malformed range (a fraction, NaN, Infinity, a negative offset or a non-positive count) names
|
|
111
|
+
// no element: it must fail, not slip to element 0 through subarray's own coercion. A range that
|
|
112
|
+
// only runs past the end is the engine's documented bounds no-op (`setComponent`/`setRaw`
|
|
113
|
+
// ignore it, as JS does), so there is nothing to read back. A required range read that comes
|
|
114
|
+
// back missing or short is a provider fault, never a silently stale shadow.
|
|
115
|
+
if (!Number.isSafeInteger(offset) || !Number.isSafeInteger(count) || offset < 0 || count <= 0)
|
|
116
|
+
throw new RangeError(
|
|
117
|
+
`TN_NATIVE_ATTRIBUTE_RANGE: [${String(offset)}, ${String(count)}) is not a valid element range`,
|
|
118
|
+
);
|
|
119
|
+
if (offset + count > entry.array.length) return;
|
|
120
|
+
const fresh = attributeArray?.call(runtime, refOf(attribute), offset, count);
|
|
121
|
+
if (fresh === undefined || fresh.length !== count)
|
|
122
|
+
throw new TypeError(
|
|
123
|
+
`TN_NATIVE_ATTRIBUTE_SYNC: attributeArray gave ${fresh === undefined ? "no array" : `${String(fresh.length)} elements`} for [${String(offset)}, ${String(count)})`,
|
|
124
|
+
);
|
|
125
|
+
entry.array.set(fresh, offset);
|
|
126
|
+
};
|
|
127
|
+
const syncFull = (attribute: object, entry: { array: TypedArray }): void => {
|
|
128
|
+
const fresh = attributeArray?.call(runtime, refOf(attribute));
|
|
129
|
+
if (fresh === undefined) return;
|
|
130
|
+
if (fresh.length === entry.array.length) entry.array.set(fresh);
|
|
131
|
+
else entry.array = fresh;
|
|
132
|
+
};
|
|
133
|
+
// three's `InstancedMesh.setMatrixAt`/`setColorAt` write the mesh's own `instanceMatrix`/
|
|
134
|
+
// `instanceColor` array item (`index * 16`, `index * 3`); the engine's mesh-level call writes only
|
|
135
|
+
// its own store, so the shadow a game already holds (core's velocity capture reads one per mesh a
|
|
136
|
+
// frame) would send its frozen snapshot back at `needsUpdate` and leave the mesh where it stood.
|
|
137
|
+
// Take the item the call writes into that same array, as three does. No shadow, no work: this is
|
|
138
|
+
// a pair of map lookups and an array copy, never an engine call.
|
|
139
|
+
const instanceItem = (
|
|
140
|
+
mesh: object,
|
|
141
|
+
attribute: string,
|
|
142
|
+
index: unknown,
|
|
143
|
+
argument: unknown,
|
|
144
|
+
): { array: TypedArray; at: number; values: ArrayLike<number> } | undefined => {
|
|
145
|
+
const held = shapes.get(mesh)?.get(attribute);
|
|
146
|
+
if (held === null || held === undefined) return undefined;
|
|
147
|
+
const entry = arrays.get(held as object);
|
|
148
|
+
if (entry === undefined) return undefined;
|
|
149
|
+
const source = argument as {
|
|
150
|
+
elements?: ArrayLike<number>;
|
|
151
|
+
r?: number;
|
|
152
|
+
g?: number;
|
|
153
|
+
b?: number;
|
|
154
|
+
} | null;
|
|
155
|
+
const values =
|
|
156
|
+
attribute === "instanceMatrix"
|
|
157
|
+
? source?.elements
|
|
158
|
+
: [Number(source?.r), Number(source?.g), Number(source?.b)];
|
|
159
|
+
if (values === undefined) return undefined;
|
|
160
|
+
const at = Number(index) * values.length;
|
|
161
|
+
// three's `toArray` writes one lane at a time, so a lane outside the array is ignored, not an error.
|
|
162
|
+
if (!Number.isSafeInteger(at) || at < 0 || at + values.length > entry.array.length)
|
|
163
|
+
return undefined;
|
|
164
|
+
return { array: entry.array, at, values };
|
|
165
|
+
};
|
|
166
|
+
return {
|
|
167
|
+
attributeArray,
|
|
168
|
+
attributeWrite,
|
|
169
|
+
arrays,
|
|
170
|
+
itemSizes,
|
|
171
|
+
shapes,
|
|
172
|
+
pending,
|
|
173
|
+
writeBack,
|
|
174
|
+
itemSizeOf,
|
|
175
|
+
rangeOf,
|
|
176
|
+
syncRange,
|
|
177
|
+
syncFull,
|
|
178
|
+
instanceItem,
|
|
179
|
+
};
|
|
180
|
+
}
|