3D: clipped groups keep their members' transparency and count their pieces

- When a group's geometry effects reshape its members inside a clipping
  group, each member's piece goes back into groups with the opacity and
  blending mode of the groups it was in.
- A clipped shape's pieces (its fills and each stroke) are counted against
  the 4,096-object and work limits before they are cut.
This commit is contained in:
dotCooCoo
2026-11-10 00:36:07 -08:00
parent bad4b986f5
commit cca1977a64
2 changed files with 67 additions and 9 deletions
+46 -9
View File
@@ -11,7 +11,7 @@ use vectorcraft_doc::{Appearance, AppearanceItem, Document, FillLayer, Node, Nod
use vectorcraft_geom::{FillRule, PathData};
use vectorcraft_pathops as po;
use super::{MAX_MEMBERS, geometry, is_container, members, profile, region, reshape_members, solid_paints};
use super::{MAX_MEMBERS, Scope, geometry, is_container, members, profile, region, reshape_members, solid_paints};
use crate::{GeomContext, is_geometry};
/// The deepest nesting of symbol instances, blends and clipping groups resolved for one 3D effect.
@@ -215,13 +215,13 @@ impl Resolver<'_> {
}
let mut shapes = members(n, self.label)?;
reshape_members(n, &n.appearance.effects, &mut shapes);
let mut cut = vec![];
for s in shapes {
let mut piece = shape(n, s.path, s.rule, s.items);
piece.opacity = s.opacity;
if let Some(c) = self.clipped(&piece, clip, depth + 1)? {
kids.push(Arc::new(c));
if let Some(c) = self.clipped(&shape(n, s.path, s.rule, s.items), clip, depth + 1)? {
cut.push((s.scopes, c));
}
}
kids = nest(n, cut, 0);
} else {
for c in n.children().into_iter().flatten() {
if !c.visible || matches!(c.kind, NodeKind::Path { guide: true, .. }) {
@@ -235,17 +235,24 @@ impl Resolver<'_> {
return Ok((!kids.is_empty()).then(|| wrap(n, kids)));
}
let Some((path, rule)) = profile(n) else { return Ok(Some(n.clone())) };
let items = solid_paints(n);
// The fills' piece goes where the top fill is, the one Extrude & Bevel and Inflate build
// their solid from, so the strokes above and below it keep their places.
let top_fill = items.iter().rposition(|i| matches!(i, AppearanceItem::Fill(_)));
// Each cut makes a piece, counted before it is made: the shape was counted once already.
let made = usize::from(top_fill.is_some()) + items.iter().filter(|i| matches!(i, AppearanceItem::Stroke(_))).count();
self.spend(made)?;
self.leaves = self.leaves.saturating_add(made.saturating_sub(1));
if self.leaves > MAX_MEMBERS {
return Err(self.too_many());
}
let path = geometry(&path, &n.appearance.effects, &GeomContext::of(n));
let cut = |area: &PathData, rule: FillRule| -> Result<Option<PathData>, String> {
let inside =
po::try_boolean(area, rule, &clip.0, clip.1, po::BoolOp::Intersect, po::DEFAULT_PRECISION).map_err(|_| self.too_complex())?;
Ok(inside.bounds().is_some().then_some(inside))
};
let items = solid_paints(n);
let fills: Vec<AppearanceItem> = items.iter().filter(|i| matches!(i, AppearanceItem::Fill(_))).cloned().collect();
// The fills' piece goes where the top fill is, the one Extrude & Bevel and Inflate build
// their solid from, so the strokes above and below it keep their places.
let top_fill = items.iter().rposition(|i| matches!(i, AppearanceItem::Fill(_)));
let mut pieces = vec![];
for (k, item) in items.iter().enumerate() {
match item {
@@ -277,6 +284,36 @@ fn clip_area(first: &Node) -> (PathData, FillRule) {
crate::clip_outline(first).map_or_else(|| (PathData::default(), FillRule::NonZero), |(bp, rule)| (PathData::from_bezpath(&bp), rule))
}
/// `pieces`, each with the [`Scope`]s of the groups it was in, back in groups of `n`'s id with
/// those groups' opacity and blending mode: consecutive pieces sharing the scope at `level` go into
/// one group, so its transparency applies to them together. `level` stays below the nesting the
/// pieces came from ([`nested_past`]).
fn nest(n: &Node, pieces: Vec<(Vec<Scope>, Node)>, level: usize) -> Vec<Arc<Node>> {
let mut out = vec![];
let mut run: Vec<(Vec<Scope>, Node)> = vec![];
let mut key = None;
let close = |run: &mut Vec<(Vec<Scope>, Node)>, scope: Option<Scope>, out: &mut Vec<Arc<Node>>| {
let Some(scope) = scope else { return };
let mut g = Node::group(n.id, nest(n, std::mem::take(run), level + 1));
g.opacity = scope.opacity;
g.blend = scope.blend;
out.push(Arc::new(g));
};
for (scopes, piece) in pieces {
let here = scopes.get(level).copied();
if here.map(|s| s.key) != key.map(|s: Scope| s.key) {
close(&mut run, key, &mut out);
key = here;
}
match here {
Some(_) => run.push((scopes, piece)),
None => out.push(Arc::new(piece)),
}
}
close(&mut run, key, &mut out);
out
}
/// Does `n` hold visible groups or layers nested more than `levels` deep? Walked without recursion.
fn nested_past(n: &Node, levels: usize) -> bool {
let mut stack = vec![(n, 0usize)];
@@ -255,6 +255,14 @@ fn nested_symbol_instances_that_repeat_hidden_art_are_refused_past_the_work_limi
assert!(art(&d, &n, id).is_none(), "{id}");
assert!(three_d_meshes_in(Some(&d), &n).is_err(), "{id}");
}
// A clipped shape with more strokes than a 3D effect takes objects is refused before its
// strokes are cut into pieces.
let mut stroked = rect(12, 100.0, 100.0, 200.0, 200.0, 0.0);
let strokes = (0..5000).map(|_| AppearanceItem::Stroke(StrokeLayer::new(Paint::solid(Color::BLACK), 2.0))).collect();
stroked.appearance = Appearance { items: strokes, ..Default::default() }.into();
let n = with(clip_group(10, Rect::new(100.0, 100.0, 150.0, 200.0), stroked), ROTATE, front());
let err = validate_three_d_in(Some(&d), &n, ROTATE, &json!({})).unwrap_err();
assert!(err.contains("takes at most 4096 objects") && !err.contains("on a group"), "{err}");
// The same nesting with 1,024 visible squares stays far below the limit and builds: each
// square, group and instance is visited once (an instance twice: listed, then resolved).
let d = nested_symbols(5, 4, rect(0, 0.0, 0.0, 10.0, 10.0, 0.0));
@@ -344,6 +352,19 @@ fn resolved_objects_and_groups_in_a_three_d_group_keep_their_geometry_effects()
let resolved = bounds(&solid_art_in(Some(&d), &group_of(stroked(NodeKind::Blend { children, spec }))).unwrap());
let plain = bounds(&solid_art(&group_of(stroked(NodeKind::Group { children: steps, clip: false }))).unwrap());
assert!(close(resolved, plain, 0.01), "{resolved:?} {plain:?}");
// A member's opacity and blending mode stay on it when its group's effects reshape it in a clip.
let mut multiplied = left();
multiplied.opacity = 0.5;
multiplied.blend = BlendMode::Multiply;
let n = clip_group(60, clip, moved(Node::group(NodeId(62), vec![Arc::new(multiplied), Arc::new(left())])));
let resolved = resolve(Some(&d), &n, "").unwrap().into_owned();
let mut found = vec![];
resolved.walk(&mut |c| {
if c.blend == BlendMode::Multiply {
found.push((c.opacity, c.children().map_or(0, Vec::len)));
}
});
assert_eq!(found, vec![(0.5, 1)], "one Multiply group at 50% holding the one member's piece");
// Groups nested past the limit under a group with geometry effects in a clipping group are
// refused before the effects reshape them.
let mut deep = left();