authoravatarclover caruso<git@paperclover.net>2026-12-01 01:37:13-08:00
committeravatarclover caruso<git@paperclover.net>2026-12-01 02:52:53-08:00
logc4030bd0198b8eb23a1da88a00856845ab41304b
tree74e68e0d3d7cd3e4ce1a5cad7697a49a26b9338f
parent7090cf87ebf69321576c2ab7da746e79d2b31799
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Honor Lighting.IsShadowCaster and MtlStdInputs.ReceivesShadows

Casters with the flag off are skipped in the shadow pass; receivers with it off (geometry MtlStdInputs, or the Mtl shader node when one is attached) skip the cube-map lookup. Assisted-by: claude-sonnet-5.5

4 files changed, 66 insertions(+), 5 deletions(-)

crates/engine/src/eval/text_3d.rs+4
......@@ -605,6 +605,8 @@ impl Engine {
605605 hash_floats.push(ms.specular_intensity);
606606 hash_floats.push(ms.specular_exponent);
607607 hash_floats.push(if ms.affected_by_lights { 1.0 } else { 0.0 });
608 hash_floats.push(if ms.casts_shadow { 1.0 } else { 0.0 });
609 hash_floats.push(if ms.receives_shadows { 1.0 } else { 0.0 });
608610 hash_floats.push(if ms.flip_normals { 1.0 } else { 0.0 });
609611 if let Some((_, sc, bi, ch)) = disp_entry {
610612 hash_floats.push(sc);
......@@ -620,6 +622,8 @@ impl Engine {
620622 specular_intensity: ms.specular_intensity,
621623 specular_exponent: ms.specular_exponent,
622624 affected_by_lights: ms.affected_by_lights,
625 casts_shadow: ms.casts_shadow,
626 receives_shadows: ms.receives_shadows,
623627 flip_normals: ms.flip_normals,
624628 displace: None,
625629 });
crates/engine/src/scene3d/collect.rs+12-1
......@@ -14,6 +14,7 @@ use model::{Comp, ExprEngine, Node, NodeId};
1414struct SceneOverrides {
1515 material_override: Option<NodeId>,
1616 affected_by_lights: Option<bool>,
17 casts_shadow: Option<bool>,
1718 flip_normals: bool,
1819 displace: Option<Displace3DSpec>,
1920 cull_back: bool,
......@@ -333,7 +334,10 @@ fn collect_scene_inner(
333334 .or_else(|| node.connected_node("SceneInput"))
334335 .and_then(|s| comp.find_node(s))
335336 });
336 let mat = resolve_material(comp, mat_node, time, expr, visited);
337 let mut mat = resolve_material(comp, mat_node, time, expr, visited);
338 if !mat.has_material_shader {
339 mat.receives_shadows = comp.eval_number(node, "MtlStdInputs.ReceivesShadows", time, 1.0, expr) > 0.5;
340 }
337341 meshes.push(make_mesh_instance(world, ShapeKind::ImagePlane, mat, None, &own));
338342 }
339343 "Shape3D" => {
......@@ -485,6 +489,7 @@ struct ResolvedMaterial {
485489 fx: MaterialFx,
486490 /// A bare texture operator used as a surface's material renders flat, ignoring lights.
487491 unlit: bool,
492 receives_shadows: bool,
488493 /// True when resolved from a shader material node (`MtlBlinn` / `MtlPhong` / `MtlCookTorrance`),
489494 /// which owns diffuse/specular rather than inheriting the geometry node's `MtlStdInputs`.
490495 has_material_shader: bool,
......@@ -500,6 +505,7 @@ impl Default for ResolvedMaterial {
500505 specular_exponent: 50.0,
501506 fx: MaterialFx::default(),
502507 unlit: false,
508 receives_shadows: true,
503509 has_material_shader: false,
504510 }
505511 }
......@@ -525,6 +531,8 @@ fn make_mesh_instance(
525531 specular_intensity: mat.specular_intensity,
526532 specular_exponent: mat.specular_exponent,
527533 affected_by_lights: !mat.unlit && ovr.affected_by_lights.unwrap_or(true),
534 casts_shadow: ovr.casts_shadow.unwrap_or(true),
535 receives_shadows: mat.receives_shadows,
528536 flip_normals: ovr.flip_normals,
529537 displace: ovr.displace.clone(),
530538 }
......@@ -543,6 +551,7 @@ fn surface_scope(
543551 let flag = |k: &str, d: f64| comp.eval_number(node, &format!("{group}.{k}"), time, d, expr) > 0.5;
544552 flag("Visibility.IsVisible", 1.0).then(|| SceneOverrides {
545553 affected_by_lights: ovr.affected_by_lights.or(Some(flag("Lighting.IsAffectedByLights", 1.0))),
554 casts_shadow: Some(flag("Lighting.IsShadowCaster", 1.0)),
546555 cull_back: flag("Visibility.CullBackFace", 0.0),
547556 cull_front: flag("Visibility.CullFrontFace", 0.0),
548557 ..ovr.clone()
......@@ -572,6 +581,7 @@ fn apply_mtl_std(mat: &mut ResolvedMaterial, comp: &Comp, node: &Node, prefix: &
572581 mat.specular_col = [f("Specular.Color.Red", 1.0), f("Specular.Color.Green", 1.0), f("Specular.Color.Blue", 1.0)];
573582 mat.specular_intensity = f("Specular.Intensity", 1.0).clamp(0.0, 4.0);
574583 mat.specular_exponent = f("Specular.Exponent", 50.0).max(1.0);
584 mat.receives_shadows = f("ReceivesShadows", 1.0) > 0.5;
575585}
576586
577587fn parse_wrap_mode(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> Option<u32> {
......@@ -737,6 +747,7 @@ fn resolve_material_inner(
737747 specular_exponent: spec_exp,
738748 fx,
739749 unlit: false,
750 receives_shadows: comp.eval_number(n, "ReceivesShadows", time, 1.0, expr) > 0.5,
740751 has_material_shader: true,
741752 }
742753 }
crates/engine/src/scene3d/mod.rs+45-2
......@@ -331,6 +331,8 @@ pub struct MeshInstance {
331331 pub specular_intensity: f32,
332332 pub specular_exponent: f32,
333333 pub affected_by_lights: bool,
334 pub casts_shadow: bool,
335 pub receives_shadows: bool,
334336 pub flip_normals: bool,
335337 pub displace: Option<Displace3DSpec>,
336338}
......@@ -381,6 +383,8 @@ pub struct DrawMesh {
381383 pub specular_intensity: f32,
382384 pub specular_exponent: f32,
383385 pub affected_by_lights: bool,
386 pub casts_shadow: bool,
387 pub receives_shadows: bool,
384388 pub flip_normals: bool,
385389 pub displace: Option<DisplaceHeightmap>,
386390}
......@@ -898,7 +902,7 @@ impl Scene3DRenderer {
898902 dir_to_light,
899903 dir_color,
900904 cam_pos: [cam_pos[0], cam_pos[1], cam_pos[2], 0.0],
901 light_counts: [n_pt as f32, n_sp as f32, n_dir as f32, 0.0],
905 light_counts: [n_pt as f32, n_sp as f32, n_dir as f32, if m.receives_shadows { 1.0 } else { 0.0 }],
902906 uv_xform: fx.uv.m,
903907 uv_off_wrap: [fx.uv.t[0], fx.uv.t[1], fx.uv_wrap as f32, 0.0],
904908 mat_fx: [fx.bump_scale, fx.bump_mode as f32, if fx.bump_only { 1.0 } else { 0.0 }, fx.spec_model as u32 as f32],
......@@ -931,7 +935,7 @@ impl Scene3DRenderer {
931935 ],
932936 });
933937 // A fully transparent mesh casts no shadow; textured ones are alpha-tested in the pass.
934 let caster = shadow_dyn_buf.as_ref().filter(|_| !fx.wireframe && m.diffuse_col[3] > 1e-3).map(|buf| {
938 let caster = shadow_dyn_buf.as_ref().filter(|_| m.casts_shadow && !fx.wireframe && m.diffuse_col[3] > 1e-3).map(|buf| {
935939 let mut data = [0.0_f32; CASTER_BYTES / 4];
936940 data[..16].copy_from_slice(&m.world);
937941 data[16..20].copy_from_slice(&fx.uv.m);
......@@ -1221,6 +1225,8 @@ mod tests {
12211225 specular_intensity: 0.0,
12221226 specular_exponent: 50.0,
12231227 affected_by_lights: false,
1228 casts_shadow: true,
1229 receives_shadows: true,
12241230 flip_normals: false,
12251231 displace: None,
12261232 };
......@@ -1320,4 +1326,41 @@ mod tests {
13201326 assert!(opaque > off + 15, "opaque card must shadow: {opaque} vs {off}");
13211327 assert_eq!(clear, off, "a fully transparent card casts no shadow");
13221328 }
1329
1330 #[test]
1331 fn shadow_caster_and_receiver_flags_are_honored() {
1332 let dark_receiver_pixels = |blocker_extra: &str, receiver_extra: &str, flat_extra: &str| -> Option<usize> {
1333 let src = format!(
1334 r#"Composition {{ CurrentTime = 0, RenderRange = {{ 0, 1 }}, GlobalRange = {{ 0, 1 }}, Tools = ordered() {{
1335 Flat = MtlBlinn {{ Inputs = {{ ["Specular.Intensity"] = Input {{ Value = 0, }}, {flat_extra} }}, }},
1336 Recv = Shape3D {{ Inputs = {{ ["SurfacePlaneInputs.Width"] = Input {{ Value = 8, }}, ["Transform3DOp.Translate.Z"] = Input {{ Value = -2, }}, {receiver_extra} }}, }},
1337 Blocker = Shape3D {{ Inputs = {{ ["SurfacePlaneInputs.Width"] = Input {{ Value = 0.8, }}, ["Transform3DOp.Translate.X"] = Input {{ Value = 0.4, }}, MaterialInput = Input {{ SourceOp = "Flat", Source = "MaterialOutput", }}, {blocker_extra} }}, }},
1338 Pt = LightPoint {{ Inputs = {{ ["Transform3DOp.Translate.X"] = Input {{ Value = 1.6, }}, ["Transform3DOp.Translate.Z"] = Input {{ Value = 2.0, }}, ["ShadowLightInputs3D.ShadowsEnabled"] = Input {{ Value = 1, }}, ["ShadowLightInputs3D.ShadowDensity"] = Input {{ Value = 1.0, }}, }}, }},
1339 Cam = Camera3D {{ Inputs = {{ ["Transform3DOp.Translate.Z"] = Input {{ Value = 4.5, }}, ApertureW = Input {{ Value = 1.0, }}, ApertureH = Input {{ Value = 1.0, }}, }}, }},
1340 M = Merge3D {{ Inputs = {{ SceneInput1 = Input {{ SourceOp = "Recv", Source = "Output", }}, SceneInput2 = Input {{ SourceOp = "Blocker", Source = "Output", }}, SceneInput3 = Input {{ SourceOp = "Pt", Source = "Output", }}, SceneInput4 = Input {{ SourceOp = "Cam", Source = "Output", }}, }}, }},
1341 R = Renderer3D {{ Inputs = {{ Width = Input {{ Value = 64, }}, Height = Input {{ Value = 64, }}, RendererType = Input {{ Value = FuID {{ "RendererOpenGL" }}, }}, ["RendererOpenGL.LightingEnabled"] = Input {{ Value = 1, }}, ["RendererOpenGL.ShadowsEnabled"] = Input {{ Value = 1, }}, SceneInput = Input {{ SourceOp = "M", Source = "Output", }}, }}, }},
1342 }} }}"#
1343 );
1344 let comp = Comp::from_fusion_str(&src).expect("parse shadow comp");
1345 let mut eng = Engine::new_headless().ok()?;
1346 let (tex, _) = eng.evaluate(&comp, comp.find_node("R").expect("R"), 0.0);
1347 let buf = eng.readback_rgba(&tex).ok()?;
1348 Some((0..64 * 64).filter(|&i| buf[i * 4 + 3] > 200 && buf[i * 4] < 40).count())
1349 };
1350 let no_caster = r#"["SurfacePlaneInputs.Lighting.IsShadowCaster"] = Input { Value = 0, },"#;
1351 let no_receive = r#"["MtlStdInputs.ReceivesShadows"] = Input { Value = 0, },"#;
1352 let flat_receiver = r#"MaterialInput = Input { SourceOp = "Flat", Source = "MaterialOutput", },"#;
1353 let (Some(shadowed), Some(uncast), Some(unreceived), Some(unreceived_by_material)) = (
1354 dark_receiver_pixels("", "", ""),
1355 dark_receiver_pixels(no_caster, "", ""),
1356 dark_receiver_pixels("", no_receive, ""),
1357 dark_receiver_pixels("", flat_receiver, "ReceivesShadows = Input { Value = 0, },"),
1358 ) else {
1359 return;
1360 };
1361 assert!(shadowed > uncast + 15, "baseline must shadow: {shadowed} vs {uncast}");
1362 assert!(shadowed > unreceived + 15, "receiver opt-out must remove the shadow: {shadowed} vs {unreceived}");
1363 assert_eq!(uncast, unreceived, "either flag alone removes the shadow entirely");
1364 assert_eq!(uncast, unreceived_by_material, "a material shader owns the receiver flag");
1365 }
13231366}
crates/engine/src/scene3d/shader.rs+5-2
......@@ -40,7 +40,7 @@ pub(super) struct MeshUniform {
4040 pub(super) dir_color: [[f32; 4]; MAX_DIR_LIGHTS],
4141 /// Camera world position `(cx, cy, cz, 0)`
4242 pub(super) cam_pos: [f32; 4],
43 /// `[num_point_lights, num_spot_lights, num_dir_lights, 0]`
43 /// `[num_point_lights, num_spot_lights, num_dir_lights, receives_shadows]`
4444 pub(super) light_counts: [f32; 4],
4545 /// UV affine matrix, column-major `[m00, m10, m01, m11]`
4646 pub(super) uv_xform: [f32; 4],
......@@ -420,7 +420,10 @@ fn fs_mesh(in: VsOut) -> @location(0) vec4<f32> {
420420 let dist = max(length(to_l), 1e-4);
421421 let att = light_attenuation(dist, u.point_pos[i].w);
422422 // Shadow density dims only the diffuse term; specular is cut outright.
423 let occ = point_shadow_occlusion(i, in.world_pos, n, u.point_pos[i].xyz);
423 var occ = 0.0;
424 if (u.light_counts.w > 0.5) {
425 occ = point_shadow_occlusion(i, in.world_pos, n, u.point_pos[i].xyz);
426 }
424427 lit = lit + eval_light_contrib(
425428 n, v, to_l / dist, u.point_col[i].rgb * att, albedo, spec_rgb, spec_int, shininess,
426429 1.0 - shadow.point[i].y * occ, 1.0 - occ,