mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
Merge pull request #4356 from dotCooCoo/fix/raster3d-inverse-square-falloff
craft-raster3d: keep inverse-square falloff at or below 1
This commit is contained in:
4 files changed
+39
-6
No files matched your search
@@ -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 256 subsamples is averaged in `f64`. EffectCraft summed every block
|
||||
in `f32`, which dropped the low bits of each sample as the sum grew (a 1024 × 1024 block of 0.7
|
||||
resolved to 0.6927), and its `f32` count of covered subsamples stopped at 2^24, which raised the
|
||||
|
||||
@@ -124,6 +124,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
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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]];
|
||||
|
||||
Reference in new issue
Block a user