From 50cb6ea41086f5a8c74fd1c964f3ac135a5d3547 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Sat, 16 Dec 2023 08:56:50 -0800 Subject: [PATCH] text: Skip drawing step when there is no intersection with the block bounds. --- all-is-cubes/src/block/text.rs | 52 +++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/all-is-cubes/src/block/text.rs b/all-is-cubes/src/block/text.rs index b7d574a74..397bfee28 100644 --- a/all-is-cubes/src/block/text.rs +++ b/all-is-cubes/src/block/text.rs @@ -191,6 +191,8 @@ impl Text { }) } + /// Called by [`Primitive::Text`] evaluation to actually produce the voxels for a specific + /// [`Block`] of text. pub(crate) fn evaluate( &self, block_offset: GridVector, @@ -216,19 +218,24 @@ impl Text { brush, block_offset, |text_obj, text_aab, drawing_transform| { - let mut voxels: Vol> = Vol::from_fn( - text_aab - .intersection(GridAab::for_block(self.resolution)) - .unwrap_or(GridAab::ORIGIN_EMPTY), - |_| Evoxel::AIR, - ); + let voxels: Evoxels = + match text_aab.intersection(GridAab::for_block(self.resolution)) { + Some(bounds_in_this_block) => { + let mut voxels: Vol> = + Vol::from_fn(bounds_in_this_block, |_| Evoxel::AIR); - text_obj - .draw(&mut DrawingPlane::new(&mut voxels, drawing_transform)) - .unwrap(); + text_obj + .draw(&mut DrawingPlane::new(&mut voxels, drawing_transform)) + .unwrap(); + + Evoxels::Many(self.resolution, voxels.map_container(Into::into)) + } + + None => Evoxels::One(Evoxel::AIR), + }; Ok(MinEval { - voxels: Evoxels::Many(self.resolution, voxels.map_container(Into::into)), + voxels, attributes: BlockAttributes { display_name: self.string.clone(), ..BlockAttributes::default() @@ -886,5 +893,30 @@ mod tests { ) } + #[test] + fn no_intersection_with_block() { + let block = single_block_test_case({ + Text::builder() + .string(literal!("ab")) + .font(Font::System16) + .layout_bounds( + Resolution::R16, + GridAab::from_lower_size([100000, 0, 0], [16, 16, 16]), + ) + .build() + }); + + let ev = block.evaluate().unwrap(); + assert_eq!( + ev.attributes, + BlockAttributes { + display_name: arcstr::literal!("ab"), + ..BlockAttributes::default() + } + ); + assert_eq!(ev.resolution(), Resolution::R1); + assert!(!ev.visible); + } + // TODO: test that voxel attributes are as expected }