@khr3/core 0.1.5 → 0.2.0
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 +3 -114
- package/dist/BundleSceneHost-DUqqPtJF.js +1036 -0
- package/dist/BundleSceneHost-DUqqPtJF.js.map +1 -0
- package/dist/KhrCollider.d.ts +1 -1
- package/dist/KhrVehicle.d.ts +5 -7
- package/dist/PhysicsWorld.d.ts +2 -2
- package/dist/assemblyPlan.d.ts +2 -0
- package/dist/curves.d.ts +11 -32
- package/dist/curves.js +141 -139
- package/dist/curves.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1639 -1240
- package/dist/index.js.map +1 -0
- package/dist/jointDynamics.d.ts +1 -1
- package/dist/khrBundleFinalize.d.ts +14 -0
- package/dist/khrConfigTypes.d.ts +62 -0
- package/dist/khrExportEncoders.d.ts +8 -0
- package/dist/khrExporter.d.ts +2 -1
- package/dist/khrExtras.d.ts +3 -3
- package/dist/khrGlb.d.ts +23 -0
- package/dist/khrJointParser.d.ts +3 -0
- package/dist/khrLoader.d.ts +2 -1
- package/dist/khrParsers.d.ts +5 -9
- package/dist/khrPlugin.d.ts +2 -3
- package/dist/khrPluginHelpers.d.ts +4 -0
- package/dist/khrRotorDecoders.d.ts +4 -0
- package/dist/khrShapeParser.d.ts +8 -0
- package/dist/khrTypes.d.ts +11 -103
- package/dist/khrVehicleChassis.d.ts +9 -17
- package/dist/khrVehicleJointHelpers.d.ts +24 -0
- package/dist/khrVehicleJoints.d.ts +18 -0
- package/dist/khrVehicleRotors.d.ts +22 -0
- package/dist/khrVehicleSlots.d.ts +19 -15
- package/dist/khrVehicleTypes.d.ts +52 -0
- package/dist/pacejka.d.ts +1 -1
- package/dist/physicsEcs.d.ts +2 -7
- package/dist/useKhrGlb.d.ts +2 -2
- package/dist/vehicle.d.ts +2 -2
- package/dist/vehicle.js +757 -451
- package/dist/vehicle.js.map +1 -0
- package/dist/wheelLoads.d.ts +4 -7
- package/docs/api-reference.md +209 -0
- package/package.json +8 -7
- package/dist/BundleSceneHost-CE3HPB8a.js +0 -556
package/dist/index.js
CHANGED
|
@@ -1,489 +1,422 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { useCallback
|
|
3
|
-
import { useFrame
|
|
4
|
-
import { Physics
|
|
5
|
-
import * as
|
|
6
|
-
import { jsx
|
|
7
|
-
import { GLTFLoader
|
|
8
|
-
import { GLTFExporter
|
|
9
|
-
// src/jointDynamics.ts
|
|
10
|
-
var z = new I.Vector3(), B = new I.Vector3(), re = new I.Vector3(), ie = new I.Quaternion(), ae = new I.Quaternion(), oe = new I.Matrix4(), se = new I.Matrix4(), ce = new I.Matrix4(), le = new I.Matrix4(), V = new I.Vector3(), H = new I.Quaternion(), ue = new I.Vector3();
|
|
11
|
-
function de(e, t, n = []) {
|
|
12
|
-
if (t.kind === "fixed") return 0;
|
|
13
|
-
let r = n.length > 0 ? [e, ...n] : [e];
|
|
14
|
-
if (t.kind === "prismatic") {
|
|
15
|
-
let t = 0;
|
|
16
|
-
for (let n of r) n.isKinematic && n !== e || (t += n.mass);
|
|
17
|
-
return t;
|
|
18
|
-
}
|
|
19
|
-
z.fromArray(t.axis).normalize(), n.length > 0 && x(e, oe).invert();
|
|
20
|
-
let i = 0;
|
|
21
|
-
for (let t of r) {
|
|
22
|
-
if (t !== e && t.isKinematic) continue;
|
|
23
|
-
t === e ? (V.set(0, 0, 0), H.identity()) : (se.multiplyMatrices(oe, x(t, le)).decompose(V, H, ue), H.normalize()), ae.copy(H).multiply(ie.fromArray(t.inertiaOrientation)).invert(), B.copy(z).applyQuaternion(ae);
|
|
24
|
-
let [n, r, a] = t.inertiaDiagonal, o = n * B.x * B.x + r * B.y * B.y + a * B.z * B.z;
|
|
25
|
-
re.fromArray(t.centerOfMass).applyQuaternion(H).add(V);
|
|
26
|
-
let s = re.dot(z);
|
|
27
|
-
i += o + t.mass * (re.lengthSq() - s * s);
|
|
28
|
-
}
|
|
29
|
-
return i;
|
|
30
|
-
}
|
|
31
|
-
function fe(t, n) {
|
|
32
|
-
if (t.kind === "fixed" || !(n > 0)) return;
|
|
33
|
-
let r = t.drive, i = r?.mode === "force", a = t.limits !== null && t.limitStiffness !== null;
|
|
34
|
-
if ((i || a) && !(t.inertia > 0)) throw new e(`Joint "${t.nodeName}" requires positive effective inertia (got ${t.inertia})`);
|
|
35
|
-
let o = i ? 1 / t.inertia : 1, s = t.positionTarget !== null && r?.stiffness !== void 0 ? r.stiffness * o : 0, c = r?.damping === void 0 ? 0 : r.damping * o, l = 0, u = 0, d = 0;
|
|
36
|
-
if (t.limits && t.limitStiffness !== null && (t.q < t.limits[0] || t.q > t.limits[1])) {
|
|
37
|
-
d = t.q < t.limits[0] ? t.limits[0] : t.limits[1];
|
|
38
|
-
let e = 1 / t.inertia;
|
|
39
|
-
l = t.limitStiffness * e, u = t.limitDamping * e;
|
|
40
|
-
}
|
|
41
|
-
let f = t.positionTarget ?? 0, p = (t.v + n * (s * (f - t.q) + c * t.velocityTarget + l * (d - t.q))) / (1 + n * (c + u) + n * n * (s + l));
|
|
42
|
-
if (r?.maxForce !== void 0) {
|
|
43
|
-
if (!(r.maxForce >= 0)) throw new e(`Joint "${t.nodeName}" has negative drive.maxForce ${r.maxForce}`);
|
|
44
|
-
let a = i ? r.maxForce / t.inertia : r.maxForce, o = s * (f - (t.q + n * p)) + c * (t.velocityTarget - p);
|
|
45
|
-
if (Math.abs(o) > a) {
|
|
46
|
-
let e = Math.sign(o) * a;
|
|
47
|
-
p = (t.v + n * (e + l * (d - t.q))) / (1 + n * u + n * n * l);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
t.v = p, t.q += p * n, t.limits && t.limitStiffness === null && (t.q < t.limits[0] ? (t.q = t.limits[0], t.v < 0 && (t.v = 0)) : t.q > t.limits[1] && (t.q = t.limits[1], t.v > 0 && (t.v = 0)));
|
|
51
|
-
}
|
|
52
|
-
var pe = 1e-7;
|
|
53
|
-
function me(e, t) {
|
|
54
|
-
if (e.stamp === t) return;
|
|
55
|
-
e.parent && e.parent.stamp !== t && me(e.parent, t), e.stamp = t;
|
|
56
|
-
let n = e.parent !== null && e.parent.dirtyStamp === t, r = e.colliders.every((e) => e.ref.current !== null);
|
|
57
|
-
if (e.lastWrittenQ === void 0 || Math.abs(e.q - e.lastWrittenQ) > pe || n) {
|
|
58
|
-
e.dirtyStamp = t, r && (e.lastWrittenQ = e.q), e.kind === "revolute" ? (ie.setFromAxisAngle(e.axis, e.q), e.object.quaternion.copy(e.restQuaternion).multiply(ie), e.object.position.copy(e.restPosition), ce.makeRotationFromQuaternion(ie)) : e.kind === "prismatic" ? (z.copy(e.axis).multiplyScalar(e.q), e.object.quaternion.copy(e.restQuaternion), e.object.position.copy(z).applyQuaternion(e.restQuaternion).add(e.restPosition), ce.makeTranslation(z.x, z.y, z.z)) : ce.identity(), e.parent ? e.current.multiplyMatrices(e.parent.current, e.restRelParent) : e.current.copy(e.restRelParent), e.current.multiply(ce);
|
|
59
|
-
for (let t of e.colliders) {
|
|
60
|
-
let n = t.ref.current;
|
|
61
|
-
n && (le.multiplyMatrices(e.current, t.local).decompose(V, H, ue), n.setTranslationWrtParent(V), n.setRotationWrtParent(H));
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
1
|
+
import { E as KhrValidationError, S as WorldGravityContext, T as writeJointPose, _ as computeRelativePose, a as planAssembly, b as readUnitQuat, c as claimBundleMount, d as releaseBundleMount, f as assertIdentityWorldScale, g as computeBodyInertia, h as computeAssemblyMassAndCom, l as cloneKhrBundle, m as assertUnitScale, n as MountSubtreeSlot, p as assertSceneIdentityWorldScale, r as captureSubtreeOrigins, s as KhrCollider, t as BundleSceneHost, u as disposeKhrBundle, v as extractMeshColliderShape, w as stepJoint, x as JointRegistryContext, y as readFiniteVec3 } from "./BundleSceneHost-DUqqPtJF.js";
|
|
2
|
+
import { useCallback, useContext, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useRef } from "react";
|
|
3
|
+
import { useFrame, useLoader } from "@react-three/fiber";
|
|
4
|
+
import { Physics, RigidBody, useRapier } from "@react-three/rapier";
|
|
5
|
+
import * as THREE from "three";
|
|
6
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
+
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
|
|
8
|
+
import { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter.js";
|
|
65
9
|
// src/PhysicsWorld.tsx
|
|
66
|
-
var
|
|
67
|
-
function
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
10
|
+
var readVal = (v) => typeof v === "number" ? v : v.current;
|
|
11
|
+
function StepDriver({ paused, timeScale, maxDelta, joints, dtRef }) {
|
|
12
|
+
const { world } = useRapier();
|
|
13
|
+
const frameRef = useRef(0);
|
|
14
|
+
useFrame((_, rawDelta) => {
|
|
15
|
+
const ts = readVal(timeScale);
|
|
16
|
+
const md = Math.max(0, readVal(maxDelta));
|
|
17
|
+
const dt = !paused && ts > 0 && md > 0 && rawDelta > 0 ? Math.min(Math.min(rawDelta, md) * ts, .5) : 0;
|
|
18
|
+
if (!(dt > 0)) {
|
|
19
|
+
dtRef.current = 0;
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
dtRef.current = dt;
|
|
23
|
+
world.timestep = dt;
|
|
24
|
+
if (joints.size > 0) {
|
|
25
|
+
const frame = ++frameRef.current;
|
|
26
|
+
for (const j of joints) stepJoint(j, dt);
|
|
27
|
+
for (const j of joints) writeJointPose(j, frame);
|
|
76
28
|
}
|
|
77
|
-
})
|
|
29
|
+
});
|
|
30
|
+
return null;
|
|
78
31
|
}
|
|
79
|
-
function
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
})
|
|
32
|
+
function StepCommit({ dtRef }) {
|
|
33
|
+
const { step } = useRapier();
|
|
34
|
+
useFrame(() => {
|
|
35
|
+
const dt = dtRef.current;
|
|
36
|
+
if (dt > 0) step(dt);
|
|
37
|
+
});
|
|
38
|
+
return null;
|
|
85
39
|
}
|
|
86
|
-
function
|
|
40
|
+
function PhysicsWorld({ paused = false, timeScale = 1, maxDelta = 1 / 30, gravity = [
|
|
87
41
|
0,
|
|
88
42
|
-9.81,
|
|
89
43
|
0
|
|
90
|
-
], children
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
44
|
+
], children, ...physicsProps }) {
|
|
45
|
+
const joints = useMemo(() => /* @__PURE__ */ new Set(), []);
|
|
46
|
+
const dtRef = useRef(0);
|
|
47
|
+
const gravityMemo = useMemo(() => [
|
|
48
|
+
gravity[0],
|
|
49
|
+
gravity[1],
|
|
50
|
+
gravity[2]
|
|
95
51
|
], [
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
52
|
+
gravity[0],
|
|
53
|
+
gravity[1],
|
|
54
|
+
gravity[2]
|
|
99
55
|
]);
|
|
100
|
-
return /* @__PURE__ */
|
|
101
|
-
value:
|
|
102
|
-
children: /* @__PURE__ */
|
|
103
|
-
value:
|
|
104
|
-
children: /* @__PURE__ */
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
activeRef: p,
|
|
121
|
-
setActiveState: f
|
|
122
|
-
}),
|
|
123
|
-
o,
|
|
124
|
-
/* @__PURE__ */ L(_e, { dtRef: l })
|
|
125
|
-
]
|
|
126
|
-
})
|
|
127
|
-
})
|
|
56
|
+
return /* @__PURE__ */ jsx(JointRegistryContext.Provider, {
|
|
57
|
+
value: joints,
|
|
58
|
+
children: /* @__PURE__ */ jsx(WorldGravityContext.Provider, {
|
|
59
|
+
value: gravityMemo,
|
|
60
|
+
children: /* @__PURE__ */ jsxs(Physics, {
|
|
61
|
+
...physicsProps,
|
|
62
|
+
paused: true,
|
|
63
|
+
timeStep: "vary",
|
|
64
|
+
gravity,
|
|
65
|
+
children: [
|
|
66
|
+
/* @__PURE__ */ jsx(StepDriver, {
|
|
67
|
+
paused,
|
|
68
|
+
timeScale,
|
|
69
|
+
maxDelta,
|
|
70
|
+
joints,
|
|
71
|
+
dtRef
|
|
72
|
+
}),
|
|
73
|
+
children,
|
|
74
|
+
/* @__PURE__ */ jsx(StepCommit, { dtRef })
|
|
75
|
+
]
|
|
128
76
|
})
|
|
129
77
|
})
|
|
130
78
|
});
|
|
131
79
|
}
|
|
132
80
|
// src/khrGlb.ts
|
|
133
|
-
var
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if (
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if (
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
81
|
+
var GLB_MAGIC = 1179937895;
|
|
82
|
+
var GLB_JSON_CHUNK = 1313821514;
|
|
83
|
+
var GLB_BIN_CHUNK = 5130562;
|
|
84
|
+
function validateGlbHeader(buffer) {
|
|
85
|
+
if (buffer.byteLength < 20) throw new KhrValidationError("GLB buffer shorter than 20-byte header");
|
|
86
|
+
const view = new DataView(buffer);
|
|
87
|
+
if (view.getUint32(0, true) !== 1179937895) throw new KhrValidationError("Invalid GLB magic");
|
|
88
|
+
if (view.getUint32(4, true) !== 2) throw new KhrValidationError("Unsupported glTF version");
|
|
89
|
+
const jsonLen = view.getUint32(12, true);
|
|
90
|
+
if (view.getUint32(16, true) !== 1313821514) throw new KhrValidationError("Missing GLB JSON chunk");
|
|
91
|
+
if (20 + jsonLen > buffer.byteLength) throw new KhrValidationError("Truncated GLB JSON chunk");
|
|
92
|
+
return jsonLen;
|
|
93
|
+
}
|
|
94
|
+
function ensureHeadlessFileReader() {
|
|
145
95
|
if (globalThis.FileReader !== void 0) return;
|
|
146
|
-
class
|
|
96
|
+
class PolyfillFileReader {
|
|
147
97
|
result = null;
|
|
148
98
|
error = null;
|
|
149
99
|
onload = null;
|
|
150
100
|
onloadend = null;
|
|
151
101
|
onerror = null;
|
|
152
|
-
readAsArrayBuffer(
|
|
153
|
-
|
|
154
|
-
this.result =
|
|
155
|
-
|
|
156
|
-
this.
|
|
102
|
+
readAsArrayBuffer(blob) {
|
|
103
|
+
blob.arrayBuffer().then((buf) => {
|
|
104
|
+
this.result = buf;
|
|
105
|
+
this.onload?.({ target: this });
|
|
106
|
+
this.onloadend?.();
|
|
107
|
+
}, (err) => {
|
|
108
|
+
this.error = err;
|
|
109
|
+
this.onerror?.(err);
|
|
110
|
+
this.onloadend?.();
|
|
157
111
|
});
|
|
158
112
|
}
|
|
159
|
-
readAsDataURL(
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
this.
|
|
113
|
+
readAsDataURL(blob) {
|
|
114
|
+
blob.arrayBuffer().then((buf) => {
|
|
115
|
+
const bytes = new Uint8Array(buf);
|
|
116
|
+
let bin = "";
|
|
117
|
+
for (let i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]);
|
|
118
|
+
this.result = `data:${blob.type || "application/octet-stream"};base64,${btoa(bin)}`;
|
|
119
|
+
this.onload?.({ target: this });
|
|
120
|
+
this.onloadend?.();
|
|
121
|
+
}, (err) => {
|
|
122
|
+
this.error = err;
|
|
123
|
+
this.onerror?.(err);
|
|
124
|
+
this.onloadend?.();
|
|
166
125
|
});
|
|
167
126
|
}
|
|
168
127
|
}
|
|
169
|
-
globalThis.FileReader =
|
|
128
|
+
globalThis.FileReader = PolyfillFileReader;
|
|
170
129
|
}
|
|
171
|
-
function
|
|
172
|
-
|
|
173
|
-
|
|
130
|
+
function packJsonToGlb(gltf) {
|
|
131
|
+
const jsonBytes = new TextEncoder().encode(JSON.stringify(gltf));
|
|
132
|
+
const paddedJsonLen = jsonBytes.byteLength + 3 & -4;
|
|
133
|
+
const totalLen = 20 + paddedJsonLen;
|
|
134
|
+
const out = new ArrayBuffer(totalLen);
|
|
135
|
+
const dv = new DataView(out);
|
|
136
|
+
const u8 = new Uint8Array(out);
|
|
137
|
+
dv.setUint32(0, GLB_MAGIC, true);
|
|
138
|
+
dv.setUint32(4, 2, true);
|
|
139
|
+
dv.setUint32(8, totalLen, true);
|
|
140
|
+
dv.setUint32(12, paddedJsonLen, true);
|
|
141
|
+
dv.setUint32(16, GLB_JSON_CHUNK, true);
|
|
142
|
+
u8.set(jsonBytes, 20);
|
|
143
|
+
u8.fill(32, 20 + jsonBytes.byteLength, 20 + paddedJsonLen);
|
|
144
|
+
return out;
|
|
174
145
|
}
|
|
175
|
-
function
|
|
176
|
-
|
|
146
|
+
function patchGlbJsonChunk(source, mutator) {
|
|
147
|
+
const rawBuf = source instanceof ArrayBuffer ? source : source.buffer.slice(source.byteOffset, source.byteOffset + source.byteLength);
|
|
148
|
+
const jsonLen = validateGlbHeader(rawBuf);
|
|
149
|
+
const dv = new DataView(rawBuf);
|
|
150
|
+
let gltf;
|
|
177
151
|
try {
|
|
178
|
-
|
|
152
|
+
gltf = JSON.parse(new TextDecoder().decode(new Uint8Array(rawBuf, 20, jsonLen)));
|
|
179
153
|
} catch {
|
|
180
|
-
throw new
|
|
154
|
+
throw new KhrValidationError("Malformed GLB JSON chunk");
|
|
181
155
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
t[r] = [...e.worldPosition];
|
|
207
|
-
}
|
|
208
|
-
if (e.contactDots) for (let [n, r] of e.contactDots) t[n] = [...r];
|
|
209
|
-
}
|
|
210
|
-
for (let [e, n] of i.ikSockets) t[e] = [...n];
|
|
211
|
-
e.sockets = t;
|
|
212
|
-
}
|
|
156
|
+
mutator(gltf);
|
|
157
|
+
const nextJson = new TextEncoder().encode(JSON.stringify(gltf));
|
|
158
|
+
const paddedJsonLen = nextJson.byteLength + 3 & -4;
|
|
159
|
+
const binHeaderOff = 20 + jsonLen;
|
|
160
|
+
const hasBin = binHeaderOff + 8 <= rawBuf.byteLength;
|
|
161
|
+
const binLen = hasBin ? Math.min(dv.getUint32(binHeaderOff, true), rawBuf.byteLength - (binHeaderOff + 8)) : 0;
|
|
162
|
+
const binTag = hasBin ? dv.getUint32(binHeaderOff + 4, true) : GLB_BIN_CHUNK;
|
|
163
|
+
const binData = hasBin ? new Uint8Array(rawBuf, binHeaderOff + 8, binLen) : /* @__PURE__ */ new Uint8Array();
|
|
164
|
+
const paddedBinLen = binData.byteLength + 3 & -4;
|
|
165
|
+
const totalLen = 20 + paddedJsonLen + (hasBin ? 8 + paddedBinLen : 0);
|
|
166
|
+
const out = new ArrayBuffer(totalLen);
|
|
167
|
+
const outDv = new DataView(out);
|
|
168
|
+
const outU8 = new Uint8Array(out);
|
|
169
|
+
outDv.setUint32(0, GLB_MAGIC, true);
|
|
170
|
+
outDv.setUint32(4, 2, true);
|
|
171
|
+
outDv.setUint32(8, totalLen, true);
|
|
172
|
+
outDv.setUint32(12, paddedJsonLen, true);
|
|
173
|
+
outDv.setUint32(16, GLB_JSON_CHUNK, true);
|
|
174
|
+
outU8.set(nextJson, 20);
|
|
175
|
+
outU8.fill(32, 20 + nextJson.byteLength, 20 + paddedJsonLen);
|
|
176
|
+
if (hasBin) {
|
|
177
|
+
outDv.setUint32(20 + paddedJsonLen, paddedBinLen, true);
|
|
178
|
+
outDv.setUint32(24 + paddedJsonLen, binTag, true);
|
|
179
|
+
outU8.set(binData, 28 + paddedJsonLen);
|
|
213
180
|
}
|
|
181
|
+
return out;
|
|
214
182
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
183
|
+
// src/khrRotorDecoders.ts
|
|
184
|
+
var _quat$1 = new THREE.Quaternion();
|
|
185
|
+
var _invParentQuat = new THREE.Quaternion();
|
|
186
|
+
var isObj$4 = (v) => typeof v === "object" && !!v && !Array.isArray(v);
|
|
187
|
+
var isFiniteNum$1 = (v) => typeof v === "number" && Number.isFinite(v);
|
|
188
|
+
function getRelativeQuatArray(obj, parent) {
|
|
189
|
+
obj.updateWorldMatrix(true, false);
|
|
190
|
+
obj.getWorldQuaternion(_quat$1);
|
|
191
|
+
if (parent) {
|
|
192
|
+
parent.updateWorldMatrix(true, false);
|
|
193
|
+
_quat$1.premultiply(parent.getWorldQuaternion(_invParentQuat).invert());
|
|
194
|
+
}
|
|
195
|
+
if (_quat$1.lengthSq() > 0) _quat$1.normalize();
|
|
196
|
+
else _quat$1.identity();
|
|
197
|
+
return [
|
|
198
|
+
_quat$1.x || 0,
|
|
199
|
+
_quat$1.y || 0,
|
|
200
|
+
_quat$1.z || 0,
|
|
201
|
+
_quat$1.w || 0
|
|
222
202
|
];
|
|
223
203
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
if (
|
|
228
|
-
|
|
229
|
-
return a;
|
|
230
|
-
}
|
|
204
|
+
function optNum$3(obj, key, tagKind, owner, strictlyPos = false) {
|
|
205
|
+
const val = obj[key];
|
|
206
|
+
if (val === void 0) return void 0;
|
|
207
|
+
if (!isFiniteNum$1(val) || (strictlyPos ? val <= 0 : val < 0)) throw new KhrValidationError(`${tagKind} "${owner}" has invalid ${key}: ${tagKind}.${key}`);
|
|
208
|
+
return val;
|
|
231
209
|
}
|
|
232
|
-
function
|
|
233
|
-
|
|
234
|
-
if (
|
|
235
|
-
|
|
236
|
-
return a;
|
|
237
|
-
}
|
|
210
|
+
function reqNum(obj, key, tagKind, owner, strictlyPos = false) {
|
|
211
|
+
const val = obj[key];
|
|
212
|
+
if (!isFiniteNum$1(val) || (strictlyPos ? val <= 0 : val < 0)) throw new KhrValidationError(`${tagKind} "${owner}" is missing valid ${tagKind}.${key}`);
|
|
213
|
+
return val;
|
|
238
214
|
}
|
|
239
|
-
function
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
return t;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
function Pe(t, n) {
|
|
248
|
-
if (t !== void 0) {
|
|
249
|
-
if (t === "shapeCast" || t === "rayCast") return t;
|
|
250
|
-
throw new e(`ecctrlWheel "${n}" has invalid ecctrlWheel.groundDetection`);
|
|
251
|
-
}
|
|
215
|
+
function optBool(obj, key, tagKind, owner) {
|
|
216
|
+
const val = obj[key];
|
|
217
|
+
if (val === void 0) return void 0;
|
|
218
|
+
if (typeof val !== "boolean") throw new KhrValidationError(`${tagKind} "${owner}" has non-boolean ${tagKind}.${key}`);
|
|
219
|
+
return val;
|
|
252
220
|
}
|
|
253
|
-
function
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
if (c || r.isIkAnchor === !0 || typeof r.socketId == "string") {
|
|
261
|
-
let e = typeof c == "string" && c.length > 0 ? c : r.socketId ?? n;
|
|
262
|
-
i[e] = [...s];
|
|
263
|
-
}
|
|
264
|
-
let l;
|
|
265
|
-
if (r.contactDots !== void 0) {
|
|
266
|
-
if (!Array.isArray(r.contactDots)) throw new e(`Node "${n}" has invalid extras.contactDots (expected array)`);
|
|
267
|
-
t.getWorldQuaternion(U), l = [];
|
|
268
|
-
for (let t of r.contactDots) {
|
|
269
|
-
if (!W(t) || typeof t.role != "string" || t.role.length === 0 || !Array.isArray(t.pos) || t.pos.length !== 3 || !t.pos.every(G)) throw new e(`Node "${n}" has invalid entry in extras.contactDots`);
|
|
270
|
-
ke.set(t.pos[0], t.pos[1], t.pos[2]).applyQuaternion(U).add(o);
|
|
271
|
-
let r = [
|
|
272
|
-
ke.x,
|
|
273
|
-
ke.y,
|
|
274
|
-
ke.z
|
|
275
|
-
];
|
|
276
|
-
i[t.role] = r, l.push([t.role, r]);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
return {
|
|
280
|
-
nodeName: n,
|
|
281
|
-
extras: r,
|
|
282
|
-
worldPosition: s,
|
|
283
|
-
...l ? { contactDots: l } : {}
|
|
221
|
+
function optBoolOrNumFlag(obj, key, tagKind, owner) {
|
|
222
|
+
const val = obj[key];
|
|
223
|
+
if (val === void 0) return void 0;
|
|
224
|
+
if (typeof val === "boolean") return { flag: val };
|
|
225
|
+
if (isFiniteNum$1(val) && val >= 0) return {
|
|
226
|
+
flag: val !== 0,
|
|
227
|
+
numeric: val
|
|
284
228
|
};
|
|
229
|
+
throw new KhrValidationError(`${tagKind} "${owner}" has invalid ${tagKind}.${key}`);
|
|
285
230
|
}
|
|
286
|
-
function
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
if (
|
|
302
|
-
|
|
231
|
+
function reqBool(obj, key, tagKind, owner) {
|
|
232
|
+
const val = obj[key];
|
|
233
|
+
if (typeof val !== "boolean") throw new KhrValidationError(`${tagKind} "${owner}" is missing boolean ${tagKind}.${key}`);
|
|
234
|
+
return val;
|
|
235
|
+
}
|
|
236
|
+
function optCurve(val, key, owner) {
|
|
237
|
+
if (val === void 0) return void 0;
|
|
238
|
+
if (!isObj$4(val) || !Array.isArray(val.points) || val.points.length === 0) throw new KhrValidationError(`ecctrlWheel "${owner}" has invalid ecctrlWheel.${key}`);
|
|
239
|
+
for (const pt of val.points) if (!isObj$4(pt) || !isFiniteNum$1(pt.x) || !isFiniteNum$1(pt.y)) throw new KhrValidationError(`ecctrlWheel "${owner}" has invalid point in ecctrlWheel.${key}`);
|
|
240
|
+
if (val.samples !== void 0 && (!isFiniteNum$1(val.samples) || val.samples <= 0)) throw new KhrValidationError(`ecctrlWheel "${owner}" has invalid samples in ecctrlWheel.${key}`);
|
|
241
|
+
return val;
|
|
242
|
+
}
|
|
243
|
+
function decodeWheelTag(obj, name, rawWheel, scene, rawOmiWheel) {
|
|
244
|
+
assertIdentityWorldScale(obj, name);
|
|
245
|
+
const hasOmi = rawOmiWheel !== void 0;
|
|
246
|
+
if (hasOmi && !isObj$4(rawOmiWheel)) throw new KhrValidationError(`OMI_vehicle_wheel "${name}" must be an object`);
|
|
247
|
+
if (rawWheel !== void 0 && !isObj$4(rawWheel)) throw new KhrValidationError(`ecctrlWheel "${name}" must be an object`);
|
|
248
|
+
if (!hasOmi && rawWheel === void 0) throw new KhrValidationError(`ecctrlWheel "${name}" must be an object`);
|
|
249
|
+
const ec = isObj$4(rawWheel) ? rawWheel : {};
|
|
250
|
+
const omi = isObj$4(rawOmiWheel) ? rawOmiWheel : void 0;
|
|
251
|
+
const radius = omi ? optNum$3(ec, "radius", "ecctrlWheel", name, true) ?? reqNum(omi, "radius", "OMI_vehicle_wheel", name, true) : reqNum(ec, "radius", "ecctrlWheel", name, true);
|
|
252
|
+
const width = omi ? optNum$3(ec, "width", "ecctrlWheel", name, true) ?? reqNum(omi, "width", "OMI_vehicle_wheel", name, true) : reqNum(ec, "width", "ecctrlWheel", name, true);
|
|
253
|
+
if (omi) {
|
|
254
|
+
reqNum(omi, "radius", "OMI_vehicle_wheel", name, true);
|
|
255
|
+
reqNum(omi, "width", "OMI_vehicle_wheel", name, true);
|
|
256
|
+
}
|
|
257
|
+
const omiTravel = omi ? optNum$3(omi, "suspensionTravel", "OMI_vehicle_wheel", name) : void 0;
|
|
258
|
+
const omiMaxForce = omi ? optNum$3(omi, "maxForce", "OMI_vehicle_wheel", name) : void 0;
|
|
259
|
+
const omiMaxSteerRaw = omi ? optNum$3(omi, "maxSteeringAngle", "OMI_vehicle_wheel", name) : void 0;
|
|
260
|
+
const omiStiffness = omi ? optNum$3(omi, "suspensionStiffness", "OMI_vehicle_wheel", name) : void 0;
|
|
261
|
+
const omiDampComp = omi ? optNum$3(omi, "suspensionDampingCompression", "OMI_vehicle_wheel", name) : void 0;
|
|
262
|
+
const omiDampReb = omi ? optNum$3(omi, "suspensionDampingRebound", "OMI_vehicle_wheel", name) : void 0;
|
|
263
|
+
const omiSteer = omi ? optBoolOrNumFlag(omi, "steering", "OMI_vehicle_wheel", name) : void 0;
|
|
264
|
+
const omiProp = omi ? optBoolOrNumFlag(omi, "propulsion", "OMI_vehicle_wheel", name) : void 0;
|
|
265
|
+
const omiSteering = omiSteer?.flag;
|
|
266
|
+
const omiPropulsion = omiProp?.flag;
|
|
267
|
+
const omiMaxSteer = omiMaxSteerRaw ?? (omiSteer?.numeric !== void 0 && omiSteer.numeric > 0 ? omiSteer.numeric : void 0);
|
|
268
|
+
const travel = omi ? optNum$3(ec, "travel", "ecctrlWheel", name) ?? omiTravel ?? .25 : reqNum(ec, "travel", "ecctrlWheel", name);
|
|
269
|
+
const sag = omi ? optNum$3(ec, "sag", "ecctrlWheel", name) ?? (travel > 0 ? Math.min(travel * .4, .1) : 0) : reqNum(ec, "sag", "ecctrlWheel", name);
|
|
270
|
+
if (sag > travel) throw new KhrValidationError(`ecctrlWheel "${name}" has invalid sag (${sag}) > travel (${travel})`);
|
|
271
|
+
const steer = omi ? optBool(ec, "steer", "ecctrlWheel", name) ?? omiSteering ?? (omiMaxSteer !== void 0 ? omiMaxSteer > 0 : false) : reqBool(ec, "steer", "ecctrlWheel", name);
|
|
272
|
+
const drive = omi ? optBool(ec, "drive", "ecctrlWheel", name) ?? omiPropulsion ?? false : reqBool(ec, "drive", "ecctrlWheel", name);
|
|
273
|
+
const brake = omi ? optBool(ec, "brake", "ecctrlWheel", name) ?? true : reqBool(ec, "brake", "ecctrlWheel", name);
|
|
274
|
+
const omiDamping = omiDampComp !== void 0 && omiDampReb !== void 0 ? .5 * (omiDampComp + omiDampReb) : omiDampComp ?? omiDampReb;
|
|
275
|
+
const springK = optNum$3(ec, "springK", "ecctrlWheel", name) ?? omiStiffness;
|
|
276
|
+
const dampingC = optNum$3(ec, "dampingC", "ecctrlWheel", name) ?? omiDamping;
|
|
277
|
+
const maxBrakeTorque = optNum$3(ec, "maxBrakeTorque", "ecctrlWheel", name) ?? (omiMaxForce !== void 0 ? omiMaxForce * radius : void 0);
|
|
278
|
+
const maxSteeringAngle = optNum$3(ec, "maxSteeringAngle", "ecctrlWheel", name) ?? omiMaxSteer;
|
|
279
|
+
const box = new THREE.Box3().setFromObject(obj);
|
|
280
|
+
const center = box.isEmpty() ? obj.getWorldPosition(new THREE.Vector3()) : box.getCenter(new THREE.Vector3());
|
|
303
281
|
return {
|
|
304
|
-
nodeName:
|
|
305
|
-
object:
|
|
282
|
+
nodeName: name,
|
|
283
|
+
object: obj,
|
|
306
284
|
position: [
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
285
|
+
center.x,
|
|
286
|
+
center.y,
|
|
287
|
+
center.z
|
|
310
288
|
],
|
|
311
|
-
quaternion:
|
|
312
|
-
radius
|
|
313
|
-
width
|
|
314
|
-
steer
|
|
315
|
-
steerInvert:
|
|
316
|
-
drive
|
|
317
|
-
driveInvert:
|
|
318
|
-
brake
|
|
319
|
-
travel
|
|
320
|
-
sag
|
|
321
|
-
springK
|
|
322
|
-
dampingC
|
|
323
|
-
maxBrakeTorque
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
driveTorqueWeight:
|
|
328
|
-
tireGripFactor:
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
minLatRelaxCoeff: K(r, "minLatRelaxCoeff", n),
|
|
335
|
-
lngSlipRatioCurveData: Ne(r.lngSlipRatioCurveData, "lngSlipRatioCurveData", n),
|
|
336
|
-
latSlipRatioCurveData: Ne(r.latSlipRatioCurveData, "latSlipRatioCurveData", n)
|
|
289
|
+
quaternion: getRelativeQuatArray(obj, scene),
|
|
290
|
+
radius,
|
|
291
|
+
width,
|
|
292
|
+
steer,
|
|
293
|
+
steerInvert: optBool(ec, "steerInvert", "ecctrlWheel", name),
|
|
294
|
+
drive,
|
|
295
|
+
driveInvert: optBool(ec, "driveInvert", "ecctrlWheel", name),
|
|
296
|
+
brake,
|
|
297
|
+
travel,
|
|
298
|
+
sag,
|
|
299
|
+
springK,
|
|
300
|
+
dampingC,
|
|
301
|
+
maxBrakeTorque,
|
|
302
|
+
...maxSteeringAngle === void 0 ? {} : { maxSteeringAngle },
|
|
303
|
+
rollingResistanceCoef: optNum$3(ec, "rollingResistanceCoef", "ecctrlWheel", name),
|
|
304
|
+
wheelDensity: optNum$3(ec, "wheelDensity", "ecctrlWheel", name, true),
|
|
305
|
+
driveTorqueWeight: optNum$3(ec, "driveTorqueWeight", "ecctrlWheel", name),
|
|
306
|
+
tireGripFactor: optNum$3(ec, "tireGripFactor", "ecctrlWheel", name),
|
|
307
|
+
latFrictionEllipseScale: optNum$3(ec, "latFrictionEllipseScale", "ecctrlWheel", name),
|
|
308
|
+
relaxLngRate: optNum$3(ec, "relaxLngRate", "ecctrlWheel", name),
|
|
309
|
+
relaxLatRate: optNum$3(ec, "relaxLatRate", "ecctrlWheel", name),
|
|
310
|
+
lngSlipRatioCurveData: optCurve(ec.lngSlipRatioCurveData, "lngSlipRatioCurveData", name),
|
|
311
|
+
latSlipRatioCurveData: optCurve(ec.latSlipRatioCurveData, "latSlipRatioCurveData", name)
|
|
337
312
|
};
|
|
338
313
|
}
|
|
339
|
-
function
|
|
340
|
-
|
|
341
|
-
if (!
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
314
|
+
function decodePropellerTag(obj, name, rawPropeller, scene) {
|
|
315
|
+
assertIdentityWorldScale(obj, name);
|
|
316
|
+
if (!isObj$4(rawPropeller)) throw new KhrValidationError(`ecctrlPropeller "${name}" must be an object`);
|
|
317
|
+
if (!isFiniteNum$1(rawPropeller.maxThrust) || rawPropeller.maxThrust <= 0) throw new KhrValidationError(`ecctrlPropeller "${name}" is missing positive finite ecctrlPropeller.maxThrust`);
|
|
318
|
+
let torqueRatio;
|
|
319
|
+
if (rawPropeller.torqueRatio !== void 0) {
|
|
320
|
+
if (!isFiniteNum$1(rawPropeller.torqueRatio)) throw new KhrValidationError(`ecctrlPropeller "${name}" has invalid ecctrlPropeller.torqueRatio`);
|
|
321
|
+
torqueRatio = rawPropeller.torqueRatio;
|
|
346
322
|
}
|
|
347
|
-
|
|
323
|
+
const box = new THREE.Box3().setFromObject(obj);
|
|
324
|
+
const center = box.isEmpty() ? obj.getWorldPosition(new THREE.Vector3()) : box.getCenter(new THREE.Vector3());
|
|
348
325
|
return {
|
|
349
|
-
nodeName:
|
|
350
|
-
object:
|
|
326
|
+
nodeName: name,
|
|
327
|
+
object: obj,
|
|
351
328
|
position: [
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
329
|
+
center.x,
|
|
330
|
+
center.y,
|
|
331
|
+
center.z
|
|
355
332
|
],
|
|
356
|
-
quaternion:
|
|
357
|
-
maxThrust:
|
|
358
|
-
torqueRatio
|
|
359
|
-
invertThrust:
|
|
360
|
-
invertTorque:
|
|
333
|
+
quaternion: getRelativeQuatArray(obj, scene),
|
|
334
|
+
maxThrust: rawPropeller.maxThrust,
|
|
335
|
+
torqueRatio,
|
|
336
|
+
invertThrust: optBool(rawPropeller, "invertThrust", "ecctrlPropeller", name),
|
|
337
|
+
invertTorque: optBool(rawPropeller, "invertTorque", "ecctrlPropeller", name)
|
|
361
338
|
};
|
|
362
339
|
}
|
|
363
|
-
// src/
|
|
364
|
-
var
|
|
365
|
-
function
|
|
366
|
-
|
|
367
|
-
if (typeof t != "number" || !Number.isFinite(t) || r && t < 0) throw new e(`Invalid ${n}`);
|
|
368
|
-
return t;
|
|
369
|
-
}
|
|
340
|
+
// src/khrExtras.ts
|
|
341
|
+
var templateMetaMap = /* @__PURE__ */ new WeakMap();
|
|
342
|
+
function registerKhrTemplateMeta(scene, meta) {
|
|
343
|
+
templateMetaMap.set(scene, meta);
|
|
370
344
|
}
|
|
371
|
-
function
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
if (
|
|
389
|
-
return {
|
|
390
|
-
type: "sphere",
|
|
391
|
-
radius: r
|
|
392
|
-
};
|
|
345
|
+
function setSocket(sockets, key, pos) {
|
|
346
|
+
Object.defineProperty(sockets, key, {
|
|
347
|
+
value: pos,
|
|
348
|
+
enumerable: true,
|
|
349
|
+
configurable: true,
|
|
350
|
+
writable: true
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
function applyKhrCustomExtractors(clonedBundle, templateBundle, extractRole, isSocketNode) {
|
|
354
|
+
const meta = templateMetaMap.get(templateBundle.scene);
|
|
355
|
+
if (!meta) return;
|
|
356
|
+
if (extractRole) for (let i = 0; i < clonedBundle.bodies.length; i++) {
|
|
357
|
+
const rawBody = meta.bodies[i];
|
|
358
|
+
const dstBody = clonedBundle.bodies[i];
|
|
359
|
+
if (!rawBody || !dstBody) continue;
|
|
360
|
+
const role = extractRole(rawBody.nodeName, rawBody.extras) ?? (typeof rawBody.extras.role === "string" ? rawBody.extras.role : null);
|
|
361
|
+
dstBody.role = role;
|
|
362
|
+
if (dstBody.joint) dstBody.joint.role = role;
|
|
393
363
|
}
|
|
394
|
-
if (
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
radiusTop: i.radiusTop,
|
|
404
|
-
radiusBottom: i.radiusBottom
|
|
405
|
-
};
|
|
364
|
+
if (isSocketNode) {
|
|
365
|
+
const sockets = {};
|
|
366
|
+
for (const node of meta.nodes) {
|
|
367
|
+
const custom = isSocketNode(node.nodeName, node.extras);
|
|
368
|
+
if (custom || node.extras.isIkAnchor === true || typeof node.extras.socketId === "string") setSocket(sockets, typeof custom === "string" && custom.length > 0 ? custom : typeof node.extras.socketId === "string" ? node.extras.socketId : node.nodeName, [...node.worldPosition]);
|
|
369
|
+
if (node.contactDots) for (const [role, pos] of node.contactDots) setSocket(sockets, role, [...pos]);
|
|
370
|
+
}
|
|
371
|
+
for (const [key, pos] of meta.ikSockets) setSocket(sockets, key, [...pos]);
|
|
372
|
+
clonedBundle.sockets = sockets;
|
|
406
373
|
}
|
|
407
|
-
throw new e(`KHR_implicit_shapes[${n}] unsupported shape type "${r}"`);
|
|
408
|
-
}
|
|
409
|
-
function Ue(t, n, r) {
|
|
410
|
-
if (n === void 0) return {};
|
|
411
|
-
if (typeof n != "number" || !Number.isInteger(n) || n < 0 || n >= t.length) throw new e(`Node "${r}" references out-of-bounds physicsMaterial ${String(n)}`);
|
|
412
|
-
let i = t[n];
|
|
413
|
-
if (!Be(i)) throw new e(`Invalid physicsMaterials[${n}] on "${r}"`);
|
|
414
|
-
let a = J(i.dynamicFriction, `physicsMaterials[${n}].dynamicFriction`, !0), o = J(i.staticFriction, `physicsMaterials[${n}].staticFriction`, !0), s = J(i.restitution, `physicsMaterials[${n}].restitution`, !0);
|
|
415
|
-
return {
|
|
416
|
-
...a !== void 0 || o !== void 0 ? { friction: a ?? o } : {},
|
|
417
|
-
...s === void 0 ? {} : { restitution: s }
|
|
418
|
-
};
|
|
419
374
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
375
|
+
var _dotVec = new THREE.Vector3();
|
|
376
|
+
var _quat = new THREE.Quaternion();
|
|
377
|
+
var isObj$3 = (v) => typeof v === "object" && !!v && !Array.isArray(v);
|
|
378
|
+
var isFiniteNum = (v) => typeof v === "number" && Number.isFinite(v);
|
|
379
|
+
function collectNodeSockets(obj, name, extras, sockets, isSocketNode) {
|
|
380
|
+
if (extras.socketId !== void 0 && (typeof extras.socketId !== "string" || extras.socketId.length === 0)) throw new KhrValidationError(`Node "${name}" has invalid extras.socketId`);
|
|
381
|
+
const wp = obj.getWorldPosition(new THREE.Vector3());
|
|
382
|
+
const worldPosition = [
|
|
383
|
+
wp.x,
|
|
384
|
+
wp.y,
|
|
385
|
+
wp.z
|
|
386
|
+
];
|
|
387
|
+
const custom = isSocketNode?.(name, extras);
|
|
388
|
+
if (custom || extras.isIkAnchor === true || typeof extras.socketId === "string") setSocket(sockets, typeof custom === "string" && custom.length > 0 ? custom : typeof extras.socketId === "string" ? extras.socketId : name, [...worldPosition]);
|
|
389
|
+
let contactDots;
|
|
390
|
+
if (extras.contactDots !== void 0) {
|
|
391
|
+
if (!Array.isArray(extras.contactDots)) throw new KhrValidationError(`Node "${name}" has invalid extras.contactDots (expected array)`);
|
|
392
|
+
obj.getWorldQuaternion(_quat);
|
|
393
|
+
contactDots = [];
|
|
394
|
+
for (const dot of extras.contactDots) {
|
|
395
|
+
if (!isObj$3(dot) || typeof dot.role !== "string" || dot.role.length === 0 || !Array.isArray(dot.pos) || dot.pos.length !== 3 || !dot.pos.every(isFiniteNum)) throw new KhrValidationError(`Node "${name}" has invalid entry in extras.contactDots`);
|
|
396
|
+
_dotVec.set(dot.pos[0], dot.pos[1], dot.pos[2]).applyQuaternion(_quat).add(wp);
|
|
397
|
+
const dotPos = [
|
|
398
|
+
_dotVec.x,
|
|
399
|
+
_dotVec.y,
|
|
400
|
+
_dotVec.z
|
|
401
|
+
];
|
|
402
|
+
setSocket(sockets, dot.role, dotPos);
|
|
403
|
+
contactDots.push([dot.role, dotPos]);
|
|
404
|
+
}
|
|
429
405
|
}
|
|
430
|
-
let s = t.centerOfMass === void 0 ? [
|
|
431
|
-
0,
|
|
432
|
-
0,
|
|
433
|
-
0
|
|
434
|
-
] : r(t.centerOfMass, `${i}.motion.centerOfMass`), c = t.inertiaOrientation === void 0 ? [
|
|
435
|
-
0,
|
|
436
|
-
0,
|
|
437
|
-
0,
|
|
438
|
-
1
|
|
439
|
-
] : b(t.inertiaOrientation, `${i}.motion.inertiaOrientation`), l = [
|
|
440
|
-
0,
|
|
441
|
-
0,
|
|
442
|
-
0
|
|
443
|
-
], u = t.inertiaDiagonal !== void 0;
|
|
444
|
-
if (u && (l = r(t.inertiaDiagonal, `${i}.motion.inertiaDiagonal`), l.some((e) => a ? e < 0 : e <= 0))) throw new e(`Node "${i}" has invalid motion.inertiaDiagonal`);
|
|
445
|
-
let d = t.linearVelocity === void 0 ? void 0 : r(t.linearVelocity, `${i}.motion.linearVelocity`), f = t.angularVelocity === void 0 ? void 0 : r(t.angularVelocity, `${i}.motion.angularVelocity`), p = J(t.gravityFactor, `${i}.motion.gravityFactor`);
|
|
446
406
|
return {
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
inertiaOrientation: c,
|
|
452
|
-
hasExplicitInertia: u,
|
|
453
|
-
...d ? { linearVelocity: d } : {},
|
|
454
|
-
...f ? { angularVelocity: f } : {},
|
|
455
|
-
...p === void 0 ? {} : { gravityFactor: p }
|
|
407
|
+
nodeName: name,
|
|
408
|
+
extras,
|
|
409
|
+
worldPosition,
|
|
410
|
+
...contactDots ? { contactDots } : {}
|
|
456
411
|
};
|
|
457
412
|
}
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
if (n !== void 0) return n;
|
|
462
|
-
if (ze > 15) throw new e(`Exceeded Rapier 16-group limit for collisionSystem "${t}"`);
|
|
463
|
-
let r = ze++;
|
|
464
|
-
return Re.set(t, r), r;
|
|
465
|
-
}, r = (t, r) => {
|
|
466
|
-
if (t === void 0) return 0;
|
|
467
|
-
if (!Array.isArray(t)) throw new e(`collisionFilter.${r} must be an array`);
|
|
468
|
-
let i = 0;
|
|
469
|
-
for (let a of t) {
|
|
470
|
-
if (typeof a != "string" || a.length === 0) throw new e(`collisionFilter.${r} has invalid system`);
|
|
471
|
-
i |= 1 << n(a);
|
|
472
|
-
}
|
|
473
|
-
return i;
|
|
474
|
-
};
|
|
475
|
-
return t.map((t, n) => {
|
|
476
|
-
if (!t || typeof t != "object" || Array.isArray(t)) throw new e(`collisionFilters[${n}] must be an object`);
|
|
477
|
-
if (t.collideWithSystems !== void 0 && t.notCollideWithSystems !== void 0) throw new e(`collisionFilters[${n}] cannot specify both collideWithSystems and notCollideWithSystems`);
|
|
478
|
-
let i = r(t.collisionSystems, "collisionSystems"), a = t.collideWithSystems === void 0 ? 65535 & ~r(t.notCollideWithSystems, "notCollideWithSystems") : r(t.collideWithSystems, "collideWithSystems");
|
|
479
|
-
return (i & 65535) << 16 | a & 65535;
|
|
480
|
-
});
|
|
481
|
-
}
|
|
482
|
-
var Ke = (e) => e === 0 ? [
|
|
413
|
+
// src/khrJointParser.ts
|
|
414
|
+
var isObj$2 = (v) => typeof v === "object" && !!v && !Array.isArray(v);
|
|
415
|
+
var axisVector = (axis) => axis === 0 ? [
|
|
483
416
|
1,
|
|
484
417
|
0,
|
|
485
418
|
0
|
|
486
|
-
] :
|
|
419
|
+
] : axis === 1 ? [
|
|
487
420
|
0,
|
|
488
421
|
1,
|
|
489
422
|
0
|
|
@@ -492,110 +425,143 @@ var Ke = (e) => e === 0 ? [
|
|
|
492
425
|
0,
|
|
493
426
|
1
|
|
494
427
|
];
|
|
495
|
-
function
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
428
|
+
function optNum$2(v, label, nonNeg = false) {
|
|
429
|
+
if (v !== void 0) {
|
|
430
|
+
if (typeof v !== "number" || !Number.isFinite(v) || nonNeg && v < 0) throw new KhrValidationError(`Invalid ${label}`);
|
|
431
|
+
return v;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
function decodeJointDrives(rawDrives, jointIdx, nodeName) {
|
|
435
|
+
return rawDrives.map((d, i) => {
|
|
436
|
+
const label = `physicsJoints[${jointIdx}].drives[${i}] on "${nodeName}"`;
|
|
437
|
+
if (!isObj$2(d)) throw new KhrValidationError(`${label} must be an object`);
|
|
438
|
+
if (d.mode !== "force" && d.mode !== "acceleration") throw new KhrValidationError(`${label} missing valid drive.mode`);
|
|
439
|
+
if (d.axis !== 0 && d.axis !== 1 && d.axis !== 2) throw new KhrValidationError(`${label} missing valid drive.axis (0|1|2)`);
|
|
440
|
+
if (d.type !== void 0 && d.type !== "linear" && d.type !== "angular") throw new KhrValidationError(`${label} invalid drive.type`);
|
|
441
|
+
const positionTarget = optNum$2(d.positionTarget, `${label} drive.positionTarget`);
|
|
442
|
+
const velocityTarget = optNum$2(d.velocityTarget, `${label} drive.velocityTarget`);
|
|
443
|
+
if (positionTarget === void 0 && velocityTarget === void 0) throw new KhrValidationError(`${label} requires finite drive.positionTarget or drive.velocityTarget`);
|
|
444
|
+
const stiffness = optNum$2(d.stiffness, `${label} drive.stiffness`, true);
|
|
445
|
+
const damping = optNum$2(d.damping, `${label} drive.damping`, true);
|
|
446
|
+
const maxForce = optNum$2(d.maxForce, `${label} drive.maxForce`, true);
|
|
505
447
|
return {
|
|
506
|
-
...
|
|
507
|
-
mode:
|
|
508
|
-
axis:
|
|
509
|
-
...
|
|
510
|
-
...
|
|
511
|
-
...
|
|
512
|
-
...
|
|
513
|
-
...
|
|
448
|
+
...d.type ? { type: d.type } : {},
|
|
449
|
+
mode: d.mode,
|
|
450
|
+
axis: d.axis,
|
|
451
|
+
...positionTarget === void 0 ? {} : { positionTarget },
|
|
452
|
+
...velocityTarget === void 0 ? {} : { velocityTarget },
|
|
453
|
+
...stiffness === void 0 ? {} : { stiffness },
|
|
454
|
+
...damping === void 0 ? {} : { damping },
|
|
455
|
+
...maxForce === void 0 ? {} : { maxForce }
|
|
514
456
|
};
|
|
515
457
|
});
|
|
516
458
|
}
|
|
517
|
-
function
|
|
518
|
-
|
|
519
|
-
if (!
|
|
520
|
-
if (
|
|
521
|
-
if (
|
|
522
|
-
|
|
459
|
+
function decodeJoint(rawJoints, jointIdx, nodeName, connectedNode, childNode, enableCollision, role, connectedNodeName = connectedNode.name) {
|
|
460
|
+
const raw = rawJoints[jointIdx];
|
|
461
|
+
if (!isObj$2(raw)) throw new KhrValidationError(`physicsJoints[${jointIdx}] out of bounds for "${nodeName}"`);
|
|
462
|
+
if (raw.limits !== void 0 && !Array.isArray(raw.limits)) throw new KhrValidationError(`physicsJoints[${jointIdx}].limits on "${nodeName}" must be an array`);
|
|
463
|
+
if (raw.drives !== void 0 && !Array.isArray(raw.drives)) throw new KhrValidationError(`physicsJoints[${jointIdx}].drives on "${nodeName}" must be an array`);
|
|
464
|
+
const rawLimits = raw.limits ?? [];
|
|
465
|
+
const rawDrives = raw.drives ?? [];
|
|
466
|
+
const lin = [
|
|
523
467
|
"unlimited",
|
|
524
468
|
"unlimited",
|
|
525
469
|
"unlimited"
|
|
526
|
-
]
|
|
470
|
+
];
|
|
471
|
+
const ang = [
|
|
527
472
|
"unlimited",
|
|
528
473
|
"unlimited",
|
|
529
474
|
"unlimited"
|
|
530
|
-
]
|
|
475
|
+
];
|
|
476
|
+
const linGain = [
|
|
531
477
|
void 0,
|
|
532
478
|
void 0,
|
|
533
479
|
void 0
|
|
534
|
-
]
|
|
480
|
+
];
|
|
481
|
+
const angGain = [
|
|
535
482
|
void 0,
|
|
536
483
|
void 0,
|
|
537
484
|
void 0
|
|
538
|
-
]
|
|
539
|
-
|
|
540
|
-
if (
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
if ((i.min !== void 0 || i.max !== void 0) && (c === void 0 || l === void 0)) throw new e(`physicsJoints[${n}].limits on "${r}" must specify both min and max`);
|
|
546
|
-
if (c !== void 0 && l !== void 0 && c > l) throw new e(`physicsJoints[${n}] limit.min > limit.max on "${r}"`);
|
|
547
|
-
let u = J(i.stiffness, `physicsJoints[${n}] limit.stiffness on "${r}"`, !0), d = J(i.damping, `physicsJoints[${n}] limit.damping on "${r}"`, !0), f = c !== void 0 && l !== void 0 && l - c <= 1e-6;
|
|
548
|
-
if (!f && s.length > 1) throw new e(`physicsJoints[${n}] coupled multi-axis limit on "${r}" is unsupported`);
|
|
549
|
-
for (let e of s) a[e] = f ? "locked" : c !== void 0 && l !== void 0 ? [c, l] : "unlimited", f || u === void 0 && d === void 0 || (o[e] = {
|
|
550
|
-
...u === void 0 ? {} : { stiffness: u },
|
|
551
|
-
...d === void 0 ? {} : { damping: d }
|
|
485
|
+
];
|
|
486
|
+
const applyAxes = (axesRaw, lim, state, gains) => {
|
|
487
|
+
if (axesRaw === void 0) return;
|
|
488
|
+
if (!Array.isArray(axesRaw)) throw new KhrValidationError(`physicsJoints[${jointIdx}].limits axes on "${nodeName}" must be an array`);
|
|
489
|
+
const axes = axesRaw.map((ax) => {
|
|
490
|
+
if (ax !== 0 && ax !== 1 && ax !== 2) throw new KhrValidationError(`physicsJoints[${jointIdx}].limits invalid axis ${String(ax)} on "${nodeName}"`);
|
|
491
|
+
return ax;
|
|
552
492
|
});
|
|
493
|
+
const min = optNum$2(lim.min, `physicsJoints[${jointIdx}] limit.min on "${nodeName}"`);
|
|
494
|
+
const max = optNum$2(lim.max, `physicsJoints[${jointIdx}] limit.max on "${nodeName}"`);
|
|
495
|
+
if ((lim.min !== void 0 || lim.max !== void 0) && (min === void 0 || max === void 0)) throw new KhrValidationError(`physicsJoints[${jointIdx}].limits on "${nodeName}" must specify both min and max`);
|
|
496
|
+
if (min !== void 0 && max !== void 0 && min > max) throw new KhrValidationError(`physicsJoints[${jointIdx}] limit.min > limit.max on "${nodeName}"`);
|
|
497
|
+
const stiffness = optNum$2(lim.stiffness, `physicsJoints[${jointIdx}] limit.stiffness on "${nodeName}"`, true);
|
|
498
|
+
const damping = optNum$2(lim.damping, `physicsJoints[${jointIdx}] limit.damping on "${nodeName}"`, true);
|
|
499
|
+
const locked = min !== void 0 && max !== void 0 && max - min <= 1e-6;
|
|
500
|
+
if (!locked && axes.length > 1) throw new KhrValidationError(`physicsJoints[${jointIdx}] coupled multi-axis limit on "${nodeName}" is unsupported`);
|
|
501
|
+
for (const ax of axes) {
|
|
502
|
+
state[ax] = locked ? "locked" : min !== void 0 && max !== void 0 ? [min, max] : "unlimited";
|
|
503
|
+
if (!locked && (stiffness !== void 0 || damping !== void 0)) gains[ax] = {
|
|
504
|
+
...stiffness === void 0 ? {} : { stiffness },
|
|
505
|
+
...damping === void 0 ? {} : { damping }
|
|
506
|
+
};
|
|
507
|
+
}
|
|
553
508
|
};
|
|
554
|
-
for (
|
|
555
|
-
if (!
|
|
556
|
-
|
|
509
|
+
for (const lim of rawLimits) {
|
|
510
|
+
if (!isObj$2(lim)) throw new KhrValidationError(`physicsJoints[${jointIdx}].limits entry on "${nodeName}" must be an object`);
|
|
511
|
+
applyAxes(lim.linearAxes, lim, lin, linGain);
|
|
512
|
+
applyAxes(lim.angularAxes, lim, ang, angGain);
|
|
557
513
|
}
|
|
558
|
-
|
|
514
|
+
const freeLin = [
|
|
559
515
|
0,
|
|
560
516
|
1,
|
|
561
517
|
2
|
|
562
|
-
].filter((
|
|
518
|
+
].filter((ax) => lin[ax] !== "locked");
|
|
519
|
+
const freeAng = [
|
|
563
520
|
0,
|
|
564
521
|
1,
|
|
565
522
|
2
|
|
566
|
-
].filter((
|
|
523
|
+
].filter((ax) => ang[ax] !== "locked");
|
|
524
|
+
let kind;
|
|
525
|
+
let axis = [
|
|
567
526
|
0,
|
|
568
527
|
1,
|
|
569
528
|
0
|
|
570
|
-
]
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
} else
|
|
581
|
-
|
|
529
|
+
];
|
|
530
|
+
let limits = null;
|
|
531
|
+
let gain;
|
|
532
|
+
if (freeLin.length === 0 && freeAng.length === 0) kind = "fixed";
|
|
533
|
+
else if (freeLin.length === 0 && freeAng.length === 1) {
|
|
534
|
+
kind = "revolute";
|
|
535
|
+
axis = axisVector(freeAng[0]);
|
|
536
|
+
const s = ang[freeAng[0]];
|
|
537
|
+
limits = Array.isArray(s) ? s : null;
|
|
538
|
+
gain = angGain[freeAng[0]];
|
|
539
|
+
} else if (freeAng.length === 0 && freeLin.length === 1) {
|
|
540
|
+
kind = "prismatic";
|
|
541
|
+
axis = axisVector(freeLin[0]);
|
|
542
|
+
const s = lin[freeLin[0]];
|
|
543
|
+
limits = Array.isArray(s) ? s : null;
|
|
544
|
+
gain = linGain[freeLin[0]];
|
|
545
|
+
} else throw new KhrValidationError(`physicsJoints[${jointIdx}] unsupported DOF (${freeLin.length} lin, ${freeAng.length} ang) on "${nodeName}"`);
|
|
546
|
+
const drives = decodeJointDrives(rawDrives, jointIdx, nodeName);
|
|
547
|
+
const anchor1Vec = childNode.getWorldPosition(new THREE.Vector3()).sub(connectedNode.getWorldPosition(new THREE.Vector3())).applyQuaternion(connectedNode.getWorldQuaternion(new THREE.Quaternion()).invert());
|
|
582
548
|
return {
|
|
583
|
-
index:
|
|
584
|
-
nodeName
|
|
585
|
-
connectedNodeName
|
|
586
|
-
kind
|
|
587
|
-
axis
|
|
588
|
-
limits
|
|
589
|
-
enableCollision
|
|
590
|
-
role
|
|
591
|
-
...
|
|
592
|
-
...
|
|
593
|
-
drive:
|
|
594
|
-
drives
|
|
549
|
+
index: jointIdx,
|
|
550
|
+
nodeName,
|
|
551
|
+
connectedNodeName,
|
|
552
|
+
kind,
|
|
553
|
+
axis,
|
|
554
|
+
limits,
|
|
555
|
+
enableCollision,
|
|
556
|
+
role,
|
|
557
|
+
...gain?.stiffness === void 0 ? {} : { limitStiffness: gain.stiffness },
|
|
558
|
+
...gain?.damping === void 0 ? {} : { limitDamping: gain.damping },
|
|
559
|
+
drive: drives[0] ?? null,
|
|
560
|
+
drives,
|
|
595
561
|
anchor1: [
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
562
|
+
anchor1Vec.x,
|
|
563
|
+
anchor1Vec.y,
|
|
564
|
+
anchor1Vec.z
|
|
599
565
|
],
|
|
600
566
|
anchor2: [
|
|
601
567
|
0,
|
|
@@ -604,215 +570,362 @@ function Je(t, n, r, i, a, o, s, c = i.name) {
|
|
|
604
570
|
]
|
|
605
571
|
};
|
|
606
572
|
}
|
|
573
|
+
// src/khrShapeParser.ts
|
|
574
|
+
var isObj$1 = (v) => typeof v === "object" && !!v && !Array.isArray(v);
|
|
575
|
+
var isPos$2 = (v) => typeof v === "number" && Number.isFinite(v) && v > 0;
|
|
576
|
+
var COMBINE_MODES$1 = /* @__PURE__ */ new Set([
|
|
577
|
+
"average",
|
|
578
|
+
"minimum",
|
|
579
|
+
"maximum",
|
|
580
|
+
"multiply"
|
|
581
|
+
]);
|
|
582
|
+
function optNum$1(v, label, nonNeg = false) {
|
|
583
|
+
if (v !== void 0) {
|
|
584
|
+
if (typeof v !== "number" || !Number.isFinite(v) || nonNeg && v < 0) throw new KhrValidationError(`Invalid ${label}`);
|
|
585
|
+
return v;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
function optCombineMode(v, label) {
|
|
589
|
+
if (v === void 0) return void 0;
|
|
590
|
+
if (typeof v !== "string" || !COMBINE_MODES$1.has(v)) throw new KhrValidationError(`Invalid ${label}: "${String(v)}"`);
|
|
591
|
+
return v;
|
|
592
|
+
}
|
|
593
|
+
function decodeShape(raw, idx) {
|
|
594
|
+
if (!isObj$1(raw)) throw new KhrValidationError(`KHR_implicit_shapes[${idx}] must be an object`);
|
|
595
|
+
const type = raw.type;
|
|
596
|
+
if (type === "box") {
|
|
597
|
+
if (raw.box !== void 0 && !isObj$1(raw.box)) throw new KhrValidationError(`KHR_implicit_shapes[${idx}] invalid box`);
|
|
598
|
+
const box = isObj$1(raw.box) ? raw.box : {};
|
|
599
|
+
const size = box.size === void 0 ? [
|
|
600
|
+
1,
|
|
601
|
+
1,
|
|
602
|
+
1
|
|
603
|
+
] : box.size;
|
|
604
|
+
if (!Array.isArray(size) || size.length !== 3 || !size.every(isPos$2)) throw new KhrValidationError(`KHR_implicit_shapes[${idx}] invalid box.size`);
|
|
605
|
+
return {
|
|
606
|
+
type: "box",
|
|
607
|
+
size: [
|
|
608
|
+
size[0],
|
|
609
|
+
size[1],
|
|
610
|
+
size[2]
|
|
611
|
+
]
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
if (type === "sphere") {
|
|
615
|
+
if (raw.sphere !== void 0 && !isObj$1(raw.sphere)) throw new KhrValidationError(`KHR_implicit_shapes[${idx}] invalid sphere`);
|
|
616
|
+
const sphere = isObj$1(raw.sphere) ? raw.sphere : {};
|
|
617
|
+
const radius = sphere.radius === void 0 ? .5 : sphere.radius;
|
|
618
|
+
if (!isPos$2(radius)) throw new KhrValidationError(`KHR_implicit_shapes[${idx}] invalid sphere.radius`);
|
|
619
|
+
return {
|
|
620
|
+
type: "sphere",
|
|
621
|
+
radius
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
if (type === "cylinder" || type === "capsule") {
|
|
625
|
+
if (raw[type] !== void 0 && !isObj$1(raw[type])) throw new KhrValidationError(`KHR_implicit_shapes[${idx}] invalid ${type}`);
|
|
626
|
+
const g = isObj$1(raw[type]) ? raw[type] : {};
|
|
627
|
+
const height = g.height === void 0 ? type === "cylinder" ? 2 : 1 : g.height;
|
|
628
|
+
if (!isPos$2(height)) throw new KhrValidationError(`KHR_implicit_shapes[${idx}] invalid ${type}.height`);
|
|
629
|
+
if (g.radius !== void 0 && !isPos$2(g.radius)) throw new KhrValidationError(`KHR_implicit_shapes[${idx}] invalid ${type}.radius`);
|
|
630
|
+
const baseR = g.radius === void 0 ? .5 : g.radius;
|
|
631
|
+
const radiusTop = g.radiusTop === void 0 ? baseR : g.radiusTop;
|
|
632
|
+
const radiusBottom = g.radiusBottom === void 0 ? baseR : g.radiusBottom;
|
|
633
|
+
if (!isPos$2(radiusTop)) throw new KhrValidationError(`KHR_implicit_shapes[${idx}] invalid ${type}.radiusTop`);
|
|
634
|
+
if (!isPos$2(radiusBottom)) throw new KhrValidationError(`KHR_implicit_shapes[${idx}] invalid ${type}.radiusBottom`);
|
|
635
|
+
if (Math.abs(radiusTop - radiusBottom) > 1e-6) throw new KhrValidationError(`KHR_implicit_shapes[${idx}] tapered ${type} (${radiusTop} !== ${radiusBottom}) is unsupported`);
|
|
636
|
+
return {
|
|
637
|
+
type,
|
|
638
|
+
height,
|
|
639
|
+
radiusTop,
|
|
640
|
+
radiusBottom
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
throw new KhrValidationError(`KHR_implicit_shapes[${idx}] unsupported shape type "${String(type)}"`);
|
|
644
|
+
}
|
|
645
|
+
function decodePhysicsMaterial(rawMaterials, idx, ownerName) {
|
|
646
|
+
if (idx === void 0) return {};
|
|
647
|
+
if (typeof idx !== "number" || !Number.isInteger(idx) || idx < 0 || idx >= rawMaterials.length) throw new KhrValidationError(`Node "${ownerName}" references out-of-bounds physicsMaterial ${String(idx)}`);
|
|
648
|
+
const mat = rawMaterials[idx];
|
|
649
|
+
if (!isObj$1(mat)) throw new KhrValidationError(`Invalid physicsMaterials[${idx}] on "${ownerName}"`);
|
|
650
|
+
const dynF = optNum$1(mat.dynamicFriction, `physicsMaterials[${idx}].dynamicFriction`, true);
|
|
651
|
+
const statF = optNum$1(mat.staticFriction, `physicsMaterials[${idx}].staticFriction`, true);
|
|
652
|
+
const rest = optNum$1(mat.restitution, `physicsMaterials[${idx}].restitution`, true);
|
|
653
|
+
const frictionCombine = optCombineMode(mat.frictionCombine, `physicsMaterials[${idx}].frictionCombine`);
|
|
654
|
+
const restitutionCombine = optCombineMode(mat.restitutionCombine, `physicsMaterials[${idx}].restitutionCombine`);
|
|
655
|
+
return {
|
|
656
|
+
...dynF !== void 0 || statF !== void 0 ? { friction: dynF ?? statF } : {},
|
|
657
|
+
...rest === void 0 ? {} : { restitution: rest },
|
|
658
|
+
...frictionCombine === void 0 ? {} : { frictionCombine },
|
|
659
|
+
...restitutionCombine === void 0 ? {} : { restitutionCombine }
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
// src/khrParsers.ts
|
|
663
|
+
var isPos$1 = (v) => typeof v === "number" && Number.isFinite(v) && v > 0;
|
|
664
|
+
var isNonNeg$1 = (v) => typeof v === "number" && Number.isFinite(v) && v >= 0;
|
|
665
|
+
function optNum(v, label, nonNeg = false) {
|
|
666
|
+
if (v !== void 0) {
|
|
667
|
+
if (typeof v !== "number" || !Number.isFinite(v) || nonNeg && v < 0) throw new KhrValidationError(`Invalid ${label}`);
|
|
668
|
+
return v;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
function decodeMotion(motion, hasMotion, name) {
|
|
672
|
+
if (motion.isKinematic !== void 0 && typeof motion.isKinematic !== "boolean") throw new KhrValidationError(`Node "${name}" has non-boolean motion.isKinematic`);
|
|
673
|
+
const isKinematic = !hasMotion || Boolean(motion.isKinematic);
|
|
674
|
+
let mass = 0;
|
|
675
|
+
if (!isKinematic) {
|
|
676
|
+
if (!isPos$1(motion.mass)) throw new KhrValidationError(`Dynamic node "${name}" is missing positive finite motion.mass`);
|
|
677
|
+
mass = motion.mass;
|
|
678
|
+
} else if (motion.mass !== void 0) {
|
|
679
|
+
if (!isNonNeg$1(motion.mass)) throw new KhrValidationError(`Kinematic node "${name}" has invalid motion.mass`);
|
|
680
|
+
mass = motion.mass;
|
|
681
|
+
}
|
|
682
|
+
const centerOfMass = motion.centerOfMass === void 0 ? [
|
|
683
|
+
0,
|
|
684
|
+
0,
|
|
685
|
+
0
|
|
686
|
+
] : readFiniteVec3(motion.centerOfMass, `${name}.motion.centerOfMass`);
|
|
687
|
+
const inertiaOrientation = motion.inertiaOrientation === void 0 ? [
|
|
688
|
+
0,
|
|
689
|
+
0,
|
|
690
|
+
0,
|
|
691
|
+
1
|
|
692
|
+
] : readUnitQuat(motion.inertiaOrientation, `${name}.motion.inertiaOrientation`);
|
|
693
|
+
let inertiaDiagonal = [
|
|
694
|
+
0,
|
|
695
|
+
0,
|
|
696
|
+
0
|
|
697
|
+
];
|
|
698
|
+
const hasExplicitInertia = motion.inertiaDiagonal !== void 0;
|
|
699
|
+
if (hasExplicitInertia) {
|
|
700
|
+
inertiaDiagonal = readFiniteVec3(motion.inertiaDiagonal, `${name}.motion.inertiaDiagonal`);
|
|
701
|
+
if (inertiaDiagonal.some((n) => isKinematic ? n < 0 : n <= 0)) throw new KhrValidationError(`Node "${name}" has invalid motion.inertiaDiagonal`);
|
|
702
|
+
}
|
|
703
|
+
const linearVelocity = motion.linearVelocity === void 0 ? void 0 : readFiniteVec3(motion.linearVelocity, `${name}.motion.linearVelocity`);
|
|
704
|
+
const angularVelocity = motion.angularVelocity === void 0 ? void 0 : readFiniteVec3(motion.angularVelocity, `${name}.motion.angularVelocity`);
|
|
705
|
+
const gravityFactor = optNum(motion.gravityFactor, `${name}.motion.gravityFactor`);
|
|
706
|
+
return {
|
|
707
|
+
isKinematic,
|
|
708
|
+
mass,
|
|
709
|
+
centerOfMass,
|
|
710
|
+
inertiaDiagonal,
|
|
711
|
+
inertiaOrientation,
|
|
712
|
+
hasExplicitInertia,
|
|
713
|
+
...linearVelocity ? { linearVelocity } : {},
|
|
714
|
+
...angularVelocity ? { angularVelocity } : {},
|
|
715
|
+
...gravityFactor === void 0 ? {} : { gravityFactor }
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
function decodeCollisionGroups(rawFilters) {
|
|
719
|
+
const collisionSystemIds = /* @__PURE__ */ new Map([["default", 0]]);
|
|
720
|
+
let nextCollisionBit = 1;
|
|
721
|
+
const bitFor = (sys) => {
|
|
722
|
+
const hit = collisionSystemIds.get(sys);
|
|
723
|
+
if (hit !== void 0) return hit;
|
|
724
|
+
if (nextCollisionBit > 15) throw new KhrValidationError(`Exceeded Rapier 16-group limit for collisionSystem "${sys}"`);
|
|
725
|
+
const b = nextCollisionBit++;
|
|
726
|
+
collisionSystemIds.set(sys, b);
|
|
727
|
+
return b;
|
|
728
|
+
};
|
|
729
|
+
const maskFor = (arr, label) => {
|
|
730
|
+
if (arr === void 0) return 0;
|
|
731
|
+
if (!Array.isArray(arr)) throw new KhrValidationError(`collisionFilter.${label} must be an array`);
|
|
732
|
+
let mask = 0;
|
|
733
|
+
for (const s of arr) {
|
|
734
|
+
if (typeof s !== "string" || s.length === 0) throw new KhrValidationError(`collisionFilter.${label} has invalid system`);
|
|
735
|
+
mask |= 1 << bitFor(s);
|
|
736
|
+
}
|
|
737
|
+
return mask;
|
|
738
|
+
};
|
|
739
|
+
return rawFilters.map((f, idx) => {
|
|
740
|
+
if (!f || typeof f !== "object" || Array.isArray(f)) throw new KhrValidationError(`collisionFilters[${idx}] must be an object`);
|
|
741
|
+
if (f.collideWithSystems !== void 0 && f.notCollideWithSystems !== void 0) throw new KhrValidationError(`collisionFilters[${idx}] cannot specify both collideWithSystems and notCollideWithSystems`);
|
|
742
|
+
const membership = maskFor(f.collisionSystems, "collisionSystems");
|
|
743
|
+
const filter = f.collideWithSystems === void 0 ? 65535 & ~maskFor(f.notCollideWithSystems, "notCollideWithSystems") : maskFor(f.collideWithSystems, "collideWithSystems");
|
|
744
|
+
return (membership & 65535) << 16 | filter & 65535;
|
|
745
|
+
});
|
|
746
|
+
}
|
|
607
747
|
// src/khrPluginHelpers.ts
|
|
608
|
-
var
|
|
609
|
-
function
|
|
610
|
-
if (
|
|
611
|
-
if (!Array.isArray(
|
|
612
|
-
return
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
|
|
748
|
+
var isPlainObject = (v) => typeof v === "object" && !!v && !Array.isArray(v);
|
|
749
|
+
function reqArr(val, label) {
|
|
750
|
+
if (val === void 0) return [];
|
|
751
|
+
if (!Array.isArray(val)) throw new KhrValidationError(`${label} must be an array`);
|
|
752
|
+
return val;
|
|
753
|
+
}
|
|
754
|
+
var HEADLESS_TEXTURE_EXT_NAMES = [
|
|
755
|
+
"EXT_texture_webp",
|
|
756
|
+
"EXT_texture_avif",
|
|
757
|
+
"KHR_texture_basisu"
|
|
758
|
+
];
|
|
759
|
+
function isHeadlessTextureEnv() {
|
|
760
|
+
return typeof document === "undefined" && (typeof createImageBitmap === "undefined" || typeof self === "undefined");
|
|
761
|
+
}
|
|
762
|
+
function stubTextureInParser(parser, textureIndex) {
|
|
763
|
+
const tex = new THREE.Texture();
|
|
764
|
+
tex.flipY = false;
|
|
765
|
+
parser.associations.set(tex, { textures: textureIndex });
|
|
766
|
+
return Promise.resolve(tex);
|
|
767
|
+
}
|
|
768
|
+
function patchHeadlessTexturePlugins(parser) {
|
|
769
|
+
const plugins = parser.plugins;
|
|
770
|
+
if (!plugins) return;
|
|
771
|
+
for (const extName of HEADLESS_TEXTURE_EXT_NAMES) {
|
|
772
|
+
const existing = plugins[extName];
|
|
773
|
+
if (!existing) continue;
|
|
774
|
+
existing.loadTexture = (textureIndex) => {
|
|
775
|
+
if (!(parser.json?.textures?.[textureIndex])?.extensions?.[extName]) return null;
|
|
776
|
+
return stubTextureInParser(parser, textureIndex);
|
|
777
|
+
};
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
function createHeadlessTexturePlugin() {
|
|
781
|
+
return (parser) => ({
|
|
616
782
|
name: "khr3_headless_texture",
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
return
|
|
620
|
-
}
|
|
783
|
+
beforeRoot() {
|
|
784
|
+
patchHeadlessTexturePlugins(parser);
|
|
785
|
+
return null;
|
|
786
|
+
},
|
|
787
|
+
loadTexture: (textureIndex) => stubTextureInParser(parser, textureIndex)
|
|
621
788
|
});
|
|
622
789
|
}
|
|
623
|
-
function
|
|
624
|
-
|
|
625
|
-
for (let
|
|
626
|
-
|
|
627
|
-
if (!
|
|
628
|
-
|
|
629
|
-
if (
|
|
630
|
-
if (!Array.isArray(
|
|
631
|
-
for (
|
|
790
|
+
function buildNodeGraphLookup(gltf, rawNodes, rawGltf = gltf.parser?.json) {
|
|
791
|
+
const parentIdx = new Int32Array(rawNodes.length).fill(-1);
|
|
792
|
+
for (let i = 0; i < rawNodes.length; i++) {
|
|
793
|
+
const node = rawNodes[i];
|
|
794
|
+
if (!isPlainObject(node)) throw new KhrValidationError(`GLTF node[${i}] must be an object`);
|
|
795
|
+
const label = typeof node.name === "string" && node.name.length > 0 ? node.name : String(i);
|
|
796
|
+
if (node.children !== void 0) {
|
|
797
|
+
if (!Array.isArray(node.children)) throw new KhrValidationError(`GLTF node "${label}" children must be an array`);
|
|
798
|
+
for (const c of node.children) if (typeof c !== "number" || !Number.isInteger(c) || c < 0 || c >= rawNodes.length) throw new KhrValidationError(`GLTF node "${label}" has out-of-bounds child index ${String(c)}`);
|
|
632
799
|
}
|
|
633
800
|
}
|
|
634
|
-
let
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
801
|
+
let sceneRoots;
|
|
802
|
+
const inActiveScene = /* @__PURE__ */ new Set();
|
|
803
|
+
if (Array.isArray(rawGltf?.scenes)) {
|
|
804
|
+
const sceneIdx = rawGltf.scene ?? 0;
|
|
805
|
+
const sceneDef = rawGltf.scenes[sceneIdx];
|
|
806
|
+
if (!isPlainObject(sceneDef)) throw new KhrValidationError(`GLTF scene[${sceneIdx}] must be an object`);
|
|
807
|
+
if (sceneDef.nodes !== void 0 && !Array.isArray(sceneDef.nodes)) throw new KhrValidationError(`GLTF scene[${sceneIdx}].nodes must be an array`);
|
|
808
|
+
sceneRoots = (sceneDef.nodes ?? []).map((n, pos) => {
|
|
809
|
+
if (typeof n !== "number" || !Number.isInteger(n) || n < 0 || n >= rawNodes.length) throw new KhrValidationError(`GLTF scene[${sceneIdx}].nodes[${pos}] has invalid node index ${String(n)}`);
|
|
810
|
+
return n;
|
|
642
811
|
});
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
if (
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
if (
|
|
812
|
+
const stack = [...sceneRoots];
|
|
813
|
+
while (stack.length > 0) {
|
|
814
|
+
const cur = stack.pop();
|
|
815
|
+
if (inActiveScene.has(cur)) continue;
|
|
816
|
+
inActiveScene.add(cur);
|
|
817
|
+
const children = rawNodes[cur].children;
|
|
818
|
+
if (children) for (const c of children) stack.push(c);
|
|
650
819
|
}
|
|
651
|
-
} else for (let
|
|
652
|
-
|
|
653
|
-
for (
|
|
654
|
-
|
|
655
|
-
if (
|
|
656
|
-
if (
|
|
657
|
-
|
|
658
|
-
throw new
|
|
820
|
+
} else for (let i = 0; i < rawNodes.length; i++) inActiveScene.add(i);
|
|
821
|
+
const rootSet = sceneRoots ? new Set(sceneRoots) : void 0;
|
|
822
|
+
for (const i of inActiveScene) {
|
|
823
|
+
const children = rawNodes[i].children;
|
|
824
|
+
if (children) for (const c of children) {
|
|
825
|
+
if (parentIdx[c] !== -1 || rootSet?.has(c)) {
|
|
826
|
+
const label = typeof rawNodes[c].name === "string" && rawNodes[c].name.length > 0 ? rawNodes[c].name : String(c);
|
|
827
|
+
throw new KhrValidationError(`GLTF node "${label}" has multiple parents in active scene`);
|
|
659
828
|
}
|
|
660
|
-
for (let
|
|
661
|
-
|
|
662
|
-
throw new
|
|
829
|
+
for (let anc = i; anc !== -1; anc = parentIdx[anc]) if (anc === c) {
|
|
830
|
+
const label = typeof rawNodes[c].name === "string" && rawNodes[c].name.length > 0 ? rawNodes[c].name : String(c);
|
|
831
|
+
throw new KhrValidationError(`GLTF node "${label}" has cyclic parent in active scene`);
|
|
663
832
|
}
|
|
664
|
-
|
|
833
|
+
parentIdx[c] = i;
|
|
665
834
|
}
|
|
666
835
|
}
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
836
|
+
const usedNames = /* @__PURE__ */ new Set();
|
|
837
|
+
const nodeNames = new Array(rawNodes.length);
|
|
838
|
+
const assignName = (idx, dedupe) => {
|
|
839
|
+
const rawName = rawNodes[idx].name;
|
|
840
|
+
let name = typeof rawName === "string" && rawName.length > 0 ? rawName : `node_${idx}`;
|
|
841
|
+
if (dedupe) {
|
|
842
|
+
if (usedNames.has(name)) name = `${name}_${idx}`;
|
|
843
|
+
while (usedNames.has(name)) name = `${name}_`;
|
|
844
|
+
usedNames.add(name);
|
|
672
845
|
}
|
|
673
|
-
|
|
846
|
+
nodeNames[idx] = name;
|
|
674
847
|
};
|
|
675
|
-
for (let
|
|
676
|
-
for (let
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
848
|
+
for (let i = 0; i < rawNodes.length; i++) if (inActiveScene.has(i)) assignName(i, true);
|
|
849
|
+
for (let i = 0; i < rawNodes.length; i++) if (!inActiveScene.has(i)) assignName(i, false);
|
|
850
|
+
const byNodeIdx = /* @__PURE__ */ new Map();
|
|
851
|
+
const byName = /* @__PURE__ */ new Map();
|
|
852
|
+
gltf.scene.traverse((obj) => {
|
|
853
|
+
const nIdx = (gltf.parser?.associations?.get(obj))?.nodes;
|
|
854
|
+
if (typeof nIdx === "number" && inActiveScene.has(nIdx) && !byNodeIdx.has(nIdx)) byNodeIdx.set(nIdx, obj);
|
|
855
|
+
if (obj.name && !byName.has(obj.name)) byName.set(obj.name, obj);
|
|
856
|
+
});
|
|
857
|
+
if (sceneRoots && gltf.scene.children.length === sceneRoots.length) {
|
|
858
|
+
const seen = /* @__PURE__ */ new Set();
|
|
859
|
+
const matchTree = (idx, obj) => {
|
|
860
|
+
if (seen.has(idx)) return;
|
|
861
|
+
seen.add(idx);
|
|
862
|
+
if (!byNodeIdx.has(idx)) byNodeIdx.set(idx, obj);
|
|
863
|
+
const children = rawNodes[idx].children;
|
|
864
|
+
if (!children || children.length === 0) return;
|
|
865
|
+
const offset = obj.children.length - children.length;
|
|
866
|
+
if (offset < 0) return;
|
|
867
|
+
for (let k = 0; k < children.length; k++) matchTree(children[k], obj.children[offset + k]);
|
|
689
868
|
};
|
|
690
|
-
for (let
|
|
869
|
+
for (let k = 0; k < sceneRoots.length; k++) matchTree(sceneRoots[k], gltf.scene.children[k]);
|
|
691
870
|
}
|
|
692
871
|
return {
|
|
693
|
-
parentIdx
|
|
694
|
-
nodeNames
|
|
695
|
-
resolveObj: (
|
|
696
|
-
if (
|
|
697
|
-
|
|
698
|
-
if (
|
|
699
|
-
|
|
700
|
-
|
|
872
|
+
parentIdx,
|
|
873
|
+
nodeNames,
|
|
874
|
+
resolveObj: (idx) => {
|
|
875
|
+
if (idx < 0 || idx >= rawNodes.length || !inActiveScene.has(idx)) return void 0;
|
|
876
|
+
const hit = byNodeIdx.get(idx);
|
|
877
|
+
if (hit) return hit;
|
|
878
|
+
const name = nodeNames[idx];
|
|
879
|
+
const sanitized = THREE.PropertyBinding.sanitizeNodeName(name);
|
|
880
|
+
return byName.get(name) ?? byName.get(sanitized) ?? gltf.scene.getObjectByName(name);
|
|
701
881
|
}
|
|
702
882
|
};
|
|
703
883
|
}
|
|
704
|
-
function
|
|
705
|
-
if (!
|
|
706
|
-
|
|
707
|
-
if (!
|
|
708
|
-
if (
|
|
709
|
-
if (typeof
|
|
710
|
-
return
|
|
884
|
+
function resolveColliderShape(col, ownerName, shapes, nodeNames, resolveObj) {
|
|
885
|
+
if (!isPlainObject(col)) throw new KhrValidationError(`Node "${ownerName}" has invalid collider (expected object)`);
|
|
886
|
+
const geo = col.geometry;
|
|
887
|
+
if (!isPlainObject(geo)) throw new KhrValidationError(`Node "${ownerName}" is missing collider.geometry object`);
|
|
888
|
+
if (geo.shape !== void 0) {
|
|
889
|
+
if (typeof geo.shape !== "number" || !Number.isInteger(geo.shape) || geo.shape < 0 || geo.shape >= shapes.length) throw new KhrValidationError(`Node "${ownerName}" references missing collider.geometry.shape ${String(geo.shape)}`);
|
|
890
|
+
return shapes[geo.shape];
|
|
711
891
|
}
|
|
712
|
-
if (
|
|
713
|
-
if (typeof
|
|
714
|
-
if (
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
892
|
+
if (geo.node !== void 0) {
|
|
893
|
+
if (typeof geo.node !== "number" || !Number.isInteger(geo.node)) throw new KhrValidationError(`Node "${ownerName}" has invalid collider.geometry.node`);
|
|
894
|
+
if (geo.convexHull !== void 0 && typeof geo.convexHull !== "boolean") throw new KhrValidationError(`Node "${ownerName}" has non-boolean collider.geometry.convexHull`);
|
|
895
|
+
const target = resolveObj(geo.node);
|
|
896
|
+
const meshName = nodeNames[geo.node] ?? `node_${geo.node}`;
|
|
897
|
+
if (!target) throw new KhrValidationError(`Node "${ownerName}" references missing mesh node "${meshName}"`);
|
|
898
|
+
return extractMeshColliderShape(target, meshName, Boolean(geo.convexHull));
|
|
718
899
|
}
|
|
719
|
-
throw new
|
|
900
|
+
throw new KhrValidationError(`Node "${ownerName}" collider.geometry must specify shape or node`);
|
|
720
901
|
}
|
|
721
|
-
// src/
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
if (o !== void 0 && !Y(o)) throw new e("KHR_implicit_shapes extension must be an object");
|
|
733
|
-
let c = X(o?.shapes, "KHR_implicit_shapes.shapes").map((e, t) => He(e, t)), l = X(a?.physicsMaterials, "KHR_physics_rigid_bodies.physicsMaterials"), d = Ge(X(a?.collisionFilters, "KHR_physics_rigid_bodies.collisionFilters")), p = X(a?.physicsJoints, "KHR_physics_rigid_bodies.physicsJoints"), m = X(i.nodes, "glTF nodes"), { parentIdx: g, nodeNames: _, resolveObj: v } = Xe(t, m, i), y = [], b = {};
|
|
734
|
-
for (let e = 0; e < m.length; e++) {
|
|
735
|
-
let t = v(e);
|
|
736
|
-
if (!t) continue;
|
|
737
|
-
let r = Y(m[e].extras) ? m[e].extras : {};
|
|
738
|
-
y.push(Fe(t, _[e], r, b, n?.isSocketNode));
|
|
739
|
-
}
|
|
740
|
-
let x = [], S = [], C = /* @__PURE__ */ new Map(), w = /* @__PURE__ */ new Set(), T = [], E = [], ee = (e) => {
|
|
741
|
-
for (let t = g[e]; t !== -1; t = g[t]) if (m[t].extensions?.KHR_physics_rigid_bodies?.motion) return t;
|
|
742
|
-
return -1;
|
|
743
|
-
}, D = (e) => {
|
|
744
|
-
for (let t = e; t !== -1; t = g[t]) {
|
|
745
|
-
let e = m[t].extras;
|
|
746
|
-
if (Y(e) && (e.ecctrlWheel !== void 0 || e.ecctrlPropeller !== void 0)) return !0;
|
|
747
|
-
}
|
|
748
|
-
return !1;
|
|
749
|
-
};
|
|
750
|
-
for (let r = 0; r < m.length; r++) {
|
|
751
|
-
let i = m[r], a = _[r], o = v(r);
|
|
752
|
-
if (!o) continue;
|
|
753
|
-
let s = Y(i.extras) ? i.extras : {};
|
|
754
|
-
if (s.ecctrlWheel !== void 0 || s.ecctrlPropeller !== void 0) {
|
|
755
|
-
if (g[r] !== -1 && D(g[r])) throw new e(`Nested rotor "${a}" inside detached rotor is unsupported`);
|
|
756
|
-
s.ecctrlWheel === void 0 ? E.push(Le(o, a, s.ecctrlPropeller, t.scene)) : T.push(Ie(o, a, s.ecctrlWheel, t.scene));
|
|
757
|
-
continue;
|
|
758
|
-
}
|
|
759
|
-
if (D(r)) continue;
|
|
760
|
-
let c = i.extensions?.KHR_physics_rigid_bodies;
|
|
761
|
-
if (c === void 0) continue;
|
|
762
|
-
if (!Y(c)) throw new e(`Node "${a}" has invalid KHR_physics_rigid_bodies`);
|
|
763
|
-
if (c.motion === void 0 && c.collider === void 0 && c.joint === void 0) continue;
|
|
764
|
-
if (c.motion === void 0 && ee(r) !== -1) {
|
|
765
|
-
if (c.joint !== void 0) throw new e(`Node "${a}" has joint without motion`);
|
|
766
|
-
continue;
|
|
767
|
-
}
|
|
768
|
-
if (c.motion !== void 0 && !Y(c.motion)) throw new e(`Node "${a}" has invalid motion`);
|
|
769
|
-
let { hasExplicitInertia: l, ...u } = We(c.motion ?? {}, c.motion !== void 0, a);
|
|
770
|
-
l && w.add(r);
|
|
771
|
-
let d = n?.extractRole?.(a, s) ?? (typeof s.role == "string" ? s.role : null), f = null;
|
|
772
|
-
if (c.joint !== void 0) {
|
|
773
|
-
if (!Y(c.joint)) throw new e(`Node "${a}" has invalid joint`);
|
|
774
|
-
let t = c.joint;
|
|
775
|
-
if (typeof t.joint != "number" || !Number.isInteger(t.joint)) throw new e(`Joint on "${a}" is missing numeric joint index`);
|
|
776
|
-
if (typeof t.connectedNode != "number" || !Number.isInteger(t.connectedNode)) throw new e(`Joint on "${a}" is missing numeric connectedNode index`);
|
|
777
|
-
if (t.enableCollision !== void 0 && typeof t.enableCollision != "boolean") throw new e(`Joint on "${a}" has non-boolean enableCollision`);
|
|
778
|
-
let n = t.connectedNode, r = _[n] ?? `node_${n}`, i = v(n);
|
|
779
|
-
if (!i) throw new e(`Joint on "${a}" targets missing connectedNode "${r}"`);
|
|
780
|
-
if (D(n)) throw new e(`Joint on "${a}" cannot target detached rotor "${r}"`);
|
|
781
|
-
f = Je(p, t.joint, a, i, o, !!t.enableCollision, d, r);
|
|
782
|
-
}
|
|
783
|
-
let h = o.getWorldPosition(new I.Vector3()), y = o.getWorldQuaternion(new I.Quaternion()), b = {
|
|
784
|
-
nodeName: a,
|
|
785
|
-
...u,
|
|
786
|
-
localPosition: [
|
|
787
|
-
h.x,
|
|
788
|
-
h.y,
|
|
789
|
-
h.z
|
|
790
|
-
],
|
|
791
|
-
localQuaternion: [
|
|
792
|
-
y.x,
|
|
793
|
-
y.y,
|
|
794
|
-
y.z,
|
|
795
|
-
y.w
|
|
796
|
-
],
|
|
797
|
-
colliders: [],
|
|
798
|
-
joint: f,
|
|
799
|
-
object: o,
|
|
800
|
-
role: d
|
|
801
|
-
};
|
|
802
|
-
x.push(b), S.push({
|
|
803
|
-
nodeName: a,
|
|
804
|
-
extras: s
|
|
805
|
-
}), C.set(r, b);
|
|
902
|
+
// src/khrBundleFinalize.ts
|
|
903
|
+
function findOwnerBodyIdx(idx, parentIdx, rawNodes) {
|
|
904
|
+
for (let p = parentIdx[idx]; p !== -1; p = parentIdx[p]) if (rawNodes[p].extensions?.KHR_physics_rigid_bodies?.motion) return p;
|
|
905
|
+
return -1;
|
|
906
|
+
}
|
|
907
|
+
function isInsideRotor(idx, parentIdx, rawNodes) {
|
|
908
|
+
for (let p = idx; p !== -1; p = parentIdx[p]) {
|
|
909
|
+
const node = rawNodes[p];
|
|
910
|
+
const ex = node.extras;
|
|
911
|
+
if (isPlainObject(ex) && (ex.ecctrlWheel !== void 0 || ex.ecctrlPropeller !== void 0)) return true;
|
|
912
|
+
if (isPlainObject(node.extensions) && node.extensions.OMI_vehicle_wheel !== void 0) return true;
|
|
806
913
|
}
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
914
|
+
return false;
|
|
915
|
+
}
|
|
916
|
+
function attachBodyColliders(rawNodes, parentIdx, nodeNames, resolveObj, bodyByIdx, shapes, materials, collisionGroups) {
|
|
917
|
+
for (let i = 0; i < rawNodes.length; i++) {
|
|
918
|
+
if (isInsideRotor(i, parentIdx, rawNodes)) continue;
|
|
919
|
+
const rawCol = rawNodes[i].extensions?.KHR_physics_rigid_bodies?.collider;
|
|
920
|
+
if (rawCol === void 0) continue;
|
|
921
|
+
const obj = resolveObj(i);
|
|
922
|
+
if (!obj) continue;
|
|
923
|
+
const ownerIdx = bodyByIdx.has(i) ? i : findOwnerBodyIdx(i, parentIdx, rawNodes);
|
|
924
|
+
const owner = bodyByIdx.get(ownerIdx);
|
|
925
|
+
if (!owner) continue;
|
|
926
|
+
const shape = resolveColliderShape(rawCol, nodeNames[i], shapes, nodeNames, resolveObj);
|
|
927
|
+
const colDef = rawCol;
|
|
928
|
+
const rel = ownerIdx === i ? {
|
|
816
929
|
localPosition: [
|
|
817
930
|
0,
|
|
818
931
|
0,
|
|
@@ -824,144 +937,306 @@ function $e(t, n) {
|
|
|
824
937
|
0,
|
|
825
938
|
1
|
|
826
939
|
]
|
|
827
|
-
} :
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
940
|
+
} : computeRelativePose(owner.object, obj);
|
|
941
|
+
const mat = decodePhysicsMaterial(materials, colDef.physicsMaterial, nodeNames[i]);
|
|
942
|
+
let colGroups;
|
|
943
|
+
if (colDef.collisionFilter !== void 0) {
|
|
944
|
+
if (typeof colDef.collisionFilter !== "number" || !Number.isInteger(colDef.collisionFilter) || colDef.collisionFilter < 0 || colDef.collisionFilter >= collisionGroups.length) throw new KhrValidationError(`Node "${nodeNames[i]}" references out-of-bounds collisionFilter ${String(colDef.collisionFilter)}`);
|
|
945
|
+
colGroups = collisionGroups[colDef.collisionFilter];
|
|
831
946
|
}
|
|
832
|
-
|
|
833
|
-
shape
|
|
834
|
-
localPosition:
|
|
835
|
-
localQuaternion:
|
|
836
|
-
...
|
|
837
|
-
...
|
|
947
|
+
owner.colliders.push({
|
|
948
|
+
shape,
|
|
949
|
+
localPosition: rel.localPosition,
|
|
950
|
+
localQuaternion: rel.localQuaternion,
|
|
951
|
+
...mat,
|
|
952
|
+
...colGroups === void 0 ? {} : { collisionGroups: colGroups }
|
|
838
953
|
});
|
|
839
954
|
}
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
if (
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
955
|
+
}
|
|
956
|
+
function computeMissingBodyInertias(bodyByIdx, explicitInertiaSet) {
|
|
957
|
+
for (const [idx, body] of bodyByIdx) if (!explicitInertiaSet.has(idx) && !body.isKinematic) body.inertiaDiagonal = computeBodyInertia(body.colliders, body.mass, body.centerOfMass, body.inertiaOrientation, body.nodeName);
|
|
958
|
+
}
|
|
959
|
+
function extractSceneExtras(json, sockets) {
|
|
960
|
+
const ikSockets = [];
|
|
961
|
+
const sceneExtras = json.scenes?.[json.scene ?? 0]?.extras ?? json.extras;
|
|
962
|
+
if (sceneExtras?.ikSockets !== void 0) {
|
|
963
|
+
if (!isPlainObject(sceneExtras.ikSockets)) throw new KhrValidationError("scene.extras.ikSockets must be an object");
|
|
964
|
+
for (const [k, v] of Object.entries(sceneExtras.ikSockets)) {
|
|
965
|
+
const pos = readFiniteVec3(v, `scene.extras.ikSockets.${k}`);
|
|
966
|
+
setSocket(sockets, k, pos);
|
|
967
|
+
ikSockets.push([k, pos]);
|
|
847
968
|
}
|
|
848
969
|
}
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
ikSockets: A
|
|
854
|
-
}), {
|
|
855
|
-
scene: t.scene,
|
|
856
|
-
bodies: x,
|
|
857
|
-
wheels: T,
|
|
858
|
-
propellers: E,
|
|
859
|
-
sockets: b,
|
|
860
|
-
totalDynamicMass: O,
|
|
861
|
-
centerOfMass: k,
|
|
862
|
-
tune: M,
|
|
863
|
-
droneTune: N
|
|
970
|
+
return {
|
|
971
|
+
ikSockets,
|
|
972
|
+
tune: isPlainObject(sceneExtras?.tune) ? sceneExtras.tune : void 0,
|
|
973
|
+
droneTune: isPlainObject(sceneExtras?.droneTune) ? sceneExtras.droneTune : void 0
|
|
864
974
|
};
|
|
865
975
|
}
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
976
|
+
// src/khrPlugin.ts
|
|
977
|
+
var KHR_BUNDLE_USERDATA_KEY = "khrBundle";
|
|
978
|
+
function extractKhrBundleFromGltf(gltf, options) {
|
|
979
|
+
if (!gltf.scene) throw new KhrValidationError("GLTF asset has no active scene");
|
|
980
|
+
gltf.scene.updateMatrixWorld(true);
|
|
981
|
+
assertSceneIdentityWorldScale(gltf.scene);
|
|
982
|
+
const json = gltf.parser?.json;
|
|
983
|
+
if (!json) throw new KhrValidationError("GLTFParser is missing parsed JSON");
|
|
984
|
+
if (json.extensions !== void 0 && !isPlainObject(json.extensions)) throw new KhrValidationError("glTF extensions must be an object");
|
|
985
|
+
const khrPhys = json.extensions?.KHR_physics_rigid_bodies;
|
|
986
|
+
if (khrPhys !== void 0 && !isPlainObject(khrPhys)) throw new KhrValidationError("KHR_physics_rigid_bodies extension must be an object");
|
|
987
|
+
const khrShapes = json.extensions?.KHR_implicit_shapes;
|
|
988
|
+
if (khrShapes !== void 0 && !isPlainObject(khrShapes)) throw new KhrValidationError("KHR_implicit_shapes extension must be an object");
|
|
989
|
+
const omiExt = json.extensions?.OMI_vehicle_wheel;
|
|
990
|
+
if (omiExt !== void 0 && !isPlainObject(omiExt)) throw new KhrValidationError("OMI_vehicle_wheel extension must be an object");
|
|
991
|
+
if (omiExt !== void 0 && !Array.isArray(omiExt.wheels)) throw new KhrValidationError("OMI_vehicle_wheel.wheels must be an array");
|
|
992
|
+
const omiWheels = omiExt ? omiExt.wheels : [];
|
|
993
|
+
const shapes = reqArr(khrShapes?.shapes, "KHR_implicit_shapes.shapes").map((s, i) => decodeShape(s, i));
|
|
994
|
+
const materials = reqArr(khrPhys?.physicsMaterials, "KHR_physics_rigid_bodies.physicsMaterials");
|
|
995
|
+
const collisionGroups = decodeCollisionGroups(reqArr(khrPhys?.collisionFilters, "KHR_physics_rigid_bodies.collisionFilters"));
|
|
996
|
+
const joints = reqArr(khrPhys?.physicsJoints, "KHR_physics_rigid_bodies.physicsJoints");
|
|
997
|
+
const rawNodes = reqArr(json.nodes, "glTF nodes");
|
|
998
|
+
const { parentIdx, nodeNames, resolveObj } = buildNodeGraphLookup(gltf, rawNodes, json);
|
|
999
|
+
gltf.scene.traverse((child) => {
|
|
1000
|
+
if (child.userData && Object.getPrototypeOf(child.userData) !== Object.prototype) Object.setPrototypeOf(child.userData, Object.prototype);
|
|
1001
|
+
});
|
|
1002
|
+
const nodeMetas = [];
|
|
1003
|
+
const sockets = {};
|
|
1004
|
+
for (let i = 0; i < rawNodes.length; i++) {
|
|
1005
|
+
const obj = resolveObj(i);
|
|
1006
|
+
if (!obj) continue;
|
|
1007
|
+
if (Object.getPrototypeOf(obj.userData) !== Object.prototype) Object.setPrototypeOf(obj.userData, Object.prototype);
|
|
1008
|
+
const rawExt = rawNodes[i].extensions;
|
|
1009
|
+
if (rawExt !== void 0 && !isPlainObject(rawExt)) throw new KhrValidationError(`Node "${nodeNames[i]}" has invalid extensions`);
|
|
1010
|
+
if (isPlainObject(rawExt)) {
|
|
1011
|
+
if (Object.hasOwn(rawExt, "KOLMO_emitter") && rawExt.KOLMO_emitter !== void 0 && !Object.hasOwn(obj.userData, "KOLMO_emitter")) obj.userData.KOLMO_emitter = structuredClone(rawExt.KOLMO_emitter);
|
|
1012
|
+
if (Object.hasOwn(rawExt, "KHR3_vfx_emitter") && rawExt.KHR3_vfx_emitter !== void 0 && !Object.hasOwn(obj.userData, "KHR3_vfx_emitter")) obj.userData.KHR3_vfx_emitter = structuredClone(rawExt.KHR3_vfx_emitter);
|
|
876
1013
|
}
|
|
1014
|
+
const extras = isPlainObject(rawNodes[i].extras) ? rawNodes[i].extras : {};
|
|
1015
|
+
nodeMetas.push(collectNodeSockets(obj, nodeNames[i], extras, sockets, options?.isSocketNode));
|
|
1016
|
+
}
|
|
1017
|
+
const bodies = [];
|
|
1018
|
+
const bodyMetas = [];
|
|
1019
|
+
const bodyByIdx = /* @__PURE__ */ new Map();
|
|
1020
|
+
const explicitInertiaSet = /* @__PURE__ */ new Set();
|
|
1021
|
+
const wheels = [];
|
|
1022
|
+
const propellers = [];
|
|
1023
|
+
for (let i = 0; i < rawNodes.length; i++) {
|
|
1024
|
+
const raw = rawNodes[i];
|
|
1025
|
+
const name = nodeNames[i];
|
|
1026
|
+
const obj = resolveObj(i);
|
|
1027
|
+
if (!obj) continue;
|
|
1028
|
+
const extras = isPlainObject(raw.extras) ? raw.extras : {};
|
|
1029
|
+
const hasOmiWheel = isPlainObject(raw.extensions) && raw.extensions.OMI_vehicle_wheel !== void 0;
|
|
1030
|
+
if (extras.ecctrlWheel !== void 0 || extras.ecctrlPropeller !== void 0 || hasOmiWheel) {
|
|
1031
|
+
if (parentIdx[i] !== -1 && isInsideRotor(parentIdx[i], parentIdx, rawNodes)) throw new KhrValidationError(`Nested rotor "${name}" inside detached rotor is unsupported`);
|
|
1032
|
+
if (extras.ecctrlPropeller !== void 0 && (extras.ecctrlWheel !== void 0 || hasOmiWheel)) throw new KhrValidationError(`Node "${name}" cannot be both wheel and propeller`);
|
|
1033
|
+
if (extras.ecctrlPropeller !== void 0) propellers.push(decodePropellerTag(obj, name, extras.ecctrlPropeller, gltf.scene));
|
|
1034
|
+
else {
|
|
1035
|
+
let rawOmiWheel;
|
|
1036
|
+
if (hasOmiWheel) {
|
|
1037
|
+
const omiRef = raw.extensions.OMI_vehicle_wheel;
|
|
1038
|
+
if (!isPlainObject(omiRef) || typeof omiRef.wheel !== "number" || !Number.isInteger(omiRef.wheel) || omiRef.wheel < 0 || omiRef.wheel >= omiWheels.length) throw new KhrValidationError(`Node "${name}" references invalid OMI_vehicle_wheel.wheel ${String(isPlainObject(omiRef) ? omiRef.wheel : omiRef)}`);
|
|
1039
|
+
rawOmiWheel = omiWheels[omiRef.wheel];
|
|
1040
|
+
}
|
|
1041
|
+
wheels.push(decodeWheelTag(obj, name, extras.ecctrlWheel, gltf.scene, rawOmiWheel));
|
|
1042
|
+
}
|
|
1043
|
+
continue;
|
|
1044
|
+
}
|
|
1045
|
+
if (isInsideRotor(i, parentIdx, rawNodes)) continue;
|
|
1046
|
+
const phys = raw.extensions?.KHR_physics_rigid_bodies;
|
|
1047
|
+
if (phys === void 0) continue;
|
|
1048
|
+
if (!isPlainObject(phys)) throw new KhrValidationError(`Node "${name}" has invalid KHR_physics_rigid_bodies`);
|
|
1049
|
+
if (phys.motion === void 0 && phys.collider === void 0 && phys.joint === void 0) continue;
|
|
1050
|
+
if (phys.motion === void 0 && findOwnerBodyIdx(i, parentIdx, rawNodes) !== -1) {
|
|
1051
|
+
if (phys.joint !== void 0) throw new KhrValidationError(`Node "${name}" has joint without motion`);
|
|
1052
|
+
continue;
|
|
1053
|
+
}
|
|
1054
|
+
if (phys.motion !== void 0 && !isPlainObject(phys.motion)) throw new KhrValidationError(`Node "${name}" has invalid motion`);
|
|
1055
|
+
const { hasExplicitInertia, ...motion } = decodeMotion(phys.motion ?? {}, phys.motion !== void 0, name);
|
|
1056
|
+
if (hasExplicitInertia) explicitInertiaSet.add(i);
|
|
1057
|
+
const role = options?.extractRole?.(name, extras) ?? (typeof extras.role === "string" ? extras.role : null);
|
|
1058
|
+
let joint = null;
|
|
1059
|
+
if (phys.joint !== void 0) {
|
|
1060
|
+
if (!isPlainObject(phys.joint)) throw new KhrValidationError(`Node "${name}" has invalid joint`);
|
|
1061
|
+
const rawJ = phys.joint;
|
|
1062
|
+
if (typeof rawJ.joint !== "number" || !Number.isInteger(rawJ.joint)) throw new KhrValidationError(`Joint on "${name}" is missing numeric joint index`);
|
|
1063
|
+
if (typeof rawJ.connectedNode !== "number" || !Number.isInteger(rawJ.connectedNode)) throw new KhrValidationError(`Joint on "${name}" is missing numeric connectedNode index`);
|
|
1064
|
+
if (rawJ.enableCollision !== void 0 && typeof rawJ.enableCollision !== "boolean") throw new KhrValidationError(`Joint on "${name}" has non-boolean enableCollision`);
|
|
1065
|
+
const cIdx = rawJ.connectedNode;
|
|
1066
|
+
const cName = nodeNames[cIdx] ?? `node_${cIdx}`;
|
|
1067
|
+
const cObj = resolveObj(cIdx);
|
|
1068
|
+
if (!cObj) throw new KhrValidationError(`Joint on "${name}" targets missing connectedNode "${cName}"`);
|
|
1069
|
+
if (isInsideRotor(cIdx, parentIdx, rawNodes)) throw new KhrValidationError(`Joint on "${name}" cannot target detached rotor "${cName}"`);
|
|
1070
|
+
joint = decodeJoint(joints, rawJ.joint, name, cObj, obj, Boolean(rawJ.enableCollision), role, cName);
|
|
1071
|
+
}
|
|
1072
|
+
const wp = obj.getWorldPosition(new THREE.Vector3());
|
|
1073
|
+
const wq = obj.getWorldQuaternion(new THREE.Quaternion());
|
|
1074
|
+
const spec = {
|
|
1075
|
+
nodeName: name,
|
|
1076
|
+
...motion,
|
|
1077
|
+
localPosition: [
|
|
1078
|
+
wp.x,
|
|
1079
|
+
wp.y,
|
|
1080
|
+
wp.z
|
|
1081
|
+
],
|
|
1082
|
+
localQuaternion: [
|
|
1083
|
+
wq.x,
|
|
1084
|
+
wq.y,
|
|
1085
|
+
wq.z,
|
|
1086
|
+
wq.w
|
|
1087
|
+
],
|
|
1088
|
+
colliders: [],
|
|
1089
|
+
joint,
|
|
1090
|
+
object: obj,
|
|
1091
|
+
role
|
|
1092
|
+
};
|
|
1093
|
+
bodies.push(spec);
|
|
1094
|
+
bodyMetas.push({
|
|
1095
|
+
nodeName: name,
|
|
1096
|
+
extras
|
|
1097
|
+
});
|
|
1098
|
+
bodyByIdx.set(i, spec);
|
|
1099
|
+
}
|
|
1100
|
+
attachBodyColliders(rawNodes, parentIdx, nodeNames, resolveObj, bodyByIdx, shapes, materials, collisionGroups);
|
|
1101
|
+
computeMissingBodyInertias(bodyByIdx, explicitInertiaSet);
|
|
1102
|
+
const { totalDynamicMass, centerOfMass } = computeAssemblyMassAndCom(bodies);
|
|
1103
|
+
const { ikSockets, tune, droneTune } = extractSceneExtras(json, sockets);
|
|
1104
|
+
registerKhrTemplateMeta(gltf.scene, {
|
|
1105
|
+
nodes: nodeMetas,
|
|
1106
|
+
bodies: bodyMetas,
|
|
1107
|
+
ikSockets
|
|
877
1108
|
});
|
|
1109
|
+
return {
|
|
1110
|
+
scene: gltf.scene,
|
|
1111
|
+
bodies,
|
|
1112
|
+
wheels,
|
|
1113
|
+
propellers,
|
|
1114
|
+
sockets,
|
|
1115
|
+
totalDynamicMass,
|
|
1116
|
+
centerOfMass,
|
|
1117
|
+
tune,
|
|
1118
|
+
droneTune
|
|
1119
|
+
};
|
|
1120
|
+
}
|
|
1121
|
+
function createKhrGltfPlugin(options) {
|
|
1122
|
+
return (parser) => {
|
|
1123
|
+
return {
|
|
1124
|
+
name: "KHR_physics_rigid_bodies",
|
|
1125
|
+
...isHeadlessTextureEnv() ? {
|
|
1126
|
+
beforeRoot() {
|
|
1127
|
+
patchHeadlessTexturePlugins(parser);
|
|
1128
|
+
return null;
|
|
1129
|
+
},
|
|
1130
|
+
loadTexture: (textureIndex) => stubTextureInParser(parser, textureIndex)
|
|
1131
|
+
} : {},
|
|
1132
|
+
afterRoot(gltf) {
|
|
1133
|
+
gltf.userData[KHR_BUNDLE_USERDATA_KEY] = extractKhrBundleFromGltf(gltf, options);
|
|
1134
|
+
return null;
|
|
1135
|
+
}
|
|
1136
|
+
};
|
|
1137
|
+
};
|
|
878
1138
|
}
|
|
879
1139
|
// src/khrLoader.ts
|
|
880
|
-
var
|
|
881
|
-
function
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
new Uint8Array(
|
|
888
|
-
|
|
889
|
-
|
|
1140
|
+
var headlessPlugin$1 = createHeadlessTexturePlugin();
|
|
1141
|
+
function createDefaultGltfLoader() {
|
|
1142
|
+
const loader = new GLTFLoader();
|
|
1143
|
+
if (isHeadlessTextureEnv()) loader.register(headlessPlugin$1);
|
|
1144
|
+
return loader;
|
|
1145
|
+
}
|
|
1146
|
+
async function loadKhrGlb(source, options) {
|
|
1147
|
+
const bytes = source instanceof Uint8Array ? source : new Uint8Array(source);
|
|
1148
|
+
const buf = new ArrayBuffer(bytes.byteLength);
|
|
1149
|
+
new Uint8Array(buf).set(bytes);
|
|
1150
|
+
validateGlbHeader(buf);
|
|
1151
|
+
const loader = options?.loader ?? createDefaultGltfLoader();
|
|
1152
|
+
const plugin = createKhrGltfPlugin(options);
|
|
1153
|
+
loader.register(plugin);
|
|
890
1154
|
try {
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
1155
|
+
const toErr = (err) => err instanceof KhrValidationError ? err : new KhrValidationError(err instanceof Error ? err.message : "Failed to parse GLB");
|
|
1156
|
+
const bundle = (await new Promise((resolve, reject) => {
|
|
1157
|
+
loader.parse(buf, "", resolve, (err) => reject(toErr(err)));
|
|
1158
|
+
}).catch((err) => {
|
|
1159
|
+
throw toErr(err);
|
|
1160
|
+
})).userData[KHR_BUNDLE_USERDATA_KEY];
|
|
1161
|
+
if (!bundle) throw new KhrValidationError("Missing parsed KHR bundle on GLTF userData");
|
|
1162
|
+
return bundle;
|
|
898
1163
|
} finally {
|
|
899
|
-
|
|
1164
|
+
loader.unregister(plugin);
|
|
900
1165
|
}
|
|
901
1166
|
}
|
|
902
|
-
// src/
|
|
903
|
-
var
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
1167
|
+
// src/khrExportEncoders.ts
|
|
1168
|
+
var isPosNum = (v) => typeof v === "number" && Number.isFinite(v) && v > 0;
|
|
1169
|
+
var isObj = (v) => typeof v === "object" && !!v && !Array.isArray(v);
|
|
1170
|
+
var COMBINE_MODES = /* @__PURE__ */ new Set([
|
|
1171
|
+
"average",
|
|
1172
|
+
"minimum",
|
|
1173
|
+
"maximum",
|
|
1174
|
+
"multiply"
|
|
1175
|
+
]);
|
|
1176
|
+
function optFinite(v, label, nonNeg = false) {
|
|
1177
|
+
if (v !== void 0) {
|
|
1178
|
+
if (typeof v !== "number" || !Number.isFinite(v) || nonNeg && v < 0) throw new KhrValidationError(`exportKhrGlb: ${label} must be finite${nonNeg ? " >= 0" : ""}`);
|
|
1179
|
+
return v;
|
|
908
1180
|
}
|
|
909
1181
|
}
|
|
910
|
-
function
|
|
911
|
-
if (
|
|
912
|
-
if (!Array.isArray(
|
|
1182
|
+
function encodeShape(shape, nodeName) {
|
|
1183
|
+
if (shape.type === "box") {
|
|
1184
|
+
if (!Array.isArray(shape.size) || shape.size.length !== 3 || !shape.size.every(isPosNum)) throw new KhrValidationError(`exportKhrGlb: invalid box.size on "${nodeName}"`);
|
|
913
1185
|
return {
|
|
914
1186
|
type: "box",
|
|
915
|
-
box: { size:
|
|
1187
|
+
box: { size: shape.size }
|
|
916
1188
|
};
|
|
917
1189
|
}
|
|
918
|
-
if (
|
|
919
|
-
if (!
|
|
1190
|
+
if (shape.type === "sphere") {
|
|
1191
|
+
if (!isPosNum(shape.radius)) throw new KhrValidationError(`exportKhrGlb: invalid sphere.radius on "${nodeName}"`);
|
|
920
1192
|
return {
|
|
921
1193
|
type: "sphere",
|
|
922
|
-
sphere: { radius:
|
|
1194
|
+
sphere: { radius: shape.radius }
|
|
923
1195
|
};
|
|
924
1196
|
}
|
|
925
|
-
if (!
|
|
926
|
-
if (Math.abs(
|
|
1197
|
+
if (!isPosNum(shape.height) || !isPosNum(shape.radiusTop) || !isPosNum(shape.radiusBottom)) throw new KhrValidationError(`exportKhrGlb: invalid ${shape.type} dimensions on "${nodeName}"`);
|
|
1198
|
+
if (Math.abs(shape.radiusTop - shape.radiusBottom) > 1e-6) throw new KhrValidationError(`exportKhrGlb: tapered ${shape.type} (${shape.radiusTop} !== ${shape.radiusBottom}) on "${nodeName}" is unsupported`);
|
|
927
1199
|
return {
|
|
928
|
-
type:
|
|
929
|
-
[
|
|
930
|
-
height:
|
|
931
|
-
radiusTop:
|
|
932
|
-
radiusBottom:
|
|
1200
|
+
type: shape.type,
|
|
1201
|
+
[shape.type]: {
|
|
1202
|
+
height: shape.height,
|
|
1203
|
+
radiusTop: shape.radiusTop,
|
|
1204
|
+
radiusBottom: shape.radiusBottom
|
|
933
1205
|
}
|
|
934
1206
|
};
|
|
935
1207
|
}
|
|
936
|
-
function
|
|
937
|
-
if (!Array.isArray(
|
|
938
|
-
|
|
939
|
-
if (Math.hypot(
|
|
940
|
-
return
|
|
941
|
-
}
|
|
942
|
-
function
|
|
943
|
-
if (!
|
|
944
|
-
if (
|
|
945
|
-
if (
|
|
946
|
-
if (
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
1208
|
+
function axisToIndex(axis, kind, nodeName) {
|
|
1209
|
+
if (!Array.isArray(axis) || axis.length !== 3 || !axis.every((n) => typeof n === "number" && Number.isFinite(n))) throw new KhrValidationError(`exportKhrGlb: ${kind} joint on "${nodeName}" requires a finite axis`);
|
|
1210
|
+
const ax = Math.abs(axis[0]), ay = Math.abs(axis[1]), az = Math.abs(axis[2]);
|
|
1211
|
+
if (Math.hypot(ax, ay, az) <= 1e-8) throw new KhrValidationError(`exportKhrGlb: ${kind} joint on "${nodeName}" requires non-zero axis`);
|
|
1212
|
+
return ax >= ay && ax >= az ? 0 : ay >= az ? 1 : 2;
|
|
1213
|
+
}
|
|
1214
|
+
function encodeJointDrive(drive, defaultType, defaultAxis, nodeName) {
|
|
1215
|
+
if (!isObj(drive)) throw new KhrValidationError(`exportKhrGlb: joint drive on "${nodeName}" must be an object`);
|
|
1216
|
+
if (drive.mode !== "force" && drive.mode !== "acceleration") throw new KhrValidationError(`exportKhrGlb: joint drive on "${nodeName}" requires drive.mode ("force" | "acceleration")`);
|
|
1217
|
+
if (drive.type !== void 0 && drive.type !== "linear" && drive.type !== "angular") throw new KhrValidationError(`exportKhrGlb: joint drive on "${nodeName}" has invalid drive.type`);
|
|
1218
|
+
if (drive.axis !== void 0 && drive.axis !== 0 && drive.axis !== 1 && drive.axis !== 2) throw new KhrValidationError(`exportKhrGlb: joint drive on "${nodeName}" has invalid drive.axis`);
|
|
1219
|
+
const positionTarget = optFinite(drive.positionTarget, `joint drive.positionTarget on "${nodeName}"`);
|
|
1220
|
+
const velocityTarget = optFinite(drive.velocityTarget, `joint drive.velocityTarget on "${nodeName}"`);
|
|
1221
|
+
if (positionTarget === void 0 && velocityTarget === void 0) throw new KhrValidationError(`exportKhrGlb: joint drive on "${nodeName}" requires finite positionTarget or velocityTarget`);
|
|
1222
|
+
const stiffness = optFinite(drive.stiffness, `joint drive.stiffness on "${nodeName}"`, true);
|
|
1223
|
+
const damping = optFinite(drive.damping, `joint drive.damping on "${nodeName}"`, true);
|
|
1224
|
+
const maxForce = optFinite(drive.maxForce, `joint drive.maxForce on "${nodeName}"`, true);
|
|
950
1225
|
return {
|
|
951
|
-
type:
|
|
952
|
-
mode:
|
|
953
|
-
axis:
|
|
954
|
-
...
|
|
955
|
-
...
|
|
956
|
-
...
|
|
957
|
-
...
|
|
958
|
-
...
|
|
1226
|
+
type: drive.type ?? defaultType,
|
|
1227
|
+
mode: drive.mode,
|
|
1228
|
+
axis: drive.axis ?? defaultAxis,
|
|
1229
|
+
...positionTarget === void 0 ? {} : { positionTarget },
|
|
1230
|
+
...velocityTarget === void 0 ? {} : { velocityTarget },
|
|
1231
|
+
...stiffness === void 0 ? {} : { stiffness },
|
|
1232
|
+
...damping === void 0 ? {} : { damping },
|
|
1233
|
+
...maxForce === void 0 ? {} : { maxForce }
|
|
959
1234
|
};
|
|
960
1235
|
}
|
|
961
|
-
function
|
|
962
|
-
if (
|
|
963
|
-
if (
|
|
964
|
-
if (
|
|
1236
|
+
function encodeJoint(j, nodeName) {
|
|
1237
|
+
if (j.kind !== "fixed" && j.kind !== "revolute" && j.kind !== "prismatic") throw new KhrValidationError(`exportKhrGlb: invalid joint.kind "${String(j.kind)}" on "${nodeName}"`);
|
|
1238
|
+
if (j.kind === "fixed") {
|
|
1239
|
+
if (j.drive || j.drives && j.drives.length > 0) throw new KhrValidationError(`exportKhrGlb: fixed joint on "${nodeName}" cannot define drives`);
|
|
965
1240
|
return { limits: [{
|
|
966
1241
|
linearAxes: [
|
|
967
1242
|
0,
|
|
@@ -980,21 +1255,26 @@ function st(t, n) {
|
|
|
980
1255
|
max: 0
|
|
981
1256
|
}] };
|
|
982
1257
|
}
|
|
983
|
-
|
|
1258
|
+
const ax = axisToIndex(j.axis, j.kind, nodeName);
|
|
1259
|
+
const lockedAxes = [
|
|
984
1260
|
0,
|
|
985
1261
|
1,
|
|
986
1262
|
2
|
|
987
|
-
].filter((
|
|
988
|
-
if (
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
1263
|
+
].filter((i) => i !== ax);
|
|
1264
|
+
if (j.limits && (!Number.isFinite(j.limits[0]) || !Number.isFinite(j.limits[1]) || j.limits[0] > j.limits[1])) throw new KhrValidationError(`exportKhrGlb: joint on "${nodeName}" has invalid joint.limits`);
|
|
1265
|
+
const stiffness = optFinite(j.limitStiffness, `joint.limitStiffness on "${nodeName}"`, true);
|
|
1266
|
+
const damping = optFinite(j.limitDamping, `joint.limitDamping on "${nodeName}"`, true);
|
|
1267
|
+
const freeKey = j.kind === "revolute" ? "angularAxes" : "linearAxes";
|
|
1268
|
+
const freeLimit = j.limits || stiffness !== void 0 || damping !== void 0 ? [{
|
|
1269
|
+
[freeKey]: [ax],
|
|
1270
|
+
...j.limits ? {
|
|
1271
|
+
min: j.limits[0],
|
|
1272
|
+
max: j.limits[1]
|
|
994
1273
|
} : {},
|
|
995
|
-
...
|
|
996
|
-
...
|
|
997
|
-
}] : []
|
|
1274
|
+
...stiffness === void 0 ? {} : { stiffness },
|
|
1275
|
+
...damping === void 0 ? {} : { damping }
|
|
1276
|
+
}] : [];
|
|
1277
|
+
const out = { limits: j.kind === "revolute" ? [
|
|
998
1278
|
{
|
|
999
1279
|
linearAxes: [
|
|
1000
1280
|
0,
|
|
@@ -1005,11 +1285,11 @@ function st(t, n) {
|
|
|
1005
1285
|
max: 0
|
|
1006
1286
|
},
|
|
1007
1287
|
{
|
|
1008
|
-
angularAxes:
|
|
1288
|
+
angularAxes: lockedAxes,
|
|
1009
1289
|
min: 0,
|
|
1010
1290
|
max: 0
|
|
1011
1291
|
},
|
|
1012
|
-
...
|
|
1292
|
+
...freeLimit
|
|
1013
1293
|
] : [
|
|
1014
1294
|
{
|
|
1015
1295
|
angularAxes: [
|
|
@@ -1021,517 +1301,636 @@ function st(t, n) {
|
|
|
1021
1301
|
max: 0
|
|
1022
1302
|
},
|
|
1023
1303
|
{
|
|
1024
|
-
linearAxes:
|
|
1304
|
+
linearAxes: lockedAxes,
|
|
1025
1305
|
min: 0,
|
|
1026
1306
|
max: 0
|
|
1027
1307
|
},
|
|
1028
|
-
...
|
|
1029
|
-
] }
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
if (
|
|
1308
|
+
...freeLimit
|
|
1309
|
+
] };
|
|
1310
|
+
const drives = j.drives ?? (j.drive ? [j.drive] : []);
|
|
1311
|
+
if (drives.length > 0) out.drives = drives.map((d) => encodeJointDrive(d, j.kind === "revolute" ? "angular" : "linear", ax, nodeName));
|
|
1312
|
+
return out;
|
|
1313
|
+
}
|
|
1314
|
+
function encodeMotion(m, nodeName) {
|
|
1315
|
+
const isKinematic = Boolean(m.isKinematic);
|
|
1316
|
+
if (!isKinematic && !isPosNum(m.mass)) throw new KhrValidationError(`exportKhrGlb: dynamic node "${nodeName}" requires positive finite motion.mass`);
|
|
1317
|
+
if (isKinematic && m.mass !== void 0) optFinite(m.mass, `kinematic motion.mass on "${nodeName}"`, true);
|
|
1318
|
+
if (m.inertiaDiagonal && readFiniteVec3(m.inertiaDiagonal, `${nodeName}.motion.inertiaDiagonal`).some((n) => isKinematic ? n < 0 : n <= 0)) throw new KhrValidationError(`exportKhrGlb: invalid motion.inertiaDiagonal on "${nodeName}"`);
|
|
1319
|
+
if (m.gravityFactor !== void 0 && (typeof m.gravityFactor !== "number" || !Number.isFinite(m.gravityFactor))) throw new KhrValidationError(`exportKhrGlb: invalid motion.gravityFactor on "${nodeName}"`);
|
|
1037
1320
|
return {
|
|
1038
|
-
isKinematic
|
|
1039
|
-
...
|
|
1040
|
-
...
|
|
1041
|
-
...
|
|
1042
|
-
...
|
|
1043
|
-
...
|
|
1044
|
-
...
|
|
1045
|
-
...
|
|
1321
|
+
isKinematic,
|
|
1322
|
+
...m.mass === void 0 ? {} : { mass: m.mass },
|
|
1323
|
+
...m.centerOfMass ? { centerOfMass: readFiniteVec3(m.centerOfMass, `${nodeName}.motion.centerOfMass`) } : {},
|
|
1324
|
+
...m.inertiaDiagonal ? { inertiaDiagonal: m.inertiaDiagonal } : {},
|
|
1325
|
+
...m.inertiaOrientation ? { inertiaOrientation: readUnitQuat(m.inertiaOrientation, `${nodeName}.motion.inertiaOrientation`) } : {},
|
|
1326
|
+
...m.linearVelocity ? { linearVelocity: readFiniteVec3(m.linearVelocity, `${nodeName}.motion.linearVelocity`) } : {},
|
|
1327
|
+
...m.angularVelocity ? { angularVelocity: readFiniteVec3(m.angularVelocity, `${nodeName}.motion.angularVelocity`) } : {},
|
|
1328
|
+
...m.gravityFactor === void 0 ? {} : { gravityFactor: m.gravityFactor }
|
|
1046
1329
|
};
|
|
1047
1330
|
}
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1331
|
+
function encodePhysicsMaterial(col, nodeName) {
|
|
1332
|
+
const friction = optFinite(col.friction, `collider.friction on "${nodeName}"`, true);
|
|
1333
|
+
const restitution = optFinite(col.restitution, `collider.restitution on "${nodeName}"`, true);
|
|
1334
|
+
for (const [k, v] of [["frictionCombine", col.frictionCombine], ["restitutionCombine", col.restitutionCombine]]) if (v !== void 0 && (typeof v !== "string" || !COMBINE_MODES.has(v))) throw new KhrValidationError(`exportKhrGlb: invalid collider.${k} "${String(v)}" on "${nodeName}"`);
|
|
1335
|
+
if (friction === void 0 && restitution === void 0 && col.frictionCombine === void 0 && col.restitutionCombine === void 0) return null;
|
|
1336
|
+
return {
|
|
1337
|
+
...friction === void 0 ? {} : {
|
|
1338
|
+
staticFriction: friction,
|
|
1339
|
+
dynamicFriction: friction
|
|
1340
|
+
},
|
|
1341
|
+
...restitution === void 0 ? {} : { restitution },
|
|
1342
|
+
...col.frictionCombine === void 0 ? {} : { frictionCombine: col.frictionCombine },
|
|
1343
|
+
...col.restitutionCombine === void 0 ? {} : { restitutionCombine: col.restitutionCombine }
|
|
1344
|
+
};
|
|
1345
|
+
}
|
|
1346
|
+
function validateSystemList(arr, label, nodeName) {
|
|
1347
|
+
if (!Array.isArray(arr) || arr.some((s) => typeof s !== "string" || s.length === 0)) throw new KhrValidationError(`exportKhrGlb: collider.${label} on "${nodeName}" must be an array of non-empty strings`);
|
|
1348
|
+
return arr;
|
|
1349
|
+
}
|
|
1350
|
+
function encodeCollisionFilter(col, nodeName) {
|
|
1351
|
+
if (col.collideWithSystems !== void 0 && col.notCollideWithSystems !== void 0) throw new KhrValidationError(`exportKhrGlb: collider on "${nodeName}" cannot specify both collideWithSystems and notCollideWithSystems`);
|
|
1352
|
+
if (col.collisionSystems === void 0 && col.collideWithSystems === void 0 && col.notCollideWithSystems === void 0) return null;
|
|
1353
|
+
return {
|
|
1354
|
+
...col.collisionSystems === void 0 ? {} : { collisionSystems: validateSystemList(col.collisionSystems, "collisionSystems", nodeName) },
|
|
1355
|
+
...col.collideWithSystems === void 0 ? {} : { collideWithSystems: validateSystemList(col.collideWithSystems, "collideWithSystems", nodeName) },
|
|
1356
|
+
...col.notCollideWithSystems === void 0 ? {} : { notCollideWithSystems: validateSystemList(col.notCollideWithSystems, "notCollideWithSystems", nodeName) }
|
|
1357
|
+
};
|
|
1358
|
+
}
|
|
1359
|
+
function encodeOmiWheel(wheel, nodeName) {
|
|
1360
|
+
if (!isObj(wheel)) throw new KhrValidationError(`exportKhrGlb: wheel on "${nodeName}" must be an object`);
|
|
1361
|
+
if (!isPosNum(wheel.radius)) throw new KhrValidationError(`exportKhrGlb: wheel.radius on "${nodeName}" must be > 0`);
|
|
1362
|
+
if (!isPosNum(wheel.width)) throw new KhrValidationError(`exportKhrGlb: wheel.width on "${nodeName}" must be > 0`);
|
|
1363
|
+
for (const [k, v] of [["steering", wheel.steering], ["propulsion", wheel.propulsion]]) if (v !== void 0 && typeof v !== "boolean") throw new KhrValidationError(`exportKhrGlb: wheel.${k} on "${nodeName}" must be boolean`);
|
|
1364
|
+
const maxForce = optFinite(wheel.maxForce, `wheel.maxForce on "${nodeName}"`, true);
|
|
1365
|
+
const maxSteeringAngle = optFinite(wheel.maxSteeringAngle, `wheel.maxSteeringAngle on "${nodeName}"`, true);
|
|
1366
|
+
const suspensionStiffness = optFinite(wheel.suspensionStiffness, `wheel.suspensionStiffness on "${nodeName}"`, true);
|
|
1367
|
+
const suspensionDampingCompression = optFinite(wheel.suspensionDampingCompression, `wheel.suspensionDampingCompression on "${nodeName}"`, true);
|
|
1368
|
+
const suspensionDampingRebound = optFinite(wheel.suspensionDampingRebound, `wheel.suspensionDampingRebound on "${nodeName}"`, true);
|
|
1369
|
+
const suspensionTravel = optFinite(wheel.suspensionTravel, `wheel.suspensionTravel on "${nodeName}"`, true);
|
|
1370
|
+
return {
|
|
1371
|
+
radius: wheel.radius,
|
|
1372
|
+
width: wheel.width,
|
|
1373
|
+
...maxForce === void 0 ? {} : { maxForce },
|
|
1374
|
+
...wheel.steering === void 0 ? {} : { steering: wheel.steering },
|
|
1375
|
+
...wheel.propulsion === void 0 ? {} : { propulsion: wheel.propulsion },
|
|
1376
|
+
...maxSteeringAngle === void 0 ? {} : { maxSteeringAngle },
|
|
1377
|
+
...suspensionStiffness === void 0 ? {} : { suspensionStiffness },
|
|
1378
|
+
...suspensionDampingCompression === void 0 ? {} : { suspensionDampingCompression },
|
|
1379
|
+
...suspensionDampingRebound === void 0 ? {} : { suspensionDampingRebound },
|
|
1380
|
+
...suspensionTravel === void 0 ? {} : { suspensionTravel }
|
|
1381
|
+
};
|
|
1382
|
+
}
|
|
1383
|
+
// src/khrExporter.ts
|
|
1384
|
+
var isPos = (v) => typeof v === "number" && Number.isFinite(v) && v > 0;
|
|
1385
|
+
var isNonNeg = (v) => typeof v === "number" && Number.isFinite(v) && v >= 0;
|
|
1386
|
+
function synthesizeOmiWheel(raw) {
|
|
1387
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
|
|
1388
|
+
const ec = raw;
|
|
1389
|
+
if (!isPos(ec.radius) || !isPos(ec.width) || !isNonNeg(ec.travel) || !isNonNeg(ec.sag) || ec.sag > ec.travel) return null;
|
|
1390
|
+
if (typeof ec.steer !== "boolean" || typeof ec.drive !== "boolean" || typeof ec.brake !== "boolean") return null;
|
|
1391
|
+
const springK = isNonNeg(ec.springK) ? ec.springK : void 0;
|
|
1392
|
+
const dampingC = isNonNeg(ec.dampingC) ? ec.dampingC : void 0;
|
|
1393
|
+
const maxForce = isNonNeg(ec.maxBrakeTorque) ? ec.maxBrakeTorque / ec.radius : void 0;
|
|
1394
|
+
const maxSteeringAngle = isNonNeg(ec.maxSteeringAngle) ? ec.maxSteeringAngle : void 0;
|
|
1395
|
+
return {
|
|
1396
|
+
radius: ec.radius,
|
|
1397
|
+
width: ec.width,
|
|
1398
|
+
steering: ec.steer,
|
|
1399
|
+
propulsion: ec.drive,
|
|
1400
|
+
suspensionTravel: ec.travel,
|
|
1401
|
+
...maxForce === void 0 ? {} : { maxForce },
|
|
1402
|
+
...maxSteeringAngle === void 0 ? {} : { maxSteeringAngle },
|
|
1403
|
+
...springK === void 0 ? {} : { suspensionStiffness: springK },
|
|
1404
|
+
...dampingC === void 0 ? {} : {
|
|
1405
|
+
suspensionDampingCompression: dampingC,
|
|
1406
|
+
suspensionDampingRebound: dampingC
|
|
1407
|
+
}
|
|
1408
|
+
};
|
|
1409
|
+
}
|
|
1410
|
+
function stripUnloadedTextures(root) {
|
|
1411
|
+
const stripped = [];
|
|
1412
|
+
root.traverse((obj) => {
|
|
1413
|
+
const rawMat = obj.material;
|
|
1414
|
+
for (const m of Array.isArray(rawMat) ? rawMat : rawMat ? [rawMat] : []) {
|
|
1415
|
+
if (!m || typeof m !== "object") continue;
|
|
1416
|
+
const rec = m;
|
|
1417
|
+
for (const slot of Object.keys(rec)) {
|
|
1418
|
+
const val = rec[slot];
|
|
1419
|
+
if (val && typeof val === "object" && val.isTexture && !val.image) {
|
|
1420
|
+
stripped.push({
|
|
1421
|
+
rec,
|
|
1422
|
+
slot,
|
|
1423
|
+
tex: val
|
|
1424
|
+
});
|
|
1425
|
+
rec[slot] = null;
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
});
|
|
1430
|
+
return stripped;
|
|
1431
|
+
}
|
|
1432
|
+
async function exportKhrGlb(root, specs, sceneExtras) {
|
|
1433
|
+
ensureHeadlessFileReader();
|
|
1434
|
+
root.updateMatrixWorld(true);
|
|
1435
|
+
const exporter = new GLTFExporter();
|
|
1436
|
+
const stripped = stripUnloadedTextures(root);
|
|
1437
|
+
let rawBuf;
|
|
1438
|
+
try {
|
|
1439
|
+
const res = await exporter.parseAsync(root, { binary: true });
|
|
1440
|
+
rawBuf = res instanceof ArrayBuffer ? res : packJsonToGlb(res);
|
|
1441
|
+
} finally {
|
|
1442
|
+
for (const item of stripped) item.rec[item.slot] = item.tex;
|
|
1443
|
+
}
|
|
1444
|
+
return patchGlbJsonChunk(rawBuf, (gltf) => {
|
|
1445
|
+
const nodes = gltf.nodes ?? [];
|
|
1446
|
+
const nameToIndices = /* @__PURE__ */ new Map();
|
|
1447
|
+
nodes.forEach((n, idx) => {
|
|
1448
|
+
if (!n.name) return;
|
|
1449
|
+
const list = nameToIndices.get(n.name);
|
|
1450
|
+
if (list) list.push(idx);
|
|
1451
|
+
else nameToIndices.set(n.name, [idx]);
|
|
1057
1452
|
});
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
if (
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1453
|
+
const requireNodeIdx = (name) => {
|
|
1454
|
+
const list = nameToIndices.get(name);
|
|
1455
|
+
if (!list || list.length === 0) throw new KhrValidationError(`exportKhrGlb: missing scene node "${name}"`);
|
|
1456
|
+
if (list.length > 1) throw new KhrValidationError(`exportKhrGlb: ambiguous duplicate scene node "${name}" (${list.length} matches)`);
|
|
1457
|
+
return list[0];
|
|
1458
|
+
};
|
|
1459
|
+
const shapes = [];
|
|
1460
|
+
const shapeIndexByJson = /* @__PURE__ */ new Map();
|
|
1461
|
+
const materials = [];
|
|
1462
|
+
const filters = [];
|
|
1463
|
+
const joints = [];
|
|
1464
|
+
const omiWheels = [];
|
|
1465
|
+
const seenSpecNodes = /* @__PURE__ */ new Set();
|
|
1466
|
+
const used = new Set(gltf.extensionsUsed ?? []);
|
|
1467
|
+
let hasPhysics = false;
|
|
1468
|
+
const hasPreIndexedOmiWheel = specs.some((s) => s.extensions && typeof s.extensions === "object" && !Array.isArray(s.extensions) && Object.hasOwn(s.extensions, "OMI_vehicle_wheel") && s.extensions.OMI_vehicle_wheel !== void 0);
|
|
1469
|
+
for (const spec of specs) {
|
|
1470
|
+
if (seenSpecNodes.has(spec.nodeName)) throw new KhrValidationError(`exportKhrGlb: duplicate spec for node "${spec.nodeName}"`);
|
|
1471
|
+
seenSpecNodes.add(spec.nodeName);
|
|
1472
|
+
const node = nodes[requireNodeIdx(spec.nodeName)];
|
|
1473
|
+
if (spec.extras) node.extras = {
|
|
1474
|
+
...node.extras,
|
|
1475
|
+
...spec.extras
|
|
1476
|
+
};
|
|
1477
|
+
if (spec.extensions !== void 0) {
|
|
1478
|
+
if (!spec.extensions || typeof spec.extensions !== "object" || Array.isArray(spec.extensions)) throw new KhrValidationError(`exportKhrGlb: extensions on "${spec.nodeName}" must be an object`);
|
|
1479
|
+
if (Object.hasOwn(spec.extensions, "OMI_vehicle_wheel") && spec.extensions.OMI_vehicle_wheel !== void 0 && spec.wheel !== void 0) throw new KhrValidationError(`exportKhrGlb: node "${spec.nodeName}" cannot specify both wheel and extensions.OMI_vehicle_wheel`);
|
|
1480
|
+
if (Object.hasOwn(spec.extensions, "KHR_physics_rigid_bodies") && spec.extensions.KHR_physics_rigid_bodies !== void 0 && (spec.motion !== void 0 || spec.collider !== void 0 || spec.joint !== void 0)) throw new KhrValidationError(`exportKhrGlb: node "${spec.nodeName}" cannot specify both extensions.KHR_physics_rigid_bodies and motion/collider/joint`);
|
|
1481
|
+
node.extensions = {
|
|
1482
|
+
...node.extensions,
|
|
1483
|
+
...spec.extensions
|
|
1484
|
+
};
|
|
1485
|
+
for (const extName of Object.keys(spec.extensions)) used.add(extName);
|
|
1486
|
+
}
|
|
1487
|
+
const khr = {};
|
|
1488
|
+
if (spec.motion) {
|
|
1489
|
+
hasPhysics = true;
|
|
1490
|
+
khr.motion = encodeMotion(spec.motion, spec.nodeName);
|
|
1491
|
+
}
|
|
1492
|
+
if (spec.collider) {
|
|
1493
|
+
hasPhysics = true;
|
|
1494
|
+
const col = {};
|
|
1495
|
+
if (spec.collider.shape) {
|
|
1496
|
+
const encoded = encodeShape(spec.collider.shape, spec.nodeName);
|
|
1497
|
+
const key = JSON.stringify(encoded);
|
|
1498
|
+
let sIdx = shapeIndexByJson.get(key);
|
|
1499
|
+
if (sIdx === void 0) {
|
|
1500
|
+
sIdx = shapes.length;
|
|
1501
|
+
shapes.push(encoded);
|
|
1502
|
+
shapeIndexByJson.set(key, sIdx);
|
|
1503
|
+
}
|
|
1504
|
+
col.geometry = { shape: sIdx };
|
|
1505
|
+
} else if (spec.collider.meshNodeName) {
|
|
1506
|
+
if (typeof spec.collider.convexHull !== "boolean") throw new KhrValidationError(`exportKhrGlb: mesh collider on "${spec.nodeName}" requires explicit boolean convexHull`);
|
|
1507
|
+
col.geometry = {
|
|
1508
|
+
node: requireNodeIdx(spec.collider.meshNodeName),
|
|
1509
|
+
convexHull: spec.collider.convexHull
|
|
1081
1510
|
};
|
|
1082
|
-
} else throw new
|
|
1083
|
-
|
|
1084
|
-
if (
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
dynamicFriction: i
|
|
1088
|
-
},
|
|
1089
|
-
...a === void 0 ? {} : { restitution: a }
|
|
1090
|
-
})), t.collider.collideWithSystems !== void 0 && t.collider.notCollideWithSystems !== void 0) throw new e(`exportKhrGlb: collider on "${t.nodeName}" cannot specify both collideWithSystems and notCollideWithSystems`);
|
|
1091
|
-
if (t.collider.collisionSystems !== void 0 || t.collider.collideWithSystems !== void 0 || t.collider.notCollideWithSystems !== void 0) {
|
|
1092
|
-
if (!Array.isArray(t.collider.collisionSystems) || t.collider.collisionSystems.length === 0) throw new e(`exportKhrGlb: collision filter on "${t.nodeName}" requires non-empty collisionSystems`);
|
|
1093
|
-
n.collisionFilter = u.length, u.push({
|
|
1094
|
-
collisionSystems: t.collider.collisionSystems,
|
|
1095
|
-
...t.collider.collideWithSystems ? { collideWithSystems: t.collider.collideWithSystems } : {},
|
|
1096
|
-
...t.collider.notCollideWithSystems ? { notCollideWithSystems: t.collider.notCollideWithSystems } : {}
|
|
1097
|
-
});
|
|
1511
|
+
} else throw new KhrValidationError(`exportKhrGlb: collider on "${spec.nodeName}" requires shape or meshNodeName`);
|
|
1512
|
+
const mat = encodePhysicsMaterial(spec.collider, spec.nodeName);
|
|
1513
|
+
if (mat) {
|
|
1514
|
+
col.physicsMaterial = materials.length;
|
|
1515
|
+
materials.push(mat);
|
|
1098
1516
|
}
|
|
1099
|
-
|
|
1517
|
+
const filter = encodeCollisionFilter(spec.collider, spec.nodeName);
|
|
1518
|
+
if (filter) {
|
|
1519
|
+
col.collisionFilter = filters.length;
|
|
1520
|
+
filters.push(filter);
|
|
1521
|
+
}
|
|
1522
|
+
khr.collider = col;
|
|
1100
1523
|
}
|
|
1101
|
-
if (
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1524
|
+
if (spec.joint) {
|
|
1525
|
+
hasPhysics = true;
|
|
1526
|
+
const connIdx = requireNodeIdx(spec.joint.connectedNodeName);
|
|
1527
|
+
joints.push(encodeJoint(spec.joint, spec.nodeName));
|
|
1528
|
+
khr.joint = {
|
|
1529
|
+
joint: joints.length - 1,
|
|
1530
|
+
connectedNode: connIdx,
|
|
1531
|
+
enableCollision: Boolean(spec.joint.enableCollision)
|
|
1108
1532
|
};
|
|
1109
1533
|
}
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1534
|
+
const wheelSpec = spec.wheel ?? (!hasPreIndexedOmiWheel ? synthesizeOmiWheel(spec.extras?.ecctrlWheel) : null);
|
|
1535
|
+
if (wheelSpec) {
|
|
1536
|
+
const wIdx = omiWheels.length;
|
|
1537
|
+
omiWheels.push(encodeOmiWheel(wheelSpec, spec.nodeName));
|
|
1538
|
+
used.add("OMI_vehicle_wheel");
|
|
1539
|
+
node.extensions = {
|
|
1540
|
+
...node.extensions,
|
|
1541
|
+
OMI_vehicle_wheel: { wheel: wIdx }
|
|
1542
|
+
};
|
|
1543
|
+
}
|
|
1544
|
+
if (Object.keys(khr).length > 0) node.extensions = {
|
|
1545
|
+
...node.extensions,
|
|
1546
|
+
KHR_physics_rigid_bodies: khr
|
|
1547
|
+
};
|
|
1114
1548
|
}
|
|
1115
|
-
if (
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
...
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1549
|
+
if (hasPreIndexedOmiWheel && omiWheels.length > 0) throw new KhrValidationError("exportKhrGlb: cannot mix spec.wheel with spec.extensions.OMI_vehicle_wheel in the same call");
|
|
1550
|
+
if (sceneExtras) {
|
|
1551
|
+
const scene = gltf.scenes?.[gltf.scene ?? 0];
|
|
1552
|
+
if (scene) scene.extras = {
|
|
1553
|
+
...scene.extras,
|
|
1554
|
+
...sceneExtras
|
|
1555
|
+
};
|
|
1556
|
+
else gltf.extras = {
|
|
1557
|
+
...gltf.extras,
|
|
1558
|
+
...sceneExtras
|
|
1123
1559
|
};
|
|
1124
1560
|
}
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
...d.length > 0 ? { physicsJoints: d } : {}
|
|
1130
|
-
}), p.size > 0 && (t.extensionsUsed = [...p]);
|
|
1131
|
-
});
|
|
1132
|
-
}
|
|
1133
|
-
// src/assemblyPlan.ts
|
|
1134
|
-
function ut(t, n) {
|
|
1135
|
-
let r = new I.Matrix4();
|
|
1136
|
-
for (let i = t; i && i !== n; i = i.parent) {
|
|
1137
|
-
if (!i.parent) throw new e(`Node "${t.name}" is detached from bundle.scene`);
|
|
1138
|
-
i.updateMatrix(), r.premultiply(i.matrix);
|
|
1139
|
-
}
|
|
1140
|
-
return r;
|
|
1141
|
-
}
|
|
1142
|
-
function dt(e, t) {
|
|
1143
|
-
let n = (e) => {
|
|
1144
|
-
let n = [];
|
|
1145
|
-
for (let r = e; r && (n.push(r), r !== t); r = r.parent);
|
|
1146
|
-
return n;
|
|
1147
|
-
}, r = new Set(n(e[0]));
|
|
1148
|
-
for (let t = 1; t < e.length; t++) {
|
|
1149
|
-
let i = new Set(n(e[t]));
|
|
1150
|
-
r = new Set([...r].filter((e) => i.has(e)));
|
|
1151
|
-
}
|
|
1152
|
-
for (let t of n(e[0])) if (r.has(t)) return t;
|
|
1153
|
-
return t;
|
|
1154
|
-
}
|
|
1155
|
-
function ft(e, t, n) {
|
|
1156
|
-
let r = new I.Vector3().fromArray(e.com), i = new I.Quaternion().fromArray(e.inertiaFrame);
|
|
1157
|
-
return t && n && (r.applyMatrix4(t.clone().invert()), i.premultiply(n.clone().invert()).normalize()), {
|
|
1158
|
-
mass: e.mass,
|
|
1159
|
-
centerOfMass: {
|
|
1160
|
-
x: r.x,
|
|
1161
|
-
y: r.y,
|
|
1162
|
-
z: r.z
|
|
1163
|
-
},
|
|
1164
|
-
principalAngularInertia: {
|
|
1165
|
-
x: e.inertia[0],
|
|
1166
|
-
y: e.inertia[1],
|
|
1167
|
-
z: e.inertia[2]
|
|
1168
|
-
},
|
|
1169
|
-
angularInertiaLocalFrame: {
|
|
1170
|
-
x: i.x,
|
|
1171
|
-
y: i.y,
|
|
1172
|
-
z: i.z,
|
|
1173
|
-
w: i.w
|
|
1561
|
+
gltf.extensions ??= {};
|
|
1562
|
+
if (shapes.length > 0) {
|
|
1563
|
+
used.add("KHR_implicit_shapes");
|
|
1564
|
+
gltf.extensions.KHR_implicit_shapes = { shapes };
|
|
1174
1565
|
}
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
let l = {
|
|
1182
|
-
spec: e,
|
|
1183
|
-
local: a,
|
|
1184
|
-
position: [
|
|
1185
|
-
o.x,
|
|
1186
|
-
o.y,
|
|
1187
|
-
o.z
|
|
1188
|
-
],
|
|
1189
|
-
quaternion: [
|
|
1190
|
-
s.x,
|
|
1191
|
-
s.y,
|
|
1192
|
-
s.z,
|
|
1193
|
-
s.w
|
|
1194
|
-
]
|
|
1195
|
-
};
|
|
1196
|
-
return r === 0 && n && (l.massProperties = ft(n, a, i)), l;
|
|
1197
|
-
});
|
|
1198
|
-
}
|
|
1199
|
-
function mt(e, t, n) {
|
|
1200
|
-
for (let r = e.joint ? n.get(e.joint.connectedNodeName) : void 0; r; r = r.joint ? n.get(r.joint.connectedNodeName) : void 0) if (r.nodeName === t) return !0;
|
|
1201
|
-
return !1;
|
|
1202
|
-
}
|
|
1203
|
-
function ht(t) {
|
|
1204
|
-
let r = new Map(t.bodies.map((e) => [e.nodeName, e])), i = (t, n = /* @__PURE__ */ new Set()) => {
|
|
1205
|
-
if (!t.joint) return t;
|
|
1206
|
-
if (n.has(t.nodeName)) throw new e(`Joint cycle through "${t.nodeName}"`);
|
|
1207
|
-
n.add(t.nodeName);
|
|
1208
|
-
let a = r.get(t.joint.connectedNodeName);
|
|
1209
|
-
if (!a) throw new e(`Joint on "${t.nodeName}" targets "${t.joint.connectedNodeName}", which is not a KHR body`);
|
|
1210
|
-
return i(a, n);
|
|
1211
|
-
}, a = t.bodies.filter((e) => !e.joint).map((a) => {
|
|
1212
|
-
let o = x(a).clone().invert(), s = t.bodies.filter((e) => e.joint && i(e) === a), c = (e) => e.joint ? 1 + c(r.get(e.joint.connectedNodeName)) : 0;
|
|
1213
|
-
s.sort((e, t) => c(e) - c(t));
|
|
1214
|
-
let l = /* @__PURE__ */ new Map(), u = s.map((t) => {
|
|
1215
|
-
let n = t.joint, i = l.get(n.connectedNodeName) ?? null;
|
|
1216
|
-
if (i && !gt(t.object, i.spec.object)) throw new e(`Jointed node "${t.nodeName}" must be a scene-graph descendant of its jointed parent "${n.connectedNodeName}"`);
|
|
1217
|
-
let a = o.clone().multiply(x(t)), c = {
|
|
1218
|
-
spec: t,
|
|
1219
|
-
joint: n,
|
|
1220
|
-
parent: i,
|
|
1221
|
-
restRelParent: i ? x(i.spec).invert().multiply(x(t)) : a,
|
|
1222
|
-
inertia: de(t, n, s.filter((e) => e !== t && mt(e, t.nodeName, r))),
|
|
1223
|
-
colliders: pt(t, a, null)
|
|
1566
|
+
if (hasPhysics) {
|
|
1567
|
+
used.add("KHR_physics_rigid_bodies");
|
|
1568
|
+
gltf.extensions.KHR_physics_rigid_bodies = {
|
|
1569
|
+
...materials.length > 0 ? { physicsMaterials: materials } : {},
|
|
1570
|
+
...filters.length > 0 ? { collisionFilters: filters } : {},
|
|
1571
|
+
...joints.length > 0 ? { physicsJoints: joints } : {}
|
|
1224
1572
|
};
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
sceneInRootPos: [
|
|
1232
|
-
g.x,
|
|
1233
|
-
g.y,
|
|
1234
|
-
g.z
|
|
1235
|
-
],
|
|
1236
|
-
sceneInRootQuat: [
|
|
1237
|
-
_.x,
|
|
1238
|
-
_.y,
|
|
1239
|
-
_.z,
|
|
1240
|
-
_.w
|
|
1241
|
-
],
|
|
1242
|
-
mountNode: m,
|
|
1243
|
-
mountInRoot: h,
|
|
1244
|
-
mountPosition: [
|
|
1245
|
-
y.x,
|
|
1246
|
-
y.y,
|
|
1247
|
-
y.z
|
|
1248
|
-
],
|
|
1249
|
-
mountQuaternion: [
|
|
1250
|
-
b.x,
|
|
1251
|
-
b.y,
|
|
1252
|
-
b.z,
|
|
1253
|
-
b.w
|
|
1254
|
-
],
|
|
1255
|
-
...p ? { rootMass: p } : {},
|
|
1256
|
-
colliders: f,
|
|
1257
|
-
parts: u
|
|
1258
|
-
};
|
|
1573
|
+
}
|
|
1574
|
+
if (omiWheels.length > 0) {
|
|
1575
|
+
used.add("OMI_vehicle_wheel");
|
|
1576
|
+
gltf.extensions.OMI_vehicle_wheel = { wheels: omiWheels };
|
|
1577
|
+
}
|
|
1578
|
+
if (used.size > 0) gltf.extensionsUsed = [...used];
|
|
1259
1579
|
});
|
|
1260
|
-
for (let t of a) for (let n of a) if (t !== n && gt(n.mountNode, t.mountNode)) throw new e(`Root bodies "${t.spec.nodeName}" and "${n.spec.nodeName}" share scene subtree "${t.mountNode.name}"; split them into separate glTF branches`);
|
|
1261
|
-
return a;
|
|
1262
|
-
}
|
|
1263
|
-
function gt(e, t) {
|
|
1264
|
-
for (let n = e; n; n = n.parent) if (n === t) return !0;
|
|
1265
|
-
return !1;
|
|
1266
1580
|
}
|
|
1267
1581
|
// src/AssemblyRootBody.tsx
|
|
1268
|
-
function
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1582
|
+
function AssemblyRootBody({ plan, assemblyId, position, quaternion, fixedRoot, rigidBodyProps, onBody, onJoints, children }) {
|
|
1583
|
+
const joints = useContext(JointRegistryContext);
|
|
1584
|
+
const rbRef = useRef(null);
|
|
1585
|
+
const partColliderRefs = useMemo(() => plan.parts.map((p) => p.colliders.map(() => ({ current: null }))), [plan]);
|
|
1586
|
+
const rootSpec = plan.spec;
|
|
1587
|
+
useEffect(() => {
|
|
1588
|
+
onBody(rootSpec.nodeName, rbRef);
|
|
1589
|
+
return () => onBody(rootSpec.nodeName, null);
|
|
1590
|
+
}, [rootSpec, onBody]);
|
|
1591
|
+
useEffect(() => {
|
|
1592
|
+
const rb = rbRef.current;
|
|
1593
|
+
const rm = plan.rootMass;
|
|
1594
|
+
if (rb && rm) {
|
|
1595
|
+
rb.setAdditionalMassProperties(rm.mass, rm.centerOfMass, rm.principalAngularInertia, rm.angularInertiaLocalFrame, true);
|
|
1596
|
+
rb.recomputeMassPropertiesFromColliders();
|
|
1597
|
+
}
|
|
1598
|
+
}, [plan.rootMass]);
|
|
1599
|
+
useLayoutEffect(() => {
|
|
1600
|
+
const map = /* @__PURE__ */ new Map();
|
|
1601
|
+
for (let i = 0; i < plan.parts.length; i++) {
|
|
1602
|
+
const part = plan.parts[i];
|
|
1603
|
+
const drive = part.joint.drive;
|
|
1604
|
+
const rt = {
|
|
1605
|
+
assemblyId,
|
|
1606
|
+
nodeName: part.spec.nodeName,
|
|
1607
|
+
role: part.joint.role,
|
|
1608
|
+
kind: part.joint.kind,
|
|
1609
|
+
object: part.spec.object,
|
|
1610
|
+
axis: new THREE.Vector3().fromArray(part.joint.axis).normalize(),
|
|
1611
|
+
restPosition: part.spec.object.position.clone(),
|
|
1612
|
+
restQuaternion: part.spec.object.quaternion.clone(),
|
|
1285
1613
|
q: 0,
|
|
1286
1614
|
v: 0,
|
|
1287
|
-
positionTarget:
|
|
1288
|
-
velocityTarget:
|
|
1289
|
-
drive
|
|
1290
|
-
limits:
|
|
1291
|
-
limitStiffness:
|
|
1292
|
-
limitDamping:
|
|
1293
|
-
inertia:
|
|
1294
|
-
parent:
|
|
1295
|
-
restRelParent:
|
|
1296
|
-
current:
|
|
1615
|
+
positionTarget: drive?.positionTarget ?? null,
|
|
1616
|
+
velocityTarget: drive?.velocityTarget ?? 0,
|
|
1617
|
+
drive,
|
|
1618
|
+
limits: part.joint.limits,
|
|
1619
|
+
limitStiffness: part.joint.limitStiffness ?? null,
|
|
1620
|
+
limitDamping: part.joint.limitDamping ?? 0,
|
|
1621
|
+
inertia: part.inertia,
|
|
1622
|
+
parent: part.parent ? map.get(part.parent.spec.nodeName) ?? null : null,
|
|
1623
|
+
restRelParent: part.restRelParent,
|
|
1624
|
+
current: part.restRelParent.clone(),
|
|
1297
1625
|
stamp: -1,
|
|
1298
|
-
colliders:
|
|
1299
|
-
ref:
|
|
1300
|
-
local:
|
|
1626
|
+
colliders: part.colliders.map((c, idx) => ({
|
|
1627
|
+
ref: partColliderRefs[i][idx],
|
|
1628
|
+
local: c.local
|
|
1301
1629
|
}))
|
|
1302
1630
|
};
|
|
1303
|
-
|
|
1631
|
+
map.set(rt.nodeName, rt);
|
|
1632
|
+
joints?.add(rt);
|
|
1304
1633
|
}
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1634
|
+
onJoints(rootSpec.nodeName, map);
|
|
1635
|
+
return () => {
|
|
1636
|
+
onJoints(rootSpec.nodeName, null);
|
|
1637
|
+
for (const rt of map.values()) {
|
|
1638
|
+
joints?.delete(rt);
|
|
1639
|
+
rt.object.position.copy(rt.restPosition);
|
|
1640
|
+
rt.object.quaternion.copy(rt.restQuaternion);
|
|
1641
|
+
}
|
|
1308
1642
|
};
|
|
1309
1643
|
}, [
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1644
|
+
plan,
|
|
1645
|
+
joints,
|
|
1646
|
+
assemblyId,
|
|
1647
|
+
partColliderRefs,
|
|
1648
|
+
rootSpec,
|
|
1649
|
+
onJoints
|
|
1316
1650
|
]);
|
|
1317
|
-
|
|
1318
|
-
return /* @__PURE__ */
|
|
1319
|
-
...
|
|
1320
|
-
ref:
|
|
1321
|
-
type:
|
|
1322
|
-
position
|
|
1323
|
-
quaternion
|
|
1324
|
-
colliders:
|
|
1325
|
-
...
|
|
1326
|
-
...
|
|
1327
|
-
...
|
|
1651
|
+
const bodyType = rootSpec.isKinematic ? fixedRoot ? "fixed" : "kinematicPosition" : "dynamic";
|
|
1652
|
+
return /* @__PURE__ */ jsxs(RigidBody, {
|
|
1653
|
+
...rigidBodyProps,
|
|
1654
|
+
ref: rbRef,
|
|
1655
|
+
type: bodyType,
|
|
1656
|
+
position,
|
|
1657
|
+
quaternion,
|
|
1658
|
+
colliders: false,
|
|
1659
|
+
...rootSpec.linearVelocity ? { linearVelocity: rootSpec.linearVelocity } : {},
|
|
1660
|
+
...rootSpec.angularVelocity ? { angularVelocity: rootSpec.angularVelocity } : {},
|
|
1661
|
+
...rootSpec.gravityFactor === void 0 ? {} : { gravityScale: rootSpec.gravityFactor },
|
|
1328
1662
|
children: [
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
c:
|
|
1332
|
-
position:
|
|
1333
|
-
quaternion:
|
|
1334
|
-
massProperties:
|
|
1335
|
-
}, `r${
|
|
1336
|
-
|
|
1337
|
-
ref:
|
|
1338
|
-
c:
|
|
1339
|
-
position:
|
|
1340
|
-
quaternion:
|
|
1341
|
-
massProperties:
|
|
1342
|
-
}, `${
|
|
1663
|
+
children,
|
|
1664
|
+
plan.colliders.map((col, idx) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
1665
|
+
c: col.spec,
|
|
1666
|
+
position: col.position,
|
|
1667
|
+
quaternion: col.quaternion,
|
|
1668
|
+
massProperties: col.massProperties
|
|
1669
|
+
}, `r${idx}`)),
|
|
1670
|
+
plan.parts.map((part, pIdx) => part.colliders.map((col, cIdx) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
1671
|
+
ref: partColliderRefs[pIdx][cIdx],
|
|
1672
|
+
c: col.spec,
|
|
1673
|
+
position: col.position,
|
|
1674
|
+
quaternion: col.quaternion,
|
|
1675
|
+
massProperties: col.massProperties
|
|
1676
|
+
}, `${part.spec.nodeName}:${cIdx}`)))
|
|
1343
1677
|
]
|
|
1344
1678
|
});
|
|
1345
1679
|
}
|
|
1346
1680
|
// src/KhrAssembly.tsx
|
|
1347
|
-
var
|
|
1348
|
-
function
|
|
1681
|
+
var nextAssemblyId = 0;
|
|
1682
|
+
function KhrAssembly({ ref, bundle, position = [
|
|
1349
1683
|
0,
|
|
1350
1684
|
0,
|
|
1351
1685
|
0
|
|
1352
|
-
], rotation
|
|
1353
|
-
|
|
1354
|
-
|
|
1686
|
+
], rotation, quaternion, scale, fixedRoot = false, rigidBodyProps, children, ...groupProps }) {
|
|
1687
|
+
assertUnitScale(scale, "KhrAssembly");
|
|
1688
|
+
bundle.scene.updateMatrixWorld(true);
|
|
1689
|
+
assertSceneIdentityWorldScale(bundle.scene);
|
|
1690
|
+
const assemblyId = useMemo(() => nextAssemblyId++, []);
|
|
1691
|
+
const plans = useMemo(() => planAssembly(bundle), [bundle]);
|
|
1692
|
+
const origins = useMemo(() => plans.length > 1 ? captureSubtreeOrigins(plans.map((p) => p.mountNode)) : null, [plans]);
|
|
1693
|
+
const [px, py, pz] = position;
|
|
1694
|
+
const [rx, ry, rz] = rotation ?? [
|
|
1355
1695
|
0,
|
|
1356
1696
|
0,
|
|
1357
1697
|
0
|
|
1358
|
-
]
|
|
1698
|
+
];
|
|
1699
|
+
const [qx, qy, qz, qw] = quaternion ?? [
|
|
1359
1700
|
NaN,
|
|
1360
1701
|
NaN,
|
|
1361
1702
|
NaN,
|
|
1362
1703
|
NaN
|
|
1363
|
-
]
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1704
|
+
];
|
|
1705
|
+
const spawnQuat = useMemo(() => quaternion ? new THREE.Quaternion(qx, qy, qz, qw) : new THREE.Quaternion().setFromEuler(new THREE.Euler(rx, ry, rz)), [
|
|
1706
|
+
qx,
|
|
1707
|
+
qy,
|
|
1708
|
+
qz,
|
|
1709
|
+
qw,
|
|
1710
|
+
rx,
|
|
1711
|
+
ry,
|
|
1712
|
+
rz
|
|
1713
|
+
]);
|
|
1714
|
+
const rootPoses = useMemo(() => plans.map((p) => {
|
|
1715
|
+
const pos = new THREE.Vector3().fromArray(p.spec.localPosition).applyQuaternion(spawnQuat).add(new THREE.Vector3(px, py, pz));
|
|
1716
|
+
const quat = spawnQuat.clone().multiply(new THREE.Quaternion().fromArray(p.spec.localQuaternion));
|
|
1373
1717
|
return {
|
|
1374
1718
|
position: [
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1719
|
+
pos.x,
|
|
1720
|
+
pos.y,
|
|
1721
|
+
pos.z
|
|
1378
1722
|
],
|
|
1379
1723
|
quaternion: [
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1724
|
+
quat.x,
|
|
1725
|
+
quat.y,
|
|
1726
|
+
quat.z,
|
|
1727
|
+
quat.w
|
|
1384
1728
|
]
|
|
1385
1729
|
};
|
|
1386
1730
|
}), [
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
])
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1731
|
+
plans,
|
|
1732
|
+
spawnQuat,
|
|
1733
|
+
px,
|
|
1734
|
+
py,
|
|
1735
|
+
pz
|
|
1736
|
+
]);
|
|
1737
|
+
const bodiesRef = useRef(/* @__PURE__ */ new Map());
|
|
1738
|
+
const jointsRef = useRef(/* @__PURE__ */ new Map());
|
|
1739
|
+
const nodeToRoot = useMemo(() => {
|
|
1740
|
+
const m = /* @__PURE__ */ new Map();
|
|
1741
|
+
for (const p of plans) {
|
|
1742
|
+
m.set(p.spec.nodeName, p.spec.nodeName);
|
|
1743
|
+
for (const part of p.parts) m.set(part.spec.nodeName, p.spec.nodeName);
|
|
1397
1744
|
}
|
|
1398
|
-
return
|
|
1399
|
-
}, [
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1745
|
+
return m;
|
|
1746
|
+
}, [plans]);
|
|
1747
|
+
const handleBody = useCallback((rootName, rb) => {
|
|
1748
|
+
if (rb) bodiesRef.current.set(rootName, rb);
|
|
1749
|
+
else bodiesRef.current.delete(rootName);
|
|
1403
1750
|
}, []);
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1751
|
+
const handleJoints = useCallback((rootName, map) => {
|
|
1752
|
+
if (map) jointsRef.current.set(rootName, map);
|
|
1753
|
+
else jointsRef.current.delete(rootName);
|
|
1754
|
+
}, []);
|
|
1755
|
+
useImperativeHandle(ref, () => {
|
|
1756
|
+
const setTarget = (rt, target) => {
|
|
1757
|
+
if (!rt.drive || rt.drive.stiffness === void 0) throw new KhrValidationError(`Joint "${rt.nodeName}" has no authored drive stiffness; cannot set a position target`);
|
|
1758
|
+
rt.positionTarget = target;
|
|
1759
|
+
const rb = bodiesRef.current.get(nodeToRoot.get(rt.nodeName))?.current;
|
|
1760
|
+
if (rb && rb.isDynamic()) rb.wakeUp();
|
|
1410
1761
|
};
|
|
1411
1762
|
return {
|
|
1412
|
-
getBody: (
|
|
1413
|
-
|
|
1414
|
-
return
|
|
1763
|
+
getBody: (nodeName) => {
|
|
1764
|
+
const rootName = nodeToRoot.get(nodeName);
|
|
1765
|
+
return rootName ? bodiesRef.current.get(rootName)?.current ?? null : null;
|
|
1415
1766
|
},
|
|
1416
|
-
setJointTarget: (
|
|
1417
|
-
|
|
1418
|
-
|
|
1767
|
+
setJointTarget: (nodeName, target) => {
|
|
1768
|
+
const rt = jointsRef.current.get(nodeToRoot.get(nodeName) ?? "")?.get(nodeName);
|
|
1769
|
+
if (rt) setTarget(rt, target);
|
|
1419
1770
|
},
|
|
1420
|
-
setRoleTarget: (
|
|
1421
|
-
for (
|
|
1771
|
+
setRoleTarget: (role, target) => {
|
|
1772
|
+
for (const map of jointsRef.current.values()) for (const rt of map.values()) if (rt.role === role) setTarget(rt, target);
|
|
1422
1773
|
}
|
|
1423
1774
|
};
|
|
1424
|
-
}, [
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1775
|
+
}, [nodeToRoot]);
|
|
1776
|
+
if (plans.length === 1) {
|
|
1777
|
+
const plan = plans[0];
|
|
1778
|
+
return /* @__PURE__ */ jsx("group", {
|
|
1779
|
+
...groupProps,
|
|
1780
|
+
children: /* @__PURE__ */ jsx(AssemblyRootBody, {
|
|
1781
|
+
plan,
|
|
1782
|
+
assemblyId,
|
|
1783
|
+
position: rootPoses[0].position,
|
|
1784
|
+
quaternion: rootPoses[0].quaternion,
|
|
1785
|
+
fixedRoot,
|
|
1786
|
+
rigidBodyProps,
|
|
1787
|
+
onBody: handleBody,
|
|
1788
|
+
onJoints: handleJoints,
|
|
1789
|
+
children: /* @__PURE__ */ jsxs("group", {
|
|
1790
|
+
position: plan.sceneInRootPos,
|
|
1791
|
+
quaternion: plan.sceneInRootQuat,
|
|
1792
|
+
children: [/* @__PURE__ */ jsx(BundleSceneHost, {
|
|
1793
|
+
bundle,
|
|
1442
1794
|
owner: "KhrAssembly"
|
|
1443
|
-
}),
|
|
1795
|
+
}), children]
|
|
1444
1796
|
})
|
|
1445
|
-
}, `${
|
|
1797
|
+
}, `${bundle.scene.uuid}:${plan.spec.nodeName}`)
|
|
1446
1798
|
});
|
|
1447
1799
|
}
|
|
1448
|
-
return /* @__PURE__ */
|
|
1449
|
-
...
|
|
1450
|
-
children: [/* @__PURE__ */
|
|
1451
|
-
position
|
|
1452
|
-
quaternion:
|
|
1453
|
-
|
|
1454
|
-
|
|
1800
|
+
return /* @__PURE__ */ jsxs("group", {
|
|
1801
|
+
...groupProps,
|
|
1802
|
+
children: [/* @__PURE__ */ jsxs("group", {
|
|
1803
|
+
position,
|
|
1804
|
+
quaternion: [
|
|
1805
|
+
spawnQuat.x,
|
|
1806
|
+
spawnQuat.y,
|
|
1807
|
+
spawnQuat.z,
|
|
1808
|
+
spawnQuat.w
|
|
1809
|
+
],
|
|
1810
|
+
children: [/* @__PURE__ */ jsx(BundleSceneHost, {
|
|
1811
|
+
bundle,
|
|
1455
1812
|
owner: "KhrAssembly"
|
|
1456
|
-
}),
|
|
1457
|
-
}),
|
|
1458
|
-
plan
|
|
1459
|
-
assemblyId
|
|
1460
|
-
position:
|
|
1461
|
-
quaternion:
|
|
1462
|
-
fixedRoot
|
|
1463
|
-
rigidBodyProps
|
|
1464
|
-
onBody:
|
|
1465
|
-
onJoints:
|
|
1466
|
-
children: /* @__PURE__ */
|
|
1467
|
-
object:
|
|
1468
|
-
position:
|
|
1469
|
-
quaternion:
|
|
1470
|
-
origin:
|
|
1813
|
+
}), children]
|
|
1814
|
+
}), plans.map((plan, idx) => /* @__PURE__ */ jsx(AssemblyRootBody, {
|
|
1815
|
+
plan,
|
|
1816
|
+
assemblyId,
|
|
1817
|
+
position: rootPoses[idx].position,
|
|
1818
|
+
quaternion: rootPoses[idx].quaternion,
|
|
1819
|
+
fixedRoot,
|
|
1820
|
+
rigidBodyProps,
|
|
1821
|
+
onBody: handleBody,
|
|
1822
|
+
onJoints: handleJoints,
|
|
1823
|
+
children: /* @__PURE__ */ jsx(MountSubtreeSlot, {
|
|
1824
|
+
object: plan.mountNode,
|
|
1825
|
+
position: plan.mountPosition,
|
|
1826
|
+
quaternion: plan.mountQuaternion,
|
|
1827
|
+
origin: origins?.get(plan.mountNode)
|
|
1471
1828
|
})
|
|
1472
|
-
}, `${
|
|
1829
|
+
}, `${bundle.scene.uuid}:${plan.spec.nodeName}`))]
|
|
1473
1830
|
});
|
|
1474
1831
|
}
|
|
1475
1832
|
// src/useKhrGlb.ts
|
|
1476
|
-
var
|
|
1833
|
+
var KhrGLTFLoader = class extends GLTFLoader {
|
|
1477
1834
|
static khrLoaderScope = Symbol("KhrGLTFLoader");
|
|
1478
|
-
}
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1835
|
+
};
|
|
1836
|
+
var headlessPlugin = createHeadlessTexturePlugin();
|
|
1837
|
+
var khrPlugin = createKhrGltfPlugin();
|
|
1838
|
+
var configuredLoaders = /* @__PURE__ */ new WeakSet();
|
|
1839
|
+
var customExtendedLoaders = /* @__PURE__ */ new WeakSet();
|
|
1840
|
+
var customLoaderRegistry = /* @__PURE__ */ new Map();
|
|
1841
|
+
function configureKhrLoader(loader) {
|
|
1842
|
+
if (configuredLoaders.has(loader)) return;
|
|
1843
|
+
configuredLoaders.add(loader);
|
|
1844
|
+
if (isHeadlessTextureEnv()) loader.register(headlessPlugin);
|
|
1845
|
+
loader.register(khrPlugin);
|
|
1846
|
+
}
|
|
1847
|
+
function hasCustomLoaderOpts(options) {
|
|
1848
|
+
return Boolean(options?.extendLoader || options?.cacheKey !== void 0);
|
|
1849
|
+
}
|
|
1850
|
+
function getLoaderCacheKey(options) {
|
|
1851
|
+
return options.cacheKey === void 0 ? `src:${Function.prototype.toString.call(options.extendLoader)}` : `key:${options.cacheKey}`;
|
|
1852
|
+
}
|
|
1853
|
+
function getScopedLoaderEntry(url, options) {
|
|
1854
|
+
const key = getLoaderCacheKey(options);
|
|
1855
|
+
let urlMap = customLoaderRegistry.get(url);
|
|
1856
|
+
if (!urlMap) {
|
|
1857
|
+
urlMap = /* @__PURE__ */ new Map();
|
|
1858
|
+
customLoaderRegistry.set(url, urlMap);
|
|
1859
|
+
}
|
|
1860
|
+
let entry = urlMap.get(key);
|
|
1861
|
+
if (!entry) {
|
|
1862
|
+
const loader = new GLTFLoader();
|
|
1863
|
+
const extend = options.extendLoader;
|
|
1864
|
+
const created = {
|
|
1865
|
+
loader,
|
|
1866
|
+
registerExtensions: (target) => {
|
|
1867
|
+
configureKhrLoader(target);
|
|
1868
|
+
if (extend && !customExtendedLoaders.has(target)) try {
|
|
1869
|
+
extend(target);
|
|
1870
|
+
customExtendedLoaders.add(target);
|
|
1871
|
+
} catch (err) {
|
|
1872
|
+
const map = customLoaderRegistry.get(url);
|
|
1873
|
+
if (map?.get(key) === created) {
|
|
1874
|
+
map.delete(key);
|
|
1875
|
+
if (map.size === 0) customLoaderRegistry.delete(url);
|
|
1876
|
+
}
|
|
1877
|
+
throw err;
|
|
1501
1878
|
}
|
|
1502
1879
|
}
|
|
1503
1880
|
};
|
|
1504
|
-
|
|
1881
|
+
entry = created;
|
|
1882
|
+
urlMap.set(key, entry);
|
|
1505
1883
|
}
|
|
1506
|
-
return
|
|
1507
|
-
}
|
|
1508
|
-
function
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1884
|
+
return entry;
|
|
1885
|
+
}
|
|
1886
|
+
function useKhrGlb(url, options) {
|
|
1887
|
+
const extractRole = options?.extractRole;
|
|
1888
|
+
const isSocketNode = options?.isSocketNode;
|
|
1889
|
+
const scoped = hasCustomLoaderOpts(options) ? getScopedLoaderEntry(url, options) : null;
|
|
1890
|
+
const loader = scoped ? scoped.loader : KhrGLTFLoader;
|
|
1891
|
+
const reg = scoped ? scoped.registerExtensions : configureKhrLoader;
|
|
1892
|
+
const template = useLoader(loader, url, reg).userData[KHR_BUNDLE_USERDATA_KEY];
|
|
1893
|
+
if (!template) throw new KhrValidationError(`useKhrGlb: missing "${KHR_BUNDLE_USERDATA_KEY}" on loaded GLTF for "${url}"`);
|
|
1894
|
+
return useMemo(() => {
|
|
1895
|
+
const cloned = cloneKhrBundle(template);
|
|
1896
|
+
if (extractRole || isSocketNode) applyKhrCustomExtractors(cloned, template, extractRole, isSocketNode);
|
|
1897
|
+
return cloned;
|
|
1514
1898
|
}, [
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1899
|
+
template,
|
|
1900
|
+
extractRole,
|
|
1901
|
+
isSocketNode
|
|
1518
1902
|
]);
|
|
1519
1903
|
}
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1904
|
+
(function(_useKhrGlb) {
|
|
1905
|
+
_useKhrGlb.preload = (url, options) => {
|
|
1906
|
+
const scoped = hasCustomLoaderOpts(options) ? getScopedLoaderEntry(url, options) : null;
|
|
1907
|
+
const loader = scoped ? scoped.loader : KhrGLTFLoader;
|
|
1908
|
+
const reg = scoped ? scoped.registerExtensions : configureKhrLoader;
|
|
1909
|
+
useLoader.preload(loader, url, reg);
|
|
1910
|
+
};
|
|
1911
|
+
_useKhrGlb.clear = (url, options) => {
|
|
1912
|
+
if (hasCustomLoaderOpts(options)) {
|
|
1913
|
+
const urlMap = customLoaderRegistry.get(url);
|
|
1914
|
+
if (!urlMap) return;
|
|
1915
|
+
const key = getLoaderCacheKey(options);
|
|
1916
|
+
const entry = urlMap.get(key);
|
|
1917
|
+
if (entry) {
|
|
1918
|
+
useLoader.clear(entry.loader, url);
|
|
1919
|
+
urlMap.delete(key);
|
|
1920
|
+
if (urlMap.size === 0) customLoaderRegistry.delete(url);
|
|
1921
|
+
}
|
|
1922
|
+
} else {
|
|
1923
|
+
useLoader.clear(KhrGLTFLoader, url);
|
|
1924
|
+
if (options === void 0) {
|
|
1925
|
+
const urlMap = customLoaderRegistry.get(url);
|
|
1926
|
+
if (urlMap) {
|
|
1927
|
+
for (const entry of urlMap.values()) useLoader.clear(entry.loader, url);
|
|
1928
|
+
customLoaderRegistry.delete(url);
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1534
1931
|
}
|
|
1535
|
-
}
|
|
1536
|
-
};
|
|
1537
|
-
export {
|
|
1932
|
+
};
|
|
1933
|
+
})(useKhrGlb || (useKhrGlb = {}));
|
|
1934
|
+
export { JointRegistryContext, KhrAssembly, KhrCollider, KhrValidationError, PhysicsWorld, assertIdentityWorldScale, assertSceneIdentityWorldScale, assertUnitScale, claimBundleMount, cloneKhrBundle, createKhrGltfPlugin, disposeKhrBundle, exportKhrGlb, loadKhrGlb, patchGlbJsonChunk, releaseBundleMount, useKhrGlb };
|
|
1935
|
+
|
|
1936
|
+
//# sourceMappingURL=index.js.map
|