@luma.gl/engine 10.0.0-beta.2 → 10.0.0-beta.3
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/dist/arrow/index.cjs.map +1 -1
- package/dist/arrow/renderers/path/transforms/gpu-path-float64-deltas.cjs +1 -0
- package/dist/arrow/renderers/path/transforms/gpu-path-float64-deltas.d.ts.map +1 -1
- package/dist/arrow/renderers/path/transforms/gpu-path-float64-deltas.js +1 -0
- package/dist/arrow/renderers/path/transforms/gpu-path-float64-deltas.js.map +1 -1
- package/dist/dist.dev.js +54 -1
- package/dist/dist.min.js +64 -50
- package/dist/gltf/gltf/create-composed-scenegraphs-from-gltf.cjs +80 -0
- package/dist/gltf/gltf/create-composed-scenegraphs-from-gltf.d.ts +28 -0
- package/dist/gltf/gltf/create-composed-scenegraphs-from-gltf.d.ts.map +1 -0
- package/dist/gltf/gltf/create-composed-scenegraphs-from-gltf.js +74 -0
- package/dist/gltf/gltf/create-composed-scenegraphs-from-gltf.js.map +1 -0
- package/dist/gltf/gltf/create-scenegraph-from-gltf.cjs +13 -4
- package/dist/gltf/gltf/create-scenegraph-from-gltf.d.ts.map +1 -1
- package/dist/gltf/gltf/create-scenegraph-from-gltf.js +11 -0
- package/dist/gltf/gltf/create-scenegraph-from-gltf.js.map +1 -1
- package/dist/gltf/gltf/gltf-skin.cjs +1 -1
- package/dist/gltf/gltf/gltf-skin.js +2 -2
- package/dist/gltf/gltf/gltf-skin.js.map +1 -1
- package/dist/gltf/index.cjs +3 -0
- package/dist/gltf/index.cjs.map +3 -3
- package/dist/gltf/index.d.ts +1 -0
- package/dist/gltf/index.d.ts.map +1 -1
- package/dist/gltf/index.js +1 -0
- package/dist/gltf/index.js.map +1 -1
- package/dist/gltf/parsers/parse-gltf-physics-pointer.cjs +1 -1
- package/dist/gltf/parsers/parse-gltf-physics-pointer.js +1 -1
- package/dist/gltf/parsers/parse-gltf-physics-pointer.js.map +1 -1
- package/dist/gltf/parsers/parse-gltf.cjs +2 -1
- package/dist/gltf/parsers/parse-gltf.d.ts +2 -0
- package/dist/gltf/parsers/parse-gltf.d.ts.map +1 -1
- package/dist/gltf/parsers/parse-gltf.js +3 -1
- package/dist/gltf/parsers/parse-gltf.js.map +1 -1
- package/dist/gltf/physics/export-state.cjs +115 -14
- package/dist/gltf/physics/export-state.d.ts +9 -2
- package/dist/gltf/physics/export-state.d.ts.map +1 -1
- package/dist/gltf/physics/export-state.js +171 -18
- package/dist/gltf/physics/export-state.js.map +1 -1
- package/dist/gltf/physics/index.cjs.map +1 -1
- package/dist/gltf/physics/index.d.ts +1 -1
- package/dist/gltf/physics/index.d.ts.map +1 -1
- package/dist/gltf/physics/index.js.map +1 -1
- package/dist/gltf/physics/parse-gltf-physics.cjs +11 -8
- package/dist/gltf/physics/parse-gltf-physics.d.ts.map +1 -1
- package/dist/gltf/physics/parse-gltf-physics.js +6 -2
- package/dist/gltf/physics/parse-gltf-physics.js.map +1 -1
- package/dist/gltf/physics/physics-types.d.ts +4 -0
- package/dist/gltf/physics/physics-types.d.ts.map +1 -1
- package/dist/gltf/physics/property-bindings.cjs +5 -2
- package/dist/gltf/physics/property-bindings.d.ts.map +1 -1
- package/dist/gltf/physics/property-bindings.js +11 -2
- package/dist/gltf/physics/property-bindings.js.map +1 -1
- package/dist/gltf/types/gltf-postprocessed-schema.d.ts +4 -0
- package/dist/gltf/types/gltf-postprocessed-schema.d.ts.map +1 -1
- package/dist/gltf/types/gltf-types.d.ts +2 -0
- package/dist/gltf/types/gltf-types.d.ts.map +1 -1
- package/dist/graph/physics/index.cjs +3 -0
- package/dist/graph/physics/index.cjs.map +3 -3
- package/dist/graph/physics/index.d.ts +1 -0
- package/dist/graph/physics/index.d.ts.map +1 -1
- package/dist/graph/physics/index.js +1 -0
- package/dist/graph/physics/index.js.map +1 -1
- package/dist/graph/physics/scenegraph-ragdoll.cjs +132 -0
- package/dist/graph/physics/scenegraph-ragdoll.d.ts +32 -0
- package/dist/graph/physics/scenegraph-ragdoll.d.ts.map +1 -0
- package/dist/graph/physics/scenegraph-ragdoll.js +117 -0
- package/dist/graph/physics/scenegraph-ragdoll.js.map +1 -0
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +2 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/scenegraph/controllers/skin-controller.cjs +20 -1
- package/dist/scenegraph/controllers/skin-controller.d.ts +4 -0
- package/dist/scenegraph/controllers/skin-controller.d.ts.map +1 -1
- package/dist/scenegraph/controllers/skin-controller.js +21 -1
- package/dist/scenegraph/controllers/skin-controller.js.map +1 -1
- package/dist/shader-plugins/depth-interval.cjs +60 -0
- package/dist/shader-plugins/depth-interval.d.ts +10 -0
- package/dist/shader-plugins/depth-interval.d.ts.map +1 -0
- package/dist/shader-plugins/depth-interval.js +42 -0
- package/dist/shader-plugins/depth-interval.js.map +1 -0
- package/dist/splats/index.cjs.map +2 -2
- package/dist/splats/index.d.ts +1 -1
- package/dist/splats/index.d.ts.map +1 -1
- package/dist/splats/index.js.map +1 -1
- package/dist/splats/splat-picking.d.ts +2 -2
- package/dist/splats/splat-picking.d.ts.map +1 -1
- package/dist/splats/splat-renderer.cjs +51 -2
- package/dist/splats/splat-renderer.d.ts +15 -1
- package/dist/splats/splat-renderer.d.ts.map +1 -1
- package/dist/splats/splat-renderer.js +61 -2
- package/dist/splats/splat-renderer.js.map +1 -1
- package/dist/splats/splat-shaders.cjs +46 -3
- package/dist/splats/splat-shaders.d.ts +5 -3
- package/dist/splats/splat-shaders.d.ts.map +1 -1
- package/dist/splats/splat-shaders.js +42 -3
- package/dist/splats/splat-shaders.js.map +1 -1
- package/package.json +6 -6
- package/src/arrow/renderers/path/transforms/gpu-path-float64-deltas.ts +1 -0
- package/src/gltf/gltf/create-composed-scenegraphs-from-gltf.ts +100 -0
- package/src/gltf/gltf/create-scenegraph-from-gltf.ts +12 -0
- package/src/gltf/gltf/gltf-skin.ts +2 -2
- package/src/gltf/index.ts +5 -0
- package/src/gltf/parsers/parse-gltf-physics-pointer.ts +1 -1
- package/src/gltf/parsers/parse-gltf.ts +6 -4
- package/src/gltf/physics/README.md +16 -0
- package/src/gltf/physics/export-state.ts +215 -18
- package/src/gltf/physics/index.ts +1 -1
- package/src/gltf/physics/parse-gltf-physics.ts +5 -2
- package/src/gltf/physics/physics-types.ts +4 -0
- package/src/gltf/physics/property-bindings.ts +10 -2
- package/src/gltf/types/gltf-postprocessed-schema.ts +4 -0
- package/src/gltf/types/gltf-types.ts +2 -0
- package/src/graph/physics/index.ts +1 -0
- package/src/graph/physics/scenegraph-ragdoll.ts +133 -0
- package/src/index.ts +2 -0
- package/src/scenegraph/controllers/skin-controller.ts +20 -1
- package/src/shader-plugins/depth-interval.ts +45 -0
- package/src/splats/README.md +10 -0
- package/src/splats/index.ts +1 -0
- package/src/splats/splat-renderer.ts +82 -2
- package/src/splats/splat-shaders.ts +48 -3
|
@@ -79,8 +79,8 @@ function makeSkinBindings(props: ParseGLTFSkinsProps): SkinBinding[] {
|
|
|
79
79
|
const inverseBindMatrices = sourceSkin.inverseBindMatrices?.value;
|
|
80
80
|
const jointMatrices = new Float32Array(joints.length * 16);
|
|
81
81
|
const usesLargeSkinPalette =
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
models.some(modelNode => modelNode.userData['gltfLargeSkinPalette'] === true) &&
|
|
83
|
+
(props.device.type === 'webgpu' || joints.length > SKIN_MAX_JOINTS);
|
|
84
84
|
let skinJointMatrices: Buffer | Texture | undefined;
|
|
85
85
|
if (usesLargeSkinPalette) {
|
|
86
86
|
skinJointMatrices = createSkinJointPaletteResource(props.device, node.id, jointMatrices);
|
package/src/gltf/index.ts
CHANGED
|
@@ -18,6 +18,11 @@ export {
|
|
|
18
18
|
type GLTFScenegraphBounds,
|
|
19
19
|
type GLTFScenegraphs
|
|
20
20
|
} from './gltf/create-scenegraph-from-gltf';
|
|
21
|
+
export {
|
|
22
|
+
createComposedScenegraphsFromGLTF,
|
|
23
|
+
type GLTFSceneComposition,
|
|
24
|
+
type GLTFSceneInstance
|
|
25
|
+
} from './gltf/create-composed-scenegraphs-from-gltf';
|
|
21
26
|
export {
|
|
22
27
|
GLTFAnimationClip,
|
|
23
28
|
type GLTFAnimationClipProps,
|
|
@@ -16,7 +16,7 @@ export function isGLTFPhysicsPropertyPointer(pointer: string): boolean {
|
|
|
16
16
|
if (Number(component[2]) < size) return isGLTFPhysicsPropertyPointer(component[1]);
|
|
17
17
|
}
|
|
18
18
|
return (
|
|
19
|
-
|
|
19
|
+
/^(?:\/extensions\/KHR_implicit_shapes)?\/shapes\/\d+\/(sphere\/radius|box\/size|(?:capsule|cylinder)\/(height|radiusBottom|radiusTop)|plane\/(sizeX|sizeZ|doubleSided))$/.test(
|
|
20
20
|
pointer
|
|
21
21
|
) ||
|
|
22
22
|
/^\/nodes\/\d+\/extensions\/KHR_physics_rigid_bodies\/(motion\/(isKinematic|mass|centerOfMass|inertiaDiagonal|inertiaOrientation|linearVelocity|angularVelocity|gravityFactor)|joint\/enableCollision)$/.test(
|
|
@@ -48,6 +48,8 @@ export type ParseGLTFOptions = {
|
|
|
48
48
|
properties?: import('@luma.gl/engine').InteractivityPropertyAdapter | null;
|
|
49
49
|
/** Borrowed rigid GPU matrices. The renderer selects a body and local transform per draw. */
|
|
50
50
|
publishedTransforms?: Buffer | null;
|
|
51
|
+
/** Compile skinned meshes for a storage palette, including small WebGPU rigs. */
|
|
52
|
+
storageSkinPalettes?: boolean;
|
|
51
53
|
/** Additional model props applied to each generated primitive model. */
|
|
52
54
|
modelOptions?: Partial<ModelProps>;
|
|
53
55
|
/** Enables shader-level PBR debug output. */
|
|
@@ -83,6 +85,7 @@ const defaultOptions: Required<ParseGLTFOptions> = {
|
|
|
83
85
|
properties: null,
|
|
84
86
|
billboards: null,
|
|
85
87
|
publishedTransforms: null,
|
|
88
|
+
storageSkinPalettes: false,
|
|
86
89
|
modelOptions: {},
|
|
87
90
|
pbrDebug: false,
|
|
88
91
|
imageBasedLightingEnvironment: undefined!,
|
|
@@ -459,10 +462,9 @@ function createNodeForGLTFPrimitive({
|
|
|
459
462
|
vertexCount,
|
|
460
463
|
bounds: [gltfPrimitive.attributes.POSITION.min, gltfPrimitive.attributes.POSITION.max],
|
|
461
464
|
instanceMatrices: instancing?.matrices,
|
|
462
|
-
usesLargeSkinPalette:
|
|
463
|
-
|
|
464
|
-
gltfPrimitive.attributes.JOINTS_1
|
|
465
|
-
),
|
|
465
|
+
usesLargeSkinPalette:
|
|
466
|
+
(options.storageSkinPalettes && device.type === 'webgpu') ||
|
|
467
|
+
usesLargeSkinPalette(gltfPrimitive.attributes.JOINTS_0, gltfPrimitive.attributes.JOINTS_1),
|
|
466
468
|
morphTargets: morphTargetState?.targets
|
|
467
469
|
});
|
|
468
470
|
|
|
@@ -162,3 +162,19 @@ Collision snapshots reject repeated subtree references and cap the flattened out
|
|
|
162
162
|
at one million vertices and three million indices. Accessor counts obey the same
|
|
163
163
|
bounds before allocating decoded arrays. Indexed snapshots discard unused vertices
|
|
164
164
|
before convex-hull cooking, including snapshots supplied by custom resolvers.
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
### Export a settled authored revision
|
|
168
|
+
|
|
169
|
+
Use `exportGLTFPhysicsState(source, description, world, {sceneNodes})` with the
|
|
170
|
+
same node map used to create the live scene session. Use `session.withWorld`
|
|
171
|
+
to serialize export against simulation, reset, and rebuild. The exporter
|
|
172
|
+
takes body poses and velocities from the accepted GPU state, restores the
|
|
173
|
+
world origin, and retains the current authored collider frames and sizes.
|
|
174
|
+
Shared analytic shapes remain shared. Animated nodes retain TRS fields so
|
|
175
|
+
existing channels still work.
|
|
176
|
+
|
|
177
|
+
Keep the source description and node map from the same session revision.
|
|
178
|
+
The exporter rejects a mismatch between the authored transforms and the live
|
|
179
|
+
collider frames. Recook mesh deformation and topology edits before exporting.
|
|
180
|
+
Export performs an explicit readback outside the normal render and simulation loop.
|
|
@@ -3,32 +3,43 @@
|
|
|
3
3
|
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
5
|
import {Matrix4} from '@math.gl/core/matrix';
|
|
6
|
-
import {decomposePhysicsTransform} from '@luma.gl/engine';
|
|
6
|
+
import {decomposePhysicsTransform, readPhysicsRotation, type ScenegraphNode} from '@luma.gl/engine';
|
|
7
7
|
import {ScenegraphPhysicsProperties} from '@luma.gl/engine/physics';
|
|
8
8
|
import {type GPURigidBodyWorld} from '@luma.gl/graph/gpu-physics';
|
|
9
9
|
import type {GLTFPhysicsDescription, GLTFPhysicsDocument} from './physics-types';
|
|
10
10
|
import {makeGLTFPhysicsProperties} from './property-bindings';
|
|
11
11
|
|
|
12
|
+
/** Authored input from the same settled session as the GPU world. */
|
|
13
|
+
export type GLTFPhysicsExportOptions = {
|
|
14
|
+
/** Current local scene transforms for animated analytic collider frames and scales. */
|
|
15
|
+
sceneNodes?: ReadonlyMap<number, ScenegraphNode>;
|
|
16
|
+
};
|
|
17
|
+
|
|
12
18
|
/** Export a settled revision as native draft glTF JSON, preserving source assets and provenance.
|
|
13
19
|
* This explicit editing operation reads GPU state. It is not a simulation/render bridge.
|
|
14
|
-
*
|
|
20
|
+
* Recook mesh deformation and topology changes first. Current analytic animation can
|
|
21
|
+
* be exported directly when the scene node map represents the same accepted revision.
|
|
15
22
|
* Animation clips remain authored: replaying a clip can intentionally replace the saved pose.
|
|
16
23
|
*/
|
|
17
24
|
export async function exportGLTFPhysicsState<T extends GLTFPhysicsDocument>(
|
|
18
25
|
source: T,
|
|
19
26
|
description: GLTFPhysicsDescription,
|
|
20
|
-
world: GPURigidBodyWorld
|
|
27
|
+
world: GPURigidBodyWorld,
|
|
28
|
+
{sceneNodes}: GLTFPhysicsExportOptions = {}
|
|
21
29
|
): Promise<T> {
|
|
22
30
|
if (world.count !== description.bodies.length)
|
|
23
31
|
throw new Error('Recook before exporting topology');
|
|
24
32
|
const output = structuredClone(source);
|
|
25
33
|
const nodes = output.nodes ?? [];
|
|
34
|
+
if (sceneNodes) writeLiveTransforms(output, sceneNodes);
|
|
26
35
|
const parents = new Map<number, number>();
|
|
27
36
|
for (const [index, node] of nodes.entries())
|
|
28
37
|
for (const child of node.children ?? []) {
|
|
29
38
|
if (parents.has(child)) throw new Error('Ambiguous export hierarchy');
|
|
30
39
|
parents.set(child, index);
|
|
31
40
|
}
|
|
41
|
+
/** Capture the local-frame origin before queuing the corresponding GPU state read. */
|
|
42
|
+
const origin = world.origin;
|
|
32
43
|
const states = await world.readState();
|
|
33
44
|
const matrices = new Map<number, Matrix4>();
|
|
34
45
|
const locals = new Map<number, Matrix4>();
|
|
@@ -44,7 +55,7 @@ export async function exportGLTFPhysicsState<T extends GLTFPhysicsDocument>(
|
|
|
44
55
|
? new Matrix4(node.matrix)
|
|
45
56
|
: new Matrix4()
|
|
46
57
|
.translate(node.translation ?? [0, 0, 0])
|
|
47
|
-
.multiplyRight(new Matrix4().fromQuaternion(node.rotation
|
|
58
|
+
.multiplyRight(new Matrix4().fromQuaternion(readPhysicsRotation(node.rotation)))
|
|
48
59
|
.scale(node.scale ?? [1, 1, 1]);
|
|
49
60
|
locals.set(index, local);
|
|
50
61
|
const parent = parents.get(index);
|
|
@@ -77,43 +88,46 @@ export async function exportGLTFPhysicsState<T extends GLTFPhysicsDocument>(
|
|
|
77
88
|
: target.joints.map(joint => ({...target, joints: [joint]}));
|
|
78
89
|
for (const candidate of candidates) {
|
|
79
90
|
const binding = new ScenegraphPhysicsProperties(world, new Map([[pointer, candidate]]));
|
|
80
|
-
if (
|
|
91
|
+
if (!equalProperty(binding.get(pointer), value))
|
|
81
92
|
throw new Error('Shared physics resource diverged');
|
|
82
93
|
}
|
|
83
94
|
}
|
|
84
|
-
if (target.type === 'shape') {
|
|
85
|
-
for (const collider of target.colliders) {
|
|
86
|
-
if (world.getColliderShapeScale(collider.index).some(component => component !== 1))
|
|
87
|
-
throw new Error('Recook animated scale before exporting');
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
95
|
writePointer(output, pointer, value.length === 1 ? value[0] : value);
|
|
91
96
|
}
|
|
92
97
|
for (const body of description.bodies) {
|
|
93
98
|
if (body.nodeIndex < 0) continue;
|
|
94
99
|
const state = states[body.bodyIndex];
|
|
95
|
-
const rotation = new Matrix4().fromQuaternion(state.orientation);
|
|
100
|
+
const rotation = new Matrix4().fromQuaternion(readPhysicsRotation(state.orientation));
|
|
96
101
|
const center = rotation.transformAsVector(
|
|
97
102
|
world.getBodyParameters(body.bodyIndex).centerOfMass ?? [0, 0, 0]
|
|
98
103
|
);
|
|
99
104
|
const matrix = new Matrix4()
|
|
100
105
|
.translate(state.position.map((value, axis) => value - center[axis]))
|
|
101
106
|
.multiplyRight(rotation)
|
|
102
|
-
.scale(decomposePhysicsTransform(
|
|
107
|
+
.scale(decomposePhysicsTransform(authored(body.nodeIndex)).scale);
|
|
103
108
|
bodyMatrices.set(body.nodeIndex, matrix);
|
|
104
109
|
}
|
|
105
110
|
matrices.clear();
|
|
111
|
+
/** Compose the accepted hierarchy near the simulation origin to preserve small local offsets. */
|
|
106
112
|
const currentWorld = (index: number): Matrix4 => {
|
|
107
113
|
const existing = bodyMatrices.get(index) ?? matrices.get(index);
|
|
108
114
|
if (existing) return existing;
|
|
115
|
+
/** Articulated and static descendants inherit the same origin-relative frame. */
|
|
109
116
|
const parent = parents.get(index);
|
|
117
|
+
/** The source local matrix is needed later and must stay in its authored frame. */
|
|
118
|
+
const local = locals.get(index)!;
|
|
119
|
+
/** Rebase an authored root by changing its world translation directly. */
|
|
110
120
|
const matrix =
|
|
111
|
-
parent === undefined
|
|
112
|
-
|
|
113
|
-
|
|
121
|
+
parent === undefined ? local.clone() : new Matrix4(currentWorld(parent)).multiplyRight(local);
|
|
122
|
+
if (parent === undefined) {
|
|
123
|
+
matrix[12] -= origin[0];
|
|
124
|
+
matrix[13] -= origin[1];
|
|
125
|
+
matrix[14] -= origin[2];
|
|
126
|
+
}
|
|
114
127
|
matrices.set(index, matrix);
|
|
115
128
|
return matrix;
|
|
116
129
|
};
|
|
130
|
+
validateColliderFrames(description, world, currentWorld, parents, locals);
|
|
117
131
|
const bodyNodes = new Set(description.bodies.map(body => body.nodeIndex));
|
|
118
132
|
for (const body of description.bodies) {
|
|
119
133
|
if (body.nodeIndex < 0) continue;
|
|
@@ -158,7 +172,11 @@ export async function exportGLTFPhysicsState<T extends GLTFPhysicsDocument>(
|
|
|
158
172
|
}
|
|
159
173
|
const node = nodes[body.nodeIndex];
|
|
160
174
|
delete node.matrix;
|
|
161
|
-
|
|
175
|
+
// Only scene roots need world-space positions. Child TRS is already relative to its parent.
|
|
176
|
+
node.translation =
|
|
177
|
+
parent === undefined || promote
|
|
178
|
+
? transform.position.map((value, axis) => value + origin[axis])
|
|
179
|
+
: transform.position;
|
|
162
180
|
node.rotation = transform.rotation;
|
|
163
181
|
node.scale = transform.scale;
|
|
164
182
|
if (mode === 'static') {
|
|
@@ -201,7 +219,7 @@ export async function exportGLTFPhysicsState<T extends GLTFPhysicsDocument>(
|
|
|
201
219
|
output,
|
|
202
220
|
`${motion}angularVelocity`,
|
|
203
221
|
new Matrix4()
|
|
204
|
-
.fromQuaternion(states[body.bodyIndex].orientation)
|
|
222
|
+
.fromQuaternion(readPhysicsRotation(states[body.bodyIndex].orientation))
|
|
205
223
|
.invert()
|
|
206
224
|
.transformAsVector(states[body.bodyIndex].angularVelocity)
|
|
207
225
|
);
|
|
@@ -210,6 +228,185 @@ export async function exportGLTFPhysicsState<T extends GLTFPhysicsDocument>(
|
|
|
210
228
|
return output;
|
|
211
229
|
}
|
|
212
230
|
|
|
231
|
+
/** Keep animated nodes in TRS form so their existing channels remain valid glTF. */
|
|
232
|
+
function writeLiveTransforms(
|
|
233
|
+
output: GLTFPhysicsDocument,
|
|
234
|
+
sceneNodes: ReadonlyMap<number, ScenegraphNode>
|
|
235
|
+
): void {
|
|
236
|
+
/** Only node TRS channels require decomposition of a supplied local transform. */
|
|
237
|
+
const animated = new Set<number>();
|
|
238
|
+
for (const animation of output.animations ?? []) {
|
|
239
|
+
for (const {target} of animation.channels) {
|
|
240
|
+
if (
|
|
241
|
+
target.node !== undefined &&
|
|
242
|
+
['translation', 'rotation', 'scale'].includes(target.path ?? '')
|
|
243
|
+
)
|
|
244
|
+
animated.add(target.node);
|
|
245
|
+
/** KHR_animation_pointer can target the same authored TRS fields. */
|
|
246
|
+
const extension = target.extensions?.['KHR_animation_pointer'];
|
|
247
|
+
if (
|
|
248
|
+
extension &&
|
|
249
|
+
typeof extension === 'object' &&
|
|
250
|
+
'pointer' in extension &&
|
|
251
|
+
typeof extension.pointer === 'string'
|
|
252
|
+
) {
|
|
253
|
+
/** Snapshot node indices remain identical to source indices. */
|
|
254
|
+
const match = /^\/nodes\/(\d+)\/(translation|rotation|scale)(?:\/\d+)?$/.exec(
|
|
255
|
+
extension.pointer
|
|
256
|
+
);
|
|
257
|
+
if (match) animated.add(Number(match[1]));
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
for (const [index, current] of sceneNodes) {
|
|
262
|
+
/** Render-only matrices retain their exact affine transform. */
|
|
263
|
+
const node = output.nodes?.[index];
|
|
264
|
+
if (!node) throw new Error('Unknown export scene node');
|
|
265
|
+
if (animated.has(index)) {
|
|
266
|
+
/** Retain valid glTF authoring channels for non-body animated descendants as well. */
|
|
267
|
+
const transform = getLiveNodeComponents(current, node);
|
|
268
|
+
delete node.matrix;
|
|
269
|
+
node.translation = transform.position;
|
|
270
|
+
node.rotation = transform.rotation;
|
|
271
|
+
node.scale = transform.scale;
|
|
272
|
+
} else {
|
|
273
|
+
node.matrix = Array.from(current.matrix);
|
|
274
|
+
delete node.translation;
|
|
275
|
+
delete node.rotation;
|
|
276
|
+
delete node.scale;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Preserve signed and zero scales when the local matrix comes from known TRS. */
|
|
282
|
+
function getLiveNodeComponents(
|
|
283
|
+
current: ScenegraphNode,
|
|
284
|
+
source: NonNullable<GLTFPhysicsDocument['nodes']>[number]
|
|
285
|
+
): {position: readonly number[]; rotation: readonly number[]; scale: readonly number[]} {
|
|
286
|
+
/** Live local translation from the scenegraph component API. */
|
|
287
|
+
const position = Array.from(current.position);
|
|
288
|
+
/** Keep each axis sign, including axes collapsed by animation. */
|
|
289
|
+
const scale = Array.from(current.scale);
|
|
290
|
+
/** Convert either quaternion or Euler components with the scenegraph convention. */
|
|
291
|
+
const rotationMatrix = new Matrix4();
|
|
292
|
+
if (current.rotation.length === 4) rotationMatrix.fromQuaternion(current.rotation);
|
|
293
|
+
else rotationMatrix.rotateXYZ(current.rotation);
|
|
294
|
+
/** Verify that the components still describe the matrix used by the scene. */
|
|
295
|
+
const liveMatrix = new Matrix4().translate(position).multiplyRight(rotationMatrix).scale(scale);
|
|
296
|
+
if (liveMatrix.every((value, index) => value === current.matrix[index])) {
|
|
297
|
+
/** glTF rotation channels always use quaternions. */
|
|
298
|
+
const rotation =
|
|
299
|
+
current.rotation.length === 4
|
|
300
|
+
? Array.from(current.rotation)
|
|
301
|
+
: decomposePhysicsTransform(rotationMatrix).rotation;
|
|
302
|
+
return {position, rotation, scale};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Some imported nodes initialize only the matrix. Retain the source TRS when it still matches.
|
|
306
|
+
if (!source.matrix) {
|
|
307
|
+
/** Authored local translation with the glTF identity default. */
|
|
308
|
+
const authoredPosition = source.translation ?? [0, 0, 0];
|
|
309
|
+
/** Normalize exactly as the physics importer does when initializing the local matrix. */
|
|
310
|
+
const authoredRotation = readPhysicsRotation(source.rotation);
|
|
311
|
+
/** Authored scale with the glTF identity default. */
|
|
312
|
+
const authoredScale = source.scale ?? [1, 1, 1];
|
|
313
|
+
/** Match the import-time matrix before using source components. */
|
|
314
|
+
const authoredMatrix = new Matrix4()
|
|
315
|
+
.translate(authoredPosition)
|
|
316
|
+
.multiplyRight(new Matrix4().fromQuaternion(authoredRotation))
|
|
317
|
+
.scale(authoredScale);
|
|
318
|
+
if (authoredMatrix.every((value, index) => value === current.matrix[index]))
|
|
319
|
+
return {position: authoredPosition, rotation: authoredRotation, scale: authoredScale};
|
|
320
|
+
}
|
|
321
|
+
return decomposePhysicsTransform(new Matrix4(current.matrix));
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** Check that the native scene reproduces the live collider geometry and placement. */
|
|
325
|
+
function validateColliderFrames(
|
|
326
|
+
description: GLTFPhysicsDescription,
|
|
327
|
+
world: GPURigidBodyWorld,
|
|
328
|
+
getWorldMatrix: (index: number) => Matrix4,
|
|
329
|
+
parents: ReadonlyMap<number, number>,
|
|
330
|
+
locals: ReadonlyMap<number, Matrix4>
|
|
331
|
+
): void {
|
|
332
|
+
for (const collider of description.colliders) {
|
|
333
|
+
/** The importer assigns the collider to its nearest rigid body ancestor. */
|
|
334
|
+
const body = description.bodies[collider.bodyIndex];
|
|
335
|
+
/** Begin at the authored collider placement, including any baked mesh center. */
|
|
336
|
+
const matrix = new Matrix4(collider.localMatrix);
|
|
337
|
+
if (body.nodeIndex < 0) {
|
|
338
|
+
matrix.multiplyLeft(getWorldMatrix(collider.nodeIndex));
|
|
339
|
+
} else {
|
|
340
|
+
/** Walk only body-local matrices to retain offsets even in distant parts of the world. */
|
|
341
|
+
let nodeIndex = collider.nodeIndex;
|
|
342
|
+
while (nodeIndex !== body.nodeIndex) {
|
|
343
|
+
matrix.multiplyLeft(locals.get(nodeIndex)!);
|
|
344
|
+
/** Collider ownership must still match the settled scene hierarchy. */
|
|
345
|
+
const parent = parents.get(nodeIndex);
|
|
346
|
+
if (parent === undefined) throw new Error('Recook before exporting collider ownership');
|
|
347
|
+
nodeIndex = parent;
|
|
348
|
+
}
|
|
349
|
+
/** Cooked collider frames include body scale and exclude rigid world motion. */
|
|
350
|
+
const {scale} = decomposePhysicsTransform(getWorldMatrix(body.nodeIndex));
|
|
351
|
+
matrix.multiplyLeft(new Matrix4().scale(scale));
|
|
352
|
+
}
|
|
353
|
+
/** Inspect the dimensions and pose in the same frame as the cooked collider. */
|
|
354
|
+
const authored = decomposePhysicsTransform(matrix);
|
|
355
|
+
/** Authored absolute scale must match the live cooked dimensions without baking scale twice. */
|
|
356
|
+
const liveScale = world.getColliderShapeScale(collider.colliderIndex);
|
|
357
|
+
if (
|
|
358
|
+
!authored.scale.every((value, axis) =>
|
|
359
|
+
closeComponent(Math.abs(value), collider.cookedScale[axis] * liveScale[axis], 1e-15)
|
|
360
|
+
)
|
|
361
|
+
)
|
|
362
|
+
throw new Error(
|
|
363
|
+
'Export scene scale does not match the live collider. Supply the settled scene nodes or recook.'
|
|
364
|
+
);
|
|
365
|
+
if (collider.shapeIndex === undefined && liveScale.some(component => component !== 1))
|
|
366
|
+
throw new Error('Recook mesh geometry before export');
|
|
367
|
+
|
|
368
|
+
if (body.nodeIndex < 0) continue;
|
|
369
|
+
/** The two quaternion signs represent the same accepted collider orientation. */
|
|
370
|
+
const live = world.getColliderTransform(collider.colliderIndex);
|
|
371
|
+
/** Quaternion alignment tests orientation independently of quaternion sign. */
|
|
372
|
+
const alignment = authored.rotation.reduce(
|
|
373
|
+
(sum, value, axis) => sum + value * live.orientation[axis],
|
|
374
|
+
0
|
|
375
|
+
);
|
|
376
|
+
if (
|
|
377
|
+
!authored.position.every((value, axis) => closeComponent(value, live.position[axis])) ||
|
|
378
|
+
!closeComponent(Math.abs(alignment), 1)
|
|
379
|
+
)
|
|
380
|
+
throw new Error(
|
|
381
|
+
'Export scene pose does not match the live collider. Supply the settled scene nodes or recook.'
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** Allow roundoff when a shared value has been independently scaled and unscaled. */
|
|
387
|
+
function equalProperty(
|
|
388
|
+
actual: readonly unknown[] | undefined,
|
|
389
|
+
expected: readonly unknown[]
|
|
390
|
+
): boolean {
|
|
391
|
+
return (
|
|
392
|
+
actual?.length === expected.length &&
|
|
393
|
+
actual.every((value, index) => {
|
|
394
|
+
/** Boolean pointer values must still match exactly. */
|
|
395
|
+
const other = expected[index];
|
|
396
|
+
return typeof value === 'number' && typeof other === 'number'
|
|
397
|
+
? closeComponent(value, other, 1e-15)
|
|
398
|
+
: value === other;
|
|
399
|
+
})
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** Browser GPU poses are float32 while source transforms and export are JavaScript numbers. */
|
|
404
|
+
function closeComponent(first: number, second: number, minimumMagnitude = 1): boolean {
|
|
405
|
+
return (
|
|
406
|
+
Math.abs(first - second) <= Math.max(minimumMagnitude, Math.abs(first), Math.abs(second)) * 2e-6
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
|
|
213
410
|
function writePointer(document: object, pointer: string, value: unknown): void {
|
|
214
411
|
const keys = pointer.slice(1).split('/');
|
|
215
412
|
let object: Record<string, unknown> = document as Record<string, unknown>;
|
|
@@ -95,6 +95,6 @@ export function makeGLTFPhysicsDefinition(
|
|
|
95
95
|
}
|
|
96
96
|
export {makeGLTFPhysicsScene} from './scene-bindings';
|
|
97
97
|
|
|
98
|
-
export {exportGLTFPhysicsState} from './export-state';
|
|
98
|
+
export {exportGLTFPhysicsState, type GLTFPhysicsExportOptions} from './export-state';
|
|
99
99
|
|
|
100
100
|
export {snapshotGLTFPhysicsScene} from './snapshot-scene';
|
|
@@ -248,7 +248,9 @@ export function parseGLTFPhysics(
|
|
|
248
248
|
const shapeRoot =
|
|
249
249
|
source.extensions?.[SHAPES] === undefined ? {} : record(source.extensions[SHAPES]);
|
|
250
250
|
checkFields(shapeRoot, ['shapes']);
|
|
251
|
-
shapes
|
|
251
|
+
if (source.shapes && source.extensions?.[SHAPES])
|
|
252
|
+
throw new Error('Physics shape indices require exactly one shape namespace');
|
|
253
|
+
shapes = source.shapes ? [...source.shapes] : array(shapeRoot['shapes']);
|
|
252
254
|
} catch (error) {
|
|
253
255
|
report('invalid-extension', '/extensions', error);
|
|
254
256
|
}
|
|
@@ -796,6 +798,7 @@ export function parseGLTFPhysics(
|
|
|
796
798
|
typeof pointer === 'string' &&
|
|
797
799
|
(pointer.includes(PHYSICS) ||
|
|
798
800
|
pointer.includes(SHAPES) ||
|
|
801
|
+
pointer.startsWith('/shapes/') ||
|
|
799
802
|
Boolean(nodeMatch && relevant.has(Number(nodeMatch[1]))));
|
|
800
803
|
const targetNode = nodeMatch ? Number(nodeMatch[1]) : channel.target.node;
|
|
801
804
|
const targetPath = nodeMatch ? nodeMatch[2] : channel.target.path;
|
|
@@ -878,7 +881,7 @@ export function parseGLTFPhysics(
|
|
|
878
881
|
result.animatedColliderIndices.push(collider.colliderIndex);
|
|
879
882
|
if (affected.length || affectedColliders.length) {
|
|
880
883
|
for (const body of affected) {
|
|
881
|
-
body.body.mode = 'kinematic';
|
|
884
|
+
if (!options.preserveAnimatedBodyModes) body.body.mode = 'kinematic';
|
|
882
885
|
if (!result.animatedBodyIndices.includes(body.bodyIndex))
|
|
883
886
|
result.animatedBodyIndices.push(body.bodyIndex);
|
|
884
887
|
}
|
|
@@ -12,6 +12,8 @@ import type {
|
|
|
12
12
|
|
|
13
13
|
/** Raw glTF JSON, before loaders.gl replaces node indices with objects. */
|
|
14
14
|
export type GLTFPhysicsDocument = {
|
|
15
|
+
/** Core analytic shapes in the glTF 2.1 document. */
|
|
16
|
+
shapes?: readonly unknown[];
|
|
15
17
|
scene?: number;
|
|
16
18
|
scenes?: readonly {nodes?: readonly number[]}[];
|
|
17
19
|
nodes?: readonly {
|
|
@@ -101,6 +103,8 @@ export type ParseGLTFPhysicsOptions = {
|
|
|
101
103
|
propertyBindings?: boolean;
|
|
102
104
|
/** Bind rigid node translation/rotation channels through ScenegraphPhysicsMotion. */
|
|
103
105
|
animationOwnership?: 'scenegraph';
|
|
106
|
+
/** Keep authored motion modes so a session can release animation-owned bodies into dynamics. */
|
|
107
|
+
preserveAnimatedBodyModes?: boolean;
|
|
104
108
|
scene?: number;
|
|
105
109
|
/** Decode one node's own mesh at its initial pose in that node's local space.
|
|
106
110
|
* Include initial skinning and morph weights; the adapter combines child transforms.
|
|
@@ -61,13 +61,21 @@ export function makeGLTFPhysicsProperties(
|
|
|
61
61
|
});
|
|
62
62
|
}
|
|
63
63
|
// Source indices and unit conversion belong to the importer; runtime mutation is shared.
|
|
64
|
+
/** Preserve the source namespace in animation pointers and native export. */
|
|
65
|
+
const shapePointer = description.source.shapes
|
|
66
|
+
? '/shapes'
|
|
67
|
+
: '/extensions/KHR_implicit_shapes/shapes';
|
|
68
|
+
/** Both profiles use the same analytic physics property bindings. */
|
|
64
69
|
const shapes =
|
|
70
|
+
(description.source.shapes as Record<string, unknown>[] | undefined) ??
|
|
65
71
|
(
|
|
66
72
|
description.source.extensions?.['KHR_implicit_shapes'] as
|
|
67
73
|
| {shapes?: Record<string, unknown>[]}
|
|
68
74
|
| undefined
|
|
69
|
-
)?.shapes ??
|
|
75
|
+
)?.shapes ??
|
|
76
|
+
[];
|
|
70
77
|
for (const [shapeIndex, source] of shapes.entries()) {
|
|
78
|
+
if (!source || typeof source !== 'object') continue;
|
|
71
79
|
const type = source['type'];
|
|
72
80
|
const fields =
|
|
73
81
|
type === 'sphere'
|
|
@@ -109,7 +117,7 @@ export function makeGLTFPhysicsProperties(
|
|
|
109
117
|
]
|
|
110
118
|
};
|
|
111
119
|
});
|
|
112
|
-
targets.set(
|
|
120
|
+
targets.set(`${shapePointer}/${shapeIndex}/${type}/${field}`, {
|
|
113
121
|
type: 'shape',
|
|
114
122
|
colliders,
|
|
115
123
|
property: property as Extract<PhysicsPropertyTarget, {type: 'shape'}>['property']
|
|
@@ -636,6 +636,8 @@ export type GLTFNodePostprocessed = {
|
|
|
636
636
|
mesh?: GLTFMeshPostprocessed;
|
|
637
637
|
/** Index of the draft glTF 2.1 external asset instantiated by this node. */
|
|
638
638
|
externalAsset?: GlTfId;
|
|
639
|
+
/** Draft glTF 2.1 authored spatial bound. */
|
|
640
|
+
boundingVolume?: import('./gltf-shape-schema').GLTFBoundingVolume;
|
|
639
641
|
/**
|
|
640
642
|
* The node's unit quaternion rotation in the order (x, y, z, w), where w is the scalar.
|
|
641
643
|
*/
|
|
@@ -781,6 +783,8 @@ export type GLTFPostprocessed = {
|
|
|
781
783
|
scene?: GLTFScenePostprocessed;
|
|
782
784
|
/** An array of scenes. */
|
|
783
785
|
scenes: GLTFScenePostprocessed[];
|
|
786
|
+
/** Draft glTF 2.1 analytic shapes shared by authored bounds and physics. */
|
|
787
|
+
shapes?: import('./gltf-shape-schema').GLTFShape[];
|
|
784
788
|
/** An array of skins. */
|
|
785
789
|
skins: GLTFSkinPostprocessed[];
|
|
786
790
|
/** An array of textures. */
|
|
@@ -93,6 +93,8 @@ export type GLTFWithBuffers = {
|
|
|
93
93
|
images?: (GLTFImageData | null)[];
|
|
94
94
|
binary?: ArrayBuffer;
|
|
95
95
|
baseUri?: string;
|
|
96
|
+
/** Decoded glTF 2.1 dependencies, indexed by the source externalAssets array. */
|
|
97
|
+
externalAssets?: Array<GLTFWithBuffers | null>;
|
|
96
98
|
/** Gaussian primitive descriptors supplied by the loader. */
|
|
97
99
|
gaussianSplatPrimitives?: GLTFGaussianSplatDescriptor[];
|
|
98
100
|
};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {Buffer, type CommandEncoder, type Device} from '@luma.gl/core';
|
|
6
|
+
import {Matrix4} from '@math.gl/core/matrix';
|
|
7
|
+
import {Computation} from '../../compute/computation';
|
|
8
|
+
import type {SkinBinding, SkinController} from '../../scenegraph/controllers/skin-controller';
|
|
9
|
+
import type {ScenegraphPhysicsPublication} from './scenegraph-publication';
|
|
10
|
+
|
|
11
|
+
/** Writes the same mesh-local matrices consumed by the standard skin shader. */
|
|
12
|
+
const PALETTE_SOURCE = /* wgsl */ `
|
|
13
|
+
@group(0) @binding(0) var<storage, read> bodyPoses: array<mat4x4<f32>>;
|
|
14
|
+
@group(0) @binding(1) var<storage, read> bodyIndices: array<u32>;
|
|
15
|
+
@group(0) @binding(2) var<storage, read> paletteTransforms: array<mat4x4<f32>>;
|
|
16
|
+
@group(0) @binding(3) var<storage, read_write> jointPalette: array<mat4x4<f32>>;
|
|
17
|
+
|
|
18
|
+
@compute @workgroup_size(32)
|
|
19
|
+
fn main(@builtin(global_invocation_id) invocation: vec3<u32>) {
|
|
20
|
+
let joint = invocation.x;
|
|
21
|
+
if (joint >= arrayLength(&bodyIndices)) { return; }
|
|
22
|
+
jointPalette[joint] = paletteTransforms[0] * bodyPoses[bodyIndices[joint]]
|
|
23
|
+
* paletteTransforms[joint + 1];
|
|
24
|
+
}
|
|
25
|
+
`;
|
|
26
|
+
|
|
27
|
+
/** Connects a physics publication to an existing WebGPU skin palette. */
|
|
28
|
+
export class ScenegraphRagdoll {
|
|
29
|
+
/** One body index for each skin joint, in original palette order. */
|
|
30
|
+
private readonly indices: Buffer;
|
|
31
|
+
/** Inverse mesh transform followed by body-local joint and inverse bind transforms. */
|
|
32
|
+
private readonly transforms: Buffer;
|
|
33
|
+
/** Borrowed storage palette owned by the SkinController. */
|
|
34
|
+
private readonly palette: Buffer;
|
|
35
|
+
/** Compute pipeline used exclusively to transfer solved poses to this skin. */
|
|
36
|
+
private readonly computation: Computation;
|
|
37
|
+
/** Identifies the publication revision from which the joint mappings were built. */
|
|
38
|
+
private publication?: ScenegraphPhysicsPublication;
|
|
39
|
+
/** Releases exclusive GPU write access to the palette. */
|
|
40
|
+
private releasePalette?: () => void;
|
|
41
|
+
|
|
42
|
+
/** Use an existing skin and its WebGPU storage palette. The caller owns its physics session. */
|
|
43
|
+
constructor(
|
|
44
|
+
device: Device,
|
|
45
|
+
private readonly skins: SkinController,
|
|
46
|
+
private readonly binding: SkinBinding
|
|
47
|
+
) {
|
|
48
|
+
if (
|
|
49
|
+
device.type !== 'webgpu' ||
|
|
50
|
+
!(binding.skinJointMatrices instanceof Buffer) ||
|
|
51
|
+
!binding.joints.length ||
|
|
52
|
+
!skins.bindings.includes(binding)
|
|
53
|
+
) {
|
|
54
|
+
throw new Error('ScenegraphRagdoll requires a WebGPU storage skin palette');
|
|
55
|
+
}
|
|
56
|
+
this.palette = binding.skinJointMatrices;
|
|
57
|
+
this.computation = new Computation(device, {id: 'ragdoll-palette', source: PALETTE_SOURCE});
|
|
58
|
+
this.indices = device.createBuffer({
|
|
59
|
+
id: 'ragdoll-body-indices',
|
|
60
|
+
byteLength: binding.joints.length * 4,
|
|
61
|
+
usage: Buffer.STORAGE | Buffer.COPY_DST
|
|
62
|
+
});
|
|
63
|
+
this.transforms = device.createBuffer({
|
|
64
|
+
id: 'ragdoll-palette-transforms',
|
|
65
|
+
byteLength: (binding.joints.length + 1) * 64,
|
|
66
|
+
usage: Buffer.STORAGE | Buffer.COPY_DST
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Encode after physics and before drawing. Supply the full mesh matrix in the publication's origin-relative frame. */
|
|
71
|
+
encode(
|
|
72
|
+
encoder: CommandEncoder,
|
|
73
|
+
publication: ScenegraphPhysicsPublication,
|
|
74
|
+
meshWorldMatrix: Matrix4
|
|
75
|
+
): void {
|
|
76
|
+
if (publication !== this.publication) this.bindPublication(publication);
|
|
77
|
+
/** Only the authored mesh frame is uploaded each frame. The solved transforms stay on the GPU. */
|
|
78
|
+
const inverseMesh = new Float32Array(meshWorldMatrix.clone().invert());
|
|
79
|
+
this.transforms.write(inverseMesh);
|
|
80
|
+
this.computation.predraw(encoder);
|
|
81
|
+
/** Schedule palette generation after physics pose publication in the same command buffer. */
|
|
82
|
+
const pass = encoder.beginComputePass({id: 'ragdoll-skinning'});
|
|
83
|
+
this.computation.dispatch(pass, Math.ceil(this.binding.joints.length / 32));
|
|
84
|
+
pass.end();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Return palette control to the scenegraph animator before sampling a fresh animation pose. */
|
|
88
|
+
reset(): void {
|
|
89
|
+
this.releasePalette?.();
|
|
90
|
+
this.releasePalette = undefined;
|
|
91
|
+
this.publication = undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Release compute resources. Destroy the ragdoll before its shared skin controller. */
|
|
95
|
+
destroy(): void {
|
|
96
|
+
this.reset();
|
|
97
|
+
this.computation.destroy();
|
|
98
|
+
this.indices.destroy();
|
|
99
|
+
this.transforms.destroy();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Resolve joints through the same scenegraph publication used by rigid rendering. */
|
|
103
|
+
private bindPublication(publication: ScenegraphPhysicsPublication): void {
|
|
104
|
+
/** Authoring resolves each joint to its closest ancestor body and body-local frame. */
|
|
105
|
+
const indices = new Uint32Array(this.binding.joints.length);
|
|
106
|
+
/** Fold each inverse bind matrix into its body-local joint frame once per handoff. */
|
|
107
|
+
const transforms = new Float32Array(this.binding.joints.length * 16);
|
|
108
|
+
for (const [index, joint] of this.binding.joints.entries()) {
|
|
109
|
+
/** Bones and rigid joint attachment nodes remain distinct scenegraph objects. */
|
|
110
|
+
const target = publication.bindings.get(joint);
|
|
111
|
+
if (!target) throw new Error(`No published rigid body for skin joint ${joint.id}`);
|
|
112
|
+
indices[index] = target.body;
|
|
113
|
+
/** The publication's transform retains authored scale and mesh-to-body offsets. */
|
|
114
|
+
const transform = target.localMatrix.clone();
|
|
115
|
+
if (this.binding.inverseBindMatrices) {
|
|
116
|
+
transform.multiplyRight(
|
|
117
|
+
this.binding.inverseBindMatrices.subarray(index * 16, index * 16 + 16)
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
transforms.set(transform, index * 16);
|
|
121
|
+
}
|
|
122
|
+
this.releasePalette ??= this.skins.claimGPUPalette(this.binding);
|
|
123
|
+
this.indices.write(indices);
|
|
124
|
+
this.transforms.write(transforms, 64);
|
|
125
|
+
this.computation.setBindings({
|
|
126
|
+
bodyPoses: publication.matrices,
|
|
127
|
+
bodyIndices: this.indices,
|
|
128
|
+
paletteTransforms: this.transforms,
|
|
129
|
+
jointPalette: this.palette
|
|
130
|
+
});
|
|
131
|
+
this.publication = publication;
|
|
132
|
+
}
|
|
133
|
+
}
|