From 67c8ec6470a4bfe9cf168b59cccb0f62c2a084d0 Mon Sep 17 00:00:00 2001 From: Rebornim <250083243+Rebornim@users.noreply.github.com> Date: Fri, 27 Nov 2026 04:36:14 +0000 Subject: [PATCH] Mask and restore a region bake's texels row by row instead of dividing per sample --- crates/bake/src/lib.rs | 15 ++++++++------- crates/bake/src/texel_box.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/crates/bake/src/lib.rs b/crates/bake/src/lib.rs index 73847b5b..d67c684c 100644 --- a/crates/bake/src/lib.rs +++ b/crates/bake/src/lib.rs @@ -273,10 +273,8 @@ fn bake_prepared( if let Some(region) = region { for (row_index, row) in gbuffer.tri.chunks_mut(sample_width).enumerate() { let y = (sample_start_y + row_index) / samples_per_axis; - for (sample_x, triangle) in row.iter_mut().enumerate() { - if !region.texels.contains(sample_x / samples_per_axis, y) { - *triangle = NO_TRI; - } + for outside in region.texels.outside_row(y, samples_per_axis, row.len()) { + row.get_mut(outside).unwrap_or_default().fill(NO_TRI); } } } @@ -329,9 +327,12 @@ fn bake_prepared( let previous = region.previous.get(map).filter(|previous| previous.len() == pixels.len()); let previous = previous.ok_or_else(|| BakeError::Invalid(format!("no whole {} map of this size to bake a region of", map.id())))?; let components = map.components(); - for (index, (pixel, kept)) in pixels.chunks_exact_mut(components).zip(previous.chunks_exact(components)).enumerate() { - if !region.texels.contains(index % width, index / width) { - pixel.copy_from_slice(kept); + let row_len = (width * components).max(1); + for (y, (row, kept)) in pixels.chunks_exact_mut(row_len).zip(previous.chunks_exact(row_len)).enumerate() { + for outside in region.texels.outside_row(y, components, row_len) { + if let (Some(row), Some(kept)) = (row.get_mut(outside.clone()), kept.get(outside)) { + row.copy_from_slice(kept); + } } } } diff --git a/crates/bake/src/texel_box.rs b/crates/bake/src/texel_box.rs index 3dc062cf..306dd04b 100644 --- a/crates/bake/src/texel_box.rs +++ b/crates/bake/src/texel_box.rs @@ -1,3 +1,5 @@ +use std::ops::Range; + /// A rectangle of output texels, from `min` (inclusive) to `max` (exclusive), in atlas pixels #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct TexelBox { @@ -11,6 +13,15 @@ impl TexelBox { (self.min.0 as usize..self.max.0 as usize).contains(&x) && (self.min.1 as usize..self.max.1 as usize).contains(&y) } + /// The items of texel row `y` outside the box, in a row of `len` items with `scale` items per texel: those before the box and those after it + pub fn outside_row(&self, y: usize, scale: usize, len: usize) -> [Range; 2] { + let (start, end) = match (self.min.1 as usize..self.max.1 as usize).contains(&y) { + true => ((self.min.0 as usize * scale).min(len), (self.max.0 as usize * scale).min(len)), + false => (len, len), + }; + [0..start, end.max(start)..len] + } + /// The box grown by `texels` on every side, kept inside a `width` x `height` image pub fn grown(&self, texels: u32, width: u32, height: u32) -> TexelBox { TexelBox { @@ -19,3 +30,28 @@ impl TexelBox { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_items_outside_a_row_are_the_ones_contains_leaves_out() { + let (width, scale) = (6, 2); + let boxes = [ + TexelBox { min: (1, 1), max: (4, 3) }, + TexelBox { min: (0, 0), max: (6, 4) }, + TexelBox { min: (3, 1), max: (9, 2) }, + TexelBox { min: (4, 1), max: (2, 3) }, + ]; + for texels in boxes { + for y in 0..4 { + let outside = texels.outside_row(y, scale, width * scale); + for item in 0..width * scale { + let is_outside = outside.iter().any(|range| range.contains(&item)); + assert_eq!(is_outside, !texels.contains(item / scale, y), "{texels:?} row {y} item {item}"); + } + } + } + } +}