three-rs 0.2.0

A port of three.js core and its WebGPURenderer to Rust on wgpu, graded against three.js's own end-to-end pixel comparison.
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//! Port of `three.js/src/animation/AnimationAction.js`.
//!
//! Ownership: Three's action holds `this._mixer` and calls back into it
//! (`_activateAction`, `_lendControlInterpolant`, `dispatchEvent`, …). Here the
//! mixer owns the actions in an arena (see
//! [`crate::animation::animation_mixer`]) and the action never names its mixer.
//! The two mixer-owned pools an action mutates —
//! [`BindingPool`] (Three's
//! `_bindings`, holding the `PropertyMixer`s) and
//! [`ControlPool`] (Three's
//! `_controlInterpolants`) — are passed in as arguments to the methods that
//! need them. Those methods carry a trailing underscore (`reset_`, `warp_`, …);
//! the un-suffixed names live on `AnimationMixer`, which takes an
//! `ActionHandle` and supplies the pools. `getMixer` therefore has no port: the
//! mixer is the thing you are calling.
//!
//! Divergences from Three, all forced by Rust:
//! - Three aliases `interpolant.resultBuffer = binding.buffer` so that
//!   `evaluate()` writes straight into the `PropertyMixer`'s `incoming` region.
//!   Two owners cannot share one buffer here, so `_update` evaluates into the
//!   interpolant's own result buffer and copies `valueSize` values into
//!   `buffer[ 0 .. valueSize ]` — bit-for-bit the same input to `accumulate`.
//! - Three shares one `_interpolantSettings` object between every interpolant
//!   of the action, so `_setEndings` mutates all of them at once. Here the
//!   settings live on the action and are pushed into each interpolant's
//!   `data.settings` right before it is evaluated.
//! - Events are dropped: Three's `_updateTime` dispatches `'loop'` and
//!   `'finished'` on the mixer. There is no `EventDispatcher` port in this
//!   crate, so the state changes happen (`paused` / `enabled` / clamped `time`)
//!   and the notification does not.

use crate::animation::animation_clip::{AnimationBlendMode, AnimationClip};
use crate::animation::animation_mixer::{BindingPool, ControlHandle, ControlPool, RootId};
use crate::animation::keyframe_track::TrackInterpolant;
use crate::math::interpolant::{Ending, InterpolantSettings};

/// `constants.js` loop modes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LoopMode {
    /// `LoopOnce` (2200).
    Once,
    /// `LoopRepeat` (2201).
    Repeat,
    /// `LoopPingPong` (2202).
    PingPong,
}

impl LoopMode {
    /// The numeric constant three.js uses.
    pub fn as_number(self) -> u32 {
        match self {
            Self::Once => 2200,
            Self::Repeat => 2201,
            Self::PingPong => 2202,
        }
    }

    /// The inverse of [`LoopMode::as_number`].
    pub fn from_number(n: u32) -> Option<Self> {
        match n {
            2200 => Some(Self::Once),
            2201 => Some(Self::Repeat),
            2202 => Some(Self::PingPong),
            _ => None,
        }
    }
}

/// `AnimationAction`.
pub struct AnimationAction {
    /// `_clip`. Three keeps a reference; the arena keeps a clone.
    pub(crate) clip: AnimationClip,
    /// `_localRoot` — `None` means "the mixer's own root".
    pub(crate) local_root: Option<RootId>,
    /// `blendMode`.
    pub blend_mode: AnimationBlendMode,

    /// `_interpolants`. `None` for a track whose `createInterpolant` returns
    /// `null` (a string track, or a Bezier track, which is not ported).
    pub(crate) interpolants: Vec<Option<TrackInterpolant>>,
    /// `_interpolantSettings`, shared by every interpolant in Three.
    pub(crate) interpolant_settings: InterpolantSettings,
    /// `_propertyBindings`, as handles into the mixer's [`BindingPool`].
    /// `None` for a track that did not resolve against the root.
    pub(crate) property_bindings: Vec<Option<usize>>,

    /// `_cacheIndex` — index into the mixer's action order, `None` when the
    /// memory manager has forgotten this action.
    pub(crate) cache_index: Option<usize>,
    /// `_byClipCacheIndex`.
    pub(crate) by_clip_cache_index: Option<usize>,

