1use super::SceneCase;
7use crate::{
8 env as e,
9 ngui::{Object, V, vector},
10 scene3d::{
11 self as s, Layout,
12 anim::{
13 humanoid::{self as h, AVATAR, OPTIONAL_BONES, Rig},
14 playback::{self as p, Dense, Storage},
15 },
16 },
17};
18
19const SPINE: [u32; 3] = [42, 43, 44];
21const FOREARM: u32 = 42 + 42;
23const HAND: u32 = 42 + 44;
24const UPPER_CHEST: u32 = 42 + 6;
26const LOOSE: i64 = 204;
28
29#[must_use]
37pub fn anim_humanoid_cases() -> Vec<(String, SceneCase)> {
38 let mut out = Vec::new();
39 for (name, data) in files() {
40 for flags in [0, 3] {
41 for meter in [0, 1, 16, 64] {
42 out.push((
43 format!("{name}-f{flags}-m{meter}"),
44 SceneCase {
45 meter,
46 flags,
47 profile: Vec::new(),
48 data: data.clone(),
49 },
50 ));
51 }
52 }
53 }
54 out
55}
56
57fn files() -> Vec<(String, Vec<u8>)> {
59 let mut out = Vec::new();
60 for (name, layout, objects) in variants() {
61 let file = s::file(layout, &[], &objects).expect("synthetic humanoid rig");
62 let bundle = e::bundle(&[("CAB-humanoid", file.clone())]);
63 out.push((format!("{name}-file"), file));
64 out.push((format!("{name}-bundle"), bundle));
65 }
66 out
67}
68
69fn variants() -> Vec<(String, Layout, Vec<Object>)> {
70 let mut out = Vec::new();
71 for (name, rig) in rigs() {
72 out.push((
73 format!("rig-{name}"),
74 Layout::Unity2022,
75 rig_with(&rig, muscles()),
76 ));
77 }
78 let clips = clips()
79 .into_iter()
80 .map(|(name, clip)| (name.to_owned(), clip))
81 .chain(root_clips());
82 for (name, clip) in clips {
83 out.push((
84 format!("clip-{name}"),
85 Layout::Unity2022,
86 rig_with(&Rig::default(), clip),
87 ));
88 }
89 for (name, field, value) in avatar_edits() {
90 let mut objects = rig_with(&Rig::default(), muscles());
91 for object in &mut objects {
92 if object.path_id == AVATAR {
93 edit(&mut object.value, &["m_Avatar"], field, value.clone());
94 }
95 }
96 out.push((format!("avatar-{name}"), Layout::Unity2022, objects));
97 }
98 let mut objects = rig_with(&body(&[]), muscles());
100 for object in &mut objects {
101 if object.path_id == AVATAR {
102 let ints = vector(V::Int(0), vec![V::Int(0); 25]);
103 edit(
104 &mut object.value,
105 &["m_Avatar", "m_Human", "data"],
106 "m_HumanBoneMass",
107 ints,
108 );
109 }
110 }
111 out.push(("avatar-mass-ints".to_owned(), Layout::Unity2022, objects));
112 for (name, layout) in [("6000", Layout::Unity6000), ("2019", Layout::Unity2019)] {
113 out.push((
114 format!("layout-{name}"),
115 layout,
116 rig_with(&Rig::default(), muscles()),
117 ));
118 }
119 out
120}
121
122fn rig_with(rig: &Rig, clip: Object) -> Vec<Object> {
125 let mut objects = h::prefab(rig);
126 objects.push(clip);
127 let mut loose = h::clip(&[HAND, FOREARM], &[]);
128 loose.path_id = LOOSE;
129 objects.push(loose);
130 objects
131}
132
133fn muscles() -> Object {
134 h::clip(&SPINE, &[])
135}
136
137fn rig(edit: impl FnOnce(&mut Rig)) -> Rig {
138 let mut rig = Rig::default();
139 edit(&mut rig);
140 rig
141}
142
143fn rigs() -> Vec<(&'static str, Rig)> {
144 vec![
145 ("neutral", Rig::default()),
146 ("body-offset", rig(|r| r.body_offset = [0., 0.9, 0.])),
147 ("no-avatar", rig(|r| r.avatar = false)),
148 ("optimized", rig(|r| r.transform_hierarchy = false)),
149 ("no-tos", rig(|r| r.no_tos = Some(3))),
150 ("renamed", rig(|r| r.renamed = Some(2))),
151 ("unmapped", rig(|r| r.unmapped_bone = Some(10))),
152 ("unmapped-spine", rig(|r| r.unmapped_bone = Some(7))),
153 ("root-offset", rig(|r| r.root_offset = true)),
154 ("root-motion", rig(|r| r.root_motion_bone = 1)),
155 ("translation", rig(|r| r.translation_dof = true)),
156 ("no-axes", rig(|r| r.no_axes = Some(7))),
157 ("twist", rig(|r| r.twist = [0.5, 0.25, 0.75, 0.375])),
158 ("stretch", rig(|r| r.stretch = [0.05, 0.1, 0.2])),
159 ("body", body(&[])),
160 ("body-no-extras", body(&[21, 22, 23])),
161 ("body-no-shoulders", body(&[11, 12, 21, 22, 23])),
162 ("body-no-chest", body(&[8, 54])),
163 ("body-no-neck", body(&[9])),
164 ("body-one-shoulder", body(&[11])),
165 ("body-one-eye", body(&[21])),
166 ("body-leaf-mass", body_mass(|_| {})),
167 ("body-mass-nan", body_mass(|m| m[7] = f32::NAN)),
168 ("body-mass-negative", body_mass(|m| m[3] = -m[3])),
169 ("body-mass-short", body_mass(|m| m.truncate(24))),
170 ("body-mass-heavy", body_mass(|m| m[0] *= 1.5)),
171 ("body-mass-absent", {
172 let mut rig = body_mass(|_| {});
173 rig.optional.retain(|&bone| bone != 23);
174 rig
175 }),
176 (
177 "body-scale",
178 Rig {
179 scale: 0.,
180 body_offset: [0.; 3],
181 ..body(&[])
182 },
183 ),
184 (
185 "body-hierarchy",
186 Rig {
187 hierarchy: true,
188 ..body(&[])
189 },
190 ),
191 (
192 "body-hierarchy-armature",
193 Rig {
194 hierarchy: true,
195 armature: vec![([0., 0.05, 0.1], [0., 0.6, 0., 0.8], 0.8)],
196 ..body(&[])
197 },
198 ),
199 (
200 "body-hierarchy-deep-armature",
201 Rig {
202 hierarchy: true,
203 armature: vec![
204 ([0., 0.05, 0.1], [0., 0.6, 0., 0.8], 0.8),
205 ([0.25, 0., 0.], [0.6, 0., 0., 0.8], 1.5),
206 ],
207 ..body(&[])
208 },
209 ),
210 (
211 "body-hierarchy-no-upper-chest",
212 Rig {
213 hierarchy: true,
214 ..body(&[54])
215 },
216 ),
217 (
218 "body-root-height",
219 Rig {
220 body_offset: [0., 1.5, 0.],
221 ..body(&[])
222 },
223 ),
224 ]
225}
226
227fn body(absent: &[usize]) -> Rig {
230 Rig {
231 optional: OPTIONAL_BONES
232 .iter()
233 .map(|&(bone, _)| bone)
234 .filter(|bone| !absent.contains(bone))
235 .collect(),
236 body_offset: [0., 1., 0.],
237 ..Rig::default()
238 }
239}
240
241fn body_mass(edit: impl FnOnce(&mut Vec<f32>)) -> Rig {
244 let mut masses = vec![1. / 32.; 25];
245 masses[0] = 1. - 24. / 32.;
246 edit(&mut masses);
247 Rig {
248 bone_mass: Some(masses),
249 ..body(&[])
250 }
251}
252
253fn flag(name: &'static str, value: bool) -> (&'static str, V) {
254 (name, V::Bool(value))
255}
256
257fn index_array(entries: &[(usize, i32)], len: usize) -> V {
258 let mut index = vec![-1; len];
259 for &(dof, slot) in entries {
260 if let Some(entry) = index.get_mut(dof) {
261 *entry = slot;
262 }
263 }
264 vector(V::Int(0), index.into_iter().map(V::Int).collect())
265}
266
267fn muscle_field(mut clip: Object, name: &str, value: Option<V>) -> Object {
270 if let V::Struct(_, root) = &mut clip.value
271 && let Some((_, muscle)) = root.iter_mut().find(|(field, _)| field == "m_MuscleClip")
272 {
273 match value {
274 Some(value) => s::set(muscle, name, value),
275 None => {
276 if let V::Struct(_, fields) = muscle {
277 fields.retain(|(field, _)| field != name);
278 }
279 }
280 }
281 }
282 clip
283}
284
285fn dense(rate: f32, bits: &[u32]) -> Object {
286 h::clip_stored(
287 &[7, 42],
288 &[],
289 &[],
290 Storage {
291 interval: [0., 1.],
292 dense: Dense {
293 frames: 2,
294 curves: 2,
295 rate,
296 begin: 0.,
297 bits,
298 },
299 ..Storage::default()
300 },
301 )
302}
303
304fn root_clips() -> Vec<(String, Object)> {
307 const BAKE: [&str; 3] = [
308 "m_LoopBlendOrientation",
309 "m_LoopBlendPositionY",
310 "m_LoopBlendPositionXZ",
311 ];
312 let mut out = vec![
313 ("root-body".to_owned(), root_clip(&[], 0, false)),
314 (
315 "root-level".to_owned(),
316 root_clip(&[("m_Level", V::Float(0.125))], 0, false),
317 ),
318 (
319 "root-orientation-offset".to_owned(),
320 root_clip(&[("m_OrientationOffsetY", V::Float(30.))], 0, false),
321 ),
322 (
323 "root-original".to_owned(),
324 root_clip(
325 &[
326 flag("m_KeepOriginalOrientation", true),
327 flag("m_KeepOriginalPositionY", true),
328 flag("m_KeepOriginalPositionXZ", true),
329 ],
330 0,
331 true,
332 ),
333 ),
334 ("root-streamed".to_owned(), root_clip(&[], 7, false)),
335 (
336 "root-original-streamed".to_owned(),
337 root_clip(
338 &[
339 flag("m_KeepOriginalOrientation", true),
340 flag("m_KeepOriginalPositionY", true),
341 flag("m_KeepOriginalPositionXZ", true),
342 ],
343 14,
344 true,
345 ),
346 ),
347 ];
348 for bits in 1..8_usize {
350 let flags: Vec<_> = (0..3)
351 .filter(|k| bits >> (2 - k) & 1 == 1)
352 .map(|k| flag(BAKE[k], true))
353 .collect();
354 out.push((
355 format!(
356 "root-bake-o{}-y{}-xz{}",
357 bits >> 2 & 1,
358 bits >> 1 & 1,
359 bits & 1
360 ),
361 root_clip(&flags, 0, false),
362 ));
363 }
364 out
365}
366
367fn root_clip(settings: &[(&str, V)], streamed: u32, motion: bool) -> Object {
372 let all: [f32; 14] = [
373 1., 0.5, 2., 0., 0.6, 0., 0.8, 0.25, 1., -0.5, 0.1, 0.3, -0.05, 0.9,
374 ];
375 let from: u32 = if motion { 0 } else { 7 };
376 let pose = &all[from as usize..];
377 let dofs: Vec<u32> = (from..14).collect();
378 let bits = |v: f32| v.to_bits();
379 let mut words = Vec::new();
380 let keys = |slope: f32| -> Vec<(u32, [u32; 4])> {
381 (0..streamed)
382 .zip(pose)
383 .map(|(slot, &v)| (slot, [0, 0, bits(slope), bits(v)]))
384 .collect()
385 };
386 if streamed > 0 {
387 p::frame(&mut words, 0., &keys(0.25));
388 p::frame(&mut words, 1., &keys(0.));
389 p::frame(&mut words, f32::INFINITY, &[]);
390 }
391 let constants: Vec<u32> = pose
392 .iter()
393 .skip(streamed as usize)
394 .copied()
395 .map(bits)
396 .collect();
397 h::clip_stored(
398 &dofs,
399 &[],
400 settings,
401 p::Storage {
402 interval: [0., 1.],
403 streamed_count: streamed,
404 words: &words,
405 constants: &constants,
406 ..p::Storage::default()
407 },
408 )
409}
410
411#[allow(clippy::too_many_lines, reason = "one list of clip variants")]
412fn clips() -> Vec<(&'static str, Object)> {
413 let zero = 0_f32.to_bits();
414 let one = 1_f32.to_bits();
415 let nan = f32::NAN.to_bits();
416 let pose = || {
417 (
418 "m_ValueArrayReferencePose",
419 vector(V::Float(0.), vec![V::Float(1.)]),
420 )
421 };
422 let mut keyed = Vec::new();
423 p::frame(&mut keyed, f32::MIN, &[(0, [zero, zero, zero, zero])]);
424 p::frame(&mut keyed, 0., &[(0, [zero, zero, one, zero])]);
425 p::frame(&mut keyed, 1., &[]);
426 p::frame(&mut keyed, f32::INFINITY, &[]);
427 let spine_writer = [h::crc32(b"Hips/Spine")];
428 let prop_writer = [h::crc32(b"Hips/Prop")];
429 vec![
430 ("writer", h::clip_with(&SPINE, &spine_writer, &[], 0, &[])),
431 ("prop", h::clip_with(&SPINE, &prop_writer, &[], 0, &[])),
432 ("locomotion", h::clip(&[0, 42], &[])),
433 ("ik", h::clip(&[14, 42], &[])),
434 ("translation", h::clip(&[137], &[])),
435 ("reference-pose", h::clip(&SPINE, &[pose()])),
436 ("body", h::clip(&[42, 13], &[])),
437 ("forearm", h::clip(&[FOREARM], &[])),
438 ("upper-chest", h::clip(&[42, UPPER_CHEST], &[])),
439 ("every-dof", h::clip(&[0, 7, 14, 42, 136, 137, 199], &[])),
440 ("level", h::clip(&SPINE, &[("m_Level", V::Float(0.125))])),
441 (
442 "mirror",
443 h::clip(&SPINE, &[flag("m_Mirror", true), flag("m_LoopTime", true)]),
444 ),
445 (
446 "position-y",
447 h::clip(&SPINE, &[flag("m_KeepOriginalPositionY", true)]),
448 ),
449 (
450 "position-xz",
451 h::clip(
452 &SPINE,
453 &[
454 flag("m_KeepOriginalPositionXZ", true),
455 flag("m_StartAtOrigin", false),
456 ],
457 ),
458 ),
459 ("feet", h::clip(&SPINE, &[flag("m_HeightFromFeet", true)])),
460 ("no-mirror", muscle_field(muscles(), "m_Mirror", None)),
461 (
462 "no-origin",
463 muscle_field(muscles(), "m_StartAtOrigin", None),
464 ),
465 (
466 "float-flag",
467 muscle_field(muscles(), "m_Mirror", Some(V::Float(1.))),
468 ),
469 ("dense", dense(30., &[zero, one, zero, one])),
470 ("dense-nan-rate", dense(f32::NAN, &[zero; 4])),
471 ("dense-nan-body", dense(30., &[nan, zero, zero, zero])),
472 ("dense-nan-muscle", dense(30., &[zero, nan, zero, zero])),
473 (
474 "constant-nan",
475 h::clip_stored(
476 &[7, 42],
477 &[],
478 &[],
479 Storage {
480 interval: [0., 1.],
481 constants: &[nan, zero],
482 ..Storage::default()
483 },
484 ),
485 ),
486 ("streamed", h::clip_with(&[42], &[], &[], 1, &keyed)),
487 ("streamed-unkeyed", h::clip_with(&[42], &[], &[], 1, &[])),
488 (
489 "streamed-huge",
490 h::clip_with(&[42], &[], &[], u32::MAX, &[]),
491 ),
492 (
493 "short-index",
494 muscle_field(
495 h::clip(&[42], &[]),
496 "m_IndexArray",
497 Some(index_array(&[(42, 0)], 199)),
498 ),
499 ),
500 (
501 "shared-index",
502 muscle_field(
503 h::clip(&[42, 43], &[]),
504 "m_IndexArray",
505 Some(index_array(&[(42, 0), (43, 0)], 200)),
506 ),
507 ),
508 (
509 "outside-index",
510 muscle_field(
511 h::clip(&[42], &[]),
512 "m_IndexArray",
513 Some(index_array(&[(42, i32::MAX), (43, -7)], 200)),
514 ),
515 ),
516 (
517 "unknown-dof",
518 muscle_field(
519 h::clip(&[250], &[]),
520 "m_IndexArray",
521 Some(index_array(&[(42, 0)], 200)),
522 ),
523 ),
524 ("generic-root", generic_root()),
525 ]
526}
527
528fn generic_root() -> Object {
529 let mut clip = muscles();
530 s::set(&mut clip.value, "m_HasGenericRootTransform", V::Bool(true));
531 clip
532}
533
534fn ints(values: impl IntoIterator<Item = i32>) -> V {
535 vector(V::Int(0), values.into_iter().map(V::Int).collect())
536}
537
538fn avatar_edits() -> Vec<(&'static str, &'static str, V)> {
540 vec![
541 ("root-motion-bone", "m_RootMotionBoneIndex", V::Int(16)),
542 ("root-motion-negative", "m_RootMotionBoneIndex", V::Int(-2)),
543 (
544 "root-motion-index",
545 "m_RootMotionSkeletonIndexArray",
546 ints([0]),
547 ),
548 (
549 "unmapped-forward",
550 "m_HumanSkeletonIndexArray",
551 ints((0..16).map(|i| if i == 5 { -1 } else { i })),
552 ),
553 (
554 "crossed-forward",
555 "m_HumanSkeletonIndexArray",
556 ints((0..16).map(|i| if i == 5 { 6 } else { i })),
557 ),
558 (
559 "short-reverse",
560 "m_HumanSkeletonReverseIndexArray",
561 ints(0..15),
562 ),
563 (
564 "huge-reverse",
565 "m_HumanSkeletonReverseIndexArray",
566 ints((0..16).map(|i| if i == 3 { i32::MAX } else { i })),
567 ),
568 (
569 "no-names",
570 "m_SkeletonNameIDArray",
571 vector(V::UInt(0), vec![]),
572 ),
573 ]
574}
575
576fn edit(value: &mut V, path: &[&str], name: &str, replacement: V) {
578 match path.split_first() {
579 None => s::set(value, name, replacement),
580 Some((first, rest)) => {
581 if let V::Struct(_, fields) = value
582 && let Some((_, inner)) = fields.iter_mut().find(|(field, _)| field == first)
583 {
584 edit(inner, rest, name, replacement);
585 }
586 }
587 }
588}