use super::SceneCase;
use crate::{
env as e,
ngui::{Object, V, vector},
scene3d::{
self as s, Layout,
anim::{
humanoid::{self as h, AVATAR, OPTIONAL_BONES, Rig},
playback::{self as p, Dense, Storage},
},
},
};
const SPINE: [u32; 3] = [42, 43, 44];
const FOREARM: u32 = 42 + 42;
const HAND: u32 = 42 + 44;
const UPPER_CHEST: u32 = 42 + 6;
const LOOSE: i64 = 204;
#[must_use]
pub fn anim_humanoid_cases() -> Vec<(String, SceneCase)> {
let mut out = Vec::new();
for (name, data) in files() {
for flags in [0, 3] {
for meter in [0, 1, 16, 64] {
out.push((
format!("{name}-f{flags}-m{meter}"),
SceneCase {
meter,
flags,
profile: Vec::new(),
data: data.clone(),
},
));
}
}
}
out
}
fn files() -> Vec<(String, Vec<u8>)> {
let mut out = Vec::new();
for (name, layout, objects) in variants() {
let file = s::file(layout, &[], &objects).expect("synthetic humanoid rig");
let bundle = e::bundle(&[("CAB-humanoid", file.clone())]);
out.push((format!("{name}-file"), file));
out.push((format!("{name}-bundle"), bundle));
}
out
}
fn variants() -> Vec<(String, Layout, Vec<Object>)> {
let mut out = Vec::new();
for (name, rig) in rigs() {
out.push((
format!("rig-{name}"),
Layout::Unity2022,
rig_with(&rig, muscles()),
));
}
let clips = clips()
.into_iter()
.map(|(name, clip)| (name.to_owned(), clip))
.chain(root_clips());
for (name, clip) in clips {
out.push((
format!("clip-{name}"),
Layout::Unity2022,
rig_with(&Rig::default(), clip),
));
}
for (name, field, value) in avatar_edits() {
let mut objects = rig_with(&Rig::default(), muscles());
for object in &mut objects {
if object.path_id == AVATAR {
edit(&mut object.value, &["m_Avatar"], field, value.clone());
}
}
out.push((format!("avatar-{name}"), Layout::Unity2022, objects));
}
let mut objects = rig_with(&body(&[]), muscles());
for object in &mut objects {
if object.path_id == AVATAR {
let ints = vector(V::Int(0), vec![V::Int(0); 25]);
edit(
&mut object.value,
&["m_Avatar", "m_Human", "data"],
"m_HumanBoneMass",
ints,
);
}
}
out.push(("avatar-mass-ints".to_owned(), Layout::Unity2022, objects));
for (name, layout) in [("6000", Layout::Unity6000), ("2019", Layout::Unity2019)] {
out.push((
format!("layout-{name}"),
layout,
rig_with(&Rig::default(), muscles()),
));
}
out
}
fn rig_with(rig: &Rig, clip: Object) -> Vec<Object> {
let mut objects = h::prefab(rig);
objects.push(clip);
let mut loose = h::clip(&[HAND, FOREARM], &[]);
loose.path_id = LOOSE;
objects.push(loose);
objects
}
fn muscles() -> Object {
h::clip(&SPINE, &[])
}
fn rig(edit: impl FnOnce(&mut Rig)) -> Rig {
let mut rig = Rig::default();
edit(&mut rig);
rig
}
fn rigs() -> Vec<(&'static str, Rig)> {
vec![
("neutral", Rig::default()),
("body-offset", rig(|r| r.body_offset = [0., 0.9, 0.])),
("no-avatar", rig(|r| r.avatar = false)),
("optimized", rig(|r| r.transform_hierarchy = false)),
("no-tos", rig(|r| r.no_tos = Some(3))),
("renamed", rig(|r| r.renamed = Some(2))),
("unmapped", rig(|r| r.unmapped_bone = Some(10))),
("unmapped-spine", rig(|r| r.unmapped_bone = Some(7))),
("root-offset", rig(|r| r.root_offset = true)),
("root-motion", rig(|r| r.root_motion_bone = 1)),
("translation", rig(|r| r.translation_dof = true)),
("no-axes", rig(|r| r.no_axes = Some(7))),
("twist", rig(|r| r.twist = [0.5, 0.25, 0.75, 0.375])),
("stretch", rig(|r| r.stretch = [0.05, 0.1, 0.2])),
("body", body(&[])),
("body-no-extras", body(&[21, 22, 23])),
("body-no-shoulders", body(&[11, 12, 21, 22, 23])),
("body-no-chest", body(&[8, 54])),
("body-no-neck", body(&[9])),
("body-one-shoulder", body(&[11])),
("body-one-eye", body(&[21])),
("body-leaf-mass", body_mass(|_| {})),
("body-mass-nan", body_mass(|m| m[7] = f32::NAN)),
("body-mass-negative", body_mass(|m| m[3] = -m[3])),
("body-mass-short", body_mass(|m| m.truncate(24))),
("body-mass-heavy", body_mass(|m| m[0] *= 1.5)),
("body-mass-absent", {
let mut rig = body_mass(|_| {});
rig.optional.retain(|&bone| bone != 23);
rig
}),
(
"body-scale",
Rig {
scale: 0.,
body_offset: [0.; 3],
..body(&[])
},
),
(
"body-hierarchy",
Rig {
hierarchy: true,
..body(&[])
},
),
(
"body-hierarchy-armature",
Rig {
hierarchy: true,
armature: vec![([0., 0.05, 0.1], [0., 0.6, 0., 0.8], 0.8)],
..body(&[])
},
),
(
"body-hierarchy-deep-armature",
Rig {
hierarchy: true,
armature: vec![
([0., 0.05, 0.1], [0., 0.6, 0., 0.8], 0.8),
([0.25, 0., 0.], [0.6, 0., 0., 0.8], 1.5),
],
..body(&[])
},
),
(
"body-hierarchy-no-upper-chest",
Rig {
hierarchy: true,
..body(&[54])
},
),
(
"body-root-height",
Rig {
body_offset: [0., 1.5, 0.],
..body(&[])
},
),
]
}
fn body(absent: &[usize]) -> Rig {
Rig {
optional: OPTIONAL_BONES
.iter()
.map(|&(bone, _)| bone)
.filter(|bone| !absent.contains(bone))
.collect(),
body_offset: [0., 1., 0.],
..Rig::default()
}
}
fn body_mass(edit: impl FnOnce(&mut Vec<f32>)) -> Rig {
let mut masses = vec![1. / 32.; 25];
masses[0] = 1. - 24. / 32.;
edit(&mut masses);
Rig {
bone_mass: Some(masses),
..body(&[])
}
}
fn flag(name: &'static str, value: bool) -> (&'static str, V) {
(name, V::Bool(value))
}
fn index_array(entries: &[(usize, i32)], len: usize) -> V {
let mut index = vec![-1; len];
for &(dof, slot) in entries {
if let Some(entry) = index.get_mut(dof) {
*entry = slot;
}
}
vector(V::Int(0), index.into_iter().map(V::Int).collect())
}
fn muscle_field(mut clip: Object, name: &str, value: Option<V>) -> Object {
if let V::Struct(_, root) = &mut clip.value
&& let Some((_, muscle)) = root.iter_mut().find(|(field, _)| field == "m_MuscleClip")
{
match value {
Some(value) => s::set(muscle, name, value),
None => {
if let V::Struct(_, fields) = muscle {
fields.retain(|(field, _)| field != name);
}
}
}
}
clip
}
fn dense(rate: f32, bits: &[u32]) -> Object {
h::clip_stored(
&[7, 42],
&[],
&[],
Storage {
interval: [0., 1.],
dense: Dense {
frames: 2,
curves: 2,
rate,
begin: 0.,
bits,
},
..Storage::default()
},
)
}
fn root_clips() -> Vec<(String, Object)> {
const BAKE: [&str; 3] = [
"m_LoopBlendOrientation",
"m_LoopBlendPositionY",
"m_LoopBlendPositionXZ",
];
let mut out = vec![
("root-body".to_owned(), root_clip(&[], 0, false)),
(
"root-level".to_owned(),
root_clip(&[("m_Level", V::Float(0.125))], 0, false),
),
(
"root-orientation-offset".to_owned(),
root_clip(&[("m_OrientationOffsetY", V::Float(30.))], 0, false),
),
(
"root-original".to_owned(),
root_clip(
&[
flag("m_KeepOriginalOrientation", true),
flag("m_KeepOriginalPositionY", true),
flag("m_KeepOriginalPositionXZ", true),
],
0,
true,
),
),
("root-streamed".to_owned(), root_clip(&[], 7, false)),
(
"root-original-streamed".to_owned(),
root_clip(
&[
flag("m_KeepOriginalOrientation", true),
flag("m_KeepOriginalPositionY", true),
flag("m_KeepOriginalPositionXZ", true),
],
14,
true,
),
),
];
for bits in 1..8_usize {
let flags: Vec<_> = (0..3)
.filter(|k| bits >> (2 - k) & 1 == 1)
.map(|k| flag(BAKE[k], true))
.collect();
out.push((
format!(
"root-bake-o{}-y{}-xz{}",
bits >> 2 & 1,
bits >> 1 & 1,
bits & 1
),
root_clip(&flags, 0, false),
));
}
out
}
fn root_clip(settings: &[(&str, V)], streamed: u32, motion: bool) -> Object {
let all: [f32; 14] = [
1., 0.5, 2., 0., 0.6, 0., 0.8, 0.25, 1., -0.5, 0.1, 0.3, -0.05, 0.9,
];
let from: u32 = if motion { 0 } else { 7 };
let pose = &all[from as usize..];
let dofs: Vec<u32> = (from..14).collect();
let bits = |v: f32| v.to_bits();
let mut words = Vec::new();
let keys = |slope: f32| -> Vec<(u32, [u32; 4])> {
(0..streamed)
.zip(pose)
.map(|(slot, &v)| (slot, [0, 0, bits(slope), bits(v)]))
.collect()
};
if streamed > 0 {
p::frame(&mut words, 0., &keys(0.25));
p::frame(&mut words, 1., &keys(0.));
p::frame(&mut words, f32::INFINITY, &[]);
}
let constants: Vec<u32> = pose
.iter()
.skip(streamed as usize)
.copied()
.map(bits)
.collect();
h::clip_stored(
&dofs,
&[],
settings,
p::Storage {
interval: [0., 1.],
streamed_count: streamed,
words: &words,
constants: &constants,
..p::Storage::default()
},
)
}
#[allow(clippy::too_many_lines, reason = "one list of clip variants")]
fn clips() -> Vec<(&'static str, Object)> {
let zero = 0_f32.to_bits();
let one = 1_f32.to_bits();
let nan = f32::NAN.to_bits();
let pose = || {
(
"m_ValueArrayReferencePose",
vector(V::Float(0.), vec![V::Float(1.)]),
)
};
let mut keyed = Vec::new();
p::frame(&mut keyed, f32::MIN, &[(0, [zero, zero, zero, zero])]);
p::frame(&mut keyed, 0., &[(0, [zero, zero, one, zero])]);
p::frame(&mut keyed, 1., &[]);
p::frame(&mut keyed, f32::INFINITY, &[]);
let spine_writer = [h::crc32(b"Hips/Spine")];
let prop_writer = [h::crc32(b"Hips/Prop")];
vec![
("writer", h::clip_with(&SPINE, &spine_writer, &[], 0, &[])),
("prop", h::clip_with(&SPINE, &prop_writer, &[], 0, &[])),
("locomotion", h::clip(&[0, 42], &[])),
("ik", h::clip(&[14, 42], &[])),
("translation", h::clip(&[137], &[])),
("reference-pose", h::clip(&SPINE, &[pose()])),
("body", h::clip(&[42, 13], &[])),
("forearm", h::clip(&[FOREARM], &[])),
("upper-chest", h::clip(&[42, UPPER_CHEST], &[])),
("every-dof", h::clip(&[0, 7, 14, 42, 136, 137, 199], &[])),
("level", h::clip(&SPINE, &[("m_Level", V::Float(0.125))])),
(
"mirror",
h::clip(&SPINE, &[flag("m_Mirror", true), flag("m_LoopTime", true)]),
),
(
"position-y",
h::clip(&SPINE, &[flag("m_KeepOriginalPositionY", true)]),
),
(
"position-xz",
h::clip(
&SPINE,
&[
flag("m_KeepOriginalPositionXZ", true),
flag("m_StartAtOrigin", false),
],
),
),
("feet", h::clip(&SPINE, &[flag("m_HeightFromFeet", true)])),
("no-mirror", muscle_field(muscles(), "m_Mirror", None)),
(
"no-origin",
muscle_field(muscles(), "m_StartAtOrigin", None),
),
(
"float-flag",
muscle_field(muscles(), "m_Mirror", Some(V::Float(1.))),
),
("dense", dense(30., &[zero, one, zero, one])),
("dense-nan-rate", dense(f32::NAN, &[zero; 4])),
("dense-nan-body", dense(30., &[nan, zero, zero, zero])),
("dense-nan-muscle", dense(30., &[zero, nan, zero, zero])),
(
"constant-nan",
h::clip_stored(
&[7, 42],
&[],
&[],
Storage {
interval: [0., 1.],
constants: &[nan, zero],
..Storage::default()
},
),
),
("streamed", h::clip_with(&[42], &[], &[], 1, &keyed)),
("streamed-unkeyed", h::clip_with(&[42], &[], &[], 1, &[])),
(
"streamed-huge",
h::clip_with(&[42], &[], &[], u32::MAX, &[]),
),
(
"short-index",
muscle_field(
h::clip(&[42], &[]),
"m_IndexArray",
Some(index_array(&[(42, 0)], 199)),
),
),
(
"shared-index",
muscle_field(
h::clip(&[42, 43], &[]),
"m_IndexArray",
Some(index_array(&[(42, 0), (43, 0)], 200)),
),
),
(
"outside-index",
muscle_field(
h::clip(&[42], &[]),
"m_IndexArray",
Some(index_array(&[(42, i32::MAX), (43, -7)], 200)),
),
),
(
"unknown-dof",
muscle_field(
h::clip(&[250], &[]),
"m_IndexArray",
Some(index_array(&[(42, 0)], 200)),
),
),
("generic-root", generic_root()),
]
}
fn generic_root() -> Object {
let mut clip = muscles();
s::set(&mut clip.value, "m_HasGenericRootTransform", V::Bool(true));
clip
}
fn ints(values: impl IntoIterator<Item = i32>) -> V {
vector(V::Int(0), values.into_iter().map(V::Int).collect())
}
fn avatar_edits() -> Vec<(&'static str, &'static str, V)> {
vec![
("root-motion-bone", "m_RootMotionBoneIndex", V::Int(16)),
("root-motion-negative", "m_RootMotionBoneIndex", V::Int(-2)),
(
"root-motion-index",
"m_RootMotionSkeletonIndexArray",
ints([0]),
),
(
"unmapped-forward",
"m_HumanSkeletonIndexArray",
ints((0..16).map(|i| if i == 5 { -1 } else { i })),
),
(
"crossed-forward",
"m_HumanSkeletonIndexArray",
ints((0..16).map(|i| if i == 5 { 6 } else { i })),
),
(
"short-reverse",
"m_HumanSkeletonReverseIndexArray",
ints(0..15),
),
(
"huge-reverse",
"m_HumanSkeletonReverseIndexArray",
ints((0..16).map(|i| if i == 3 { i32::MAX } else { i })),
),
(
"no-names",
"m_SkeletonNameIDArray",
vector(V::UInt(0), vec![]),
),
]
}
fn edit(value: &mut V, path: &[&str], name: &str, replacement: V) {
match path.split_first() {
None => s::set(value, name, replacement),
Some((first, rest)) => {
if let V::Struct(_, fields) = value
&& let Some((_, inner)) = fields.iter_mut().find(|(field, _)| field == first)
{
edit(inner, rest, name, replacement);
}
}
}
}