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
use glam::{DVec3, Mat4, Quat, Vec2, Vec3};
use super::exposure::PhysicalExposure;
use super::pose::Pose;
use super::projection::Projection;
/// An orbit view: the point a camera turns about, its distance from the eye,
/// and the camera orientation.
///
/// Orbit controllers keep one as their state, [`Camera::orbit_target`] reads
/// one off a camera, and the framing helpers
/// ([`Camera::fit_sphere_target`], [`Camera::fit_aabb_target`]) return one for
/// a preset or a fly-to: pass `target.centre` / `target.distance` /
/// `target.orientation` to `CameraAnimator::fly_to`.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CameraTarget {
/// Orbit centre in world space, in f64.
#[cfg_attr(feature = "serde", serde(alias = "center"))]
pub centre: DVec3,
/// Distance from the centre to the eye.
pub distance: f32,
/// Camera orientation as a unit quaternion.
pub orientation: Quat,
}
impl CameraTarget {
/// An orbit view from its parts.
pub fn new(centre: impl Into<DVec3>, distance: f32, orientation: Quat) -> Self {
Self {
centre: centre.into(),
distance,
orientation,
}
}
/// The camera pose this view describes.
pub fn pose(&self) -> Pose {
Pose::orbit(self.centre, self.distance, self.orientation)
}
/// Eye position in world space.
pub fn eye(&self) -> DVec3 {
self.pose().position
}
/// Turn about the centre by `yaw` and `pitch` radians:
/// `Quat::from_rotation_z(-yaw) * orientation * Quat::from_rotation_x(-pitch)`.
/// Positive yaw turns counter-clockwise seen from above (Z-up), positive
/// pitch tilts up.
pub fn orbit(&mut self, yaw: f32, pitch: f32) {
self.orientation =
(Quat::from_rotation_z(-yaw) * self.orientation * Quat::from_rotation_x(-pitch))
.normalize();
}
/// Move the centre by world-space amounts along the view's right and up
/// axes. `right_delta` moves it left and `up_delta` up, so a drag to the
/// right moves the scene with the pointer.
pub fn pan_world(&mut self, right_delta: f32, up_delta: f32) {
self.centre -= (self.orientation * Vec3::X * right_delta).as_dvec3();
self.centre += (self.orientation * Vec3::Y * up_delta).as_dvec3();
}
/// Multiply the distance by `factor`, clamped to
/// `[Camera::MIN_DISTANCE, Camera::MAX_DISTANCE]`.
pub fn zoom_by_factor(&mut self, factor: f32) {
self.distance = (self.distance * factor).clamp(Camera::MIN_DISTANCE, Camera::MAX_DISTANCE);
}
/// Add `delta` to the distance, clamped to
/// `[Camera::MIN_DISTANCE, Camera::MAX_DISTANCE]`.
pub fn zoom_by_delta(&mut self, delta: f32) {
self.distance = (self.distance + delta).clamp(Camera::MIN_DISTANCE, Camera::MAX_DISTANCE);
}
}
/// The viewport camera: where it is, how it projects, and how it exposes.
///
/// [`pose`](Self::pose) places the eye in f64 world space and orients it.
/// [`projection`](Self::projection) maps view space to the screen, and
/// [`aspect`](Self::aspect) is the viewport's width over height, set each
/// frame from the viewport size. [`exposure`](Self::exposure) carries the
/// physical camera settings and the focus distance.
///
/// Build an orbit camera with [`Camera::orbit`] or a pointed one with
/// [`Camera::look_at`]. The camera itself does not orbit: orbit controllers
/// keep the centre and distance, and write the camera's pose. The focus
/// distance doubles as the orbit distance, so [`target`](Self::target) is the
/// orbit centre of a camera an orbit controller drives.
///
/// All matrices are right-handed (wgpu NDC convention).
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Camera {
/// Eye position and orientation.
pub pose: Pose,
/// Projection.
pub projection: Projection,
/// Physical exposure settings and focus distance. The renderer reads
/// exposure from `DisplaySettings::exposure`, not from here; set it
/// from [`PhysicalExposure::to_exposure_mode`].
pub exposure: PhysicalExposure,
/// Viewport width / height ratio, updated each frame from the viewport rect.
pub aspect: f32,
}
impl Default for Camera {
/// An orbit camera 5 units from the origin, about 27 degrees above the
/// x-y plane, with a 45 degree perspective.
fn default() -> Self {
// from_rotation_x(1.1) is ~63 deg from the top, ~27 deg above horizontal.
Self::orbit(
DVec3::ZERO,
5.0,
Quat::from_rotation_z(0.6) * Quat::from_rotation_x(1.1),
)
}
}
impl Camera {
/// Minimum orbit distance the zoom helpers clamp to.
pub const MIN_DISTANCE: f32 = 0.01;
/// Maximum orbit distance the zoom helpers clamp to.
pub const MAX_DISTANCE: f32 = 1.0e6;
// -----------------------------------------------------------------------
// Construction
// -----------------------------------------------------------------------
/// A camera with the given pose, the default perspective projection and
/// the default exposure.
pub fn new(pose: Pose) -> Self {
Self {
pose,
projection: Projection::default(),
exposure: PhysicalExposure::default(),
aspect: 1.5,
}
}
/// An orbit camera: the eye sits `distance` from `centre` along the
/// orientation's local `+Z` (`eye = centre + orientation * (Z * distance)`),
/// looking at `centre`, which is also the focus point.
pub fn orbit(centre: impl Into<DVec3>, distance: f32, orientation: Quat) -> Self {
let mut camera = Self::new(Pose::orbit(centre, distance, orientation));
camera.exposure.focus_distance = distance;
camera
}
/// A camera at `eye` looking at `target`, with `up` as close to the
/// screen's up as it can be. `target` is the focus point.
pub fn look_at(eye: impl Into<DVec3>, target: impl Into<DVec3>, up: Vec3) -> Self {
let (eye, target) = (eye.into(), target.into());
let mut camera = Self::new(Pose::look_at(eye, target, up));
camera.exposure.focus_distance = (target - eye).length() as f32;
camera
}
/// Builder: set the projection.
pub fn with_projection(mut self, projection: Projection) -> Self {
self.projection = projection;
self
}
/// Builder: set the aspect ratio (width / height).
pub fn with_aspect(mut self, aspect: f32) -> Self {
self.aspect = aspect;
self
}
/// Builder: switch to the orthographic projection that frames the same
/// height at the focus distance; see [`set_orthographic`](Self::set_orthographic).
pub fn orthographic(mut self) -> Self {
self.set_orthographic(true);
self
}
/// Builder: set the vertical field of view; see [`set_fov_y`](Self::set_fov_y).
pub fn with_fov_y(mut self, fov_y: f32) -> Self {
self.set_fov_y(fov_y);
self
}
/// Builder: set the exposure.
pub fn with_exposure(mut self, exposure: PhysicalExposure) -> Self {
self.exposure = exposure;
self
}
// -----------------------------------------------------------------------
// Pose
// -----------------------------------------------------------------------
/// Eye position in world space, in f64.
pub fn eye(&self) -> DVec3 {
self.pose.position
}
/// Camera orientation in f32.
pub fn orientation(&self) -> Quat {
self.pose.orientation_f32()
}
/// The direction the camera looks, in world space.
pub fn forward(&self) -> Vec3 {
self.pose.forward()
}
/// Camera right vector in world space.
pub fn right(&self) -> Vec3 {
self.pose.right()
}
/// Camera up vector in world space.
pub fn up(&self) -> Vec3 {
self.pose.up()
}
/// The point `distance` in front of the eye along the view direction.
pub fn target_at(&self, distance: f32) -> DVec3 {
self.pose.target_at(distance)
}
/// The focus point: [`focus_distance`](Self::focus_distance) in front of
/// the eye. For a camera an orbit controller drives, the orbit centre.
pub fn target(&self) -> DVec3 {
self.target_at(self.focus_distance())
}
/// Distance from the eye to the focus point.
pub fn focus_distance(&self) -> f32 {
self.exposure.focus_distance
}
/// Set the focus distance without moving the camera.
pub fn set_focus_distance(&mut self, distance: f32) {
self.exposure.focus_distance = distance;
}
/// Replace the pose. The projection is left alone, so an orthographic
/// height stays as it is.
pub fn set_pose(&mut self, pose: Pose) {
self.pose = pose;
}
/// The camera `t` of the way from `a` to `b`, with `t` clamped to
/// `0..=1`: `t = 0` returns `a` and `t = 1` returns `b` exactly.
///
/// The eye position is lerped and the orientation slerped, with no orbit
/// centre involved. The aspect and the exposure (focus distance
/// included) are lerped.
///
/// When both projections are orthographic the height is lerped, and when
/// both are perspective the field of view is; near and far planes are
/// lerped when both cameras set them, and otherwise taken from `a` below
/// `t = 0.5` and from `b` at or above it. When the projections differ in
/// kind, or either is a matrix projection, the result takes `a`'s
/// projection unchanged below `t = 0.5` and `b`'s at or above it: there
/// is no smooth blend between orthographic and perspective here, and a
/// host that wants one builds it itself.
pub fn lerp(a: &Camera, b: &Camera, t: f32) -> Camera {
let t = if t.is_nan() { 0.0 } else { t.clamp(0.0, 1.0) };
if t == 0.0 {
return a.clone();
}
if t == 1.0 {
return b.clone();
}
let mix = |x: f32, y: f32| x * (1.0 - t) + y * t;
let pick = |x: Option<f32>, y: Option<f32>| match (x, y) {
(Some(x), Some(y)) => Some(mix(x, y)),
_ if t < 0.5 => x,
_ => y,
};
let projection = match (a.projection, b.projection) {
(
Projection::Orthographic {
height: ha,
near: na,
far: fa,
},
Projection::Orthographic {
height: hb,
near: nb,
far: fb,
},
) => Projection::Orthographic {
height: mix(ha, hb),
near: pick(na, nb),
far: pick(fa, fb),
},
(
Projection::Perspective {
fov_y: va,
near: na,
far: fa,
},
Projection::Perspective {
fov_y: vb,
near: nb,
far: fb,
},
) => Projection::Perspective {
fov_y: mix(va, vb),
near: pick(na, nb),
far: pick(fa, fb),
},
(pa, pb) => {
if t < 0.5 {
pa
} else {
pb
}
}
};
let td = f64::from(t);
let pose = Pose::new(
a.pose.position * (1.0 - td) + b.pose.position * td,
a.pose.orientation.slerp(b.pose.orientation, td).normalize(),
);
let (ea, eb) = (&a.exposure, &b.exposure);
let exposure = PhysicalExposure {
aperture: mix(ea.aperture, eb.aperture),
shutter_seconds: mix(ea.shutter_seconds, eb.shutter_seconds),
iso: mix(ea.iso, eb.iso),
focus_distance: mix(ea.focus_distance, eb.focus_distance),
};
Camera {
pose,
projection,
exposure,
aspect: mix(a.aspect, b.aspect),
}
}
/// The camera's orbit view: its focus point, focus distance and
/// orientation.
pub fn orbit_target(&self) -> CameraTarget {
CameraTarget {
centre: self.target(),
distance: self.focus_distance(),
orientation: self.orientation(),
}
}
/// Place the camera on an orbit: the pose of [`Camera::orbit`], with the
/// focus distance set to `distance`. An orthographic view height scales
/// with the change in distance, so zooming frames the same way in both
/// projections.
pub fn set_orbit(&mut self, centre: impl Into<DVec3>, distance: f32, orientation: Quat) {
let previous = self.focus_distance();
if let Projection::Orthographic { height, .. } = &mut self.projection {
if previous > 0.0 && distance != previous {
*height *= distance / previous;
}
}
self.pose = Pose::orbit(centre, distance, orientation);
self.exposure.focus_distance = distance;
}
/// Move the orbit centre to `centre`, keeping the distance and
/// orientation. Goes through [`set_orbit`](Self::set_orbit), so an
/// orthographic height is rescaled if the focus distance differs from the
/// distance kept.
pub fn set_orbit_centre(&mut self, centre: impl Into<DVec3>) {
self.set_orbit(centre, self.focus_distance(), self.orientation());
}
/// Move the eye to `distance` from the orbit centre, keeping the centre
/// and orientation. The distance is clamped to
/// `[MIN_DISTANCE, MAX_DISTANCE]`. An orthographic height scales with the
/// change in distance, as in [`set_orbit`](Self::set_orbit).
pub fn set_orbit_distance(&mut self, distance: f32) {
let distance = distance.clamp(Self::MIN_DISTANCE, Self::MAX_DISTANCE);
self.set_orbit(self.target(), distance, self.orientation());
}
/// Turn the camera to `orientation` (normalised) about the orbit centre,
/// keeping the centre and distance. Goes through
/// [`set_orbit`](Self::set_orbit), so an orthographic height is rescaled
/// if the focus distance changes.
pub fn set_orbit_orientation(&mut self, orientation: Quat) {
self.set_orbit(
self.target(),
self.focus_distance(),
orientation.normalize(),
);
}
/// [`set_orbit`](Self::set_orbit) from a [`CameraTarget`]. An orthographic
/// height scales with the change in distance.
pub fn set_orbit_target(&mut self, target: CameraTarget) {
self.set_orbit(target.centre, target.distance, target.orientation);
}
// -----------------------------------------------------------------------
// Projection
// -----------------------------------------------------------------------
/// Vertical angle that frames the view at the focus distance: a
/// perspective camera's field of view, or the matching angle for an
/// orthographic one.
pub fn fov_y(&self) -> f32 {
self.projection.fov_y(self.focus_distance())
}
/// Set the vertical field of view in radians. An orthographic camera
/// takes the height that angle frames at the focus distance, so on an
/// orthographic camera this changes the zoom; use
/// [`set_ortho_height`](Self::set_ortho_height) to set it directly. A
/// matrix projection is left alone.
pub fn set_fov_y(&mut self, fov_y: f32) {
let distance = self.focus_distance();
match &mut self.projection {
Projection::Perspective { fov_y: f, .. } => *f = fov_y,
Projection::Orthographic { height, .. } => {
*height = 2.0 * (distance * (fov_y / 2.0).tan());
}
_ => {}
}
}
/// Whether the projection is orthographic.
pub fn is_orthographic(&self) -> bool {
self.projection.is_orthographic()
}
/// Set the orthographic view height in world units, leaving the pose and
/// focus distance alone. A perspective camera becomes orthographic with
/// that height, keeping its near and far planes; a matrix projection is
/// left alone.
pub fn set_ortho_height(&mut self, height: f32) {
match &mut self.projection {
Projection::Orthographic { height: h, .. } => *h = height,
Projection::Perspective { near, far, .. } => {
self.projection = Projection::Orthographic {
height,
near: *near,
far: *far,
};
}
Projection::Matrix { .. } => {}
}
}
/// Switch between perspective and orthographic, keeping the framing at
/// the focus distance. A matrix projection is left alone.
pub fn set_orthographic(&mut self, orthographic: bool) {
let distance = self.focus_distance();
self.projection = if orthographic {
self.projection.to_orthographic(distance)
} else {
self.projection.to_perspective(distance)
};
}
/// Set the aspect ratio from pixel dimensions. A zero or negative height
/// gives `1.0`.
pub fn set_aspect_ratio(&mut self, width: f32, height: f32) {
self.aspect = if height > 0.0 { width / height } else { 1.0 };
}
/// Set the near and far plane distances. On a matrix projection, sets the
/// culling range.
pub fn set_clip_planes(&mut self, znear: f32, zfar: f32) {
match &mut self.projection {
Projection::Perspective { near, far, .. }
| Projection::Orthographic { near, far, .. } => {
*near = Some(znear);
*far = Some(zfar);
}
Projection::Matrix { culling_range, .. } => *culling_range = (znear, zfar),
}
}
/// `(near, far)` distances the camera culls between; see
/// [`Projection::depth_range`].
pub fn depth_range(&self) -> (f32, f32) {
self.projection.depth_range(self.focus_distance())
}
/// World-space height of the view at view depth `depth`.
pub fn view_height_at(&self, depth: f32) -> f32 {
self.projection.view_height_at(depth)
}
// -----------------------------------------------------------------------
// Matrices
// -----------------------------------------------------------------------
/// Right-handed view matrix (world -> camera space), in f32 world
/// coordinates. Far from the world origin its translation loses
/// precision; the renderer rebuilds it from the f64 eye.
pub fn view_matrix(&self) -> Mat4 {
let orientation = self.orientation();
let distance = self.focus_distance();
let up = orientation * Vec3::Y;
if !(distance > 0.0 && distance.is_finite()) {
return Mat4::look_to_rh(self.eye().as_vec3(), self.forward(), up);
}
// Built looking from the eye at the focus point, both rounded to f32
// from the f64 focus point, so an orbit camera's matrix depends only
// on its centre, distance and orientation.
let centre = self.target().as_vec3();
let eye = centre + orientation * (Vec3::Z * distance);
Mat4::look_at_rh(eye, centre, up)
}
/// The projection matrix the renderer draws with; see
/// [`Projection::proj_matrix`].
pub fn proj_matrix(&self) -> Mat4 {
self.projection
.proj_matrix(self.aspect, self.focus_distance())
}
/// The finite culling projection; see [`Projection::culling_proj_matrix`].
pub fn culling_proj_matrix(&self) -> Mat4 {
self.projection
.culling_proj_matrix(self.aspect, self.focus_distance())
}
/// [`proj_matrix`](Self::proj_matrix) times the view matrix. Unproject
/// screen points with [`screen_ray`](Self::screen_ray) rather than through
/// its inverse at depth 0 and 1.
pub fn view_proj_matrix(&self) -> Mat4 {
self.proj_matrix() * self.view_matrix()
}
/// [`culling_proj_matrix`](Self::culling_proj_matrix) times the view matrix.
pub fn culling_view_proj_matrix(&self) -> Mat4 {
self.culling_proj_matrix() * self.view_matrix()
}
/// The view frustum of [`culling_view_proj_matrix`](Self::culling_view_proj_matrix)
/// in world space, built relative to the eye; see
/// [`Frustum::from_eye_relative`](crate::camera::frustum::Frustum::from_eye_relative).
pub fn frustum(&self) -> crate::camera::frustum::Frustum {
let rotation = glam::DMat3::from_quat(self.pose.orientation.normalize()).transpose();
let view = Mat4::from_mat3(rotation.as_mat3());
crate::camera::frustum::Frustum::from_eye_relative(
&(self.culling_proj_matrix() * view),
self.eye(),
)
}
/// The world-space ray through a point on screen, as `(origin, direction)`
/// with a unit direction.
///
/// `ndc` is the point in normalised device coordinates: -1..1 left to
/// right and bottom to top. A perspective ray starts at the eye; an
/// orthographic ray starts on the near plane and runs along
/// [`forward`](Self::forward).
pub fn screen_ray(&self, ndc: Vec2) -> (Vec3, Vec3) {
let forward = self.forward();
let right = self.right();
let up = self.up();
let eye = self.eye().as_vec3();
if self.is_orthographic() {
let half_h = self.view_height_at(1.0) * 0.5;
let half_w = half_h * self.aspect;
let origin = eye
+ right * (ndc.x * half_w)
+ up * (ndc.y * half_h)
+ forward * self.depth_range().0;
(origin, forward)
} else {
let tan_half = self.view_height_at(1.0) * 0.5;
let dir = forward + right * (ndc.x * tan_half * self.aspect) + up * (ndc.y * tan_half);
(eye, dir.normalize())
}
}
// -----------------------------------------------------------------------
// Framing helpers
// -----------------------------------------------------------------------
/// Frame a bounding sphere, keeping the orientation. Goes through
/// [`set_orbit`](Self::set_orbit), so an orthographic height scales with
/// the change in distance; set it afterwards with
/// [`set_ortho_height`](Self::set_ortho_height) to fix the zoom.
pub fn frame_sphere(&mut self, centre: impl Into<DVec3>, radius: f32) {
let (c, d) = self.fit_sphere(centre, radius);
self.set_orbit(
c,
d.clamp(Self::MIN_DISTANCE, Self::MAX_DISTANCE),
self.orientation(),
);
}
/// Frame an AABB, keeping the orientation. An orthographic height scales
/// with the change in distance, as in [`frame_sphere`](Self::frame_sphere).
pub fn frame_aabb(&mut self, aabb: &crate::scene::aabb::Aabb) {
let (c, d) = self.fit_aabb(aabb);
self.set_orbit(
c,
d.clamp(Self::MIN_DISTANCE, Self::MAX_DISTANCE),
self.orientation(),
);
}
/// The [`CameraTarget`] that frames a bounding sphere, keeping the
/// orientation.
pub fn fit_sphere_target(&self, centre: impl Into<DVec3>, radius: f32) -> CameraTarget {
let (c, d) = self.fit_sphere(centre, radius);
CameraTarget::new(c, d, self.orientation())
}
/// The [`CameraTarget`] that frames an AABB, keeping the orientation.
pub fn fit_aabb_target(&self, aabb: &crate::scene::aabb::Aabb) -> CameraTarget {
let (c, d) = self.fit_aabb(aabb);
CameraTarget::new(c, d, self.orientation())
}
/// `(centre, distance)` that frames a bounding sphere, keeping the
/// orientation. Pads by 1.2x so the object does not touch the edges.
pub fn fit_sphere(&self, centre: impl Into<DVec3>, radius: f32) -> (DVec3, f32) {
let distance = if self.is_orthographic() {
radius * 1.2
} else {
radius / (self.fov_y() / 2.0).tan() * 1.2
};
(centre.into(), distance)
}
/// `(centre, distance)` that frames an AABB, through its bounding sphere.
pub fn fit_aabb(&self, aabb: &crate::scene::aabb::Aabb) -> (DVec3, f32) {
self.fit_sphere(aabb.centre(), aabb.half_extents().length())
}
/// Centre the camera on a domain of the given extents, with the distance
/// and clipping planes set so all of it is visible. An orthographic height
/// scales with the change in distance, as in [`set_orbit`](Self::set_orbit).
pub fn centre_on_domain(&mut self, nx: f32, ny: f32, nz: f32) {
let centre = Vec3::new(nx, ny, nz).as_dvec3() / 2.0;
let diagonal = (nx * nx + ny * ny + nz * nz).sqrt();
let distance = (diagonal / 2.0) / (self.fov_y() / 2.0).tan() * 1.2;
self.set_orbit(centre, distance, self.orientation());
self.set_clip_planes((diagonal * 0.0001).max(0.01), (diagonal * 10.0).max(1000.0));
}
}
#[cfg(test)]
#[path = "camera_tests.rs"]
mod tests;