Skip to content

ProCraft for Android: An Android native painting app written in Rust

Rust

feat(3d): a stroke on a model's colour lays down the brush's roughness and metalness too

Josh644dfef8eef7
diff --git a/apps/procraft-android/src/lib.rs b/apps/procraft-android/src/lib.rs
index 851a166..edafe2f 100644
--- a/apps/procraft-android/src/lib.rs
+++ b/apps/procraft-android/src/lib.rs
@@ -418,6 +418,11 @@ impl State {
/// Finishes an open stroke.
fn finish_stroke(&mut self) {
self.shape = None;
+ // a stroke on a model ends there, as the pen lifting would end it
+ if self.scene_shown() && self.scene.as_ref().is_some_and(|s| s.stroking) {
+ self.scene_stroke_add(&[], true);
+ return;
+ }
if let (Some(open), Some(canvas)) = (self.stroke.take(), self.canvas.as_mut()) {
close_stroke(canvas, open, &self.doc.stroke_guide());
self.bake_painted_on();
@@ -1458,9 +1463,15 @@ impl CanvasSession {
return;
}
st.settle();
+ st.flush();
+ let before = st.canvas.as_ref().map(|c| (c.undo_step(), c.redo_step()));
if let Some(canvas) = st.canvas.as_mut() {
f(canvas);
st.flush();
+ // a stroke on a model's colour painted its roughness and metalness too
+ if let Some(before) = before {
+ st.follow_joint_step(before);
+ }
st.sync_selection();
st.revision += 1;
self.shared.frame(st);
diff --git a/apps/procraft-android/src/scene3d.rs b/apps/procraft-android/src/scene3d.rs
index db9a0c7..ef62c9c 100644
--- a/apps/procraft-android/src/scene3d.rs
+++ b/apps/procraft-android/src/scene3d.rs
@@ -17,9 +17,10 @@ use procraft_engine::geom::{IRect, Vec2};
use procraft_engine::input::Sample;
use procraft_engine::mesh::{MAX_TRIANGLES, Mesh, MeshError, PartImages, read_obj};
use procraft_engine::stroke::StrokeBuilder;
+use procraft_engine::studio::MAX_GRAIN_SCALE;
use procraft_engine::usd::{read_usd, read_usdc, read_usdz};
use procraft_engine::view::View;
-use procraft_gpu::{Canvas, Light, Lighting, MOST_LIGHTS, MeshView, Orbit, StrokeTool, wgpu};
+use procraft_gpu::{Canvas, Light, Lighting, MOST_LIGHTS, MeshView, Orbit, StrokeStyle, StrokeTool, wgpu};
use crate::project::{THUMBNAIL_SIDE, file_error, pixels_hash, read_png, write_atomically, write_png};
use crate::{CanvasSession, MobileError, PenSample, PointerKind, State, ToolKind};
@@ -282,8 +283,38 @@ pub(crate) struct Scene {
/// A stroke is being carried onto the model each frame; `ending`: once more, then it ends.
pub(crate) stroking: bool,
ending: bool,
+ /// What the stroke on the current part's colour lays down in its roughness and metalness as
+ /// well (a stroke on the colour paints all three), while it goes on.
+ material_paint: Option<[MaterialPaint; 2]>,
+ /// The brush's roughness and metalness sources as textures, with their keys.
+ material_sources: [Option<(u64, wgpu::TextureView)>; 2],
+ /// Strokes that painted the colour, roughness and metalness together: the step each made in
+ /// each one's canvas, so undo and redo take them together.
+ joint_steps: Vec<JointStep>,
}
+/// What a stroke on a part's colour lays down in its roughness or metalness: the brush's amount
+/// (0..1), one tile of its source every so many canvas pixels, and the source's key (none: the
+/// amount all over).
+#[derive(Clone, Copy, Debug, PartialEq)]
+struct MaterialPaint {
+ amount: f32,
+ tile: f32,
+ source: Option<u64>,
+}
+
+/// A stroke that painted part `part`'s colour, roughness and metalness: the step it made in each
+/// (as [`Canvas::undo_step`] numbers them), if it made one there.
+#[derive(Clone, Copy, Debug, PartialEq)]
+struct JointStep {
+ part: usize,
+ steps: [Option<u64>; 3],
+}
+
+/// The most strokes remembered as painting every channel (older ones are long out of the undo
+/// history).
+const MOST_JOINT_STEPS: usize = 512;
+
impl Scene {
/// `mesh` with a texture set `texture_size` square for each of its parts, set `current` the
/// session's own; the others are `waiting` where given, else new.
@@ -332,6 +363,9 @@ impl Scene {
surface_shown: false,
stroking: false,
ending: false,
+ material_paint: None,
+ material_sources: [None, None],
+ joint_steps: Vec::new(),
}
}
@@ -1721,6 +1755,58 @@ impl State {
canvas.begin_drawn_stroke(current, canvas_style);
}
self.stroke = Some(builder);
+ self.begin_material_strokes(&brush, canvas_style, current);
+ }
+
+ /// A stroke on a part's colour paints its roughness and metalness too, on the same layer and
+ /// with the same coverage: the brush's amount of each through its source (an eraser takes
+ /// all three away). A stroke on one of those channels paints only it.
+ fn begin_material_strokes(&mut self, brush: &procraft_engine::brush::Brush, style: StrokeStyle, layer: LayerId) {
+ let structure = self.doc.clone();
+ let (Some(gpu), Some(scene)) = (self.gpu.as_ref(), self.scene.as_mut()) else { return };
+ scene.material_paint = None;
+ if scene.channel != 0 {
+ return;
+ }
+ let (part, side) = (scene.current, scene.texture_size);
+ let channels = [
+ (brush.setting("roughness_amount"), brush.setting("roughness_scale"), &brush.roughness),
+ (brush.setting("metallic_amount"), brush.setting("metallic_scale"), &brush.metallic),
+ ];
+ let mut paint = [MaterialPaint { amount: 0.0, tile: 1.0, source: None }; 2];
+ for (k, ((amount, scale, source), out)) in channels.into_iter().zip(&mut paint).enumerate() {
+ // the channel's document, made now if it never was, with the colour's layers
+ let Some(slot) = scene.slot(part, k + 1) else { return };
+ let set = slot.get_or_insert_with(|| TextureSet::empty(side));
+ follow_layers(set, &structure);
+ if set.canvas.is_none() {
+ let Ok(mut made) = Canvas::new(gpu, &set.doc) else { return };
+ for (id, pixels) in std::mem::take(&mut set.pending) {
+ if let Err(e) = made.upload_layer(gpu, id, &pixels) {
+ log::error!("opening a texture set's layer: {e}");
+ }
+ }
+ set.canvas = Some(made);
+ }
+ let Some(canvas) = set.canvas.as_mut() else { return };
+ let amount = amount.clamp(0.0, 1.0);
+ // the colour's coverage, laid down plainly: no blend mode, grain or wet paint of its own
+ let plain =
+ StrokeStyle { color: [amount; 3], blend: 0, burnt_edges: 0.0, grain: None, wet_pull: None, ..style };
+ canvas.begin_drawn_stroke(layer, plain);
+ let key = crate::studio::source_key(source);
+ let cached = scene.material_sources.get(k).and_then(|c| c.as_ref()).map(|(key, _)| *key);
+ if key.is_some() && cached != key {
+ let made = crate::brush_files::material_pixels(source).and_then(|pixels| {
+ procraft_gpu::material_source(gpu, procraft_engine::textures::TEXTURE_SIZE as u32, &pixels)
+ });
+ if let Some(slot) = scene.material_sources.get_mut(k) {
+ *slot = key.zip(made);
+ }
+ }
+ *out = MaterialPaint { amount, tile: (scale * MAX_GRAIN_SCALE).max(4.0), source: key };
+ }
+ scene.material_paint = Some(paint);
}
pub(crate) fn scene_stroke_add(&mut self, samples: &[PenSample], last: bool) {
@@ -1749,6 +1835,13 @@ impl State {
if let Some((_, screen)) = scene.screen.as_mut() {
screen.cancel_stroke();
}
+ if scene.material_paint.take().is_some()
+ && let Some(channels) = scene.channels.get_mut(scene.current)
+ {
+ for canvas in channels.iter_mut().flatten().filter_map(|s| s.canvas.as_mut()) {
+ canvas.cancel_stroke();
+ }
+ }
if let Some(canvas) = self.canvas.as_mut() {
canvas.cancel_stroke();
}
@@ -1810,10 +1903,52 @@ impl State {
size.1,
canvas.stroke_target(),
);
+ // the roughness and metalness take the colour's coverage as it is now
+ let part = scene.current;
+ let materials = scene.material_paint.zip(scene.channels.get_mut(part));
+ if let Some((paint, channels)) = &materials {
+ for (k, (look, set)) in paint.iter().zip(channels.iter()).enumerate() {
+ let Some(target) = set.as_ref().and_then(|s| s.canvas.as_ref()) else { continue };
+ let source = scene
+ .material_sources
+ .get(k)
+ .and_then(Option::as_ref)
+ .filter(|(key, _)| Some(*key) == look.source)
+ .map(|(_, view)| view);
+ let look = procraft_gpu::MaterialLook { amount: look.amount, source, tile: look.tile };
+ view.project_material(gpu, &mut encoder, part, k, canvas.stroke_view(), &look, target.stroke_target());
+ }
+ }
gpu.queue.submit([encoder.finish()]);
canvas.stroke_drawn(reach);
+ let mut sets: Vec<&mut TextureSet> =
+ materials.map(|(_, channels)| channels.iter_mut().flatten().collect()).unwrap_or_default();
+ for set in &mut sets {
+ if let Some(c) = set.canvas.as_mut() {
+ c.stroke_drawn(reach);
+ c.render(gpu, &mut set.doc, None);
+ }
+ }
if scene.ending {
- canvas.end_stroke();
+ // each canvas's step made now, to be undone and redone together
+ let made = |c: &mut Canvas, doc: &mut Document| {
+ let before = c.steps_made();
+ c.end_stroke();
+ c.render(gpu, doc, None);
+ (c.steps_made() > before).then(|| c.undo_step()).flatten()
+ };
+ let mut steps = [made(canvas, &mut self.doc), None, None];
+ for (step, set) in steps.iter_mut().skip(1).zip(&mut sets) {
+ if let Some(c) = set.canvas.as_mut() {
+ *step = made(c, &mut set.doc);
+ }
+ }
+ if scene.material_paint.take().is_some() && steps.iter().flatten().count() > 1 {
+ if scene.joint_steps.len() >= MOST_JOINT_STEPS {
+ scene.joint_steps.remove(0);
+ }
+ scene.joint_steps.push(JointStep { part, steps });
+ }
screen.cancel_stroke();
screen.render(gpu, screen_doc, None);
scene.stroking = false;
@@ -1821,6 +1956,42 @@ impl State {
}
}
+ /// After an undo or redo (`before`: the session canvas's next undo and redo steps before
+ /// it), the rest of a stroke that painted every channel follows: the step it made in each
+ /// other channel is undone or redone too, if that is that channel's next step.
+ pub(crate) fn follow_joint_step(&mut self, before: (Option<u64>, Option<u64>)) {
+ let Some(canvas) = self.canvas.as_ref() else { return };
+ let after = (canvas.undo_step(), canvas.redo_step());
+ let (step, undone) = match (before, after) {
+ ((Some(s), _), (u, Some(r))) if r == s && u != Some(s) => (s, true),
+ ((_, Some(s)), (Some(u), r)) if u == s && r != Some(s) => (s, false),
+ _ => return,
+ };
+ let (Some(gpu), Some(scene)) = (self.gpu.as_ref(), self.scene.as_mut()) else { return };
+ let (part, channel) = (scene.current, scene.channel);
+ let Some(joint) =
+ scene.joint_steps.iter().find(|j| j.part == part && j.steps.get(channel) == Some(&Some(step))).copied()
+ else {
+ return;
+ };
+ for (k, want) in joint.steps.iter().enumerate() {
+ let Some(want) = *want else { continue };
+ if k == channel {
+ continue;
+ }
+ let Some(set) = scene.slot(part, k).and_then(Option::as_mut) else { continue };
+ let Some(c) = set.canvas.as_mut() else { continue };
+ if undone && c.undo_step() == Some(want) {
+ c.undo();
+ } else if !undone && c.redo_step() == Some(want) {
+ c.redo();
+ } else {
+ continue;
+ }
+ c.render(gpu, &mut set.doc, None);
+ }
+ }
+
/// Draws a frame of the model into `target` (the window's size): the stroke carried onto it,
/// the current texture set composited, the model drawn with each set showing.
#[cfg_attr(not(any(target_os = "android", test)), allow(dead_code))]
@@ -2365,11 +2536,11 @@ mod tests {
dab(&s);
let colour = painted(&s);
assert!(colour > 20, "{colour}");
- // roughness: the same layers, nothing painted in them yet
+ // roughness: the same layers, the dab on the colour laid down there too
assert_eq!(s.model_channel(), 0);
s.set_model_channel(1);
assert_eq!(s.model_channel(), 1);
- assert_eq!(painted(&s), 0);
+ assert!(painted(&s).abs_diff(colour) * 10 < colour, "{} {colour}", painted(&s));
s.stroke_begin(PointerKind::Stylus, vec![sample(70.0, 100.0, 0.0)]);
s.stroke_end(vec![sample(130.0, 100.0, 50.0)]);
frame(&s, 200);
@@ -2382,9 +2553,9 @@ mod tests {
assert_eq!(painted(&s), colour);
assert!(s.layers().iter().any(|l| l.id == added));
// exports take every channel whichever is painted on: the colour and roughness as
- // painted, the metalness (nothing painted) the plain one
+ // painted, the metalness painted with the brush's (none, as the plain one)
s.set_model_channel(2);
- assert_eq!(painted(&s), 0);
+ assert!(painted(&s).abs_diff(colour) * 10 < colour, "{} {colour}", painted(&s));
let dir = std::env::temp_dir().join(format!("procraft-channels-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let pngs = s.export_model_textures(dir.join("t").to_string_lossy().into_owned()).unwrap();
@@ -2419,6 +2590,138 @@ mod tests {
let _ = std::fs::remove_dir_all(&dir);
}
+ /// The current channel's texels as painted: each one's grey (unpremultiplied) and alpha.
+ fn channel_texels(s: &CanvasSession) -> Vec<(u8, u8)> {
+ let mut guard = s.shared.lock();
+ let st = &mut *guard;
+ let (canvas, gpu) = (st.canvas.as_mut().unwrap(), st.gpu.as_ref().unwrap());
+ canvas.render(gpu, &mut st.doc, None);
+ let px = canvas.read_composite(gpu).unwrap();
+ px.chunks(4)
+ .map(|p| (if p[3] == 0 { 0 } else { (u16::from(p[0]) * 255 / u16::from(p[3])) as u8 }, p[3]))
+ .collect()
+ }
+
+ /// The grey of the current channel's fully covered texels, if all are the same within one.
+ fn covered_grey(s: &CanvasSession) -> Option<u8> {
+ let full: Vec<u8> = channel_texels(s).into_iter().filter(|&(_, a)| a == 255).map(|(v, _)| v).collect();
+ let (lo, hi) = (*full.iter().min()?, *full.iter().max()?);
+ (hi - lo <= 1).then_some(lo)
+ }
+
+ /// Sets the paint brush's material controls as the studio does.
+ fn materials(s: &CanvasSession, roughness: f32, metallic: f32) {
+ s.studio_begin();
+ s.set_studio_setting("roughness_amount".into(), roughness);
+ s.set_studio_setting("metallic_amount".into(), metallic);
+ s.studio_end(true);
+ }
+
+ #[test]
+ fn a_stroke_on_the_colour_lays_down_the_brushs_roughness_and_metalness() {
+ let s = CanvasSession::open_model(CUBE.into(), "cube.obj".into(), Vec::new(), "Cube".into(), 64).unwrap();
+ if !shown(&s) {
+ return;
+ }
+ materials(&s, 0.2, 0.8);
+ frame(&s, 200);
+ dab(&s);
+ let colour = painted(&s);
+ assert!(colour > 20, "{colour}");
+ // each the brush's amount where the stroke covers, nothing elsewhere
+ s.set_model_channel(1);
+ assert_eq!(covered_grey(&s), Some(51));
+ assert!(channel_texels(&s).iter().filter(|t| t.1 > 0).count().abs_diff(colour) * 10 < colour);
+ s.set_model_channel(2);
+ assert_eq!(covered_grey(&s), Some(204));
+ // a stroke on a channel paints that channel alone
+ s.stroke_begin(PointerKind::Stylus, vec![sample(60.0, 60.0, 0.0)]);
+ s.stroke_end(vec![sample(140.0, 60.0, 50.0)]);
+ frame(&s, 200);
+ let metal = painted(&s);
+ assert!(metal > colour);
+ s.set_model_channel(1);
+ assert!(painted(&s).abs_diff(colour) * 10 < colour);
+ s.set_model_channel(0);
+ assert_eq!(painted(&s), colour);
+ }
+
+ #[test]
+ fn the_brushs_material_source_shapes_what_it_lays_down() {
+ let s = CanvasSession::open_model(CUBE.into(), "cube.obj".into(), Vec::new(), "Cube".into(), 64).unwrap();
+ if !shown(&s) {
+ return;
+ }
+ let size = crate::studio::source_size() as usize;
+ s.studio_begin();
+ // the metalness through a mid grey, the roughness through black
+ assert!(s.set_studio_source(crate::studio::SourceKind::Metallic, vec![128; size * size]));
+ assert!(s.set_studio_source(crate::studio::SourceKind::Roughness, vec![0; size * size]));
+ s.set_studio_setting("metallic_amount".into(), 1.0);
+ s.set_studio_setting("roughness_amount".into(), 1.0);
+ s.studio_end(true);
+ frame(&s, 200);
+ dab(&s);
+ s.set_model_channel(2);
+ let grey = covered_grey(&s).unwrap();
+ assert!(grey.abs_diff(128) <= 2, "{grey}");
+ s.set_model_channel(1);
+ assert_eq!(covered_grey(&s), Some(0));
+ }
+
+ #[test]
+ fn undo_and_redo_take_a_strokes_channels_together() {
+ let s = CanvasSession::open_model(CUBE.into(), "cube.obj".into(), Vec::new(), "Cube".into(), 64).unwrap();
+ if !shown(&s) {
+ return;
+ }
+ materials(&s, 0.4, 0.6);
+ frame(&s, 200);
+ dab(&s);
+ let colour = painted(&s);
+ let all = |s: &CanvasSession| {
+ let counts: Vec<usize> = (0..3)
+ .map(|c| {
+ s.set_model_channel(c);
+ painted(s)
+ })
+ .collect();
+ s.set_model_channel(0);
+ counts
+ };
+ let painted_all = all(&s);
+ assert!(painted_all.iter().all(|&n| n > 20), "{painted_all:?}");
+ s.undo();
+ assert_eq!(all(&s), [0, 0, 0]);
+ s.redo();
+ assert_eq!(all(&s), painted_all);
+ // undone from the roughness, the colour and metalness go too
+ s.set_model_channel(1);
+ s.undo();
+ assert_eq!(painted(&s), 0);
+ assert_eq!(all(&s), [0, 0, 0]);
+ // an eraser on the colour takes all three away
+ s.redo();
+ assert_eq!(all(&s), painted_all);
+ let cover = |s: &CanvasSession| {
+ let sums: Vec<u32> = (0..3)
+ .map(|c| {
+ s.set_model_channel(c);
+ channel_texels(s).iter().map(|t| u32::from(t.1)).sum()
+ })
+ .collect();
+ s.set_model_channel(0);
+ sums
+ };
+ let before = cover(&s);
+ s.stroke_begin(PointerKind::StylusEraser, vec![sample(80.0, 100.0, 0.0)]);
+ s.stroke_end(vec![sample(120.0, 100.0, 50.0)]);
+ frame(&s, 200);
+ let after = cover(&s);
+ assert!(after.iter().zip(&before).all(|(a, b)| a * 10 < b * 9), "{after:?} {before:?}");
+ assert_eq!(colour, painted_all[0]);
+ }
+
#[test]
fn an_obj_takes_its_material_file_and_images_from_the_files_picked_with_it() {
let obj = format!("mtllib Cube Parts.mtl\nusemtl body\n{CUBE}");
diff --git a/apps/procraft-android/src/studio.rs b/apps/procraft-android/src/studio.rs
index f907436..f8e1df4 100644
--- a/apps/procraft-android/src/studio.rs
+++ b/apps/procraft-android/src/studio.rs
@@ -529,8 +529,7 @@ pub(crate) struct SpherePad {
}
/// What tells a material source's image from another's: its own image's, else the shipped one's.
-#[cfg_attr(not(target_os = "android"), allow(dead_code))]
-fn source_key(m: &MaterialSource) -> Option<u64> {
+pub(crate) fn source_key(m: &MaterialSource) -> Option<u64> {
m.source.as_ref().map(|s| s.id).or_else(|| m.image.map(|i| (1 << 63) | u64::from(i)))
}
diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs
index 5d226f8..06b1ed7 100644
--- a/crates/gpu/src/lib.rs
+++ b/crates/gpu/src/lib.rs
@@ -17,7 +17,7 @@ pub use canvas::{
SmallScene, Stripes, StrokeStyle, StrokeTool, SurroundGrid, Target, Thumbnail, read_texture,
};
pub use lighting::Lighting;
-pub use scene::{Light, MOST_LIGHTS, MeshView, Orbit, SurfaceMap};
+pub use scene::{Light, MOST_LIGHTS, MaterialLook, MeshView, Orbit, SurfaceMap, material_source};
pub use sphere::{SphereLook, SpherePreview, preview_environment};
pub use wgpu;
diff --git a/crates/gpu/src/scene.rs b/crates/gpu/src/scene.rs
index 175fce8..450dc20 100644
--- a/crates/gpu/src/scene.rs
+++ b/crates/gpu/src/scene.rs
@@ -19,6 +19,45 @@ const DISTANCE: wgpu::TextureFormat = wgpu::TextureFormat::R32Float;
const ISLANDS: wgpu::TextureFormat = wgpu::TextureFormat::R8Unorm;
/// How far past its islands a projected stroke is grown (texels; as the shader's `DILATE`).
const DILATE: i32 = 2;
+/// A brush's tiling is in canvas pixels; on a model each pixel counts as this many framing units
+/// (the model's largest side counted as 4).
+const TILE_UNITS_PER_PIXEL: f32 = 0.00225;
+
+/// What a stroke on a model's colour lays down in its roughness or metalness: the brush's
+/// `amount` (0..1), through its `source` if it has one (grey, white taking the amount, made with
+/// [`material_source`]), one tile of it every `tile` canvas pixels, seen from the model's three
+/// axes.
+pub struct MaterialLook<'a> {
+ pub amount: f32,
+ pub source: Option<&'a wgpu::TextureView>,
+ pub tile: f32,
+}
+
+/// A brush's material source (`side` square grey pixels) as [`MaterialLook`] takes it; none if
+/// the pixels aren't that size.
+pub fn material_source(gpu: &Gpu, side: u32, pixels: &[u8]) -> Option<wgpu::TextureView> {
+ (side > 0 && pixels.len() == (side as usize) * (side as usize)).then(|| grey_view(gpu, side, pixels))
+}
+
+/// `side` square grey `pixels` as a texture.
+fn grey_view(gpu: &Gpu, side: u32, pixels: &[u8]) -> wgpu::TextureView {
+ let texture = gpu.device.create_texture_with_data(
+ &gpu.queue,
+ &wgpu::TextureDescriptor {
+ label: Some("material source"),
+ size: wgpu::Extent3d { width: side, height: side, depth_or_array_layers: 1 },
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format: wgpu::TextureFormat::R8Unorm,
+ usage: wgpu::TextureUsages::TEXTURE_BINDING,
+ view_formats: &[],
+ },
+ wgpu::util::TextureDataOrder::LayerMajor,
+ pixels,
+ );
+ texture.create_view(&wgpu::TextureViewDescriptor::default())
+}
/// A model is framed as if its largest side were this long, seen from this far off, with this
/// field of view (radians) across the screen's shorter side.
@@ -98,6 +137,15 @@ struct ProjectUniform {
eye: [f32; 4],
}
+#[repr(C)]
+#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
+struct MaterialUniform {
+ /// The model's middle, and framing units per model unit.
+ frame: [f32; 4],
+ /// The amount, a source tile's side (framing units), 1 with a source.
+ look: [f32; 4],
+}
+
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct PartUniform {
@@ -442,6 +490,14 @@ struct Projector {
dilate_pipeline: wgpu::RenderPipeline,
layout: wgpu::BindGroupLayout,
uniform: wgpu::Buffer,
+ /// What carries a stroke's coverage into a roughness or metalness: its pipeline, the layout
+ /// of its look, a uniform for each of the two (so both can be drawn in one submission), the
+ /// sources' sampler (tiling) and white for no source.
+ material_pipeline: wgpu::RenderPipeline,
+ material_layout: wgpu::BindGroupLayout,
+ material_uniforms: [wgpu::Buffer; 2],
+ source_sampler: wgpu::Sampler,
+ white: wgpu::TextureView,
screen: Option<(wgpu::Texture, wgpu::TextureView, wgpu::TextureView)>,
/// The stroke as projected, before it is grown past the islands (the texture's size).
scratch: Option<(wgpu::Texture, wgpu::TextureView)>,
@@ -1589,6 +1645,103 @@ impl MeshView {
grown.intersect(IRect::from_size(self.texture_size, self.texture_size))
}
+ /// Carries the coverage of a stroke projected onto part `part` (`coverage`: as
+ /// [`MeshView::project_stroke`] left it in the colour's target) into `target` (the same
+ /// part's roughness or metalness stroke, the texture's size square, the stroke format) as
+ /// `look` says: grey, the amount through its source where the stroke covers. `slot` (0 or 1)
+ /// keeps the roughness's look apart from the metalness's when both are drawn in one
+ /// submission. Does nothing before the part has taken a stroke.
+ pub fn project_material(
+ &mut self,
+ gpu: &Gpu,
+ encoder: &mut wgpu::CommandEncoder,
+ part: usize,
+ slot: usize,
+ coverage: &wgpu::TextureView,
+ look: &MaterialLook<'_>,
+ target: &wgpu::TextureView,
+ ) {
+ let shown = self.shown_ranges(part);
+ let frame = [self.middle[0], self.middle[1], self.middle[2], 1.0 / self.unit.max(1e-6)];
+ let Some(projector) = self.projector.as_ref() else { return };
+ let (Some((_, scratch)), Some(Some((_, islands))), Some(uniform)) =
+ (&projector.scratch, projector.islands.get(part), projector.material_uniforms.get(slot))
+ else {
+ return;
+ };
+ let has_source = if look.source.is_some() { 1.0 } else { 0.0 };
+ let tile = (look.tile * TILE_UNITS_PER_PIXEL).max(1e-4);
+ gpu.queue.write_buffer(
+ uniform,
+ 0,
+ bytemuck::bytes_of(&MaterialUniform { frame, look: [look.amount.clamp(0.0, 1.0), tile, has_source, 0.0] }),
+ );
+ let look_bind = gpu.device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("material look"),
+ layout: &projector.material_layout,
+ entries: &[
+ wgpu::BindGroupEntry { binding: 0, resource: uniform.as_entire_binding() },
+ wgpu::BindGroupEntry {
+ binding: 1,
+ resource: wgpu::BindingResource::TextureView(look.source.unwrap_or(&projector.white)),
+ },
+ wgpu::BindGroupEntry {
+ binding: 2,
+ resource: wgpu::BindingResource::Sampler(&projector.source_sampler),
+ },
+ ],
+ });
+ // the part in its texture's space, each texel the amount where the colour has coverage
+ {
+ let bind = projector.bind(gpu, coverage, None);
+ let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
+ label: Some("material stroke"),
+ color_attachments: &[Some(wgpu::RenderPassColorAttachment {
+ view: scratch,
+ depth_slice: None,
+ resolve_target: None,
+ ops: wgpu::Operations {
+ load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
+ store: wgpu::StoreOp::Store,
+ },
+ })],
+ depth_stencil_attachment: None,
+ timestamp_writes: None,
+ occlusion_query_set: None,
+ multiview_mask: None,
+ });
+ pass.set_pipeline(&projector.material_pipeline);
+ pass.set_bind_group(0, &bind, &[]);
+ pass.set_bind_group(1, &look_bind, &[]);
+ pass.set_vertex_buffer(0, self.vertices.slice(..));
+ pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
+ for range in shown {
+ pass.draw_indexed(range, 0, 0..1);
+ }
+ }
+ // grown past the islands' edges into the target, as the colour was
+ let bind = projector.bind(gpu, scratch, Some(islands));
+ let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
+ label: Some("dilate material"),
+ color_attachments: &[Some(wgpu::RenderPassColorAttachment {
+ view: target,
+ depth_slice: None,
+ resolve_target: None,
+ ops: wgpu::Operations {
+ load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
+ store: wgpu::StoreOp::Store,
+ },
+ })],
+ depth_stencil_attachment: None,
+ timestamp_writes: None,
+ occlusion_query_set: None,
+ multiview_mask: None,
+ });
+ pass.set_pipeline(&projector.dilate_pipeline);
+ pass.set_bind_group(0, &bind, &[]);
+ pass.draw(0..3, 0..1);
+ }
+
/// The texels of part `part` whose triangles land on the screen inside `bounds`.
fn reach(&self, part: usize, view_proj: [[f32; 4]; 4], bounds: IRect, width: u32, height: u32) -> IRect {
let Some(p) = self.parts.get(part) else { return IRect::EMPTY };
@@ -1834,6 +1987,89 @@ impl Projector {
multiview_mask: None,
cache: None,
});
+ // a roughness or metalness from the colour's coverage: the look in a group of its own
+ let material_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("material look"),
+ entries: &[
+ wgpu::BindGroupLayoutEntry {
+ binding: 0,
+ visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
+ ty: wgpu::BindingType::Buffer {
+ ty: wgpu::BufferBindingType::Uniform,
+ has_dynamic_offset: false,
+ min_binding_size: None,
+ },
+ count: None,
+ },
+ wgpu::BindGroupLayoutEntry {
+ binding: 1,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Texture {
+ sample_type: wgpu::TextureSampleType::Float { filterable: true },
+ view_dimension: wgpu::TextureViewDimension::D2,
+ multisampled: false,
+ },
+ count: None,
+ },
+ wgpu::BindGroupLayoutEntry {
+ binding: 2,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
+ count: None,
+ },
+ ],
+ });
+ let material_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+ label: Some("material"),
+ bind_group_layouts: &[Some(&layout), Some(&material_layout)],
+ immediate_size: 0,
+ });
+ let material_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
+ label: Some("material stroke"),
+ layout: Some(&material_pipeline_layout),
+ vertex: wgpu::VertexState {
+ module: &module,
+ entry_point: Some("vs_material"),
+ compilation_options: Default::default(),
+ buffers: &[Some(wgpu::VertexBufferLayout {
+ array_stride: std::mem::size_of::<VertexGpu>() as u64,
+ step_mode: wgpu::VertexStepMode::Vertex,
+ attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3, 2 => Float32x2],
+ })],
+ },
+ primitive: wgpu::PrimitiveState::default(),
+ depth_stencil: None,
+ multisample: wgpu::MultisampleState::default(),
+ fragment: Some(wgpu::FragmentState {
+ module: &module,
+ entry_point: Some("fs_material"),
+ compilation_options: Default::default(),
+ targets: &[Some(wgpu::ColorTargetState {
+ format: STROKE,
+ blend: Some(MAX),
+ write_mask: wgpu::ColorWrites::ALL,
+ })],
+ }),
+ multiview_mask: None,
+ cache: None,
+ });
+ let material_uniform = || {
+ device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("material look"),
+ size: std::mem::size_of::<MaterialUniform>() as u64,
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ })
+ };
+ let source_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
+ label: Some("material source"),
+ address_mode_u: wgpu::AddressMode::Repeat,
+ address_mode_v: wgpu::AddressMode::Repeat,
+ mag_filter: wgpu::FilterMode::Linear,
+ min_filter: wgpu::FilterMode::Linear,
+ ..Default::default()
+ });
+ let white = grey_view(gpu, 1, &[255]);
Projector {
distance_pipeline: pipeline("screen distance", "vs_distance", "fs_distance", DISTANCE, true, None),
project_pipeline: pipeline("project stroke", "vs_project", "fs_project", STROKE, false, Some(MAX)),
@@ -1841,6 +2077,11 @@ impl Projector {
dilate_pipeline,
layout,
uniform,
+ material_pipeline,
+ material_layout,
+ material_uniforms: [material_uniform(), material_uniform()],
+ source_sampler,
+ white,
screen: None,
scratch: None,
islands: Vec::new(),
diff --git a/crates/gpu/src/shaders/project.wgsl b/crates/gpu/src/shaders/project.wgsl
index 118ec45..b796af9 100644
--- a/crates/gpu/src/shaders/project.wgsl
+++ b/crates/gpu/src/shaders/project.wgsl
@@ -129,3 +129,65 @@ fn fs_dilate(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> {
}
return best;
}
+
+// ---- a stroke's roughness or metalness: the colour's coverage (binding 1, in the texture's
+// space) carrying the brush's amount, through its source seen from the model's three axes -----
+
+struct Material {
+ // the model's middle, and how many framing units one of its own units is
+ frame: vec4<f32>,
+ // the amount, one source tile's side (framing units), and 1 when there is a source
+ look: vec4<f32>,
+}
+
+@group(1) @binding(0) var<uniform> material: Material;
+@group(1) @binding(1) var source: texture_2d<f32>;
+@group(1) @binding(2) var source_sampler: sampler;
+
+struct MaterialOut {
+ @builtin(position) clip: vec4<f32>,
+ @location(0) framed: vec3<f32>,
+ @location(1) normal: vec3<f32>,
+}
+
+@vertex
+fn vs_material(v: In) -> MaterialOut {
+ var o: MaterialOut;
+ o.clip = vec4<f32>(v.uv.x * 2.0 - 1.0, v.uv.y * 2.0 - 1.0, 0.0, 1.0);
+ o.framed = (v.position - material.frame.xyz) * material.frame.w;
+ o.normal = v.normal;
+ return o;
+}
+
+// how sharply the three views part where the surface turns from one axis to another
+const AXIS_SHARPNESS: f32 = 10.0;
+
+// the source where a surface at `p` facing `n` is, read along the axis it faces most: each
+// axis's reading weighted by how squarely the surface faces it, the weights' squares summing
+// to one; each read so it isn't mirrored from either side
+fn from_three_axes(p: vec3<f32>, n: vec3<f32>, tile: f32) -> f32 {
+ let along = pow(abs(n), vec3<f32>(AXIS_SHARPNESS));
+ let weights = saturate(along * inverseSqrt(max(dot(along, along), 1e-12)));
+ let q = p / tile;
+ let across_x = vec2<f32>(select(q.z, -q.z, n.x >= 0.0), q.y);
+ let across_y = vec2<f32>(q.x, select(q.z, -q.z, n.y < 0.0));
+ let across_z = vec2<f32>(select(-q.x, q.x, n.z > 0.0), q.y);
+ let seen = vec3<f32>(
+ textureSampleLevel(source, source_sampler, across_x, 0.0).r,
+ textureSampleLevel(source, source_sampler, across_y, 0.0).r,
+ textureSampleLevel(source, source_sampler, across_z, 0.0).r,
+ );
+ return dot(seen, weights);
+}
+
+@fragment
+fn fs_material(i: MaterialOut) -> @location(0) vec4<f32> {
+ let cover = textureLoad(stroke, vec2<i32>(i.clip.xy), 0).a;
+ var amount = material.look.x;
+ if (material.look.z > 0.5) {
+ let n = normalize(i.normal);
+ amount = amount * from_three_axes(i.framed, n, max(material.look.y, 1e-4));
+ }
+ let v = clamp(amount, 0.0, 1.0) * cover;
+ return vec4<f32>(v, v, v, cover);
+}
diff --git a/crates/gpu/tests/mesh.rs b/crates/gpu/tests/mesh.rs
index ebb84e6..01f2c43 100644
--- a/crates/gpu/tests/mesh.rs
+++ b/crates/gpu/tests/mesh.rs
@@ -223,6 +223,84 @@ fn a_projected_stroke_grows_a_little_past_its_island() {
assert_eq!(gpu.errors(), 0);
}
+#[test]
+fn a_strokes_roughness_or_metalness_takes_its_coverage_and_the_brushs_amount() {
+ use procraft_engine::document::Document;
+ use procraft_engine::geom::{IRect, Vec2};
+ use procraft_engine::stroke::Stamp;
+ use procraft_gpu::{Canvas, MaterialLook, StrokeStyle, StrokeTool, material_source};
+ let Some(gpu) = gpu() else { return };
+ assert!(material_source(&gpu, 2, &[0; 3]).is_none());
+ let island = "v -1 -1 0
+v 1 -1 0
+v 1 1 0
+v -1 1 0
+vt 0.25 0.25
+vt 0.75 0.25
+vt 0.75 0.75
+vt 0.25 0.75
+f 1/1 2/2 3/3 4/4
+";
+ let mesh = read_obj(island, None).unwrap();
+ let mut view = MeshView::new(&gpu, &mesh, 16, wgpu::TextureFormat::Rgba8Unorm);
+ view.orbit = Orbit { target: [0.0, 0.0, 0.0], rotation: Orbit::rotation_of(0.0, 0.0), distance: 6.0, fov: 0.9 };
+ let mut screen_doc = Document::new(SIDE, SIDE);
+ let mut screen = Canvas::new(&gpu, &screen_doc).unwrap();
+ let style = StrokeStyle::new(StrokeTool::Paint, [1.0, 0.0, 0.0], 1.0);
+ screen.begin_stroke(screen_doc.current(), style);
+ let middle = SIDE as f32 / 2.0;
+ screen.add_stamps(&[Stamp::round(Vec2::new(middle, middle), 30.0, 1.0)]);
+ screen.render(&gpu, &mut screen_doc, None);
+ let mut doc = Document::new(16, 16);
+ doc.background_visible = false;
+ let mut colour = Canvas::new(&gpu, &doc).unwrap();
+ colour.begin_drawn_stroke(doc.current(), style);
+ // a material carried at half, and one through a black source (none of the amount)
+ let black = material_source(&gpu, 2, &[0; 4]).unwrap();
+ let looks = [
+ MaterialLook { amount: 0.5, source: None, tile: 100.0 },
+ MaterialLook { amount: 1.0, source: Some(&black), tile: 100.0 },
+ ];
+ let mut materials: Vec<(Document, Canvas)> = looks
+ .iter()
+ .map(|_| {
+ let mut d = Document::new(16, 16);
+ d.background_visible = false;
+ let mut c = Canvas::new(&gpu, &d).unwrap();
+ c.begin_drawn_stroke(d.current(), StrokeStyle::new(StrokeTool::Paint, [1.0; 3], 1.0));
+ (d, c)
+ })
+ .collect();
+ let mut encoder = gpu.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("project") });
+ let bounds = IRect::from_size(SIDE, SIDE);
+ let reach =
+ view.project_stroke(&gpu, &mut encoder, 0, screen.stroke_view(), bounds, SIDE, SIDE, colour.stroke_target());
+ for (slot, (look, (_, c))) in looks.iter().zip(&materials).enumerate() {
+ view.project_material(&gpu, &mut encoder, 0, slot, colour.stroke_view(), look, c.stroke_target());
+ }
+ gpu.queue.submit([encoder.finish()]);
+ let read = |c: &mut Canvas, d: &mut Document| {
+ c.stroke_drawn(reach);
+ c.end_stroke();
+ c.render(&gpu, d, None);
+ c.read_layer(&gpu, d.current()).unwrap()
+ };
+ let colour_px = read(&mut colour, &mut doc);
+ let px: Vec<Vec<u8>> = materials.iter_mut().map(|(d, c)| read(c, d)).collect();
+ // the same coverage as the colour, grown past the island as it is; the grey its amount
+ for x in 0..16 {
+ let i = ((8 * 16 + x) * 4) as usize;
+ assert_eq!(px[0][i + 3], colour_px[i + 3], "texel {x}");
+ assert_eq!(px[1][i + 3], colour_px[i + 3], "texel {x}");
+ if colour_px[i + 3] == 255 {
+ assert!(px[0][i].abs_diff(128) <= 1, "texel {x}: {}", px[0][i]);
+ assert_eq!(px[1][i], 0, "texel {x}");
+ }
+ }
+ assert_eq!(colour_px[(8 * 16 + 8) * 4 + 3], 255);
+ assert_eq!(gpu.errors(), 0);
+}
+
#[test]
fn a_far_surface_shows_its_texture_filtered() {
let Some(gpu) = gpu() else { return };