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
use amethyst_assets::{PrefabData, PrefabError};
use amethyst_core::cgmath::{Deg, Matrix4, Ortho, PerspectiveFov};
use amethyst_core::specs::prelude::{Component, Entity, HashMapStorage, Write, WriteStorage};
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub enum Projection {
Orthographic(Ortho<f32>),
Perspective(PerspectiveFov<f32>),
}
impl Projection {
pub fn orthographic(l: f32, r: f32, t: f32, b: f32) -> Projection {
Projection::Orthographic(Ortho {
left: l,
right: r,
top: t,
bottom: b,
near: 0.1,
far: 2000.0,
})
}
pub fn perspective<D: Into<Deg<f32>>>(aspect: f32, fov: D) -> Projection {
Projection::Perspective(PerspectiveFov {
fovy: fov.into().into(),
aspect,
near: 0.1,
far: 2000.0,
})
}
}
impl From<Projection> for Matrix4<f32> {
fn from(proj: Projection) -> Self {
match proj {
Projection::Orthographic(ortho) => ortho.into(),
Projection::Perspective(perspective) => perspective.into(),
}
}
}
impl From<Projection> for Camera {
fn from(proj: Projection) -> Self {
Self { proj: proj.into() }
}
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Camera {
pub proj: Matrix4<f32>,
}
impl Camera {
pub fn standard_2d() -> Self {
Self::from(Projection::orthographic(-1., 1., 1., -1.))
}
pub fn standard_3d(width: f32, height: f32) -> Self {
use amethyst_core::cgmath::Deg;
Self::from(Projection::perspective(width / height, Deg(60.)))
}
}
impl Component for Camera {
type Storage = HashMapStorage<Self>;
}
#[derive(Clone, Debug, PartialEq)]
pub struct ActiveCamera {
pub entity: Entity,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum CameraPrefab {
Orthographic(Ortho<f32>),
Perspective(PerspectiveFov<f32>),
Matrix(Matrix4<f32>),
}
impl<'a> PrefabData<'a> for CameraPrefab {
type SystemData = WriteStorage<'a, Camera>;
type Result = ();
fn load_prefab(
&self,
entity: Entity,
storage: &mut Self::SystemData,
_: &[Entity],
) -> Result<(), PrefabError> {
let proj = match *self {
CameraPrefab::Matrix(mat) => mat,
CameraPrefab::Orthographic(ortho) => ortho.into(),
CameraPrefab::Perspective(perspective) => perspective.into(),
};
storage.insert(entity, Camera { proj }).map(|_| ())
}
}
pub struct ActiveCameraPrefab(usize);
impl<'a> PrefabData<'a> for ActiveCameraPrefab {
type SystemData = (Option<Write<'a, ActiveCamera>>,);
type Result = ();
fn load_prefab(
&self,
_: Entity,
system_data: &mut Self::SystemData,
entities: &[Entity],
) -> Result<(), PrefabError> {
if let Some(ref mut cam) = system_data.0 {
cam.entity = entities[self.0];
}
Ok(())
}
}