From c0f41045dafe1a2c25b2976464732704db7804c6 Mon Sep 17 00:00:00 2001 From: nullprop Date: Fri, 10 Nov 2023 20:09:20 +0200 Subject: [PATCH] Fix fog scene depth comparison --- res/shaders/fog.wgsl | 22 ++++++++-------------- src/core/camera.rs | 5 +++-- src/core/state.rs | 6 ++---- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/res/shaders/fog.wgsl b/res/shaders/fog.wgsl index 304f146..bded66e 100644 --- a/res/shaders/fog.wgsl +++ b/res/shaders/fog.wgsl @@ -85,23 +85,18 @@ fn depth_to_linear(depth: f32) -> f32 { @fragment fn fs_main(vert: FogVertexOutput) -> @location(0) vec4 { - let cam_to_volume = vert.world_position.xyz - camera.position.xyz; - var distance_to_volume = length(cam_to_volume); - let direction = cam_to_volume / distance_to_volume; - // FIXME: geom depth should always be greater than the volume surface depth... - // why is this broken? - distance_to_volume = depth_to_linear(vert.clip_position.z); - let uv = vert.clip_position.xy / camera.planes.xy; - let depth = textureSample(t_geometry_depth, s_geometry_depth, uv); - let geometry_depth = depth_to_linear(depth) - distance_to_volume; - if (geometry_depth <= 0.0) + let direction = normalize(vert.world_position.xyz - camera.position.xyz); + let volume_depth = depth_to_linear(vert.clip_position.z); + let uv = vert.clip_position.xy / camera.planes.zw; + let geometry_depth = depth_to_linear(textureSample(t_geometry_depth, s_geometry_depth, uv)); + let max_fog_depth = geometry_depth - volume_depth; + if (max_fog_depth <= 0.0) { return vec4(0.0); } - return vec4(1.0); - /* - let density = ray_march(vert.world_position.xyz, direction, geometry_depth); + let density = ray_march(vert.world_position.xyz, direction, max_fog_depth); +// return vec4(1.0, 1.0, 1.0, density); var in_light = 0.0; if (global_uniforms.use_shadowmaps > 0u) { @@ -153,5 +148,4 @@ fn fs_main(vert: FogVertexOutput) -> @location(0) vec4 { result = result / (result + vec3(1.0)); return vec4(result, density * FOG_ALPHA); - */ } diff --git a/src/core/camera.rs b/src/core/camera.rs index c6c5320..fc8c09e 100644 --- a/src/core/camera.rs +++ b/src/core/camera.rs @@ -118,11 +118,11 @@ impl CameraUniform { proj: cgmath::Matrix4::identity().into(), inv_view_proj: cgmath::Matrix4::identity().into(), position: [0.0; 4], - planes: [NEAR_PLANE, FAR_PLANE, 0.0, 0.0], + planes: [NEAR_PLANE, FAR_PLANE, 1.0, 1.0], } } - pub fn update(&mut self, camera: &Camera) { + pub fn update(&mut self, camera: &Camera, config: &wgpu::SurfaceConfiguration) { let view = camera.get_view_matrix(); let proj = camera.projection.get_matrix(); self.view = view.into(); @@ -130,6 +130,7 @@ impl CameraUniform { let inv_view_proj = (proj * view).invert().unwrap(); self.inv_view_proj = inv_view_proj.into(); self.position = camera.position.to_homogeneous().into(); + self.planes = [NEAR_PLANE, FAR_PLANE, config.width as f32, config.height as f32]; } } diff --git a/src/core/state.rs b/src/core/state.rs index 26dc507..1623730 100644 --- a/src/core/state.rs +++ b/src/core/state.rs @@ -45,7 +45,6 @@ pub struct State { fog_instances: Vec, fog_instance_buffer: wgpu::Buffer, geometry_depth_texture: Texture, - light_depth_texture: Texture, geom_model: Model, fog_model: Model, light_model: Model, @@ -118,7 +117,7 @@ impl State { ); let mut camera_uniform = CameraUniform::new(); - camera_uniform.update(&camera); + camera_uniform.update(&camera, &config); let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Camera Buffer"), @@ -500,7 +499,6 @@ impl State { fog_instances, fog_instance_buffer, geometry_depth_texture, - light_depth_texture, geom_model, fog_model, light_model, @@ -575,7 +573,7 @@ impl State { // Update camera self.camera.update(dt, &self.camera_controller); self.camera_controller.reset(false); - self.camera_uniform.update(&self.camera); + self.camera_uniform.update(&self.camera, &self.config); self.queue.write_buffer( &self.camera_buffer, 0,