diff --git a/crates/compose/src/dripping_rust.rs b/crates/compose/src/dripping_rust.rs index c098fe29..43e42858 100644 --- a/crates/compose/src/dripping_rust.rs +++ b/crates/compose/src/dripping_rust.rs @@ -144,7 +144,11 @@ fn drips(inp: &Inputs, rust: &DrippingRustParameters, field: &[f32], (width, hei let along = step as f32 / samples as f32; let distance = reach * along; let (sample_x, sample_y) = (x as f32 + distance * uphill.0 / length, y as f32 + distance * uphill.1 / length); - match field_at(sample_x.round() as i64, sample_y.round() as i64) { + let (sample_x, sample_y) = (sample_x.round() as i64, sample_y.round() as i64); + + // Rust drips down: a tap the ray reaches past a ridge, lower on the axis, gives none + let above = axis_at(sample_x, sample_y).is_some_and(|axis| axis >= axis_here); + match field_at(sample_x, sample_y).filter(|_| above) { Some(value) => most.max(value * (1.0 - DRIP_FADE * along.powf(DRIP_FADE_POWER))), None => most, } @@ -152,3 +156,52 @@ fn drips(inp: &Inputs, rust: &DrippingRustParameters, field: &[f32], (width, hei }) .collect() } + +#[cfg(test)] +mod tests { + use texturecraft_core::{Project, TextureSet}; + use texturecraft_geom::{UvGBuffer, primitives}; + + use super::*; + use crate::{BakedMaps, PixelStore}; + + const RES: u32 = 256; + + #[test] + fn a_drip_takes_no_rust_from_past_a_ridge_lower_on_the_axis() { + // A strip rising gently to a ridge at its middle, then falling steeply + let mut mesh = primitives::plane(2.0, 64); + for position in &mut mesh.positions { + position.y = if position.z <= 0.0 { 1.0 + position.z } else { 1.0 - 8.0 * position.z }; + } + let gbuf = UvGBuffer::new(&mesh, 0, RES, RES); + let mut project = Project::new("t", "strip"); + project.texture_sets.push(TextureSet::new("A", 0, RES)); + let (pixels, baked, images) = (PixelStore::default(), BakedMaps::default(), Default::default()); + let inputs = Inputs { + project: &project, + ts: 0, + mesh: &mesh, + gbuf: &gbuf, + pixels: &pixels, + baked: &baked, + block_scales: project.texture_sets[0].block_scales(), + preview: None, + images: &images, + bounds: mesh.bounds(), + }; + + // Rust only on the steep side, all lower than the gentle side's texel just below the ridge + let rusted = |surface: &texturecraft_geom::Surface| surface.pos.z > 0.01; + let field: Vec = (0..RES * RES).map(|texel| gbuf.surface(&mesh, texel as usize).filter(rusted).map_or(0.0, |_| 1.0)).collect(); + let rust = DrippingRustParameters { drips_intensity: 1.0, drips_samples: DrippingRustParameters::MAX_SAMPLES, axis: 1, ..Default::default() }; + let dripped = drips(&inputs, &rust, &field, (RES, RES), &|_, _| RES as f32); + + let below_ridge = (0..RES * RES) + .find(|texel| { + gbuf.surface(&mesh, *texel as usize).is_some_and(|surface| (surface.pos.z + 0.04).abs() < 1.0 / RES as f32 && surface.pos.x.abs() < 0.1) + }) + .expect("a texel just below the ridge"); + assert_eq!(dripped[below_ridge as usize], 0.0); + } +} diff --git a/crates/compose/src/histogram_scan.rs b/crates/compose/src/histogram_scan.rs index ca9c851c..8c474d7e 100644 --- a/crates/compose/src/histogram_scan.rs +++ b/crates/compose/src/histogram_scan.rs @@ -2,6 +2,14 @@ /// `value` scanned at `position` (0 all black, 1 all white) over a ramp `1 − contrast` wide; contrast 1 is a hard step pub(crate) fn histogram_scan(value: f32, position: f32, contrast: f32) -> f32 { + // The ends hold at a hard step too, where the ramp has no width to reach them + if position <= 0.0 { + return 0.0; + } + if position >= 1.0 { + return 1.0; + } + let width = 1.0 - contrast.clamp(0.0, 1.0); // The ramp's low end runs from 1 at position 0 to −width at position 1, so the ends are wholly black or white let low = (1.0 - position.clamp(0.0, 1.0)) * (1.0 + width) - width; @@ -23,6 +31,12 @@ mod tests { } } + #[test] + fn the_ends_of_the_position_hold_at_a_hard_step() { + assert_eq!(histogram_scan(1.0, 0.0, 1.0), 0.0, "position 0 is all black, even for white"); + assert_eq!(histogram_scan(0.0, 1.0, 1.0), 1.0, "position 1 is all white, even for black"); + } + #[test] fn the_middle_position_keeps_mid_gray_and_contrast_narrows_around_it() { assert!((histogram_scan(0.5, 0.5, 0.5) - 0.5).abs() < 1e-6); diff --git a/crates/compose/src/slot_placement.rs b/crates/compose/src/slot_placement.rs index 69af657b..d5c18cde 100644 --- a/crates/compose/src/slot_placement.rs +++ b/crates/compose/src/slot_placement.rs @@ -4,4 +4,6 @@ pub(crate) struct SlotPlacement { pub offset: f32, /// How sharply the three projections meet, or None to read in UV pub triplanar_contrast: Option, + /// The X projection's mirrored U turns about the bounds' centre, which a centred scale keeps fixed (Use Texture); else about 0, as custom grunge was fitted + pub mirrored_about_centre: bool, } diff --git a/crates/compose/src/texture_layer.rs b/crates/compose/src/texture_layer.rs index 02cf6769..4d121f42 100644 --- a/crates/compose/src/texture_layer.rs +++ b/crates/compose/src/texture_layer.rs @@ -13,7 +13,12 @@ const MIN_NORMAL_COMPONENT: f32 = 1e-6; /// The texture is scaled about the centre of the UV square (or of the mesh's bounds) pub(crate) fn texture_placement(texture: &TextureLayer) -> SlotPlacement { - SlotPlacement { scale: texture.scale, offset: 0.5 * (1.0 - texture.scale), triplanar_contrast: texture.triplanar.then_some(texture.triplanar_contrast) } + SlotPlacement { + scale: texture.scale, + offset: 0.5 * (1.0 - texture.scale), + triplanar_contrast: texture.triplanar.then_some(texture.triplanar_contrast), + mirrored_about_centre: true, + } } /// Where a slot is read in UV @@ -24,7 +29,8 @@ pub(crate) fn uv_coordinates(placement: &SlotPlacement, uv: Vec2) -> Vec2 { /// Where a slot is read on each of the three projections (facing X, Y and Z) for `position` across the mesh's bounds (0..1) pub(crate) fn triplanar_coordinates(placement: &SlotPlacement, position: Vec3) -> [Vec2; 3] { let placed = |u: f32, v: f32| Vec2::new(u, v) * placement.scale + Vec2::splat(placement.offset); - [placed(-position.z, position.y), placed(position.x, position.z), placed(position.x, position.y)] + let mirrored_z = if placement.mirrored_about_centre { 1.0 - position.z } else { -position.z }; + [placed(mirrored_z, position.y), placed(position.x, position.z), placed(position.x, position.y)] } /// How much each projection (facing X, Y and Z) counts for a surface facing `normal` at triplanar `contrast`, summing to 1 @@ -153,4 +159,12 @@ mod tests { assert_eq!(uv_coordinates(&placement, Vec2::splat(0.5)), Vec2::splat(0.5)); assert_eq!(uv_coordinates(&placement, Vec2::new(0.75, 0.25)), Vec2::new(1.0, 0.0)); } + + #[test] + fn every_triplanar_projection_is_scaled_about_the_centre_of_the_bounds() { + let placement = texture_placement(&TextureLayer { scale: 1.5, triplanar: true, ..texture() }); + for (axis, at) in triplanar_coordinates(&placement, Vec3::splat(0.5)).into_iter().enumerate() { + assert!((at - Vec2::splat(0.5)).abs().max_element() < 1e-6, "projection {axis} reads {at} at the centre"); + } + } } diff --git a/crates/compose/src/wear.rs b/crates/compose/src/wear.rs index 14903b8f..3eb2d7d9 100644 --- a/crates/compose/src/wear.rs +++ b/crates/compose/src/wear.rs @@ -65,9 +65,9 @@ pub(crate) fn grunge(kind: GeneratorKind, wear: &WearParameters, seed: u32, coor /// Where a custom grunge image is read: repeated grunge-scale times across the UV square (or the mesh's bounds, triplanar) pub(crate) fn custom_grunge_placement(wear: &WearParameters) -> SlotPlacement { if wear.triplanar { - SlotPlacement { scale: wear.grunge_scale, offset: 0.0, triplanar_contrast: Some(wear.triplanar_contrast) } + SlotPlacement { scale: wear.grunge_scale, offset: 0.0, triplanar_contrast: Some(wear.triplanar_contrast), mirrored_about_centre: false } } else { - SlotPlacement { scale: wear.grunge_scale, offset: CUSTOM_GRUNGE_UV_OFFSET, triplanar_contrast: None } + SlotPlacement { scale: wear.grunge_scale, offset: CUSTOM_GRUNGE_UV_OFFSET, triplanar_contrast: None, mirrored_about_centre: false } } }