From 725146c0ca1f3a37568ec5db440cfe4d5fce2e85 Mon Sep 17 00:00:00 2001 From: Cosmic <84875247+Online5277@users.noreply.github.com> Date: Thu, 15 Oct 2026 15:41:53 -0400 Subject: [PATCH] Generate sprite mips before array-only enlargement --- .../horizon-client/examples/mipmap_probe.rs | 9 +-- crates/horizon-client/src/gfx.rs | 63 +++++++++++++++++-- crates/horizon-client/src/mipmap.rs | 54 ++++++++++++---- crates/horizon-client/src/sprites.rs | 1 + 4 files changed, 104 insertions(+), 23 deletions(-) diff --git a/crates/horizon-client/examples/mipmap_probe.rs b/crates/horizon-client/examples/mipmap_probe.rs index 0cdb9655f..99155fb32 100644 --- a/crates/horizon-client/examples/mipmap_probe.rs +++ b/crates/horizon-client/examples/mipmap_probe.rs @@ -31,14 +31,7 @@ fn main() -> horizon_client::Result<()> { }; let s = sprites.mips[layer as usize]; writeln!(out, "== {name}")?; - for mip in horizon_client::mipmap::generate( - base, - size.trailing_zeros(), - s.strategy, - s.alpha_cutoff_bias, - s.has_transparent, - s.item, - ) { + for mip in horizon_client::mipmap::generate(base, size.trailing_zeros(), s) { write!(out, "{}x{}", mip.width, mip.height)?; for row in mip.pixels.chunks(mip.width as usize) { out.push(' '); diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 8d096c3c5..da481e170 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -336,10 +336,10 @@ fn mip_chain( for image in crate::mipmap::generate( base, levels - 1, - s.strategy, - s.alpha_cutoff_bias, - has_transparent, - s.item, + crate::mipmap::Settings { + has_transparent, + ..s + }, ) { out.extend(image.pixels.iter().flatten()); } @@ -2829,6 +2829,61 @@ fn animate_sprites( mod sprite_upload_tests { use super::*; #[test] + fn real_rail_mips_do_not_change_with_larger_array_layers() { + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client/26.3"); + if !root + .join("assets/minecraft/textures/block/rail.png") + .exists() + { + return; + } + let mut sprites = crate::sprites::Sprites::default(); + let layers: Vec<_> = [ + "rail", + "powered_rail", + "powered_rail_on", + "detector_rail", + "activator_rail", + ] + .map(|name| { + sprites + .insert(&root, &format!("minecraft:block/{name}")) + .unwrap() + }) + .into(); + sprites.finish(); + assert_eq!(sprites.size, 16); + let native: Vec<_> = layers + .iter() + .map(|&layer| { + let start = layer as usize * 16 * 16 * 4; + mip_chain( + &sprites.pixels[start..start + 16 * 16 * 4], + 16, + &sprites.mips, + layer, + 1, + ) + .0 + }) + .collect(); + sprites.insert(&root, "minecraft:block/oak_sign").unwrap(); + sprites.finish(); + assert_eq!(sprites.size, 32); + for (&layer, expected) in layers.iter().zip(native) { + let start = layer as usize * 32 * 32 * 4; + let (actual, _) = mip_chain( + &sprites.pixels[start..start + 32 * 32 * 4], + 32, + &sprites.mips, + layer, + 1, + ); + assert_eq!(&actual[32 * 32 * 4..], expected, "rail layer {layer}"); + } + } + #[test] fn mipmap_option_keeps_real_16px_reductions() { for levels in 0..=4 { assert_eq!(sprite_mipmap_limit(levels, 32, 16, 6), levels + 1); diff --git a/crates/horizon-client/src/mipmap.rs b/crates/horizon-client/src/mipmap.rs index 98911e54f..33c99cdc3 100644 --- a/crates/horizon-client/src/mipmap.rs +++ b/crates/horizon-client/src/mipmap.rs @@ -31,6 +31,9 @@ impl Strategy { /// it has a fully transparent pixel and whether it is an `item/` sprite. #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct Settings { + /// Original square frame edge, before fitting the sprite into the array. + /// Zero means the supplied image is already at its native size. + pub native_size: u32, pub strategy: Strategy, pub alpha_cutoff_bias: f32, pub has_transparent: bool, @@ -45,6 +48,15 @@ pub struct Image { pub pixels: Vec<[u8; 4]>, } impl Image { + fn resized(&self, size: u32) -> Self { + Self { + width: size, + height: size, + pixels: (0..size * size) + .map(|i| self.get(i % size * self.width / size, i / size * self.height / size)) + .collect(), + } + } fn get(&self, x: u32, y: u32) -> [u8; 4] { self.pixels[(x + y * self.width) as usize] } @@ -263,17 +275,37 @@ fn darkened_alpha_blend(c: [[u8; 4]; 4]) -> [u8; 4] { } /// `MipmapGenerator.generateMipLevels` from the base image (`levels` mips -/// below it): `has_transparent` is `Transparency.hasTransparent` (a pixel -/// with zero alpha), `item` whether the sprite is under `item/` (its base -/// is left as is). -pub fn generate( - mut base: Image, - levels: u32, - strategy: Strategy, - alpha_cutoff_bias: f32, - has_transparent: bool, - item: bool, -) -> Vec { +/// below it). Array-only enlargement must not run extra alpha-coverage steps +/// before the sprite's native mip chain. +pub fn generate(base: Image, levels: u32, settings: Settings) -> Vec { + let native = settings.native_size; + if native.is_power_of_two() + && native < base.width + && base.width == base.height + && base.width.is_power_of_two() + { + let extra = (base.width / native).ilog2(); + let mut mips = + generate_native(base.resized(native), levels.saturating_sub(extra), settings); + let mut out: Vec<_> = (0..extra.min(levels + 1)) + .map(|level| mips[0].resized(base.width >> level)) + .collect(); + if levels >= extra { + out.append(&mut mips); + } + return out; + } + generate_native(base, levels, settings) +} + +fn generate_native(mut base: Image, levels: u32, settings: Settings) -> Vec { + let Settings { + strategy, + alpha_cutoff_bias, + has_transparent, + item, + .. + } = settings; let strategy = match strategy { Strategy::Auto if has_transparent => Strategy::Cutout, Strategy::Auto => Strategy::Mean, diff --git a/crates/horizon-client/src/sprites.rs b/crates/horizon-client/src/sprites.rs index e59a2b6c5..df0d82eec 100644 --- a/crates/horizon-client/src/sprites.rs +++ b/crates/horizon-client/src/sprites.rs @@ -223,6 +223,7 @@ impl Sprites { .and_then(|v| v.get("texture")) .and_then(|v| serde_json::from_str::(v.get()).ok()); self.mips.push(crate::mipmap::Settings { + native_size: side, strategy: texture .as_ref() .and_then(|t| t["mipmap_strategy"].as_str())