viewport-lib 0.2.1

3D viewport rendering library
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
//! Smooth camera motion with exponential damping and fly-to animations.
//!
//! `CameraAnimator` does NOT own the [`Camera`]. It takes `&mut Camera` in
//! [`update()`](crate::camera::animator::CameraAnimator::update) so the application decides whether to
//! use animation at all.

use crate::camera::camera::{Camera, Projection};

/// Easing function for fly-to animations.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub enum Easing {
    /// Constant velocity (t).
    Linear,
    /// Decelerates near the end — feels natural for camera snapping.
    EaseOutCubic,
    /// Accelerates then decelerates — smooth for longer transitions.
    EaseInOutCubic,
}

impl Easing {
    /// Evaluate the easing curve at `t` (clamped to 0..1).
    pub fn eval(self, t: f32) -> f32 {
        let t = t.clamp(0.0, 1.0);
        match self {
            Self::Linear => t,
            Self::EaseOutCubic => {
                let inv = 1.0 - t;
                1.0 - inv * inv * inv
            }
            Self::EaseInOutCubic => {
                if t < 0.5 {
                    4.0 * t * t * t
                } else {
                    let p = -2.0 * t + 2.0;
                    1.0 - p * p * p / 2.0
                }
            }
        }
    }
}

/// Damping coefficients for camera motion channels.
///
/// Each value is in the range `0.0..1.0` where 0 = no damping (instant) and
/// 0.85 = smooth deceleration. Values are frame-rate independent via
/// `damping.powf(dt * 60.0)`.
#[derive(Clone, Debug)]
pub struct CameraDamping {
    /// Orbit (rotation) damping factor.
    pub orbit: f32,
    /// Pan (translation) damping factor.
    pub pan: f32,
    /// Zoom (distance) damping factor.
    pub zoom: f32,
    /// Velocity magnitude below which motion stops.
    pub epsilon: f32,
}

impl Default for CameraDamping {
    fn default() -> Self {
        Self {
            orbit: 0.85,
            pan: 0.85,
            zoom: 0.85,
            epsilon: 0.0001,
        }
    }
}

/// An in-progress fly-to animation.
#[derive(Clone, Debug)]
struct CameraFlight {
    start_center: glam::Vec3,
    start_distance: f32,
    start_orientation: glam::Quat,
    #[allow(dead_code)]
    start_projection: Projection,
    target_center: glam::Vec3,
    target_distance: f32,
    target_orientation: glam::Quat,
    target_projection: Option<Projection>,
    duration: f32,
    elapsed: f32,
    easing: Easing,
}

/// Smooth camera controller with exponential damping and animated transitions.
///
/// Feed raw input deltas via [`apply_orbit`](Self::apply_orbit),
/// [`apply_pan`](Self::apply_pan), [`apply_zoom`](Self::apply_zoom). Then call
/// [`update`](Self::update) once per frame to apply decayed velocities to the
/// camera.
///
/// For animated transitions (view presets, zoom-to-fit), use
/// [`fly_to`](Self::fly_to). Any raw input during a flight cancels it.
pub struct CameraAnimator {
    damping: CameraDamping,
    /// (yaw_rate, pitch_rate) in radians.
    orbit_velocity: glam::Vec2,
    /// (right, up) in world units.
    pan_velocity: glam::Vec2,
    /// Distance change rate.
    zoom_velocity: f32,
    /// Active fly-to animation, if any.
    flight: Option<CameraFlight>,
}

impl CameraAnimator {
    /// Create an animator with custom damping.
    pub fn new(damping: CameraDamping) -> Self {
        Self {
            damping,
            orbit_velocity: glam::Vec2::ZERO,
            pan_velocity: glam::Vec2::ZERO,
            zoom_velocity: 0.0,
            flight: None,
        }
    }

    /// Create an animator with sensible default damping values.
    pub fn with_default_damping() -> Self {
        Self::new(CameraDamping::default())
    }

    /// Feed a raw orbit delta (yaw, pitch) in radians.
    pub fn apply_orbit(&mut self, yaw_delta: f32, pitch_delta: f32) {
        if self.flight.is_some() {
            self.flight = None;
        }
        self.orbit_velocity += glam::Vec2::new(yaw_delta, pitch_delta);
    }

    /// Feed a raw pan delta (right, up) in world units.
    pub fn apply_pan(&mut self, right_delta: f32, up_delta: f32) {
        if self.flight.is_some() {
            self.flight = None;
        }
        self.pan_velocity += glam::Vec2::new(right_delta, up_delta);
    }

