use std::{error::Error, fmt};
use bevy_ecs::prelude::Resource;
use sim_engine::{
Camera3d, Color, Fog3d, Lighting3d, LogicalViewport, Mesh3dStyleError, Projection3d,
Pseudo3dError, SurfaceStyle3d, UnitError, Vec3, WorldLength,
};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum ThreeDSurfacePolicy {
#[default]
StrictPortable,
Native,
}
#[derive(Debug, Clone, Copy, PartialEq, Resource)]
pub struct View3d {
position: Vec3,
target: Vec3,
vertical_fov_radians: f32,
near: WorldLength,
far: WorldLength,
background: Color,
orthographic_span: Option<WorldLength>,
surface_policy: ThreeDSurfacePolicy,
lighting: Lighting3d,
fog: Option<Fog3d>,
enabled: bool,
}
impl View3d {
pub fn new(position: Vec3, target: Vec3) -> Result<Self, View3dError> {
let view = Self {
position,
target,
vertical_fov_radians: std::f32::consts::FRAC_PI_3,
near: WorldLength::new(0.1)?,
far: WorldLength::new(1000.0)?,
background: Color::BLACK,
orthographic_span: None,
surface_policy: ThreeDSurfacePolicy::StrictPortable,
lighting: Lighting3d::default(),
fog: None,
enabled: true,
};
view.validate()?;
Ok(view)
}
pub const fn position(self) -> Vec3 {
self.position
}
pub const fn target(self) -> Vec3 {
self.target
}
pub const fn vertical_fov_radians(self) -> f32 {
self.vertical_fov_radians
}
pub const fn near(self) -> WorldLength {
self.near
}
pub const fn far(self) -> WorldLength {
self.far
}
pub const fn background(self) -> Color {
self.background
}
pub const fn enabled(self) -> bool {
self.enabled
}
pub const fn orthographic_span(self) -> Option<WorldLength> {
self.orthographic_span
}
pub const fn surface_policy(self) -> ThreeDSurfacePolicy {
self.surface_policy
}
pub fn set_surface_policy(&mut self, policy: ThreeDSurfacePolicy) {
self.surface_policy = policy;
}
pub const fn lighting(self) -> Lighting3d {
self.lighting
}
pub fn set_lighting(&mut self, lighting: Lighting3d) {
self.lighting = lighting;
}
pub const fn fog(self) -> Option<Fog3d> {
self.fog
}
pub fn set_fog(&mut self, fog: Option<Fog3d>) {
self.fog = fog;
}
pub fn set_pose(&mut self, position: Vec3, target: Vec3) -> Result<(), View3dError> {
let proposed = Self {
position,
target,
..*self
};
proposed.validate()?;
*self = proposed;
Ok(())
}
pub fn set_perspective(
&mut self,
vertical_fov_radians: f32,
near: WorldLength,
far: WorldLength,
) -> Result<(), View3dError> {
let proposed = Self {
vertical_fov_radians,
near,
far,
orthographic_span: None,
..*self
};
proposed.validate()?;
*self = proposed;
Ok(())
}
pub fn set_orthographic(
&mut self,
vertical_span: WorldLength,
near: WorldLength,
far: WorldLength,
) -> Result<(), View3dError> {
let proposed = Self {
orthographic_span: Some(vertical_span),
near,
far,
..*self
};
proposed.validate()?;
*self = proposed;
Ok(())
}
pub fn set_background(&mut self, background: Color) -> Result<(), View3dError> {
SurfaceStyle3d::blend(background)?;
self.background = background;
Ok(())
}
pub fn set_enabled(&mut self, enabled: bool) {
self.enabled = enabled;
}
pub fn camera(self, viewport: LogicalViewport) -> Result<Camera3d, View3dError> {
self.camera_with_aspect(viewport.width() / viewport.height())
}
pub(crate) fn validate(self) -> Result<(), View3dError> {
SurfaceStyle3d::blend(self.background)?;
self.camera_with_aspect(1.0)?;
Ok(())
}
fn camera_with_aspect(self, aspect: f32) -> Result<Camera3d, View3dError> {
let projection = match self.orthographic_span {
Some(span) => Projection3d::orthographic(span, aspect, self.near, self.far)?,
None => {
Projection3d::perspective(self.vertical_fov_radians, aspect, self.near, self.far)?
}
};
Ok(Camera3d::look_at(
self.position,
self.target,
Vec3::Y,
projection,
)?)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum View3dError {
Geometry(Pseudo3dError),
Units(UnitError),
Background(Mesh3dStyleError),
}
impl From<Pseudo3dError> for View3dError {
fn from(error: Pseudo3dError) -> Self {
Self::Geometry(error)
}
}
impl From<UnitError> for View3dError {
fn from(error: UnitError) -> Self {
Self::Units(error)
}
}
impl From<Mesh3dStyleError> for View3dError {
fn from(error: Mesh3dStyleError) -> Self {
Self::Background(error)
}
}
impl fmt::Display for View3dError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Geometry(error) => write!(formatter, "invalid 3D view: {error}"),
Self::Units(error) => write!(formatter, "invalid 3D view units: {error}"),
Self::Background(error) => write!(formatter, "invalid 3D background: {error}"),
}
}
}
impl Error for View3dError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Geometry(error) => Some(error),
Self::Units(error) => Some(error),
Self::Background(error) => Some(error),
}
}
}