    /// `_timeScaleInterpolant`.
    pub(crate) time_scale_interpolant: Option<ControlHandle>,
    /// `_restoreTimeScale`.
    pub(crate) restore_time_scale: Option<f64>,
    /// `_weightInterpolant`.
    pub(crate) weight_interpolant: Option<ControlHandle>,

    /// `loop` (a Rust keyword), set via `setLoop`.
    pub loop_mode: LoopMode,
    /// `_loopCount`.
    pub loop_count: i32,

    /// `_startTime`: the global mixer time when the action is to be started.
    pub start_time: Option<f64>,

    /// `time` — the local time of this action, in seconds.
    pub time: f64,
    /// `timeScale`.
    pub time_scale: f64,
    /// `_effectiveTimeScale`.
    pub(crate) effective_time_scale: f64,
    /// `weight`.
    pub weight: f64,
    /// `_effectiveWeight`.
    pub(crate) effective_weight: f64,
    /// `repetitions` (`Infinity` by default).
    pub repetitions: f64,
    /// `paused`.
    pub paused: bool,
    /// `enabled`.
    pub enabled: bool,
    /// `clampWhenFinished`.
    pub clamp_when_finished: bool,
    /// `zeroSlopeAtStart`.
    pub zero_slope_at_start: bool,
    /// `zeroSlopeAtEnd`.
    pub zero_slope_at_end: bool,
}

impl AnimationAction {
    /// `new AnimationAction( mixer, clip, localRoot, blendMode )`. The mixer is
    /// implicit (it is the arena that will hold this action).
    pub fn new(
        clip: AnimationClip,
        local_root: Option<RootId>,
        blend_mode: Option<AnimationBlendMode>,
    ) -> Self {
        let blend_mode = blend_mode.unwrap_or(clip.blend_mode);

        let interpolants: Vec<Option<TrackInterpolant>> = clip
            .tracks
            .iter()
            .map(|track| track.create_interpolant(None))
            .collect();

        let n_tracks = interpolants.len();

        Self {
            clip,
            local_root,
            blend_mode,
            interpolants,
            interpolant_settings: InterpolantSettings {
                ending_start: Ending::ZeroCurvature,
                ending_end: Ending::ZeroCurvature,
            },
            property_bindings: vec![None; n_tracks],
            cache_index: None,
            by_clip_cache_index: None,
            time_scale_interpolant: None,
            restore_time_scale: None,
            weight_interpolant: None,
            loop_mode: LoopMode::Repeat,
            loop_count: -1,
            start_time: None,
            time: 0.0,
            time_scale: 1.0,
            effective_time_scale: 1.0,
            weight: 1.0,
            effective_weight: 1.0,
            repetitions: f64::INFINITY,
            paused: false,
            enabled: true,
            clamp_when_finished: false,
            zero_slope_at_start: true,
            zero_slope_at_end: true,
        }
    }

    /// `getClip()`.
    pub fn get_clip(&self) -> &AnimationClip {
        &self.clip
    }

    /// `_localRoot` — `getRoot()` is on the mixer, since `_localRoot ||
    /// mixer._root` needs the mixer.
    pub fn local_root(&self) -> Option<RootId> {
        self.local_root
    }

    /// `reset()`.
    pub(crate) fn reset_(&mut self, control: &mut ControlPool) -> &mut Self {
        self.paused = false;
        self.enabled = true;

        self.time = 0.0; // restart clip
        self.loop_count = -1; // forget previous loops
        self.start_time = None; // forget scheduling

        self.stop_fading_(control).stop_warping_(control)
    }

    /// The `enabled && ! paused && timeScale !== 0 && _startTime === null` half
    /// of `isRunning()`; the mixer ands in `_isActiveAction`.
    pub(crate) fn is_running_locally(&self) -> bool {
        self.enabled && !self.paused && self.time_scale != 0.0 && self.start_time.is_none()
    }

    /// `startAt( time )`.
    pub(crate) fn start_at_(&mut self, time: f64) -> &mut Self {
        self.start_time = Some(time);
        self
    }

