mesh generation
This commit is contained in:
@@ -1,6 +1,73 @@
|
||||
mod data;
|
||||
use data::Chunk;
|
||||
use crate::{
|
||||
hex_utils::{to_hex_pos, INNER_RADIUS, OUTER_RADIUS},
|
||||
prelude::*,
|
||||
};
|
||||
use bevy::{
|
||||
prelude::*,
|
||||
render::{
|
||||
mesh::{Indices, PrimitiveTopology},
|
||||
render_asset::RenderAssetUsages,
|
||||
},
|
||||
};
|
||||
|
||||
pub fn generate_chunk_mesh(chunk: &Chunk){
|
||||
const HEX_CORNERS: [Vec3; 6] = [
|
||||
Vec3::new(0., 0., OUTER_RADIUS),
|
||||
Vec3::new(INNER_RADIUS, 0., 0.5 * OUTER_RADIUS),
|
||||
Vec3::new(INNER_RADIUS, 0., -0.5 * OUTER_RADIUS),
|
||||
Vec3::new(0., 0., -OUTER_RADIUS),
|
||||
Vec3::new(-INNER_RADIUS, 0., -0.5 * OUTER_RADIUS),
|
||||
Vec3::new(-INNER_RADIUS, 0., 0.5 * OUTER_RADIUS),
|
||||
];
|
||||
|
||||
}
|
||||
pub fn generate_chunk_mesh(chunk: &Chunk) -> Mesh {
|
||||
let vertex_count: usize = chunk.size * chunk.size;
|
||||
let mut verts = Vec::with_capacity(vertex_count);
|
||||
let mut uvs = Vec::with_capacity(vertex_count);
|
||||
let mut normals = Vec::with_capacity(vertex_count);
|
||||
let mut indices = Vec::with_capacity(vertex_count);
|
||||
|
||||
for z in 0..chunk.size {
|
||||
for x in 0..chunk.size {
|
||||
let height = chunk.points[x + z * chunk.size];
|
||||
let off_pos = Vec3::new(x as f32, height, z as f32);
|
||||
let grid_pos = to_hex_pos(off_pos);
|
||||
create_tile(grid_pos, &mut verts, &mut uvs, &mut normals, &mut indices);
|
||||
}
|
||||
}
|
||||
|
||||
let mesh = Mesh::new(
|
||||
PrimitiveTopology::TriangleList,
|
||||
RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD,
|
||||
)
|
||||
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, verts)
|
||||
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
|
||||
// .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
|
||||
.with_inserted_indices(Indices::U32(indices))
|
||||
.with_duplicated_vertices()
|
||||
.with_computed_flat_normals();
|
||||
return mesh;
|
||||
}
|
||||
|
||||
//TODO: figure out texture index
|
||||
fn create_tile(
|
||||
pos: Vec3,
|
||||
verts: &mut Vec<Vec3>,
|
||||
uvs: &mut Vec<Vec2>,
|
||||
normals: &mut Vec<Vec3>,
|
||||
indices: &mut Vec<u32>,
|
||||
) {
|
||||
let idx = verts.len() as u32;
|
||||
let center = Vec3::new(pos.x, 0., pos.z);
|
||||
normals.push(Vec3::Y);
|
||||
uvs.push(pos.xz());
|
||||
verts.push(pos);
|
||||
for i in 0..6 {
|
||||
let p = pos + HEX_CORNERS[i];
|
||||
verts.push(p);
|
||||
uvs.push(p.xz());
|
||||
normals.push((p - center).normalize());
|
||||
indices.push(idx);
|
||||
indices.push(idx + 1 + i as u32);
|
||||
indices.push(idx + 1 + ((i as u32 + 1) % 6));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user