use glam::{DVec3, Mat4, Quat, Vec2, Vec3};
use super::exposure::PhysicalExposure;
use super::pose::Pose;
use super::projection::Projection;
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CameraTarget {
#[cfg_attr(feature = "serde", serde(alias = "center"))]
pub centre: DVec3,
pub distance: f32,
pub orientation: Quat,
}
impl CameraTarget {
pub fn new(centre: impl Into<DVec3>, distance: f32, orientation: Quat) -> Self {
Self {
centre: centre.into(),
distance,
orientation,
}
}
pub fn pose(&self) -> Pose {
Pose::orbit(self.centre, self.distance, self.orientation)
}
pub fn eye(&self) -> DVec3 {
self.pose().position
}
pub fn orbit(&mut self, yaw: f32, pitch: f32) {
self.orientation =
(Quat::from_rotation_z(-yaw) * self.orientation * Quat::from_rotation_x(-pitch))
.normalize();
}
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();
}
pub fn zoom_by_factor(&mut self, factor: f32) {
self.distance = (self.distance * factor).clamp(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);
}
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Camera {
pub pose: Pose,
pub projection: Projection,
pub exposure: PhysicalExposure,
pub aspect: f32,
}
impl Default for Camera {
fn default() -> Self {
Self::orbit(
DVec3::ZERO,
5.0,
Quat::from_rotation_z(0.6) * Quat::from_rotation_x(1.1),
)
}
}
impl Camera {
pub const MIN_DISTANCE: f32 = 0.01;
pub const MAX_DISTANCE: f32 = 1.0e6;
pub fn new(pose: Pose) -> Self {
Self {
pose,
projection: Projection::default(),
exposure: PhysicalExposure::default(),
aspect: 1.5,
}
}
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
}
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
}
pub fn with_projection(mut self, projection: Projection) -> Self {
self.projection = projection;
self
}
pub fn with_aspect(mut self, aspect: f32) -> Self {
self.aspect = aspect;
self
}
pub fn orthographic(mut self) -> Self {
self.set_orthographic(true);
self
}
pub fn with_fov_y(mut self, fov_y: f32) -> Self {
self.set_fov_y(fov_y);
self
}
pub fn with_exposure(mut self, exposure: PhysicalExposure) -> Self {
self.exposure = exposure;
self
}
pub fn eye(&self) -> DVec3 {
self.pose.position
}
pub fn orientation(&self) -> Quat {
self.pose.orientation_f32()
}
pub fn forward(&self) -> Vec3 {
self.pose.forward()
}
pub fn right(&self) -> Vec3 {
self.pose.right()
}
pub fn up(&self) -> Vec3 {
self.pose.up()
}
pub fn target_at(&self, distance: f32) -> DVec3 {
self.pose.target_at(distance)
}
pub fn target(&self) -> DVec3 {
self.target_at(self.focus_distance())
}
pub fn focus_distance(&self) -> f32 {
self.exposure.focus_distance
}
pub fn set_focus_distance(&mut self, distance: f32) {
self.exposure.focus_distance = distance;
}
pub fn set_pose(&mut self, pose: Pose) {
self.pose = pose;
}
pub fn orbit_target(&self) -> CameraTarget {
CameraTarget {
centre: self.target(),
distance: self.focus_distance(),
orientation: self.orientation(),
}
}
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;
}
pub fn set_orbit_centre(&mut self, centre: impl Into<DVec3>) {
self.set_orbit(centre, self.focus_distance(), self.orientation());
}
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());
}
pub fn set_orbit_orientation(&mut self, orientation: Quat) {
self.set_orbit(
self.target(),
self.focus_distance(),
orientation.normalize(),
);
}
pub fn set_orbit_target(&mut self, target: CameraTarget) {
self.set_orbit(target.centre, target.distance, target.orientation);
}
pub fn fov_y(&self) -> f32 {
self.projection.fov_y(self.focus_distance())
}
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());
}
_ => {}
}
}
pub fn is_orthographic(&self) -> bool {
self.projection.is_orthographic()
}
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)
};
}
pub fn set_aspect_ratio(&mut self, width: f32, height: f32) {
self.aspect = if height > 0.0 { width / height } else { 1.0 };
}
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),
}
}
pub fn depth_range(&self) -> (f32, f32) {
self.projection.depth_range(self.focus_distance())
}
pub fn view_height_at(&self, depth: f32) -> f32 {
self.projection.view_height_at(depth)
}
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);
}
let centre = self.target().as_vec3();
let eye = centre + orientation * (Vec3::Z * distance);
Mat4::look_at_rh(eye, centre, up)
}
pub fn proj_matrix(&self) -> Mat4 {
self.projection
.proj_matrix(self.aspect, self.focus_distance())
}
pub fn culling_proj_matrix(&self) -> Mat4 {
self.projection
.culling_proj_matrix(self.aspect, self.focus_distance())
}
pub fn view_proj_matrix(&self) -> Mat4 {
self.proj_matrix() * self.view_matrix()
}
pub fn culling_view_proj_matrix(&self) -> Mat4 {
self.culling_proj_matrix() * self.view_matrix()
}
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(),
)
}
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())
}
}
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(),
);
}
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(),
);
}
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())
}
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())
}
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)
}
pub fn fit_aabb(&self, aabb: &crate::scene::aabb::Aabb) -> (DVec3, f32) {
self.fit_sphere(aabb.centre(), aabb.half_extents().length())
}
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;