Merge pull request #404 from Online5277/fix/rail-mipmap-cutout

Preserve native sprite mipmaps in mixed-size texture arrays
This commit is contained in:
Ludovic Marie
2026-10-16 00:37:42 +02:00
committed by GitHub
4 changed files with 104 additions and 23 deletions
@@ -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(' ');
+59 -4
View File
@@ -358,10 +358,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());
}
@@ -3001,6 +3001,61 @@ fn mix_frames(current: &mut [u8], next: &[u8], millis: u32) {
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);
+43 -11
View File
@@ -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<Image> {
/// 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<Image> {
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<Image> {
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,
+1
View File
@@ -252,6 +252,7 @@ impl Sprites {
.and_then(|v| v.get("texture"))
.and_then(|v| serde_json::from_str::<serde_json::Value>(v.get()).ok());
self.mips.push(crate::mipmap::Settings {
native_size: side,
strategy: texture
.as_ref()
.and_then(|t| t["mipmap_strategy"].as_str())