imaginu 0.4.0

AI-drivable procedural 3D asset compiler: JSON recipes -> beautiful game-ready GLB for Babylon.js
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Procedural clip driver for monsters. A generic set of eased, multi-axis
//! channels parameterized by the rig's `GaitDesc` — phase-offset leg swing
//! for locomotion, spine counter-rotation, plus attack / hurt / death and an
//! optional roar. The locomotion clip is named per `gait.style`.

use core::f32::consts::TAU;

use glam::{Quat, Vec3};

use crate::gltf::{AnimationClip, Channel, ChannelData};
use crate::recipe::MonsterParams;

use super::rig::{Gait, MonsterRig};

/// Evenly spaced key times over [0, dur].
pub(crate) fn keys(n: usize, dur: f32) -> Vec<f32> {
    (0..=n).map(|i| i as f32 / n as f32 * dur).collect()
}

/// Rotation channel driven by a normalized-phase function.
pub(crate) fn rot_channel(joint: usize, times: &[f32], f: impl Fn(f32) -> Quat) -> Channel {
    let dur = *times.last().unwrap();
    Channel {
        joint,
        times: times.to_vec(),
        data: ChannelData::Rotation(times.iter().map(|&t| f(t / dur)).collect()),
    }
}

/// Translation channel offset from a bind position by a phase function.
pub(crate) fn trans_channel(
    joint: usize,
    times: &[f32],
    bind: Vec3,
    f: impl Fn(f32) -> Vec3,
) -> Channel {
    let dur = *times.last().unwrap();
    Channel {
        joint,
        times: times.to_vec(),
        data: ChannelData::Translation(times.iter().map(|&t| bind + f(t / dur)).collect()),
    }
}

/// Smooth one-shot envelope: eases 0→1 over [a, b] and holds.
pub(crate) fn env(p: f32, a: f32, b: f32) -> f32 {
    let t = ((p - a) / (b - a).max(1e-4)).clamp(0.0, 1.0);
    t * t * (3.0 - 2.0 * t)
}

pub(crate) fn bind_of(rig: &MonsterRig, joint: usize) -> Vec3 {
    rig.skeleton.joints[joint].translation
}

/// Build the full clip set for a rig. Always: `idle`, a locomotion clip named
/// per `gait.style`, `attack`, `hurt`, `death`. Plus `roar` iff the plan has
/// a head joint.
pub fn build_clips(rig: &MonsterRig, _p: &MonsterParams) -> Vec<AnimationClip> {
    let g = &rig.gait;
    let mut clips = vec![
        idle_clip(rig),
        locomotion_clip(rig),
        attack_clip(rig),
        hurt_clip(rig),
        death_clip(rig),
    ];
    if g.head.is_some() {
        clips.push(roar_clip(rig));
    }
    clips
}

/// Subtle breathing: spine bob + head sway.
pub(crate) fn idle_clip(rig: &MonsterRig) -> AnimationClip {
    let g = &rig.gait;
    let dur = 2.6;
    let t = keys(16, dur);
    let mut channels = Vec::new();
    // spine breathing along the chain (skip hips root)
    for (i, &j) in g.spine.iter().enumerate().skip(1) {
        let ph = i as f32 * 0.3;
        channels.push(rot_channel(j, &t, move |p| {
            Quat::from_rotation_x(0.02 * (p * TAU + ph).sin())
        }));
    }
    if let Some(h) = g.head {
        channels.push(rot_channel(h, &t, move |p| {
            Quat::from_rotation_y(0.06 * (p * TAU + 1.0).sin())
        }));
    }
    for (i, &j) in g.tail.iter().enumerate() {
        let ph = i as f32 * 0.5;
        channels.push(rot_channel(j, &t, move |p| {
            Quat::from_rotation_y(0.09 * (p * TAU + ph).sin())
        }));
    }
    // breathing bob on the hips
    let hips = g.spine[0];
    let bob = 0.006 * body_scale(rig);
    channels.push(trans_channel(hips, &t, bind_of(rig, hips), move |p| {
        Vec3::Y * bob * (p * TAU).sin()
    }));
    AnimationClip {
        name: "idle".into(),
        channels,
    }
}

/// Characteristic body length for scaling translational motion.
pub(crate) fn body_scale(rig: &MonsterRig) -> f32 {
    (rig.bounds.1 - rig.bounds.0).length().max(1.0)
}

