From 0aafbdeb6de417b821d51ee6b511f8af692d375b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 21 Nov 2026 01:38:37 -0800 Subject: [PATCH] 3D: twisted bevel pieces meet the inset cap at sharp joins A twist cut a beveled edge at points of the art's curve itself, while the inset cap's edge eases its corners' miter offsets across the whole edge. Where the art meets another edge at a sharp angle, the pieces next to the cap drew a different curve from the cap's, leaving a gap of up to about 1.5 pt between them. Each cut corner now takes the edge's corner offsets, eased across the edge as the cap's edge eases them, and each piece follows the whole edge's curve, so the pieces draw the cap's edge within the cap's sampling tolerance, with and without perspective. The tiny-circle test now also requires the solids to keep at least one curve before it checks their control points. --- crates/three-d/src/solid/build.rs | 22 ++++-- crates/three-d/src/solid/outline.rs | 85 +++++++++++++++++++----- crates/three-d/src/solid/tests_curves.rs | 48 ++++++++++++- docs/target-app-parity.md | 2 +- 4 files changed, 135 insertions(+), 22 deletions(-) diff --git a/crates/three-d/src/solid/build.rs b/crates/three-d/src/solid/build.rs index 20c147b1cd..c945d12d1a 100644 --- a/crates/three-d/src/solid/build.rs +++ b/crates/three-d/src/solid/build.rs @@ -41,6 +41,11 @@ impl Camera { let f = perspective_scale(self.eye, v.2); Point::new(self.origin.x + v.0 * f, self.origin.y + v.1 * f) } + /// The view-space point at depth `z` that [`Camera::project`] takes to `p`. + pub(super) fn unproject(&self, p: Point, z: f64) -> V3 { + let f = perspective_scale(self.eye, z); + V3((p.x - self.origin.x) / f, (p.y - self.origin.y) / f, z) + } /// The cosine between a face's outward unit normal `n` and the direction from `at` (both in /// view space) to the eye: positive when the face is turned towards the viewer. pub(super) fn facing(&self, at: V3, n: V3) -> f64 { @@ -303,15 +308,24 @@ impl<'a> Builder<'a> { let places = curves.as_ref().map_or([None, None], |w| w.places); // Where the edge's curve is at share `s` of it. let t = |s: f64| span.map_or(0.0, |span| span.t0 + (span.t1 - span.t0) * s); - // A cut corner on a ring that follows a curve lies on the curve, so the pieces' edges - // follow the same curve as the cap's edge they meet. + // A cut corner on a ring that follows a curve lies on the curve, moved by the offsets of + // the edge's corners from it (a bevel's miters), eased across the edge as the cap's edge + // eases them, so the pieces' edges follow the same curve as the cap's edge they meet. The + // offsets are eased as drawn, and the corner keeps the depth they give it in view space. + let camera = self.camera; let corner = |p: V3, q: V3, s: f64, ring: usize| match (span, places.get(ring).copied().flatten()) { (Some(span), Some(place)) if s > 0.0 && s < 1.0 => { - let v = place(&span, t(s)); + let (c, c0, c1) = (place(&span, t(s)), place(&span, span.t0), place(&span, span.t1)); + let (ep, eq) = (outline::fade(s), outline::fade(1.0 - s)); + let (dp, dq) = (p.sub(c0), q.sub(c1)); + let z = c.2 + dp.2 * ep + dq.2 * eq; + let (dp, dq) = (camera.project(p) - camera.project(c0), camera.project(q) - camera.project(c1)); + let v = camera.unproject(camera.project(c) + dp * ep + dq * eq, z); if v.0.is_finite() && v.1.is_finite() && v.2.is_finite() { v } else { lerp(p, q, s) } } _ => lerp(p, q, s), }; + let ends = [[self.camera.project(a[i]), self.camera.project(a[j])], [self.camera.project(b[i]), self.camera.project(b[j])]]; for k in 0..pieces { let (s0, s1) = (k as f64 / pieces as f64, (k + 1) as f64 / pieces as f64); let quad = vec![corner(a[i], a[j], s0, 0), corner(a[i], a[j], s1, 0), corner(b[i], b[j], s1, 1), corner(b[i], b[j], s0, 1)]; @@ -359,7 +373,7 @@ impl<'a> Builder<'a> { // stays straight. let near = w.near.filter(|_| pieces == 1 || w.places[0].is_some()); let far = w.far.filter(|_| pieces == 1 || w.places[1].is_some()); - self.outline(before, |c| c.first().and_then(|quad| outline::wall(&span, (t(s0), t(s1)), quad, near, far, w.budget))); + self.outline(before, |c| c.first().and_then(|quad| outline::wall(&span, (t(s0), t(s1)), ends, quad, near, far, w.budget))); } } } diff --git a/crates/three-d/src/solid/outline.rs b/crates/three-d/src/solid/outline.rs index 75d62251f2..b2491896d7 100644 --- a/crates/three-d/src/solid/outline.rs +++ b/crates/three-d/src/solid/outline.rs @@ -2,7 +2,7 @@ //! wall along the profile follow the profile's curves as cubics, while each facet's polygon stays //! the shape that visibility and depth are worked out on. -use vectorcraft_geom::{BezPath, CubicBez, ParamCurve, PathData, Point}; +use vectorcraft_geom::{BezPath, CubicBez, ParamCurve, PathData, Point, Vec2}; use super::rings::{Ring, Span}; @@ -75,15 +75,54 @@ fn fit(g: &dyn Fn(f64) -> Point, p: Point, q: Point, a: f64, b: f64, tolerance: fit(g, p, m, a, mid, tolerance, depth + 1, out) && fit(g, m, q, mid, b, tolerance, depth + 1, out) } +/// How much of a corner's offset from the outline is left at share `x` of the way from the corner +/// to where the offset is eased out: 1 at the corner, 0 from 1 on, with no slope at either. +pub(super) fn fade(x: f64) -> f64 { + let x = x.clamp(0.0, 1.0); + 1.0 - x * x * (3.0 - 2.0 * x) +} + +/// 1 at `from`, 0 from `to` on ([`fade`] over `t`). +fn ease(t: f64, from: f64, to: f64) -> f64 { + if to == from { 1.0 } else { fade((t - from) / (to - from)) } +} + +/// The edge a piece of a wall's edge is cut from: its corners lie off the outline by `d.0` where +/// the edge starts on its span's curve (`t.0`) and by `d.1` where it ends (`t.1`). Each offset is +/// eased out across the whole edge, as a cap's edge eases it, so the pieces follow the cap's curve. +#[derive(Clone, Copy)] +struct Whole { + t: (f64, f64), + d: (Vec2, Vec2), +} + +impl Whole { + /// The outline at `t` on `span`'s curve, moved by the corners' offsets eased across the edge. + fn at(&self, step: &Along, span: &Span, t: f64) -> Point { + (step.at)(span, t) + self.d.0 * ease(t, self.t.0, self.t.1) + self.d.1 * ease(t, self.t.1, self.t.0) + } +} + /// The cubics from `p` to `q` along the piece of `span`'s curve from `t0` to `t1` at `step`, or /// `None` when they do not fit, or would take the budget past [`MAX_OUTLINE_CURVES`]. /// /// `p` and `q` are the face's own corners, which a bevel's miter can move off the curve's offset /// where the curve meets another edge at an angle. The difference is eased out from `p` by `fade.0` /// and into `q` from `fade.1` (the ends of the first and last sampled edge), as the face's polygon -/// does over those edges, so the curves meet the face's corners. +/// does over those edges, so the curves meet the face's corners. With `whole`, the piece is cut from +/// that edge, and the curve it follows is the whole edge's, with `p` and `q` measured from it. #[allow(clippy::too_many_arguments)] -fn piece(step: &Along, span: &Span, t0: f64, t1: f64, fade: (f64, f64), p: Point, q: Point, budget: &mut Budget) -> Option> { +fn piece( + step: &Along, + span: &Span, + t0: f64, + t1: f64, + fade: (f64, f64), + p: Point, + q: Point, + whole: Option, + budget: &mut Budget, +) -> Option> { if let Some(map) = step.map { let c = span.curve.subsegment(t0..t1); let c = CubicBez::new(p, map(c.p1), map(c.p2), q); @@ -94,13 +133,9 @@ fn piece(step: &Along, span: &Span, t0: f64, t1: f64, fade: (f64, f64), p: Point return (budget.0 <= MAX_OUTLINE_CURVES).then(|| vec![c]); } } - let (dp, dq) = (p - (step.at)(span, t0), q - (step.at)(span, t1)); - // 1 at the end, 0 from `to` on, with no slope at either. - let ease = |t: f64, from: f64, to: f64| { - let x = if to == from { 1.0 } else { ((t - from) / (to - from)).clamp(0.0, 1.0) }; - 1.0 - x * x * (3.0 - 2.0 * x) - }; - let g = |t: f64| (step.at)(span, t) + dp * ease(t, t0, fade.0) + dq * ease(t, t1, fade.1); + let base = |t: f64| whole.map_or_else(|| (step.at)(span, t), |w| w.at(step, span, t)); + let (dp, dq) = (p - base(t0), q - base(t1)); + let g = |t: f64| base(t) + dp * ease(t, t0, fade.0) + dq * ease(t, t1, fade.1); let mut out = vec![]; if !fit(&g, p, q, t0, t1, step.tolerance, 0, &mut out) { return None; @@ -141,7 +176,7 @@ fn contour(path: &mut BezPath, ring: &Ring, points: &[Point], step: &Along, budg (j, t1, last) = (j + 1, next.t1, next.t0); } let (p, q) = (points.get(i).copied().unwrap_or(first), end(j)); - match piece(step, &span, span.t0, t1, (span.t1, last), p, q, budget) { + match piece(step, &span, span.t0, t1, (span.t1, last), p, q, None, budget) { Some(curves) => { curved = true; for c in curves { @@ -174,12 +209,30 @@ pub(super) fn face(rings: &[&Ring], points: &[Vec], step: &Along, budget: /// The outline of a wall's quad `quad` (`a`, `a'`, `b'`, `b` projected: the edge at `near` from /// `a` to `a'`, then the edge at `far` back from `b'` to `b`), whose edges along the profile follow /// `span`'s curve from `t.0` to `t.1` (all of `span`, or the piece of it one cut of a twisted wall -/// takes). An edge without its outline stays straight. `None` when neither edge is curved. -pub(super) fn wall(span: &Span, t: (f64, f64), quad: &[Point], near: Option<&Along>, far: Option<&Along>, budget: &mut Budget) -> Option { +/// takes). `ends` are the corners of the whole edges at `near` and at `far` (projected, where each +/// starts and ends on all of `span`), so that a piece follows the curve the whole edge does, as the +/// cap's edge it meets. An edge without its outline stays straight. `None` when neither edge is +/// curved. +#[allow(clippy::too_many_arguments)] +pub(super) fn wall( + span: &Span, + t: (f64, f64), + ends: [[Point; 2]; 2], + quad: &[Point], + near: Option<&Along>, + far: Option<&Along>, + budget: &mut Budget, +) -> Option { let [a, a2, b2, b] = <[Point; 4]>::try_from(quad).ok()?; let (t0, t1) = t; - let front = near.and_then(|near| piece(near, span, t0, t1, (t1, t0), a, a2, budget)); - let back = far.and_then(|far| piece(far, span, t1, t0, (t0, t1), b2, b, budget)); + let whole = |step: &Along, [p, q]: [Point; 2]| { + let d = (p - (step.at)(span, span.t0), q - (step.at)(span, span.t1)); + let finite = [d.0, d.1].iter().all(|d| d.x.is_finite() && d.y.is_finite()); + finite.then_some(Whole { t: (span.t0, span.t1), d }) + }; + let [near_ends, far_ends] = ends; + let front = near.and_then(|near| piece(near, span, t0, t1, (t1, t0), a, a2, whole(near, near_ends), budget)); + let back = far.and_then(|far| piece(far, span, t1, t0, (t0, t1), b2, b, whole(far, far_ends), budget)); if front.is_none() && back.is_none() { return None; } @@ -220,7 +273,7 @@ pub(super) fn edges(points: &[Vec], curves: &[Vec>], st path.move_to(first); for (k, c) in curves.iter().enumerate() { let (p, q) = (points.get(k).copied().unwrap_or(first), points.get((k + 1) % n).copied().unwrap_or(first)); - match c.as_ref().and_then(|(span, t0, t1)| piece(step, span, *t0, *t1, (*t1, *t0), p, q, budget)) { + match c.as_ref().and_then(|(span, t0, t1)| piece(step, span, *t0, *t1, (*t1, *t0), p, q, None, budget)) { Some(cubics) => { curved = true; cubics.iter().for_each(|c| path.curve_to(c.p1, c.p2, c.p3)); diff --git a/crates/three-d/src/solid/tests_curves.rs b/crates/three-d/src/solid/tests_curves.rs index 07ba46d19f..4d00fe8353 100644 --- a/crates/three-d/src/solid/tests_curves.rs +++ b/crates/three-d/src/solid/tests_curves.rs @@ -162,6 +162,50 @@ fn a_twisted_wall_keeps_curves_where_a_twist_cuts_it() { } } +/// A half disc: a straight diameter and a round arc, meeting it at right angles. +fn half_disc() -> BezPath { + let k = 0.552_284_749_830_793_6 * 50.0; + let mut b = BezPath::new(); + b.move_to((0.0, 50.0)); + b.line_to((100.0, 50.0)); + b.curve_to((100.0, 50.0 - k), (50.0 + k, 0.0), (50.0, 0.0)); + b.curve_to((50.0 - k, 0.0), (0.0, 50.0 - k), (0.0, 50.0)); + b.close_path(); + b +} + +/// With a bevel, the cap is inset, and its corners where the arc meets the diameter at a right +/// angle lie off the curve's inset by the miter. A twist cuts the band next to the cap into pieces: +/// the pieces that start or end at one of the cap's corners draw the cap's edge, within the cap's +/// sampling tolerance of its curves, with no gap where they meet it. +#[test] +fn twisted_bevel_pieces_meet_the_cap_at_sharp_joins() { + let (p, b) = shape(&half_disc()); + let bevel = Bevel { inside: true, width: 4.0, height: 1.0, ..Bevel::default() }; + let o = Extrude { depth: 4.0, twist: 30.0, bevel: Some(bevel), ..Extrude::default() }; + let turned = View { rotation_x: 20.0, rotation_y: 30.0, ..front() }; + for view in [turned, View { perspective: 60.0, ..turned }] { + let f = extrude(&p, FillRule::EvenOdd, b, o, view).unwrap(); + let cap = f.iter().find(|f| f.depth == f64::MAX).unwrap(); + let corners: Vec = cap.contours.iter().flatten().copied().collect(); + let cap = fine(&cap.path().to_bezpath(), |q| q); + let mut pieces = 0; + for side in f.iter().filter(|f| f.side) { + for c in curves(&side.path()) { + let at_corner = |q: Point| corners.iter().any(|v| v.distance(q) < 1e-6); + // One end at a corner of the cap, the other where the twist cuts the edge. + if at_corner(c.p0) == at_corner(c.p3) { + continue; + } + pieces += 1; + let far = farthest(&c, &cap); + assert!(far <= 2.0 * 0.05 + 1e-6, "{far}: {c:?}"); + } + } + assert!(pieces > 10, "{pieces}"); + } +} + /// Inflate keeps the art's curves where its faces meet the outline: the flat back is the art /// turned, one cubic for each of its curves within 0.001 pt; every side facet has both its edges /// curved; and the front's cells along the outline are curved there, within the sampling @@ -228,7 +272,9 @@ fn straight_tiny_and_twisted_solids_keep_their_curves_bounded() { ), inflate(&tiny, FillRule::NonZero, b, Inflate { depth: 0.02, twist: 360.0, taper: 0.01, both_sides: true, ..Inflate::default() }, view), ] { - for f in f.unwrap() { + let f = f.unwrap(); + assert!(f.iter().any(|f| !curves(&f.path()).is_empty()), "no curve on a tiny circle"); + for f in f { for c in curves(&f.path()) { assert!([c.p0, c.p1, c.p2, c.p3].iter().all(|q| q.x.is_finite() && q.y.is_finite()), "{c:?}"); } diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index aa80094688..e176ac62a9 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1688,7 +1688,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - The solids of one Extrude & Bevel (both kinds) or Inflate effect on one object, its fill's, its strokes' and a group's members' together, keep up to 163,840 faces (VectorCraft's own limit). Each solid is built in turn; once their faces pass the limit, no later solid is built, the object draws its source art, and applying or editing the effect, Export 3D Object and Expand as Wireframes report that the 3D surface is too complex. Tests `solids::tests::an_objects_solids_count_their_faces_together`, `a_groups_solids_together_past_the_limit_draw_the_source`, `a_fill_and_strokes_together_past_the_limit_draw_the_source`. - Inflate at a Volume of 0 % works out each shape's puff only when its solid is built, so no puff is worked out for the shapes after the face limit stops, and the solids are the same bit for bit; with any volume every puff is worked out first, as the highest one places the eye. The shadow builds no solid for a fully transparent fill or stroke. Tests `solid::tests::a_flat_inflate_solves_each_puff_as_its_solid_is_taken`, `a_flat_inflate_solved_as_taken_matches_one_solved_first`, `shadow::tests::an_inflate_shape_that_casts_nothing_builds_no_solid`. - Extrude & Bevel (both kinds) and Rotate keep the art's curves in the faces they draw, export and expand: caps and the edges of walls and bevels where they meet a cap are cubics with smooth anchors where the art's are, fitted within 0.001 pt in perspective and within the sampling tolerance on a bevel's inset. A piece of curve that does not fit after 8 halvings, and the curves past 1,048,576 for one solid stay straight (VectorCraft's own values; [three-d.md](three-d.md#what-differs-from-the-public-documentation)). Tests `solid::tests::a_cap_keeps_the_curves_of_the_art`, `a_cap_in_perspective_fits_the_projected_curves`, `a_beveled_cap_and_its_walls_share_their_curves`, `twisted_beveled_tiny_and_degenerate_caps_keep_their_curves`. - - The edges of Extrude & Bevel's walls and bevels between two bevel steps or two wall slices follow the art's curves too, the same curves on both faces that meet there; a twist cuts a wall's edge at points of the curve, so each piece meets the cap along its curve; and Inflate keeps the curves of its flat back, its side walls, and its front and puffed back along the outline (within the sampling tolerance there). Tests `solid::tests_curves::interior_bevel_edges_keep_the_curves_of_the_art`, `a_twisted_wall_keeps_curves_where_a_twist_cuts_it`, `inflate_keeps_the_curves_of_its_outline`, `straight_tiny_and_twisted_solids_keep_their_curves_bounded`. + - The edges of Extrude & Bevel's walls and bevels between two bevel steps or two wall slices follow the art's curves too, the same curves on both faces that meet there; a twist cuts a wall's edge at points of the curve, so each piece meets the cap along its curve, a bevel's inset cap included; and Inflate keeps the curves of its flat back, its side walls, and its front and puffed back along the outline (within the sampling tolerance there). Tests `solid::tests_curves::interior_bevel_edges_keep_the_curves_of_the_art`, `a_twisted_wall_keeps_curves_where_a_twist_cuts_it`, `twisted_bevel_pieces_meet_the_cap_at_sharp_joins`, `inflate_keeps_the_curves_of_its_outline`, `straight_tiny_and_twisted_solids_keep_their_curves_bounded`. - Export 3D Object (Asset Export's menu): an object with Extrude & Bevel, Revolve, Inflate or Rotate becomes a 3D asset whose Format is USDA (the default), USDZ, glTF or OBJ, and Export writes each of its solids as a mesh with a material in its paint's color (`assets.add3D`, `assets.set3DFormat`, `assets.export3D`; [export 3D vector artwork](https://helpx.adobe.com/illustrator/desktop/special-effects-styles/create-3d-graphics/export-3d-vector-artwork.html)). - Export 3D Object builds each Extrude & Bevel (both kinds) and Inflate solid once: the same build gives the faces that count toward the object's limit and the faces of its mesh, and the meshes match those of a separate build bit for bit. Tests `mesh::tests::a_meshed_solid_is_built_once`, `an_extruded_solid_and_its_mesh_match_the_two_built_apart`, `inflated_solids_and_their_meshes_match_the_two_built_apart`, `solids::tests_mesh::one_build_exports_the_meshes_built_apart`, `a_stroke_past_the_limits_is_left_out_of_the_export`. - Export 3D Object writes the graphics mapped onto a plane or onto Extrude & Bevel's front and back caps as a PNG texture of each face, the face's color with its graphics drawn flat and unlit over it, on a mesh of that face with texture coordinates, in all four formats (glTF embeds it, USDZ packs it after the layer, OBJ and USDA write `-texture-1.png` beside them; `assets.export3D` `textureSize`, 128 to 4096 pixels, default 1024, VectorCraft's own values; [the help page](https://helpx.adobe.com/illustrator/desktop/special-effects-styles/create-3d-graphics/map-artwork-on-3d-objects.html)). The graphics wrapped around Revolve's side export the same way, as a texture of the side unrolled as the canvas lays it out, on a mesh of the side's triangles, with its end caps and cut sides plain. Materials and lighting are not exported. Tests `a_caps_mesh_corners_go_back_to_the_art_they_were_made_from`, `a_mesh_carries_its_caps_graphics_flat_in_the_arts_plane`, `each_format_carries_a_textured_part_with_its_coordinates_and_picture`, `export_3d_paints_mapped_graphics_on_their_caps_in_every_format`, `a_revolved_sides_corners_lie_where_the_canvas_places_the_unrolled_side`, `a_revolved_sides_layout_matches_its_drawn_faces_at_every_turn`, `export_3d_paints_graphics_mapped_onto_a_revolves_side_in_every_format`, `a_partial_revolve_keeps_its_cut_sides_plain_beside_its_textured_side`.