primase_core/
light_falloff.rs1use serde::Serialize;
3
4use crate::{Component, Entity, Geometry, JsonError, World};
5
6#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
11pub struct LightFalloff {
12 pub distance: f32,
14}
15
16impl Component for LightFalloff {
17 const NAME: &'static str = "core.light_falloff";
18 const GEOMETRY: Geometry = Geometry::None;
19 fn to_json(&self) -> Result<Option<serde_json::Value>, JsonError> {
20 crate::json::to_value(self).map(Some)
21 }
22 fn verify(&self, _e: Entity, _w: &World) -> Result<(), String> {
23 self.inverse_distance().map(|_| ())
24 }
25}
26
27impl LightFalloff {
28 pub fn inverse_distance(self) -> Result<f32, String> {
30 if !(self.distance.is_finite() && self.distance > 0. && self.distance.recip().is_finite()) {
31 return Err(format!(
32 "light falloff distance {} is not positive with a finite reciprocal",
33 self.distance
34 ));
35 }
36 Ok(self.distance.recip())
37 }
38}