1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//! A module adding debug visualization of [`PointLight`]s, [`SpotLight`]s and [`DirectionalLight`]s.

use std::f32::consts::PI;

use crate::{self as bevy_gizmos, primitives::dim3::GizmoPrimitive3d};

use bevy_app::{Plugin, PostUpdate};
use bevy_color::{
    palettes::basic::{BLUE, GREEN, RED},
    Color, Oklcha,
};
use bevy_ecs::{
    component::Component,
    entity::Entity,
    query::Without,
    reflect::ReflectComponent,
    schedule::IntoSystemConfigs,
    system::{Query, Res},
};
use bevy_math::{
    primitives::{Cone, Sphere},
    Quat, Vec3,
};
use bevy_pbr::{DirectionalLight, PointLight, SpotLight};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_transform::{components::GlobalTransform, TransformSystem};

use crate::{
    config::{GizmoConfigGroup, GizmoConfigStore},
    gizmos::Gizmos,
    AppGizmoBuilder,
};

/// Draws a standard sphere for the radius and an axis sphere for the range.
fn point_light_gizmo(
    transform: &GlobalTransform,
    point_light: &PointLight,
    color: Color,
    gizmos: &mut Gizmos<LightGizmoConfigGroup>,
) {
    let position = transform.translation();
    gizmos
        .primitive_3d(
            &Sphere {
                radius: point_light.radius,
            },
            position,
            Quat::IDENTITY,
            color,
        )
        .resolution(16);
    gizmos
        .sphere(position, Quat::IDENTITY, point_light.range, color)
        .resolution(32);
}

/// Draws a sphere for the radius, two cones for the inner and outer angles, plus two 3d arcs crossing the
/// farthest point of effect of the spot light along its direction.
fn spot_light_gizmo(
    transform: &GlobalTransform,
    spot_light: &SpotLight,
    color: Color,
    gizmos: &mut Gizmos<LightGizmoConfigGroup>,
) {
    let (_, rotation, translation) = transform.to_scale_rotation_translation();
    gizmos
        .primitive_3d(
            &Sphere {
                radius: spot_light.radius,
            },
            translation,
            Quat::IDENTITY,
            color,
        )
        .resolution(16);

    // Offset the tip of the cone to the light position.
    for angle in [spot_light.inner_angle, spot_light.outer_angle] {
        let height = spot_light.range * angle.cos();
        let position = translation + rotation * Vec3::NEG_Z * height / 2.0;
        gizmos
            .primitive_3d(
                &Cone {
                    radius: spot_light.range * angle.sin(),
                    height,
                },
                position,
                rotation * Quat::from_rotation_x(PI / 2.0),
                color,
            )
            .height_resolution(4)
            .base_resolution(32);
    }

    for arc_rotation in [
        Quat::from_rotation_y(PI / 2.0 - spot_light.outer_angle),
        Quat::from_euler(
            bevy_math::EulerRot::XZY,
            0.0,
            PI / 2.0,
            PI / 2.0 - spot_light.outer_angle,
        ),
    ] {
        gizmos
            .arc_3d(
                2.0 * spot_light.outer_angle,
                spot_light.range,
                translation,
                rotation * arc_rotation,
                color,
            )
            .resolution(16);
    }
}

/// Draws an arrow alongside the directional light direction.
fn directional_light_gizmo(
    transform: &GlobalTransform,
    color: Color,
    gizmos: &mut Gizmos<LightGizmoConfigGroup>,
) {
    let (_, rotation, translation) = transform.to_scale_rotation_translation();
    gizmos
        .arrow(translation, translation + rotation * Vec3::NEG_Z, color)
        .with_tip_length(0.3);
}

/// A [`Plugin`] that provides visualization of [`PointLight`]s, [`SpotLight`]s
/// and [`DirectionalLight`]s for debugging.
pub struct LightGizmoPlugin;

impl Plugin for LightGizmoPlugin {
    fn build(&self, app: &mut bevy_app::App) {
        app.register_type::<LightGizmoConfigGroup>()
            .init_gizmo_group::<LightGizmoConfigGroup>()
            .add_systems(
                PostUpdate,
                (
                    draw_lights,
                    draw_all_lights.run_if(|config: Res<GizmoConfigStore>| {
                        config.config::<LightGizmoConfigGroup>().1.draw_all
                    }),
                )
                    .after(TransformSystem::TransformPropagate),
            );
    }
}

/// Configures how a color is attributed to a light gizmo.
#[derive(Debug, Clone, Copy, Default, Reflect)]
pub enum LightGizmoColor {
    /// User-specified color.
    Manual(Color),
    /// Random color derived from the light's [`Entity`].
    Varied,
    /// Take the color of the represented light.
    #[default]
    MatchLightColor,
    /// Take the color provided by [`LightGizmoConfigGroup`] depending on the light kind.
    ByLightType,
}

/// The [`GizmoConfigGroup`] used to configure the visualization of lights.
#[derive(Clone, Reflect, GizmoConfigGroup)]
pub struct LightGizmoConfigGroup {
    /// Draw a gizmo for all lights if true.
    ///
    /// Defaults to `false`.
    pub draw_all: bool,
    /// Default color strategy for all light gizmos.
    ///
    /// Defaults to [`LightGizmoColor::MatchLightColor`].
    pub color: LightGizmoColor,
    /// [`Color`] to use for drawing a [`PointLight`] gizmo when [`LightGizmoColor::ByLightType`] is used.
    ///
    /// Defaults to [`RED`].
    pub point_light_color: Color,
    /// [`Color`] to use for drawing a [`SpotLight`] gizmo when [`LightGizmoColor::ByLightType`] is used.
    ///
    /// Defaults to [`GREEN`].
    pub spot_light_color: Color,
    /// [`Color`] to use for drawing a [`DirectionalLight`] gizmo when [`LightGizmoColor::ByLightType`] is used.
    ///
    /// Defaults to [`BLUE`].
    pub directional_light_color: Color,
}

