diff --git a/res/shaders/depth.wgsl b/res/shaders/depth.wgsl index 6e9bd66..d990c4a 100644 --- a/res/shaders/depth.wgsl +++ b/res/shaders/depth.wgsl @@ -13,5 +13,5 @@ fn vs_main( ); let world_position = model_matrix * vec4(model.position, 1.0); - return light.matrices[light_matrix_index] * (world_position - vec4(light.position, 0.0)); + return light.matrices[light_matrix_index] * world_position; } diff --git a/res/shaders/pbr.wgsl b/res/shaders/pbr.wgsl index ef83088..812c541 100644 --- a/res/shaders/pbr.wgsl +++ b/res/shaders/pbr.wgsl @@ -82,6 +82,31 @@ fn sample_direct_light(index: i32, light_coords: vec4) -> 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 { var total_radiance: vec3; 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(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; } diff --git a/src/core/light.rs b/src/core/light.rs index bfd2f89..618ca6e 100644 --- a/src/core/light.rs +++ b/src/core/light.rs @@ -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,8 +74,8 @@ pub trait DrawLight<'a> { } impl<'a, 'b> DrawLight<'b> for wgpu::RenderPass<'a> -where - 'b: 'a, + where + 'b: 'a, { fn draw_light_mesh( &mut self, diff --git a/src/core/state.rs b/src/core/state.rs index d4d1390..eabe75e 100644 --- a/src/core/state.rs +++ b/src/core/state.rs @@ -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,