1use super::{Color, Mesh3dStyleError, SurfaceLighting3d};
4
5#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum SurfaceAlphaMode3d {
9 #[default]
11 Opaque,
12 Mask,
14 Blend,
16}
17
18#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
20#[non_exhaustive]
21pub enum SurfaceSidedness3d {
22 #[default]
24 TwoSided,
25 FrontOnly,
28}
29
30#[derive(Debug, Clone, Copy, PartialEq)]
40pub struct SurfaceStyle3d {
41 color: Color,
42 alpha_mode: SurfaceAlphaMode3d,
43 mask_cutoff: f32,
44 sidedness: SurfaceSidedness3d,
45 lighting: SurfaceLighting3d,
46 fog: bool,
47}
48
49impl SurfaceStyle3d {
50 pub fn opaque(color: Color) -> Result<Self, Mesh3dStyleError> {
53 if !color.is_normalized() || color.alpha() != 1.0 {
54 return Err(Mesh3dStyleError::InvalidSurfaceColor);
55 }
56 Ok(Self {
57 color,
58 alpha_mode: SurfaceAlphaMode3d::Opaque,
59 mask_cutoff: 0.0,
60 sidedness: SurfaceSidedness3d::TwoSided,
61 lighting: SurfaceLighting3d::Unlit,
62 fog: false,
63 })
64 }
65
66 pub fn mask(color: Color, cutoff: f32) -> Result<Self, Mesh3dStyleError> {
71 if !color.is_normalized() {
72 return Err(Mesh3dStyleError::InvalidMaterialColor);
73 }
74 if !cutoff.is_finite()
75 || !(0.0..=1.0).contains(&cutoff)
76 || (cutoff != 0.0 && !cutoff.is_normal())
77 {
78 return Err(Mesh3dStyleError::InvalidMaskCutoff);
79 }
80 Ok(Self {
81 color,
82 alpha_mode: SurfaceAlphaMode3d::Mask,
83 mask_cutoff: cutoff,
84 sidedness: SurfaceSidedness3d::TwoSided,
85 lighting: SurfaceLighting3d::Unlit,
86 fog: false,
87 })
88 }
89
90 pub fn blend(color: Color) -> Result<Self, Mesh3dStyleError> {
93 if !color.is_normalized() {
94 return Err(Mesh3dStyleError::InvalidMaterialColor);
95 }
96 Ok(Self {
97 color,
98 alpha_mode: SurfaceAlphaMode3d::Blend,
99 mask_cutoff: 0.0,
100 sidedness: SurfaceSidedness3d::TwoSided,
101 lighting: SurfaceLighting3d::Unlit,
102 fog: false,
103 })
104 }
105
106 pub const fn with_sidedness(mut self, sidedness: SurfaceSidedness3d) -> Self {
108 self.sidedness = sidedness;
109 self
110 }
111 pub const fn with_lighting(mut self, lighting: SurfaceLighting3d) -> Self {
114 self.lighting = lighting;
115 self
116 }
117 pub const fn lighting(self) -> SurfaceLighting3d {
119 self.lighting
120 }
121 pub const fn with_fog(mut self, enabled: bool) -> Self {
123 self.fog = enabled;
124 self
125 }
126 pub const fn fog_enabled(self) -> bool {
128 self.fog
129 }
130 pub const fn color(self) -> Color {
132 self.color
133 }
134 pub const fn alpha_mode(self) -> SurfaceAlphaMode3d {
136 self.alpha_mode
137 }
138 pub const fn mask_cutoff(self) -> Option<f32> {
140 match self.alpha_mode {
141 SurfaceAlphaMode3d::Mask => Some(self.mask_cutoff),
142 _ => None,
143 }
144 }
145 pub const fn sidedness(self) -> SurfaceSidedness3d {
147 self.sidedness
148 }
149}
150
151#[cfg(test)]
152mod tests {
153 use super::*;
154 #[test]
155 fn explicit_surface_alpha_preserves_opaque_validation_and_cutoff_endpoints() {
156 assert_eq!(
157 SurfaceStyle3d::opaque(Color::rgba(1.0, 0.0, 0.0, 0.5)),
158 Err(Mesh3dStyleError::InvalidSurfaceColor)
159 );
160 for alpha in [0.0, 0.5, 1.0] {
161 let color = Color::rgba(0.25, 0.5, 1.0, alpha);
162 assert_eq!(
163 SurfaceStyle3d::blend(color).unwrap().alpha_mode(),
164 SurfaceAlphaMode3d::Blend
165 );
166 for cutoff in [0.0, 0.5, 1.0] {
167 let surface = SurfaceStyle3d::mask(color, cutoff).unwrap();
168 assert_eq!(surface.mask_cutoff(), Some(cutoff));
169 assert_eq!(surface.sidedness(), SurfaceSidedness3d::TwoSided);
170 assert_eq!(
171 surface
172 .with_sidedness(SurfaceSidedness3d::FrontOnly)
173 .color(),
174 color
175 );
176 }
177 }
178 for cutoff in [f32::NAN, f32::INFINITY, f32::from_bits(1), -0.1, 1.1] {
179 assert_eq!(
180 SurfaceStyle3d::mask(Color::WHITE, cutoff),
181 Err(Mesh3dStyleError::InvalidMaskCutoff)
182 );
183 }
184 for color in [
185 Color::rgba(f32::NAN, 0.0, 0.0, 1.0),
186 Color::rgba(1.1, 0.0, 0.0, 0.5),
187 ] {
188 assert_eq!(
189 SurfaceStyle3d::blend(color),
190 Err(Mesh3dStyleError::InvalidMaterialColor)
191 );
192 assert_eq!(
193 SurfaceStyle3d::mask(color, 0.5),
194 Err(Mesh3dStyleError::InvalidMaterialColor)
195 );
196 }
197 }
198}