@threenative/three-native 0.3.10 → 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
package/src/texture-sources.ts
CHANGED
|
@@ -23,9 +23,10 @@ interface IPixels {
|
|
|
23
23
|
interface ICanvasLike {
|
|
24
24
|
readonly width: number;
|
|
25
25
|
readonly height: number;
|
|
26
|
-
getContext(
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
getContext(kind: "2d"): {
|
|
27
|
+
getImageData(x: number, y: number, w: number, h: number): IPixels;
|
|
28
|
+
getContextAttributes?(): { willReadFrequently?: boolean };
|
|
29
|
+
} | null;
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
/** three's Texture defaults: an image is flipped, linear-filtered and mipmapped, unlike DataTexture's. */
|
|
@@ -40,26 +41,34 @@ function isPixels(image: unknown): image is IPixels {
|
|
|
40
41
|
return ArrayBuffer.isView(candidate?.data) && typeof candidate?.width === "number";
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
/** An image's RGBA pixels: its own `data`, a canvas
|
|
44
|
+
/** An image's RGBA pixels: its own `data`, a CPU-backed canvas read directly, or a drawable copied. */
|
|
44
45
|
function pixelsOf(image: unknown): IPixels {
|
|
45
46
|
if (isPixels(image)) return image;
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
const own = isCanvas(image) ? image.getContext("2d") : null;
|
|
48
|
+
// A canvas made `willReadFrequently` is CPU-backed, so its own readback is exact and needs no copy,
|
|
49
|
+
// which keeps a canvas redrawn and re-sent each frame at one getImageData.
|
|
50
|
+
if (own?.getContextAttributes?.().willReadFrequently === true)
|
|
51
|
+
return own.getImageData(0, 0, (image as ICanvasLike).width, (image as ICanvasLike).height);
|
|
51
52
|
const drawable = image as { width?: number; height?: number } | null;
|
|
52
|
-
if (
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
53
|
+
if (typeof OffscreenCanvas !== "undefined" && drawable?.width && drawable.height) {
|
|
54
|
+
// A GPU-backed canvas does not hand its pixels back: measured on Chromium over Vulkan,
|
|
55
|
+
// `getImageData` of a red `fillRect` reads 0/0/0 there and 255/0/0 in a `willReadFrequently`
|
|
56
|
+
// canvas, which uploaded every `assets.texture()` as black (the Strata tundra ground). A canvas
|
|
57
|
+
// made with `document.createElement` and a default 2D context is GPU-backed, and a webgl canvas
|
|
58
|
+
// has no 2D context at all. Drawing either into a CPU-backed canvas is a plain copy the browser
|
|
59
|
+
// performs correctly, whatever context the caller created.
|
|
60
|
+
const context = new OffscreenCanvas(drawable.width, drawable.height).getContext("2d", {
|
|
61
|
+
willReadFrequently: true,
|
|
62
|
+
});
|
|
59
63
|
context?.drawImage(image as CanvasImageSource, 0, 0);
|
|
60
64
|
const pixels = context?.getImageData(0, 0, drawable.width, drawable.height);
|
|
61
65
|
if (pixels !== undefined) return pixels;
|
|
62
66
|
}
|
|
67
|
+
if (own !== null) {
|
|
68
|
+
// No OffscreenCanvas to copy into: the caller's canvas is the only readback left.
|
|
69
|
+
const { width, height } = image as ICanvasLike;
|
|
70
|
+
return own.getImageData(0, 0, width, height);
|
|
71
|
+
}
|
|
63
72
|
throw new TypeError(
|
|
64
73
|
"TN_BROWSER_TEXTURE_SOURCE: expected ImageData, a canvas with a 2D context, or an image OffscreenCanvas can draw",
|
|
65
74
|
);
|
|
@@ -72,12 +81,13 @@ export function defineTextureSources(
|
|
|
72
81
|
): Record<string, unknown> {
|
|
73
82
|
const BaseTexture = classes.Texture as EngineClass;
|
|
74
83
|
const BaseVolume = classes.Data3DTexture as EngineClass | undefined;
|
|
84
|
+
const BaseLayers = classes.DataArrayTexture as EngineClass | undefined;
|
|
75
85
|
const BaseData = classes.DataTexture as EngineClass;
|
|
76
86
|
const BaseCanvas = classes.CanvasTexture as EngineClass | undefined;
|
|
77
87
|
if (BaseCanvas === undefined) throw new TypeError("TN_BROWSER_UNBOUND: CanvasTexture");
|
|
78
88
|
const sources = new WeakMap<object, unknown>();
|
|
79
89
|
const prototype = BaseData.prototype as object;
|
|
80
|
-
for (const Native of [BaseTexture, BaseData, BaseCanvas, BaseVolume]) {
|
|
90
|
+
for (const Native of [BaseTexture, BaseData, BaseCanvas, BaseVolume, BaseLayers]) {
|
|
81
91
|
if (Native === undefined) continue;
|
|
82
92
|
const target = Native.prototype as { clone(): object };
|
|
83
93
|
const clone = target.clone;
|
|
@@ -87,6 +97,19 @@ export function defineTextureSources(
|
|
|
87
97
|
return result;
|
|
88
98
|
};
|
|
89
99
|
}
|
|
100
|
+
// three's `texture.source`: `dataReady = false` tells renderer.initTexture that copyTextureToTexture writes the texels.
|
|
101
|
+
const dataSources = new WeakMap<object, { dataReady: boolean }>();
|
|
102
|
+
for (const Native of [BaseTexture, BaseData, BaseCanvas, BaseVolume, BaseLayers]) {
|
|
103
|
+
if (Native === undefined) continue;
|
|
104
|
+
Object.defineProperty(Native.prototype, "source", {
|
|
105
|
+
configurable: true,
|
|
106
|
+
get(this: object) {
|
|
107
|
+
const source = dataSources.get(this) ?? { dataReady: true };
|
|
108
|
+
dataSources.set(this, source);
|
|
109
|
+
return source;
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
}
|
|
90
113
|
/** `image` is the JS source; `needsUpdate` re-sends its pixels, as three re-reads `texture.image`. */
|
|
91
114
|
const followSource = (target: object, name: string): void => {
|
|
92
115
|
const needsUpdate = Object.getOwnPropertyDescriptor(target, "needsUpdate")?.set;
|
|
@@ -122,6 +145,7 @@ export function defineTextureSources(
|
|
|
122
145
|
};
|
|
123
146
|
followSource(prototype, "DataTexture");
|
|
124
147
|
if (BaseVolume !== undefined) followSource(BaseVolume.prototype as object, "Data3DTexture");
|
|
148
|
+
if (BaseLayers !== undefined) followSource(BaseLayers.prototype as object, "DataArrayTexture");
|
|
125
149
|
followSource(BaseCanvas.prototype as object, "CanvasTexture");
|
|
126
150
|
|
|
127
151
|
/** An engine DataTexture holding `pixels`, re-read from `image` on every needsUpdate. */
|
|
@@ -172,6 +196,32 @@ export function defineTextureSources(
|
|
|
172
196
|
}
|
|
173
197
|
if (BaseVolume !== undefined) Data3DTexture.prototype = BaseVolume.prototype;
|
|
174
198
|
|
|
199
|
+
/** three's DataArrayTexture(data, width, height, depth): the engine's own class, `depth` layers of RGBA texels. */
|
|
200
|
+
function DataArrayTexture(
|
|
201
|
+
this: unknown,
|
|
202
|
+
data?: ArrayLike<number> | null,
|
|
203
|
+
width = 1,
|
|
204
|
+
height = 1,
|
|
205
|
+
depth = 1,
|
|
206
|
+
) {
|
|
207
|
+
if (BaseLayers === undefined) throw new TypeError("TN_BROWSER_UNBOUND: DataArrayTexture");
|
|
208
|
+
const texture = Reflect.construct(
|
|
209
|
+
BaseLayers,
|
|
210
|
+
[data ?? undefined, width, height, depth],
|
|
211
|
+
new.target ?? DataArrayTexture,
|
|
212
|
+
);
|
|
213
|
+
sources.set(texture, { data: data ?? null, width, height, depth });
|
|
214
|
+
return texture;
|
|
215
|
+
}
|
|
216
|
+
if (BaseLayers !== undefined) {
|
|
217
|
+
DataArrayTexture.prototype = BaseLayers.prototype;
|
|
218
|
+
// three's flags: r185 names the array class both ways.
|
|
219
|
+
Object.assign(BaseLayers.prototype as object, {
|
|
220
|
+
isArrayTexture: true,
|
|
221
|
+
isDataArrayTexture: true,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
175
225
|
/** three's CanvasTexture: the engine's own class over the canvas's pixels, with a full mip chain. */
|
|
176
226
|
function CanvasTexture(this: unknown, canvas: unknown, ...rest: unknown[]) {
|
|
177
227
|
const pixels = pixelsOf(canvas);
|
|
@@ -267,6 +317,7 @@ export function defineTextureSources(
|
|
|
267
317
|
CanvasTexture,
|
|
268
318
|
DataTexture,
|
|
269
319
|
...(BaseVolume === undefined ? {} : { Data3DTexture }),
|
|
320
|
+
...(BaseLayers === undefined ? {} : { DataArrayTexture }),
|
|
270
321
|
ImageBitmapLoader,
|
|
271
322
|
Texture,
|
|
272
323
|
TextureLoader,
|
package/src/tsl-extras.ts
CHANGED
|
@@ -7,6 +7,16 @@ interface IExtrasTsl {
|
|
|
7
7
|
readonly clamp: (value: unknown, low: number, high: number) => unknown;
|
|
8
8
|
readonly float: (value: number) => IFogNode;
|
|
9
9
|
readonly linearDepth: () => IFogNode;
|
|
10
|
+
// quality-allow: three's TSL export name.
|
|
11
|
+
// biome-ignore lint/style/useNamingConvention: three's TSL export name.
|
|
12
|
+
readonly mx_noise_float: (position: unknown) => IFogNode;
|
|
13
|
+
readonly vec3: (value: unknown) => IFogNode;
|
|
14
|
+
readonly vec2: (
|
|
15
|
+
x: unknown,
|
|
16
|
+
y?: unknown,
|
|
17
|
+
) => IFogNode & { readonly x: IFogNode; readonly y: IFogNode };
|
|
18
|
+
readonly cos: (value: unknown) => IFogNode;
|
|
19
|
+
readonly sin: (value: unknown) => IFogNode;
|
|
10
20
|
readonly vec4: (color: unknown, factor: unknown) => IFogNode;
|
|
11
21
|
}
|
|
12
22
|
|
|
@@ -43,6 +53,46 @@ export function defineTslExtras(tsl: IExtrasTsl) {
|
|
|
43
53
|
Object.defineProperty(node, "isFogNode", { value: true });
|
|
44
54
|
return node;
|
|
45
55
|
},
|
|
56
|
+
// r187's mx_fractal_noise_float(position, octaves, lacunarity, diminish, amplitude)
|
|
57
|
+
// (materialx/MaterialXNoise.js): the octave sum of amplitude * perlin(p), amplitude *= diminish and
|
|
58
|
+
// p *= lacunarity per octave, over p as a vec3. The octave count is a number here, so the loop is
|
|
59
|
+
// unrolled; a node count refuses by name.
|
|
60
|
+
// quality-allow: three's TSL export name.
|
|
61
|
+
// biome-ignore lint/style/useNamingConvention: three's TSL export name.
|
|
62
|
+
mx_fractal_noise_float: (
|
|
63
|
+
position: unknown,
|
|
64
|
+
octaves: unknown = 3,
|
|
65
|
+
lacunarity: number | IFogNode = 2,
|
|
66
|
+
diminish: number | IFogNode = 0.5,
|
|
67
|
+
amplitude: number | IFogNode = 1,
|
|
68
|
+
) => {
|
|
69
|
+
if (typeof octaves !== "number" || !Number.isInteger(octaves) || octaves < 0 || octaves > 16)
|
|
70
|
+
throw new Error(
|
|
71
|
+
"TN_NATIVE_FRACTAL_OCTAVES: mx_fractal_noise_float needs an integer octave count 0..16 on the native engine",
|
|
72
|
+
);
|
|
73
|
+
const node = (value: number | IFogNode) =>
|
|
74
|
+
typeof value === "number" ? tsl.float(value) : value;
|
|
75
|
+
let p = tsl.vec3(position);
|
|
76
|
+
let weight = tsl.float(1);
|
|
77
|
+
let sum = tsl.float(0);
|
|
78
|
+
for (let octave = 0; octave < octaves; octave += 1) {
|
|
79
|
+
sum = sum.add(weight.mul(tsl.mx_noise_float(p)));
|
|
80
|
+
weight = weight.mul(node(diminish));
|
|
81
|
+
p = p.mul(node(lacunarity));
|
|
82
|
+
}
|
|
83
|
+
return sum.mul(node(amplitude));
|
|
84
|
+
},
|
|
85
|
+
// r187's rotateUV(uv, rotation, center = vec2(0.5)) (utils/UVUtils.js): rotate(uv - center, rotation) + center,
|
|
86
|
+
// where RotateNode turns a vec2 by mat2(cos, sin, -sin, cos).
|
|
87
|
+
rotateUV: (uv: unknown, rotation: unknown, center: unknown = tsl.vec2(0.5, 0.5)) => {
|
|
88
|
+
const p = tsl.vec2(uv).sub(center) as IFogNode & {
|
|
89
|
+
readonly x: IFogNode;
|
|
90
|
+
readonly y: IFogNode;
|
|
91
|
+
};
|
|
92
|
+
const c = tsl.cos(rotation);
|
|
93
|
+
const s = tsl.sin(rotation);
|
|
94
|
+
return tsl.vec2(c.mul(p.x).sub(s.mul(p.y)), s.mul(p.x).add(c.mul(p.y))).add(center);
|
|
95
|
+
},
|
|
46
96
|
// context() and velocity: the context that carries the previous-frame velocity, and that accessor.
|
|
47
97
|
// The native engine has neither, so they refuse at use, by name.
|
|
48
98
|
context: unsupportedExport("context"),
|
package/src/tsl-uniforms.ts
CHANGED
|
@@ -28,22 +28,59 @@ interface IUniformState {
|
|
|
28
28
|
ranFrame?: number;
|
|
29
29
|
/** The lanes last written to the engine: an unchanged value is not written again. */
|
|
30
30
|
lanes?: readonly number[];
|
|
31
|
+
/** True when sync() scans this uniform's value object every call (it could not be watched). */
|
|
32
|
+
scanned?: boolean;
|
|
31
33
|
/** Reused extraction storage, never aliased with `lanes`: the sink reads it before it is refilled. */
|
|
32
34
|
scratch?: number[];
|
|
33
35
|
}
|
|
34
36
|
|
|
37
|
+
/** A watched value's hook back to scanning, called by a back end before it replaces the value's lanes. */
|
|
38
|
+
export const UNWATCH = Symbol.for("threenative.uniformUnwatch");
|
|
39
|
+
|
|
40
|
+
/** The lanes a watched value object (Color, VectorN) carries as plain fields. */
|
|
41
|
+
const WATCHED_LANES = ["x", "y", "z", "w", "r", "g", "b"] as const;
|
|
42
|
+
|
|
35
43
|
function sameLanes(a: readonly number[] | undefined, b: readonly number[]): boolean {
|
|
36
44
|
if (a === undefined || a.length !== b.length) return false;
|
|
37
45
|
for (let i = 0; i < b.length; i++) if (!Object.is(a[i], b[i])) return false;
|
|
38
46
|
return true;
|
|
39
47
|
}
|
|
40
48
|
|
|
49
|
+
/** A three Matrix3 or Matrix4's elements (9 or 16 numbers), or undefined for any other value. */
|
|
50
|
+
export function matrixElements(value: unknown): ArrayLike<number> | undefined {
|
|
51
|
+
if (typeof value !== "object" || value === null) return undefined;
|
|
52
|
+
const elements = (value as { elements?: unknown }).elements;
|
|
53
|
+
if (
|
|
54
|
+
(Array.isArray(elements) || ArrayBuffer.isView(elements)) &&
|
|
55
|
+
((elements as ArrayLike<number>).length === 16 || (elements as ArrayLike<number>).length === 9)
|
|
56
|
+
)
|
|
57
|
+
return elements as ArrayLike<number>;
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** "mat3" or "mat4" for a three Matrix3 or Matrix4, the type its uniform is made at; otherwise undefined. */
|
|
62
|
+
function matrixType(value: unknown): "mat3" | "mat4" | undefined {
|
|
63
|
+
if (typeof value !== "object" || value === null) return undefined;
|
|
64
|
+
const v = value as { isMatrix3?: unknown; isMatrix4?: unknown };
|
|
65
|
+
return v.isMatrix3 === true ? "mat3" : v.isMatrix4 === true ? "mat4" : undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
41
68
|
/**
|
|
42
|
-
* One float per lane: a number, a Color's r, g, b,
|
|
43
|
-
*
|
|
69
|
+
* One float per lane: a number, a Color's r, g, b, a VectorN's x, y, z, w, or a Matrix3/Matrix4's
|
|
70
|
+
* elements (column-major, as three stores them). `out`, when given, receives the lanes and is
|
|
71
|
+
* returned, so a uniform can refill one bounded scratch array per sync.
|
|
44
72
|
*/
|
|
45
73
|
export function uniformLanes(value: unknown, out?: number[]): number[] {
|
|
74
|
+
// A Matrix3 or Matrix4: its column-major elements, the order the engine writes a matN uniform in.
|
|
75
|
+
const elements = matrixElements(value);
|
|
76
|
+
if (elements === undefined) return valueLanes(value, out ?? []);
|
|
46
77
|
const lanes = out ?? [];
|
|
78
|
+
lanes.length = elements.length;
|
|
79
|
+
for (let i = 0; i < elements.length; i += 1) lanes[i] = elements[i] as number;
|
|
80
|
+
return lanes;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function valueLanes(value: unknown, lanes: number[]): number[] {
|
|
47
84
|
if (typeof value === "number") {
|
|
48
85
|
lanes.length = 1;
|
|
49
86
|
lanes[0] = value;
|
|
@@ -67,7 +104,9 @@ export function uniformLanes(value: unknown, out?: number[]): number[] {
|
|
|
67
104
|
return lanes;
|
|
68
105
|
}
|
|
69
106
|
}
|
|
70
|
-
throw new TypeError(
|
|
107
|
+
throw new TypeError(
|
|
108
|
+
`TN_TSL_UNIFORM_VALUE: ${String(value)} is not a number, Color, vector or Matrix3/Matrix4`,
|
|
109
|
+
);
|
|
71
110
|
}
|
|
72
111
|
|
|
73
112
|
function referenceValue(property: string, type: string, object: unknown): unknown {
|
|
@@ -140,10 +179,95 @@ export function liveUniforms<TNode extends object>(
|
|
|
140
179
|
};
|
|
141
180
|
};
|
|
142
181
|
const states = new WeakMap<object, IUniformState>();
|
|
182
|
+
// Object values sync() must scan every call: a matrix (its elements edit in place) or an object whose lanes it
|
|
183
|
+
// cannot watch (accessors, a frozen or native object).
|
|
184
|
+
// A hook's node or an object-valued node (`uniform(camera.matrixWorld)`) is chained into a material and dropped,
|
|
185
|
+
// so the wrapper is all JS holds, and a collected wrapper silently ended its updates (a stale eye in a post
|
|
186
|
+
// pass). An object-valued node is therefore pinned through the value the game owns, not forever: it lives as long
|
|
187
|
+
// as the object it reads, so a per-material matrix is released with that matrix instead of growing a page-wide set
|
|
188
|
+
// every sync walks. ponytail: the value is the closest owner this module sees; the engine release of such a node
|
|
189
|
+
// waits too.
|
|
143
190
|
const objects = new Set<WeakRef<TNode>>();
|
|
144
|
-
|
|
145
|
-
//
|
|
146
|
-
//
|
|
191
|
+
const pinned = new WeakMap<object, TNode[]>();
|
|
192
|
+
// The node lives exactly as long as `value` does, so a wrapper the game dropped is released with the object it
|
|
193
|
+
// reads.
|
|
194
|
+
const own = (node: TNode, value: object) => {
|
|
195
|
+
const owners = pinned.get(value);
|
|
196
|
+
if (owners === undefined) pinned.set(value, [node]);
|
|
197
|
+
else owners.push(node);
|
|
198
|
+
};
|
|
199
|
+
// A plain Color or VectorN value is watched instead: its lanes become accessors that mark the uniforms reading it
|
|
200
|
+
// dirty, so a sync reads only what was edited since the last one, however many uniforms the app holds. An edit
|
|
201
|
+
// between two renders of one frame still reaches the second (FluidVolume edits uniforms between its passes).
|
|
202
|
+
const watchers = new WeakMap<object, WeakRef<TNode>[]>();
|
|
203
|
+
// Weak, as `objects` is: the value owns the node (own), and an entry whose node went with its value is dropped by
|
|
204
|
+
// the sync that finds it gone.
|
|
205
|
+
const dirty = new Set<WeakRef<TNode>>();
|
|
206
|
+
const watch = (node: TNode, value: object): boolean => {
|
|
207
|
+
if (matrixType(value) !== undefined) return false;
|
|
208
|
+
const known = watchers.get(value);
|
|
209
|
+
if (known !== undefined) {
|
|
210
|
+
known.push(new WeakRef(node));
|
|
211
|
+
own(node, value);
|
|
212
|
+
return true;
|
|
213
|
+
}
|
|
214
|
+
const keys = WATCHED_LANES.filter((key) => Object.hasOwn(value, key));
|
|
215
|
+
const plain = (key: string) => {
|
|
216
|
+
const field = Object.getOwnPropertyDescriptor(value, key);
|
|
217
|
+
return (
|
|
218
|
+
field !== undefined &&
|
|
219
|
+
"value" in field &&
|
|
220
|
+
field.writable === true &&
|
|
221
|
+
field.configurable === true
|
|
222
|
+
);
|
|
223
|
+
};
|
|
224
|
+
if (keys.length === 0 || !keys.every(plain)) return false;
|
|
225
|
+
const readers = [new WeakRef(node)];
|
|
226
|
+
watchers.set(value, readers);
|
|
227
|
+
own(node, value);
|
|
228
|
+
// The Wasm back end gives a value it adopts (promote) engine-backed lanes: it calls this first, and the
|
|
229
|
+
// uniforms reading it go back to being scanned.
|
|
230
|
+
Object.defineProperty(value, UNWATCH, {
|
|
231
|
+
configurable: true,
|
|
232
|
+
value: () => {
|
|
233
|
+
watchers.delete(value);
|
|
234
|
+
for (const reader of readers) {
|
|
235
|
+
const target = reader.deref();
|
|
236
|
+
const state = target === undefined ? undefined : states.get(target);
|
|
237
|
+
if (target === undefined || state === undefined || state.scanned === true) continue;
|
|
238
|
+
state.scanned = true;
|
|
239
|
+
objects.add(new WeakRef(target));
|
|
240
|
+
own(target, value);
|
|
241
|
+
}
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
for (const key of keys) {
|
|
245
|
+
let lane = (value as Record<string, unknown>)[key];
|
|
246
|
+
Object.defineProperty(value, key, {
|
|
247
|
+
configurable: true,
|
|
248
|
+
enumerable: true,
|
|
249
|
+
get: () => lane,
|
|
250
|
+
set: (next: unknown) => {
|
|
251
|
+
if (Object.is(next, lane)) return;
|
|
252
|
+
lane = next;
|
|
253
|
+
for (const reader of readers) {
|
|
254
|
+
const target = reader.deref();
|
|
255
|
+
if (target !== undefined) dirty.add(new WeakRef(target));
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
return true;
|
|
261
|
+
};
|
|
262
|
+
const track = (node: TNode, state: IUniformState, value: unknown) => {
|
|
263
|
+
if (typeof value !== "object" || value === null) return;
|
|
264
|
+
dirty.add(new WeakRef(node)); // its first sync writes it, as a scan did
|
|
265
|
+
if (state.scanned !== true && !watch(node, value)) {
|
|
266
|
+
state.scanned = true;
|
|
267
|
+
objects.add(new WeakRef(node));
|
|
268
|
+
own(node, value);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
147
271
|
const updating = new Set<TNode>();
|
|
148
272
|
let frameId = 0;
|
|
149
273
|
// An unchanged value is not written again: an edited Color or vector is found by its lanes, and a
|
|
@@ -172,18 +296,27 @@ export function liveUniforms<TNode extends object>(
|
|
|
172
296
|
};
|
|
173
297
|
const live = {
|
|
174
298
|
uniform(value: unknown, type?: unknown): TNode {
|
|
175
|
-
|
|
299
|
+
// A matrix uniform is made by its type name at identity, then given the matrix's elements.
|
|
300
|
+
const matrix = matrixType(value);
|
|
301
|
+
const node =
|
|
302
|
+
matrix !== undefined
|
|
303
|
+
? uniform(matrix)
|
|
304
|
+
: type === undefined
|
|
305
|
+
? uniform(value)
|
|
306
|
+
: uniform(value, type);
|
|
176
307
|
const state: IUniformState = { value, scratch: [] };
|
|
177
308
|
states.set(node, state);
|
|
309
|
+
if (matrix !== undefined) write(node, state, uniformLanes(value, state.scratch));
|
|
178
310
|
Object.defineProperty(node, "value", {
|
|
179
311
|
configurable: true,
|
|
180
312
|
get: () => state.value,
|
|
181
313
|
set: (next: unknown) => {
|
|
182
314
|
write(node, state, uniformLanes(next, state.scratch));
|
|
315
|
+
if (next !== state.value) track(node, state, next);
|
|
183
316
|
state.value = next;
|
|
184
317
|
},
|
|
185
318
|
});
|
|
186
|
-
|
|
319
|
+
track(node, state, value);
|
|
187
320
|
// three's UniformNode.onUpdate: the callback runs with the node as `this` and its result,
|
|
188
321
|
// unless undefined, becomes the value. RENDER runs once per sync (one per render call); FRAME
|
|
189
322
|
// runs once per renderer frame. A post chain renders about ten passes a frame (Lantern Vale), and
|
|
@@ -304,6 +437,15 @@ export function liveUniforms<TNode extends object>(
|
|
|
304
437
|
} else next = state.update(frame);
|
|
305
438
|
if (next !== undefined) (node as { value: unknown }).value = next;
|
|
306
439
|
}
|
|
440
|
+
// Deleted only once written: a sink that throws leaves the rest to retry at the next sync.
|
|
441
|
+
for (const ref of dirty) {
|
|
442
|
+
const node = ref.deref();
|
|
443
|
+
if (node !== undefined) {
|
|
444
|
+
const state = states.get(node);
|
|
445
|
+
if (state !== undefined) write(node, state, uniformLanes(state.value, state.scratch));
|
|
446
|
+
}
|
|
447
|
+
dirty.delete(ref);
|
|
448
|
+
}
|
|
307
449
|
for (const ref of objects) {
|
|
308
450
|
const node = ref.deref();
|
|
309
451
|
if (node === undefined) {
|
package/src/update-ranges.ts
CHANGED
|
@@ -22,6 +22,22 @@ const MUTATORS = new Set([
|
|
|
22
22
|
"copyWithin",
|
|
23
23
|
]);
|
|
24
24
|
|
|
25
|
+
const UPLOAD_RANGES = Symbol("tnUploadRanges");
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The `{ start, count }` element ranges the attribute's next upload sends, or none for the whole array,
|
|
29
|
+
* as three's `WebGPUAttributeUtils.updateAttribute` reads `updateRanges`. Malformed engine JSON throws.
|
|
30
|
+
*/
|
|
31
|
+
export function uploadRanges(attribute: object): readonly IRange[] {
|
|
32
|
+
const read = Reflect.get(attribute, UPLOAD_RANGES) as (() => string) | undefined;
|
|
33
|
+
const json = read?.call(attribute) ?? "[]";
|
|
34
|
+
if (json === "[]") return [];
|
|
35
|
+
const ranges = JSON.parse(json) as unknown;
|
|
36
|
+
if (!Array.isArray(ranges))
|
|
37
|
+
throw new TypeError(`TN_NATIVE_UNSUPPORTED updateRanges: expected a JSON array, got ${json}`);
|
|
38
|
+
return ranges as IRange[];
|
|
39
|
+
}
|
|
40
|
+
|
|
25
41
|
export function withUpdateRanges(prototype: object): void {
|
|
26
42
|
const read = Object.getOwnPropertyDescriptor(prototype, "updateRanges")?.get;
|
|
27
43
|
const proto = prototype as {
|
|
@@ -43,6 +59,13 @@ export function withUpdateRanges(prototype: object): void {
|
|
|
43
59
|
emptied.delete(this);
|
|
44
60
|
return result;
|
|
45
61
|
};
|
|
62
|
+
// The ranges the engine's next upload sends, as one getter read: no proxy, no parse when empty.
|
|
63
|
+
Object.defineProperty(prototype, UPLOAD_RANGES, {
|
|
64
|
+
configurable: true,
|
|
65
|
+
value(this: object): string {
|
|
66
|
+
return emptied.has(this) ? "[]" : String(read.call(this));
|
|
67
|
+
},
|
|
68
|
+
});
|
|
46
69
|
Object.defineProperty(prototype, "updateRanges", {
|
|
47
70
|
configurable: true,
|
|
48
71
|
get(this: object) {
|