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