unityrs-export 1.0.0

Unity asset exports beside the textures: sprites, the NGUI atlas cutter and its crops, and the UnityPy extractor tree.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Bounded source-space subdivision followed by a single f32 quantization.
mod bounds;

use super::{
    AnimError, Arc, CollectionTargets, ExportError, Meter, MeteredVec, Scene, clip_field, same,
};
use unityrs_anim::{BindingTarget, BoundClip, BoundTrack, TransformProperty};
use unityrs_gltf::{
    Accessor, Animation, AnimationChannel, AnimationSampler, AnimationTarget, CollectionIssue,
    Data, Interpolation, Kind, Node,
};
use unityrs_scene::Graph;

type Result<T> = std::result::Result<T, ExportError>;
const MAX_KEYS: usize = 65_536;
const MAX_DEPTH: u32 = 20;
const MAX_TIME: f64 = 1024.;
const MAX_COMPONENT: f64 = 1_048_576.;

#[derive(Clone, Copy, Debug)]
struct Key {
    source: f64,
    output: f32,
    left: bool,
    sided: bool,
}

struct Channel<'a, 'c, 'r> {
    target: AnimationTarget,
    tracks: &'a [BoundTrack<'c, 'r>],
    rest: &'a Node,
    width: usize,
    start: f64,
    step: bool,
    meter: &'a Arc<Meter>,
}

struct Baked {
    times: MeteredVec<f64>,
    values: MeteredVec<f64>,
    sided: bool,
}

fn family(target: BindingTarget<'_>) -> (usize, AnimationTarget) {
    use TransformProperty as T;
    match target {
        BindingTarget::Transform { node, property } => (
            node,
            match property {
                T::Translation => AnimationTarget::Translation,
                T::Quaternion | T::Euler => AnimationTarget::Rotation,
                T::Scale => AnimationTarget::Scale,
            },
        ),
        BindingTarget::Morph { node, .. } => (node, AnimationTarget::Weights),
        BindingTarget::Metadata => (usize::MAX, AnimationTarget::Weights),
    }
}

