RAY CASTED OCT TREE RENDERING

This commit is contained in:
2024-06-25 22:06:19 -04:00
parent 3280a0ed53
commit 653a1192e0
18 changed files with 1468 additions and 230 deletions

View File

@@ -0,0 +1,52 @@
use bevy_ecs::component::Component;
use block_mesh::{ndshape::RuntimeShape, UnitQuadBuffer, RIGHT_HANDED_Y_UP_CONFIG};
use nalgebra::Vector3;
use ndarray::{ArrayView3, Axis};
use crate::client::render::voxel::{VoxelColor, /*VoxelFace*/};
#[derive(Debug, Clone, Component)]
pub struct ChunkMesh {
// pub faces: [Vec<VoxelFace>; 6],
}
impl ChunkMesh {
pub fn from_data(data: ArrayView3<VoxelColor>) -> Self {
// let dim_pad = Vector3::new(
// data.len_of(Axis(0)) as u32,
// data.len_of(Axis(1)) as u32,
// data.len_of(Axis(2)) as u32,
// );
// let dim = dim_pad - Vector3::from_element(2);
// let mut buffer = UnitQuadBuffer::new();
// let shape = RuntimeShape::<u32, 3>::new(dim_pad.into());
// let slice = data.as_slice().unwrap();
// block_mesh::visible_block_faces(
// slice,
// &shape,
// [0; 3],
// (dim_pad - Vector3::new(1, 1, 1)).into(),
// &RIGHT_HANDED_Y_UP_CONFIG.faces,
// &mut buffer,
// );
// let faces = [2, 1, 0, 5, 4, 3].map(|f| {
// buffer.groups[f]
// .iter()
// .map(|a| {
// let i = (a.minimum[0] - 1)
// + (a.minimum[1] - 1) * dim.y
// + (a.minimum[2] - 1) * dim.y * dim.x;
// let i_pad = a.minimum[0]
// + a.minimum[1] * dim_pad.y
// + a.minimum[2] * dim_pad.y * dim_pad.x;
// VoxelFace {
// index: i,
// color: slice[i_pad as usize],
// }
// })
// .collect()
// });
Self { /*faces*/ }
}
}

View File

@@ -0,0 +1,80 @@
mod mesh;
pub use mesh::*;
use std::collections::{HashMap, HashSet};
use crate::util::oct_tree::OctTree;
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{bundle::Bundle, component::Component, entity::Entity, system::Resource};
use nalgebra::Vector3;
pub const SIDE_LENGTH: usize = 16 * 16;
pub const SHAPE: (usize, usize, usize) = (SIDE_LENGTH, SIDE_LENGTH, SIDE_LENGTH);
pub const DIMENSIONS: Vector3<usize> = Vector3::new(SIDE_LENGTH, SIDE_LENGTH, SIDE_LENGTH);
pub const LEN: usize = SHAPE.0 * SHAPE.1 * SHAPE.2;
#[derive(Debug, Component, Clone, Deref, DerefMut)]
pub struct ChunkData {
#[deref]
data: OctTree,
}
impl ChunkData {
pub fn from_tree(t: OctTree) -> Self {
Self { data: t }
}
pub fn empty() -> Self {
Self {
data: OctTree::from_leaf(0, 8),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Component, Default, Deref, DerefMut)]
pub struct ChunkPos(pub Vector3<i32>);
impl ChunkPos {
pub fn new(x: i32, y: i32, z: i32) -> Self {
Self(Vector3::new(x, y, z))
}
}
impl From<Vector3<i32>> for ChunkPos {
fn from(val: Vector3<i32>) -> Self {
ChunkPos(val)
}
}
#[derive(Debug, Clone, Component, Deref, DerefMut)]
pub struct LoadedChunks {
loaded: HashSet<ChunkPos>,
}
impl LoadedChunks {
pub fn new() -> Self {
Self {
loaded: HashSet::new(),
}
}
}
#[derive(Resource, Deref, DerefMut)]
pub struct ChunkMap {
#[deref]
map: HashMap<ChunkPos, Entity>,
pub generating: HashSet<ChunkPos>,
}
impl ChunkMap {
pub fn new() -> Self {
Self {
map: HashMap::new(),
generating: HashSet::new(),
}
}
}
#[derive(Bundle, Clone)]
pub struct ChunkBundle {
pub pos: ChunkPos,
pub data: ChunkData,
pub mesh: ChunkMesh,
}