use super::{Binding, playback as p};
use crate::ngui::{Object, V, pair, pptr, st, vector};
use crate::scene3d::{Layout, game_object, set, transform, xyz};
pub const BONES: [(usize, &str); 15] = [
(0, "Hips"),
(1, "LeftUpperLeg"),
(2, "RightUpperLeg"),
(3, "LeftLowerLeg"),
(4, "RightLowerLeg"),
(5, "LeftFoot"),
(6, "RightFoot"),
(7, "Spine"),
(10, "Head"),
(13, "LeftUpperArm"),
(14, "RightUpperArm"),
(15, "LeftLowerArm"),
(16, "RightLowerArm"),
(17, "LeftHand"),
(18, "RightHand"),
];
pub const OPTIONAL_BONES: [(usize, &str); 10] = [
(8, "Chest"),
(54, "UpperChest"),
(9, "Neck"),
(11, "LeftShoulder"),
(12, "RightShoulder"),
(19, "LeftToes"),
(20, "RightToes"),
(21, "LeftEye"),
(22, "RightEye"),
(23, "Jaw"),
];
pub const ANIMATOR: i64 = 200;
pub const AVATAR: i64 = 201;
pub const CONTROLLER: i64 = 202;
pub const CLIP: i64 = 203;
#[derive(Clone, Debug)]
#[allow(
clippy::struct_excessive_bools,
reason = "independent fixture switches"
)]
pub struct Rig {
pub twist: [f32; 4],
pub stretch: [f32; 3],
pub translation_dof: bool,
pub root_motion_bone: i32,
pub unmapped_bone: Option<usize>,
pub no_axes: Option<usize>,
pub no_tos: Option<usize>,
pub renamed: Option<usize>,
pub transform_hierarchy: bool,
pub avatar: bool,
pub body_offset: [f32; 3],
pub root_offset: bool,
pub optional: Vec<usize>,
pub bone_mass: Option<Vec<f32>>,
pub scale: f32,
pub hierarchy: bool,
pub armature: Vec<([f32; 3], [f32; 4], f32)>,
pub positions: Vec<(usize, [f32; 3])>,
}
impl Default for Rig {
fn default() -> Self {
Self {
twist: [0.; 4],
stretch: [0.; 3],
translation_dof: false,
root_motion_bone: -1,
unmapped_bone: None,
no_axes: None,
no_tos: None,
renamed: None,
transform_hierarchy: true,
avatar: true,
body_offset: [0.; 3],
root_offset: false,
optional: Vec::new(),
bone_mass: None,
scale: 1.,
hierarchy: false,
armature: Vec::new(),
positions: Vec::new(),
}
}
}
#[must_use]
pub fn crc32(bytes: &[u8]) -> u32 {
let mut crc = !0_u32;
for &byte in bytes {
crc ^= u32::from(byte);
for _ in 0..8 {
crc = if crc & 1 == 1 {
(crc >> 1) ^ 0xedb8_8320
} else {
crc >> 1
};
}
}
!crc
}
fn bones(rig: &Rig) -> Vec<(usize, &'static str)> {
let mut out = BONES.to_vec();
out.extend(
rig.optional
.iter()
.filter_map(|bone| OPTIONAL_BONES.iter().find(|(b, _)| b == bone)),
);
out
}
fn rig_parent(rig: &Rig, bones: &[(usize, &str)], bone: usize) -> Option<usize> {
if bone == 0 {
return None;
}
if !rig.hierarchy {
return Some(0);
}
let mut up = trait_parent(bones[bone].0);
while let Some(b) = up {
if let Some(k) = bones.iter().position(|&(other, _)| other == b) {
return Some(k);
}
up = trait_parent(b);
}
Some(0)
}
fn path(rig: &Rig, bones: &[(usize, &str)], bone: usize) -> String {
let mut names = vec![bones[bone].1];
let mut at = bone;
while let Some(parent) = rig_parent(rig, bones, at) {
names.push(bones[parent].1);
at = parent;
}
let armature: Vec<String> = (0..rig.armature.len())
.map(|k| format!("Armature{k}"))
.collect();
names.extend(armature.iter().rev().map(String::as_str));
names.reverse();
names.join("/")
}
const fn slot(bone: usize) -> usize {
match bone {
0..=8 => bone,
54 => 9,
_ => bone + 1,
}
}
fn ints(items: impl IntoIterator<Item = i32>) -> V {
vector(V::Int(0), items.into_iter().map(V::Int).collect())
}
fn uints(items: impl IntoIterator<Item = u32>) -> V {
vector(V::UInt(0), items.into_iter().map(V::UInt).collect())
}
fn xform() -> V {
xform_at([0.; 3])
}
fn xform_at(t: [f32; 3]) -> V {
st(
"xform",
vec![
("t", xyz("float3", &t)),
("q", xyz("float4", &[0., 0., 0., 1.])),
("s", xyz("float3", &[1., 1., 1.])),
],
)
}
fn data(value: V) -> V {
st("OffsetPtr", vec![("data", value)])
}
fn axes() -> V {
st(
"Axes",
vec![
("m_PreQ", xyz("float4", &[-0.5, -0.5, 0.5, 0.5])),
("m_PostQ", xyz("float4", &[-0.5, -0.5, 0.5, 0.5])),
("m_Sgn", xyz("float3", &[1., 1., 1.])),
(
"m_Limit",
st(
"Limit",
vec![
("m_Min", xyz("float3", &[-0.5, -0.5, -0.5])),
("m_Max", xyz("float3", &[0.5, 0.5, 0.5])),
],
),
),
("m_Length", V::Float(0.1)),
("m_Type", V::Int(1)),
],
)
}
fn skeleton(rig: &Rig, axes_rows: bool) -> V {
let bones = bones(rig);
let nodes = 1 + bones.len();
let node = |parent: i32, axes: i32| {
st(
"Node",
vec![("m_ParentId", V::Int(parent)), ("m_AxesId", V::Int(axes))],
)
};
let mut rows = vec![node(-1, -1)];
let mut records = Vec::new();
for bone in 0..bones.len() {
let axes_id = if axes_rows && rig.no_axes != Some(bone) {
records.push(axes());
i32::try_from(records.len() - 1).unwrap_or_default()
} else {
-1
};
rows.push(node(i32::from(bone != 0), axes_id));
}
let ids = (0..nodes).map(|i| {
if i == 0 {
0
} else {
crc32(path(rig, &bones, i - 1).as_bytes())
}
});
data(st(
"Skeleton",
vec![
("m_Node", vector(node(-1, -1), rows)),
("m_ID", uints(ids)),
("m_AxesArray", vector(axes(), records)),
],
))
}
fn pose(count: usize) -> V {
data(st(
"SkeletonPose",
vec![("m_X", vector(xform(), vec![xform(); count]))],
))
}
fn human_bones(rig: &Rig) -> [i32; 25] {
let mut index = [-1_i32; 25];
for (node, &(bone, _)) in bones(rig).iter().enumerate() {
if rig.unmapped_bone != Some(bone) {
index[slot(bone)] = i32::try_from(node + 1).unwrap_or_default();
}
}
index
}
fn bone_mass(rig: &Rig, index: &[i32; 25]) -> V {
let masses = rig.bone_mass.clone().unwrap_or_else(|| {
let heavy = |(slot, &node): (usize, &i32)| node >= 0 && slot < 22;
let mapped = index.iter().enumerate().filter(|&e| heavy(e)).count();
#[allow(clippy::cast_precision_loss, reason = "at most 25 slots")]
let share = 1. / mapped.max(1) as f32;
index
.iter()
.enumerate()
.map(|e| if heavy(e) { share } else { 0. })
.collect()
});
vector(V::Float(0.), masses.into_iter().map(V::Float).collect())
}
#[must_use]
pub fn avatar(rig: &Rig) -> Object {
let bones = bones(rig);
let nodes = 1 + bones.len();
let index = human_bones(rig);
let hand = || data(st("Hand", vec![("m_HandBoneIndex", ints([-1; 15]))]));
let [arm, fore_arm, upper_leg, leg] = rig.twist;
let [arm_stretch, leg_stretch, feet] = rig.stretch;
let human = data(st(
"Human",
vec![
("m_RootX", xform_at(rig.body_offset)),
("m_Skeleton", skeleton(rig, true)),
("m_SkeletonPose", pose(nodes)),
("m_LeftHand", hand()),
("m_RightHand", hand()),
("m_HumanBoneIndex", ints(index)),
("m_HumanBoneMass", bone_mass(rig, &index)),
("m_Scale", V::Float(rig.scale)),
("m_ArmTwist", V::Float(arm)),
("m_ForeArmTwist", V::Float(fore_arm)),
("m_UpperLegTwist", V::Float(upper_leg)),
("m_LegTwist", V::Float(leg)),
("m_ArmStretch", V::Float(arm_stretch)),
("m_LegStretch", V::Float(leg_stretch)),
("m_FeetSpacing", V::Float(feet)),
("m_HasLeftHand", V::Bool(false)),
("m_HasRightHand", V::Bool(false)),
("m_HasTDoF", V::Bool(rig.translation_dof)),
],
));
let identity = || ints((0..nodes).map(|i| i32::try_from(i).unwrap_or_default()));
let mut tos = vec![pair(V::UInt(0), V::Str(Vec::new()))];
for bone in 0..bones.len() {
if rig.no_tos != Some(bone) {
let path = path(rig, &bones, bone);
tos.push(pair(
V::UInt(crc32(path.as_bytes())),
V::Str(path.into_bytes()),
));
}
}
let runtime = st(
"AvatarConstant",
vec![
("m_AvatarSkeleton", skeleton(rig, false)),
("m_AvatarSkeletonPose", pose(nodes)),
("m_DefaultPose", pose(nodes)),
(
"m_SkeletonNameIDArray",
uints((0..nodes).map(|i| u32::try_from(i).unwrap_or_default())),
),
("m_Human", human),
("m_HumanSkeletonIndexArray", identity()),
("m_HumanSkeletonReverseIndexArray", identity()),
("m_RootMotionBoneIndex", V::Int(rig.root_motion_bone)),
("m_RootMotionBoneX", xform()),
(
"m_RootMotionSkeleton",
data(st(
"Skeleton",
vec![
(
"m_Node",
vector(
st(
"Node",
vec![("m_ParentId", V::Int(-1)), ("m_AxesId", V::Int(-1))],
),
vec![],
),
),
("m_ID", uints([])),
("m_AxesArray", vector(axes(), vec![])),
],
)),
),
("m_RootMotionSkeletonPose", pose(0)),
("m_RootMotionSkeletonIndexArray", ints([])),
],
);
let mut avatar = Object::new(
AVATAR,
90,
st(
"Avatar",
vec![
("m_Name", V::Str(b"avatar".to_vec())),
("m_Avatar", runtime),
("m_TOS", vector(pair(V::UInt(0), V::Str(Vec::new())), tos)),
],
),
);
if rig.hierarchy {
arrange(rig, &mut avatar.value);
}
avatar
}
#[must_use]
pub const fn trait_parent(bone: usize) -> Option<usize> {
Some(match bone {
1 | 2 | 7 => 0,
3 => 1,
4 => 2,
5 => 3,
6 => 4,
8 => 7,
54 => 8,
9 | 11 | 12 => 54,
10 => 9,
13 => 11,
14 => 12,
15 => 13,
16 => 14,
17 => 15,
18 => 16,
19 => 5,
20 => 6,
21..=23 => 10,
_ => return None,
})
}
#[must_use]
pub const fn t_pose(bone: usize) -> [f32; 3] {
match bone {
0 => [0., 1., 0.],
1 => [-0.125, 0.875, 0.],
2 => [0.125, 0.875, 0.],
3 => [-0.125, 0.5, 0.],
4 => [0.125, 0.5, 0.],
5 => [-0.125, 0.125, 0.],
6 => [0.125, 0.125, 0.],
7 => [0., 1.125, 0.],
8 => [0., 1.25, 0.],
54 => [0., 1.375, 0.],
9 => [0., 1.5, 0.],
10 => [0., 1.625, 0.],
11 => [-0.0625, 1.4375, 0.],
12 => [0.0625, 1.4375, 0.],
13 => [-0.1875, 1.4375, 0.],
14 => [0.1875, 1.4375, 0.],
15 => [-0.4375, 1.4375, 0.],
16 => [0.4375, 1.4375, 0.],
17 => [-0.6875, 1.4375, 0.],
18 => [0.6875, 1.4375, 0.],
19 => [-0.125, 0., 0.125],
20 => [0.125, 0., 0.125],
21 => [-0.03125, 1.6875, 0.0625],
22 => [0.03125, 1.6875, 0.0625],
_ => [0., 1.5625, 0.0625],
}
}
fn field<'v>(value: &'v mut V, path: &[&str]) -> Option<&'v mut V> {
path.iter().try_fold(value, |value, name| match value {
V::Struct(_, fields) => fields.iter_mut().find(|(f, _)| f == name).map(|f| &mut f.1),
_ => None,
})
}
fn rows<'v>(value: &'v mut V, path: &[&str]) -> &'v mut [V] {
match field(value, path) {
Some(V::Vector(_, _, items)) => items,
_ => &mut [],
}
}
fn arrange(rig: &Rig, avatar: &mut V) {
let bones: Vec<usize> = bones(rig).iter().map(|&(bone, _)| bone).collect();
let node = |bone: usize| bones.iter().position(|&b| b == bone).map(|k| k + 1);
let parent = |bone: usize| {
let mut up = trait_parent(bone);
while let Some(b) = up {
if node(b).is_some() {
return Some(b);
}
up = trait_parent(b);
}
None
};
let n = bones.len() + 1;
let up: Vec<usize> = (0..n)
.map(|k| match k.checked_sub(1).and_then(|b| bones.get(b)) {
Some(&bone) => parent(bone).and_then(node).unwrap_or(0),
None => 0,
})
.collect();
let depth = |mut k: usize| {
let mut d = 0;
while k != 0 && d < n {
k = up[k];
d += 1;
}
d
};
let mut order: Vec<usize> = (0..n).collect();
order.sort_by_key(|&k| (depth(k), k));
let mut moved = vec![0; n];
for (new, &old) in order.iter().enumerate() {
moved[old] = new;
}
let human = ["m_Avatar", "m_Human", "data"];
let at = |tail: &[&'static str]| [&human[..], tail].concat();
let permute = |items: &mut [V]| {
let old = items.to_vec();
for (item, &k) in items.iter_mut().zip(&order) {
*item = old[k].clone();
}
};
let place = |bone: usize| {
rig.positions
.iter()
.find(|&&(b, _)| b == bone)
.map_or_else(|| t_pose(bone), |&(_, at)| at)
};
let pose = rows(avatar, &at(&["m_SkeletonPose", "data", "m_X"]));
for (k, &bone) in bones.iter().enumerate() {
let from = parent(bone).map_or([0.; 3], place);
let to = place(bone);
if let Some(x) = pose.get_mut(k + 1) {
set(x, "t", xyz("float3", &[0, 1, 2].map(|c| to[c] - from[c])));
}
}
permute(pose);
let nodes = rows(avatar, &at(&["m_Skeleton", "data", "m_Node"]));
for (k, row) in nodes.iter_mut().enumerate().skip(1) {
let parent = i32::try_from(moved[up[k]]).unwrap_or_default();
set(row, "m_ParentId", V::Int(parent));
}
permute(nodes);
permute(rows(avatar, &at(&["m_Skeleton", "data", "m_ID"])));
for slot in rows(avatar, &at(&["m_HumanBoneIndex"])) {
if let V::Int(index) = slot
&& let Ok(old) = usize::try_from(*index)
&& let Some(&new) = moved.get(old)
{
*index = i32::try_from(new).unwrap_or_default();
}
}
}
#[must_use]
pub fn prefab(rig: &Rig) -> Vec<Object> {
let bones = bones(rig);
let mut objects = vec![
Object::new(1, 1, game_object(b"rig", &[(0, 101), (0, ANIMATOR)])),
Object::new(101, 4, {
let first = if rig.armature.is_empty() { 102 } else { 190 };
let mut root = transform((0, 1), (0, 0), &[(0, first)], false);
if rig.root_offset {
set(
&mut root,
"m_LocalPosition",
xyz("Vector3f", &[2., 0.25, -1.]),
);
}
root
}),
];
for (k, &(position, rotation, scale)) in rig.armature.iter().enumerate() {
let id = 90 + i64::try_from(k).unwrap_or_default();
let name = format!("Armature{k}");
objects.push(Object::new(
id,
1,
game_object(name.as_bytes(), &[(0, id + 100)]),
));
let father = if k == 0 { 101 } else { id + 99 };
let child = if k + 1 == rig.armature.len() {
102
} else {
id + 101
};
let mut node = transform((0, id), (0, father), &[(0, child)], false);
set(&mut node, "m_LocalPosition", xyz("Vector3f", &position));
set(&mut node, "m_LocalRotation", xyz("Quaternionf", &rotation));
set(&mut node, "m_LocalScale", xyz("Vector3f", &[scale; 3]));
objects.push(Object::new(id + 100, 4, node));
}
let children = |of: usize| -> Vec<_> {
(1..bones.len())
.filter(|&i| rig_parent(rig, &bones, i) == Some(of))
.map(|i| (0, 102 + i64::try_from(i).unwrap_or_default()))
.collect()
};
for (i, &(_, name)) in bones.iter().enumerate() {
let id = 2 + i64::try_from(i).unwrap_or_default();
let name = if rig.renamed == Some(i) {
b"Renamed".as_slice()
} else {
name.as_bytes()
};
objects.push(Object::new(id, 1, game_object(name, &[(0, id + 100)])));
let top = if rig.armature.is_empty() {
101
} else {
189 + i64::try_from(rig.armature.len()).unwrap_or_default()
};
let father = rig_parent(rig, &bones, i).map_or(top, |parent| {
102 + i64::try_from(parent).unwrap_or_default()
});
objects.push(Object::new(
id + 100,
4,
transform((0, id), (0, father), &children(i), false),
));
}
let mut animator = super::animator(ANIMATOR, (0, CONTROLLER));
set(
&mut animator.value,
"m_GameObject",
pptr("GameObject", (0, 1)),
);
set(
&mut animator.value,
"m_Avatar",
pptr("Avatar", (0, if rig.avatar { AVATAR } else { 0 })),
);
set(
&mut animator.value,
"m_HasTransformHierarchy",
V::Bool(rig.transform_hierarchy),
);
objects.push(animator);
objects.push(super::controller(CONTROLLER, &[(0, CLIP)], &[0]));
if rig.avatar {
objects.push(avatar(rig));
}
objects
}
#[must_use]
pub fn clip(dofs: &[u32], settings: &[(&str, V)]) -> Object {
clip_with(dofs, &[], settings, 0, &[])
}
#[must_use]
pub fn clip_with(
dofs: &[u32],
transforms: &[u32],
settings: &[(&str, V)],
streamed: u32,
words: &[u32],
) -> Object {
let slots = dofs.len() + 3 * transforms.len();
let constants = vec![0_u32; slots.saturating_sub(streamed as usize)];
clip_stored(
dofs,
transforms,
settings,
p::Storage {
interval: [0., 1.],
streamed_count: streamed,
words,
constants: &constants,
..p::Storage::default()
},
)
}
#[must_use]
pub fn clip_stored(
dofs: &[u32],
transforms: &[u32],
settings: &[(&str, V)],
storage: p::Storage<'_>,
) -> Object {
let mut object = p::explicit(CLIP, 30., &[]);
let mut bindings: Vec<_> = dofs
.iter()
.map(|&attribute| Binding {
class: 95,
custom: 8,
attribute,
..Binding::default()
})
.collect();
bindings.extend(transforms.iter().map(|&path| Binding {
path,
class: 4,
attribute: 1,
..Binding::default()
}));
p::add_compiled(&mut object, Layout::Unity2022, &bindings, &[], storage);
let mut index = vec![-1_i32; 200];
for (slot, &dof) in dofs.iter().enumerate() {
if let Some(entry) = usize::try_from(dof).ok().and_then(|dof| index.get_mut(dof)) {
*entry = i32::try_from(slot).unwrap_or_default();
}
}
let V::Struct(_, root) = &mut object.value else {
return object;
};
let Some((_, muscle)) = root.iter_mut().find(|(name, _)| name == "m_MuscleClip") else {
return object;
};
set(muscle, "m_IndexArray", ints(index));
set(
muscle,
"m_ValueArrayReferencePose",
vector(V::Float(0.), vec![]),
);
for field in [
"m_Mirror",
"m_LoopTime",
"m_LoopBlend",
"m_LoopBlendOrientation",
"m_LoopBlendPositionY",
"m_LoopBlendPositionXZ",
"m_KeepOriginalOrientation",
"m_KeepOriginalPositionY",
"m_KeepOriginalPositionXZ",
"m_HeightFromFeet",
] {
set(muscle, field, V::Bool(false));
}
set(muscle, "m_StartAtOrigin", V::Bool(true));
set(
muscle,
"m_StartX",
st(
"xform",
vec![
("t", xyz("Vector3f", &[0.; 3])),
("q", xyz("Quaternionf", &[0., 0., 0., 1.])),
("s", xyz("Vector3f", &[1.; 3])),
],
),
);
for field in ["m_CycleOffset", "m_Level", "m_OrientationOffsetY"] {
set(muscle, field, V::Float(0.));
}
for (field, value) in settings {
set(muscle, field, value.clone());
}
object
}