euv-core 0.48.2

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
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
627
628
629
630
631
632
633
634
635
use super::*;

/// Construction, zero-allocation in-place capture, discontinuity handling, and interpolation sampling for `DoubleBufferedSnapshot`.
impl<S> DoubleBufferedSnapshot<S>
where
    S: Clone,
{
    /// Initializes a three-slot double-buffered snapshot store with `initial` across all persistent slots.
    ///
    /// # Arguments
    ///
    /// - `S` - Initial simulation state snapshot.
    ///
    /// # Returns
    ///
    /// - `Self` - Initialized three-slot snapshot buffer.
    #[must_use]
    pub fn new(initial: S) -> Self {
        Self {
            prev_state: initial.clone(),
            interp_scratch: initial.clone(),
            curr_state: initial,
            tick: 0,
            discontinuity_tick: 0,
            has_prev: false,
        }
    }

    /// Commits a continuous fixed-step snapshot, promoting the prior `curr_state` into `prev_state`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable snapshot buffer.
    /// - `S` - New immutable simulation state snapshot.
    pub fn commit(&mut self, next: S) {
        let mut incoming: S = next;
        swap(self.get_mut_curr_state(), &mut incoming);
        self.set_prev_state(incoming);
        let next_tick: u64 = self.get_tick().wrapping_add(1);
        self.set_tick(next_tick);
        self.set_has_prev(true);
    }

    /// Rotates `curr_state` into `prev_state` and populates the recycled `curr_state` slot in place via `capture_fn` with zero heap allocations.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable snapshot buffer.
    /// - `F` - Closure overwriting the recycled `&mut S` slot in place.
    pub fn capture_into<F>(&mut self, capture_fn: F)
    where
        F: FnOnce(&mut S),
    {
        let Self {
            prev_state,
            curr_state,
            ..
        } = self;
        swap(prev_state, curr_state);
        capture_fn(curr_state);
        let next_tick: u64 = self.get_tick().wrapping_add(1);
        self.set_tick(next_tick);
        self.set_has_prev(true);
    }

    /// Captures a discontinuous snapshot in place via `capture_fn`, preventing interpolation across the boundary without reallocating backing vectors.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable snapshot buffer.
    /// - `F` - Closure overwriting `curr_state` in place.
    pub fn capture_discontinuous_into<F>(&mut self, capture_fn: F)
    where
        F: FnOnce(&mut S),
    {
        let Self { curr_state, .. } = self;
        capture_fn(curr_state);
        let next_tick: u64 = self.get_tick().wrapping_add(1);
        self.set_tick(next_tick);
        self.set_discontinuity_tick(next_tick);
        self.set_has_prev(false);
    }

    /// Commits a discontinuous snapshot (teleport, scene reset, or hard camera cut), preventing interpolation across the boundary.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable snapshot buffer.
    /// - `S` - New immutable simulation state snapshot.
    pub fn commit_discontinuous(&mut self, next: S) {
        let next_tick: u64 = self.get_tick().wrapping_add(1);
        self.set_prev_state(next.clone());
        self.set_curr_state(next);
        self.set_tick(next_tick);
        self.set_discontinuity_tick(next_tick);
        self.set_has_prev(false);
    }

    /// Samples the render-time state at normalized `alpha` in `[0.0, 1.0]` directly into pre-allocated `out` with zero heap allocations.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Snapshot buffer reference.
    /// - `f32` - Normalized interpolation factor between previous and current fixed steps.
    /// - `&mut S` - Persistent destination snapshot overwritten in place.
    pub fn sample_into(&self, alpha: f32, out: &mut S)
    where
        S: InterpolatableState,
    {
        let clamped: f32 = if alpha.is_finite() {
            alpha.clamp(0.0, 1.0)
        } else {
            1.0
        };
        if !self.get_has_prev()
            || self.get_discontinuity_tick() == self.get_tick()
            || clamped >= 1.0
        {
            out.copy_from_snapshot(self.get_curr_state());
            return;
        }
        if clamped <= 0.0 {
            out.copy_from_snapshot(self.get_prev_state());
            return;
        }
        self.get_prev_state()
            .interpolate_into(self.get_curr_state(), clamped, out);
    }

    /// Samples the render-time state at `alpha` into the internal third persistent `interp_scratch` slot and borrows it with zero allocations.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable snapshot buffer.
    /// - `f32` - Normalized interpolation factor in `[0.0, 1.0]`.
    ///
    /// # Returns
    ///
    /// - `&S` - Borrowed interpolated snapshot stored in the persistent scratch slot.
    #[must_use]
    pub fn sample_cached(&mut self, alpha: f32) -> &S
    where
        S: InterpolatableState,
    {
        let clamped: f32 = if alpha.is_finite() {
            alpha.clamp(0.0, 1.0)
        } else {
            1.0
        };
        let use_curr: bool = !self.get_has_prev()
            || self.get_discontinuity_tick() == self.get_tick()
            || clamped >= 1.0;
        let Self {
            prev_state,
            curr_state,
            interp_scratch,
            ..
        } = self;
        if use_curr {
            interp_scratch.copy_from_snapshot(curr_state);
        } else if clamped <= 0.0 {
            interp_scratch.copy_from_snapshot(prev_state);
        } else {
            prev_state.interpolate_into(curr_state, clamped, interp_scratch);
        }
        interp_scratch
    }

    /// Samples the render-time state at normalized `alpha` in `[0.0, 1.0]`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Snapshot buffer reference.
    /// - `f32` - Normalized interpolation factor between previous and current fixed steps.
    ///
    /// # Returns
    ///
    /// - `S` - Interpolated snapshot or exact `curr_state` on discontinuities.
    #[must_use]
    pub fn sample(&self, alpha: f32) -> S
    where
        S: InterpolatableState,
    {
        let clamped: f32 = if alpha.is_finite() {
            alpha.clamp(0.0, 1.0)
        } else {
            1.0
        };
        if !self.get_has_prev()
            || self.get_discontinuity_tick() == self.get_tick()
            || clamped >= 1.0
        {
            return self.get_curr_state().clone();
        }
        if clamped <= 0.0 {
            return self.get_prev_state().clone();
        }
        self.get_prev_state()
            .interpolate(self.get_curr_state(), clamped)
    }
}

