three_d/renderer/effect/
fxaa.rs

1use crate::renderer::*;
2
3///
4/// A simple anti-aliasing approach which smooths otherwise jagged edges (for example lines) but also
5/// smooths the rest of the image.
6///
7#[derive(Clone, Default, Debug)]
8pub struct FxaaEffect {}
9
10impl Effect for FxaaEffect {
11    fn fragment_shader_source(
12        &self,
13        _lights: &[&dyn Light],
14        color_texture: Option<ColorTexture>,
15        _depth_texture: Option<DepthTexture>,
16    ) -> String {
17        let color_texture =
18            color_texture.expect("Must supply a color texture to apply a fxaa effect");
19        format!(
20            "{}\n{}",
21            color_texture.fragment_shader_source(),
22            include_str!("shaders/fxaa_effect.frag")
23        )
24    }
25
26    fn id(
27        &self,
28        color_texture: Option<ColorTexture>,
29        _depth_texture: Option<DepthTexture>,
30    ) -> EffectMaterialId {
31        EffectMaterialId::FxaaEffect(
32            color_texture.expect("Must supply a color texture to apply a fxaa effect"),
33        )
34    }
35
36    fn use_uniforms(
37        &self,
38        program: &Program,
39        _viewer: &dyn Viewer,
40        _lights: &[&dyn Light],
41        color_texture: Option<ColorTexture>,
42        _depth_texture: Option<DepthTexture>,
43    ) {
44        let color_texture =
45            color_texture.expect("Must supply a color texture to apply a fxaa effect");
46        let w = color_texture.width();
47        let h = color_texture.height();
48        color_texture.use_uniforms(program);
49        program.use_uniform("resolution", vec2(w as f32, h as f32));
50    }
51
52    fn render_states(&self) -> RenderStates {
53        RenderStates {
54            write_mask: WriteMask::COLOR,
55            depth_test: DepthTest::Always,
56            cull: Cull::Back,
57            ..Default::default()
58        }
59    }
60}