    /// Feed a raw zoom delta (distance change).
    pub fn apply_zoom(&mut self, delta: f32) {
        if self.flight.is_some() {
            self.flight = None;
        }
        self.zoom_velocity += delta;
    }

    /// Start an animated fly-to transition with default easing.
    pub fn fly_to(
        &mut self,
        camera: &Camera,
        target_center: glam::Vec3,
        target_distance: f32,
        target_orientation: glam::Quat,
        duration: f32,
    ) {
        self.fly_to_full(
            camera,
            target_center,
            target_distance,
            target_orientation,
            None,
            duration,
            Easing::EaseOutCubic,
        );
    }

    /// Start an animated fly-to transition with custom easing and optional projection change.
    pub fn fly_to_with_easing(
        &mut self,
        camera: &Camera,
        target_center: glam::Vec3,
        target_distance: f32,
        target_orientation: glam::Quat,
        duration: f32,
        easing: Easing,
    ) {
        self.fly_to_full(
            camera,
            target_center,
            target_distance,
            target_orientation,
            None,
            duration,
            easing,
        );
    }

    /// Start a fly-to transition with all options.
    #[allow(clippy::too_many_arguments)]
    pub fn fly_to_full(
        &mut self,
        camera: &Camera,
        target_center: glam::Vec3,
        target_distance: f32,
        target_orientation: glam::Quat,
        target_projection: Option<Projection>,
        duration: f32,
        easing: Easing,
    ) {
        // Zero out velocities — the flight takes over.
        self.orbit_velocity = glam::Vec2::ZERO;
        self.pan_velocity = glam::Vec2::ZERO;
        self.zoom_velocity = 0.0;

        self.flight = Some(CameraFlight {
            start_center: camera.center,
            start_distance: camera.distance,
            start_orientation: camera.orientation,
            start_projection: camera.projection,
            target_center,
            target_distance,
            target_orientation,
            target_projection,
            duration: duration.max(0.001),
            elapsed: 0.0,
            easing,
        });
    }

    /// Cancel any active fly-to animation.
    pub fn cancel_flight(&mut self) {
        self.flight = None;
    }

    /// Returns `true` if the animator has residual velocity or an active flight.
    pub fn is_animating(&self) -> bool {
        if self.flight.is_some() {
            return true;
        }
        let eps = self.damping.epsilon;
        self.orbit_velocity.length() > eps
            || self.pan_velocity.length() > eps
            || self.zoom_velocity.abs() > eps
    }

