From 7b21d4bc6c362adf6550ccd03bcc42f70a18de1a Mon Sep 17 00:00:00 2001 From: Cosmic <84875247+Online5277@users.noreply.github.com> Date: Tue, 13 Oct 2026 17:08:34 -0400 Subject: [PATCH] fix(client): restore directional lighting for mob models --- .../horizon-client/src/block_entity_render.rs | 4 + crates/horizon-client/src/entity.wgsl | 25 +- crates/horizon-client/src/game.rs | 10 + crates/horizon-client/src/game/creatures.rs | 2 + crates/horizon-client/src/game/dragon.rs | 3 + crates/horizon-client/src/game/end.rs | 4 + crates/horizon-client/src/game/fireworks.rs | 1 + crates/horizon-client/src/game/fishing.rs | 1 + crates/horizon-client/src/game/frames.rs | 1 + crates/horizon-client/src/game/leads.rs | 1 + crates/horizon-client/src/game/lightning.rs | 1 + crates/horizon-client/src/game/maps.rs | 3 + crates/horizon-client/src/game/minecarts.rs | 1 + crates/horizon-client/src/game/mob_blocks.rs | 1 + crates/horizon-client/src/game/names.rs | 1 + crates/horizon-client/src/game/paintings.rs | 1 + crates/horizon-client/src/game/tnt.rs | 1 + crates/horizon-client/src/gfx.rs | 14 +- crates/horizon-client/src/item_render.rs | 1 + crates/horizon-client/src/mesher.rs | 3 + crates/horizon-client/src/mesher/fluid.rs | 1 + crates/horizon-client/src/mobs.rs | 246 +++++++++++++++++- crates/horizon-client/src/mobs/guardian.rs | 1 + crates/horizon-client/src/mobs/render.rs | 37 ++- crates/horizon-client/src/particles.rs | 1 + crates/horizon-client/src/shadows.rs | 1 + .../parity/VanillaEntityModelExporter.java | 7 + 27 files changed, 361 insertions(+), 12 deletions(-) diff --git a/crates/horizon-client/src/block_entity_render.rs b/crates/horizon-client/src/block_entity_render.rs index 399527b78..9ffd888a1 100644 --- a/crates/horizon-client/src/block_entity_render.rs +++ b/crates/horizon-client/src/block_entity_render.rs @@ -143,6 +143,7 @@ pub fn draw_part( let color = pack_color(tint, 255, diffuse(normal, lights)); for (v, position) in poly.iter().zip(pos) { out.push(Vertex { + normal: [0; 4], pos: position.to_array(), uv: [r.u0 + (r.u1 - r.u0) * v[3], r.v0 + (r.v1 - r.v0) * v[4]], layer: 0, @@ -637,6 +638,7 @@ fn draw_text(scene: &Scene, call: &TextCall, out: &mut Vec) { 0xFF00_0000 | (argb & 255) << 16 | (argb >> 8 & 255) << 8 | (argb >> 16 & 255); for ((cx, cy), uv) in corners.into_iter().zip(uvs) { out.push(Vertex { + normal: [0; 4], pos: call .matrix .transform_point3(Vec3::new(cx, cy, 0.0)) @@ -1587,6 +1589,7 @@ impl BeamPart { (at(b, self.end), [s.u0, v1]), ] { out.push(Vertex { + normal: [0; 4], pos: p.to_array(), uv, layer: 6, @@ -1656,6 +1659,7 @@ fn portal_cube(m: Mat4, faces: &[Direction], layer: u32, out: &mut Vec) for &d in faces { for c in PORTAL_FACES[d as usize] { out.push(Vertex { + normal: [0; 4], pos: m.transform_point3(Vec3::from(c)).to_array(), uv: [0.0; 2], layer, diff --git a/crates/horizon-client/src/entity.wgsl b/crates/horizon-client/src/entity.wgsl index 8a8a598f7..c4e737ca8 100644 --- a/crates/horizon-client/src/entity.wgsl +++ b/crates/horizon-client/src/entity.wgsl @@ -14,6 +14,7 @@ struct Globals { // Atlas rects (u0, v0, width, height) of end_sky.png and end_portal.png. end_portal_sprites: array, 2>, // x: vanilla's GameTime ((game time % 24000 + partial tick) / 24000) + // y: Nether cardinal lighting. time: vec4, } @group(0) @binding(0) var globals: Globals; @@ -33,9 +34,16 @@ struct Out { @location(6) @interpolate(flat) wrap: vec2, // Overlays 9 and 10: projection_from_position(gl_Position), the End portal's texProj0. @location(7) proj: vec4, + @location(8) back_color: vec4, +} +// Shipped light.glsl; normals are already transformed and RGBA8_SNORM packed. +fn entity_light(normal: vec3, nether: bool) -> f32 { + let l0 = normalize(vec3(0.2, 1.0, -0.7)); + let l1 = normalize(vec3(-0.2, select(1.0, -1.0, nether), 0.7)); + return min(1.0, (max(0.0, dot(l0, normal)) + max(0.0, dot(l1, normal))) * 0.6 + 0.4); } @vertex fn vs_entity(@location(0) position: vec3, @location(1) uv: vec2, - @location(2) overlay: u32, @location(3) color: vec4, @location(4) light: vec4) -> Out { + @location(2) overlay: u32, @location(3) color: vec4, @location(4) light: vec4, @location(5) normal: vec4) -> Out { var out: Out; out.clip = globals.view_proj * vec4(position, 1.0); out.proj = vec4(out.clip.xy * 0.5 + out.clip.w * 0.5, out.clip.zw); @@ -47,9 +55,18 @@ struct Out { var c = select(color * lm, color, overlay == 2u || overlay == 4u || overlay == 6u); // Overlay 6 (beacon beams): the light bytes carry the sprite's v range. out.wrap = vec2(f32(light.x | (light.y << 8u)), f32(light.z | (light.w << 8u))) / 65535.0; + var back = c; + if normal.w != 0.0 { + c = vec4(c.rgb * entity_light(normal.xyz, globals.time.y != 0.0), c.a); + back = select(c, vec4(back.rgb * entity_light(-normal.xyz, globals.time.y != 0.0), back.a), normal.w < 0.0); + } // Hurt overlay (OverlayTexture red tint). - if overlay == 1u || overlay == 8u { c = vec4(mix(c.rgb, vec3(1.0, 0.0, 0.0), 0.3), c.a); } + if overlay == 1u || overlay == 8u { + c = vec4(mix(c.rgb, vec3(1.0, 0.0, 0.0), 0.3), c.a); + back = vec4(mix(back.rgb, vec3(1.0, 0.0, 0.0), 0.3), back.a); + } out.color = c; + out.back_color = back; // Overlay 5: OverlayTexture white flash (primed TNT), applied after texturing. out.flash = select(0.0, 0.75, overlay == 5u); // Sky geometry (clouds, celestial bodies) is exempt from render-distance fog. @@ -99,10 +116,10 @@ fn linear_fog(d: f32, start: f32, end: f32) -> f32 { if d >= end { return 1.0; } return (d - start) / (end - start); } -@fragment fn fs_entity(in: Out) -> @location(0) vec4 { +@fragment fn fs_entity(in: Out, @builtin(front_facing) front: bool) -> @location(0) vec4 { var uv = in.uv; if in.overlay == 6u { uv.y = in.wrap.x + fract(in.uv.y) * in.wrap.y; } - var color = textureSample(atlas, atlas_sampler, uv) * in.color; + var color = textureSample(atlas, atlas_sampler, uv) * select(in.back_color, in.color, front); if in.overlay == 9u || in.overlay == 10u { // rendertype_end_portal: end_sky and PORTAL_LAYERS (15 for portals, 16 // for gateways) layers of end_portal.png projected from the screen. diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index de855dd31..d70532b00 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -4333,6 +4333,7 @@ impl Game<'_> { .zip([[r.u0, v1], [u1, v1], [u1, r.v0], [r.u0, r.v0]]) { out.push(Vertex { + normal: [0; 4], pos: v.to_array(), uv, layer: 0, @@ -4494,6 +4495,7 @@ impl Game<'_> { { let v = c + right * (k.0 * s) + up * (k.1 * s); out.push(Vertex { + normal: [0; 4], pos: v.to_array(), uv, layer: 0, @@ -4559,6 +4561,7 @@ impl Game<'_> { .zip([[r.u0, r.v1], [r.u1, r.v1], [r.u1, r.v0], [r.u0, r.v0]]) { out.push(Vertex { + normal: [0; 4], pos: p.to_array(), uv, layer: 0, @@ -4658,6 +4661,7 @@ impl Game<'_> { .zip([[r.u0, r.v1], [r.u1, r.v1], [r.u1, r.v0], [r.u0, r.v0]]) { out.push(Vertex { + normal: [0; 4], pos: v.to_array(), uv, layer: 0, @@ -4981,6 +4985,7 @@ impl Game<'_> { for ((x, z), uv) in corners.iter().zip(uv) { let p = m.transform_point3(glam::Vec3::new(*x, 100.0, *z)); out.push(Vertex { + normal: [0; 4], pos: p.to_array(), uv, layer: 2, @@ -5040,6 +5045,7 @@ impl Game<'_> { for (a, b) in [(size, -size), (size, size), (-size, size), (-size, -size)] { let p = m.transform_point3(center + r2 * a + u2 * b); out.push(Vertex { + normal: [0; 4], pos: p.to_array(), uv: [cu, cv], layer: 2, @@ -5102,6 +5108,7 @@ impl Game<'_> { for v in p { // Overlay 4: clouds (unlit, faded by the cloud fog end). out.push(Vertex { + normal: [0; 4], pos: v, uv, layer: 4, @@ -5780,6 +5787,7 @@ impl Game<'_> { let uv = [[r.u0, r.v0], [r.u1, r.v0], [r.u1, r.v1], [r.u0, r.v1]]; for (p, uv) in corners.iter().zip(uv) { out.push(Vertex { + normal: [0; 4], pos: p.to_array(), uv, layer: 2, @@ -6754,6 +6762,7 @@ impl Game<'_> { let uvs = [[r.u0, uv1], [r.u1, uv1], [r.u1, uv0], [r.u0, uv0]]; for (p, uv) in corners.iter().zip(uvs) { out.push(Vertex { + normal: [0; 4], pos: p.to_array(), uv, layer: 0, @@ -7370,6 +7379,7 @@ impl Game<'_> { .then(|| self.border_draw(camera.to_array(), alpha as f32)) .flatten(); let frame = Frame { + nether_lighting: self.dim == Dim::Nether, camera, yaw, pitch, diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 07cdfe6ac..83205805a 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -2407,6 +2407,7 @@ impl Game<'_> { .zip([[r.u0, v1], [u1, v1], [u1, r.v0], [r.u0, r.v0]]) { out.push(Vertex { + normal: [0; 4], pos: v.to_array(), uv, layer: 0, @@ -2472,6 +2473,7 @@ impl Game<'_> { { let v = c + right * k.0 * s + up * k.1 * s; out.push(Vertex { + normal: [0; 4], pos: v.to_array(), uv, layer: 0, diff --git a/crates/horizon-client/src/game/dragon.rs b/crates/horizon-client/src/game/dragon.rs index bbee16a9f..a1cf7bfc2 100644 --- a/crates/horizon-client/src/game/dragon.rs +++ b/crates/horizon-client/src/game/dragon.rs @@ -2109,6 +2109,7 @@ impl Game<'_> { // A triangle as a quad with its last corner repeated. for (p, c) in [(o, inner), (p1, outer), (p2, outer), (p2, outer)] { out.push(Vertex { + normal: [0; 4], pos: m.transform_point3(p).to_array(), uv, layer: 2, @@ -2173,6 +2174,7 @@ impl Game<'_> { ), ] { out.push(Vertex { + normal: [0; 4], pos: m.transform_point3(p).to_array(), uv, layer: 6, @@ -2217,6 +2219,7 @@ impl Game<'_> { (p - right * h + up * h, [s.u0, s.v0]), ] { out.push(Vertex { + normal: [0; 4], pos: c.as_vec3().to_array(), uv, layer: 0, diff --git a/crates/horizon-client/src/game/end.rs b/crates/horizon-client/src/game/end.rs index 163c76e73..b7366a03e 100644 --- a/crates/horizon-client/src/game/end.rs +++ b/crates/horizon-client/src/game/end.rs @@ -169,6 +169,7 @@ impl Game<'_> { for (x, z, uv) in corners { let p = pose.transform_point3(glam::Vec3::new(x, -100.0, z)); out.push(Vertex { + normal: [0; 4], pos: p.to_array(), uv, layer: 2, @@ -203,6 +204,7 @@ impl Game<'_> { for (x, z, uv) in corners { let p = pose.transform_point3(glam::Vec3::new(x * 60.0, 100.0, z * 60.0)); out.push(Vertex { + normal: [0; 4], pos: p.to_array(), uv, layer: 2, @@ -990,6 +992,7 @@ impl Game<'_> { ]; for (pos, uv) in quad { out.push(Vertex { + normal: [0; 4], pos: pos.to_array(), uv, layer: 0, @@ -1127,6 +1130,7 @@ impl Game<'_> { ]; for (pos, uv) in corners { out.push(Vertex { + normal: [0; 4], pos: pos.to_array(), uv, layer: 0, diff --git a/crates/horizon-client/src/game/fireworks.rs b/crates/horizon-client/src/game/fireworks.rs index 80b72ad26..60fca288e 100644 --- a/crates/horizon-client/src/game/fireworks.rs +++ b/crates/horizon-client/src/game/fireworks.rs @@ -272,6 +272,7 @@ impl Game<'_> { .zip([[s.u0, s.v1], [s.u1, s.v1], [s.u1, s.v0], [s.u0, s.v0]]) { out.push(Vertex { + normal: [0; 4], pos: v.as_vec3().to_array(), uv, layer: 0, diff --git a/crates/horizon-client/src/game/fishing.rs b/crates/horizon-client/src/game/fishing.rs index 3c628a29b..a551d8e92 100644 --- a/crates/horizon-client/src/game/fishing.rs +++ b/crates/horizon-client/src/game/fishing.rs @@ -485,6 +485,7 @@ impl Game<'_> { let mut quad = |c: [glam::DVec3; 4], uv: [[f32; 2]; 4], color: u32| { for (v, uv) in c.iter().zip(uv) { out.push(Vertex { + normal: [0; 4], pos: (*v - camera).as_vec3().to_array(), uv, layer: 0, diff --git a/crates/horizon-client/src/game/frames.rs b/crates/horizon-client/src/game/frames.rs index 5d80a424b..773fd2193 100644 --- a/crates/horizon-client/src/game/frames.rs +++ b/crates/horizon-client/src/game/frames.rs @@ -333,6 +333,7 @@ impl Game<'_> { for (c, uv) in corners.iter().zip(uv) { let p = m.transform_point3(glam::DVec3::from_array(*c)); out.push(Vertex { + normal: [0; 4], pos: p.as_vec3().to_array(), uv, layer: 0, diff --git a/crates/horizon-client/src/game/leads.rs b/crates/horizon-client/src/game/leads.rs index 699b362f8..c9e3627e3 100644 --- a/crates/horizon-client/src/game/leads.rs +++ b/crates/horizon-client/src/game/leads.rs @@ -373,6 +373,7 @@ impl Game<'_> { for quad in [[a0, b0, b1, a1], [a1, b1, b0, a0]] { for (v, uv) in quad.iter().zip(uv) { out.push(Vertex { + normal: [0; 4], pos: (*v - camera).as_vec3().to_array(), uv, layer: 0, diff --git a/crates/horizon-client/src/game/lightning.rs b/crates/horizon-client/src/game/lightning.rs index 13ea88317..f85bebd28 100644 --- a/crates/horizon-client/src/game/lightning.rs +++ b/crates/horizon-client/src/game/lightning.rs @@ -385,6 +385,7 @@ impl Game<'_> { ]; for c in corners { out.push(Vertex { + normal: [0; 4], pos: (o + glam::Vec3::from_array(c)).to_array(), uv, layer: 2, diff --git a/crates/horizon-client/src/game/maps.rs b/crates/horizon-client/src/game/maps.rs index 9cc2add80..d5e800b0e 100644 --- a/crates/horizon-client/src/game/maps.rs +++ b/crates/horizon-client/src/game/maps.rs @@ -514,6 +514,7 @@ impl Game<'_> { let quad = |corners: [[f32; 3]; 4], uv: [[f32; 2]; 4], out: &mut Vec| { for (c, uv) in corners.into_iter().zip(uv) { out.push(Vertex { + normal: [0; 4], pos: m.transform_point3(glam::Vec3::from_array(c)).to_array(), uv, layer: 0, @@ -560,6 +561,7 @@ impl Game<'_> { let mut q = |corners: [[f32; 3]; 4], uv: [[f32; 2]; 4]| { for (c, uv) in corners.into_iter().zip(uv) { out.push(Vertex { + normal: [0; 4], pos: m.transform_point3(glam::Vec3::from_array(c)).to_array(), uv, layer: 0, @@ -628,6 +630,7 @@ impl Game<'_> { ([-7.0, -7.0, 0.0], [r.u0, r.v0]), ] { out.push(Vertex { + normal: [0; 4], pos: m.transform_point3(glam::Vec3::from_array(c)).to_array(), uv, layer: 0, diff --git a/crates/horizon-client/src/game/minecarts.rs b/crates/horizon-client/src/game/minecarts.rs index 87b074043..15a6b72b7 100644 --- a/crates/horizon-client/src/game/minecarts.rs +++ b/crates/horizon-client/src/game/minecarts.rs @@ -1113,6 +1113,7 @@ impl Game<'_> { let p = face.quad.positions[k]; let uv = face.quad.uv[k]; out.push(Vertex { + normal: [0; 4], 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, diff --git a/crates/horizon-client/src/game/mob_blocks.rs b/crates/horizon-client/src/game/mob_blocks.rs index d2fe1f8af..52ea5df43 100644 --- a/crates/horizon-client/src/game/mob_blocks.rs +++ b/crates/horizon-client/src/game/mob_blocks.rs @@ -197,6 +197,7 @@ impl Game<'_> { for (corner, uv) in corners.iter().zip(uvs) { let p = t.transform_point3(glam::Vec3::from(*corner)); out.push(Vertex { + normal: [0; 4], pos: p.to_array(), uv, layer: overlay, diff --git a/crates/horizon-client/src/game/names.rs b/crates/horizon-client/src/game/names.rs index 224ce17e0..1b39f051f 100644 --- a/crates/horizon-client/src/game/names.rs +++ b/crates/horizon-client/src/game/names.rs @@ -154,6 +154,7 @@ impl Game<'_> { ]; for ((x, y), uv) in corners.into_iter().zip(uvs) { out.push(Vertex { + normal: [0; 4], pos: (at(x, y) + if background { away } else { glam::Vec3::ZERO }) .to_array(), uv, diff --git a/crates/horizon-client/src/game/paintings.rs b/crates/horizon-client/src/game/paintings.rs index 2c078c11e..9e50a91eb 100644 --- a/crates/horizon-client/src/game/paintings.rs +++ b/crates/horizon-client/src/game/paintings.rs @@ -283,6 +283,7 @@ impl Game<'_> { let color = 0xFF00_0000 | k << 16 | k << 8 | k; for (p, uv) in corners.iter().zip(uv) { out.push(Vertex { + normal: [0; 4], pos: p.as_vec3().to_array(), uv, layer: 0, diff --git a/crates/horizon-client/src/game/tnt.rs b/crates/horizon-client/src/game/tnt.rs index 71b88cc9b..a7f3fde46 100644 --- a/crates/horizon-client/src/game/tnt.rs +++ b/crates/horizon-client/src/game/tnt.rs @@ -256,6 +256,7 @@ impl Game<'_> { { // Overlay 5: OverlayTexture white flash. out.push(Vertex { + normal: [0; 4], pos: p.to_array(), uv, layer: if lit { 5 } else { 0 }, diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 6aa0ae406..5ee4ad082 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -33,7 +33,7 @@ struct Globals { fog_ends: [f32; 4], /// Atlas rects (u0, v0, width, height) of end_sky.png and end_portal.png. end_portal_sprites: [[f32; 4]; 2], - /// x: vanilla's `GameTime` uniform; yzw unused. + /// x: vanilla's `GameTime` uniform; y: nether cardinal lighting, zw unused. time: [f32; 4], } pub type SectionKey = (i32, i32, i32); @@ -79,6 +79,8 @@ pub struct Frame<'a> { /// The hit block outline as line segments (from, to) in world space. pub outline: Vec<[f64; 6]>, pub hud: &'a HudBatch, + /// CardinalLighting.Type.NETHER, otherwise DEFAULT. + pub nether_lighting: bool, /// Camera-relative entity geometry textured from the shared atlas. pub entities: &'a [Vertex], /// Additive translucent geometry after the entities (lightning, dragon rays). @@ -803,10 +805,11 @@ impl Renderer { bind_group_layouts: &[Some(&entity_bindings)], ..Default::default() }); + let entity_attributes = wgpu::vertex_attr_array![0=>Float32x3,1=>Float32x2,2=>Uint32,3=>Unorm8x4,4=>Uint8x4,5=>Snorm8x4]; let entity_vertex = [Some(wgpu::VertexBufferLayout { array_stride: std::mem::size_of::() as u64, step_mode: wgpu::VertexStepMode::Vertex, - attributes: &vertex_attributes, + attributes: &entity_attributes, })]; let entity_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some("Entities"), @@ -1766,7 +1769,12 @@ impl Renderer { 0.0, ], end_portal_sprites: frame.end_portal_sprites, - time: [frame.shader_game_time, 0.0, 0.0, 0.0], + time: [ + frame.shader_game_time, + f32::from(frame.nether_lighting), + 0.0, + 0.0, + ], }; self.queue .write_buffer(&self.globals, 0, bytemuck::bytes_of(&globals)); diff --git a/crates/horizon-client/src/item_render.rs b/crates/horizon-client/src/item_render.rs index bd5c47179..66246e4a9 100644 --- a/crates/horizon-client/src/item_render.rs +++ b/crates/horizon-client/src/item_render.rs @@ -724,6 +724,7 @@ impl ItemSink for WorldSink<'_> { let overlay = if q.foil == Foil::None { 0 } else { 7 }; for (p, uv) in q.pos.iter().zip(q.uv) { self.out.push(Vertex { + normal: [0; 4], pos: p.to_array(), uv, layer: overlay, diff --git a/crates/horizon-client/src/mesher.rs b/crates/horizon-client/src/mesher.rs index ee296269b..41c1ca4a0 100644 --- a/crates/horizon-client/src/mesher.rs +++ b/crates/horizon-client/src/mesher.rs @@ -33,6 +33,8 @@ pub struct Vertex { pub color: u32, /// Smooth light, 16x fixed point: sky in bits 0..8, block in bits 8..16. pub light: u32, + /// RGBA8_SNORM face normal; w = -127 for two-sided lighting, 0 to keep baked/unlit colour. + pub normal: [i8; 4], } pub struct SectionMesh { pub solid: Vec, @@ -722,6 +724,7 @@ impl Mesher<'_> { }; let p = q.positions[i]; target.push(Vertex { + normal: [0; 4], pos: [p[0] + origin[0], p[1] + origin[1], p[2] + origin[2]], uv: q.uv[i], // Opaque leaves (forceOpaque) draw in the solid layer: no alpha cutout. diff --git a/crates/horizon-client/src/mesher/fluid.rs b/crates/horizon-client/src/mesher/fluid.rs index e555e12ca..a1940bf4d 100644 --- a/crates/horizon-client/src/mesher/fluid.rs +++ b/crates/horizon-client/src/mesher/fluid.rs @@ -305,6 +305,7 @@ pub(super) fn tesselate( let mut quad = |v: [([f32; 3], [f32; 2]); 4], layer: u32, color: u32, light: u32, back: bool| { let vertex = |(pos, uv): ([f32; 3], [f32; 2])| Vertex { + normal: [0; 4], pos, uv, layer, diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 57f17e178..f8ebca55c 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -82,8 +82,107 @@ use std::{collections::HashMap, fs, path::Path, sync::Arc}; #[derive(Clone, Debug)] pub struct Polygon { + pub normal: [f32; 3], pub vertices: [[f32; 5]; 4], } +impl Polygon { + fn vertex_normal(&self, pose: ModelNormals) -> [i8; 4] { + let n = pose.matrix * glam::Vec3::from_array(self.normal); + let n = if pose.trusted { + n + } else { + n.normalize_or_zero() + }; + let packed = n.to_array().map(|v| (v.clamp(-1.0, 1.0) * 127.0) as i8); + [packed[0], packed[1], packed[2], -127] + } +} + +/// ModelPart uses a separate normal stack: uniform scaling keeps normal lengths, +/// while nonuniform scaling requires normalization after the complete part transform. +#[derive(Clone, Copy)] +struct ModelNormals { + matrix: glam::Mat3, + trusted: bool, +} +impl ModelNormals { + // shortcut: infer the root normal pose from its geometry matrix; pass an explicit + // root normal stack when root-transform rounding is ported as well. + fn new(base: glam::Mat4) -> Self { + let m = glam::Mat3::from_mat4(base); + let lengths = glam::Vec3::new(m.x_axis.length(), m.y_axis.length(), m.z_axis.length()); + let uniform = + lengths.min_element() > 0.0 && lengths.max_element() - lengths.min_element() < 1.0e-6; + Self { + matrix: if uniform { + m / lengths.x + } else { + m.inverse().transpose() + }, + trusted: uniform, + } + } + fn rotation(x: f32, y: f32, z: f32) -> glam::Mat3 { + // JOML derives cosine from the rounded sine, then retains quaternion length. + // Assuming an exactly unit quaternion changes a packed normal by one byte. + let sin_cos = |angle: f32| { + let angle = angle * 0.5; + let sin = f64::from(angle).sin() as f32; + let cos = (1.0 - sin * sin).sqrt(); + let a = angle + std::f32::consts::FRAC_PI_2; + let tau = std::f32::consts::TAU; + let mut quadrant = a - (a / tau) as i32 as f32 * tau; + if quadrant < 0.0 { + quadrant += tau; + } + ( + sin, + if quadrant >= std::f32::consts::PI { + -cos + } else { + cos + }, + ) + }; + let (sx, cx) = sin_cos(x); + let (sy, cy) = sin_cos(y); + let (sz, cz) = sin_cos(z); + let (cycz, sysz, sycz, cysz) = (cy * cz, sy * sz, sy * cz, cy * sz); + let (w, x, y, z) = ( + cx * cycz + sx * sysz, + sx * cycz - cx * sysz, + cx * sycz + sx * cysz, + cx * cysz - sx * sycz, + ); + let (ww, xx, yy, zz) = (w * w, x * x, y * y, z * z); + let (xy, xz, xw, yz, yw, zw) = (x * y, x * z, x * w, y * z, y * w, z * w); + glam::Mat3::from_cols_array(&[ + ww + xx - zz - yy, + (xy + xy) + (zw + zw), + (xz + xz) - (yw + yw), + (xy + xy) - (zw + zw), + yy - zz + ww - xx, + (yz + yz) + (xw + xw), + (yw + yw) + (xz + xz), + (yz + yz) - (xw + xw), + zz - yy - xx + ww, + ]) + } + fn part(mut self, pose: [f32; 9]) -> Self { + self.matrix *= Self::rotation(pose[3], pose[4], pose[5]); + let scale = glam::Vec3::new(pose[6], pose[7], pose[8]); + if scale.x.abs() == scale.y.abs() && scale.x.abs() == scale.z.abs() { + if scale.min_element() < 0.0 { + self.matrix *= glam::Mat3::from_diagonal(scale.signum()); + } + } else { + self.matrix *= glam::Mat3::from_diagonal(scale.recip()); + self.trusted = false; + } + self + } +} + #[derive(Clone, Debug)] pub struct Part { pub name: String, @@ -108,7 +207,8 @@ impl Part { .as_array() .into_iter() .flatten() - .filter_map(|poly| { + .enumerate() + .filter_map(|(index, poly)| { let verts: Vec<[f32; 5]> = poly .as_array()? .iter() @@ -123,6 +223,20 @@ impl Part { }) .collect(); (verts.len() == 4).then(|| Polygon { + normal: v["normals"][index].as_array().map_or_else( + || { + // shortcut: collapsed legacy faces have no recoverable normal; re-prepare resources. + let p = verts + .iter() + .map(|v| glam::Vec3::new(v[0], v[1], v[2])) + .collect::>(); + (p[1] - p[0]) + .cross(p[2] - p[0]) + .normalize_or_zero() + .to_array() + }, + |n| std::array::from_fn(|i| n[i].as_f64().unwrap_or(0.0) as f32), + ), vertices: [verts[0], verts[1], verts[2], verts[3]], }) }) @@ -1584,10 +1698,20 @@ pub fn render_model( overlay: u32, out: &mut Vec, ) { - visit(part, base, pose_for, uv, light, overlay, out); + visit( + part, + base, + ModelNormals::new(base), + pose_for, + uv, + light, + overlay, + out, + ); fn visit( part: &Part, parent: glam::Mat4, + normals: ModelNormals, pose_for: &dyn Fn(&Part) -> [f32; 9], uv: [f32; 4], light: u32, @@ -1604,10 +1728,13 @@ pub fn render_model( p[3], )) * glam::Mat4::from_scale(glam::Vec3::new(p[6], p[7], p[8])); + let normals = normals.part(p); for poly in &part.polygons { + let normal = poly.vertex_normal(normals); for v in poly.vertices { let pos = m.transform_point3(glam::Vec3::new(v[0], v[1], v[2]) / 16.0); out.push(Vertex { + normal, pos: pos.to_array(), uv: [ uv[0] + (uv[2] - uv[0]) * v[3], @@ -1620,7 +1747,7 @@ pub fn render_model( } } for c in &part.children { - visit(c, m, pose_for, uv, light, overlay, out); + visit(c, m, normals, pose_for, uv, light, overlay, out); } } } @@ -1645,3 +1772,116 @@ impl Mob { .filter(|s| s.damage != 0 || s.extra.is_some() || &*s.item != "minecraft:saddle"); } } + +#[cfg(test)] +mod lighting_tests { + use super::*; + use glam::{Mat4, Vec3}; + use serde_json::json; + + fn plane(normal: [f32; 3]) -> Part { + Part::parse( + "root", + &json!({ + "polygons": [[[0,0,0,0,0], [0,0,0,1,0], [0,0,0,1,1], [0,0,0,0,1]]], + "normals": [normal] + }), + ) + } + + #[test] + fn exported_normals_survive_collapsed_faces_and_mirroring() { + let part = plane([1.0, 0.0, 0.0]); + assert_eq!( + part.polygons[0].vertex_normal(ModelNormals::new(Mat4::IDENTITY)), + [127, 0, 0, -127] + ); + assert_eq!( + part.polygons[0].vertex_normal(ModelNormals::new(Mat4::from_scale(Vec3::new( + -1.0, -1.0, 1.0 + )))), + [-127, 0, 0, -127] + ); + } + + #[test] + fn normal_matrix_includes_parent_scale_and_child_rotation() { + let mut child = plane([1.0, 0.0, 0.0]); + child.pose[5] = std::f32::consts::FRAC_PI_4; + let parent = Part { + name: "parent".into(), + pose: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 1.0, 0.5], + polygons: Vec::new(), + children: vec![child], + }; + let mut animated = Vec::new(); + render::draw( + &parent, + Mat4::IDENTITY, + &anim::Poses::new(&parent), + [0.0, 0.0, 1.0, 1.0], + 0x80402010, + 240, + 1, + &mut animated, + ); + let mut direct = Vec::new(); + render_model( + &parent, + Mat4::IDENTITY, + &|p| p.pose, + [0.0, 0.0, 1.0, 1.0], + 240, + 1, + &mut direct, + ); + assert_eq!(animated.len(), 4); + for (a, b) in animated.iter().zip(&direct) { + assert_eq!(a.normal, [56, 113, 0, -127]); + assert_eq!(a.normal, b.normal); + assert_eq!(a.pos, b.pos); + assert_eq!(a.uv, b.uv); + assert_eq!((a.color, a.light, a.layer), (0x80402010, 240, 1)); + } + } + + #[test] + fn trusted_rotation_retains_native_quaternion_rounding() { + let mut part = plane([1.0, 0.0, 0.0]); + part.pose[3] = std::f32::consts::FRAC_PI_2; + let mut vertices = Vec::new(); + render::draw( + &part, + Mat4::IDENTITY, + &anim::Poses::new(&part), + [0.0, 0.0, 1.0, 1.0], + 0xffffffff, + 240, + 0, + &mut vertices, + ); + assert_eq!(vertices[0].normal, [126, 0, 0, -127]); + } + + #[test] + fn legacy_models_infer_non_degenerate_face_normals() { + let part = Part::parse( + "root", + &json!({"polygons": [ + [[0,0,0,0,0], [1,0,0,1,0], [1,1,0,1,1], [0,1,0,0,1]] + ]}), + ); + assert_eq!(part.polygons[0].normal, [0.0, 0.0, 1.0]); + } + + #[test] + fn packed_normal_uses_native_snorm_bytes_without_changing_vertex_offsets() { + assert_eq!(std::mem::offset_of!(Vertex, normal), 32); + assert_eq!(std::mem::size_of::(), 36); + let part = plane([0.0, 1.0, 0.0]); + assert_eq!( + part.polygons[0].vertex_normal(ModelNormals::new(Mat4::from_scale(Vec3::ZERO))), + [0, 0, 0, -127] + ); + } +} diff --git a/crates/horizon-client/src/mobs/guardian.rs b/crates/horizon-client/src/mobs/guardian.rs index 3b773e001..5eb72a6e4 100644 --- a/crates/horizon-client/src/mobs/guardian.rs +++ b/crates/horizon-client/src/mobs/guardian.rs @@ -382,6 +382,7 @@ pub fn beam_geometry( let atlas = |u: f32, v: f32| [uv[0] + (uv[2] - uv[0]) * u, uv[1] + (uv[3] - uv[1]) * v]; let mut push = |p: [f32; 3], t: [f32; 2]| { out.push(crate::mesher::Vertex { + normal: [0; 4], pos: pose.transform_point3(glam::Vec3::from(p)).to_array(), uv: t, layer: 0, diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index cc8b24ead..ef45ffb46 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -3,7 +3,7 @@ //! scale, and extra layers (sheep wool, spider eyes, drowned/stray outer //! layers, villager overlays, collars...). use super::{ - Mob, MobData, Part, Species, + Mob, MobData, ModelNormals, Part, Species, anim::{self, Poses}, living::wrap, }; @@ -810,6 +810,30 @@ pub fn draw( light: u32, overlay: u32, out: &mut Vec, +) { + draw_normals( + part, + base, + ModelNormals::new(base), + poses, + uv, + color, + light, + overlay, + out, + ); +} +#[allow(clippy::too_many_arguments)] +fn draw_normals( + part: &Part, + base: glam::Mat4, + normals: ModelNormals, + poses: &Poses, + uv: [f32; 4], + color: u32, + light: u32, + overlay: u32, + out: &mut Vec, ) { let p = poses.pose(part); let m = base @@ -824,10 +848,13 @@ pub fn draw( if poses.is_hidden(part) || p[6] == 0.0 && p[7] == 0.0 && p[8] == 0.0 { return; } + let normals = normals.part(p); for poly in part.polygons.iter().filter(|_| !poses.skips(part)) { + let normal = poly.vertex_normal(normals); for v in poly.vertices { let pos = m.transform_point3(glam::Vec3::new(v[0], v[1], v[2]) / 16.0); out.push(Vertex { + normal, pos: pos.to_array(), uv: [ uv[0] + (uv[2] - uv[0]) * v[3], @@ -840,7 +867,7 @@ pub fn draw( } } for c in &part.children { - draw(c, m, poses, uv, color, light, overlay, out); + draw_normals(c, m, normals, poses, uv, color, light, overlay, out); } } /// Render a mob with its variant model, texture and layers. @@ -955,7 +982,13 @@ fn render_visible( } else { overlay }; + let start = out.len(); draw(&part, base, &poses, uv, layer.color, light, overlay, out); + if layer.full_bright && m.species() != Species::Warden { + for vertex in &mut out[start..] { + vertex.normal = [0; 4]; + } + } } } /// The equipment slot of a `HumanoidArmorLayer` model (`zombie#helmet`). diff --git a/crates/horizon-client/src/particles.rs b/crates/horizon-client/src/particles.rs index a52c34e5a..e81e41bd8 100644 --- a/crates/horizon-client/src/particles.rs +++ b/crates/horizon-client/src/particles.rs @@ -2806,6 +2806,7 @@ impl ParticleEngine { ] { let v = center + rotation * FVec3::new(n.0, n.1, 0.0) * size; out.push(Vertex { + normal: [0; 4], pos: v.to_array(), uv, layer: 0, diff --git a/crates/horizon-client/src/shadows.rs b/crates/horizon-client/src/shadows.rs index 641f53642..66e4f272b 100644 --- a/crates/horizon-client/src/shadows.rs +++ b/crates/horizon-client/src/shadows.rs @@ -107,6 +107,7 @@ pub fn shadow( for (wx, wz) in [(bx0, bz0), (bx0, bz1), (bx1, bz1), (bx1, bz0)] { let p = DVec3::new(wx, top, wz) - camera; out.push(Vertex { + normal: [0; 4], pos: p.as_vec3().to_array(), uv: [ sprite.u0 + (sprite.u1 - sprite.u0) * u(wx), diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaEntityModelExporter.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaEntityModelExporter.java index 7d801bbbc..8a38d7645 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaEntityModelExporter.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaEntityModelExporter.java @@ -26,6 +26,7 @@ public final class VanillaEntityModelExporter { pose.add(f); o.add("pose", pose); var polygons = new JsonArray(); + var normals = new JsonArray(); for (ModelPart.Cube cube : (List) cubes.get(part)) { for (ModelPart.Polygon polygon : cube.polygons) { var p = new JsonArray(); @@ -37,9 +38,15 @@ public final class VanillaEntityModelExporter { p.add(vertex); } polygons.add(p); + var normal = new JsonArray(); + normal.add(polygon.normal().x()); + normal.add(polygon.normal().y()); + normal.add(polygon.normal().z()); + normals.add(normal); } } o.add("polygons", polygons); + o.add("normals", normals); var kids = new JsonObject(); for (var entry : ((Map) children.get(part)).entrySet()) kids.add(entry.getKey(), part(entry.getValue()));