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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//! Contains different types of light sources.

use gfx;
use object::{Base, Object, ObjectType};
use std::ops;

use camera::Orthographic;
use color::Color;
use hub::{self, Operation, SubLight, SubNode};
use render::{BackendResources, ShadowFormat};
use scene::SyncGuard;

#[derive(Debug)]
pub(crate) enum LightOperation {
    Color(Color),
    Intensity(f32),
}

/// Marks light sources and implements their common methods.
pub trait Light: Object {
    /// Change light color.
    fn set_color(
        &self,
        color: Color,
    ) {
        let msg = Operation::SetLight(LightOperation::Color(color));
        let _ = self.as_ref().tx.send((self.as_ref().node.downgrade(), msg));
    }

    /// Change light intensity.
    fn set_intensity(
        &self,
        intensity: f32,
    ) {
        let msg = Operation::SetLight(LightOperation::Intensity(intensity));
        let _ = self.as_ref().tx.send((self.as_ref().node.downgrade(), msg));
    }
}

impl Light for Ambient {}
impl Light for Directional {}
impl Light for Hemisphere {}
impl Light for Point {}

/// `ShadowMap` is used to render shadows from [`PointLight`](struct.PointLight.html)
/// and [`DirectionalLight`](struct.DirectionalLight.html).
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ShadowMap {
    pub(crate) resource: gfx::handle::ShaderResourceView<BackendResources, f32>,
    pub(crate) target: gfx::handle::DepthStencilView<BackendResources, ShadowFormat>,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum ShadowProjection {
    Orthographic(Orthographic),
}

impl ShadowMap {
    pub(crate) fn to_target(&self) -> gfx::handle::DepthStencilView<BackendResources, ShadowFormat> {
        self.target.clone()
    }

    pub(crate) fn to_resource(&self) -> gfx::handle::ShaderResourceView<BackendResources, f32> {
        self.resource.clone()
    }
}

/// Omni-directional, fixed-intensity and fixed-color light source that affects
/// all objects in the scene equally.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Ambient {
    pub(crate) object: Base,
}

impl Ambient {
    pub(crate) fn new(object: Base) -> Self {
        Ambient { object }
    }
}

impl AsRef<Base> for Ambient {
    fn as_ref(&self) -> &Base { &self.object }
}

impl Object for Ambient {
    type Data = LightData;

    fn resolve_data(&self, sync_guard: &SyncGuard) -> Self::Data {
        match &sync_guard.hub[self].sub_node {
            SubNode::Light(ref light_data) => light_data.into(),
            sub_node @ _ => panic!("`Ambient` had a bad sub node type: {:?}", sub_node),
        }
    }
}

derive_DowncastObject!(Ambient => ObjectType::AmbientLight);

/// The light source that illuminates all objects equally from a given direction,
/// like an area light of infinite size and infinite distance from the scene;
/// there is shading, but cannot be any distance falloff.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Directional {
    pub(crate) object: Base,
}

impl Directional {
    pub(crate) fn new(object: Base) -> Self {
        Directional {
            object,
        }
    }

    /// Adds or updates the shadow map for this light source.
    pub fn set_shadow(
        &mut self,
        map: ShadowMap,
        extent_y: f32,
        range: ops::Range<f32>,
    ) {
        let sp = ShadowProjection::Orthographic(Orthographic {
            center: [0.0; 2].into(),
            extent_y,
            range,
        });
        let msg = Operation::SetShadow(map, sp);
        let _ = self.object.tx.send((self.object.node.downgrade(), msg));
    }
}

impl AsRef<Base> for Directional {
    fn as_ref(&self) -> &Base { &self.object }
}

impl Object for Directional {
    type Data = LightData;

    fn resolve_data(&self, sync_guard: &SyncGuard) -> Self::Data {
        match &sync_guard.hub[self].sub_node {
            SubNode::Light(ref light_data) => light_data.into(),
            sub_node @ _ => panic!("`Directional` had a bad sub node type: {:?}", sub_node),
        }
    }
}

derive_DowncastObject!(Directional => ObjectType::DirectionalLight);

/// `HemisphereLight` uses two different colors in opposite to
/// [`Ambient`](struct.Ambient.html).
///
/// The color of each fragment is determined by direction of normal. If the
/// normal points in the direction of the upper hemisphere, the fragment has
/// color of the "sky". If the direction of the normal is opposite, then fragment
/// takes color of the "ground". In other cases, color is determined as
/// interpolation between colors of upper and lower hemispheres, depending on
/// how much the normal is oriented to the upper and the lower hemisphere.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Hemisphere {
    pub(crate) object: Base,
}

impl Hemisphere {
    pub(crate) fn new(object: Base) -> Self {
        Hemisphere { object }
    }
}

impl AsRef<Base> for Hemisphere {
    fn as_ref(&self) -> &Base { &self.object }
}

impl Object for Hemisphere {
    type Data = HemisphereLightData;

    fn resolve_data(&self, sync_guard: &SyncGuard) -> Self::Data {
        match &sync_guard.hub[self].sub_node {
            SubNode::Light(ref light_data) => light_data.into(),
            sub_node @ _ => panic!("`Hemisphere` had a bad sub node type: {:?}", sub_node),
        }
    }
}

derive_DowncastObject!(Hemisphere => ObjectType::HemisphereLight);

/// Light originates from a single point, and spreads outward in all directions.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Point {
    pub(crate) object: Base,
}

impl Point {
    pub(crate) fn new(object: Base) -> Self {
        Point { object }
    }
}

impl AsRef<Base> for Point {
    fn as_ref(&self) -> &Base { &self.object }
}

impl Object for Point {
    type Data = LightData;

    fn resolve_data(&self, sync_guard: &SyncGuard) -> Self::Data {
        match &sync_guard.hub[self].sub_node {
            SubNode::Light(ref light_data) => light_data.into(),
            sub_node @ _ => panic!("`Point` had a bad sub node type: {:?}", sub_node),
        }
    }
}

derive_DowncastObject!(Point => ObjectType::PointLight);

/// Internal data for [`Ambient`], [`Directional`], and [`Point`] lights.
///
/// [`Ambient`]: ./struct.Ambient.html
/// [`Directional`]: ./struct.Directional.html
/// [`Point`]: ./struct.Point.html
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LightData {
    /// The color of the light.
    pub color: Color,

    /// The intensity of the light.
    pub intensity: f32,
}

impl<'a> From<&'a hub::LightData> for LightData {
    fn from(from: &'a hub::LightData) -> Self {
        LightData {
            color: from.color,
            intensity: from.intensity,
        }
    }
}

/// Internal data for [`Hemisphere`] lights.
///
/// [`Hemisphere`]: ./struct.Hemisphere.html
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct HemisphereLightData {
    /// The ground color of the light.
    pub ground_color: Color,

    /// The sky color of the light.
    pub sky_color: Color,

    /// The intensity of the light.
    pub intensity: f32,
}

impl<'a> From<&'a hub::LightData> for HemisphereLightData {
    fn from(from: &'a hub::LightData) -> Self {
        let ground_color = match from.sub_light {
            SubLight::Hemisphere { ground } => ground,
            _ => panic!("Bad sub-light for `Hemisphere`: {:?}", from.sub_light),
        };
        HemisphereLightData {
            sky_color: from.color,
            ground_color,
            intensity: from.intensity,
        }
    }
}