Skip to main content

mittens_engine/engine/ecs/component/
directional_light.rs

1use super::Component;
2use crate::engine::ecs::ComponentId;
3
4/// Directional light (infinite distance light).
5///
6/// The renderer interprets this light's *world position* as a direction vector.
7/// In other words: set the node's translation to the direction you want (it will
8/// be normalized on the GPU).
9#[derive(Debug, Clone, Copy)]
10pub struct DirectionalLightComponent {
11    pub intensity: f32,
12    /// Linear RGB color in 0..1.
13    pub color: [f32; 3],
14
15    component: Option<ComponentId>,
16}
17
18impl DirectionalLightComponent {
19    pub fn new() -> Self {
20        Self {
21            intensity: 1.0,
22            color: [1.0, 1.0, 1.0],
23            component: None,
24        }
25    }
26
27    pub fn with_intensity(mut self, intensity: f32) -> Self {
28        self.intensity = intensity;
29        self
30    }
31
32    pub fn with_color(mut self, r: f32, g: f32, b: f32) -> Self {
33        self.color = [r, g, b];
34        self
35    }
36
37    pub fn id(&self) -> Option<ComponentId> {
38        self.component
39    }
40}
41
42impl Default for DirectionalLightComponent {
43    fn default() -> Self {
44        Self::new()
45    }
46}
47
48impl Component for DirectionalLightComponent {
49    fn set_id(&mut self, component: ComponentId) {
50        self.component = Some(component);
51    }
52
53    fn name(&self) -> &'static str {
54        "directional_light"
55    }
56
57    fn init(
58        &mut self,
59        emit: &mut dyn crate::engine::ecs::SignalEmitter,
60        component: crate::engine::ecs::ComponentId,
61    ) {
62        // Uses the same light registration path as point lights.
63        emit.push_intent_now(
64            component,
65            crate::engine::ecs::IntentValue::RegisterLight {
66                component_ids: vec![component],
67            },
68        );
69    }
70
71    fn as_any(&self) -> &dyn std::any::Any {
72        self
73    }
74
75    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
76        self
77    }
78
79    fn to_mms_ast(
80        &self,
81        _world: &crate::engine::ecs::World,
82    ) -> crate::scripting::ast::ComponentExpression {
83        use crate::engine::ecs::component::ce_helpers::*;
84        ce("DirectionalLight")
85            .with_call("intensity", vec![num(self.intensity as f64)])
86            .with_call("color", nums(self.color.iter().map(|&v| v as f64)))
87    }
88}