Only render the top block at each XZ cell

Description

We're rendering a lot of blocks that aren't visible because they are underground. To cut down on the amount of work the renderer has to do, we can just send the top block at each X-Z coordinate.

Commands

git clone git@github.com:atsheehan/iridium
cd iridium
git checkout fc5afbc329d4bb6d3b47de649af1888e99c0a6d8
cargo run --release

Code Changes

Modified src/main.rsGitHub

@@ -25,7 +25,7 @@
2525 let mut renderer = Renderer::new(&event_loop, options.windowed, options.disable_vsync);
2626
2727 let mut world = World::new(256, 32, 256);
28- renderer.update_block_cache(world.block_positions());
28+ renderer.update_block_cache(world.visible_block_positions());
2929
3030 let mut last_instant = Instant::now();
3131 let mut fps_counter = FrameCounter::new(last_instant);
@@ -25,7 +25,7 @@
25 let mut renderer = Renderer::new(&event_loop, options.windowed, options.disable_vsync);
26
27 let mut world = World::new(256, 32, 256);
28- renderer.update_block_cache(world.block_positions());
29
30 let mut last_instant = Instant::now();
31 let mut fps_counter = FrameCounter::new(last_instant);
@@ -25,7 +25,7 @@
25 let mut renderer = Renderer::new(&event_loop, options.windowed, options.disable_vsync);
26
27 let mut world = World::new(256, 32, 256);
28+ renderer.update_block_cache(world.visible_block_positions());
29
30 let mut last_instant = Instant::now();
31 let mut fps_counter = FrameCounter::new(last_instant);

Modified src/world.rsGitHub

@@ -53,13 +53,13 @@
5353 self.camera.position = self.camera.position + actual_velocity;
5454 }
5555
56- pub(crate) fn block_positions(&self) -> impl Iterator<Item = Vec3> + '_ {
56+ pub(crate) fn visible_block_positions(&self) -> impl Iterator<Item = Vec3> + '_ {
5757 (0..self.z_depth).flat_map(move |z| {
58- (0..self.x_width).flat_map(move |x| {
58+ (0..self.x_width).map(move |x| {
5959 let index = ((self.x_width * z) + x) as usize;
60- let y_max = self.heights[index];
60+ let y = self.heights[index];
6161
62- (0..y_max).map(move |y| Vec3(x as f32, y as f32, z as f32))
62+ Vec3(x as f32, y as f32, z as f32)
6363 })
6464 })
6565 }
@@ -53,13 +53,13 @@
53 self.camera.position = self.camera.position + actual_velocity;
54 }
55
56- pub(crate) fn block_positions(&self) -> impl Iterator<Item = Vec3> + '_ {
57 (0..self.z_depth).flat_map(move |z| {
58- (0..self.x_width).flat_map(move |x| {
59 let index = ((self.x_width * z) + x) as usize;
60- let y_max = self.heights[index];
61
62- (0..y_max).map(move |y| Vec3(x as f32, y as f32, z as f32))
63 })
64 })
65 }
@@ -53,13 +53,13 @@
53 self.camera.position = self.camera.position + actual_velocity;
54 }
55
56+ pub(crate) fn visible_block_positions(&self) -> impl Iterator<Item = Vec3> + '_ {
57 (0..self.z_depth).flat_map(move |z| {
58+ (0..self.x_width).map(move |x| {
59 let index = ((self.x_width * z) + x) as usize;
60+ let y = self.heights[index];
61
62+ Vec3(x as f32, y as f32, z as f32)
63 })
64 })
65 }