    /// `setLoop( mode, repetitions )`.
    pub(crate) fn set_loop_(&mut self, mode: LoopMode, repetitions: f64) -> &mut Self {
        self.loop_mode = mode;
        self.repetitions = repetitions;
        self
    }

    /// `setEffectiveWeight( weight )`.
    pub(crate) fn set_effective_weight_(
        &mut self,
        weight: f64,
        control: &mut ControlPool,
    ) -> &mut Self {
        self.weight = weight;

        // note: same logic as when updated at runtime
        self.effective_weight = if self.enabled { weight } else { 0.0 };

        self.stop_fading_(control)
    }

    /// `getEffectiveWeight()`.
    pub fn get_effective_weight(&self) -> f64 {
        self.effective_weight
    }

    /// `fadeIn( duration )`.
    pub(crate) fn fade_in_(
        &mut self,
        duration: f64,
        now: f64,
        control: &mut ControlPool,
    ) -> &mut Self {
        self.schedule_fading_(duration, 0.0, 1.0, now, control)
    }

    /// `fadeOut( duration )`.
    pub(crate) fn fade_out_(
        &mut self,
        duration: f64,
        now: f64,
        control: &mut ControlPool,
    ) -> &mut Self {
        self.schedule_fading_(duration, 1.0, 0.0, now, control)
    }

    /// `stopFading()`.
    pub(crate) fn stop_fading_(&mut self, control: &mut ControlPool) -> &mut Self {
        if let Some(handle) = self.weight_interpolant.take() {
            control.take_back_control_interpolant(handle);
        }
        self
    }

    /// `setEffectiveTimeScale( timeScale )`.
    pub(crate) fn set_effective_time_scale_(
        &mut self,
        time_scale: f64,
        control: &mut ControlPool,
    ) -> &mut Self {
        self.time_scale = time_scale;
        self.effective_time_scale = if self.paused { 0.0 } else { time_scale };

        self.stop_warping_(control)
    }

    /// `getEffectiveTimeScale()`.
    pub fn get_effective_time_scale(&self) -> f64 {
        self.effective_time_scale
    }

    /// `setDuration( duration )`.
    pub(crate) fn set_duration_(&mut self, duration: f64, control: &mut ControlPool) -> &mut Self {
        self.time_scale = self.clip.duration / duration;
        self.stop_warping_(control)
    }

    /// `syncWith( action )`, with the other action's `time` / `timeScale` read
    /// out by the mixer (the arena cannot hand out two `&mut` actions).
    pub(crate) fn sync_with_(
        &mut self,
        other_time: f64,
        other_time_scale: f64,
        control: &mut ControlPool,
    ) -> &mut Self {
        self.time = other_time;
        self.time_scale = other_time_scale;
        self.stop_warping_(control)
    }

    /// `halt( duration )`.
    pub(crate) fn halt_(
        &mut self,
        duration: f64,
        now: f64,
        control: &mut ControlPool,
    ) -> &mut Self {
        let from = self.effective_time_scale;
        self.warp_(from, 0.0, duration, now, control)
    }

    /// `warp( startTimeScale, endTimeScale, duration )`.
    pub(crate) fn warp_(
        &mut self,
        start_time_scale: f64,
        end_time_scale: f64,
        duration: f64,
        now: f64,
        control: &mut ControlPool,
    ) -> &mut Self {
        let time_scale = self.time_scale;

        let handle = match self.time_scale_interpolant {
            Some(handle) => handle,
            None => {
                let handle = control.lend_control_interpolant();
                self.time_scale_interpolant = Some(handle);
                handle
            }
        };

        let data = control.data_mut(handle);
        data.parameter_positions[0] = now;
        data.parameter_positions[1] = now + duration;
        data.sample_values[0] = start_time_scale / time_scale;
        data.sample_values[1] = end_time_scale / time_scale;

        self
    }

    /// `stopWarping()`.
    pub(crate) fn stop_warping_(&mut self, control: &mut ControlPool) -> &mut Self {
        if let Some(handle) = self.time_scale_interpolant.take() {
            control.take_back_control_interpolant(handle);
        }

        self.restore_time_scale = None;

        self
    }

    // Internal

