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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use std::mem::{self, Discriminant};
use rin_math::{Vec2, vec2, Vec4, vec4};
use rin_util::{Result, Error};
use rinecs::{Component, OneToNComponent};
use serde_derive::{Serialize, Deserialize};

#[derive(Component, Clone, Copy, PartialEq, Debug, Serialize, Deserialize)]
pub enum Type{
    PCSS,
    Poisson{
        soft_scatter: f32
    },
    Gaussian3,
    Gaussian5,
    Gaussian7,
    Hard,
}

impl Type{
    pub fn to_str(&self) -> &'static str{
        match self{
            Type::PCSS => "SHADOW_MAP_PCSS",
            Type::Poisson{..} => "SHADOW_MAP_POISON_DISK",
            Type::Gaussian3 | Type::Gaussian5 | Type::Gaussian7 => "SHADOW_MAP_GAUSSIAN",
            Type::Hard => "SHADOW_MAP_HARD",
        }
    }

    pub fn discriminant_to_str(ty: Discriminant<Type>) -> &'static str{
        if ty == mem::discriminant(&Type::PCSS) { "SHADOW_MAP_PCSS" }
        else if ty == mem::discriminant(&Type::Poisson{soft_scatter: 0.}) { "SHADOW_MAP_POISON_DISK" }
        else if ty == mem::discriminant(&Type::Gaussian3)
            || ty == mem::discriminant(&Type::Gaussian5)
            || ty == mem::discriminant(&Type::Gaussian7) { "SHADOW_MAP_GAUSSIAN" }
        else if ty == mem::discriminant(&Type::Hard) { "SHADOW_MAP_HARD" }
        else { unreachable!() }
    }
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum Resolution{
    _16,
    _24,
    _32,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Parameters{
    pub map_size: u16,
    pub resolution: Resolution,
    pub left: f32,
    pub right: f32,
    pub bottom: f32,
    pub top: f32,
    pub near_clip: f32,
    pub far_clip: f32,
    pub bias: f32,
    pub depth_clamp: bool,
}

impl Parameters{
    pub fn frustum_size(&self) -> Vec2{
        vec2(self.right - self.left, self.top - self.bottom)
    }
}

#[derive(OneToNComponent, Debug, Copy, Clone, Serialize, Deserialize)]
#[autochanges]
pub struct Map{
    parameters: Parameters,
    next_parameters: Option<Parameters>,
    update_frequency: usize,
    last_updated: usize,
    changed: bool,
}

impl Map{
    pub fn new(parameters: Parameters) -> Map {
        Map{
            parameters,
            next_parameters: None,
            update_frequency: 1,
            last_updated: 0,
            changed: true,
        }
    }

    pub fn new_with_update_frequency(parameters: Parameters, update_frequency: usize) -> Map {
        Map{
            parameters,
            next_parameters: None,
            update_frequency,
            last_updated: 0,
            changed: true,
        }
    }

    pub fn parameters(&self) -> &Parameters{
        &self.parameters
    }

    pub fn set_parameters(&mut self, params: Parameters){
        if self.update_frequency == 1{
            self.changed = true;
            self.parameters = params;
        }else{
            self.next_parameters = Some(params)
        }
    }

    pub fn update_frequency(&self) -> usize{
        self.update_frequency
    }

    pub fn set_update_frequency(&mut self, update_frequency: usize){
        self.changed = true;
        self.update_frequency = update_frequency;
    }

    pub fn update(&mut self){
        if self.update_frequency == 0{
            return;
        }

        if self.last_updated == 0 {
            self.last_updated = self.update_frequency;
            if let Some(next_parameters) = self.next_parameters.take(){
                self.parameters = next_parameters;
                self.changed = true;
            }
        }
        self.last_updated -= 1;
    }

    pub fn needs_update(&self) -> bool{
        self.update_frequency != 0 && self.last_updated == self.update_frequency - 1
    }
}

impl rinecs::Changes for Map{
    fn has_changed(&self) -> bool{
        self.changed
    }

    fn reset_changed(&mut self){
        self.changed = false;
    }
}

impl std::ops::Deref for Map{
    type Target = Parameters;
    fn deref(&self) -> &Parameters{
        self.parameters()
    }
}

impl std::ops::DerefMut for Map{
    fn deref_mut(&mut self) -> &mut Parameters{
        if self.update_frequency == 1 {
            &mut self.parameters
        }else{
            if self.next_parameters.is_none(){
                self.next_parameters = Some(self.parameters.clone())
            }
            self.next_parameters.as_mut().unwrap()
        }
    }
}

#[derive(OneToNComponent, Debug, Copy, Clone, Serialize, Deserialize)]
#[autochanges]
pub struct StaticMap{
    parameters: Parameters,
    changed: bool,
}

impl StaticMap{
    pub fn new(parameters: Parameters) -> StaticMap {
        StaticMap{
            parameters,
            changed: true,
        }
    }

    pub fn parameters(&self) -> &Parameters{
        &self.parameters
    }

    pub fn set_parameters(&mut self, params: Parameters){
        self.changed = true;
        self.parameters = params;
    }
}

impl rinecs::Changes for StaticMap{
    fn has_changed(&self) -> bool{
        self.changed
    }

    fn reset_changed(&mut self){
        self.changed = false;
    }
}

impl std::ops::Deref for StaticMap{
    type Target = Parameters;
    fn deref(&self) -> &Parameters{
        &self.parameters
    }
}

impl std::ops::DerefMut for StaticMap{
    fn deref_mut(&mut self) -> &mut Parameters{
        &mut self.parameters
    }
}

#[derive(Component, Debug, Copy, Clone, Serialize, Deserialize)]
pub struct Cascades{
    starts: Vec4,
    ends: Vec4,
    debug: bool,
}

impl Cascades{
    pub fn new(cascades: Vec<(f32,f32)>) -> Result<Cascades>{
        if cascades.len() <= 4 {
            let far = cascades.last().map(|(_,e)| *e).unwrap_or(0.);
            Ok(Cascades{
                starts: vec4(
                    cascades.get(0).map(|(s,_)| *s).unwrap_or(far),
                    cascades.get(1).map(|(s,_)| *s).unwrap_or(far),
                    cascades.get(2).map(|(s,_)| *s).unwrap_or(far),
                    cascades.get(3).map(|(s,_)| *s).unwrap_or(far),
                ),
                ends: vec4(
                    cascades.get(0).map(|(_,e)| *e).unwrap_or(far),
                    cascades.get(1).map(|(_,e)| *e).unwrap_or(far),
                    cascades.get(2).map(|(_,e)| *e).unwrap_or(far),
                    cascades.get(3).map(|(_,e)| *e).unwrap_or(far),
                ),
                debug: false,
            })
        }else{
            Err(Error::new("Only a maximum of 4 shadow cascades allowed"))
        }
    }

    pub fn starts(&self) -> Vec4{
        self.starts
    }

    pub fn ends(&self) -> Vec4{
        self.ends
    }

    pub fn enable_debug(&mut self){
        self.debug = true;
    }

    pub fn disable_debug(&mut self){
        self.debug = true;
    }

    pub fn debug_enabled(&self) -> bool{
        self.debug
    }
}

#[derive(Component, Debug, Copy, Clone, Serialize, Deserialize)]
pub struct StaticCascades{
    starts: Vec4,
    ends: Vec4,
    debug: bool,
}

impl StaticCascades{
    pub fn new(cascades: Vec<(f32,f32)>) -> Result<StaticCascades>{
        if cascades.len() <= 4 {
            let far = cascades.last().map(|(_,e)| *e).unwrap_or(0.);
            Ok(StaticCascades{
                starts: vec4(
                    cascades.get(0).map(|(s,_)| *s).unwrap_or(far),
                    cascades.get(1).map(|(s,_)| *s).unwrap_or(far),
                    cascades.get(2).map(|(s,_)| *s).unwrap_or(far),
                    cascades.get(3).map(|(s,_)| *s).unwrap_or(far),
                ),
                ends: vec4(
                    cascades.get(0).map(|(_,e)| *e).unwrap_or(far),
                    cascades.get(1).map(|(_,e)| *e).unwrap_or(far),
                    cascades.get(2).map(|(_,e)| *e).unwrap_or(far),
                    cascades.get(3).map(|(_,e)| *e).unwrap_or(far),
                ),
                debug: false,
            })
        }else{
            Err(Error::new("Only a maximum of 4 shadow cascades allowed"))
        }
    }

    pub fn starts(&self) -> Vec4{
        self.starts
    }

    pub fn ends(&self) -> Vec4{
        self.ends
    }

    pub fn enable_debug(&mut self){
        self.debug = true;
    }

    pub fn disable_debug(&mut self){
        self.debug = true;
    }

    pub fn debug_enabled(&self) -> bool{
        self.debug
    }
}