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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use crate::core::*;
use crate::renderer::light::*;
use crate::renderer::*;
pub struct DirectionalLight {
context: Context,
shadow_texture: Option<DepthTargetTexture2D>,
shadow_matrix: Mat4,
pub intensity: f32,
pub color: Color,
pub direction: Vec3,
}
impl DirectionalLight {
pub fn new(
context: &Context,
intensity: f32,
color: Color,
direction: &Vec3,
) -> ThreeDResult<DirectionalLight> {
Ok(DirectionalLight {
context: context.clone(),
shadow_matrix: Mat4::identity(),
shadow_texture: None,
intensity,
color,
direction: *direction,
})
}
#[deprecated]
#[allow(missing_docs)]
pub fn set_color(&mut self, color: Color) {
self.color = color;
}
#[deprecated]
#[allow(missing_docs)]
pub fn color(&self) -> Color {
self.color
}
#[deprecated]
#[allow(missing_docs)]
pub fn set_intensity(&mut self, intensity: f32) {
self.intensity = intensity;
}
#[deprecated]
#[allow(missing_docs)]
pub fn intensity(&self) -> f32 {
self.intensity
}
#[deprecated]
#[allow(missing_docs)]
pub fn set_direction(&mut self, direction: &Vec3) {
self.direction = *direction;
}
#[deprecated]
#[allow(missing_docs)]
pub fn direction(&self) -> Vec3 {
self.direction
}
pub fn clear_shadow_map(&mut self) {
self.shadow_texture = None;
self.shadow_matrix = Mat4::identity();
}
pub fn generate_shadow_map(
&mut self,
texture_size: u32,
geometries: &[&dyn Geometry],
) -> ThreeDResult<()> {
let up = compute_up_direction(self.direction);
let viewport = Viewport::new_at_origo(texture_size, texture_size);
let mut aabb = AxisAlignedBoundingBox::EMPTY;
for geometry in geometries {
aabb.expand_with_aabb(&geometry.aabb());
}
if aabb.is_empty() {
return Ok(());
}
let target = aabb.center();
let position = target - aabb.max().distance(aabb.min()) * self.direction;
let z_far = aabb.distance_max(&position);
let z_near = aabb.distance(&position);
let frustum_height = aabb.max().distance(aabb.min());
let shadow_camera = Camera::new_orthographic(
&self.context,
viewport,
position,
target,
up,
frustum_height,
z_near,
z_far,
)?;
let mut shadow_texture = DepthTargetTexture2D::new(
&self.context,
texture_size,
texture_size,
Wrapping::ClampToEdge,
Wrapping::ClampToEdge,
DepthFormat::Depth32F,
)?;
let depth_material = DepthMaterial {
render_states: RenderStates {
write_mask: WriteMask::DEPTH,
..Default::default()
},
..Default::default()
};
shadow_texture.write(Some(1.0), || {
for geometry in geometries
.iter()
.filter(|g| shadow_camera.in_frustum(&g.aabb()))
{
geometry.render_with_material(&depth_material, &shadow_camera, &[])?;
}
Ok(())
})?;
self.shadow_texture = Some(shadow_texture);
self.shadow_matrix = shadow_matrix(&shadow_camera);
Ok(())
}
pub fn shadow_map(&self) -> Option<&DepthTargetTexture2D> {
self.shadow_texture.as_ref()
}
}
impl Light for DirectionalLight {
fn shader_source(&self, i: u32) -> String {
if self.shadow_texture.is_some() {
format!(
"
uniform sampler2D shadowMap{};
uniform mat4 shadowMVP{};
uniform vec3 color{};
uniform vec3 direction{};
vec3 calculate_lighting{}(vec3 surface_color, vec3 position, vec3 normal, vec3 view_direction, float metallic, float roughness, float occlusion)
{{
return calculate_light(color{}, -direction{}, surface_color, view_direction, normal, metallic, roughness)
* calculate_shadow(shadowMap{}, shadowMVP{}, position);
}}
", i, i, i, i, i, i, i, i, i)
} else {
format!(
"
uniform vec3 color{};
uniform vec3 direction{};
vec3 calculate_lighting{}(vec3 surface_color, vec3 position, vec3 normal, vec3 view_direction, float metallic, float roughness, float occlusion)
{{
return calculate_light(color{}, -direction{}, surface_color, view_direction, normal, metallic, roughness);
}}
", i, i, i, i, i)
}
}
fn use_uniforms(&self, program: &Program, i: u32) -> ThreeDResult<()> {
if let Some(ref tex) = self.shadow_texture {
program.use_depth_texture(&format!("shadowMap{}", i), tex)?;
program.use_uniform(&format!("shadowMVP{}", i), &self.shadow_matrix)?;
}
program.use_uniform(
&format!("color{}", i),
&(self.color.to_vec3() * self.intensity),
)?;
program.use_uniform(&format!("direction{}", i), &self.direction.normalize())?;
Ok(())
}
}