diff --git a/crates/effects/src/lighting.rs b/crates/effects/src/lighting.rs index 7e73594d89..e62e34b55e 100644 --- a/crates/effects/src/lighting.rs +++ b/crates/effects/src/lighting.rs @@ -175,6 +175,20 @@ pub(crate) fn footprint(path: &vectorcraft_geom::BezPath, seam: f64) -> vectorcr /// every face is a facet a surface draws: a path filled and stroked by its seam, or a facet composed /// in a clip of its footprint, neither with an opacity mask. pub(crate) fn sealed(id: vectorcraft_doc::NodeId, faces: &[std::sync::Arc]) -> Option { + use std::sync::Arc; + use vectorcraft_doc::{Node, NodeKind}; + let (clip, mut underlay) = underlay(id, faces)?; + underlay.extend(faces.iter().cloned()); + Some(Node::new(id, NodeKind::Group { children: vec![Arc::new(clip), Arc::new(Node::group(id, underlay))], clip: true })) +} + +/// The parts of [`sealed`] for `faces`: a path of the union of the facets' footprints, to clip to, +/// and the facets grown to seams [`UNDERLAY_SEAM`] wide, in the same order. `None` under the same +/// conditions as [`sealed`]. +pub(crate) fn underlay( + id: vectorcraft_doc::NodeId, + faces: &[std::sync::Arc], +) -> Option<(vectorcraft_doc::Node, Vec>)> { use std::sync::Arc; use vectorcraft_doc::{Appearance, Node, NodeKind}; let mut outline = vectorcraft_geom::BezPath::new(); @@ -207,9 +221,7 @@ pub(crate) fn sealed(id: vectorcraft_doc::NodeId, faces: &[std::sync::Arc return None, } } - let clip = Node::path(id, vectorcraft_geom::PathData::from_bezpath(&outline), Appearance::default()); - underlay.extend(faces.iter().cloned()); - Some(Node::new(id, NodeKind::Group { children: vec![Arc::new(clip), Arc::new(Node::group(id, underlay))], clip: true })) + Some((Node::path(id, vectorcraft_geom::PathData::from_bezpath(&outline), Appearance::default()), underlay)) } /// Composite smooth solid-fill facets with one clip and three channel groups per surface. In each diff --git a/crates/effects/src/revolve.rs b/crates/effects/src/revolve.rs index a49e817ef2..0b760ee552 100644 --- a/crates/effects/src/revolve.rs +++ b/crates/effects/src/revolve.rs @@ -296,11 +296,35 @@ fn paint_copies(faces: usize, paint: &Paint) -> usize { faces.saturating_mul(4).saturating_mul(paint_bytes(paint)) } +/// Do Revolve's `options` light its surface with colored lights? +fn colored(options: &Revolve) -> bool { + options.light_color != [1.0; 3] || options.lights.is_some() +} + +/// Do Revolve's `faces` in `paint` lie over an underlay of themselves ([`sealed_run`])? Under +/// colored lights (`colored`), in a solid color or a gradient, when a face takes a smooth light +/// ramp ([`crate::lighting::rgb_gradient_art`], [`crate::lighting::gradient_art`], +/// [`crate::lighting::smooth_paint`]). Not in a gradient with transparent stops: its composed faces +/// carry its opacity mask, which the underlay does not take ([`crate::lighting::underlay`]). +fn seals(faces: &[vectorcraft_three_d::Face], paint: &Paint, colored: bool) -> bool { + colored + && (matches!(paint, Paint::Gradient(_)) || paint.color().is_some()) + && faces.iter().any(|f| f.rgb_gradient.is_some() || f.light_gradient.is_some()) + && crate::lighting::opaque_paint(paint).1.is_none() +} + +/// The bytes the copies of `paint` take on Revolve's `faces` ([`paint_copies`]), twice that when the +/// faces lie over an underlay of themselves ([`seals`]). +fn surface_copies(faces: &[vectorcraft_three_d::Face], paint: &Paint, colored: bool) -> usize { + let copies = paint_copies(faces.len(), paint); + if seals(faces, paint, colored) { copies.saturating_mul(2) } else { copies } +} + /// An error when `path` does not revolve or when its surface in `item`'s paint takes `copies`, the /// bytes of an object's paint copies so far, past [`MAX_PAINT_COPIES`]. fn check_surface(path: &PathData, b: Rect, options: Revolve, item: Option<&AppearanceItem>, copies: &mut usize) -> Result<(), String> { let faces = vectorcraft_three_d::revolve(path, b, options)?; - *copies = copies.saturating_add(item.map_or(0, |i| paint_copies(faces.len(), &paint_of(i).0))); + *copies = copies.saturating_add(item.map_or(0, |i| surface_copies(&faces, &paint_of(i).0, colored(&options)))); if *copies > MAX_PAINT_COPIES { return Err("Revolve paint is too complex for this surface; reduce Segments or simplify the gradient".into()); } @@ -438,7 +462,9 @@ fn group_shadow( /// with its classic surface, as Extrude & Bevel (Classic) does. Revolve's graphics, with the /// symbols of `doc`, are drawn on the faces of the side ([`crate::solids::graphics::surface_parts`]): /// a face with a part of a graphic is a group of the face and that part. With `cast`, the sweep -/// that draws the surface also keeps the faces its shadow is cast from ([`SolidFaces`]). +/// that draws the surface also keeps the faces its shadow is cast from ([`SolidFaces`]). The last +/// value is whether the faces go over an underlay of themselves ([`seals`]): the caller seals them +/// ([`sealed_surface`], [`sealed_runs`]) in the order it draws them. #[allow(clippy::too_many_arguments)] fn surface( doc: Option<&Document>, @@ -453,7 +479,7 @@ fn surface( expand: bool, copies: &mut Copies, cast: bool, -) -> Option<(Vec, Node, Option)> { +) -> Option<(Vec, Node, Option, bool)> { #[cfg(test)] crate::solids::SOLIDS_BUILT.with(|n| n.set(n.get() + 1)); // A loaded file or a profile edited past the limits keeps drawing its source path instead @@ -473,15 +499,16 @@ fn surface( let drawn = facets.iter().map(|f| Drawn { depth: f.depth, points: f.contours.first().cloned().unwrap_or_default(), plane: f.plane }).collect(); let p = merged_params(&fx.id, &fx.params); - return Some((drawn, crate::solids::classic::surface(n, facets, &paint, opacity, blend, &p), kept)); + return Some((drawn, crate::solids::classic::surface(n, facets, &paint, opacity, blend, &p), kept, false)); } - let colored = options.light_color != [1.0; 3] || options.lights.is_some(); + let colored = colored(&options); let (faces, kept) = if cast { vectorcraft_three_d::revolve_shadowed(path, b, options).ok().map(|(f, kept)| (f, Some(kept)))? } else { (vectorcraft_three_d::revolve(path, b, options).ok()?, None) }; - copies.surfaces = copies.surfaces.saturating_add(paint_copies(faces.len(), &paint)); + let seal = seals(&faces, &paint, colored); + copies.surfaces = copies.surfaces.saturating_add(surface_copies(&faces, &paint, colored)); if copies.surfaces > MAX_PAINT_COPIES { return None; } @@ -604,7 +631,62 @@ fn surface( let mut group = Node::group(n.id, children); group.opacity = opacity; group.blend = blend; - Some((keys, group, kept)) + Some((keys, group, kept, seal)) +} + +/// Plain `surface` ([`surface`]: a group of its faces) over an underlay of its faces when `seal` +/// ([`sealed_run`]), keeping the surface's opacity and blending mode. +fn sealed_surface(id: NodeId, surface: Node, seal: bool) -> Node { + let NodeKind::Group { children, clip: false } = &surface.kind else { return surface }; + let Some(mut out) = seal.then(|| sealed_run(id, children)).flatten() else { return surface }; + out.opacity = surface.opacity; + out.blend = surface.blend; + out +} + +/// The faces `run` of one surface, in drawing order, over an underlay of themselves, all clipped +/// once to the faces' outline, as [`crate::lighting::sealed`] draws them. A face drawn with a part +/// of a graphic, a group of the face and that part, takes its face's underlay and stays in the +/// order with its part; the part is cut to its face's outline ([`crate::solids::graphics::surface_parts`]), +/// so the clip keeps it. `None` when [`crate::lighting::underlay`] does not take the faces. +fn sealed_run(id: NodeId, run: &[Arc]) -> Option { + let faces: Vec> = run + .iter() + .map(|face| match &face.kind { + NodeKind::Group { children, clip: false } => children.first().cloned(), + _ => Some(face.clone()), + }) + .collect::>()?; + if faces.is_empty() { + return None; + } + let (clip, mut underlay) = crate::lighting::underlay(id, &faces)?; + underlay.extend(run.iter().cloned()); + Some(Node::new(id, NodeKind::Group { children: vec![Arc::new(clip), Arc::new(Node::group(id, underlay))], clip: true })) +} + +/// `faces` in their drawing order, each with the index of its surface. Each run of consecutive +/// faces of one surface that `seal` takes lies over an underlay of its own faces ([`sealed_run`]), +/// so no underlay reaches under another surface's faces, as Inflate's surfaces do +/// (`crate::solids::underlaid_runs`). A run the underlay does not take keeps its faces. +fn sealed_runs(id: NodeId, faces: Vec<(usize, Arc)>, seal: impl Fn(usize) -> bool) -> Vec> { + let mut out = Vec::with_capacity(faces.len()); + let mut run: Vec> = vec![]; + let mut faces = faces.into_iter().peekable(); + while let Some((surface, face)) = faces.next() { + run.push(face); + if faces.peek().is_some_and(|(next, _)| *next == surface) { + continue; + } + match seal(surface).then(|| sealed_run(id, &run)).flatten() { + Some(sealed) => { + out.push(Arc::new(sealed)); + run.clear(); + } + None => out.append(&mut run), + } + } + out } /// A face [`surface`] draws: its depth and, for [`crate::solids::surface_order`], its polygon on the @@ -636,7 +718,8 @@ fn object_surface( let b = path.bounds()?; let (paint, opacity, blend) = paint_of(item); let cast = casts(fx, opacity); - surface(doc, n, path, b, fx_options(fx), fx, paint, opacity, blend, expand, copies, cast).map(|(_, art, kept)| (art, kept)) + surface(doc, n, path, b, fx_options(fx), fx, paint, opacity, blend, expand, copies, cast) + .map(|(_, art, kept, seal)| (sealed_surface(n.id, art, seal), kept)) } /// The paint of a graphic's solid color `c` on `face`, shaded as the face shades a solid fill of @@ -1055,6 +1138,8 @@ fn group_art(doc: Option<&Document>, n: &Node, expand: bool) -> Option { let mut surfaces = vec![]; // The faces each member's surface kept for the shadow, member by member. let mut kept = vec![]; + // Whether each member's faces lie over an underlay of themselves ([`surface`]). + let mut seals = vec![]; let mut opaque = true; // The graphics go once, on the side of the member with the longest profile. Without graphics // no member is measured for them. @@ -1072,10 +1157,14 @@ fn group_art(doc: Option<&Document>, n: &Node, expand: bool) -> Option { let expand = expand && s.scopes.is_empty(); let doc = doc.filter(|_| longest == Some(k)); let cast = casts(fx, opacity * s.opacity); - let (depths, art, faces) = surface(doc, n, &s.path, b, options, fx, paint, opacity, blend, expand, &mut copies, cast)?; + let (depths, art, faces, seal) = surface(doc, n, &s.path, b, options, fx, paint, opacity, blend, expand, &mut copies, cast)?; surfaces.push((depths, art, s.scopes.as_slice())); kept.push(faces); + seals.push(seal); } + // The faces of a member whose surface takes an underlay lie over one, run by run in the one + // order of every member's faces ([`sealed_runs`]). + let seal = |member: usize| seals.get(member) == Some(&true); if surfaces.is_empty() { return None; } @@ -1101,7 +1190,7 @@ fn group_art(doc: Option<&Document>, n: &Node, expand: bool) -> Option { let order = crate::solids::surface_order(faces.iter().map(|(d, ..)| (d.points.as_slice(), d.plane, d.depth))); let faces = in_order(faces, &order); if opaque { - vec![Arc::new(Node::group(n.id, faces.into_iter().map(|(.., face)| face).collect()))] + vec![Arc::new(Node::group(n.id, sealed_runs(n.id, faces.into_iter().map(|(_, member, .., face)| (member, face)).collect(), seal)))] } else { // An opaque member's face goes in the order on its own. The faces of a member with // transparency share a run, one group with the opacity and blending mode of its fill or @@ -1110,6 +1199,8 @@ fn group_art(doc: Option<&Document>, n: &Node, expand: bool) -> Option { // where one group can't be both behind and in front of another face. struct Run<'a> { member: Option, + // The member the faces are of. + of: usize, scopes: &'a [Scope], bounds: Rect, faces: Vec>, @@ -1125,12 +1216,12 @@ fn group_art(doc: Option<&Document>, n: &Node, expand: bool) -> Option { let paints = opacity > 0.0 && scopes.iter().all(|s| s.opacity > 0.0); let Some(cover) = Coverage::of(&face).filter(|_| paints) else { // A face that paints nothing goes in the order on its own and overlaps nothing. - runs.push(Run { member: None, scopes, bounds: Rect::ZERO, faces: vec![face], cover: vec![], opacity, blend }); + runs.push(Run { member: None, of: member, scopes, bounds: Rect::ZERO, faces: vec![face], cover: vec![], opacity, blend }); continue; }; let masked = masks.get(member).is_some_and(Option::is_some); if opacity >= 1.0 && blend == Default::default() && scopes.is_empty() && !masked { - runs.push(Run { member: None, scopes, bounds: cover.bounds, faces: vec![face], cover: vec![cover], opacity, blend }); + runs.push(Run { member: None, of: member, scopes, bounds: cover.bounds, faces: vec![face], cover: vec![cover], opacity, blend }); continue; } let mut clear = |r: &Run| { @@ -1153,24 +1244,49 @@ fn group_art(doc: Option<&Document>, n: &Node, expand: bool) -> Option { if let Some(last) = last_run.get_mut(member) { *last = Some(runs.len()); } - runs.push(Run { member: Some(member), scopes, bounds: cover.bounds, faces: vec![face], cover: vec![cover], opacity, blend }); + runs.push(Run { + member: Some(member), + of: member, + scopes, + bounds: cover.bounds, + faces: vec![face], + cover: vec![cover], + opacity, + blend, + }); } } } - let runs = runs.into_iter().filter_map(|mut r| { + // Consecutive faces of opaque members on their own wait in `solo` and go over an + // underlay of their member's faces, run by run ([`sealed_runs`]). + let mut out: Vec<(&[Scope], Arc)> = Vec::with_capacity(runs.len()); + let mut solo: Vec<(usize, Arc)> = vec![]; + for mut r in runs { // A face on its own needs a group only for the opacity or blending mode of its fill or stroke. if r.member.is_none() && r.opacity >= 1.0 && r.blend == Default::default() { - return r.faces.pop().map(|face| (r.scopes, face)); + let Some(face) = r.faces.pop() else { continue }; + if r.scopes.is_empty() && !r.cover.is_empty() { + solo.push((r.of, face)); + } else { + out.extend(sealed_runs(n.id, std::mem::take(&mut solo), seal).into_iter().map(|face| (&[][..], face))); + out.push((r.scopes, face)); + } + continue; } - let mut group = Node::group(n.id, r.faces); + out.extend(sealed_runs(n.id, std::mem::take(&mut solo), seal).into_iter().map(|face| (&[][..], face))); + let mut group = match r.member.filter(|m| seal(*m)).and_then(|_| sealed_run(n.id, &r.faces)) { + Some(sealed) => sealed, + None => Node::group(n.id, r.faces), + }; group.opacity = r.opacity; group.blend = r.blend; if let Some(mask) = r.member.and_then(|m| masks.get(m)).cloned().flatten() { group.extras_mut().mask = Some(mask); } - Some((r.scopes, Arc::new(group))) - }); - scoped(n.id, runs.collect(), 0) + out.push((r.scopes, Arc::new(group))); + } + out.extend(sealed_runs(n.id, solo, seal).into_iter().map(|face| (&[][..], face))); + scoped(n.id, out, 0) } }; // One shadow for every member's surface, behind them all. @@ -1693,6 +1809,156 @@ mod tests { assert_eq!(revolve_art(&two), None, "past the budget the source draws"); let object = painted(3, 0.0, stops * 2); assert!(validate_revolve(&object, &json!({})).is_err(), "one object past the budget"); + // Under a colored light the faces lie over an underlay of themselves, which copies their + // paints again: the gradient that fits in white light is past the budget. + let lit = json!({"lightColor": "#ff0000"}); + let object = painted(4, 0.0, stops); + assert_eq!(validate_revolve(&object, &json!({})), Ok(())); + let refused = validate_revolve(&object, &lit).unwrap_err(); + assert!(refused.contains("simplify the gradient"), "{refused}"); + let mut drawn = (*object).clone(); + drawn.appearance.effects.push(crate::new_effect(REVOLVE, &lit).unwrap()); + assert_eq!(revolve_art(&drawn), None, "past the budget the source draws"); + } + + /// Is `u` the underlay [`crate::lighting::underlay`] draws for `face`, a face [`surface`] draws? + fn underlies(u: &Node, face: &Node) -> bool { + match (&u.kind, &face.kind) { + (NodeKind::Path { .. }, NodeKind::Path { .. }) => { + u.path_data() == face.path_data() + && u.appearance.items.iter().all(|i| !matches!(i, AppearanceItem::Stroke(s) if s.width != crate::lighting::UNDERLAY_SEAM)) + } + (NodeKind::Group { children: under, clip: true }, NodeKind::Group { children, clip: true }) => { + matches!((under.get(1), children.get(1)), (Some(a), Some(b)) if Arc::ptr_eq(a, b)) + } + (_, NodeKind::Group { children, clip: false }) => children.first().is_some_and(|f| underlies(u, f)), + _ => false, + } + } + + /// The runs of faces in `art` that lie over an underlay of themselves ([`sealed_run`]), each + /// run's faces in drawing order. + pub(super) fn sealed_faces(art: &Node) -> Vec>> { + let mut out = vec![]; + art.walk(&mut |n| { + let NodeKind::Group { children, clip: true } = &n.kind else { return }; + let [clip, inner] = &children[..] else { return }; + let NodeKind::Group { children: drawn, clip: false } = &inner.kind else { return }; + let (under, faces) = drawn.split_at(drawn.len() / 2); + if clip.path_data().is_some() && !inner.isolate && drawn.len() % 2 == 0 && under.iter().zip(faces).all(|(u, f)| underlies(u, f)) { + out.push(faces.to_vec()); + } + }); + out + } + + /// `n`'s surface as [`surface`] draws it before its faces are sealed, and whether they are. + fn unsealed(n: &Node) -> (Node, bool) { + let path = n.path_data().unwrap(); + let fx = n.appearance.effects.first().unwrap(); + let (paint, opacity, blend) = paint_of(n.appearance.items.first().unwrap()); + let (_, art, _, seal) = + surface(None, n, path, path.bounds().unwrap(), fx_options(fx), fx, paint, opacity, blend, false, &mut Copies::default(), false).unwrap(); + (art, seal) + } + + #[test] + fn colored_light_faces_lie_over_an_underlay_of_themselves_once_per_surface() { + use vectorcraft_doc::color::{BlendMode, Gradient, GradientKind, GradientPaint, GradientStop}; + let lit = |params: Value, paint: Paint| { + let mut n = profile_node(); + n.appearance = Appearance::basic(paint, Paint::None, 0.0).into(); + n.appearance.effects.push(crate::new_effect(REVOLVE, ¶ms).unwrap()); + n + }; + let red = Paint::solid(Color::rgb(0.8, 0.2, 0.1)); + let ramp = |opacity: f32| { + let stops = [Color::WHITE, Color::rgb(0.5, 0.5, 0.5)].into_iter().enumerate(); + let stops = stops.map(|(i, c)| GradientStop { opacity, ..GradientStop::new(i as f32, c) }).collect(); + Paint::Gradient(Box::new(GradientPaint::new(Gradient::new(GradientKind::Linear, stops)))) + }; + let lights = json!([{"color": "#ff8080", "rotation": 120.0}, {"color": "#8080ff", "rotation": -30.0}]); + // In white light nothing is sealed. + let white = lit(json!({}), red.clone()); + assert!(!unsealed(&white).1); + assert!(sealed_faces(&revolve_art(&white).unwrap()).is_empty()); + for (name, n) in [ + ("solid", lit(json!({"lightColor": "#ff0000"}), red.clone())), + ("gradient", lit(json!({"lightColor": "#ff0000"}), ramp(1.0))), + ("two lights", lit(json!({"lights": lights}), ramp(1.0))), + ("edge-on", lit(json!({"lightColor": "#ff0000", "rotationX": -80}), red.clone())), + ] { + // Every face of the surface, in its order, lies over its underlay, once. + let (plain, seal) = unsealed(&n); + assert!(seal, "{name}"); + let faces = plain.children().unwrap().to_vec(); + assert_eq!(sealed_faces(&revolve_art(&n).unwrap()), vec![faces], "{name}"); + } + // The surface's opacity and blending mode apply once, to the sealed surface. + let mut n = lit(json!({"lightColor": "#ff0000"}), ramp(1.0)); + if let Some(AppearanceItem::Fill(f)) = n.appearance.items.first_mut() { + f.opacity = 0.5; + f.blend = BlendMode::Multiply; + } + let art = revolve_art(&n).unwrap(); + assert_eq!(sealed_faces(&art).len(), 1); + // Inside the surface only a face's own lit art blends (its light multiplied over its paint). + let once = |chain: &Chain| chain.first() == Some(&(0.5, BlendMode::Multiply)) && chain.iter().skip(1).all(|(o, _)| *o == 1.0); + let faces = faces_in_order(&art); + assert!(faces.iter().all(|(_, chain)| once(chain)), "{:?}", faces.iter().map(|f| &f.1).find(|c| !once(c))); + // Faces in a gradient with transparent stops carry its opacity mask, which the underlay + // does not take: the surface keeps its faces as they are, and their paints are not + // counted twice. + let n = lit(json!({"lightColor": "#ff0000"}), ramp(0.6)); + let (plain, seal) = unsealed(&n); + assert!(!seal); + assert_eq!(sealed_surface(n.id, plain.clone(), true), plain); + assert!(sealed_faces(&revolve_art(&n).unwrap()).is_empty()); + let path = n.path_data().unwrap(); + let faces = vectorcraft_three_d::revolve(path, path.bounds().unwrap(), fx_options(&n.appearance.effects[0])).unwrap(); + assert_eq!(surface_copies(&faces, &ramp(0.6), true), paint_copies(faces.len(), &ramp(0.6))); + assert_eq!(surface_copies(&faces, &ramp(1.0), true), 2 * paint_copies(faces.len(), &ramp(1.0))); + // Revolve (Classic) takes no colored light, so none of its faces are sealed. + assert!(!colored(&classic::options(&json!({"lightColor": "#ff0000"})))); + } + + #[test] + fn each_members_run_of_faces_lies_over_an_underlay_of_its_own() { + // Rings one above the other: their faces alternate in the one order, so each ring's + // faces go over underlays run by run, and no run holds faces of both. + let ring = |id, y: f64, opacity: f32| { + let mut n = (*member(id, 40.0)).clone(); + if let Some(AppearanceItem::Fill(f)) = n.appearance.items.first_mut() { + f.opacity = opacity; + } + if let NodeKind::Path { path, .. } = &mut n.kind { + *path = PathData::from_bezpath(&(Affine::translate((0.0, y)) * path.to_bezpath())); + } + Arc::new(n) + }; + let lit = json!({"segments": 16, "lightColor": "#ff0000"}); + for opacity in [1.0, 0.5] { + let members = vec![ring(1, 0.0, 1.0), ring(2, 200.0, opacity)]; + let art = revolve_art(&group_with(lit.clone(), members.clone())).unwrap(); + let runs = sealed_faces(&art); + assert!(runs.len() >= 2, "{opacity}: {} runs", runs.len()); + for run in &runs { + let ys: Vec = run.iter().filter_map(|f| f.visual_bounds()).map(|b| b.center().y).collect(); + assert!(ys.iter().all(|y| *y < 100.0) || ys.iter().all(|y| *y > 100.0), "{opacity}: a run holds one ring's faces: {ys:?}"); + } + // Each ring's opacity applies once, to its faces and their underlays; inside, only a + // face's own lit art blends. + let faded = |chain: &Chain| chain.iter().filter(|(o, _)| *o != 1.0).count(); + let chains: Vec = faces_in_order(&art).into_iter().map(|(_, c)| c).collect(); + assert!(chains.iter().all(|c| faded(c) <= 1 && c.iter().all(|(o, _)| *o == 1.0 || *o == opacity)), "{opacity}"); + assert_eq!(chains.iter().any(|c| faded(c) == 1), opacity < 1.0, "{opacity}"); + if opacity == 1.0 { + // Opaque rings share one order, and every face in it lies over an underlay. + let white = revolve_art(&group_with(json!({"segments": 16}), members)).unwrap(); + let count = white.children().unwrap()[0].children().unwrap().len(); + assert_eq!(runs.iter().map(Vec::len).sum::(), count); + } + } } #[test] diff --git a/crates/effects/src/revolve/tests_graphics.rs b/crates/effects/src/revolve/tests_graphics.rs index 5d9500628b..e21a88a26e 100644 --- a/crates/effects/src/revolve/tests_graphics.rs +++ b/crates/effects/src/revolve/tests_graphics.rs @@ -279,6 +279,23 @@ fn under_several_lights_a_graphic_takes_each_faces_own_lit_art() { assert!(translucent > base && opaque - base == 2 * (translucent - base), "{base} {opaque} {translucent}"); } +#[test] +fn under_a_colored_light_each_part_of_a_graphic_stays_right_after_its_face_over_the_underlay() { + let d = document(); + let n = revolved(&CYLINDER, json!({"shade": true, "lightColor": "#ff8040", "graphics": [{"symbol": "Red"}]})); + let art = revolve_art_in(Some(&d), &n).unwrap(); + // One run holds every face; a face with a part of the bar is a group of the face and that part. + let runs = super::tests::sealed_faces(&art); + assert_eq!(runs.len(), 1); + let carrying: Vec<&Arc> = runs[0].iter().filter(|f| matches!(f.kind, NodeKind::Group { clip: false, .. })).collect(); + assert!(!carrying.is_empty()); + assert!(carrying.iter().all(|f| f.children().is_some_and(|c| c.len() > 1 && parts(&c[0], false).is_empty()))); + // Every part of the bar is drawn in the run, after its face, and none in the underlay. + let in_run: usize = carrying.iter().map(|f| parts(f, false).len()).sum(); + assert!(in_run > 0); + assert_eq!(in_run, parts(&art, false).len()); +} + #[test] fn graphics_past_the_point_limit_are_refused() { let mut d = document(); diff --git a/crates/engine/tests/it/three_d_light_color.rs b/crates/engine/tests/it/three_d_light_color.rs index 381bc92f15..fb5903c411 100644 --- a/crates/engine/tests/it/three_d_light_color.rs +++ b/crates/engine/tests/it/three_d_light_color.rs @@ -648,6 +648,107 @@ fn batched_colored_inflate_preserves_compound_holes_and_surface_opacity() { assert_eq!(image.pixels, render(doc(&s)).pixels); } +/// The alpha of `d` drawn at 1:1 against a 3x render averaged down. Inside the art, on each pixel +/// whose 3x average and its eight neighbors' share one alpha, a pixel is off when it is below 255 +/// where that alpha is 255, otherwise more than 3 levels away from it. Outside the art, a pixel is +/// painted past the outline when it has an alpha above 3 where it and its eight neighbors are empty +/// at 3x. Returns (pixels measured inside, pixels off, the largest alpha off, pixels painted past the +/// outline, the first few of either). +fn alpha_against_supersampled(d: &Document) -> (usize, usize, u8, usize, Vec) { + let image = render(d); + let fine = vectorcraft_render::Renderer::new().render( + d, + 1200, + 1200, + kurbo::Affine::scale(3.0), + &vectorcraft_render::RenderOptions { background: None, artboards: false, ..Default::default() }, + ); + let average = fine.area_resized(400, 400).unwrap(); + let (mut measured, mut off, mut worst, mut past) = (0, 0, 0, 0); + let mut first = vec![]; + for y in 1..399 { + for x in 1..399 { + let q = average.pixel(x, y)[3]; + if !(y - 1..=y + 1).all(|y| (x - 1..=x + 1).all(|x| average.pixel(x, y)[3] == q)) { + continue; + } + let p = image.pixel(x, y)[3]; + let wrong = if q == 0 { + let painted = p > 3; + past += usize::from(painted); + painted + } else { + measured += 1; + let hole = if q == 255 { p < 255 } else { p.abs_diff(q) > 3 }; + off += usize::from(hole); + worst = worst.max(p.abs_diff(q)); + hole + }; + if wrong && first.len() < 4 { + first.push(format!("({x},{y}) {p} vs {q}")); + } + } + } + (measured, off, worst, past, first) +} + +/// Revolve under colored lights draws each face of a gradient-lit surface on its own, over an +/// underlay of its faces clipped to the surface's outline: at 1:1, no pixel inside the art lets the +/// background through where a 3x render averaged down is opaque, none paints past the outline, and +/// a surface's opacity, its gradient's transparent stops and its blending mode apply once. The same +/// holds for the art read back from SVG and PDF and for Expand Appearance. +#[test] +fn colored_revolve_faces_leave_no_alpha_holes_at_one_to_one() { + let gradient = |opacity: f64| { + json!({"gradient":{"kind":"linear","start":[100,150],"end":[200,150],"stops":[ + {"offset":0,"color":"#ffffff","opacity":opacity},{"offset":1,"color":"#808080","opacity":opacity} + ]}}) + }; + let lights = json!([ + {"color":"#ff0000","rotation":135,"height":35,"intensity":70}, + {"color":"#0000ff","rotation":-35,"height":25,"intensity":65} + ]); + let cases = [ + ("red light, gradient", gradient(1.0), json!({}), None), + ("red light, solid", json!({"color":"#ffcc66"}), json!({}), None), + ("two lights, gradient", gradient(1.0), json!({"ambient":10,"lights":lights}), None), + ("two lights, solid", json!({"color":"#ffcc66"}), json!({"ambient":10,"lights":lights}), None), + ("red light, edge-on faces", gradient(1.0), json!({"rotationX":-80}), None), + ("red light, gradient at 60%", gradient(0.6), json!({}), None), + ("red light, fill at 60%", gradient(1.0), json!({}), Some(json!({"item":0,"opacity":60}))), + ("red light, multiply", gradient(1.0), json!({}), Some(json!({"item":0,"blend":"Multiply"}))), + ]; + let mut failures = vec![]; + for (name, fill, params, transparency) in cases { + let (mut s, _) = capped_profile(); + s.execute("effect.setParams", &json!({"index":0,"item":null,"params":params})).unwrap(); + s.execute("paint.setFill", &fill).unwrap(); + if let Some(t) = transparency { + s.execute("transparency.set", &t).unwrap(); + } + let svg = s.execute("document.serialize", &json!({"format":"svg"})).unwrap()["text"].as_str().unwrap().to_owned(); + assert!(!svg.contains(" 10_000, "{name} {of}: {measured} pixels inside"); + if off > 0 || past > 0 { + failures.push(format!("{name} {of}: of {measured} pixels, {off} off (at most {worst}), {past} past the outline: {first:?}")); + } + } + } + assert!(failures.is_empty(), "{failures:#?}"); +} + /// Under colored lights, an Inflate fill and stroke in solid colors draw each face on its own, in /// one depth order: zoomed in, the fill's faces show the colors the one composite of the surface /// shows (drawn when the stroke is translucent), with no seam between faces; at 1x, where a pixel diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index b12d7d836a..eee4af439f 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2222,7 +2222,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur `xtask/src/pngsuite.rs` verifies each file from the pinned PngSuite mirror against `xtask/pngsuite.sha256` before it reaches the corpus. Seven synthetic regressions cover substitution, oversize, cache repair, failure cleanup, nonregular targets, stale staging and manifest limits. The opt-in corpus remains gitignored; no PNGs are committed. -Evidence for the shared light Color: geometry tests in `tests_lighting`, effect paint tests, command/save/undo/expand and SVG/PDF checks in `three_d_light_color`, and headless picker/reset/preview tests in `tests_revolve`. Revolve keeps smooth colored vector ramps and continuous flat-lit caps. Inflate geometry tests check spherical lighting, shared vertex/edge continuity, concave cell coverage, puffed backs at perspective and legacy single-light/list equivalence; rendered command/export regressions cover smooth shading and editable expansion. Four legacy white-light PNGs matched main byte for byte when this was measured (2026-10-17); since every 3D seam took round joins (2026-11-12), white-light output is expected to differ from them at facet corners ([Seams](three-d.md#seams)); the four scenes were not exported again to measure it; [release measurements](3d-lighting-performance.md) record the colored solid-fill batch and the remaining source-gradient cost. Colored Inflate's facets lie over an underlay of themselves under one clip, so their shared edges show neither the black channel backdrop nor the background ([Seams](three-d.md#seams)); `three_d_light_color::colored_light_surfaces_match_supersampling_at_internal_facet_edges` compares 1:1 with 3x averaged down, and `colored_light_seams_stay_closed_in_svg_pdf_and_expansion` checks SVG, PDF and Expand Appearance. +Evidence for the shared light Color: geometry tests in `tests_lighting`, effect paint tests, command/save/undo/expand and SVG/PDF checks in `three_d_light_color`, and headless picker/reset/preview tests in `tests_revolve`. Revolve keeps smooth colored vector ramps and continuous flat-lit caps. Inflate geometry tests check spherical lighting, shared vertex/edge continuity, concave cell coverage, puffed backs at perspective and legacy single-light/list equivalence; rendered command/export regressions cover smooth shading and editable expansion. Four legacy white-light PNGs matched main byte for byte when this was measured (2026-10-17); since every 3D seam took round joins (2026-11-12), white-light output is expected to differ from them at facet corners ([Seams](three-d.md#seams)); the four scenes were not exported again to measure it; [release measurements](3d-lighting-performance.md) record the colored solid-fill batch and the remaining source-gradient cost. Colored Inflate's facets lie over an underlay of themselves under one clip, so their shared edges show neither the black channel backdrop nor the background ([Seams](three-d.md#seams)); `three_d_light_color::colored_light_surfaces_match_supersampling_at_internal_facet_edges` compares 1:1 with 3x averaged down, and `colored_light_seams_stay_closed_in_svg_pdf_and_expansion` checks SVG, PDF and Expand Appearance. Colored Revolve's faces lie over an underlay of themselves the same way, run by run in a group and with mapped graphics kept after their faces; `colored_revolve_faces_leave_no_alpha_holes_at_one_to_one` checks that no pixel inside the art lets the background through at 1:1 where 3x averaged down is opaque, live, read back from SVG and PDF, and expanded. ## Eyedropper character dimensions (2026-11-11) diff --git a/docs/three-d.md b/docs/three-d.md index b71f7adc7d..2c2e2f86d9 100644 --- a/docs/three-d.md +++ b/docs/three-d.md @@ -162,6 +162,12 @@ How a facet carries its seam depends on the lights and the paint: - Revolve, with shading on: when a `lights` list is given (even one of white lights), a solid-color or gradient face fills its light ramps through a clip of the face and its 0.75 pt seam, outlined with round joins. With the single legacy light, a face is a path and a seam; with a colored `lightColor` a gradient face fills through the same kind of clip. + Under a colored `lightColor` or a `lights` list, the faces of a solid-color or gradient surface lie over an underlay + of themselves under one clip, as colored Inflate's facets do; in a group, each run of one member's faces in the + shared depth order takes its own underlay, and a face with a part of a mapped graphic keeps that part right after + it. Faces in a gradient with transparent stops carry its opacity mask and are drawn without an underlay. At 1:1, + away from the surface's outline, a pixel is opaque where the same art drawn at 3x and averaged down is opaque + (VectorCraft's own measure). A facet seen almost edge-on is a sliver, and a seam with mitered joins would reach past the solid as a thorn; a round one reaches no more than half its width past its facet. So where a facet has no neighbor (Rotate's plane, every