|
| 1 | +use std::f32::consts::PI; |
| 2 | + |
| 3 | +use crate::{self as bevy_gizmos, primitives::dim3::GizmoPrimitive3d}; |
| 4 | + |
| 5 | +use bevy_app::{Plugin, PostUpdate}; |
| 6 | +use bevy_color::{Color, LinearRgba}; |
| 7 | +use bevy_ecs::{ |
| 8 | + component::Component, |
| 9 | + query::{With, Without}, |
| 10 | + reflect::ReflectComponent, |
| 11 | + schedule::IntoSystemConfigs, |
| 12 | + system::{Query, Res}, |
| 13 | +}; |
| 14 | +use bevy_math::{primitives::Cone, Quat, Vec3}; |
| 15 | +use bevy_pbr::{DirectionalLight, PointLight, SpotLight}; |
| 16 | +use bevy_reflect::{std_traits::ReflectDefault, Reflect}; |
| 17 | +use bevy_transform::{components::GlobalTransform, TransformSystem}; |
| 18 | + |
| 19 | +use crate::{ |
| 20 | + config::{GizmoConfigGroup, GizmoConfigStore}, |
| 21 | + gizmos::Gizmos, |
| 22 | + AppGizmoBuilder, |
| 23 | +}; |
| 24 | + |
| 25 | +fn draw_gizmos<'a, P, S, D>( |
| 26 | + point_lights: P, |
| 27 | + spot_lights: S, |
| 28 | + directional_lights: D, |
| 29 | + gizmos: &mut Gizmos<LightGizmoConfigGroup>, |
| 30 | +) where |
| 31 | + P: 'a + IntoIterator<Item = (&'a PointLight, &'a GlobalTransform)>, |
| 32 | + S: 'a + IntoIterator<Item = (&'a SpotLight, &'a GlobalTransform)>, |
| 33 | + D: 'a + IntoIterator<Item = (&'a DirectionalLight, &'a GlobalTransform)>, |
| 34 | +{ |
| 35 | + for (point_light, transform) in point_lights { |
| 36 | + let position = transform.translation(); |
| 37 | + gizmos.sphere( |
| 38 | + position, |
| 39 | + Quat::IDENTITY, |
| 40 | + point_light.radius, |
| 41 | + point_light.color, |
| 42 | + ); |
| 43 | + gizmos |
| 44 | + .sphere( |
| 45 | + position, |
| 46 | + Quat::IDENTITY, |
| 47 | + point_light.range, |
| 48 | + point_light.color, |
| 49 | + ) |
| 50 | + .circle_segments(32); |
| 51 | + } |
| 52 | + |
| 53 | + for (spot_light, transform) in spot_lights { |
| 54 | + let (_, rotation, translation) = transform.to_scale_rotation_translation(); |
| 55 | + gizmos.sphere( |
| 56 | + translation, |
| 57 | + Quat::IDENTITY, |
| 58 | + spot_light.radius, |
| 59 | + spot_light.color, |
| 60 | + ); |
| 61 | + |
| 62 | + // Offset the tip of the cone to the light position. |
| 63 | + gizmos.sphere(translation, Quat::IDENTITY, 0.01, LinearRgba::GREEN); |
| 64 | + for angle in [spot_light.inner_angle, spot_light.outer_angle] { |
| 65 | + let height = spot_light.range * angle.cos(); |
| 66 | + let position = translation + rotation * Vec3::NEG_Z * height / 2.0; |
| 67 | + gizmos |
| 68 | + .primitive_3d( |
| 69 | + Cone { |
| 70 | + radius: spot_light.range * angle.sin(), |
| 71 | + height, |
| 72 | + }, |
| 73 | + position, |
| 74 | + rotation * Quat::from_rotation_x(PI / 2.0), |
| 75 | + spot_light.color, |
| 76 | + ) |
| 77 | + .height_segments(4) |
| 78 | + .base_segments(32); |
| 79 | + } |
| 80 | + |
| 81 | + for arc_rotation in [ |
| 82 | + Quat::from_rotation_y(PI / 2.0 - spot_light.outer_angle), |
| 83 | + Quat::from_euler( |
| 84 | + bevy_math::EulerRot::XZY, |
| 85 | + 0.0, |
| 86 | + PI / 2.0, |
| 87 | + PI / 2.0 - spot_light.outer_angle, |
| 88 | + ), |
| 89 | + ] { |
| 90 | + gizmos |
| 91 | + .arc_3d( |
| 92 | + 2.0 * spot_light.outer_angle, |
| 93 | + spot_light.range, |
| 94 | + translation, |
| 95 | + rotation * arc_rotation, |
| 96 | + spot_light.color, |
| 97 | + ) |
| 98 | + .segments(16); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + for (directional_light, transform) in directional_lights { |
| 103 | + let (_, rotation, translation) = transform.to_scale_rotation_translation(); |
| 104 | + gizmos |
| 105 | + .arrow( |
| 106 | + translation, |
| 107 | + translation + rotation * Vec3::NEG_Z, |
| 108 | + directional_light.color, |
| 109 | + ) |
| 110 | + .with_tip_length(0.3); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +pub struct LightGizmoPlugin; |
| 115 | + |
| 116 | +impl Plugin for LightGizmoPlugin { |
| 117 | + fn build(&self, app: &mut bevy_app::App) { |
| 118 | + app.register_type::<LightGizmoConfigGroup>() |
| 119 | + .init_gizmo_group::<LightGizmoConfigGroup>() |
| 120 | + .add_systems( |
| 121 | + PostUpdate, |
| 122 | + ( |
| 123 | + draw_lights, |
| 124 | + draw_all_lights.run_if(|config: Res<GizmoConfigStore>| { |
| 125 | + config.config::<LightGizmoConfigGroup>().1.draw_all |
| 126 | + }), |
| 127 | + ) |
| 128 | + .after(TransformSystem::TransformPropagate), |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +#[derive(Clone, Default, Reflect, GizmoConfigGroup)] |
| 134 | +pub struct LightGizmoConfigGroup { |
| 135 | + pub draw_all: bool, |
| 136 | +} |
| 137 | + |
| 138 | +#[derive(Component, Reflect, Default, Debug)] |
| 139 | +#[reflect(Component, Default)] |
| 140 | +pub struct ShowLightGizmo; |
| 141 | + |
| 142 | +fn draw_lights( |
| 143 | + point_query: Query<(&PointLight, &GlobalTransform), With<ShowLightGizmo>>, |
| 144 | + spot_query: Query<(&SpotLight, &GlobalTransform), With<ShowLightGizmo>>, |
| 145 | + directional_query: Query<(&DirectionalLight, &GlobalTransform), With<ShowLightGizmo>>, |
| 146 | + mut gizmos: Gizmos<LightGizmoConfigGroup>, |
| 147 | +) { |
| 148 | + draw_gizmos(&point_query, &spot_query, &directional_query, &mut gizmos); |
| 149 | +} |
| 150 | + |
| 151 | +fn draw_all_lights( |
| 152 | + point_query: Query<(&PointLight, &GlobalTransform), Without<ShowLightGizmo>>, |
| 153 | + spot_query: Query<(&SpotLight, &GlobalTransform), Without<ShowLightGizmo>>, |
| 154 | + directional_query: Query<(&DirectionalLight, &GlobalTransform), Without<ShowLightGizmo>>, |
| 155 | + mut gizmos: Gizmos<LightGizmoConfigGroup>, |
| 156 | +) { |
| 157 | + draw_gizmos(&point_query, &spot_query, &directional_query, &mut gizmos); |
| 158 | +} |
0 commit comments