use super::SceneCase;
use crate::{
env as e,
ngui::{Object, V},
scene3d::{
self as s, Layout,
anim::{
self as a, Binding,
playback::{self as p, Dense, Key, Storage},
},
mesh as m,
},
};
const PREFAB: &str = "assets/synthetic.prefab";
const CLIP: &str = "assets/synthetic.anim";
const CAB: &str = "CAB-synthetic";
const BEND_ATTRIBUTE: u32 = 1_339_427_383;
#[must_use]
pub fn anim_clip_cases() -> Vec<(String, SceneCase)> {
let mut out = Vec::new();
for (name, data, _) in scenes() {
for flags in [0, 1, 7] {
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
}
#[must_use]
pub fn anim_gltf_cases() -> Vec<(String, SceneCase)> {
let mut out = Vec::new();
for (name, data, file_index) in scenes() {
let address = |path_id| format!(r#"{{"file_index":{file_index},"path_id":{path_id}}}"#);
let explicit = format!(
r#"{{"profile_version":1,"clips":[{{"root":{},"component":{},"clip":{}}}],"sample_rate":5.5}}"#,
address(2),
address(3),
address(9),
);
for (policy, flags, profile) in [
(
"all",
0,
br#"{"profile_version":1,"clips":"all_discovered","sample_rate":4}"#.to_vec(),
),
(
"prefab",
1,
br#"{"profile_version":1,"clips":"all_discovered"}"#.to_vec(),
),
("explicit", 3, explicit.into_bytes()),
] {
for meter in [0, 1, 16, 64] {
out.push((
format!("{name}-{policy}-m{meter}"),
SceneCase {
meter,
flags,
profile: profile.clone(),
data: data.clone(),
},
));
}
}
}
out
}
fn scenes() -> Vec<(String, Vec<u8>, u32)> {
let mut rigs: Vec<(String, Rig, Object, Layout)> = Vec::new();
for (name, clip) in clips() {
for rig in [Rig::Legacy, Rig::Animator] {
rigs.push((
format!("{name}-{rig:?}"),
rig,
clip.clone(),
Layout::Unity2022,
));
}
}
for rig in [
Rig::Override,
Rig::LeafInfo,
Rig::Cycle,
Rig::Dangling,
Rig::Negative,
] {
rigs.push((
format!("generic-{rig:?}"),
rig,
generic_clip(Generic::ConstantTrs),
Layout::Unity2022,
));
}
for layout in [Layout::Unity2019, Layout::Unity6000] {
rigs.push((
format!("bindings-{layout:?}"),
Rig::Animator,
a::bound_clip(9, layout, &bindings(), &[(0, 9)]),
layout,
));
}
let mut out = Vec::new();
for (name, rig, clip, layout) in rigs {
let file = s::file(layout, &[], &objects(rig, clip, layout)).expect("synthetic animation");
let index = e::serialized(
&[CAB],
&[e::Obj::asset_bundle(
100,
e::Bundle {
rows: vec![e::row(PREFAB, (0, 0), (1, 1)), e::row(CLIP, (0, 0), (1, 9))],
..e::Bundle::default()
},
)],
);
let bundle = e::bundle(&[("CAB-index", index), (CAB, file.clone())]);
out.push((format!("{name}-file"), file, 0));
out.push((format!("{name}-bundle"), bundle, 2));
}
out
}
#[derive(Clone, Copy, Debug)]
enum Rig {
Legacy,
Animator,
Override,
LeafInfo,
Cycle,
Dangling,
Negative,
}
fn objects(rig: Rig, clip: Object, layout: Layout) -> Vec<Object> {
let version = match layout {
Layout::Unity2019 => m::MeshVersion::V2019,
Layout::Unity6000 => m::MeshVersion::V6000,
_ => m::MeshVersion::V2022,
};
let mut mesh = m::mesh(
7,
b"mesh",
&[[1., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
&[0, 1, 2],
version,
);
s::set(
&mut mesh.value,
"m_Shapes",
m::blend_shapes(
vec![
m::blend_vertex(0, [0.5, 0., 0.], [0.; 3], [0.; 3]),
m::blend_vertex(1, [1., 0., 0.], [0.; 3], [0.; 3]),
],
vec![
m::blend_frame(0, 1, false, false),
m::blend_frame(1, 1, false, false),
],
vec![m::blend_channel(b"bend", 0, 0, 2)],
vec![25., 80.],
),
);
let mut second = clip.clone();
second.path_id = 12;
let component = match rig {
Rig::Legacy => a::legacy(3, (0, 9), &[(0, 9), (0, 12)]),
Rig::Dangling => a::legacy(3, (0, 999), &[(0, 9), (0, 999)]),
Rig::Negative => a::legacy(3, (-1, 9), &[(-1, 9), (0, 9)]),
Rig::Override | Rig::Cycle => a::animator(3, (0, 11)),
Rig::Animator | Rig::LeafInfo => a::animator(3, (0, 10)),
};
let mut objects = vec![
Object::new(1, 1, s::game_object(b"root", &[(0, 2), (0, 3), (0, 6)])),
Object::new(2, 4, s::transform((0, 1), (0, 0), &[(0, 5)], false)),
component,
Object::new(4, 1, s::game_object(b"instance", &[(0, 5), (0, 8)])),
Object::new(5, 4, s::transform((0, 4), (0, 2), &[], false)),
Object::new(
6,
137,
s::skinned_mesh_renderer((0, 1), (0, 7), &[], &[10.]),
),
mesh,
Object::new(
8,
137,
s::skinned_mesh_renderer((0, 4), (0, 7), &[], &[30.]),
),
clip,
second,
];
match rig {
Rig::LeafInfo => objects.push(a::controller_with_leaf_info(
10,
&[(0, 9), (0, 12)],
&[0, 1, 7],
Some((&[5, 0], 1)),
)),
Rig::Cycle => objects.push(a::overrides(11, (0, 11), &[((0, 9), (0, 12))])),
Rig::Override => {
objects.push(a::controller(10, &[(0, 12), (0, 9)], &[0, 1]));
objects.push(a::overrides(11, (0, 10), &[((0, 12), (0, 9))]));
}
_ => objects.push(a::controller(10, &[(0, 9)], &[0])),
}
objects
}
fn clips() -> Vec<(&'static str, Object)> {
let packed = |slopes| {
let mut clip = legacy_clip();
s::set(&mut clip.value, "m_Compressed", V::Bool(true));
s::set(
&mut clip.value,
"m_CompressedRotationCurves",
p::one_row(p::packed_row(slopes)),
);
clip
};
let mut nan = generic_clip(Generic::ConstantTrs);
let mut words = Vec::new();
p::frame(&mut words, f32::MIN, &[]);
p::frame(&mut words, f32::NAN, &[(0, [0, 0, 0, u32::MAX])]);
p::frame(&mut words, f32::INFINITY, &[]);
p::add_compiled(
&mut nan,
Layout::Unity2022,
&bindings(),
&[(0, 9)],
Storage {
interval: [f32::NAN, 3.],
streamed_count: u32::MAX,
words: &words,
dense: Dense {
frames: i32::MAX,
curves: 5,
rate: 0.,
begin: f32::INFINITY,
bits: &[0; 5],
},
constants: &[0; 3],
discrete_count: None,
},
);
let mut out = vec![
("legacy", legacy_clip()),
("constant", generic_clip(Generic::ConstantTrs)),
("clamped", generic_clip(Generic::ClampedTranslation)),
("flip", generic_clip(Generic::HeldQuaternionFlip)),
("explicit", p::explicit_fixture()),
("compiled", p::compiled_fixture()),
("replay", p::explicit_replay()),
("dense", p::dense_replay()),
("packed", packed(false)),
("slopes", packed(true)),
("metadata", a::clip(9)),
("hostile", nan),
];
for (_, clip) in &mut out {
clip.path_id = 9;
}
out
}
fn bindings() -> [Binding; 4] {
[
Binding {
class: 4,
attribute: 1,
..Binding::default()
},
Binding {
class: 137,
custom: 20,
attribute: BEND_ATTRIBUTE,
..Binding::default()
},
Binding {
class: 999,
attribute: 7,
path: 1,
..Binding::default()
},
Binding {
class: 137,
pptr: 1,
attribute: 3,
..Binding::default()
},
]
}
fn legacy_clip() -> Object {
let mut left = Key::new(2., [12.5]);
left.outgoing = [f32::INFINITY];
let mut middle = Key::new(2.25, [17.5]);
middle.outgoing = [f32::INFINITY];
let right = Key::new(3., [17.5]);
let float =
|keys: &[Key<1>], attribute| p::curve("", keys, [2, 2], None, true, Some(attribute));
p::explicit(
9,
4.,
&[
(
"m_FloatCurves",
vec![
float(&[left, middle, right], ("blendShape.bend", 137)),
float(&[Key::new(2., [0.5])], ("material.opacity", 23)),
],
),
(
"m_PositionCurves",
vec![p::curve(
"",
&[Key::new(2., [0.; 3]), Key::new(3., [1., -2., 0.])],
[2, 2],
None,
true,
None,
)],
),
(
"m_RotationCurves",
vec![p::curve(
"instance",
&[
Key::new(2., [0., 0., 0., 1.]),
Key::new(3., [0., 0., 0.6, 0.8]),
],
[2, 2],
None,
true,
None,
)],
),
],
)
}
#[derive(Clone, Copy)]
enum Generic {
ConstantTrs,
ClampedTranslation,
HeldQuaternionFlip,
}
fn generic_clip(kind: Generic) -> Object {
let clamped = matches!(kind, Generic::ClampedTranslation);
let held = matches!(kind, Generic::HeldQuaternionFlip);
let mut object = p::explicit(9, 4., &[]);
let mut words = Vec::new();
p::frame(&mut words, f32::MIN, &[]);
for (frame, (time, weight)) in [(2., 12.5_f32), (2.25, 17.5), (3., 17.5)]
.into_iter()
.enumerate()
{
let last = frame == 2;
if clamped && last {
p::frame(&mut words, 2.75, &[(1, [0, 0, 0, 1000_f32.to_bits()])]);
}
if held && last {
p::frame(&mut words, 2.75, &[(4, [0, 0, 0, (-1_f32).to_bits()])]);
}
let mut keys = vec![(0, [0, 0, 0, weight.to_bits()])];
if clamped && frame == 0 {
keys.extend([(1, [0, 0, 1_f32.to_bits(), 0]), (2, [0; 4]), (3, [0; 4])]);
}
if held {
let w = if time < 2.75 { 1_f32 } else { -1. };
keys.extend([
(1, [0; 4]),
(2, [0; 4]),
(3, [0; 4]),
(4, [0, 0, 0, w.to_bits()]),
]);
}
p::frame(&mut words, time, &keys);
}
p::frame(&mut words, f32::INFINITY, &[]);
let (attributes, streamed_count, constants): (_, _, &[f32]) = match kind {
Generic::ConstantTrs => ([1, 2], 1, &[1., -2., 0., 0., 0., 0., 1.]),
Generic::ClampedTranslation => ([1, 2], 4, &[0., 0., 0., 1.]),
Generic::HeldQuaternionFlip => ([2, 1], 5, &[1., -2., 0.]),
};
let constants: Vec<u32> = constants.iter().map(|x| x.to_bits()).collect();
let transform = |attribute| Binding {
class: 4,
attribute,
..Binding::default()
};
p::add_compiled(
&mut object,
Layout::Unity2022,
&[
Binding {
class: 137,
custom: 20,
attribute: BEND_ATTRIBUTE,
..Binding::default()
},
transform(attributes[0]),
transform(attributes[1]),
],
&[],
Storage {
interval: [2., 3.],
streamed_count,
words: &words,
constants: &constants,
..Storage::default()
},
);
object
}