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
use amethyst_core::cgmath::{Deg, Matrix4, Ortho, PerspectiveFov};
use amethyst_core::specs::prelude::{Component, Entity};
use amethyst_core::specs::storage::HashMapStorage;
#[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,
}