diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 03d0ab497..504cc4682 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -5564,9 +5564,10 @@ impl Game<'_> { let start = out.len(); self.block_model_vertices( f.state, - f.start_pos, + horizon_core::random::position_seed(f.start_pos), glam::Mat4::from_translation(o), packed, + false, &mut out, ); let tint = horizon_client::mobs::render::abgr(tint); diff --git a/crates/horizon-client/src/game/armor_stands.rs b/crates/horizon-client/src/game/armor_stands.rs index 78eb22330..df85b9e62 100644 --- a/crates/horizon-client/src/game/armor_stands.rs +++ b/crates/horizon-client/src/game/armor_stands.rs @@ -693,7 +693,7 @@ impl Game<'_> { } } /// `AbstractSkullBlock.getType` of a skull item. -fn skull_kind(item: &str) -> Option<&'static str> { +pub(super) fn skull_kind(item: &str) -> Option<&'static str> { Some(match item { "minecraft:skeleton_skull" => "skeleton", "minecraft:wither_skeleton_skull" => "wither_skeleton", diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 28c6f0996..8c7baa5b8 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -2740,6 +2740,7 @@ impl Game<'_> { { self.sulfur_cube_body_geometry( mob, + alpha as f32, horizon_client::mobs::render::base_transform(mob, alpha as f32, camera), packed, out, diff --git a/crates/horizon-client/src/game/minecarts.rs b/crates/horizon-client/src/game/minecarts.rs index 06d5466da..fd10b4562 100644 --- a/crates/horizon-client/src/game/minecarts.rs +++ b/crates/horizon-client/src/game/minecarts.rs @@ -17,7 +17,6 @@ use horizon_client::{ }; use horizon_core::{ geometry::{Aabb, Vec3}, - position::BlockPos, random::RandomSource, }; use horizon_world::registry::BlockStateId; @@ -1139,16 +1138,17 @@ impl Game<'_> { } } } - /// A block model's faces under `m` (block space 0..1), lit flat. + /// Baked block faces with terrain shade or block-item diffuse lighting. pub(super) fn block_model_vertices( &self, state: BlockStateId, - seed_pos: BlockPos, + seed: i64, m: glam::Mat4, light: u32, + diffuse: bool, out: &mut Vec, ) { - let Ok(models) = self.res.assets.model_catalog.collect(state, seed_pos) else { + let Ok(models) = self.res.assets.model_catalog.collect_seeded(state, seed) else { return; }; for (_, model) in &models { @@ -1168,12 +1168,30 @@ impl Game<'_> { D::North | D::South => 204, D::West | D::East => 153, }; - let color = 0xFF00_0000 | shade << 16 | shade << 8 | shade; + let color = if diffuse { + u32::MAX + } else { + 0xFF00_0000 | shade << 16 | shade << 8 | shade + }; + let normal = if diffuse { + use horizon_client::item_render::{direction_vector, transform_normal}; + let n = transform_normal(m, direction_vector(face.quad.facing)); + let n = n.to_array().map(|v| (v.clamp(-1.0, 1.0) * 127.0) as i8); + [n[0], n[1], n[2], 127] + } else { + [0; 4] + }; + let light = if diffuse { + let emission = u32::from(face.quad.light_emission) * 16; + (light & 0xFF) | (light >> 8 & 0xFF).max(emission) << 8 + } else { + light + }; for k in 0..4 { let p = face.quad.positions[k]; let uv = face.quad.uv[k]; out.push(Vertex { - normal: [0; 4], + normal, pos: m.transform_point3(glam::Vec3::from_array(p)).to_array(), uv: [s.u0 + uv[0] * (s.u1 - s.u0), s.v0 + uv[1] * (s.v1 - s.v0)], layer: 0, @@ -1297,7 +1315,7 @@ impl Game<'_> { * glam::Mat4::from_translation(glam::Vec3::splat(-0.5)); let flash = c.kind == CartKind::Tnt && c.fuse > -1 && (c.fuse / 5) % 2 == 0; let start = out.len(); - self.block_model_vertices(state, BlockPos::default(), m, packed, out); + self.block_model_vertices(state, 0, m, packed, false, out); if flash { // submitWhiteSolidBlock: OverlayTexture.u(1.0) on the cart's own light. for v in &mut out[start..] { diff --git a/crates/horizon-client/src/game/spawner.rs b/crates/horizon-client/src/game/spawner.rs index 0f9685e9b..1563d9459 100644 --- a/crates/horizon-client/src/game/spawner.rs +++ b/crates/horizon-client/src/game/spawner.rs @@ -413,6 +413,7 @@ impl Game<'_> { if mob.species() == horizon_client::mobs::Species::SulfurCube { self.sulfur_cube_body_geometry( mob, + alpha as f32, horizon_client::mobs::render::base_transform_at(mob, alpha as f32, origin), packed, out, diff --git a/crates/horizon-client/src/game/sulfur_cubes.rs b/crates/horizon-client/src/game/sulfur_cubes.rs index 6b3cc1b21..59c5f9ed0 100644 --- a/crates/horizon-client/src/game/sulfur_cubes.rs +++ b/crates/horizon-client/src/game/sulfur_cubes.rs @@ -8,11 +8,30 @@ use horizon_client::{ mesher::Vertex, mobs::{Mob, Species, sulfur_cube}, }; -use horizon_core::{position::BlockPos, random::RandomSource}; +use horizon_core::random::RandomSource; -fn block_transform(base: glam::Mat4) -> glam::Mat4 { +fn block_transform(base: glam::Mat4, baby: bool) -> glam::Mat4 { base * glam::Mat4::from_scale(glam::Vec3::new(1.0, -1.0, -1.0)) - * glam::Mat4::from_translation(glam::Vec3::splat(-0.5)) + * glam::Mat4::from_scale(glam::Vec3::splat(if baby { 0.5 } else { 1.0 })) + * glam::Mat4::from_translation(glam::Vec3::new(-0.5, -0.518, -0.5)) +} + +fn block_overlay(fuse_ticks: i32, alpha: f32, hurt: bool) -> u32 { + let fuse = if fuse_ticks >= 0 { + fuse_ticks as f32 - alpha + 1.0 + } else { + 0.0 + }; + if fuse > 0.0 && (fuse / 5.0) as i32 % 2 == 0 { + horizon_client::overlay::living(1.0, false) + } else { + horizon_client::overlay::living(0.0, hurt) + } +} + +fn block_light(light: u32, emission: u8) -> u32 { + let block = (light >> 8 & 0xFF).max(u32::from(emission) * 16); + (light & 0xFF) | block << 8 } impl Game<'_> { @@ -20,28 +39,34 @@ impl Game<'_> { pub(super) fn sulfur_cube_body_geometry( &self, mob: &Mob, + alpha: f32, base: glam::Mat4, light: u32, out: &mut Vec, ) { - let Some(state) = mob - .extra - .head - .as_deref() - .and_then(|item| self.res.items.get(item)) - .and_then(|item| item.block) - else { + let Some(item) = mob.extra.head.as_deref() else { + return; + }; + if super::armor_stands::skull_kind(item).is_some() { + return; + } + let Some(state) = self.res.items.get(item).and_then(|item| item.block) else { return; }; let start = out.len(); self.block_model_vertices( state, - BlockPos::default(), - block_transform(base), - light, + 42, + block_transform(base, mob.is_baby()), + block_light(light, self.res.facts.get(state).emission), + true, out, ); - let overlay = horizon_client::overlay::living(0.0, mob.hurt_time > 0 || mob.death_time > 0); + let overlay = block_overlay( + sulfur_cube::fuse(mob), + alpha, + mob.hurt_time > 0 || mob.death_time > 0, + ); if overlay != 0 { for vertex in &mut out[start..] { vertex.layer = overlay; @@ -225,73 +250,40 @@ mod tests { use super::*; #[test] - fn swallowed_block_occupies_the_default_cores_volume() { - use horizon_client::mobs::{EntityModelDocument, anim::Poses, render}; - let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) - .join("../../.local/horizon-client/26.3"); - if !root.join("entity-models.json").exists() { - return; - } - let model = EntityModelDocument::load(&root) - .unwrap() - .model("minecraft:sulfur_cube#inner") - .unwrap(); + fn swallowed_block_uses_vanilla_lift_and_baby_scale() { let base = glam::Mat4::from_translation(glam::Vec3::new(3.0, 4.0, -2.0)) * glam::Mat4::from_rotation_y(0.7) * glam::Mat4::from_scale(glam::Vec3::new(-1.0, -1.0, 1.0)); - let mut core = Vec::new(); - render::draw( - &model, - base, - &Poses::new(&model), - [0.0; 4], - u32::MAX, - 0, - 0, - &mut core, - ); - let block = block_transform(base); - let corners: Vec<_> = (0..8) - .map(|i| { - block.transform_point3(glam::Vec3::new( - (i & 1) as f32, - ((i >> 1) & 1) as f32, - ((i >> 2) & 1) as f32, - )) - }) - .collect(); - for axis in 0..3 { - let lo = core - .iter() - .map(|v| v.pos[axis]) - .fold(f32::INFINITY, f32::min); - let hi = core - .iter() - .map(|v| v.pos[axis]) - .fold(f32::NEG_INFINITY, f32::max); - assert!( - (corners - .iter() - .map(|p| p[axis]) - .fold(f32::INFINITY, f32::min) - - lo) - .abs() - < 1e-5 - ); - assert!( - (corners - .iter() - .map(|p| p[axis]) - .fold(f32::NEG_INFINITY, f32::max) - - hi) - .abs() - < 1e-5 - ); + for baby in [false, true] { + let size = if baby { 0.5 } else { 1.0 }; + let block = block_transform(base, baby); + for (point, expected) in [ + (glam::Vec3::ZERO, glam::Vec3::new(-0.5, 0.518, 0.5)), + (glam::Vec3::ONE, glam::Vec3::new(0.5, -0.482, -0.5)), + ] { + let actual = block.transform_point3(point); + let expected = base.transform_point3(expected * size); + assert!(actual.abs_diff_eq(expected, 1e-5), "baby={baby}"); + } + assert!(block.transform_vector3(glam::Vec3::Y).y > 0.0); } - let up = block.transform_vector3(glam::Vec3::Y); - assert!( - up.y > 0.0, - "block textures must stay upright inside the model" - ); + } + + #[test] + fn swallowed_tnt_flashes_white_over_hurt_using_the_interpolated_fuse() { + let white = horizon_client::overlay::WHITE + 15; + assert_eq!(block_overlay(-1, 0.0, false), 0); + assert_eq!(block_overlay(-1, 0.0, true), 1); + assert_eq!(block_overlay(9, 0.0, true), white); + assert_eq!(block_overlay(9, 0.5, false), 0); + assert_eq!(block_overlay(9, 0.5, true), 1); + assert_eq!(block_overlay(0, 0.5, false), white); + } + + #[test] + fn contained_block_emission_preserves_brighter_light_and_the_sky_channel() { + assert_eq!(block_light(0x2050, 1), 0x2050); + assert_eq!(block_light(0x2050, 7), 0x7050); + assert_eq!(block_light(0x0070, 0), 0x0070); } } diff --git a/crates/horizon-client/src/models/catalog.rs b/crates/horizon-client/src/models/catalog.rs index 59a885821..4b5d36d26 100644 --- a/crates/horizon-client/src/models/catalog.rs +++ b/crates/horizon-client/src/models/catalog.rs @@ -222,14 +222,20 @@ impl StateModels { &self, state: BlockStateId, position: BlockPos, + ) -> Result> { + // Blocks use their position seed; contained/displayed blocks can supply one. + self.collect_seeded(state, position_seed(position)) + } + pub fn collect_seeded( + &self, + state: BlockStateId, + seed: i64, ) -> Result> { let (choice, selected) = self .states .get(&state) .ok_or("unbound gallery block state")?; - // These inspection blocks use Block's default position seed. Blocks with - // specialized model seeds (e.g. two-block structures) need their callbacks. - let mut random = LegacyRandom::new(position_seed(position)); + let mut random = LegacyRandom::new(seed); let mut variants = Vec::new(); self.choices[*choice].collect(selected, &mut random, &mut variants)?; variants