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
use crate::{
camera::{ActiveCamera, Camera},
pod::{self, IntoPod},
resources::AmbientColor,
};
use amethyst_core::{
ecs::{Join, Read, ReadStorage, SystemData, World},
math::{convert, Matrix4, Vector3},
transform::Transform,
};
use glsl_layout::*;
#[cfg(feature = "profiler")]
use thread_profiler::profile_scope;
type Std140<T> = <T as AsStd140>::Std140;
#[derive(Debug)]
pub struct CameraGatherer {
pub camera_position: vec3,
pub projview: Std140<pod::ViewArgs>,
}
impl CameraGatherer {
pub fn gather(world: &World) -> Self {
#[cfg(feature = "profiler")]
profile_scope!("gather_cameras");
let (active_camera, cameras, transforms) = <(
Read<'_, ActiveCamera>,
ReadStorage<'_, Camera>,
ReadStorage<'_, Transform>,
)>::fetch(world);
let defcam = Camera::standard_2d(1.0, 1.0);
let identity = Transform::default();
let (camera, transform) = active_camera
.entity
.as_ref()
.and_then(|ac| {
cameras
.get(*ac)
.map(|camera| (camera, transforms.get(*ac).unwrap_or(&identity)))
})
.unwrap_or_else(|| {
(&cameras, &transforms)
.join()
.next()
.unwrap_or((&defcam, &identity))
});
let camera_position =
convert::<_, Vector3<f32>>(transform.global_matrix().column(3).xyz()).into_pod();
let proj: [[f32; 4]; 4] = (*camera.as_matrix()).into();
let view: [[f32; 4]; 4] = convert::<_, Matrix4<f32>>(transform.global_view_matrix()).into();
let projview = pod::ViewArgs {
proj: proj.into(),
view: view.into(),
}
.std140();
Self {
camera_position,
projview,
}
}
}
#[derive(Debug)]
pub struct AmbientGatherer;
impl AmbientGatherer {
pub fn gather(world: &World) -> vec3 {
let ambient_color = <Option<Read<'_, AmbientColor>>>::fetch(world);
ambient_color.map_or([0.0, 0.0, 0.0].into(), |c| {
let (r, g, b, _) = c.0.into_components();
[r, g, b].into()
})
}
}