use super::{Color, Mesh3dStyleError, SurfaceLighting3d};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SurfaceAlphaMode3d {
#[default]
Opaque,
Mask,
Blend,
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SurfaceSidedness3d {
#[default]
TwoSided,
FrontOnly,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SurfaceStyle3d {
color: Color,
alpha_mode: SurfaceAlphaMode3d,
mask_cutoff: f32,
sidedness: SurfaceSidedness3d,
lighting: SurfaceLighting3d,
fog: bool,
}
impl SurfaceStyle3d {
pub fn opaque(color: Color) -> Result<Self, Mesh3dStyleError> {
if !color.is_normalized() || color.alpha() != 1.0 {
return Err(Mesh3dStyleError::InvalidSurfaceColor);
}
Ok(Self {
color,
alpha_mode: SurfaceAlphaMode3d::Opaque,
mask_cutoff: 0.0,
sidedness: SurfaceSidedness3d::TwoSided,
lighting: SurfaceLighting3d::Unlit,
fog: false,
})
}
pub fn mask(color: Color, cutoff: f32) -> Result<Self, Mesh3dStyleError> {
if !color.is_normalized() {
return Err(Mesh3dStyleError::InvalidMaterialColor);
}
if !cutoff.is_finite()
|| !(0.0..=1.0).contains(&cutoff)
|| (cutoff != 0.0 && !cutoff.is_normal())
{
return Err(Mesh3dStyleError::InvalidMaskCutoff);
}
Ok(Self {
color,
alpha_mode: SurfaceAlphaMode3d::Mask,
mask_cutoff: cutoff,
sidedness: SurfaceSidedness3d::TwoSided,
lighting: SurfaceLighting3d::Unlit,
fog: false,
})
}
pub fn blend(color: Color) -> Result<Self, Mesh3dStyleError> {
if !color.is_normalized() {
return Err(Mesh3dStyleError::InvalidMaterialColor);
}
Ok(Self {
color,
alpha_mode: SurfaceAlphaMode3d::Blend,
mask_cutoff: 0.0,
sidedness: SurfaceSidedness3d::TwoSided,
lighting: SurfaceLighting3d::Unlit,
fog: false,
})
}
pub const fn with_sidedness(mut self, sidedness: SurfaceSidedness3d) -> Self {
self.sidedness = sidedness;
self
}
pub const fn with_lighting(mut self, lighting: SurfaceLighting3d) -> Self {
self.lighting = lighting;
self
}
pub const fn lighting(self) -> SurfaceLighting3d {
self.lighting
}
pub const fn with_fog(mut self, enabled: bool) -> Self {
self.fog = enabled;
self
}
pub const fn fog_enabled(self) -> bool {
self.fog
}
pub const fn color(self) -> Color {
self.color
}
pub const fn alpha_mode(self) -> SurfaceAlphaMode3d {
self.alpha_mode
}
pub const fn mask_cutoff(self) -> Option<f32> {
match self.alpha_mode {
SurfaceAlphaMode3d::Mask => Some(self.mask_cutoff),
_ => None,
}
}
pub const fn sidedness(self) -> SurfaceSidedness3d {
self.sidedness
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn explicit_surface_alpha_preserves_opaque_validation_and_cutoff_endpoints() {
assert_eq!(
SurfaceStyle3d::opaque(Color::rgba(1.0, 0.0, 0.0, 0.5)),
Err(Mesh3dStyleError::InvalidSurfaceColor)
);
for alpha in [0.0, 0.5, 1.0] {
let color = Color::rgba(0.25, 0.5, 1.0, alpha);
assert_eq!(
SurfaceStyle3d::blend(color).unwrap().alpha_mode(),
SurfaceAlphaMode3d::Blend
);
for cutoff in [0.0, 0.5, 1.0] {
let surface = SurfaceStyle3d::mask(color, cutoff).unwrap();
assert_eq!(surface.mask_cutoff(), Some(cutoff));
assert_eq!(surface.sidedness(), SurfaceSidedness3d::TwoSided);
assert_eq!(
surface
.with_sidedness(SurfaceSidedness3d::FrontOnly)
.color(),
color
);
}
}
for cutoff in [f32::NAN, f32::INFINITY, f32::from_bits(1), -0.1, 1.1] {
assert_eq!(
SurfaceStyle3d::mask(Color::WHITE, cutoff),
Err(Mesh3dStyleError::InvalidMaskCutoff)
);
}
for color in [
Color::rgba(f32::NAN, 0.0, 0.0, 1.0),
Color::rgba(1.1, 0.0, 0.0, 0.5),
] {
assert_eq!(
SurfaceStyle3d::blend(color),
Err(Mesh3dStyleError::InvalidMaterialColor)
);
assert_eq!(
SurfaceStyle3d::mask(color, 0.5),
Err(Mesh3dStyleError::InvalidMaterialColor)
);
}
}
}