@luma.gl/engine 10.0.0-alpha.6 → 10.0.0-alpha.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/README.md +2 -2
- package/dist/arrow/arrow-geometry.cjs +1 -1
- package/dist/arrow/arrow-geometry.js +1 -1
- package/dist/arrow/arrow-picking.cjs +1 -1
- package/dist/arrow/arrow-picking.d.ts +1 -1
- package/dist/arrow/arrow-picking.js +1 -1
- package/dist/arrow/renderers/path/conversion/arrow-path-gpu-vectors.cjs +5 -5
- package/dist/arrow/renderers/path/conversion/arrow-path-gpu-vectors.d.ts +2 -2
- package/dist/arrow/renderers/path/conversion/arrow-path-gpu-vectors.js +5 -5
- package/dist/arrow/renderers/path/conversion/arrow-path-storage-gpu-vectors.cjs +3 -3
- package/dist/arrow/renderers/path/conversion/arrow-path-storage-gpu-vectors.js +3 -3
- package/dist/arrow/renderers/path/source/arrow-path-source-mapping.cjs +1 -1
- package/dist/arrow/renderers/path/source/arrow-path-source-mapping.js +1 -1
- package/dist/arrow/renderers/path/transforms/close-arrow-paths.cjs +5 -5
- package/dist/arrow/renderers/path/transforms/close-arrow-paths.d.ts +1 -1
- package/dist/arrow/renderers/path/transforms/close-arrow-paths.js +5 -5
- package/dist/arrow/renderers/path/transforms/gpu-path-float64-deltas.cjs +3 -3
- package/dist/arrow/renderers/path/transforms/gpu-path-float64-deltas.d.ts +1 -1
- package/dist/arrow/renderers/path/transforms/gpu-path-float64-deltas.js +3 -3
- package/dist/arrow/renderers/path/transforms/path-view-origins.cjs +1 -1
- package/dist/arrow/renderers/path/transforms/path-view-origins.d.ts +1 -1
- package/dist/arrow/renderers/path/transforms/path-view-origins.js +1 -1
- package/dist/arrow/renderers/polygon/conversion/arrow-polygon-gpu-vectors.cjs +1 -1
- package/dist/arrow/renderers/polygon/conversion/arrow-polygon-gpu-vectors.d.ts +1 -1
- package/dist/arrow/renderers/polygon/conversion/arrow-polygon-gpu-vectors.js +1 -1
- package/dist/arrow/renderers/polygon/renderers/arrow-polygon-renderer.cjs +4 -4
- package/dist/arrow/renderers/polygon/renderers/arrow-polygon-renderer.js +4 -4
- package/dist/arrow/renderers/polygon/source/arrow-polygon-source-mapping.cjs +1 -1
- package/dist/arrow/renderers/polygon/source/arrow-polygon-source-mapping.js +1 -1
- package/dist/arrow/renderers/splats/conversion/make-gpu-splat-data-from-arrow.cjs +1 -1
- package/dist/arrow/renderers/splats/conversion/make-gpu-splat-data-from-arrow.js +1 -1
- package/dist/arrow/renderers/text/conversion/convert-arrow-text-to-attribute.cjs +1 -1
- package/dist/arrow/renderers/text/conversion/convert-arrow-text-to-attribute.d.ts +1 -1
- package/dist/arrow/renderers/text/conversion/convert-arrow-text-to-attribute.js +1 -1
- package/dist/arrow/renderers/text/conversion/convert-arrow-text-vectors.cjs +5 -5
- package/dist/arrow/renderers/text/conversion/convert-arrow-text-vectors.d.ts +1 -1
- package/dist/arrow/renderers/text/conversion/convert-arrow-text-vectors.js +5 -5
- package/dist/arrow/renderers/text/conversion/make-gpu-text-data-from-arrow.cjs +1 -1
- package/dist/arrow/renderers/text/conversion/make-gpu-text-data-from-arrow.js +1 -1
- package/dist/arrow/renderers/text/renderers/arrow-text-picking.cjs +1 -1
- package/dist/arrow/renderers/text/renderers/arrow-text-picking.js +1 -1
- package/dist/arrow/renderers/text/renderers/arrow-text-renderer.cjs +5 -5
- package/dist/arrow/renderers/text/renderers/arrow-text-renderer.js +5 -5
- package/dist/arrow/renderers/text/source/arrow-text-source-mapping.cjs +1 -1
- package/dist/arrow/renderers/text/source/arrow-text-source-mapping.js +1 -1
- package/dist/compute/computation.cjs +5 -2
- package/dist/compute/computation.d.ts +1 -1
- package/dist/compute/computation.d.ts.map +1 -1
- package/dist/compute/computation.js +5 -2
- package/dist/compute/computation.js.map +1 -1
- package/dist/compute/texture-transform.d.ts +2 -2
- package/dist/compute/texture-transform.d.ts.map +1 -1
- package/dist/compute/texture-transform.js.map +1 -1
- package/dist/dist.dev.js +889 -597
- package/dist/dist.min.js +64 -59
- package/dist/experimental/materials/emissive-material.cjs +1 -1
- package/dist/experimental/materials/emissive-material.d.ts +1 -1
- package/dist/experimental/materials/emissive-material.d.ts.map +1 -1
- package/dist/experimental/materials/emissive-material.js +1 -1
- package/dist/experimental/materials/emissive-material.js.map +1 -1
- package/dist/experimental/materials/glass-material.cjs +1 -1
- package/dist/experimental/materials/glass-material.d.ts +1 -1
- package/dist/experimental/materials/glass-material.d.ts.map +1 -1
- package/dist/experimental/materials/glass-material.js +1 -1
- package/dist/experimental/materials/glass-material.js.map +1 -1
- package/dist/experimental/materials/glass-transmission.cjs +1 -1
- package/dist/experimental/materials/glass-transmission.d.ts +1 -1
- package/dist/experimental/materials/glass-transmission.d.ts.map +1 -1
- package/dist/experimental/materials/glass-transmission.js +1 -1
- package/dist/experimental/materials/glass-transmission.js.map +1 -1
- package/dist/experimental/materials/index.cjs.map +1 -1
- package/dist/experimental/materials/optical-caustics.cjs +1 -1
- package/dist/experimental/materials/optical-caustics.d.ts +1 -1
- package/dist/experimental/materials/optical-caustics.d.ts.map +1 -1
- package/dist/experimental/materials/optical-caustics.js +1 -1
- package/dist/experimental/materials/optical-caustics.js.map +1 -1
- package/dist/experimental/materials/optical-point-lights.cjs +1 -1
- package/dist/experimental/materials/optical-point-lights.d.ts +1 -1
- package/dist/experimental/materials/optical-point-lights.d.ts.map +1 -1
- package/dist/experimental/materials/optical-point-lights.js +1 -1
- package/dist/experimental/materials/optical-point-lights.js.map +1 -1
- package/dist/experimental/materials/reflective-material.cjs +1 -1
- package/dist/experimental/materials/reflective-material.d.ts +1 -1
- package/dist/experimental/materials/reflective-material.d.ts.map +1 -1
- package/dist/experimental/materials/reflective-material.js +1 -1
- package/dist/experimental/materials/reflective-material.js.map +1 -1
- package/dist/geometry/gpu-geometry.cjs +1 -1
- package/dist/geometry/gpu-geometry.d.ts.map +1 -1
- package/dist/geometry/gpu-geometry.js +1 -1
- package/dist/geometry/gpu-geometry.js.map +1 -1
- package/dist/gltf/gltf/gltf-extension-support.cjs +25 -5
- package/dist/gltf/gltf/gltf-extension-support.d.ts.map +1 -1
- package/dist/gltf/gltf/gltf-extension-support.js +27 -5
- package/dist/gltf/gltf/gltf-extension-support.js.map +1 -1
- package/dist/gltf/gltf/gltf-lod.cjs +21 -6
- package/dist/gltf/gltf/gltf-lod.d.ts.map +1 -1
- package/dist/gltf/gltf/gltf-lod.js +20 -4
- package/dist/gltf/gltf/gltf-lod.js.map +1 -1
- package/dist/gltf/index.cjs.map +1 -1
- package/dist/gltf/types/gltf-json-schema.d.ts +37 -37
- package/dist/gltf/types/gltf-json-schema.d.ts.map +1 -1
- package/dist/gltf/types/gltf-postprocessed-schema.d.ts +28 -28
- package/dist/gltf/types/gltf-postprocessed-schema.d.ts.map +1 -1
- package/dist/graph/models/index.cjs.map +3 -3
- package/dist/graph/models/path/path-attribute-model.cjs +2 -2
- package/dist/graph/models/path/path-attribute-model.d.ts +2 -2
- package/dist/graph/models/path/path-attribute-model.js +2 -2
- package/dist/graph/models/path/path-storage-model.cjs +1 -1
- package/dist/graph/models/path/path-storage-model.d.ts +1 -1
- package/dist/graph/models/path/path-storage-model.js +1 -1
- package/dist/graph/models/path/path-trips-storage-model.d.ts +2 -2
- package/dist/graph/models/polygon/polygon-attribute-model.cjs +2 -2
- package/dist/graph/models/polygon/polygon-attribute-model.js +2 -2
- package/dist/graph/models/polygon/polygon-storage-model.cjs +2 -2
- package/dist/graph/models/polygon/polygon-storage-model.js +2 -2
- package/dist/graph/physics/index.cjs +3 -0
- package/dist/graph/physics/index.cjs.map +3 -3
- package/dist/graph/physics/index.d.ts +2 -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/physics-controller-geometry.cjs +203 -0
- package/dist/graph/physics/physics-controller-geometry.d.ts +6 -0
- package/dist/graph/physics/physics-controller-geometry.d.ts.map +1 -0
- package/dist/graph/physics/physics-controller-geometry.js +205 -0
- package/dist/graph/physics/physics-controller-geometry.js.map +1 -0
- package/dist/graph/physics/physics-controller.cjs +277 -0
- package/dist/graph/physics/physics-controller.d.ts +90 -0
- package/dist/graph/physics/physics-controller.d.ts.map +1 -0
- package/dist/graph/physics/physics-controller.js +266 -0
- package/dist/graph/physics/physics-controller.js.map +1 -0
- package/dist/graph/physics/scenegraph-instance.cjs +11 -2
- package/dist/graph/physics/scenegraph-instance.d.ts +3 -1
- package/dist/graph/physics/scenegraph-instance.d.ts.map +1 -1
- package/dist/graph/physics/scenegraph-instance.js +11 -0
- package/dist/graph/physics/scenegraph-instance.js.map +1 -1
- package/dist/graph/physics/scenegraph-motion.cjs +3 -3
- package/dist/graph/physics/scenegraph-motion.d.ts +5 -1
- package/dist/graph/physics/scenegraph-motion.d.ts.map +1 -1
- package/dist/graph/physics/scenegraph-motion.js +3 -3
- package/dist/graph/physics/scenegraph-motion.js.map +1 -1
- package/dist/graph/physics/scenegraph-publication.cjs +13 -3
- package/dist/graph/physics/scenegraph-publication.d.ts +4 -2
- package/dist/graph/physics/scenegraph-publication.d.ts.map +1 -1
- package/dist/graph/physics/scenegraph-publication.js +14 -3
- package/dist/graph/physics/scenegraph-publication.js.map +1 -1
- package/dist/graph/physics/scenegraph-session.cjs +11 -1
- package/dist/graph/physics/scenegraph-session.d.ts +11 -2
- package/dist/graph/physics/scenegraph-session.d.ts.map +1 -1
- package/dist/graph/physics/scenegraph-session.js +13 -1
- package/dist/graph/physics/scenegraph-session.js.map +1 -1
- package/dist/graph/tables/gpu-table-model.cjs +1 -1
- package/dist/graph/tables/gpu-table-model.js +1 -1
- package/dist/graph/tables/gpu-table-transform.cjs +2 -2
- package/dist/graph/tables/gpu-table-transform.d.ts +1 -1
- package/dist/graph/tables/gpu-table-transform.js +2 -2
- package/dist/graph/tables/index.cjs.map +3 -3
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/model/batch-uniform-buffer.cjs +70 -0
- package/dist/model/batch-uniform-buffer.d.ts +21 -0
- package/dist/model/batch-uniform-buffer.d.ts.map +1 -0
- package/dist/model/batch-uniform-buffer.js +48 -0
- package/dist/model/batch-uniform-buffer.js.map +1 -0
- package/dist/model/gpu-vector-bindings.cjs +57 -16
- package/dist/model/gpu-vector-bindings.d.ts.map +1 -1
- package/dist/model/gpu-vector-bindings.js +65 -15
- package/dist/model/gpu-vector-bindings.js.map +1 -1
- package/dist/model/gpu-vector-draw-batches.cjs +4 -2
- package/dist/model/gpu-vector-draw-batches.d.ts.map +1 -1
- package/dist/model/gpu-vector-draw-batches.js +5 -2
- package/dist/model/gpu-vector-draw-batches.js.map +1 -1
- package/dist/model/gpu-vector-model-batches.d.ts +5 -0
- package/dist/model/gpu-vector-model-batches.d.ts.map +1 -1
- package/dist/model/gpu-vector-model-batches.js.map +1 -1
- package/dist/model/gpu-vector-model.cjs +144 -81
- package/dist/model/gpu-vector-model.d.ts +9 -2
- package/dist/model/gpu-vector-model.d.ts.map +1 -1
- package/dist/model/gpu-vector-model.js +173 -100
- package/dist/model/gpu-vector-model.js.map +1 -1
- package/dist/model/model.cjs +8 -0
- package/dist/model/model.d.ts +3 -0
- package/dist/model/model.d.ts.map +1 -1
- package/dist/model/model.js +8 -0
- package/dist/model/model.js.map +1 -1
- package/dist/scenegraph/geometry-node.cjs +47 -0
- package/dist/scenegraph/geometry-node.d.ts +13 -0
- package/dist/scenegraph/geometry-node.d.ts.map +1 -0
- package/dist/scenegraph/geometry-node.js +26 -0
- package/dist/scenegraph/geometry-node.js.map +1 -0
- package/dist/scenegraph/group-node.cjs +42 -0
- package/dist/scenegraph/group-node.d.ts +8 -0
- package/dist/scenegraph/group-node.d.ts.map +1 -1
- package/dist/scenegraph/group-node.js +47 -1
- package/dist/scenegraph/group-node.js.map +1 -1
- package/dist/scenegraph/transform-source.cjs +15 -0
- package/dist/scenegraph/transform-source.d.ts +24 -0
- package/dist/scenegraph/transform-source.d.ts.map +1 -0
- package/dist/scenegraph/transform-source.js +5 -0
- package/dist/scenegraph/transform-source.js.map +1 -0
- package/dist/splats/gpu-splat-graph-renderer.d.ts +1 -1
- package/dist/splats/gpu-splat-graph-renderer.d.ts.map +1 -1
- package/dist/splats/index.cjs.map +1 -1
- package/dist/splats/splat-data.cjs +1 -1
- package/dist/splats/splat-data.d.ts +1 -1
- package/dist/splats/splat-data.js +1 -1
- package/dist/splats/splat-renderer.cjs +7 -2
- package/dist/splats/splat-renderer.d.ts +3 -0
- package/dist/splats/splat-renderer.d.ts.map +1 -1
- package/dist/splats/splat-renderer.js +7 -2
- package/dist/splats/splat-renderer.js.map +1 -1
- package/dist/splats/splat-sort.d.ts +1 -1
- package/dist/splats/splat-sort.d.ts.map +1 -1
- package/dist/text/experimental.cjs +151 -3
- package/dist/text/experimental.d.ts +1 -1
- package/dist/text/experimental.d.ts.map +1 -1
- package/dist/text/experimental.js +1 -1
- package/dist/text/experimental.js.map +1 -1
- package/dist/text/index.cjs +41 -3
- package/dist/text/index.d.ts +1 -1
- package/dist/text/index.d.ts.map +1 -1
- package/dist/text/index.js +1 -1
- package/dist/text/index.js.map +1 -1
- package/dist/text/text-2d/experimental.cjs +128 -18
- package/dist/text/text-2d/experimental.d.ts +8 -8
- package/dist/text/text-2d/experimental.d.ts.map +1 -1
- package/dist/text/text-2d/experimental.js +7 -8
- package/dist/text/text-2d/experimental.js.map +1 -1
- package/dist/text/text-2d/model-utils/text-model-props.d.ts +1 -1
- package/dist/text/text-2d/models/text-attribute-model.cjs +1 -1
- package/dist/text/text-2d/models/text-attribute-model.js +1 -1
- package/dist/utils/deep-equal.cjs +3 -1
- package/dist/utils/deep-equal.d.ts +1 -1
- package/dist/utils/deep-equal.d.ts.map +1 -1
- package/dist/utils/deep-equal.js +3 -1
- package/dist/utils/deep-equal.js.map +1 -1
- package/dist/utils/get-platform-info.cjs +9 -3
- package/dist/utils/get-platform-info.d.ts +1 -1
- package/dist/utils/get-platform-info.d.ts.map +1 -1
- package/dist/utils/get-platform-info.js +1 -1
- package/dist/utils/get-platform-info.js.map +1 -1
- package/package.json +8 -9
- package/src/arrow/arrow-geometry.ts +1 -1
- package/src/arrow/arrow-picking.ts +2 -2
- package/src/arrow/renderers/path/conversion/arrow-path-gpu-vectors.ts +9 -9
- package/src/arrow/renderers/path/conversion/arrow-path-storage-gpu-vectors.ts +4 -4
- package/src/arrow/renderers/path/source/arrow-path-source-mapping.ts +1 -1
- package/src/arrow/renderers/path/transforms/close-arrow-paths.ts +5 -5
- package/src/arrow/renderers/path/transforms/gpu-path-float64-deltas.ts +3 -3
- package/src/arrow/renderers/path/transforms/path-view-origins.ts +2 -2
- package/src/arrow/renderers/polygon/conversion/arrow-polygon-gpu-vectors.ts +1 -1
- package/src/arrow/renderers/polygon/renderers/arrow-polygon-renderer.ts +4 -4
- package/src/arrow/renderers/polygon/source/arrow-polygon-source-mapping.ts +1 -1
- package/src/arrow/renderers/splats/conversion/make-gpu-splat-data-from-arrow.ts +1 -1
- package/src/arrow/renderers/text/conversion/convert-arrow-text-to-attribute.ts +2 -2
- package/src/arrow/renderers/text/conversion/convert-arrow-text-to-storage.ts +1 -1
- package/src/arrow/renderers/text/conversion/convert-arrow-text-vectors.ts +6 -6
- package/src/arrow/renderers/text/conversion/make-gpu-text-data-from-arrow.ts +1 -1
- package/src/arrow/renderers/text/renderers/arrow-text-picking.ts +1 -1
- package/src/arrow/renderers/text/renderers/arrow-text-renderer.ts +5 -5
- package/src/arrow/renderers/text/source/arrow-text-source-mapping.ts +1 -1
- package/src/compute/computation.ts +8 -3
- package/src/compute/texture-transform.ts +2 -2
- package/src/experimental/materials/emissive-material.ts +1 -1
- package/src/experimental/materials/glass-material.ts +1 -1
- package/src/experimental/materials/glass-transmission.ts +1 -1
- package/src/experimental/materials/optical-caustics.ts +1 -1
- package/src/experimental/materials/optical-point-lights.ts +1 -1
- package/src/experimental/materials/reflective-material.ts +1 -1
- package/src/experimental/textures/rgba-decoder.ts +1 -1
- package/src/geometry/gpu-geometry.ts +1 -4
- package/src/gltf/gltf/gltf-extension-support.ts +27 -5
- package/src/gltf/gltf/gltf-lod.ts +22 -4
- package/src/gltf/physics/README.md +1 -1
- package/src/gltf/types/gltf-json-schema.ts +37 -37
- package/src/gltf/types/gltf-postprocessed-schema.ts +28 -28
- package/src/graph/models/path/path-attribute-model.ts +3 -3
- package/src/graph/models/path/path-storage-model.ts +2 -2
- package/src/graph/models/path/path-trips-storage-model.ts +2 -2
- package/src/graph/models/polygon/polygon-attribute-model.ts +2 -2
- package/src/graph/models/polygon/polygon-storage-model.ts +2 -2
- package/src/graph/physics/index.ts +10 -0
- package/src/graph/physics/physics-controller-geometry.ts +264 -0
- package/src/graph/physics/physics-controller.ts +348 -0
- package/src/graph/physics/scenegraph-instance.ts +12 -1
- package/src/graph/physics/scenegraph-motion.ts +14 -3
- package/src/graph/physics/scenegraph-publication.ts +24 -4
- package/src/graph/physics/scenegraph-session.ts +35 -3
- package/src/graph/tables/gpu-table-model.ts +1 -1
- package/src/graph/tables/gpu-table-transform.ts +3 -3
- package/src/index.ts +6 -0
- package/src/model/batch-uniform-buffer.ts +51 -0
- package/src/model/gpu-vector-bindings.ts +77 -19
- package/src/model/gpu-vector-draw-batches.ts +5 -2
- package/src/model/gpu-vector-model-batches.ts +5 -0
- package/src/model/gpu-vector-model.ts +194 -122
- package/src/model/model.ts +10 -0
- package/src/scenegraph/geometry-node.ts +31 -0
- package/src/scenegraph/group-node.ts +54 -1
- package/src/scenegraph/transform-source.ts +30 -0
- package/src/splats/gpu-splat-graph-renderer.ts +1 -1
- package/src/splats/splat-data.ts +1 -1
- package/src/splats/splat-renderer.ts +10 -2
- package/src/splats/splat-sort.ts +1 -1
- package/src/text/README.md +1 -1
- package/src/text/experimental.ts +122 -1
- package/src/text/index.ts +40 -1
- package/src/text/text-2d/experimental.ts +104 -8
- package/src/text/text-2d/model-utils/text-model-props.ts +1 -1
- package/src/text/text-2d/models/text-attribute-model.ts +1 -1
- package/src/utils/deep-equal.ts +4 -2
- package/src/utils/get-platform-info.ts +1 -1
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {assert} from '@luma.gl/core';
|
|
6
|
+
import {Geometry} from '@math.gl/geometry';
|
|
7
|
+
import {Matrix4} from '@math.gl/core';
|
|
8
|
+
import {
|
|
9
|
+
CubeGeometry,
|
|
10
|
+
GroupNode,
|
|
11
|
+
IcoSphereGeometry,
|
|
12
|
+
ModelNode,
|
|
13
|
+
GeometryNode,
|
|
14
|
+
SphereGeometry,
|
|
15
|
+
decomposePhysicsTransform,
|
|
16
|
+
JointController,
|
|
17
|
+
type ScenegraphJoint,
|
|
18
|
+
type ScenegraphBounds,
|
|
19
|
+
type ScenegraphNode
|
|
20
|
+
} from '../../index';
|
|
21
|
+
import {
|
|
22
|
+
makeConvexHullGeometry,
|
|
23
|
+
makeTriangleMeshGeometry,
|
|
24
|
+
scaleRigidBodyShape
|
|
25
|
+
} from '@luma.gl/graph/gpu-physics';
|
|
26
|
+
import type {
|
|
27
|
+
GPURigidBody,
|
|
28
|
+
GPURigidBodyCollider,
|
|
29
|
+
GPURigidBodyShape
|
|
30
|
+
} from '@luma.gl/graph/gpu-physics';
|
|
31
|
+
import type {PhysicsBodyOptions} from './physics-controller';
|
|
32
|
+
import type {ScenegraphPhysicsScene} from './scenegraph-session';
|
|
33
|
+
import {PhysicsGeometryNode} from './physics-scenegraph';
|
|
34
|
+
import {makeRigidBodyJointsFromScenegraph} from './scenegraph-joints';
|
|
35
|
+
|
|
36
|
+
/** Cook CPU-authored geometry once; live body poses remain independent GPU state. */
|
|
37
|
+
export function makePhysicsControllerScene(
|
|
38
|
+
scene: GroupNode,
|
|
39
|
+
definitions: ReadonlyMap<ScenegraphNode, PhysicsBodyOptions>,
|
|
40
|
+
worldOptions: Omit<ScenegraphPhysicsScene['definition']['world'], 'bodies'>,
|
|
41
|
+
joints: readonly ScenegraphJoint[] = []
|
|
42
|
+
): ScenegraphPhysicsScene {
|
|
43
|
+
const matrices = new Map<ScenegraphNode, Matrix4>();
|
|
44
|
+
const pending: {node: ScenegraphNode; parent: Matrix4}[] = [{node: scene, parent: new Matrix4()}];
|
|
45
|
+
while (pending.length) {
|
|
46
|
+
const {node, parent} = pending.pop()!;
|
|
47
|
+
// Physics authoring requires a tree, including hidden nodes.
|
|
48
|
+
assert(!matrices.has(node));
|
|
49
|
+
const matrix = new Matrix4(parent).multiplyRight(node.matrix);
|
|
50
|
+
matrices.set(node, matrix);
|
|
51
|
+
if (node instanceof GroupNode) {
|
|
52
|
+
for (let index = node.children.length - 1; index >= 0; index--)
|
|
53
|
+
pending.push({node: node.children[index], parent: matrix});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const bodies: GPURigidBody[] = [];
|
|
57
|
+
const bindings = new Map<ScenegraphNode, number>();
|
|
58
|
+
const geometries = new Map<
|
|
59
|
+
ScenegraphNode,
|
|
60
|
+
{geometry: Geometry; placements: readonly Matrix4[]}
|
|
61
|
+
>();
|
|
62
|
+
const localBounds = new Map<ScenegraphNode, ScenegraphBounds>();
|
|
63
|
+
for (const node of matrices.keys()) {
|
|
64
|
+
const source =
|
|
65
|
+
definitions.get(node)?.geometry ??
|
|
66
|
+
(node instanceof PhysicsGeometryNode || node instanceof GeometryNode
|
|
67
|
+
? node.geometry
|
|
68
|
+
: node instanceof ModelNode
|
|
69
|
+
? node.model.sourceGeometry
|
|
70
|
+
: null);
|
|
71
|
+
if (!(source instanceof Geometry)) continue;
|
|
72
|
+
const placements =
|
|
73
|
+
node instanceof ModelNode && node.instanceMatrices
|
|
74
|
+
? node.instanceMatrices.map(matrix => new Matrix4(matrix))
|
|
75
|
+
: [new Matrix4()];
|
|
76
|
+
geometries.set(node, {geometry: source, placements});
|
|
77
|
+
const positions = source.attributes['POSITION'];
|
|
78
|
+
if (!positions || positions.size !== 3) continue;
|
|
79
|
+
const minimum: [number, number, number] = [Infinity, Infinity, Infinity];
|
|
80
|
+
const maximum: [number, number, number] = [-Infinity, -Infinity, -Infinity];
|
|
81
|
+
for (const placement of placements)
|
|
82
|
+
for (let index = 0; index < positions.value.length; index += 3) {
|
|
83
|
+
const point = placement.transformAsPoint([
|
|
84
|
+
positions.value[index],
|
|
85
|
+
positions.value[index + 1],
|
|
86
|
+
positions.value[index + 2]
|
|
87
|
+
]);
|
|
88
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
89
|
+
minimum[axis] = Math.min(minimum[axis], point[axis]);
|
|
90
|
+
maximum[axis] = Math.max(maximum[axis], point[axis]);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (placements.length) localBounds.set(node, [minimum, maximum]);
|
|
94
|
+
}
|
|
95
|
+
for (const [node, options] of definitions) {
|
|
96
|
+
const matrix = matrices.get(node);
|
|
97
|
+
if (!matrix) throw new Error(`Physics node is outside the scene: ${node.id}`);
|
|
98
|
+
const {position, rotation, scale} = decomposePhysicsTransform(matrix);
|
|
99
|
+
const inverse = new Matrix4()
|
|
100
|
+
.translate(position)
|
|
101
|
+
.multiplyRight(new Matrix4().fromQuaternion(rotation))
|
|
102
|
+
.invert();
|
|
103
|
+
const colliders: GPURigidBodyCollider[] = [];
|
|
104
|
+
if (options.collider && options.collider !== 'geometry') {
|
|
105
|
+
const shape = options.collider;
|
|
106
|
+
if (shape.type === 'convex-hull' || shape.type === 'triangle-mesh') {
|
|
107
|
+
const positions = shape.geometry.positions.map((value, index) => value * scale[index % 3]);
|
|
108
|
+
const indices = [...shape.geometry.indices];
|
|
109
|
+
if (scale[0] * scale[1] * scale[2] < 0)
|
|
110
|
+
for (let index = 0; index < indices.length; index += 3)
|
|
111
|
+
[indices[index + 1], indices[index + 2]] = [indices[index + 2], indices[index + 1]];
|
|
112
|
+
colliders.push({
|
|
113
|
+
shape:
|
|
114
|
+
shape.type === 'convex-hull'
|
|
115
|
+
? {type: 'convex-hull', geometry: makeConvexHullGeometry(positions)}
|
|
116
|
+
: {type: 'triangle-mesh', geometry: makeTriangleMeshGeometry(positions, indices)}
|
|
117
|
+
});
|
|
118
|
+
} else colliders.push({shape: scaleRigidBodyShape(shape, scale.map(Math.abs))});
|
|
119
|
+
} else {
|
|
120
|
+
const descendants = [node];
|
|
121
|
+
while (descendants.length) {
|
|
122
|
+
const descendant = descendants.pop()!;
|
|
123
|
+
if (descendant !== node && definitions.has(descendant)) continue;
|
|
124
|
+
const source = geometries.get(descendant);
|
|
125
|
+
if (!source && descendant instanceof ModelNode)
|
|
126
|
+
throw new Error(
|
|
127
|
+
`Missing CPU collision geometry for model node ${descendant.id}; supply an explicit collider`
|
|
128
|
+
);
|
|
129
|
+
if (source) {
|
|
130
|
+
for (const placement of source.placements)
|
|
131
|
+
colliders.push(
|
|
132
|
+
makeGeometryCollider(
|
|
133
|
+
source.geometry,
|
|
134
|
+
new Matrix4(inverse)
|
|
135
|
+
.multiplyRight(matrices.get(descendant)!)
|
|
136
|
+
.multiplyRight(placement),
|
|
137
|
+
options.geometryMode,
|
|
138
|
+
options.motion === 'static'
|
|
139
|
+
)
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
if (descendant instanceof GroupNode) {
|
|
143
|
+
for (let index = descendant.children.length - 1; index >= 0; index--)
|
|
144
|
+
descendants.push(descendant.children[index]);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (!colliders.length)
|
|
149
|
+
throw new Error(`No CPU collision geometry for physics node: ${node.id}`);
|
|
150
|
+
const {motion = 'dynamic', collider, geometry, geometryMode, ...properties} = options;
|
|
151
|
+
bindings.set(node, bodies.length);
|
|
152
|
+
bodies.push({
|
|
153
|
+
colliders,
|
|
154
|
+
position,
|
|
155
|
+
orientation: rotation,
|
|
156
|
+
mode: motion,
|
|
157
|
+
mass: motion === 'static' ? 0 : 1,
|
|
158
|
+
...properties
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
if (!bodies.length) throw new Error('Physics scene has no bodies');
|
|
162
|
+
const resolved = new JointController(joints).resolve([scene], new Set(bindings.keys()));
|
|
163
|
+
let fixedWorldBody: number | undefined;
|
|
164
|
+
if (resolved.some(joint => !joint.first.body || !joint.second.body)) {
|
|
165
|
+
fixedWorldBody = bodies.length;
|
|
166
|
+
bodies.push({mode: 'static', mass: 0, position: [0, 0, 0], colliders: []});
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
roots: [scene],
|
|
170
|
+
localBounds,
|
|
171
|
+
definition: {
|
|
172
|
+
bodies: bindings,
|
|
173
|
+
world: {
|
|
174
|
+
...worldOptions,
|
|
175
|
+
bodies,
|
|
176
|
+
joints: resolved.flatMap(joint =>
|
|
177
|
+
makeRigidBodyJointsFromScenegraph(joint, bindings, fixedWorldBody)
|
|
178
|
+
)
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function makeGeometryCollider(
|
|
185
|
+
geometry: Geometry,
|
|
186
|
+
matrix: Matrix4,
|
|
187
|
+
mode: PhysicsBodyOptions['geometryMode'],
|
|
188
|
+
staticBody = false
|
|
189
|
+
): GPURigidBodyCollider {
|
|
190
|
+
const positions = geometry.attributes['POSITION'];
|
|
191
|
+
if (geometry.topology !== 'triangle-list' || !positions || positions.size !== 3)
|
|
192
|
+
throw new Error('Physics cooking requires triangle-list POSITION geometry');
|
|
193
|
+
const minimum = [Infinity, Infinity, Infinity];
|
|
194
|
+
const maximum = [-Infinity, -Infinity, -Infinity];
|
|
195
|
+
let radius = 0;
|
|
196
|
+
for (let index = 0; index < positions.value.length; index += 3) {
|
|
197
|
+
radius = Math.max(
|
|
198
|
+
radius,
|
|
199
|
+
Math.hypot(positions.value[index], positions.value[index + 1], positions.value[index + 2])
|
|
200
|
+
);
|
|
201
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
202
|
+
minimum[axis] = Math.min(minimum[axis], positions.value[index + axis]);
|
|
203
|
+
maximum[axis] = Math.max(maximum[axis], positions.value[index + axis]);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
let shape: GPURigidBodyShape;
|
|
207
|
+
// A flat static surface has no volumetric hull; retain its authored triangles instead.
|
|
208
|
+
if (!mode && staticBody && maximum.some((value, axis) => value === minimum[axis]))
|
|
209
|
+
mode = 'triangle-mesh';
|
|
210
|
+
const transform = decomposePhysicsTransform(matrix);
|
|
211
|
+
const scale = transform.scale.map(Math.abs);
|
|
212
|
+
const isBox =
|
|
213
|
+
geometry instanceof CubeGeometry &&
|
|
214
|
+
Array.from(positions.value).every(
|
|
215
|
+
(value, index) =>
|
|
216
|
+
Math.min(Math.abs(value - minimum[index % 3]), Math.abs(value - maximum[index % 3])) < 1e-6
|
|
217
|
+
);
|
|
218
|
+
const isSphere =
|
|
219
|
+
(geometry instanceof SphereGeometry || geometry instanceof IcoSphereGeometry) &&
|
|
220
|
+
scale.every(value => Math.abs(value - scale[0]) < 1e-6) &&
|
|
221
|
+
Array.from({length: positions.value.length / 3}, (_, index) =>
|
|
222
|
+
Math.hypot(
|
|
223
|
+
positions.value[index * 3],
|
|
224
|
+
positions.value[index * 3 + 1],
|
|
225
|
+
positions.value[index * 3 + 2]
|
|
226
|
+
)
|
|
227
|
+
).every(value => Math.abs(value - radius) < 1e-6 * Math.max(1, radius));
|
|
228
|
+
if (!mode && isBox) {
|
|
229
|
+
const halfExtents = maximum.map((value, axis) => ((value - minimum[axis]) * scale[axis]) / 2);
|
|
230
|
+
const center = matrix.transformAsPoint(
|
|
231
|
+
minimum.map((value, axis) => (value + maximum[axis]) / 2)
|
|
232
|
+
);
|
|
233
|
+
return {
|
|
234
|
+
shape: {type: 'box', halfExtents: [halfExtents[0], halfExtents[1], halfExtents[2]]},
|
|
235
|
+
position: [center[0], center[1], center[2]],
|
|
236
|
+
orientation: transform.rotation
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
if (!mode && isSphere)
|
|
240
|
+
return {
|
|
241
|
+
shape: {type: 'sphere', radius: radius * scale[0]},
|
|
242
|
+
position: transform.position,
|
|
243
|
+
orientation: transform.rotation
|
|
244
|
+
};
|
|
245
|
+
const vertices: number[] = [];
|
|
246
|
+
for (let index = 0; index < positions.value.length; index += 3)
|
|
247
|
+
vertices.push(
|
|
248
|
+
...matrix.transformAsPoint([
|
|
249
|
+
positions.value[index],
|
|
250
|
+
positions.value[index + 1],
|
|
251
|
+
positions.value[index + 2]
|
|
252
|
+
])
|
|
253
|
+
);
|
|
254
|
+
if (mode === 'triangle-mesh') {
|
|
255
|
+
const indices = geometry.indices
|
|
256
|
+
? Array.from(geometry.indices.value)
|
|
257
|
+
: Array.from({length: vertices.length / 3}, (_, index) => index);
|
|
258
|
+
if (matrix.determinant() < 0)
|
|
259
|
+
for (let index = 0; index < indices.length; index += 3)
|
|
260
|
+
[indices[index + 1], indices[index + 2]] = [indices[index + 2], indices[index + 1]];
|
|
261
|
+
shape = {type: 'triangle-mesh', geometry: makeTriangleMeshGeometry(vertices, indices)};
|
|
262
|
+
} else shape = {type: 'convex-hull', geometry: makeConvexHullGeometry(vertices)};
|
|
263
|
+
return {shape};
|
|
264
|
+
}
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {assert, type Device} from '@luma.gl/core';
|
|
6
|
+
import type {Geometry} from '@math.gl/geometry';
|
|
7
|
+
import type {
|
|
8
|
+
GPURigidBody,
|
|
9
|
+
GPURigidBodyShape,
|
|
10
|
+
GPURigidBodyWorldProps
|
|
11
|
+
} from '@luma.gl/graph/gpu-physics';
|
|
12
|
+
import {rotateRigidBodyVector} from '@luma.gl/graph/gpu-physics';
|
|
13
|
+
import {
|
|
14
|
+
GroupNode,
|
|
15
|
+
type ScenegraphNode,
|
|
16
|
+
type ScenegraphJoint,
|
|
17
|
+
type ScenegraphTransformSource
|
|
18
|
+
} from '../../index';
|
|
19
|
+
import {ScenegraphPhysicsSession} from './scenegraph-session';
|
|
20
|
+
import {makePhysicsControllerScene} from './physics-controller-geometry';
|
|
21
|
+
|
|
22
|
+
export type PhysicsNodeReference = ScenegraphNode | string;
|
|
23
|
+
export type PhysicsJointOptions = Omit<ScenegraphJoint, 'first' | 'second'> & {
|
|
24
|
+
first: PhysicsNodeReference;
|
|
25
|
+
second: PhysicsNodeReference;
|
|
26
|
+
};
|
|
27
|
+
export type PhysicsBodyOptions = Pick<
|
|
28
|
+
GPURigidBody,
|
|
29
|
+
| 'mass'
|
|
30
|
+
| 'restitution'
|
|
31
|
+
| 'friction'
|
|
32
|
+
| 'velocity'
|
|
33
|
+
| 'angularVelocity'
|
|
34
|
+
| 'centerOfMass'
|
|
35
|
+
| 'inertiaDiagonal'
|
|
36
|
+
| 'inertiaOrientation'
|
|
37
|
+
| 'gravityFactor'
|
|
38
|
+
> & {
|
|
39
|
+
motion?: 'static' | 'dynamic' | 'kinematic';
|
|
40
|
+
/** Infer CPU scene geometry by default, or supply an explicit node-local shape. */
|
|
41
|
+
collider?: 'geometry' | GPURigidBodyShape;
|
|
42
|
+
/** Optional CPU geometry for a node without retained model geometry. */
|
|
43
|
+
geometry?: Geometry;
|
|
44
|
+
/** Non-primitives default to a convex hull; concave surfaces must be explicit. */
|
|
45
|
+
geometryMode?: 'convex-hull' | 'triangle-mesh';
|
|
46
|
+
};
|
|
47
|
+
export type PhysicsControllerProps = Omit<
|
|
48
|
+
GPURigidBodyWorldProps,
|
|
49
|
+
'bodies' | 'publicationBounds' | 'publishPoses' | 'joints'
|
|
50
|
+
> & {
|
|
51
|
+
scene: GroupNode;
|
|
52
|
+
/** Bound catch-up work; excess elapsed time is reported, not silently simulated. */
|
|
53
|
+
maximumSubsteps?: number;
|
|
54
|
+
};
|
|
55
|
+
export type PhysicsBodyPose = {
|
|
56
|
+
/** World-space scene-node origin, rather than the internal center of mass. */
|
|
57
|
+
position: readonly [number, number, number];
|
|
58
|
+
rotation: readonly [number, number, number, number];
|
|
59
|
+
};
|
|
60
|
+
export type PhysicsBody = {
|
|
61
|
+
readonly node: ScenegraphNode;
|
|
62
|
+
applyImpulse(
|
|
63
|
+
impulse: readonly [number, number, number],
|
|
64
|
+
options?: {point?: readonly [number, number, number]}
|
|
65
|
+
): void;
|
|
66
|
+
setKinematicTarget(pose: PhysicsBodyPose): void;
|
|
67
|
+
/** Resume taking a kinematic target from the node's authored world transform. */
|
|
68
|
+
followScenegraph(): void;
|
|
69
|
+
/** Explicit asynchronous inspection; never called by frame updates. */
|
|
70
|
+
readPose(): Promise<PhysicsBodyPose>;
|
|
71
|
+
};
|
|
72
|
+
export type PhysicsUpdateResult = {steps: number; simulatedTime: number; droppedTime: number};
|
|
73
|
+
|
|
74
|
+
/** Application-facing scene physics, with setup-time path resolution and GPU-only frame updates. */
|
|
75
|
+
export class PhysicsController {
|
|
76
|
+
readonly scene: GroupNode;
|
|
77
|
+
readonly timeStep: number;
|
|
78
|
+
private readonly maximumSubsteps: number;
|
|
79
|
+
private readonly worldOptions: Omit<GPURigidBodyWorldProps, 'bodies'>;
|
|
80
|
+
private readonly definitions = new Map<ScenegraphNode, PhysicsBodyOptions>();
|
|
81
|
+
private readonly handles = new Map<ScenegraphNode, PhysicsBody>();
|
|
82
|
+
private readonly manualTargets = new Set<ScenegraphNode>();
|
|
83
|
+
private readonly joints: ScenegraphJoint[] = [];
|
|
84
|
+
private indices: ReadonlyMap<ScenegraphNode, number> = new Map();
|
|
85
|
+
private session?: ScenegraphPhysicsSession;
|
|
86
|
+
private pending?: Promise<unknown>;
|
|
87
|
+
private closed = false;
|
|
88
|
+
private destruction?: Promise<void>;
|
|
89
|
+
private dirty = true;
|
|
90
|
+
private accumulator = 0;
|
|
91
|
+
/** Stable proxy: renderers fetch current borrowed buffers after reset/rebuild. */
|
|
92
|
+
readonly transformSource: ScenegraphTransformSource = {
|
|
93
|
+
getFrame: () => {
|
|
94
|
+
// A pending replacement must not expose buffers being retired asynchronously.
|
|
95
|
+
if (this.closed || this.dirty || this.pending || !this.session) return null;
|
|
96
|
+
const publication = this.session.publication;
|
|
97
|
+
return {
|
|
98
|
+
matrices: publication.matrices,
|
|
99
|
+
matrixCount: this.session.world.count,
|
|
100
|
+
revision: this.session.revision,
|
|
101
|
+
bindings: publication.transformBindings
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
constructor(
|
|
107
|
+
private readonly device: Device,
|
|
108
|
+
props: PhysicsControllerProps
|
|
109
|
+
) {
|
|
110
|
+
const {scene, maximumSubsteps = 4, ...worldOptions} = props;
|
|
111
|
+
this.scene = scene;
|
|
112
|
+
this.timeStep = props.timeStep ?? 1 / 120;
|
|
113
|
+
this.maximumSubsteps = maximumSubsteps;
|
|
114
|
+
this.worldOptions = structuredClone({...worldOptions, timeStep: this.timeStep});
|
|
115
|
+
if (
|
|
116
|
+
!Number.isFinite(this.timeStep) ||
|
|
117
|
+
this.timeStep <= 0 ||
|
|
118
|
+
!Number.isSafeInteger(maximumSubsteps) ||
|
|
119
|
+
maximumSubsteps <= 0
|
|
120
|
+
)
|
|
121
|
+
throw new Error('Invalid physics timestep or substep limit');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Borrowed rendering publication. Fetch again after prepare/reset changes the revision. */
|
|
125
|
+
get publication() {
|
|
126
|
+
return this.requireReady().publication;
|
|
127
|
+
}
|
|
128
|
+
get revision(): number {
|
|
129
|
+
return this.session?.revision ?? 0;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
addBody(reference: PhysicsNodeReference, options: PhysicsBodyOptions = {}): PhysicsBody {
|
|
133
|
+
this.assertEditable();
|
|
134
|
+
const node = this.resolveNode(reference);
|
|
135
|
+
if (!node) throw new Error(`Physics node not found: ${reference}`);
|
|
136
|
+
if (this.definitions.has(node)) throw new Error(`Physics body already attached: ${node.id}`);
|
|
137
|
+
// Snapshot caller-owned options before asynchronous preparation, retaining borrowed geometry.
|
|
138
|
+
const {geometry, ...properties} = options;
|
|
139
|
+
this.definitions.set(node, {...structuredClone(properties), geometry});
|
|
140
|
+
const binding = () => {
|
|
141
|
+
const session = this.requireReady();
|
|
142
|
+
const index = this.indices.get(node);
|
|
143
|
+
if (index === undefined || this.handles.get(node) !== handle)
|
|
144
|
+
throw new Error(`Physics body is no longer attached: ${node.id}`);
|
|
145
|
+
return {session, world: session.world, index};
|
|
146
|
+
};
|
|
147
|
+
const handle: PhysicsBody = {
|
|
148
|
+
node,
|
|
149
|
+
applyImpulse: (impulse, {point} = {}) => {
|
|
150
|
+
const {world, index} = binding();
|
|
151
|
+
world.applyImpulse(index, impulse, point);
|
|
152
|
+
},
|
|
153
|
+
setKinematicTarget: pose => {
|
|
154
|
+
const {world, index} = binding();
|
|
155
|
+
if (this.definitions.get(node)?.motion !== 'kinematic')
|
|
156
|
+
throw new Error('Explicit targets require a kinematic body');
|
|
157
|
+
world.setKinematicTarget(index, pose.position, pose.rotation);
|
|
158
|
+
this.manualTargets.add(node);
|
|
159
|
+
},
|
|
160
|
+
followScenegraph: () => {
|
|
161
|
+
binding();
|
|
162
|
+
this.manualTargets.delete(node);
|
|
163
|
+
},
|
|
164
|
+
readPose: () => {
|
|
165
|
+
const {session, index} = binding();
|
|
166
|
+
return this.exclusive(() =>
|
|
167
|
+
session.withWorld(async world => {
|
|
168
|
+
const state = (await world.readState())[index];
|
|
169
|
+
const center = world.getBodyParameters(index).centerOfMass ?? [0, 0, 0];
|
|
170
|
+
const offset = rotateRigidBodyVector(state.orientation, center);
|
|
171
|
+
return {
|
|
172
|
+
position: [
|
|
173
|
+
state.position[0] - offset[0],
|
|
174
|
+
state.position[1] - offset[1],
|
|
175
|
+
state.position[2] - offset[2]
|
|
176
|
+
],
|
|
177
|
+
rotation: state.orientation
|
|
178
|
+
};
|
|
179
|
+
})
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
this.handles.set(node, handle);
|
|
184
|
+
this.dirty = true;
|
|
185
|
+
return handle;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
getBody(reference: PhysicsNodeReference): PhysicsBody | undefined {
|
|
189
|
+
if (this.closed) return undefined;
|
|
190
|
+
const node = this.resolveNode(reference);
|
|
191
|
+
return node ? this.handles.get(node) : undefined;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
removeBody(reference: PhysicsNodeReference): boolean {
|
|
195
|
+
this.assertEditable();
|
|
196
|
+
const node = this.resolveNode(reference);
|
|
197
|
+
if (!node || !this.definitions.delete(node)) return false;
|
|
198
|
+
this.handles.delete(node);
|
|
199
|
+
this.manualTargets.delete(node);
|
|
200
|
+
this.dirty = true;
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
addJoint(options: PhysicsJointOptions): ScenegraphJoint {
|
|
205
|
+
this.assertEditable();
|
|
206
|
+
const {first, second, ...properties} = options;
|
|
207
|
+
const firstNode = this.resolveNode(first),
|
|
208
|
+
secondNode = this.resolveNode(second);
|
|
209
|
+
if (!firstNode || !secondNode) throw new Error('Physics joint attachment not found');
|
|
210
|
+
const joint = {...structuredClone(properties), first: firstNode, second: secondNode};
|
|
211
|
+
this.joints.push(joint);
|
|
212
|
+
this.dirty = true;
|
|
213
|
+
return joint;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
removeJoint(joint: ScenegraphJoint): boolean {
|
|
217
|
+
this.assertEditable();
|
|
218
|
+
const index = this.joints.indexOf(joint);
|
|
219
|
+
if (index < 0) return false;
|
|
220
|
+
this.joints.splice(index, 1);
|
|
221
|
+
this.dirty = true;
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Compile once, or explicitly rebuild after body, geometry, scale or topology edits. */
|
|
226
|
+
prepare({preserveState = true}: {preserveState?: boolean} = {}): Promise<void> {
|
|
227
|
+
return this.exclusive(async () => {
|
|
228
|
+
// Destruction can begin before this queued preparation starts.
|
|
229
|
+
assert(!this.closed);
|
|
230
|
+
if (this.scene.transformSource && this.scene.transformSource !== this.transformSource)
|
|
231
|
+
throw new Error('Scene already has a transform source');
|
|
232
|
+
// Claim before the first await so another controller cannot prepare this root concurrently.
|
|
233
|
+
this.scene.transformSource = this.transformSource;
|
|
234
|
+
try {
|
|
235
|
+
this.dirty = true;
|
|
236
|
+
const scene = makePhysicsControllerScene(
|
|
237
|
+
this.scene,
|
|
238
|
+
this.definitions,
|
|
239
|
+
this.worldOptions,
|
|
240
|
+
this.joints
|
|
241
|
+
);
|
|
242
|
+
if (this.session) await this.session.rebuild(async () => scene, preserveState);
|
|
243
|
+
else this.session = await ScenegraphPhysicsSession.create(this.device, scene);
|
|
244
|
+
this.indices = scene.definition.bodies;
|
|
245
|
+
this.manualTargets.clear();
|
|
246
|
+
this.accumulator = 0;
|
|
247
|
+
this.dirty = false;
|
|
248
|
+
} catch (error) {
|
|
249
|
+
if (!this.session && this.scene.transformSource === this.transformSource)
|
|
250
|
+
this.scene.transformSource = null;
|
|
251
|
+
throw error;
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** Synchronously submit bounded fixed steps; rendering can follow on the same GPU queue. */
|
|
257
|
+
update(
|
|
258
|
+
deltaTime: number,
|
|
259
|
+
{
|
|
260
|
+
animatedNodes = new Set<ScenegraphNode>()
|
|
261
|
+
}: {
|
|
262
|
+
animatedNodes?: ReadonlySet<ScenegraphNode>;
|
|
263
|
+
} = {}
|
|
264
|
+
): PhysicsUpdateResult {
|
|
265
|
+
const session = this.requireReady();
|
|
266
|
+
if (!Number.isFinite(deltaTime) || deltaTime < 0) throw new Error('Invalid physics frame time');
|
|
267
|
+
const available = this.accumulator + deltaTime;
|
|
268
|
+
const maximumTime = this.timeStep * this.maximumSubsteps;
|
|
269
|
+
this.accumulator = Math.min(available, maximumTime);
|
|
270
|
+
const steps = Math.min(
|
|
271
|
+
this.maximumSubsteps,
|
|
272
|
+
Math.floor((this.accumulator + this.timeStep * 1e-9) / this.timeStep)
|
|
273
|
+
);
|
|
274
|
+
if (steps) {
|
|
275
|
+
const targetNodes = new Set<ScenegraphNode>();
|
|
276
|
+
for (const [node, options] of this.definitions)
|
|
277
|
+
if (options.motion === 'kinematic' && !this.manualTargets.has(node)) targetNodes.add(node);
|
|
278
|
+
const encoder = this.device.createCommandEncoder();
|
|
279
|
+
session.encode(encoder, {
|
|
280
|
+
steps,
|
|
281
|
+
animatedNodes,
|
|
282
|
+
targetNodes,
|
|
283
|
+
excludedNodes: this.manualTargets
|
|
284
|
+
});
|
|
285
|
+
this.device.submit(encoder.finish());
|
|
286
|
+
this.accumulator = Math.max(0, this.accumulator - steps * this.timeStep);
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
steps,
|
|
290
|
+
simulatedTime: steps * this.timeStep,
|
|
291
|
+
droppedTime: Math.max(0, available - maximumTime)
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Restore authored bodies and node transforms without changing application cameras. */
|
|
296
|
+
reset(): Promise<void> {
|
|
297
|
+
this.requireReady();
|
|
298
|
+
return this.exclusive(async () => {
|
|
299
|
+
await this.session!.reset();
|
|
300
|
+
this.accumulator = 0;
|
|
301
|
+
this.manualTargets.clear();
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
destroy(): Promise<void> {
|
|
306
|
+
if (this.destruction) return this.destruction;
|
|
307
|
+
this.closed = true;
|
|
308
|
+
if (this.scene.transformSource === this.transformSource) this.scene.transformSource = null;
|
|
309
|
+
this.destruction = (async () => {
|
|
310
|
+
try {
|
|
311
|
+
await this.pending;
|
|
312
|
+
} catch {
|
|
313
|
+
/* A rejected edit retains an owned session. */
|
|
314
|
+
}
|
|
315
|
+
await this.session?.destroy();
|
|
316
|
+
this.session = undefined;
|
|
317
|
+
this.handles.clear();
|
|
318
|
+
this.definitions.clear();
|
|
319
|
+
this.manualTargets.clear();
|
|
320
|
+
this.joints.length = 0;
|
|
321
|
+
this.indices = new Map();
|
|
322
|
+
})();
|
|
323
|
+
return this.destruction;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
private resolveNode(reference: PhysicsNodeReference): ScenegraphNode | undefined {
|
|
327
|
+
return typeof reference === 'string' ? this.scene.findNode(reference) : reference;
|
|
328
|
+
}
|
|
329
|
+
private assertEditable(): void {
|
|
330
|
+
// Editing cannot overlap an asynchronous operation or follow destruction.
|
|
331
|
+
assert(!this.pending && !this.closed);
|
|
332
|
+
}
|
|
333
|
+
private requireReady(): ScenegraphPhysicsSession {
|
|
334
|
+
// Preparation must finish before frame updates, body commands or publication borrowing.
|
|
335
|
+
assert(this.session && !this.dirty && !this.pending && !this.closed);
|
|
336
|
+
return this.session;
|
|
337
|
+
}
|
|
338
|
+
private async exclusive<T>(operation: () => Promise<T>): Promise<T> {
|
|
339
|
+
this.assertEditable();
|
|
340
|
+
const pending = Promise.resolve().then(operation);
|
|
341
|
+
this.pending = pending;
|
|
342
|
+
try {
|
|
343
|
+
return await pending;
|
|
344
|
+
} finally {
|
|
345
|
+
this.pending = undefined;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
|
-
import type
|
|
5
|
+
import {assert, type Device, type CommandEncoder} from '@luma.gl/core';
|
|
6
6
|
import {Vector3} from '@math.gl/core';
|
|
7
7
|
import type {ScenegraphNode} from '../../index';
|
|
8
8
|
import {GPUCommandGraph} from '@luma.gl/graph/gpu-core';
|
|
@@ -129,6 +129,17 @@ export class ScenegraphPhysicsInstance {
|
|
|
129
129
|
});
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
+
/** Encode fixed steps into a caller-owned encoder without submission or readback. */
|
|
133
|
+
encode(encoder: CommandEncoder, steps = 1): void {
|
|
134
|
+
// Encoding cannot race an asynchronous edit or outlive the owned execution.
|
|
135
|
+
assert(!this.pending && !this.destroying && !this.destroyed);
|
|
136
|
+
// A batch contains a positive integral number of fixed simulation steps.
|
|
137
|
+
assert(Number.isSafeInteger(steps) && steps > 0);
|
|
138
|
+
for (let index = 0; index < steps; index++) {
|
|
139
|
+
this.graph.encode(encoder, {parameters: undefined});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
132
143
|
/** Advance one fixed step; state remains on GPU and only submission completion is awaited. */
|
|
133
144
|
async advance(
|
|
134
145
|
beforeStep?: (world: GPURigidBodyWorld) => void,
|
|
@@ -43,14 +43,25 @@ export class ScenegraphPhysicsMotion {
|
|
|
43
43
|
* Paused actions should remain in this set; stopped actions should be removed. On release,
|
|
44
44
|
* physics retains the last accepted pose and kinematic velocity rather than snapping to authoring.
|
|
45
45
|
*/
|
|
46
|
-
setAnimatedNodes(
|
|
46
|
+
setAnimatedNodes(
|
|
47
|
+
nodes: ReadonlySet<ScenegraphNode>,
|
|
48
|
+
{
|
|
49
|
+
excludedNodes = new Set<ScenegraphNode>(),
|
|
50
|
+
targetNodes = new Set<ScenegraphNode>()
|
|
51
|
+
}: {
|
|
52
|
+
excludedNodes?: ReadonlySet<ScenegraphNode>;
|
|
53
|
+
/** Direct scene-following targets; unlike animation roots, these do not own descendants. */
|
|
54
|
+
targetNodes?: ReadonlySet<ScenegraphNode>;
|
|
55
|
+
} = {}
|
|
56
|
+
): void {
|
|
47
57
|
const active = new Set<ScenegraphNode>();
|
|
48
58
|
const visit = (node: ScenegraphNode, inherited: boolean) => {
|
|
49
59
|
const animated = inherited || nodes.has(node);
|
|
50
|
-
if (animated
|
|
60
|
+
if ((animated || targetNodes.has(node)) && !excludedNodes.has(node) && this.targets.has(node))
|
|
61
|
+
active.add(node);
|
|
51
62
|
if (node instanceof GroupNode) for (const child of node.children) visit(child, animated);
|
|
52
63
|
};
|
|
53
|
-
if (nodes.size) for (const root of this.roots) visit(root, false);
|
|
64
|
+
if (nodes.size || targetNodes.size) for (const root of this.roots) visit(root, false);
|
|
54
65
|
for (const [node, body] of this.targets) {
|
|
55
66
|
if (active.has(node)) {
|
|
56
67
|
if (!this.releasedModes.has(body) || this.world.getBodyMode(body) !== 'kinematic')
|