/// Snapshot builders and zero-allocation in-place capture helpers for `World2DSnapshot`.
impl World2DSnapshot {
    /// Captures `world` and `camera` into `self` reusing existing `entities` and `discrete_events` capacities with zero steady-state allocations.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable destination 2D snapshot.
    /// - `&GameWorld` - Authoritative 2D ECS world.
    /// - `Camera2D` - Active 2D camera state.
    /// - `bool` - Whether a hard camera cut occurred on this tick.
    /// - `&[DiscreteGameEvent]` - Discrete un-interpolated gameplay events emitted on this tick.
    pub fn capture_into(
        &mut self,
        world: &GameWorld,
        camera: Camera2D,
        camera_cut: bool,
        discrete_events: &[DiscreteGameEvent],
    ) {
        self.set_tick(world.get_tick());
        self.set_camera(camera);
        self.set_camera_cut(camera_cut);
        let entities: &mut Vec<EntityPose2D> = self.get_mut_entities();
        entities.clear();
        let records: &[EntityRecord] = world.get_entities().as_slice();
        if entities.capacity() < records.len() {
            entities.reserve(records.len() - entities.capacity());
        }
        for ent in records.iter() {
            entities.push(EntityPose2D::new(
                ent.get_id(),
                ent.get_transform(),
                ent.get_teleport_epoch(),
                ent.get_spawn_tick(),
                ent.get_alive(),
            ));
        }
        let events_buf: &mut Vec<DiscreteGameEvent> = self.get_mut_discrete_events();
        events_buf.clear();
        events_buf.extend_from_slice(discrete_events);
    }

