tree dissolve + instances
This commit is contained in:
@@ -2,6 +2,26 @@ use bytemuck::{Pod, Zeroable};
|
||||
use glam::{Mat4, Quat, Vec3};
|
||||
use std::path::Path;
|
||||
|
||||
fn compute_aabb(vertices: &[Vertex]) -> (Vec3, Vec3)
|
||||
{
|
||||
let mut aabb_min = Vec3::splat(f32::MAX);
|
||||
let mut aabb_max = Vec3::splat(f32::MIN);
|
||||
for v in vertices
|
||||
{
|
||||
let p = Vec3::from(v.position);
|
||||
aabb_min = aabb_min.min(p);
|
||||
aabb_max = aabb_max.max(p);
|
||||
}
|
||||
if vertices.is_empty()
|
||||
{
|
||||
(Vec3::ZERO, Vec3::ZERO)
|
||||
}
|
||||
else
|
||||
{
|
||||
(aabb_min, aabb_max)
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Pod, Zeroable)]
|
||||
pub struct Vertex
|
||||
@@ -113,6 +133,10 @@ pub struct Mesh
|
||||
pub vertex_buffer: wgpu::Buffer,
|
||||
pub index_buffer: wgpu::Buffer,
|
||||
pub num_indices: u32,
|
||||
pub aabb_min: Vec3,
|
||||
pub aabb_max: Vec3,
|
||||
pub cpu_positions: Vec<[f32; 3]>,
|
||||
pub cpu_indices: Vec<u32>,
|
||||
}
|
||||
|
||||
impl Mesh
|
||||
@@ -121,6 +145,8 @@ impl Mesh
|
||||
{
|
||||
use wgpu::util::DeviceExt;
|
||||
|
||||
let (aabb_min, aabb_max) = compute_aabb(vertices);
|
||||
|
||||
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Vertex Buffer"),
|
||||
contents: bytemuck::cast_slice(vertices),
|
||||
@@ -137,6 +163,10 @@ impl Mesh
|
||||
vertex_buffer,
|
||||
index_buffer,
|
||||
num_indices: indices.len() as u32,
|
||||
aabb_min,
|
||||
aabb_max,
|
||||
cpu_positions: vertices.iter().map(|v| v.position).collect(),
|
||||
cpu_indices: indices.to_vec(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user