mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 19:40:21 +00:00
3D and Materials: the Export 3D Object button's state is worked out once per document revision
The 3D and Materials panel checked on every frame whether a selected object has a 3D effect that Export 3D Object exports, looking up each selected object's parent and the node twice. With a plain selection of 200,000 paths that scan ran in full on every frame, about 60 ms of thread CPU time. The state is now kept in the canvas cache for the document's uid and revision, beside the other per-revision panel states, and the button shows the same state.
This commit is contained in:
@@ -89,6 +89,7 @@ impl VectorcraftApp {
|
||||
selection_box: None,
|
||||
selection_bounds: None,
|
||||
content_bounds: None,
|
||||
three_d_exportable: None,
|
||||
pixel_aligned: None,
|
||||
plugin_keys: Default::default(),
|
||||
placed_keys: Default::default(),
|
||||
|
||||
@@ -61,6 +61,9 @@ pub struct CanvasCache {
|
||||
pub selection_bounds: Option<((u64, u64), Option<vectorcraft_geom::Rect>)>,
|
||||
/// [`VectorcraftApp::content_bounds`] for (document uid, revision).
|
||||
pub content_bounds: Option<((u64, u64), Option<vectorcraft_geom::Rect>)>,
|
||||
/// Whether the 3D and Materials panel's Export 3D Object button is available (a selected
|
||||
/// object has a 3D effect that `assets.add3D` exports) for (document uid, revision).
|
||||
pub three_d_exportable: Option<((u64, u64), bool)>,
|
||||
/// The Transform panel's Align to Pixel Grid state (on, off or mixed, and whether every
|
||||
/// selected object can be aligned) for (document uid, revision).
|
||||
pub pixel_aligned: Option<((u64, u64), Option<PixelAligned>)>,
|
||||
|
||||
@@ -1259,6 +1259,180 @@ fn export_3d_object_leaves_out_a_group_with_a_3d_fill() {
|
||||
assert_eq!(list.iter().map(|a| a["nodes"].clone()).collect::<Vec<_>>(), [json!([a])]);
|
||||
}
|
||||
|
||||
/// Whether Export 3D Object is available as the panel reads it ([`super::can_export_3d_shown`]),
|
||||
/// checked against the state worked out from scratch ([`super::can_export_3d`]); with a document
|
||||
/// open, the panel keeps it for the document's uid and revision.
|
||||
fn exportable(app: &mut VectorcraftApp, why: &str) -> bool {
|
||||
let fresh = super::can_export_3d(app);
|
||||
assert_eq!(super::can_export_3d_shown(app), fresh, "{why}");
|
||||
if let Some(key) = app.session.active().map(|st| (st.uid, st.revision)) {
|
||||
assert_eq!(app.canvas.three_d_exportable, Some((key, fresh)), "{why}");
|
||||
}
|
||||
fresh
|
||||
}
|
||||
|
||||
/// The Export 3D Object state the panel keeps for a revision equals the state worked out from
|
||||
/// scratch: with no document and no selection, for plain paths, with a 3D object selected first or
|
||||
/// last, for a path of a compound path whose compound path has the effect, after the effect is
|
||||
/// hidden, Undo and Redo, in another document and back, and for a group whose 3D effect moves onto
|
||||
/// its fill.
|
||||
#[test]
|
||||
fn export_3d_object_state_kept_for_a_revision_equals_a_fresh_one() {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Services::default());
|
||||
assert!(!exportable(&mut app, "no document"));
|
||||
app.run("file.new", json!({"width": 400, "height": 400})).unwrap();
|
||||
assert!(!exportable(&mut app, "no selection"));
|
||||
let rect = |app: &mut VectorcraftApp, x: f64| {
|
||||
app.run("shape.rectangle", json!({"x": x, "y": 10, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap()
|
||||
};
|
||||
let (p, q) = (rect(&mut app, 10.0), rect(&mut app, 40.0));
|
||||
app.run("select.set", json!({"ids": [p, q]})).unwrap();
|
||||
assert!(!exportable(&mut app, "plain paths"));
|
||||
let solid = rect(&mut app, 70.0);
|
||||
app.run("effect.apply", json!({"effect": "threeD.extrude", "item": null})).unwrap();
|
||||
app.run("select.set", json!({"ids": [solid, p, q]})).unwrap();
|
||||
assert!(exportable(&mut app, "a 3D object first"));
|
||||
app.run("select.set", json!({"ids": [p, q]})).unwrap();
|
||||
assert!(!exportable(&mut app, "plain paths again"));
|
||||
app.run("select.set", json!({"ids": [p, q, solid]})).unwrap();
|
||||
assert!(exportable(&mut app, "a 3D object last"));
|
||||
let (a, b) = (rect(&mut app, 100.0), rect(&mut app, 110.0));
|
||||
app.run("select.set", json!({"ids": [a, b]})).unwrap();
|
||||
app.run("object.compoundPath.make", json!({})).unwrap();
|
||||
let compound = app.session.active().unwrap().selection.objects[0].0;
|
||||
app.run("effect.apply", json!({"effect": "threeD.extrude", "item": null})).unwrap();
|
||||
let member = app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(compound)).unwrap().children().unwrap()[0].id.0;
|
||||
app.run("select.set", json!({"ids": [p, q]})).unwrap();
|
||||
assert!(!exportable(&mut app, "plain paths before the compound path's member"));
|
||||
app.run("select.set", json!({"ids": [p, member]})).unwrap();
|
||||
assert!(exportable(&mut app, "a path of a 3D compound path"));
|
||||
// An edit that leaves the selection as it is.
|
||||
app.run("effect.setParams", json!({"ids": [compound], "index": 0, "item": null, "visible": false})).unwrap();
|
||||
assert!(!exportable(&mut app, "the compound path's 3D effect hidden"));
|
||||
app.run("edit.undo", json!({})).unwrap();
|
||||
assert!(exportable(&mut app, "Undo"));
|
||||
app.run("edit.redo", json!({})).unwrap();
|
||||
assert!(!exportable(&mut app, "Redo"));
|
||||
app.run("edit.undo", json!({})).unwrap();
|
||||
assert!(exportable(&mut app, "Undo again"));
|
||||
app.run("file.new", json!({"width": 400, "height": 400})).unwrap();
|
||||
assert!(!exportable(&mut app, "a new document"));
|
||||
app.session.set_active(0);
|
||||
assert!(exportable(&mut app, "back to the first document"));
|
||||
// A group's own 3D effect exports; the same effect on its fill does not.
|
||||
app.run("select.set", json!({"ids": [p, q]})).unwrap();
|
||||
let group = app.run("object.group", json!({})).unwrap()["id"].as_u64().unwrap();
|
||||
app.run("select.set", json!({"ids": [group]})).unwrap();
|
||||
app.run("appearance.addFill", json!({})).unwrap();
|
||||
app.run("effect.apply", json!({"effect": "threeD.extrude"})).unwrap();
|
||||
assert!(exportable(&mut app, "a group with its own 3D effect"));
|
||||
app.session
|
||||
.edit("fill", |d, _| {
|
||||
let n = d.node_mut(vectorcraft_doc::NodeId(group)).unwrap();
|
||||
let fx = n.appearance.effects.remove(0);
|
||||
n.appearance.items[0].effects_mut().push(fx);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
assert!(!exportable(&mut app, "a group with a 3D effect on its fill"));
|
||||
}
|
||||
|
||||
/// A frame of the panel works out the Export 3D Object state once and keeps it for the document's
|
||||
/// revision: later frames read it, and a new revision works it out again.
|
||||
#[test]
|
||||
fn export_3d_object_state_is_worked_out_once_per_revision() {
|
||||
let key = |app: &VectorcraftApp| app.session.active().map(|st| (st.uid, st.revision)).unwrap();
|
||||
let mut app = app();
|
||||
painted_text(&mut app, show);
|
||||
assert_eq!(app.canvas.three_d_exportable, Some((key(&app), false)));
|
||||
// A wrong answer planted for this revision is what the panel reads: nothing works it out again.
|
||||
app.canvas.three_d_exportable = Some((key(&app), true));
|
||||
assert!(super::can_export_3d_shown(&mut app));
|
||||
painted_text(&mut app, show);
|
||||
assert_eq!(app.canvas.three_d_exportable, Some((key(&app), true)));
|
||||
app.run("select.none", json!({})).unwrap();
|
||||
painted_text(&mut app, show);
|
||||
assert_eq!(app.canvas.three_d_exportable, Some((key(&app), false)));
|
||||
}
|
||||
|
||||
/// The thread's CPU time in nanoseconds, where Linux gives it.
|
||||
fn thread_cpu_ns() -> Option<u128> {
|
||||
std::fs::read_to_string("/proc/thread-self/schedstat").ok()?.split_whitespace().next()?.parse().ok()
|
||||
}
|
||||
|
||||
/// Time the Export 3D Object state with 200,000 selected paths: worked out from scratch on every
|
||||
/// frame ([`super::can_export_3d`]), by [`super::can_export_3d_shown`] on the first frame of a
|
||||
/// revision, and by [`super::can_export_3d_shown`] on later frames of the same revision. The first
|
||||
/// case selects plain paths alone; the second adds a path of a compound path with Extrude & Bevel,
|
||||
/// selected last. The sides alternate, and the median time per frame of 7 runs is printed.
|
||||
/// Run with `cargo test --release -p vectorcraft-ui-egui --lib export_3d_object_state_timing -- --ignored --nocapture`.
|
||||
#[test]
|
||||
#[ignore = "manual timing probe; performance depends on machine load"]
|
||||
fn export_3d_object_state_timing() {
|
||||
const PATHS: usize = 200_000;
|
||||
const MEASURES: u32 = 20;
|
||||
const KEPT: u32 = 1_000_000;
|
||||
let mut app = VectorcraftApp::new(Session::new(), Services::default());
|
||||
app.run("file.new", json!({"width": 2000, "height": 2000})).unwrap();
|
||||
let rect = app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap()["id"].as_u64().unwrap();
|
||||
let rect = app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(rect)).cloned().unwrap();
|
||||
app.session
|
||||
.edit("Fill", |d, _| {
|
||||
let layer = d.layers[0].id;
|
||||
for _ in 0..PATHS - 1 {
|
||||
let mut n = rect.clone();
|
||||
n.id = d.alloc_id();
|
||||
d.insert(Some(layer), usize::MAX, n)?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
app.run("select.all", json!({})).unwrap();
|
||||
let plain: Vec<u64> = app.session.active().unwrap().selection.objects.iter().map(|id| id.0).collect();
|
||||
let a = app.run("shape.rectangle", json!({"x": 0, "y": 1000, "width": 30, "height": 30})).unwrap()["id"].clone();
|
||||
let b = app.run("shape.rectangle", json!({"x": 5, "y": 1005, "width": 10, "height": 10})).unwrap()["id"].clone();
|
||||
app.run("select.set", json!({"ids": [a, b]})).unwrap();
|
||||
app.run("object.compoundPath.make", json!({})).unwrap();
|
||||
let compound = app.session.active().unwrap().selection.objects[0];
|
||||
app.run("effect.apply", json!({"effect": "threeD.extrude", "item": null})).unwrap();
|
||||
let member = app.session.active().unwrap().doc.node(compound).unwrap().children().unwrap()[0].id.0;
|
||||
let mut last = plain.clone();
|
||||
last.push(member);
|
||||
let median = |v: &mut Vec<f64>| {
|
||||
v.sort_by(f64::total_cmp);
|
||||
v[v.len() / 2]
|
||||
};
|
||||
let clock = if thread_cpu_ns().is_some() { "thread CPU time" } else { "wall-clock time" };
|
||||
for (name, ids, expected) in [("plain paths", plain, false), ("plain paths, then a path of a 3D compound path", last, true)] {
|
||||
app.run("select.set", json!({"ids": &ids})).unwrap();
|
||||
let selected: Vec<u64> = app.session.active().unwrap().selection.objects.iter().map(|id| id.0).collect();
|
||||
assert_eq!(selected.len(), ids.len(), "{name}");
|
||||
assert_eq!(selected.last(), ids.last(), "{name}");
|
||||
assert_eq!(super::can_export_3d(&app), expected, "{name}");
|
||||
let mut time = |frames: u32, f: &mut dyn FnMut(&mut VectorcraftApp) -> bool| {
|
||||
let (cpu, wall) = (thread_cpu_ns(), std::time::Instant::now());
|
||||
for _ in 0..frames {
|
||||
assert_eq!(f(&mut app), expected);
|
||||
}
|
||||
let ns = thread_cpu_ns().zip(cpu).map_or_else(|| wall.elapsed().as_nanos(), |(b, a)| b - a);
|
||||
ns as f64 / f64::from(frames) / 1000.0
|
||||
};
|
||||
let (mut full, mut first, mut kept) = (vec![], vec![], vec![]);
|
||||
for _ in 0..7 {
|
||||
full.push(time(MEASURES, &mut |app| super::can_export_3d(app)));
|
||||
first.push(time(MEASURES, &mut |app| {
|
||||
app.canvas.three_d_exportable = None;
|
||||
super::can_export_3d_shown(app)
|
||||
}));
|
||||
kept.push(time(KEPT, &mut super::can_export_3d_shown));
|
||||
}
|
||||
eprintln!("{name}: {} objects selected, available: {expected}; {clock} per frame, median of 7 runs", selected.len());
|
||||
eprintln!("every frame worked out ({MEASURES} frames a run): {:>12.3} us {full:.3?}", median(&mut full));
|
||||
eprintln!("first frame of a revision ({MEASURES} a run): {:>12.3} us {first:.3?}", median(&mut first));
|
||||
eprintln!("later frames ({KEPT} frames a run): {:>12.4} us {kept:.4?}", median(&mut kept));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn changed_settings_lists_only_new_values() {
|
||||
let before = json!({"depth": 50.0, "cap": true, "bevel": "none"});
|
||||
|
||||
@@ -278,9 +278,9 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
}
|
||||
|
||||
/// The Export 3D Object button, available while a selected object has a 3D effect that
|
||||
/// `assets.add3D` exports ([`can_export_3d`]). An error goes to the status bar.
|
||||
/// `assets.add3D` exports ([`can_export_3d_shown`]). An error goes to the status bar.
|
||||
fn export_button(app: &mut VectorcraftApp, ui: &mut Ui, ctx: &egui::Context) {
|
||||
let any = can_export_3d(app);
|
||||
let any = can_export_3d_shown(app);
|
||||
ui.add_space(10.0);
|
||||
if ui.add_enabled(any, egui::Button::new(tl!("Export 3D Object"))).clicked() {
|
||||
finish(app, ctx);
|
||||
@@ -299,6 +299,22 @@ pub(crate) fn can_export_3d(app: &VectorcraftApp) -> bool {
|
||||
st.selection.objects.iter().any(|id| st.doc.node(compound(*id).unwrap_or(*id)).is_some_and(vectorcraft_effects::exports_three_d))
|
||||
}
|
||||
|
||||
/// [`can_export_3d`], worked out once per (document uid, revision)
|
||||
/// ([`crate::caches::CanvasCache::three_d_exportable`]): the panel reads it every frame, and Select
|
||||
/// All can select hundreds of thousands of objects.
|
||||
pub(crate) fn can_export_3d_shown(app: &mut VectorcraftApp) -> bool {
|
||||
let Some(st) = app.session.active() else { return false };
|
||||
let key = (st.uid, st.revision);
|
||||
if let Some((k, any)) = app.canvas.three_d_exportable
|
||||
&& k == key
|
||||
{
|
||||
return any;
|
||||
}
|
||||
let any = can_export_3d(app);
|
||||
app.canvas.three_d_exportable = Some((key, any));
|
||||
any
|
||||
}
|
||||
|
||||
/// Export 3D Object: adds the selected objects that have a 3D effect to Asset Export as 3D assets
|
||||
/// (`assets.add3D` on the selection, one undo step, none when each is an asset already), selects
|
||||
/// their assets there and shows the Asset Export panel. The command leaves out the selected
|
||||
|
||||
@@ -1470,6 +1470,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
- 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 `<name>-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)). Graphics on Revolve's side, 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`.
|
||||
- The 3D and Materials panel works out whether Export 3D Object is available once for each document revision and reuses it on later frames. The button shows the same state. With 200,000 paths selected, working it out on every frame took a median 61.6 ms of thread CPU time per frame in a release build on Linux (57.1 ms with a path of a compound path with Extrude & Bevel selected last); the first frame after a change still takes about as long, and later frames take about 2 ns (`panels::three_d::tests::export_3d_object_state_kept_for_a_revision_equals_a_fresh_one`, `export_3d_object_state_is_worked_out_once_per_revision`, `export_3d_object_state_timing`).
|
||||
- Checking an Extrude & Bevel (both kinds) or Inflate effect builds each solid the effect draws once, about the bounds the effect draws it about; a shape with no fill still has its profile built on its own. Tests `solids::tests::the_check_builds_each_drawn_solid_once`, `the_check_builds_the_profile_of_a_shape_with_no_fill`.
|
||||
- Initial live Revolve (#605, #846): a portable geometry and shading crate (`three-d`), editable path and compound profiles, compact options with artboard rotation and light gizmos, SVG/PDF vector output and Expand Appearance that can trim covered faces. Open ends have continuous caps by default, shading is smooth with one to ten colored lights, and intersecting surfaces' visibility is approximate ([revolve.md](revolve.md)).
|
||||
- Revolve takes graphics on its swept side, shaded with the object's light ([the archived reference, page 185](https://web.archive.org/web/20210601162814/https://helpx.adobe.com/pdf/illustrator_reference.pdf#page=185)). By VectorCraft's own rule a graphic wraps around the axis at its own size at every distance from it, narrows toward the silhouette, follows the profile along its length and is hidden where the side faces away (`threeD.mapGraphic`; `revolve::tests_graphics`, `tests::side_faces_lie_on_the_unrolled_surface_and_caps_do_not`, `map_graphic_puts_a_graphic_on_the_side_of_a_revolve`).
|
||||
|
||||
Reference in New Issue
Block a user