    /// Captures an immutable `World2DSnapshot` from `world` and `camera`.
    ///
    /// # Arguments
    ///
    /// - `&GameWorld` - Authoritative 2D ECS world.
    /// - `Camera2D` - Active 2D camera state.
    /// - `bool` - Whether a hard camera cut occurred on this tick.
    /// - `Vec<DiscreteGameEvent>` - Discrete un-interpolated gameplay events emitted on this tick.
    ///
    /// # Returns
    ///
    /// - `Self` - Captured immutable 2D world snapshot.
    #[must_use]
    pub fn capture(
        world: &GameWorld,
        camera: Camera2D,
        camera_cut: bool,
        discrete_events: Vec<DiscreteGameEvent>,
    ) -> Self {
        let mut snap: Self = Self::default();
        snap.capture_into(world, camera, camera_cut, &[]);
        snap.set_discrete_events(discrete_events);
        snap
    }
}

/// Discontinuity-safe render-time interpolation for `World2DSnapshot`.
impl InterpolatableState for World2DSnapshot {
    /// Copies `src` into `self` reusing backing vector allocations.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable destination snapshot.
    /// - `&Self` - Source snapshot.
    fn copy_from_snapshot(&mut self, src: &Self) {
        self.set_tick(src.get_tick());
        self.set_camera(src.get_camera());
        self.set_camera_cut(src.get_camera_cut());
        let ents: &mut Vec<EntityPose2D> = self.get_mut_entities();
        ents.clear();
        ents.extend_from_slice(src.get_entities().as_slice());
        let evs: &mut Vec<DiscreteGameEvent> = self.get_mut_discrete_events();
        evs.clear();
        evs.extend_from_slice(src.get_discrete_events().as_slice());
    }

    /// Interpolates camera and continuous entity poses into `out` with zero steady-state heap allocations.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Previous fixed-step 2D snapshot.
    /// - `&Self` - Current fixed-step 2D snapshot.
    /// - `f32` - Normalized interpolation factor in `[0.0, 1.0]`.
    /// - `&mut Self` - Pre-allocated destination 2D snapshot overwritten in place.
    fn interpolate_into(&self, next: &Self, alpha: f32, out: &mut Self) {
        let k: f32 = alpha.clamp(0.0, 1.0);
        let camera: Camera2D = if next.get_camera_cut() {
            next.get_camera()
        } else {
            self.get_camera().lerp(next.get_camera(), k)
        };
        out.set_tick(next.get_tick());
        out.set_camera(camera);
        out.set_camera_cut(next.get_camera_cut());
        let prev_entities: &[EntityPose2D] = self.get_entities().as_slice();
        let next_entities: &[EntityPose2D] = next.get_entities().as_slice();
        let out_entities: &mut Vec<EntityPose2D> = out.get_mut_entities();
        out_entities.clear();
        if out_entities.capacity() < next_entities.len() {
            out_entities.reserve(next_entities.len() - out_entities.capacity());
        }
        let prev_tick: u64 = self.get_tick();
        for (idx, curr) in next_entities.iter().copied().enumerate() {
            let mut blended: EntityPose2D = curr;
            if curr.get_alive()
                && let Some(prev) = prev_entities.get(idx).copied()
            {
                let same_entity: bool = prev.get_alive() && prev.get_id() == curr.get_id();
                let no_teleport: bool = prev.get_teleport_epoch() == curr.get_teleport_epoch();
                let not_fresh_spawn: bool = curr.get_spawn_tick() <= prev_tick;
                if same_entity && no_teleport && not_fresh_spawn {
                    let interp_tr: EntityTransform =
                        prev.get_transform().lerp(curr.get_transform(), k);
                    blended.set_transform(interp_tr);
                }
            }
            out_entities.push(blended);
        }
        let out_events: &mut Vec<DiscreteGameEvent> = out.get_mut_discrete_events();
        out_events.clear();
        out_events.extend_from_slice(next.get_discrete_events().as_slice());
    }

    /// Interpolates camera and continuous entity poses while snapping spawns, despawns, teleports, camera cuts, and discrete events.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Previous fixed-step 2D snapshot.
    /// - `&Self` - Current fixed-step 2D snapshot.
    /// - `f32` - Normalized interpolation factor in `[0.0, 1.0]`.
    ///
    /// # Returns
    ///
    /// - `Self` - Render-time interpolated 2D world snapshot.
    fn interpolate(&self, next: &Self, alpha: f32) -> Self {
        let mut out: Self = Self::default();
        self.interpolate_into(next, alpha, &mut out);
        out
    }
}

