use crcbl_shaders::light::{self, GpuLight};
use glam::Vec3;
use crate::camera::DirectionalLight;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Light {
Point(PointLight),
Spot(SpotLight),
Rect(RectLight),
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PointLight {
pub position: Vec3,
pub radius: f32,
pub color: Vec3,
pub fill: bool,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SpotLight {
pub position: Vec3,
pub radius: f32,
pub color: Vec3,
pub direction: Vec3,
pub inner_angle: f32,
pub outer_angle: f32,
pub fill: bool,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RectLight {
pub position: Vec3,
pub radius: f32,
pub color: Vec3,
pub direction: Vec3,
pub tangent: Vec3,
pub half_width: f32,
pub half_height: f32,
pub fill: bool,
}
impl RectLight {
#[must_use]
pub fn frame(&self) -> (Vec3, Vec3) {
let normal = self.direction.normalize_or_zero();
let normal = if normal.length_squared() > 0.0 {
normal
} else {
Vec3::Z
};
let across = self.tangent - normal * self.tangent.dot(normal);
let tangent = across.normalize_or_zero();
if tangent.length_squared() > 0.0 {
return (normal, tangent);
}
let seed = if normal.z.abs() < 0.9 {
Vec3::Z
} else {
Vec3::X
};
(normal, seed.cross(normal).normalize())
}
}
impl Light {
#[must_use]
pub const fn is_fill(&self) -> bool {
match self {
Self::Point(point) => point.fill,
Self::Spot(spot) => spot.fill,
Self::Rect(rect) => rect.fill,
}
}
#[must_use]
pub fn row(&self, base_tile: Option<usize>) -> GpuLight {
let shadow_tile = base_tile.map_or(light::NO_SHADOW_TILE, |base| {
u32::try_from(base).unwrap_or(light::NO_SHADOW_TILE)
});
let flags = if self.is_fill() { light::FLAG_FILL } else { 0 };
match self {
Self::Point(point) => GpuLight {
position: point.position.extend(point.radius).to_array(),
color: point.color.extend(0.0).to_array(),
direction: [0.0; 4],
tangent: [0.0; 4],
kind: light::KIND_POINT,
cos_inner: 0.0,
shadow_tile,
flags,
},
Self::Spot(spot) => {
let outer = spot.outer_angle.max(spot.inner_angle);
GpuLight {
position: spot.position.extend(spot.radius).to_array(),
color: spot.color.extend(0.0).to_array(),
direction: spot
.direction
.normalize_or_zero()
.extend(outer.cos())
.to_array(),
tangent: [0.0; 4],
kind: light::KIND_SPOT,
cos_inner: spot.inner_angle.cos(),
shadow_tile,
flags,
}
}
Self::Rect(rect) => {
let (normal, tangent) = rect.frame();
GpuLight {
position: rect.position.extend(rect.radius).to_array(),
color: rect.color.extend(0.0).to_array(),
direction: normal.extend(rect.half_height).to_array(),
tangent: tangent.extend(rect.half_width).to_array(),
kind: light::KIND_RECT,
cos_inner: 0.0,
shadow_tile,
flags,
}
}
}
}
#[must_use]
pub const fn sphere(&self) -> (Vec3, f32) {
match self {
Self::Point(point) => (point.position, point.radius),
Self::Spot(spot) => (spot.position, spot.radius),
Self::Rect(rect) => (rect.position, rect.radius),
}
}
}
#[must_use]
pub fn sun_row(sun: &DirectionalLight) -> GpuLight {
GpuLight {
position: [0.0; 4],
color: sun.color.extend(0.0).to_array(),
direction: sun.direction.normalize_or_zero().extend(0.0).to_array(),
tangent: [0.0; 4],
kind: light::KIND_DIRECTIONAL,
cos_inner: 0.0,
shadow_tile: light::NO_SHADOW_TILE,
flags: 0,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_sun_s_row_is_the_frame_block_s_two_fields() {
let sun = DirectionalLight::default();
let row = sun_row(&sun);
assert_eq!(row.kind, light::KIND_DIRECTIONAL);
let normalised = sun.direction.normalize_or_zero();
assert_eq!(
[row.direction[0], row.direction[1], row.direction[2]],
normalised.to_array(),
"the row must carry the same bits the uniform block carried, or the \
shader's `normalize` is not the same arithmetic"
);
assert_eq!(
[row.color[0], row.color[1], row.color[2]],
sun.color.to_array()
);
assert_eq!(
row.position, [0.0; 4],
"a directional light has no position and no radius"
);
}
#[test]
fn a_point_light_carries_its_radius_in_the_position_s_w() {
let row = Light::Point(PointLight {
position: Vec3::new(1.0, 2.0, 3.0),
radius: 7.0,
color: Vec3::new(0.25, 0.5, 0.75),
fill: false,
})
.row(None);
assert_eq!(row.kind, light::KIND_POINT);
assert_eq!(row.position, [1.0, 2.0, 3.0, 7.0]);
assert_eq!(row.color, [0.25, 0.5, 0.75, 0.0]);
}
#[test]
fn a_spot_with_its_angles_the_wrong_way_round_is_widened_not_inverted() {
let spot = SpotLight {
position: Vec3::ZERO,
radius: 5.0,
color: Vec3::ONE,
direction: Vec3::new(0.0, -2.0, 0.0),
inner_angle: 0.6,
outer_angle: 0.2,
fill: false,
};
let row = Light::Spot(spot).row(None);
assert_eq!(row.kind, light::KIND_SPOT);
assert!(
row.cos_inner >= row.direction[3],
"the inner cosine must not be smaller than the outer one, or the \
shader divides by a negative and lights the outside of the cone: \
inner {} outer {}",
row.cos_inner,
row.direction[3]
);
assert_eq!(
[row.direction[0], row.direction[1], row.direction[2]],
[0.0, -1.0, 0.0],
"the cone axis is normalised on the way in"
);
}
#[test]
fn a_spot_the_right_way_round_keeps_both_of_its_angles() {
let row = Light::Spot(SpotLight {
position: Vec3::ZERO,
radius: 5.0,
color: Vec3::ONE,
direction: -Vec3::Y,
inner_angle: 0.2,
outer_angle: 0.6,
fill: false,
})
.row(None);
assert!((row.cos_inner - 0.2_f32.cos()).abs() < 1e-6);
assert!((row.direction[3] - 0.6_f32.cos()).abs() < 1e-6);
}
#[test]
fn a_fill_light_of_any_kind_carries_the_flag_into_its_row() {
let point = PointLight {
position: Vec3::ZERO,
radius: 5.0,
color: Vec3::ONE,
fill: false,
};
let spot = SpotLight {
position: Vec3::ZERO,
radius: 5.0,
color: Vec3::ONE,
direction: -Vec3::Y,
inner_angle: 0.2,
outer_angle: 0.6,
fill: false,
};
let rect = RectLight {
position: Vec3::ZERO,
radius: 5.0,
color: Vec3::ONE,
direction: -Vec3::Y,
tangent: Vec3::X,
half_width: 0.5,
half_height: 0.25,
fill: false,
};
for (kind, light) in [
("point", Light::Point(point)),
("spot", Light::Spot(spot)),
("rect", Light::Rect(rect)),
] {
assert_eq!(
light.row(None).flags,
0,
"an ordinary {kind} light's row must carry no flags"
);
let filled = match light {
Light::Point(point) => Light::Point(PointLight {
fill: true,
..point
}),
Light::Spot(spot) => Light::Spot(SpotLight { fill: true, ..spot }),
Light::Rect(rect) => Light::Rect(RectLight { fill: true, ..rect }),
};
assert!(filled.is_fill(), "a filled {kind} light must answer yes");
assert_eq!(
filled.row(None).flags,
light::FLAG_FILL,
"a fill {kind} light's row must carry the bit `mesh.slang` drops \
the specular on"
);
}
}
}