/// Dispatch the locomotion clip on the gait style. All share the generic
/// phase-offset leg swing; the name follows the style.
pub(crate) fn locomotion_clip(rig: &MonsterRig) -> AnimationClip {
    let name = match rig.gait.style {
        Gait::Walk => "walk",
        Gait::Slither => "slither",
        Gait::Fly => "fly",
        Gait::Crawl => "crawl",
        Gait::Pulse => "pulse",
    };
    match rig.gait.style {
        Gait::Slither => slither_clip(rig, name),
        Gait::Fly => fly_clip(rig, name),
        Gait::Pulse => pulse_clip(rig, name),
        // Walk and Crawl both use the legged gait (crawl is lower + slower).
        _ => legged_clip(rig, name),
    }
}

/// Phase-offset leg swing across `gait.legs` with knee bend behind the body
/// and a gentle spine counter-rotation — a diagonal-sequence walk.
fn legged_clip(rig: &MonsterRig, name: &str) -> AnimationClip {
    let g = &rig.gait;
    let dur = if name == "crawl" { 1.4 } else { 1.0 };
    let t = keys(20, dur);
    let swing = if name == "crawl" { 0.35 } else { 0.55 };
    let mut channels = Vec::new();
    // diagonal gait: legs offset by a half-cycle in a front-left/rear-right
    // vs front-right/rear-left pattern. Offsets by leg index give a natural
    // sequence for the canonical FL, FR, RL, RR ordering.
    let offsets = [0.0f32, 0.5, 0.5, 0.0];
    for (li, chain) in g.legs.iter().enumerate() {
        let off = offsets.get(li).copied().unwrap_or(li as f32 * 0.25) * TAU;
        // upper: fore-aft swing
        if let Some(&up) = chain.first() {
            channels.push(rot_channel(up, &t, move |p| {
                Quat::from_rotation_x(swing * (p * TAU + off).sin())
            }));
        }
        // lower: bend as the leg passes behind (positive knee tuck)
        if chain.len() >= 2 {
            let lo = chain[1];
            channels.push(rot_channel(lo, &t, move |p| {
                Quat::from_rotation_x(-(0.8 * (p * TAU + off + 0.6).sin()).max(0.0) - 0.05)
            }));
        }
        // foot: subtle counter-roll to plant
        if chain.len() >= 3 {
            let ft = chain[2];
            channels.push(rot_channel(ft, &t, move |p| {
                Quat::from_rotation_x(0.2 * (p * TAU + off + 1.2).sin())
            }));
        }
    }
    // spine counter-rotation (yaw) and a small head bob
    for (i, &j) in g.spine.iter().enumerate().skip(1) {
        let amp = 0.05 / (i as f32);
        channels.push(rot_channel(j, &t, move |p| {
            Quat::from_rotation_y(amp * (p * TAU).sin())
        }));
    }
    if let Some(h) = g.head {
        channels.push(rot_channel(h, &t, move |p| {
            Quat::from_rotation_x(0.04 * (p * TAU * 2.0).sin())
        }));
    }
    for (i, &j) in g.tail.iter().enumerate() {
        let ph = i as f32 * 0.6;
        channels.push(rot_channel(j, &t, move |p| {
            Quat::from_rotation_y(0.12 * (p * TAU + ph).sin())
        }));
    }
    // body bob: twice per cycle (each diagonal plant)
    let hips = g.spine[0];
    let bob = 0.02 * body_scale(rig) * if name == "crawl" { 0.4 } else { 1.0 };
    channels.push(trans_channel(hips, &t, bind_of(rig, hips), move |p| {
        Vec3::Y * bob * (p * TAU * 2.0).sin().abs()
    }));
    AnimationClip {
        name: name.into(),
        channels,
    }
}

/// Sinusoidal wave travelling down the spine (serpents/wyrms).
fn slither_clip(rig: &MonsterRig, name: &str) -> AnimationClip {
    let g = &rig.gait;
    let dur = 1.6;
    let t = keys(24, dur);
    let mut channels = Vec::new();
    let n = g.spine.len().max(1) as f32;
    for (i, &j) in g.spine.iter().enumerate() {
        let ph = i as f32 / n * TAU;
        channels.push(rot_channel(j, &t, move |p| {
            Quat::from_rotation_y(0.4 * (p * TAU + ph).sin())
        }));
    }
    for (i, &j) in g.tail.iter().enumerate() {
        let ph = (g.spine.len() + i) as f32 / n * TAU;
        channels.push(rot_channel(j, &t, move |p| {
            Quat::from_rotation_y(0.5 * (p * TAU + ph).sin())
        }));
    }
    AnimationClip {
        name: name.into(),
        channels,
    }
}

