mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-15 23:02:14 +00:00
Environment lights: review fixes — edit keeps Classic 3D, NaN sun guarded (#2436)
Three small fixes from the review of the shadow commit: - light_settings switched a Classic 3D comp to Advanced 3D whenever the edited light was an Environment light (the resulting kind), so editing the intensity of one in a comp the user had switched back to Classic 3D dragged the renderer back. The switch now happens only on the non-Environment to Environment transition (the sources before and after), matching the docs; ordinary edits leave the renderer to the user. - dominant_direction's thresholds let NaN through (every NaN comparison is false): a NaN or infinite texel produced a NaN sun direction and NaN shading with it. A non-finite weight or length now returns no direction — garbage casts no shadow. - add_environment walked the full-resolution image for the dominant direction even when the light casts no shadows, where nothing reads it. Only a shadow-casting light pays for the pass now. Tests: editing an existing Environment light in a Classic 3D comp keeps the renderer and stays toast-silent; a NaN and an infinite environment image yield no sun direction.
This commit is contained in:
1 parent
863f0e49c1
commit
0539098887
4 files changed
+31
-12
No files matched your search
@@ -294,11 +294,12 @@ fn light_settings(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
l.name = n.clone();
|
||||
}
|
||||
apply_light(&mut l, p, t, "layer.lightSettings")?;
|
||||
// The resulting kind (kept or changed to): an Environment light in a Classic 3D comp
|
||||
// switches it, like adding one does.
|
||||
let env = matches!(l.source, LayerSource::Light { kind: LightKind::Environment });
|
||||
// A light that becomes an Environment light switches a Classic 3D comp to Advanced 3D,
|
||||
// like adding one does; editing one that already is leaves the renderer to the user.
|
||||
let was_env = matches!(old.source, LayerSource::Light { kind: LightKind::Environment });
|
||||
let is_env = matches!(l.source, LayerSource::Light { kind: LightKind::Environment });
|
||||
*layer_mut(proj, cid, lid)? = l;
|
||||
Ok(if env { use_advanced_3d(proj, cid)? } else { false })
|
||||
Ok(if !was_env && is_env { use_advanced_3d(proj, cid)? } else { false })
|
||||
})?;
|
||||
report_switch(s, cid, switched, "which Environment lights need to light the scene");
|
||||
Ok(Value::Null)
|
||||
|
||||
@@ -186,10 +186,14 @@ fn environment_lights_switch_classic_comps_to_advanced_3d() {
|
||||
s.execute("layer.newLight", json!({"kind": "Point"})).unwrap();
|
||||
assert_eq!(s.active_comp().unwrap().renderer, Renderer::Classic3D);
|
||||
// Switching back to Classic 3D with an Environment light in the comp explains itself.
|
||||
s.execute("layer.newLight", json!({"kind": "Environment"})).unwrap();
|
||||
let env = s.execute("layer.newLight", json!({"kind": "Environment"})).unwrap()["layer"].as_u64().unwrap();
|
||||
toasts(&mut s);
|
||||
s.execute("comp.renderer", json!({"renderer": "classic3d"})).unwrap();
|
||||
assert!(toasts(&mut s).iter().any(|m| m.contains("Environment light")), "the Classic 3D limit is explained");
|
||||
// Editing that Environment light (no type change) leaves the renderer the user chose.
|
||||
s.execute("layer.lightSettings", json!({"layer": env, "intensity": 80})).unwrap();
|
||||
assert_eq!(s.active_comp().unwrap().renderer, Renderer::Classic3D, "an ordinary edit doesn't re-switch");
|
||||
assert!(toasts(&mut s).is_empty(), "and doesn't toast either");
|
||||
}
|
||||
|
||||
/// An Environment light takes Casts Shadows (with Shadow Darkness and Diffusion) like every
|
||||
|
||||
@@ -174,7 +174,8 @@ pub struct EnvInfo {
|
||||
pub rotation: f32,
|
||||
/// Unit direction from the scene towards the environment's dominant (radiance-weighted)
|
||||
/// bright region — the direction its shadow is cast from, like a parallel light's
|
||||
/// `to_light`. `[0; 3]` when the image is directionless (uniform): no shadow then.
|
||||
/// `to_light`. `[0; 3]` when the image is directionless (uniform) or the light casts no
|
||||
/// shadow (nothing reads it then): no shadow.
|
||||
pub sun: [f32; 3],
|
||||
/// Shadow map index into [`Scene::shadows`]: 0 marked (assigned by `raster::shadow_maps`),
|
||||
/// -1 casts no shadow. Gated by the light's Cast Shadows.
|
||||
@@ -815,7 +816,7 @@ pub fn irradiance_map(sw: u32, sh: u32, src: &[[f32; 4]], w: u32, h: u32) -> Vec
|
||||
/// (solid-angle weighted, rotation applied), or `[0; 3]` when the image is directionless
|
||||
/// (a uniform environment cancels to zero). The mean of a single bright spot on black points
|
||||
/// straight at it; a gradient points somewhere between its ends.
|
||||
fn dominant_direction(w: u32, h: u32, d: &[[f32; 4]], rot: f32) -> [f32; 3] {
|
||||
pub(crate) fn dominant_direction(w: u32, h: u32, d: &[[f32; 4]], rot: f32) -> [f32; 3] {
|
||||
use super::shade::equirect_dir;
|
||||
let (mut x, mut y, mut z) = (0.0f32, 0.0f32, 0.0f32);
|
||||
let mut weight = 0.0f32;
|
||||
@@ -830,9 +831,10 @@ fn dominant_direction(w: u32, h: u32, d: &[[f32; 4]], rot: f32) -> [f32; 3] {
|
||||
z += dir[2] * lum;
|
||||
weight += lum.abs();
|
||||
}
|
||||
// Under 1 % directional, the image is uniform to the eye: no dominant region to shadow from.
|
||||
// Under 1 % directional, the image is uniform to the eye: no dominant region to shadow
|
||||
// from. A non-finite sum (a NaN or infinite texel) is garbage, not a direction: none then.
|
||||
let len = (x * x + y * y + z * z).sqrt();
|
||||
if weight <= 1e-9 || len < weight * 0.01 {
|
||||
if !weight.is_finite() || !len.is_finite() || weight <= 1e-9 || len < weight * 0.01 {
|
||||
return [0.0; 3];
|
||||
}
|
||||
[x / len, y / len, z / len]
|
||||
@@ -850,8 +852,9 @@ pub(crate) fn add_environment(
|
||||
shadow_darkness: f64,
|
||||
shadow_diffusion: f64,
|
||||
) {
|
||||
// The shadow direction from the full-resolution image, before the mip chain averages it.
|
||||
let sun = dominant_direction(w, h, &data, rotation);
|
||||
// The shadow direction from the full-resolution image, before the mip chain averages it —
|
||||
// only a shadow-casting light reads it, so only one pays for the pass.
|
||||
let sun = if casts_shadows { dominant_direction(w, h, &data, rotation) } else { [0.0; 3] };
|
||||
// Radiance level 0 at most 1024 wide.
|
||||
let (mut w, mut h, mut d) = (w, h, data);
|
||||
while w > 1024 {
|
||||
|
||||
@@ -2,7 +2,7 @@ use effectcraft_keyframe::Value;
|
||||
use effectcraft_project::{Comp, ItemId, ItemKind, LayerSource, LightKind, PrimitiveKind, Project, Renderer as R3, build};
|
||||
use effectcraft_time::{FrameRate, Tick};
|
||||
|
||||
use super::scene::{Material, Scene, TexInfo, Vertex, irradiance_map};
|
||||
use super::scene::{Material, Scene, TexInfo, Vertex, dominant_direction, irradiance_map};
|
||||
use super::shade::{self, PI};
|
||||
use super::*;
|
||||
use crate::{NoFootage, RenderOpts, Renderer};
|
||||
@@ -67,6 +67,17 @@ fn constant_environment_has_constant_irradiance() {
|
||||
assert!(irr[16][0] > 0.9 && irr[15 * 32 + 16][0] < 0.1, "{:?} {:?}", irr[16], irr[15 * 32 + 16]);
|
||||
}
|
||||
|
||||
/// A NaN or infinite texel poisons the sums (every NaN comparison is false, so the thresholds
|
||||
/// alone would pass it through as a NaN direction); an infinite one even where sin θ = 0 turns
|
||||
/// into NaN. Either way the sum is garbage, not a direction: no sun, so no shadow (#2436).
|
||||
#[test]
|
||||
fn a_non_finite_environment_has_no_sun_direction() {
|
||||
let nan = vec![[f32::NAN; 4]; 8 * 4];
|
||||
assert_eq!(dominant_direction(8, 4, &nan, 0.0), [0.0; 3]);
|
||||
let inf = vec![[f32::INFINITY, 0.0, 0.0, 1.0]; 8 * 4];
|
||||
assert_eq!(dominant_direction(8, 4, &inf, 0.0), [0.0; 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn equirect_round_trip_and_env_brdf() {
|
||||
for (u, v) in [(0.1f32, 0.2f32), (0.5, 0.5), (0.9, 0.7)] {
|
||||
|
||||
Reference in new issue
Block a user