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/map/curvature.rs b/crates/bake/src/map/curvature.rs index eac8dd52..fad35403 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; +/// 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; @@ -20,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, @@ -64,7 +67,8 @@ impl Curvature { rays_per_sample, wall_count: ring.first().map_or(0, Vec::len), walls, - spread: walls * 4.0 / 3.0, + // 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 { @@ -87,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/test/bent_normal_space.rs b/crates/bake/src/test/bent_normal_space.rs new file mode 100644 index 00000000..1d9eb87f --- /dev/null +++ b/crates/bake/src/test/bent_normal_space.rs @@ -0,0 +1,75 @@ +//! 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..dab94f80 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; @@ -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; @@ -243,12 +246,67 @@ 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 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}"); } -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_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 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) +} + +/// 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 }; @@ -259,19 +317,24 @@ fn check_box_curvature(m: &Mesh, edge: 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 > 0.55, "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() { - 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, None); } #[test] fn cube_edges_are_convex() { - check_box_curvature(&primitives::cube(2.0), 0.97); + check_box_curvature(&primitives::cube(2.0), 0.97, Some(0.55)); } #[test] @@ -570,11 +633,18 @@ 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 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 > 0.55, "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"); } } @@ -588,13 +658,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); - 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 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 > 0.6, "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}"); + } } } @@ -693,7 +771,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 77a872f7..c43c9a68 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/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 e1d98196..fd13bec2 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,25 @@ 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) { + // 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 (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)); + } + } + 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, or set its image again if it came from a file.", + 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..4364f39e 100644 --- a/crates/formats/src/project_file/tests.rs +++ b/crates/formats/src/project_file/tests.rs @@ -111,6 +111,60 @@ 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 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(); + 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); + + // 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/paint/src/raster.rs b/crates/paint/src/raster.rs index b033538b..5d50a92f 100644 --- a/crates/paint/src/raster.rs +++ b/crates/paint/src/raster.rs @@ -104,6 +104,52 @@ 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` 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, 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; + 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; + } + + // Lift off the surface on the eye's side to avoid self-hits without skipping nearby occluders + 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, 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 { + // 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, allowed_point, geometric_normal, to_eye) +} + /// What the rasterizer reads. pub struct RasterCtx<'a> { pub mesh: &'a Mesh, @@ -126,6 +172,12 @@ fn smoothstep(e0: f32, e1: f32, x: f32) -> f32 { /// Accumulate `stamps` into `canvas` ("over": `m = a + m·(1 − a)`). pub fn rasterize(canvas: &mut StrokeCanvas, context: &RasterCtx, stamps: &[Stamp]) { + // Broad-phase padding for rotated stamp footprints + const STAMP_QUERY_PADDING: f32 = 1.42; + + // Keep small batches serial to avoid scheduling overhead + const PARALLEL_TRIANGLE_THRESHOLD: usize = 16; + let (width, height) = (context.gbuf.width, context.gbuf.height); canvas.fit(context.gbuf); let fade_lo = context.angle_fade.1.to_radians().cos(); @@ -136,20 +188,24 @@ pub fn rasterize(canvas: &mut StrokeCanvas, context: &RasterCtx, stamps: &[Stamp let ndc_per_unit = depth.view_proj.row(1).truncate().length(); (depth.height as f32 * ndc_per_unit).max(f32::EPSILON) }); + let mut triangles = Vec::new(); for stamp in stamps { if stamp.radius.is_nan() || stamp.radius <= 0.0 || !stamp.center.is_finite() { continue; } - let extent = Vec3::splat(stamp.radius * stamp.depth.max(1.0) * 1.42); + let extent = Vec3::splat(stamp.radius * stamp.depth.max(1.0) * STAMP_QUERY_PADDING); triangles.clear(); context.bvh.query_box(stamp.center - extent, stamp.center + extent, |triangle| triangles.push(triangle)); // Each texel belongs to one triangle, so large stamps can scan triangles in parallel let scan = |&triangle: &u32| scan_triangle(context, stamp, triangle, width, height, fade_lo, fade_hi, pixels_per_unit); - let parts: Vec = - if triangles.len() > 16 { triangles.par_iter().filter_map(scan).collect() } else { triangles.iter().filter_map(scan).collect() }; + let parts: Vec = if triangles.len() > PARALLEL_TRIANGLE_THRESHOLD { + triangles.par_iter().filter_map(scan).collect() + } else { + triangles.iter().filter_map(scan).collect() + }; // The footprint texel nearest the stamp center supplies the pickup's center let mut center = ([0.0f32; 2], f32::MAX); @@ -159,6 +215,7 @@ pub fn rasterize(canvas: &mut StrokeCanvas, context: &RasterCtx, stamps: &[Stamp } canvas.texels.extend(texels); } + canvas.add_stamp(center.0); } } @@ -185,6 +242,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; @@ -232,16 +293,18 @@ fn scan_triangle( // Allow half the stamp depth when the mirrored point is off an asymmetric surface let (seen, seen_normal) = stamp.source.map_or((position, normal), |source| (source.point(position), source.direction(normal))); let off_surface = if stamp.source.is_some() { stamp.radius * stamp.depth } else { 0.0 }; - let to_eye = (context.eye - seen).try_normalize().unwrap_or(seen_normal); + let to_eye = + context.depth.and_then(|depth| depth.orthographic_to_eye).unwrap_or_else(|| (context.eye - seen).try_normalize().unwrap_or(seen_normal)); let facing = seen_normal.dot(to_eye); if context.backface_culling && facing <= 0.0 { 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 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; + } } let fade = if context.backface_culling { smoothstep(fade_lo, fade_hi, facing) } else { 1.0 }; @@ -284,15 +347,400 @@ mod tests { use texturecraft_geom::primitives; #[test] - fn one_stamp_on_a_plane_paints_a_disc() { - let m = primitives::plane(2.0, 8); - let bvh = Bvh::new(&m); - let g = UvGBuffer::new(&m, 0, 128, 128); - let mut canvas = StrokeCanvas::new(128, 128); - let ctx = RasterCtx { - mesh: &m, + 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: &g, + 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)]; + 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, to_eye, skip, 0.0)); + } + + #[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)]; + let mesh = Mesh::from_parts(positions, None, vec![texturecraft_geom::glam::Vec2::ZERO; 3], vec![0, 1, 2], "blocker").unwrap(); + let camera = texturecraft_geom::OrbitCamera { pitch: 0.0, ortho: true, ..Default::default() }; + let mesh_bvh = Bvh::new(&mesh); + let uv_buffer = UvGBuffer::new(&mesh, 0, 4, 4); + let depth = EyeDepth::render(&mesh, &camera, 128, 128); + 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::new(1.0, 0.0, 0.0); + let to_eye = (camera.eye() - point).normalize(); + + 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)]; + let shelf = [Vec3::new(0.95, 0.025, -2.0), Vec3::new(1.05, 0.025, -2.0), Vec3::new(1.05, 0.025, 2.0), Vec3::new(0.95, 0.025, 2.0)]; + let floor_uvs = floor.iter().map(|point| texturecraft_geom::glam::Vec2::new((point.x + 2.0) / 4.0, (point.z + 2.0) / 4.0)); + let uvs = floor_uvs.chain([texturecraft_geom::glam::Vec2::ZERO; 4]).collect(); + let quad_indices = [0u32, 2, 1, 0, 3, 2]; + let indices = quad_indices.iter().copied().chain(quad_indices.iter().map(|index| index + 4)).collect(); + let mesh = Mesh::from_parts(floor.into_iter().chain(shelf).collect(), None, uvs, indices, "floor and shelf").unwrap(); + let camera = texturecraft_geom::OrbitCamera { ortho: true, ..looking_down(3.0) }; + let center = Vec3::new(1.0, 0.0, 0.0); + let depth = EyeDepth::render(&mesh, &camera, 480, 270); + let to_eye = -camera.forward(); + let pixels_per_unit = depth.height as f32 * depth.view_proj.row(1).truncate().length(); + let skip = grazing_allowance(&depth, center, Vec3::Y.dot(to_eye), pixels_per_unit); + + assert_eq!(depth.visibility(center + to_eye * skip), (false, true), "the stamp center requires the exact visibility test"); + + let all = painted_texels(&mesh, &camera, center, 0.2, false, false); + let visible = painted_texels(&mesh, &camera, center, 0.2, false, true); + + // Columns 191 and 192 lie strictly beneath the shelf at x = 1 + assert!(all.iter().any(|&(x, _)| (191..=192).contains(&x)), "the stamp covers the hidden floor"); + assert!(!visible.is_empty(), "the floor beside the shelf remains paintable"); + assert!(!visible.iter().any(|&(x, _)| (191..=192).contains(&x)), "the shelf hides the floor in the orthographic view"); + } + + /// 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 mesh_bvh = Bvh::new(mesh); + let uv_buffer = 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 context = RasterCtx { + mesh, + bvh: &mesh_bvh, + gbuf: &uv_buffer, + 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, &context, &[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 = |point: Vec3| texturecraft_geom::glam::Vec2::new((point.x + 1.0) / 2.0, (point.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 quad_indices = [0u32, 2, 1, 0, 3, 2]; + let indices = quad_indices.iter().copied().chain(quad_indices.iter().map(|i| i + 4)).collect(); + let mesh = Mesh::from_parts(floor.into_iter().chain(shelf).collect(), None, uvs, indices, "floor and shelf").unwrap(); + + for pitch in [60.0, 30.0] { + let camera = looking_down(pitch); + let open = Vec3::new(0.5, 0.0, -0.5); + let open_coverage = painted(&mesh, &camera, open, 0.2, true, false); + let visible_coverage = painted(&mesh, &camera, open, 0.2, true, true); + + assert!(open_coverage > 100, "the stamp covers the open half"); + assert_eq!(visible_coverage, open_coverage, "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}°"); + + // The depth buffer cannot resolve the shelf edge; the exact ray decides + 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}°"); + } + } + + /// 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] + 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 straddling a sphere's perspective silhouette exercise visible and hidden texels. + /// `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 mesh_bvh = Bvh::new(&sphere); + let uv_buffer = UvGBuffer::new(&sphere, 0, 2048, 2048); + let camera = looking_down(0.0); + let depth = EyeDepth::render(&sphere, &camera, 960, 540); + let context = RasterCtx { + mesh: &sphere, + bvh: &mesh_bvh, + gbuf: &uv_buffer, + 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 }, + }; + + // A silhouette normal is perpendicular to the direction from the surface to the eye + let silhouette_z = 1.0 / camera.eye().z; + let silhouette_radius = (1.0 - silhouette_z * silhouette_z).sqrt(); + let stamps: Vec = (0..64) + .map(|stamp_index| { + let angle = stamp_index as f32 / 64.0 * std::f32::consts::TAU; + let center = Vec3::new(silhouette_radius * angle.cos(), silhouette_radius * angle.sin(), silhouette_z); + let tangent = (camera.eye() - center).normalize(); + Stamp { + center, + normal: center, + tangent, + bitangent: center.cross(tangent), + radius: 0.15, + flow: 1.0, + hardness: 0.9, + depth: 1.0, + seed: stamp_index, + source: None, + } + }) + .collect(); + + let painted_count = |canvas: &StrokeCanvas| { + canvas.alpha.allocated().filter_map(|(x, y)| canvas.alpha.tile(x, y)).map(|tile| tile.iter().filter(|&&alpha| alpha > 0).count()).sum::() + }; + let mut visible = StrokeCanvas::new(2048, 2048); + rasterize(&mut visible, &context, &stamps); + let visible_texels = painted_count(&visible); + drop(visible); + let mut all = StrokeCanvas::new(2048, 2048); + rasterize(&mut all, &RasterCtx { depth: None, ..context }, &stamps); + let all_texels = painted_count(&all); + drop(all); + + assert!(visible_texels > 0, "the benchmark must exercise visible paint"); + assert!(visible_texels < all_texels, "the benchmark must also reject hidden texels"); + eprintln!("silhouette coverage: {visible_texels} visible texels / {all_texels} without visibility"); + + let mut elapsed_ms = Vec::new(); + for _ in 0..9 { + let mut canvas = StrokeCanvas::new(2048, 2048); + let start = std::time::Instant::now(); + rasterize(&mut canvas, &context, &stamps); + elapsed_ms.push(start.elapsed().as_secs_f64() * 1000.0); + } + + eprintln!("elapsed milliseconds: {elapsed_ms:?}"); + elapsed_ms.sort_by(f64::total_cmp); + eprintln!("64 silhouette stamps at 2048²: median {:.2} ms, range {:.2}..{:.2} ms", elapsed_ms[4], elapsed_ms[0], elapsed_ms[8]); + } + + #[test] + fn one_stamp_on_a_plane_paints_a_disc() { + let mesh = primitives::plane(2.0, 8); + let bvh = Bvh::new(&mesh); + let uv_buffer = UvGBuffer::new(&mesh, 0, 128, 128); + let mut canvas = StrokeCanvas::new(128, 128); + let context = RasterCtx { + mesh: &mesh, + bvh: &bvh, + gbuf: &uv_buffer, material: 0, depth: None, eye: Vec3::new(0.0, 5.0, 0.0), @@ -300,7 +748,7 @@ mod tests { angle_fade: (70.0, 85.0), shape: Shape { alpha: crate::BrushAlpha::Round, image: None, library: None, level: 0 }, }; - let st = Stamp { + let stamp = Stamp { center: Vec3::ZERO, normal: Vec3::Y, tangent: Vec3::X, @@ -312,19 +760,23 @@ mod tests { seed: 0, source: None, }; - rasterize(&mut canvas, &ctx, &[st]); - // Plane is 2 units over 128 texels: radius 0.25 = 16 texels around the center (64, 64). + + rasterize(&mut canvas, &context, &[stamp]); + + // Plane is 2 units over 128 texels: radius 0.25 = 16 texels around the center (64, 64) let at = |x: u32, y: u32| from_u16(canvas.alpha.get(x, y)[0]); assert!(at(64, 64) > 0.99); assert!(at(64 + 12, 64) > 0.9); assert!(at(64 + 20, 64) < 0.01); assert!(!canvas.dirty.is_empty()); - // From below (backface), nothing is painted. - let mut c2 = StrokeCanvas::new(128, 128); - let below = RasterCtx { eye: Vec3::new(0.0, -5.0, 0.0), ..ctx }; - rasterize(&mut c2, &below, &[st]); - assert!(c2.dirty.is_empty()); + // Backface culling keeps the underside unpainted + let mut underside = StrokeCanvas::new(128, 128); + let below = RasterCtx { eye: Vec3::new(0.0, -5.0, 0.0), ..context }; + + rasterize(&mut underside, &below, &[stamp]); + + assert!(underside.dirty.is_empty()); } } diff --git a/crates/paint/src/visibility.rs b/crates/paint/src/visibility.rs index ed7c575e..40fd8db1 100644 --- a/crates/paint/src/visibility.rs +++ b/crates/paint/src/visibility.rs @@ -22,6 +22,12 @@ const EDGE_TOLERANCE: f32 = 1e-4; /// [`EyeDepth::bias`] as a fraction of the scene radius. const BIAS_PER_RADIUS: f32 = 0.004; +/// Extra tolerance for depth variation across a coarse screen pixel. +const DISTANCE_TOLERANCE_FRACTION: f32 = 0.002; + +/// Query points closer to the camera plane cannot be projected reliably. +const MIN_QUERY_CLIP_W: f32 = 1e-6; + /// Distance from the eye to the nearest surface per screen pixel (f32::MAX where empty). #[derive(Clone, Debug)] pub struct EyeDepth { @@ -29,6 +35,8 @@ pub struct EyeDepth { pub height: u32, pub view_proj: Mat4, pub eye: Vec3, + /// Parallel direction toward the camera in an orthographic view. + pub orthographic_to_eye: Option, pub dist: Vec, /// The nearest triangle per screen pixel ([`NO_TRI`] where empty); empty unless rendered with [`EyeDepth::render_with_triangles`]. pub triangle: Vec, @@ -69,10 +77,12 @@ impl EyeDepth { let (width, height) = (width.clamp(1, MAX_SIZE), height.clamp(1, MAX_SIZE)); let size = Vec2::new(width as f32, height as f32); let eye = camera.eye(); + // Brush depth closes seams, but expanding triangle IDs can expose hidden faces at silhouettes let edge_tolerance = if record_triangles { 0.0 } else { EDGE_TOLERANCE }; let screen: Vec> = mesh.positions.iter().map(|position| screen_point(view_proj, *position, size)).collect(); + // Inverse view depth is affine in perspective and avoids the precision loss of normalized clip depth let vertex_depth: Vec = mesh.positions.iter().map(|position| depth_key(-(view * position.extend(1.0)).z)).collect(); @@ -156,36 +166,85 @@ impl EyeDepth { } }); - EyeDepth { width, height, view_proj, eye, dist, triangle, bias: mesh.radius() * BIAS_PER_RADIUS } + EyeDepth { + width, + height, + view_proj, + eye, + orthographic_to_eye: camera.ortho.then(|| -camera.forward()), + dist, + triangle, + bias: mesh.radius() * BIAS_PER_RADIUS, + } } - /// Whether world point `p` is visible (not hidden behind other geometry, inside the view). - pub fn visible(&self, p: Vec3) -> bool { - let c = self.view_proj * p.extend(1.0); - if c.w <= 1e-6 { - return false; + /// Whether a world point is visible (not hidden behind other geometry, inside the view). + pub fn visible(&self, point: Vec3) -> bool { + self.visibility(point).0 + } + + /// Whether a world point inside the view lies no farther than the farthest surface in its + /// 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, point: Vec3) -> bool { + self.visibility(point).1 + } + + /// Reuse the same depth neighbourhood for the fast and exact-test gates. + pub(crate) fn visibility(&self, point: Vec3) -> (bool, bool) { + let Some((near, far, has_empty)) = self.neighbourhood(point) else { return (false, false) }; + + let distance = point.distance(self.eye); + let visible = distance <= near + self.bias; + let uncertain = has_empty || (far < f32::MAX && distance <= far + self.tolerance(far)); + + (visible, uncertain) + } + + /// Allowance at an eye distance: a bias that grows with distance (depth buffer resolution). + fn tolerance(&self, distance: f32) -> f32 { + self.bias + distance * DISTANCE_TOLERANCE_FRACTION + } + + /// The nearest and farthest surface among the pixel a point lands in and its neighbours (f32::MAX + /// 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, point: Vec3) -> Option<(f32, f32, bool)> { + let clip = self.view_proj * point.extend(1.0); + if clip.w <= MIN_QUERY_CLIP_W { + return None; } - let (nx, ny) = (c.x / c.w, c.y / c.w); + + let (nx, ny) = (clip.x / clip.w, clip.y / clip.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, 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); 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(&surface_distance) = self.dist.get((sy as u32 * self.width + sx as u32) as usize) { + near = near.min(surface_distance); + if surface_distance < f32::MAX { + far = far.max(surface_distance); + } else { + has_empty = true; + } } } } - // 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 }, has_empty)) } } @@ -194,17 +253,41 @@ 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 m = primitives::sphere(1.0, 64, 32); - let mut cam = OrbitCamera::default(); - cam.yaw = 0.0; - cam.pitch = 0.0; - cam.frame(Vec3::splat(-1.0), Vec3::splat(1.0)); - let d = EyeDepth::render(&m, &cam, 256, 256); - let toward = (cam.eye() - cam.target).normalize(); - assert!(d.visible(toward * 1.0)); - assert!(!d.visible(-toward * 1.0)); - assert!(!d.visible(Vec3::new(100.0, 0.0, 0.0))); + let mesh = primitives::sphere(1.0, 64, 32); + let mut camera = OrbitCamera { yaw: 0.0, pitch: 0.0, ..Default::default() }; + camera.frame(Vec3::splat(-1.0), Vec3::splat(1.0)); + let toward = (camera.eye() - camera.target).normalize(); + + let depth = EyeDepth::render(&mesh, &camera, 256, 256); + + assert!(depth.visible(toward)); + assert!(!depth.visible(-toward)); + assert!(!depth.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!(!depth.uncertain(-toward)); + assert!(depth.uncertain(toward)); + assert!(!depth.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!(depth.uncertain(Vec3::new(0.0, 1.0, 0.0)), "on the silhouette"); } } diff --git a/crates/render/src/shaders/bake_view.wgsl b/crates/render/src/shaders/bake_view.wgsl index 0720e318..846f1d31 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,10 @@ 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); + // 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; 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 c91b00aa..1a57790f 100644 --- a/crates/render/src/shaders/mesh.wgsl +++ b/crates/render/src/shaders/mesh.wgsl @@ -485,7 +485,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 f6df9ab6..845de3df 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/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; 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