From 340da93ecea410e479959578edae51be805e686f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 14:30:50 -0800 Subject: [PATCH 1/2] craft-raster3d: keep inverse-square falloff at or below 1 A clamped inverse-square light with a radius between 0 and 1 lit fully up to its radius and then used (1 / distance)^2, so it was brighter than full intensity between the radius and 1 unit. The plateau and the falloff now use the same radius, at least 1, and the `Light::radius` docs say so. --- crates/raster3d/src/scene.rs | 3 +++ crates/raster3d/src/shade.rs | 9 ++++----- crates/raster3d/tests/it/bounds.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/crates/raster3d/src/scene.rs b/crates/raster3d/src/scene.rs index 9667561ac1..ba8efd56e9 100644 --- a/crates/raster3d/src/scene.rs +++ b/crates/raster3d/src/scene.rs @@ -123,6 +123,9 @@ pub struct Light { pub cos_outer: f32, /// 0 none, 1 smooth, 2 inverse square clamped. Other values return [`RenderError::BadLight`]. pub falloff: u32, + /// For point and spot lights with falloff 1 or 2, the distance factor stays at 1 within the radius. + /// With falloff 2, a radius below 1 counts as 1; beyond that radius, the distance factor is (radius / distance)². + /// Falloff 0 ignores the radius. pub radius: f32, pub falloff_distance: f32, /// First shadow map (point lights, and spot lights whose outer cone's half angle is over 1.40 diff --git a/crates/raster3d/src/shade.rs b/crates/raster3d/src/shade.rs index 1af5b737f8..364bf10edf 100644 --- a/crates/raster3d/src/shade.rs +++ b/crates/raster3d/src/shade.rs @@ -521,11 +521,10 @@ fn attenuation(l: &Light, p: [f32; 3], to_l: [f32; 3], dist: f32) -> f32 { } } 2 if l.kind != 0 => { - if dist <= l.radius || (l.radius <= 0.0 && dist <= 1.0) { - 1.0 - } else { - (l.radius.max(1.0) / dist).powi(2) - } + // The plateau and the inverse square use the same radius, at least 1, so the + // factor never exceeds 1. + let r = l.radius.max(1.0); + if dist <= r { 1.0 } else { (r / dist).powi(2) } } _ => 1.0, }; diff --git a/crates/raster3d/tests/it/bounds.rs b/crates/raster3d/tests/it/bounds.rs index b73dcc7f35..fcca73aea1 100644 --- a/crates/raster3d/tests/it/bounds.rs +++ b/crates/raster3d/tests/it/bounds.rs @@ -1390,6 +1390,32 @@ fn a_point_light_whose_squared_distance_overflows_f32_still_lights() { assert!((far.data[px][0] - near.data[px][0]).abs() < 1e-3 * near.data[px][0], "{:?} {:?}", far.data[px], near.data[px]); } +#[test] +fn inverse_square_falloff_with_a_radius_below_one_never_brightens_the_light() { + // A clamped inverse-square point light 0.75 units from the pixel center (4.5, 4.5) of a quad. + // With a radius of 0.5 it lights the pixel as fully as a radius of 1 does, and past 1 unit + // both fall off the same. + let mut s = quad_scene(8, 8, &[([0.0, 0.0, 8.0, 8.0], 500.0, WHITE)]); + s.materials[0].unlit = false; + let mut point = spot([4.5, 4.5, 499.25], [0.0, 0.0, 1.0], 1.0, 1.0); + (point.kind, point.falloff, point.shadow) = (2, 2, -1); + s.lights = vec![point]; + let px = 4 * 8 + 4; + let mut lit = Vec::new(); + for z in [499.25, 497.0] { + s.lights[0].pos[2] = z; + s.lights[0].radius = 1.0; + let (one, _) = try_render_cpu(&s, &Limits::default()).unwrap(); + s.lights[0].radius = 0.5; + let (half, _) = try_render_cpu(&s, &Limits::default()).unwrap(); + assert!(one.data[px][0] > 0.01, "{z}: {:?}", one.data[px]); + assert_eq!(bits4(&half.data), bits4(&one.data), "{z}"); + lit.push(one.data[px][0]); + } + // 3 units away the light falls off to (1 / 3)² of its full intensity. + assert!((lit[1] * 9.0 - lit[0]).abs() < 1e-3 * lit[0], "{lit:?}"); +} + #[test] fn coordinates_from_two_to_the_twenty_third_texels_keep_the_half_texel_shift() { let texels = [[0.0, 0.0, 0.0, 1.0], [1.0, 0.0, 0.0, 1.0]]; From 091ebbd78a0dff6ac3cd784606314a8993cc5f56 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 30 Nov 2026 09:36:29 +0700 Subject: [PATCH 2/2] raster3d: list the inverse-square radius change in the README --- crates/raster3d/README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/raster3d/README.md b/crates/raster3d/README.md index 4e0e9600ed..3e45b41e71 100644 --- a/crates/raster3d/README.md +++ b/crates/raster3d/README.md @@ -144,7 +144,8 @@ lights' outer cones are over 1.40 radians wide, where resolve blocks hold more t subsamples, where shadows-only materials do not receive shadows, where fragments under discarded opaque pixels leave the depth range, where blended casters have no base texture, where opaque-mode and masked casters have an opacity below 0.5, where casters reach within 1 unit of -light-space depth of a spot or point light, where an encoded color is negative, and where a +light-space depth of a spot or point light, where an inverse-square point or spot light's radius is +between 0 and 1, where an encoded color is negative, and where a texture is sampled between large finite texels of opposite signs. Halfway between red values of `3e38` and `−3e38`, for example, the sampler here returns red 0 and EffectCraft's interpolation overflows `f32`. @@ -198,6 +199,10 @@ Changes from those files: - A spot light whose outer cone's half angle plus 0.05 radians is over 1.45 radians gets six maps, as a point light does. EffectCraft gave it one map 1.45 radians wide, so receivers in the rest of the cone were not shadowed. +- An inverse-square point or spot light whose radius is between 0 and 1 takes a radius of 1 for both + its full-strength plateau and its falloff, so its distance factor never exceeds 1. EffectCraft kept + the plateau at the radius and divided 1 by the distance beyond it, so a fragment between the radius + and 1 unit from the light took more than the light's full strength. - A resolve block of more than 2^24 subsamples is averaged in `f64`. EffectCraft's `f32` count of covered subsamples stopped at 2^24, which raised the mean depth. - A clipped corner whose `1/w` overflows `f32` (a clip transform scaled near `f32`'s smallest