Get light pos directly from matrices, debugging shadowmaps
This commit is contained in:
parent
2dc20ad12c
commit
5e0001e2e1
4 changed files with 49 additions and 20 deletions
|
@ -13,5 +13,5 @@ fn vs_main(
|
||||||
);
|
);
|
||||||
|
|
||||||
let world_position = model_matrix * vec4<f32>(model.position, 1.0);
|
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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,6 +82,31 @@ fn sample_direct_light(index: i32, light_coords: vec4<f32>) -> f32 {
|
||||||
index,
|
index,
|
||||||
light_coords.z * proj_correction
|
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
|
@fragment
|
||||||
|
@ -100,8 +125,8 @@ fn fs_main(vert: VertexOutput) -> @location(0) vec4<f32> {
|
||||||
var total_radiance: vec3<f32>;
|
var total_radiance: vec3<f32>;
|
||||||
|
|
||||||
var in_light = 0.0;
|
var in_light = 0.0;
|
||||||
for (var i: i32 = 0; i < 6; i++) {
|
for (var i: i32 = 0; i < 1; i++) {
|
||||||
in_light = sample_direct_light(i, light.matrices[i] * (vert.world_position - vec4<f32>(light.position, 0.0)));
|
in_light = sample_direct_light(i, light.matrices[i] * vert.world_position);
|
||||||
if (in_light > 0.0) {
|
if (in_light > 0.0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ use super::{
|
||||||
use cgmath::{Matrix4, Vector3};
|
use cgmath::{Matrix4, Vector3};
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
#[derive(Debug, Default, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||||
pub struct LightUniform {
|
pub struct LightUniform {
|
||||||
pub position: [f32; 3],
|
pub position: [f32; 3],
|
||||||
_padding: u32,
|
_padding: u32,
|
||||||
|
@ -18,23 +18,26 @@ pub struct LightUniform {
|
||||||
|
|
||||||
impl LightUniform {
|
impl LightUniform {
|
||||||
pub fn new(position: [f32; 3], color: [f32; 4]) -> Self {
|
pub fn new(position: [f32; 3], color: [f32; 4]) -> Self {
|
||||||
let proj = cgmath::perspective(cgmath::Deg(90.0), 1.0, NEAR_PLANE, FAR_PLANE);
|
let mut s = Self {
|
||||||
#[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 {
|
|
||||||
position,
|
position,
|
||||||
_padding: 0,
|
_padding: 0,
|
||||||
color,
|
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,8 +74,8 @@ pub trait DrawLight<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> DrawLight<'b> for wgpu::RenderPass<'a>
|
impl<'a, 'b> DrawLight<'b> for wgpu::RenderPass<'a>
|
||||||
where
|
where
|
||||||
'b: 'a,
|
'b: 'a,
|
||||||
{
|
{
|
||||||
fn draw_light_mesh(
|
fn draw_light_mesh(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|
|
@ -484,6 +484,7 @@ impl State {
|
||||||
self.light_uniform.position =
|
self.light_uniform.position =
|
||||||
(cgmath::Quaternion::from_angle_y(cgmath::Deg(90.0 * dt.as_secs_f32())) * old_position)
|
(cgmath::Quaternion::from_angle_y(cgmath::Deg(90.0 * dt.as_secs_f32())) * old_position)
|
||||||
.into();
|
.into();
|
||||||
|
self.light_uniform.update_matrices();
|
||||||
self.queue.write_buffer(
|
self.queue.write_buffer(
|
||||||
&self.light_buffer,
|
&self.light_buffer,
|
||||||
0,
|
0,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue