diff --git a/crates/horizon-client/src/font.rs b/crates/horizon-client/src/font.rs index aba0581c6..aeedb92bd 100644 --- a/crates/horizon-client/src/font.rs +++ b/crates/horizon-client/src/font.rs @@ -163,6 +163,17 @@ fn providers(root: &Path, id: &str, depth: u32, out: &mut Vec } } } +// FontTexture.add nudges UVs inside the glyph to avoid neighboring texels. +fn glyph_rect(x: u32, y: u32, w: u32, h: u32, atlas: (u32, u32)) -> Rect { + Rect { + u0: (x as f32 + 0.01) / atlas.0 as f32, + v0: (y as f32 + 0.01) / atlas.1 as f32, + u1: ((x + w) as f32 - 0.01) / atlas.0 as f32, + v1: ((y + h) as f32 - 0.01) / atlas.1 as f32, + w, + h, + } +} impl Font { /// Build from the atlas holding the bitmap font sheets. `pixels` is the /// RGBA atlas (for glyph widths) and `region` the reserved rows. @@ -237,14 +248,7 @@ impl Font { bitmap.insert( *ch, Glyph { - rect: Some(Rect { - u0: cx as f32 / atlas.0 as f32, - v0: cy as f32 / atlas.1 as f32, - u1: (cx + cell_w) as f32 / atlas.0 as f32, - v1: (cy + cell_h) as f32 / atlas.1 as f32, - w: cell_w, - h: cell_h, - }), + rect: Some(glyph_rect(cx, cy, cell_w, cell_h, atlas)), left: 0.0, top, right: cell_w as f32 * scale, @@ -414,14 +418,7 @@ impl Font { }); cache.cursor = (x + width + 1, y); Some(Glyph { - rect: Some(Rect { - u0: ax as f32 / self.atlas.0 as f32, - v0: ay as f32 / self.atlas.1 as f32, - u1: (ax + width) as f32 / self.atlas.0 as f32, - v1: (ay + 16) as f32 / self.atlas.1 as f32, - w: width, - h: 16, - }), + rect: Some(glyph_rect(ax, ay, width, 16, self.atlas)), left: 0.0, top: 0.0, right: width as f32 / 2.0, @@ -609,4 +606,16 @@ mod tests { parse_hex(b"zz:00\n", &mut out); assert_eq!(out.len(), 2); } + + #[test] + fn glyph_uvs_have_the_native_texel_inset() { + for (x, y, w, h) in [(120, 48, 8, 8), (0, 0, 1, 16), (4000, 2000, 32, 16)] { + let r = glyph_rect(x, y, w, h, (4096, 4096)); + assert_eq!((r.w, r.h), (w, h)); + assert!(r.u0 > x as f32 / 4096.0 && r.v0 > y as f32 / 4096.0); + assert!(r.u1 < (x + w) as f32 / 4096.0 && r.v1 < (y + h) as f32 / 4096.0); + assert_eq!(r.u0, (x as f32 + 0.01) / 4096.0); + assert_eq!(r.v1, ((y + h) as f32 - 0.01) / 4096.0); + } + } } diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 9650b306b..c12744a77 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -3293,10 +3293,8 @@ impl Game<'_> { batch } fn inventory_origin(&self, batch: &HudBatch) -> (f32, f32) { - ( - (batch.width - 176.0) / 2.0 + self.recipe_book_shift(batch.width), - (batch.height - self.screen_height()) / 2.0, - ) + let (x, y) = batch.centered_origin(176.0, self.screen_height()); + (x + self.recipe_book_shift(batch.width), y) } /// Slot ids: 0..36 player inventory, 100..109 crafting grid, 200 result. fn container_kind(&self) -> Option<&BlockEntity> { @@ -6889,10 +6887,7 @@ impl Game<'_> { } } fn creative_origin(batch: &HudBatch) -> (f32, f32) { - ( - (batch.width - Creative::WIDTH) / 2.0, - (batch.height - Creative::HEIGHT) / 2.0, - ) + batch.centered_origin(Creative::WIDTH, Creative::HEIGHT) } /// Vanilla tooltip: dark box with a purple gradient border. fn tooltip(&self, batch: &mut HudBatch, text: &str, x: f32, y: f32) { diff --git a/crates/horizon-client/src/hud.rs b/crates/horizon-client/src/hud.rs index d9c095115..6161160dd 100644 --- a/crates/horizon-client/src/hud.rs +++ b/crates/horizon-client/src/hud.rs @@ -307,6 +307,13 @@ impl HudBatch { patches: Vec::new(), } } + /// `AbstractContainerScreen.init`: center in integer GUI coordinates. + pub fn centered_origin(&self, width: f32, height: f32) -> (f32, f32) { + ( + ((self.width - width) / 2.0).trunc(), + ((self.height - height) / 2.0).trunc(), + ) + } pub fn quad(&mut self, p: [[f32; 2]; 4], uv: [[f32; 2]; 4], color: u32) { for i in [0, 1, 2, 0, 2, 3] { self.vertices.push(HudVertex { @@ -790,3 +797,31 @@ impl HudRenderer { pass.draw(0..count as u32, 0..1); } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn container_origins_use_java_integer_division() { + // Odd scaled dimensions, odd image sizes and an oversized panel. + for (width, height) in [(640, 360), (641, 361), (456, 256), (534, 300), (100, 100)] { + let batch = HudBatch { + vertices: Vec::new(), + scale: 3.0, + width: width as f32, + height: height as f32, + patches: Vec::new(), + }; + for (image_width, image_height) in [(195, 136), (176, 166), (176, 222)] { + assert_eq!( + batch.centered_origin(image_width as f32, image_height as f32), + ( + ((width - image_width) / 2) as f32, + ((height - image_height) / 2) as f32 + ) + ); + } + } + } +} diff --git a/crates/horizon-client/src/item_render/gui.rs b/crates/horizon-client/src/item_render/gui.rs index 930524dce..bbdb2cb02 100644 --- a/crates/horizon-client/src/item_render/gui.rs +++ b/crates/horizon-client/src/item_render/gui.rs @@ -247,12 +247,6 @@ pub fn banner_result_quads( sprites, &mut quads, ); - // The winding normal flips with the pose's mirrored axis; PoseStack's - // normal matrix only takes the scale's signs, so the lit normal is the - // opposite one. - for q in &mut quads { - q.normal = -q.normal; - } quads } diff --git a/crates/horizon-client/src/item_render/special.rs b/crates/horizon-client/src/item_render/special.rs index a36b5b118..d42378db1 100644 --- a/crates/horizon-client/src/item_render/special.rs +++ b/crates/horizon-client/src/item_render/special.rs @@ -101,6 +101,12 @@ fn draw( } else { normal }; + // A reflection reverses winding, not PoseStack normal directions. + let normal = if m.determinant() < 0.0 { + -normal + } else { + normal + }; sink.quad(ItemQuad { pos, uv: poly.map(|v| { @@ -621,4 +627,49 @@ mod tests { "entity/chest/christmas" ); } + + #[test] + fn model_normals_follow_the_pose_not_the_mirrored_winding() { + let part = ModelPart::parse(&serde_json::json!({ + "pose": [0, 0, 0, 0, 0, 0, 1, 1, 1], + "polygons": [[[0, 0, 0, 0, 0], [1, 0, -1, 1, 0], + [0, 1, -1, 1, 1], [-1, 1, 0, 0, 1]]], + "children": {} + })); + let sprite = Rect { + u0: 0.0, + v0: 0.0, + u1: 1.0, + v1: 1.0, + w: 16, + h: 16, + }; + let local = Vec3::ONE.normalize(); + for scale in [ + Vec3::ONE, + Vec3::new(16.0, -16.0, 16.0), + Vec3::new(2.0, 3.0, -4.0), + Vec3::new(-2.0, -3.0, 4.0), + ] { + let pose = Mat4::from_rotation_y(0.7) * Mat4::from_scale(scale); + let mut quads = Vec::new(); + draw( + &part, + "root", + pose, + &none, + sprite, + argb(-1), + Foil::None, + &mut quads, + ); + assert_eq!(quads.len(), 1); + let expected = super::super::transform_normal(pose, local); + assert!( + (quads[0].normal - expected).length() < 1e-6, + "scale {scale:?}: {:?} != {expected:?}", + quads[0].normal + ); + } + } }