/// Discontinuity-safe skeletal joint interpolation for `SkeletonPoseSnapshot`.
impl InterpolatableState for SkeletonPoseSnapshot {
    /// Copies `src` into `self` reusing backing joint transform storage.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable destination skeletal pose snapshot.
    /// - `&Self` - Source skeletal pose snapshot.
    fn copy_from_snapshot(&mut self, src: &Self) {
        self.set_cut(src.get_cut());
        let dst: &mut Vec<Transform3D> = self.get_mut_joint_transforms();
        dst.clear();
        dst.extend_from_slice(src.get_joint_transforms().as_slice());
    }

    /// Interpolates joint transforms between `self` and `next` into `out` without heap reallocation.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Previous skeletal pose snapshot.
    /// - `&Self` - Current skeletal pose snapshot.
    /// - `f32` - Normalized interpolation factor in `[0.0, 1.0]`.
    /// - `&mut Self` - Pre-allocated destination skeletal pose snapshot.
    fn interpolate_into(&self, next: &Self, alpha: f32, out: &mut Self) {
        let prev_joints: &[Transform3D] = self.get_joint_transforms().as_slice();
        let next_joints: &[Transform3D] = next.get_joint_transforms().as_slice();
        if next.get_cut() || prev_joints.len() != next_joints.len() {
            out.copy_from_snapshot(next);
            return;
        }
        let k: f32 = alpha.clamp(0.0, 1.0);
        out.set_cut(false);
        let dst: &mut Vec<Transform3D> = out.get_mut_joint_transforms();
        dst.clear();
        if dst.capacity() < next_joints.len() {
            dst.reserve(next_joints.len() - dst.capacity());
        }
        for (a, b) in prev_joints.iter().copied().zip(next_joints.iter().copied()) {
            dst.push(a.lerp_shortest_angle(b, k));
        }
    }

    /// Interpolates joint transforms between `self` and `next` unless `next.cut` is set or joint counts differ.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Previous skeletal pose snapshot.
    /// - `&Self` - Current skeletal pose snapshot.
    /// - `f32` - Normalized interpolation factor in `[0.0, 1.0]`.
    ///
    /// # Returns
    ///
    /// - `Self` - Interpolated skeletal pose snapshot.
    fn interpolate(&self, next: &Self, alpha: f32) -> Self {
        let mut out: Self = Self::default();
        self.interpolate_into(next, alpha, &mut out);
        out
    }
}

/// Snapshot builders and zero-allocation in-place capture helpers for `World3DSnapshot`.
impl World3DSnapshot {
    /// Captures all 3D object poses from `world` into `self` reusing backing buffers with zero steady-state allocations.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable destination 3D snapshot.
    /// - `&World3D` - Authoritative 3D world state.
    /// - `u64` - Fixed simulation tick number.
    /// - `bool` - Whether an instant camera cut occurred on this tick.
    /// - `&[SkeletonPoseSnapshot]` - Skeletal pose snapshots sampled on this tick.
    /// - `&[DiscreteGameEvent]` - Un-interpolated discrete events emitted on this tick.
    pub fn capture_into(
        &mut self,
        world: &World3D,
        tick: u64,
        camera_cut: bool,
        skeletons: &[SkeletonPoseSnapshot],
        discrete_events: &[DiscreteGameEvent],
    ) {
        self.set_tick(tick);
        self.set_camera(world.get_camera());
        self.set_camera_cut(camera_cut);
        let objs: &[SceneObject3D] = world.get_objects().as_slice();
        let poses: &mut Vec<ObjectPose3D> = self.get_mut_objects();
        poses.clear();
        if poses.capacity() < objs.len() {
            poses.reserve(objs.len() - poses.capacity());
        }
        for (idx, obj) in objs.iter().enumerate() {
            poses.push(ObjectPose3D::new(
                idx,
                obj.get_transform(),
                obj.get_teleport_epoch(),
                true,
            ));
        }
        let skels: &mut Vec<SkeletonPoseSnapshot> = self.get_mut_skeletons();
        skels.resize_with(skeletons.len(), SkeletonPoseSnapshot::default);
        for (dst, src) in skels.iter_mut().zip(skeletons.iter()) {
            dst.copy_from_snapshot(src);
        }
        let evs: &mut Vec<DiscreteGameEvent> = self.get_mut_discrete_events();
        evs.clear();
        evs.extend_from_slice(discrete_events);
    }

