three_d/core/render_target/
clear_state.rs

1use crate::core::*;
2
3///
4/// Defines which channels (red, green, blue, alpha and depth) to clear when starting to write to a [RenderTarget].
5/// If `None` then the channel is not cleared and if `Some(value)` the channel is cleared to that value (the value must be between 0 and 1).
6///
7#[derive(Debug, Copy, Clone, PartialEq)]
8pub struct ClearState {
9    /// Defines the clear value for the red channel.
10    pub red: Option<f32>,
11    /// Defines the clear value for the green channel.
12    pub green: Option<f32>,
13    /// Defines the clear value for the blue channel.
14    pub blue: Option<f32>,
15    /// Defines the clear value for the alpha channel.
16    pub alpha: Option<f32>,
17    /// Defines the clear value for the depth channel. A value of 1 means a depth value equal to the far plane and 0 means a depth value equal to the near plane.
18    pub depth: Option<f32>,
19}
20
21impl ClearState {
22    ///
23    /// Nothing will be cleared.
24    ///
25    pub const fn none() -> Self {
26        Self {
27            red: None,
28            green: None,
29            blue: None,
30            alpha: None,
31            depth: None,
32        }
33    }
34
35    ///
36    /// The depth will be cleared to the given value.
37    ///
38    pub const fn depth(depth: f32) -> Self {
39        Self {
40            red: None,
41            green: None,
42            blue: None,
43            alpha: None,
44            depth: Some(depth),
45        }
46    }
47
48    ///
49    /// The color channels (red, green, blue and alpha) will be cleared to the given values.
50    ///
51    pub const fn color(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
52        Self {
53            red: Some(red),
54            green: Some(green),
55            blue: Some(blue),
56            alpha: Some(alpha),
57            depth: None,
58        }
59    }
60
61    ///
62    /// Both the color channels (red, green, blue and alpha) and depth will be cleared to the given values.
63    ///
64    pub const fn color_and_depth(red: f32, green: f32, blue: f32, alpha: f32, depth: f32) -> Self {
65        Self {
66            red: Some(red),
67            green: Some(green),
68            blue: Some(blue),
69            alpha: Some(alpha),
70            depth: Some(depth),
71        }
72    }
73
74    pub(in crate::core) fn apply(&self, context: &Context) {
75        context.set_write_mask(WriteMask {
76            red: self.red.is_some(),
77            green: self.green.is_some(),
78            blue: self.blue.is_some(),
79            alpha: self.alpha.is_some(),
80            depth: self.depth.is_some(),
81        });
82        unsafe {
83            let clear_color = self.red.is_some()
84                || self.green.is_some()
85                || self.blue.is_some()
86                || self.alpha.is_some();
87            if clear_color {
88                context.clear_color(
89                    self.red.unwrap_or(0.0),
90                    self.green.unwrap_or(0.0),
91                    self.blue.unwrap_or(0.0),
92                    self.alpha.unwrap_or(1.0),
93                );
94            }
95            if let Some(depth) = self.depth {
96                context.clear_depth_f32(depth);
97            }
98            context.clear(if clear_color && self.depth.is_some() {
99                crate::context::COLOR_BUFFER_BIT | crate::context::DEPTH_BUFFER_BIT
100            } else if clear_color {
101                crate::context::COLOR_BUFFER_BIT
102            } else {
103                crate::context::DEPTH_BUFFER_BIT
104            });
105        }
106    }
107}
108
109impl Default for ClearState {
110    fn default() -> Self {
111        Self::color_and_depth(0.0, 0.0, 0.0, 1.0, 1.0)
112    }
113}