forked from StrafesNET/strafe-client
Compare commits
30 Commits
Author | SHA1 | Date | |
---|---|---|---|
05156d1741 | |||
d72e3fcabd | |||
99d4e51ee8 | |||
7effceff03 | |||
355956e532 | |||
8aad878d49 | |||
5fb9f8b1bc | |||
a9977c0bd2 | |||
fd34fbe367 | |||
75695946a0 | |||
2dba9554ea | |||
b5880d484a | |||
0b230b7597 | |||
988f204358 | |||
abd002e3bf | |||
1776dc4c39 | |||
946687639a | |||
69c7896011 | |||
1a1509f315 | |||
87f674238d | |||
cce9800045 | |||
8792b8e782 | |||
c9cb22be68 | |||
d4172f5040 | |||
df43dc6f6e | |||
c3290e2ce6 | |||
e347d4fd51 | |||
ad46418e79 | |||
82da9bf40f | |||
bda898a1f1 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -1405,7 +1405,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strafe-client"
|
name = "strafe-client"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-executor",
|
"async-executor",
|
||||||
"bytemuck",
|
"bytemuck",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "strafe-client"
|
name = "strafe-client"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
@ -16,3 +16,8 @@ obj = "0.10.2"
|
|||||||
pollster = "0.3.0"
|
pollster = "0.3.0"
|
||||||
wgpu = "0.17.0"
|
wgpu = "0.17.0"
|
||||||
winit = "0.28.6"
|
winit = "0.28.6"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
lto = true
|
||||||
|
strip = true
|
||||||
|
codegen-units = 1
|
||||||
|
2866
models/teapot.obj
Normal file
2866
models/teapot.obj
Normal file
File diff suppressed because it is too large
Load Diff
222
src/main.rs
222
src/main.rs
@ -8,14 +8,28 @@ const IMAGE_SIZE: u32 = 128;
|
|||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
struct Vertex {
|
struct Vertex {
|
||||||
pos: [f32; 3],
|
pos: [f32; 3],
|
||||||
|
texture: [f32; 2],
|
||||||
normal: [f32; 3],
|
normal: [f32; 3],
|
||||||
entity_id: u32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Entity {
|
struct Entity {
|
||||||
transform: glam::Affine3A,
|
index_count: u32,
|
||||||
vertex_count: u32,
|
index_buf: wgpu::Buffer,
|
||||||
|
}
|
||||||
|
|
||||||
|
//temp?
|
||||||
|
struct ModelData {
|
||||||
|
transform: glam::Mat4,
|
||||||
vertex_buf: wgpu::Buffer,
|
vertex_buf: wgpu::Buffer,
|
||||||
|
entities: Vec<Entity>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Model {
|
||||||
|
transform: glam::Mat4,
|
||||||
|
vertex_buf: wgpu::Buffer,
|
||||||
|
entities: Vec<Entity>,
|
||||||
|
bind_group: wgpu::BindGroup,
|
||||||
|
model_buf: wgpu::Buffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: we use the Y=up coordinate space in this example.
|
// Note: we use the Y=up coordinate space in this example.
|
||||||
@ -36,8 +50,6 @@ struct Camera {
|
|||||||
walkspeed: f32,
|
walkspeed: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
const MAGIC_ENTITIES: usize = 7;
|
|
||||||
|
|
||||||
const CONTROL_MOVEFORWARD:u32 = 0b00000001;
|
const CONTROL_MOVEFORWARD:u32 = 0b00000001;
|
||||||
const CONTROL_MOVEBACK:u32 = 0b00000010;
|
const CONTROL_MOVEBACK:u32 = 0b00000010;
|
||||||
const CONTROL_MOVERIGHT:u32 = 0b00000100;
|
const CONTROL_MOVERIGHT:u32 = 0b00000100;
|
||||||
@ -95,16 +107,16 @@ impl Camera {
|
|||||||
}else{
|
}else{
|
||||||
self.fov/5.0
|
self.fov/5.0
|
||||||
};
|
};
|
||||||
let proj = perspective_rh(fov, aspect, 1.0, 200.0);
|
let proj = perspective_rh(fov, aspect, 0.5, 1000.0);
|
||||||
let view = (glam::Mat4::from_translation(self.pos+self.offset) * glam::Mat4::from_euler(glam::EulerRot::YXZ, self.yaw, self.pitch, 0f32)).inverse();
|
|
||||||
let proj_inv = proj.inverse();
|
let proj_inv = proj.inverse();
|
||||||
|
let view = glam::Mat4::from_translation(self.pos+self.offset) * glam::Mat4::from_euler(glam::EulerRot::YXZ, self.yaw, self.pitch, 0f32);
|
||||||
|
let view_inv = view.inverse();
|
||||||
|
|
||||||
let mut raw = [0f32; 16 * 3 + 4];
|
let mut raw = [0f32; 16 * 3 + 4];
|
||||||
raw[..16].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj)[..]);
|
raw[..16].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj)[..]);
|
||||||
raw[16..32].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj_inv)[..]);
|
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[32..48].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view_inv)[..]);
|
||||||
raw[48..51].copy_from_slice(AsRef::<[f32; 3]>::as_ref(&self.pos));
|
raw[48..52].copy_from_slice(AsRef::<[f32; 4]>::as_ref(&view.col(3)));
|
||||||
raw[51] = 1.0;//cam_pos is vec4
|
|
||||||
raw
|
raw
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -114,13 +126,11 @@ pub struct Skybox {
|
|||||||
sky_pipeline: wgpu::RenderPipeline,
|
sky_pipeline: wgpu::RenderPipeline,
|
||||||
entity_pipeline: wgpu::RenderPipeline,
|
entity_pipeline: wgpu::RenderPipeline,
|
||||||
ground_pipeline: wgpu::RenderPipeline,
|
ground_pipeline: wgpu::RenderPipeline,
|
||||||
bind_group: wgpu::BindGroup,
|
main_bind_group: wgpu::BindGroup,
|
||||||
camera_buf: wgpu::Buffer,
|
camera_buf: wgpu::Buffer,
|
||||||
entity_buf: wgpu::Buffer,
|
models: Vec<Model>,
|
||||||
entities: Vec<Entity>,
|
|
||||||
depth_view: wgpu::TextureView,
|
depth_view: wgpu::TextureView,
|
||||||
camera_stagingbelt: wgpu::util::StagingBelt,
|
staging_belt: wgpu::util::StagingBelt,
|
||||||
entity_stagingbelt: wgpu::util::StagingBelt,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Skybox {
|
impl Skybox {
|
||||||
@ -149,46 +159,59 @@ impl Skybox {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_entity_uniform_data(entities:& Vec<Entity>) -> [f32; (9+3)*MAGIC_ENTITIES] {
|
fn get_transform_uniform_data(transform:&glam::Mat4) -> [f32; 4*4] {
|
||||||
let mut raw = [0f32; (9+3)*MAGIC_ENTITIES];
|
let mut raw = [0f32; 4*4];
|
||||||
for (i,entity) in entities.iter().enumerate() {
|
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(transform)[..]);
|
||||||
raw[i*12..(i+1)*12-3].copy_from_slice(&AsRef::<[f32; 9]>::as_ref(&glam::Mat3::from(entity.transform.matrix3))[..]);
|
|
||||||
raw[i*12+9..(i+1)*12].copy_from_slice(AsRef::<[f32; 3]>::as_ref(&glam::Vec3::from(entity.transform.translation)));
|
|
||||||
}
|
|
||||||
raw
|
raw
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_obj(device:&wgpu::Device,entities:& mut Vec<Entity>,source:&[u8]){
|
fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec<ModelData>,source:&[u8]){
|
||||||
let data = obj::ObjData::load_buf(&source[..]).unwrap();
|
let data = obj::ObjData::load_buf(&source[..]).unwrap();
|
||||||
let mut vertices = Vec::new();
|
let mut vertices = Vec::new();
|
||||||
|
let mut vertex_index = std::collections::HashMap::<obj::IndexTuple,u16>::new();
|
||||||
for object in data.objects {
|
for object in data.objects {
|
||||||
|
let mut entities = Vec::<Entity>::new();
|
||||||
for group in object.groups {
|
for group in object.groups {
|
||||||
vertices.clear();
|
let mut indices = Vec::new();
|
||||||
let entity_id = entities.len() as u32;
|
|
||||||
for poly in group.polys {
|
for poly in group.polys {
|
||||||
for end_index in 2..poly.0.len() {
|
for end_index in 2..poly.0.len() {
|
||||||
for &index in &[0, end_index - 1, end_index] {
|
for &index in &[0, end_index - 1, end_index] {
|
||||||
let obj::IndexTuple(position_id, _texture_id, normal_id) =
|
let vert = poly.0[index];
|
||||||
poly.0[index];
|
if let Some(&i)=vertex_index.get(&vert){
|
||||||
|
indices.push(i as u16);
|
||||||
|
}else{
|
||||||
|
let i=vertices.len() as u16;
|
||||||
vertices.push(Vertex {
|
vertices.push(Vertex {
|
||||||
pos: data.position[position_id],
|
pos: data.position[vert.0],
|
||||||
normal: data.normal[normal_id.unwrap()],
|
texture: data.texture[vert.1.unwrap()],
|
||||||
entity_id,
|
normal: data.normal[vert.2.unwrap()],
|
||||||
})
|
});
|
||||||
|
vertex_index.insert(vert,i);
|
||||||
|
indices.push(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
let index_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
|
label: Some("Index"),
|
||||||
|
contents: bytemuck::cast_slice(&indices),
|
||||||
|
usage: wgpu::BufferUsages::INDEX,
|
||||||
|
});
|
||||||
|
entities.push(Entity {
|
||||||
|
index_buf,
|
||||||
|
index_count: indices.len() as u32,
|
||||||
|
});
|
||||||
|
}
|
||||||
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
label: Some("Vertex"),
|
label: Some("Vertex"),
|
||||||
contents: bytemuck::cast_slice(&vertices),
|
contents: bytemuck::cast_slice(&vertices),
|
||||||
usage: wgpu::BufferUsages::VERTEX,
|
usage: wgpu::BufferUsages::VERTEX,
|
||||||
});
|
});
|
||||||
entities.push(Entity {
|
modeldatas.push(ModelData {
|
||||||
transform: glam::Affine3A::default(),
|
transform: glam::Mat4::default(),
|
||||||
vertex_count: vertices.len() as u32,
|
|
||||||
vertex_buf,
|
vertex_buf,
|
||||||
});
|
entities,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,7 +220,6 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
wgpu::Features::TEXTURE_COMPRESSION_ASTC
|
wgpu::Features::TEXTURE_COMPRESSION_ASTC
|
||||||
| wgpu::Features::TEXTURE_COMPRESSION_ETC2
|
| wgpu::Features::TEXTURE_COMPRESSION_ETC2
|
||||||
| wgpu::Features::TEXTURE_COMPRESSION_BC
|
| wgpu::Features::TEXTURE_COMPRESSION_BC
|
||||||
| wgpu::Features::BUFFER_BINDING_ARRAY
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init(
|
fn init(
|
||||||
@ -206,13 +228,15 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
queue: &wgpu::Queue,
|
queue: &wgpu::Queue,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let mut entities = Vec::<Entity>::new();
|
let mut modeldatas = Vec::<ModelData>::new();
|
||||||
add_obj(device,& mut entities,include_bytes!("../models/teslacyberv3.0.obj"));
|
add_obj(device,& mut modeldatas,include_bytes!("../models/teslacyberv3.0.obj"));
|
||||||
add_obj(device,& mut entities,include_bytes!("../models/suzanne.obj"));
|
add_obj(device,& mut modeldatas,include_bytes!("../models/suzanne.obj"));
|
||||||
println!("entities.len = {:?}", entities.len());
|
add_obj(device,& mut modeldatas,include_bytes!("../models/teapot.obj"));
|
||||||
entities[6].transform=glam::Affine3A::from_translation(glam::vec3(10.,5.,10.));
|
println!("models.len = {:?}", modeldatas.len());
|
||||||
|
modeldatas[1].transform=glam::Mat4::from_translation(glam::vec3(10.,5.,10.));
|
||||||
|
modeldatas[2].transform=glam::Mat4::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,
|
label: None,
|
||||||
entries: &[
|
entries: &[
|
||||||
wgpu::BindGroupLayoutEntry {
|
wgpu::BindGroupLayoutEntry {
|
||||||
@ -241,15 +265,20 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
|
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
|
||||||
count: None,
|
count: None,
|
||||||
},
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
let model_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
|
label: None,
|
||||||
|
entries: &[
|
||||||
wgpu::BindGroupLayoutEntry {
|
wgpu::BindGroupLayoutEntry {
|
||||||
binding: 3,
|
binding: 0,
|
||||||
visibility: wgpu::ShaderStages::VERTEX,
|
visibility: wgpu::ShaderStages::VERTEX,
|
||||||
ty: wgpu::BindingType::Buffer {
|
ty: wgpu::BindingType::Buffer {
|
||||||
ty: wgpu::BufferBindingType::Uniform,
|
ty: wgpu::BufferBindingType::Uniform,
|
||||||
has_dynamic_offset: false,
|
has_dynamic_offset: false,
|
||||||
min_binding_size: None,
|
min_binding_size: None,
|
||||||
},
|
},
|
||||||
count: Some(std::num::NonZeroU32::new(MAGIC_ENTITIES as u32).unwrap()),
|
count: None,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
@ -264,7 +293,7 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
time: Instant::now(),
|
time: Instant::now(),
|
||||||
pos: glam::Vec3::new(5.0,0.0,5.0),
|
pos: glam::Vec3::new(5.0,0.0,5.0),
|
||||||
vel: glam::Vec3::new(0.0,0.0,0.0),
|
vel: glam::Vec3::new(0.0,0.0,0.0),
|
||||||
gravity: glam::Vec3 { x: 0.0, y: -100.0, z: 0.0 },
|
gravity: glam::Vec3::new(0.0,-100.0,0.0),
|
||||||
friction: 90.0,
|
friction: 90.0,
|
||||||
screen_size: (config.width, config.height),
|
screen_size: (config.width, config.height),
|
||||||
offset: glam::Vec3::new(0.0,4.5,0.0),
|
offset: glam::Vec3::new(0.0,4.5,0.0),
|
||||||
@ -283,16 +312,9 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||||
});
|
});
|
||||||
|
|
||||||
let entity_uniforms = get_entity_uniform_data(&entities);
|
|
||||||
let entity_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
||||||
label: Some("EntityData"),
|
|
||||||
contents: bytemuck::cast_slice(&entity_uniforms),
|
|
||||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
|
||||||
});
|
|
||||||
|
|
||||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||||
label: None,
|
label: None,
|
||||||
bind_group_layouts: &[&bind_group_layout],
|
bind_group_layouts: &[&main_bind_group_layout, &model_bind_group_layout],
|
||||||
push_constant_ranges: &[],
|
push_constant_ranges: &[],
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -333,7 +355,7 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
buffers: &[wgpu::VertexBufferLayout {
|
buffers: &[wgpu::VertexBufferLayout {
|
||||||
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
|
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
|
||||||
step_mode: wgpu::VertexStepMode::Vertex,
|
step_mode: wgpu::VertexStepMode::Vertex,
|
||||||
attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3, 2 => Uint32],
|
attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x2, 2 => Float32x3],
|
||||||
}],
|
}],
|
||||||
},
|
},
|
||||||
fragment: Some(wgpu::FragmentState {
|
fragment: Some(wgpu::FragmentState {
|
||||||
@ -466,8 +488,8 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
dimension: Some(wgpu::TextureViewDimension::Cube),
|
dimension: Some(wgpu::TextureViewDimension::Cube),
|
||||||
..wgpu::TextureViewDescriptor::default()
|
..wgpu::TextureViewDescriptor::default()
|
||||||
});
|
});
|
||||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
let main_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
layout: &bind_group_layout,
|
layout: &main_bind_group_layout,
|
||||||
entries: &[
|
entries: &[
|
||||||
wgpu::BindGroupEntry {
|
wgpu::BindGroupEntry {
|
||||||
binding: 0,
|
binding: 0,
|
||||||
@ -481,13 +503,38 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
binding: 2,
|
binding: 2,
|
||||||
resource: wgpu::BindingResource::Sampler(&sampler),
|
resource: wgpu::BindingResource::Sampler(&sampler),
|
||||||
},
|
},
|
||||||
|
],
|
||||||
|
label: Some("Camera"),
|
||||||
|
});
|
||||||
|
|
||||||
|
//drain the modeldata vec so entities can be /moved/ to models.entities
|
||||||
|
let mut models = Vec::<Model>::with_capacity(modeldatas.len());
|
||||||
|
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 {
|
wgpu::BindGroupEntry {
|
||||||
binding: 3,
|
binding: 0,
|
||||||
resource: entity_buf.as_entire_binding(),
|
resource: model_buf.as_entire_binding(),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
label: None,
|
label: Some(format!("Model{}",i).as_str()),
|
||||||
});
|
});
|
||||||
|
//all of these are being moved here
|
||||||
|
models.push(Model{
|
||||||
|
transform: modeldata.transform,
|
||||||
|
vertex_buf:modeldata.vertex_buf,
|
||||||
|
entities: modeldata.entities,
|
||||||
|
bind_group: model_bind_group,
|
||||||
|
model_buf,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
let depth_view = Self::create_depth_texture(config, device);
|
let depth_view = Self::create_depth_texture(config, device);
|
||||||
|
|
||||||
@ -496,13 +543,11 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
sky_pipeline,
|
sky_pipeline,
|
||||||
entity_pipeline,
|
entity_pipeline,
|
||||||
ground_pipeline,
|
ground_pipeline,
|
||||||
bind_group,
|
main_bind_group,
|
||||||
camera_buf,
|
camera_buf,
|
||||||
entity_buf,
|
models,
|
||||||
entities,
|
|
||||||
depth_view,
|
depth_view,
|
||||||
camera_stagingbelt: wgpu::util::StagingBelt::new(0x100),
|
staging_belt: wgpu::util::StagingBelt::new(0x100),
|
||||||
entity_stagingbelt: wgpu::util::StagingBelt::new(0x100),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -559,8 +604,8 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn move_mouse(&mut self, delta: (f64,f64)) {
|
fn move_mouse(&mut self, delta: (f64,f64)) {
|
||||||
self.camera.pitch+=(delta.1/-512.) as f32;
|
self.camera.pitch=(self.camera.pitch as f64+delta.1/-2048.) as f32;
|
||||||
self.camera.yaw+=(delta.0/-512.) as f32;
|
self.camera.yaw=(self.camera.yaw as f64+delta.0/-2048.) as f32;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resize(
|
fn resize(
|
||||||
@ -618,7 +663,7 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
|
|
||||||
// update rotation
|
// update rotation
|
||||||
let camera_uniforms = self.camera.to_uniform_data();
|
let camera_uniforms = self.camera.to_uniform_data();
|
||||||
self.camera_stagingbelt
|
self.staging_belt
|
||||||
.write_buffer(
|
.write_buffer(
|
||||||
&mut encoder,
|
&mut encoder,
|
||||||
&self.camera_buf,
|
&self.camera_buf,
|
||||||
@ -627,21 +672,20 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
device,
|
device,
|
||||||
)
|
)
|
||||||
.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
|
||||||
self.camera_stagingbelt.finish();
|
for model in self.models.iter() {
|
||||||
|
let model_uniforms = get_transform_uniform_data(&model.transform);
|
||||||
let entity_uniforms = get_entity_uniform_data(&self.entities);
|
self.staging_belt
|
||||||
self.entity_stagingbelt
|
|
||||||
.write_buffer(
|
.write_buffer(
|
||||||
&mut encoder,
|
&mut encoder,
|
||||||
&self.entity_buf,//description of where data will be written when command is executed
|
&model.model_buf,//description of where data will be written when command is executed
|
||||||
0,//offset in staging belt?
|
0,//offset in staging belt?
|
||||||
wgpu::BufferSize::new((entity_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
|
wgpu::BufferSize::new((model_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
|
||||||
device,
|
device,
|
||||||
)
|
)
|
||||||
.copy_from_slice(bytemuck::cast_slice(&entity_uniforms));
|
.copy_from_slice(bytemuck::cast_slice(&model_uniforms));
|
||||||
|
}
|
||||||
self.entity_stagingbelt.finish();
|
self.staging_belt.finish();
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||||
@ -669,15 +713,22 @@ 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);
|
rpass.set_pipeline(&self.entity_pipeline);
|
||||||
for entity in self.entities.iter() {
|
for model in self.models.iter() {
|
||||||
rpass.set_vertex_buffer(0, entity.vertex_buf.slice(..));
|
rpass.set_bind_group(1, &model.bind_group, &[]);
|
||||||
rpass.draw(0..entity.vertex_count, 0..1);
|
rpass.set_vertex_buffer(0, model.vertex_buf.slice(..));
|
||||||
|
|
||||||
|
for entity in model.entities.iter() {
|
||||||
|
rpass.set_index_buffer(entity.index_buf.slice(..), wgpu::IndexFormat::Uint16);
|
||||||
|
rpass.draw_indexed(0..entity.index_count, 0, 0..1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rpass.set_pipeline(&self.ground_pipeline);
|
rpass.set_pipeline(&self.ground_pipeline);
|
||||||
|
//rpass.set_index_buffer(&[0u16,1,2,1,2,3][..] as wgpu::BufferSlice, wgpu::IndexFormat::Uint16);
|
||||||
|
//rpass.draw_indexed(0..4, 0, 0..1);
|
||||||
rpass.draw(0..6, 0..1);
|
rpass.draw(0..6, 0..1);
|
||||||
|
|
||||||
rpass.set_pipeline(&self.sky_pipeline);
|
rpass.set_pipeline(&self.sky_pipeline);
|
||||||
@ -686,11 +737,14 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
|
|
||||||
queue.submit(std::iter::once(encoder.finish()));
|
queue.submit(std::iter::once(encoder.finish()));
|
||||||
|
|
||||||
self.camera_stagingbelt.recall();
|
self.staging_belt.recall();
|
||||||
self.entity_stagingbelt.recall();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
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()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ fn vs_ground(@builtin(vertex_index) vertex_index: u32) -> GroundOutput {
|
|||||||
f32(tmp1) * 2.0 - 1.0,
|
f32(tmp1) * 2.0 - 1.0,
|
||||||
0.0,
|
0.0,
|
||||||
f32(tmp2) * 2.0 - 1.0
|
f32(tmp2) * 2.0 - 1.0
|
||||||
) * 100.0;
|
) * 160.0;
|
||||||
|
|
||||||
var result: GroundOutput;
|
var result: GroundOutput;
|
||||||
result.pos = pos;
|
result.pos = pos;
|
||||||
@ -63,30 +63,27 @@ fn vs_ground(@builtin(vertex_index) vertex_index: u32) -> GroundOutput {
|
|||||||
|
|
||||||
struct EntityOutput {
|
struct EntityOutput {
|
||||||
@builtin(position) position: vec4<f32>,
|
@builtin(position) position: vec4<f32>,
|
||||||
@location(1) normal: vec3<f32>,
|
@location(1) texture: vec2<f32>,
|
||||||
|
@location(2) normal: vec3<f32>,
|
||||||
@location(3) view: vec3<f32>,
|
@location(3) view: vec3<f32>,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@group(1)
|
||||||
struct EntityTransform {
|
@binding(0)
|
||||||
matrix3: mat3x3<f32>,
|
var<uniform> r_EntityTransform: mat4x4<f32>;
|
||||||
translation: vec3<f32>,
|
|
||||||
};
|
|
||||||
@group(0)
|
|
||||||
@binding(3)
|
|
||||||
var<uniform> r_EntityTransforms: array<EntityTransform, 7>;
|
|
||||||
|
|
||||||
@vertex
|
@vertex
|
||||||
fn vs_entity(
|
fn vs_entity(
|
||||||
@location(0) pos: vec3<f32>,
|
@location(0) pos: vec3<f32>,
|
||||||
@location(1) normal: vec3<f32>,
|
@location(1) texture: vec2<f32>,
|
||||||
@location(2) entity_id: u32,
|
@location(2) normal: vec3<f32>,
|
||||||
) -> EntityOutput {
|
) -> EntityOutput {
|
||||||
var position: vec3<f32> = r_EntityTransforms[entity_id].matrix3 * pos+r_EntityTransforms[entity_id].translation;
|
var position: vec4<f32> = r_EntityTransform * vec4<f32>(pos, 1.0);
|
||||||
var result: EntityOutput;
|
var result: EntityOutput;
|
||||||
result.normal = r_EntityTransforms[entity_id].matrix3 * normal;
|
result.normal = (r_EntityTransform * vec4<f32>(normal, 0.0)).xyz;
|
||||||
result.view = position - r_data.cam_pos.xyz;
|
result.texture=texture;
|
||||||
result.position = r_data.proj * r_data.view * vec4<f32>(position, 1.0);
|
result.view = position.xyz - r_data.cam_pos.xyz;
|
||||||
|
result.position = r_data.proj * r_data.view * position;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,10 +103,13 @@ fn fs_sky(vertex: SkyOutput) -> @location(0) vec4<f32> {
|
|||||||
fn fs_entity(vertex: EntityOutput) -> @location(0) vec4<f32> {
|
fn fs_entity(vertex: EntityOutput) -> @location(0) vec4<f32> {
|
||||||
let incident = normalize(vertex.view);
|
let incident = normalize(vertex.view);
|
||||||
let normal = normalize(vertex.normal);
|
let normal = normalize(vertex.normal);
|
||||||
let reflected = incident - 2.0 * dot(normal, incident) * normal;
|
let d = dot(normal, incident);
|
||||||
|
let reflected = incident - 2.0 * d * normal;
|
||||||
|
|
||||||
|
let dir = vec3<f32>(-1.0)+2.0*vec3<f32>(vertex.texture.x,0.0,vertex.texture.y);
|
||||||
|
let texture_color = textureSample(r_texture, r_sampler, dir).rgb;
|
||||||
let reflected_color = textureSample(r_texture, r_sampler, reflected).rgb;
|
let reflected_color = textureSample(r_texture, r_sampler, reflected).rgb;
|
||||||
return vec4<f32>(vec3<f32>(0.1) + 0.5 * reflected_color, 1.0);
|
return vec4<f32>(mix(vec3<f32>(0.1) + 0.5 * reflected_color,texture_color,1.0-pow(1.0-abs(d),2.0)), 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn modulo_euclidean (a: f32, b: f32) -> f32 {
|
fn modulo_euclidean (a: f32, b: f32) -> f32 {
|
||||||
|
Loading…
Reference in New Issue
Block a user