Compare commits
9 Commits
e347d4fd51
...
69c7896011
Author | SHA1 | Date | |
---|---|---|---|
69c7896011 | |||
1a1509f315 | |||
87f674238d | |||
cce9800045 | |||
8792b8e782 | |||
c9cb22be68 | |||
d4172f5040 | |||
df43dc6f6e | |||
c3290e2ce6 |
2580
models/suzanne.obj
Normal file
2580
models/suzanne.obj
Normal file
File diff suppressed because it is too large
Load Diff
207
src/main.rs
207
src/main.rs
@ -16,6 +16,19 @@ struct Entity {
|
||||
vertex_buf: wgpu::Buffer,
|
||||
}
|
||||
|
||||
//temp?
|
||||
struct ModelData {
|
||||
transform: glam::Affine3A,
|
||||
entities: Vec<Entity>,
|
||||
}
|
||||
|
||||
struct Model {
|
||||
transform: glam::Affine3A,
|
||||
entities: Vec<Entity>,
|
||||
bind_group: wgpu::BindGroup,
|
||||
model_buf: wgpu::Buffer,
|
||||
}
|
||||
|
||||
// Note: we use the Y=up coordinate space in this example.
|
||||
struct Camera {
|
||||
time: Instant,
|
||||
@ -100,7 +113,7 @@ impl Camera {
|
||||
raw[16..32].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj_inv)[..]);
|
||||
raw[32..48].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view)[..]);
|
||||
raw[48..51].copy_from_slice(AsRef::<[f32; 3]>::as_ref(&self.pos));
|
||||
raw[51] = 1.0;
|
||||
raw[51] = 1.0;//cam_pos is vec4
|
||||
raw
|
||||
}
|
||||
}
|
||||
@ -110,9 +123,9 @@ pub struct Skybox {
|
||||
sky_pipeline: wgpu::RenderPipeline,
|
||||
entity_pipeline: wgpu::RenderPipeline,
|
||||
ground_pipeline: wgpu::RenderPipeline,
|
||||
bind_group: wgpu::BindGroup,
|
||||
uniform_buf: wgpu::Buffer,
|
||||
entities: Vec<Entity>,
|
||||
main_bind_group: wgpu::BindGroup,
|
||||
camera_buf: wgpu::Buffer,
|
||||
models: Vec<Model>,
|
||||
depth_view: wgpu::TextureView,
|
||||
staging_belt: wgpu::util::StagingBelt,
|
||||
}
|
||||
@ -143,6 +156,48 @@ impl Skybox {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_transform_uniform_data(transform:&glam::Affine3A) -> [f32; 4*4] {
|
||||
let mut raw = [0f32; 4*4];
|
||||
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&glam::Mat4::from(*transform))[..]);
|
||||
raw
|
||||
}
|
||||
|
||||
fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec<ModelData>,source:&[u8]){
|
||||
let data = obj::ObjData::load_buf(&source[..]).unwrap();
|
||||
let mut vertices = Vec::new();
|
||||
for object in data.objects {
|
||||
let mut entities = Vec::<Entity>::new();
|
||||
for group in object.groups {
|
||||
vertices.clear();
|
||||
for poly in group.polys {
|
||||
for end_index in 2..poly.0.len() {
|
||||
for &index in &[0, end_index - 1, end_index] {
|
||||
let obj::IndexTuple(position_id, _texture_id, normal_id) =
|
||||
poly.0[index];
|
||||
vertices.push(Vertex {
|
||||
pos: data.position[position_id],
|
||||
normal: data.normal[normal_id.unwrap()],
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Vertex"),
|
||||
contents: bytemuck::cast_slice(&vertices),
|
||||
usage: wgpu::BufferUsages::VERTEX,
|
||||
});
|
||||
entities.push(Entity {
|
||||
vertex_count: vertices.len() as u32,
|
||||
vertex_buf,
|
||||
});
|
||||
}
|
||||
modeldatas.push(ModelData {
|
||||
transform: glam::Affine3A::default(),
|
||||
entities,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl strafe_client::framework::Example for Skybox {
|
||||
fn optional_features() -> wgpu::Features {
|
||||
wgpu::Features::TEXTURE_COMPRESSION_ASTC
|
||||
@ -156,45 +211,18 @@ impl strafe_client::framework::Example for Skybox {
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
) -> Self {
|
||||
let mut entities = Vec::new();
|
||||
{
|
||||
let source = include_bytes!("../models/teslacyberv3.0.obj");
|
||||
let data = obj::ObjData::load_buf(&source[..]).unwrap();
|
||||
let mut vertices = Vec::new();
|
||||
for object in data.objects {
|
||||
for group in object.groups {
|
||||
vertices.clear();
|
||||
for poly in group.polys {
|
||||
for end_index in 2..poly.0.len() {
|
||||
for &index in &[0, end_index - 1, end_index] {
|
||||
let obj::IndexTuple(position_id, _texture_id, normal_id) =
|
||||
poly.0[index];
|
||||
vertices.push(Vertex {
|
||||
pos: data.position[position_id],
|
||||
normal: data.normal[normal_id.unwrap()],
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Vertex"),
|
||||
contents: bytemuck::cast_slice(&vertices),
|
||||
usage: wgpu::BufferUsages::VERTEX,
|
||||
});
|
||||
entities.push(Entity {
|
||||
vertex_count: vertices.len() as u32,
|
||||
vertex_buf,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut modeldatas = Vec::<ModelData>::new();
|
||||
add_obj(device,& mut modeldatas,include_bytes!("../models/teslacyberv3.0.obj"));
|
||||
add_obj(device,& mut modeldatas,include_bytes!("../models/suzanne.obj"));
|
||||
println!("models.len = {:?}", modeldatas.len());
|
||||
modeldatas[1].transform=glam::Affine3A::from_translation(glam::vec3(10.,5.,10.));
|
||||
|
||||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
let main_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: None,
|
||||
entries: &[
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
|
||||
visibility: wgpu::ShaderStages::VERTEX,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Uniform,
|
||||
has_dynamic_offset: false,
|
||||
@ -220,6 +248,21 @@ impl strafe_client::framework::Example for Skybox {
|
||||
},
|
||||
],
|
||||
});
|
||||
let model_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: None,
|
||||
entries: &[
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStages::VERTEX,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Uniform,
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
// Create the render pipeline
|
||||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
@ -243,16 +286,16 @@ impl strafe_client::framework::Example for Skybox {
|
||||
grounded: true,
|
||||
walkspeed: 18.0,
|
||||
};
|
||||
let raw_uniforms = camera.to_uniform_data();
|
||||
let uniform_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Buffer"),
|
||||
contents: bytemuck::cast_slice(&raw_uniforms),
|
||||
let camera_uniforms = camera.to_uniform_data();
|
||||
let camera_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Camera"),
|
||||
contents: bytemuck::cast_slice(&camera_uniforms),
|
||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||
});
|
||||
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: None,
|
||||
bind_group_layouts: &[&bind_group_layout],
|
||||
bind_group_layouts: &[&main_bind_group_layout, &model_bind_group_layout],
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
|
||||
@ -426,12 +469,12 @@ impl strafe_client::framework::Example for Skybox {
|
||||
dimension: Some(wgpu::TextureViewDimension::Cube),
|
||||
..wgpu::TextureViewDescriptor::default()
|
||||
});
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &bind_group_layout,
|
||||
let main_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &main_bind_group_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: uniform_buf.as_entire_binding(),
|
||||
resource: camera_buf.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
@ -442,8 +485,33 @@ impl strafe_client::framework::Example for Skybox {
|
||||
resource: wgpu::BindingResource::Sampler(&sampler),
|
||||
},
|
||||
],
|
||||
label: None,
|
||||
label: Some("Camera"),
|
||||
});
|
||||
let mut models = Vec::<Model>::new();
|
||||
for (i,modeldata) in modeldatas.drain(..).enumerate() {
|
||||
let model_uniforms = get_transform_uniform_data(&modeldata.transform);
|
||||
let model_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some(format!("Model{}",i).as_str()),
|
||||
contents: bytemuck::cast_slice(&model_uniforms),
|
||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||
});
|
||||
let model_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &model_bind_group_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: model_buf.as_entire_binding(),
|
||||
},
|
||||
],
|
||||
label: Some(format!("Model{}",i).as_str()),
|
||||
});
|
||||
models.push(Model{
|
||||
transform: modeldata.transform,
|
||||
entities: modeldata.entities,
|
||||
bind_group: model_bind_group,
|
||||
model_buf: model_buf,
|
||||
})
|
||||
}
|
||||
|
||||
let depth_view = Self::create_depth_texture(config, device);
|
||||
|
||||
@ -452,9 +520,9 @@ impl strafe_client::framework::Example for Skybox {
|
||||
sky_pipeline,
|
||||
entity_pipeline,
|
||||
ground_pipeline,
|
||||
bind_group,
|
||||
uniform_buf,
|
||||
entities,
|
||||
main_bind_group,
|
||||
camera_buf,
|
||||
models,
|
||||
depth_view,
|
||||
staging_belt: wgpu::util::StagingBelt::new(0x100),
|
||||
}
|
||||
@ -571,17 +639,28 @@ impl strafe_client::framework::Example for Skybox {
|
||||
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
|
||||
|
||||
// update rotation
|
||||
let raw_uniforms = self.camera.to_uniform_data();
|
||||
let camera_uniforms = self.camera.to_uniform_data();
|
||||
self.staging_belt
|
||||
.write_buffer(
|
||||
&mut encoder,
|
||||
&self.uniform_buf,
|
||||
&self.camera_buf,
|
||||
0,
|
||||
wgpu::BufferSize::new((raw_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
|
||||
wgpu::BufferSize::new((camera_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
|
||||
device,
|
||||
)
|
||||
.copy_from_slice(bytemuck::cast_slice(&raw_uniforms));
|
||||
|
||||
.copy_from_slice(bytemuck::cast_slice(&camera_uniforms));
|
||||
for model in self.models.iter() {
|
||||
let model_uniforms = get_transform_uniform_data(&model.transform);
|
||||
self.staging_belt
|
||||
.write_buffer(
|
||||
&mut encoder,
|
||||
&model.model_buf,//description of where data will be written when command is executed
|
||||
0,//offset in staging belt?
|
||||
wgpu::BufferSize::new((model_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
|
||||
device,
|
||||
)
|
||||
.copy_from_slice(bytemuck::cast_slice(&model_uniforms));
|
||||
}
|
||||
self.staging_belt.finish();
|
||||
|
||||
{
|
||||
@ -610,12 +689,16 @@ impl strafe_client::framework::Example for Skybox {
|
||||
}),
|
||||
});
|
||||
|
||||
rpass.set_bind_group(0, &self.bind_group, &[]);
|
||||
rpass.set_bind_group(0, &self.main_bind_group, &[]);
|
||||
|
||||
rpass.set_pipeline(&self.entity_pipeline);
|
||||
for entity in self.entities.iter() {
|
||||
rpass.set_vertex_buffer(0, entity.vertex_buf.slice(..));
|
||||
rpass.draw(0..entity.vertex_count, 0..1);
|
||||
for model in self.models.iter() {
|
||||
rpass.set_bind_group(1, &model.bind_group, &[]);
|
||||
|
||||
for entity in model.entities.iter() {
|
||||
rpass.set_vertex_buffer(0, entity.vertex_buf.slice(..));
|
||||
rpass.draw(0..entity.vertex_count, 0..1);
|
||||
}
|
||||
}
|
||||
|
||||
rpass.set_pipeline(&self.ground_pipeline);
|
||||
@ -634,5 +717,9 @@ impl strafe_client::framework::Example for Skybox {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
strafe_client::framework::run::<Skybox>("Strafe Client v0.1");
|
||||
strafe_client::framework::run::<Skybox>(
|
||||
format!("Strafe Client v{}",
|
||||
env!("CARGO_PKG_VERSION")
|
||||
).as_str()
|
||||
);
|
||||
}
|
||||
|
@ -67,15 +67,20 @@ struct EntityOutput {
|
||||
@location(3) view: vec3<f32>,
|
||||
};
|
||||
|
||||
@group(1)
|
||||
@binding(0)
|
||||
var<uniform> r_EntityTransform: mat4x4<f32>;
|
||||
|
||||
@vertex
|
||||
fn vs_entity(
|
||||
@location(0) pos: vec3<f32>,
|
||||
@location(1) normal: vec3<f32>,
|
||||
) -> EntityOutput {
|
||||
var position: vec4<f32> = r_EntityTransform * vec4<f32>(pos, 1.0);
|
||||
var result: EntityOutput;
|
||||
result.normal = normal;
|
||||
result.view = pos - r_data.cam_pos.xyz;
|
||||
result.position = r_data.proj * r_data.view * vec4<f32>(pos, 1.0);
|
||||
result.normal = (r_EntityTransform * vec4<f32>(normal, 0.0)).xyz;
|
||||
result.view = position.xyz - r_data.cam_pos.xyz;
|
||||
result.position = r_data.proj * r_data.view * position;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user