diff --git a/crates/engine/src/eval/text_3d.rs b/crates/engine/src/eval/text_3d.rs index e92e8721ff2ae3adb9b68d1fd8bacb7180efe1ca..c478328a9d9cf8ebe07a5b7bda90f9a6f9989cfe 100644 --- a/crates/engine/src/eval/text_3d.rs +++ b/crates/engine/src/eval/text_3d.rs @@ -605,6 +605,8 @@ impl Engine { hash_floats.push(ms.specular_intensity); hash_floats.push(ms.specular_exponent); hash_floats.push(if ms.affected_by_lights { 1.0 } else { 0.0 }); + hash_floats.push(if ms.casts_shadow { 1.0 } else { 0.0 }); + hash_floats.push(if ms.receives_shadows { 1.0 } else { 0.0 }); hash_floats.push(if ms.flip_normals { 1.0 } else { 0.0 }); if let Some((_, sc, bi, ch)) = disp_entry { hash_floats.push(sc); @@ -620,6 +622,8 @@ impl Engine { specular_intensity: ms.specular_intensity, specular_exponent: ms.specular_exponent, affected_by_lights: ms.affected_by_lights, + casts_shadow: ms.casts_shadow, + receives_shadows: ms.receives_shadows, flip_normals: ms.flip_normals, displace: None, }); diff --git a/crates/engine/src/scene3d/collect.rs b/crates/engine/src/scene3d/collect.rs index 1c82ef2c53b8c5ffd019733025fcae56e9b44295..d48c8eaedfe9addc7092cbef4ae3499b7256634c 100644 --- a/crates/engine/src/scene3d/collect.rs +++ b/crates/engine/src/scene3d/collect.rs @@ -14,6 +14,7 @@ use model::{Comp, ExprEngine, Node, NodeId}; struct SceneOverrides { material_override: Option, affected_by_lights: Option, + casts_shadow: Option, flip_normals: bool, displace: Option, cull_back: bool, @@ -333,7 +334,10 @@ fn collect_scene_inner( .or_else(|| node.connected_node("SceneInput")) .and_then(|s| comp.find_node(s)) }); - let mat = resolve_material(comp, mat_node, time, expr, visited); + let mut mat = resolve_material(comp, mat_node, time, expr, visited); + if !mat.has_material_shader { + mat.receives_shadows = comp.eval_number(node, "MtlStdInputs.ReceivesShadows", time, 1.0, expr) > 0.5; + } meshes.push(make_mesh_instance(world, ShapeKind::ImagePlane, mat, None, &own)); } "Shape3D" => { @@ -485,6 +489,7 @@ struct ResolvedMaterial { fx: MaterialFx, /// A bare texture operator used as a surface's material renders flat, ignoring lights. unlit: bool, + receives_shadows: bool, /// True when resolved from a shader material node (`MtlBlinn` / `MtlPhong` / `MtlCookTorrance`), /// which owns diffuse/specular rather than inheriting the geometry node's `MtlStdInputs`. has_material_shader: bool, @@ -500,6 +505,7 @@ impl Default for ResolvedMaterial { specular_exponent: 50.0, fx: MaterialFx::default(), unlit: false, + receives_shadows: true, has_material_shader: false, } } @@ -525,6 +531,8 @@ fn make_mesh_instance( specular_intensity: mat.specular_intensity, specular_exponent: mat.specular_exponent, affected_by_lights: !mat.unlit && ovr.affected_by_lights.unwrap_or(true), + casts_shadow: ovr.casts_shadow.unwrap_or(true), + receives_shadows: mat.receives_shadows, flip_normals: ovr.flip_normals, displace: ovr.displace.clone(), } @@ -543,6 +551,7 @@ fn surface_scope( let flag = |k: &str, d: f64| comp.eval_number(node, &format!("{group}.{k}"), time, d, expr) > 0.5; flag("Visibility.IsVisible", 1.0).then(|| SceneOverrides { affected_by_lights: ovr.affected_by_lights.or(Some(flag("Lighting.IsAffectedByLights", 1.0))), + casts_shadow: Some(flag("Lighting.IsShadowCaster", 1.0)), cull_back: flag("Visibility.CullBackFace", 0.0), cull_front: flag("Visibility.CullFrontFace", 0.0), ..ovr.clone() @@ -572,6 +581,7 @@ fn apply_mtl_std(mat: &mut ResolvedMaterial, comp: &Comp, node: &Node, prefix: & mat.specular_col = [f("Specular.Color.Red", 1.0), f("Specular.Color.Green", 1.0), f("Specular.Color.Blue", 1.0)]; mat.specular_intensity = f("Specular.Intensity", 1.0).clamp(0.0, 4.0); mat.specular_exponent = f("Specular.Exponent", 50.0).max(1.0); + mat.receives_shadows = f("ReceivesShadows", 1.0) > 0.5; } fn parse_wrap_mode(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> Option { @@ -737,6 +747,7 @@ fn resolve_material_inner( specular_exponent: spec_exp, fx, unlit: false, + receives_shadows: comp.eval_number(n, "ReceivesShadows", time, 1.0, expr) > 0.5, has_material_shader: true, } } diff --git a/crates/engine/src/scene3d/mod.rs b/crates/engine/src/scene3d/mod.rs index 2da6eb18dd139fa15b544f98ba76974abb4006f1..7fe758cabcb9e50f24fa72c26cf8795a55657169 100644 --- a/crates/engine/src/scene3d/mod.rs +++ b/crates/engine/src/scene3d/mod.rs @@ -331,6 +331,8 @@ pub struct MeshInstance { pub specular_intensity: f32, pub specular_exponent: f32, pub affected_by_lights: bool, + pub casts_shadow: bool, + pub receives_shadows: bool, pub flip_normals: bool, pub displace: Option, } @@ -381,6 +383,8 @@ pub struct DrawMesh { pub specular_intensity: f32, pub specular_exponent: f32, pub affected_by_lights: bool, + pub casts_shadow: bool, + pub receives_shadows: bool, pub flip_normals: bool, pub displace: Option, } @@ -898,7 +902,7 @@ impl Scene3DRenderer { dir_to_light, dir_color, cam_pos: [cam_pos[0], cam_pos[1], cam_pos[2], 0.0], - light_counts: [n_pt as f32, n_sp as f32, n_dir as f32, 0.0], + light_counts: [n_pt as f32, n_sp as f32, n_dir as f32, if m.receives_shadows { 1.0 } else { 0.0 }], uv_xform: fx.uv.m, uv_off_wrap: [fx.uv.t[0], fx.uv.t[1], fx.uv_wrap as f32, 0.0], 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 { ], }); // A fully transparent mesh casts no shadow; textured ones are alpha-tested in the pass. - let caster = shadow_dyn_buf.as_ref().filter(|_| !fx.wireframe && m.diffuse_col[3] > 1e-3).map(|buf| { + let caster = shadow_dyn_buf.as_ref().filter(|_| m.casts_shadow && !fx.wireframe && m.diffuse_col[3] > 1e-3).map(|buf| { let mut data = [0.0_f32; CASTER_BYTES / 4]; data[..16].copy_from_slice(&m.world); data[16..20].copy_from_slice(&fx.uv.m); @@ -1221,6 +1225,8 @@ mod tests { specular_intensity: 0.0, specular_exponent: 50.0, affected_by_lights: false, + casts_shadow: true, + receives_shadows: true, flip_normals: false, displace: None, }; @@ -1320,4 +1326,41 @@ mod tests { assert!(opaque > off + 15, "opaque card must shadow: {opaque} vs {off}"); assert_eq!(clear, off, "a fully transparent card casts no shadow"); } + + #[test] + fn shadow_caster_and_receiver_flags_are_honored() { + let dark_receiver_pixels = |blocker_extra: &str, receiver_extra: &str, flat_extra: &str| -> Option { + let src = format!( + r#"Composition {{ CurrentTime = 0, RenderRange = {{ 0, 1 }}, GlobalRange = {{ 0, 1 }}, Tools = ordered() {{ + Flat = MtlBlinn {{ Inputs = {{ ["Specular.Intensity"] = Input {{ Value = 0, }}, {flat_extra} }}, }}, + Recv = Shape3D {{ Inputs = {{ ["SurfacePlaneInputs.Width"] = Input {{ Value = 8, }}, ["Transform3DOp.Translate.Z"] = Input {{ Value = -2, }}, {receiver_extra} }}, }}, + Blocker = Shape3D {{ Inputs = {{ ["SurfacePlaneInputs.Width"] = Input {{ Value = 0.8, }}, ["Transform3DOp.Translate.X"] = Input {{ Value = 0.4, }}, MaterialInput = Input {{ SourceOp = "Flat", Source = "MaterialOutput", }}, {blocker_extra} }}, }}, + 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, }}, }}, }}, + Cam = Camera3D {{ Inputs = {{ ["Transform3DOp.Translate.Z"] = Input {{ Value = 4.5, }}, ApertureW = Input {{ Value = 1.0, }}, ApertureH = Input {{ Value = 1.0, }}, }}, }}, + 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", }}, }}, }}, + 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", }}, }}, }}, + }} }}"# + ); + let comp = Comp::from_fusion_str(&src).expect("parse shadow comp"); + let mut eng = Engine::new_headless().ok()?; + let (tex, _) = eng.evaluate(&comp, comp.find_node("R").expect("R"), 0.0); + let buf = eng.readback_rgba(&tex).ok()?; + Some((0..64 * 64).filter(|&i| buf[i * 4 + 3] > 200 && buf[i * 4] < 40).count()) + }; + let no_caster = r#"["SurfacePlaneInputs.Lighting.IsShadowCaster"] = Input { Value = 0, },"#; + let no_receive = r#"["MtlStdInputs.ReceivesShadows"] = Input { Value = 0, },"#; + let flat_receiver = r#"MaterialInput = Input { SourceOp = "Flat", Source = "MaterialOutput", },"#; + let (Some(shadowed), Some(uncast), Some(unreceived), Some(unreceived_by_material)) = ( + dark_receiver_pixels("", "", ""), + dark_receiver_pixels(no_caster, "", ""), + dark_receiver_pixels("", no_receive, ""), + dark_receiver_pixels("", flat_receiver, "ReceivesShadows = Input { Value = 0, },"), + ) else { + return; + }; + assert!(shadowed > uncast + 15, "baseline must shadow: {shadowed} vs {uncast}"); + assert!(shadowed > unreceived + 15, "receiver opt-out must remove the shadow: {shadowed} vs {unreceived}"); + assert_eq!(uncast, unreceived, "either flag alone removes the shadow entirely"); + assert_eq!(uncast, unreceived_by_material, "a material shader owns the receiver flag"); + } } diff --git a/crates/engine/src/scene3d/shader.rs b/crates/engine/src/scene3d/shader.rs index 81f11603c063f0a75405d6ec8d18063453bb4467..db8831956aa44be816980752bcc23cc613371bc3 100644 --- a/crates/engine/src/scene3d/shader.rs +++ b/crates/engine/src/scene3d/shader.rs @@ -40,7 +40,7 @@ pub(super) struct MeshUniform { pub(super) dir_color: [[f32; 4]; MAX_DIR_LIGHTS], /// Camera world position `(cx, cy, cz, 0)` pub(super) cam_pos: [f32; 4], - /// `[num_point_lights, num_spot_lights, num_dir_lights, 0]` + /// `[num_point_lights, num_spot_lights, num_dir_lights, receives_shadows]` pub(super) light_counts: [f32; 4], /// UV affine matrix, column-major `[m00, m10, m01, m11]` pub(super) uv_xform: [f32; 4], @@ -420,7 +420,10 @@ fn fs_mesh(in: VsOut) -> @location(0) vec4 { let dist = max(length(to_l), 1e-4); let att = light_attenuation(dist, u.point_pos[i].w); // Shadow density dims only the diffuse term; specular is cut outright. - let occ = point_shadow_occlusion(i, in.world_pos, n, u.point_pos[i].xyz); + var occ = 0.0; + if (u.light_counts.w > 0.5) { + occ = point_shadow_occlusion(i, in.world_pos, n, u.point_pos[i].xyz); + } lit = lit + eval_light_contrib( n, v, to_l / dist, u.point_col[i].rgb * att, albedo, spec_rgb, spec_int, shininess, 1.0 - shadow.point[i].y * occ, 1.0 - occ,