From 09197049cb0d95b9923b1e3430aec47344b8201f Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Fri, 27 Sep 2019 11:06:32 +0100 Subject: [PATCH] Made meshing considerably faster --- voxygen/src/mesh/segment.rs | 8 ++----- voxygen/src/mesh/terrain.rs | 5 ++-- voxygen/src/mesh/vol.rs | 47 +++++++++++++++++++++++++++---------- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/voxygen/src/mesh/segment.rs b/voxygen/src/mesh/segment.rs index 3d8657f4eb..659cb6c926 100644 --- a/voxygen/src/mesh/segment.rs +++ b/voxygen/src/mesh/segment.rs @@ -25,14 +25,12 @@ impl Meshable for Segment { for (pos, vox) in self.full_vol_iter() { if let Some(col) = vox.get_color() { - let col = col.map(|e| e as f32 / 255.0); - vol::push_vox_verts( &mut mesh, self, pos, offs + pos.map(|e| e as f32), - &[[[Some(col); 3]; 3]; 3], + &[[[Rgba::from_opaque(col); 3]; 3]; 3], |origin, norm, col, ao, light| { FigureVertex::new( origin, @@ -84,14 +82,12 @@ impl Meshable for Segment { for (pos, vox) in self.full_vol_iter() { if let Some(col) = vox.get_color() { - let col = col.map(|e| e as f32 / 255.0); - vol::push_vox_verts( &mut mesh, self, pos, offs + pos.map(|e| e as f32), - &[[[Some(col); 3]; 3]; 3], + &[[[Rgba::from_opaque(col); 3]; 3]; 3], |origin, norm, col, ao, light| { SpriteVertex::new( origin, diff --git a/voxygen/src/mesh/terrain.rs b/voxygen/src/mesh/terrain.rs index bb38183b15..b0a8a8755d 100644 --- a/voxygen/src/mesh/terrain.rs +++ b/voxygen/src/mesh/terrain.rs @@ -160,10 +160,11 @@ impl + ReadVol + Debug> Meshable( - vol: &V, + _vol: &V, pos: Vec3, shift: Vec3, dirs: &[Vec3], @@ -20,22 +21,40 @@ fn get_ao_quad( ) -> Vec4<(f32, f32)> { dirs.windows(2) .map(|offs| { + let vox_opaque = |pos: Vec3| { + let pos = (pos + 1).map(|e| e as usize); + unsafe { + darknesses + .get_unchecked(pos.z) + .get_unchecked(pos.y) + .get_unchecked(pos.x) + <= &0.0 + } + }; + let (s1, s2) = ( + vox_opaque(shift + offs[0]), + vox_opaque(shift + offs[1]), + /* vol.get(pos + shift + offs[0]) .map(&is_opaque) .unwrap_or(false), vol.get(pos + shift + offs[1]) .map(&is_opaque) .unwrap_or(false), + */ ); let mut darkness = 0.0; for x in 0..2 { for y in 0..2 { let dark_pos = shift + offs[0] * x + offs[1] * y + 1; - darkness += darknesses[dark_pos.z as usize][dark_pos.y as usize] - [dark_pos.x as usize] - / 4.0; + darkness += unsafe { + darknesses + .get_unchecked(dark_pos.z as usize) + .get_unchecked(dark_pos.y as usize) + .get_unchecked(dark_pos.x as usize) + } / 4.0; } } @@ -66,27 +85,29 @@ fn get_col_quad( _pos: Vec3, _shift: Vec3, dirs: &[Vec3], - cols: &[[[Option>; 3]; 3]; 3], + cols: &[[[Rgba; 3]; 3]; 3], _is_opaque: impl Fn(&V::Vox) -> bool, ) -> Vec4> { dirs.windows(2) .map(|offs| { - let primary_col = cols[1][1][1].unwrap_or(Rgb::zero()); + let primary_col = Rgb::from(cols[1][1][1]).map(|e: u8| e as f32); let mut color = Rgb::zero(); let mut total = 0.0; for x in 0..2 { for y in 0..2 { let col_pos = offs[0] * x + offs[1] * y + 1; - if let Some(col) = unsafe { + let col = unsafe { cols.get_unchecked(col_pos.z as usize) .get_unchecked(col_pos.y as usize) .get_unchecked(col_pos.x as usize) - } { - if Vec3::::from(primary_col).distance_squared(Vec3::from(*col)) - < 0.25 * 0.25 + }; + if col.a > 0 { + let col = Rgb::new(col.r, col.g, col.b).map(|e| e as f32); + if Vec3::::from(primary_col).distance_squared(Vec3::from(col)) + < (0.25f32 * 256.0).powf(2.0) { - color += *col; - total += 1.0; + color += col; + total += 256.0; } } } @@ -148,7 +169,7 @@ pub fn push_vox_verts( vol: &V, pos: Vec3, offs: Vec3, - cols: &[[[Option>; 3]; 3]; 3], + cols: &[[[Rgba; 3]; 3]; 3], vcons: impl Fn(Vec3, Vec3, Rgb, f32, f32) -> P::Vertex, error_makes_face: bool, darknesses: &[[[f32; 3]; 3]; 3],