Get light pos directly from matrices, debugging shadowmaps

This commit is contained in:
Lauri Räsänen 2023-11-04 18:15:11 +02:00
parent 2dc20ad12c
commit 5e0001e2e1
4 changed files with 49 additions and 20 deletions

View file

@ -13,5 +13,5 @@ fn vs_main(
);
let world_position = model_matrix * vec4<f32>(model.position, 1.0);
return light.matrices[light_matrix_index] * (world_position - vec4<f32>(light.position, 0.0));
return light.matrices[light_matrix_index] * world_position;
}

View file

@ -82,6 +82,31 @@ fn sample_direct_light(index: i32, light_coords: vec4<f32>) -> f32 {
index,
light_coords.z * proj_correction
);
/*
var coords = light_coords;
// correct projection
coords.x /= coords.w;
coords.y /= coords.w;
coords.z /= coords.w;
// flip correction
coords.x *= 0.5;
coords.y *= -0.5;
// map to [0,1] range
coords.x += 0.5;
coords.y += 0.5;
return textureSampleCompareLevel(
t_light_depth,
s_light_depth,
coords.xy,
index,
coords.z
);
*/
}
@fragment
@ -100,8 +125,8 @@ fn fs_main(vert: VertexOutput) -> @location(0) vec4<f32> {
var total_radiance: vec3<f32>;
var in_light = 0.0;
for (var i: i32 = 0; i < 6; i++) {
in_light = sample_direct_light(i, light.matrices[i] * (vert.world_position - vec4<f32>(light.position, 0.0)));
for (var i: i32 = 0; i < 1; i++) {
in_light = sample_direct_light(i, light.matrices[i] * vert.world_position);
if (in_light > 0.0) {
break;
}

View file

@ -8,7 +8,7 @@ use super::{
use cgmath::{Matrix4, Vector3};
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
#[derive(Debug, Default, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct LightUniform {
pub position: [f32; 3],
_padding: u32,
@ -18,23 +18,26 @@ pub struct LightUniform {
impl LightUniform {
pub fn new(position: [f32; 3], color: [f32; 4]) -> Self {
let proj = cgmath::perspective(cgmath::Deg(90.0), 1.0, NEAR_PLANE, FAR_PLANE);
#[rustfmt::skip]
let matrices: [[[f32; 4]; 4]; 6] = [
(proj * Matrix4::look_to_rh(position.into(), Vector3::unit_x(), Vector3::unit_y())).into(),
(proj * Matrix4::look_to_rh(position.into(), -Vector3::unit_x(), Vector3::unit_y())).into(),
(proj * Matrix4::look_to_rh(position.into(), Vector3::unit_y(), Vector3::unit_x())).into(),
(proj * Matrix4::look_to_rh(position.into(), -Vector3::unit_y(), -Vector3::unit_x())).into(),
(proj * Matrix4::look_to_rh(position.into(), Vector3::unit_z(), Vector3::unit_y())).into(),
(proj * Matrix4::look_to_rh(position.into(), -Vector3::unit_z(), Vector3::unit_y())).into(),
];
Self {
let mut s = Self {
position,
_padding: 0,
color,
matrices,
..Default::default()
};
s.update_matrices();
s
}
pub fn update_matrices(&mut self) {
let proj = cgmath::perspective(cgmath::Deg(90.0), 1.0, NEAR_PLANE, FAR_PLANE);
self.matrices = [
(proj * Matrix4::look_to_rh(self.position.into(), Vector3::unit_x(), Vector3::unit_y())).into(), // forward
(proj * Matrix4::look_to_rh(self.position.into(), -Vector3::unit_x(), Vector3::unit_y())).into(), // back
(proj * Matrix4::look_to_rh(self.position.into(), Vector3::unit_y(), Vector3::unit_x())).into(), // up
(proj * Matrix4::look_to_rh(self.position.into(), -Vector3::unit_y(), -Vector3::unit_x())).into(), // down
(proj * Matrix4::look_to_rh(self.position.into(), Vector3::unit_z(), Vector3::unit_y())).into(), // right
(proj * Matrix4::look_to_rh(self.position.into(), -Vector3::unit_z(), Vector3::unit_y())).into(), // left
];
}
}
@ -71,7 +74,7 @@ pub trait DrawLight<'a> {
}
impl<'a, 'b> DrawLight<'b> for wgpu::RenderPass<'a>
where
where
'b: 'a,
{
fn draw_light_mesh(

View file

@ -484,6 +484,7 @@ impl State {
self.light_uniform.position =
(cgmath::Quaternion::from_angle_y(cgmath::Deg(90.0 * dt.as_secs_f32())) * old_position)
.into();
self.light_uniform.update_matrices();
self.queue.write_buffer(
&self.light_buffer,
0,