/// Wing flap on `gait.wings` plus a body pitch bob (flyers).
fn fly_clip(rig: &MonsterRig, name: &str) -> AnimationClip {
    let g = &rig.gait;
    let dur = 0.9;
    let t = keys(20, dur);
    let mut channels = Vec::new();
    for (i, &j) in g.wings.iter().enumerate() {
        let side = if i % 2 == 0 { 1.0 } else { -1.0 };
        channels.push(rot_channel(j, &t, move |p| {
            Quat::from_rotation_z(side * (0.7 * (p * TAU).sin()))
        }));
    }
    // if there are legs, tuck them
    for chain in &g.legs {
        if let Some(&up) = chain.first() {
            channels.push(rot_channel(up, &t, move |_| Quat::from_rotation_x(0.5)));
        }
    }
    let hips = g.spine[0];
    let bob = 0.03 * body_scale(rig);
    channels.push(trans_channel(hips, &t, bind_of(rig, hips), move |p| {
        Vec3::Y * bob * (p * TAU).sin()
    }));
    if channels.is_empty() {
        // never emit an empty clip
        channels.push(rot_channel(hips, &t, move |p| {
            Quat::from_rotation_x(0.05 * (p * TAU).sin())
        }));
    }
    AnimationClip {
        name: name.into(),
        channels,
    }
}

/// Uniform vertical squash-and-stretch bob for oozes (no limbs to swing).
fn pulse_clip(rig: &MonsterRig, name: &str) -> AnimationClip {
    let g = &rig.gait;
    let dur = 1.3;
    let t = keys(16, dur);
    let hips = g.spine[0];
    let amp = 0.04 * body_scale(rig);
    let channels = vec![trans_channel(hips, &t, bind_of(rig, hips), move |p| {
        Vec3::Y * amp * (p * TAU).sin()
    })];
    AnimationClip {
        name: name.into(),
        channels,
    }
}

/// Lunge forward + head/maw snap.
pub(crate) fn attack_clip(rig: &MonsterRig) -> AnimationClip {
    let g = &rig.gait;
    let dur = 0.85;
    let t = keys(20, dur);
    let lunge = move |p: f32| env(p, 0.2, 0.4) - env(p, 0.5, 0.95);
    let mut channels = Vec::new();
    // spine rears then drives forward
    for (i, &j) in g.spine.iter().enumerate().skip(1) {
        let amp = 0.25 / (i as f32);
        channels.push(rot_channel(j, &t, move |p| {
            Quat::from_rotation_x(-amp * lunge(p))
        }));
    }
    if let Some(h) = g.head {
        channels.push(rot_channel(h, &t, move |p| {
            Quat::from_rotation_x(0.5 * lunge(p))
        }));
    }
    // front legs plant/pull, rear legs push
    for (li, chain) in g.legs.iter().enumerate() {
        if let Some(&up) = chain.first() {
            let dir = if li < 2 { -0.4 } else { 0.5 };
            channels.push(rot_channel(up, &t, move |p| {
                Quat::from_rotation_x(dir * lunge(p))
            }));
        }
    }
    let hips = g.spine[0];
    let reach = 0.08 * body_scale(rig);
    channels.push(trans_channel(hips, &t, bind_of(rig, hips), move |p| {
        // +Z forward per the quadruped layout
        Vec3::new(0.0, 0.0, reach * lunge(p))
    }));
    AnimationClip {
        name: "attack".into(),
        channels,
    }
}

/// Sharp recoil then settle.
pub(crate) fn hurt_clip(rig: &MonsterRig) -> AnimationClip {
    let g = &rig.gait;
    let dur = 0.5;
    let t = keys(14, dur);
    let hit = move |p: f32| env(p, 0.0, 0.15) - env(p, 0.2, 1.0);
    let mut channels = Vec::new();
    for (i, &j) in g.spine.iter().enumerate().skip(1) {
        let amp = 0.3 / (i as f32);
        channels.push(rot_channel(j, &t, move |p| {
            Quat::from_rotation_x(amp * hit(p)) * Quat::from_rotation_z(0.12 * hit(p))
        }));
    }
    if let Some(h) = g.head {
        channels.push(rot_channel(h, &t, move |p| {
            Quat::from_rotation_x(0.35 * hit(p))
        }));
    }
    let hips = g.spine[0];
    let dip = 0.03 * body_scale(rig);
    channels.push(trans_channel(hips, &t, bind_of(rig, hips), move |p| {
        Vec3::new(0.0, -dip * hit(p), -dip * hit(p))
    }));
    AnimationClip {
        name: "hurt".into(),
        channels,
    }
}

