shits wack yo

This commit is contained in:
Chris Bras 2021-06-02 16:07:59 +02:00
parent 37b0c99d16
commit 071f300368
4 changed files with 103 additions and 5 deletions

View file

@ -11,6 +11,7 @@ mod time;
mod uniforms; mod uniforms;
mod vertex; mod vertex;
mod world; mod world;
mod npc;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use wgpu::SwapChainError; use wgpu::SwapChainError;

View file

@ -1,5 +1,67 @@
pub struct Npc { extern crate gltf;
Vector3<f32> position; extern crate wgpu;
Vector3<f32> scale;
Vector3<f32> rotation; use cgmath::{Vector3};
use crate::{
vertex::Vertex,
}; };
pub struct Npc {
pub position: Vector3<f32>,
pub scale: Vector3<f32>,
pub rotation: Vector3<f32>,
pub vertices: Vec<Vertex>,
pub indices: Vec<u32>,
pub vertex_buffer: Option<wgpu::Buffer>,
pub index_buffer: Option<wgpu::Buffer>,
}
impl Npc {
pub fn load() -> Self {
let position: Vector3<f32> = Vector3::new(0.0, 0.0, 0.0);
let scale: Vector3<f32> = Vector3::new(0.0, 0.0, 0.0);
let rotation: Vector3<f32> = Vector3::new(0.0, 0.0, 0.0);
let (model, buffers, _) = gltf::import("assets/models/minecrab.glb").unwrap();
let mut indices: Vec<u32> = Vec::new();
let mut vertices: Vec<Vertex> = Vec::new();
for mesh in model.meshes() {
for primitive in mesh.primitives() {
let reader = primitive.reader(|buffer| Some(&buffers[buffer.index()]));
indices = reader.read_indices().unwrap().into_u32().collect();
// loop over all primitives and get the normals, position and color
let pos_iter = reader.read_positions().unwrap();
let norm_iter = reader.read_normals().unwrap();
let tex_iter = reader.read_tex_coords(0).unwrap().into_f32();
for it in pos_iter.zip(norm_iter).zip(tex_iter) {
let ((position, normal), [tex_x, tex_y]) = it;
let current_vert: Vertex = Vertex {
position,
texture_coordinates: [tex_x, tex_y, 0.0],
normal,
highlighted: 0
};
vertices.push(current_vert);
}
}
}
return Self {
position,
scale,
rotation,
indices,
vertices,
vertex_buffer: None,
index_buffer: None
};
}
}

View file

@ -258,6 +258,27 @@ impl WorldState {
println!("World update took {:?}", elapsed); println!("World update took {:?}", elapsed);
} }
pub fn load_npc_geometry(
&mut self,
render_context: &RenderContext,
) {
self.world.npc.vertex_buffer = Some(render_context
.device
.create_buffer_init(&BufferInitDescriptor {
label: None,
contents: &bytemuck::cast_slice(&self.world.npc.vertices),
usage: wgpu::BufferUsage::VERTEX,
}));
self.world.npc.index_buffer = Some(render_context
.device
.create_buffer_init(&BufferInitDescriptor {
label: None,
contents: &bytemuck::cast_slice(&self.world.npc.indices),
usage: wgpu::BufferUsage::INDEX,
}));
}
pub fn update_chunk_geometry( pub fn update_chunk_geometry(
&mut self, &mut self,
render_context: &RenderContext, render_context: &RenderContext,
@ -370,6 +391,7 @@ impl WorldState {
}; };
world_state.update_world_geometry(render_context); world_state.update_world_geometry(render_context);
world_state.load_npc_geometry(render_context);
world_state world_state
} }
@ -425,6 +447,15 @@ impl WorldState {
triangle_count += index_count / 3; triangle_count += index_count / 3;
} }
{
let vertex_buffer = self.world.npc.vertex_buffer.unwrap();
let index_buffer = self.world.npc.index_buffer.unwrap();
render_pass.set_vertex_buffer(0, vertex_buffer.slice(..));
render_pass.set_index_buffer(index_buffer.slice(..), wgpu::IndexFormat::Uint32);
render_pass.draw_indexed(0..self.world.npc.indices.len() as u32 , 0, 0..1);
}
triangle_count triangle_count
} }

View file

@ -1,12 +1,14 @@
use crate::{ use crate::{
chunk::{Block, Chunk, CHUNK_SIZE}, chunk::{Block, Chunk, CHUNK_SIZE},
vertex::Vertex, vertex::Vertex,
npc::Npc,
}; };
use cgmath::{InnerSpace, Vector3}; use cgmath::{InnerSpace, Vector3};
use rayon::prelude::*; use rayon::prelude::*;
pub struct World { pub struct World {
pub chunks: Vec<Vec<Vec<Chunk>>>, pub chunks: Vec<Vec<Vec<Chunk>>>,
pub npc: Npc
} }
const WORLD_SIZE: Vector3<usize> = Vector3::new( const WORLD_SIZE: Vector3<usize> = Vector3::new(
@ -19,6 +21,8 @@ impl World {
pub fn generate() -> Self { pub fn generate() -> Self {
let mut chunks = Vec::new(); let mut chunks = Vec::new();
let npc = Npc::load();
(0..WORLD_SIZE.y) (0..WORLD_SIZE.y)
.into_par_iter() .into_par_iter()
.map(|y| { .map(|y| {
@ -35,7 +39,7 @@ impl World {
}) })
.collect_into_vec(&mut chunks); .collect_into_vec(&mut chunks);
Self { chunks } Self { chunks, npc }
} }
pub fn highlighted_for_chunk( pub fn highlighted_for_chunk(