forked from StrafesNET/strafe-client
instanced rendering
This commit is contained in:
parent
d16485ae6d
commit
7e427b3879
36
src/main.rs
36
src/main.rs
@ -19,13 +19,13 @@ struct Entity {
|
|||||||
|
|
||||||
//temp?
|
//temp?
|
||||||
struct ModelData {
|
struct ModelData {
|
||||||
transform: glam::Mat4,
|
transforms: Vec<glam::Mat4>,
|
||||||
vertex_buf: wgpu::Buffer,
|
vertex_buf: wgpu::Buffer,
|
||||||
entities: Vec<Entity>,
|
entities: Vec<Entity>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ModelGraphics {
|
struct ModelGraphics {
|
||||||
transform: glam::Mat4,
|
transforms: Vec<glam::Mat4>,
|
||||||
vertex_buf: wgpu::Buffer,
|
vertex_buf: wgpu::Buffer,
|
||||||
entities: Vec<Entity>,
|
entities: Vec<Entity>,
|
||||||
bind_group: wgpu::BindGroup,
|
bind_group: wgpu::BindGroup,
|
||||||
@ -152,9 +152,13 @@ impl Skybox {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_transform_uniform_data(transform:&glam::Mat4) -> [f32; 4*4] {
|
fn get_transform_uniform_data(transforms:&Vec<glam::Mat4>) -> Vec<f32> {
|
||||||
let mut raw = [0f32; 4*4];
|
let mut raw = Vec::with_capacity(4*4*transforms.len());
|
||||||
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(transform)[..]);
|
for (i,t) in transforms.iter().enumerate(){
|
||||||
|
let mut v = raw.split_off(4*4*i);
|
||||||
|
raw.extend_from_slice(&AsRef::<[f32; 4*4]>::as_ref(t)[..]);
|
||||||
|
raw.append(&mut v);
|
||||||
|
}
|
||||||
raw
|
raw
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,7 +204,7 @@ fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec<ModelData>,data:obj::ObjDat
|
|||||||
usage: wgpu::BufferUsages::VERTEX,
|
usage: wgpu::BufferUsages::VERTEX,
|
||||||
});
|
});
|
||||||
modeldatas.push(ModelData {
|
modeldatas.push(ModelData {
|
||||||
transform: glam::Mat4::default(),
|
transforms: vec![glam::Mat4::default()],
|
||||||
vertex_buf,
|
vertex_buf,
|
||||||
entities,
|
entities,
|
||||||
})
|
})
|
||||||
@ -246,9 +250,13 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
add_obj(device,& mut modeldatas,obj::ObjData::load_buf(&include_bytes!("../models/teapot.obj")[..]).unwrap());
|
add_obj(device,& mut modeldatas,obj::ObjData::load_buf(&include_bytes!("../models/teapot.obj")[..]).unwrap());
|
||||||
add_obj(device,& mut modeldatas,ground);
|
add_obj(device,& mut modeldatas,ground);
|
||||||
println!("models.len = {:?}", modeldatas.len());
|
println!("models.len = {:?}", modeldatas.len());
|
||||||
modeldatas[1].transform=glam::Mat4::from_translation(glam::vec3(10.,5.,10.));
|
modeldatas[0].transforms[0]=glam::Mat4::from_translation(glam::vec3(10.,0.,-10.));
|
||||||
modeldatas[2].transform=glam::Mat4::from_translation(glam::vec3(-10.,5.,10.));
|
modeldatas[1].transforms[0]=glam::Mat4::from_translation(glam::vec3(10.,5.,10.));
|
||||||
modeldatas[3].transform=glam::Mat4::from_translation(glam::vec3(0.,-10.,0.))*glam::Mat4::from_scale(glam::vec3(160.0, 1.0, 160.0));
|
modeldatas[1].transforms.push(glam::Mat4::from_translation(glam::vec3(20.,5.,10.)));
|
||||||
|
modeldatas[1].transforms.push(glam::Mat4::from_translation(glam::vec3(10.,5.,20.)));
|
||||||
|
modeldatas[1].transforms.push(glam::Mat4::from_translation(glam::vec3(20.,5.,20.)));
|
||||||
|
modeldatas[2].transforms[0]=glam::Mat4::from_translation(glam::vec3(-10.,5.,10.));
|
||||||
|
modeldatas[3].transforms[0]=glam::Mat4::from_translation(glam::vec3(0.,0.,0.))*glam::Mat4::from_scale(glam::vec3(160.0, 1.0, 160.0));
|
||||||
|
|
||||||
let main_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
let main_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
label: None,
|
label: None,
|
||||||
@ -326,7 +334,7 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
temp_control_dir: glam::Vec3::ZERO,
|
temp_control_dir: glam::Vec3::ZERO,
|
||||||
walkspeed: 18.0,
|
walkspeed: 18.0,
|
||||||
contacts: std::collections::HashSet::new(),
|
contacts: std::collections::HashSet::new(),
|
||||||
models_cringe_clone: modeldatas.iter().map(|m|strafe_client::body::Model::new(m.transform)).collect(),
|
models_cringe_clone: modeldatas.iter().map(|m|m.transforms.iter().map(|t|strafe_client::body::Model::new(*t))).flatten().collect(),
|
||||||
walk: strafe_client::body::WalkState::new(),
|
walk: strafe_client::body::WalkState::new(),
|
||||||
hitbox_halfsize: glam::vec3(1.0,2.5,1.0),
|
hitbox_halfsize: glam::vec3(1.0,2.5,1.0),
|
||||||
};
|
};
|
||||||
@ -341,7 +349,7 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
//drain the modeldata vec so entities can be /moved/ to models.entities
|
//drain the modeldata vec so entities can be /moved/ to models.entities
|
||||||
let mut models = Vec::<ModelGraphics>::with_capacity(modeldatas.len());
|
let mut models = Vec::<ModelGraphics>::with_capacity(modeldatas.len());
|
||||||
for (i,modeldata) in modeldatas.drain(..).enumerate() {
|
for (i,modeldata) in modeldatas.drain(..).enumerate() {
|
||||||
let model_uniforms = get_transform_uniform_data(&modeldata.transform);
|
let model_uniforms = get_transform_uniform_data(&modeldata.transforms);
|
||||||
let model_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
let model_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
label: Some(format!("ModelGraphics{}",i).as_str()),
|
label: Some(format!("ModelGraphics{}",i).as_str()),
|
||||||
contents: bytemuck::cast_slice(&model_uniforms),
|
contents: bytemuck::cast_slice(&model_uniforms),
|
||||||
@ -359,7 +367,7 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
});
|
});
|
||||||
//all of these are being moved here
|
//all of these are being moved here
|
||||||
models.push(ModelGraphics{
|
models.push(ModelGraphics{
|
||||||
transform: modeldata.transform,
|
transforms: modeldata.transforms,
|
||||||
vertex_buf:modeldata.vertex_buf,
|
vertex_buf:modeldata.vertex_buf,
|
||||||
entities: modeldata.entities,
|
entities: modeldata.entities,
|
||||||
bind_group: model_bind_group,
|
bind_group: model_bind_group,
|
||||||
@ -675,7 +683,7 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
.copy_from_slice(bytemuck::cast_slice(&camera_uniforms));
|
.copy_from_slice(bytemuck::cast_slice(&camera_uniforms));
|
||||||
//This code only needs to run when the uniforms change
|
//This code only needs to run when the uniforms change
|
||||||
for model in self.models.iter() {
|
for model in self.models.iter() {
|
||||||
let model_uniforms = get_transform_uniform_data(&model.transform);
|
let model_uniforms = get_transform_uniform_data(&model.transforms);
|
||||||
self.staging_belt
|
self.staging_belt
|
||||||
.write_buffer(
|
.write_buffer(
|
||||||
&mut encoder,
|
&mut encoder,
|
||||||
@ -723,7 +731,7 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
|
|
||||||
for entity in model.entities.iter() {
|
for entity in model.entities.iter() {
|
||||||
rpass.set_index_buffer(entity.index_buf.slice(..), wgpu::IndexFormat::Uint16);
|
rpass.set_index_buffer(entity.index_buf.slice(..), wgpu::IndexFormat::Uint16);
|
||||||
rpass.draw_indexed(0..entity.index_count, 0, 0..1);
|
rpass.draw_indexed(0..entity.index_count, 0, 0..model.transforms.len() as u32);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,20 +45,21 @@ struct EntityOutput {
|
|||||||
@location(2) normal: vec3<f32>,
|
@location(2) normal: vec3<f32>,
|
||||||
@location(3) view: vec3<f32>,
|
@location(3) view: vec3<f32>,
|
||||||
};
|
};
|
||||||
|
const MAX_ENTITY_INSTANCES=1024;
|
||||||
@group(1)
|
@group(1)
|
||||||
@binding(0)
|
@binding(0)
|
||||||
var<uniform> r_EntityTransform: mat4x4<f32>;
|
var<uniform> r_EntityTransform: array<mat4x4<f32>,MAX_ENTITY_INSTANCES>;
|
||||||
|
|
||||||
@vertex
|
@vertex
|
||||||
fn vs_entity(
|
fn vs_entity(
|
||||||
|
@builtin(instance_index) instance: u32,
|
||||||
@location(0) pos: vec3<f32>,
|
@location(0) pos: vec3<f32>,
|
||||||
@location(1) texture: vec2<f32>,
|
@location(1) texture: vec2<f32>,
|
||||||
@location(2) normal: vec3<f32>,
|
@location(2) normal: vec3<f32>,
|
||||||
) -> EntityOutput {
|
) -> EntityOutput {
|
||||||
var position: vec4<f32> = r_EntityTransform * vec4<f32>(pos, 1.0);
|
var position: vec4<f32> = r_EntityTransform[instance] * vec4<f32>(pos, 1.0);
|
||||||
var result: EntityOutput;
|
var result: EntityOutput;
|
||||||
result.normal = (r_EntityTransform * vec4<f32>(normal, 0.0)).xyz;
|
result.normal = (r_EntityTransform[instance] * vec4<f32>(normal, 0.0)).xyz;
|
||||||
result.texture=texture;
|
result.texture=texture;
|
||||||
result.view = position.xyz - r_data.cam_pos.xyz;
|
result.view = position.xyz - r_data.cam_pos.xyz;
|
||||||
result.position = r_data.proj * r_data.view * position;
|
result.position = r_data.proj * r_data.view * position;
|
||||||
|
Loading…
Reference in New Issue
Block a user