@margoengine/ragdoll 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +381 -3
- package/dist/balance.d.ts +195 -0
- package/dist/balance.js +468 -0
- package/dist/getup.d.ts +86 -0
- package/dist/getup.js +135 -0
- package/dist/hulls.d.ts +71 -0
- package/dist/hulls.js +173 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +741 -0
- package/dist/limits-CCe6Pdej.js +55 -0
- package/dist/limits.d.ts +12 -0
- package/dist/powered.d.ts +145 -0
- package/dist/powered.js +245 -0
- package/dist/profile.d.ts +62 -0
- package/dist/profile.js +267 -0
- package/dist/ragdoll.d.ts +276 -0
- package/package.json +82 -6
package/dist/getup.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { Mesh } from '@margoengine/core/extension';
|
|
3
|
+
// @ts-ignore optional peer: its types apply when it is installed
|
|
4
|
+
import type { Animator } from '@margoengine/animator';
|
|
5
|
+
import type { Ragdoll } from './ragdoll.js';
|
|
6
|
+
/** Options of {@link addGetUp}. */
|
|
7
|
+
export interface GetUpOptions {
|
|
8
|
+
/** Animator state of the clip that gets up from lying on the back. */
|
|
9
|
+
faceUp: string;
|
|
10
|
+
/** Animator state of the clip that gets up from lying on the front. */
|
|
11
|
+
faceDown: string;
|
|
12
|
+
/** Animator state to go to when the clip ends. Default the state the animator is in when this is added. */
|
|
13
|
+
then?: string;
|
|
14
|
+
/** Seconds of the blend from the ragdoll's pose into the clip. Default 0.4. */
|
|
15
|
+
blend?: number;
|
|
16
|
+
/** Seconds of the fade from the end of the clip into `then`. Default 0.2. */
|
|
17
|
+
fade?: number;
|
|
18
|
+
/** The top node of the character, moved and turned to the body. Default the animator's model root. */
|
|
19
|
+
root?: Mesh;
|
|
20
|
+
/** World up, a unit vector. Default [0, 1, 0]. */
|
|
21
|
+
up?: readonly [number, number, number];
|
|
22
|
+
}
|
|
23
|
+
/** Which way up a body lies. */
|
|
24
|
+
export type GetUpSide = 'faceUp' | 'faceDown';
|
|
25
|
+
/** Options of {@link GetUp.start}. */
|
|
26
|
+
export interface GetUpStartOptions {
|
|
27
|
+
/** Force a clip instead of choosing by how the body lies. */
|
|
28
|
+
side?: GetUpSide;
|
|
29
|
+
/** Stops waiting: the promise rejects with the reason; the clip keeps playing. */
|
|
30
|
+
signal?: AbortSignal;
|
|
31
|
+
}
|
|
32
|
+
/** What {@link GetUp.start} did. */
|
|
33
|
+
export interface GetUpResult {
|
|
34
|
+
/** The clip chosen. */
|
|
35
|
+
side: GetUpSide;
|
|
36
|
+
/** Radians between the body and the clip's first frame, after the turn: how much the blend has to cover at the root. */
|
|
37
|
+
mismatch: number;
|
|
38
|
+
}
|
|
39
|
+
/** A character getting up ({@link addGetUp}). */
|
|
40
|
+
export declare class GetUp {
|
|
41
|
+
private readonly ragdoll;
|
|
42
|
+
private readonly animator;
|
|
43
|
+
private readonly root;
|
|
44
|
+
private readonly up;
|
|
45
|
+
private readonly then;
|
|
46
|
+
private readonly blend;
|
|
47
|
+
private readonly fade;
|
|
48
|
+
private readonly first;
|
|
49
|
+
private readonly states;
|
|
50
|
+
private watching?;
|
|
51
|
+
private disposed;
|
|
52
|
+
/** @internal use {@link addGetUp}. */
|
|
53
|
+
constructor(ragdoll: Ragdoll, animator: Animator, options: GetUpOptions);
|
|
54
|
+
/**
|
|
55
|
+
* The root bone at the first frame of `side`'s clip, in the top node's frame. The animator is the sampler, so layers
|
|
56
|
+
* and blend trees count; it is put back as it was, and a `statechange` listener sees the visit.
|
|
57
|
+
*/
|
|
58
|
+
private sample;
|
|
59
|
+
/** The turn that best fits `side`'s first frame to the body, and what is left over. */
|
|
60
|
+
private fit;
|
|
61
|
+
/** Radians between the body and `side`'s first frame at the root bone, after the free turn. */
|
|
62
|
+
mismatch(side: GetUpSide): number;
|
|
63
|
+
/** Which clip fits the body as it lies now. */
|
|
64
|
+
pick(): GetUpSide;
|
|
65
|
+
/**
|
|
66
|
+
* Gets up: chooses the clip, turns and moves the character so the clip's first frame lies on the body, on the ground
|
|
67
|
+
* under it, starts the clip and hands the bones back over `blend` seconds. Resolves when the clip has played and the
|
|
68
|
+
* animator is on its way to `then`.
|
|
69
|
+
* @throws MargoError with code `state` when no bone is limp or a get-up is already running.
|
|
70
|
+
*/
|
|
71
|
+
start(options?: GetUpStartOptions): Promise<GetUpResult>;
|
|
72
|
+
private live;
|
|
73
|
+
/** Stops watching for the end of a clip. The animator and the ragdoll stay as they are. Safe to call twice. */
|
|
74
|
+
dispose(): void;
|
|
75
|
+
[Symbol.dispose]: () => void;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Adds getting up to a ragdoll: `start()` picks the face-up or face-down clip by how the body lies, matches the character
|
|
79
|
+
* to the body and blends into the clip. Build the ragdoll on the same skeleton as the animator.
|
|
80
|
+
* @example
|
|
81
|
+
* const getUp = addGetUp(ragdoll, animator, { faceUp: 'getUpBack', faceDown: 'getUpFront', then: 'idle' });
|
|
82
|
+
* ragdoll.activate();
|
|
83
|
+
* setTimeout(() => getUp.start(), 2000);
|
|
84
|
+
* @throws TypeError without a ragdoll and an animator, RangeError for an unknown state or an option out of range.
|
|
85
|
+
*/
|
|
86
|
+
export declare function addGetUp(ragdoll: Ragdoll, animator: Animator, options: GetUpOptions): GetUp;
|
package/dist/getup.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
2
|
+
import { conjugateQuaternion as t, multiplyQuaternions as n, normalizeQuaternion as r, rotateByQuaternion as i, rotationToQuaternion as a } from "@margoengine/math/quat";
|
|
3
|
+
//#region src/getup.ts
|
|
4
|
+
var o = (e) => {
|
|
5
|
+
let t = Math.hypot(e[0], e[1], e[2]), n = Math.hypot(e[4], e[5], e[6]), i = Math.hypot(e[8], e[9], e[10]);
|
|
6
|
+
return r(a(e, [
|
|
7
|
+
0,
|
|
8
|
+
0,
|
|
9
|
+
0,
|
|
10
|
+
1
|
|
11
|
+
], t, n, i));
|
|
12
|
+
}, s = (e) => 2 * Math.atan2(Math.hypot(e[0], e[1], e[2]), Math.abs(e[3]));
|
|
13
|
+
function c(e, t) {
|
|
14
|
+
let n = e[0] * t[0] + e[1] * t[1] + e[2] * t[2], r = Math.hypot(n, e[3]);
|
|
15
|
+
return r < 1e-9 ? [
|
|
16
|
+
0,
|
|
17
|
+
0,
|
|
18
|
+
0,
|
|
19
|
+
1
|
|
20
|
+
] : [
|
|
21
|
+
t[0] * n / r,
|
|
22
|
+
t[1] * n / r,
|
|
23
|
+
t[2] * n / r,
|
|
24
|
+
e[3] / r
|
|
25
|
+
];
|
|
26
|
+
}
|
|
27
|
+
var l = class {
|
|
28
|
+
constructor(e, t, n) {
|
|
29
|
+
this.ragdoll = e, this.animator = t, this.disposed = !1, this.states = {
|
|
30
|
+
faceUp: n.faceUp,
|
|
31
|
+
faceDown: n.faceDown
|
|
32
|
+
};
|
|
33
|
+
for (let e of [
|
|
34
|
+
n.faceUp,
|
|
35
|
+
n.faceDown,
|
|
36
|
+
n.then ?? t.state
|
|
37
|
+
]) if (!Object.hasOwn(t.options.states, e)) throw RangeError(`Unknown animator state "${e}"; pass a state name from the Animator's options.`);
|
|
38
|
+
if (this.root = n.root ?? t.animation.model.root, this.then = n.then ?? t.state, this.blend = n.blend ?? .4, this.fade = n.fade ?? .2, !(this.blend >= 0) || this.blend === Infinity || !(this.fade >= 0) || this.fade === Infinity) throw RangeError("Get-up blend and fade must be finite numbers of seconds, 0 or more.");
|
|
39
|
+
let r = n.up ?? [
|
|
40
|
+
0,
|
|
41
|
+
1,
|
|
42
|
+
0
|
|
43
|
+
], i = Math.hypot(r[0], r[1], r[2]);
|
|
44
|
+
if (!(i > 0) || i === Infinity) throw RangeError("Get-up up must be a nonzero finite [x, y, z]; pass the world up, e.g. [0, 1, 0].");
|
|
45
|
+
this.up = [
|
|
46
|
+
r[0] / i,
|
|
47
|
+
r[1] / i,
|
|
48
|
+
r[2] / i
|
|
49
|
+
], this.first = {
|
|
50
|
+
faceUp: this.sample("faceUp"),
|
|
51
|
+
faceDown: this.sample("faceDown")
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
sample(e) {
|
|
55
|
+
let { animator: r } = this, a = r.state, s = r.time, c = r.entryTime, l = this.ragdoll.bones[0].node, u = l.engine.store;
|
|
56
|
+
r.cancelFade(), r.transition(this.states[e], 0), r.onPreUpdate(0), u.tick(!1);
|
|
57
|
+
let d = this.root.matrix, f = l.matrix, p = t(o(d)), m = {
|
|
58
|
+
rotation: n(p, o(f)),
|
|
59
|
+
position: i([
|
|
60
|
+
f[12] - d[12],
|
|
61
|
+
f[13] - d[13],
|
|
62
|
+
f[14] - d[14]
|
|
63
|
+
], p)
|
|
64
|
+
};
|
|
65
|
+
return r.transition(a, 0), r.enterAt(c), r.time = s, r.onPreUpdate(0), u.tick(!1), m;
|
|
66
|
+
}
|
|
67
|
+
fit(e) {
|
|
68
|
+
let r = this.ragdoll.bones[0].body.rotation, i = n(o(this.root.matrix), this.first[e].rotation), a = c(n(r, t(i)), this.up);
|
|
69
|
+
return {
|
|
70
|
+
turn: a,
|
|
71
|
+
mismatch: s(n(t(r), n(a, i)))
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
mismatch(e) {
|
|
75
|
+
return this.live(), this.fit(e).mismatch;
|
|
76
|
+
}
|
|
77
|
+
pick() {
|
|
78
|
+
return this.live(), this.fit("faceUp").mismatch <= this.fit("faceDown").mismatch ? "faceUp" : "faceDown";
|
|
79
|
+
}
|
|
80
|
+
start(t = {}) {
|
|
81
|
+
if (this.live(), !this.ragdoll.active) throw new e("state", "The ragdoll is not limp, so there is nothing to get up from; call activate first.");
|
|
82
|
+
if (this.watching) throw new e("state", "A get-up is already running; wait for its promise.");
|
|
83
|
+
t.signal?.throwIfAborted();
|
|
84
|
+
let r = t.side ?? this.pick(), { turn: a, mismatch: s } = this.fit(r), { animator: c, ragdoll: l, root: u } = this, d = l.bones[0], f = u.engine.store, p = d.body.position, m = u.matrix, h = [
|
|
85
|
+
m[12],
|
|
86
|
+
m[13],
|
|
87
|
+
m[14]
|
|
88
|
+
], g = this.first[r].position, _ = n(a, o(m)), v = i(g, _), y = this.up.map((e) => -e), b = l.physics.raycast(p, y, {
|
|
89
|
+
maxDistance: 100,
|
|
90
|
+
excludeDynamic: !0,
|
|
91
|
+
excludeKinematic: !0
|
|
92
|
+
}), x = (e) => e[0] * this.up[0] + e[1] * this.up[1] + e[2] * this.up[2], S = x(b ? b.point : h), C = [
|
|
93
|
+
0,
|
|
94
|
+
1,
|
|
95
|
+
2
|
|
96
|
+
].map((e) => p[e] - v[e]), w = S - x(C);
|
|
97
|
+
return u.setQuaternion(_), u.setPosition([
|
|
98
|
+
C[0] + this.up[0] * w,
|
|
99
|
+
C[1] + this.up[1] * w,
|
|
100
|
+
C[2] + this.up[2] * w
|
|
101
|
+
]), f.tick(!1), c.cancelFade(), c.transition(this.states[r], 0), l.deactivate(this.blend), new Promise((e, n) => {
|
|
102
|
+
let i = () => {
|
|
103
|
+
this.watching?.(), this.watching = void 0, t.signal?.removeEventListener("abort", a);
|
|
104
|
+
}, a = () => {
|
|
105
|
+
i(), n(t.signal.reason);
|
|
106
|
+
};
|
|
107
|
+
t.signal?.addEventListener("abort", a, { once: !0 });
|
|
108
|
+
let o = c.addLateStep(() => {
|
|
109
|
+
c.state === this.states[r] ? c.normalizedTime >= 1 && (c.transition(this.then, this.fade), i(), e({
|
|
110
|
+
side: r,
|
|
111
|
+
mismatch: s
|
|
112
|
+
})) : (i(), e({
|
|
113
|
+
side: r,
|
|
114
|
+
mismatch: s
|
|
115
|
+
}));
|
|
116
|
+
});
|
|
117
|
+
this.watching = o;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
live() {
|
|
121
|
+
if (this.disposed) throw new e("disposed", "This get-up is disposed; call addGetUp again.");
|
|
122
|
+
}
|
|
123
|
+
dispose() {
|
|
124
|
+
this.disposed = !0, this.watching?.(), this.watching = void 0;
|
|
125
|
+
}
|
|
126
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
127
|
+
this.dispose();
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
function u(e, t, n) {
|
|
131
|
+
if (!e?.bones || typeof t?.transition != "function" || !n) throw TypeError("addGetUp needs a Ragdoll, an Animator and options: addGetUp(ragdoll, animator, {faceUp, faceDown}).");
|
|
132
|
+
return new l(e, t, n);
|
|
133
|
+
}
|
|
134
|
+
//#endregion
|
|
135
|
+
export { l as GetUp, u as addGetUp };
|
package/dist/hulls.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { Mesh } from '@margoengine/core/extension';
|
|
2
|
+
import type { Ragdoll, RagdollPart, RagdollShape } from './ragdoll.js';
|
|
3
|
+
/** A skinned mesh in its bind pose, for {@link fitRagdollShapes}. */
|
|
4
|
+
export interface RagdollSkin {
|
|
5
|
+
/** Bind-pose vertex positions, xyz per vertex. */
|
|
6
|
+
positions: ArrayLike<number>;
|
|
7
|
+
/** Skin joint indices, `influences` per vertex: indices into `nodes`. */
|
|
8
|
+
joints: Uint16Array;
|
|
9
|
+
/** Skin weights, one per entry of `joints`. */
|
|
10
|
+
weights: Float32Array;
|
|
11
|
+
/** Slots per vertex. Default 4. */
|
|
12
|
+
influences?: number;
|
|
13
|
+
/** The skin's joint nodes, in skin order. */
|
|
14
|
+
nodes: readonly Mesh[];
|
|
15
|
+
/** Inverse bind matrices, 16 numbers per entry of `nodes`, column-major. */
|
|
16
|
+
inverseBindMatrices: ArrayLike<number>;
|
|
17
|
+
}
|
|
18
|
+
/** What {@link fitRagdollShapes} reads of a glTF model (a `PBRModel`). */
|
|
19
|
+
export interface SkinnedModel {
|
|
20
|
+
readonly nodes: readonly Mesh[];
|
|
21
|
+
readonly primitives: readonly {
|
|
22
|
+
readonly nodeIndex: number;
|
|
23
|
+
readonly definition: {
|
|
24
|
+
readonly attributes?: Record<string, unknown>;
|
|
25
|
+
};
|
|
26
|
+
}[];
|
|
27
|
+
readonly asset: {
|
|
28
|
+
readonly doc: {
|
|
29
|
+
readonly nodes?: readonly {
|
|
30
|
+
readonly skin?: number;
|
|
31
|
+
}[];
|
|
32
|
+
readonly skins?: readonly {
|
|
33
|
+
readonly joints: readonly number[];
|
|
34
|
+
readonly inverseBindMatrices?: unknown;
|
|
35
|
+
}[];
|
|
36
|
+
};
|
|
37
|
+
accessor(id: unknown, type?: string): Float32Array;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** Options of {@link fitRagdollShapes}. */
|
|
41
|
+
export interface RagdollFitOptions {
|
|
42
|
+
/** `'hull'`, a convex hull of the bone's vertices, or `'box'`, the smallest box around them that lies along the bone. Default `'hull'`. */
|
|
43
|
+
shape?: 'hull' | 'box';
|
|
44
|
+
/** A vertex counts toward a bone when the bone carries at least this share of it. Default 0.5. */
|
|
45
|
+
minWeight?: number;
|
|
46
|
+
/** Most corners a hull keeps, 8 to 1,024. Default 64. */
|
|
47
|
+
maxPoints?: number;
|
|
48
|
+
}
|
|
49
|
+
/** One bone that got a new shape. */
|
|
50
|
+
export interface RagdollFit {
|
|
51
|
+
/** The bone. */
|
|
52
|
+
part: RagdollPart;
|
|
53
|
+
/** Its shape now, in its own frame. */
|
|
54
|
+
shape: RagdollShape;
|
|
55
|
+
/** Vertices that counted toward it. */
|
|
56
|
+
vertexCount: number;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Fits each bone's collider to the mesh it carries: a vertex belongs to the bone that dominates its skin weights, skin
|
|
60
|
+
* joints with no body counting toward the bone above them, and the bone's capsule gives way to the hull or the box of its
|
|
61
|
+
* vertices. A bone's mass, friction, groups and joint stay as they are; a bone with no vertices that span a volume keeps
|
|
62
|
+
* its capsule. Call it on a new ragdoll, before `addRagdollMotors`.
|
|
63
|
+
* @example
|
|
64
|
+
* const ragdoll = Ragdoll.attach(physics, { root: hips });
|
|
65
|
+
* const fitted = fitRagdollShapes(ragdoll, model); // or { shape: 'box' }
|
|
66
|
+
* console.log(`${fitted.length} of ${ragdoll.bones.length} bones fit the mesh`);
|
|
67
|
+
* @param source The glTF model the skeleton belongs to, or a skinned mesh in its bind pose.
|
|
68
|
+
* @returns The bones that got a shape.
|
|
69
|
+
* @throws TypeError without a ragdoll or a source, RangeError for a model with no skin on these bones or a bad option.
|
|
70
|
+
*/
|
|
71
|
+
export declare function fitRagdollShapes(ragdoll: Ragdoll, source: SkinnedModel | RagdollSkin, options?: RagdollFitOptions): RagdollFit[];
|
package/dist/hulls.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { normalizeQuaternion as e, rotateByQuaternion as t } from "@margoengine/math/quat";
|
|
2
|
+
import { jointHulls as n } from "@margoengine/rigging/hulls";
|
|
3
|
+
//#region src/hulls.ts
|
|
4
|
+
var r = [
|
|
5
|
+
1,
|
|
6
|
+
0,
|
|
7
|
+
0,
|
|
8
|
+
0,
|
|
9
|
+
0,
|
|
10
|
+
1,
|
|
11
|
+
0,
|
|
12
|
+
0,
|
|
13
|
+
0,
|
|
14
|
+
0,
|
|
15
|
+
1,
|
|
16
|
+
0,
|
|
17
|
+
0,
|
|
18
|
+
0,
|
|
19
|
+
0,
|
|
20
|
+
1
|
|
21
|
+
], i = 18;
|
|
22
|
+
function a(e, t) {
|
|
23
|
+
let { doc: n } = e.asset, i = (n.skins ?? []).findIndex((n) => n.joints.some((n) => e.nodes[n] === t));
|
|
24
|
+
if (i < 0) throw RangeError("No skin of this model holds the ragdoll's root bone; pass the model the skeleton belongs to.");
|
|
25
|
+
let a = e.primitives.filter((e) => n.nodes?.[e.nodeIndex]?.skin === i);
|
|
26
|
+
if (!a.length) throw RangeError("No mesh of this model uses the skin; there is nothing to fit shapes to.");
|
|
27
|
+
let { joints: o, inverseBindMatrices: s } = n.skins[i], c = a.some((e) => e.definition.attributes?.JOINTS_1 !== void 0) ? 2 : 1, l = c * 4, u = a.map((t) => e.asset.accessor(t.definition.attributes.POSITION, "VEC3")), d = u.reduce((e, t) => e + t.length / 3, 0), f = new Uint16Array(d * l), p = new Float32Array(d * l), m = new Float32Array(d * 3), h = 0;
|
|
28
|
+
a.forEach((t, n) => {
|
|
29
|
+
let r = u[n].length / 3;
|
|
30
|
+
m.set(u[n], h * 3);
|
|
31
|
+
for (let n = 0; n < c; n++) {
|
|
32
|
+
let i = t.definition.attributes, a = i["JOINTS_" + n], o = i["WEIGHTS_" + n];
|
|
33
|
+
if (a === void 0 || o === void 0) continue;
|
|
34
|
+
let s = e.asset.accessor(a, "VEC4"), c = e.asset.accessor(o, "VEC4");
|
|
35
|
+
for (let e = 0; e < r; e++) for (let t = 0; t < 4; t++) f[(h + e) * l + n * 4 + t] = s[e * 4 + t], p[(h + e) * l + n * 4 + t] = c[e * 4 + t];
|
|
36
|
+
}
|
|
37
|
+
h += r;
|
|
38
|
+
});
|
|
39
|
+
for (let e = 0; e < d; e++) {
|
|
40
|
+
let t = 0;
|
|
41
|
+
for (let n = 0; n < l; n++) t += p[e * l + n];
|
|
42
|
+
if (t > 0) for (let n = 0; n < l; n++) p[e * l + n] /= t;
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
positions: m,
|
|
46
|
+
joints: f,
|
|
47
|
+
weights: p,
|
|
48
|
+
influences: l,
|
|
49
|
+
nodes: o.map((t) => e.nodes[t]),
|
|
50
|
+
inverseBindMatrices: s === void 0 ? Float32Array.from(o.flatMap(() => r)) : e.asset.accessor(s, "MAT4")
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function o(e, t) {
|
|
54
|
+
let { parents: n } = e.engine.store;
|
|
55
|
+
for (let r = e.id; r !== -1; r = n[r]) {
|
|
56
|
+
let e = t.get(r);
|
|
57
|
+
if (e !== void 0) return e;
|
|
58
|
+
}
|
|
59
|
+
return -1;
|
|
60
|
+
}
|
|
61
|
+
function s(t) {
|
|
62
|
+
return t[1] < -.999999 ? [
|
|
63
|
+
1,
|
|
64
|
+
0,
|
|
65
|
+
0,
|
|
66
|
+
0
|
|
67
|
+
] : e([
|
|
68
|
+
t[2],
|
|
69
|
+
0,
|
|
70
|
+
-t[0],
|
|
71
|
+
1 + t[1]
|
|
72
|
+
]);
|
|
73
|
+
}
|
|
74
|
+
var c = (e, t) => [
|
|
75
|
+
e[3] * t[0] + e[0] * t[3] + e[1] * t[2] - e[2] * t[1],
|
|
76
|
+
e[3] * t[1] - e[0] * t[2] + e[1] * t[3] + e[2] * t[0],
|
|
77
|
+
e[3] * t[2] + e[0] * t[1] - e[1] * t[0] + e[2] * t[3],
|
|
78
|
+
e[3] * t[3] - e[0] * t[0] - e[1] * t[1] - e[2] * t[2]
|
|
79
|
+
];
|
|
80
|
+
function l(e, n) {
|
|
81
|
+
let r = s(n), a, o = Infinity;
|
|
82
|
+
for (let n = 0; n < i; n++) {
|
|
83
|
+
let s = n / i * Math.PI / 4, l = c(r, [
|
|
84
|
+
0,
|
|
85
|
+
Math.sin(s),
|
|
86
|
+
0,
|
|
87
|
+
Math.cos(s)
|
|
88
|
+
]), u = [
|
|
89
|
+
-l[0],
|
|
90
|
+
-l[1],
|
|
91
|
+
-l[2],
|
|
92
|
+
l[3]
|
|
93
|
+
], d = [
|
|
94
|
+
Infinity,
|
|
95
|
+
Infinity,
|
|
96
|
+
Infinity
|
|
97
|
+
], f = [
|
|
98
|
+
-Infinity,
|
|
99
|
+
-Infinity,
|
|
100
|
+
-Infinity
|
|
101
|
+
];
|
|
102
|
+
for (let n = 0; n < e.length; n += 3) {
|
|
103
|
+
let r = t([
|
|
104
|
+
e[n],
|
|
105
|
+
e[n + 1],
|
|
106
|
+
e[n + 2]
|
|
107
|
+
], u);
|
|
108
|
+
for (let e = 0; e < 3; e++) d[e] = Math.min(d[e], r[e]), f[e] = Math.max(f[e], r[e]);
|
|
109
|
+
}
|
|
110
|
+
let p = (f[0] - d[0]) * (f[1] - d[1]) * (f[2] - d[2]);
|
|
111
|
+
p >= o || (o = p, a = {
|
|
112
|
+
type: "box",
|
|
113
|
+
center: t([
|
|
114
|
+
0,
|
|
115
|
+
1,
|
|
116
|
+
2
|
|
117
|
+
].map((e) => (d[e] + f[e]) / 2), l),
|
|
118
|
+
halfExtents: [
|
|
119
|
+
(f[0] - d[0]) / 2,
|
|
120
|
+
(f[1] - d[1]) / 2,
|
|
121
|
+
(f[2] - d[2]) / 2
|
|
122
|
+
],
|
|
123
|
+
rotation: l
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return a;
|
|
127
|
+
}
|
|
128
|
+
function u(e, t, i = {}) {
|
|
129
|
+
if (!e?.bones || !t) throw TypeError("fitRagdollShapes needs a Ragdoll and a model: fitRagdollShapes(ragdoll, model).");
|
|
130
|
+
let { shape: s = "hull", minWeight: c, maxPoints: u } = i;
|
|
131
|
+
if (s !== "hull" && s !== "box") throw RangeError("fitRagdollShapes shape must be 'hull' or 'box'.");
|
|
132
|
+
let d = "primitives" in t ? a(t, e.bones[0].node) : t, { nodes: f, inverseBindMatrices: p } = d, m = new Map(e.bones.map((e, t) => [e.node.id, t])), h = new Map(f.flatMap((e) => m.has(e.id) ? [[e.id, m.get(e.id)]] : [])), g = new Float32Array(e.bones.length * 16);
|
|
133
|
+
if (!h.size) throw RangeError("The skin holds none of the ragdoll bones; pass the model the skeleton belongs to.");
|
|
134
|
+
for (let t = 0; t < e.bones.length; t++) g.set(r, t * 16);
|
|
135
|
+
f.forEach((e, t) => {
|
|
136
|
+
let n = h.get(e.id);
|
|
137
|
+
if (n !== void 0) for (let e = 0; e < 16; e++) g[n * 16 + e] = p[t * 16 + e];
|
|
138
|
+
});
|
|
139
|
+
let _ = f.map((e) => o(e, h)), v = new Uint16Array(d.joints.length), y = Float32Array.from(d.weights);
|
|
140
|
+
for (let e = 0; e < v.length; e++) {
|
|
141
|
+
let t = _[d.joints[e]];
|
|
142
|
+
if (t === void 0) throw RangeError(`Skin joint ${d.joints[e]} has no node; pass one node per joint.`);
|
|
143
|
+
t < 0 ? y[e] = 0 : v[e] = t;
|
|
144
|
+
}
|
|
145
|
+
return n({
|
|
146
|
+
positions: d.positions,
|
|
147
|
+
skin: {
|
|
148
|
+
joints: v,
|
|
149
|
+
weights: y,
|
|
150
|
+
influences: d.influences ?? 4
|
|
151
|
+
}
|
|
152
|
+
}, {
|
|
153
|
+
minWeight: c,
|
|
154
|
+
maxPoints: u,
|
|
155
|
+
inverseBindMatrices: g
|
|
156
|
+
}).map((t) => {
|
|
157
|
+
let n = e.bones[t.joint], r = n.node.matrix, i = [
|
|
158
|
+
Math.hypot(r[0], r[1], r[2]),
|
|
159
|
+
Math.hypot(r[4], r[5], r[6]),
|
|
160
|
+
Math.hypot(r[8], r[9], r[10])
|
|
161
|
+
], a = t.points.map((e, t) => e * i[t % 3]), o = s === "box" ? l(a, n.axis) : {
|
|
162
|
+
type: "hull",
|
|
163
|
+
points: a
|
|
164
|
+
};
|
|
165
|
+
return e.setShape(n, o), {
|
|
166
|
+
part: n,
|
|
167
|
+
shape: o,
|
|
168
|
+
vertexCount: t.vertexCount
|
|
169
|
+
};
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
//#endregion
|
|
173
|
+
export { u as fitRagdollShapes };
|
package/dist/index.d.ts
ADDED