mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 22:33:08 +00:00
Export 3D Object: a texture takes the face's and the object's opacity as the canvas draws them
The texture drew its graphics opaque whatever the object's opacity, and a gradient's clear stops would have made the graphics on it clear. - The graphics are drawn at the opacity of the face's fill or stroke, the face at its paint's opacity, and the object's opacity applies to the whole texture, as on the canvas. - A gradient's stops set the face's opacity only, so a graphic on a gradient with clear stops stays visible.
This commit is contained in:
@@ -2277,10 +2277,11 @@ impl SolidMesh {
|
||||
}
|
||||
|
||||
/// The parts of `mesh`, a solid that 3D effect `id` with parameters `p` built about `bounds`, painted
|
||||
/// `color`: for each planar face that carries graphics (Extrude & Bevel's caps, the plane), the
|
||||
/// triangles on it as a mesh of their own with the art point of each corner ([`MeshGraphics`]),
|
||||
/// then the rest. Revolve's side, whose graphics are not mapped onto its mesh, stays plain.
|
||||
fn split_graphics(id: &str, p: &Value, bounds: Rect, mesh: Mesh, color: [f32; 4]) -> Vec<SolidMesh> {
|
||||
/// `color` from a fill or stroke at `opacity`: for each planar face that carries graphics (Extrude &
|
||||
/// Bevel's caps, the plane), the triangles on it as a mesh of their own with the art point of each
|
||||
/// corner ([`MeshGraphics`]), then the rest. Revolve's side, whose graphics are not mapped onto its
|
||||
/// mesh, stays plain.
|
||||
fn split_graphics(id: &str, p: &Value, bounds: Rect, mesh: Mesh, (color, opacity): ([f32; 4], f32)) -> Vec<SolidMesh> {
|
||||
let list = graphics::mapped_graphics(p);
|
||||
if list.is_empty() || !takes_graphics(id) || id == crate::REVOLVE {
|
||||
return vec![SolidMesh::plain(mesh, color)];
|
||||
@@ -2320,7 +2321,8 @@ fn split_graphics(id: &str, p: &Value, bounds: Rect, mesh: Mesh, color: [f32; 4]
|
||||
rest = left;
|
||||
if !taken.is_empty() {
|
||||
let graphics = list.iter().filter(|g| g.face == face).cloned().collect();
|
||||
out.push(SolidMesh { mesh: taken, color, graphics: Some(MeshGraphics { face, graphics, center: bounds.center(), art }) });
|
||||
let graphics = MeshGraphics { face, graphics, center: bounds.center(), art, opacity, object: 1.0 };
|
||||
out.push(SolidMesh { mesh: taken, color, graphics: Some(graphics) });
|
||||
}
|
||||
}
|
||||
if !rest.is_empty() {
|
||||
@@ -2369,15 +2371,15 @@ fn meshes_for(fx: &Effect, shapes: &[Profile], total: &mut usize, limit: usize)
|
||||
let view = view_options(&p);
|
||||
let mut out = vec![];
|
||||
// A solid's parts with and without graphics count together against the limit.
|
||||
let mut keep = |out: &mut Vec<SolidMesh>, mesh: Mesh, color: [f32; 4], bounds: Rect| {
|
||||
split_graphics(&fx.id, &p, bounds, mesh, color).into_iter().try_for_each(|part| keep_mesh(out, total, part, limit))
|
||||
let mut keep = |out: &mut Vec<SolidMesh>, mesh: Mesh, (paint, opacity): (&Paint, f32), bounds: Rect| {
|
||||
split_graphics(&fx.id, &p, bounds, mesh, (mesh_color(paint, opacity), opacity)).into_iter().try_for_each(|part| keep_mesh(out, total, part, limit))
|
||||
};
|
||||
if is_plane(&fx.id) {
|
||||
for s in shapes {
|
||||
for item in &s.items {
|
||||
let (paint, opacity, _) = paint_of(item, s.opacity);
|
||||
let (area, rule) = region(&s.path, s.rule, item);
|
||||
keep(&mut out, vectorcraft_three_d::plane_mesh(&area, rule, b, view)?, mesh_color(&paint, opacity), b)?;
|
||||
keep(&mut out, vectorcraft_three_d::plane_mesh(&area, rule, b, view)?, (&paint, opacity), b)?;
|
||||
}
|
||||
}
|
||||
return Ok(out);
|
||||
@@ -2414,7 +2416,7 @@ fn meshes_for(fx: &Effect, shapes: &[Profile], total: &mut usize, limit: usize)
|
||||
match solid.mesh() {
|
||||
Ok(mesh) => {
|
||||
let (paint, opacity, _) = paint_of(item, *member);
|
||||
keep(&mut out, mesh, mesh_color(&paint, opacity), bounds)?;
|
||||
keep(&mut out, mesh, (&paint, opacity), bounds)?;
|
||||
}
|
||||
Err(_) if matches!(item, AppearanceItem::Stroke(_)) => {}
|
||||
Err(e) => return Err(e),
|
||||
@@ -2502,6 +2504,9 @@ fn meshes_within(doc: Option<&Document>, n: &Node, total: &mut usize, limit: usi
|
||||
.filter(|m| !m.mesh.is_empty())
|
||||
.map(|mut m| {
|
||||
m.color[3] *= n.opacity.clamp(0.0, 1.0);
|
||||
if let Some(g) = &mut m.graphics {
|
||||
g.object *= n.opacity.clamp(0.0, 1.0);
|
||||
}
|
||||
m
|
||||
})
|
||||
.collect();
|
||||
|
||||
@@ -530,6 +530,12 @@ pub struct MeshGraphics {
|
||||
pub center: Point,
|
||||
/// One for each position of the mesh.
|
||||
pub art: Vec<Point>,
|
||||
/// The opacity of the fill or stroke the face is painted with, which the graphics take too, as
|
||||
/// the canvas draws them; neither a gradient's stops nor the object's own opacity is in it.
|
||||
pub opacity: f32,
|
||||
/// The opacity of the object the face belongs to, which applies to the face and its graphics
|
||||
/// together.
|
||||
pub object: f32,
|
||||
}
|
||||
|
||||
impl MeshGraphics {
|
||||
|
||||
@@ -508,7 +508,7 @@ pub(crate) fn textures(doc: &Document, parts: &[SolidMesh], size: u32, pixels: &
|
||||
));
|
||||
}
|
||||
let renderer = renderer.get_or_insert_with(vectorcraft_render::Renderer::new);
|
||||
Some(picture(doc, renderer, part.color, art, (frame, scale), (w, h))?)
|
||||
Some(picture(doc, renderer, (part.color, g.opacity, g.object), art, (frame, scale), (w, h))?)
|
||||
}
|
||||
};
|
||||
drawn.push((part, frame, png.clone()));
|
||||
@@ -544,17 +544,19 @@ type Drawn<'a> = (&'a SolidMesh, Rect, Option<Picture>);
|
||||
|
||||
/// Do `a` and `b` draw the same graphics on their face?
|
||||
fn same_graphics(a: &MeshGraphics, b: &MeshGraphics) -> bool {
|
||||
a.face == b.face && a.center == b.center && a.graphics == b.graphics
|
||||
a.face == b.face && a.center == b.center && a.graphics == b.graphics && a.opacity == b.opacity && a.object == b.object
|
||||
}
|
||||
|
||||
/// `art` drawn over `color` (at its opacity) as a PNG of `w` × `h` pixels whose top left is `frame`'s
|
||||
/// top left, at `scale` pixels a point, with a grayscale PNG of its alpha when some of it is not
|
||||
/// opaque.
|
||||
/// `art` drawn over `color` as a PNG of `w` × `h` pixels whose top left is `frame`'s top left, at
|
||||
/// `scale` pixels a point, with a grayscale PNG of its alpha when some of it is not opaque. As on
|
||||
/// the canvas, the art is drawn at `face` (the opacity of the face's fill or stroke), the color at
|
||||
/// its own opacity without `object`'s, and `object` (the object's opacity) applies to the whole
|
||||
/// picture.
|
||||
fn picture(
|
||||
doc: &Document,
|
||||
renderer: &mut vectorcraft_render::Renderer,
|
||||
color: [f32; 4],
|
||||
art: Node,
|
||||
(color, face, object): ([f32; 4], f32, f32),
|
||||
mut art: Node,
|
||||
(frame, scale): (Rect, f64),
|
||||
(w, h): (u32, u32),
|
||||
) -> Result<Picture, String> {
|
||||
@@ -563,8 +565,13 @@ fn picture(
|
||||
let back = Rect::new(frame.x0 - reach, frame.y0 - reach, frame.x0 + f64::from(w) / scale + reach, frame.y0 + f64::from(h) / scale + reach);
|
||||
let fill = Paint::solid(Color::rgb(color[0], color[1], color[2]));
|
||||
let mut ground = Node::path(NodeId(0), vectorcraft_geom::shapes::rectangle(back), Appearance::basic(fill, Paint::None, 0.0));
|
||||
ground.opacity = color[3];
|
||||
let group = Arc::new(Node::group(NodeId(0), vec![Arc::new(ground), Arc::new(art)]));
|
||||
let unit = |v: f32| if v.is_finite() { v.clamp(0.0, 1.0) } else { 0.0 };
|
||||
let object = unit(object);
|
||||
ground.opacity = if object > 0.0 { unit(color[3] / object) } else { 0.0 };
|
||||
art.opacity *= unit(face);
|
||||
let mut group = Node::group(NodeId(0), vec![Arc::new(ground), Arc::new(art)]);
|
||||
group.opacity = object;
|
||||
let group = Arc::new(group);
|
||||
let (pw, ph) = (u16::try_from(w).map_err(|_| "texture too large")?, u16::try_from(h).map_err(|_| "texture too large")?);
|
||||
let view = Affine::scale(scale) * Affine::translate((-frame.x0, -frame.y0));
|
||||
let drawn = renderer.render_node(doc, &group, pw, ph, view).ok_or_else(|| "the graphics could not be drawn".to_string())?;
|
||||
|
||||
@@ -274,7 +274,10 @@ fn export_3d_paints_mapped_graphics_on_their_caps_in_every_format() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_half_opaque_face_keeps_its_graphics_opaque_in_the_texture() {
|
||||
fn a_texture_takes_the_opacity_of_the_fill_and_of_the_object_as_the_canvas_draws_them() {
|
||||
let half = |a: u8| (120..=135).contains(&a);
|
||||
// A half-opaque fill: the face and the graphic over it are each drawn at half opacity, so the
|
||||
// graphic covers three quarters.
|
||||
let (mut s, asset) = mapped("usda");
|
||||
let doc = &s.active().unwrap().doc;
|
||||
let id = doc.asset(asset).unwrap().nodes[0];
|
||||
@@ -282,11 +285,9 @@ fn a_half_opaque_face_keeps_its_graphics_opaque_in_the_texture() {
|
||||
run(&mut s, "select.set", json!({"ids": [id.0]}));
|
||||
run(&mut s, "transparency.set", json!({"item": fill, "opacity": 50}));
|
||||
let out = files(&run(&mut s, "assets.export3D", json!({"textureSize": 128})));
|
||||
// The graphic stays opaque where it covers the face, and the face around it is half opaque.
|
||||
let front = &out[1].1;
|
||||
assert_eq!(pixel(front, 64, 64), [255, 0, 0, 255]);
|
||||
let around = pixel(front, 2, 2);
|
||||
assert!(around[2] > 200 && (120..=135).contains(&around[3]), "{around:?}");
|
||||
let (on, around) = (pixel(&out[1].1, 64, 64), pixel(&out[1].1, 2, 2));
|
||||
assert!(on[0] > on[2] && (185..=197).contains(&on[3]), "{on:?}");
|
||||
assert!(around[2] > 200 && half(around[3]), "{around:?}");
|
||||
let usda = String::from_utf8(out[0].1.clone()).unwrap();
|
||||
assert!(usda.contains("float inputs:opacity.connect"), "the texture's alpha is the opacity");
|
||||
// An OBJ's opacity map has the alpha as gray levels.
|
||||
@@ -294,8 +295,27 @@ fn a_half_opaque_face_keeps_its_graphics_opaque_in_the_texture() {
|
||||
let out = files(&run(&mut s, "assets.export3D", json!({"textureSize": 128})));
|
||||
assert_eq!(out[3].0, "Box-texture-1-opacity.png");
|
||||
let gray = image::load_from_memory(&out[3].1).unwrap().to_luma8();
|
||||
assert_eq!(gray.get_pixel(64, 64).0, [255]);
|
||||
assert!((120..=135).contains(&gray.get_pixel(2, 2).0[0]), "{:?}", gray.get_pixel(2, 2));
|
||||
let (g_on, g_around) = (gray.get_pixel(64, 64).0[0], gray.get_pixel(2, 2).0[0]);
|
||||
assert!((185..=197).contains(&g_on) && half(g_around), "{g_on} {g_around}");
|
||||
// A half-opaque object: the face and the graphic are drawn opaque, and the whole picture at
|
||||
// half opacity.
|
||||
let (mut s, asset) = mapped("usda");
|
||||
let id = s.active().unwrap().doc.asset(asset).unwrap().nodes[0];
|
||||
run(&mut s, "transparency.set", json!({"ids": [id.0], "opacity": 50}));
|
||||
let out = files(&run(&mut s, "assets.export3D", json!({"textureSize": 128})));
|
||||
let (on, around) = (pixel(&out[1].1, 64, 64), pixel(&out[1].1, 2, 2));
|
||||
assert!(on[0] > 200 && on[2] < 50 && half(on[3]), "{on:?}");
|
||||
assert!(around[2] > 200 && around[0] < 50 && half(around[3]), "{around:?}");
|
||||
// A gradient whose stops are clear: the face is clear, and the graphic keeps its opacity.
|
||||
let (mut s, asset) = mapped("usda");
|
||||
let id = s.active().unwrap().doc.asset(asset).unwrap().nodes[0];
|
||||
run(&mut s, "select.set", json!({"ids": [id.0]}));
|
||||
let clear = json!({"stops": [{"offset": 0, "color": "#0000ff", "opacity": 0}, {"offset": 1, "color": "#0000ff", "opacity": 0}]});
|
||||
run(&mut s, "paint.setFill", json!({"gradient": clear}));
|
||||
let out = files(&run(&mut s, "assets.export3D", json!({"textureSize": 128})));
|
||||
let (on, around) = (pixel(&out[1].1, 64, 64), pixel(&out[1].1, 2, 2));
|
||||
assert!(is_red(on), "{on:?}");
|
||||
assert_eq!(around[3], 0, "{around:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user