1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use crate::core::*;
use crate::renderer::*;
use std::sync::Arc;

/// The background of the scene.
#[derive(Clone)]
pub enum Background {
    /// Environnment texture.
    Texture(Arc<TextureCubeMap>),
    /// Background color.
    Color(Srgba),
}

impl Default for Background {
    fn default() -> Self {
        Self::Color(Srgba::WHITE)
    }
}

///
/// An effect that simulates a water surface and should therefore only be applied to a water surface geometry.
/// This effect needs the rendered scene (without the water surface) in a color and depth texture to be able to add reflections and refractions.
///
#[derive(Clone)]
pub struct WaterEffect {
    /// The background of the scene which is used for reflections.
    pub background: Background,
    /// A value in the range `[0..1]` specifying how metallic the surface is.
    pub metallic: f32,
    /// A value in the range `[0..1]` specifying how rough the surface is.
    pub roughness: f32,
    /// The lighting model used when rendering this effect
    pub lighting_model: LightingModel,
}

impl Effect for WaterEffect {
    fn fragment_shader_source(
        &self,
        lights: &[&dyn Light],
        color_texture: Option<ColorTexture>,
        depth_texture: Option<DepthTexture>,
    ) -> String {
        format!(
            "{}\n{}\n{}\n{}\n{}\n{}\n{}",
            match &self.background {
                Background::Color(_) => "",
                Background::Texture(_) => "#define USE_BACKGROUND_TEXTURE",
            },
            color_texture
                .expect("Must supply a color texture to apply a water effect")
                .fragment_shader_source(),
            depth_texture
                .expect("Must supply a depth texture to apply a water effect")
                .fragment_shader_source(),
            lights_shader_source(lights, self.lighting_model),
            ToneMapping::fragment_shader_source(),
            ColorMapping::fragment_shader_source(),
            include_str!("shaders/water_effect.frag")
        )
    }

    fn id(&self, color_texture: Option<ColorTexture>, depth_texture: Option<DepthTexture>) -> u16 {
        0b1u16 << 14
            | 0b1u16 << 12
            | 0b1u16 << 11
            | color_texture
                .expect("Must supply a color texture to apply a water effect")
                .id()
            | depth_texture
                .expect("Must supply a depth texture to apply a water effect")
                .id()
    }

    fn fragment_attributes(&self) -> FragmentAttributes {
        FragmentAttributes {
            position: true,
            normal: true,
            uv: true,
            ..FragmentAttributes::NONE
        }
    }

    fn render_states(&self) -> RenderStates {
        RenderStates {
            blend: Blend::TRANSPARENCY,
            ..Default::default()
        }
    }

    fn use_uniforms(
        &self,
        program: &Program,
        camera: &Camera,
        lights: &[&dyn Light],
        color_texture: Option<ColorTexture>,
        depth_texture: Option<DepthTexture>,
    ) {
        camera.tone_mapping.use_uniforms(program);
        camera.color_mapping.use_uniforms(program);
        color_texture
            .expect("Must supply a color texture to apply a water effect")
            .use_uniforms(program);
        depth_texture
            .expect("Must supply a depth texture to apply a water effect")
            .use_uniforms(program);
        for (i, light) in lights.iter().enumerate() {
            light.use_uniforms(program, i as u32);
        }
        program.use_uniform("viewProjection", camera.projection() * camera.view());
        program.use_uniform(
            "viewProjectionInverse",
            (camera.projection() * camera.view()).invert().unwrap(),
        );
        program.use_uniform("cameraPosition", camera.position());
        program.use_uniform(
            "screenSize",
            vec2(
                camera.viewport().width as f32,
                camera.viewport().height as f32,
            ),
        );
        program.use_uniform("metallic", self.metallic);
        program.use_uniform("roughness", self.roughness);
        match &self.background {
            Background::Color(color) => {
                program.use_uniform("environmentColor", color.to_linear_srgb())
            }
            Background::Texture(tex) => program.use_texture_cube("environmentMap", tex),
        }
    }
}

impl Default for WaterEffect {
    fn default() -> Self {
        Self {
            background: Background::default(),
            metallic: 0.0,
            roughness: 1.0,
            lighting_model: LightingModel::Blinn,
        }
    }
}