three_d/renderer/effect/
copy.rs

1use crate::renderer::*;
2
3///
4/// Copies the content of the color and/or depth texture by rendering a quad with those textures applied.
5/// The difference from [ScreenEffect] is that this effect does not apply any tone and color mapping specified in the [Viewer].
6///
7#[derive(Clone, Debug, Default)]
8pub struct CopyEffect {
9    /// Defines which channels (red, green, blue, alpha and depth) to copy.
10    pub write_mask: WriteMask,
11    /// Defines which type of blending to use when writing the copied color to the render target.
12    pub blend: Blend,
13}
14
15impl Effect for CopyEffect {
16    fn fragment_shader_source(
17        &self,
18        _lights: &[&dyn crate::Light],
19        color_texture: Option<ColorTexture>,
20        depth_texture: Option<DepthTexture>,
21    ) -> String {
22        format!(
23            "{}{}
24
25            in vec2 uvs;
26            layout (location = 0) out vec4 outColor;
27
28            void main()
29            {{
30                {}
31                {}
32            }}
33
34        ",
35            color_texture
36                .map(|t| t.fragment_shader_source())
37                .unwrap_or("".to_string()),
38            depth_texture
39                .map(|t| t.fragment_shader_source())
40                .unwrap_or("".to_string()),
41            color_texture
42                .map(|_| "
43                    outColor = sample_color(uvs);"
44                    .to_string())
45                .unwrap_or("".to_string()),
46            depth_texture
47                .map(|_| "gl_FragDepth = sample_depth(uvs);".to_string())
48                .unwrap_or("".to_string()),
49        )
50    }
51
52    fn id(
53        &self,
54        color_texture: Option<ColorTexture>,
55        depth_texture: Option<DepthTexture>,
56    ) -> EffectMaterialId {
57        EffectMaterialId::CopyEffect(color_texture, depth_texture)
58    }
59
60    fn use_uniforms(
61        &self,
62        program: &Program,
63        _viewer: &dyn Viewer,
64        _lights: &[&dyn crate::Light],
65        color_texture: Option<ColorTexture>,
66        depth_texture: Option<DepthTexture>,
67    ) {
68        if let Some(color_texture) = color_texture {
69            color_texture.use_uniforms(program);
70        }
71        if let Some(depth_texture) = depth_texture {
72            depth_texture.use_uniforms(program);
73        }
74    }
75
76    fn render_states(&self) -> RenderStates {
77        RenderStates {
78            depth_test: DepthTest::Always,
79            cull: Cull::Back,
80            write_mask: self.write_mask,
81            blend: self.blend,
82        }
83    }
84}