use crate::core::World;
use crate::math::{Vec3, Vec4};
use crate::renderer::components::{DirectionalLight, LightRole, PointLight, SpotLight};
use crate::renderer::gpu_types::LightData;
use gizmo_physics_core::components::{GlobalTransform, Transform};
pub struct SceneLights {
pub lights: [LightData; 10],
pub num_lights: u32,
pub sun_dir: Vec3,
pub sun_col: Vec4,
pub has_sun: bool,
pub shadow_point_index: i32,
}
pub fn collect_scene_lights(world: &World) -> SceneLights {
let globals = world.borrow::<GlobalTransform>();
let locals = world.borrow::<Transform>();
let world_tf = |e| {
globals
.get(e)
.map(|g| {
let (_, rot, pos) = g.matrix.to_scale_rotation_translation();
(pos, rot)
})
.or_else(|| locals.get(e).map(|t| (t.position, t.rotation)))
};
let mut lights = [LightData {
position: [0.0; 4],
color: [0.0; 4],
direction: [0.0, -1.0, 0.0, 0.0],
params: [0.0; 4],
}; 10];
let mut num_lights = 0usize;
let mut shadow_point_index: i32 = -1;
if let Some(q) = world.query::<&PointLight>() {
for (e, light) in q.iter() {
if num_lights >= 10 {
break;
}
let Some((pos, _)) = world_tf(e) else { continue };
if shadow_point_index < 0 {
shadow_point_index = num_lights as i32;
}
lights[num_lights] = LightData {
position: [pos.x, pos.y, pos.z, light.intensity],
color: [light.color.x, light.color.y, light.color.z, light.radius],
direction: [0.0, -1.0, 0.0, 0.0],
params: [0.0, 0.0, 0.0, 0.0], };
num_lights += 1;
}
}
if let Some(q) = world.query::<&SpotLight>() {
for (e, light) in q.iter() {
if num_lights >= 10 {
break;
}
let Some((pos, rot)) = world_tf(e) else { continue };
let dir = rot.mul_vec3(Vec3::new(0.0, 0.0, -1.0)).normalize();
lights[num_lights] = LightData {
position: [pos.x, pos.y, pos.z, light.intensity],
color: [light.color.x, light.color.y, light.color.z, light.radius],
direction: [dir.x, dir.y, dir.z, light.inner_angle.cos()],
params: [light.outer_angle.cos(), 1.0, 0.0, 0.0], };
num_lights += 1;
}
}
let mut sun_dir = Vec3::new(0.0, -1.0, 0.0);
let mut sun_col = Vec4::new(0.0, 0.0, 0.0, 0.0); let mut has_sun = false;
if let Some(q) = world.query::<&DirectionalLight>() {
for (e, light) in q.iter() {
if light.role == LightRole::Sun {
if let Some((_, rot)) = world_tf(e) {
sun_dir = rot.mul_vec3(Vec3::new(0.0, 0.0, -1.0)).normalize();
sun_col = Vec4::new(light.color.x, light.color.y, light.color.z, light.intensity);
has_sun = true;
}
break; }
}
}
SceneLights {
lights,
num_lights: num_lights as u32,
sun_dir,
sun_col,
has_sun,
shadow_point_index,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::World;
use crate::renderer::components::{PointLight, SpotLight};
use gizmo_physics_core::components::GlobalTransform;
#[test]
fn spotlight_cutoffs_are_stored_as_cosines() {
let mut world = World::new();
let e = world.spawn();
world.add_component(e, GlobalTransform::default());
world.add_component(e, SpotLight::new(Vec3::ONE, 10.0, 30.0, 0.4, 0.6));
let l = collect_scene_lights(&world);
assert_eq!(l.num_lights, 1);
let spot = l.lights[0];
assert_eq!(spot.params[1], 1.0, "params.y == 1 marks a spot light");
assert!(
(spot.direction[3] - 0.4_f32.cos()).abs() < 1e-5,
"inner cutoff must be cos(inner_angle), got {}",
spot.direction[3]
);
assert!(
(spot.params[0] - 0.6_f32.cos()).abs() < 1e-5,
"outer cutoff must be cos(outer_angle), got {}",
spot.params[0]
);
assert!(spot.direction[3] > spot.params[0]);
}
#[test]
fn point_before_spot_and_transform_fallback() {
let mut world = World::new();
let p = world.spawn();
world.add_component(p, GlobalTransform::default());
world.add_component(p, PointLight::new(Vec3::ONE, 5.0, 12.0));
let s = world.spawn();
world.add_component(s, Transform::new(Vec3::new(1.0, 2.0, 3.0)));
world.add_component(s, SpotLight::new(Vec3::ONE, 7.0, 20.0, 0.3, 0.5));
let l = collect_scene_lights(&world);
assert_eq!(l.num_lights, 2);
assert_eq!(l.lights[0].params[1], 0.0, "point light packed first");
assert_eq!(l.lights[1].params[1], 1.0, "spot light packed second");
assert_eq!(l.lights[1].position, [1.0, 2.0, 3.0, 7.0]);
assert_eq!(l.shadow_point_index, 0, "first point light is the shadow caster");
}
#[test]
fn no_point_light_has_no_shadow_caster() {
let mut world = World::new();
let s = world.spawn();
world.add_component(s, GlobalTransform::default());
world.add_component(s, SpotLight::new(Vec3::ONE, 7.0, 20.0, 0.3, 0.5));
let l = collect_scene_lights(&world);
assert_eq!(l.num_lights, 1);
assert_eq!(l.shadow_point_index, -1, "no point light → no point-shadow caster");
}
}