@threenative/three-native 0.3.6 → 0.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/addons/gltf-loader-web.ts +4 -0
- package/src/addons/mesh-bvh.ts +16 -0
- package/src/addons/post-effects-web.ts +10 -2
- package/src/addons/post-effects.ts +126 -16
- package/src/addons/screen-space.ts +849 -0
- package/src/bridge-meter.ts +237 -0
- package/src/browser-backend.ts +1329 -183
- package/src/browser-entry.ts +90 -11
- package/src/browser-math.ts +41 -8
- package/src/browser-renderer.ts +592 -36
- package/src/browser-tsl.ts +167 -10
- package/src/debug-telemetry.ts +115 -0
- package/src/fn-builder.ts +30 -0
- package/src/gpu-stages.ts +72 -0
- package/src/pass-node.ts +94 -4
- package/src/refused.ts +4 -2
- package/src/render-target.ts +12 -8
- package/src/renderer-utils.ts +65 -0
- package/src/rtt.ts +9 -1
- package/src/shadow-node.ts +192 -0
- package/src/sphere-reflection-capture.ts +123 -0
- package/src/storage.ts +37 -0
- package/src/temp-node.ts +141 -0
- package/src/texture-sources.ts +30 -8
- package/src/tone-mapping.ts +33 -0
- package/src/tsl-uniforms.ts +178 -25
- package/src/update-ranges.ts +33 -9
package/src/temp-node.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { TSL_NODE } from "./browser-backend.js";
|
|
2
|
+
|
|
3
|
+
// three r185: src/nodes/core/{TempNode,Node,NodeFrame}.js; shader temporaries stay in the engine.
|
|
4
|
+
export const NodeUpdateType = Object.freeze({
|
|
5
|
+
// quality-allow: three r185 fixes this public enum key
|
|
6
|
+
// biome-ignore lint/style/useNamingConvention: three r185 public enum key.
|
|
7
|
+
NONE: "none",
|
|
8
|
+
// quality-allow: three r185 fixes this public enum key
|
|
9
|
+
// biome-ignore lint/style/useNamingConvention: three r185 public enum key.
|
|
10
|
+
FRAME: "frame",
|
|
11
|
+
// quality-allow: three r185 fixes this public enum key
|
|
12
|
+
// biome-ignore lint/style/useNamingConvention: three r185 public enum key.
|
|
13
|
+
RENDER: "render",
|
|
14
|
+
// quality-allow: three r185 fixes this public enum key
|
|
15
|
+
// biome-ignore lint/style/useNamingConvention: three r185 public enum key.
|
|
16
|
+
OBJECT: "object",
|
|
17
|
+
});
|
|
18
|
+
export interface INodeFrame {
|
|
19
|
+
renderer?: object;
|
|
20
|
+
frameId?: number;
|
|
21
|
+
camera?: unknown;
|
|
22
|
+
scene?: unknown;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* three's TempNode over a back end's node `prototype`. `key` is the property the back end reads a
|
|
27
|
+
* node through and `read` answers it from the setup output: the Wasm runtime's integer handle by
|
|
28
|
+
* default, the engine node itself on the V8 player (its TSL adapter reads `threenative.tslNode`).
|
|
29
|
+
*/
|
|
30
|
+
export function defineTempNode(
|
|
31
|
+
prototype: object,
|
|
32
|
+
key: symbol = TSL_NODE,
|
|
33
|
+
read: (output: object) => unknown = (output) => {
|
|
34
|
+
const id = (output as { readonly [TSL_NODE]?: unknown })[TSL_NODE];
|
|
35
|
+
return Number.isInteger(id) ? id : undefined;
|
|
36
|
+
},
|
|
37
|
+
) {
|
|
38
|
+
// Consumed graphs live in C++; retain their hooks until the owner disposes them.
|
|
39
|
+
const active = new Set<TempNode>();
|
|
40
|
+
const frames = new WeakMap<
|
|
41
|
+
object,
|
|
42
|
+
{ frameId: number; time: number; deltaTime: number; last: number; updating: boolean }
|
|
43
|
+
>();
|
|
44
|
+
class TempNode {
|
|
45
|
+
readonly isNode = true;
|
|
46
|
+
readonly isTempNode = true;
|
|
47
|
+
updateBeforeType: string = NodeUpdateType.NONE;
|
|
48
|
+
#output?: object;
|
|
49
|
+
#building = false;
|
|
50
|
+
#disposed = false;
|
|
51
|
+
#frames = new WeakMap<object, number>();
|
|
52
|
+
constructor(readonly nodeType: string | null = null) {}
|
|
53
|
+
setup(_builder: unknown): unknown {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
updateBefore(_frame: unknown): unknown {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
get [key](): unknown {
|
|
60
|
+
if (this.#disposed)
|
|
61
|
+
throw new Error("TN_NATIVE_TEMPNODE_DISPOSED: a disposed node cannot be consumed");
|
|
62
|
+
if (this.#building) throw new Error("TN_NATIVE_TEMPNODE_SETUP: cyclic setup output");
|
|
63
|
+
if (this.#output === undefined) {
|
|
64
|
+
this.#building = true;
|
|
65
|
+
try {
|
|
66
|
+
const properties = new WeakMap<object, object>();
|
|
67
|
+
const output = this.setup({
|
|
68
|
+
getNodeProperties(node: object) {
|
|
69
|
+
if (!properties.has(node)) properties.set(node, {});
|
|
70
|
+
return properties.get(node);
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
// Resolve while building to diagnose cycles instead of overflowing the JS stack.
|
|
74
|
+
if (typeof output !== "object" || output === null || read(output) === undefined)
|
|
75
|
+
throw new Error("TN_NATIVE_TEMPNODE_SETUP: setup(builder) must return a TSL node");
|
|
76
|
+
this.#output = output;
|
|
77
|
+
active.add(this);
|
|
78
|
+
} finally {
|
|
79
|
+
this.#building = false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return read(this.#output);
|
|
83
|
+
}
|
|
84
|
+
run(
|
|
85
|
+
frame: INodeFrame & { renderer: object; frameId: number; time: number; deltaTime: number },
|
|
86
|
+
): void {
|
|
87
|
+
const type = this.updateBeforeType;
|
|
88
|
+
if (type === NodeUpdateType.NONE) return;
|
|
89
|
+
if (type !== NodeUpdateType.FRAME)
|
|
90
|
+
throw new Error(
|
|
91
|
+
`TN_NATIVE_TEMPNODE_UPDATE_TYPE: '${type}' is unsupported; use NodeUpdateType.FRAME or NONE`,
|
|
92
|
+
);
|
|
93
|
+
if (this.#frames.get(frame.renderer) === frame.frameId) return;
|
|
94
|
+
const previous = this.#frames.get(frame.renderer);
|
|
95
|
+
this.#frames.set(frame.renderer, frame.frameId);
|
|
96
|
+
if (this.updateBefore(frame) === false) {
|
|
97
|
+
if (previous === undefined) this.#frames.delete(frame.renderer);
|
|
98
|
+
else this.#frames.set(frame.renderer, previous);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
dispose(): void {
|
|
102
|
+
this.#disposed = true;
|
|
103
|
+
active.delete(this);
|
|
104
|
+
this.#output = undefined;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
Object.setPrototypeOf(TempNode.prototype, prototype);
|
|
108
|
+
return {
|
|
109
|
+
TempNode,
|
|
110
|
+
update(frame?: INodeFrame) {
|
|
111
|
+
if (!frame?.renderer || frame.frameId === undefined) return;
|
|
112
|
+
const renderer = frame.renderer;
|
|
113
|
+
let state = frames.get(renderer);
|
|
114
|
+
if (state?.updating) return;
|
|
115
|
+
const now = performance.now() / 1000;
|
|
116
|
+
if (!state) {
|
|
117
|
+
state = { frameId: frame.frameId, time: 0, deltaTime: 0, last: now, updating: false };
|
|
118
|
+
frames.set(renderer, state);
|
|
119
|
+
}
|
|
120
|
+
if (state.frameId !== frame.frameId) {
|
|
121
|
+
state.deltaTime = now - state.last;
|
|
122
|
+
state.time += state.deltaTime;
|
|
123
|
+
state.last = now;
|
|
124
|
+
state.frameId = frame.frameId;
|
|
125
|
+
}
|
|
126
|
+
state.updating = true;
|
|
127
|
+
try {
|
|
128
|
+
for (const node of active)
|
|
129
|
+
node.run({
|
|
130
|
+
...frame,
|
|
131
|
+
renderer,
|
|
132
|
+
frameId: frame.frameId,
|
|
133
|
+
time: state.time,
|
|
134
|
+
deltaTime: state.deltaTime,
|
|
135
|
+
});
|
|
136
|
+
} finally {
|
|
137
|
+
state.updating = false;
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
}
|
package/src/texture-sources.ts
CHANGED
|
@@ -5,7 +5,12 @@
|
|
|
5
5
|
* before an upload. The V8 player's equivalent is `runtime-native/src/engine/player/core-textures.mjs`;
|
|
6
6
|
* the browser decodes images itself, so no bitmap needs the engine decoder here.
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
type EngineValue,
|
|
10
|
+
type IBrowserRuntime,
|
|
11
|
+
type IEngineRef,
|
|
12
|
+
engineRef,
|
|
13
|
+
} from "./browser-backend.js";
|
|
9
14
|
|
|
10
15
|
type EngineClass = new (...args: unknown[]) => object;
|
|
11
16
|
|
|
@@ -72,6 +77,16 @@ export function defineTextureSources(
|
|
|
72
77
|
if (BaseCanvas === undefined) throw new TypeError("TN_BROWSER_UNBOUND: CanvasTexture");
|
|
73
78
|
const sources = new WeakMap<object, unknown>();
|
|
74
79
|
const prototype = BaseData.prototype as object;
|
|
80
|
+
for (const Native of [BaseTexture, BaseData, BaseCanvas, BaseVolume]) {
|
|
81
|
+
if (Native === undefined) continue;
|
|
82
|
+
const target = Native.prototype as { clone(): object };
|
|
83
|
+
const clone = target.clone;
|
|
84
|
+
target.clone = function (this: object): object {
|
|
85
|
+
const result = clone.call(this);
|
|
86
|
+
if (sources.has(this)) sources.set(result, sources.get(this));
|
|
87
|
+
return result;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
75
90
|
/** `image` is the JS source; `needsUpdate` re-sends its pixels, as three re-reads `texture.image`. */
|
|
76
91
|
const followSource = (target: object, name: string): void => {
|
|
77
92
|
const needsUpdate = Object.getOwnPropertyDescriptor(target, "needsUpdate")?.set;
|
|
@@ -90,12 +105,16 @@ export function defineTextureSources(
|
|
|
90
105
|
configurable: true,
|
|
91
106
|
set(this: object, value: unknown) {
|
|
92
107
|
const image = value ? sources.get(this) : undefined;
|
|
93
|
-
if (image !== undefined)
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
108
|
+
if (image !== undefined) {
|
|
109
|
+
const data = pixelsOf(image).data;
|
|
110
|
+
const pixels =
|
|
111
|
+
runtime.typedArrays === true &&
|
|
112
|
+
ArrayBuffer.isView(data) &&
|
|
113
|
+
!(data instanceof DataView)
|
|
114
|
+
? (data as EngineValue)
|
|
115
|
+
: Array.from(data);
|
|
116
|
+
runtime.set(engineRef(this) as IEngineRef, "image.data", pixels);
|
|
117
|
+
}
|
|
99
118
|
needsUpdate.call(this, value);
|
|
100
119
|
},
|
|
101
120
|
},
|
|
@@ -140,9 +159,12 @@ export function defineTextureSources(
|
|
|
140
159
|
depth = 1,
|
|
141
160
|
) {
|
|
142
161
|
if (BaseVolume === undefined) throw new TypeError("TN_BROWSER_UNBOUND: Data3DTexture");
|
|
162
|
+
// The engine's constructor takes bytes or floats. A Uint16Array holds half-float bits, so the engine
|
|
163
|
+
// gets zero floats of the same size and the bits go in on needsUpdate, after `type` says HalfFloat.
|
|
164
|
+
const bits = data instanceof Uint16Array;
|
|
143
165
|
const texture = Reflect.construct(
|
|
144
166
|
BaseVolume,
|
|
145
|
-
[data ?? undefined, width, height, depth],
|
|
167
|
+
[bits ? new Float32Array(data.length) : (data ?? undefined), width, height, depth],
|
|
146
168
|
new.target ?? Data3DTexture,
|
|
147
169
|
);
|
|
148
170
|
sources.set(texture, { data: data ?? null, width, height, depth });
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** three's NodeLibrary tone functions; the engine compiles the game's curve at its output stage. */
|
|
2
|
+
export function defineToneMappingLibrary(
|
|
3
|
+
inputs: () => [unknown, unknown],
|
|
4
|
+
install: (node: unknown) => void,
|
|
5
|
+
) {
|
|
6
|
+
const functions = new Map<number, (color: never, exposure: never) => unknown>();
|
|
7
|
+
let installed: unknown;
|
|
8
|
+
return {
|
|
9
|
+
addToneMapping(fn: (color: never, exposure: never) => unknown, mode: number): void {
|
|
10
|
+
if (typeof fn !== "function" || !Number.isInteger(mode))
|
|
11
|
+
throw new TypeError(
|
|
12
|
+
"TN_NATIVE_TONEMAPPING_REGISTRATION: expected a function and integer mode",
|
|
13
|
+
);
|
|
14
|
+
if (!functions.has(mode)) functions.set(mode, fn);
|
|
15
|
+
},
|
|
16
|
+
getToneMappingFunction: (mode: number) => functions.get(mode) ?? null,
|
|
17
|
+
apply(mode: number): unknown {
|
|
18
|
+
if (mode !== 5) return null;
|
|
19
|
+
const fn = functions.get(mode);
|
|
20
|
+
if (!fn)
|
|
21
|
+
throw new Error(
|
|
22
|
+
"TN_NATIVE_CUSTOM_TONEMAPPING_MISSING: register a TSL tone-mapping function before using mode 5",
|
|
23
|
+
);
|
|
24
|
+
if (installed === undefined) {
|
|
25
|
+
const [color, exposure] = inputs();
|
|
26
|
+
const node = fn(color as never, exposure as never);
|
|
27
|
+
install(node);
|
|
28
|
+
installed = node;
|
|
29
|
+
}
|
|
30
|
+
return installed;
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
package/src/tsl-uniforms.ts
CHANGED
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
* lanes do not fit the uniform's type. Either way the engine keeps the program.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
+
import { callerOf } from "./bridge-meter.js";
|
|
10
|
+
import { debugRequested } from "./debug-telemetry.js";
|
|
11
|
+
|
|
9
12
|
/** three's update `frame`: the rendering camera and scene, with the frame counter. */
|
|
10
13
|
export interface IUpdateFrame {
|
|
11
14
|
frameId: number;
|
|
@@ -17,17 +20,52 @@ interface IUniformState {
|
|
|
17
20
|
value: unknown;
|
|
18
21
|
/** three's `node.update`, set by onRenderUpdate/onFrameUpdate; its result becomes `.value`. */
|
|
19
22
|
update?: (frame: IUpdateFrame) => unknown;
|
|
23
|
+
/** three's update type: "frame" runs once per renderer frame, "render" once per render call. */
|
|
24
|
+
updateType?: string;
|
|
25
|
+
/** Bounded debug-only callback registration site. */
|
|
26
|
+
debugSource?: string;
|
|
27
|
+
/** The renderer frame a "frame" update last ran in. */
|
|
28
|
+
ranFrame?: number;
|
|
29
|
+
/** The lanes last written to the engine: an unchanged value is not written again. */
|
|
30
|
+
lanes?: readonly number[];
|
|
31
|
+
/** Reused extraction storage, never aliased with `lanes`: the sink reads it before it is refilled. */
|
|
32
|
+
scratch?: number[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function sameLanes(a: readonly number[] | undefined, b: readonly number[]): boolean {
|
|
36
|
+
if (a === undefined || a.length !== b.length) return false;
|
|
37
|
+
for (let i = 0; i < b.length; i++) if (!Object.is(a[i], b[i])) return false;
|
|
38
|
+
return true;
|
|
20
39
|
}
|
|
21
40
|
|
|
22
|
-
/**
|
|
23
|
-
|
|
24
|
-
|
|
41
|
+
/**
|
|
42
|
+
* One float per lane: a number, a Color's r, g, b, or a VectorN's x, y, z, w. `out`, when given,
|
|
43
|
+
* receives the lanes and is returned, so a uniform can refill one bounded scratch array per sync.
|
|
44
|
+
*/
|
|
45
|
+
export function uniformLanes(value: unknown, out?: number[]): number[] {
|
|
46
|
+
const lanes = out ?? [];
|
|
47
|
+
if (typeof value === "number") {
|
|
48
|
+
lanes.length = 1;
|
|
49
|
+
lanes[0] = value;
|
|
50
|
+
return lanes;
|
|
51
|
+
}
|
|
25
52
|
if (typeof value === "object" && value !== null) {
|
|
26
53
|
const v = value as Record<string, unknown>;
|
|
27
|
-
if (typeof v.x === "number" && typeof v.y === "number")
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
54
|
+
if (typeof v.x === "number" && typeof v.y === "number") {
|
|
55
|
+
lanes.length = 2;
|
|
56
|
+
lanes[0] = v.x;
|
|
57
|
+
lanes[1] = v.y;
|
|
58
|
+
if (typeof v.z === "number") lanes[lanes.length] = v.z;
|
|
59
|
+
if (typeof v.w === "number") lanes[lanes.length] = v.w;
|
|
60
|
+
return lanes;
|
|
61
|
+
}
|
|
62
|
+
if (typeof v.r === "number" && typeof v.g === "number" && typeof v.b === "number") {
|
|
63
|
+
lanes.length = 3;
|
|
64
|
+
lanes[0] = v.r;
|
|
65
|
+
lanes[1] = v.g;
|
|
66
|
+
lanes[2] = v.b;
|
|
67
|
+
return lanes;
|
|
68
|
+
}
|
|
31
69
|
}
|
|
32
70
|
throw new TypeError(`TN_TSL_UNIFORM_VALUE: ${String(value)} is not a number, Color or vector`);
|
|
33
71
|
}
|
|
@@ -61,7 +99,7 @@ function referenceValue(property: string, type: string, object: unknown): unknow
|
|
|
61
99
|
* Wraps a back end's `uniform` so each uniform node it returns answers `.value` (an accessor on the
|
|
62
100
|
* node itself: a V8 node has no prototype of its own to carry it) and three's update hooks. `setValues`
|
|
63
101
|
* writes a node's lanes into the engine. `sync()` runs once per render: the update callbacks first,
|
|
64
|
-
* then the edited objects' lanes.
|
|
102
|
+
* then the edited objects' lanes, each written only when its lanes changed.
|
|
65
103
|
*/
|
|
66
104
|
export function liveUniforms<TNode extends object>(
|
|
67
105
|
uniform: (value: unknown, type?: unknown) => TNode,
|
|
@@ -73,8 +111,34 @@ export function liveUniforms<TNode extends object>(
|
|
|
73
111
|
type?: string,
|
|
74
112
|
) => { array: unknown[]; element(index: unknown): TNode };
|
|
75
113
|
reference(property: string, type: string, object?: unknown): TNode;
|
|
76
|
-
sync(frame?: { camera?: unknown; scene?: unknown }): void;
|
|
114
|
+
sync(frame?: { camera?: unknown; scene?: unknown; frameId?: number }): void;
|
|
77
115
|
} {
|
|
116
|
+
const debug = debugRequested();
|
|
117
|
+
const host = globalThis as {
|
|
118
|
+
__tnEngineCost?: Record<string, number>;
|
|
119
|
+
__tnDebugSources?: Record<
|
|
120
|
+
string,
|
|
121
|
+
{ area: string; caller: string; lever: string; unit?: string }
|
|
122
|
+
>;
|
|
123
|
+
};
|
|
124
|
+
if (debug) {
|
|
125
|
+
host.__tnEngineCost ??= {};
|
|
126
|
+
host.__tnDebugSources ??= {};
|
|
127
|
+
}
|
|
128
|
+
const totals = debug ? host.__tnEngineCost : undefined;
|
|
129
|
+
const sources = debug ? host.__tnDebugSources : undefined;
|
|
130
|
+
let callbackSites = 0;
|
|
131
|
+
const count = (name: string, value = 1, caller = "tsl-uniforms.ts:liveUniforms.sync") => {
|
|
132
|
+
if (totals === undefined || sources === undefined) return;
|
|
133
|
+
totals[name] = (totals[name] ?? 0) + value;
|
|
134
|
+
sources[name] ??= {
|
|
135
|
+
area: "uniform",
|
|
136
|
+
caller,
|
|
137
|
+
...(name.endsWith("Ms") ? { unit: "ms" } : {}),
|
|
138
|
+
lever:
|
|
139
|
+
"PERFORMANCE-LEARNINGS.md#2026-10-28--post-stages-re-run-the-scene-materials-render-callbacks--open-sized-not-cut",
|
|
140
|
+
};
|
|
141
|
+
};
|
|
78
142
|
const states = new WeakMap<object, IUniformState>();
|
|
79
143
|
const objects = new Set<WeakRef<TNode>>();
|
|
80
144
|
// Strong: a hook's node is chained into a material and dropped, so the wrapper is all JS holds, and a
|
|
@@ -82,23 +146,48 @@ export function liveUniforms<TNode extends object>(
|
|
|
82
146
|
// of such a node waits too. Release with the owning material if a game builds hooked uniforms in a loop.
|
|
83
147
|
const updating = new Set<TNode>();
|
|
84
148
|
let frameId = 0;
|
|
149
|
+
// An unchanged value is not written again: an edited Color or vector is found by its lanes, and a
|
|
150
|
+
// hook that returns the same number every render costs no engine write.
|
|
151
|
+
const write = (node: TNode, state: IUniformState, lanes: readonly number[]) => {
|
|
152
|
+
if (debug) {
|
|
153
|
+
count("uniformWriteAttempts");
|
|
154
|
+
count("uniformLaneWriteAttempts", lanes.length);
|
|
155
|
+
}
|
|
156
|
+
if (sameLanes(state.lanes, lanes)) {
|
|
157
|
+
if (debug) {
|
|
158
|
+
count("uniformUnchangedWriteAttempts");
|
|
159
|
+
count("uniformUnchangedLaneWriteAttempts", lanes.length);
|
|
160
|
+
}
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
// A snapshot for the sink and the state: `lanes` is the state's scratch, refilled by the next
|
|
164
|
+
// extraction, so a sink that keeps its argument must not see later values.
|
|
165
|
+
const written = lanes.slice();
|
|
166
|
+
setValues(node, written);
|
|
167
|
+
if (debug) {
|
|
168
|
+
count("uniformWrites");
|
|
169
|
+
count("uniformLaneWrites", lanes.length);
|
|
170
|
+
}
|
|
171
|
+
state.lanes = written;
|
|
172
|
+
};
|
|
85
173
|
const live = {
|
|
86
174
|
uniform(value: unknown, type?: unknown): TNode {
|
|
87
175
|
const node = type === undefined ? uniform(value) : uniform(value, type);
|
|
88
|
-
const state: IUniformState = { value };
|
|
176
|
+
const state: IUniformState = { value, scratch: [] };
|
|
89
177
|
states.set(node, state);
|
|
90
178
|
Object.defineProperty(node, "value", {
|
|
91
179
|
configurable: true,
|
|
92
180
|
get: () => state.value,
|
|
93
181
|
set: (next: unknown) => {
|
|
94
|
-
|
|
182
|
+
write(node, state, uniformLanes(next, state.scratch));
|
|
95
183
|
state.value = next;
|
|
96
184
|
},
|
|
97
185
|
});
|
|
98
186
|
if (typeof value === "object" && value !== null) objects.add(new WeakRef(node));
|
|
99
187
|
// three's UniformNode.onUpdate: the callback runs with the node as `this` and its result,
|
|
100
|
-
// unless undefined, becomes the value.
|
|
101
|
-
// per
|
|
188
|
+
// unless undefined, becomes the value. RENDER runs once per sync (one per render call); FRAME
|
|
189
|
+
// runs once per renderer frame. A post chain renders about ten passes a frame (Lantern Vale), and
|
|
190
|
+
// running every FRAME callback in each of them cost a tenth of the frame.
|
|
102
191
|
const onUpdate = (
|
|
103
192
|
callback: (this: TNode, frame: unknown, node: TNode) => unknown,
|
|
104
193
|
updateType: string,
|
|
@@ -109,6 +198,15 @@ export function liveUniforms<TNode extends object>(
|
|
|
109
198
|
);
|
|
110
199
|
if (state.update === undefined) updating.add(node);
|
|
111
200
|
state.update = (frame) => callback.call(node, frame, node);
|
|
201
|
+
state.updateType = updateType;
|
|
202
|
+
if (debug) {
|
|
203
|
+
state.debugSource = undefined;
|
|
204
|
+
if (callbackSites < 64) {
|
|
205
|
+
state.debugSource = callerOf(new Error().stack);
|
|
206
|
+
callbackSites++;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
state.ranFrame = undefined;
|
|
112
210
|
return node;
|
|
113
211
|
};
|
|
114
212
|
Object.defineProperties(node, {
|
|
@@ -174,25 +272,80 @@ export function liveUniforms<TNode extends object>(
|
|
|
174
272
|
},
|
|
175
273
|
};
|
|
176
274
|
},
|
|
177
|
-
sync(extra?: { camera?: unknown; scene?: unknown }) {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
continue;
|
|
275
|
+
sync(extra?: { camera?: unknown; scene?: unknown; frameId?: number }) {
|
|
276
|
+
// `extra.frameId` is the renderer's frame: every render, target draw and compute of one frame
|
|
277
|
+
// shares it. The callbacks see this module's own counter, as three's NodeFrame counts renders.
|
|
278
|
+
const started = debug ? performance.now() : 0;
|
|
279
|
+
if (debug) count("uniformSyncCalls");
|
|
280
|
+
try {
|
|
281
|
+
const rendererFrame = extra?.frameId;
|
|
282
|
+
const frame: IUpdateFrame = { ...extra, frameId: ++frameId };
|
|
283
|
+
for (const node of updating) {
|
|
284
|
+
const state = states.get(node);
|
|
285
|
+
if (state?.update === undefined) continue;
|
|
286
|
+
if (state.updateType === "frame" && rendererFrame !== undefined) {
|
|
287
|
+
if (state.ranFrame === rendererFrame) continue;
|
|
288
|
+
state.ranFrame = rendererFrame;
|
|
289
|
+
}
|
|
290
|
+
let next: unknown;
|
|
291
|
+
if (debug) {
|
|
292
|
+
const callbackStart = performance.now();
|
|
293
|
+
count(
|
|
294
|
+
state.updateType === "frame" ? "uniformFrameCallbacks" : "uniformRenderCallbacks",
|
|
295
|
+
);
|
|
296
|
+
try {
|
|
297
|
+
next = state.update(frame);
|
|
298
|
+
} finally {
|
|
299
|
+
const elapsed = performance.now() - callbackStart;
|
|
300
|
+
count("uniformCallbackMs", elapsed);
|
|
301
|
+
if (state.debugSource !== undefined)
|
|
302
|
+
count(`uniformCallback:${state.debugSource}Ms`, elapsed, state.debugSource);
|
|
303
|
+
}
|
|
304
|
+
} else next = state.update(frame);
|
|
305
|
+
if (next !== undefined) (node as { value: unknown }).value = next;
|
|
306
|
+
}
|
|
307
|
+
for (const ref of objects) {
|
|
308
|
+
const node = ref.deref();
|
|
309
|
+
if (node === undefined) {
|
|
310
|
+
objects.delete(ref);
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
const state = states.get(node);
|
|
314
|
+
if (state !== undefined) write(node, state, uniformLanes(state.value, state.scratch));
|
|
188
315
|
}
|
|
189
|
-
|
|
316
|
+
} finally {
|
|
317
|
+
if (debug) count("uniformSyncMs", performance.now() - started);
|
|
190
318
|
}
|
|
191
319
|
},
|
|
192
320
|
};
|
|
193
321
|
return live;
|
|
194
322
|
}
|
|
195
323
|
|
|
324
|
+
/**
|
|
325
|
+
* three's `uniform(false)` (a boolean toggle such as the exposure meter's `enabled`) over a back end's numeric
|
|
326
|
+
* uniform. The ABI stores numeric lanes, so the boolean is the live float compared with zero (`notZero`); its
|
|
327
|
+
* `.value` reads and writes booleans. Any other value goes to `uniform` as before.
|
|
328
|
+
*/
|
|
329
|
+
export function booleanUniforms<TNode extends object>(
|
|
330
|
+
uniform: (value: unknown, type?: unknown) => TNode,
|
|
331
|
+
notZero: (scalar: TNode) => TNode,
|
|
332
|
+
): (value: unknown, type?: unknown) => TNode {
|
|
333
|
+
return (value, type) => {
|
|
334
|
+
if (typeof value !== "boolean") return uniform(value, type);
|
|
335
|
+
if (type !== undefined && type !== "bool")
|
|
336
|
+
throw new TypeError("TN_TSL_UNIFORM_VALUE: a boolean uniform requires bool type");
|
|
337
|
+
const scalar = uniform(Number(value)) as TNode & { value: number };
|
|
338
|
+
return Object.defineProperty(notZero(scalar), "value", {
|
|
339
|
+
get: () => Boolean(scalar.value),
|
|
340
|
+
set: (next: unknown) => {
|
|
341
|
+
if (typeof next !== "boolean")
|
|
342
|
+
throw new TypeError("TN_TSL_UNIFORM_VALUE: expected a boolean");
|
|
343
|
+
scalar.value = Number(next);
|
|
344
|
+
},
|
|
345
|
+
});
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
|
|
196
349
|
/** The three classes a constant's value is made of. */
|
|
197
350
|
export type ConstClasses = Readonly<
|
|
198
351
|
Record<"Vector2" | "Vector3" | "Vector4" | "Color", new (...lanes: number[]) => object>
|
package/src/update-ranges.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `BufferAttribute.updateRanges` as three's live array of `{ start, count }` records, on the web facade.
|
|
3
3
|
* The engine hands the getter canonical JSON and takes ranges through `addUpdateRange` and
|
|
4
|
-
* `clearUpdateRanges
|
|
5
|
-
*
|
|
4
|
+
* `clearUpdateRanges`. Push appends validated records directly; other mutations replay the array.
|
|
5
|
+
* An explicit clear supplies the next empty snapshot; other reads still observe native upload clears.
|
|
6
|
+
* The V8 adapter installs the same surface (adapters/v8/adapter.cpp, rangesSource).
|
|
6
7
|
*/
|
|
7
8
|
interface IRange {
|
|
8
9
|
start: number;
|
|
@@ -24,13 +25,24 @@ const MUTATORS = new Set([
|
|
|
24
25
|
export function withUpdateRanges(prototype: object): void {
|
|
25
26
|
const read = Object.getOwnPropertyDescriptor(prototype, "updateRanges")?.get;
|
|
26
27
|
const proto = prototype as {
|
|
27
|
-
addUpdateRange?: (start: number, count: number) =>
|
|
28
|
-
clearUpdateRanges?: () =>
|
|
28
|
+
addUpdateRange?: (start: number, count: number) => unknown;
|
|
29
|
+
clearUpdateRanges?: () => unknown;
|
|
29
30
|
};
|
|
30
31
|
const clear = proto.clearUpdateRanges;
|
|
31
32
|
const add = proto.addUpdateRange;
|
|
32
33
|
if (read === undefined || clear === undefined || add === undefined) return;
|
|
33
34
|
const proxies = new WeakMap<object, IRange[]>();
|
|
35
|
+
const emptied = new WeakSet<object>();
|
|
36
|
+
proto.clearUpdateRanges = function (this: object) {
|
|
37
|
+
const result = clear.call(this);
|
|
38
|
+
emptied.add(this);
|
|
39
|
+
return result;
|
|
40
|
+
};
|
|
41
|
+
proto.addUpdateRange = function (this: object, start: number, count: number) {
|
|
42
|
+
const result = add.call(this, start, count);
|
|
43
|
+
emptied.delete(this);
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
34
46
|
Object.defineProperty(prototype, "updateRanges", {
|
|
35
47
|
configurable: true,
|
|
36
48
|
get(this: object) {
|
|
@@ -39,13 +51,13 @@ export function withUpdateRanges(prototype: object): void {
|
|
|
39
51
|
const target: IRange[] = [];
|
|
40
52
|
let snapshot: unknown;
|
|
41
53
|
const refresh = () => {
|
|
42
|
-
const current = read.call(this) as unknown;
|
|
54
|
+
const current = emptied.delete(this) ? "[]" : (read.call(this) as unknown);
|
|
43
55
|
if (current === snapshot) return;
|
|
44
56
|
target.splice(0, target.length, ...(JSON.parse(String(current)) as IRange[]));
|
|
45
57
|
snapshot = current;
|
|
46
58
|
};
|
|
47
|
-
const
|
|
48
|
-
for (const range of
|
|
59
|
+
const validate = (ranges: readonly IRange[]) => {
|
|
60
|
+
for (const range of ranges)
|
|
49
61
|
if (
|
|
50
62
|
!range ||
|
|
51
63
|
!Number.isSafeInteger(range.start) ||
|
|
@@ -56,9 +68,12 @@ export function withUpdateRanges(prototype: object): void {
|
|
|
56
68
|
throw new TypeError(
|
|
57
69
|
"TN_NATIVE_UNSUPPORTED updateRanges: expected nonnegative integer start and count",
|
|
58
70
|
);
|
|
71
|
+
};
|
|
72
|
+
const write = () => {
|
|
73
|
+
validate(target);
|
|
59
74
|
clear.call(this);
|
|
60
75
|
for (const range of target) add.call(this, range.start, range.count);
|
|
61
|
-
snapshot =
|
|
76
|
+
snapshot = undefined;
|
|
62
77
|
};
|
|
63
78
|
const guarded = <T>(change: () => T): T => {
|
|
64
79
|
refresh();
|
|
@@ -70,9 +85,17 @@ export function withUpdateRanges(prototype: object): void {
|
|
|
70
85
|
throw error;
|
|
71
86
|
}
|
|
72
87
|
};
|
|
88
|
+
const owner = this;
|
|
73
89
|
const proxy: IRange[] = new Proxy(target, {
|
|
74
90
|
get(array, key, receiver) {
|
|
75
|
-
|
|
91
|
+
if (key === "push")
|
|
92
|
+
return (...ranges: IRange[]) => {
|
|
93
|
+
validate(ranges);
|
|
94
|
+
refresh();
|
|
95
|
+
for (const range of ranges) add.call(owner, range.start, range.count);
|
|
96
|
+
snapshot = undefined;
|
|
97
|
+
return target.push(...ranges);
|
|
98
|
+
};
|
|
76
99
|
if (typeof key === "string" && MUTATORS.has(key))
|
|
77
100
|
return (...args: unknown[]) =>
|
|
78
101
|
guarded(() => {
|
|
@@ -84,6 +107,7 @@ export function withUpdateRanges(prototype: object): void {
|
|
|
84
107
|
write();
|
|
85
108
|
return result === array ? proxy : result;
|
|
86
109
|
});
|
|
110
|
+
refresh();
|
|
87
111
|
return Reflect.get(array, key, receiver);
|
|
88
112
|
},
|
|
89
113
|
set(array, key, value) {
|