Skip to main content

unityrs_testkit/scene3d/
anim.rs

1//! Minimal embedded trees for animation discovery and binding tests.
2use super::pointers;
3use crate::ngui::{Object, V, pair, pptr, s, st, vector};
4
5type Ptr = (i32, i64);
6
7#[must_use]
8pub fn clip(id: i64) -> Object {
9    Object::new(
10        id,
11        74,
12        st("AnimationClip", vec![("m_Name", s("same clip"))]),
13    )
14}
15
16#[must_use]
17pub fn legacy(id: i64, default: Ptr, clips: &[Ptr]) -> Object {
18    Object::new(
19        id,
20        111,
21        st(
22            "Animation",
23            vec![
24                ("m_Animation", pptr("AnimationClip", default)),
25                ("m_Animations", pointers("AnimationClip", clips)),
26            ],
27        ),
28    )
29}
30
31#[must_use]
32pub fn animator(id: i64, controller: Ptr) -> Object {
33    Object::new(
34        id,
35        95,
36        st(
37            "Animator",
38            vec![(
39                "m_Controller",
40                pptr("RuntimeAnimatorController", controller),
41            )],
42        ),
43    )
44}
45
46#[must_use]
47pub fn overrides(id: i64, controller: Ptr, rows: &[(Ptr, Ptr)]) -> Object {
48    let row = |original, replacement| {
49        st(
50            "AnimationClipOverride",
51            vec![
52                ("m_OriginalClip", pptr("AnimationClip", original)),
53                ("m_OverrideClip", pptr("AnimationClip", replacement)),
54            ],
55        )
56    };
57    Object::new(
58        id,
59        221,
60        st(
61            "AnimatorOverrideController",
62            vec![
63                (
64                    "m_Controller",
65                    pptr("RuntimeAnimatorController", controller),
66                ),
67                (
68                    "m_Clips",
69                    vector(
70                        row((0, 0), (0, 0)),
71                        rows.iter().map(|&(a, b)| row(a, b)).collect(),
72                    ),
73                ),
74            ],
75        ),
76    )
77}
78
79/// Unity 5.2+ controller table with one machine, state and tree.
80#[must_use]
81pub fn controller(id: i64, clips: &[Ptr], leaves: &[u32]) -> Object {
82    controller_with_leaf_info(id, clips, leaves, None)
83}
84
85/// A pre-5.2 controller whose leaves refer to IDs in a state-local table.
86#[must_use]
87pub fn controller_with_leaf_info(
88    id: i64,
89    clips: &[Ptr],
90    leaves: &[u32],
91    info: Option<(&[u32], u32)>,
92) -> Object {
93    let node = |clip| {
94        offset(st(
95            "BlendTreeNodeConstant",
96            vec![
97                ("m_ChildIndices", vector(V::UInt(0), vec![])),
98                ("m_ClipID", V::UInt(clip)),
99            ],
100        ))
101    };
102    let tree = st(
103        "BlendTreeConstant",
104        vec![(
105            "m_NodeArray",
106            vector(node(0), leaves.iter().copied().map(node).collect()),
107        )],
108    );
109    let mut fields = vec![("m_BlendTreeConstantArray", one(offset(tree)))];
110    if let Some((ids, base)) = info {
111        fields.push((
112            "m_LeafInfoArray",
113            one(st(
114                "LeafInfoConstant",
115                vec![
116                    (
117                        "m_IDArray",
118                        vector(V::UInt(0), ids.iter().copied().map(V::UInt).collect()),
119                    ),
120                    ("m_IndexOffset", V::UInt(base)),
121                ],
122            )),
123        ));
124    }
125    let state = offset(st("StateConstant", fields));
126    let machine = offset(st(
127        "StateMachineConstant",
128        vec![("m_StateConstantArray", one(state))],
129    ));
130    let constant = st(
131        "ControllerConstant",
132        vec![("m_StateMachineArray", one(machine))],
133    );
134    Object::new(
135        id,
136        91,
137        st(
138            "AnimatorController",
139            vec![
140                ("m_Controller", constant),
141                (
142                    "m_TOS",
143                    vector(pair(V::UInt(0), s("")), vec![pair(V::UInt(0), s(""))]),
144                ),
145                ("m_AnimationClips", pointers("AnimationClip", clips)),
146            ],
147        ),
148    )
149}
150
151fn offset(value: V) -> V {
152    st("OffsetPtr", vec![("data", value)])
153}
154
155fn one(value: V) -> V {
156    vector(value.clone(), vec![value])
157}
158
159/// Stored values for one `GenericBinding` test row.
160#[derive(Clone, Copy, Debug, Default)]
161pub struct Binding {
162    pub path: u32,
163    pub attribute: u32,
164    pub script: Ptr,
165    pub class: i32,
166    pub custom: u8,
167    pub pptr: u8,
168    pub integer: u8,
169    pub managed: u8,
170}
171
172/// A clip with only its embedded binding constant. Consecutive records
173/// exercise alignment as well as field reads.
174#[must_use]
175pub fn bound_clip(id: i64, layout: super::Layout, bindings: &[Binding], mapping: &[Ptr]) -> Object {
176    use super::Layout;
177    let record = |b: &Binding| {
178        let (class_name, class) = if matches!(layout, Layout::Unity51 | Layout::Unity55) {
179            (
180                "classID",
181                V::UInt16(u16::try_from(b.class).unwrap_or_default()),
182            )
183        } else {
184            ("typeID", V::Int(b.class))
185        };
186        let mut fields = vec![
187            ("path", V::UInt(b.path)),
188            ("attribute", V::UInt(b.attribute)),
189            ("script", pptr("Object", b.script)),
190            (class_name, class),
191            ("customType", V::UInt8(b.custom)),
192            ("isPPtrCurve", V::UInt8(b.pptr)),
193        ];
194        if matches!(
195            layout,
196            Layout::Unity20221 | Layout::Unity2022 | Layout::Unity6000
197        ) {
198            fields.push(("isIntCurve", V::UInt8(b.integer)));
199        }
200        if matches!(layout, Layout::Unity2022 | Layout::Unity6000) {
201            fields.push(("isSerializeReferenceCurve", V::UInt8(b.managed)));
202        }
203        if !matches!(layout, Layout::Unity51 | Layout::Unity55)
204            && let Some((_, last)) = fields.last_mut()
205            && let V::UInt8(value) = last
206        {
207            *last = V::AlignedUInt8(*value);
208        }
209        st("GenericBinding", fields)
210    };
211    let table = st(
212        "AnimationClipBindingConstant",
213        vec![
214            (
215                "genericBindings",
216                vector(
217                    record(&Binding::default()),
218                    bindings.iter().map(record).collect(),
219                ),
220            ),
221            ("pptrCurveMapping", pointers("Object", mapping)),
222        ],
223    );
224    Object::new(
225        id,
226        74,
227        st("AnimationClip", vec![("m_ClipBindingConstant", table)]),
228    )
229}
230
231pub mod humanoid;
232pub mod playback;