@threenative/three-native 0.3.12 → 0.3.15
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/ccd-ik-solver.ts +170 -0
- package/src/addons/gltf-loader-web.ts +104 -17
- package/src/addons/screen-space.ts +43 -21
- package/src/animation-utils.ts +366 -0
- package/src/box2.ts +172 -0
- package/src/bridge-meter.ts +23 -9
- package/src/browser-abi-types.ts +260 -43
- package/src/browser-attribute-sync.ts +69 -0
- package/src/browser-backend.ts +75 -8
- package/src/browser-entry.ts +51 -12
- package/src/browser-loaders.ts +12 -0
- package/src/browser-renderer.ts +194 -118
- package/src/browser-tsl.ts +93 -16
- package/src/browser-value-views.ts +18 -2
- package/src/browser-wasm-runtime.ts +72 -30
- package/src/buffer-attributes.ts +19 -0
- package/src/compressed-texture.ts +116 -0
- package/src/debug-telemetry.ts +20 -6
- package/src/edges-geometry.ts +111 -0
- package/src/generated/console-markers.ts +14 -0
- package/src/generated/web-frame-fields.ts +85 -0
- package/src/gpu-stages.ts +17 -16
- package/src/grid-helper.ts +49 -0
- package/src/object-surface.ts +49 -6
- package/src/pass-node.ts +26 -6
- package/src/render-target.ts +478 -12
- package/src/shadow-node.ts +24 -1
- package/src/storage.ts +16 -5
- package/src/texture-sources.ts +12 -6
- package/src/tsl-uniforms.ts +146 -57
- package/src/update-ranges.ts +27 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@threenative/three-native",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.15",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "The native engine's machine-readable compatibility catalog and the generators that adapt it to TypeScript declarations and the C binding ABI.",
|
|
6
6
|
"keywords": [
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `three/addons/animation/CCDIKSolver.js` as the engine back ends provide it: three's CCD solver,
|
|
3
|
+
* ported from three r187 examples/jsm/animation/CCDIKSolver.js (MIT), over whichever engine `three`
|
|
4
|
+
* names in this build, the V8 facade or the Wasm entry. Every call it makes is a bound engine method.
|
|
5
|
+
* The scratch values are per solver, not module-level, so importing the addon creates no engine object.
|
|
6
|
+
* CCDIKHelper is not ported: `createHelper` refuses by name.
|
|
7
|
+
*/
|
|
8
|
+
import { type Bone, Matrix4, Quaternion, type SkinnedMesh, Vector3 } from "three";
|
|
9
|
+
|
|
10
|
+
export interface IIkLink {
|
|
11
|
+
index: number;
|
|
12
|
+
enabled?: boolean;
|
|
13
|
+
limitation?: Vector3;
|
|
14
|
+
rotationMin?: Vector3;
|
|
15
|
+
rotationMax?: Vector3;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface IIkChain {
|
|
19
|
+
target: number;
|
|
20
|
+
effector: number;
|
|
21
|
+
links: IIkLink[];
|
|
22
|
+
iteration?: number;
|
|
23
|
+
minAngle?: number;
|
|
24
|
+
maxAngle?: number;
|
|
25
|
+
blendFactor?: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// MathUtils.euclideanModulo.
|
|
29
|
+
const modulo = (n: number, m: number) => ((n % m) + m) % m;
|
|
30
|
+
|
|
31
|
+
export class CCDIKSolver {
|
|
32
|
+
readonly #quaternion = new Quaternion();
|
|
33
|
+
readonly #targetPos = new Vector3();
|
|
34
|
+
readonly #targetVec = new Vector3();
|
|
35
|
+
readonly #effectorPos = new Vector3();
|
|
36
|
+
readonly #effectorVec = new Vector3();
|
|
37
|
+
readonly #linkPos = new Vector3();
|
|
38
|
+
readonly #invLinkQ = new Quaternion();
|
|
39
|
+
readonly #linkScale = new Vector3();
|
|
40
|
+
readonly #axis = new Vector3();
|
|
41
|
+
readonly #vector = new Vector3();
|
|
42
|
+
readonly #center = new Vector3();
|
|
43
|
+
readonly #workingQuaternion = new Quaternion();
|
|
44
|
+
readonly #initialQuaternions: Quaternion[][];
|
|
45
|
+
|
|
46
|
+
constructor(
|
|
47
|
+
readonly mesh: SkinnedMesh,
|
|
48
|
+
readonly iks: IIkChain[] = [],
|
|
49
|
+
) {
|
|
50
|
+
this.#initialQuaternions = iks.map((ik) => ik.links.map(() => new Quaternion()));
|
|
51
|
+
this.#valid();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
update(globalBlendFactor = 1.0): this {
|
|
55
|
+
for (const ik of this.iks) this.updateOne(ik, globalBlendFactor);
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
updateOne(ik: IIkChain, overrideBlend = 1.0): this {
|
|
60
|
+
const chainBlend = ik.blendFactor ?? overrideBlend;
|
|
61
|
+
const bones = this.mesh.skeleton.bones;
|
|
62
|
+
const initialQuaternions = this.#initialQuaternions[this.iks.indexOf(ik)] ?? [];
|
|
63
|
+
const effector = bones[ik.effector] as Bone;
|
|
64
|
+
const target = bones[ik.target] as Bone;
|
|
65
|
+
// three reads matrixWorld, not getWorldPosition(), which updates the world matrices first.
|
|
66
|
+
this.#targetPos.setFromMatrixPosition(target.matrixWorld);
|
|
67
|
+
const links = ik.links;
|
|
68
|
+
if (chainBlend < 1.0)
|
|
69
|
+
for (const [j, link] of links.entries())
|
|
70
|
+
initialQuaternions[j]?.copy((bones[link.index] as Bone).quaternion);
|
|
71
|
+
for (let i = 0; i < (ik.iteration ?? 1); i++) if (!this.#iterate(ik, bones, effector)) break;
|
|
72
|
+
if (chainBlend < 1.0) this.#blend(links, bones, initialQuaternions, chainBlend);
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
createHelper(): never {
|
|
77
|
+
throw new Error(
|
|
78
|
+
"TN_NATIVE_UNSUPPORTED_CCDIKHELPER: CCDIKSolver.createHelper needs three's CCDIKHelper, which the engine does not bind; draw the chain from skeleton.bones instead.",
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// One pass over the chain; false when no link turned, which ends the solve.
|
|
83
|
+
#iterate(ik: IIkChain, bones: Bone[], effector: Bone): boolean {
|
|
84
|
+
let rotated = false;
|
|
85
|
+
for (const entry of ik.links) {
|
|
86
|
+
// An off link stops the chain here.
|
|
87
|
+
if (entry.enabled === false) break;
|
|
88
|
+
if (this.#rotateLink(ik, entry, bones[entry.index] as Bone, effector)) rotated = true;
|
|
89
|
+
}
|
|
90
|
+
return rotated;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// A chain blend under 1 slerps each link from its pose before the solve toward the solved pose.
|
|
94
|
+
#blend(links: IIkLink[], bones: Bone[], initial: Quaternion[], chainBlend: number): void {
|
|
95
|
+
for (const [j, entry] of links.entries()) {
|
|
96
|
+
const link = bones[entry.index] as Bone;
|
|
97
|
+
this.#workingQuaternion.copy(initial[j] as Quaternion).slerp(link.quaternion, chainBlend);
|
|
98
|
+
link.quaternion.copy(this.#workingQuaternion);
|
|
99
|
+
link.updateMatrixWorld(true);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// One CCD step on one link: rotate it so the effector points at the target. False when the angle
|
|
104
|
+
// is too small to turn, which three skips to prevent bone vibration.
|
|
105
|
+
#rotateLink(ik: IIkChain, entry: IIkLink, link: Bone, effector: Bone): boolean {
|
|
106
|
+
link.matrixWorld.decompose(this.#linkPos, this.#invLinkQ, this.#linkScale);
|
|
107
|
+
this.#invLinkQ.invert();
|
|
108
|
+
this.#effectorPos.setFromMatrixPosition(effector.matrixWorld);
|
|
109
|
+
// Work in the link's world frame.
|
|
110
|
+
this.#effectorVec
|
|
111
|
+
.subVectors(this.#effectorPos, this.#linkPos)
|
|
112
|
+
.applyQuaternion(this.#invLinkQ)
|
|
113
|
+
.normalize();
|
|
114
|
+
this.#targetVec
|
|
115
|
+
.subVectors(this.#targetPos, this.#linkPos)
|
|
116
|
+
.applyQuaternion(this.#invLinkQ)
|
|
117
|
+
.normalize();
|
|
118
|
+
let angle = Math.acos(Math.min(1, Math.max(-1, this.#targetVec.dot(this.#effectorVec))));
|
|
119
|
+
if (angle < 1e-5) return false;
|
|
120
|
+
if (ik.minAngle !== undefined && angle < ik.minAngle) angle = ik.minAngle;
|
|
121
|
+
if (ik.maxAngle !== undefined && angle > ik.maxAngle) angle = ik.maxAngle;
|
|
122
|
+
this.#axis.crossVectors(this.#effectorVec, this.#targetVec).normalize();
|
|
123
|
+
this.#quaternion.setFromAxisAngle(this.#axis, angle);
|
|
124
|
+
link.quaternion.multiply(this.#quaternion);
|
|
125
|
+
this.#limit(entry, link);
|
|
126
|
+
link.updateMatrixWorld(true);
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// three's link limits: a hinge axis, then Euler angle bounds.
|
|
131
|
+
#limit(entry: IIkLink, link: Bone): void {
|
|
132
|
+
const { limitation, rotationMin, rotationMax } = entry;
|
|
133
|
+
if (limitation !== undefined) {
|
|
134
|
+
const q = link.quaternion;
|
|
135
|
+
const c = Math.min(q.w, 1.0);
|
|
136
|
+
// Keep the sign of the rotation along the limitation axis; else a negative turn mirrors.
|
|
137
|
+
const dot = q.x * limitation.x + q.y * limitation.y + q.z * limitation.z;
|
|
138
|
+
const c2 = (dot < 0 ? -1 : 1) * Math.sqrt(1 - c * c);
|
|
139
|
+
q.set(limitation.x * c2, limitation.y * c2, limitation.z * c2, c);
|
|
140
|
+
}
|
|
141
|
+
if (rotationMin !== undefined || rotationMax !== undefined) {
|
|
142
|
+
const v = this.#vector.setFromEuler(link.rotation);
|
|
143
|
+
if (rotationMin !== undefined && rotationMax !== undefined) {
|
|
144
|
+
// Wrap each angle to within pi of the limits' centre, so a range across +-pi clamps right.
|
|
145
|
+
const c = this.#center.addVectors(rotationMin, rotationMax).multiplyScalar(0.5);
|
|
146
|
+
v.x = c.x + modulo(v.x - c.x + Math.PI, 2 * Math.PI) - Math.PI;
|
|
147
|
+
v.y = c.y + modulo(v.y - c.y + Math.PI, 2 * Math.PI) - Math.PI;
|
|
148
|
+
v.z = c.z + modulo(v.z - c.z + Math.PI, 2 * Math.PI) - Math.PI;
|
|
149
|
+
}
|
|
150
|
+
if (rotationMin !== undefined) v.max(rotationMin);
|
|
151
|
+
if (rotationMax !== undefined) v.min(rotationMax);
|
|
152
|
+
link.rotation.setFromVector3(v);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
#valid(): void {
|
|
157
|
+
const bones = this.mesh.skeleton.bones;
|
|
158
|
+
for (const ik of this.iks) {
|
|
159
|
+
let child = bones[ik.effector] as Bone;
|
|
160
|
+
for (const entry of ik.links) {
|
|
161
|
+
const parent = bones[entry.index] as Bone;
|
|
162
|
+
if (child.parent !== parent)
|
|
163
|
+
console.warn(
|
|
164
|
+
`THREE.CCDIKSolver: bone ${child.name} is not the child of bone ${parent.name}`,
|
|
165
|
+
);
|
|
166
|
+
child = parent;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -15,10 +15,12 @@ import { __tnLoadGltf } from "three";
|
|
|
15
15
|
|
|
16
16
|
interface IEngineModel {
|
|
17
17
|
readonly scene: object;
|
|
18
|
+
/** Every scene in file order: a model can keep each LOD level as a named scene (`fir_lod1`). */
|
|
19
|
+
readonly scenes: readonly object[];
|
|
18
20
|
readonly animations: readonly object[];
|
|
19
21
|
}
|
|
20
22
|
|
|
21
|
-
/** GLTFLoader's result:
|
|
23
|
+
/** GLTFLoader's result: the default scene, every scene, its clips, and the members three's callers read. */
|
|
22
24
|
export interface IGltfResult {
|
|
23
25
|
readonly scene: object;
|
|
24
26
|
readonly scenes: readonly object[];
|
|
@@ -30,7 +32,7 @@ export interface IGltfResult {
|
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
const load = __tnLoadGltf as
|
|
33
|
-
| ((bytes: Uint8Array, files: ReadonlyMap<string, Uint8Array>) => IEngineModel)
|
|
35
|
+
| ((bytes: Uint8Array, files: ReadonlyMap<string, Uint8Array>) => Promise<IEngineModel>)
|
|
34
36
|
| undefined;
|
|
35
37
|
|
|
36
38
|
interface IGltfJson {
|
|
@@ -42,7 +44,12 @@ interface IGltfJson {
|
|
|
42
44
|
sparse?: unknown;
|
|
43
45
|
type?: string;
|
|
44
46
|
}[];
|
|
45
|
-
bufferViews?: {
|
|
47
|
+
bufferViews?: {
|
|
48
|
+
byteLength?: number;
|
|
49
|
+
byteOffset?: number;
|
|
50
|
+
byteStride?: number;
|
|
51
|
+
extensions?: Record<string, IMeshoptView | undefined>;
|
|
52
|
+
}[];
|
|
46
53
|
images?: { uri?: unknown }[];
|
|
47
54
|
meshes?: { primitives?: unknown[] }[];
|
|
48
55
|
nodes?: { children?: number[]; mesh?: number }[];
|
|
@@ -50,6 +57,30 @@ interface IGltfJson {
|
|
|
50
57
|
scenes?: { nodes?: number[] }[];
|
|
51
58
|
}
|
|
52
59
|
|
|
60
|
+
/** An EXT/KHR_meshopt_compression buffer view: the compressed stream and how it decodes. */
|
|
61
|
+
interface IMeshoptView {
|
|
62
|
+
buffer?: number;
|
|
63
|
+
byteLength?: number;
|
|
64
|
+
byteOffset?: number;
|
|
65
|
+
byteStride?: number;
|
|
66
|
+
count?: number;
|
|
67
|
+
filter?: string;
|
|
68
|
+
mode?: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** three's MeshoptDecoder, as core hands it to `setMeshoptDecoder`. */
|
|
72
|
+
interface IMeshoptDecoder {
|
|
73
|
+
readonly ready: Promise<void>;
|
|
74
|
+
decodeGltfBuffer(
|
|
75
|
+
target: Uint8Array,
|
|
76
|
+
count: number,
|
|
77
|
+
size: number,
|
|
78
|
+
source: Uint8Array,
|
|
79
|
+
mode: string,
|
|
80
|
+
filter?: string,
|
|
81
|
+
): void;
|
|
82
|
+
}
|
|
83
|
+
|
|
53
84
|
/** The JSON and, for a GLB, the binary chunk. Throws on a file that is neither. */
|
|
54
85
|
function readGltf(data: ArrayBuffer | string): { json: IGltfJson; bin: Uint8Array | undefined } {
|
|
55
86
|
if (typeof data === "string") return { bin: undefined, json: JSON.parse(data) as IGltfJson };
|
|
@@ -108,12 +139,48 @@ type PluginFactory = (parser: IEngineParser) => IHostedPlugin;
|
|
|
108
139
|
|
|
109
140
|
const ELEMENTS = { 5121: Uint8Array, 5123: Uint16Array, 5125: Uint32Array } as const;
|
|
110
141
|
|
|
142
|
+
/**
|
|
143
|
+
* A buffer view's bytes from the GLB's binary chunk: as stored, or decoded when the cook
|
|
144
|
+
* meshopt-compressed it (the engine's own loader decodes the mesh; this reads only the LOD chain).
|
|
145
|
+
*/
|
|
146
|
+
async function viewBytes(
|
|
147
|
+
view: NonNullable<IGltfJson["bufferViews"]>[number],
|
|
148
|
+
bin: Uint8Array,
|
|
149
|
+
decoder: IMeshoptDecoder | undefined,
|
|
150
|
+
index: number,
|
|
151
|
+
): Promise<Uint8Array> {
|
|
152
|
+
const meshopt =
|
|
153
|
+
view.extensions?.EXT_meshopt_compression ?? view.extensions?.KHR_meshopt_compression;
|
|
154
|
+
if (meshopt === undefined) return bin.subarray(view.byteOffset ?? 0);
|
|
155
|
+
const { byteLength = 0, byteOffset = 0, byteStride = 0, count = 0, mode = "" } = meshopt;
|
|
156
|
+
if (
|
|
157
|
+
decoder === undefined ||
|
|
158
|
+
(meshopt.buffer ?? 0) !== 0 ||
|
|
159
|
+
byteOffset + byteLength > bin.byteLength
|
|
160
|
+
)
|
|
161
|
+
throw new Error(
|
|
162
|
+
`TN_WASM_GLTF_LOD_ACCESSOR: accessor ${String(index)} is meshopt-compressed ${decoder === undefined ? "and no meshopt decoder was set" : "outside the binary chunk"}`,
|
|
163
|
+
);
|
|
164
|
+
await decoder.ready;
|
|
165
|
+
const decoded = new Uint8Array(count * byteStride);
|
|
166
|
+
decoder.decodeGltfBuffer(
|
|
167
|
+
decoded,
|
|
168
|
+
count,
|
|
169
|
+
byteStride,
|
|
170
|
+
bin.subarray(byteOffset, byteOffset + byteLength),
|
|
171
|
+
mode,
|
|
172
|
+
meshopt.filter,
|
|
173
|
+
);
|
|
174
|
+
return decoded;
|
|
175
|
+
}
|
|
176
|
+
|
|
111
177
|
/** An index accessor's values, copied out of the GLB's binary chunk. */
|
|
112
|
-
function readIndices(
|
|
178
|
+
async function readIndices(
|
|
113
179
|
json: IGltfJson,
|
|
114
180
|
bin: Uint8Array | undefined,
|
|
115
181
|
index: number,
|
|
116
|
-
|
|
182
|
+
decoder: IMeshoptDecoder | undefined,
|
|
183
|
+
): Promise<{ array: ArrayLike<number> }> {
|
|
117
184
|
const accessor = json.accessors?.[index];
|
|
118
185
|
const view = json.bufferViews?.[accessor?.bufferView ?? -1];
|
|
119
186
|
const Element = ELEMENTS[accessor?.componentType as keyof typeof ELEMENTS];
|
|
@@ -131,13 +198,14 @@ function readIndices(
|
|
|
131
198
|
);
|
|
132
199
|
if (bin === undefined)
|
|
133
200
|
throw new Error("TN_WASM_GLTF_LOD_ACCESSOR: only a GLB's binary chunk is read for a LOD chain");
|
|
134
|
-
const
|
|
201
|
+
const bytes = await viewBytes(view, bin, decoder, index);
|
|
202
|
+
const start = accessor.byteOffset ?? 0;
|
|
135
203
|
const end = start + accessor.count * Element.BYTES_PER_ELEMENT;
|
|
136
|
-
if (!Number.isInteger(start) || end >
|
|
204
|
+
if (!Number.isInteger(start) || end > bytes.byteLength)
|
|
137
205
|
throw new Error(
|
|
138
206
|
`TN_WASM_GLTF_LOD_ACCESSOR: accessor ${String(index)} reads past the binary chunk`,
|
|
139
207
|
);
|
|
140
|
-
return { array: new Element(
|
|
208
|
+
return { array: new Element(bytes.slice(start, end).buffer) };
|
|
141
209
|
}
|
|
142
210
|
|
|
143
211
|
/**
|
|
@@ -179,13 +247,14 @@ function associate(json: IGltfJson, scene: object, into: IEngineParser["associat
|
|
|
179
247
|
function hostPlugins(
|
|
180
248
|
factories: readonly PluginFactory[],
|
|
181
249
|
data: ArrayBuffer | string,
|
|
250
|
+
decoder: IMeshoptDecoder | undefined,
|
|
182
251
|
): { parser: IEngineParser; plugins: IHostedPlugin[] } | undefined {
|
|
183
252
|
if (factories.length === 0) return undefined;
|
|
184
253
|
const { bin, json } = readGltf(data);
|
|
185
254
|
const parser: IEngineParser = {
|
|
186
255
|
associations: new Map(),
|
|
187
256
|
getDependency: async (type, index) => {
|
|
188
|
-
if (type === "accessor") return readIndices(json, bin, index);
|
|
257
|
+
if (type === "accessor") return readIndices(json, bin, index, decoder);
|
|
189
258
|
throw new Error(
|
|
190
259
|
`TN_WASM_GLTF_DEPENDENCY_UNSUPPORTED: the engine glTF loader returns no '${type}'`,
|
|
191
260
|
);
|
|
@@ -226,9 +295,15 @@ function imageBytes(url: string): Promise<Uint8Array> {
|
|
|
226
295
|
|
|
227
296
|
export class GLTFLoader {
|
|
228
297
|
path = "";
|
|
298
|
+
/**
|
|
299
|
+
* Serves one image file's bytes in place of a plain fetch: core hands the resident levels of a
|
|
300
|
+
* KTX2 here (PRD-860). `undefined` from it means "fetch it as usual".
|
|
301
|
+
*/
|
|
302
|
+
imageSource: ((url: string) => Promise<Uint8Array | undefined>) | undefined = undefined;
|
|
229
303
|
/** A plugin defect (a chain the file declares and cannot deliver) rejects the load; core's plugin logs it on stock three. */
|
|
230
304
|
readonly failClosed = true;
|
|
231
305
|
readonly #factories: PluginFactory[] = [];
|
|
306
|
+
#meshopt: IMeshoptDecoder | undefined;
|
|
232
307
|
|
|
233
308
|
setPath(path: string): this {
|
|
234
309
|
this.path = path;
|
|
@@ -247,12 +322,18 @@ export class GLTFLoader {
|
|
|
247
322
|
return this;
|
|
248
323
|
}
|
|
249
324
|
|
|
250
|
-
|
|
251
|
-
|
|
325
|
+
/** The engine's loader transcodes KHR_texture_basisu itself (PRD-861), so the JS loader goes unused. */
|
|
326
|
+
setKTX2Loader(_loader?: unknown): this {
|
|
327
|
+
return this;
|
|
252
328
|
}
|
|
253
329
|
|
|
254
|
-
|
|
255
|
-
|
|
330
|
+
/**
|
|
331
|
+
* The engine's loader decodes EXT/KHR_meshopt_compression itself (PRD-861); the JS decoder reads
|
|
332
|
+
* only a hosted LOD chain's compressed index views.
|
|
333
|
+
*/
|
|
334
|
+
setMeshoptDecoder(decoder?: IMeshoptDecoder): this {
|
|
335
|
+
this.#meshopt = decoder;
|
|
336
|
+
return this;
|
|
256
337
|
}
|
|
257
338
|
|
|
258
339
|
setDRACOLoader(): never {
|
|
@@ -279,11 +360,11 @@ export class GLTFLoader {
|
|
|
279
360
|
throw new Error("TN_WASM_GLTF_UNAVAILABLE: this web engine has no glTF loader");
|
|
280
361
|
const bytes =
|
|
281
362
|
typeof data === "string" ? new TextEncoder().encode(data) : new Uint8Array(data);
|
|
282
|
-
const hosted = hostPlugins(this.#factories, data);
|
|
283
|
-
const model = load(bytes, files);
|
|
363
|
+
const hosted = hostPlugins(this.#factories, data, this.#meshopt);
|
|
364
|
+
const model = await load(bytes, files);
|
|
284
365
|
result = {
|
|
285
366
|
scene: model.scene,
|
|
286
|
-
scenes: [model.
|
|
367
|
+
scenes: [...model.scenes],
|
|
287
368
|
animations: [...model.animations],
|
|
288
369
|
cameras: [],
|
|
289
370
|
asset: { version: "2.0" },
|
|
@@ -308,7 +389,13 @@ export class GLTFLoader {
|
|
|
308
389
|
}
|
|
309
390
|
Promise.all(
|
|
310
391
|
// The engine loader asks for the uri with its percent escapes decoded.
|
|
311
|
-
uris.map(
|
|
392
|
+
uris.map(
|
|
393
|
+
async (uri) =>
|
|
394
|
+
[
|
|
395
|
+
decodeURIComponent(uri),
|
|
396
|
+
(await this.imageSource?.(path + uri)) ?? (await imageBytes(path + uri)),
|
|
397
|
+
] as const,
|
|
398
|
+
),
|
|
312
399
|
).then(
|
|
313
400
|
(files) => settle(new Map(files)),
|
|
314
401
|
(error: unknown) => {
|
|
@@ -419,11 +419,25 @@ export interface IScreenSpaceGiOptions {
|
|
|
419
419
|
readonly camera: IScreenSpaceCamera;
|
|
420
420
|
/**
|
|
421
421
|
* The light a ray that finds nothing on screen would have returned: the tier below the screen traces. It is
|
|
422
|
-
* called
|
|
423
|
-
*
|
|
424
|
-
*
|
|
422
|
+
* called for each ray that hits, with the receiver's view-space position and the ray's unit view-space direction,
|
|
423
|
+
* and answers the radiance that tier supplies along that direction (a cosine-convolved field answers E / pi for the
|
|
424
|
+
* direction as its normal). The gather then returns `hit - fallback` for the rays that hit, so the two tiers never add
|
|
425
|
+
* the same light twice. Lumen samples its radiance cache the same way, per ray direction (EpicGames/UnrealEngine:
|
|
426
|
+
* Engine/Shaders/Private/Lumen/LumenScreenProbeTracing.usf:831, SampleRadianceCacheAndApply with RayWorldDirection).
|
|
427
|
+
* One value per pixel (the cache's mean over the hemisphere) made every hit into a dark corner a loss: its rays find
|
|
428
|
+
* the shadowed walls nearby while its bright directions leave the screen. Missing means 0, as three's SSGINode.
|
|
425
429
|
*/
|
|
426
|
-
readonly fallback?: (position: TslNode, normal: TslNode) => TslNode;
|
|
430
|
+
readonly fallback?: (position: TslNode, direction: TslNode, normal: TslNode) => TslNode;
|
|
431
|
+
/**
|
|
432
|
+
* What a ray that leaves the screen finds in the tier past the screen: Unreal continues the ray in the distance field
|
|
433
|
+
* up to the radiance cache's coverage and reads the cache there, so a miss is a direction's radiance and not the
|
|
434
|
+
* pixel's mean (`ScreenProbeGather` composes that walk). Called for each ray that found nothing on screen, with the
|
|
435
|
+
* receiver's view-space position, the ray's unit view-space direction and its normal, and answers the light that
|
|
436
|
+
* arrives from off screen minus what the frame already holds at the pixel: the composite adds the pixel's own
|
|
437
|
+
* indirect diffuse, so a value that did not take it out would count it twice. Missing means 0, as three's SSGINode
|
|
438
|
+
* and this gather before PRD-640 Phase 2: a miss then leaves the pixel's cache average in place.
|
|
439
|
+
*/
|
|
440
|
+
readonly miss?: (position: TslNode, direction: TslNode, normal: TslNode) => TslNode;
|
|
427
441
|
/**
|
|
428
442
|
* Gather with screen probes instead of per-pixel rays (Unreal's ScreenProbeGather): one probe per
|
|
429
443
|
* `downsampleFactor` pixel tile traces an octahedron of rays, the probe radiance is turned into an irradiance
|
|
@@ -475,7 +489,7 @@ const GI_MAX_LUMINANCE = 40;
|
|
|
475
489
|
* share of its rays and answers `pow(clamp(1 - occludedShare), aoIntensity)` for the caller to multiply the lit frame
|
|
476
490
|
* by (SSGINode.js:637-638, 651). This answers the same quantity as `pow(clamp(1 - a), aoIntensity)` over rgb. A
|
|
477
491
|
* constant 1 dropped the term: the frame kept the light the occluder blocked and SSGI only added its gather
|
|
478
|
-
* (2026-11-02, PRD-617 AC-7).
|
|
492
|
+
* (2026-11-02, PRD-617 AC-7). With a `fallback` tier it answers 1: each hit already replaced the cache along its ray.
|
|
479
493
|
*/
|
|
480
494
|
export function defineScreenSpaceGi(tsl: IScreenSpaceTsl) {
|
|
481
495
|
return function ssgi(
|
|
@@ -517,7 +531,11 @@ export function defineScreenSpaceGi(tsl: IScreenSpaceTsl) {
|
|
|
517
531
|
const screenEdgeFade = uniform(0.1);
|
|
518
532
|
const probeJitter = uniform(1);
|
|
519
533
|
const probeDebug = uniform(0);
|
|
534
|
+
// Over a fallback tier the frame holds no occluded light to remove: a ray that hits returns `hit - fallback`, so
|
|
535
|
+
// the cache along it is already swapped for what the ray found, and the hit share multiplied in again took that
|
|
536
|
+
// light out twice, with the direct light (Lumen's screen probe gather applies no such term over its radiance cache).
|
|
520
537
|
const occlusion = (gather: TslNode): TslNode => {
|
|
538
|
+
if (options.fallback !== undefined) return vec4(1, 1, 1, 1);
|
|
521
539
|
const ao = pow(clamp(float(1).sub(gather.a), float(0), float(1)), aoIntensity);
|
|
522
540
|
return vec4(ao, ao, ao, 1);
|
|
523
541
|
};
|
|
@@ -572,7 +590,6 @@ export function defineScreenSpaceGi(tsl: IScreenSpaceTsl) {
|
|
|
572
590
|
If(centreDepth.lessThan(1).and(centreNormal.dot(centreNormal).greaterThan(0.25)), () => {
|
|
573
591
|
const position = viewPosition(coordinate, centreDepth);
|
|
574
592
|
const normal = normalize(centreNormal);
|
|
575
|
-
const fallback = options.fallback?.(position, normal) ?? vec3(0, 0, 0);
|
|
576
593
|
// A frame around the normal, and a per-pixel rotation of the ray set (interleaved gradient noise on a
|
|
577
594
|
// fixed lattice of the uv: the noise is the denoiser's to remove, not a pattern to see).
|
|
578
595
|
const helper = normal.y
|
|
@@ -615,8 +632,14 @@ export function defineScreenSpaceGi(tsl: IScreenSpaceTsl) {
|
|
|
615
632
|
const trace = march(origin, direction, position.z);
|
|
616
633
|
If(trace.hitDistance.greaterThanEqual(0), () => {
|
|
617
634
|
const hit = shade(trace.hitCoordinate, trace.hitDistance, direction);
|
|
635
|
+
const fallback = options.fallback?.(position, direction, normal) ?? vec3(0, 0, 0);
|
|
618
636
|
gathered.assign(gathered.add(hit.radiance.sub(fallback).mul(hit.weight)));
|
|
619
637
|
hits.assign(hits.add(hit.weight));
|
|
638
|
+
}).Else(() => {
|
|
639
|
+
// A ray the screen cannot answer reaches the tier past it (the world-space trace and the radiance cache):
|
|
640
|
+
// the delta over what the pixel's own indirect diffuse already holds, at the ray's weight.
|
|
641
|
+
const past = options.miss?.(position, direction, normal);
|
|
642
|
+
if (past !== undefined) gathered.assign(gathered.add(past));
|
|
620
643
|
});
|
|
621
644
|
},
|
|
622
645
|
);
|
|
@@ -682,10 +705,11 @@ function octahedronEncode(tsl: IScreenSpaceTsl, direction: TslNode): TslNode {
|
|
|
682
705
|
/**
|
|
683
706
|
* The screen-probe gather (PRD-640). Three passes, none per pixel until the last:
|
|
684
707
|
* 1. trace one texel per ray: a probe per tile traces an `n x n` octahedron of rays (view space) and keeps the lit
|
|
685
|
-
* colour each hit found and its weight
|
|
708
|
+
* colour each hit found and its weight, or, where the screen has nothing, what the tier past it answers
|
|
709
|
+
* (`miss`: the world-space trace and the radiance cache).
|
|
686
710
|
* 2. filter each probe turns its rays into an irradiance octahedron: per direction, the cosine-weighted mean of
|
|
687
|
-
* `hit - fallback`
|
|
688
|
-
* the
|
|
711
|
+
* `hit - fallback` (the trace subtracts the cache along each ray), so a ray that hits keeps the screen's
|
|
712
|
+
* light and a ray that misses keeps the tier beyond it. A pixel then reads one bilinear tap, not 64 rays.
|
|
689
713
|
* 3. gather each pixel blends the four nearest probes by position, depth-plane distance and normal agreement, and
|
|
690
714
|
* reads each probe's irradiance along the pixel normal. A pixel no probe can see gets the cache alone.
|
|
691
715
|
*/
|
|
@@ -764,9 +788,15 @@ function defineScreenProbes(
|
|
|
764
788
|
const origin = position.add(normal.mul(max(0.01, position.z.negate().mul(0.004))));
|
|
765
789
|
const trace = march(origin, direction, position.z);
|
|
766
790
|
If(trace.hitDistance.greaterThanEqual(0), () => {
|
|
767
|
-
//
|
|
791
|
+
// What the lit colour the ray found adds over the cache along the same ray, and its weight.
|
|
768
792
|
const hit = shade(trace.hitCoordinate, trace.hitDistance, direction);
|
|
769
|
-
|
|
793
|
+
const believed = options.fallback?.(position, direction, normal) ?? vec3(0, 0, 0);
|
|
794
|
+
result.assign(vec4(hit.radiance.sub(believed).mul(hit.weight), hit.weight));
|
|
795
|
+
}).Else(() => {
|
|
796
|
+
// A ray the screen cannot answer carries the tier past it: the filter mixes it with the probe's other rays, so a
|
|
797
|
+
// probe keeps a direction's radiance where the frame ends (the `a` channel stays the hit share).
|
|
798
|
+
const past = options.miss?.(position, direction, normal);
|
|
799
|
+
if (past !== undefined) result.assign(vec4(past, 0));
|
|
770
800
|
});
|
|
771
801
|
});
|
|
772
802
|
return result;
|
|
@@ -795,17 +825,9 @@ function defineScreenProbes(
|
|
|
795
825
|
sum.assign(sum.add(found.mul(weight)));
|
|
796
826
|
weights.assign(weights.add(weight));
|
|
797
827
|
});
|
|
798
|
-
// The
|
|
799
|
-
// `hit - cache` over every ray, so the two tiers never add the same light twice.
|
|
800
|
-
const probeUv = tile.add(0.5).div(tiles).min(vec2(1, 1));
|
|
801
|
-
const depth = depthNode.sample(probeUv).r;
|
|
802
|
-
const rawNormal = normalNode.sample(probeUv).xyz;
|
|
803
|
-
const believed = valid(depth, rawNormal).select(
|
|
804
|
-
options.fallback?.(viewPosition(probeUv, depth), normal) ?? vec3(0, 0, 0),
|
|
805
|
-
vec3(0, 0, 0),
|
|
806
|
-
);
|
|
828
|
+
// The trace already holds `hit - cache` along each ray, so the two tiers never add the same light twice.
|
|
807
829
|
const total = max(weights, float(1e-4));
|
|
808
|
-
return vec4(sum.rgb.
|
|
830
|
+
return vec4(sum.rgb.div(total), sum.a.div(total));
|
|
809
831
|
})();
|
|
810
832
|
const filtered = rtt(filterGraph, tilesX * IRRADIANCE_SIDE, tilesY * IRRADIANCE_SIDE, {
|
|
811
833
|
type: HALF_FLOAT_TYPE,
|