#[allow(
    clippy::too_many_lines,
    reason = "construct an animation and its emitted tables together"
)]
pub(super) fn animation(
    scene: &mut Scene,
    targets: CollectionTargets<'_>,
    graph: &Graph,
    bound: &BoundClip<'_, '_>,
    rate: Option<f64>,
    meter: &Arc<Meter>,
) -> Result<()> {
    if bound.tracks().is_empty() {
        return Ok(());
    }
    let timing = bound.timing().ok_or(AnimError::InvalidClipInterval)?;
    let start = timing.start();
    let stop = timing.stop();
    let duration = stop - start;
    if duration > MAX_TIME || (duration > 0. && round(duration)? == 0.) {
        return Err(ExportError::Bake("clip duration"));
    }

    let mut animation = Animation::new(meter);
    if let Some(name) =
        clip_field(bound.clip(), b"m_Name", meter)?.and_then(unityrs_core::value::Value::as_str)
    {
        animation.name = Some(unityrs_gltf::text(meter, name)?);
    }
    if let Some(wrap) =
        clip_field(bound.clip(), b"m_WrapMode", meter)?.and_then(unityrs_core::value::Value::as_i64)
    {
        // The temporary decimal representation fits in 20 bytes.
        let formatted = wrap.to_string();
        animation.extras.push((
            unityrs_gltf::text(meter, "unity_wrap_mode")?,
            unityrs_gltf::text(meter, &formatted)?,
        ))?;
    }
    for tracks in bound
        .tracks()
        .chunk_by(|a, b| family(a.target) == family(b.target))
    {
        meter.count_decode(tracks.len() as u64)?;
        let first = tracks.first().ok_or(ExportError::Bake("empty target"))?;
        let (source_node, target) = family(first.target);
        let mut node = targets
            .transforms
            .get(source_node)
            .copied()
            .flatten()
            .ok_or(AnimError::FilteredTarget(source_node))?;
        let mut approximations = 0;
        if let BindingTarget::Morph { renderer, .. } = first.target {
            let mut attachment = None;
            for row in targets.renderers {
                if same(row.renderer, renderer, meter)? {
                    attachment = Some(row);
                    break;
                }
            }
            let row = attachment.ok_or(AnimError::MissingAttachment(source_node))?;
            node = row.attachment;
            for track in tracks {
                if let BindingTarget::Morph { channel, .. } = track.target
                    && row
                        .channels
                        .as_slice()
                        .get(channel.target_slot)
                        .is_some_and(|c| c.frame_count > 1)
                {
                    approximations += 1;
                }
            }
        }
        let rest = scene
            .nodes
            .as_slice()
            .get(node)
            .ok_or(AnimError::MissingPath)?;
        if target == AnimationTarget::Rotation {
            let local = &graph
                .nodes()
                .get(source_node)
                .ok_or(AnimError::MissingPath)?
                .local;
            if local.rotation.iter().any(|x| !x.is_finite())
                || local.rotation.iter().all(|&x| x == 0.)
            {
                return Err(AnimError::InvalidRestPose(source_node).into());
            }
        }
        let (kind, width) = match target {
            AnimationTarget::Rotation => (Kind::AnimationRotation, 4),
            AnimationTarget::Weights => (Kind::AnimationWeights, rest.weights.len()),
            _ => (Kind::AnimationVec3, 3),
        };
        let mut channel = Channel {
            target,
            tracks,
            rest,
            width,
            start,
            step: true,
            meter,
        };
        let baked = channel.bake(stop, rate.unwrap_or(timing.rate()))?;
        let interpolation = if channel.step {
            Interpolation::Step
        } else {
            Interpolation::Linear
        };
        let count = baked.times.len();
        let output_count = if target == AnimationTarget::Weights {
            baked.values.len()
        } else {
            count
        };
        let input = scene.accessors.len();
        scene.accessors.push(Accessor {
            kind: Kind::AnimationTime,
            count,
            data: Data::Float(baked.times),
            sparse_indices: None,
        })?;
        let output = scene.accessors.len();
        scene.accessors.push(Accessor {
            kind,
            count: output_count,
            data: Data::Float(baked.values),
            sparse_indices: None,
        })?;
        let sampler = animation.samplers.len();
        animation.samplers.push(AnimationSampler {
            input,
            output,
            interpolation,
        })?;
        animation.channels.push(AnimationChannel {
            sampler,
            node,
            target,
        })?;
        if approximations != 0 {
            scene.collection_issues.push(CollectionIssue {
                resource: None,
                node,
                reason: "animation_morph_last_frame_approximation",
                dropped_renderer: false,
                count: approximations,
            })?;
        }
    }
    scene.animations.push(animation)?;
    Ok(())
}

