lecodes-sdk 0.20.0 → 0.20.2
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/dist/global.d.ts +31 -0
- package/dist/inject.js +361 -260
- package/dist/types/audio/Bus.d.ts +45 -0
- package/dist/types/audio/Sound.d.ts +28 -0
- package/dist/types/audio/Voice.d.ts +27 -0
- package/dist/types/audio/audio.d.ts +83 -0
- package/dist/types/audio/support.d.ts +1 -0
- package/dist/types/gl/AudioSource.d.ts +60 -0
- package/dist/types/gl/AudioZone.d.ts +32 -0
- package/dist/types/gl/DecalSet.d.ts +103 -0
- package/dist/types/gl/Geometry.d.ts +5 -0
- package/dist/types/gl/Light.d.ts +7 -0
- package/dist/types/gl/Material.d.ts +86 -2
- package/dist/types/gl/Mesh.d.ts +11 -0
- package/dist/types/gl/Scene.d.ts +23 -0
- package/dist/types/gl/SceneAudio.d.ts +11 -0
- package/dist/types/gl/Texture.d.ts +29 -1
- package/dist/types/gl/animation/AnimationClip.d.ts +25 -12
- package/dist/types/gl/animation/core.d.ts +15 -10
- package/dist/types/gl/state.d.ts +0 -1
- package/dist/types/inject.d.ts +10 -0
- package/dist/types/runtime/input.d.ts +11 -0
- package/dist/types/ui/UIImage.d.ts +15 -5
- package/dist/types.json +1 -1
- package/package.json +1 -1
- package/prompts/dist/2d-game.md +408 -197
- package/prompts/dist/3d-app.md +491 -166
- package/prompts/dist/ar-app.md +373 -163
- package/prompts/dist/design.md +83 -87
- package/prompts/dist/ui-app.md +325 -136
- package/src/audio/Bus.ts +102 -0
- package/src/audio/Sound.ts +96 -0
- package/src/audio/Voice.ts +102 -0
- package/src/audio/audio.ts +161 -0
- package/src/audio/support.ts +6 -0
- package/src/bridges.d.ts +1481 -1352
- package/src/compile/compileProject.ts +30 -15
- package/src/compile/index.ts +3 -0
- package/src/core/Aspect.ts +33 -8
- package/src/g2/Scene2D.ts +7 -0
- package/src/gl/AudioSource.ts +113 -0
- package/src/gl/AudioZone.ts +75 -0
- package/src/gl/CameraPlace.ts +52 -52
- package/src/gl/DecalSet.ts +233 -0
- package/src/gl/Geometry.ts +5 -0
- package/src/gl/Light.ts +16 -0
- package/src/gl/Lightmap.ts +3 -2
- package/src/gl/Material.ts +152 -4
- package/src/gl/Mesh.ts +20 -1
- package/src/gl/Ragdoll.ts +270 -270
- package/src/gl/Scene.ts +41 -7
- package/src/gl/SceneAudio.ts +26 -0
- package/src/gl/Texture.ts +43 -3
- package/src/gl/Trigger.ts +45 -45
- package/src/gl/Vehicle.ts +5 -5
- package/src/gl/animation/AnimationClip.ts +43 -20
- package/src/gl/animation/Animator.ts +4 -3
- package/src/gl/animation/core.ts +20 -15
- package/src/gl/scenarios.ts +291 -291
- package/src/gl/state.ts +1 -1
- package/src/inject.ts +12 -0
- package/src/runtime/input.ts +6 -1
- package/src/scene/gizmos.ts +148 -148
- package/src/ui/UIImage.ts +21 -7
package/src/gl/Ragdoll.ts
CHANGED
|
@@ -1,270 +1,270 @@
|
|
|
1
|
-
// A ragdoll: the model's skeleton handed to physics. One rigid body per listed bone — a capsule from
|
|
2
|
-
// the bone's origin to the next joint down the chain — joined to its parent part by a swing-twist
|
|
3
|
-
// constraint (a cone + a twist range, or a one-way hinge for knees and elbows). Built once at
|
|
4
|
-
// attach from the pose the model is in (that pose is the joints' neutral for the limits), and
|
|
5
|
-
// switched on and off at will:
|
|
6
|
-
//
|
|
7
|
-
// const rag = hero.aspect(Ragdoll).get(Ragdoll)! // 'humanoid': the parts by bone name
|
|
8
|
-
// rag.activate({ velocity: capsule.controller.velocity }) // drops from its CURRENT pose, keeps its speed
|
|
9
|
-
// …
|
|
10
|
-
// rag.deactivate() // the animator owns the bones again
|
|
11
|
-
//
|
|
12
|
-
// While active the engine writes the bodies' poses onto the bones every frame AFTER the animator, so
|
|
13
|
-
// the listed bones hide whatever plays underneath; bones NOT listed (fingers, toes, the rest of a
|
|
14
|
-
// spine chain between two parts) keep their animated local pose under the ragdolled parents, and
|
|
15
|
-
// anything mounted on a bone (a weapon in the hand) rides along. Raycasts hit the parts and report
|
|
16
|
-
// the bone node, so a shot knows which limb it struck — `impulse(bone, …)` pushes that part.
|
|
17
|
-
//
|
|
18
|
-
// The character CONTROLLER is yours to switch off (`node.removeAspect(CharacterController)`): a
|
|
19
|
-
// capsule left standing would keep the fallen body from settling. `root` (the hips) tells you where
|
|
20
|
-
// the body ended up when it is time to get back up.
|
|
21
|
-
//
|
|
22
|
-
// A fallen body is scenery, not an obstacle: by default (`collide: 'static'`) only the static world
|
|
23
|
-
// and other ragdolls stop it — dynamic bodies, character controllers and vehicles pass through, so it
|
|
24
|
-
// never shoves or blocks anything and the solver never pairs it with them — and once every part has
|
|
25
|
-
// come to rest (`freeze`, or `freezeAfter` seconds at the latest) the parts turn static where they
|
|
26
|
-
// lie: the pose holds, nothing can wake them, the solver skips them. Raycasts hit it either way, and
|
|
27
|
-
// a frozen `'all'` body is an obstacle you can stand on.
|
|
28
|
-
|
|
29
|
-
import { Aspect } from "../core/Aspect"
|
|
30
|
-
import type { FieldMeta } from "../core/fields"
|
|
31
|
-
import { cx, cy, cz, type Vec3Like } from "../math/vec"
|
|
32
|
-
import { Node } from "./Node"
|
|
33
|
-
import { DEFAULT_FRICTION } from "./Physics"
|
|
34
|
-
|
|
35
|
-
export interface RagdollHinge {
|
|
36
|
-
/** The hinge axis in the MODEL's space (its rest pose: +Z forward, +Y up, +X the model's left in
|
|
37
|
-
* glTF). Perpendicular to the bone; the engine drops any component along it. */
|
|
38
|
-
axis: Vec3Like
|
|
39
|
-
/** The bend range about `axis` in degrees, relative to the pose the ragdoll was built in — a knee
|
|
40
|
-
* is `{ axis: [1, 0, 0], min: 0, max: 140 }`: it folds one way only. */
|
|
41
|
-
min: number
|
|
42
|
-
max: number
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface RagdollPart {
|
|
46
|
-
/** The bone the body sits on — its origin is the joint. */
|
|
47
|
-
bone: string
|
|
48
|
-
/** The bone the capsule points at (the next joint down). Omitted = a leaf: `length` along the line
|
|
49
|
-
* from the parent part's origin (a head, a hand, a foot without toes). */
|
|
50
|
-
to?: string
|
|
51
|
-
/** The bone of the part this one hangs from (default: the previous part in the list). The root
|
|
52
|
-
* has none. */
|
|
53
|
-
parent?: string
|
|
54
|
-
/** Capsule radius in metres (default 0.05). */
|
|
55
|
-
radius?: number
|
|
56
|
-
/** Length in metres: a leaf's, or an override of the distance to `to`. */
|
|
57
|
-
length?: number
|
|
58
|
-
/** This part's share of the ragdoll's `mass` (relative weight, default 1). */
|
|
59
|
-
mass?: number
|
|
60
|
-
/** Cone half-angle in degrees the part may swing off its parent (default 30). Ignored with `hinge`. */
|
|
61
|
-
swing?: number
|
|
62
|
-
/** Twist half-angle in degrees about its own axis (default 20). */
|
|
63
|
-
twist?: number
|
|
64
|
-
/** A one-way joint instead of the cone. */
|
|
65
|
-
hinge?: RagdollHinge
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const VERSION = 2
|
|
69
|
-
const HEADER = 12
|
|
70
|
-
const STRIDE = 12
|
|
71
|
-
|
|
72
|
-
// Bone names per humanoid role, tried in order: Mixamo / MotusMan / Unity-style first, then UE-style.
|
|
73
|
-
// `node.bone()` already matches the part after a `:` / `|` prefix (`mixamorig:Hips`).
|
|
74
|
-
const HUMANOID: Record<string, string[]> = {
|
|
75
|
-
hips: [ "Hips", "pelvis", "Pelvis", "hip" ],
|
|
76
|
-
spine: [ "Spine", "spine_01", "spine", "Spine1" ],
|
|
77
|
-
neck: [ "Neck", "neck_01", "neck" ],
|
|
78
|
-
head: [ "Head", "head" ],
|
|
79
|
-
upperArmL: [ "LeftArm", "upperarm_l", "LeftUpperArm", "UpperArm_L", "L_UpperArm" ],
|
|
80
|
-
foreArmL: [ "LeftForeArm", "lowerarm_l", "LeftLowerArm", "LowerArm_L", "L_Forearm" ],
|
|
81
|
-
handL: [ "LeftHand", "hand_l", "Hand_L", "L_Hand" ],
|
|
82
|
-
upLegL: [ "LeftUpLeg", "thigh_l", "LeftUpperLeg", "UpperLeg_L", "L_Thigh" ],
|
|
83
|
-
legL: [ "LeftLeg", "calf_l", "LeftLowerLeg", "LowerLeg_L", "L_Calf" ],
|
|
84
|
-
footL: [ "LeftFoot", "foot_l", "Foot_L", "L_Foot" ],
|
|
85
|
-
toeL: [ "LeftToeBase", "ball_l", "LeftToe", "Toe_L", "L_Toe" ],
|
|
86
|
-
upperArmR: [ "RightArm", "upperarm_r", "RightUpperArm", "UpperArm_R", "R_UpperArm" ],
|
|
87
|
-
foreArmR: [ "RightForeArm", "lowerarm_r", "RightLowerArm", "LowerArm_R", "R_Forearm" ],
|
|
88
|
-
handR: [ "RightHand", "hand_r", "Hand_R", "R_Hand" ],
|
|
89
|
-
upLegR: [ "RightUpLeg", "thigh_r", "RightUpperLeg", "UpperLeg_R", "R_Thigh" ],
|
|
90
|
-
legR: [ "RightLeg", "calf_r", "RightLowerLeg", "LowerLeg_R", "R_Calf" ],
|
|
91
|
-
footR: [ "RightFoot", "foot_r", "Foot_R", "R_Foot" ],
|
|
92
|
-
toeR: [ "RightToeBase", "ball_r", "RightToe", "Toe_R", "R_Toe" ],
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/** The standard humanoid layout (hips, one spine capsule up to the neck, head, two-segment arms and
|
|
96
|
-
* legs, hands and feet where the rig has them): sizes for a ~1.8 m human, mass shares from body
|
|
97
|
-
* segment tables, knees and elbows as hinges that fold the natural way for a model standing in a
|
|
98
|
-
* T- or A-pose facing +Z. */
|
|
99
|
-
const humanoidParts = (find: (role: string) => string | null): RagdollPart[] => {
|
|
100
|
-
const hips = find("hips"), spine = find("spine"), neck = find("neck"), head = find("head")
|
|
101
|
-
if (!hips) throw new Error("Ragdoll 'humanoid': no hips bone (Hips / pelvis) on this model — list the parts by name")
|
|
102
|
-
const parts: RagdollPart[] = []
|
|
103
|
-
parts.push({ bone: hips, to: spine ?? undefined, length: spine ? 0 : 0.2, radius: 0.12, mass: 15 })
|
|
104
|
-
const torso = spine ?? hips
|
|
105
|
-
if (spine) parts.push({ bone: spine, to: neck ?? head ?? undefined, length: neck || head ? 0 : 0.4, parent: hips, radius: 0.11, mass: 25, swing: 25, twist: 25 })
|
|
106
|
-
if (head) parts.push({ bone: head, parent: torso, radius: 0.1, length: 0.2, mass: 6, swing: 35, twist: 45 })
|
|
107
|
-
for (const side of [ "L", "R" ] as const) {
|
|
108
|
-
const upper = find(`upperArm${side}`), fore = find(`foreArm${side}`), hand = find(`hand${side}`)
|
|
109
|
-
if (upper) parts.push({ bone: upper, to: fore ?? undefined, length: fore ? 0 : 0.3, parent: torso, radius: 0.05, mass: 3, swing: 80, twist: 45 })
|
|
110
|
-
// the elbow: a T-pose arm along ±X folds its hand forward (+Z) — a negative turn about +Y on the
|
|
111
|
-
// left, a positive one on the right
|
|
112
|
-
if (upper && fore) parts.push({ bone: fore, to: hand ?? undefined, length: hand ? 0 : 0.28, parent: upper, radius: 0.04, mass: 2, twist: 30, hinge: { axis: [ 0, 1, 0 ], min: side === "L" ? -140 : 0, max: side === "L" ? 0 : 140 } })
|
|
113
|
-
if (upper && fore && hand) parts.push({ bone: hand, parent: fore, radius: 0.035, length: 0.16, mass: 1, swing: 30, twist: 20 })
|
|
114
|
-
const up = find(`upLeg${side}`), leg = find(`leg${side}`), foot = find(`foot${side}`), toe = find(`toe${side}`)
|
|
115
|
-
if (up) parts.push({ bone: up, to: leg ?? undefined, length: leg ? 0 : 0.45, parent: hips, radius: 0.08, mass: 10, swing: 60, twist: 30 })
|
|
116
|
-
// the knee: the shin (pointing down) swings its foot backward (−Z) for a positive turn about +X
|
|
117
|
-
if (up && leg) parts.push({ bone: leg, to: foot ?? undefined, length: foot ? 0 : 0.42, parent: up, radius: 0.06, mass: 5, twist: 10, hinge: { axis: [ 1, 0, 0 ], min: 0, max: 140 } })
|
|
118
|
-
if (up && leg && foot) parts.push({ bone: foot, to: toe ?? undefined, length: toe ? 0 : 0.2, parent: leg, radius: 0.04, mass: 2, swing: 30, twist: 15 })
|
|
119
|
-
}
|
|
120
|
-
return parts
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export class Ragdoll extends Aspect<"ragdoll", Node> {
|
|
124
|
-
static readonly aspect = "ragdoll"
|
|
125
|
-
|
|
126
|
-
/** The parts: `'humanoid'` (default) finds the standard bones by name; a list places bodies on any
|
|
127
|
-
* bones — parents before children. */
|
|
128
|
-
parts: RagdollPart[] | "humanoid" = "humanoid"
|
|
129
|
-
/** Total mass in kg (default 70), split between the parts by their `mass` shares. */
|
|
130
|
-
mass = 70
|
|
131
|
-
/** Friction of every part (default 0.6 — the physics default). */
|
|
132
|
-
friction = DEFAULT_FRICTION
|
|
133
|
-
/** Linear / angular damping of every part; 0 = Jolt's own 0.05. More angular damping settles a
|
|
134
|
-
* flailing body sooner. */
|
|
135
|
-
linearDamping = 0
|
|
136
|
-
angularDamping = 0
|
|
137
|
-
/** What the parts collide with. `'static'` (default): the static geometry and other such ragdolls
|
|
138
|
-
* only — dynamic bodies, character controllers and vehicles pass through a fallen body, which then
|
|
139
|
-
* never shoves or blocks anything and costs nothing when they meet. `'all'`: a regular dynamic
|
|
140
|
-
* body that bumps into everything (and gets kicked awake by everything). */
|
|
141
|
-
collide: "static" | "all" = "static"
|
|
142
|
-
/** Once every part has come to rest the parts turn static where they lie (default true): the pose
|
|
143
|
-
* holds, nothing can wake a settled body and it costs the solver nothing — `deactivate()` /
|
|
144
|
-
* `activate()` still work (activate makes it dynamic again). */
|
|
145
|
-
freeze = true
|
|
146
|
-
/** Seconds after `activate()` at which the body freezes whatever it is doing — a twitch on a slope
|
|
147
|
-
* or a pile never sleeps on its own. 0 (default) = no cap. */
|
|
148
|
-
freezeAfter = 0
|
|
149
|
-
|
|
150
|
-
static fields: FieldMeta<Ragdoll> = {
|
|
151
|
-
mass: { min: 1, step: 5 },
|
|
152
|
-
friction: { min: 0, max: 2, step: 0.05 },
|
|
153
|
-
linearDamping: { label: "Linear damping", min: 0, max: 5, step: 0.05 },
|
|
154
|
-
angularDamping: { label: "Angular damping", min: 0, max: 5, step: 0.05 },
|
|
155
|
-
collide: { options: [ "static", "all" ] },
|
|
156
|
-
freezeAfter: { label: "Freeze after (s)", min: 0, max: 60, step: 0.5 },
|
|
157
|
-
parts: { hidden: true },
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
private _id = 0
|
|
161
|
-
private _active = false
|
|
162
|
-
private _bones: { name: string; node: Node }[] = []
|
|
163
|
-
|
|
164
|
-
onAttach(): void {
|
|
165
|
-
if (!_creator.physicsHasSupport || !_creator.physicsHasSupport()) return
|
|
166
|
-
if (!_creator.ragdollCreate) {
|
|
167
|
-
console.warn("Ragdoll needs a newer host (no ragdollCreate) — the body will not fall")
|
|
168
|
-
return
|
|
169
|
-
}
|
|
170
|
-
const find = (role: string): string | null => {
|
|
171
|
-
for (const name of HUMANOID[role] ?? []) if (this.node.bone(name)) return name
|
|
172
|
-
return null
|
|
173
|
-
}
|
|
174
|
-
const parts = this.parts === "humanoid" ? humanoidParts(find) : this.parts
|
|
175
|
-
if (parts.length === 0) throw new Error("Ragdoll: no parts")
|
|
176
|
-
|
|
177
|
-
const n = parts.length
|
|
178
|
-
const bones = new Uint32Array(n * 2)
|
|
179
|
-
const s = new Float32Array(HEADER + n * STRIDE)
|
|
180
|
-
s[0] = VERSION; s[1] = n; s[2] = STRIDE; s[3] = this.friction; s[4] = this.linearDamping; s[5] = this.angularDamping
|
|
181
|
-
s[6] = this.collide === "all" ? 0 : 1; s[7] = this.freeze ? 1 : 0; s[8] = Math.max(0, this.freezeAfter)
|
|
182
|
-
let shares = 0
|
|
183
|
-
for (const p of parts) shares += p.mass ?? 1
|
|
184
|
-
this._bones = []
|
|
185
|
-
for (let i = 0; i < n; i++) {
|
|
186
|
-
const p = parts[i]!
|
|
187
|
-
const bone = this.node.bone(p.bone)
|
|
188
|
-
if (!bone) throw new Error(`Ragdoll: no bone '${p.bone}' on '${this.node.name}'`)
|
|
189
|
-
const to = p.to ? this.node.bone(p.to) : null
|
|
190
|
-
if (p.to && !to) throw new Error(`Ragdoll: no bone '${p.to}' (the 'to' of '${p.bone}') on '${this.node.name}'`)
|
|
191
|
-
let parent = -1
|
|
192
|
-
if (i > 0) {
|
|
193
|
-
parent = p.parent ? parts.findIndex((q, j) => j < i && q.bone === p.parent) : i - 1
|
|
194
|
-
if (parent < 0) throw new Error(`Ragdoll: the parent '${p.parent}' of '${p.bone}' must be listed before it`)
|
|
195
|
-
}
|
|
196
|
-
bones[i * 2] = bone.id
|
|
197
|
-
bones[i * 2 + 1] = to ? to.id : 0
|
|
198
|
-
const o = HEADER + i * STRIDE
|
|
199
|
-
s[o] = parent
|
|
200
|
-
s[o + 1] = p.radius ?? 0.05
|
|
201
|
-
s[o + 2] = p.length ?? 0
|
|
202
|
-
s[o + 3] = this.mass * (p.mass ?? 1) / shares
|
|
203
|
-
s[o + 4] = p.swing ?? 30
|
|
204
|
-
s[o + 5] = p.twist ?? 20
|
|
205
|
-
if (p.hinge) {
|
|
206
|
-
s[o + 6] = 1
|
|
207
|
-
s[o + 7] = cx(p.hinge.axis); s[o + 8] = cy(p.hinge.axis); s[o + 9] = cz(p.hinge.axis)
|
|
208
|
-
s[o + 10] = p.hinge.min; s[o + 11] = p.hinge.max
|
|
209
|
-
}
|
|
210
|
-
this._bones.push({ name: p.bone, node: bone })
|
|
211
|
-
}
|
|
212
|
-
this._id = _creator.ragdollCreate(this.node.id, bones, s)
|
|
213
|
-
if (!this._id) console.warn(`Ragdoll: the host refused the parts on '${this.node.name}'`)
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
onDetach(): void {
|
|
217
|
-
if (this._id) {
|
|
218
|
-
if (this._active) _creator.ragdollDeactivate!(this._id)
|
|
219
|
-
_creator.ragdollDestroy!(this._id)
|
|
220
|
-
this._id = 0
|
|
221
|
-
}
|
|
222
|
-
this._active = false
|
|
223
|
-
this._bones = []
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/** Hand the bones to physics from the pose they are in right now. `velocity` (world m/s) is given
|
|
227
|
-
* to every part — pass the character's, so a running body keeps travelling. */
|
|
228
|
-
activate(opts: { velocity?: Vec3Like } = {}): boolean {
|
|
229
|
-
if (!this._id) return false
|
|
230
|
-
const v = opts.velocity ?? [ 0, 0, 0 ]
|
|
231
|
-
this._active = _creator.ragdollActivate!(this._id, cx(v), cy(v), cz(v))
|
|
232
|
-
return this._active
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/** Take the bones back: the animator's pose shows again from the next frame. */
|
|
236
|
-
deactivate(): void {
|
|
237
|
-
if (!this._id || !this._active) return
|
|
238
|
-
_creator.ragdollDeactivate!(this._id)
|
|
239
|
-
this._active = false
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/** Physics owns the bones right now. */
|
|
243
|
-
get active(): boolean { return this._active }
|
|
244
|
-
|
|
245
|
-
/** Active and every part asleep (or frozen) — the body has come to rest. */
|
|
246
|
-
get settled(): boolean {
|
|
247
|
-
return this._active && !!this._id && !_creator.ragdollActive!(this._id)
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/** The root part's bone (the hips): where the body is. */
|
|
251
|
-
get root(): Node | null { return this._bones[0]?.node ?? null }
|
|
252
|
-
|
|
253
|
-
/** The bones that carry a part, in order. */
|
|
254
|
-
get bones(): string[] { return this._bones.map((b) => b.name) }
|
|
255
|
-
|
|
256
|
-
/** The rigid-body id of a part (0 if none) — for the plain body calls. */
|
|
257
|
-
bodyOf(bone: string): number {
|
|
258
|
-
const i = this._bones.findIndex((b) => b.name === bone)
|
|
259
|
-
return i < 0 || !this._id ? 0 : _creator.ragdollBodyId!(this._id, i)
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/** Push one part: an impulse in N·s, at a world point (a hit) or through its centre. Wakes the body. */
|
|
263
|
-
impulse(bone: string, v: Vec3Like, at?: Vec3Like): boolean {
|
|
264
|
-
const id = this.bodyOf(bone)
|
|
265
|
-
if (!id) return false
|
|
266
|
-
if (at && _creator.physicsApplyImpulseAt) _creator.physicsApplyImpulseAt(id, cx(v), cy(v), cz(v), cx(at), cy(at), cz(at))
|
|
267
|
-
else _creator.physicsApplyImpulse(id, cx(v), cy(v), cz(v))
|
|
268
|
-
return true
|
|
269
|
-
}
|
|
270
|
-
}
|
|
1
|
+
// A ragdoll: the model's skeleton handed to physics. One rigid body per listed bone — a capsule from
|
|
2
|
+
// the bone's origin to the next joint down the chain — joined to its parent part by a swing-twist
|
|
3
|
+
// constraint (a cone + a twist range, or a one-way hinge for knees and elbows). Built once at
|
|
4
|
+
// attach from the pose the model is in (that pose is the joints' neutral for the limits), and
|
|
5
|
+
// switched on and off at will:
|
|
6
|
+
//
|
|
7
|
+
// const rag = hero.aspect(Ragdoll).get(Ragdoll)! // 'humanoid': the parts by bone name
|
|
8
|
+
// rag.activate({ velocity: capsule.controller.velocity }) // drops from its CURRENT pose, keeps its speed
|
|
9
|
+
// …
|
|
10
|
+
// rag.deactivate() // the animator owns the bones again
|
|
11
|
+
//
|
|
12
|
+
// While active the engine writes the bodies' poses onto the bones every frame AFTER the animator, so
|
|
13
|
+
// the listed bones hide whatever plays underneath; bones NOT listed (fingers, toes, the rest of a
|
|
14
|
+
// spine chain between two parts) keep their animated local pose under the ragdolled parents, and
|
|
15
|
+
// anything mounted on a bone (a weapon in the hand) rides along. Raycasts hit the parts and report
|
|
16
|
+
// the bone node, so a shot knows which limb it struck — `impulse(bone, …)` pushes that part.
|
|
17
|
+
//
|
|
18
|
+
// The character CONTROLLER is yours to switch off (`node.removeAspect(CharacterController)`): a
|
|
19
|
+
// capsule left standing would keep the fallen body from settling. `root` (the hips) tells you where
|
|
20
|
+
// the body ended up when it is time to get back up.
|
|
21
|
+
//
|
|
22
|
+
// A fallen body is scenery, not an obstacle: by default (`collide: 'static'`) only the static world
|
|
23
|
+
// and other ragdolls stop it — dynamic bodies, character controllers and vehicles pass through, so it
|
|
24
|
+
// never shoves or blocks anything and the solver never pairs it with them — and once every part has
|
|
25
|
+
// come to rest (`freeze`, or `freezeAfter` seconds at the latest) the parts turn static where they
|
|
26
|
+
// lie: the pose holds, nothing can wake them, the solver skips them. Raycasts hit it either way, and
|
|
27
|
+
// a frozen `'all'` body is an obstacle you can stand on.
|
|
28
|
+
|
|
29
|
+
import { Aspect } from "../core/Aspect"
|
|
30
|
+
import type { FieldMeta } from "../core/fields"
|
|
31
|
+
import { cx, cy, cz, type Vec3Like } from "../math/vec"
|
|
32
|
+
import { Node } from "./Node"
|
|
33
|
+
import { DEFAULT_FRICTION } from "./Physics"
|
|
34
|
+
|
|
35
|
+
export interface RagdollHinge {
|
|
36
|
+
/** The hinge axis in the MODEL's space (its rest pose: +Z forward, +Y up, +X the model's left in
|
|
37
|
+
* glTF). Perpendicular to the bone; the engine drops any component along it. */
|
|
38
|
+
axis: Vec3Like
|
|
39
|
+
/** The bend range about `axis` in degrees, relative to the pose the ragdoll was built in — a knee
|
|
40
|
+
* is `{ axis: [1, 0, 0], min: 0, max: 140 }`: it folds one way only. */
|
|
41
|
+
min: number
|
|
42
|
+
max: number
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface RagdollPart {
|
|
46
|
+
/** The bone the body sits on — its origin is the joint. */
|
|
47
|
+
bone: string
|
|
48
|
+
/** The bone the capsule points at (the next joint down). Omitted = a leaf: `length` along the line
|
|
49
|
+
* from the parent part's origin (a head, a hand, a foot without toes). */
|
|
50
|
+
to?: string
|
|
51
|
+
/** The bone of the part this one hangs from (default: the previous part in the list). The root
|
|
52
|
+
* has none. */
|
|
53
|
+
parent?: string
|
|
54
|
+
/** Capsule radius in metres (default 0.05). */
|
|
55
|
+
radius?: number
|
|
56
|
+
/** Length in metres: a leaf's, or an override of the distance to `to`. */
|
|
57
|
+
length?: number
|
|
58
|
+
/** This part's share of the ragdoll's `mass` (relative weight, default 1). */
|
|
59
|
+
mass?: number
|
|
60
|
+
/** Cone half-angle in degrees the part may swing off its parent (default 30). Ignored with `hinge`. */
|
|
61
|
+
swing?: number
|
|
62
|
+
/** Twist half-angle in degrees about its own axis (default 20). */
|
|
63
|
+
twist?: number
|
|
64
|
+
/** A one-way joint instead of the cone. */
|
|
65
|
+
hinge?: RagdollHinge
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const VERSION = 2
|
|
69
|
+
const HEADER = 12
|
|
70
|
+
const STRIDE = 12
|
|
71
|
+
|
|
72
|
+
// Bone names per humanoid role, tried in order: Mixamo / MotusMan / Unity-style first, then UE-style.
|
|
73
|
+
// `node.bone()` already matches the part after a `:` / `|` prefix (`mixamorig:Hips`).
|
|
74
|
+
const HUMANOID: Record<string, string[]> = {
|
|
75
|
+
hips: [ "Hips", "pelvis", "Pelvis", "hip" ],
|
|
76
|
+
spine: [ "Spine", "spine_01", "spine", "Spine1" ],
|
|
77
|
+
neck: [ "Neck", "neck_01", "neck" ],
|
|
78
|
+
head: [ "Head", "head" ],
|
|
79
|
+
upperArmL: [ "LeftArm", "upperarm_l", "LeftUpperArm", "UpperArm_L", "L_UpperArm" ],
|
|
80
|
+
foreArmL: [ "LeftForeArm", "lowerarm_l", "LeftLowerArm", "LowerArm_L", "L_Forearm" ],
|
|
81
|
+
handL: [ "LeftHand", "hand_l", "Hand_L", "L_Hand" ],
|
|
82
|
+
upLegL: [ "LeftUpLeg", "thigh_l", "LeftUpperLeg", "UpperLeg_L", "L_Thigh" ],
|
|
83
|
+
legL: [ "LeftLeg", "calf_l", "LeftLowerLeg", "LowerLeg_L", "L_Calf" ],
|
|
84
|
+
footL: [ "LeftFoot", "foot_l", "Foot_L", "L_Foot" ],
|
|
85
|
+
toeL: [ "LeftToeBase", "ball_l", "LeftToe", "Toe_L", "L_Toe" ],
|
|
86
|
+
upperArmR: [ "RightArm", "upperarm_r", "RightUpperArm", "UpperArm_R", "R_UpperArm" ],
|
|
87
|
+
foreArmR: [ "RightForeArm", "lowerarm_r", "RightLowerArm", "LowerArm_R", "R_Forearm" ],
|
|
88
|
+
handR: [ "RightHand", "hand_r", "Hand_R", "R_Hand" ],
|
|
89
|
+
upLegR: [ "RightUpLeg", "thigh_r", "RightUpperLeg", "UpperLeg_R", "R_Thigh" ],
|
|
90
|
+
legR: [ "RightLeg", "calf_r", "RightLowerLeg", "LowerLeg_R", "R_Calf" ],
|
|
91
|
+
footR: [ "RightFoot", "foot_r", "Foot_R", "R_Foot" ],
|
|
92
|
+
toeR: [ "RightToeBase", "ball_r", "RightToe", "Toe_R", "R_Toe" ],
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The standard humanoid layout (hips, one spine capsule up to the neck, head, two-segment arms and
|
|
96
|
+
* legs, hands and feet where the rig has them): sizes for a ~1.8 m human, mass shares from body
|
|
97
|
+
* segment tables, knees and elbows as hinges that fold the natural way for a model standing in a
|
|
98
|
+
* T- or A-pose facing +Z. */
|
|
99
|
+
const humanoidParts = (find: (role: string) => string | null): RagdollPart[] => {
|
|
100
|
+
const hips = find("hips"), spine = find("spine"), neck = find("neck"), head = find("head")
|
|
101
|
+
if (!hips) throw new Error("Ragdoll 'humanoid': no hips bone (Hips / pelvis) on this model — list the parts by name")
|
|
102
|
+
const parts: RagdollPart[] = []
|
|
103
|
+
parts.push({ bone: hips, to: spine ?? undefined, length: spine ? 0 : 0.2, radius: 0.12, mass: 15 })
|
|
104
|
+
const torso = spine ?? hips
|
|
105
|
+
if (spine) parts.push({ bone: spine, to: neck ?? head ?? undefined, length: neck || head ? 0 : 0.4, parent: hips, radius: 0.11, mass: 25, swing: 25, twist: 25 })
|
|
106
|
+
if (head) parts.push({ bone: head, parent: torso, radius: 0.1, length: 0.2, mass: 6, swing: 35, twist: 45 })
|
|
107
|
+
for (const side of [ "L", "R" ] as const) {
|
|
108
|
+
const upper = find(`upperArm${side}`), fore = find(`foreArm${side}`), hand = find(`hand${side}`)
|
|
109
|
+
if (upper) parts.push({ bone: upper, to: fore ?? undefined, length: fore ? 0 : 0.3, parent: torso, radius: 0.05, mass: 3, swing: 80, twist: 45 })
|
|
110
|
+
// the elbow: a T-pose arm along ±X folds its hand forward (+Z) — a negative turn about +Y on the
|
|
111
|
+
// left, a positive one on the right
|
|
112
|
+
if (upper && fore) parts.push({ bone: fore, to: hand ?? undefined, length: hand ? 0 : 0.28, parent: upper, radius: 0.04, mass: 2, twist: 30, hinge: { axis: [ 0, 1, 0 ], min: side === "L" ? -140 : 0, max: side === "L" ? 0 : 140 } })
|
|
113
|
+
if (upper && fore && hand) parts.push({ bone: hand, parent: fore, radius: 0.035, length: 0.16, mass: 1, swing: 30, twist: 20 })
|
|
114
|
+
const up = find(`upLeg${side}`), leg = find(`leg${side}`), foot = find(`foot${side}`), toe = find(`toe${side}`)
|
|
115
|
+
if (up) parts.push({ bone: up, to: leg ?? undefined, length: leg ? 0 : 0.45, parent: hips, radius: 0.08, mass: 10, swing: 60, twist: 30 })
|
|
116
|
+
// the knee: the shin (pointing down) swings its foot backward (−Z) for a positive turn about +X
|
|
117
|
+
if (up && leg) parts.push({ bone: leg, to: foot ?? undefined, length: foot ? 0 : 0.42, parent: up, radius: 0.06, mass: 5, twist: 10, hinge: { axis: [ 1, 0, 0 ], min: 0, max: 140 } })
|
|
118
|
+
if (up && leg && foot) parts.push({ bone: foot, to: toe ?? undefined, length: toe ? 0 : 0.2, parent: leg, radius: 0.04, mass: 2, swing: 30, twist: 15 })
|
|
119
|
+
}
|
|
120
|
+
return parts
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export class Ragdoll extends Aspect<"ragdoll", Node> {
|
|
124
|
+
static readonly aspect = "ragdoll"
|
|
125
|
+
|
|
126
|
+
/** The parts: `'humanoid'` (default) finds the standard bones by name; a list places bodies on any
|
|
127
|
+
* bones — parents before children. */
|
|
128
|
+
parts: RagdollPart[] | "humanoid" = "humanoid"
|
|
129
|
+
/** Total mass in kg (default 70), split between the parts by their `mass` shares. */
|
|
130
|
+
mass = 70
|
|
131
|
+
/** Friction of every part (default 0.6 — the physics default). */
|
|
132
|
+
friction = DEFAULT_FRICTION
|
|
133
|
+
/** Linear / angular damping of every part; 0 = Jolt's own 0.05. More angular damping settles a
|
|
134
|
+
* flailing body sooner. */
|
|
135
|
+
linearDamping = 0
|
|
136
|
+
angularDamping = 0
|
|
137
|
+
/** What the parts collide with. `'static'` (default): the static geometry and other such ragdolls
|
|
138
|
+
* only — dynamic bodies, character controllers and vehicles pass through a fallen body, which then
|
|
139
|
+
* never shoves or blocks anything and costs nothing when they meet. `'all'`: a regular dynamic
|
|
140
|
+
* body that bumps into everything (and gets kicked awake by everything). */
|
|
141
|
+
collide: "static" | "all" = "static"
|
|
142
|
+
/** Once every part has come to rest the parts turn static where they lie (default true): the pose
|
|
143
|
+
* holds, nothing can wake a settled body and it costs the solver nothing — `deactivate()` /
|
|
144
|
+
* `activate()` still work (activate makes it dynamic again). */
|
|
145
|
+
freeze = true
|
|
146
|
+
/** Seconds after `activate()` at which the body freezes whatever it is doing — a twitch on a slope
|
|
147
|
+
* or a pile never sleeps on its own. 0 (default) = no cap. */
|
|
148
|
+
freezeAfter = 0
|
|
149
|
+
|
|
150
|
+
static fields: FieldMeta<Ragdoll> = {
|
|
151
|
+
mass: { min: 1, step: 5 },
|
|
152
|
+
friction: { min: 0, max: 2, step: 0.05 },
|
|
153
|
+
linearDamping: { label: "Linear damping", min: 0, max: 5, step: 0.05 },
|
|
154
|
+
angularDamping: { label: "Angular damping", min: 0, max: 5, step: 0.05 },
|
|
155
|
+
collide: { options: [ "static", "all" ] },
|
|
156
|
+
freezeAfter: { label: "Freeze after (s)", min: 0, max: 60, step: 0.5 },
|
|
157
|
+
parts: { hidden: true },
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private _id = 0
|
|
161
|
+
private _active = false
|
|
162
|
+
private _bones: { name: string; node: Node }[] = []
|
|
163
|
+
|
|
164
|
+
onAttach(): void {
|
|
165
|
+
if (!_creator.physicsHasSupport || !_creator.physicsHasSupport()) return
|
|
166
|
+
if (!_creator.ragdollCreate) {
|
|
167
|
+
console.warn("Ragdoll needs a newer host (no ragdollCreate) — the body will not fall")
|
|
168
|
+
return
|
|
169
|
+
}
|
|
170
|
+
const find = (role: string): string | null => {
|
|
171
|
+
for (const name of HUMANOID[role] ?? []) if (this.node.bone(name)) return name
|
|
172
|
+
return null
|
|
173
|
+
}
|
|
174
|
+
const parts = this.parts === "humanoid" ? humanoidParts(find) : this.parts
|
|
175
|
+
if (parts.length === 0) throw new Error("Ragdoll: no parts")
|
|
176
|
+
|
|
177
|
+
const n = parts.length
|
|
178
|
+
const bones = new Uint32Array(n * 2)
|
|
179
|
+
const s = new Float32Array(HEADER + n * STRIDE)
|
|
180
|
+
s[0] = VERSION; s[1] = n; s[2] = STRIDE; s[3] = this.friction; s[4] = this.linearDamping; s[5] = this.angularDamping
|
|
181
|
+
s[6] = this.collide === "all" ? 0 : 1; s[7] = this.freeze ? 1 : 0; s[8] = Math.max(0, this.freezeAfter)
|
|
182
|
+
let shares = 0
|
|
183
|
+
for (const p of parts) shares += p.mass ?? 1
|
|
184
|
+
this._bones = []
|
|
185
|
+
for (let i = 0; i < n; i++) {
|
|
186
|
+
const p = parts[i]!
|
|
187
|
+
const bone = this.node.bone(p.bone)
|
|
188
|
+
if (!bone) throw new Error(`Ragdoll: no bone '${p.bone}' on '${this.node.name}'`)
|
|
189
|
+
const to = p.to ? this.node.bone(p.to) : null
|
|
190
|
+
if (p.to && !to) throw new Error(`Ragdoll: no bone '${p.to}' (the 'to' of '${p.bone}') on '${this.node.name}'`)
|
|
191
|
+
let parent = -1
|
|
192
|
+
if (i > 0) {
|
|
193
|
+
parent = p.parent ? parts.findIndex((q, j) => j < i && q.bone === p.parent) : i - 1
|
|
194
|
+
if (parent < 0) throw new Error(`Ragdoll: the parent '${p.parent}' of '${p.bone}' must be listed before it`)
|
|
195
|
+
}
|
|
196
|
+
bones[i * 2] = bone.id
|
|
197
|
+
bones[i * 2 + 1] = to ? to.id : 0
|
|
198
|
+
const o = HEADER + i * STRIDE
|
|
199
|
+
s[o] = parent
|
|
200
|
+
s[o + 1] = p.radius ?? 0.05
|
|
201
|
+
s[o + 2] = p.length ?? 0
|
|
202
|
+
s[o + 3] = this.mass * (p.mass ?? 1) / shares
|
|
203
|
+
s[o + 4] = p.swing ?? 30
|
|
204
|
+
s[o + 5] = p.twist ?? 20
|
|
205
|
+
if (p.hinge) {
|
|
206
|
+
s[o + 6] = 1
|
|
207
|
+
s[o + 7] = cx(p.hinge.axis); s[o + 8] = cy(p.hinge.axis); s[o + 9] = cz(p.hinge.axis)
|
|
208
|
+
s[o + 10] = p.hinge.min; s[o + 11] = p.hinge.max
|
|
209
|
+
}
|
|
210
|
+
this._bones.push({ name: p.bone, node: bone })
|
|
211
|
+
}
|
|
212
|
+
this._id = _creator.ragdollCreate(this.node.id, bones, s)
|
|
213
|
+
if (!this._id) console.warn(`Ragdoll: the host refused the parts on '${this.node.name}'`)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
onDetach(): void {
|
|
217
|
+
if (this._id) {
|
|
218
|
+
if (this._active) _creator.ragdollDeactivate!(this._id)
|
|
219
|
+
_creator.ragdollDestroy!(this._id)
|
|
220
|
+
this._id = 0
|
|
221
|
+
}
|
|
222
|
+
this._active = false
|
|
223
|
+
this._bones = []
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Hand the bones to physics from the pose they are in right now. `velocity` (world m/s) is given
|
|
227
|
+
* to every part — pass the character's, so a running body keeps travelling. */
|
|
228
|
+
activate(opts: { velocity?: Vec3Like } = {}): boolean {
|
|
229
|
+
if (!this._id) return false
|
|
230
|
+
const v = opts.velocity ?? [ 0, 0, 0 ]
|
|
231
|
+
this._active = _creator.ragdollActivate!(this._id, cx(v), cy(v), cz(v))
|
|
232
|
+
return this._active
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Take the bones back: the animator's pose shows again from the next frame. */
|
|
236
|
+
deactivate(): void {
|
|
237
|
+
if (!this._id || !this._active) return
|
|
238
|
+
_creator.ragdollDeactivate!(this._id)
|
|
239
|
+
this._active = false
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Physics owns the bones right now. */
|
|
243
|
+
get active(): boolean { return this._active }
|
|
244
|
+
|
|
245
|
+
/** Active and every part asleep (or frozen) — the body has come to rest. */
|
|
246
|
+
get settled(): boolean {
|
|
247
|
+
return this._active && !!this._id && !_creator.ragdollActive!(this._id)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** The root part's bone (the hips): where the body is. */
|
|
251
|
+
get root(): Node | null { return this._bones[0]?.node ?? null }
|
|
252
|
+
|
|
253
|
+
/** The bones that carry a part, in order. */
|
|
254
|
+
get bones(): string[] { return this._bones.map((b) => b.name) }
|
|
255
|
+
|
|
256
|
+
/** The rigid-body id of a part (0 if none) — for the plain body calls. */
|
|
257
|
+
bodyOf(bone: string): number {
|
|
258
|
+
const i = this._bones.findIndex((b) => b.name === bone)
|
|
259
|
+
return i < 0 || !this._id ? 0 : _creator.ragdollBodyId!(this._id, i)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** Push one part: an impulse in N·s, at a world point (a hit) or through its centre. Wakes the body. */
|
|
263
|
+
impulse(bone: string, v: Vec3Like, at?: Vec3Like): boolean {
|
|
264
|
+
const id = this.bodyOf(bone)
|
|
265
|
+
if (!id) return false
|
|
266
|
+
if (at && _creator.physicsApplyImpulseAt) _creator.physicsApplyImpulseAt(id, cx(v), cy(v), cz(v), cx(at), cy(at), cz(at))
|
|
267
|
+
else _creator.physicsApplyImpulse(id, cx(v), cy(v), cz(v))
|
|
268
|
+
return true
|
|
269
|
+
}
|
|
270
|
+
}
|