    /// Captures only dynamic 3D objects from `world` into `self` (skipping static buildings/roads) with zero steady-state allocations.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable destination 3D snapshot.
    /// - `&World3D` - Authoritative 3D world state.
    /// - `u64` - Fixed simulation tick number.
    /// - `bool` - Whether an instant camera cut occurred on this tick.
    /// - `&[SkeletonPoseSnapshot]` - Skeletal pose snapshots sampled on this tick.
    /// - `&[DiscreteGameEvent]` - Un-interpolated discrete events emitted on this tick.
    pub fn capture_dynamic_into(
        &mut self,
        world: &World3D,
        tick: u64,
        camera_cut: bool,
        skeletons: &[SkeletonPoseSnapshot],
        discrete_events: &[DiscreteGameEvent],
    ) {
        self.set_tick(tick);
        self.set_camera(world.get_camera());
        self.set_camera_cut(camera_cut);
        let objs: &[SceneObject3D] = world.get_objects().as_slice();
        let poses: &mut Vec<ObjectPose3D> = self.get_mut_objects();
        poses.clear();
        for (idx, obj) in objs.iter().enumerate() {
            if obj.get_is_dynamic() {
                poses.push(ObjectPose3D::new(
                    idx,
                    obj.get_transform(),
                    obj.get_teleport_epoch(),
                    true,
                ));
            }
        }
        let skels: &mut Vec<SkeletonPoseSnapshot> = self.get_mut_skeletons();
        skels.resize_with(skeletons.len(), SkeletonPoseSnapshot::default);
        for (dst, src) in skels.iter_mut().zip(skeletons.iter()) {
            dst.copy_from_snapshot(src);
        }
        let evs: &mut Vec<DiscreteGameEvent> = self.get_mut_discrete_events();
        evs.clear();
        evs.extend_from_slice(discrete_events);
    }

    /// Captures an immutable `World3DSnapshot` from `world` with optional skeletal poses and discrete events.
    ///
    /// # Arguments
    ///
    /// - `&World3D` - Authoritative 3D world state.
    /// - `u64` - Fixed simulation tick number.
    /// - `bool` - Whether an instant camera cut occurred on this tick.
    /// - `Vec<SkeletonPoseSnapshot>` - Skeletal pose snapshots sampled on this tick.
    /// - `Vec<DiscreteGameEvent>` - Un-interpolated discrete events emitted on this tick.
    ///
    /// # Returns
    ///
    /// - `Self` - Captured immutable 3D world snapshot.
    #[must_use]
    pub fn capture(
        world: &World3D,
        tick: u64,
        camera_cut: bool,
        skeletons: Vec<SkeletonPoseSnapshot>,
        discrete_events: Vec<DiscreteGameEvent>,
    ) -> Self {
        let mut snap: Self = Self::default();
        snap.capture_into(world, tick, camera_cut, &[], &[]);
        snap.set_skeletons(skeletons);
        snap.set_discrete_events(discrete_events);
        snap
    }
}

