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
//! The unit the world's lights were lowered from (`core.light_unit`, a world resource).
use serde::Serialize;
use serde_json::Value;
use crate::{JsonError, Resource, World};
/// What one unit of the source's own value of a point or spot light is in candela: the calibration the lowering that
/// made the world's point and spot `core.light`s chose (a directional light's intensity, in lux, is not in it). Such a
/// light's intensity is its source value times `candela_per_unit`, so `colour * intensity / candela_per_unit` is the
/// value the source stored, the one a program of the source reads its lights in ([`crate::shader::LightValue::Colour`]). The lowering that makes the lights writes it; a world whose lights no
/// lowering made (a scene read in candela) has none, and a light table's colours have no value there.
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
pub struct LightUnit {
/// Candela per unit of the source's light value; finite and above 0.
pub candela_per_unit: f32,
}
impl Resource for LightUnit {
const NAME: &'static str = "core.light_unit";
fn to_json(&self) -> Result<Option<Value>, JsonError> {
crate::json::to_value(self).map(Some)
}
/// A finite factor above 0: a light of the source's value 1 has some intensity.
fn verify(&self, _w: &World) -> Result<(), String> {
if !(self.candela_per_unit.is_finite() && self.candela_per_unit > 0.0) {
return Err(format!(
"candela per unit {} is not finite and above 0",
self.candela_per_unit
));
}
Ok(())
}
}
/// What one unit of the source's own value of a directional light (its sun) is in lux: the calibration the lowering that
/// made the scene's sun (`core.environment`'s) chose. The sun's intensity is its source value times `lux_per_unit`, so
/// `color * intensity / lux_per_unit` is the value the source stored, the one a program of the source reads its sun in
/// ([`crate::shader::FrameParameter::LightColour`]). The lowering that makes the sun writes it; a world whose sun no
/// lowering made has none.
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
pub struct DirectionalLightUnit {
/// Lux per unit of the source's directional light value; finite and above 0.
pub lux_per_unit: f32,
}
impl Resource for DirectionalLightUnit {
const NAME: &'static str = "core.directional_light_unit";
fn to_json(&self) -> Result<Option<Value>, JsonError> {
crate::json::to_value(self).map(Some)
}
/// A finite factor above 0: a sun of the source's value 1 has some intensity.
fn verify(&self, _w: &World) -> Result<(), String> {
if !(self.lux_per_unit.is_finite() && self.lux_per_unit > 0.0) {
return Err(format!(
"lux per unit {} is not finite and above 0",
self.lux_per_unit
));
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
/// The directional unit is held to the same: a finite factor above 0.
#[test]
fn the_directional_unit_is_a_finite_factor_above_zero() {
let w = World::new();
for bad in [0.0, -1.0, f32::NAN, f32::INFINITY] {
assert!(
DirectionalLightUnit { lux_per_unit: bad }.verify(&w).is_err(),
"{bad}"
);
}
assert_eq!(DirectionalLightUnit { lux_per_unit: 2.0 }.verify(&w), Ok(()));
}
/// A factor of 0, a negative one or a non-finite one is refused; any finite one above 0 is kept.
#[test]
fn the_unit_is_a_finite_factor_above_zero() {
let w = World::new();
for bad in [0.0, -1.0, f32::NAN, f32::INFINITY] {
assert!(
LightUnit {
candela_per_unit: bad
}
.verify(&w)
.is_err(),
"{bad}"
);
}
for good in [1.0, 0.001, 25.0] {
assert_eq!(
LightUnit {
candela_per_unit: good
}
.verify(&w),
Ok(())
);
}
}
}