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