/// Discontinuity-safe render-time interpolation for `World3DSnapshot`.
impl InterpolatableState for World3DSnapshot {
    /// Copies `src` into `self` reusing existing vector allocations.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable destination 3D snapshot.
    /// - `&Self` - Source 3D snapshot.
    fn copy_from_snapshot(&mut self, src: &Self) {
        self.set_tick(src.get_tick());
        self.set_camera(src.get_camera());
        self.set_camera_cut(src.get_camera_cut());
        let objs: &mut Vec<ObjectPose3D> = self.get_mut_objects();
        objs.clear();
        objs.extend_from_slice(src.get_objects().as_slice());
        let src_skels: &[SkeletonPoseSnapshot] = src.get_skeletons().as_slice();
        let dst_skels: &mut Vec<SkeletonPoseSnapshot> = self.get_mut_skeletons();
        dst_skels.resize_with(src_skels.len(), SkeletonPoseSnapshot::default);
        for (dst, s) in dst_skels.iter_mut().zip(src_skels.iter()) {
            dst.copy_from_snapshot(s);
        }
        let evs: &mut Vec<DiscreteGameEvent> = self.get_mut_discrete_events();
        evs.clear();
        evs.extend_from_slice(src.get_discrete_events().as_slice());
    }

    /// Interpolates 3D camera, object transforms, and skeletal poses into `out` without heap allocation.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Previous fixed-step 3D snapshot.
    /// - `&Self` - Current fixed-step 3D snapshot.
    /// - `f32` - Normalized interpolation factor in `[0.0, 1.0]`.
    /// - `&mut Self` - Pre-allocated destination 3D snapshot overwritten in place.
    fn interpolate_into(&self, next: &Self, alpha: f32, out: &mut Self) {
        let k: f32 = alpha.clamp(0.0, 1.0);
        let camera: Camera3D = if next.get_camera_cut() {
            next.get_camera()
        } else {
            self.get_camera().lerp(next.get_camera(), k)
        };
        out.set_tick(next.get_tick());
        out.set_camera(camera);
        out.set_camera_cut(next.get_camera_cut());
        let prev_objs: &[ObjectPose3D] = self.get_objects().as_slice();
        let next_objs: &[ObjectPose3D] = next.get_objects().as_slice();
        let out_objs: &mut Vec<ObjectPose3D> = out.get_mut_objects();
        out_objs.clear();
        if out_objs.capacity() < next_objs.len() {
            out_objs.reserve(next_objs.len() - out_objs.capacity());
        }
        for (idx, curr) in next_objs.iter().copied().enumerate() {
            let mut blended: ObjectPose3D = curr;
            if curr.get_visible()
                && let Some(prev) = prev_objs.get(idx).copied()
            {
                let continuous: bool = prev.get_visible()
                    && prev.get_object_index() == curr.get_object_index()
                    && prev.get_teleport_epoch() == curr.get_teleport_epoch();
                if continuous {
                    blended.set_transform(
                        prev.get_transform()
                            .lerp_shortest_angle(curr.get_transform(), k),
                    );
                }
            }
            out_objs.push(blended);
        }
        let prev_skels: &[SkeletonPoseSnapshot] = self.get_skeletons().as_slice();
        let next_skels: &[SkeletonPoseSnapshot] = next.get_skeletons().as_slice();
        let out_skels: &mut Vec<SkeletonPoseSnapshot> = out.get_mut_skeletons();
        out_skels.resize_with(next_skels.len(), SkeletonPoseSnapshot::default);
        for (idx, curr_sk) in next_skels.iter().enumerate() {
            if let Some(prev_sk) = prev_skels.get(idx) {
                prev_sk.interpolate_into(curr_sk, k, &mut out_skels[idx]);
            } else {
                out_skels[idx].copy_from_snapshot(curr_sk);
            }
        }
        let out_evs: &mut Vec<DiscreteGameEvent> = out.get_mut_discrete_events();
        out_evs.clear();
        out_evs.extend_from_slice(next.get_discrete_events().as_slice());
    }

    /// Interpolates 3D camera, continuous object transforms, and skeletal poses while snapping cuts, teleports, and discrete events.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Previous fixed-step 3D snapshot.
    /// - `&Self` - Current fixed-step 3D snapshot.
    /// - `f32` - Normalized interpolation factor in `[0.0, 1.0]`.
    ///
    /// # Returns
    ///
    /// - `Self` - Render-time interpolated 3D world snapshot.
    fn interpolate(&self, next: &Self, alpha: f32) -> Self {
        let mut out: Self = Self::default();
        self.interpolate_into(next, alpha, &mut out);
        out
    }
}