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
use crate::resources::AmbientColor;
use amethyst_assets::{PrefabData, ProgressCounter};
use amethyst_core::{
ecs::prelude::{Component, DenseVecStorage, Entity, WriteStorage},
math::Vector3,
};
use amethyst_error::Error;
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize, PrefabData)]
#[prefab(Component)]
pub enum Light {
Area,
Directional(DirectionalLight),
Point(PointLight),
Spot(SpotLight),
Sun(SunLight),
}
#[repr(C)]
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct DirectionalLight {
#[serde(with = "crate::serde_shim::srgb")]
pub color: palette::Srgb,
pub intensity: f32,
pub direction: Vector3<f32>,
}
impl Default for DirectionalLight {
fn default() -> Self {
DirectionalLight {
color: Default::default(),
intensity: 1.0,
direction: [-1.0, -1.0, -1.0].into(),
}
}
}
impl From<DirectionalLight> for Light {
fn from(dir: DirectionalLight) -> Self {
Light::Directional(dir)
}
}
#[repr(C)]
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct PointLight {
#[serde(with = "crate::serde_shim::srgb")]
pub color: palette::Srgb,
pub intensity: f32,
pub radius: f32,
pub smoothness: f32,
}
impl Default for PointLight {
fn default() -> Self {
PointLight {
color: Default::default(),
intensity: 10.0,
radius: 10.0,
smoothness: 4.0,
}
}
}
impl From<PointLight> for Light {
fn from(pt: PointLight) -> Self {
Light::Point(pt)
}
}
#[repr(C)]
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct SpotLight {
pub angle: f32,
#[serde(with = "crate::serde_shim::srgb")]
pub color: palette::Srgb,
pub direction: Vector3<f32>,
pub intensity: f32,
pub range: f32,
pub smoothness: f32,
}
impl Default for SpotLight {
fn default() -> Self {
SpotLight {
angle: std::f32::consts::FRAC_PI_3,
color: Default::default(),
direction: [0.0, -1.0, 0.0].into(),
intensity: 10.0,
range: 10.0,
smoothness: 4.0,
}
}
}
impl From<SpotLight> for Light {
fn from(sp: SpotLight) -> Self {
Light::Spot(sp)
}
}
#[repr(C)]
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct SunLight {
pub angle: f32,
#[serde(with = "crate::serde_shim::srgb")]
pub color: palette::Srgb,
pub direction: Vector3<f32>,
pub intensity: f32,
}
impl Default for SunLight {
fn default() -> Self {
SunLight {
angle: 0.0093_f32.to_radians(),
color: Default::default(),
direction: [-1.0, -1.0, -1.0].into(),
intensity: 64_000.0,
}
}
}
impl From<SunLight> for Light {
fn from(sun: SunLight) -> Self {
Light::Sun(sun)
}
}
impl Component for Light {
type Storage = DenseVecStorage<Self>;
}
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize, PrefabData)]
#[serde(default)]
pub struct LightPrefab {
light: Option<Light>,
ambient_color: Option<AmbientColor>,
}