voxels: extract greedy meshing phase under the 500-line limit
- Commit
1ce73a3266597d9527fd9549e318efeaf52238a5- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 20:39 UTC (4 days ago)
- Parents
ee9f474
world/mesher.rs (806 lines) exceeded the 500-line rustFileLength limit because mesh_chunk is one large function; the established include! split cannot cut inside it. Extract its greedy cube-meshing phase -- the direction loop with the three axis arms that build each direction mask and emit the merged quads -- into fn mesh_cubes_greedy in mesher_part2.rs, spliced into the same module with include! beside the existing mesher_part1.rs, so every item, visibility and call path is identical. The 390-line loop body moves verbatim (token-identical); the helper only receives the section, its base Y and world origin, the occ/hidden/ face_light samplers and grass tint mesh_chunk already built, and the vertex/index buffers it was filling. The water and box phases stay in mesh_chunk, in their original order. No public API changes: mesh_cubes_greedy is a private fn in the same module and no pub item changes. mesher.rs drops 806 -> 429 lines; the new helper file is 411 lines. Verified: cargo fmt -p voxels_engine -- --check clean, cargo test -p voxels_engine --locked green (252 passed, plus 5 integration tests), and nm -D on libvoxels_engine.so shows the same 55 exported JNI symbols.
Diff
games/voxels/src/main/rust/src/world/mesher.rs
diff --git a/games/voxels/src/main/rust/src/world/mesher.rs b/games/voxels/src/main/rust/src/world/mesher.rsindex bec9eafb48..ff55c9d87e 100644--- a/games/voxels/src/main/rust/src/world/mesher.rs+++ b/games/voxels/src/main/rust/src/world/mesher.rs@@ -1,5 +1,5 @@ use super::block::{Aabb, Block, Id, Shape, GRASS_SIDE_TILE};-use super::chunk::{Chunk, CHUNK_HEIGHT, SECTIONS_PER_CHUNK, SECTION_SIZE};+use super::chunk::{BlockSection, Chunk, CHUNK_HEIGHT, SECTIONS_PER_CHUNK, SECTION_SIZE}; /// A neighbour lookup: block id plus its meta byte, since occlusion now depends on both. pub type Neighbor<'a> = dyn Fn(i32, i32, i32) -> (Id, u8) + 'a;@@ -282,396 +282,18 @@ pub fn mesh_chunk( let base_y = (sec_idx * SECTION_SIZE) as i32; let mut verts = Vec::with_capacity(1024); let mut indices = Vec::with_capacity(1536);- let dirs =- [(1, 0, 0, 0), (-1, 0, 0, 0), (0, 1, 0, 1), (0, -1, 0, 1), (0, 0, 1, 2), (0, 0, -1, 2)];- for &(dx, dy, dz, axis) in &dirs {- let nrm = [dx as f32, dy as f32, dz as f32];- // Mask entry: (block id, tile, per-corner AO in this direction's vertex order).- match axis {- 0 => {- for x in 0..SECTION_SIZE {- let mut mask = [[None::<(Id, u32, [u8; 4], u8)>; 16]; 16];- for (y, row) in mask.iter_mut().enumerate() {- for (z, cell) in row.iter_mut().enumerate() {- let id = section.get(x, y, z);- if id == 0 || id == Block::Water as Id {- continue;- }- if Block::from_id(id).shape() != Shape::Cube {- continue;- } // emitted as boxes below- let wx = ox + x as i32;- let wy = base_y + y as i32;- let wz = oz + z as i32;- if !hidden(wx, wy, wz, dx, dy, dz) {- let tile = Block::from_id(id).tile_for_dir(dx, dy, dz);- let nb = wx + dx;- let ao = |ys: i32, zs: i32| {- vao(- occ(nb, wy + ys, wz),- occ(nb, wy, wz + zs),- occ(nb, wy + ys, wz + zs),- )- };- let ao4 = if dx == 1 {- [ao(-1, -1), ao(1, -1), ao(1, 1), ao(-1, 1)]- } else {- [ao(-1, 1), ao(1, 1), ao(1, -1), ao(-1, -1)]- };- *cell = Some((- id,- tile,- ao4,- face_light(wx + dx, wy + dy, wz + dz),- ));- }- }- }- let mut visited = [[false; 16]; 16];- for (y, row) in mask.iter().enumerate() {- for (z, cell) in row.iter().enumerate() {- if visited[y][z] {- continue;- }- let Some((bid, tile_idx, ao4, lpk)) = *cell else {- continue;- };- let mut w = 1;- while z + w < SECTION_SIZE- && !visited[y][z + w]- && mask[y][z + w] == Some((bid, tile_idx, ao4, lpk))- {- w += 1;- }- let mut h = 1;- 'outer: while y + h < SECTION_SIZE {- for k in 0..w {- if visited[y + h][z + k]- || mask[y + h][z + k] != Some((bid, tile_idx, ao4, lpk))- {- break 'outer;- }- }- h += 1;- }- for row in visited.iter_mut().skip(y).take(h) {- for cell in row.iter_mut().skip(z).take(w) {- *cell = true;- }- }- let x0 = x as f32 + if dx == 1 { 1.0 } else { 0.0 };- let y0 = y as f32;- let z0 = z as f32;- let ww = w as f32;- let hh = h as f32;- let (u0, v0, u1, v1) = (0.0f32, 0.0f32, ww, hh);- // Grass side: sentinel tile so the shader composites dirt + tinted overlay in one quad.- let is_grass = bid == Block::Grass as Id;- let color = if is_grass {- grass_tint(ox + x as i32, oz + z as i32)- } else {- Block::from_id(bid).color()- };- let etile =- if is_grass { GRASS_SIDE_TILE as f32 } else { tile_idx as f32 };- let quad = if dx == 1 {- [- ([x0, y0, z0], [u0, v1]),- ([x0, y0 + hh, z0], [u0, v0]),- ([x0, y0 + hh, z0 + ww], [u1, v0]),- ([x0, y0, z0 + ww], [u1, v1]),- ]- } else {- [- ([x0, y0, z0 + ww], [u0, v1]),- ([x0, y0 + hh, z0 + ww], [u0, v0]),- ([x0, y0 + hh, z0], [u1, v0]),- ([x0, y0, z0], [u1, v1]),- ]- };- let wx0 = ox as f32;- let wz0 = oz as f32;- let by = base_y as f32;- let start = verts.len() as u32;- for (i, (pos, uv)) in quad.iter().enumerate() {- verts.push(Vertex {- pos: [wx0 + pos[0], by + pos[1], wz0 + pos[2]],- uv: *uv,- color,- ao: aof(ao4[i]),- tile_idx: etile,- normal: nrm,- light: light_to_f32(lpk),- });- }- indices.extend_from_slice(&[- start,- start + 1,- start + 2,- start,- start + 2,- start + 3,- ]);- }- }- }- }- 1 => {- for y in 0..SECTION_SIZE {- let mut mask = [[None::<(Id, u32, [u8; 4], u8)>; 16]; 16];- for (x, row) in mask.iter_mut().enumerate() {- for (z, cell) in row.iter_mut().enumerate() {- let id = section.get(x, y, z);- if id == 0 || id == Block::Water as Id {- continue;- }- if Block::from_id(id).shape() != Shape::Cube {- continue;- }- let wx = ox + x as i32;- let wy = base_y + y as i32;- let wz = oz + z as i32;- if !hidden(wx, wy, wz, dx, dy, dz) {- {- let tile = Block::from_id(id).tile_for_dir(dx, dy, dz);- let nb = wy + dy;- let ao = |xs: i32, zs: i32| {- vao(- occ(wx + xs, nb, wz),- occ(wx, nb, wz + zs),- occ(wx + xs, nb, wz + zs),- )- };- let ao4 = if dy == 1 {- [ao(-1, -1), ao(1, -1), ao(1, 1), ao(-1, 1)]- } else {- [ao(-1, 1), ao(1, 1), ao(1, -1), ao(-1, -1)]- };- *cell = Some((- id,- tile,- ao4,- face_light(wx + dx, wy + dy, wz + dz),- ));- }- }- }- }- let mut visited = [[false; 16]; 16];- for (x, row) in mask.iter().enumerate() {- for (z, cell) in row.iter().enumerate() {- if visited[x][z] {- continue;- }- let Some((bid, tile_idx, ao4, lpk)) = *cell else {- continue;- };- let mut w = 1;- while z + w < SECTION_SIZE- && !visited[x][z + w]- && mask[x][z + w] == Some((bid, tile_idx, ao4, lpk))- {- w += 1;- }- let mut h = 1;- 'outer2: while x + h < SECTION_SIZE {- for k in 0..w {- if visited[x + h][z + k]- || mask[x + h][z + k] != Some((bid, tile_idx, ao4, lpk))- {- break 'outer2;- }- }- h += 1;- }- for row in visited.iter_mut().skip(x).take(h) {- for cell in row.iter_mut().skip(z).take(w) {- *cell = true;- }- }- let y0 = y as f32 + if dy == 1 { 1.0 } else { 0.0 };- let x0 = x as f32;- let z0 = z as f32;- let ww = w as f32;- let hh = h as f32;- let (u0, v0, u1, v1) = (0.0f32, 0.0f32, hh, ww);- let color = if bid == Block::Grass as Id && dy == 1 {- grass_tint(ox + x as i32, oz + z as i32)- } else {- Block::from_id(bid).color()- };- let wx0 = ox as f32;- let wz0 = oz as f32;- let by = base_y as f32;- let quad = if dy == 1 {- [- ([x0, y0, z0], [u0, v0]),- ([x0 + hh, y0, z0], [u1, v0]),- ([x0 + hh, y0, z0 + ww], [u1, v1]),- ([x0, y0, z0 + ww], [u0, v1]),- ]- } else {- [- ([x0, y0, z0 + ww], [u0, v0]),- ([x0 + hh, y0, z0 + ww], [u1, v0]),- ([x0 + hh, y0, z0], [u1, v1]),- ([x0, y0, z0], [u0, v1]),- ]- };- let start = verts.len() as u32;- for (i, (lp, uv)) in quad.iter().enumerate() {- verts.push(Vertex {- pos: [wx0 + lp[0], by + lp[1], wz0 + lp[2]],- uv: *uv,- color,- ao: aof(ao4[i]),- tile_idx: tile_idx as f32,- normal: nrm,- light: light_to_f32(lpk),- });- }- indices.extend_from_slice(&[- start,- start + 1,- start + 2,- start,- start + 2,- start + 3,- ]);- }- }- }- }- 2 => {- for z in 0..SECTION_SIZE {- let mut mask = [[None::<(Id, u32, [u8; 4], u8)>; 16]; 16];- for (x, row) in mask.iter_mut().enumerate() {- for (y, cell) in row.iter_mut().enumerate() {- let id = section.get(x, y, z);- if id == 0 || id == Block::Water as Id {- continue;- }- if Block::from_id(id).shape() != Shape::Cube {- continue;- }- let wx = ox + x as i32;- let wy = base_y + y as i32;- let wz = oz + z as i32;- if !hidden(wx, wy, wz, dx, dy, dz) {- let tile = Block::from_id(id).tile_for_dir(dx, dy, dz);- let nb = wz + dz;- let ao = |xs: i32, ys: i32| {- vao(- occ(wx + xs, wy, nb),- occ(wx, wy + ys, nb),- occ(wx + xs, wy + ys, nb),- )- };- let ao4 = if dz == 1 {- [ao(-1, -1), ao(1, -1), ao(1, 1), ao(-1, 1)]- } else {- [ao(1, -1), ao(-1, -1), ao(-1, 1), ao(1, 1)]- };- *cell = Some((- id,- tile,- ao4,- face_light(wx + dx, wy + dy, wz + dz),- ));- }- }- }- let mut visited = [[false; 16]; 16];- for (x, row) in mask.iter().enumerate() {- for (y, cell) in row.iter().enumerate() {- if visited[x][y] {- continue;- }- let Some((bid, tile_idx, ao4, lpk)) = *cell else {- continue;- };- let mut w = 1;- while y + w < SECTION_SIZE- && !visited[x][y + w]- && mask[x][y + w] == Some((bid, tile_idx, ao4, lpk))- {- w += 1;- }- let mut h = 1;- 'outer3: while x + h < SECTION_SIZE {- for k in 0..w {- if visited[x + h][y + k]- || mask[x + h][y + k] != Some((bid, tile_idx, ao4, lpk))- {- break 'outer3;- }- }- h += 1;- }- for row in visited.iter_mut().skip(x).take(h) {- for cell in row.iter_mut().skip(y).take(w) {- *cell = true;- }- }- let z0 = z as f32 + if dz == 1 { 1.0 } else { 0.0 };- let x0 = x as f32;- let y0 = y as f32;- let ww = w as f32;- let hh = h as f32;- let (u0, v0, u1, v1) = (0.0f32, 0.0f32, hh, ww);- let is_grass = bid == Block::Grass as Id;- let color = if is_grass {- grass_tint(ox + x as i32, oz + z as i32)- } else {- Block::from_id(bid).color()- };- let etile =- if is_grass { GRASS_SIDE_TILE as f32 } else { tile_idx as f32 };- let wx0 = ox as f32;- let wz0 = oz as f32;- let by = base_y as f32;- let quad = if dz == 1 {- [- ([x0, y0, z0], [u0, v1]),- ([x0 + hh, y0, z0], [u1, v1]),- ([x0 + hh, y0 + ww, z0], [u1, v0]),- ([x0, y0 + ww, z0], [u0, v0]),- ]- } else {- [- ([x0 + hh, y0, z0], [u0, v1]),- ([x0, y0, z0], [u1, v1]),- ([x0, y0 + ww, z0], [u1, v0]),- ([x0 + hh, y0 + ww, z0], [u0, v0]),- ]- };- let start = verts.len() as u32;- for (i, (lp, uv)) in quad.iter().enumerate() {- verts.push(Vertex {- pos: [wx0 + lp[0], by + lp[1], wz0 + lp[2]],- uv: *uv,- color,- ao: aof(ao4[i]),- tile_idx: etile,- normal: nrm,- light: light_to_f32(lpk),- });- }- indices.extend_from_slice(&[- start,- start + 1,- start + 2,- start,- start + 2,- start + 3,- ]);- }- }- }- }- _ => {}- }- }+ mesh_cubes_greedy(+ section,+ base_y,+ ox,+ oz,+ &occ,+ &hidden,+ &face_light,+ grass_tint,+ &mut verts,+ &mut indices,+ ); // --- Water surface pass: top faces of water columns exposed to air, greedy-merged. --- let mut wverts: Vec<Vertex> = Vec::new(); let mut windices: Vec<u32> = Vec::new();@@ -801,6 +423,7 @@ pub fn mesh_chunk( } include!("mesher_part1.rs");+include!("mesher_part2.rs"); #[cfg(test)] #[path = "mesher_coverage_tests.rs"] mod mesher_coverage_tests;
games/voxels/src/main/rust/src/world/mesher_part2.rs
diff --git a/games/voxels/src/main/rust/src/world/mesher_part2.rs b/games/voxels/src/main/rust/src/world/mesher_part2.rsnew file mode 100644index 0000000000..8d12c846ee--- /dev/null+++ b/games/voxels/src/main/rust/src/world/mesher_part2.rs@@ -0,0 +1,411 @@+// Greedy cube meshing for one section, split from `mesher.rs` (file-length+// limit) and spliced into the same module. The three axis arms, their mask+// construction and their quad emission move verbatim, in the same order.++/// Emit the greedy-merged cube faces of one section into `verts`/`indices`.+/// `occ`, `hidden` and `face_light` are the whole-cell samplers `mesh_chunk`+/// builds from the chunk and its neighbours.+#[allow(clippy::too_many_arguments)]+fn mesh_cubes_greedy(+ section: &BlockSection,+ base_y: i32,+ ox: i32,+ oz: i32,+ occ: &dyn Fn(i32, i32, i32) -> bool,+ hidden: &dyn Fn(i32, i32, i32, i32, i32, i32) -> bool,+ face_light: &dyn Fn(i32, i32, i32) -> u8,+ grass_tint: &dyn Fn(i32, i32) -> [f32; 3],+ verts: &mut Vec<Vertex>,+ indices: &mut Vec<u32>,+) {+ let dirs =+ [(1, 0, 0, 0), (-1, 0, 0, 0), (0, 1, 0, 1), (0, -1, 0, 1), (0, 0, 1, 2), (0, 0, -1, 2)];+ for &(dx, dy, dz, axis) in &dirs {+ let nrm = [dx as f32, dy as f32, dz as f32];+ // Mask entry: (block id, tile, per-corner AO in this direction's vertex order).+ match axis {+ 0 => {+ for x in 0..SECTION_SIZE {+ let mut mask = [[None::<(Id, u32, [u8; 4], u8)>; 16]; 16];+ for (y, row) in mask.iter_mut().enumerate() {+ for (z, cell) in row.iter_mut().enumerate() {+ let id = section.get(x, y, z);+ if id == 0 || id == Block::Water as Id {+ continue;+ }+ if Block::from_id(id).shape() != Shape::Cube {+ continue;+ } // emitted as boxes below+ let wx = ox + x as i32;+ let wy = base_y + y as i32;+ let wz = oz + z as i32;+ if !hidden(wx, wy, wz, dx, dy, dz) {+ let tile = Block::from_id(id).tile_for_dir(dx, dy, dz);+ let nb = wx + dx;+ let ao = |ys: i32, zs: i32| {+ vao(+ occ(nb, wy + ys, wz),+ occ(nb, wy, wz + zs),+ occ(nb, wy + ys, wz + zs),+ )+ };+ let ao4 = if dx == 1 {+ [ao(-1, -1), ao(1, -1), ao(1, 1), ao(-1, 1)]+ } else {+ [ao(-1, 1), ao(1, 1), ao(1, -1), ao(-1, -1)]+ };+ *cell = Some((+ id,+ tile,+ ao4,+ face_light(wx + dx, wy + dy, wz + dz),+ ));+ }+ }+ }+ let mut visited = [[false; 16]; 16];+ for (y, row) in mask.iter().enumerate() {+ for (z, cell) in row.iter().enumerate() {+ if visited[y][z] {+ continue;+ }+ let Some((bid, tile_idx, ao4, lpk)) = *cell else {+ continue;+ };+ let mut w = 1;+ while z + w < SECTION_SIZE+ && !visited[y][z + w]+ && mask[y][z + w] == Some((bid, tile_idx, ao4, lpk))+ {+ w += 1;+ }+ let mut h = 1;+ 'outer: while y + h < SECTION_SIZE {+ for k in 0..w {+ if visited[y + h][z + k]+ || mask[y + h][z + k] != Some((bid, tile_idx, ao4, lpk))+ {+ break 'outer;+ }+ }+ h += 1;+ }+ for row in visited.iter_mut().skip(y).take(h) {+ for cell in row.iter_mut().skip(z).take(w) {+ *cell = true;+ }+ }+ let x0 = x as f32 + if dx == 1 { 1.0 } else { 0.0 };+ let y0 = y as f32;+ let z0 = z as f32;+ let ww = w as f32;+ let hh = h as f32;+ let (u0, v0, u1, v1) = (0.0f32, 0.0f32, ww, hh);+ // Grass side: sentinel tile so the shader composites dirt + tinted overlay in one quad.+ let is_grass = bid == Block::Grass as Id;+ let color = if is_grass {+ grass_tint(ox + x as i32, oz + z as i32)+ } else {+ Block::from_id(bid).color()+ };+ let etile =+ if is_grass { GRASS_SIDE_TILE as f32 } else { tile_idx as f32 };+ let quad = if dx == 1 {+ [+ ([x0, y0, z0], [u0, v1]),+ ([x0, y0 + hh, z0], [u0, v0]),+ ([x0, y0 + hh, z0 + ww], [u1, v0]),+ ([x0, y0, z0 + ww], [u1, v1]),+ ]+ } else {+ [+ ([x0, y0, z0 + ww], [u0, v1]),+ ([x0, y0 + hh, z0 + ww], [u0, v0]),+ ([x0, y0 + hh, z0], [u1, v0]),+ ([x0, y0, z0], [u1, v1]),+ ]+ };+ let wx0 = ox as f32;+ let wz0 = oz as f32;+ let by = base_y as f32;+ let start = verts.len() as u32;+ for (i, (pos, uv)) in quad.iter().enumerate() {+ verts.push(Vertex {+ pos: [wx0 + pos[0], by + pos[1], wz0 + pos[2]],+ uv: *uv,+ color,+ ao: aof(ao4[i]),+ tile_idx: etile,+ normal: nrm,+ light: light_to_f32(lpk),+ });+ }+ indices.extend_from_slice(&[+ start,+ start + 1,+ start + 2,+ start,+ start + 2,+ start + 3,+ ]);+ }+ }+ }+ }+ 1 => {+ for y in 0..SECTION_SIZE {+ let mut mask = [[None::<(Id, u32, [u8; 4], u8)>; 16]; 16];+ for (x, row) in mask.iter_mut().enumerate() {+ for (z, cell) in row.iter_mut().enumerate() {+ let id = section.get(x, y, z);+ if id == 0 || id == Block::Water as Id {+ continue;+ }+ if Block::from_id(id).shape() != Shape::Cube {+ continue;+ }+ let wx = ox + x as i32;+ let wy = base_y + y as i32;+ let wz = oz + z as i32;+ if !hidden(wx, wy, wz, dx, dy, dz) {+ {+ let tile = Block::from_id(id).tile_for_dir(dx, dy, dz);+ let nb = wy + dy;+ let ao = |xs: i32, zs: i32| {+ vao(+ occ(wx + xs, nb, wz),+ occ(wx, nb, wz + zs),+ occ(wx + xs, nb, wz + zs),+ )+ };+ let ao4 = if dy == 1 {+ [ao(-1, -1), ao(1, -1), ao(1, 1), ao(-1, 1)]+ } else {+ [ao(-1, 1), ao(1, 1), ao(1, -1), ao(-1, -1)]+ };+ *cell = Some((+ id,+ tile,+ ao4,+ face_light(wx + dx, wy + dy, wz + dz),+ ));+ }+ }+ }+ }+ let mut visited = [[false; 16]; 16];+ for (x, row) in mask.iter().enumerate() {+ for (z, cell) in row.iter().enumerate() {+ if visited[x][z] {+ continue;+ }+ let Some((bid, tile_idx, ao4, lpk)) = *cell else {+ continue;+ };+ let mut w = 1;+ while z + w < SECTION_SIZE+ && !visited[x][z + w]+ && mask[x][z + w] == Some((bid, tile_idx, ao4, lpk))+ {+ w += 1;+ }+ let mut h = 1;+ 'outer2: while x + h < SECTION_SIZE {+ for k in 0..w {+ if visited[x + h][z + k]+ || mask[x + h][z + k] != Some((bid, tile_idx, ao4, lpk))+ {+ break 'outer2;+ }+ }+ h += 1;+ }+ for row in visited.iter_mut().skip(x).take(h) {+ for cell in row.iter_mut().skip(z).take(w) {+ *cell = true;+ }+ }+ let y0 = y as f32 + if dy == 1 { 1.0 } else { 0.0 };+ let x0 = x as f32;+ let z0 = z as f32;+ let ww = w as f32;+ let hh = h as f32;+ let (u0, v0, u1, v1) = (0.0f32, 0.0f32, hh, ww);+ let color = if bid == Block::Grass as Id && dy == 1 {+ grass_tint(ox + x as i32, oz + z as i32)+ } else {+ Block::from_id(bid).color()+ };+ let wx0 = ox as f32;+ let wz0 = oz as f32;+ let by = base_y as f32;+ let quad = if dy == 1 {+ [+ ([x0, y0, z0], [u0, v0]),+ ([x0 + hh, y0, z0], [u1, v0]),+ ([x0 + hh, y0, z0 + ww], [u1, v1]),+ ([x0, y0, z0 + ww], [u0, v1]),+ ]+ } else {+ [+ ([x0, y0, z0 + ww], [u0, v0]),+ ([x0 + hh, y0, z0 + ww], [u1, v0]),+ ([x0 + hh, y0, z0], [u1, v1]),+ ([x0, y0, z0], [u0, v1]),+ ]+ };+ let start = verts.len() as u32;+ for (i, (lp, uv)) in quad.iter().enumerate() {+ verts.push(Vertex {+ pos: [wx0 + lp[0], by + lp[1], wz0 + lp[2]],+ uv: *uv,+ color,+ ao: aof(ao4[i]),+ tile_idx: tile_idx as f32,+ normal: nrm,+ light: light_to_f32(lpk),+ });+ }+ indices.extend_from_slice(&[+ start,+ start + 1,+ start + 2,+ start,+ start + 2,+ start + 3,+ ]);+ }+ }+ }+ }+ 2 => {+ for z in 0..SECTION_SIZE {+ let mut mask = [[None::<(Id, u32, [u8; 4], u8)>; 16]; 16];+ for (x, row) in mask.iter_mut().enumerate() {+ for (y, cell) in row.iter_mut().enumerate() {+ let id = section.get(x, y, z);+ if id == 0 || id == Block::Water as Id {+ continue;+ }+ if Block::from_id(id).shape() != Shape::Cube {+ continue;+ }+ let wx = ox + x as i32;+ let wy = base_y + y as i32;+ let wz = oz + z as i32;+ if !hidden(wx, wy, wz, dx, dy, dz) {+ let tile = Block::from_id(id).tile_for_dir(dx, dy, dz);+ let nb = wz + dz;+ let ao = |xs: i32, ys: i32| {+ vao(+ occ(wx + xs, wy, nb),+ occ(wx, wy + ys, nb),+ occ(wx + xs, wy + ys, nb),+ )+ };+ let ao4 = if dz == 1 {+ [ao(-1, -1), ao(1, -1), ao(1, 1), ao(-1, 1)]+ } else {+ [ao(1, -1), ao(-1, -1), ao(-1, 1), ao(1, 1)]+ };+ *cell = Some((+ id,+ tile,+ ao4,+ face_light(wx + dx, wy + dy, wz + dz),+ ));+ }+ }+ }+ let mut visited = [[false; 16]; 16];+ for (x, row) in mask.iter().enumerate() {+ for (y, cell) in row.iter().enumerate() {+ if visited[x][y] {+ continue;+ }+ let Some((bid, tile_idx, ao4, lpk)) = *cell else {+ continue;+ };+ let mut w = 1;+ while y + w < SECTION_SIZE+ && !visited[x][y + w]+ && mask[x][y + w] == Some((bid, tile_idx, ao4, lpk))+ {+ w += 1;+ }+ let mut h = 1;+ 'outer3: while x + h < SECTION_SIZE {+ for k in 0..w {+ if visited[x + h][y + k]+ || mask[x + h][y + k] != Some((bid, tile_idx, ao4, lpk))+ {+ break 'outer3;+ }+ }+ h += 1;+ }+ for row in visited.iter_mut().skip(x).take(h) {+ for cell in row.iter_mut().skip(y).take(w) {+ *cell = true;+ }+ }+ let z0 = z as f32 + if dz == 1 { 1.0 } else { 0.0 };+ let x0 = x as f32;+ let y0 = y as f32;+ let ww = w as f32;+ let hh = h as f32;+ let (u0, v0, u1, v1) = (0.0f32, 0.0f32, hh, ww);+ let is_grass = bid == Block::Grass as Id;+ let color = if is_grass {+ grass_tint(ox + x as i32, oz + z as i32)+ } else {+ Block::from_id(bid).color()+ };+ let etile =+ if is_grass { GRASS_SIDE_TILE as f32 } else { tile_idx as f32 };+ let wx0 = ox as f32;+ let wz0 = oz as f32;+ let by = base_y as f32;+ let quad = if dz == 1 {+ [+ ([x0, y0, z0], [u0, v1]),+ ([x0 + hh, y0, z0], [u1, v1]),+ ([x0 + hh, y0 + ww, z0], [u1, v0]),+ ([x0, y0 + ww, z0], [u0, v0]),+ ]+ } else {+ [+ ([x0 + hh, y0, z0], [u0, v1]),+ ([x0, y0, z0], [u1, v1]),+ ([x0, y0 + ww, z0], [u1, v0]),+ ([x0 + hh, y0 + ww, z0], [u0, v0]),+ ]+ };+ let start = verts.len() as u32;+ for (i, (lp, uv)) in quad.iter().enumerate() {+ verts.push(Vertex {+ pos: [wx0 + lp[0], by + lp[1], wz0 + lp[2]],+ uv: *uv,+ color,+ ao: aof(ao4[i]),+ tile_idx: etile,+ normal: nrm,+ light: light_to_f32(lpk),+ });+ }+ indices.extend_from_slice(&[+ start,+ start + 1,+ start + 2,+ start,+ start + 2,+ start + 3,+ ]);+ }+ }+ }+ }+ _ => {}+ }+ }+}