combine quads

This commit is contained in:
zomseffen 2025-02-27 13:34:08 +01:00
parent 457d3e2d6b
commit 5bd181adc9
8 changed files with 181 additions and 116 deletions

View file

@ -718,37 +718,79 @@ impl EmptyVolume {
println!("volume creation took {} s", start_time.elapsed().as_millis() as f32 / 1000.0);
(volumes, neighbors)
}
fn check_quad_index(u: usize, v: usize, vsize: usize, size1: usize, size2: usize, colors: &Vec<Vector3<u8>>, neighbors: &Vec<Option<Rc<RefCell<EmptyVolume>>>>) -> bool {
let index = (u + size1) * vsize + (v + size2);
if colors.len() <= index {
return false;
}
if neighbors.len() > index || neighbors.len() == 1 {
if let Some(_) = neighbors[index.min(neighbors.len() - 1)] {
if colors[index] == (Vector3 {x: 0, y: 0, z: 0}) {
return false;
}
}
}
return true
}
fn grow_quad(u: usize, v: usize, size_u: usize, size_v: usize, colors: &Vec<Vector3<u8>>, neighbors: &Vec<Option<Rc<RefCell<EmptyVolume>>>>) -> (usize, usize) {
let mut size_1 = 0;
let mut size_2 = 0;
let mut grow = true;
let mut v_size_check = 0;
while grow {
for u_size_check in 0..size_u - u {
if EmptyVolume::check_quad_index(u, v, size_v, u_size_check, v_size_check, colors, neighbors) {
size_1 = size_1.max(u_size_check);
} else {
grow = false;
break;
}
}
if grow {
size_2 = v_size_check;
}
v_size_check += 1;
}
(size_1, size_2)
}
// MARK: To Quads
pub fn to_quads(&self) -> Vec<Quad> {
let mut quads = vec![];
let float_pos = Vector3 {x: (self.tree_offset.x * self.tree_size + self.position.x) as f32, y: (self.tree_offset.y * self.tree_size + self.position.y) as f32, z: (self.tree_offset.z * self.tree_size + self.position.z) as f32};
//bottom sides of the volumes, top side of the block
let mut done = vec![];
for x in 0..self.size_x {
for y in 0..self.size_y {
let index = x * self.size_y + y;
if self.color_bottom.len() <= index {
if done.contains(&(x, y)) {
continue;
}
if self.neighbor_bottom.len() > index || self.neighbor_bottom.len() == 1 {
if let Some(_) = self.neighbor_bottom[index.min(self.neighbor_bottom.len() - 1)] {
if self.color_bottom[index] == (Vector3 {x: 0, y: 0, z: 0}) {
continue;
}
}
}
if self.color_bottom[index] == (Vector3 {x: 0, y: 0, z: 0}) {
println!("No neighbor reference, but no color! x: {}, y: {}, z: {}", self.position.x + x, self.position.y + y, self.position.z);
if self.neighbor_bottom.len() == 1 {
println!("neighbor length is one!");
if !EmptyVolume::check_quad_index(x, y, self.size_y, 0, 0, &self.color_bottom, &self.neighbor_bottom) {
continue;
}
let (size_1, size_2) = EmptyVolume::grow_quad(x, y, self.size_x, self.size_y, &self.color_bottom, &self.neighbor_bottom);
done.push((x, y));
for done_x in 0..size_1 + 1 {
for done_y in 0..size_2 + 1 {
done.push((x + done_x, y + done_y));
}
}
let quad = Quad {
pos1: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + y as f32, z: -0.5 },
pos4: float_pos + Vector3 { x: 0.5 + x as f32, y: -0.5 + y as f32, z: -0.5 },
pos3: float_pos + Vector3 { x: 0.5 + x as f32, y: 0.5 + y as f32, z: -0.5 },
pos2: float_pos + Vector3 { x: -0.5 + x as f32, y: 0.5 + y as f32, z: -0.5 },
pos4: float_pos + Vector3 { x: 0.5 + (x + size_1) as f32, y: -0.5 + y as f32, z: -0.5 },
pos3: float_pos + Vector3 { x: 0.5 + (x + size_1) as f32, y: 0.5 + (y + size_2) as f32, z: -0.5 },
pos2: float_pos + Vector3 { x: -0.5 + x as f32, y: 0.5 + (y + size_2) as f32, z: -0.5 },
raster_pos: cgmath::Vector2 { x: x as u32, y: y as u32 },
size: cgmath::Vector2 { x: (size_1 + 1) as u32, y: (size_2 + 1) as u32 },
volume_index: self.memory_start as u32,
facing: vertex::Facing::Bottom
};
@ -756,33 +798,32 @@ impl EmptyVolume {
}
}
//top sides of the volumes, bottom side of the block
let mut done = vec![];
for x in 0..self.size_x {
for y in 0..self.size_y {
let index = x * self.size_y + y;
if self.color_top.len() <= 0 {
if done.contains(&(x, y)) {
continue;
}
if self.neighbor_top.len() > index || self.neighbor_top.len() == 1 {
if let Some(_) = self.neighbor_top[index.min(self.neighbor_top.len() - 1)] {
if self.color_top[index] == (Vector3 {x: 0, y: 0, z: 0}) {
continue;
}
}
if !EmptyVolume::check_quad_index(x, y, self.size_y, 0, 0, &self.color_top, &self.neighbor_top) {
continue;
}
let (size_1, size_2) = EmptyVolume::grow_quad(x, y, self.size_x, self.size_y, &self.color_top, &self.neighbor_top);
if self.color_top[index] == (Vector3 {x: 0, y: 0, z: 0}) {
println!("No neighbor reference, but no color! x: {}, y: {}, z: {}", self.position.x + x, self.position.y + y, self.position.z + self.size_z);
if self.neighbor_top.len() == 1 {
println!("neighbor length is one!");
done.push((x, y));
for done_x in 0..size_1 + 1 {
for done_y in 0..size_2 + 1 {
done.push((x + done_x, y + done_y));
}
}
let quad = Quad {
pos4: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + y as f32, z: self.size_z as f32 - 0.5 },
pos1: float_pos + Vector3 { x: 0.5 + x as f32, y: -0.5 + y as f32, z: self.size_z as f32 - 0.5 },
pos2: float_pos + Vector3 { x: 0.5 + x as f32, y: 0.5 + y as f32, z: self.size_z as f32 - 0.5 },
pos3: float_pos + Vector3 { x: -0.5 + x as f32, y: 0.5 + y as f32, z: self.size_z as f32 - 0.5 },
pos1: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + y as f32, z: self.size_z as f32 - 0.5 },
pos4: float_pos + Vector3 { x: 0.5 + (x + size_1) as f32, y: -0.5 + y as f32, z: self.size_z as f32 - 0.5 },
pos3: float_pos + Vector3 { x: 0.5 + (x + size_1) as f32, y: 0.5 + (y + size_2) as f32, z: self.size_z as f32 - 0.5 },
pos2: float_pos + Vector3 { x: -0.5 + x as f32, y: 0.5 + (y + size_2) as f32, z: self.size_z as f32 - 0.5 },
raster_pos: cgmath::Vector2 { x: x as u32, y: y as u32 },
size: cgmath::Vector2 { x: (size_1 + 1) as u32, y: (size_2 + 1) as u32 },
volume_index: self.memory_start as u32,
facing: vertex::Facing::Top
};
@ -791,32 +832,32 @@ impl EmptyVolume {
}
//front sides of the volumes, back side of the block
let mut done = vec![];
for x in 0..self.size_x {
for z in 0..self.size_z {
let index = x * self.size_z + z;
if self.color_front.len() <= 0 {
if done.contains(&(x, z)) {
continue;
}
if self.neighbor_front.len() > index || self.neighbor_front.len() == 1 {
if let Some(_) = self.neighbor_front[index.min(self.neighbor_front.len() - 1)] {
if self.color_front[index] == (Vector3 {x: 0, y: 0, z: 0}) {
continue;
}
}
if !EmptyVolume::check_quad_index(x, z, self.size_z, 0, 0, &self.color_front, &self.neighbor_front) {
continue;
}
let (size_1, size_2) = EmptyVolume::grow_quad(x, z, self.size_x, self.size_z, &self.color_front, &self.neighbor_front);
if self.color_front[index] == (Vector3 {x: 0, y: 0, z: 0}) {
println!("No neighbor reference, but no color! x: {}, y: {}, z: {}", self.position.x + x, self.position.y, self.position.z + z);
if self.neighbor_front.len() == 1 {
println!("neighbor length is one!");
done.push((x, z));
for done_x in 0..size_1 + 1 {
for done_z in 0..size_2 + 1 {
done.push((x + done_x, z + done_z));
}
}
let quad = Quad {
pos1: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + 0 as f32, z: z as f32 - 0.5 },
pos4: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + 0 as f32, z: z as f32 + 0.5 },
pos3: float_pos + Vector3 { x: 0.5 + x as f32, y: -0.5 + 0 as f32, z: z as f32 + 0.5 },
pos2: float_pos + Vector3 { x: 0.5 + x as f32, y: -0.5 + 0 as f32, z: z as f32 - 0.5 },
pos2: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + 0 as f32, z: z as f32 - 0.5 },
pos1: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + 0 as f32, z: (z + size_2) as f32 + 0.5 },
pos4: float_pos + Vector3 { x: 0.5 + (x + size_1) as f32, y: -0.5 + 0 as f32, z: (z + size_2) as f32 + 0.5 },
pos3: float_pos + Vector3 { x: 0.5 + (x + size_1) as f32, y: -0.5 + 0 as f32, z: z as f32 - 0.5 },
raster_pos: cgmath::Vector2 { x: x as u32, y: z as u32 },
size: cgmath::Vector2 { x: (size_1 + 1) as u32, y: (size_2 + 1) as u32 },
volume_index: self.memory_start as u32,
facing: vertex::Facing::Front
};
@ -825,32 +866,32 @@ impl EmptyVolume {
}
//back sides of the volumes, front side of the block
let mut done = vec![];
for x in 0..self.size_x {
for z in 0..self.size_z {
let index = x * self.size_z + z;
if self.color_back.len() <= 0 {
if done.contains(&(x, z)) {
continue;
}
if self.neighbor_back.len() > index || self.neighbor_back.len() == 1 {
if let Some(_) = self.neighbor_back[index.min(self.neighbor_back.len() - 1)] {
if self.color_back[index] == (Vector3 {x: 0, y: 0, z: 0}) {
continue;
}
}
if !EmptyVolume::check_quad_index(x, z, self.size_z, 0, 0, &self.color_back, &self.neighbor_back) {
continue;
}
let (size_1, size_2) = EmptyVolume::grow_quad(x, z, self.size_x, self.size_z, &self.color_back, &self.neighbor_back);
if self.color_back[index] == (Vector3 {x: 0, y: 0, z: 0}) {
println!("No neighbor reference, but no color! x: {}, y: {}, z: {}", self.position.x + x, self.position.y + self.size_y, self.position.z + z);
if self.neighbor_back.len() == 1 {
println!("neighbor length is one!");
done.push((x, z));
for done_x in 0..size_1 + 1 {
for done_z in 0..size_2 + 1 {
done.push((x + done_x, z + done_z));
}
}
let quad = Quad {
pos4: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + self.size_y as f32, z: z as f32 - 0.5 },
pos1: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + self.size_y as f32, z: z as f32 + 0.5 },
pos2: float_pos + Vector3 { x: 0.5 + x as f32, y: -0.5 + self.size_y as f32, z: z as f32 + 0.5 },
pos3: float_pos + Vector3 { x: 0.5 + x as f32, y: -0.5 + self.size_y as f32, z: z as f32 - 0.5 },
pos1: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + self.size_y as f32, z: z as f32 - 0.5 },
pos2: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + self.size_y as f32, z: (z + size_2) as f32 + 0.5 },
pos3: float_pos + Vector3 { x: 0.5 + (x + size_1) as f32, y: -0.5 + self.size_y as f32, z: (z + size_2) as f32 + 0.5 },
pos4: float_pos + Vector3 { x: 0.5 + (x + size_1) as f32, y: -0.5 + self.size_y as f32, z: z as f32 - 0.5 },
raster_pos: cgmath::Vector2 { x: x as u32, y: z as u32 },
size: cgmath::Vector2 { x: (size_1 + 1) as u32, y: (size_2 + 1) as u32 },
volume_index: self.memory_start as u32,
facing: vertex::Facing::Back
};
@ -859,32 +900,32 @@ impl EmptyVolume {
}
//left sides of the volumes, right side of the block
let mut done = vec![];
for y in 0..self.size_y {
for z in 0..self.size_z {
let index = y * self.size_z + z;
if self.color_left.len() <= 0 {
if done.contains(&(y, z)) {
continue;
}
if self.neighbor_left.len() > index || self.neighbor_left.len() == 1 {
if let Some(_) = self.neighbor_left[index.min(self.neighbor_left.len() - 1)] {
if self.color_left[index] == (Vector3 {x: 0, y: 0, z: 0}) {
continue;
}
}
if !EmptyVolume::check_quad_index(y, z, self.size_z, 0, 0, &self.color_left, &self.neighbor_left) {
continue;
}
let (size_1, size_2) = EmptyVolume::grow_quad(y, z, self.size_y, self.size_z, &self.color_left, &self.neighbor_left);
if self.color_left[index] == (Vector3 {x: 0, y: 0, z: 0}) {
println!("No neighbor reference, but no color! x: {}, y: {}, z: {}", self.position.x, self.position.y + y, self.position.z + z);
if self.neighbor_left.len() == 1 {
println!("neighbor length is one!");
done.push((y, z));
for done_y in 0..size_1 + 1 {
for done_z in 0..size_2 + 1 {
done.push((y + done_y, z + done_z));
}
}
let quad = Quad {
pos4: float_pos + Vector3 { x: -0.5 + 0.0 as f32, y: y as f32 - 0.5, z: z as f32 - 0.5 },
pos1: float_pos + Vector3 { x: -0.5 + 0.0 as f32, y: y as f32 - 0.5, z: z as f32 + 0.5 },
pos2: float_pos + Vector3 { x: -0.5 + 0.0 as f32, y: y as f32 + 0.5, z: z as f32 + 0.5 },
pos3: float_pos + Vector3 { x: -0.5 + 0.0 as f32, y: y as f32 + 0.5, z: z as f32 - 0.5 },
pos1: float_pos + Vector3 { x: -0.5 + 0.0 as f32, y: y as f32 - 0.5, z: z as f32 - 0.5 },
pos2: float_pos + Vector3 { x: -0.5 + 0.0 as f32, y: y as f32 - 0.5, z: (z + size_2) as f32 + 0.5 },
pos3: float_pos + Vector3 { x: -0.5 + 0.0 as f32, y: (y + size_1) as f32 + 0.5, z: (z + size_2) as f32 + 0.5 },
pos4: float_pos + Vector3 { x: -0.5 + 0.0 as f32, y: (y + size_1) as f32 + 0.5, z: z as f32 - 0.5 },
raster_pos: cgmath::Vector2 { x: y as u32, y: z as u32 },
size: cgmath::Vector2 { x: (size_1 + 1) as u32, y: (size_2 + 1) as u32 },
volume_index: self.memory_start as u32,
facing: vertex::Facing::Left
};
@ -893,32 +934,32 @@ impl EmptyVolume {
}
//right sides of the volumes, left side of the block
let mut done = vec![];
for y in 0..self.size_y {
for z in 0..self.size_z {
let index = y * self.size_z + z;
if self.color_right.len() <= 0 {
if done.contains(&(y, z)) {
continue;
}
if self.neighbor_right.len() > index || self.neighbor_right.len() == 1 {
if let Some(_) = self.neighbor_right[index.min(self.neighbor_right.len() - 1)] {
if self.color_right[index] == (Vector3 {x: 0, y: 0, z: 0}) {
continue;
}
}
if !EmptyVolume::check_quad_index(y, z, self.size_z, 0, 0, &self.color_right, &self.neighbor_right) {
continue;
}
let (size_1, size_2) = EmptyVolume::grow_quad(y, z, self.size_y, self.size_z, &self.color_right, &self.neighbor_right);
if self.color_right[index] == (Vector3 {x: 0, y: 0, z: 0}) {
println!("No neighbor reference, but no color! x: {}, y: {}, z: {}", self.position.x + self.size_x, self.position.y + y, self.position.z + z);
if self.neighbor_right.len() == 1 {
println!("neighbor length is one!");
done.push((y, z));
for done_y in 0..size_1 + 1 {
for done_z in 0..size_2 + 1 {
done.push((y + done_y, z + done_z));
}
}
let quad = Quad {
pos1: float_pos + Vector3 { x: -0.5 + self.size_x as f32, y: y as f32 - 0.5, z: z as f32 - 0.5 },
pos4: float_pos + Vector3 { x: -0.5 + self.size_x as f32, y: y as f32 - 0.5, z: z as f32 + 0.5 },
pos3: float_pos + Vector3 { x: -0.5 + self.size_x as f32, y: y as f32 + 0.5, z: z as f32 + 0.5 },
pos2: float_pos + Vector3 { x: -0.5 + self.size_x as f32, y: y as f32 + 0.5, z: z as f32 - 0.5 },
pos2: float_pos + Vector3 { x: -0.5 + self.size_x as f32, y: y as f32 - 0.5, z: z as f32 - 0.5 },
pos1: float_pos + Vector3 { x: -0.5 + self.size_x as f32, y: y as f32 - 0.5, z: (z + size_2) as f32 + 0.5 },
pos4: float_pos + Vector3 { x: -0.5 + self.size_x as f32, y: (y + size_1) as f32 + 0.5, z: (z + size_2) as f32 + 0.5 },
pos3: float_pos + Vector3 { x: -0.5 + self.size_x as f32, y: (y + size_1) as f32 + 0.5, z: z as f32 - 0.5 },
raster_pos: cgmath::Vector2 { x: y as u32, y: z as u32 },
size: cgmath::Vector2 { x: (size_1 + 1) as u32, y: (size_2 + 1) as u32 },
volume_index: self.memory_start as u32,
facing: vertex::Facing::Right
};