    /// `_update( time, deltaTime, timeDirection, accuIndex )`.
    pub(crate) fn update_(
        &mut self,
        time: f64,
        mut delta_time: f64,
        time_direction: f64,
        accu_index: usize,
        bindings: &mut BindingPool,
        control: &mut ControlPool,
    ) {
        // called by the mixer

        if !self.enabled {
            // call ._updateWeight() to update ._effectiveWeight

            self.update_weight_(time, control);
            return;
        }

        if let Some(start_time) = self.start_time {
            // check for scheduled start of action

            let time_running = (time - start_time) * time_direction;
            if time_running < 0.0 || time_direction == 0.0 {
                delta_time = 0.0;
            } else {
                self.start_time = None; // unschedule
                delta_time = time_direction * time_running;
            }
        }

        // apply time scale and advance time

        delta_time *= self.update_time_scale_(time, control);
        let clip_time = self.update_time_(delta_time);

        // note: _updateTime may disable the action resulting in
        // an effective weight of 0

        let weight = self.update_weight_(time, control);

        if weight > 0.0 {
            let blend_mode = self.blend_mode;

            for j in 0..self.interpolants.len() {
                let Some(binding_handle) = self.property_bindings[j] else {
                    continue;
                };
                let Some(interpolant) = self.interpolants[j].as_mut() else {
                    continue;
                };

                // Three's shared `_interpolantSettings` object, pushed in.
                interpolant.data().settings = Some(self.interpolant_settings);

                let values = interpolant.evaluate(clip_time);

                let Some(property_mixer) = bindings.get_mut(binding_handle) else {
                    continue;
                };

                // Stands in for `interpolant.resultBuffer = binding.buffer`.
                let n = property_mixer.value_size;
                property_mixer.buffer[..n].copy_from_slice(&values[..n]);

                match blend_mode {
                    AnimationBlendMode::Additive => {
                        property_mixer.accumulate_additive(weight);
                    }
                    AnimationBlendMode::Normal => {
                        property_mixer.accumulate(accu_index, weight);
                    }
                }
            }
        }
    }

    /// `_updateWeight( time )`.
    pub(crate) fn update_weight_(&mut self, time: f64, control: &mut ControlPool) -> f64 {
        let mut weight = 0.0;

        if self.enabled {
            weight = self.weight;

            if let Some(handle) = self.weight_interpolant {
                let interpolant_value = control.evaluate(handle, time);

                weight *= interpolant_value;

                if time > control.data_mut(handle).parameter_positions[1] {
                    self.stop_fading_(control);

                    if interpolant_value == 0.0 {
                        // faded out, disable
                        self.enabled = false;
                    }
                }
            }
        }

        self.effective_weight = weight;
        weight
    }

    /// `_updateTimeScale( time )`.
    pub(crate) fn update_time_scale_(&mut self, time: f64, control: &mut ControlPool) -> f64 {
        let mut time_scale = 0.0;

        if !self.paused {
            time_scale = self.time_scale;

            if let Some(handle) = self.time_scale_interpolant {
                let interpolant_value = control.evaluate(handle, time);

                time_scale *= interpolant_value;

                if time > control.data_mut(handle).parameter_positions[1] {
                    if time_scale == 0.0 {
                        // motion has halted, pause
                        self.paused = true;
                    } else {
                        if let Some(restore) = self.restore_time_scale {
                            time_scale = restore;
                        }

                        // warp done - apply final time scale
                        self.time_scale = time_scale;
                    }

                    self.stop_warping_(control);
                }
            }
        }

        self.effective_time_scale = time_scale;
        time_scale
    }

