use super::*;
use glam::camera;
use glam::{DVec3, DVec4};
use wasm_bindgen_test::wasm_bindgen_test;
const MAP: u32 = 2048;
fn shadow(cascades: u32) -> DirectionalShadow {
DirectionalShadow {
distance: 200.,
cascades,
}
}
struct Sequence(u64);
impl Sequence {
fn next(&mut self) -> f64 {
self.0 = self
.0
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
(self.0 >> 11) as f64 / (1u64 << 53) as f64
}
fn between(&mut self, low: f64, high: f64) -> f64 {
low + (high - low) * self.next()
}
}
fn clip(cascade: &Cascade, point: DVec3) -> DVec3 {
let clip = cascade.clip_from_world.as_dmat4() * point.extend(1.);
clip.truncate() / clip.w
}
fn holds(cascade: &Cascade, point: DVec3) -> bool {
let p = clip(cascade, point);
p.x.abs() <= 1. && p.y.abs() <= 1. && (0. ..=1.).contains(&p.z)
}
#[wasm_bindgen_test(unsupported = test)]
fn a_fixed_point_moves_by_whole_texels_as_the_camera_moves_and_turns() {
let projection = crate::perspective(1.1, 16. / 9., 0.3);
let points = [
DVec3::new(13.7, 2.3, -41.9),
DVec3::new(-3.1, 0.2, -6.4),
DVec3::new(120.5, -8.25, 77.125),
];
for direction in [
Vec3::new(0.6, -1., -0.4),
Vec3::new(-180., 220., 260.).normalize() * -1.,
Vec3::NEG_Y,
] {
let mut sequence = Sequence(7);
let poses: Vec<Mat4> = (0..12)
.map(|_| {
let eye = Vec3::new(
sequence.between(-60., 60.) as f32,
sequence.between(0.5, 30.) as f32,
sequence.between(-60., 60.) as f32,
);
let yaw = sequence.between(-3.1, 3.1) as f32;
let pitch = sequence.between(-0.6, 0.4) as f32;
let forward = Vec3::new(
yaw.sin() * pitch.cos(),
pitch.sin(),
-yaw.cos() * pitch.cos(),
);
camera::rh::view::look_to_mat4(eye, forward, Vec3::Y)
})
.collect();
let fits: Vec<Cascades> = poses
.iter()
.map(|&view| {
Cascades::camera(view, projection, direction, &shadow(4), MAP, DVec3::ZERO)
})
.collect();
for (index, first) in fits[0].as_slice().iter().enumerate() {
for later in &fits[1..] {
let later = &later.as_slice()[index];
for point in points {
let texels = |cascade: &Cascade| {
let p = clip(cascade, point);
(p.truncate() * 0.5 + 0.5) * f64::from(MAP)
};
let moved = texels(later) - texels(first);
let fraction = (moved - moved.round()).abs();
assert!(
fraction.max_element() < 0.01,
"cascade {index}, light {direction:?}: {point:?} moved {moved:?} texels"
);
}
}
}
}
}
#[wasm_bindgen_test(unsupported = test)]
fn every_visible_point_within_the_shadow_distance_falls_inside_its_cascade() {
struct Camera {
projection: Mat4,
slope: DVec3,
half: DVec3,
centre: DVec3,
near: f64,
}
let perspective = |fov: f32, aspect: f32, near: f32| {
let tan = f64::from((fov / 2.).tan());
Camera {
projection: crate::perspective(fov, aspect, near),
slope: DVec3::new(tan * f64::from(aspect), tan, 0.),
half: DVec3::ZERO,
centre: DVec3::ZERO,
near: f64::from(near),
}
};
let cameras = [
perspective(1.1, 16. / 9., 0.3),
perspective(0.4, 1., 0.05),
perspective(2.2, 21. / 9., 1.),
Camera {
projection: camera::rh::proj::directx::orthographic(-30., 50., -20., 25., 400., 0.5),
slope: DVec3::ZERO,
half: DVec3::new(40., 22.5, 0.),
centre: DVec3::new(10., 2.5, 0.),
near: 0.5,
},
Camera {
projection: camera::rh::proj::directx::orthographic(-40., 40., -25., 25., 200., -40.),
slope: DVec3::ZERO,
half: DVec3::new(40., 25., 0.),
centre: DVec3::ZERO,
near: -40.,
},
Camera {
projection: camera::rh::proj::directx::orthographic(-40., 40., -25., 25., 200., -400.),
slope: DVec3::ZERO,
half: DVec3::new(40., 25., 0.),
centre: DVec3::ZERO,
near: -400.,
},
];
let directions = [
Vec3::new(0.6, -1., -0.4),
Vec3::new(0.05, -1., 0.02),
Vec3::new(-1., -0.1, 0.3),
];
let mut sequence = Sequence(11);
for camera in &cameras {
let projection = camera.projection;
for direction in directions {
for count in 1..=4 {
let eye = Vec3::new(17., 4., -230.);
let view = camera::rh::view::look_to_mat4(eye, Vec3::new(0.3, -0.2, -1.), Vec3::Y);
let shadow = shadow(count);
let cascades =
Cascades::camera(view, projection, direction, &shadow, MAP, DVec3::ZERO);
let cascades = cascades.as_slice();
assert_eq!(cascades.len(), count as usize);
let clip_from_world = (projection * view).as_dmat4();
let world_from_view = view.inverse().as_dmat4();
let mut seen = 0;
while seen < 3000 {
let depth = sequence.between(camera.near, f64::from(shadow.distance));
let reach = (camera.slope * depth + camera.half) * 1.2;
let candidate = DVec4::new(
camera.centre.x + sequence.between(-reach.x, reach.x),
camera.centre.y + sequence.between(-reach.y, reach.y),
-depth,
1.,
);
let world = (world_from_view * candidate).truncate();
let c = clip_from_world * world.extend(1.);
let device = c.truncate() / c.w;
if c.w <= 0. || device.x.abs() > 1. || device.y.abs() > 1. || device.z > 1. {
continue;
}
seen += 1;
let index = cascades
.iter()
.position(|cascade| depth < f64::from(cascade.far_bound))
.unwrap_or(cascades.len() - 1);
assert!(
holds(&cascades[index], world),
"{count} cascades, light {direction:?}: cascade {index} misses a point at depth {depth}"
);
let overlap = f64::from(next_near_bound(cascades[index].far_bound));
if index + 1 < cascades.len() && depth >= overlap {
assert!(
holds(&cascades[index + 1], world),
"{count} cascades: cascade {} misses a blended point at depth {depth}",
index + 1
);
}
}
let last = cascades.last().unwrap().far_bound;
assert!(
(last - shadow.distance).abs() < 1e-3,
"the last cascade ends at {last}, not the shadow distance"
);
}
}
}
}
#[wasm_bindgen_test(unsupported = test)]
fn every_point_a_capture_sees_within_the_shadow_distance_falls_inside_its_cascade() {
let center = Vec3::new(-41., 3.5, 812.);
let mut sequence = Sequence(3);
for direction in [Vec3::new(0.6, -1., -0.4), Vec3::NEG_Y] {
let cascades = Cascades::capture(center, direction, &shadow(4), MAP, DVec3::ZERO);
let cascades = cascades.as_slice();
for _ in 0..4000 {
let offset = DVec3::new(
sequence.between(-200., 200.),
sequence.between(-200., 200.),
sequence.between(-200., 200.),
);
let reach = offset.abs().max_element();
let index = cascades
.iter()
.position(|cascade| reach < f64::from(cascade.far_bound))
.unwrap();
assert!(
holds(&cascades[index], center.as_dvec3() + offset),
"cascade {index} misses a point {reach} m out"
);
}
}
}
#[wasm_bindgen_test(unsupported = test)]
fn cascade_bounds_are_godots_splits_of_the_range_from_the_near_plane() {
let view = camera::rh::view::look_to_mat4(Vec3::new(3., 2., 1.), Vec3::NEG_Z, Vec3::Y);
let projection = crate::perspective(1.1, 16. / 9., 0.3);
let camera: [&[f32]; 4] = [
&[150.3],
&[15.3, 150.3],
&[15.3, 30.3, 150.3],
&[15.3, 30.3, 75.3, 150.3],
];
let capture: [&[f32]; 4] = [&[40.], &[4., 40.], &[4., 8., 40.], &[4., 8., 20., 40.]];
for (count, (camera, capture)) in (1..=4u32).zip(camera.into_iter().zip(capture)) {
let fits = [
(
"camera",
camera,
Cascades::camera(
view,
projection,
Vec3::NEG_Y,
&DirectionalShadow {
distance: 150.3,
cascades: count,
},
MAP,
DVec3::ZERO,
),
),
(
"capture",
capture,
Cascades::capture(
Vec3::new(5., 1., -3.),
Vec3::NEG_Y,
&DirectionalShadow {
distance: 40.,
cascades: count,
},
MAP,
DVec3::ZERO,
),
),
];
for (label, expected, cascades) in fits {
let bounds: Vec<f32> = cascades
.as_slice()
.iter()
.map(|cascade| cascade.far_bound)
.collect();
assert!(
bounds.len() == expected.len()
&& bounds
.iter()
.zip(expected)
.all(|(a, b)| (a - b).abs() < 1e-3),
"{label}, {count} cascades: bounds {bounds:?}, not {expected:?}"
);
}
}
}
#[wasm_bindgen_test(unsupported = test)]
fn a_distance_out_of_range_shadows_to_the_nearer_bound() {
let view = camera::rh::view::look_to_mat4(Vec3::new(3., 2., 1.), Vec3::NEG_Z, Vec3::Y);
let projection = crate::perspective(1.1, 16. / 9., 0.3);
for (distance, camera, capture) in [
(0., 0.301, 0.001),
(-5., 0.301, 0.001),
(0.2, 0.301, 0.2),
(f32::NAN, 0.301, 0.001),
(f32::NEG_INFINITY, 0.301, 0.001),
(f32::INFINITY, 8192., 8192.),
(1e9, 8192., 8192.),
] {
let shadow = DirectionalShadow {
distance,
cascades: 1,
};
let fits = [
(
"camera",
camera,
Cascades::camera(view, projection, Vec3::NEG_Y, &shadow, MAP, DVec3::ZERO),
),
(
"capture",
capture,
Cascades::capture(
Vec3::new(5., 1., -3.),
Vec3::NEG_Y,
&shadow,
MAP,
DVec3::ZERO,
),
),
];
for (label, expected, cascades) in fits {
let [cascade] = cascades.as_slice() else {
panic!("{label}: distance {distance} cast no shadow");
};
assert!(
(cascade.far_bound - expected).abs() < 1e-4 * expected.max(1.)
&& cascade.clip_from_world.is_finite(),
"{label}: distance {distance} reaches {}, not {expected}",
cascade.far_bound
);
}
}
}
#[wasm_bindgen_test(unsupported = test)]
fn moving_the_render_origin_shifts_no_shadow_texel() {
let projection = crate::perspective(1.1, 16. / 9., 0.3);
let far = DVec3::new(10_000_000.375, -2_500.5, -20_000_000.625);
for direction in [
Vec3::new(0.6, -1., -0.4),
Vec3::new(-0.3, -1., 0.8),
Vec3::NEG_Y,
] {
for (first, moved) in [
(DVec3::ZERO, DVec3::new(731.37, 12.6, -409.81)),
(far, far + DVec3::new(1024., 0., -2048.)),
(far, far + DVec3::new(-77.7, 3.3, 129.9)),
] {
let eye = first + DVec3::new(5.25, 20., -3.5);
let forward = Vec3::new(0.3, -0.4, -1.).normalize();
let fit = |origin: DVec3| {
let view =
camera::rh::view::look_to_mat4((eye - origin).as_vec3(), forward, Vec3::Y);
Cascades::camera(view, projection, direction, &shadow(4), MAP, origin)
};
let (before, after) = (fit(first), fit(moved));
for (index, (before, after)) in
before.as_slice().iter().zip(after.as_slice()).enumerate()
{
for offset in [
DVec3::new(13.7, 2.3, -41.9),
DVec3::new(-3.1, -18.8, -6.4),
DVec3::new(60.5, -8.25, -77.125),
] {
let point = eye + offset;
let texels = |cascade: &Cascade, origin: DVec3| {
let p = clip(cascade, point - origin);
(p.truncate() * 0.5 + 0.5) * f64::from(MAP)
};
let shift = texels(after, moved) - texels(before, first);
let fraction = (shift - shift.round()).abs();
assert!(
fraction.max_element() < 0.01,
"cascade {index}, light {direction:?}, origin {first:?} to {moved:?}: shifted {shift:?} texels"
);
}
}
}
}
}