#[allow(clippy::float_cmp)]
impl Channel<'_, '_, '_> {
    #[allow(
        clippy::too_many_lines,
        reason = "process continuous spans using adjacent jump brackets"
    )]
    fn bake(&mut self, stop: f64, rate: f64) -> Result<Baked> {
        let mut times = MeteredVec::new(self.meter);
        let (start, duration) = (self.start, stop - self.start);
        times.push(start)?;
        if duration > 0. {
            times.push(stop)?;
            let steps = (duration * rate).ceil();
            if steps > f64::from(u16::MAX) {
                return Err(ExportError::Bake("schedule key cap"));
            }
            #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
            for index in 1..steps as u32 {
                self.meter.count_decode(1)?;
                let time = start + f64::from(index) / rate;
                if start < time && time < stop {
                    times.push(time)?;
                }
            }
        }
        for track in self.tracks {
            track.visit_key_times(|time| {
                self.meter.count_decode(1)?;
                if start < time && time < stop {
                    times.push(time)?;
                }
                Ok(())
            })?;
        }
        self.meter.count_decode(
            (times.len() as u64).saturating_mul(u64::from(times.len().bit_width())),
        )?;
        times.as_mut_slice().sort_unstable_by(f64::total_cmp);
        // Compact in place before the cap, as components often share schedules.
        let mut retained = 0;
        let mut previous = None;
        for time in times.as_slice() {
            if previous != Some(*time) {
                previous = Some(*time);
                retained += 1;
            }
        }
        if retained > MAX_KEYS {
            return Err(ExportError::Bake("source key cap"));
        }
        let mut keys = MeteredVec::with_capacity(self.meter, retained)?;
        for &source in times.as_slice() {
            if keys
                .as_slice()
                .last()
                .is_none_or(|key: &Key| key.source != source)
            {
                keys.push(Key {
                    source,
                    output: round(source - start)?,
                    left: false,
                    sided: false,
                })?;
            }
        }
        drop(times);

        for pair in keys.as_slice().windows(2) {
            let [left, _] = pair else { continue };
            let mid = left.source;
            for track in self.tracks {
                for axis in 0..track.width() {
                    if !track.segment(axis, mid, false, self.meter)?.is_held() {
                        self.step = false;
                    }
                }
            }
        }
        let mut baked = Baked {
            times: MeteredVec::new(self.meter),
            values: MeteredVec::new(self.meter),
            sided: false,
        };
        let mut left = *keys
            .as_slice()
            .first()
            .ok_or(ExportError::Bake("missing endpoints"))?;
        self.append(&mut baked, left)?;
        let mut previous_window = None;
        for (index, &right) in keys.as_slice().iter().enumerate().skip(1) {
            let mut limit = Key {
                left: true,
                ..right
            };
            let jump = self.jump(limit, right)?;
            if jump {
                let b = f64::from(right.output);
                let j = right.source - start;
                let previous = f64::from(left.output);
                let next = keys.as_slice().get(index + 1);
                if previous >= b || next.is_some_and(|key| key.output <= right.output) {
                    return Err(ExportError::Bake("rounded jump bracket"));
                }
                let a = if self.step {
                    right.output
                } else {
                    right.output.next_down()
                };
                let gap = if j == b {
                    0.
                } else {
                    let toward = if j < b {
                        right.output.next_down()
                    } else {
                        right.output.next_up()
                    };
                    (f64::from(toward) - b).abs()
                };
                let a_time = f64::from(a);
                if (b - j).abs() > gap * 0.5
                    || (a_time - j).abs() > (b - a_time) + gap * 0.5
                    || !self.distinct_rounded_sides(limit, right)?
                {
                    return Err(ExportError::Bake("unrepresentable held sides"));
                }
                let span = (f64::from(a).min(j), b.max(j));
                if span.0 < 0.
                    || span.1 > duration.max(f64::from(round(duration)?))
                    || previous >= span.0
                    || previous_window.is_some_and(|end| end >= span.0)
                    || next.is_some_and(|key| f64::from(key.output) <= span.1)
                {
                    return Err(ExportError::Bake("overlapping jump window"));
                }
                previous_window = Some(span.1);
                limit.output = a;
                limit.sided = !self.step;
            } else {
                limit = right;
            }

            if self.step {
                if !self.same_value(left, limit)? {
                    return Err(ExportError::Bake("STEP changed between boundaries"));
                }
                self.append(
                    &mut baked,
                    Key {
                        sided: jump,
                        ..right
                    },
                )?;
            } else {
                self.refine(&mut baked, left, limit, 0)?;
                if jump {
                    self.append(
                        &mut baked,
                        Key {
                            sided: true,
                            ..right
                        },
                    )?;
                }
            }
            left = Key {
                sided: jump,
                ..right
            };
        }
        Ok(baked)
    }

    fn track(&self, axis: usize) -> Result<Option<(&BoundTrack<'_, '_>, usize, f64)>> {
        let axis_key = |track: &BoundTrack<'_, '_>| match track.target {
            BindingTarget::Morph { channel, .. } => channel.target_slot,
            _ => track.component.unwrap_or(0),
        };
        if let Some(track) = self.tracks.first()
            && matches!(track.target, BindingTarget::Transform { .. })
            && track.component.is_none()
        {
            return Ok(Some((
                track,
                axis,
                if self.target == AnimationTarget::Translation && axis == 0 {
                    -1.
                } else {
                    1.
                },
            )));
        }
        self.meter
            .count_decode(u64::from(self.tracks.len().bit_width()) + 1)?;
        let Some(track) = self
            .tracks
            .binary_search_by_key(&axis, axis_key)
            .ok()
            .and_then(|index| self.tracks.get(index))
        else {
            return Ok(None);
        };
        let gain = if let BindingTarget::Morph { channel, .. } = track.target {
            1. / f64::from(channel.full_weight)
        } else if self.target == AnimationTarget::Translation && axis == 0 {
            -1.
        } else {
            1.
        };
        Ok(Some((track, 0, gain)))
    }

    fn component(&self, axis: usize, key: Key) -> Result<f64> {
        if let Some((track, component, gain)) = self.track(axis)? {
            let values = track.raw_at(key.source, key.left, self.meter)?;
            Ok(values.get(component).ok_or(AnimError::InvalidSampleValue)? * gain)
        } else {
            let rest: &[f64] = match self.target {
                AnimationTarget::Translation => &self.rest.translation,
                AnimationTarget::Scale => &self.rest.scale,
                _ => self.rest.weights.as_slice(),
            };
            rest.get(axis)
                .copied()
                .ok_or(AnimError::InvalidSampleValue.into())
        }
    }

    fn rotation(&self, key: Key) -> Result<[f64; 4]> {
        let track = self.tracks.first().ok_or(AnimError::InvalidQuaternion)?;
        let [x, y, z, w] =
            track.rotation(track.raw_at(key.source, key.left, self.meter)?, self.meter)?;
        Ok([x, -y, -z, w])
    }

    fn jump(&self, left: Key, right: Key) -> Result<bool> {
        if self.target == AnimationTarget::Rotation {
            let distance = bounds::angle(self.rotation(left)?, self.rotation(right)?);
            if distance == 0. || distance > 1e-12 {
                return Ok(distance > 0.);
            }
            let track = self.tracks.first().ok_or(AnimError::InvalidQuaternion)?;
            let a = track.raw_at(left.source, true, self.meter)?;
            let b = track.raw_at(right.source, false, self.meter)?;
            for (axis, (a, b)) in a.into_iter().zip(b).enumerate().take(track.width()) {
                if a != b
                    && track
                        .segment(axis, left.source, true, self.meter)?
                        .is_held()
                {
                    return Ok(true);
                }
            }
            return Ok(false);
        }
        for axis in 0..self.width {
            let a = self.component(axis, left)?;
            let b = self.component(axis, right)?;
            if a == b {
                continue;
            }
            if self.step || (a - b).abs() > 1e-12 * a.abs().max(b.abs()).max(1.) {
                return Ok(true);
            }
            if let Some((track, component, _)) = self.track(axis)?
                && track
                    .segment(component, left.source, true, self.meter)?
                    .is_held()
            {
                return Ok(true);
            }
        }
        Ok(false)
    }

    fn distinct_rounded_sides(&self, left: Key, right: Key) -> Result<bool> {
        if self.target == AnimationTarget::Rotation {
            let a = bounds::rounded(self.rotation(left)?)?;
            let b = bounds::rounded(self.rotation(right)?)?;
            return Ok(bounds::angle(a, b) > 0.);
        }
        for axis in 0..self.width {
            if round(self.component(axis, left)?)? != round(self.component(axis, right)?)? {
                return Ok(true);
            }
        }
        Ok(false)
    }

    fn same_value(&self, left: Key, right: Key) -> Result<bool> {
        if self.target == AnimationTarget::Rotation {
            return Ok(bounds::angle(self.rotation(left)?, self.rotation(right)?) <= 1e-12);
        }
        for axis in 0..self.width {
            if self.component(axis, left)? != self.component(axis, right)? {
                return Ok(false);
            }
        }
        Ok(true)
    }

    fn append(&self, baked: &mut Baked, key: Key) -> Result<()> {
        let previous = baked.times.as_slice().last().copied();
        let output = f64::from(key.output);
        let duplicate = previous == Some(output);
        if previous.is_some_and(|prev| prev > output) || (duplicate && (key.sided || baked.sided)) {
            return Err(ExportError::Bake("conflicting rounded timestamps"));
        }
        let count = baked.times.len();
        if !duplicate {
            if count >= MAX_KEYS {
                return Err(ExportError::Bake("subdivision key cap"));
            }
            count
                .checked_add(1)
                .and_then(|n| n.checked_mul(self.width))
                .ok_or(ExportError::Bake("output size"))?;
            baked.times.push(output)?;
        }
        let mut rotation = [0.; 4];
        if self.target == AnimationTarget::Rotation {
            let [x, y, z, w] = self.rotation(key)?;
            rotation = [round(x)?, round(y)?, round(z)?, round(w)?];
            let previous = count
                .checked_sub(1)
                .and_then(|n| baked.values.as_slice().get(n * self.width..));
            if let Some([x, y, z, w]) = previous {
                rotation = unityrs_gltf::align_rotation(
                    [round(*x)?, round(*y)?, round(*z)?, round(*w)?],
                    rotation,
                );
            }
        }
        for axis in 0..self.width {
            self.meter.count_decode(8)?;
            let rounded = if self.target == AnimationTarget::Rotation {
                f64::from(*rotation.get(axis).ok_or(AnimError::InvalidQuaternion)?)
            } else {
                let value = self.component(axis, key)?;
                let rounded = f64::from(round(value)?);
                if value.abs() > MAX_COMPONENT {
                    return Err(ExportError::Bake("component magnitude"));
                }
                rounded
            };
            if duplicate {
                if baked.values.as_slice().get((count - 1) * self.width + axis) != Some(&rounded) {
                    return Err(ExportError::Bake("collapsed key values"));
                }
            } else {
                baked.values.push(rounded)?;
            }
        }
        baked.sided = key.sided;
        Ok(())
    }

    fn refine(&self, baked: &mut Baked, left: Key, right: Key, depth: u32) -> Result<()> {
        let certified = if self.target == AnimationTarget::Rotation {
            bounds::rotation(self, left, right)?
        } else {
            bounds::components(self, left, right)?
        };
        if certified {
            return self.append(baked, right);
        }
        let source = midpoint(left.source, right.source);
        let output = round(source - self.start)?;
        if depth >= MAX_DEPTH
            || source <= left.source
            || source >= right.source
            || output <= left.output
            || output >= right.output
        {
            return Err(ExportError::Bake("cannot certify subdivision"));
        }
        let middle = Key {
            source,
            output,
            left: false,
            sided: false,
        };
        self.refine(baked, left, middle, depth + 1)?;
        self.refine(baked, middle, right, depth + 1)
    }

    fn derivative(&self, axis: usize, left: Key, right: Key) -> Result<(f64, f64)> {
        if let Some((track, component, gain)) = self.track(axis)? {
            let (lo, hi) = (left.source, right.source);
            let (rate, acceleration) = track
                .segment(component, lo, false, self.meter)?
                .derivatives(lo, hi);
            Ok((rate * gain.abs(), acceleration * gain.abs()))
        } else {
            Ok((0., 0.))
        }
    }
}

fn midpoint(left: f64, right: f64) -> f64 {
    left + (right - left) * 0.5
}

#[allow(clippy::cast_possible_truncation)]
fn round(value: f64) -> Result<f32> {
    let rounded = value as f32;
    if !rounded.is_finite() {
        return Err(AnimError::InvalidSampleValue.into());
    }
    Ok(rounded)
}