diff --git a/crates/effects/src/solids/rendered/tests.rs b/crates/effects/src/solids/rendered/tests.rs index b22e543fe5..a15b80fa12 100644 --- a/crates/effects/src/solids/rendered/tests.rs +++ b/crates/effects/src/solids/rendered/tests.rs @@ -582,6 +582,77 @@ fn a_see_through_solid_draws_its_shadow_plane_behind_it() { assert!(shadow > 0 && covered > 0, "{shadow} shadow pixels, {covered} solid pixels"); } +/// With Shadows on, solids in the same opacity and blending mode share one shadow plane image, and +/// solids in different ones each get their own, all before the solids' own images. +#[test] +fn solids_in_one_ink_share_a_shadow_plane_image() { + use vectorcraft_doc::color::BlendMode; + let mut n = Node::path( + NodeId(1), + square(100.0, 100.0, 100.0), + Appearance::basic(Paint::solid(Color::rgb(0.2, 0.5, 0.8)), Paint::solid(Color::rgb(0.8, 0.2, 0.2)), 8.0), + ); + n.appearance + .effects + .push(crate::new_effect(super::super::EXTRUDE, &json!({"shadow": true, "render": {"mode": "rendered", "quality": "low"}})).unwrap()); + let inked = |fill: (f32, BlendMode), stroke: (f32, BlendMode)| { + let mut n = n.clone(); + // Appearance::basic holds the fill, then the stroke. + if let vectorcraft_doc::AppearanceItem::Fill(f) = &mut n.appearance.items[0] { + (f.opacity, f.blend) = fill; + } + if let vectorcraft_doc::AppearanceItem::Stroke(s) = &mut n.appearance.items[1] { + (s.opacity, s.blend) = stroke; + } + let (images, alpha) = image_nodes(&drawn(&n)) + .into_iter() + .map(|(i, o, b)| { + let pixels = decoded(&i); + let pixels = pixels.as_chunks::<4>().0; + let plane = pixels.iter().any(|p| p[3] > 0) && pixels.iter().all(|p| p[3] == 0 || p[..3] == [0, 0, 0]); + (((o, b), plane), pixels.iter().map(|p| p[3]).collect::>()) + }) + .unzip::<_, _, Vec<_>, Vec<_>>(); + (n, images, alpha) + }; + let core = |alpha: &[u8]| { + let full = *alpha.iter().max().unwrap(); + alpha.iter().enumerate().filter_map(|(i, &a)| (a == full).then_some(i)).collect::>() + }; + let half = (0.5, BlendMode::Normal); + let (shared_node, shared, shared_alpha) = inked(half, half); + assert_eq!(shared, [(half, true), (half, false), (half, false)], "one plane image for both solids, then each solid"); + // `scene_of` leaves out the shadow plane the drawn art places; a plane under the square stands in for it. + let plane = Some([[100.0, 100.0, 0.0], [200.0, 100.0, 0.0], [200.0, 200.0, 0.0], [100.0, 200.0, 0.0]]); + let (meshes, mut lighting) = scene_of(&shared_node); + lighting.receiver = plane; + assert_eq!(layers(&meshes, &lighting, false), [(Layer::Plane(0, 2), half), (Layer::Solid(0), half), (Layer::Solid(1), half)]); + let quarter = (0.25, BlendMode::Normal); + let (split_node, split, split_alpha) = inked(half, quarter); + assert_eq!(split, [(half, true), (quarter, true), (half, false), (quarter, false)], "one plane image for each ink, then each solid"); + let (meshes, mut lighting) = scene_of(&split_node); + lighting.receiver = plane; + assert_eq!( + layers(&meshes, &lighting, false), + [(Layer::Plane(0, 1), half), (Layer::Plane(1, 2), quarter), (Layer::Solid(0), half), (Layer::Solid(1), quarter)] + ); + let measured = measure(&meshes, &lighting, MEDIUM, 72.0, false, &Limits::default()).unwrap(); + for (layer, casts) in [(Layer::Plane(0, 1), [true, false]), (Layer::Plane(1, 2), [false, true])] { + let plane = scene(&meshes, &lighting, layer, &measured).unwrap(); + assert_eq!(plane.materials[..meshes.len()].iter().map(|m| m.casts_shadows).collect::>(), casts, "{layer:?}"); + } + // The stroke reaches 4 pt past the fill, so its shadow covers pixels outside the fill's shadow. + let (fill_core, stroke_core) = (core(&split_alpha[0]), core(&split_alpha[1])); + assert!(stroke_core.iter().any(|&i| split_alpha[0][i] == 0), "the stroke's plane shows the stroke's shadow"); + assert!(fill_core.union(&stroke_core).all(|&i| shared_alpha[0][i] > 0), "the shared plane covers both solids' shadows"); + let multiply = (0.5, BlendMode::Multiply); + assert_eq!( + inked(half, multiply).1, + [(half, true), (multiply, true), (half, false), (multiply, false)], + "a blending mode splits the plane image too" + ); +} + /// With a fill and a stroke, each drawn on its own once either is see-through, in the order of the /// appearance, each in its own ink; a fill at 0% opacity draws nothing and its stroke still draws. #[test] @@ -928,6 +999,45 @@ fn check_only_counts_shadow_work_and_lights() { } } +/// In a solid's own image the other solids stay in the scene unseen. Check counts no raster work +/// for their triangles, as drawing the image counts none, so a limit the solid's own image fits +/// is a limit it draws within. +#[test] +fn check_counts_raster_work_only_for_drawn_triangles() { + let n = solid_square(super::super::EXTRUDE, Color::BLACK, json!({"rotationX": 0, "rotationY": 0, "rotationZ": 0, "shadow": true})); + let (meshes, lighting) = scene_of(&n); + assert!(lighting.lights.iter().any(|l| lighting.casts(l))); + let two = vec![meshes[0].clone(), meshes[0].clone()]; + // At 600 ppi the image's raster work outweighs the draft shadow maps' work. + let settings = RenderSettings { quality: 0, resolution: Some(600.0), white: false }; + let limits = |max_work| { + let mut limits = Limits::default(); + limits.max_work = max_work; + limits + }; + let fits = |max_work| check(&meshes[..1], &lighting, Layer::Solid(0), settings, 72.0, &limits(max_work)).is_ok(); + let (mut lo, mut hi) = (0, Limits::default().max_work); + assert!(fits(hi)); + while lo + 1 < hi { + let mid = lo + (hi - lo) / 2; + if fits(mid) { + hi = mid; + } else { + lo = mid; + } + } + let one = hi; + assert!(check(&two, &lighting, Layer::Solid(0), settings, 72.0, &limits(one)).is_ok(), "the unseen duplicate adds no raster work"); + assert!( + check(&two, &lighting, Layer::All, settings, 72.0, &limits(one)).is_err(), + "drawing both solids takes more raster work than the solid's own image" + ); + assert!( + render_layer(&two, &lighting, Layer::Solid(0), settings, 72.0, false, &limits(one)).is_ok(), + "the image draws within the limit check accepts" + ); +} + #[test] fn check_only_art_reports_overlapping_faces_past_the_work_limit() { let n = solid_square(super::super::ROTATE, Color::BLACK, json!({"rotationX": 0, "rotationY": 0, "rotationZ": 0, "shade": false}));