rafx_plugins/
components.rs

1use glam::f32::Vec3;
2use glam::Quat;
3use rafx::framework::visibility::VisibilityObjectArc;
4use rafx::render_features::RenderObjectHandle;
5use rafx::visibility::ViewFrustumArc;
6
7#[derive(Clone)]
8pub struct SpriteComponent {
9    pub render_object_handle: RenderObjectHandle,
10}
11
12#[derive(Clone)]
13pub struct MeshComponent {
14    pub render_object_handle: RenderObjectHandle,
15}
16
17#[derive(Clone)]
18pub struct VisibilityComponent {
19    pub visibility_object_handle: VisibilityObjectArc,
20}
21
22#[derive(Clone, Copy)]
23pub struct TransformComponent {
24    pub translation: Vec3,
25    pub rotation: Quat,
26    pub scale: Vec3,
27}
28
29impl Default for TransformComponent {
30    fn default() -> Self {
31        Self {
32            translation: Vec3::ZERO,
33            rotation: Quat::IDENTITY,
34            scale: Vec3::ONE,
35        }
36    }
37}
38
39impl TransformComponent {
40    pub fn rotate(
41        &mut self,
42        rotation: Quat,
43    ) {
44        self.rotation *= rotation;
45    }
46
47    pub fn mul_transform(
48        &self,
49        transform: TransformComponent,
50    ) -> Self {
51        let translation = self.mul_vec3(transform.translation);
52        let rotation = self.rotation * transform.rotation;
53        let scale = self.scale * transform.scale;
54        TransformComponent {
55            translation,
56            rotation,
57            scale,
58        }
59    }
60
61    pub fn mul_vec3(
62        &self,
63        mut value: Vec3,
64    ) -> Vec3 {
65        value = self.rotation * value;
66        value = self.scale * value;
67        value += self.translation;
68        value
69    }
70}
71
72const LIGHT_MIN_INTENSITY_TO_RENDER: f32 = 0.2;
73
74#[derive(Clone)]
75pub struct PointLightComponent {
76    pub color: glam::Vec4,
77    pub range: Option<f32>,
78    pub intensity: f32,
79    pub shadow_view_frustums: Option<[ViewFrustumArc; 6]>,
80}
81
82impl PointLightComponent {
83    pub fn range_sq_from_intensity(intensity: f32) -> f32 {
84        intensity / LIGHT_MIN_INTENSITY_TO_RENDER
85    }
86
87    pub fn range(&self) -> f32 {
88        if let Some(range) = self.range {
89            range
90        } else {
91            Self::range_sq_from_intensity(self.intensity).sqrt()
92        }
93    }
94
95    pub fn range_sq(&self) -> f32 {
96        if let Some(range) = self.range {
97            range * range
98        } else {
99            Self::range_sq_from_intensity(self.intensity)
100        }
101    }
102}
103
104#[derive(Clone)]
105pub struct DirectionalLightComponent {
106    pub direction: glam::Vec3,
107    pub color: glam::Vec4,
108    pub intensity: f32,
109    pub shadow_view_frustum: Option<ViewFrustumArc>,
110}
111
112#[derive(Clone)]
113pub struct SpotLightComponent {
114    pub direction: glam::Vec3,
115    pub color: glam::Vec4,
116    pub spotlight_half_angle: f32,
117    pub range: Option<f32>,
118    pub intensity: f32,
119    pub shadow_view_frustum: Option<ViewFrustumArc>,
120}
121
122impl SpotLightComponent {
123    pub fn range_sq_from_intensity(intensity: f32) -> f32 {
124        intensity / LIGHT_MIN_INTENSITY_TO_RENDER
125    }
126
127    pub fn range(&self) -> f32 {
128        if let Some(range) = self.range {
129            range
130        } else {
131            Self::range_sq_from_intensity(self.intensity).sqrt()
132        }
133    }
134
135    pub fn range_sq(&self) -> f32 {
136        if let Some(range) = self.range {
137            range * range
138        } else {
139            Self::range_sq_from_intensity(self.intensity)
140        }
141    }
142}