diff --git a/examples/fog/src/main.rs b/examples/fog/src/main.rs index 2dd310df1..bee60d11a 100644 --- a/examples/fog/src/main.rs +++ b/examples/fog/src/main.rs @@ -44,7 +44,7 @@ pub async fn run() { // Fog let mut fog_effect = FogEffect { color: Srgba::new_opaque(200, 200, 200), - density: 0.2, + density: 0.1, animation: 0.1, ..Default::default() }; @@ -88,7 +88,7 @@ pub async fn run() { if camera.viewport().width != color_texture.width() || camera.viewport().height != color_texture.height() { - color_texture = Texture2D::new_empty::<[u8; 4]>( + color_texture = Texture2D::new_empty::<[f16; 4]>( &context, camera.viewport().width, camera.viewport().height, @@ -121,13 +121,6 @@ pub async fn run() { if change { camera.tone_mapping = ToneMapping::default(); camera.target_color_space = ColorSpace::Srgb; - frame_input.screen().apply_screen_effect( - &CopyEffect::default(), - &camera, - &[], - Some(ColorTexture::Single(&color_texture)), - Some(DepthTexture::Single(&depth_texture)), - ); if fog_enabled { fog_effect.time = frame_input.accumulated_time as f32; @@ -135,7 +128,15 @@ pub async fn run() { &fog_effect, &camera, &[], - None, + Some(ColorTexture::Single(&color_texture)), + Some(DepthTexture::Single(&depth_texture)), + ); + } else { + frame_input.screen().apply_screen_effect( + &CopyEffect::default(), + &camera, + &[], + Some(ColorTexture::Single(&color_texture)), Some(DepthTexture::Single(&depth_texture)), ); } diff --git a/src/renderer/effect/fog.rs b/src/renderer/effect/fog.rs index e3534fd70..4ef88efc0 100644 --- a/src/renderer/effect/fog.rs +++ b/src/renderer/effect/fog.rs @@ -49,23 +49,31 @@ impl Effect for FogEffect { fn fragment_shader_source( &self, _lights: &[&dyn Light], - _color_texture: Option, + color_texture: Option, depth_texture: Option, ) -> String { format!( - "{}\n{}\n{}", + "{}\n{}\n{}\n{}\n{}\n{}", include_str!("../../core/shared.frag"), + color_texture + .expect("Must supply a depth texture to apply a fog effect") + .fragment_shader_source(), depth_texture .expect("Must supply a depth texture to apply a fog effect") .fragment_shader_source(), + ToneMapping::fragment_shader_source(), + ColorSpace::fragment_shader_source(), include_str!("shaders/fog_effect.frag") ) } - fn id(&self, _color_texture: Option, depth_texture: Option) -> u16 { + fn id(&self, color_texture: Option, depth_texture: Option) -> u16 { 0b1u16 << 14 | 0b1u16 << 13 | 0b1u16 << 12 + | color_texture + .expect("Must supply a color texture to apply a fog effect") + .id() | depth_texture .expect("Must supply a depth texture to apply a fog effect") .id() @@ -83,9 +91,14 @@ impl Effect for FogEffect { program: &Program, camera: &Camera, _lights: &[&dyn Light], - _color_texture: Option, + color_texture: Option, depth_texture: Option, ) { + camera.tone_mapping.use_uniforms(program); + camera.target_color_space.use_uniforms(program); + color_texture + .expect("Must supply a color texture to apply a fog effect") + .use_uniforms(program); depth_texture .expect("Must supply a depth texture to apply a fog effect") .use_uniforms(program); @@ -102,8 +115,7 @@ impl Effect for FogEffect { fn render_states(&self) -> RenderStates { RenderStates { - write_mask: WriteMask::COLOR, - blend: Blend::TRANSPARENCY, + depth_test: DepthTest::Always, cull: Cull::Back, ..Default::default() } diff --git a/src/renderer/effect/lighting_pass.rs b/src/renderer/effect/lighting_pass.rs index 5283c5c71..25ef05a91 100644 --- a/src/renderer/effect/lighting_pass.rs +++ b/src/renderer/effect/lighting_pass.rs @@ -59,7 +59,11 @@ impl Effect for LightingPassEffect { } fn render_states(&self) -> RenderStates { - RenderStates::default() + RenderStates { + depth_test: DepthTest::Always, + cull: Cull::Back, + ..Default::default() + } } } diff --git a/src/renderer/effect/shaders/fog_effect.frag b/src/renderer/effect/shaders/fog_effect.frag index 5770cbf63..592ec6a5b 100644 --- a/src/renderer/effect/shaders/fog_effect.frag +++ b/src/renderer/effect/shaders/fog_effect.frag @@ -9,7 +9,7 @@ uniform vec3 eyePosition; in vec2 uvs; -layout (location = 0) out vec4 color; +layout (location = 0) out vec4 outColor; // @@ -127,6 +127,7 @@ float snoise(vec3 v) // factor: 1 == full fog, 0 == no fog void main() { + vec4 color = sample_color(uvs); float depth = sample_depth(uvs); vec3 pos = world_pos_from_depth(viewProjectionInverse, depth, uvs); @@ -142,5 +143,8 @@ void main() factor = clamp(factor, 0., 1.); // Output - color = vec4(fogColor.rgb, factor); + outColor = mix(color, fogColor, factor); + outColor.rgb = tone_mapping(outColor.rgb); + outColor.rgb = color_mapping(outColor.rgb); + gl_FragDepth = depth; } \ No newline at end of file