impl Default for LightGizmoConfigGroup {
    fn default() -> Self {
        Self {
            draw_all: false,
            color: LightGizmoColor::MatchLightColor,
            point_light_color: RED.into(),
            spot_light_color: GREEN.into(),
            directional_light_color: BLUE.into(),
        }
    }
}

/// Add this [`Component`] to an entity to draw any of its lights components
/// ([`PointLight`], [`SpotLight`] and [`DirectionalLight`]).
#[derive(Component, Reflect, Default, Debug)]
#[reflect(Component, Default)]
pub struct ShowLightGizmo {
    /// Default color strategy for this light gizmo. if [`None`], use the one provided by [`LightGizmoConfigGroup`].
    ///
    /// Defaults to [`None`].
    pub color: Option<LightGizmoColor>,
}

fn draw_lights(
    point_query: Query<(Entity, &PointLight, &GlobalTransform, &ShowLightGizmo)>,
    spot_query: Query<(Entity, &SpotLight, &GlobalTransform, &ShowLightGizmo)>,
    directional_query: Query<(Entity, &DirectionalLight, &GlobalTransform, &ShowLightGizmo)>,
    mut gizmos: Gizmos<LightGizmoConfigGroup>,
) {
    let color = |entity: Entity, gizmo_color: Option<LightGizmoColor>, light_color, type_color| {
        match gizmo_color.unwrap_or(gizmos.config_ext.color) {
            LightGizmoColor::Manual(color) => color,
            LightGizmoColor::Varied => Oklcha::sequential_dispersed(entity.index()).into(),
            LightGizmoColor::MatchLightColor => light_color,
            LightGizmoColor::ByLightType => type_color,
        }
    };
    for (entity, light, transform, light_gizmo) in &point_query {
        let color = color(
            entity,
            light_gizmo.color,
            light.color,
            gizmos.config_ext.point_light_color,
        );
        point_light_gizmo(transform, light, color, &mut gizmos);
    }
    for (entity, light, transform, light_gizmo) in &spot_query {
        let color = color(
            entity,
            light_gizmo.color,
            light.color,
            gizmos.config_ext.spot_light_color,
        );
        spot_light_gizmo(transform, light, color, &mut gizmos);
    }
    for (entity, light, transform, light_gizmo) in &directional_query {
        let color = color(
            entity,
            light_gizmo.color,
            light.color,
            gizmos.config_ext.directional_light_color,
        );
        directional_light_gizmo(transform, color, &mut gizmos);
    }
}

fn draw_all_lights(
    point_query: Query<(Entity, &PointLight, &GlobalTransform), Without<ShowLightGizmo>>,
    spot_query: Query<(Entity, &SpotLight, &GlobalTransform), Without<ShowLightGizmo>>,
    directional_query: Query<
        (Entity, &DirectionalLight, &GlobalTransform),
        Without<ShowLightGizmo>,
    >,
    mut gizmos: Gizmos<LightGizmoConfigGroup>,
) {
    match gizmos.config_ext.color {
        LightGizmoColor::Manual(color) => {
            for (_, light, transform) in &point_query {
                point_light_gizmo(transform, light, color, &mut gizmos);
            }
            for (_, light, transform) in &spot_query {
                spot_light_gizmo(transform, light, color, &mut gizmos);
            }
            for (_, _, transform) in &directional_query {
                directional_light_gizmo(transform, color, &mut gizmos);
            }
        }
        LightGizmoColor::Varied => {
            let color = |entity: Entity| Oklcha::sequential_dispersed(entity.index()).into();
            for (entity, light, transform) in &point_query {
                point_light_gizmo(transform, light, color(entity), &mut gizmos);
            }
            for (entity, light, transform) in &spot_query {
                spot_light_gizmo(transform, light, color(entity), &mut gizmos);
            }
            for (entity, _, transform) in &directional_query {
                directional_light_gizmo(transform, color(entity), &mut gizmos);
            }
        }
        LightGizmoColor::MatchLightColor => {
            for (_, light, transform) in &point_query {
                point_light_gizmo(transform, light, light.color, &mut gizmos);
            }
            for (_, light, transform) in &spot_query {
                spot_light_gizmo(transform, light, light.color, &mut gizmos);
            }
            for (_, light, transform) in &directional_query {
                directional_light_gizmo(transform, light.color, &mut gizmos);
            }
        }
        LightGizmoColor::ByLightType => {
            for (_, light, transform) in &point_query {
                point_light_gizmo(
                    transform,
                    light,
                    gizmos.config_ext.point_light_color,
                    &mut gizmos,
                );
            }
            for (_, light, transform) in &spot_query {
                spot_light_gizmo(
                    transform,
                    light,
                    gizmos.config_ext.spot_light_color,
                    &mut gizmos,
                );
            }
            for (_, _, transform) in &directional_query {
                directional_light_gizmo(
                    transform,
                    gizmos.config_ext.directional_light_color,
                    &mut gizmos,
                );
            }
        }
    }
}