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
use crate::core::*;
pub struct FogEffect {
pub color: Color,
pub density: f32,
pub animation: f32,
image_effect: ImageEffect,
}
impl FogEffect {
pub fn new(
context: &Context,
color: Color,
density: f32,
animation: f32,
) -> ThreeDResult<FogEffect> {
Ok(FogEffect {
color,
density,
animation,
image_effect: ImageEffect::new(
context,
&format!(
"{}{}",
include_str!("../../core/shared.frag"),
include_str!("shaders/fog.frag")
),
)?,
})
}
pub fn apply(
&self,
camera: &Camera,
depth_texture: &DepthTargetTexture2D,
time: f32,
) -> ThreeDResult<()> {
let render_states = RenderStates {
write_mask: WriteMask::COLOR,
blend: Blend::TRANSPARENCY,
cull: Cull::Back,
..Default::default()
};
self.image_effect
.use_depth_texture("depthMap", depth_texture)?;
self.image_effect.use_uniform(
"viewProjectionInverse",
(camera.projection() * camera.view()).invert().unwrap(),
)?;
self.image_effect.use_uniform("fogColor", self.color)?;
self.image_effect.use_uniform("fogDensity", self.density)?;
self.image_effect.use_uniform("animation", self.animation)?;
self.image_effect.use_uniform("time", 0.001 * time)?;
self.image_effect
.use_uniform("eyePosition", camera.position())?;
self.image_effect.apply(render_states, camera.viewport())?;
Ok(())
}
}