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 {...@@ -605,6 +605,8 @@ impl Engine {
605 hash_floats.push(ms.specular_intensity);605 hash_floats.push(ms.specular_intensity);
606 hash_floats.push(ms.specular_exponent);606 hash_floats.push(ms.specular_exponent);
607 hash_floats.push(if ms.affected_by_lights { 1.0 } else { 0.0 });607 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 });
608 hash_floats.push(if ms.flip_normals { 1.0 } else { 0.0 });610 hash_floats.push(if ms.flip_normals { 1.0 } else { 0.0 });
609 if let Some((_, sc, bi, ch)) = disp_entry {611 if let Some((_, sc, bi, ch)) = disp_entry {
610 hash_floats.push(sc);612 hash_floats.push(sc);
...@@ -620,6 +622,8 @@ impl Engine {...@@ -620,6 +622,8 @@ impl Engine {
620 specular_intensity: ms.specular_intensity,622 specular_intensity: ms.specular_intensity,
621 specular_exponent: ms.specular_exponent,623 specular_exponent: ms.specular_exponent,
622 affected_by_lights: ms.affected_by_lights,624 affected_by_lights: ms.affected_by_lights,
625 casts_shadow: ms.casts_shadow,
626 receives_shadows: ms.receives_shadows,
623 flip_normals: ms.flip_normals,627 flip_normals: ms.flip_normals,
624 displace: None,628 displace: None,
625 });629 });
crates/engine/src/scene3d/collect.rs+12-1
...@@ -14,6 +14,7 @@ use model::{Comp, ExprEngine, Node, NodeId};...@@ -14,6 +14,7 @@ use model::{Comp, ExprEngine, Node, NodeId};
14struct SceneOverrides {14struct SceneOverrides {
15 material_override: Option<NodeId>,15 material_override: Option<NodeId>,
16 affected_by_lights: Option<bool>,16 affected_by_lights: Option<bool>,
17 casts_shadow: Option<bool>,
17 flip_normals: bool,18 flip_normals: bool,
18 displace: Option<Displace3DSpec>,19 displace: Option<Displace3DSpec>,
19 cull_back: bool,20 cull_back: bool,
...@@ -333,7 +334,10 @@ fn collect_scene_inner(...@@ -333,7 +334,10 @@ fn collect_scene_inner(
333 .or_else(|| node.connected_node("SceneInput"))334 .or_else(|| node.connected_node("SceneInput"))
334 .and_then(|s| comp.find_node(s))335 .and_then(|s| comp.find_node(s))
335 });336 });
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 }
337 meshes.push(make_mesh_instance(world, ShapeKind::ImagePlane, mat, None, &own));341 meshes.push(make_mesh_instance(world, ShapeKind::ImagePlane, mat, None, &own));
338 }342 }
339 "Shape3D" => {343 "Shape3D" => {
...@@ -485,6 +489,7 @@ struct ResolvedMaterial {...@@ -485,6 +489,7 @@ struct ResolvedMaterial {
485 fx: MaterialFx,489 fx: MaterialFx,
486 /// A bare texture operator used as a surface's material renders flat, ignoring lights.490 /// A bare texture operator used as a surface's material renders flat, ignoring lights.
487 unlit: bool,491 unlit: bool,
492 receives_shadows: bool,
488 /// True when resolved from a shader material node (`MtlBlinn` / `MtlPhong` / `MtlCookTorrance`),493 /// True when resolved from a shader material node (`MtlBlinn` / `MtlPhong` / `MtlCookTorrance`),
489 /// which owns diffuse/specular rather than inheriting the geometry node's `MtlStdInputs`.494 /// which owns diffuse/specular rather than inheriting the geometry node's `MtlStdInputs`.
490 has_material_shader: bool,495 has_material_shader: bool,
...@@ -500,6 +505,7 @@ impl Default for ResolvedMaterial {...@@ -500,6 +505,7 @@ impl Default for ResolvedMaterial {
500 specular_exponent: 50.0,505 specular_exponent: 50.0,
501 fx: MaterialFx::default(),506 fx: MaterialFx::default(),
502 unlit: false,507 unlit: false,
508 receives_shadows: true,
503 has_material_shader: false,509 has_material_shader: false,
504 }510 }
505 }511 }
...@@ -525,6 +531,8 @@ fn make_mesh_instance(...@@ -525,6 +531,8 @@ fn make_mesh_instance(
525 specular_intensity: mat.specular_intensity,531 specular_intensity: mat.specular_intensity,
526 specular_exponent: mat.specular_exponent,532 specular_exponent: mat.specular_exponent,
527 affected_by_lights: !mat.unlit && ovr.affected_by_lights.unwrap_or(true),533 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,
528 flip_normals: ovr.flip_normals,536 flip_normals: ovr.flip_normals,
529 displace: ovr.displace.clone(),537 displace: ovr.displace.clone(),
530 }538 }
...@@ -543,6 +551,7 @@ fn surface_scope(...@@ -543,6 +551,7 @@ fn surface_scope(
543 let flag = |k: &str, d: f64| comp.eval_number(node, &format!("{group}.{k}"), time, d, expr) > 0.5;551 let flag = |k: &str, d: f64| comp.eval_number(node, &format!("{group}.{k}"), time, d, expr) > 0.5;
544 flag("Visibility.IsVisible", 1.0).then(|| SceneOverrides {552 flag("Visibility.IsVisible", 1.0).then(|| SceneOverrides {
545 affected_by_lights: ovr.affected_by_lights.or(Some(flag("Lighting.IsAffectedByLights", 1.0))),553 affected_by_lights: ovr.affected_by_lights.or(Some(flag("Lighting.IsAffectedByLights", 1.0))),
554 casts_shadow: Some(flag("Lighting.IsShadowCaster", 1.0)),
546 cull_back: flag("Visibility.CullBackFace", 0.0),555 cull_back: flag("Visibility.CullBackFace", 0.0),
547 cull_front: flag("Visibility.CullFrontFace", 0.0),556 cull_front: flag("Visibility.CullFrontFace", 0.0),
548 ..ovr.clone()557 ..ovr.clone()
...@@ -572,6 +581,7 @@ fn apply_mtl_std(mat: &mut ResolvedMaterial, comp: &Comp, node: &Node, prefix: &...@@ -572,6 +581,7 @@ fn apply_mtl_std(mat: &mut ResolvedMaterial, comp: &Comp, node: &Node, prefix: &
572 mat.specular_col = [f("Specular.Color.Red", 1.0), f("Specular.Color.Green", 1.0), f("Specular.Color.Blue", 1.0)];581 mat.specular_col = [f("Specular.Color.Red", 1.0), f("Specular.Color.Green", 1.0), f("Specular.Color.Blue", 1.0)];
573 mat.specular_intensity = f("Specular.Intensity", 1.0).clamp(0.0, 4.0);582 mat.specular_intensity = f("Specular.Intensity", 1.0).clamp(0.0, 4.0);
574 mat.specular_exponent = f("Specular.Exponent", 50.0).max(1.0);583 mat.specular_exponent = f("Specular.Exponent", 50.0).max(1.0);
584 mat.receives_shadows = f("ReceivesShadows", 1.0) > 0.5;
575}585}
576586
577fn parse_wrap_mode(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> Option<u32> {587fn parse_wrap_mode(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> Option<u32> {
...@@ -737,6 +747,7 @@ fn resolve_material_inner(...@@ -737,6 +747,7 @@ fn resolve_material_inner(
737 specular_exponent: spec_exp,747 specular_exponent: spec_exp,
738 fx,748 fx,
739 unlit: false,749 unlit: false,
750 receives_shadows: comp.eval_number(n, "ReceivesShadows", time, 1.0, expr) > 0.5,
740 has_material_shader: true,751 has_material_shader: true,
741 }752 }
742 }753 }
crates/engine/src/scene3d/mod.rs+45-2
...@@ -331,6 +331,8 @@ pub struct MeshInstance {...@@ -331,6 +331,8 @@ pub struct MeshInstance {
331 pub specular_intensity: f32,331 pub specular_intensity: f32,
332 pub specular_exponent: f32,332 pub specular_exponent: f32,
333 pub affected_by_lights: bool,333 pub affected_by_lights: bool,
334 pub casts_shadow: bool,
335 pub receives_shadows: bool,
334 pub flip_normals: bool,336 pub flip_normals: bool,
335 pub displace: Option<Displace3DSpec>,337 pub displace: Option<Displace3DSpec>,
336}338}
...@@ -381,6 +383,8 @@ pub struct DrawMesh {...@@ -381,6 +383,8 @@ pub struct DrawMesh {
381 pub specular_intensity: f32,383 pub specular_intensity: f32,
382 pub specular_exponent: f32,384 pub specular_exponent: f32,
383 pub affected_by_lights: bool,385 pub affected_by_lights: bool,
386 pub casts_shadow: bool,
387 pub receives_shadows: bool,
384 pub flip_normals: bool,388 pub flip_normals: bool,
385 pub displace: Option<DisplaceHeightmap>,389 pub displace: Option<DisplaceHeightmap>,
386}390}
...@@ -898,7 +902,7 @@ impl Scene3DRenderer {...@@ -898,7 +902,7 @@ impl Scene3DRenderer {
898 dir_to_light,902 dir_to_light,
899 dir_color,903 dir_color,
900 cam_pos: [cam_pos[0], cam_pos[1], cam_pos[2], 0.0],904 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 }],
902 uv_xform: fx.uv.m,906 uv_xform: fx.uv.m,
903 uv_off_wrap: [fx.uv.t[0], fx.uv.t[1], fx.uv_wrap as f32, 0.0],907 uv_off_wrap: [fx.uv.t[0], fx.uv.t[1], fx.uv_wrap as f32, 0.0],
904 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],908 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 {...@@ -931,7 +935,7 @@ impl Scene3DRenderer {
931 ],935 ],
932 });936 });
933 // A fully transparent mesh casts no shadow; textured ones are alpha-tested in the pass.937 // 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| {
935 let mut data = [0.0_f32; CASTER_BYTES / 4];939 let mut data = [0.0_f32; CASTER_BYTES / 4];
936 data[..16].copy_from_slice(&m.world);940 data[..16].copy_from_slice(&m.world);
937 data[16..20].copy_from_slice(&fx.uv.m);941 data[16..20].copy_from_slice(&fx.uv.m);
...@@ -1221,6 +1225,8 @@ mod tests {...@@ -1221,6 +1225,8 @@ mod tests {
1221 specular_intensity: 0.0,1225 specular_intensity: 0.0,
1222 specular_exponent: 50.0,1226 specular_exponent: 50.0,
1223 affected_by_lights: false,1227 affected_by_lights: false,
1228 casts_shadow: true,
1229 receives_shadows: true,
1224 flip_normals: false,1230 flip_normals: false,
1225 displace: None,1231 displace: None,
1226 };1232 };
...@@ -1320,4 +1326,41 @@ mod tests {...@@ -1320,4 +1326,41 @@ mod tests {
1320 assert!(opaque > off + 15, "opaque card must shadow: {opaque} vs {off}");1326 assert!(opaque > off + 15, "opaque card must shadow: {opaque} vs {off}");
1321 assert_eq!(clear, off, "a fully transparent card casts no shadow");1327 assert_eq!(clear, off, "a fully transparent card casts no shadow");
1322 }1328 }
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 }
1323}1366}
crates/engine/src/scene3d/shader.rs+5-2
...@@ -40,7 +40,7 @@ pub(super) struct MeshUniform {...@@ -40,7 +40,7 @@ pub(super) struct MeshUniform {
40 pub(super) dir_color: [[f32; 4]; MAX_DIR_LIGHTS],40 pub(super) dir_color: [[f32; 4]; MAX_DIR_LIGHTS],
41 /// Camera world position `(cx, cy, cz, 0)`41 /// Camera world position `(cx, cy, cz, 0)`
42 pub(super) cam_pos: [f32; 4],42 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]`
44 pub(super) light_counts: [f32; 4],44 pub(super) light_counts: [f32; 4],
45 /// UV affine matrix, column-major `[m00, m10, m01, m11]`45 /// UV affine matrix, column-major `[m00, m10, m01, m11]`
46 pub(super) uv_xform: [f32; 4],46 pub(super) uv_xform: [f32; 4],
...@@ -420,7 +420,10 @@ fn fs_mesh(in: VsOut) -> @location(0) vec4<f32> {...@@ -420,7 +420,10 @@ fn fs_mesh(in: VsOut) -> @location(0) vec4<f32> {
420 let dist = max(length(to_l), 1e-4);420 let dist = max(length(to_l), 1e-4);
421 let att = light_attenuation(dist, u.point_pos[i].w);421 let att = light_attenuation(dist, u.point_pos[i].w);
422 // Shadow density dims only the diffuse term; specular is cut outright.422 // 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 }
424 lit = lit + eval_light_contrib(427 lit = lit + eval_light_contrib(
425 n, v, to_l / dist, u.point_col[i].rgb * att, albedo, spec_rgb, spec_int, shininess,428 n, v, to_l / dist, u.point_col[i].rgb * att, albedo, spec_rgb, spec_int, shininess,
426 1.0 - shadow.point[i].y * occ, 1.0 - occ,429 1.0 - shadow.point[i].y * occ, 1.0 - occ,