Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.
Rust differential-testinggame-enginegamedevlinuxminecraftminecraft-javareimplementationrustvoxelvulkanwgpu
//! Vanilla 26.3 MeshData.SortState / VertexSorting.byDistance.
//! Keep vertex order intact; only the quad indices change as the camera moves.
/// MeshData.decodeQuadCentroids uses the opposite corners, not all four vertices.
pub fn quad_center(a: [f32; 3], b: [f32; 3]) -> [f32; 3] {
std::array::from_fn(|i| (a[i] + b[i]) / 2.0)
}
#[derive(Default)]
pub struct Sorter {
order: Vec<(u32, f32)>,
indices: Vec<u32>,
}
impl Sorter {
pub fn sort(&mut self, centers: &[[f32; 3]], camera: [f32; 3]) -> &[u32] {
self.order.clear();
self.order.extend(centers.iter().enumerate().map(|(i, c)| {
let [x, y, z] = std::array::from_fn(|j| camera[j] - c[j]);
(i as u32, x * x + y * y + z * z)
}));
// VertexSorting uses a stable merge sort, preserving mesh order for ties.
self.order.sort_by(|a, b| b.1.total_cmp(&a.1));
self.indices.clear();
for &(quad, _) in &self.order {
let v = quad * 4;
self.indices.extend([v, v + 1, v + 2, v + 2, v + 3, v]);
}
&self.indices
}
}
pub struct SortState {
centers: Vec<[f32; 3]>,
last: Option<([i32; 3], [i32; 3])>,
}
impl SortState {
pub fn new(centers: Vec<[f32; 3]>) -> Self {
Self {
centers,
last: None,
}
}
pub fn is_sorted(&self) -> bool {
self.last.is_some()
}
/// Large tiles cannot use the section viewpoint shortcut: the camera may
/// cross a quad's plane while remaining on the same side of the first 16³.
pub fn update_lod<'a>(
&mut self,
camera: [f64; 3],
origin: [i32; 3],
sorter: &'a mut Sorter,
) -> Option<&'a [u32]> {
let block = camera.map(|v| v.floor() as i32);
if self.last.is_some_and(|(last, _)| last == block) {
return None;
}
self.last = Some((block, [0; 3]));
Some(sorter.sort(
&self.centers,
std::array::from_fn(|i| (camera[i] - f64::from(origin[i])) as f32),
))
}
/// TranslucencyPointOfView and ResortTransparencyTask: diagonal views keep
/// their ordering until the point of view changes; aligned views also
/// resort on crossing a block boundary. Sub-block movement needs no upload.
pub fn update<'a>(
&mut self,
camera: [f64; 3],
origin: [i32; 3],
sorter: &'a mut Sorter,
) -> Option<&'a [u32]> {
let block = camera.map(|v| v.floor() as i32);
let pov = std::array::from_fn(|i| ((block[i] >> 4) - (origin[i] >> 4)).clamp(-1, 1));
if let Some((last_block, last_pov)) = self.last
&& last_pov == pov
&& (last_block == block || !pov.contains(&0))
{
return None;
}
self.last = Some((block, pov));
Some(sorter.sort(
&self.centers,
std::array::from_fn(|i| (camera[i] - f64::from(origin[i])) as f32),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn equal_distances_keep_mesh_order_and_triangle_winding() {
let mut sorter = Sorter::default();
assert_eq!(
sorter.sort(
&[[1.0, 0.0, 0.0], [-1.0, 0.0, 0.0], [4.0, 0.0, 0.0]],
[0.0; 3]
),
&[8, 9, 10, 10, 11, 8, 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4]
);
assert!(sorter.sort(&[], [0.0; 3]).is_empty());
}
#[test]
fn movement_reverses_faces_without_moving_vertices() {
let centers = [[0.5, 0.5, 0.0], [0.5, 0.5, 1.0]];
let mut sorter = Sorter::default();
assert_eq!(sorter.sort(¢ers, [0.5, 0.5, -2.0])[0], 4);
assert_eq!(sorter.sort(¢ers, [0.5, 0.5, 3.0])[0], 0);
}
#[test]
fn cached_viewpoint_handles_negative_and_large_coordinates() {
let origin = [-30_000_000, -64, 30_000_000];
let mut state = SortState::new(vec![[0.5; 3]]);
let mut sorter = Sorter::default();
let camera = [-29_999_999.5, -63.5, 30_000_000.5];
assert!(state.update(camera, origin, &mut sorter).is_some());
assert!(state.update(camera, origin, &mut sorter).is_none());
assert!(
state
.update([-29_999_999.1, -63.1, 30_000_000.9], origin, &mut sorter)
.is_none()
);
assert!(
state
.update([-29_999_998.9, -63.1, 30_000_000.9], origin, &mut sorter)
.is_some()
);
}
#[test]
fn large_tiles_resort_inside_the_same_section_viewpoint() {
let mut state = SortState::new(vec![[32.0, 0.0, 0.0], [96.0, 0.0, 0.0]]);
let mut sorter = Sorter::default();
assert_eq!(
state
.update_lod([48.0, 20.0, 20.0], [0; 3], &mut sorter)
.unwrap()[0],
4
);
assert!(
state
.update_lod([48.5, 20.0, 20.0], [0; 3], &mut sorter)
.is_none()
);
// Both views have positive x/y/z signs relative to the first section,
// so the section-sized shortcut would incorrectly keep the old order.
assert_eq!(
state
.update_lod([80.0, 20.0, 20.0], [0; 3], &mut sorter)
.unwrap()[0],
0
);
}
}