From 7b752703ea8cad4176db24a704ddf6162c92da31 Mon Sep 17 00:00:00 2001 From: nullprop Date: Fri, 10 Nov 2023 01:50:30 +0200 Subject: [PATCH] Tweak fog light falloff & density --- res/shaders/constants.wgsl | 2 +- res/shaders/fog.wgsl | 22 +++++++++++----------- src/core/pass.rs | 5 ++++- src/core/state.rs | 8 ++++++-- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/res/shaders/constants.wgsl b/res/shaders/constants.wgsl index b584400..298919c 100644 --- a/res/shaders/constants.wgsl +++ b/res/shaders/constants.wgsl @@ -8,6 +8,6 @@ const INV_SHADOW_SAMPLES = 1.0 / 25.0; const FOG_MAX_STEPS = 20; const FOG_MAX_DIST = 300.0; const FOG_SCALE = 0.01; -const FOG_DENSITY = 1.0; +const FOG_DENSITY = 2.0; const FOG_ALPHA = 1.0; const FOG_BLEND_DIST = 10.0; diff --git a/res/shaders/fog.wgsl b/res/shaders/fog.wgsl index 67fb450..510da2f 100644 --- a/res/shaders/fog.wgsl +++ b/res/shaders/fog.wgsl @@ -63,7 +63,7 @@ var s_roughness_metalness: sampler; fn fog_noise(pos: vec3) -> f32 { var p = pos * FOG_SCALE; p.x += global_uniforms.time * 0.01; - p.y += global_uniforms.time * 0.1; + p.y += global_uniforms.time * 0.2; p.z += sin(global_uniforms.time * 0.1) * 0.1; return fbm(p); } @@ -112,12 +112,12 @@ fn fs_main(vert: FogVertexOutput) -> @location(0) vec4 { let distance_to_volume = length(cam_to_volume); let direction = cam_to_volume / distance_to_volume; // FIXME: t_geometry_depth is 0 -// let geometry_depth = scene_depth(vert.clip_position) - distance_to_volume; -// if (geometry_depth <= 0.0) -// { -// return vec4(0.0); -// } - let geometry_depth = 3000.0; + var geometry_depth = scene_depth(vert.clip_position) - distance_to_volume; + geometry_depth = 3000.0; + if (geometry_depth <= 0.0) + { + return vec4(0.0); + } let density = ray_march(vert.world_position.xyz, direction, geometry_depth); var in_light = 0.0; @@ -149,16 +149,16 @@ fn fs_main(vert: FogVertexOutput) -> @location(0) vec4 { in_light = 1.0; } - var color = vec3(0.5, 0.5, 0.5); + var base_color = vec3(mix(0.8, 0.4, density)); let ambient_strength = 0.02; - let ambient_color = color * ambient_strength; + let ambient_color = base_color * ambient_strength; var radiance = vec3(0.0); if (in_light > 0.0) { // attenuation let light_dist = length(light.position - vert.world_position.xyz); - let coef_a = 0.0; - let coef_b = 1.0; + let coef_a = 2.0; + let coef_b = 4.0; let light_attenuation = 1.0 / (1.0 + coef_a * light_dist + coef_b * light_dist * light_dist); radiance = light.color.rgb * light.color.a * light_attenuation * in_light; diff --git a/src/core/pass.rs b/src/core/pass.rs index 2b07e01..4ad60e0 100644 --- a/src/core/pass.rs +++ b/src/core/pass.rs @@ -21,6 +21,7 @@ impl RenderPass { is_shadow: bool, has_transparency: bool, write_depth: bool, + cull_mode: Option, ) -> Self { let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some((label.to_owned() + " pipeline Layout").as_str()), @@ -42,6 +43,7 @@ impl RenderPass { is_shadow, has_transparency, write_depth, + cull_mode, ); Self { pipeline } @@ -58,6 +60,7 @@ impl RenderPass { is_shadow: bool, has_transparency: bool, write_depth: bool, + cull_mode: Option, ) -> wgpu::RenderPipeline { let shader = device.create_shader_module(shader); @@ -101,7 +104,7 @@ impl RenderPass { topology: wgpu::PrimitiveTopology::TriangleList, strip_index_format: None, front_face: wgpu::FrontFace::Ccw, - cull_mode: Some(wgpu::Face::Back), + cull_mode: cull_mode, // Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE polygon_mode: wgpu::PolygonMode::Fill, // Requires Features::DEPTH_CLIP_CONTROL diff --git a/src/core/state.rs b/src/core/state.rs index 33d17a4..f605f2d 100644 --- a/src/core/state.rs +++ b/src/core/state.rs @@ -156,7 +156,7 @@ impl State { config.width, config.height, 1, - wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, true, ); @@ -445,6 +445,7 @@ impl State { true, false, true, + Some(wgpu::Face::Back), ); let geometry_pass = RenderPass::new( @@ -464,6 +465,7 @@ impl State { false, false, true, + Some(wgpu::Face::Back), ); let light_debug_pass = RenderPass::new( @@ -478,6 +480,7 @@ impl State { false, false, true, + Some(wgpu::Face::Back), ); let fog_pass = RenderPass::new( @@ -497,6 +500,7 @@ impl State { false, true, false, + Some(wgpu::Face::Back), ); Self { @@ -548,7 +552,7 @@ impl State { self.config.width, self.config.height, 1, - wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, true, ); }