    /// `_updateTime( deltaTime )`.
    ///
    /// The `'loop'` / `'finished'` events are dropped (see the module docs);
    /// everything else is Three's control flow verbatim.
    pub(crate) fn update_time_(&mut self, delta_time: f64) -> f64 {
        let duration = self.clip.duration;
        let loop_mode = self.loop_mode;

        let mut time = self.time + delta_time;
        let mut loop_count = self.loop_count;

        let ping_pong = loop_mode == LoopMode::PingPong;

        if delta_time == 0.0 {
            if loop_count == -1 {
                return time;
            }

            return if ping_pong && (loop_count & 1) == 1 {
                duration - time
            } else {
                time
            };
        }

        if loop_mode == LoopMode::Once {
            if loop_count == -1 {
                // just started

                self.loop_count = 0;
                self.set_endings_(true, true, false);
            }

            // `handle_stop:` labelled block.
            #[allow(clippy::never_loop)]
            // deliberately mirrors three.js's `handle_stop:` labelled block, used only to `break` out early
            loop {
                if time >= duration {
                    time = duration;
                } else if time < 0.0 {
                    time = 0.0;
                } else {
                    self.time = time;

                    break;
                }

                if self.clamp_when_finished {
                    self.paused = true;
                } else {
                    self.enabled = false;
                }

                self.time = time;

                // DROPPED: mixer.dispatchEvent( { type: 'finished', … } ).

                break;
            }
        } else {
            // repetitive Repeat or PingPong

            if loop_count == -1 {
                // just started

                if delta_time >= 0.0 {
                    loop_count = 0;

                    self.set_endings_(true, self.repetitions == 0.0, ping_pong);
                } else {
                    // when looping in reverse direction, the initial
                    // transition through zero counts as a repetition,
                    // so leave loopCount at -1

                    self.set_endings_(self.repetitions == 0.0, true, ping_pong);
                }
            }

            if time >= duration || time < 0.0 {
                // wrap around

                let loop_delta = (time / duration).floor(); // signed
                time -= duration * loop_delta;

                loop_count += loop_delta.abs() as i32;

                let pending = self.repetitions - loop_count as f64;

                if pending <= 0.0 {
                    // have to stop (switch state, clamp time, fire event)

                    if self.clamp_when_finished {
                        self.paused = true;
                    } else {
                        self.enabled = false;
                    }

                    time = if delta_time > 0.0 { duration } else { 0.0 };

                    self.time = time;

                    // DROPPED: mixer.dispatchEvent( { type: 'finished', … } ).
                } else {
                    // keep running

                    if pending == 1.0 {
                        // entering the last round

                        let at_start = delta_time < 0.0;
                        self.set_endings_(at_start, !at_start, ping_pong);
                    } else {
                        self.set_endings_(false, false, ping_pong);
                    }

                    self.loop_count = loop_count;

                    self.time = time;

                    // DROPPED: mixer.dispatchEvent( { type: 'loop', … } ).
                }
            } else {
                self.loop_count = loop_count;
                self.time = time;
            }

            if ping_pong && (loop_count & 1) == 1 {
                // invert time for the "pong round"

                return duration - time;
            }
        }

        time
    }

    /// `_setEndings( atStart, atEnd, pingPong )`.
    pub(crate) fn set_endings_(&mut self, at_start: bool, at_end: bool, ping_pong: bool) {
        let settings = &mut self.interpolant_settings;

        if ping_pong {
            settings.ending_start = Ending::ZeroSlope;
            settings.ending_end = Ending::ZeroSlope;
        } else {
            // assuming for LoopOnce atStart == atEnd == true

            settings.ending_start = if at_start {
                if self.zero_slope_at_start {
                    Ending::ZeroSlope
                } else {
                    Ending::ZeroCurvature
                }
            } else {
                Ending::WrapAround
            };

            settings.ending_end = if at_end {
                if self.zero_slope_at_end {
                    Ending::ZeroSlope
                } else {
                    Ending::ZeroCurvature
                }
            } else {
                Ending::WrapAround
            };
        }
    }

    /// `_scheduleFading( duration, weightNow, weightThen )`.
    pub(crate) fn schedule_fading_(
        &mut self,
        duration: f64,
        weight_now: f64,
        weight_then: f64,
        now: f64,
        control: &mut ControlPool,
    ) -> &mut Self {
        let handle = match self.weight_interpolant {
            Some(handle) => handle,
            None => {
                let handle = control.lend_control_interpolant();
                self.weight_interpolant = Some(handle);
                handle
            }
        };

        let data = control.data_mut(handle);
        data.parameter_positions[0] = now;
        data.sample_values[0] = weight_now;
        data.parameter_positions[1] = now + duration;
        data.sample_values[1] = weight_then;

        self
    }
}