use crate::asset::{Asset, CpuMesh, Image, Material, Vertex};
use crate::renderer::Renderer;
use crate::settings::{self, Settings};
use crate::view::cascades::SHADOW_PANCAKE_SIZE;
use crate::{
Backdrop, Camera, DirectionalLight, DirectionalShadow, FrameInput, InstanceId, InstanceState,
Light, LightShape, Mobility, Scene, test_support,
};
use glam::{Mat4, Vec3};
const SIZE: [u32; 2] = [64, 64];
const DEPTH: f32 = 5.;
const EDGE_COLUMN: u32 = 40;
fn quad(min: [f32; 2], max: [f32; 2], z: f32) -> CpuMesh {
CpuMesh {
vertices: [[0., 0.], [1., 0.], [1., 1.], [0., 1.]]
.map(|[s, t]| Vertex {
tangent: [0.; 4],
lightmap_bounds: [0., 0., 1., 1.],
lightmap_uv: [0.; 2],
position: [
min[0] + (max[0] - min[0]) * s,
min[1] + (max[1] - min[1]) * t,
z,
],
normal: [0., 0., 1.],
uv: [s, t],
color: [1.; 4],
})
.to_vec(),
indices: vec![0, 1, 2, 0, 2, 3],
material: 0,
deformation: Default::default(),
}
}
fn white(mesh: CpuMesh, tilted: bool) -> Asset {
let mut asset = test_support::cube();
asset.meshes = vec![mesh];
asset.materials = vec![Material {
base: [0.8, 0.8, 0.8, 1.],
metallic: 0.,
roughness: 1.,
normal_texture: tilted.then_some(0),
..Default::default()
}];
asset.images = vec![Image::Rgba8(image::RgbaImage::from_pixel(
4,
4,
image::Rgba([204, 128, 230, 255]),
))];
asset
}
fn edge(projection: Mat4) -> f32 {
let ndc = (EDGE_COLUMN as f32 + 0.5) / SIZE[0] as f32 * 2. - 1.;
ndc / projection.x_axis.x * DEPTH
}
fn add(
scene: &mut Scene,
gpu: (&wgpu::Device, &wgpu::Queue),
asset: Asset,
visible: bool,
) -> InstanceId {
let (device, queue) = gpu;
let model = scene.add_asset(device, queue, asset).unwrap().model;
let state = InstanceState {
model,
pose: Mat4::IDENTITY,
visible,
capture_visible: true,
};
scene
.add_instance(device, queue, state, Mobility::Static)
.unwrap()
}
#[derive(Clone, Copy, Debug)]
enum Shining {
Directional,
Spot,
}
fn visibility(gpu: (&wgpu::Device, &wgpu::Queue), tilted: bool, shining: Shining) -> Vec<f32> {
let (device, queue) = gpu;
let settings = Settings {
antialiasing: settings::Antialiasing::Off,
bloom: settings::Bloom::Off,
atmosphere: false,
ambient_occlusion: settings::AmbientOcclusionQuality::Off,
..Settings::default()
};
let mut renderer = Renderer::for_test(device, queue, SIZE, &settings);
let mut scene = Scene::new(device, queue);
add(
&mut scene,
gpu,
white(quad([-4., -4.], [4., 4.], -DEPTH), tilted),
true,
);
let projection = crate::perspective(1., 1., 0.1);
let x = edge(projection);
let mut input = FrameInput::new(Camera {
view: Mat4::IDENTITY,
projection,
eye: Vec3::ZERO,
});
input.backdrop = Backdrop::Color([0.; 3]);
input.baked_lighting = false;
let occluder = match shining {
Shining::Directional => {
input.directional_lights[0] = Some(DirectionalLight {
direction: Vec3::NEG_Z,
color: [1.; 3],
illuminance: 1.,
shadow: Some(DirectionalShadow {
distance: 10.,
cascades: 2,
}),
..Default::default()
});
quad([x - 8., -8.], [x, 8.], SHADOW_PANCAKE_SIZE + 10.)
}
Shining::Spot => {
let light = Light {
shape: LightShape::Spot {
direction: Vec3::NEG_Z,
inner_angle: 0.6,
outer_angle: 0.7,
radius: LightShape::DEFAULT_RADIUS,
},
intensity: DEPTH * DEPTH,
range: 20.,
casts_shadow: true,
..Default::default()
};
scene.add_light(device, queue, light).unwrap();
quad([x / 2. - 4., -4.], [x / 2., 4.], -DEPTH / 2.)
}
};
let occluder = add(&mut scene, gpu, white(occluder, false), false);
let output = crate::view::targets::target(
device,
"shadow output",
SIZE,
crate::shading::gbuffer::COLOR,
);
let mut row = |scene: &mut Scene| {
let mut encoder = device.create_command_encoder(&Default::default());
renderer.render(
device,
queue,
&mut encoder,
scene,
&input,
&settings,
&output,
None,
);
queue.submit([encoder.finish()]);
renderer.finish_frame(scene);
let color = test_support::read(device, queue, renderer.targets().color.texture(), 8);
let start = (SIZE[1] / 2 * SIZE[0]) as usize;
(0..SIZE[0] as usize)
.map(|x| test_support::half(&color[(start + x) * 8..]))
.collect::<Vec<_>>()
};
let shadowed = row(&mut scene);
let mut state = *scene.instance(occluder).unwrap();
state.capture_visible = false;
scene.set_instance(queue, occluder, state).unwrap();
let lit = row(&mut scene);
shadowed.iter().zip(&lit).map(|(s, l)| s / l).collect()
}
#[test]
fn normal_maps_do_not_move_shadows() {
let Some((device, queue)) = test_support::device() else {
return;
};
let gpu = (&device, &queue);
for shining in [Shining::Directional, Shining::Spot] {
let label = format!("{shining:?}");
let flat = visibility(gpu, false, shining);
let tilted = visibility(gpu, true, shining);
let at = EDGE_COLUMN as usize;
assert!(
(0.1..=0.9).contains(&flat[at]),
"{label}: the edge column takes {} of the light; the fixture misses the edge",
flat[at]
);
for column in at - 3..=at + 3 {
assert!(
(flat[column] - tilted[column]).abs() < 0.02,
"{label}: column {column} takes {} of the light under a tilted normal map and {} without",
tilted[column],
flat[column]
);
}
}
}