/// Buckle and topple to the side, then settle.
pub(crate) fn death_clip(rig: &MonsterRig) -> AnimationClip {
    let g = &rig.gait;
    let dur = 1.4;
    let t = keys(22, dur);
    let buckle = move |p: f32| env(p, 0.0, 0.35);
    let fall = move |p: f32| env(p, 0.3, 0.9);
    let mut channels = Vec::new();
    // legs collapse
    for chain in &g.legs {
        if let Some(&up) = chain.first() {
            channels.push(rot_channel(up, &t, move |p| {
                Quat::from_rotation_x(0.6 * buckle(p))
            }));
        }
        if chain.len() >= 2 {
            let lo = chain[1];
            channels.push(rot_channel(lo, &t, move |p| {
                Quat::from_rotation_x(-1.1 * buckle(p))
            }));
        }
    }
    // spine sags
    for (i, &j) in g.spine.iter().enumerate().skip(1) {
        let amp = 0.2 / (i as f32);
        channels.push(rot_channel(j, &t, move |p| {
            Quat::from_rotation_x(-amp * fall(p))
        }));
    }
    if let Some(h) = g.head {
        channels.push(rot_channel(h, &t, move |p| {
            Quat::from_rotation_x(-0.8 * fall(p))
        }));
    }
    // hips roll onto their side and drop to the ground
    let hips = g.spine[0];
    let hips_bind = bind_of(rig, hips);
    let drop = hips_bind.y * 0.75;
    channels.push(Channel {
        joint: hips,
        times: t.clone(),
        data: ChannelData::Rotation(
            t.iter()
                .map(|&tt| Quat::from_rotation_z(1.5 * fall(tt / dur)))
                .collect(),
        ),
    });
    channels.push(trans_channel(hips, &t, hips_bind, move |p| {
        Vec3::Y * -drop * fall(p)
    }));
    AnimationClip {
        name: "death".into(),
        channels,
    }
}

/// Head raise + jaw/maw open, chest swell.
pub(crate) fn roar_clip(rig: &MonsterRig) -> AnimationClip {
    let g = &rig.gait;
    let dur = 1.5;
    let t = keys(20, dur);
    let up = move |p: f32| env(p, 0.1, 0.35) - env(p, 0.75, 1.0);
    let mut channels = Vec::new();
    // rear the neck/head back and up
    for (i, &j) in g.spine.iter().enumerate().skip(1) {
        let amp = 0.3 / (i as f32);
        channels.push(rot_channel(j, &t, move |p| {
            Quat::from_rotation_x(-amp * up(p))
        }));
    }
    if let Some(h) = g.head {
        channels.push(rot_channel(h, &t, move |p| {
            // raise + a small tremor while roaring
            Quat::from_rotation_x(-0.5 * up(p) + 0.04 * (p * TAU * 6.0).sin() * up(p))
        }));
    }
    let hips = g.spine[0];
    let rise = 0.03 * body_scale(rig);
    channels.push(trans_channel(hips, &t, bind_of(rig, hips), move |p| {
        Vec3::Y * rise * up(p)
    }));
    AnimationClip {
        name: "roar".into(),
        channels,
    }
}

#[cfg(test)]
mod tests {
    use super::super::rig::build_rig;
    use super::*;

    #[test]
    fn quadruped_has_expected_clips() {
        let p = MonsterParams::default();
        let rig = build_rig(&p);
        let clips = build_clips(&rig, &p);
        let names: Vec<_> = clips.iter().map(|c| c.name.as_str()).collect();
        for want in ["idle", "walk", "attack", "hurt", "death"] {
            assert!(names.contains(&want), "missing clip {want}");
        }
        // every channel targets a real joint, durations > 0
        for c in &clips {
            assert!(crate::anim::clip_duration(c) > 0.0);
            for ch in &c.channels {
                assert!((ch.joint) < rig.skeleton.joints.len());
            }
        }
    }
}