@khr3/core 0.2.11 → 0.2.13
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 +54 -3
- package/dist/KhrAssembly.d.ts +2 -5
- package/dist/KhrCollider.d.ts +5 -1
- package/dist/clone.d.ts +5 -0
- package/dist/decoders.d.ts +33 -0
- package/dist/heightfield.d.ts +45 -0
- package/dist/index.d.ts +6 -5
- package/dist/index.js +1182 -339
- package/dist/index.js.map +1 -1
- package/dist/inertia.d.ts +26 -5
- package/dist/{KhrAssembly-CKW8I_2z.js → joints-H3uae8pm.js} +296 -624
- package/dist/joints-H3uae8pm.js.map +1 -0
- package/dist/joints.d.ts +2 -0
- package/dist/types.d.ts +14 -0
- package/dist/vehicle.d.ts +4 -17
- package/dist/vehicle.js +409 -422
- package/dist/vehicle.js.map +1 -1
- package/dist/vehicleJoints.d.ts +50 -0
- package/docs/api-reference.md +160 -0
- package/package.json +4 -3
- package/dist/KhrAssembly-CKW8I_2z.js.map +0 -1
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import React, { createContext, useCallback,
|
|
1
|
+
import React, { createContext, useCallback, useEffect, useLayoutEffect, useRef } from "react";
|
|
2
|
+
import { BallCollider, CapsuleCollider, CoefficientCombineRule, ConvexHullCollider, CuboidCollider, CylinderCollider, TrimeshCollider } from "@react-three/rapier";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
4
|
import * as THREE from "three";
|
|
3
|
-
import { useFrame } from "@react-three/fiber";
|
|
4
|
-
import { BallCollider, CapsuleCollider, CoefficientCombineRule, ConvexHullCollider, CuboidCollider, CylinderCollider, RigidBody, TrimeshCollider, useRapier } from "@react-three/rapier";
|
|
5
|
-
import { useCustomGravity } from "@pinkaphant/ecctrl/gravity";
|
|
6
5
|
import { ConvexGeometry } from "three/examples/jsm/geometries/ConvexGeometry.js";
|
|
7
|
-
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
8
6
|
// src/types.ts
|
|
9
7
|
var KhrValidationError = class extends Error {
|
|
10
8
|
constructor(message) {
|
|
@@ -35,6 +33,7 @@ function assignRef(ref, value) {
|
|
|
35
33
|
// src/inertia.ts
|
|
36
34
|
var SCALE_EPS = 1e-4;
|
|
37
35
|
var _wsScratch = new THREE.Vector3();
|
|
36
|
+
var _one = new THREE.Vector3(1, 1, 1);
|
|
38
37
|
function isUnitScalar(v) {
|
|
39
38
|
return typeof v === "number" && Number.isFinite(v) && Math.abs(v - 1) <= 1e-9;
|
|
40
39
|
}
|
|
@@ -137,6 +136,10 @@ function extractMeshColliderShape(targetObj, meshNodeName, convexHull) {
|
|
|
137
136
|
meshNodeName
|
|
138
137
|
};
|
|
139
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Exact 3x3 symmetric Jacobi eigendecomposition returning positive principal inertia diagonal
|
|
141
|
+
* and right-handed `SO(3)` principal frame quaternion `[x, y, z, w]`.
|
|
142
|
+
*/
|
|
140
143
|
function jacobiPrincipalInertia(ixx, iyy, izz, ixy, ixz, iyz) {
|
|
141
144
|
const a = [
|
|
142
145
|
[
|
|
@@ -225,10 +228,9 @@ function jacobiPrincipalInertia(ixx, iyy, izz, ixy, ixz, iyz) {
|
|
|
225
228
|
};
|
|
226
229
|
}
|
|
227
230
|
/**
|
|
228
|
-
* Exact signed-tetrahedron divergence integration for closed triangle meshes
|
|
229
|
-
* Falls back cleanly to AABB slab inertia only when the mesh is flat/open with near-zero enclosed volume.
|
|
231
|
+
* Exact 3D signed-tetrahedron divergence integration for closed solid triangle meshes.
|
|
230
232
|
*/
|
|
231
|
-
function
|
|
233
|
+
function integrateClosedMeshProps(vertices, indices) {
|
|
232
234
|
const triCount = indices ? Math.floor(indices.length / 3) : Math.floor(vertices.length / 9);
|
|
233
235
|
if (triCount < 4) return null;
|
|
234
236
|
let rx = 0, ry = 0, rz = 0;
|
|
@@ -283,7 +285,8 @@ function integrateTriangleMeshProps(vertices, indices) {
|
|
|
283
285
|
const izz = cxx + cyy;
|
|
284
286
|
if (!(ixx > 0 && iyy > 0 && izz > 0)) return null;
|
|
285
287
|
return {
|
|
286
|
-
volume,
|
|
288
|
+
measure: volume,
|
|
289
|
+
isVolumetric: true,
|
|
287
290
|
centroid: new THREE.Vector3(rx + comRelX, ry + comRelY, rz + comRelZ),
|
|
288
291
|
unitTensor: [
|
|
289
292
|
ixx,
|
|
@@ -295,30 +298,101 @@ function integrateTriangleMeshProps(vertices, indices) {
|
|
|
295
298
|
]
|
|
296
299
|
};
|
|
297
300
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
301
|
+
/**
|
|
302
|
+
* Exact 2D surface-area second-moment integration over open triangle shells
|
|
303
|
+
* (used when an unclosed sheet-metal panel mesh has zero enclosed 3D volume).
|
|
304
|
+
* Throws `KhrValidationError` if all triangles are collinear/degenerate.
|
|
305
|
+
*/
|
|
306
|
+
function integrateSurfaceShellProps(vertices, indices, label) {
|
|
307
|
+
const triCount = indices ? Math.floor(indices.length / 3) : Math.floor(vertices.length / 9);
|
|
308
|
+
if (triCount < 1) throw new KhrValidationError(`Collider mesh "${label}" has no triangles`);
|
|
309
|
+
let rx = 0, ry = 0, rz = 0;
|
|
310
|
+
const vCount = Math.floor(vertices.length / 3);
|
|
311
|
+
for (let i = 0; i < vCount; i++) {
|
|
312
|
+
rx += vertices[i * 3];
|
|
313
|
+
ry += vertices[i * 3 + 1];
|
|
314
|
+
rz += vertices[i * 3 + 2];
|
|
315
|
+
}
|
|
316
|
+
rx /= vCount;
|
|
317
|
+
ry /= vCount;
|
|
318
|
+
rz /= vCount;
|
|
319
|
+
let area2 = 0;
|
|
320
|
+
let cx6 = 0, cy6 = 0, cz6 = 0;
|
|
321
|
+
let sxx24 = 0, syy24 = 0, szz24 = 0;
|
|
322
|
+
let sxy24 = 0, sxz24 = 0, syz24 = 0;
|
|
323
|
+
for (let t = 0; t < triCount; t++) {
|
|
324
|
+
const i0 = (indices ? indices[t * 3] : t * 3) * 3;
|
|
325
|
+
const i1 = (indices ? indices[t * 3 + 1] : t * 3 + 1) * 3;
|
|
326
|
+
const i2 = (indices ? indices[t * 3 + 2] : t * 3 + 2) * 3;
|
|
327
|
+
const ax = vertices[i0] - rx, ay = vertices[i0 + 1] - ry, az = vertices[i0 + 2] - rz;
|
|
328
|
+
const bx = vertices[i1] - rx, by = vertices[i1 + 1] - ry, bz = vertices[i1 + 2] - rz;
|
|
329
|
+
const cx = vertices[i2] - rx, cy = vertices[i2 + 1] - ry, cz = vertices[i2 + 2] - rz;
|
|
330
|
+
const e1x = bx - ax, e1y = by - ay, e1z = bz - az;
|
|
331
|
+
const e2x = cx - ax, e2y = cy - ay, e2z = cz - az;
|
|
332
|
+
const nx = e1y * e2z - e1z * e2y;
|
|
333
|
+
const ny = e1z * e2x - e1x * e2z;
|
|
334
|
+
const nz = e1x * e2y - e1y * e2x;
|
|
335
|
+
const dblArea = Math.hypot(nx, ny, nz);
|
|
336
|
+
if (dblArea <= 1e-14) continue;
|
|
337
|
+
area2 += dblArea;
|
|
338
|
+
const sx = ax + bx + cx, sy = ay + by + cy, sz = az + bz + cz;
|
|
339
|
+
cx6 += dblArea * sx;
|
|
340
|
+
cy6 += dblArea * sy;
|
|
341
|
+
cz6 += dblArea * sz;
|
|
342
|
+
sxx24 += dblArea * (sx * sx + ax * ax + bx * bx + cx * cx);
|
|
343
|
+
syy24 += dblArea * (sy * sy + ay * ay + by * by + cy * cy);
|
|
344
|
+
szz24 += dblArea * (sz * sz + az * az + bz * bz + cz * cz);
|
|
345
|
+
sxy24 += dblArea * (sx * sy + ax * ay + bx * by + cx * cy);
|
|
346
|
+
sxz24 += dblArea * (sx * sz + ax * az + bx * bz + cx * cz);
|
|
347
|
+
syz24 += dblArea * (sy * sz + ay * az + by * bz + cy * cz);
|
|
348
|
+
}
|
|
349
|
+
const area = .5 * area2;
|
|
350
|
+
if (!(area > 1e-10)) throw new KhrValidationError(`Collider mesh "${label}" has zero surface area (collinear/degenerate vertices)`);
|
|
351
|
+
const comRelX = cx6 / (6 * area);
|
|
352
|
+
const comRelY = cy6 / (6 * area);
|
|
353
|
+
const comRelZ = cz6 / (6 * area);
|
|
354
|
+
const cxx = sxx24 / (24 * area) - comRelX * comRelX;
|
|
355
|
+
const cyy = syy24 / (24 * area) - comRelY * comRelY;
|
|
356
|
+
const czz = szz24 / (24 * area) - comRelZ * comRelZ;
|
|
357
|
+
const cxy = sxy24 / (24 * area) - comRelX * comRelY;
|
|
358
|
+
const cxz = sxz24 / (24 * area) - comRelX * comRelZ;
|
|
359
|
+
const cyz = syz24 / (24 * area) - comRelY * comRelZ;
|
|
360
|
+
const ixx = Math.max(cyy + czz, 1e-9);
|
|
361
|
+
const iyy = Math.max(cxx + czz, 1e-9);
|
|
362
|
+
const izz = Math.max(cxx + cyy, 1e-9);
|
|
304
363
|
return {
|
|
305
|
-
|
|
306
|
-
|
|
364
|
+
measure: area,
|
|
365
|
+
isVolumetric: false,
|
|
366
|
+
centroid: new THREE.Vector3(rx + comRelX, ry + comRelY, rz + comRelZ),
|
|
307
367
|
unitTensor: [
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
368
|
+
ixx,
|
|
369
|
+
iyy,
|
|
370
|
+
izz,
|
|
371
|
+
-cxy,
|
|
372
|
+
-cxz,
|
|
373
|
+
-cyz
|
|
314
374
|
]
|
|
315
375
|
};
|
|
316
376
|
}
|
|
377
|
+
function deduplicateQuantizedPoints(vertices) {
|
|
378
|
+
const pts = [];
|
|
379
|
+
const seen = /* @__PURE__ */ new Set();
|
|
380
|
+
for (let i = 0; i < vertices.length; i += 3) {
|
|
381
|
+
const vx = vertices[i], vy = vertices[i + 1], vz = vertices[i + 2];
|
|
382
|
+
const key = `${Math.round(vx * 1e5)},${Math.round(vy * 1e5)},${Math.round(vz * 1e5)}`;
|
|
383
|
+
if (!seen.has(key)) {
|
|
384
|
+
seen.add(key);
|
|
385
|
+
pts.push(new THREE.Vector3(vx, vy, vz));
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return pts;
|
|
389
|
+
}
|
|
317
390
|
function unitInertiaForShape(shape) {
|
|
318
391
|
if (shape.type === "box") {
|
|
319
392
|
const [x, y, z] = shape.size;
|
|
320
393
|
return {
|
|
321
|
-
|
|
394
|
+
measure: x * y * z,
|
|
395
|
+
isVolumetric: true,
|
|
322
396
|
centroid: new THREE.Vector3(),
|
|
323
397
|
unitTensor: [
|
|
324
398
|
(y * y + z * z) / 12,
|
|
@@ -334,7 +408,8 @@ function unitInertiaForShape(shape) {
|
|
|
334
408
|
const r = shape.radius;
|
|
335
409
|
const i = .4 * r * r;
|
|
336
410
|
return {
|
|
337
|
-
|
|
411
|
+
measure: 4 / 3 * Math.PI * r * r * r,
|
|
412
|
+
isVolumetric: true,
|
|
338
413
|
centroid: new THREE.Vector3(),
|
|
339
414
|
unitTensor: [
|
|
340
415
|
i,
|
|
@@ -346,31 +421,33 @@ function unitInertiaForShape(shape) {
|
|
|
346
421
|
]
|
|
347
422
|
};
|
|
348
423
|
}
|
|
349
|
-
if (shape.type === "trimesh") return
|
|
424
|
+
if (shape.type === "trimesh") return integrateClosedMeshProps(shape.vertices, shape.indices) ?? integrateSurfaceShellProps(shape.vertices, shape.indices, shape.meshNodeName);
|
|
350
425
|
if (shape.type === "convexHull") {
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
426
|
+
const pts = deduplicateQuantizedPoints(shape.vertices);
|
|
427
|
+
if (pts.length >= 4) try {
|
|
428
|
+
const hullGeo = new ConvexGeometry(pts);
|
|
429
|
+
const pos = hullGeo.getAttribute("position");
|
|
430
|
+
const closed = pos && pos.count >= 12 ? integrateClosedMeshProps(pos.array, null) : null;
|
|
431
|
+
hullGeo.dispose();
|
|
432
|
+
if (closed) return closed;
|
|
433
|
+
} catch {}
|
|
434
|
+
if (pts.length >= 3) {
|
|
435
|
+
const fanVerts = new Float32Array((pts.length - 2) * 9);
|
|
436
|
+
let o = 0;
|
|
437
|
+
for (let i = 1; i + 1 < pts.length; i++) {
|
|
438
|
+
fanVerts[o++] = pts[0].x;
|
|
439
|
+
fanVerts[o++] = pts[0].y;
|
|
440
|
+
fanVerts[o++] = pts[0].z;
|
|
441
|
+
fanVerts[o++] = pts[i].x;
|
|
442
|
+
fanVerts[o++] = pts[i].y;
|
|
443
|
+
fanVerts[o++] = pts[i].z;
|
|
444
|
+
fanVerts[o++] = pts[i + 1].x;
|
|
445
|
+
fanVerts[o++] = pts[i + 1].y;
|
|
446
|
+
fanVerts[o++] = pts[i + 1].z;
|
|
364
447
|
}
|
|
365
|
-
|
|
366
|
-
const hullGeo = new ConvexGeometry(pts);
|
|
367
|
-
const pos = hullGeo.getAttribute("position");
|
|
368
|
-
const exact = pos && pos.count >= 12 ? integrateTriangleMeshProps(pos.array, null) : null;
|
|
369
|
-
hullGeo.dispose();
|
|
370
|
-
if (exact) return exact;
|
|
371
|
-
} catch {}
|
|
448
|
+
return integrateSurfaceShellProps(fanVerts, null, shape.meshNodeName);
|
|
372
449
|
}
|
|
373
|
-
|
|
450
|
+
throw new KhrValidationError(`ConvexHull collider "${shape.meshNodeName}" has fewer than 3 distinct vertices`);
|
|
374
451
|
}
|
|
375
452
|
const r = shape.radiusTop;
|
|
376
453
|
const h = shape.height;
|
|
@@ -382,7 +459,8 @@ function unitInertiaForShape(shape) {
|
|
|
382
459
|
const iy = .5 * fc * r * r + .4 * fh * r * r;
|
|
383
460
|
const ix = fc * (h * h / 12 + .25 * r * r) + fh * (.4 * r * r + .25 * h * h + .375 * h * r);
|
|
384
461
|
return {
|
|
385
|
-
|
|
462
|
+
measure: totVol,
|
|
463
|
+
isVolumetric: true,
|
|
386
464
|
centroid: new THREE.Vector3(),
|
|
387
465
|
unitTensor: [
|
|
388
466
|
ix,
|
|
@@ -396,7 +474,8 @@ function unitInertiaForShape(shape) {
|
|
|
396
474
|
}
|
|
397
475
|
const ix = (3 * r * r + h * h) / 12;
|
|
398
476
|
return {
|
|
399
|
-
|
|
477
|
+
measure: Math.PI * r * r * h,
|
|
478
|
+
isVolumetric: true,
|
|
400
479
|
centroid: new THREE.Vector3(),
|
|
401
480
|
unitTensor: [
|
|
402
481
|
ix,
|
|
@@ -408,6 +487,10 @@ function unitInertiaForShape(shape) {
|
|
|
408
487
|
]
|
|
409
488
|
};
|
|
410
489
|
}
|
|
490
|
+
/**
|
|
491
|
+
* Computes exact principal inertia diagonal `[I1, I2, I3]` for a single `KhrColliderShape` of `mass` kg
|
|
492
|
+
* about its centroid.
|
|
493
|
+
*/
|
|
411
494
|
function computeShapeInertia(shape, mass) {
|
|
412
495
|
const [ixx, iyy, izz, ixy, ixz, iyz] = unitInertiaForShape(shape).unitTensor;
|
|
413
496
|
if (Math.abs(ixy) + Math.abs(ixz) + Math.abs(iyz) > 1e-12 * (ixx + iyy + izz)) {
|
|
@@ -424,44 +507,110 @@ function computeShapeInertia(shape, mass) {
|
|
|
424
507
|
mass * izz
|
|
425
508
|
];
|
|
426
509
|
}
|
|
427
|
-
|
|
510
|
+
/**
|
|
511
|
+
* Resolves exact center of mass, full 3x3 Steiner parallel-axis inertia tensor, and Jacobi principal frame
|
|
512
|
+
* for a dynamic `KhrBodySpec` from its colliders when `motion.inertiaDiagonal` is omitted in the `.glb`.
|
|
513
|
+
*/
|
|
514
|
+
function resolveBodyMassFromColliders(colliders, mass, explicitCom, explicitInertiaOrientation, nodeName) {
|
|
428
515
|
if (colliders.length === 0) throw new KhrValidationError(`Dynamic node "${nodeName}" is missing motion.inertiaDiagonal and has no colliders`);
|
|
429
516
|
const shapes = colliders.map((c) => unitInertiaForShape(c.shape));
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
const
|
|
433
|
-
const com = new THREE.Vector3().fromArray(centerOfMass);
|
|
517
|
+
const hasVolumetric = shapes.some((s) => s.isVolumetric);
|
|
518
|
+
let totalWeight = 0;
|
|
519
|
+
for (const s of shapes) if (!hasVolumetric || s.isVolumetric) totalWeight += s.measure;
|
|
434
520
|
const colQuat = new THREE.Quaternion();
|
|
521
|
+
const worldCentroids = [];
|
|
522
|
+
let wx = 0, wy = 0, wz = 0;
|
|
523
|
+
for (let i = 0; i < colliders.length; i++) {
|
|
524
|
+
const col = colliders[i];
|
|
525
|
+
const shp = shapes[i];
|
|
526
|
+
const w = !hasVolumetric || shp.isVolumetric ? shp.measure : 0;
|
|
527
|
+
colQuat.fromArray(col.localQuaternion);
|
|
528
|
+
const cBody = shp.centroid.clone().applyQuaternion(colQuat).add(new THREE.Vector3().fromArray(col.localPosition));
|
|
529
|
+
worldCentroids.push(cBody);
|
|
530
|
+
wx += cBody.x * w;
|
|
531
|
+
wy += cBody.y * w;
|
|
532
|
+
wz += cBody.z * w;
|
|
533
|
+
}
|
|
534
|
+
const com = explicitCom ?? (totalWeight > 0 ? [
|
|
535
|
+
wx / totalWeight,
|
|
536
|
+
wy / totalWeight,
|
|
537
|
+
wz / totalWeight
|
|
538
|
+
] : [
|
|
539
|
+
0,
|
|
540
|
+
0,
|
|
541
|
+
0
|
|
542
|
+
]);
|
|
543
|
+
const invFrame = explicitInertiaOrientation ? new THREE.Quaternion().fromArray(explicitInertiaOrientation).invert() : new THREE.Quaternion();
|
|
435
544
|
const relQuat = new THREE.Quaternion();
|
|
436
545
|
const offset = new THREE.Vector3();
|
|
437
546
|
const rotMat = new THREE.Matrix4();
|
|
438
547
|
const m = rotMat.elements;
|
|
439
|
-
let
|
|
548
|
+
let ixx = 0, iyy = 0, izz = 0, ixy = 0, ixz = 0, iyz = 0;
|
|
440
549
|
for (let i = 0; i < colliders.length; i++) {
|
|
441
550
|
const col = colliders[i];
|
|
442
551
|
const shp = shapes[i];
|
|
443
|
-
const
|
|
552
|
+
const w = !hasVolumetric || shp.isVolumetric ? shp.measure : 0;
|
|
553
|
+
const mShare = totalWeight > 0 ? mass * (w / totalWeight) : mass / colliders.length;
|
|
554
|
+
if (mShare <= 0) continue;
|
|
444
555
|
colQuat.fromArray(col.localQuaternion);
|
|
445
|
-
offset.copy(
|
|
556
|
+
offset.copy(worldCentroids[i]).sub(new THREE.Vector3(com[0], com[1], com[2])).applyQuaternion(invFrame);
|
|
446
557
|
relQuat.copy(invFrame).multiply(colQuat);
|
|
447
558
|
rotMat.makeRotationFromQuaternion(relQuat);
|
|
448
|
-
const [
|
|
559
|
+
const [uxx, uyy, uzz, uxy, uxz, uyz] = shp.unitTensor;
|
|
449
560
|
const r00 = m[0], r01 = m[4], r02 = m[8];
|
|
450
561
|
const r10 = m[1], r11 = m[5], r12 = m[9];
|
|
451
562
|
const r20 = m[2], r21 = m[6], r22 = m[10];
|
|
452
|
-
const
|
|
453
|
-
const
|
|
454
|
-
const
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
563
|
+
const a00 = uxx * r00 + uxy * r01 + uxz * r02;
|
|
564
|
+
const a01 = uxy * r00 + uyy * r01 + uyz * r02;
|
|
565
|
+
const a02 = uxz * r00 + uyz * r01 + uzz * r02;
|
|
566
|
+
const a10 = uxx * r10 + uxy * r11 + uxz * r12;
|
|
567
|
+
const a11 = uxy * r10 + uyy * r11 + uyz * r12;
|
|
568
|
+
const a12 = uxz * r10 + uyz * r11 + uzz * r12;
|
|
569
|
+
const txx = r00 * a00 + r01 * a01 + r02 * a02;
|
|
570
|
+
const tyy = r10 * a10 + r11 * a11 + r12 * a12;
|
|
571
|
+
const tzz = r20 * (uxx * r20 + uxy * r21 + uxz * r22) + r21 * (uxy * r20 + uyy * r21 + uyz * r22) + r22 * (uxz * r20 + uyz * r21 + uzz * r22);
|
|
572
|
+
const txy = r00 * a10 + r01 * a11 + r02 * a12;
|
|
573
|
+
const txz = r00 * (uxx * r20 + uxy * r21 + uxz * r22) + r01 * (uxy * r20 + uyy * r21 + uyz * r22) + r02 * (uxz * r20 + uyz * r21 + uzz * r22);
|
|
574
|
+
const tyz = r10 * (uxx * r20 + uxy * r21 + uxz * r22) + r11 * (uxy * r20 + uyy * r21 + uyz * r22) + r12 * (uxz * r20 + uyz * r21 + uzz * r22);
|
|
575
|
+
const dx = offset.x, dy = offset.y, dz = offset.z;
|
|
576
|
+
ixx += mShare * (txx + dy * dy + dz * dz);
|
|
577
|
+
iyy += mShare * (tyy + dx * dx + dz * dz);
|
|
578
|
+
izz += mShare * (tzz + dx * dx + dy * dy);
|
|
579
|
+
ixy += mShare * (txy - dx * dy);
|
|
580
|
+
ixz += mShare * (txz - dx * dz);
|
|
581
|
+
iyz += mShare * (tyz - dy * dz);
|
|
458
582
|
}
|
|
459
|
-
if (!Number.isFinite(
|
|
460
|
-
return
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
583
|
+
if (!Number.isFinite(ixx) || !Number.isFinite(iyy) || !Number.isFinite(izz) || ixx <= 0 || iyy <= 0 || izz <= 0) throw new KhrValidationError(`Dynamic node "${nodeName}" has degenerate collider geometry; cannot compute positive motion.inertiaDiagonal`);
|
|
584
|
+
if (explicitInertiaOrientation) return {
|
|
585
|
+
centerOfMass: com,
|
|
586
|
+
inertiaDiagonal: [
|
|
587
|
+
ixx,
|
|
588
|
+
iyy,
|
|
589
|
+
izz
|
|
590
|
+
],
|
|
591
|
+
inertiaOrientation: explicitInertiaOrientation
|
|
592
|
+
};
|
|
593
|
+
const diagSum = Math.max(ixx + iyy + izz, 0);
|
|
594
|
+
if (Math.max(Math.abs(ixy), Math.abs(ixz), Math.abs(iyz)) <= 1e-12 * diagSum) return {
|
|
595
|
+
centerOfMass: com,
|
|
596
|
+
inertiaDiagonal: [
|
|
597
|
+
ixx,
|
|
598
|
+
iyy,
|
|
599
|
+
izz
|
|
600
|
+
],
|
|
601
|
+
inertiaOrientation: [
|
|
602
|
+
0,
|
|
603
|
+
0,
|
|
604
|
+
0,
|
|
605
|
+
1
|
|
606
|
+
]
|
|
607
|
+
};
|
|
608
|
+
const { inertia, inertiaFrame } = jacobiPrincipalInertia(ixx, iyy, izz, ixy, ixz, iyz);
|
|
609
|
+
return {
|
|
610
|
+
centerOfMass: com,
|
|
611
|
+
inertiaDiagonal: inertia,
|
|
612
|
+
inertiaOrientation: inertiaFrame
|
|
613
|
+
};
|
|
465
614
|
}
|
|
466
615
|
function computeAssemblyMassAndCom(bodies) {
|
|
467
616
|
let totalDynamicMass = 0, mx = 0, my = 0, mz = 0;
|
|
@@ -488,7 +637,6 @@ function computeAssemblyMassAndCom(bodies) {
|
|
|
488
637
|
]
|
|
489
638
|
};
|
|
490
639
|
}
|
|
491
|
-
var _one = new THREE.Vector3(1, 1, 1);
|
|
492
640
|
function bodyPoseMatrix(body, target = new THREE.Matrix4()) {
|
|
493
641
|
return target.compose(new THREE.Vector3().fromArray(body.localPosition), new THREE.Quaternion().fromArray(body.localQuaternion), _one);
|
|
494
642
|
}
|
|
@@ -591,6 +739,29 @@ function computeLumpedMassProps(bodies, invFrame) {
|
|
|
591
739
|
inertiaFrame
|
|
592
740
|
};
|
|
593
741
|
}
|
|
742
|
+
/**
|
|
743
|
+
* Applies exact `LumpedMassProps` directly in `RigidBody` local space (`density = 0` on colliders).
|
|
744
|
+
*/
|
|
745
|
+
function applyRapierBodyMass(rb, lumped) {
|
|
746
|
+
if (!lumped || !(lumped.mass > 0)) return;
|
|
747
|
+
const n = rb.numColliders();
|
|
748
|
+
for (let i = 0; i < n; i++) rb.collider(i).setDensity(0);
|
|
749
|
+
rb.setAdditionalMassProperties(lumped.mass, {
|
|
750
|
+
x: lumped.com[0],
|
|
751
|
+
y: lumped.com[1],
|
|
752
|
+
z: lumped.com[2]
|
|
753
|
+
}, {
|
|
754
|
+
x: lumped.inertia[0],
|
|
755
|
+
y: lumped.inertia[1],
|
|
756
|
+
z: lumped.inertia[2]
|
|
757
|
+
}, {
|
|
758
|
+
x: lumped.inertiaFrame[0],
|
|
759
|
+
y: lumped.inertiaFrame[1],
|
|
760
|
+
z: lumped.inertiaFrame[2],
|
|
761
|
+
w: lumped.inertiaFrame[3]
|
|
762
|
+
}, true);
|
|
763
|
+
rb.recomputeMassPropertiesFromColliders();
|
|
764
|
+
}
|
|
594
765
|
// src/clone.ts
|
|
595
766
|
var mountedScenes = /* @__PURE__ */ new WeakSet();
|
|
596
767
|
var clonedScenes = /* @__PURE__ */ new WeakSet();
|
|
@@ -654,6 +825,22 @@ function cloneCarTune(tune) {
|
|
|
654
825
|
};
|
|
655
826
|
}
|
|
656
827
|
/**
|
|
828
|
+
* Returns a structural `Object3D` clone with any mounted `RotorVisualSlot` / `MountSubtreeSlot`
|
|
829
|
+
* subtrees restored synchronously when `root` is currently mounted, or `root` itself otherwise.
|
|
830
|
+
*/
|
|
831
|
+
function snapshotSceneWithMountedSubtrees(root) {
|
|
832
|
+
const scopedSet = subtreeRestorersByScene.get(root);
|
|
833
|
+
if (!scopedSet || scopedSet.size === 0) return root;
|
|
834
|
+
const restorers = [...scopedSet];
|
|
835
|
+
const reattach = [];
|
|
836
|
+
for (let i = restorers.length - 1; i >= 0; i--) reattach.push(restorers[i]());
|
|
837
|
+
try {
|
|
838
|
+
return root.clone(true);
|
|
839
|
+
} finally {
|
|
840
|
+
for (let i = reattach.length - 1; i >= 0; i--) reattach[i]();
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
657
844
|
* Clones a parsed `KhrAssetBundle` in O(nodes) while sharing GPU `BufferGeometry` / `Material` buffers
|
|
658
845
|
* and Rapier `Float32Array` collider vertices. Only restores subtrees belonging to `bundle.scene`.
|
|
659
846
|
*/
|
|
@@ -722,6 +909,13 @@ function cloneKhrBundle(bundle) {
|
|
|
722
909
|
position: [...p.position],
|
|
723
910
|
...p.quaternion ? { quaternion: [...p.quaternion] } : {}
|
|
724
911
|
})),
|
|
912
|
+
emitters: bundle.emitters.map((e) => ({
|
|
913
|
+
...e,
|
|
914
|
+
...e.color ? { color: [...e.color] } : {},
|
|
915
|
+
position: [...e.position],
|
|
916
|
+
quaternion: [...e.quaternion],
|
|
917
|
+
object: remapNode(bundle.scene, e.object, scene)
|
|
918
|
+
})),
|
|
725
919
|
sockets,
|
|
726
920
|
totalDynamicMass: bundle.totalDynamicMass,
|
|
727
921
|
centerOfMass: [...bundle.centerOfMass],
|
|
@@ -879,7 +1073,9 @@ var COMBINE_RULES = {
|
|
|
879
1073
|
maximum: CoefficientCombineRule.Max,
|
|
880
1074
|
multiply: CoefficientCombineRule.Multiply
|
|
881
1075
|
};
|
|
882
|
-
|
|
1076
|
+
/** `RAPIER.TriMeshFlags.FIX_INTERNAL_EDGES` (144) — merges duplicate vertices and suppresses internal seam ghost normals. */
|
|
1077
|
+
var KHR_TRIMESH_FLAGS = 144;
|
|
1078
|
+
function KhrCollider({ c, position, quaternion, density, mass, massProperties, collisionGroups, solverGroups, ref }) {
|
|
883
1079
|
const innerRef = useRef(null);
|
|
884
1080
|
const setRef = useCallback((node) => {
|
|
885
1081
|
innerRef.current = node;
|
|
@@ -887,14 +1083,20 @@ function KhrCollider({ c, position, quaternion, massProperties, collisionGroups,
|
|
|
887
1083
|
}, [ref]);
|
|
888
1084
|
useEffect(() => {
|
|
889
1085
|
innerRef.current?.parent()?.recomputeMassPropertiesFromColliders();
|
|
890
|
-
}, [
|
|
1086
|
+
}, [
|
|
1087
|
+
c,
|
|
1088
|
+
density,
|
|
1089
|
+
mass,
|
|
1090
|
+
massProperties
|
|
1091
|
+
]);
|
|
891
1092
|
const effCollisionGroups = c.collisionGroups ?? collisionGroups;
|
|
892
1093
|
const effSolverGroups = solverGroups ?? effCollisionGroups;
|
|
1094
|
+
const massOption = massProperties ? { massProperties } : mass !== void 0 ? { mass } : { density: density ?? 0 };
|
|
893
1095
|
const common = {
|
|
894
1096
|
ref: setRef,
|
|
895
1097
|
position: position ?? c.localPosition,
|
|
896
1098
|
quaternion: quaternion ?? c.localQuaternion,
|
|
897
|
-
...
|
|
1099
|
+
...massOption,
|
|
898
1100
|
...c.friction === void 0 ? {} : { friction: c.friction },
|
|
899
1101
|
...c.restitution === void 0 ? {} : { restitution: c.restitution },
|
|
900
1102
|
...c.frictionCombine === void 0 ? {} : { frictionCombineRule: COMBINE_RULES[c.frictionCombine] },
|
|
@@ -931,7 +1133,11 @@ function KhrCollider({ c, position, quaternion, massProperties, collisionGroups,
|
|
|
931
1133
|
...common
|
|
932
1134
|
});
|
|
933
1135
|
return /* @__PURE__ */ jsx(TrimeshCollider, {
|
|
934
|
-
args: [
|
|
1136
|
+
args: [
|
|
1137
|
+
shape.vertices,
|
|
1138
|
+
shape.indices,
|
|
1139
|
+
144
|
|
1140
|
+
],
|
|
935
1141
|
...common
|
|
936
1142
|
});
|
|
937
1143
|
}
|
|
@@ -1110,558 +1316,24 @@ function buildJointRuntime(params) {
|
|
|
1110
1316
|
function openTargetForLimits(limits) {
|
|
1111
1317
|
return Math.abs(limits[0]) > Math.abs(limits[1]) ? limits[0] : limits[1];
|
|
1112
1318
|
}
|
|
1319
|
+
function closedTargetForLimits(limits) {
|
|
1320
|
+
return Math.min(limits[1], Math.max(limits[0], 0));
|
|
1321
|
+
}
|
|
1113
1322
|
function isJointOpen(rt) {
|
|
1114
1323
|
if (!rt.limits) return false;
|
|
1115
1324
|
const openTgt = openTargetForLimits(rt.limits);
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
const com = new THREE.Vector3().fromArray(lumped.com);
|
|
1121
|
-
const frame = new THREE.Quaternion().fromArray(lumped.inertiaFrame);
|
|
1122
|
-
if (colMat && colQuat) {
|
|
1123
|
-
com.applyMatrix4(colMat.clone().invert());
|
|
1124
|
-
frame.premultiply(colQuat.clone().invert()).normalize();
|
|
1125
|
-
}
|
|
1126
|
-
return {
|
|
1127
|
-
mass: lumped.mass,
|
|
1128
|
-
centerOfMass: {
|
|
1129
|
-
x: com.x,
|
|
1130
|
-
y: com.y,
|
|
1131
|
-
z: com.z
|
|
1132
|
-
},
|
|
1133
|
-
principalAngularInertia: {
|
|
1134
|
-
x: lumped.inertia[0],
|
|
1135
|
-
y: lumped.inertia[1],
|
|
1136
|
-
z: lumped.inertia[2]
|
|
1137
|
-
},
|
|
1138
|
-
angularInertiaLocalFrame: {
|
|
1139
|
-
x: frame.x,
|
|
1140
|
-
y: frame.y,
|
|
1141
|
-
z: frame.z,
|
|
1142
|
-
w: frame.w
|
|
1143
|
-
}
|
|
1144
|
-
};
|
|
1145
|
-
}
|
|
1146
|
-
function matrixToRoot(node, sceneRoot) {
|
|
1147
|
-
const out = new THREE.Matrix4();
|
|
1148
|
-
for (let cur = node; cur && cur !== sceneRoot; cur = cur.parent) {
|
|
1149
|
-
if (!cur.parent) throw new KhrValidationError(`Node "${node.name}" is detached from bundle.scene`);
|
|
1150
|
-
cur.updateMatrix();
|
|
1151
|
-
out.premultiply(cur.matrix);
|
|
1152
|
-
}
|
|
1153
|
-
return out;
|
|
1154
|
-
}
|
|
1155
|
-
function isSceneDescendant(node, ancestor) {
|
|
1156
|
-
for (let cur = node; cur; cur = cur.parent) if (cur === ancestor) return true;
|
|
1157
|
-
return false;
|
|
1158
|
-
}
|
|
1159
|
-
function lowestCommonAncestor(nodes, sceneRoot) {
|
|
1160
|
-
let candidate = nodes[0];
|
|
1161
|
-
while (candidate && candidate !== sceneRoot) {
|
|
1162
|
-
if (nodes.every((n) => isSceneDescendant(n, candidate))) return candidate;
|
|
1163
|
-
candidate = candidate.parent ?? sceneRoot;
|
|
1164
|
-
}
|
|
1165
|
-
return sceneRoot;
|
|
1166
|
-
}
|
|
1167
|
-
function planOwnColliders(body, lumped, frameQuat) {
|
|
1168
|
-
const one = new THREE.Vector3(1, 1, 1);
|
|
1169
|
-
const frameMat = frameQuat ? new THREE.Matrix4().makeRotationFromQuaternion(frameQuat) : null;
|
|
1170
|
-
const pos = new THREE.Vector3(), quat = new THREE.Quaternion(), scl = new THREE.Vector3();
|
|
1171
|
-
return body.colliders.map((spec, idx) => {
|
|
1172
|
-
const colQuat = new THREE.Quaternion().fromArray(spec.localQuaternion);
|
|
1173
|
-
const local = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(spec.localPosition), colQuat, one);
|
|
1174
|
-
if (frameMat) local.premultiply(frameMat);
|
|
1175
|
-
local.decompose(pos, quat, scl);
|
|
1176
|
-
quat.normalize();
|
|
1177
|
-
const planned = {
|
|
1178
|
-
spec,
|
|
1179
|
-
position: [
|
|
1180
|
-
pos.x,
|
|
1181
|
-
pos.y,
|
|
1182
|
-
pos.z
|
|
1183
|
-
],
|
|
1184
|
-
quaternion: [
|
|
1185
|
-
quat.x,
|
|
1186
|
-
quat.y,
|
|
1187
|
-
quat.z,
|
|
1188
|
-
quat.w
|
|
1189
|
-
]
|
|
1190
|
-
};
|
|
1191
|
-
if (idx === 0 && lumped) planned.massProperties = toColliderMass(lumped, local, quat);
|
|
1192
|
-
return planned;
|
|
1193
|
-
});
|
|
1194
|
-
}
|
|
1195
|
-
function planAssembly(bundle) {
|
|
1196
|
-
const byName = new Map(bundle.bodies.map((b) => [b.nodeName, b]));
|
|
1197
|
-
const rootMemo = /* @__PURE__ */ new Map();
|
|
1198
|
-
const depthMemo = /* @__PURE__ */ new Map();
|
|
1199
|
-
const resolveRootAndDepth = (b, visiting = /* @__PURE__ */ new Set()) => {
|
|
1200
|
-
const cachedRoot = rootMemo.get(b.nodeName);
|
|
1201
|
-
const cachedDepth = depthMemo.get(b.nodeName);
|
|
1202
|
-
if (cachedRoot && cachedDepth !== void 0) return {
|
|
1203
|
-
root: cachedRoot,
|
|
1204
|
-
depth: cachedDepth
|
|
1205
|
-
};
|
|
1206
|
-
if (!b.joint) {
|
|
1207
|
-
rootMemo.set(b.nodeName, b);
|
|
1208
|
-
depthMemo.set(b.nodeName, 0);
|
|
1209
|
-
return {
|
|
1210
|
-
root: b,
|
|
1211
|
-
depth: 0
|
|
1212
|
-
};
|
|
1213
|
-
}
|
|
1214
|
-
if (visiting.has(b.nodeName)) throw new KhrValidationError(`Joint cycle through "${b.nodeName}"`);
|
|
1215
|
-
visiting.add(b.nodeName);
|
|
1216
|
-
const parent = byName.get(b.joint.connectedNodeName);
|
|
1217
|
-
if (!parent) throw new KhrValidationError(`Joint on "${b.nodeName}" targets "${b.joint.connectedNodeName}", which is not a KHR body`);
|
|
1218
|
-
const up = resolveRootAndDepth(parent, visiting);
|
|
1219
|
-
rootMemo.set(b.nodeName, up.root);
|
|
1220
|
-
depthMemo.set(b.nodeName, up.depth + 1);
|
|
1221
|
-
return {
|
|
1222
|
-
root: up.root,
|
|
1223
|
-
depth: up.depth + 1
|
|
1224
|
-
};
|
|
1225
|
-
};
|
|
1226
|
-
for (const b of bundle.bodies) resolveRootAndDepth(b);
|
|
1227
|
-
const plans = bundle.bodies.filter((b) => !b.joint).map((root) => {
|
|
1228
|
-
const invRoot = bodyPoseMatrix(root).clone().invert();
|
|
1229
|
-
const descendants = bundle.bodies.filter((b) => b.joint && rootMemo.get(b.nodeName) === root).sort((a, b) => (depthMemo.get(a.nodeName) ?? 0) - (depthMemo.get(b.nodeName) ?? 0));
|
|
1230
|
-
const partByName = /* @__PURE__ */ new Map();
|
|
1231
|
-
const parts = descendants.map((b) => {
|
|
1232
|
-
const joint = b.joint;
|
|
1233
|
-
const parent = partByName.get(joint.connectedNodeName) ?? null;
|
|
1234
|
-
if (parent && !isSceneDescendant(b.object, parent.spec.object)) throw new KhrValidationError(`Jointed node "${b.nodeName}" must be a scene-graph descendant of its jointed parent "${joint.connectedNodeName}"`);
|
|
1235
|
-
const relParentMat = bodyPoseMatrix(parent ? parent.spec : root).clone().invert().multiply(bodyPoseMatrix(b));
|
|
1236
|
-
const relRootMat = invRoot.clone().multiply(bodyPoseMatrix(b));
|
|
1237
|
-
const rpPos = new THREE.Vector3(), tmpQ = new THREE.Quaternion(), rpScl = new THREE.Vector3();
|
|
1238
|
-
relParentMat.decompose(rpPos, tmpQ, rpScl);
|
|
1239
|
-
const rrPos = new THREE.Vector3(), rrQuat = new THREE.Quaternion();
|
|
1240
|
-
relRootMat.decompose(rrPos, rrQuat, rpScl);
|
|
1241
|
-
rrQuat.normalize();
|
|
1242
|
-
const axChildVec = new THREE.Vector3().fromArray(joint.axis).normalize();
|
|
1243
|
-
const axParentVec = axChildVec.clone().applyQuaternion(rrQuat).normalize();
|
|
1244
|
-
const anchorParent = parent ? [
|
|
1245
|
-
rrPos.x - parent.restRelRootPos[0],
|
|
1246
|
-
rrPos.y - parent.restRelRootPos[1],
|
|
1247
|
-
rrPos.z - parent.restRelRootPos[2]
|
|
1248
|
-
] : [
|
|
1249
|
-
rpPos.x,
|
|
1250
|
-
rpPos.y,
|
|
1251
|
-
rpPos.z
|
|
1252
|
-
];
|
|
1253
|
-
const bodyAlignInv = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(b.localPosition), new THREE.Quaternion().fromArray(root.localQuaternion), new THREE.Vector3(1, 1, 1)).invert();
|
|
1254
|
-
const ownLumped = b.isKinematic ? null : computeLumpedMassProps([b], bodyAlignInv);
|
|
1255
|
-
const colliders = planOwnColliders(b, ownLumped, rrQuat);
|
|
1256
|
-
const partMass = ownLumped && colliders.length === 0 ? toColliderMass(ownLumped) : void 0;
|
|
1257
|
-
const plan = {
|
|
1258
|
-
spec: b,
|
|
1259
|
-
joint,
|
|
1260
|
-
parent,
|
|
1261
|
-
restRelParentPos: anchorParent,
|
|
1262
|
-
restRelParentQuat: [
|
|
1263
|
-
rrQuat.x,
|
|
1264
|
-
rrQuat.y,
|
|
1265
|
-
rrQuat.z,
|
|
1266
|
-
rrQuat.w
|
|
1267
|
-
],
|
|
1268
|
-
restRelRootPos: [
|
|
1269
|
-
rrPos.x,
|
|
1270
|
-
rrPos.y,
|
|
1271
|
-
rrPos.z
|
|
1272
|
-
],
|
|
1273
|
-
restRelRootQuat: [
|
|
1274
|
-
rrQuat.x,
|
|
1275
|
-
rrQuat.y,
|
|
1276
|
-
rrQuat.z,
|
|
1277
|
-
rrQuat.w
|
|
1278
|
-
],
|
|
1279
|
-
axisParent: [
|
|
1280
|
-
axParentVec.x,
|
|
1281
|
-
axParentVec.y,
|
|
1282
|
-
axParentVec.z
|
|
1283
|
-
],
|
|
1284
|
-
axisChild: [
|
|
1285
|
-
axChildVec.x,
|
|
1286
|
-
axChildVec.y,
|
|
1287
|
-
axChildVec.z
|
|
1288
|
-
],
|
|
1289
|
-
...partMass ? { partMass } : {},
|
|
1290
|
-
colliders
|
|
1291
|
-
};
|
|
1292
|
-
partByName.set(b.nodeName, plan);
|
|
1293
|
-
return plan;
|
|
1294
|
-
});
|
|
1295
|
-
const rootLumped = root.isKinematic ? null : computeLumpedMassProps([root], invRoot);
|
|
1296
|
-
const colliders = planOwnColliders(root, rootLumped);
|
|
1297
|
-
const rootMass = rootLumped && colliders.length === 0 ? toColliderMass(rootLumped) : void 0;
|
|
1298
|
-
const mountNode = lowestCommonAncestor([root.object, ...descendants.map((d) => d.object)], bundle.scene);
|
|
1299
|
-
const mountInRoot = invRoot.clone().multiply(matrixToRoot(mountNode, bundle.scene));
|
|
1300
|
-
const scenePos = new THREE.Vector3(), sceneQuat = new THREE.Quaternion(), sceneScl = new THREE.Vector3();
|
|
1301
|
-
invRoot.decompose(scenePos, sceneQuat, sceneScl);
|
|
1302
|
-
sceneQuat.normalize();
|
|
1303
|
-
const mountPos = new THREE.Vector3(), mountQuat = new THREE.Quaternion();
|
|
1304
|
-
mountInRoot.decompose(mountPos, mountQuat, sceneScl);
|
|
1305
|
-
mountQuat.normalize();
|
|
1306
|
-
return {
|
|
1307
|
-
spec: root,
|
|
1308
|
-
sceneInRootPos: [
|
|
1309
|
-
scenePos.x,
|
|
1310
|
-
scenePos.y,
|
|
1311
|
-
scenePos.z
|
|
1312
|
-
],
|
|
1313
|
-
sceneInRootQuat: [
|
|
1314
|
-
sceneQuat.x,
|
|
1315
|
-
sceneQuat.y,
|
|
1316
|
-
sceneQuat.z,
|
|
1317
|
-
sceneQuat.w
|
|
1318
|
-
],
|
|
1319
|
-
mountNode,
|
|
1320
|
-
mountPosition: [
|
|
1321
|
-
mountPos.x,
|
|
1322
|
-
mountPos.y,
|
|
1323
|
-
mountPos.z
|
|
1324
|
-
],
|
|
1325
|
-
mountQuaternion: [
|
|
1326
|
-
mountQuat.x,
|
|
1327
|
-
mountQuat.y,
|
|
1328
|
-
mountQuat.z,
|
|
1329
|
-
mountQuat.w
|
|
1330
|
-
],
|
|
1331
|
-
...rootMass ? { rootMass } : {},
|
|
1332
|
-
colliders,
|
|
1333
|
-
parts
|
|
1334
|
-
};
|
|
1335
|
-
});
|
|
1336
|
-
for (const a of plans) for (const b of plans) if (a !== b && isSceneDescendant(b.mountNode, a.mountNode)) throw new KhrValidationError(`Root bodies "${a.spec.nodeName}" and "${b.spec.nodeName}" share scene subtree "${a.mountNode.name}"; split them into separate glTF branches`);
|
|
1337
|
-
return plans;
|
|
1338
|
-
}
|
|
1339
|
-
function ArticulatedPartBody({ part, rootSpec, rootPos, rootQuat, parentBody, origin, onBody, onJointReady }) {
|
|
1340
|
-
const { rapier, world } = useRapier();
|
|
1341
|
-
const registry = useContext(JointRegistryContext);
|
|
1342
|
-
const childRef = useRef(null);
|
|
1343
|
-
const rtRef = useRef(null);
|
|
1344
|
-
const [childBody, setChildBody] = React.useState(null);
|
|
1345
|
-
const worldPose = useMemo(() => {
|
|
1346
|
-
const rq = new THREE.Quaternion(rootQuat[0], rootQuat[1], rootQuat[2], rootQuat[3]).normalize();
|
|
1347
|
-
const pos = new THREE.Vector3().fromArray(part.restRelRootPos).applyQuaternion(rq).add(new THREE.Vector3(rootPos[0], rootPos[1], rootPos[2]));
|
|
1348
|
-
return {
|
|
1349
|
-
pos: [
|
|
1350
|
-
pos.x,
|
|
1351
|
-
pos.y,
|
|
1352
|
-
pos.z
|
|
1353
|
-
],
|
|
1354
|
-
quat: [
|
|
1355
|
-
rq.x,
|
|
1356
|
-
rq.y,
|
|
1357
|
-
rq.z,
|
|
1358
|
-
rq.w
|
|
1359
|
-
]
|
|
1360
|
-
};
|
|
1361
|
-
}, [
|
|
1362
|
-
part.restRelRootPos,
|
|
1363
|
-
rootPos,
|
|
1364
|
-
rootQuat
|
|
1365
|
-
]);
|
|
1366
|
-
const assignChildRef = useCallback((rb) => {
|
|
1367
|
-
childRef.current = rb;
|
|
1368
|
-
setChildBody(rb);
|
|
1369
|
-
onBody(part.spec.nodeName, rb);
|
|
1370
|
-
}, [part.spec.nodeName, onBody]);
|
|
1371
|
-
useEffect(() => () => onBody(part.spec.nodeName, null), [part.spec.nodeName, onBody]);
|
|
1372
|
-
useEffect(() => {
|
|
1373
|
-
const pm = part.partMass;
|
|
1374
|
-
if (childBody && pm) {
|
|
1375
|
-
childBody.setAdditionalMassProperties(pm.mass, pm.centerOfMass, pm.principalAngularInertia, pm.angularInertiaLocalFrame, true);
|
|
1376
|
-
childBody.recomputeMassPropertiesFromColliders();
|
|
1377
|
-
}
|
|
1378
|
-
}, [childBody, part.partMass]);
|
|
1379
|
-
useEffect(() => {
|
|
1380
|
-
if (!parentBody || !childBody) return;
|
|
1381
|
-
const imp = createRapierImpulseJoint(rapier, world, parentBody, childBody, part.joint, part.restRelParentPos, [
|
|
1382
|
-
0,
|
|
1383
|
-
0,
|
|
1384
|
-
0
|
|
1385
|
-
], part.axisParent, part.axisChild, part.restRelParentQuat);
|
|
1386
|
-
const rt = buildJointRuntime({
|
|
1387
|
-
rapier,
|
|
1388
|
-
world,
|
|
1389
|
-
nodeName: part.spec.nodeName,
|
|
1390
|
-
role: part.joint.role,
|
|
1391
|
-
kind: part.joint.kind,
|
|
1392
|
-
object: part.spec.object,
|
|
1393
|
-
axisParent: part.axisParent,
|
|
1394
|
-
anchorParent: part.restRelParentPos,
|
|
1395
|
-
limits: part.joint.limits,
|
|
1396
|
-
drive: part.joint.drive,
|
|
1397
|
-
breakaway: part.spec.breakaway,
|
|
1398
|
-
rapierJoint: imp,
|
|
1399
|
-
parentBody,
|
|
1400
|
-
childBody
|
|
1401
|
-
});
|
|
1402
|
-
rtRef.current = rt;
|
|
1403
|
-
registry?.add(rt);
|
|
1404
|
-
onJointReady(rt.nodeName, part.spec.object, rt);
|
|
1405
|
-
return () => {
|
|
1406
|
-
rtRef.current = null;
|
|
1407
|
-
registry?.delete(rt);
|
|
1408
|
-
onJointReady(rt.nodeName, part.spec.object, null);
|
|
1409
|
-
if (rt.rapierJoint && world.getImpulseJoint(rt.rapierJoint.handle)) world.removeImpulseJoint(rt.rapierJoint, true);
|
|
1410
|
-
};
|
|
1411
|
-
}, [
|
|
1412
|
-
rapier,
|
|
1413
|
-
world,
|
|
1414
|
-
part,
|
|
1415
|
-
parentBody,
|
|
1416
|
-
childBody,
|
|
1417
|
-
registry,
|
|
1418
|
-
onJointReady
|
|
1419
|
-
]);
|
|
1420
|
-
const onForce = useMemo(() => part.spec.breakaway ? (p) => {
|
|
1421
|
-
if (world.timestep > 0) rtRef.current?.applyImpactImpulse(p.totalForceMagnitude * world.timestep);
|
|
1422
|
-
} : void 0, [part.spec.breakaway, world]);
|
|
1423
|
-
const linVel = part.spec.linearVelocity ?? rootSpec.linearVelocity;
|
|
1424
|
-
const angVel = part.spec.angularVelocity ?? rootSpec.angularVelocity;
|
|
1425
|
-
const gravScale = part.spec.gravityFactor ?? rootSpec.gravityFactor;
|
|
1426
|
-
return /* @__PURE__ */ jsxs(RigidBody, {
|
|
1427
|
-
ref: assignChildRef,
|
|
1428
|
-
type: part.spec.isKinematic ? "kinematicPosition" : "dynamic",
|
|
1429
|
-
position: worldPose.pos,
|
|
1430
|
-
quaternion: worldPose.quat,
|
|
1431
|
-
colliders: false,
|
|
1432
|
-
onContactForce: onForce,
|
|
1433
|
-
...linVel ? { linearVelocity: linVel } : {},
|
|
1434
|
-
...angVel ? { angularVelocity: angVel } : {},
|
|
1435
|
-
...gravScale === void 0 ? {} : { gravityScale: gravScale },
|
|
1436
|
-
children: [/* @__PURE__ */ jsx(MountSubtreeSlot, {
|
|
1437
|
-
object: part.spec.object,
|
|
1438
|
-
position: [
|
|
1439
|
-
0,
|
|
1440
|
-
0,
|
|
1441
|
-
0
|
|
1442
|
-
],
|
|
1443
|
-
quaternion: part.restRelRootQuat,
|
|
1444
|
-
origin
|
|
1445
|
-
}), part.colliders.map((col, cIdx) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
1446
|
-
c: col.spec,
|
|
1447
|
-
position: col.position,
|
|
1448
|
-
quaternion: col.quaternion,
|
|
1449
|
-
massProperties: col.massProperties
|
|
1450
|
-
}, `${part.spec.nodeName}:${cIdx}`))]
|
|
1451
|
-
});
|
|
1325
|
+
const closedTgt = closedTargetForLimits(rt.limits);
|
|
1326
|
+
if (Math.abs(openTgt - closedTgt) <= 1e-6) return false;
|
|
1327
|
+
const cur = rt.positionTarget ?? rt.getCoordinate();
|
|
1328
|
+
return Math.abs(cur - openTgt) < Math.abs(cur - closedTgt);
|
|
1452
1329
|
}
|
|
1453
|
-
function
|
|
1454
|
-
|
|
1455
|
-
const
|
|
1456
|
-
const
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
setRootBody(rb);
|
|
1460
|
-
onBody(rootSpec.nodeName, rb);
|
|
1461
|
-
}, [rootSpec.nodeName, onBody]);
|
|
1462
|
-
useEffect(() => () => onBody(rootSpec.nodeName, null), [rootSpec.nodeName, onBody]);
|
|
1463
|
-
useEffect(() => {
|
|
1464
|
-
const rm = plan.rootMass;
|
|
1465
|
-
if (rootBody && rm) {
|
|
1466
|
-
rootBody.setAdditionalMassProperties(rm.mass, rm.centerOfMass, rm.principalAngularInertia, rm.angularInertiaLocalFrame, true);
|
|
1467
|
-
rootBody.recomputeMassPropertiesFromColliders();
|
|
1468
|
-
}
|
|
1469
|
-
}, [rootBody, plan.rootMass]);
|
|
1470
|
-
const handlePartBody = useCallback((nodeName, rb) => {
|
|
1471
|
-
setPartBodies((prev) => {
|
|
1472
|
-
if ((prev.get(nodeName) ?? null) === rb) return prev;
|
|
1473
|
-
const next = new Map(prev);
|
|
1474
|
-
if (rb) next.set(nodeName, rb);
|
|
1475
|
-
else next.delete(nodeName);
|
|
1476
|
-
return next;
|
|
1477
|
-
});
|
|
1478
|
-
onBody(nodeName, rb);
|
|
1479
|
-
}, [onBody]);
|
|
1480
|
-
const bodyType = rootSpec.isKinematic ? fixedRoot ? "fixed" : "kinematicPosition" : "dynamic";
|
|
1481
|
-
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs(RigidBody, {
|
|
1482
|
-
...rigidBodyProps,
|
|
1483
|
-
ref: assignRootRef,
|
|
1484
|
-
type: bodyType,
|
|
1485
|
-
position,
|
|
1486
|
-
quaternion,
|
|
1487
|
-
colliders: false,
|
|
1488
|
-
...rootSpec.linearVelocity ? { linearVelocity: rootSpec.linearVelocity } : {},
|
|
1489
|
-
...rootSpec.angularVelocity ? { angularVelocity: rootSpec.angularVelocity } : {},
|
|
1490
|
-
...rootSpec.gravityFactor === void 0 ? {} : { gravityScale: rootSpec.gravityFactor },
|
|
1491
|
-
children: [children, plan.colliders.map((col, idx) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
1492
|
-
c: col.spec,
|
|
1493
|
-
position: col.position,
|
|
1494
|
-
quaternion: col.quaternion,
|
|
1495
|
-
massProperties: col.massProperties
|
|
1496
|
-
}, `r${idx}`))]
|
|
1497
|
-
}), plan.parts.map((part) => /* @__PURE__ */ jsx(ArticulatedPartBody, {
|
|
1498
|
-
part,
|
|
1499
|
-
rootSpec,
|
|
1500
|
-
rootPos: position,
|
|
1501
|
-
rootQuat: quaternion,
|
|
1502
|
-
parentBody: part.parent ? partBodies.get(part.parent.spec.nodeName) ?? null : rootBody,
|
|
1503
|
-
origin: partOrigins?.get(part.spec.object),
|
|
1504
|
-
onBody: handlePartBody,
|
|
1505
|
-
onJointReady
|
|
1506
|
-
}, part.spec.nodeName))] });
|
|
1507
|
-
}
|
|
1508
|
-
function KhrAssembly({ ref, bundle, position = [
|
|
1509
|
-
0,
|
|
1510
|
-
0,
|
|
1511
|
-
0
|
|
1512
|
-
], rotation, quaternion, scale, fixedRoot = false, enableCustomGravity = false, rigidBodyProps, children, ...groupProps }) {
|
|
1513
|
-
assertUnitScale(scale, "KhrAssembly");
|
|
1514
|
-
useMemo(() => assertSceneIdentityWorldScale(bundle.scene), [bundle]);
|
|
1515
|
-
const { world } = useRapier();
|
|
1516
|
-
const applyGravityField = useCustomGravity((s) => s.applyGravityField);
|
|
1517
|
-
const plans = useMemo(() => planAssembly(bundle), [bundle]);
|
|
1518
|
-
const origins = useMemo(() => plans.length > 1 ? captureSubtreeOrigins(plans.map((p) => p.mountNode), bundle.scene) : null, [plans, bundle.scene]);
|
|
1519
|
-
const [px, py, pz] = position;
|
|
1520
|
-
const [rx, ry, rz] = rotation ?? [
|
|
1521
|
-
0,
|
|
1522
|
-
0,
|
|
1523
|
-
0
|
|
1524
|
-
];
|
|
1525
|
-
const [qx, qy, qz, qw] = quaternion ?? [
|
|
1526
|
-
NaN,
|
|
1527
|
-
NaN,
|
|
1528
|
-
NaN,
|
|
1529
|
-
NaN
|
|
1530
|
-
];
|
|
1531
|
-
const spawnQuat = useMemo(() => quaternion ? new THREE.Quaternion(qx, qy, qz, qw) : new THREE.Quaternion().setFromEuler(new THREE.Euler(rx, ry, rz)), [
|
|
1532
|
-
qx,
|
|
1533
|
-
qy,
|
|
1534
|
-
qz,
|
|
1535
|
-
qw,
|
|
1536
|
-
rx,
|
|
1537
|
-
ry,
|
|
1538
|
-
rz,
|
|
1539
|
-
Boolean(quaternion)
|
|
1540
|
-
]);
|
|
1541
|
-
const rootPoses = useMemo(() => plans.map((p) => {
|
|
1542
|
-
const pos = new THREE.Vector3().fromArray(p.spec.localPosition).applyQuaternion(spawnQuat).add(new THREE.Vector3(px, py, pz));
|
|
1543
|
-
const quat = spawnQuat.clone().multiply(new THREE.Quaternion().fromArray(p.spec.localQuaternion));
|
|
1544
|
-
return {
|
|
1545
|
-
position: [
|
|
1546
|
-
pos.x,
|
|
1547
|
-
pos.y,
|
|
1548
|
-
pos.z
|
|
1549
|
-
],
|
|
1550
|
-
quaternion: [
|
|
1551
|
-
quat.x,
|
|
1552
|
-
quat.y,
|
|
1553
|
-
quat.z,
|
|
1554
|
-
quat.w
|
|
1555
|
-
]
|
|
1556
|
-
};
|
|
1557
|
-
}), [
|
|
1558
|
-
plans,
|
|
1559
|
-
spawnQuat,
|
|
1560
|
-
px,
|
|
1561
|
-
py,
|
|
1562
|
-
pz
|
|
1563
|
-
]);
|
|
1564
|
-
const bodiesRef = useRef(/* @__PURE__ */ new Map());
|
|
1565
|
-
const jointsRef = useRef(/* @__PURE__ */ new Map());
|
|
1566
|
-
const byObjectRef = useRef(/* @__PURE__ */ new Map());
|
|
1567
|
-
const handleBody = useCallback((nodeName, rb) => {
|
|
1568
|
-
if (rb) bodiesRef.current.set(nodeName, rb);
|
|
1569
|
-
else bodiesRef.current.delete(nodeName);
|
|
1570
|
-
}, []);
|
|
1571
|
-
const handleJointReady = useCallback((nodeName, obj, rt) => {
|
|
1572
|
-
if (rt) {
|
|
1573
|
-
jointsRef.current.set(nodeName, rt);
|
|
1574
|
-
byObjectRef.current.set(obj, rt);
|
|
1575
|
-
} else {
|
|
1576
|
-
jointsRef.current.delete(nodeName);
|
|
1577
|
-
byObjectRef.current.delete(obj);
|
|
1578
|
-
}
|
|
1579
|
-
}, []);
|
|
1580
|
-
useFrame(() => {
|
|
1581
|
-
if (!enableCustomGravity || world.timestep <= 0) return;
|
|
1582
|
-
for (const rb of bodiesRef.current.values()) if (rb.isDynamic()) applyGravityField(rb, world.timestep);
|
|
1583
|
-
});
|
|
1584
|
-
useImperativeHandle(ref, () => ({
|
|
1585
|
-
get jointNames() {
|
|
1586
|
-
return [...jointsRef.current.keys()];
|
|
1587
|
-
},
|
|
1588
|
-
getBody: (nodeName) => bodiesRef.current.get(nodeName) ?? null,
|
|
1589
|
-
getJointRuntime: (nodeName) => jointsRef.current.get(nodeName) ?? null,
|
|
1590
|
-
setJointTarget: (nodeName, target) => {
|
|
1591
|
-
jointsRef.current.get(nodeName)?.applyTarget(target);
|
|
1592
|
-
},
|
|
1593
|
-
setRoleTarget: (role, target) => {
|
|
1594
|
-
for (const rt of jointsRef.current.values()) if (rt.role === role) rt.applyTarget(target);
|
|
1595
|
-
},
|
|
1596
|
-
toggleHitObject: (hit) => {
|
|
1597
|
-
for (let cur = hit; cur; cur = cur.parent) {
|
|
1598
|
-
const rt = byObjectRef.current.get(cur);
|
|
1599
|
-
if (rt && rt.drive?.stiffness !== void 0) {
|
|
1600
|
-
const limits = rt.limits ?? [0, Math.PI / 2];
|
|
1601
|
-
const nextTgt = isJointOpen(rt) ? Math.min(limits[1], Math.max(limits[0], 0)) : openTargetForLimits(limits);
|
|
1602
|
-
rt.applyTarget(nextTgt);
|
|
1603
|
-
return true;
|
|
1604
|
-
}
|
|
1605
|
-
}
|
|
1606
|
-
return false;
|
|
1607
|
-
}
|
|
1608
|
-
}), []);
|
|
1609
|
-
if (plans.length === 1) {
|
|
1610
|
-
const plan = plans[0];
|
|
1611
|
-
return /* @__PURE__ */ jsx("group", {
|
|
1612
|
-
...groupProps,
|
|
1613
|
-
children: /* @__PURE__ */ jsx(AssemblyRootBody, {
|
|
1614
|
-
plan,
|
|
1615
|
-
bundleScene: bundle.scene,
|
|
1616
|
-
position: rootPoses[0].position,
|
|
1617
|
-
quaternion: rootPoses[0].quaternion,
|
|
1618
|
-
fixedRoot,
|
|
1619
|
-
rigidBodyProps,
|
|
1620
|
-
onBody: handleBody,
|
|
1621
|
-
onJointReady: handleJointReady,
|
|
1622
|
-
children: /* @__PURE__ */ jsxs("group", {
|
|
1623
|
-
position: plan.sceneInRootPos,
|
|
1624
|
-
quaternion: plan.sceneInRootQuat,
|
|
1625
|
-
children: [/* @__PURE__ */ jsx(BundleSceneHost, {
|
|
1626
|
-
bundle,
|
|
1627
|
-
owner: "KhrAssembly"
|
|
1628
|
-
}), children]
|
|
1629
|
-
})
|
|
1630
|
-
}, `${bundle.scene.uuid}:${plan.spec.nodeName}`)
|
|
1631
|
-
});
|
|
1632
|
-
}
|
|
1633
|
-
return /* @__PURE__ */ jsxs("group", {
|
|
1634
|
-
...groupProps,
|
|
1635
|
-
children: [/* @__PURE__ */ jsxs("group", {
|
|
1636
|
-
position,
|
|
1637
|
-
quaternion: [
|
|
1638
|
-
spawnQuat.x,
|
|
1639
|
-
spawnQuat.y,
|
|
1640
|
-
spawnQuat.z,
|
|
1641
|
-
spawnQuat.w
|
|
1642
|
-
],
|
|
1643
|
-
children: [/* @__PURE__ */ jsx(BundleSceneHost, {
|
|
1644
|
-
bundle,
|
|
1645
|
-
owner: "KhrAssembly"
|
|
1646
|
-
}), children]
|
|
1647
|
-
}), plans.map((plan, idx) => /* @__PURE__ */ jsx(AssemblyRootBody, {
|
|
1648
|
-
plan,
|
|
1649
|
-
bundleScene: bundle.scene,
|
|
1650
|
-
position: rootPoses[idx].position,
|
|
1651
|
-
quaternion: rootPoses[idx].quaternion,
|
|
1652
|
-
fixedRoot,
|
|
1653
|
-
rigidBodyProps,
|
|
1654
|
-
onBody: handleBody,
|
|
1655
|
-
onJointReady: handleJointReady,
|
|
1656
|
-
children: /* @__PURE__ */ jsx(MountSubtreeSlot, {
|
|
1657
|
-
object: plan.mountNode,
|
|
1658
|
-
position: plan.mountPosition,
|
|
1659
|
-
quaternion: plan.mountQuaternion,
|
|
1660
|
-
origin: origins?.get(plan.mountNode)
|
|
1661
|
-
})
|
|
1662
|
-
}, `${bundle.scene.uuid}:${plan.spec.nodeName}`))]
|
|
1663
|
-
});
|
|
1330
|
+
function toggleDrivenJoint(rt) {
|
|
1331
|
+
if (rt.detached || rt.drive?.stiffness === void 0) return false;
|
|
1332
|
+
const limits = rt.limits ?? [0, Math.PI / 2];
|
|
1333
|
+
const nextTarget = isJointOpen(rt) ? closedTargetForLimits(limits) : openTargetForLimits(limits);
|
|
1334
|
+
rt.applyTarget(nextTarget);
|
|
1335
|
+
return true;
|
|
1664
1336
|
}
|
|
1665
|
-
export {
|
|
1337
|
+
export { computeRelativePose as A, assignRef as B, applyRapierBodyMass as C, bodyPoseMatrix as D, assertUnitScale as E, readUnitQuat as F, optFiniteNum as G, isNonNegNum as H, resolveBodyMassFromColliders as I, COMBINE_MODES as L, extractMeshColliderShape as M, jacobiPrincipalInertia as N, computeAssemblyMassAndCom as O, readFiniteVec3 as P, JointRegistryContext as R, snapshotSceneWithMountedSubtrees as S, assertSceneIdentityWorldScale as T, isObj as U, isFiniteNum as V, isPosNum as W, cloneKhrBundle as _, configureJointDrive as a, releaseBundleMount as b, measureJointCoordinate as c, KHR_TRIMESH_FLAGS as d, KhrCollider as f, claimBundleMount as g, captureSubtreeOrigins as h, closedTargetForLimits as i, computeShapeInertia as j, computeLumpedMassProps as k, openTargetForLimits as l, MountSubtreeSlot as m, DETACHED_PANEL_INTERACTION_GROUPS as n, createRapierImpulseJoint as o, BundleSceneHost as p, buildJointRuntime as r, isJointOpen as s, ATTACHED_PANEL_INTERACTION_GROUPS as t, toggleDrivenJoint as u, disposeKhrBundle as v, assertIdentityWorldScale as w, restoreSubtreeOrigin as x, registerMountedSubtree as y, KhrValidationError as z };
|
|
1666
1338
|
|
|
1667
|
-
//# sourceMappingURL=
|
|
1339
|
+
//# sourceMappingURL=joints-H3uae8pm.js.map
|