From 92fb48906a7429952efc95d9b949b8337d21cf44 Mon Sep 17 00:00:00 2001 From: SteamSmith Date: Thu, 12 Nov 2026 08:49:51 -0500 Subject: [PATCH 01/14] Paint the whole stroke on surfaces seen at a steep angle A 3D stroke painted with the surface turned far from the camera lost paint in stripes, speckles and jagged chunks: the brush's depth buffer is rendered at half the viewport size, and on such a surface the neighbouring pixels of the same surface are much nearer than the texel, so visible texels were judged hidden. Painting in the 2D view, which skips the test, was unaffected. When the depth buffer calls a texel hidden but its pixel neighbourhood reaches as far as the texel (`EyeDepth::uncertain`), the rasterizer now casts an exact ray toward the eye through the BVH and paints the texel when nothing is in the way. The ray starts just off the surface on the eye's side (so back faces with culling off don't hit themselves) and skips the same grazing allowance the buffer test uses. Texels behind every surface around them are still rejected without a ray, and geometry in front still hides what is behind it. Tests: a plane seen at 10, 3 and 1 degrees takes the whole stamp; a shelf hides the floor under it, including a stamp straddling its edge; a back face with culling off takes paint; `uncertain` rules out the far side of a sphere. Each fails with its part of the fix removed. Ignored benchmark `benchmark_silhouette_stamps` (64 stamps along a sphere's silhouette, 2048): 44 ms before, 53 ms after (268 ms without the `uncertain` gate). Co-Authored-By: Claude Opus 5.5 --- crates/paint/src/raster.rs | 185 ++++++++++++++++++++++++++++++++- crates/paint/src/visibility.rs | 41 ++++++-- 2 files changed, 214 insertions(+), 12 deletions(-) diff --git a/crates/paint/src/raster.rs b/crates/paint/src/raster.rs index b033538b..2e01aa6a 100644 --- a/crates/paint/src/raster.rs +++ b/crates/paint/src/raster.rs @@ -104,6 +104,31 @@ fn grazing_allowance(depth: &EyeDepth, point: Vec3, facing: f32, pixels_per_unit world_per_pixel * NEIGHBOUR_REACH * (1.0 - facing * facing).sqrt() / facing } +/// How far off the surface [`ray_reaches_eye`] starts its ray, so it doesn't hit the texel's own +/// triangle, as a fraction of [`EyeDepth::bias`] (itself 0.4 % of the mesh radius: this is +/// 0.001 % of the radius) +const RAY_START_PER_BIAS: f32 = 0.0025; + +/// Whether a ray from `point` (on a surface facing `facing` toward the eye along `to_eye`) reaches +/// the eye without hitting the mesh, ignoring the first `skip` world units. The depth buffer is +/// asked first; this exact test only checks the texels it calls hidden but can't rule out +/// ([`EyeDepth::uncertain`]): its pixels are coarse, so on a surface turned far from the camera it +/// hides texels the camera does see, and strokes painted at a steep angle came out in stripes and +/// jagged gaps +fn ray_reaches_eye(context: &RasterCtx, depth: &EyeDepth, point: Vec3, normal: Vec3, facing: f32, to_eye: Vec3, skip: f32) -> bool { + let eye_distance = context.eye.distance(point); + let lift = depth.bias * RAY_START_PER_BIAS; + let start = skip.max(0.0) + lift; + if !eye_distance.is_finite() || start >= eye_distance { + return true; + } + + // Lift off the surface on the eye's side, so back faces (culling off) don't hit themselves + let side = if facing < 0.0 { -normal } else { normal }; + let origin = point + side * lift + to_eye * start; + !context.bvh.occluded(origin, to_eye, eye_distance - start) +} + /// What the rasterizer reads. pub struct RasterCtx<'a> { pub mesh: &'a Mesh, @@ -238,10 +263,14 @@ fn scan_triangle( continue; } - if let Some(depth) = context.depth - && !depth.visible(seen + to_eye * (grazing_allowance(depth, seen, facing, pixels_per_unit) + off_surface)) - { - continue; + if let Some(depth) = context.depth { + let skip = grazing_allowance(depth, seen, facing, pixels_per_unit) + off_surface; + let tested = seen + to_eye * skip; + // The exact test runs only where the buffer calls a texel hidden but can't be sure + let hidden = !depth.visible(tested) && (!depth.uncertain(tested) || !ray_reaches_eye(context, depth, seen, seen_normal, facing, to_eye, skip)); + if hidden { + continue; + } } let fade = if context.backface_culling { smoothstep(fade_lo, fade_hi, facing) } else { 1.0 }; @@ -283,6 +312,154 @@ mod tests { use super::*; use texturecraft_geom::primitives; + /// Texels a stamp of `radius` at `center` paints (alpha above 1 %) on `mesh` seen by `camera`, + /// with the brush's visibility test (as a 3D stroke) or without it. + fn painted(mesh: &Mesh, camera: &texturecraft_geom::OrbitCamera, center: Vec3, radius: f32, culling: bool, visibility: bool) -> usize { + painted_texels(mesh, camera, center, radius, culling, visibility).len() + } + + fn painted_texels(mesh: &Mesh, camera: &texturecraft_geom::OrbitCamera, center: Vec3, radius: f32, culling: bool, visibility: bool) -> Vec<(u32, u32)> { + let bvh = Bvh::new(mesh); + let g = UvGBuffer::new(mesh, 0, 256, 256); + let mut canvas = StrokeCanvas::new(256, 256); + // Half a 960×540 viewport, as the session renders it for a stroke + let depth = EyeDepth::render(mesh, camera, 480, 270); + let ctx = RasterCtx { + mesh, + bvh: &bvh, + gbuf: &g, + material: 0, + depth: visibility.then_some(&depth), + eye: camera.eye(), + backface_culling: culling, + angle_fade: (89.0, 90.0), + shape: Shape { alpha: crate::BrushAlpha::Round, image: None, library: None, level: 0 }, + }; + let stamp = + Stamp { center, normal: Vec3::Y, tangent: Vec3::X, bitangent: Vec3::Z, radius, flow: 1.0, hardness: 0.9, depth: 1.0, seed: 0, source: None }; + rasterize(&mut canvas, &ctx, &[stamp]); + (0..256).flat_map(|y| (0..256).map(move |x| (x, y))).filter(|&(x, y)| from_u16(canvas.alpha.get(x, y)[0]) > 0.01).collect() + } + + fn looking_down(pitch_degrees: f32) -> texturecraft_geom::OrbitCamera { + texturecraft_geom::OrbitCamera { pitch: pitch_degrees.to_radians(), ..Default::default() } + } + + /// A plane seen almost edge-on: every texel of the stamp is in plain view, so the brush must + /// paint all of them. The coarse depth buffer used to call many hidden, and strokes painted at + /// a steep angle came out in stripes and jagged gaps. + #[test] + fn a_surface_seen_at_a_grazing_angle_takes_the_whole_stamp() { + let plane = primitives::plane(2.0, 8); + for pitch in [10.0, 3.0, 1.0] { + let all = painted(&plane, &looking_down(pitch), Vec3::ZERO, 0.3, true, false); + let seen = painted(&plane, &looking_down(pitch), Vec3::ZERO, 0.3, true, true); + assert!(all > 1000, "the stamp covers the plane: {all}"); + assert_eq!(seen, all, "at {pitch}° every texel the stamp covers is visible"); + } + } + + /// A shelf over half of a floor: under it the brush paints nothing, steep angle or not, while + /// the open half takes the stamp. + #[test] + fn geometry_in_front_still_hides_what_is_behind_it() { + let floor = [Vec3::new(-1.0, 0.0, -1.0), Vec3::new(1.0, 0.0, -1.0), Vec3::new(1.0, 0.0, 1.0), Vec3::new(-1.0, 0.0, 1.0)]; + let shelf = [Vec3::new(-1.0, 0.3, -1.0), Vec3::new(0.0, 0.3, -1.0), Vec3::new(0.0, 0.3, 1.0), Vec3::new(-1.0, 0.3, 1.0)]; + let uv = |p: Vec3| texturecraft_geom::glam::Vec2::new((p.x + 1.0) / 2.0, (p.z + 1.0) / 2.0); + // The shelf maps to one corner point of the texture, so only the floor has texels + let uvs = floor.iter().map(|p| uv(*p)).chain([texturecraft_geom::glam::Vec2::ONE; 4]).collect(); + // Wound to face up (+Y), toward the camera + let quads = [0u32, 2, 1, 0, 3, 2]; + let indices = quads.iter().copied().chain(quads.iter().map(|i| i + 4)).collect(); + let mesh = Mesh::from_parts(floor.into_iter().chain(shelf).collect(), None, uvs, indices, "m").unwrap(); + for pitch in [60.0, 30.0] { + let camera = looking_down(pitch); + let open = Vec3::new(0.5, 0.0, -0.5); + assert!(painted(&mesh, &camera, open, 0.2, true, false) > 100, "the stamp covers the open half"); + assert_eq!( + painted(&mesh, &camera, open, 0.2, true, true), + painted(&mesh, &camera, open, 0.2, true, false), + "the open half takes the stamp at {pitch}°" + ); + let under = Vec3::new(-0.5, 0.0, -0.5); + assert!(painted(&mesh, &camera, under, 0.2, true, false) > 100, "the stamp reaches under the shelf"); + assert_eq!(painted(&mesh, &camera, under, 0.2, true, true), 0, "the shelf hides the floor under it at {pitch}°"); + // Across the shelf's edge (x = 0, texel column 128) the buffer can't tell the two sides + // apart, and the exact test decides: the open side is painted, the side under the shelf isn't + let edge = Vec3::new(0.0, 0.0, -0.5); + let all = painted_texels(&mesh, &camera, edge, 0.2, true, false); + let seen = painted_texels(&mesh, &camera, edge, 0.2, true, true); + let open: Vec<_> = all.iter().filter(|t| t.0 >= 129).copied().collect(); + assert!(open.len() > 100 && all.len() > open.len() + 100, "the stamp straddles the edge"); + assert!(open.iter().all(|t| seen.contains(t)), "every open texel is painted at {pitch}°"); + assert!(seen.iter().all(|t| t.0 >= 127), "nothing under the shelf is painted at {pitch}°"); + } + } + + /// With backface culling off, a single-sided surface seen from behind still takes paint: the + /// exact test starts on the eye's side of the surface, so it doesn't hit the surface itself. + #[test] + fn a_back_face_with_culling_off_takes_paint() { + let plane = primitives::plane(2.0, 8); + for pitch in [-45.0, -3.0] { + let all = painted(&plane, &looking_down(pitch), Vec3::ZERO, 0.3, false, false); + assert!(all > 1000); + assert_eq!(painted(&plane, &looking_down(pitch), Vec3::ZERO, 0.3, false, true), all, "from below at {pitch}°"); + } + } + + /// Stamps along a sphere's silhouette, half of each really hidden: the most texels the exact + /// ray test has to check. `cargo test --release -p texturecraft-paint --lib -- --ignored benchmark_silhouette --nocapture` + #[test] + #[ignore = "release performance measurement"] + fn benchmark_silhouette_stamps() { + let sphere = primitives::sphere(1.0, 256, 128); + let bvh = Bvh::new(&sphere); + let g = UvGBuffer::new(&sphere, 0, 2048, 2048); + let camera = looking_down(0.0); + let depth = EyeDepth::render(&sphere, &camera, 960, 540); + let ctx = RasterCtx { + mesh: &sphere, + bvh: &bvh, + gbuf: &g, + material: 0, + depth: Some(&depth), + eye: camera.eye(), + backface_culling: false, + angle_fade: (89.0, 90.0), + shape: Shape { alpha: crate::BrushAlpha::Round, image: None, library: None, level: 0 }, + }; + // The silhouette seen from +Z is the circle z = 0 + let stamps: Vec = (0..64) + .map(|k| { + let a = k as f32 / 64.0 * std::f32::consts::TAU; + let center = Vec3::new(a.cos(), a.sin(), 0.0); + let tangent = Vec3::Z; + Stamp { + center, + normal: center, + tangent, + bitangent: center.cross(tangent), + radius: 0.15, + flow: 1.0, + hardness: 0.9, + depth: 1.0, + seed: k, + source: None, + } + }) + .collect(); + let mut times = Vec::new(); + for _ in 0..9 { + let mut canvas = StrokeCanvas::new(2048, 2048); + let start = std::time::Instant::now(); + rasterize(&mut canvas, &ctx, &stamps); + times.push(start.elapsed().as_secs_f64() * 1000.0); + } + times.sort_by(f64::total_cmp); + eprintln!("64 silhouette stamps at 2048²: median {:.2} ms, range {:.2}..{:.2} ms", times[4], times[0], times[8]); + } + #[test] fn one_stamp_on_a_plane_paints_a_disc() { let m = primitives::plane(2.0, 8); diff --git a/crates/paint/src/visibility.rs b/crates/paint/src/visibility.rs index ed7c575e..6342a22c 100644 --- a/crates/paint/src/visibility.rs +++ b/crates/paint/src/visibility.rs @@ -161,31 +161,52 @@ impl EyeDepth { /// Whether world point `p` is visible (not hidden behind other geometry, inside the view). pub fn visible(&self, p: Vec3) -> bool { + // Conservative: the nearest surface among the pixel and its neighbours. + self.neighbourhood(p).is_some_and(|(near, _)| p.distance(self.eye) <= near + self.tolerance(near)) + } + + /// Whether world point `p`, inside the view, lies no farther than the farthest surface in its + /// pixel's neighbourhood: then the buffer alone can't tell whether it is hidden (on a surface + /// turned far from the camera, neighbouring pixels of the same surface are much nearer), while + /// a point behind every surface around it is hidden for sure. + pub fn uncertain(&self, p: Vec3) -> bool { + self.neighbourhood(p).is_some_and(|(_, far)| far < f32::MAX && p.distance(self.eye) <= far + self.tolerance(far)) + } + + /// Allowance at eye distance `d`: a bias that grows with distance (depth buffer resolution). + fn tolerance(&self, d: f32) -> f32 { + self.bias + d * 0.002 + } + + /// The nearest and farthest surface among the pixel `p` lands in and its neighbours (f32::MAX + /// for both where they are all empty), or None outside the view or behind the camera. + fn neighbourhood(&self, p: Vec3) -> Option<(f32, f32)> { let c = self.view_proj * p.extend(1.0); if c.w <= 1e-6 { - return false; + return None; } let (nx, ny) = (c.x / c.w, c.y / c.w); if nx.abs() > 1.0 || ny.abs() > 1.0 { - return false; + return None; } let x = (((nx * 0.5 + 0.5) * self.width as f32) as u32).min(self.width - 1); let y = (((0.5 - ny * 0.5) * self.height as f32) as u32).min(self.height - 1); - // Conservative: the nearest surface among the pixel and its neighbours. - let mut near = f32::MAX; + let (mut near, mut far) = (f32::MAX, f32::MIN); for dy in -1i32..=1 { for dx in -1i32..=1 { let (sx, sy) = (x as i32 + dx, y as i32 + dy); if sx < 0 || sy < 0 || sx >= self.width as i32 || sy >= self.height as i32 { continue; } - if let Some(d) = self.dist.get((sy as u32 * self.width + sx as u32) as usize) { - near = near.min(*d); + if let Some(&d) = self.dist.get((sy as u32 * self.width + sx as u32) as usize) { + near = near.min(d); + if d < f32::MAX { + far = far.max(d); + } } } } - // Allow a bias that grows with distance (depth buffer resolution). - p.distance(self.eye) <= near + self.bias + near * 0.002 + Some((near, if far == f32::MIN { f32::MAX } else { far })) } } @@ -206,5 +227,9 @@ mod tests { assert!(d.visible(toward * 1.0)); assert!(!d.visible(-toward * 1.0)); assert!(!d.visible(Vec3::new(100.0, 0.0, 0.0))); + // The far side is behind every surface around it: hidden for sure, no exact test needed + assert!(!d.uncertain(-toward * 1.0)); + assert!(d.uncertain(toward * 1.0)); + assert!(!d.uncertain(Vec3::new(100.0, 0.0, 0.0)), "outside the view"); } } From 926b0ae34222ce1afda7c8ac0c6316ff58bd4021 Mon Sep 17 00:00:00 2001 From: SteamSmith Date: Sat, 14 Nov 2026 11:05:02 -0500 Subject: [PATCH 02/14] fix(paint): treat a pixel beside empty ones as uncertain, so a grazing surface's far edge gets the exact test Co-Authored-By: Claude Sonnet 5.5 --- crates/paint/src/raster.rs | 21 +++++++++++++++++++++ crates/paint/src/visibility.rs | 25 ++++++++++++++++--------- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/crates/paint/src/raster.rs b/crates/paint/src/raster.rs index 2e01aa6a..26711285 100644 --- a/crates/paint/src/raster.rs +++ b/crates/paint/src/raster.rs @@ -396,6 +396,27 @@ mod tests { } } + /// A stamp at the far edge of a grazing plane: the pixels beyond the edge are empty, so the + /// texels at the edge lie farther than every finite neighbour. They are in plain view, so they + /// must still be painted (the buffer can't prove them hidden, the exact test must decide). + #[test] + fn the_far_edge_of_a_grazing_surface_takes_paint() { + let plane = primitives::plane(2.0, 8); + for culling in [true, false] { + for pitch in [10.0, 3.0, 1.0, -3.0] { + if culling && pitch < 0.0 { + continue; + } + for edge in [Vec3::new(0.0, 0.0, -1.0), Vec3::new(0.0, 0.0, 1.0), Vec3::new(-1.0, 0.0, 0.0), Vec3::new(1.0, 0.0, 0.0)] { + let all = painted_texels(&plane, &looking_down(pitch), edge, 0.3, culling, false); + let seen = painted_texels(&plane, &looking_down(pitch), edge, 0.3, culling, true); + assert!(all.len() > 200, "the stamp covers part of the plane: {}", all.len()); + assert_eq!(seen.len(), all.len(), "edge {edge} at {pitch} degrees (culling {culling}): every texel is in view"); + } + } + } + } + /// With backface culling off, a single-sided surface seen from behind still takes paint: the /// exact test starts on the eye's side of the surface, so it doesn't hit the surface itself. #[test] diff --git a/crates/paint/src/visibility.rs b/crates/paint/src/visibility.rs index 6342a22c..366fd8cf 100644 --- a/crates/paint/src/visibility.rs +++ b/crates/paint/src/visibility.rs @@ -162,15 +162,17 @@ impl EyeDepth { /// Whether world point `p` is visible (not hidden behind other geometry, inside the view). pub fn visible(&self, p: Vec3) -> bool { // Conservative: the nearest surface among the pixel and its neighbours. - self.neighbourhood(p).is_some_and(|(near, _)| p.distance(self.eye) <= near + self.tolerance(near)) + self.neighbourhood(p).is_some_and(|(near, _, _)| p.distance(self.eye) <= near + self.tolerance(near)) } /// Whether world point `p`, inside the view, lies no farther than the farthest surface in its - /// pixel's neighbourhood: then the buffer alone can't tell whether it is hidden (on a surface - /// turned far from the camera, neighbouring pixels of the same surface are much nearer), while - /// a point behind every surface around it is hidden for sure. + /// pixel's neighbourhood, or has an empty pixel beside it: then the buffer alone can't tell + /// whether it is hidden (on a surface turned far from the camera, neighbouring pixels of the + /// same surface are much nearer, and where such a surface ends between pixel centres its edge + /// lies beyond every finite neighbour), while a point behind every surface all around it is + /// hidden for sure. pub fn uncertain(&self, p: Vec3) -> bool { - self.neighbourhood(p).is_some_and(|(_, far)| far < f32::MAX && p.distance(self.eye) <= far + self.tolerance(far)) + self.neighbourhood(p).is_some_and(|(_, far, has_empty)| has_empty || (far < f32::MAX && p.distance(self.eye) <= far + self.tolerance(far))) } /// Allowance at eye distance `d`: a bias that grows with distance (depth buffer resolution). @@ -179,8 +181,9 @@ impl EyeDepth { } /// The nearest and farthest surface among the pixel `p` lands in and its neighbours (f32::MAX - /// for both where they are all empty), or None outside the view or behind the camera. - fn neighbourhood(&self, p: Vec3) -> Option<(f32, f32)> { + /// for both where they are all empty), and whether any of those pixels is empty, or None + /// outside the view or behind the camera. + fn neighbourhood(&self, p: Vec3) -> Option<(f32, f32, bool)> { let c = self.view_proj * p.extend(1.0); if c.w <= 1e-6 { return None; @@ -191,7 +194,7 @@ impl EyeDepth { } let x = (((nx * 0.5 + 0.5) * self.width as f32) as u32).min(self.width - 1); let y = (((0.5 - ny * 0.5) * self.height as f32) as u32).min(self.height - 1); - let (mut near, mut far) = (f32::MAX, f32::MIN); + let (mut near, mut far, mut has_empty) = (f32::MAX, f32::MIN, false); for dy in -1i32..=1 { for dx in -1i32..=1 { let (sx, sy) = (x as i32 + dx, y as i32 + dy); @@ -202,11 +205,13 @@ impl EyeDepth { near = near.min(d); if d < f32::MAX { far = far.max(d); + } else { + has_empty = true; } } } } - Some((near, if far == f32::MIN { f32::MAX } else { far })) + Some((near, if far == f32::MIN { f32::MAX } else { far }, has_empty)) } } @@ -231,5 +236,7 @@ mod tests { assert!(!d.uncertain(-toward * 1.0)); assert!(d.uncertain(toward * 1.0)); assert!(!d.uncertain(Vec3::new(100.0, 0.0, 0.0)), "outside the view"); + // Beside empty pixels (a silhouette) the buffer can't prove a point hidden + assert!(d.uncertain(Vec3::new(0.0, 1.0, 0.0)), "on the silhouette"); } } From df816f52694c913faa50ebd258fcef1bb0a90720 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 16 Nov 2026 00:12:35 +0100 Subject: [PATCH 03/14] Bake: a rounded edge reads the same in mesh curvature at every map size The smooth-shading ring of the mesh curvature baker was two texels wide, so it reached half as far on the mesh with every doubling of the map size, and a smooth bevel's brightness above flat halved with it: rounded edges faded at 2048 and 4096 and edge wear faded with them. The ring is now two texels of a 1024 map per UV tile, the same distance on the mesh at every map size. A bake on a 1024x1024 map is bit-identical (the factor is exactly 1). Low-resolution bakes (128, 256) lose most of their smooth-bevel brightness and read like 1024; 2048 and 4096 gain it back. The wall rings stay sized in texels, so hard edge and contact lines keep their width in texels. Tests on low-size bakes that pinned the old, size-dependent brightness of smooth bevels now expect the 1024 reading. Fixes #536 --- crates/bake/src/map/curvature.rs | 5 +++- crates/bake/src/tests.rs | 51 ++++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/crates/bake/src/map/curvature.rs b/crates/bake/src/map/curvature.rs index eac8dd52..ac6237e6 100644 --- a/crates/bake/src/map/curvature.rs +++ b/crates/bake/src/map/curvature.rs @@ -7,6 +7,8 @@ use texturecraft_geom::{Hit, Mesh, Surface}; const RING_COUNT: usize = 4; const WALL_RADIUS_TEXELS: f32 = 1.5; +/// Map size per UV tile whose texels size the smooth-shading ring: at any other size the ring reaches as far on the mesh, so a rounded edge reads alike at every resolution +const SPREAD_REFERENCE_RESOLUTION: f32 = 1024.0; const CONCAVE_WEIGHT: f32 = 1.5; const LOW_POLY_CONVEX_WEIGHT: f32 = 1.5; const HIGH_POLY_CONVEX_WEIGHT: f32 = 0.5; @@ -64,7 +66,8 @@ impl Curvature { rays_per_sample, wall_count: ring.first().map_or(0, Vec::len), walls, - spread: walls * 4.0 / 3.0, + // Texels of a 1024 map (a factor of exactly 1 at 1024): a texel-sized ring halved a smooth bevel's response with each doubling of the map size + spread: walls * 4.0 / 3.0 * ((width as f32 * height as f32).sqrt() / SPREAD_REFERENCE_RESOLUTION), fallback, texels: (0..triangle_count).into_par_iter().map(|t| texel(t).filter(|v| v.is_finite()).unwrap_or(fallback)).collect(), low: CurvatureTuning { diff --git a/crates/bake/src/tests.rs b/crates/bake/src/tests.rs index 0c313462..5efc0221 100644 --- a/crates/bake/src/tests.rs +++ b/crates/bake/src/tests.rs @@ -3,7 +3,7 @@ use texturecraft_geom::{BaseColor, Bvh}; use texturecraft_core::{ AmbientOcclusionSettings, BakeAntialiasing, BakeResolution, BakeSettings, BentNormalSettings, CageSettings, CommonBakeSettings, CurvatureSettings, - PositionSettings, + MeshCurvatureSettings, PositionSettings, }; use std::sync::atomic::Ordering::Relaxed; @@ -243,12 +243,34 @@ fn a_rounded_bevel_reads_the_same_at_any_map_size() { v.sort_by(f32::total_cmp); v.get(v.len() / 2).copied().unwrap_or(0.5) }; - let (coarse, fine) = (peak(512), peak(1024)); - assert!(coarse > 0.58, "bevel at 512: {coarse}"); - assert!((coarse - fine).abs() < 0.1, "512: {coarse}, 1024: {fine}"); + let (coarse, reference, fine) = (peak(512), peak(1024), peak(2048)); + assert!(reference > 0.525, "bevel at 1024: {reference}"); + // A texel-sized ring read this bevel about 0.03 brighter at 512 and 0.02 darker at 2048 + assert!((coarse - reference).abs() < 0.005 && (fine - reference).abs() < 0.005, "512: {coarse}, 1024: {reference}, 2048: {fine}"); } -fn check_box_curvature(m: &Mesh, edge: f32) { +/// Median curvature on the sample crate's four upright body bevels, away from the top and bottom bevels +fn crate_body_bevel(resolution: u32) -> f32 { + let mesh = primitives::crate_scene(); + let mut parameters = settings(resolution, &[BakedMap::Curvature]); + parameters.maps.curvature.parameters.mesh.auto_tonemapping = MeshCurvatureSettings::default().auto_tonemapping; + let curvature = covered_values(&mesh, &run(&mesh, ¶meters).unwrap(), BakedMap::Curvature); + let mut bevel: Vec = + curvature.iter().filter(|(p, _)| p.x.abs() > 1.1 && p.z.abs() > 0.7 && (0.3..1.3).contains(&p.y)).map(|(_, value)| *value).collect(); + assert!(!bevel.is_empty()); + bevel.sort_by(f32::total_cmp); + bevel.get(bevel.len() / 2).copied().unwrap_or(0.5) +} + +#[test] +fn the_sample_crate_bevels_keep_their_1024_reading_at_2048() { + let (reference, fine) = (crate_body_bevel(1024), crate_body_bevel(2048)); + // The 1024 bake is unchanged: the smooth-shading ring is sized in texels of a 1024 map + assert!((0.505..0.545).contains(&reference), "1024: {reference}"); + assert!((fine - reference).abs() < 0.004, "1024: {reference}, 2048: {fine}"); +} + +fn check_box_curvature(m: &Mesh, edge: f32, brightness: f32) { let s = { let mut base = settings(128, &[BakedMap::Curvature]); base.common = CommonBakeSettings { dilation: 4, ..base.common }; @@ -260,18 +282,19 @@ fn check_box_curvature(m: &Mesh, edge: f32) { let faces: Vec = c.iter().filter(|(p, _)| near(*p, 0.5) == 1).map(|v| v.1).collect(); assert!(!edges.is_empty() && !faces.is_empty()); let min_edge = edges.iter().copied().fold(1.0, f32::min); - assert!(min_edge > 0.55, "edge min {min_edge}"); + assert!(min_edge > brightness, "edge min {min_edge}"); assert!(faces.iter().all(|v| (0.45..=0.55).contains(v)), "face {:?}", faces.iter().copied().fold((1.0f32, 0.0f32), |(a, b), v| (a.min(v), b.max(v)))); } #[test] fn rounded_box_bevels_are_convex() { - check_box_curvature(&primitives::rounded_box(Vec3::ONE, 0.15), 0.9); + // A smooth bevel reads as it does at 1024 px: a little above flat, not as bright as a hard edge + check_box_curvature(&primitives::rounded_box(Vec3::ONE, 0.15), 0.9, 0.505); } #[test] fn cube_edges_are_convex() { - check_box_curvature(&primitives::cube(2.0), 0.97); + check_box_curvature(&primitives::cube(2.0), 0.97, 0.55); } #[test] @@ -570,11 +593,12 @@ fn a_high_poly_box_has_bright_edges_and_flat_faces() { base.common = CommonBakeSettings { dilation: 4, ..base.common }; base }; - for (m, edge) in [(primitives::cube(2.0), 0.97), (primitives::rounded_box(Vec3::ONE, 0.15), 0.9)] { + // A smooth bevel reads as it does at 1024 px, a little above flat + for (m, edge, bright) in [(primitives::cube(2.0), 0.97, 0.55), (primitives::rounded_box(Vec3::ONE, 0.15), 0.9, 0.51)] { let c = covered_values(&m, &transfer(&m, &m, None, &s).unwrap(), BakedMap::Curvature); let near = |p: Vec3, t: f32| p.abs().to_array().iter().filter(|c| **c > t).count(); let brightest = c.iter().filter(|(p, _)| near(*p, edge) >= 2).map(|v| v.1).fold(0.0, f32::max); - assert!(brightest > 0.55, "edge {brightest}"); + assert!(brightest > bright, "edge {brightest}"); assert!(c.iter().filter(|(p, _)| near(*p, 0.5) == 1).all(|v| (0.45..=0.55).contains(&v.1)), "faces"); } } @@ -588,13 +612,14 @@ fn default_curvature_brightens_box_edges_and_never_darkens_them() { // A box has no concave texel, whether it is baked from itself or from a beveled high poly let low = primitives::cube(2.0); let high = primitives::rounded_box(Vec3::ONE, 0.15); - let bakes = [("itself", run(&low, &bake_settings).unwrap()), ("a beveled high poly", transfer(&low, &high, None, &bake_settings).unwrap())]; - for (source, bake_output) in bakes { + // The smooth bevel reads as it does at 1024 px, a little above flat; the hard edges stay bright + let bakes = [("itself", run(&low, &bake_settings).unwrap(), 0.6), ("a beveled high poly", transfer(&low, &high, None, &bake_settings).unwrap(), 0.52)]; + for (source, bake_output, bright) in bakes { let curvature = covered_values(&low, &bake_output, BakedMap::Curvature); let darkest = curvature.iter().map(|(_, value)| *value).fold(1.0, f32::min); let brightest = curvature.iter().map(|(_, value)| *value).fold(0.0, f32::max); assert!(darkest > 0.45, "baked from {source}, darkest {darkest}"); - assert!(brightest > 0.6, "baked from {source}, brightest {brightest}"); + assert!(brightest > bright, "baked from {source}, brightest {brightest}"); } } From a93d5891f0d7d3c382aa537401770feae26d09d3 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 16 Nov 2026 00:24:57 +0100 Subject: [PATCH 04/14] Bake tests: measured bounds for smooth bevels at every map size The crate check compares 512, 1024 and 2048 and pins the 1024 value; a texel-sized ring read 0.531, 0.516 and 0.508 there. Smooth-bevel checks at 128 and 256 compare the edge median with the face median instead of an absolute bound near 0.5; hard-edge bounds stay absolute. --- crates/bake/src/tests.rs | 61 +++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/crates/bake/src/tests.rs b/crates/bake/src/tests.rs index 5efc0221..59288630 100644 --- a/crates/bake/src/tests.rs +++ b/crates/bake/src/tests.rs @@ -245,7 +245,7 @@ fn a_rounded_bevel_reads_the_same_at_any_map_size() { }; let (coarse, reference, fine) = (peak(512), peak(1024), peak(2048)); assert!(reference > 0.525, "bevel at 1024: {reference}"); - // A texel-sized ring read this bevel about 0.03 brighter at 512 and 0.02 darker at 2048 + // A texel-sized ring read this bevel 0.04 brighter at 512 and 0.02 darker at 2048 assert!((coarse - reference).abs() < 0.005 && (fine - reference).abs() < 0.005, "512: {coarse}, 1024: {reference}, 2048: {fine}"); } @@ -263,14 +263,20 @@ fn crate_body_bevel(resolution: u32) -> f32 { } #[test] -fn the_sample_crate_bevels_keep_their_1024_reading_at_2048() { - let (reference, fine) = (crate_body_bevel(1024), crate_body_bevel(2048)); - // The 1024 bake is unchanged: the smooth-shading ring is sized in texels of a 1024 map - assert!((0.505..0.545).contains(&reference), "1024: {reference}"); - assert!((fine - reference).abs() < 0.004, "1024: {reference}, 2048: {fine}"); +fn the_sample_crate_bevels_read_the_same_at_512_1024_and_2048() { + let (coarse, reference, fine) = (crate_body_bevel(512), crate_body_bevel(1024), crate_body_bevel(2048)); + // The 1024 bake is unchanged (the smooth-shading ring is sized in texels of a 1024 map); a texel-sized ring read 0.531 at 512 and 0.508 at 2048 + assert!((0.5135..0.5175).contains(&reference), "1024: {reference}"); + assert!((coarse - reference).abs() < 0.002 && (fine - reference).abs() < 0.002, "512: {coarse}, 1024: {reference}, 2048: {fine}"); } -fn check_box_curvature(m: &Mesh, edge: f32, brightness: f32) { +fn median(mut values: Vec) -> f32 { + values.sort_by(f32::total_cmp); + values.get(values.len() / 2).copied().unwrap_or(0.5) +} + +/// Edges brighter than the faces, faces flat; `darkest_edge` also bounds every edge texel (hard edges) +fn check_box_curvature(m: &Mesh, edge: f32, darkest_edge: Option) { let s = { let mut base = settings(128, &[BakedMap::Curvature]); base.common = CommonBakeSettings { dilation: 4, ..base.common }; @@ -281,20 +287,24 @@ fn check_box_curvature(m: &Mesh, edge: f32, brightness: f32) { let edges: Vec = c.iter().filter(|(p, _)| near(*p, edge) >= 2).map(|v| v.1).collect(); let faces: Vec = c.iter().filter(|(p, _)| near(*p, 0.5) == 1).map(|v| v.1).collect(); assert!(!edges.is_empty() && !faces.is_empty()); - let min_edge = edges.iter().copied().fold(1.0, f32::min); - assert!(min_edge > brightness, "edge min {min_edge}"); + let (edge_median, face_median) = (median(edges.clone()), median(faces.clone())); + assert!(edge_median > face_median + 0.01, "edge median {edge_median}, face median {face_median}"); + if let Some(darkest_edge) = darkest_edge { + let min_edge = edges.iter().copied().fold(1.0, f32::min); + assert!(min_edge > darkest_edge, "edge min {min_edge}"); + } assert!(faces.iter().all(|v| (0.45..=0.55).contains(v)), "face {:?}", faces.iter().copied().fold((1.0f32, 0.0f32), |(a, b), v| (a.min(v), b.max(v)))); } #[test] fn rounded_box_bevels_are_convex() { // A smooth bevel reads as it does at 1024 px: a little above flat, not as bright as a hard edge - check_box_curvature(&primitives::rounded_box(Vec3::ONE, 0.15), 0.9, 0.505); + check_box_curvature(&primitives::rounded_box(Vec3::ONE, 0.15), 0.9, None); } #[test] fn cube_edges_are_convex() { - check_box_curvature(&primitives::cube(2.0), 0.97, 0.55); + check_box_curvature(&primitives::cube(2.0), 0.97, Some(0.55)); } #[test] @@ -593,12 +603,18 @@ fn a_high_poly_box_has_bright_edges_and_flat_faces() { base.common = CommonBakeSettings { dilation: 4, ..base.common }; base }; - // A smooth bevel reads as it does at 1024 px, a little above flat - for (m, edge, bright) in [(primitives::cube(2.0), 0.97, 0.55), (primitives::rounded_box(Vec3::ONE, 0.15), 0.9, 0.51)] { + // A hard edge is bright; a smooth bevel reads as it does at 1024 px, a little above the flat faces + for (m, edge, hard) in [(primitives::cube(2.0), 0.97, true), (primitives::rounded_box(Vec3::ONE, 0.15), 0.9, false)] { let c = covered_values(&m, &transfer(&m, &m, None, &s).unwrap(), BakedMap::Curvature); let near = |p: Vec3, t: f32| p.abs().to_array().iter().filter(|c| **c > t).count(); - let brightest = c.iter().filter(|(p, _)| near(*p, edge) >= 2).map(|v| v.1).fold(0.0, f32::max); - assert!(brightest > bright, "edge {brightest}"); + let edges: Vec = c.iter().filter(|(p, _)| near(*p, edge) >= 2).map(|v| v.1).collect(); + if hard { + let brightest = edges.iter().copied().fold(0.0, f32::max); + assert!(brightest > 0.55, "edge {brightest}"); + } else { + let (edge_median, face_median) = (median(edges), median(c.iter().filter(|(p, _)| near(*p, 0.5) == 1).map(|v| v.1).collect())); + assert!(edge_median > face_median + 0.008, "edge median {edge_median}, face median {face_median}"); + } assert!(c.iter().filter(|(p, _)| near(*p, 0.5) == 1).all(|v| (0.45..=0.55).contains(&v.1)), "faces"); } } @@ -612,14 +628,21 @@ fn default_curvature_brightens_box_edges_and_never_darkens_them() { // A box has no concave texel, whether it is baked from itself or from a beveled high poly let low = primitives::cube(2.0); let high = primitives::rounded_box(Vec3::ONE, 0.15); - // The smooth bevel reads as it does at 1024 px, a little above flat; the hard edges stay bright - let bakes = [("itself", run(&low, &bake_settings).unwrap(), 0.6), ("a beveled high poly", transfer(&low, &high, None, &bake_settings).unwrap(), 0.52)]; - for (source, bake_output, bright) in bakes { + // The hard edges stay bright; the smooth bevel reads as it does at 1024 px, a little above the flat faces + let bakes = [("itself", run(&low, &bake_settings).unwrap(), true), ("a beveled high poly", transfer(&low, &high, None, &bake_settings).unwrap(), false)]; + for (source, bake_output, hard) in bakes { let curvature = covered_values(&low, &bake_output, BakedMap::Curvature); let darkest = curvature.iter().map(|(_, value)| *value).fold(1.0, f32::min); let brightest = curvature.iter().map(|(_, value)| *value).fold(0.0, f32::max); assert!(darkest > 0.45, "baked from {source}, darkest {darkest}"); - assert!(brightest > bright, "baked from {source}, brightest {brightest}"); + if hard { + assert!(brightest > 0.6, "baked from {source}, brightest {brightest}"); + } else { + let near = |p: Vec3, t: f32| p.abs().to_array().iter().filter(|c| **c > t).count(); + let edge_median = median(curvature.iter().filter(|(p, _)| near(*p, 0.9) >= 2).map(|v| v.1).collect()); + let face_median = median(curvature.iter().filter(|(p, _)| near(*p, 0.5) == 1).map(|v| v.1).collect()); + assert!(edge_median > face_median + 0.008, "baked from {source}, edge median {edge_median}, face median {face_median}"); + } } } From 343b33ee60a30d74a436585ede2b4199150e1580 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 16 Nov 2026 19:50:20 +0100 Subject: [PATCH 05/14] Bake bent normal maps in tangent space like the normal map The bent normal map packed the world-space direction while the normal map uses the low poly's tangent frame (OpenGL green). Encode the bent normal in the same frame on the shared CPU/GPU packing path, read imported bent normal images in the project's normal format, and decode it with the vertex tangent frame in the neutral material view. Projects saved before format version 3 held world-space bent normal maps: loading drops them, keeps the bake settings and asks for a re-bake. Part of #538 --- crates/bake/README.md | 2 +- crates/bake/src/map/bake_context.rs | 3 +- crates/bake/src/test/bent_normal_space.rs | 74 +++++++++++++++++++++++ crates/bake/src/tests.rs | 5 +- crates/core/src/lib.rs | 2 +- crates/core/src/project.rs | 6 +- crates/engine/src/meshmap_cmds.rs | 7 ++- crates/engine/tests/it/painter_gaps.rs | 17 ++++++ crates/formats/README.md | 4 ++ crates/formats/src/project_file/mod.rs | 27 ++++++++- crates/formats/src/project_file/tests.rs | 29 +++++++++ crates/render/src/shaders/bake_view.wgsl | 10 +-- crates/render/src/shaders/mesh.wgsl | 2 +- crates/render/tests/headless.rs | 27 +++++++++ docs/control-protocol.md | 7 ++- 15 files changed, 205 insertions(+), 17 deletions(-) create mode 100644 crates/bake/src/test/bent_normal_space.rs diff --git a/crates/bake/README.md b/crates/bake/README.md index 0eac65cc..e24de0ad 100644 --- a/crates/bake/README.md +++ b/crates/bake/README.md @@ -22,7 +22,7 @@ Clean-room mesh-map baking from low-poly UVs and optional high-poly meshes. Pers | World Normal | World-space shading normal | | ID | Vertex Color, Material Color, Mesh ID, Submesh ID, or File ID; Hue Shift, Random, and Grayscale generation | | Ambient Occlusion | Independent ray count, distance interval, units, cone and distribution; backface and self-occlusion modes; None, Linear or Smooth attenuation; optional ground plane | -| Bent Normal | Independent occlusion settings and mean visible direction, encoded in world space | +| Bent Normal | Independent occlusion settings and mean visible direction, encoded in tangent space like the normal map (OpenGL green, low-poly tangent frame) | | Thickness | Inward ray distances with self-occlusion filtering; raw, ray-distance or per-UV-tile min/max normalization | | Curvature | Mesh-ray curvature with radius, ray count, self-intersection and automatic/manual tonemapping; normal-map curvature with detail strength and corresponding UV-edge seam blending | | Position | All axes or a selected axis; scene or material bounds; box, uniform sphere-style or raw coordinates | diff --git a/crates/bake/src/map/bake_context.rs b/crates/bake/src/map/bake_context.rs index f3e35df6..bbeb623e 100644 --- a/crates/bake/src/map/bake_context.rs +++ b/crates/bake/src/map/bake_context.rs @@ -232,7 +232,8 @@ impl<'a> BakeContext<'a> { for (&map, output) in maps.iter().zip(outputs.iter_mut()) { *output = match map { BakedMap::AmbientOcclusion => [traced.ao; 3], - BakedMap::BentNormal => pack(traced.bent.unwrap_or(surface.nrm)), + // In the low poly's tangent frame (OpenGL green), like the normal map + BakedMap::BentNormal => tangent_normal(low_surface, traced.bent.unwrap_or(surface.nrm)), BakedMap::Thickness => [traced.thickness; 3], BakedMap::Curvature if self.curvature_method == CurvatureMethod::NormalMap => [0.5; 3], BakedMap::Curvature => [traced.curvature; 3], diff --git a/crates/bake/src/test/bent_normal_space.rs b/crates/bake/src/test/bent_normal_space.rs new file mode 100644 index 00000000..6afa0745 --- /dev/null +++ b/crates/bake/src/test/bent_normal_space.rs @@ -0,0 +1,74 @@ +//! Bent normal maps are stored in the low poly's tangent frame (OpenGL green), like the normal map + +use super::*; + +/// How far a packed bent normal may stray from the expected value: the hemisphere's few rays leave +/// a small sideways remainder +const TOLERANCE: f32 = 0.04; + +/// The packed bent normal of every covered texel, with the low poly's surface normal there +fn bent_texels(mesh: &Mesh, out: &BakeOutput) -> Vec<(Vec3, [f32; 3])> { + let res = out.resolution; + let g = UvGBuffer::new(mesh, 0, res, res); + let bent = &out.maps[&BakedMap::BentNormal]; + (0..(res * res) as usize).filter_map(|i| g.surface(mesh, i).map(|s| (s.nrm, [bent[i * 3], bent[i * 3 + 1], bent[i * 3 + 2]]))).collect() +} + +fn near(value: [f32; 3], expected: [f32; 3]) -> bool { + value.iter().zip(expected).all(|(v, e)| (v - e).abs() < TOLERANCE) +} + +#[test] +fn every_face_of_an_open_cube_bakes_the_straight_up_tangent_value() { + let cube = primitives::cube(2.0); + let out = run(&cube, &settings(64, &[BakedMap::BentNormal])).unwrap(); + + let texels = bent_texels(&cube, &out); + assert!(texels.len() > 1000, "{} texels", texels.len()); + for (normal, value) in texels { + assert!(near(value, [0.5, 0.5, 1.0]), "nothing occludes the face facing {normal}, so it bakes (0.5, 0.5, 1.0): {value:?}"); + } +} + +#[test] +fn an_open_sphere_bakes_the_straight_up_tangent_value_all_around() { + let sphere = primitives::sphere(1.0, 64, 32); + let out = run(&sphere, &settings(128, &[BakedMap::BentNormal])).unwrap(); + + // The poles squeeze the UV rows, so their tangent frame is left out + let texels: Vec<_> = bent_texels(&sphere, &out).into_iter().filter(|(normal, _)| normal.y.abs() < 0.9).collect(); + assert!(texels.len() > 1000, "{} texels", texels.len()); + for (normal, value) in texels { + assert!(near(value, [0.5, 0.5, 1.0]), "the world normal {normal} turns with the surface, the tangent value does not: {value:?}"); + } +} + +#[test] +fn a_floor_under_a_sphere_tilts_its_bent_normals_up_the_texture() { + let mut mesh = primitives::sphere(1.0, 64, 32); + // The floor occludes but takes no texels (all its UVs on one point) + let floor = Mesh::from_parts( + vec![Vec3::new(-6.0, -1.0, -6.0), Vec3::new(6.0, -1.0, -6.0), Vec3::new(6.0, -1.0, 6.0), Vec3::new(-6.0, -1.0, 6.0)], + Some(vec![Vec3::Y; 4]), + vec![Vec2::ZERO; 4], + vec![0, 2, 1, 0, 3, 2], + "Floor", + ) + .unwrap(); + mesh.append(&floor); + let mut parameters = settings(128, &[BakedMap::BentNormal]); + parameters.maps.bent_normal.parameters.rays = RaySettings { rays: 128, max_distance: 4.0, relative_to_bounding_box: false, ..parameters.maps.bent_normal.parameters.rays }; + + let out = run(&mesh, ¶meters).unwrap(); + + // Around the equator the open sky is up: the sphere's V runs down, so OpenGL green (-V) points up. + // Sideways (red) nothing differs, and the bent normal stays close to the surface normal (blue). + let equator: Vec<_> = bent_texels(&mesh, &out).into_iter().filter(|(normal, _)| normal.y.abs() < 0.15).collect(); + assert!(equator.len() > 500, "{} texels", equator.len()); + for (normal, [red, _, blue]) in &equator { + assert!((red - 0.5).abs() < 2.0 * TOLERANCE, "no sideways lean where the normal is {normal}: red {red}"); + assert!(*blue > 0.85, "still mostly along the normal {normal}: blue {blue}"); + } + let green = mean(equator.iter().map(|(_, value)| value[1])); + assert!(green > 0.56, "the bent normals lean away from the floor, up the texture: mean green {green}"); +} diff --git a/crates/bake/src/tests.rs b/crates/bake/src/tests.rs index 0c313462..4ea28fac 100644 --- a/crates/bake/src/tests.rs +++ b/crates/bake/src/tests.rs @@ -14,6 +14,9 @@ use texturecraft_geom::primitives; use super::*; +#[path = "test/bent_normal_space.rs"] +mod bent_normal_space; + #[path = "test/cage_shell.rs"] mod cage_shell; @@ -693,7 +696,7 @@ fn bent_normals_point_away_from_occluders() { let mean_x = |v: &[&[f32]]| mean(v.iter().map(|b| b[0] * 2.0 - 1.0)); assert!(mean_x(&near) < -0.15, "near the wall the bent normal leans away from it: {}", mean_x(&near)); assert!(mean_x(&near) < mean_x(&far), "{} vs {}", mean_x(&near), mean_x(&far)); - assert!(near.iter().all(|b| b[1] > 0.6), "still points up"); + assert!(near.iter().all(|b| b[2] > 0.6), "still points up: along the surface normal, tangent-space z"); let ao_near = mean(ao.iter().filter(|(p, _)| p.x > 0.6).map(|v| v.1)); assert!(ao_near < mean(ao.iter().filter(|(p, _)| p.x < -0.6).map(|v| v.1))); } diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 4a17e89e..52e2bc8d 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -30,7 +30,7 @@ pub use layer::{ }; pub use project::{ AoMixing, BlockScales, Camera, DepthOfField, DisplaySettings, FORMAT_VERSION, MAX_RESOLUTION, MIN_RESOLUTION, NormalFormat, NormalMixing, Project, - TextureSet, TileGrid, ToneMapping, UDIM_RANGE, udim_at, + TANGENT_BENT_NORMAL_VERSION, TextureSet, TileGrid, ToneMapping, UDIM_RANGE, udim_at, }; #[derive(Debug, thiserror::Error, Clone, PartialEq)] diff --git a/crates/core/src/project.rs b/crates/core/src/project.rs index 3fe26e04..f669aa79 100644 --- a/crates/core/src/project.rs +++ b/crates/core/src/project.rs @@ -6,7 +6,11 @@ use serde::{Deserialize, Serialize}; use crate::{BakeSettings, BakedMap, Channel, ChannelFormat, CoreError, Effect, Layer, LayerId, LayerKind, Result}; -pub const FORMAT_VERSION: u32 = 2; +/// Project file format. 3: bent normal maps are in tangent space (older files hold world-space ones). +pub const FORMAT_VERSION: u32 = 3; + +/// First format whose baked bent normal maps are in tangent space +pub const TANGENT_BENT_NORMAL_VERSION: u32 = 3; /// Maximum group nesting (also bounds recursion over hostile project files) pub const MAX_DEPTH: usize = 16; diff --git a/crates/engine/src/meshmap_cmds.rs b/crates/engine/src/meshmap_cmds.rs index 4524b361..f08d34c4 100644 --- a/crates/engine/src/meshmap_cmds.rs +++ b/crates/engine/src/meshmap_cmds.rs @@ -20,7 +20,7 @@ pub fn specs() -> Vec { "Use image as mesh map", [], None, - "{ts?, map: mesh map id (normal, ambientOcclusion, curvature, id, height…), image: resource name} → {map, resolution} — resampled to the size of the set's other mesh maps (or its bake size); a normal map is read in the project's normal format. A UV-tiled set takes one image per tile, named alike with the tile number (`image: \"ao.1001.png\"` finds ao.1002.png…)", + "{ts?, map: mesh map id (normal, ambientOcclusion, curvature, id, height…), image: resource name} → {map, resolution} — resampled to the size of the set's other mesh maps (or its bake size); a normal or bent normal map is read in the project's normal format. A UV-tiled set takes one image per tile, named alike with the tile number (`image: \"ao.1001.png\"` finds ao.1002.png…)", always, set ), @@ -62,7 +62,8 @@ pub(crate) fn use_images(s: &mut Session, cmd: &str, ts: usize, map: BakedMap, i let (width, height) = image_map_size(s, cmd, ts)?; - let flip_green = map == BakedMap::Normal && s.doc.project.normal_format == NormalFormat::DirectX; + // Bent normals are tangent-space maps like the normal map, so both follow the project's normal format + let flip_green = matches!(map, BakedMap::Normal | BakedMap::BentNormal) && s.doc.project.normal_format == NormalFormat::DirectX; let data = match (grid, images.values().next()) { (_, None) => return Err(bad(cmd, "no image to use")), (None, Some(image)) => resample(map.components(), (width, height), flip_green, |u, v| image.sample(u, v)), @@ -134,7 +135,7 @@ fn udim_in(name: &str) -> Option { /// `w` × `h` texels of `comps` values each from `sample` (atlas UV → RGBA), as stored in the /// file (mesh maps hold data, not colors, so nothing is linearized); `flip_green` reads a -/// DirectX normal map. +/// DirectX normal or bent normal map. fn resample(comps: usize, (w, h): (u32, u32), flip_green: bool, sample: impl Fn(f32, f32) -> [f32; 4] + Sync) -> Vec { let mut out = vec![0.0; w as usize * h as usize * comps]; out.par_chunks_mut(w as usize * comps).enumerate().for_each(|(y, row)| { diff --git a/crates/engine/tests/it/painter_gaps.rs b/crates/engine/tests/it/painter_gaps.rs index f729476e..b1b7859d 100644 --- a/crates/engine/tests/it/painter_gaps.rs +++ b/crates/engine/tests/it/painter_gaps.rs @@ -536,6 +536,23 @@ fn images_fill_mesh_map_slots() { assert_eq!(back.doc.baked, s.doc.baked); } +#[test] +fn bent_normal_images_are_read_in_the_project_normal_format() { + use texturecraft_engine::core::BakedMap; + let mut s = session(); + let image = s.import_resource_bytes("bent.png", png(2, 2, |_, _| [128, 64, 255, 255])).unwrap(); + let sample = |s: &Session| s.baked(0).unwrap().sample(BakedMap::BentNormal, 50, 50, (100, 100)).unwrap(); + + execute(&mut s, "textureSet.meshMap.set", &json!({"map": "bentNormal", "image": image})).unwrap(); + assert!((sample(&s)[1] - 64.0 / 255.0).abs() < 1e-3, "OpenGL: green as stored {:?}", sample(&s)); + + // A tangent-space map like the normal map: DirectX green flipped + execute(&mut s, "textureSet.settings", &json!({"normalFormat": "directX"})).unwrap(); + execute(&mut s, "textureSet.meshMap.set", &json!({"map": "bentNormal", "image": image})).unwrap(); + let v = sample(&s); + assert!((v[0] - 128.0 / 255.0).abs() < 1e-3 && (v[1] - (1.0 - 64.0 / 255.0)).abs() < 1e-3 && (v[2] - 1.0).abs() < 1e-3, "{v:?}"); +} + #[test] fn fill_projection_edits_keep_what_they_do_not_change() { let mut s = session(); diff --git a/crates/formats/README.md b/crates/formats/README.md index 9c0580fe..b11c8087 100644 --- a/crates/formats/README.md +++ b/crates/formats/README.md @@ -152,6 +152,10 @@ tile-resolution overrides. Older projects load their stored bake pixels as-is, w the old layout; their appearance may change until rebaked. Painted layers and cage/skew correction maps retain their working atlas layout. +Project format version 3 stores baked bent normal maps in tangent space (OpenGL green, like the +normal map). Version 2 and older files held them in world space: loading drops those maps, keeps +the bake settings and warns that the Bent Normals map needs a re-bake. + Loading runs `Project::validate`, refuses a newer `format_version` (`FormatError::NewerVersion`), checks every tile against its texture set's resolution and component count, rejects repeated or out-of-range tiles and trailing data, and drops pixels of layers that are in no stack. Total diff --git a/crates/formats/src/project_file/mod.rs b/crates/formats/src/project_file/mod.rs index e1d98196..877a49fb 100644 --- a/crates/formats/src/project_file/mod.rs +++ b/crates/formats/src/project_file/mod.rs @@ -23,7 +23,7 @@ use std::io::{Cursor, Read, Seek, Write}; use std::sync::Arc; use texturecraft_compose::{BakedMaps, PixelStore}; -use texturecraft_core::{BakePaintTarget, Channel, FORMAT_VERSION, LayerId, Project}; +use texturecraft_core::{BakePaintTarget, BakedMap, Channel, FORMAT_VERSION, LayerId, Project, TANGENT_BENT_NORMAL_VERSION}; use texturecraft_texel::{TILE_AREA, TileImage}; use zip::write::SimpleFileOptions; use zip::{CompressionMethod, ZipArchive, ZipWriter}; @@ -466,6 +466,9 @@ fn load_with_budget(reader: R, mut budget: LoadBudget) -> Result // Ignore other entries from newer minor revisions } + if version < TANGENT_BENT_NORMAL_VERSION { + drop_world_space_bent_normals(&mut pf); + } pf.project.format_version = FORMAT_VERSION; if recovered { @@ -477,5 +480,27 @@ fn load_with_budget(reader: R, mut budget: LoadBudget) -> Result Ok(pf) } +/// Older files hold bent normal maps in world space, which every reader now takes as tangent +/// space: drop them (the bake settings stay) and ask for a re-bake. +fn drop_world_space_bent_normals(pf: &mut ProjectFile) { + let mut names = Vec::new(); + for (ts, maps) in &mut pf.baked { + if maps.maps.remove(&BakedMap::BentNormal).is_none() { + continue; + } + if let Some(set) = pf.project.texture_sets.get_mut(*ts) { + set.baked.remove(&BakedMap::BentNormal); + names.push(format!("\"{}\"", set.name)); + } + } + pf.baked.retain(|_, maps| !maps.maps.is_empty()); + if !names.is_empty() { + pf.warnings.push(format!( + "Warning: This project was saved with bent normal maps in world space. TextureCraft now bakes them in tangent space and removed the old ones from texture set {}. Bake the Bent Normals map again.", + names.join(", ") + )); + } +} + #[cfg(test)] mod tests; diff --git a/crates/formats/src/project_file/tests.rs b/crates/formats/src/project_file/tests.rs index 4fcc0aed..a9fefec5 100644 --- a/crates/formats/src/project_file/tests.rs +++ b/crates/formats/src/project_file/tests.rs @@ -111,6 +111,35 @@ fn legacy_bakes_load_as_is_without_layout_conversion() { assert_same(&loaded, &reopened); } +#[test] +fn world_space_bent_normals_of_older_projects_are_dropped_for_a_rebake() { + let mut sample = sample(); + let bent = Arc::new(vec![0.5, 1.0, 0.5].repeat(16 * 16)); + sample.pf.baked.get_mut(&0).unwrap().maps.insert(BakedMap::BentNormal, bent); + sample.pf.project.texture_sets[0].baked.insert(BakedMap::BentNormal); + + // A current project keeps its tangent-space bent normals + let current = load_project_bytes(&save_project_bytes(&sample.pf).unwrap()).unwrap(); + assert!(current.warnings.is_empty(), "{:?}", current.warnings); + assert_same(&sample.pf, ¤t); + + // One saved before tangent space held world vectors: they go, the other maps stay + sample.pf.project.format_version = TANGENT_BENT_NORMAL_VERSION - 1; + let older = load_project_bytes(&save_project_bytes(&sample.pf).unwrap()).unwrap(); + assert_eq!(older.warnings.len(), 1, "{:?}", older.warnings); + assert!(older.warnings[0].contains("\"A\"") && older.warnings[0].contains("Bent Normals"), "{}", older.warnings[0]); + let maps = &older.baked[&0].maps; + assert!(!maps.contains_key(&BakedMap::BentNormal)); + assert!(maps.contains_key(&BakedMap::AmbientOcclusion) && maps.contains_key(&BakedMap::Normal)); + assert!(!older.project.texture_sets[0].baked.contains(&BakedMap::BentNormal)); + assert_eq!(older.project.texture_sets[0].bake, sample.pf.project.texture_sets[0].bake, "the bake settings stay for the re-bake"); + + // Saved again, the project is current and opens without the warning + let reopened = load_project_bytes(&save_project_bytes(&older).unwrap()).unwrap(); + assert!(reopened.warnings.is_empty(), "{:?}", reopened.warnings); + assert_same(&older, &reopened); +} + #[cfg(not(target_arch = "wasm32"))] #[test] fn save_and_load_through_files() { diff --git a/crates/render/src/shaders/bake_view.wgsl b/crates/render/src/shaders/bake_view.wgsl index 0720e318..b878549d 100644 --- a/crates/render/src/shaders/bake_view.wgsl +++ b/crates/render/src/shaders/bake_view.wgsl @@ -124,8 +124,9 @@ fn fs_wire_cage_missing_seam(surface: BakeViewOut) -> @location(0) vec4 { return g.bake_missing_seam; } -// Shade the neutral material using the texture set's normal, occlusion and optional bent normal -fn neutral_color(n: vec3, n_geo: vec3, v: vec3, world_position: vec3, ao_channel: f32, packed_bent: vec3) -> vec4 { +// Shade the neutral material using the texture set's normal, occlusion and optional bent normal. +// `t`, `b` and `n_geo` are the tangent frame the normal map is read in; the bent normal is stored in it too. +fn neutral_color(n: vec3, n_geo: vec3, t: vec3, b: vec3, v: vec3, world_position: vec3, ao_channel: f32, packed_bent: vec3) -> vec4 { let albedo = g.bake_neutral_color.rgb; let rough = clamp(g.bake_neutral.x, MIN_NEUTRAL_ROUGHNESS, 1.0); let metal = g.bake_neutral.y; @@ -135,8 +136,9 @@ fn neutral_color(n: vec3, n_geo: vec3, v: vec3, world_position: v let nov = clamp(abs(dot(n, v)), MIN_COSINE, 1.0); // An unbaked bent normal reads as the neutral gray (length 0): the surface normal stands in - var bent = packed_bent * 2.0 - 1.0; - bent = select(n, normalize(bent), dot(bent, bent) > BENT_NORMAL_MIN_LENGTH * BENT_NORMAL_MIN_LENGTH); + let bent_tangent = packed_bent * 2.0 - 1.0; + let bent_world = t * bent_tangent.x + b * bent_tangent.y + n_geo * bent_tangent.z; + let bent = select(n, normalize(bent_world), dot(bent_tangent, bent_tangent) > BENT_NORMAL_MIN_LENGTH * BENT_NORMAL_MIN_LENGTH); let diffuse = sh_irradiance(normalize(mix(n, bent, g.bake_bent_normal.x)), vec3(0.0)) * diffuse_col * ao; let r = reflect(-v, n); let horizon = clamp(1.0 + HORIZON_FADE * dot(r, n_geo), 0.0, 1.0); diff --git a/crates/render/src/shaders/mesh.wgsl b/crates/render/src/shaders/mesh.wgsl index aac1d147..2687745b 100644 --- a/crates/render/src/shaders/mesh.wgsl +++ b/crates/render/src/shaders/mesh.wgsl @@ -457,7 +457,7 @@ fn shade_mesh(i: VOut, front: bool) -> Shaded { } if (g.bake_neutral.w > 0.5) { - return Shaded(neutral_color(n, n_geo, v, i.wpos, orm.r, aux.rgb), vec3(0.0)); + return Shaded(neutral_color(n, n_geo, t, b, v, i.wpos, orm.r, aux.rgb), vec3(0.0)); } // Change the alpha dither each frame so accumulation smooths it diff --git a/crates/render/tests/headless.rs b/crates/render/tests/headless.rs index f6c25615..51398abe 100644 --- a/crates/render/tests/headless.rs +++ b/crates/render/tests/headless.rs @@ -263,6 +263,33 @@ fn bake_neutral_material_shades_the_low_poly_in_its_settings() { ); } +#[test] +fn bake_neutral_material_reads_the_bent_normal_in_the_tangent_frame() { + let Some((mut h, frame)) = setup() else { return }; + let upload_bent = |h: &mut Headless, texel: [u8; 4]| { + let size = texturecraft_render::aux_size((256, 256)); + let rgba = texel.repeat((size * size) as usize); + for ts in 0..2 { + h.renderer.upload_aux(&h.device, &h.queue, ts, size, &rgba); + } + }; + + // Only the diffuse turn: reflections dim near grazing angles even along the normal + let neutral = NeutralMaterial { bent_normal_diffuse: 1.0, bent_normal_specular: 0.0, ..NeutralMaterial::default() }; + let off = BakeVisualization { low_poly: shown(BakeVisualization::default().low_poly), neutral: Some(neutral), ..BakeVisualization::default() }; + let on = BakeVisualization { neutral: Some(NeutralMaterial { bent_normal: true, ..neutral }), ..off }; + let plain = render(&mut h, &shaded(&frame, |s| s.bake = Some(off)), "bake_bent_off"); + + // (0.5, 0.5, 1.0) is the surface normal on every face of the ball, not one world direction + upload_bent(&mut h, [128, 128, 255, 255]); + let straight_up = render(&mut h, &shaded(&frame, |s| s.bake = Some(on)), "bake_bent_straight_up"); + assert_same(&straight_up, &plain, "a bent normal along the surface normal lights like no bent normal"); + + upload_bent(&mut h, [255, 128, 128, 255]); + let along_tangent = render(&mut h, &shaded(&frame, |s| s.bake = Some(on)), "bake_bent_along_tangent"); + assert_ne!(along_tangent.px, plain.px, "a tilted bent normal turns the light"); +} + #[test] fn bake_neutral_material_looks_the_same_moving_as_still() { // Full quality keeps the first frame near settled; one sample missed by 6.6 levels diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 0ead3445..eda79bd9 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -342,9 +342,10 @@ Re-cooks use `materialcraft-cli batch` to open the saved graph by name and run ` (the app is `$MATERIALCRAFT` / `materialcraft`). Baked maps feed graph input nodes whose identifier matches the map's shared id (`ambientOcclusion`, `curvature`, `thickness`, `position`, `worldNormal`, `normal`, `height`, `id`, `bentNormal`, `opacity`, `colorMap`, `worldDirection`; -case and underscores ignored). Usages map to channels of the same id (`baseColor`, `normal` in -OpenGL convention, `roughness`, `metallic`, `height`, `ambientOcclusion`, `emissive`, `opacity`, -`specular`); missing channels join the texture set. +case and underscores ignored; `normal` and `bentNormal` are tangent space, OpenGL green). Usages +map to channels of the same id (`baseColor`, `normal` in OpenGL convention, `roughness`, `metallic`, +`height`, `ambientOcclusion`, `emissive`, `opacity`, `specular`); missing channels join the texture +set. ## CLI From 4c9fe1e13e05535e65f4c089e3513211469195c8 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 16 Nov 2026 19:54:27 +0100 Subject: [PATCH 06/14] Decode the bent normal like the normal map and name both ways back in the load warning --- crates/formats/src/project_file/mod.rs | 2 +- crates/render/src/shaders/bake_view.wgsl | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/formats/src/project_file/mod.rs b/crates/formats/src/project_file/mod.rs index 877a49fb..58c7711c 100644 --- a/crates/formats/src/project_file/mod.rs +++ b/crates/formats/src/project_file/mod.rs @@ -496,7 +496,7 @@ fn drop_world_space_bent_normals(pf: &mut ProjectFile) { pf.baked.retain(|_, maps| !maps.maps.is_empty()); if !names.is_empty() { pf.warnings.push(format!( - "Warning: This project was saved with bent normal maps in world space. TextureCraft now bakes them in tangent space and removed the old ones from texture set {}. Bake the Bent Normals map again.", + "Warning: This project was saved with bent normal maps in world space. TextureCraft now bakes them in tangent space and removed the old ones from texture set {}. Bake the Bent Normals map again, or set its image again if it came from a file.", names.join(", ") )); } diff --git a/crates/render/src/shaders/bake_view.wgsl b/crates/render/src/shaders/bake_view.wgsl index b878549d..846f1d31 100644 --- a/crates/render/src/shaders/bake_view.wgsl +++ b/crates/render/src/shaders/bake_view.wgsl @@ -136,7 +136,8 @@ fn neutral_color(n: vec3, n_geo: vec3, t: vec3, b: vec3, v: let nov = clamp(abs(dot(n, v)), MIN_COSINE, 1.0); // An unbaked bent normal reads as the neutral gray (length 0): the surface normal stands in - let bent_tangent = packed_bent * 2.0 - 1.0; + // Decoded like the normal map (texel 128 is exactly 0) + let bent_tangent = snorm8(packed_bent); let bent_world = t * bent_tangent.x + b * bent_tangent.y + n_geo * bent_tangent.z; let bent = select(n, normalize(bent_world), dot(bent_tangent, bent_tangent) > BENT_NORMAL_MIN_LENGTH * BENT_NORMAL_MIN_LENGTH); let diffuse = sh_irradiance(normalize(mix(n, bent, g.bake_bent_normal.x)), vec3(0.0)) * diffuse_col * ao; From b587b94d6711ee6c55f23a702671c6fa24d4aa62 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 16 Nov 2026 20:02:06 +0100 Subject: [PATCH 07/14] Format the bent normal tests and fix a clippy warning --- crates/bake/src/test/bent_normal_space.rs | 3 ++- crates/formats/src/project_file/tests.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/bake/src/test/bent_normal_space.rs b/crates/bake/src/test/bent_normal_space.rs index 6afa0745..1d9eb87f 100644 --- a/crates/bake/src/test/bent_normal_space.rs +++ b/crates/bake/src/test/bent_normal_space.rs @@ -57,7 +57,8 @@ fn a_floor_under_a_sphere_tilts_its_bent_normals_up_the_texture() { .unwrap(); mesh.append(&floor); let mut parameters = settings(128, &[BakedMap::BentNormal]); - parameters.maps.bent_normal.parameters.rays = RaySettings { rays: 128, max_distance: 4.0, relative_to_bounding_box: false, ..parameters.maps.bent_normal.parameters.rays }; + parameters.maps.bent_normal.parameters.rays = + RaySettings { rays: 128, max_distance: 4.0, relative_to_bounding_box: false, ..parameters.maps.bent_normal.parameters.rays }; let out = run(&mesh, ¶meters).unwrap(); diff --git a/crates/formats/src/project_file/tests.rs b/crates/formats/src/project_file/tests.rs index a9fefec5..684f89d5 100644 --- a/crates/formats/src/project_file/tests.rs +++ b/crates/formats/src/project_file/tests.rs @@ -114,7 +114,7 @@ fn legacy_bakes_load_as_is_without_layout_conversion() { #[test] fn world_space_bent_normals_of_older_projects_are_dropped_for_a_rebake() { let mut sample = sample(); - let bent = Arc::new(vec![0.5, 1.0, 0.5].repeat(16 * 16)); + let bent = Arc::new([0.5, 1.0, 0.5].repeat(16 * 16)); sample.pf.baked.get_mut(&0).unwrap().maps.insert(BakedMap::BentNormal, bent); sample.pf.project.texture_sets[0].baked.insert(BakedMap::BentNormal); From 47b7ffb7cb2dc68831c8913d712bf2821c3f29c2 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 16 Nov 2026 21:36:14 +0100 Subject: [PATCH 08/14] Bake: a uniformly curved surface reads the same in mesh curvature everywhere The smooth-shading ring of the mesh curvature baker was sized in texels of the triangle under the sample, so it reached further on the mesh wherever the UV layout gave larger texels. A sphere, whose curvature is the same everywhere, read brighter at the equator than near the poles and brighter again on a smaller UV island; on the sample sphere the reading followed the texel size with a correlation of 0.99, while the reference baker reads it as one value. Small, densely unwrapped parts such as the crate handle read almost flat. The ring now reaches four sampling radii on the mesh. It follows the sampling radius setting, no longer the UV layout or the map size. Four radii match the reference baker on the cylinder side, the crate body bevels and the material ball within 2/255. The wall rings stay sized in texels, so hard edge and contact lines keep their width. The crate check pins the new 1024 reading, 0.521 (was 0.516). --- crates/bake/src/map/curvature.rs | 11 ++++++----- crates/bake/src/tests.rs | 31 +++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/crates/bake/src/map/curvature.rs b/crates/bake/src/map/curvature.rs index ac6237e6..fad35403 100644 --- a/crates/bake/src/map/curvature.rs +++ b/crates/bake/src/map/curvature.rs @@ -7,8 +7,8 @@ use texturecraft_geom::{Hit, Mesh, Surface}; const RING_COUNT: usize = 4; const WALL_RADIUS_TEXELS: f32 = 1.5; -/// Map size per UV tile whose texels size the smooth-shading ring: at any other size the ring reaches as far on the mesh, so a rounded edge reads alike at every resolution -const SPREAD_REFERENCE_RESOLUTION: f32 = 1024.0; +/// How far the smooth-shading ring reaches, in sampling radii: a distance on the mesh, so a rounded surface reads alike at every map size and wherever its UV texels are larger or smaller +const SPREAD_RADII: f32 = 4.0; const CONCAVE_WEIGHT: f32 = 1.5; const LOW_POLY_CONVEX_WEIGHT: f32 = 1.5; const HIGH_POLY_CONVEX_WEIGHT: f32 = 0.5; @@ -22,6 +22,7 @@ const GENTLE_CURVATURE_THRESHOLD: f32 = 0.1; pub(super) struct Curvature { pub(super) is_enabled: bool, walls: f32, + /// Reach of the smooth-shading ring, in mesh units spread: f32, fallback: f32, texels: Vec, @@ -66,8 +67,8 @@ impl Curvature { rays_per_sample, wall_count: ring.first().map_or(0, Vec::len), walls, - // Texels of a 1024 map (a factor of exactly 1 at 1024): a texel-sized ring halved a smooth bevel's response with each doubling of the map size - spread: walls * 4.0 / 3.0 * ((width as f32 * height as f32).sqrt() / SPREAD_REFERENCE_RESOLUTION), + // In mesh units: a ring sized in texels followed the UV layout, so a sphere read brighter where its texels were larger + spread: radius * SPREAD_RADII, fallback, texels: (0..triangle_count).into_par_iter().map(|t| texel(t).filter(|v| v.is_finite()).unwrap_or(fallback)).collect(), low: CurvatureTuning { @@ -90,7 +91,7 @@ impl Curvature { fn radii(&self, tuning: &CurvatureTuning, texel: f32) -> [f32; RING_COUNT] { let r = self.walls * texel; - [r, r, self.spread * texel, tuning.true_radius] + [r, r, self.spread, tuning.true_radius] } fn directions(&self, ring: usize, tangent: Vec3, bitangent: Vec3) -> impl Iterator + '_ { diff --git a/crates/bake/src/tests.rs b/crates/bake/src/tests.rs index 59288630..0c9d6c39 100644 --- a/crates/bake/src/tests.rs +++ b/crates/bake/src/tests.rs @@ -265,11 +265,38 @@ fn crate_body_bevel(resolution: u32) -> f32 { #[test] fn the_sample_crate_bevels_read_the_same_at_512_1024_and_2048() { let (coarse, reference, fine) = (crate_body_bevel(512), crate_body_bevel(1024), crate_body_bevel(2048)); - // The 1024 bake is unchanged (the smooth-shading ring is sized in texels of a 1024 map); a texel-sized ring read 0.531 at 512 and 0.508 at 2048 - assert!((0.5135..0.5175).contains(&reference), "1024: {reference}"); + // The smooth-shading ring is sized on the mesh (0.521 at 1024); a texel-sized ring read 0.531 at 512, 0.516 at 1024 and 0.508 at 2048 + assert!((0.5196..0.5234).contains(&reference), "1024: {reference}"); assert!((coarse - reference).abs() < 0.002 && (fine - reference).abs() < 0.002, "512: {coarse}, 1024: {reference}, 2048: {fine}"); } +/// Median curvature of the sphere texels whose height `|y|` lies in `band` +fn sphere_band(curvature: &[(Vec3, f32)], band: std::ops::Range) -> f32 { + median(curvature.iter().filter(|(p, _)| band.contains(&p.y.abs())).map(|(_, value)| *value).collect()) +} + +#[test] +fn a_sphere_reads_the_same_wherever_its_uv_texels_are_larger_or_smaller() { + let full = primitives::sphere(1.0, 64, 32); + let mut half = full.clone(); + half.uvs.iter_mut().for_each(|uv| *uv *= 0.5); + let mut parameters = settings(128, &[BakedMap::Curvature]); + // A narrow manual range, so the sphere's gentle curvature reads well above mid-grey + parameters.maps.curvature.parameters.mesh.value_min = -0.05; + parameters.maps.curvature.parameters.mesh.value_max = 0.05; + let bake = |m: &Mesh| covered_values(m, &run(m, ¶meters).unwrap(), BakedMap::Curvature); + let (full, half) = (bake(&full), bake(&half)); + let (equator, pole) = (sphere_band(&full, 0.0..0.3), sphere_band(&full, 0.8..0.95)); + let (half_equator, half_pole) = (sphere_band(&half, 0.0..0.3), sphere_band(&half, 0.8..0.95)); + assert!(equator > 0.55, "equator {equator}"); + // A ring sized in texels read the poles, where the texels are a third smaller, darker, and the half-size island brighter + assert!((pole - equator).abs() < 0.01, "equator {equator}, near the poles {pole}"); + assert!( + (half_equator - equator).abs() < 0.01 && (half_pole - pole).abs() < 0.01, + "full-size island {equator} and {pole}, half-size island {half_equator} and {half_pole}" + ); +} + fn median(mut values: Vec) -> f32 { values.sort_by(f32::total_cmp); values.get(values.len() / 2).copied().unwrap_or(0.5) From 55169f4bd94ff78c1ac5c153d1a0694f2e51bcd8 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 16 Nov 2026 21:51:53 +0100 Subject: [PATCH 09/14] Write DirectX green for baked normal and bent normal maps in mesh map exports; clear stale bent normal declarations of older projects in every set --- crates/formats/src/export/mod.rs | 10 +++++++- crates/formats/src/export/tests.rs | 30 ++++++++++++++++++++++++ crates/formats/src/project_file/mod.rs | 12 +++++----- crates/formats/src/project_file/tests.rs | 12 ++++++++++ 4 files changed, 57 insertions(+), 7 deletions(-) diff --git a/crates/formats/src/export/mod.rs b/crates/formats/src/export/mod.rs index 24fc9833..811a60bc 100644 --- a/crates/formats/src/export/mod.rs +++ b/crates/formats/src/export/mod.rs @@ -675,7 +675,15 @@ impl Sampler<'_> { [if self.set.ao_mixing == AoMixing::Multiply { ch * baked } else { ch }; 3] } }, - SourceRef::Baked { map, .. } => self.baked(*map, at), + SourceRef::Baked { map, .. } => { + let v = self.baked(*map, at); + // Tangent-space maps are stored with OpenGL green, like the normal map: write the format of the export + if matches!(map, BakedMap::Normal | BakedMap::BentNormal) && self.normal_override.unwrap_or(self.project_normal) == NormalFormat::DirectX { + [v[0], 1.0 - v[1], v[2]] + } else { + v + } + } }; v.get(k).copied().unwrap_or(0.0) } diff --git a/crates/formats/src/export/tests.rs b/crates/formats/src/export/tests.rs index 8bf6ad66..bf8c5a44 100644 --- a/crates/formats/src/export/tests.rs +++ b/crates/formats/src/export/tests.rs @@ -204,6 +204,36 @@ fn ao_mixes_baked_and_channel() { assert_eq!(ao.color(), image::ColorType::L16); } +#[test] +fn mesh_maps_write_tangent_normals_with_the_green_of_the_project_format() { + let green = |fx: &Fx, opts: &ExportOptions, name: &str| { + let files = render_maps(&fx.input(), opts).unwrap(); + decode(find(&files, name)).to_rgb16().get_pixel(3, 3)[1] as i32 + }; + let mut fx = Fx::new(&default_channels()); + let stored = |v: f32| std::sync::Arc::new([0.5, v, 0.5].repeat((RES * RES) as usize)); + fx.baked = BakedMaps { resolution: RES, maps: [(BakedMap::BentNormal, stored(0.25)), (BakedMap::Normal, stored(0.25))].into() }; + fx.project.texture_sets[0].baked.extend([BakedMap::BentNormal, BakedMap::Normal]); + let opts = ExportOptions::new(builtin_preset("Mesh maps").unwrap()); + let quarter = (0.25f32 * 65535.0).round() as i32; + let three_quarters = (0.75f32 * 65535.0).round() as i32; + for name in ["_bentNormal.png", "_normal.png"] { + assert!((green(&fx, &opts, name) - quarter).abs() <= 1, "{name}: OpenGL project keeps the stored green"); + } + fx.project.normal_format = NormalFormat::DirectX; + for name in ["_bentNormal.png", "_normal.png"] { + assert!((green(&fx, &opts, name) - three_quarters).abs() <= 1, "{name}: DirectX project flips green"); + } + // The export's own format wins over the project's + let mut gl = opts.clone(); + gl.normal_format = Some(NormalFormat::OpenGl); + assert!((green(&fx, &gl, "_bentNormal.png") - quarter).abs() <= 1); + fx.project.normal_format = NormalFormat::OpenGl; + let mut dx = opts.clone(); + dx.normal_format = Some(NormalFormat::DirectX); + assert!((green(&fx, &dx, "_bentNormal.png") - three_quarters).abs() <= 1, "a DirectX export of an OpenGL project flips green"); +} + #[test] fn templates_are_sanitized_and_collisions_suffixed() { let fx = Fx::new(&default_channels()); diff --git a/crates/formats/src/project_file/mod.rs b/crates/formats/src/project_file/mod.rs index 58c7711c..40065eef 100644 --- a/crates/formats/src/project_file/mod.rs +++ b/crates/formats/src/project_file/mod.rs @@ -483,13 +483,13 @@ fn load_with_budget(reader: R, mut budget: LoadBudget) -> Result /// Older files hold bent normal maps in world space, which every reader now takes as tangent /// space: drop them (the bake settings stay) and ask for a re-bake. fn drop_world_space_bent_normals(pf: &mut ProjectFile) { + for maps in pf.baked.values_mut() { + maps.maps.remove(&BakedMap::BentNormal); + } + // The declaration goes for every set, with or without pixels, so a set that only declared it is told too let mut names = Vec::new(); - for (ts, maps) in &mut pf.baked { - if maps.maps.remove(&BakedMap::BentNormal).is_none() { - continue; - } - if let Some(set) = pf.project.texture_sets.get_mut(*ts) { - set.baked.remove(&BakedMap::BentNormal); + for set in &mut pf.project.texture_sets { + if set.baked.remove(&BakedMap::BentNormal) { names.push(format!("\"{}\"", set.name)); } } diff --git a/crates/formats/src/project_file/tests.rs b/crates/formats/src/project_file/tests.rs index 684f89d5..9fae7a72 100644 --- a/crates/formats/src/project_file/tests.rs +++ b/crates/formats/src/project_file/tests.rs @@ -111,6 +111,18 @@ fn legacy_bakes_load_as_is_without_layout_conversion() { assert_same(&loaded, &reopened); } +#[test] +fn a_bent_normal_declared_without_pixels_in_an_older_project_is_dropped_and_reported() { + let mut sample = sample(); + sample.pf.project.texture_sets[0].baked.insert(BakedMap::BentNormal); + assert!(!sample.pf.baked[&0].maps.contains_key(&BakedMap::BentNormal)); + sample.pf.project.format_version = TANGENT_BENT_NORMAL_VERSION - 1; + let older = load_project_bytes(&save_project_bytes(&sample.pf).unwrap()).unwrap(); + assert!(!older.project.texture_sets[0].baked.contains(&BakedMap::BentNormal)); + assert_eq!(older.warnings.len(), 1, "{:?}", older.warnings); + assert!(older.warnings[0].contains("\"A\""), "{}", older.warnings[0]); +} + #[test] fn world_space_bent_normals_of_older_projects_are_dropped_for_a_rebake() { let mut sample = sample(); From 1122e4ef434e1c4ebe746ae3ca4d9325efb1459f Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 16 Nov 2026 22:49:21 +0100 Subject: [PATCH 10/14] Re-run CI after a Windows runner crash in an untouched crate From 0ca460078fa34d595685cba03337ac58ee51fe9e Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 16 Nov 2026 23:19:06 +0100 Subject: [PATCH 11/14] Name texture sets whose old bent normal pixels are dropped even when the declaration is missing --- crates/formats/src/project_file/mod.rs | 10 ++++------ crates/formats/src/project_file/tests.rs | 13 +++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/crates/formats/src/project_file/mod.rs b/crates/formats/src/project_file/mod.rs index 40065eef..fd13bec2 100644 --- a/crates/formats/src/project_file/mod.rs +++ b/crates/formats/src/project_file/mod.rs @@ -483,13 +483,11 @@ fn load_with_budget(reader: R, mut budget: LoadBudget) -> Result /// Older files hold bent normal maps in world space, which every reader now takes as tangent /// space: drop them (the bake settings stay) and ask for a re-bake. fn drop_world_space_bent_normals(pf: &mut ProjectFile) { - for maps in pf.baked.values_mut() { - maps.maps.remove(&BakedMap::BentNormal); - } - // The declaration goes for every set, with or without pixels, so a set that only declared it is told too + // Pixels and declaration go for every set, either one alone is enough to name the set in the warning let mut names = Vec::new(); - for set in &mut pf.project.texture_sets { - if set.baked.remove(&BakedMap::BentNormal) { + for (i, set) in pf.project.texture_sets.iter_mut().enumerate() { + let had_pixels = pf.baked.get_mut(&i).is_some_and(|maps| maps.maps.remove(&BakedMap::BentNormal).is_some()); + if set.baked.remove(&BakedMap::BentNormal) || had_pixels { names.push(format!("\"{}\"", set.name)); } } diff --git a/crates/formats/src/project_file/tests.rs b/crates/formats/src/project_file/tests.rs index 9fae7a72..4364f39e 100644 --- a/crates/formats/src/project_file/tests.rs +++ b/crates/formats/src/project_file/tests.rs @@ -123,6 +123,19 @@ fn a_bent_normal_declared_without_pixels_in_an_older_project_is_dropped_and_repo assert!(older.warnings[0].contains("\"A\""), "{}", older.warnings[0]); } +#[test] +fn a_bent_normal_with_pixels_but_no_declaration_in_an_older_project_is_dropped_and_reported() { + let mut sample = sample(); + let bent = Arc::new([0.5, 1.0, 0.5].repeat(16 * 16)); + sample.pf.baked.get_mut(&0).unwrap().maps.insert(BakedMap::BentNormal, bent); + assert!(!sample.pf.project.texture_sets[0].baked.contains(&BakedMap::BentNormal)); + sample.pf.project.format_version = TANGENT_BENT_NORMAL_VERSION - 1; + let older = load_project_bytes(&save_project_bytes(&sample.pf).unwrap()).unwrap(); + assert!(!older.baked[&0].maps.contains_key(&BakedMap::BentNormal)); + assert_eq!(older.warnings.len(), 1, "{:?}", older.warnings); + assert!(older.warnings[0].contains("\"A\""), "{}", older.warnings[0]); +} + #[test] fn world_space_bent_normals_of_older_projects_are_dropped_for_a_rebake() { let mut sample = sample(); From b255266e22315ab22018b9bff7b7e23307313b43 Mon Sep 17 00:00:00 2001 From: chasse20 Date: Tue, 17 Nov 2026 07:04:08 +0000 Subject: [PATCH 12/14] Auto: Catch thin occluders in grazing paint visibility Check unshifted depth before accepting a grazing allowance and start exact rays just off the surface. Keep the wider depth tolerance in the uncertain gate. Add regressions for both reported cases. --- crates/paint/src/raster.rs | 71 +++++++++++++++++++++++++--------- crates/paint/src/visibility.rs | 18 ++++++++- 2 files changed, 70 insertions(+), 19 deletions(-) diff --git a/crates/paint/src/raster.rs b/crates/paint/src/raster.rs index c86d88ed..2068efcb 100644 --- a/crates/paint/src/raster.rs +++ b/crates/paint/src/raster.rs @@ -110,24 +110,33 @@ fn grazing_allowance(depth: &EyeDepth, point: Vec3, facing: f32, pixels_per_unit const RAY_START_PER_BIAS: f32 = 0.0025; /// Whether a ray from `point` (on a surface facing `facing` toward the eye along `to_eye`) reaches -/// the eye without hitting the mesh, ignoring the first `skip` world units. The depth buffer is -/// asked first; this exact test only checks the texels it calls hidden but can't rule out -/// ([`EyeDepth::uncertain`]): its pixels are coarse, so on a surface turned far from the camera it -/// hides texels the camera does see, and strokes painted at a steep angle came out in stripes and -/// jagged gaps. Orthographic rays travel parallel to the view direction up to the eye plane. -fn ray_reaches_eye(context: &RasterCtx, depth: &EyeDepth, point: Vec3, normal: Vec3, facing: f32, to_eye: Vec3, skip: f32) -> bool { +/// the eye without hitting the mesh. The coarse depth buffer is checked first; this ray resolves +/// uncertain texels and those made apparently visible only by the grazing allowance. Orthographic +/// rays travel parallel to the view direction up to the eye plane. +fn ray_reaches_eye(context: &RasterCtx, depth: &EyeDepth, point: Vec3, normal: Vec3, facing: f32, to_eye: Vec3) -> bool { let direction = depth.orthographic_to_eye.unwrap_or(to_eye); let eye_distance = (context.eye - point).dot(direction); let lift = depth.bias * RAY_START_PER_BIAS; - let start = skip.max(0.0) + lift; - if !eye_distance.is_finite() || start >= eye_distance { + if !eye_distance.is_finite() || lift >= eye_distance { return true; } - // Lift off the surface on the eye's side, so back faces (culling off) don't hit themselves + // Lift off the surface on the eye's side to avoid self-hits without skipping nearby occluders let side = if facing < 0.0 { -normal } else { normal }; - let origin = point + side * lift + direction * start; - !context.bvh.occluded(origin, direction, eye_distance - start) + let origin = point + side * lift + direction * lift; + !context.bvh.occluded(origin, direction, eye_distance - lift) +} + +/// Resolve coarse depth uncertainty without letting a grazing allowance hide nearby geometry +fn surface_visible(context: &RasterCtx, depth: &EyeDepth, point: Vec3, normal: Vec3, facing: f32, to_eye: Vec3, skip: f32) -> bool { + let tested = point + to_eye * skip; + let (visible, uncertain) = depth.visibility(tested); + if visible { + // A shifted query can pass a sheet inside the allowance, so confirm at the surface + return (skip <= 0.0 || depth.visible(point)) || ray_reaches_eye(context, depth, point, normal, facing, to_eye); + } + + uncertain && ray_reaches_eye(context, depth, point, normal, facing, to_eye) } /// What the rasterizer reads. @@ -278,12 +287,7 @@ fn scan_triangle( if let Some(depth) = context.depth { let skip = grazing_allowance(depth, seen, facing, pixels_per_unit) + off_surface; - let tested = seen + to_eye * skip; - - // The exact test runs only where the buffer calls a texel hidden but can't be sure - let (visible, uncertain) = depth.visibility(tested); - let hidden = !visible && (!uncertain || !ray_reaches_eye(context, depth, seen, seen_normal, facing, to_eye, skip)); - if hidden { + if !surface_visible(context, depth, seen, seen_normal, facing, to_eye, skip) { continue; } } @@ -327,6 +331,37 @@ mod tests { use super::*; use texturecraft_geom::primitives; + #[test] + fn grazing_ray_detects_a_sheet_inside_the_depth_allowance() { + let positions = vec![Vec3::new(-0.2, -0.2, 0.005), Vec3::new(0.2, -0.2, 0.005), Vec3::new(0.0, 0.2, 0.005)]; + let mesh = Mesh::from_parts(positions, None, vec![texturecraft_geom::glam::Vec2::ZERO; 3], vec![0, 1, 2], "thin sheet").unwrap(); + let camera = looking_down(3.0); + let mesh_bvh = Bvh::new(&mesh); + let uv_buffer = UvGBuffer::new(&mesh, 0, 4, 4); + let depth = EyeDepth::render(&mesh, &camera, 480, 270); + let context = RasterCtx { + mesh: &mesh, + bvh: &mesh_bvh, + gbuf: &uv_buffer, + material: 0, + depth: Some(&depth), + eye: camera.eye(), + backface_culling: true, + angle_fade: (89.0, 90.0), + shape: Shape { alpha: crate::BrushAlpha::Round, image: None, library: None, level: 0 }, + }; + let point = Vec3::ZERO; + let to_eye = (camera.eye() - point).normalize(); + let pixels_per_unit = depth.height as f32 * depth.view_proj.row(1).truncate().length(); + let facing = Vec3::Y.dot(to_eye); + let skip = grazing_allowance(&depth, point, facing, pixels_per_unit); + + assert!(skip > 0.005 / to_eye.z, "the sheet lies inside the allowance band"); + assert_eq!(depth.visibility(point), (false, true)); + assert!(depth.visible(point + to_eye * skip), "the allowance alone marks the hidden point visible"); + assert!(!surface_visible(&context, &depth, point, Vec3::Y, facing, to_eye, skip)); + } + #[test] fn orthographic_occlusion_ray_stays_above_an_off_axis_point() { let positions = vec![Vec3::new(0.9, -0.2, 1.0), Vec3::new(1.1, -0.2, 1.0), Vec3::new(1.0, 0.2, 1.0)]; @@ -349,7 +384,7 @@ mod tests { let point = Vec3::new(1.0, 0.0, 0.0); let to_eye = (camera.eye() - point).normalize(); - assert!(!ray_reaches_eye(&context, &depth, point, Vec3::Z, 1.0, to_eye, 0.0)); + assert!(!ray_reaches_eye(&context, &depth, point, Vec3::Z, 1.0, to_eye)); } #[test] diff --git a/crates/paint/src/visibility.rs b/crates/paint/src/visibility.rs index c8a2898e..40fd8db1 100644 --- a/crates/paint/src/visibility.rs +++ b/crates/paint/src/visibility.rs @@ -198,7 +198,7 @@ impl EyeDepth { let Some((near, far, has_empty)) = self.neighbourhood(point) else { return (false, false) }; let distance = point.distance(self.eye); - let visible = distance <= near + self.tolerance(near); + let visible = distance <= near + self.bias; let uncertain = has_empty || (far < f32::MAX && distance <= far + self.tolerance(far)); (visible, uncertain) @@ -253,6 +253,22 @@ mod tests { use super::*; use texturecraft_geom::primitives; + #[test] + fn foreground_depth_outside_bias_needs_an_exact_test() { + let depth = EyeDepth { + width: 3, + height: 3, + view_proj: Mat4::IDENTITY, + eye: Vec3::new(0.0, 0.0, 10.0), + orthographic_to_eye: None, + dist: vec![9.985; 9], + triangle: Vec::new(), + bias: 0.005, + }; + + assert_eq!(depth.visibility(Vec3::ZERO), (false, true)); + } + #[test] fn front_of_sphere_is_visible_back_is_not() { let mesh = primitives::sphere(1.0, 64, 32); From 5133cc5216bf825984128126e2b1dd0a0ffeb115 Mon Sep 17 00:00:00 2001 From: chasse20 Date: Tue, 17 Nov 2026 07:49:45 +0000 Subject: [PATCH 13/14] Auto: Preserve mirrored and tilted-normal paint visibility Choose ray offsets from mapped triangle normals and separate mirror displacement from grazing tolerance. Add regressions for both dropped-texel cases. --- crates/paint/src/raster.rs | 95 +++++++++++++++++++++++++++++++++----- 1 file changed, 83 insertions(+), 12 deletions(-) diff --git a/crates/paint/src/raster.rs b/crates/paint/src/raster.rs index 2068efcb..48635823 100644 --- a/crates/paint/src/raster.rs +++ b/crates/paint/src/raster.rs @@ -109,11 +109,11 @@ fn grazing_allowance(depth: &EyeDepth, point: Vec3, facing: f32, pixels_per_unit /// 0.001 % of the radius) const RAY_START_PER_BIAS: f32 = 0.0025; -/// Whether a ray from `point` (on a surface facing `facing` toward the eye along `to_eye`) reaches +/// Whether a ray from `point` toward the eye along `to_eye` reaches /// the eye without hitting the mesh. The coarse depth buffer is checked first; this ray resolves /// uncertain texels and those made apparently visible only by the grazing allowance. Orthographic /// rays travel parallel to the view direction up to the eye plane. -fn ray_reaches_eye(context: &RasterCtx, depth: &EyeDepth, point: Vec3, normal: Vec3, facing: f32, to_eye: Vec3) -> bool { +fn ray_reaches_eye(context: &RasterCtx, depth: &EyeDepth, point: Vec3, geometric_normal: Vec3, to_eye: Vec3) -> bool { let direction = depth.orthographic_to_eye.unwrap_or(to_eye); let eye_distance = (context.eye - point).dot(direction); let lift = depth.bias * RAY_START_PER_BIAS; @@ -122,21 +122,23 @@ fn ray_reaches_eye(context: &RasterCtx, depth: &EyeDepth, point: Vec3, normal: V } // Lift off the surface on the eye's side to avoid self-hits without skipping nearby occluders - let side = if facing < 0.0 { -normal } else { normal }; + let side = if geometric_normal.dot(direction) < 0.0 { -geometric_normal } else { geometric_normal }; let origin = point + side * lift + direction * lift; !context.bvh.occluded(origin, direction, eye_distance - lift) } /// Resolve coarse depth uncertainty without letting a grazing allowance hide nearby geometry -fn surface_visible(context: &RasterCtx, depth: &EyeDepth, point: Vec3, normal: Vec3, facing: f32, to_eye: Vec3, skip: f32) -> bool { - let tested = point + to_eye * skip; +fn surface_visible(context: &RasterCtx, depth: &EyeDepth, point: Vec3, geometric_normal: Vec3, to_eye: Vec3, grazing_offset: f32, off_surface: f32) -> bool { + // Mirror displacement may cross the source surface; grazing displacement may cross an occluder + let allowed_point = point + to_eye * off_surface; + let tested = allowed_point + to_eye * grazing_offset; let (visible, uncertain) = depth.visibility(tested); if visible { - // A shifted query can pass a sheet inside the allowance, so confirm at the surface - return (skip <= 0.0 || depth.visible(point)) || ray_reaches_eye(context, depth, point, normal, facing, to_eye); + // Confirm past mirror displacement so a grazing offset cannot skip a foreground sheet + return (grazing_offset <= 0.0 || depth.visible(allowed_point)) || ray_reaches_eye(context, depth, allowed_point, geometric_normal, to_eye); } - uncertain && ray_reaches_eye(context, depth, point, normal, facing, to_eye) + uncertain && ray_reaches_eye(context, depth, allowed_point, geometric_normal, to_eye) } /// What the rasterizer reads. @@ -231,6 +233,10 @@ fn scan_triangle( let (uv_a, uv_b, uv_c) = (context.mesh.uvs.get(first)?, context.mesh.uvs.get(second)?, context.mesh.uvs.get(third)?); let (position_a, position_b, position_c) = (context.mesh.positions.get(first)?, context.mesh.positions.get(second)?, context.mesh.positions.get(third)?); let (normal_a, normal_b, normal_c) = (context.mesh.normals.get(first)?, context.mesh.normals.get(second)?, context.mesh.normals.get(third)?); + let seen_geometric_normal = (*position_b - *position_a) + .cross(*position_c - *position_a) + .try_normalize() + .map(|normal| stamp.source.map_or(normal, |source| source.direction(normal))); let x0 = (uv_a.x.min(uv_b.x).min(uv_c.x) * width as f32).floor().max(0.0) as u32; let x1 = ((uv_a.x.max(uv_b.x).max(uv_c.x) * width as f32).ceil().max(0.0) as u32).min(width); let y0 = (uv_a.y.min(uv_b.y).min(uv_c.y) * height as f32).floor().max(0.0) as u32; @@ -286,8 +292,8 @@ fn scan_triangle( } if let Some(depth) = context.depth { - let skip = grazing_allowance(depth, seen, facing, pixels_per_unit) + off_surface; - if !surface_visible(context, depth, seen, seen_normal, facing, to_eye, skip) { + let grazing_offset = grazing_allowance(depth, seen, facing, pixels_per_unit); + if !surface_visible(context, depth, seen, seen_geometric_normal.unwrap_or(seen_normal), to_eye, grazing_offset, off_surface) { continue; } } @@ -331,6 +337,71 @@ mod tests { use super::*; use texturecraft_geom::primitives; + #[test] + fn tilted_normals_do_not_hide_a_visible_grazing_plane() { + let mut plane = primitives::plane(2.0, 8); + plane.normals.fill(Vec3::new(0.0, 0.866_025_4, -0.5)); + let camera = looking_down(3.0); + let all = painted(&plane, &camera, Vec3::ZERO, 0.3, false, false); + let visible = painted(&plane, &camera, Vec3::ZERO, 0.3, false, true); + + let to_eye = camera.eye().normalize(); + assert!(plane.normals.first().is_some_and(|normal| normal.dot(to_eye) < 0.0)); + assert!(Vec3::Y.dot(to_eye) > 0.0, "the geometric plane faces the eye"); + assert!(all > 1000, "the stamp reaches the plane"); + assert_eq!(visible, all, "tilted shading normals must not move the ray below the plane"); + } + + #[test] + fn mirrored_stamp_accepts_a_source_behind_its_surface() { + use texturecraft_geom::glam::{Mat3, Vec2}; + + let corners = [Vec3::new(-1.0, 0.03, -1.0), Vec3::new(1.0, 0.03, -1.0), Vec3::new(1.0, 0.03, 1.0), Vec3::new(-1.0, 0.03, 1.0)]; + let source_corners = corners.map(|point| Vec3::new(point.x, -0.02, point.z)); + let uvs = [Vec2::ZERO, Vec2::X, Vec2::ONE, Vec2::Y].into_iter().chain([Vec2::ZERO; 4]).collect(); + let indices = [0, 2, 1, 0, 3, 2, 4, 6, 5, 4, 7, 6].to_vec(); + let mesh = Mesh::from_parts(corners.into_iter().chain(source_corners).collect(), None, uvs, indices, "asymmetric mirror").unwrap(); + let camera = looking_down(45.0); + let bvh = Bvh::new(&mesh); + let gbuf = UvGBuffer::new(&mesh, 0, 128, 128); + let depth = EyeDepth::render(&mesh, &camera, 480, 270); + let context = RasterCtx { + mesh: &mesh, + bvh: &bvh, + gbuf: &gbuf, + material: 0, + depth: Some(&depth), + eye: camera.eye(), + backface_culling: false, + angle_fade: (89.0, 90.0), + shape: Shape { alpha: crate::BrushAlpha::Round, image: None, library: None, level: 0 }, + }; + let stamp = Stamp { + center: Vec3::new(0.0, 0.03, 0.0), + normal: Vec3::Y, + tangent: Vec3::X, + bitangent: Vec3::Z, + radius: 0.3, + flow: 1.0, + hardness: 0.9, + depth: 1.0, + seed: 0, + source: Some(crate::StampSource { origin: Vec3::ZERO, to_source: Mat3::from_diagonal(Vec3::new(1.0, -1.0, 1.0)) }), + }; + let mut canvas = StrokeCanvas::new(128, 128); + let mapped = Vec3::new(0.0, -0.03, 0.0); + let to_eye = (camera.eye() - mapped).normalize(); + + assert_eq!(stamp.source.map(|source| source.point(stamp.center)), Some(mapped)); + assert!(stamp.radius * stamp.depth > 0.01, "the mapped point is inside the off-surface allowance"); + assert!(bvh.closest(mapped, to_eye, stamp.radius * stamp.depth).is_some(), "the old exact ray hits the source surface"); + assert!(!depth.visible(mapped), "the unshifted depth query rejects the mapped point"); + assert!(depth.visible(mapped + to_eye * stamp.radius * stamp.depth), "the off-surface query accepts it"); + rasterize(&mut canvas, &context, &[stamp]); + + assert!(from_u16(canvas.alpha.get(64, 64)[0]) > 0.9, "the mirrored stamp paints the visible copy texel"); + } + #[test] fn grazing_ray_detects_a_sheet_inside_the_depth_allowance() { let positions = vec![Vec3::new(-0.2, -0.2, 0.005), Vec3::new(0.2, -0.2, 0.005), Vec3::new(0.0, 0.2, 0.005)]; @@ -359,7 +430,7 @@ mod tests { assert!(skip > 0.005 / to_eye.z, "the sheet lies inside the allowance band"); assert_eq!(depth.visibility(point), (false, true)); assert!(depth.visible(point + to_eye * skip), "the allowance alone marks the hidden point visible"); - assert!(!surface_visible(&context, &depth, point, Vec3::Y, facing, to_eye, skip)); + assert!(!surface_visible(&context, &depth, point, Vec3::Y, to_eye, skip, 0.0)); } #[test] @@ -384,7 +455,7 @@ mod tests { let point = Vec3::new(1.0, 0.0, 0.0); let to_eye = (camera.eye() - point).normalize(); - assert!(!ray_reaches_eye(&context, &depth, point, Vec3::Z, 1.0, to_eye)); + assert!(!ray_reaches_eye(&context, &depth, point, Vec3::Z, to_eye)); } #[test] From 1b5d9bd16c549f03e38676d54bbd15a8bd6a0809 Mon Sep 17 00:00:00 2001 From: chasse20 Date: Tue, 17 Nov 2026 09:25:22 +0000 Subject: [PATCH 14/14] Auto: Integrate main, fix orthographic visibility, trim UI tests Reject orthographic paint points on or behind the eye plane, with regression coverage preserving near-eye and nonfinite fallbacks. Remove three UI anchor-picker tests. Integrate main's no-op tile-free sharing optimization, copy-on-write regression tests, clearer benchmark fixtures and architecture documentation, and adapter-tolerant sheen and refraction assertions. --- crates/paint/src/raster.rs | 42 ++++++++++++++++++++- crates/ui-egui/src/dock.rs | 76 -------------------------------------- 2 files changed, 41 insertions(+), 77 deletions(-) diff --git a/crates/paint/src/raster.rs b/crates/paint/src/raster.rs index 48635823..5d50a92f 100644 --- a/crates/paint/src/raster.rs +++ b/crates/paint/src/raster.rs @@ -117,7 +117,16 @@ fn ray_reaches_eye(context: &RasterCtx, depth: &EyeDepth, point: Vec3, geometric let direction = depth.orthographic_to_eye.unwrap_or(to_eye); let eye_distance = (context.eye - point).dot(direction); let lift = depth.bias * RAY_START_PER_BIAS; - if !eye_distance.is_finite() || lift >= eye_distance { + if !eye_distance.is_finite() { + return true; + } + + // Orthographic points on or behind the eye plane cannot be visible + if eye_distance <= 0.0 { + return false; + } + + if lift >= eye_distance { return true; } @@ -458,6 +467,37 @@ mod tests { assert!(!ray_reaches_eye(&context, &depth, point, Vec3::Z, to_eye)); } + #[test] + fn orthographic_ray_rejects_points_on_or_behind_the_eye_plane() { + let mesh = primitives::plane(2.0, 8); + let camera = texturecraft_geom::OrbitCamera { ortho: true, ..Default::default() }; + let bvh = Bvh::new(&mesh); + let gbuf = UvGBuffer::new(&mesh, 0, 4, 4); + let mut depth = EyeDepth::render(&mesh, &camera, 4, 4); + depth.dist.fill(0.1); + depth.dist[10] = f32::MAX; + let context = RasterCtx { + mesh: &mesh, + bvh: &bvh, + gbuf: &gbuf, + material: 0, + depth: Some(&depth), + eye: camera.eye(), + backface_culling: false, + angle_fade: (89.0, 90.0), + shape: Shape { alpha: crate::BrushAlpha::Round, image: None, library: None, level: 0 }, + }; + let to_eye = -camera.forward(); + let behind = context.eye + to_eye; + let near = context.eye - to_eye * (depth.bias * RAY_START_PER_BIAS * 0.5); + + assert_eq!(depth.visibility(behind), (false, true), "the uncertain depth reaches the exact ray fallback"); + assert!(!surface_visible(&context, &depth, behind, to_eye, to_eye, 0.0, 0.0)); + assert!(!ray_reaches_eye(&context, &depth, context.eye, to_eye, to_eye)); + assert!(ray_reaches_eye(&context, &depth, near, to_eye, to_eye), "the existing near-eye fallback remains accepted"); + assert!(ray_reaches_eye(&context, &depth, Vec3::splat(f32::NAN), to_eye, to_eye), "nonfinite input retains its fallback"); + } + #[test] fn orthographic_stamps_do_not_paint_behind_an_off_axis_shelf() { let floor = [Vec3::new(-2.0, 0.0, -2.0), Vec3::new(2.0, 0.0, -2.0), Vec3::new(2.0, 0.0, 2.0), Vec3::new(-2.0, 0.0, 2.0)]; diff --git a/crates/ui-egui/src/dock.rs b/crates/ui-egui/src/dock.rs index 6e9d006f..23bdbd7a 100644 --- a/crates/ui-egui/src/dock.rs +++ b/crates/ui-egui/src/dock.rs @@ -2250,82 +2250,6 @@ pub(crate) fn anchor_label(app: &TextureCraftApp, anchor: LayerId, channel: Chan if mask_anchor_enabled(app, anchor) == Some(false) { format!("{label} ({})", tr("Off")) } else { label } } -#[cfg(test)] -mod mask_anchor_label_tests { - use egui_kittest::Harness; - use serde_json::{Value, json}; - use texturecraft_engine::core::{Channel, LayerId}; - use texturecraft_engine::execute; - - use super::{anchor_label, anchors_below, effect_anchors_below}; - use crate::TextureCraftApp; - - #[test] - fn disabled_mask_anchors_keep_their_selection_and_show_off() { - let mut session = texturecraft_engine::sample_session(256, 64).unwrap(); - let lower = execute(&mut session, "layer.add.fill", &Value::Null).unwrap()["layer"].as_u64().unwrap(); - execute(&mut session, "mask.add.black", &json!({"layer": lower})).unwrap(); - let anchor = execute(&mut session, "effect.add.anchor", &json!({"layer": lower, "target": "mask"})).unwrap()["effect"].as_u64().unwrap(); - let upper = execute(&mut session, "layer.add.fill", &Value::Null).unwrap()["layer"].as_u64().unwrap(); - execute(&mut session, "mask.toggle", &json!({"layer": lower, "enabled": false})).unwrap(); - - let mut harness = Harness::builder().build_eframe(|context| TextureCraftApp::new(context, session)); - let labels = anchors_below(harness.state(), LayerId(upper)); - assert!(labels.iter().any(|(id, label)| *id == LayerId(anchor) && label.contains("(Off)"))); - assert!(anchor_label(harness.state(), LayerId(anchor), Channel::Height).contains("(Off)")); - - execute(&mut harness.state_mut().session, "mask.toggle", &json!({"layer": lower, "enabled": true})).unwrap(); - let labels = anchors_below(harness.state(), LayerId(upper)); - assert!(labels.iter().any(|(id, label)| *id == LayerId(anchor) && !label.contains("(Off)"))); - assert!(!anchor_label(harness.state(), LayerId(anchor), Channel::Height).contains("(Off)")); - } - - #[test] - fn inherited_effect_pickers_list_anchors_below_the_owner() { - let mut session = texturecraft_engine::sample_session(256, 64).unwrap(); - let lower = execute(&mut session, "layer.add.fill", &Value::Null).unwrap()["layer"].as_u64().unwrap(); - let valid = execute(&mut session, "effect.add.anchor", &json!({"layer": lower})).unwrap()["effect"].as_u64().unwrap(); - let source = execute(&mut session, "layer.add.fill", &Value::Null).unwrap()["layer"].as_u64().unwrap(); - execute(&mut session, "mask.add.black", &json!({"layer": source})).unwrap(); - let fill = execute(&mut session, "effect.add.fill", &json!({"layer": source})).unwrap()["effect"].as_u64().unwrap(); - let generator = execute(&mut session, "effect.add.generator", &json!({"layer": source})).unwrap()["effect"].as_u64().unwrap(); - let middle = execute(&mut session, "layer.add.fill", &Value::Null).unwrap()["layer"].as_u64().unwrap(); - let invalid = execute(&mut session, "effect.add.anchor", &json!({"layer": middle})).unwrap()["effect"].as_u64().unwrap(); - let instance = execute(&mut session, "layer.instance", &json!({"layer": source})).unwrap()["layer"].as_u64().unwrap(); - - assert!(execute(&mut session, "effect.set", &json!({"effect": fill, "anchor": invalid})).is_err()); - assert!(execute(&mut session, "effect.set", &json!({"effect": generator, "microAnchor": invalid})).is_err()); - - let harness = Harness::builder().build_eframe(|context| TextureCraftApp::new(context, session)); - assert!(anchors_below(harness.state(), LayerId(instance)).iter().any(|(id, _)| *id == LayerId(invalid))); - for effect in [fill, generator] { - let choices = effect_anchors_below(harness.state(), LayerId(instance), LayerId(effect)); - assert!(choices.iter().any(|(id, _)| *id == LayerId(valid))); - assert!(!choices.iter().any(|(id, _)| *id == LayerId(invalid))); - } - } - - #[test] - fn inherited_effect_pickers_hide_unrecorded_source_set_anchors() { - let mesh = texturecraft_engine::geom::primitives::material_ball(); - let mut session = texturecraft_engine::Session::new_project("test", mesh, "builtin:material_ball", None, 256).unwrap(); - let lower = execute(&mut session, "layer.add.fill", &Value::Null).unwrap()["layer"].as_u64().unwrap(); - let anchor = execute(&mut session, "effect.add.anchor", &json!({"layer": lower})).unwrap()["effect"].as_u64().unwrap(); - let source = execute(&mut session, "layer.add.fill", &Value::Null).unwrap()["layer"].as_u64().unwrap(); - execute(&mut session, "mask.add.black", &json!({"layer": source})).unwrap(); - let fill = execute(&mut session, "effect.add.fill", &json!({"layer": source, "anchor": anchor})).unwrap()["effect"].as_u64().unwrap(); - let generator = execute(&mut session, "effect.add.generator", &json!({"layer": source, "microAnchor": anchor})).unwrap()["effect"].as_u64().unwrap(); - let instance = execute(&mut session, "layer.instance", &json!({"layer": source, "ts": 1})).unwrap()["layer"].as_u64().unwrap(); - - let harness = Harness::builder().build_eframe(|context| TextureCraftApp::new(context, session)); - assert!(anchors_below(harness.state(), LayerId(instance)).is_empty()); - for effect in [fill, generator] { - let choices = effect_anchors_below(harness.state(), LayerId(instance), LayerId(effect)); - assert!(choices.is_empty(), "source-set anchor {anchor} is not recorded for the instance"); - } - } -} - /// Space above a fill's Material caption, setting it apart from the projection const FILL_MATERIAL_GAP: f32 = 6.0;