ProCraft for Android: An Android native painting app written in Rust
Rust
feat(gpu): read a stroke's grain along a model's three axes
Joshac270cbe71b3
diff --git a/crates/gpu/src/canvas.rs b/crates/gpu/src/canvas.rsindex 4d3c224..d35ca35 100644--- a/crates/gpu/src/canvas.rs+++ b/crates/gpu/src/canvas.rs@@ -1162,6 +1162,9 @@ pub struct Canvas {shapes: wgpu::Texture,grains: wgpu::Texture,brush_images: bool,+ /// The open stroke's grain fixed to the canvas, from a picture its size in place of a brush+ /// image (see [`Canvas::set_stroke_grain`]); kept until the stroke has ended.+ stroke_grain: Option<wgpu::TextureView>,shape_sources: SourceSlots,grain_sources: SourceSlots,/// Full coverage, stood in for the selection when there is none.@@ -1489,6 +1492,7 @@ impl Canvas {shapes,grains,brush_images: images,+ stroke_grain: None,shape_sources: SourceSlots::after(shape_images.len()),grain_sources: SourceSlots::after(grain_images.len()),};@@ -1544,6 +1548,30 @@ impl Canvas {self.bind_brush_images(&gpu.device);}+ /// The open stroke's grain fixed to the canvas is read from `grain` from now on, in place of a+ /// brush image: a picture the canvas's size (square), one grey per pixel, viewed as an array+ /// of one, such as 3D painting reads along a model's three axes. How the grain shows (its+ /// depth, brightness, contrast and blend mode) stays the stroke's. Kept until the stroke has+ /// ended; nothing without an open stroke whose grain is fixed to the canvas.+ pub fn set_stroke_grain(&mut self, device: &wgpu::Device, grain: &wgpu::TextureView) {+ let square = self.width == self.height;+ let Some(active) = self.active.as_mut().filter(|a| square && a.style.canvas_grain().is_some()) else {+ return;+ };+ // one tile the canvas, read texel for texel+ active.style.grain = Some(0);+ active.style.grain_scale = self.width as f32;+ active.style.grain_rotation = 0.0;+ active.style.grain_sharp = false;+ self.stroke_grain = Some(grain.clone());+ self.bind_brush_images(device);+ }++ /// The grains strokes read: the open stroke's own picture, else the brush images.+ fn grain_view(&self) -> &wgpu::TextureView {+ self.stroke_grain.as_ref().unwrap_or(&self.grains_view)+ }+/// Whether it holds its shape and grain images (else stand-ins until something wants them).pub fn has_brush_images(&self) -> bool {self.brush_images@@ -1564,7 +1592,7 @@ impl Canvas {device,&self.pipelines.quad_layout,[&self.stroke_view; 5],- &self.grains_view,+ self.grain_view(),&self.grain_sampler,);}@@ -1572,7 +1600,7 @@ impl Canvas {/// Whether the queued work or the stroke open reads the shape or grain images: stamps, the/// predicted tail, a smudge, or a grain fixed to the canvas.fn wants_brush_images(&self) -> bool {- let grained = |s: &StrokeStyle| s.canvas_grain().is_some();+ let grained = |s: &StrokeStyle| s.canvas_grain().is_some() && self.stroke_grain.is_none();!self.prediction.is_empty()|| self.smudge.is_some()|| self.active.is_some_and(|a| grained(&a.style))@@ -3159,6 +3187,10 @@ impl Canvas {if history_changed {self.collect_layers(doc);}+ // a stroke's own grain goes with it once it has ended+ if self.active.is_none() && self.stroke_grain.take().is_some() {+ self.bind_brush_images(device);+ }if !smudge_uniforms.data.is_empty() {gpu.queue.write_buffer(&self.smudge_params, 0, &smudge_uniforms.data);@@ -5352,7 +5384,7 @@ impl Canvas {mask: &wgpu::TextureView,) -> wgpu::BindGroup {let [a, b, c, d] = views;- quad_bind(device, &self.pipelines.quad_layout, [a, b, c, d, mask], &self.grains_view, &self.grain_sampler)+ quad_bind(device, &self.pipelines.quad_layout, [a, b, c, d, mask], self.grain_view(), &self.grain_sampler)}/// Layer textures held (the document's and those the history still needs).diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rsindex 6986944..df21898 100644--- a/crates/gpu/src/lib.rs+++ b/crates/gpu/src/lib.rs@@ -18,7 +18,7 @@ pub use canvas::{textures_bytes,};pub use lighting::Lighting;-pub use scene::{Light, MOST_LIGHTS, MaterialLook, MeshView, Orbit, SurfaceMap, material_source};+pub use scene::{GrainLook, 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.rsindex 450dc20..2808aa7 100644--- a/crates/gpu/src/scene.rs+++ b/crates/gpu/src/scene.rs@@ -17,6 +17,8 @@ const STROKE: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float;const DISTANCE: wgpu::TextureFormat = wgpu::TextureFormat::R32Float;/// Where a part's triangles lie in its texture.const ISLANDS: wgpu::TextureFormat = wgpu::TextureFormat::R8Unorm;+/// A stroke's grain in a part's texture's space: one grey per texel.+const GRAIN: 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@@ -26,11 +28,21 @@ 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.+/// axes: fixed to the model, or (`follows_camera`) to the camera, as a stroke's grain is.pub struct MaterialLook<'a> {pub amount: f32,pub source: Option<&'a wgpu::TextureView>,pub tile: f32,+ pub follows_camera: bool,+}++/// A stroke's grain on a model: its image (grey, made with [`material_source`]), one tile of it+/// every `tile` canvas pixels, read along the model's three axes, fixed to the model or (as+/// brushes have it unless set otherwise) to the camera.+pub struct GrainLook<'a> {+ pub source: &'a wgpu::TextureView,+ pub tile: f32,+ pub follows_camera: bool,}/// A brush's material source (`side` square grey pixels) as [`MaterialLook`] takes it; none if@@ -140,9 +152,10 @@ struct ProjectUniform {#[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.+ /// Grain space from the model's (columns): framing units about its middle, or about the+ /// camera.+ space: [[f32; 4]; 4],+ /// The amount, a source tile's side (grain space units), 1 with a source.look: [f32; 4],}@@ -498,6 +511,13 @@ struct Projector {material_uniforms: [wgpu::Buffer; 2],source_sampler: wgpu::Sampler,white: wgpu::TextureView,+ /// What reads a stroke's grain along the model's axes into the texture's space: its pipeline,+ /// the dilation into the grain's own format, its uniform, and the picture it makes (the+ /// texture's size; viewed whole and as an array of one, as a canvas reads grains).+ grain_pipeline: wgpu::RenderPipeline,+ grain_dilate_pipeline: wgpu::RenderPipeline,+ grain_uniform: wgpu::Buffer,+ grain: Option<(wgpu::TextureView, 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)>,@@ -1456,8 +1476,9 @@ impl MeshView {target: &wgpu::TextureView,) -> IRect {let (width, height) = (width.max(1), height.max(1));- let Some(p) = self.parts.get(part) else { return IRect::EMPTY };- let (first, count) = (p.first, p.count);+ if self.parts.get(part).is_none() {+ return IRect::EMPTY;+ }let view_proj = self.orbit.view_proj(width as f32 / height as f32);// what's shown of each part, worked out before the projector is borrowedlet shown_by_part: Vec<Vec<std::ops::Range<u32>>> =@@ -1488,57 +1509,10 @@ impl MeshView {let depth_view = depth.create_view(&wgpu::TextureViewDescriptor::default());projector.screen = Some((distance, distance_view, depth_view));}- let side = self.texture_size;- let make_square = |label, format| {- let texture = gpu.device.create_texture(&wgpu::TextureDescriptor {- label: Some(label),- size: wgpu::Extent3d { width: side, height: side, depth_or_array_layers: 1 },- mip_level_count: 1,- sample_count: 1,- dimension: wgpu::TextureDimension::D2,- format,- usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,- view_formats: &[],- });- let view = texture.create_view(&wgpu::TextureViewDescriptor::default());- (texture, view)- };- if projector.scratch.is_none() {- projector.scratch = Some(make_square("projected stroke", STROKE));- }- if projector.islands.len() < self.parts.len() {- projector.islands.resize_with(self.parts.len(), || None);- }- // the part's islands, drawn the first time it is painted- if projector.islands.get(part).is_some_and(Option::is_none) {- let (texture, view) = make_square("islands", ISLANDS);- {- let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {- label: Some("islands"),- color_attachments: &[Some(wgpu::RenderPassColorAttachment {- view: &view,- 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.islands_pipeline);- pass.set_bind_group(0, &projector.bind(gpu, stroke, None), &[]);- pass.set_vertex_buffer(0, self.vertices.slice(..));- pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);- pass.draw_indexed(first..first + count, 0, 0..1);- }- if let Some(slot) = projector.islands.get_mut(part) {- *slot = Some((texture, view));- }+ if self.prepare_part(gpu, encoder, part).is_none() {+ return IRect::EMPTY;}+ let Some(projector) = self.projector.as_ref() else { return IRect::EMPTY };let (Some((_, distance_view, depth_view)), Some((_, scratch)), Some(Some((_, islands)))) =(&projector.screen, &projector.scratch, projector.islands.get(part))else {@@ -1662,7 +1636,7 @@ impl MeshView {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 space = self.grain_space(look.follows_camera);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))@@ -1674,7 +1648,7 @@ impl MeshView {gpu.queue.write_buffer(uniform,0,- bytemuck::bytes_of(&MaterialUniform { frame, look: [look.amount.clamp(0.0, 1.0), tile, has_source, 0.0] }),+ bytemuck::bytes_of(&MaterialUniform { space, 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"),@@ -1742,6 +1716,193 @@ impl MeshView {pass.draw(0..3, 0..1);}+ /// Grain space from the model's (columns): framing units about the model's middle, or (when+ /// the grain follows the camera) about the camera, turned with it.+ fn grain_space(&self, follows_camera: bool) -> [[f32; 4]; 4] {+ let s = 1.0 / self.unit.max(1e-6);+ let scale = [[s, 0.0, 0.0, 0.0], [0.0, s, 0.0, 0.0], [0.0, 0.0, s, 0.0], [0.0, 0.0, 0.0, 1.0]];+ let [mx, my, mz] = self.middle;+ let from = if follows_camera {+ look_at(self.orbit.eye(), self.orbit.target, self.orbit.up())+ } else {+ [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [-mx, -my, -mz, 1.0]]+ };+ mul(scale, from)+ }++ /// Reads `look` along part `part`'s three axes into its texture's space: each texel the grain+ /// where its surface is, grown past the islands as strokes are. Returns the picture as an+ /// array of one (as a canvas reads grains; see [`crate::Canvas::set_stroke_grain`]); kept+ /// until the next call.+ pub fn project_grain(+ &mut self,+ gpu: &Gpu,+ encoder: &mut wgpu::CommandEncoder,+ part: usize,+ look: &GrainLook<'_>,+ ) -> Option<wgpu::TextureView> {+ let shown = self.shown_ranges(part);+ let space = self.grain_space(look.follows_camera);+ self.prepare_part(gpu, encoder, part)?;+ let side = self.texture_size;+ let projector = self.projector.as_mut()?;+ if projector.grain.is_none() {+ let texture = gpu.device.create_texture(&wgpu::TextureDescriptor {+ label: Some("stroke grain"),+ size: wgpu::Extent3d { width: side, height: side, depth_or_array_layers: 1 },+ mip_level_count: 1,+ sample_count: 1,+ dimension: wgpu::TextureDimension::D2,+ format: GRAIN,+ usage: wgpu::TextureUsages::RENDER_ATTACHMENT+ | wgpu::TextureUsages::TEXTURE_BINDING+ | wgpu::TextureUsages::COPY_SRC,+ view_formats: &[],+ });+ let whole = texture.create_view(&wgpu::TextureViewDescriptor::default());+ let array = texture.create_view(&wgpu::TextureViewDescriptor {+ dimension: Some(wgpu::TextureViewDimension::D2Array),+ ..Default::default()+ });+ projector.grain = Some((whole, array));+ }+ let projector = self.projector.as_ref()?;+ let (Some((_, scratch)), Some(Some((_, islands))), Some((grain, array))) =+ (&projector.scratch, projector.islands.get(part), &projector.grain)+ else {+ return None;+ };+ let tile = (look.tile * TILE_UNITS_PER_PIXEL).max(1e-4);+ gpu.queue.write_buffer(+ &projector.grain_uniform,+ 0,+ bytemuck::bytes_of(&MaterialUniform { space, look: [1.0, tile, 1.0, 0.0] }),+ );+ let look_bind = gpu.device.create_bind_group(&wgpu::BindGroupDescriptor {+ label: Some("stroke grain"),+ layout: &projector.material_layout,+ entries: &[+ wgpu::BindGroupEntry { binding: 0, resource: projector.grain_uniform.as_entire_binding() },+ wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::TextureView(look.source) },+ wgpu::BindGroupEntry {+ binding: 2,+ resource: wgpu::BindingResource::Sampler(&projector.source_sampler),+ },+ ],+ });+ // the part in its texture's space, each texel its surface's grain+ {+ let bind = projector.bind(gpu, islands, None);+ let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {+ label: Some("stroke grain"),+ 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.grain_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, as strokes are+ let bind = projector.bind(gpu, scratch, Some(islands));+ let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {+ label: Some("dilate grain"),+ color_attachments: &[Some(wgpu::RenderPassColorAttachment {+ view: grain,+ 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.grain_dilate_pipeline);+ pass.set_bind_group(0, &bind, &[]);+ pass.draw(0..3, 0..1);+ drop(pass);+ Some(array.clone())+ }++ /// Makes what carrying strokes onto part `part` takes and doesn't change: the projector, the+ /// texture-sized scratch and the part's islands (drawn the first time).+ fn prepare_part(&mut self, gpu: &Gpu, encoder: &mut wgpu::CommandEncoder, part: usize) -> Option<()> {+ let (first, count) = self.parts.get(part).map(|p| (p.first, p.count))?;+ let side = self.texture_size;+ let projector = self.projector.get_or_insert_with(|| Projector::new(gpu));+ let make_square = |label, format| {+ let texture = gpu.device.create_texture(&wgpu::TextureDescriptor {+ label: Some(label),+ size: wgpu::Extent3d { width: side, height: side, depth_or_array_layers: 1 },+ mip_level_count: 1,+ sample_count: 1,+ dimension: wgpu::TextureDimension::D2,+ format,+ usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,+ view_formats: &[],+ });+ let view = texture.create_view(&wgpu::TextureViewDescriptor::default());+ (texture, view)+ };+ if projector.scratch.is_none() {+ projector.scratch = Some(make_square("projected stroke", STROKE));+ }+ if projector.islands.len() < self.parts.len() {+ projector.islands.resize_with(self.parts.len(), || None);+ }+ // the part's islands, drawn the first time it is painted+ if projector.islands.get(part).is_some_and(Option::is_none) {+ let (texture, view) = make_square("islands", ISLANDS);+ {+ let (_, scratch) = projector.scratch.as_ref()?;+ let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {+ label: Some("islands"),+ color_attachments: &[Some(wgpu::RenderPassColorAttachment {+ view: &view,+ 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.islands_pipeline);+ pass.set_bind_group(0, &projector.bind(gpu, scratch, None), &[]);+ pass.set_vertex_buffer(0, self.vertices.slice(..));+ pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);+ pass.draw_indexed(first..first + count, 0, 0..1);+ }+ if let Some(slot) = projector.islands.get_mut(part) {+ *slot = Some((texture, view));+ }+ }+ Some(())+ }+/// 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 };@@ -2070,6 +2231,61 @@ impl Projector {..Default::default()});let white = grey_view(gpu, 1, &[255]);+ // a stroke's grain: read as the materials' sources are, then grown into its own format+ let grain_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {+ label: Some("stroke grain"),+ 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_grain"),+ compilation_options: Default::default(),+ targets: &[Some(wgpu::ColorTargetState {+ format: STROKE,+ blend: None,+ write_mask: wgpu::ColorWrites::ALL,+ })],+ }),+ multiview_mask: None,+ cache: None,+ });+ let grain_dilate_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {+ label: Some("dilate grain"),+ layout: Some(&pipeline_layout),+ vertex: wgpu::VertexState {+ module: &module,+ entry_point: Some("vs_full"),+ compilation_options: Default::default(),+ buffers: &[],+ },+ primitive: wgpu::PrimitiveState::default(),+ depth_stencil: None,+ multisample: wgpu::MultisampleState::default(),+ fragment: Some(wgpu::FragmentState {+ module: &module,+ entry_point: Some("fs_dilate"),+ compilation_options: Default::default(),+ targets: &[Some(wgpu::ColorTargetState {+ format: GRAIN,+ blend: None,+ write_mask: wgpu::ColorWrites::ALL,+ })],+ }),+ multiview_mask: None,+ cache: None,+ });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)),@@ -2082,6 +2298,10 @@ impl Projector {material_uniforms: [material_uniform(), material_uniform()],source_sampler,white,+ grain_pipeline,+ grain_dilate_pipeline,+ grain_uniform: material_uniform(),+ grain: None,screen: None,scratch: None,islands: Vec::new(),diff --git a/crates/gpu/src/shaders/project.wgsl b/crates/gpu/src/shaders/project.wgslindex b796af9..ff2152f 100644--- a/crates/gpu/src/shaders/project.wgsl+++ b/crates/gpu/src/shaders/project.wgsl@@ -131,12 +131,14 @@ fn fs_dilate(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> {}// ---- 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 -----+// space) carrying the brush's amount, through its source seen from the model's three axes; and+// a stroke's grain, read the same way -----------------------------------------------------------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+ // grain space from the model's (columns): framing units about the model's middle, or about+ // the camera and turned with it when the grain follows the camera+ space: mat4x4<f32>,+ // the amount, one source tile's side (grain space units), and 1 when there is a sourcelook: vec4<f32>,}@@ -154,8 +156,8 @@ struct MaterialOut {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;+ o.framed = (material.space * vec4<f32>(v.position, 1.0)).xyz;+ o.normal = (material.space * vec4<f32>(v.normal, 0.0)).xyz;return o;}@@ -164,13 +166,15 @@ 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+// to one; seen from the x side across z and up y (z turned about for the side facing +x), from+// above across x and z (z turned about from above), from the front across x and up y (x turned+// about from behind)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_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,@@ -191,3 +195,11 @@ fn fs_material(i: MaterialOut) -> @location(0) vec4<f32> {let v = clamp(amount, 0.0, 1.0) * cover;return vec4<f32>(v, v, v, cover);}++// each texel the grain where its surface is (the source read along the model's three axes),+// what a stroke's grain fixed to the canvas becomes on a model+@fragment+fn fs_grain(i: MaterialOut) -> @location(0) vec4<f32> {+ let g = saturate(from_three_axes(i.framed, normalize(i.normal), max(material.look.y, 1e-4)));+ return vec4<f32>(g, g, g, 1.0);+}diff --git a/crates/gpu/tests/canvas.rs b/crates/gpu/tests/canvas.rsindex d2395fa..c6fcbac 100644--- a/crates/gpu/tests/canvas.rs+++ b/crates/gpu/tests/canvas.rs@@ -1345,3 +1345,58 @@ fn canvases_take_turns_with_a_shared_stroke_and_make_brush_images_when_wanted()assert!(c.draw_strokes_in(&gpu, &shared));assert!(c.textures().contains(shared.texture()));}++#[test]+fn a_drawn_stroke_takes_its_grain_from_a_picture_of_its_own() {+ use wgpu::util::DeviceExt;+ let Some(gpu) = gpu() else { return };+ let _check = NoErrors(&gpu);+ let mut doc = Document::new(32, 32);+ let mut canvas = Canvas::new(&gpu, &doc).unwrap();+ // the grain: black on the left half, white on the right+ let pixels: Vec<u8> = (0..32 * 32).map(|i| if i % 32 < 16 { 0 } else { 255 }).collect();+ let picture = gpu.device.create_texture_with_data(+ &gpu.queue,+ &wgpu::TextureDescriptor {+ label: Some("stroke grain"),+ size: wgpu::Extent3d { width: 32, height: 32, 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,+ );+ let grain = picture.create_view(&wgpu::TextureViewDescriptor {+ dimension: Some(wgpu::TextureViewDimension::D2Array),+ ..Default::default()+ });+ // a stroke with a grain fixed to the canvas, all of it deep, drawn into from elsewhere: a+ // band across the middle+ let grained = StrokeStyle {+ grain: Some(3),+ grain_depth: 1.0,+ grain_scale: 7.0,+ grain_rotation: 0.4,+ ..StrokeStyle::new(StrokeTool::Paint, [0.0; 3], 1.0)+ };+ canvas.begin_drawn_stroke(doc.current(), grained);+ canvas.set_stroke_grain(&gpu.device, &grain);+ // stamped into the stroke here (as a projection would draw it)+ canvas.add_stamps(&(0..32).map(|x| dot(Vec2::new(x as f32, 16.0), 5.0)).collect::<Vec<_>>());+ canvas.end_stroke();+ canvas.render(&gpu, &mut doc, None);+ let layer = canvas.read_layer(&gpu, doc.current()).unwrap();+ // painted where the picture is white, not where it is black, whatever the brush's own grain+ assert_eq!(pixel(&layer, 32, 24, 16)[3], 255);+ assert_eq!(pixel(&layer, 32, 8, 16)[3], 0);+ // the picture goes with the stroke: the next one reads the brush's grains again+ canvas.begin_stroke(doc.current(), style(StrokeTool::Paint, 1.0));+ canvas.add_stamps(&[dot(Vec2::new(8.0, 8.0), 3.0)]);+ canvas.end_stroke();+ canvas.render(&gpu, &mut doc, None);+ assert_eq!(pixel(&canvas.read_layer(&gpu, doc.current()).unwrap(), 32, 8, 8)[3], 255);+}diff --git a/crates/gpu/tests/mesh.rs b/crates/gpu/tests/mesh.rsindex 01f2c43..056a6b6 100644--- a/crates/gpu/tests/mesh.rs+++ b/crates/gpu/tests/mesh.rs@@ -258,8 +258,8 @@ f 1/1 2/2 3/3 4/4// 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 },+ MaterialLook { amount: 0.5, source: None, tile: 100.0, follows_camera: false },+ MaterialLook { amount: 1.0, source: Some(&black), tile: 100.0, follows_camera: true },];let mut materials: Vec<(Document, Canvas)> = looks.iter()@@ -651,3 +651,73 @@ f 1/1 2/2 3/3 4/4assert!(toward > away + 20, "lit from +y: {toward} leaning +v, {away} leaning -v");assert_eq!(gpu.errors(), 0);}++#[test]+fn a_strokes_grain_is_read_along_the_models_axes() {+ use procraft_gpu::{GrainLook, material_source};+ let Some(gpu) = gpu() else { return };+ // a square facing up over the whole texture (its far edge at the texture's top), and the+ // same facing down+ let up = "v -1 0 1\nv 1 0 1\nv 1 0 -1\nv -1 0 -1\nvt 0 0\nvt 1 0\nvt 1 1\nvt 0 1\nf 1/1 2/2 3/3 4/4\n";+ let down = "v -1 0 1\nv 1 0 1\nv 1 0 -1\nv -1 0 -1\nvt 0 0\nvt 1 0\nvt 1 1\nvt 0 1\nf 4/4 3/3 2/2 1/1\n";+ // a grain whose grey goes once round a wave down the image, the same across it+ let n = procraft_engine::textures::TEXTURE_SIZE;+ let wave = |t: f32| 128.0 + 127.0 * (std::f32::consts::TAU * t).sin();+ let pixels: Vec<u8> = (0..n * n).map(|i| wave(((i / n) as f32 + 0.5) / n as f32).round() as u8).collect();+ let source = material_source(&gpu, n as u32, &pixels).unwrap();+ // the square is 4 framing units across; one tile every 16 (16 / 0.00225 canvas pixels)+ let tile_units = 16.0;+ let side = 32u32;+ for (obj, facing_up) in [(up, true), (down, false)] {+ let mesh = read_obj(obj, None).unwrap();+ let mut view = MeshView::new(&gpu, &mesh, side, wgpu::TextureFormat::Rgba8Unorm);+ let look = GrainLook { source: &source, tile: tile_units / 0.00225, follows_camera: false };+ let mut encoder = gpu.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("grain") });+ let grain = view.project_grain(&gpu, &mut encoder, 0, &look).unwrap();+ gpu.queue.submit([encoder.finish()]);+ let px = read_texture_r8(&gpu, grain.texture(), side);+ // texel rows run down from the square's far edge (z -1) to its near one (z 1); seen from+ // above the image's rows run towards the far edge, from below towards the near one+ for row in [4u32, 12, 20, 28] {+ let z = -1.0 + 2.0 * (row as f32 + 0.5) / side as f32;+ let q = z * 2.0 / tile_units;+ let along = if facing_up { -q } else { q };+ let expected = wave(along.rem_euclid(1.0));+ for col in [3u32, 16, 29] {+ let got = f32::from(px[(row * side + col) as usize]);+ assert!((got - expected).abs() <= 4.0, "up {facing_up} row {row} col {col}: {got} vs {expected}");+ }+ }+ }+ assert_eq!(gpu.errors(), 0);+}++/// A one-grey texture `side` square read back.+fn read_texture_r8(gpu: &Gpu, texture: &wgpu::Texture, side: u32) -> Vec<u8> {+ let row = side.div_ceil(256) * 256;+ let buffer = gpu.device.create_buffer(&wgpu::BufferDescriptor {+ label: Some("read grain"),+ size: u64::from(row * side),+ usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,+ mapped_at_creation: false,+ });+ let mut encoder = gpu.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("read") });+ encoder.copy_texture_to_buffer(+ wgpu::TexelCopyTextureInfo {+ texture,+ mip_level: 0,+ origin: wgpu::Origin3d::ZERO,+ aspect: wgpu::TextureAspect::All,+ },+ wgpu::TexelCopyBufferInfo {+ buffer: &buffer,+ layout: wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(row), rows_per_image: Some(side) },+ },+ wgpu::Extent3d { width: side, height: side, depth_or_array_layers: 1 },+ );+ gpu.queue.submit([encoder.finish()]);+ buffer.slice(..).map_async(wgpu::MapMode::Read, |_| {});+ gpu.device.poll(wgpu::PollType::wait_indefinitely()).unwrap();+ let data = buffer.slice(..).get_mapped_range().unwrap().to_vec();+ (0..side).flat_map(|y| data[(y * row) as usize..(y * row + side) as usize].to_vec()).collect()+}