Skip to main content

mittens_engine/engine/ecs/component/
bloom.rs

1use crate::engine::ecs::component::Component;
2
3#[derive(Debug, Clone)]
4pub struct BloomComponent {
5    pub enabled: bool,
6    pub intensity: f32,
7    pub radius_ndc: f32,
8    pub emissive_scale: f32,
9    pub half_res: bool,
10    pub output_texture: Option<String>,
11}
12
13impl Default for BloomComponent {
14    fn default() -> Self {
15        Self::new()
16    }
17}
18
19impl BloomComponent {
20    pub fn new() -> Self {
21        let cfg = crate::engine::graphics::BloomConfig::default();
22        Self {
23            enabled: true,
24            intensity: cfg.intensity,
25            radius_ndc: cfg.radius_ndc,
26            emissive_scale: cfg.emissive_scale,
27            half_res: cfg.half_res,
28            output_texture: None,
29        }
30    }
31
32    pub fn with_enabled(mut self, enabled: bool) -> Self {
33        self.enabled = enabled;
34        self
35    }
36
37    pub fn with_intensity(mut self, intensity: f32) -> Self {
38        if intensity.is_finite() {
39            self.intensity = intensity.max(0.0);
40        }
41        self
42    }
43
44    pub fn with_radius_ndc(mut self, radius_ndc: f32) -> Self {
45        if radius_ndc.is_finite() {
46            self.radius_ndc = radius_ndc.max(0.0);
47        }
48        self
49    }
50
51    pub fn with_emissive_scale(mut self, emissive_scale: f32) -> Self {
52        if emissive_scale.is_finite() {
53            self.emissive_scale = emissive_scale.max(0.0);
54        }
55        self
56    }
57
58    pub fn with_half_res(mut self, half_res: bool) -> Self {
59        self.half_res = half_res;
60        self
61    }
62
63    pub fn with_output_texture(mut self, output_texture: impl Into<String>) -> Self {
64        self.output_texture = Some(output_texture.into());
65        self
66    }
67}
68
69impl Component for BloomComponent {
70    fn name(&self) -> &'static str {
71        "bloom"
72    }
73
74    fn as_any(&self) -> &dyn std::any::Any {
75        self
76    }
77
78    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
79        self
80    }
81
82    fn to_mms_ast(
83        &self,
84        _world: &crate::engine::ecs::World,
85    ) -> crate::scripting::ast::ComponentExpression {
86        use crate::engine::ecs::component::ce_helpers::*;
87        let mut ce = ce("Bloom")
88            .with_call("enabled", vec![b(self.enabled)])
89            .with_call("intensity", vec![num(self.intensity as f64)])
90            .with_call("radius_ndc", vec![num(self.radius_ndc as f64)])
91            .with_call("emissive_scale", vec![num(self.emissive_scale as f64)])
92            .with_call("half_res", vec![b(self.half_res)]);
93        if let Some(tex) = &self.output_texture {
94            ce = ce.with_call("output_texture", vec![s(tex)]);
95        }
96        ce
97    }
98}