Merge remote-tracking branch 'origin/review-52' into merge-52

This commit is contained in:
Ludovic Marie
2026-10-13 23:38:55 +02:00
28 changed files with 362 additions and 12 deletions
@@ -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<Vertex>) {
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<Vertex>)
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,
+1
View File
@@ -34,6 +34,7 @@ pub fn geometry(model: &BakedModel, origin: glam::Vec3, translucent: bool, out:
layer: if translucent { 12 } else { 11 },
color: 0xffff_ffff,
light: 240 | (240 << 8),
normal: [0; 4],
});
}
}
+21 -4
View File
@@ -14,6 +14,7 @@ struct Globals {
// Atlas rects (u0, v0, width, height) of end_sky.png and end_portal.png.
end_portal_sprites: array<vec4<f32>, 2>,
// x: vanilla's GameTime ((game time % 24000 + partial tick) / 24000)
// y: Nether cardinal lighting.
time: vec4<f32>,
crumbling_sprite: vec4<f32>,
// Selection outline parameters (read by terrain.wgsl).
@@ -36,9 +37,16 @@ struct Out {
@location(6) @interpolate(flat) wrap: vec2<f32>,
// Overlays 9 and 10: projection_from_position(gl_Position), the End portal's texProj0.
@location(7) proj: vec4<f32>,
@location(8) back_color: vec4<f32>,
}
// Shipped light.glsl; normals are already transformed and RGBA8_SNORM packed.
fn entity_light(normal: vec3<f32>, 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<f32>, @location(1) uv: vec2<f32>,
@location(2) overlay: u32, @location(3) color: vec4<f32>, @location(4) light: vec4<u32>) -> Out {
@location(2) overlay: u32, @location(3) color: vec4<f32>, @location(4) light: vec4<u32>, @location(5) normal: vec4<f32>) -> 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);
@@ -50,9 +58,18 @@ struct Out {
var c = select(color * lm, color, overlay == 2u || overlay == 4u || overlay == 6u || overlay == 11u || overlay == 12u);
// 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.
@@ -102,13 +119,13 @@ 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<f32> {
@fragment fn fs_entity(in: Out, @builtin(front_facing) front: bool) -> @location(0) vec4<f32> {
var uv = in.uv;
if in.overlay == 6u { uv.y = in.wrap.x + fract(in.uv.y) * in.wrap.y; }
if in.overlay == 11u || in.overlay == 12u {
uv = globals.crumbling_sprite.xy + fract(in.uv) * globals.crumbling_sprite.zw;
}
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.
+9
View File
@@ -4379,6 +4379,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,
@@ -4540,6 +4541,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,
@@ -4605,6 +4607,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,
@@ -4989,6 +4992,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,
@@ -5048,6 +5052,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,
@@ -5107,6 +5112,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,
@@ -5798,6 +5804,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,
@@ -6766,6 +6773,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,
@@ -7383,6 +7391,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,
@@ -2408,6 +2408,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,
@@ -2473,6 +2474,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,
+3
View File
@@ -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,
+4
View File
@@ -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,
@@ -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,
@@ -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,
+1
View File
@@ -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,
+1
View File
@@ -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,
@@ -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,
+3
View File
@@ -532,6 +532,7 @@ impl Game<'_> {
let quad = |corners: [[f32; 3]; 4], uv: [[f32; 2]; 4], out: &mut Vec<Vertex>| {
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,
@@ -578,6 +579,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,
@@ -646,6 +648,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,
@@ -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,
@@ -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,
+1
View File
@@ -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,
@@ -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,
+1
View File
@@ -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 },
+12 -3
View File
@@ -93,7 +93,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],
/// Atlas rect of the current destroy stage (entity.wgsl).
crumbling_sprite: [f32; 4],
@@ -143,6 +143,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).
@@ -871,10 +873,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::<Vertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &vertex_attributes,
attributes: &entity_attributes,
})];
let entity_pipeline = |label, cull_mode, depth_write, bias, blend| {
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
@@ -1846,7 +1849,12 @@ impl Renderer {
],
end_portal_sprites: frame.end_portal_sprites,
crumbling_sprite: frame.crumbling_sprite,
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,
],
outline_params: outline_params(self.config.width, self.config.height),
};
self.queue
@@ -2451,6 +2459,7 @@ mod tests {
layer,
color: 0xffff_ffff,
light: id,
normal: [0; 4],
}; 4]
};
let entities: Vec<_> = [(0, 0), (4, 1), (11, 2), (7, 3), (4, 4), (12, 7)]
+1
View File
@@ -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,
+3
View File
@@ -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<Vertex>,
@@ -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.
@@ -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,
+243 -3
View File
@@ -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::<Vec<_>>();
(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<Vertex>,
) {
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::<Vertex>(), 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]
);
}
}
@@ -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,
+35 -2
View File
@@ -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<Vertex>,
) {
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<Vertex>,
) {
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`).
+1
View File
@@ -2864,6 +2864,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,
+1
View File
@@ -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),
@@ -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<ModelPart.Cube>) 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<String, ModelPart>) children.get(part)).entrySet())
kids.add(entry.getKey(), part(entry.getValue()));