forked from StrafesNET/strafe-project
Compare commits
42 Commits
web
...
depth-fudg
| Author | SHA1 | Date | |
|---|---|---|---|
| 67c4aa07ab | |||
| 34bfeab267 | |||
| 52cbda4710 | |||
| 9d8faeb6fb | |||
| 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 | |||
| ce5f6fceb9 | |||
| 7179f20e67 | |||
| e25a2d7a7a | |||
| 7c247b6949 | |||
| 49fde16c4f | |||
| 403910d2d8 | |||
| 7a23ef2734 | |||
| 1e25f71618 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -1405,7 +1405,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||
|
||||
[[package]]
|
||||
name = "strafe-client"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"async-executor",
|
||||
"bytemuck",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "strafe-client"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
edition = "2021"
|
||||
|
||||
# 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"
|
||||
wgpu = "0.17.0"
|
||||
winit = "0.28.6"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
strip = true
|
||||
codegen-units = 1
|
||||
|
||||
2580
models/suzanne.obj
Normal file
2580
models/suzanne.obj
Normal file
File diff suppressed because it is too large
Load Diff
2866
models/teapot.obj
Normal file
2866
models/teapot.obj
Normal file
File diff suppressed because it is too large
Load Diff
417
src/main.rs
417
src/main.rs
@@ -1,5 +1,5 @@
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use std::{borrow::Cow, f32::consts,time::Instant};
|
||||
use std::{borrow::Cow, time::Instant};
|
||||
use wgpu::{util::DeviceExt, AstcBlock, AstcChannel};
|
||||
|
||||
const IMAGE_SIZE: u32 = 128;
|
||||
@@ -8,12 +8,32 @@ const IMAGE_SIZE: u32 = 128;
|
||||
#[repr(C)]
|
||||
struct Vertex {
|
||||
pos: [f32; 3],
|
||||
texture: [f32; 2],
|
||||
normal: [f32; 3],
|
||||
}
|
||||
|
||||
struct Entity {
|
||||
vertex_count: u32,
|
||||
index_count: u32,
|
||||
index_buf: wgpu::Buffer,
|
||||
}
|
||||
|
||||
//temp?
|
||||
struct ModelData {
|
||||
transform: glam::Mat4,
|
||||
transform_depth: glam::Mat4,
|
||||
use_depth: glam::Vec4,
|
||||
vertex_buf: wgpu::Buffer,
|
||||
entities: Vec<Entity>,
|
||||
}
|
||||
|
||||
struct Model {
|
||||
transform: glam::Mat4,
|
||||
transform_depth: glam::Mat4,
|
||||
use_depth: glam::Vec4,
|
||||
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.
|
||||
@@ -25,11 +45,13 @@ struct Camera {
|
||||
friction: f32,
|
||||
screen_size: (u32, u32),
|
||||
offset: glam::Vec3,
|
||||
fov: f32,
|
||||
yaw: f32,
|
||||
pitch: f32,
|
||||
controls: u32,
|
||||
mv: f32,
|
||||
grounded: bool,
|
||||
walkspeed: f32,
|
||||
}
|
||||
|
||||
const CONTROL_MOVEFORWARD:u32 = 0b00000001;
|
||||
@@ -39,7 +61,7 @@ const CONTROL_MOVELEFT:u32 = 0b00001000;
|
||||
const CONTROL_MOVEUP:u32 = 0b00010000;
|
||||
const CONTROL_MOVEDOWN:u32 = 0b00100000;
|
||||
const CONTROL_JUMP:u32 = 0b01000000;
|
||||
//const CONTROL_ZOOM:u32 = 0b10000000;
|
||||
const CONTROL_ZOOM:u32 = 0b10000000;
|
||||
|
||||
const FORWARD_DIR:glam::Vec3 = glam::Vec3::new(0.0,0.0,-1.0);
|
||||
const RIGHT_DIR:glam::Vec3 = glam::Vec3::new(1.0,0.0,0.0);
|
||||
@@ -69,19 +91,36 @@ fn get_control_dir(controls: u32) -> glam::Vec3{
|
||||
return control_dir
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn perspective_rh(fov_y_slope: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> glam::Mat4 {
|
||||
//glam_assert!(z_near > 0.0 && z_far > 0.0);
|
||||
let r = z_far / (z_near - z_far);
|
||||
glam::Mat4::from_cols(
|
||||
glam::Vec4::new(1.0/(fov_y_slope * aspect_ratio), 0.0, 0.0, 0.0),
|
||||
glam::Vec4::new(0.0, 1.0/fov_y_slope, 0.0, 0.0),
|
||||
glam::Vec4::new(0.0, 0.0, r, -1.0),
|
||||
glam::Vec4::new(0.0, 0.0, r * z_near, 0.0),
|
||||
)
|
||||
}
|
||||
|
||||
impl Camera {
|
||||
fn to_uniform_data(&self) -> [f32; 16 * 3 + 4] {
|
||||
let aspect = self.screen_size.0 as f32 / self.screen_size.1 as f32;
|
||||
let proj = glam::Mat4::perspective_rh(consts::FRAC_PI_2, aspect, 1.0, 200.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 fov = if self.controls&CONTROL_ZOOM==0 {
|
||||
self.fov
|
||||
}else{
|
||||
self.fov/5.0
|
||||
};
|
||||
let proj = perspective_rh(fov, aspect, 0.5, 1000.0);
|
||||
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];
|
||||
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[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[32..48].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view_inv)[..]);
|
||||
raw[48..52].copy_from_slice(AsRef::<[f32; 4]>::as_ref(&view.col(3)));
|
||||
raw
|
||||
}
|
||||
}
|
||||
@@ -91,11 +130,15 @@ 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>,
|
||||
special_teapot: Model,
|
||||
checkered_pipeline: wgpu::RenderPipeline,
|
||||
depth_overwrite_pipeline: wgpu::RenderPipeline,
|
||||
main_bind_group: wgpu::BindGroup,
|
||||
camera_buf: wgpu::Buffer,
|
||||
models: Vec<Model>,
|
||||
depth_view: wgpu::TextureView,
|
||||
staging_belt: wgpu::util::StagingBelt,
|
||||
start_time: Instant,
|
||||
}
|
||||
|
||||
impl Skybox {
|
||||
@@ -124,6 +167,73 @@ impl Skybox {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_model_uniform_data(model:&Model) -> [f32; 4*4*2+4] {
|
||||
let mut raw = [0f32; 4*4*2+4];
|
||||
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&model.transform)[..]);
|
||||
raw[16..32].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&model.transform_depth)[..]);
|
||||
raw[32..36].copy_from_slice(AsRef::<[f32; 4]>::as_ref(&model.use_depth));
|
||||
raw
|
||||
}
|
||||
fn get_modeldata_uniform_data(model:&ModelData) -> [f32; 4*4*2+4] {
|
||||
let mut raw = [0f32; 4*4*2+4];
|
||||
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&model.transform)[..]);
|
||||
raw[16..32].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&model.transform_depth)[..]);
|
||||
raw[32..36].copy_from_slice(AsRef::<[f32; 4]>::as_ref(&model.use_depth));
|
||||
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();
|
||||
let mut vertex_index = std::collections::HashMap::<obj::IndexTuple,u16>::new();
|
||||
for object in data.objects {
|
||||
let mut entities = Vec::<Entity>::new();
|
||||
for group in object.groups {
|
||||
let mut indices = Vec::new();
|
||||
for poly in group.polys {
|
||||
for end_index in 2..poly.0.len() {
|
||||
for &index in &[0, end_index - 1, end_index] {
|
||||
let vert = 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 {
|
||||
pos: data.position[vert.0],
|
||||
texture: data.texture[vert.1.unwrap()],
|
||||
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 {
|
||||
label: Some("Vertex"),
|
||||
contents: bytemuck::cast_slice(&vertices),
|
||||
usage: wgpu::BufferUsages::VERTEX,
|
||||
});
|
||||
modeldatas.push(ModelData {
|
||||
transform: glam::Mat4::default(),
|
||||
transform_depth: glam::Mat4::default(),
|
||||
use_depth: glam::Vec4::ZERO,
|
||||
vertex_buf,
|
||||
entities,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl strafe_client::framework::Example for Skybox {
|
||||
fn optional_features() -> wgpu::Features {
|
||||
wgpu::Features::TEXTURE_COMPRESSION_ASTC
|
||||
@@ -137,45 +247,26 @@ 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"));
|
||||
add_obj(device,& mut modeldatas,include_bytes!("../models/teapot.obj"));
|
||||
println!("models.len = {:?}", modeldatas.len());
|
||||
modeldatas[1].transform=glam::Mat4::from_translation(glam::vec3(10.,5.,10.));
|
||||
|
||||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
let proj1 = glam::Mat4::orthographic_rh(-20.0, 20.0, -20.0, 20.0, -20.0, 20.0);
|
||||
let model0 = glam::Mat4::from_translation(glam::vec3(-10.,5.,10.)) * glam::Mat4::from_scale(glam::vec3(10.0,10.0,10.0));
|
||||
|
||||
modeldatas[2].transform=model0;
|
||||
modeldatas[2].transform_depth=proj1;// * view1_inv
|
||||
modeldatas[2].use_depth=glam::Vec4::Z;
|
||||
|
||||
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,
|
||||
@@ -201,6 +292,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 {
|
||||
@@ -212,26 +318,28 @@ impl strafe_client::framework::Example for Skybox {
|
||||
time: Instant::now(),
|
||||
pos: glam::Vec3::new(5.0,0.0,5.0),
|
||||
vel: glam::Vec3::new(0.0,0.0,0.0),
|
||||
gravity: glam::Vec3 { x: 0.0, y: -100.0, z: 0.0 },
|
||||
friction: 80.0,
|
||||
gravity: glam::Vec3::new(0.0,-100.0,0.0),
|
||||
friction: 90.0,
|
||||
screen_size: (config.width, config.height),
|
||||
offset: glam::Vec3::new(0.0,4.5,0.0),
|
||||
fov: 1.0, //fov_slope = tan(fov_y/2)
|
||||
pitch: 0.0,
|
||||
yaw: 0.0,
|
||||
mv: 2.7,
|
||||
controls:0,
|
||||
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: &[],
|
||||
});
|
||||
|
||||
@@ -272,7 +380,7 @@ impl strafe_client::framework::Example for Skybox {
|
||||
buffers: &[wgpu::VertexBufferLayout {
|
||||
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
|
||||
step_mode: wgpu::VertexStepMode::Vertex,
|
||||
attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3],
|
||||
attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x2, 2 => Float32x3],
|
||||
}],
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
@@ -321,6 +429,60 @@ impl strafe_client::framework::Example for Skybox {
|
||||
multisample: wgpu::MultisampleState::default(),
|
||||
multiview: None,
|
||||
});
|
||||
let checkered_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Checkered"),
|
||||
layout: Some(&pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "vs_square",
|
||||
buffers: &[],
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: "fs_checkered",
|
||||
targets: &[Some(config.view_formats[0].into())],
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
front_face: wgpu::FrontFace::Cw,
|
||||
..Default::default()
|
||||
},
|
||||
depth_stencil: Some(wgpu::DepthStencilState {
|
||||
format: Self::DEPTH_FORMAT,
|
||||
depth_write_enabled: false,
|
||||
depth_compare: wgpu::CompareFunction::Always,
|
||||
stencil: wgpu::StencilState::default(),
|
||||
bias: wgpu::DepthBiasState::default(),
|
||||
}),
|
||||
multisample: wgpu::MultisampleState::default(),
|
||||
multiview: None,
|
||||
});
|
||||
let depth_overwrite_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Overwrite"),
|
||||
layout: Some(&pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "vs_square",
|
||||
buffers: &[],
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: "fs_overwrite",
|
||||
targets: &[Some(config.view_formats[0].into())],
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
front_face: wgpu::FrontFace::Cw,
|
||||
..Default::default()
|
||||
},
|
||||
depth_stencil: Some(wgpu::DepthStencilState {
|
||||
format: Self::DEPTH_FORMAT,
|
||||
depth_write_enabled: true,
|
||||
depth_compare: wgpu::CompareFunction::Always,
|
||||
stencil: wgpu::StencilState::default(),
|
||||
bias: wgpu::DepthBiasState::default(),
|
||||
}),
|
||||
multisample: wgpu::MultisampleState::default(),
|
||||
multiview: None,
|
||||
});
|
||||
|
||||
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
||||
label: None,
|
||||
@@ -405,12 +567,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,
|
||||
@@ -421,9 +583,42 @@ impl strafe_client::framework::Example for Skybox {
|
||||
resource: wgpu::BindingResource::Sampler(&sampler),
|
||||
},
|
||||
],
|
||||
label: None,
|
||||
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_modeldata_uniform_data(&modeldata);
|
||||
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()),
|
||||
});
|
||||
//all of these are being moved here
|
||||
models.push(Model{
|
||||
transform: modeldata.transform,
|
||||
transform_depth: modeldata.transform_depth,
|
||||
use_depth: modeldata.use_depth,
|
||||
vertex_buf:modeldata.vertex_buf,
|
||||
entities: modeldata.entities,
|
||||
bind_group: model_bind_group,
|
||||
model_buf,
|
||||
})
|
||||
}
|
||||
|
||||
let teapot = models.pop().unwrap();
|
||||
|
||||
let depth_view = Self::create_depth_texture(config, device);
|
||||
|
||||
Skybox {
|
||||
@@ -431,11 +626,15 @@ impl strafe_client::framework::Example for Skybox {
|
||||
sky_pipeline,
|
||||
entity_pipeline,
|
||||
ground_pipeline,
|
||||
bind_group,
|
||||
uniform_buf,
|
||||
entities,
|
||||
special_teapot: teapot,
|
||||
checkered_pipeline,
|
||||
depth_overwrite_pipeline,
|
||||
main_bind_group,
|
||||
camera_buf,
|
||||
models,
|
||||
depth_view,
|
||||
staging_belt: wgpu::util::StagingBelt::new(0x100),
|
||||
start_time: Instant::now(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -480,6 +679,10 @@ impl strafe_client::framework::Example for Skybox {
|
||||
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_JUMP,
|
||||
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_JUMP,
|
||||
}
|
||||
(k,winit::event::VirtualKeyCode::Z) => match k {
|
||||
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_ZOOM,
|
||||
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_ZOOM,
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
@@ -488,8 +691,8 @@ impl strafe_client::framework::Example for Skybox {
|
||||
}
|
||||
|
||||
fn move_mouse(&mut self, delta: (f64,f64)) {
|
||||
self.camera.pitch+=(delta.1/-512.) as f32;
|
||||
self.camera.yaw+=(delta.0/-512.) as f32;
|
||||
self.camera.pitch=(self.camera.pitch as f64+delta.1/-2048.) as f32;
|
||||
self.camera.yaw=(self.camera.yaw as f64+delta.0/-2048.) as f32;
|
||||
}
|
||||
|
||||
fn resize(
|
||||
@@ -515,7 +718,7 @@ impl strafe_client::framework::Example for Skybox {
|
||||
let dt=(time-self.camera.time).as_secs_f32();
|
||||
self.camera.time=time;
|
||||
let camera_mat=glam::Mat3::from_euler(glam::EulerRot::YXZ,self.camera.yaw,0f32,0f32);
|
||||
let control_dir=camera_mat*get_control_dir(self.camera.controls&(CONTROL_MOVELEFT|CONTROL_MOVERIGHT|CONTROL_MOVEFORWARD|CONTROL_MOVEBACK));
|
||||
let control_dir=camera_mat*get_control_dir(self.camera.controls&(CONTROL_MOVELEFT|CONTROL_MOVERIGHT|CONTROL_MOVEFORWARD|CONTROL_MOVEBACK)).normalize_or_zero();
|
||||
let d=self.camera.vel.dot(control_dir);
|
||||
if d<self.camera.mv {
|
||||
self.camera.vel+=(self.camera.mv-d)*control_dir;
|
||||
@@ -529,32 +732,64 @@ impl strafe_client::framework::Example for Skybox {
|
||||
}
|
||||
if self.camera.grounded&&(self.camera.controls&CONTROL_JUMP)!=0 {
|
||||
self.camera.grounded=false;
|
||||
self.camera.vel+=glam::Vec3::new(0.0,50.0,0.0);
|
||||
self.camera.vel+=glam::Vec3::new(0.0,0.715588/2.0*100.0,0.0);
|
||||
}
|
||||
if self.camera.grounded {
|
||||
let applied_friction=self.camera.friction*dt;
|
||||
if applied_friction*applied_friction<self.camera.vel.length_squared() {
|
||||
self.camera.vel-=applied_friction*self.camera.vel.normalize();
|
||||
let targetv=control_dir*self.camera.walkspeed;
|
||||
let diffv=targetv-self.camera.vel;
|
||||
if applied_friction*applied_friction<diffv.length_squared() {
|
||||
self.camera.vel+=applied_friction*diffv.normalize();
|
||||
} else {
|
||||
self.camera.vel=glam::Vec3::new(0.0,0.0,0.0);
|
||||
self.camera.vel=targetv;
|
||||
}
|
||||
}
|
||||
|
||||
let proj1 = glam::Mat4::orthographic_rh(-20.0, 20.0, -20.0, 20.0, -20.0, 20.0);
|
||||
let model1 = glam::Mat4::from_euler(glam::EulerRot::YXZ, self.start_time.elapsed().as_secs_f32(),0f32,0f32);
|
||||
|
||||
self.special_teapot.transform_depth=proj1 * model1;
|
||||
|
||||
let mut encoder =
|
||||
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));
|
||||
//special teapot
|
||||
{
|
||||
let model_uniforms = get_model_uniform_data(&self.special_teapot);
|
||||
self.staging_belt
|
||||
.write_buffer(
|
||||
&mut encoder,
|
||||
&self.special_teapot.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));
|
||||
}
|
||||
//This code only needs to run when the uniforms change
|
||||
for model in self.models.iter() {
|
||||
let model_uniforms = get_model_uniform_data(&model);
|
||||
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();
|
||||
|
||||
{
|
||||
@@ -583,15 +818,39 @@ impl strafe_client::framework::Example for Skybox {
|
||||
}),
|
||||
});
|
||||
|
||||
rpass.set_bind_group(0, &self.bind_group, &[]);
|
||||
rpass.set_bind_group(0, &self.main_bind_group, &[]);
|
||||
|
||||
//draw special teapot
|
||||
rpass.set_bind_group(1, &self.special_teapot.bind_group, &[]);
|
||||
rpass.set_pipeline(&self.checkered_pipeline);
|
||||
rpass.draw(0..6, 0..1);
|
||||
|
||||
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);
|
||||
rpass.set_vertex_buffer(0, self.special_teapot.vertex_buf.slice(..));
|
||||
|
||||
for entity in self.special_teapot.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.depth_overwrite_pipeline);
|
||||
rpass.draw(0..6, 0..1);
|
||||
|
||||
//draw models
|
||||
rpass.set_pipeline(&self.entity_pipeline);
|
||||
for model in self.models.iter() {
|
||||
rpass.set_bind_group(1, &model.bind_group, &[]);
|
||||
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_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.set_pipeline(&self.sky_pipeline);
|
||||
@@ -605,5 +864,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()
|
||||
);
|
||||
}
|
||||
|
||||
102
src/shader.wgsl
102
src/shader.wgsl
@@ -47,13 +47,13 @@ struct GroundOutput {
|
||||
@vertex
|
||||
fn vs_ground(@builtin(vertex_index) vertex_index: u32) -> GroundOutput {
|
||||
// hacky way to draw two triangles that make a square
|
||||
let tmp1 = (i32(vertex_index)-i32(vertex_index)/3*2) / 2;
|
||||
let tmp2 = (i32(vertex_index)-i32(vertex_index)/3*2) & 1;
|
||||
let tmp1 = i32(vertex_index)/2-i32(vertex_index)/3;
|
||||
let tmp2 = i32(vertex_index)&1;
|
||||
let pos = vec3<f32>(
|
||||
f32(tmp1) * 2.0 - 1.0,
|
||||
0.0,
|
||||
f32(tmp2) * 2.0 - 1.0
|
||||
) * 100.0;
|
||||
) * 160.0;
|
||||
|
||||
var result: GroundOutput;
|
||||
result.pos = pos;
|
||||
@@ -63,19 +63,63 @@ fn vs_ground(@builtin(vertex_index) vertex_index: u32) -> GroundOutput {
|
||||
|
||||
struct EntityOutput {
|
||||
@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>,
|
||||
};
|
||||
|
||||
struct TransformData {
|
||||
transform: mat4x4<f32>,
|
||||
depth: mat4x4<f32>,
|
||||
use_depth: vec4<f32>,
|
||||
};
|
||||
|
||||
@group(1)
|
||||
@binding(0)
|
||||
var<uniform> transform: TransformData;
|
||||
|
||||
@vertex
|
||||
fn vs_entity(
|
||||
@location(0) pos: vec3<f32>,
|
||||
@location(1) normal: vec3<f32>,
|
||||
@location(1) texture: vec2<f32>,
|
||||
@location(2) normal: vec3<f32>,
|
||||
) -> EntityOutput {
|
||||
var position_depth: vec4<f32> = transform.depth * vec4<f32>(pos, 1.0);
|
||||
var position_depth_0: vec4<f32> = position_depth;
|
||||
position_depth_0.z=0.0;
|
||||
var position: vec4<f32> = transform.transform * mix(position_depth,position_depth_0,transform.use_depth);
|
||||
|
||||
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 = (transform.transform * mix(vec4<f32>(normal,0.0),vec4<f32>(0.0,0.0,1.0,0.0),transform.use_depth.z)).xyz;
|
||||
result.texture=texture;
|
||||
result.view = position.xyz - r_data.cam_pos.xyz;
|
||||
var screen_position: vec4<f32> = r_data.proj * r_data.view * position;
|
||||
result.position = mix(screen_position,position_depth,transform.use_depth);
|
||||
return result;
|
||||
}
|
||||
|
||||
struct SquareOutput {
|
||||
@builtin(position) position: vec4<f32>,
|
||||
@location(2) normal: vec3<f32>,
|
||||
@location(3) view: vec3<f32>,
|
||||
@location(4) pos: vec3<f32>,
|
||||
};
|
||||
@vertex
|
||||
fn vs_square(@builtin(vertex_index) vertex_index: u32) -> SquareOutput {
|
||||
// hacky way to draw two triangles that make a square
|
||||
let tmp1 = i32(vertex_index)/2-i32(vertex_index)/3;
|
||||
let tmp2 = i32(vertex_index)&1;
|
||||
let pos = vec3<f32>(
|
||||
(f32(tmp1) - 0.5)*1.8,
|
||||
f32(tmp2) - 0.5,
|
||||
0.0
|
||||
);
|
||||
|
||||
var result: SquareOutput;
|
||||
result.normal = vec3<f32>(0.0,0.0,1.0);
|
||||
result.pos = (transform.transform * vec4<f32>(pos, 1.0)).xyz;
|
||||
result.view = result.pos - r_data.cam_pos.xyz;
|
||||
result.position = r_data.proj * r_data.view * transform.transform * vec4<f32>(pos, 1.0);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -95,14 +139,50 @@ fn fs_sky(vertex: SkyOutput) -> @location(0) vec4<f32> {
|
||||
fn fs_entity(vertex: EntityOutput) -> @location(0) vec4<f32> {
|
||||
let incident = normalize(vertex.view);
|
||||
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;
|
||||
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 {
|
||||
var m = a % b;
|
||||
if (m < 0.0) {
|
||||
if (b < 0.0) {
|
||||
m -= b;
|
||||
} else {
|
||||
m += b;
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn fs_ground(vertex: GroundOutput) -> @location(0) vec4<f32> {
|
||||
let dir = vec3<f32>(-1.0)+vec3<f32>(vertex.pos.x/16.%1.0,0.0,vertex.pos.z/16.%1.0)*2.0;
|
||||
let dir = vec3<f32>(-1.0)+vec3<f32>(modulo_euclidean(vertex.pos.x/16.,1.0),0.0,modulo_euclidean(vertex.pos.z/16.,1.0))*2.0;
|
||||
return vec4<f32>(textureSample(r_texture, r_sampler, dir).rgb, 1.0);
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn fs_checkered(vertex: SquareOutput) -> @location(0) vec4<f32> {
|
||||
let voxel_parity: f32 = f32(
|
||||
u32(modulo_euclidean(vertex.pos.x,2.0)<1.0)
|
||||
^ u32(modulo_euclidean(vertex.pos.y,2.0)<1.0)
|
||||
//^ u32(modulo_euclidean(vertex.pos.z,2.0)<1.0)
|
||||
);
|
||||
|
||||
let incident = normalize(vertex.view);
|
||||
let normal = normalize(vertex.normal);
|
||||
let d = dot(normal, incident);
|
||||
let reflected = incident - 2.0 * d * normal;
|
||||
|
||||
let texture_color = vec3<f32>(1.0)*voxel_parity;
|
||||
let reflected_color = textureSample(r_texture, r_sampler, reflected).rgb;
|
||||
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);
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn fs_overwrite(vertex: SquareOutput) {}
|
||||
Reference in New Issue
Block a user