    /// Advance the animator by `dt` seconds and apply motion to `camera`.
    ///
    /// Returns `true` if the camera was modified (useful for triggering redraws).
    pub fn update(&mut self, dt: f32, camera: &mut Camera) -> bool {
        // Handle active flight.
        if let Some(ref mut flight) = self.flight {
            flight.elapsed += dt;
            let raw_t = (flight.elapsed / flight.duration).min(1.0);
            let t = flight.easing.eval(raw_t);

            camera.center = flight.start_center.lerp(flight.target_center, t);
            camera.distance =
                flight.start_distance + (flight.target_distance - flight.start_distance) * t;
            camera.orientation = flight.start_orientation.slerp(flight.target_orientation, t);

            // Switch projection at the end of the animation.
            if raw_t >= 1.0 {
                if let Some(proj) = flight.target_projection {
                    camera.projection = proj;
                }
                self.flight = None;
            }
            return true;
        }

        let eps = self.damping.epsilon;
        let mut changed = false;

        // Apply orbit velocity.
        if self.orbit_velocity.length() > eps {
            let yaw = self.orbit_velocity.x;
            let pitch = self.orbit_velocity.y;
            let yaw_rot = glam::Quat::from_rotation_y(-yaw);
            let pitch_rot = glam::Quat::from_rotation_x(-pitch);
            camera.orientation = (yaw_rot * camera.orientation * pitch_rot).normalize();
            self.orbit_velocity *= self.damping.orbit.powf(dt * 60.0);
            if self.orbit_velocity.length() <= eps {
                self.orbit_velocity = glam::Vec2::ZERO;
            }
            changed = true;
        }

        // Apply pan velocity.
        if self.pan_velocity.length() > eps {
            let right = camera.right();
            let up = camera.up();
            camera.center -= right * self.pan_velocity.x + up * self.pan_velocity.y;
            self.pan_velocity *= self.damping.pan.powf(dt * 60.0);
            if self.pan_velocity.length() <= eps {
                self.pan_velocity = glam::Vec2::ZERO;
            }
            changed = true;
        }

        // Apply zoom velocity.
        if self.zoom_velocity.abs() > eps {
            camera.distance = (camera.distance + self.zoom_velocity).max(0.01);
            self.zoom_velocity *= self.damping.zoom.powf(dt * 60.0);
            if self.zoom_velocity.abs() <= eps {
                self.zoom_velocity = 0.0;
            }
            changed = true;
        }

        changed
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn default_camera() -> Camera {
        Camera::default()
    }

    #[test]
    fn test_damping_decays_velocity() {
        let mut anim = CameraAnimator::with_default_damping();
        let mut cam = default_camera();
        anim.apply_orbit(0.5, 0.3);
        assert!(anim.is_animating());
        // Run many frames — velocity should decay to zero.
        for _ in 0..300 {
            anim.update(1.0 / 60.0, &mut cam);
        }
        assert!(!anim.is_animating(), "should have settled after 300 frames");
    }

    #[test]
    fn test_zero_damping_passes_through() {
        let damping = CameraDamping {
            orbit: 0.0,
            pan: 0.0,
            zoom: 0.0,
            epsilon: 0.0001,
        };
        let mut anim = CameraAnimator::new(damping);
        let mut cam = default_camera();
        let orig_orientation = cam.orientation;
        anim.apply_orbit(0.1, 0.0);
        anim.update(1.0 / 60.0, &mut cam);
        // With zero damping, velocity decays instantly (0^anything=0).
        assert!(
            !anim.is_animating(),
            "zero damping should settle in one frame"
        );
        // Camera should have moved.
        assert!(
            (cam.orientation.x - orig_orientation.x).abs() > 1e-6
                || (cam.orientation.y - orig_orientation.y).abs() > 1e-6,
            "camera orientation should have changed"
        );
    }

    #[test]
    fn test_fly_to_reaches_target() {
        let mut anim = CameraAnimator::with_default_damping();
        let mut cam = default_camera();
        let target_center = glam::Vec3::new(10.0, 20.0, 30.0);
        let target_dist = 15.0;
        let target_orient = glam::Quat::from_rotation_y(std::f32::consts::PI);
        anim.fly_to(&cam, target_center, target_dist, target_orient, 0.5);

        // Advance well past the duration.
        for _ in 0..120 {
            anim.update(1.0 / 60.0, &mut cam);
        }
        assert!(
            (cam.center - target_center).length() < 1e-4,
            "center should match target: {:?}",
            cam.center
        );
        assert!(
            (cam.distance - target_dist).abs() < 1e-4,
            "distance should match target: {}",
            cam.distance
        );
    }

    #[test]
    fn test_fly_to_cancelled_by_input() {
        let mut anim = CameraAnimator::with_default_damping();
        let mut cam = default_camera();
        let target = glam::Vec3::new(100.0, 0.0, 0.0);
        anim.fly_to(&cam, target, 50.0, glam::Quat::IDENTITY, 1.0);

        // Advance a few frames.
        for _ in 0..5 {
            anim.update(1.0 / 60.0, &mut cam);
        }
        // Apply user input — should cancel the flight.
        anim.apply_orbit(0.1, 0.0);
        // The flight should be gone.
        anim.update(1.0 / 60.0, &mut cam);
        // Camera should NOT have reached the target.
        assert!(
            (cam.center - target).length() > 1.0,
            "flight should have been cancelled"
        );
    }

    #[test]
    fn test_is_animating_reflects_state() {
        let mut anim = CameraAnimator::with_default_damping();
        let mut cam = default_camera();
        assert!(!anim.is_animating());

        anim.apply_zoom(1.0);
        assert!(anim.is_animating());

        for _ in 0..300 {
            anim.update(1.0 / 60.0, &mut cam);
        }
        assert!(!anim.is_animating());
    }

    #[test]
    fn test_easing_boundaries() {
        for easing in [Easing::Linear, Easing::EaseOutCubic, Easing::EaseInOutCubic] {
            let v0 = easing.eval(0.0);
            let v1 = easing.eval(1.0);
            assert!(v0.abs() < 1e-6, "{easing:?}: eval(0) = {v0}, expected 0");
            assert!(
                (v1 - 1.0).abs() < 1e-6,
                "{easing:?}: eval(1) = {v1}, expected 1"
            );
        }
    }
}