Skip to content

Commit 43f0fab

Browse files
committed
feat: some refactorings
1 parent 2f68c66 commit 43f0fab

File tree

5 files changed

+23
-54
lines changed

5 files changed

+23
-54
lines changed

src/blocks/block.rs

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use bytemuck::{Pod, Zeroable};
22

33
use super::block_type::BlockType;
4+
use crate::chunk::BlockVec;
45
use crate::world::CHUNK_SIZE;
56
use glam::Vec3;
67
use std::sync::{Arc, MutexGuard, RwLock};
@@ -39,53 +40,10 @@ pub enum FaceDirections {
3940
}
4041

4142
impl FaceDirections {
42-
pub fn create_face_data(&self, block: Arc<RwLock<Block>>) -> (Vec<BlockVertexData>, Vec<u32>) {
43-
let indices = self.get_indices();
44-
45-
let mut unique_indices: Vec<u32> = Vec::with_capacity(4);
46-
47-
let mut vertex_data: Vec<BlockVertexData> = Vec::with_capacity(4);
48-
49-
let mut indices_map: Vec<u32> = vec![0; 6];
50-
51-
for ind in indices.iter() {
52-
if unique_indices.contains(ind) {
53-
continue;
54-
} else {
55-
unique_indices.push(*ind);
56-
}
57-
}
58-
for (i, indices_map) in indices_map.iter_mut().enumerate() {
59-
let index_of = unique_indices
60-
.iter()
61-
.enumerate()
62-
.find_map(|(k, ind)| if *ind == indices[i] { Some(k) } else { None })
63-
.unwrap();
64-
*indices_map = index_of as u32;
65-
}
66-
67-
let block_read = block.read().unwrap();
68-
let face_texcoords = block_read.block_type.get_texcoords(*self);
69-
let normals = self.get_normal_vector();
70-
71-
unique_indices.iter().enumerate().for_each(|(i, index)| {
72-
vertex_data.push(BlockVertexData {
73-
position: [
74-
CUBE_VERTEX[(*index as usize * 3 + 0) as usize] + block_read.position.x,
75-
CUBE_VERTEX[(*index as usize * 3 + 1) as usize] + block_read.position.y,
76-
CUBE_VERTEX[(*index as usize * 3 + 2) as usize] + block_read.position.z,
77-
],
78-
normal: normals.into(),
79-
tex_coords: face_texcoords[i],
80-
})
81-
});
82-
83-
(vertex_data, indices_map)
84-
}
85-
86-
pub fn create_face_data_abs(
43+
pub fn create_face_data(
8744
&self,
8845
block: Arc<RwLock<Block>>,
46+
blocks: &BlockVec,
8947
) -> (Vec<BlockVertexData>, Vec<u32>) {
9048
let indices = self.get_indices();
9149

@@ -118,10 +76,11 @@ impl FaceDirections {
11876
unique_indices.iter().enumerate().for_each(|(i, index)| {
11977
vertex_data.push(BlockVertexData {
12078
position: [
121-
CUBE_VERTEX[(*index as usize * 3 + 0) as usize] + block_read.absolute_position.x,
122-
CUBE_VERTEX[(*index as usize * 3 + 1) as usize] + block_read.absolute_position.y,
123-
CUBE_VERTEX[(*index as usize * 3 + 2) as usize] + block_read.absolute_position.z,
79+
CUBE_VERTEX[(*index as usize * 3 + 0) as usize] + block_read.position.x,
80+
CUBE_VERTEX[(*index as usize * 3 + 1) as usize] + block_read.position.y,
81+
CUBE_VERTEX[(*index as usize * 3 + 2) as usize] + block_read.position.z,
12482
],
83+
ao: 0.0,
12584
normal: normals.into(),
12685
tex_coords: face_texcoords[i],
12786
})
@@ -137,6 +96,7 @@ pub struct BlockVertexData {
13796
pub position: [f32; 3],
13897
pub normal: [f32; 3],
13998
pub tex_coords: [f32; 2],
99+
pub ao: f32,
140100
}
141101

142102
impl Block {

src/chunk.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl Chunk {
166166

167167
if is_visible {
168168
let (mut vertex_data, index_data) =
169-
face.create_face_data(block_ptr.clone());
169+
face.create_face_data(block_ptr.clone(), &self.blocks.clone());
170170
vertex.append(&mut vertex_data);
171171
let indices_offset = vertex.len() as u32 - 4;
172172
indices.append(
@@ -179,6 +179,7 @@ impl Chunk {
179179
}
180180
}
181181

182+
182183
let chunk_vertex_buffer =
183184
self.device
184185
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
@@ -293,7 +294,6 @@ impl Chunk {
293294
for block in tree_blocks.iter() {
294295
let block_brw = block.read().unwrap();
295296
let block_chunk = block_brw.get_chunk_coords();
296-
std::mem::drop(block_brw);
297297
if block_chunk == (self.x, self.y) {
298298
self.add_block(block.clone());
299299
} else {

src/state.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ impl State {
214214
let new_block_abs_position =
215215
block_borrow.absolute_position + facing_face.get_normal_vector();
216216

217-
std::mem::drop(block_borrow); /* Prevent deadlock since we'll be using when building the mesh */
218217
let chunk = new_block_abs_position.get_chunk_from_position_absolute();
219218
let position = new_block_abs_position.relative_from_absolute();
220219

src/ui.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,20 @@ impl UI {
4848
.iter()
4949
.find(|f| **f == player.facing_face.unwrap())
5050
.unwrap()
51-
.create_face_data_abs(block_ptr.clone());
51+
.create_face_data(block_ptr.clone(), &vec![]);
5252

53-
let blocks_position = face_data.0.iter().map(|v| v.position).collect::<Vec<_>>();
53+
let blocks_position = face_data
54+
.0
55+
.iter()
56+
.map(|v| {
57+
[
58+
// TODO: This is kinda ugly
59+
v.position[0] + (block.absolute_position.x - block.position.x),
60+
v.position[1] + (block.absolute_position.y - block.position.y),
61+
v.position[2] + (block.absolute_position.z - block.position.z),
62+
]
63+
})
64+
.collect::<Vec<_>>();
5465

5566
self.indices = face_data.1.len();
5667
queue.write_buffer(

src/world.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ impl World {
8787
.expect("Cannot delete a block from unloaded chunk");
8888

8989
chunks_to_rerender.push(chunk.clone());
90-
std::mem::drop(block_borrow);
9190
let mut chunk_lock = chunk.write().unwrap();
9291
chunk_lock.add_block(block.clone());
9392

0 commit comments

Comments
 (0)