Skip to content

Commit

Permalink
Light: Remove redundant total_rays.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid committed Jan 16, 2025
1 parent 46a38a2 commit 86c4c72
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions all-is-cubes/src/space/light/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,6 @@ struct LightBuffer {
/// Accumulator of incoming light encountered.
/// TODO: Make this a vector of `f32` to save NaN checks?
incoming_light: Rgb,
/// Number of rays contributing to `incoming_light`.
total_rays: usize,
/// Number of rays, weighted by the ray angle versus local cube faces.
total_ray_weight: f32,
/// Cubes whose lighting value contributed to the `incoming_light` value.
Expand Down Expand Up @@ -658,7 +656,6 @@ impl LightBuffer {
fn new() -> Self {
Self {
incoming_light: Rgb::ZERO,
total_rays: 0,
total_ray_weight: 0.0,
dependencies: Vec::new(),
cost: 0,
Expand Down Expand Up @@ -810,21 +807,18 @@ impl LightBuffer {
}
}

/// Add the given color to the sum counting it as having the given weight,
/// as if it was an entire ray's contribution
/// (that is, incrementing `self.total_rays`).
/// Add the given color to the sum counting it as having the given weight.
fn add_weighted_light(&mut self, color: Rgb, weight: f32) {
self.incoming_light += color * weight;
self.total_rays += 1;
self.total_ray_weight += weight;
}

/// Return the [`PackedLight`] value accumulated here
fn finish(&self, origin_is_opaque: bool) -> PackedLight {
// if total_rays is zero then incoming_light is zero so the result will be zero.
// if total_ray_weight is zero then incoming_light is zero so the result will be zero.
// We just need to avoid dividing by zero.
let scale = PositiveSign::<f32>::new_clamped(1.0 / self.total_ray_weight.max(1.0));
let new_light_value: PackedLight = if self.total_rays > 0 {
let new_light_value: PackedLight = if self.total_ray_weight > 0.0 {
PackedLight::some(self.incoming_light * scale)
} else if origin_is_opaque {
PackedLight::OPAQUE
Expand Down

0 comments on commit 86c4c72

Please sign in to comment.