Skip to main content

scenix_material/
wireframe.rs

1use scenix_core::Color;
2
3use crate::traits::double_sided_bit;
4use crate::{FEATURE_WIREFRAME, Material, PipelineAlphaMode, PipelineKey, ShaderKind};
5
6/// Wireframe overlay material.
7#[derive(Clone, Copy, Debug, PartialEq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct WireframeMaterial {
10    /// Wire color in linear RGBA.
11    pub color: Color,
12    /// Wire opacity in `0.0..=1.0`.
13    pub opacity: f32,
14    /// Wire width in logical pixels.
15    pub line_width: f32,
16    /// Whether the material is rendered double-sided.
17    pub double_sided: bool,
18}
19
20impl WireframeMaterial {
21    /// Creates a default black wireframe material.
22    #[inline]
23    pub const fn new() -> Self {
24        Self {
25            color: Color::BLACK,
26            opacity: 1.0,
27            line_width: 1.0,
28            double_sided: true,
29        }
30    }
31}
32
33impl Default for WireframeMaterial {
34    #[inline]
35    fn default() -> Self {
36        Self::new()
37    }
38}
39
40impl Material for WireframeMaterial {
41    #[inline]
42    fn pipeline_key(&self) -> PipelineKey {
43        let alpha = if self.opacity < 1.0 || self.color.a < 1.0 {
44            PipelineAlphaMode::Blend
45        } else {
46            PipelineAlphaMode::Opaque
47        };
48        PipelineKey::new(
49            ShaderKind::Wireframe,
50            alpha,
51            double_sided_bit(self.double_sided) | FEATURE_WIREFRAME,
52        )
53    }
54
55    #[inline]
56    fn is_transparent(&self) -> bool {
57        self.opacity < 1.0 || self.color.a < 1.0
58    }
59
60    #[inline]
61    fn double_sided(&self) -> bool {
62        self.double_sided
63    }
64
65    #[inline]
66    fn alpha_cutoff(&self) -> Option<f32> {
67        None
68    }
69}