|
|
|
@ -1,5 +1,4 @@
|
|
|
|
|
use bytemuck::{Pod, Zeroable};
|
|
|
|
|
use strafe_client::{instruction::{TimedInstruction, InstructionConsumer},body::{InputInstruction, PhysicsInstruction}};
|
|
|
|
|
use std::{borrow::Cow, time::Instant};
|
|
|
|
|
use wgpu::{util::DeviceExt, AstcBlock, AstcChannel};
|
|
|
|
|
|
|
|
|
@ -21,30 +20,125 @@ struct Entity {
|
|
|
|
|
//temp?
|
|
|
|
|
struct ModelData {
|
|
|
|
|
transform: glam::Mat4,
|
|
|
|
|
transform_depth: glam::Mat4,
|
|
|
|
|
use_depth: glam::Vec4,
|
|
|
|
|
vertex_buf: wgpu::Buffer,
|
|
|
|
|
entities: Vec<Entity>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ModelGraphics {
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Skybox {
|
|
|
|
|
start_time: std::time::Instant,
|
|
|
|
|
// Note: we use the Y=up coordinate space in this example.
|
|
|
|
|
struct Camera {
|
|
|
|
|
time: Instant,
|
|
|
|
|
pos: glam::Vec3,
|
|
|
|
|
vel: glam::Vec3,
|
|
|
|
|
gravity: glam::Vec3,
|
|
|
|
|
friction: f32,
|
|
|
|
|
screen_size: (u32, u32),
|
|
|
|
|
physics: strafe_client::body::PhysicsState,
|
|
|
|
|
offset: glam::Vec3,
|
|
|
|
|
fov: f32,
|
|
|
|
|
yaw: f32,
|
|
|
|
|
pitch: f32,
|
|
|
|
|
controls: u32,
|
|
|
|
|
mv: f32,
|
|
|
|
|
grounded: bool,
|
|
|
|
|
walkspeed: f32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const CONTROL_MOVEFORWARD:u32 = 0b00000001;
|
|
|
|
|
const CONTROL_MOVEBACK:u32 = 0b00000010;
|
|
|
|
|
const CONTROL_MOVERIGHT:u32 = 0b00000100;
|
|
|
|
|
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 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);
|
|
|
|
|
const UP_DIR:glam::Vec3 = glam::Vec3::new(0.0,1.0,0.0);
|
|
|
|
|
|
|
|
|
|
fn get_control_dir(controls: u32) -> glam::Vec3{
|
|
|
|
|
//don't get fancy just do it
|
|
|
|
|
let mut control_dir:glam::Vec3 = glam::Vec3::new(0.0,0.0,0.0);
|
|
|
|
|
if controls & CONTROL_MOVEFORWARD == CONTROL_MOVEFORWARD {
|
|
|
|
|
control_dir+=FORWARD_DIR;
|
|
|
|
|
}
|
|
|
|
|
if controls & CONTROL_MOVEBACK == CONTROL_MOVEBACK {
|
|
|
|
|
control_dir+=-FORWARD_DIR;
|
|
|
|
|
}
|
|
|
|
|
if controls & CONTROL_MOVELEFT == CONTROL_MOVELEFT {
|
|
|
|
|
control_dir+=-RIGHT_DIR;
|
|
|
|
|
}
|
|
|
|
|
if controls & CONTROL_MOVERIGHT == CONTROL_MOVERIGHT {
|
|
|
|
|
control_dir+=RIGHT_DIR;
|
|
|
|
|
}
|
|
|
|
|
if controls & CONTROL_MOVEUP == CONTROL_MOVEUP {
|
|
|
|
|
control_dir+=UP_DIR;
|
|
|
|
|
}
|
|
|
|
|
if controls & CONTROL_MOVEDOWN == CONTROL_MOVEDOWN {
|
|
|
|
|
control_dir+=-UP_DIR;
|
|
|
|
|
}
|
|
|
|
|
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 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_inv)[..]);
|
|
|
|
|
raw[48..52].copy_from_slice(AsRef::<[f32; 4]>::as_ref(&view.col(3)));
|
|
|
|
|
raw
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Skybox {
|
|
|
|
|
camera: Camera,
|
|
|
|
|
sky_pipeline: wgpu::RenderPipeline,
|
|
|
|
|
entity_pipeline: wgpu::RenderPipeline,
|
|
|
|
|
ground_pipeline: wgpu::RenderPipeline,
|
|
|
|
|
special_teapot: Model,
|
|
|
|
|
checkered_pipeline: wgpu::RenderPipeline,
|
|
|
|
|
depth_overwrite_pipeline: wgpu::RenderPipeline,
|
|
|
|
|
main_bind_group: wgpu::BindGroup,
|
|
|
|
|
camera_buf: wgpu::Buffer,
|
|
|
|
|
models: Vec<ModelGraphics>,
|
|
|
|
|
models: Vec<Model>,
|
|
|
|
|
depth_view: wgpu::TextureView,
|
|
|
|
|
staging_belt: wgpu::util::StagingBelt,
|
|
|
|
|
start_time: Instant,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Skybox {
|
|
|
|
@ -73,9 +167,18 @@ impl Skybox {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_transform_uniform_data(transform:&glam::Mat4) -> [f32; 4*4] {
|
|
|
|
|
let mut raw = [0f32; 4*4];
|
|
|
|
|
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(transform)[..]);
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -92,7 +195,7 @@ fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec<ModelData>,source:&[u8]){
|
|
|
|
|
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);
|
|
|
|
|
indices.push(i as u16);
|
|
|
|
|
}else{
|
|
|
|
|
let i=vertices.len() as u16;
|
|
|
|
|
vertices.push(Vertex {
|
|
|
|
@ -123,27 +226,14 @@ fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec<ModelData>,source:&[u8]){
|
|
|
|
|
});
|
|
|
|
|
modeldatas.push(ModelData {
|
|
|
|
|
transform: glam::Mat4::default(),
|
|
|
|
|
transform_depth: glam::Mat4::default(),
|
|
|
|
|
use_depth: glam::Vec4::ZERO,
|
|
|
|
|
vertex_buf,
|
|
|
|
|
entities,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn to_uniform_data(camera: &strafe_client::body::Camera, pos: glam::Vec3) -> [f32; 16 * 3 + 4] {
|
|
|
|
|
let proj=camera.proj();
|
|
|
|
|
let proj_inv = proj.inverse();
|
|
|
|
|
let view=camera.view(pos);
|
|
|
|
|
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_inv)[..]);
|
|
|
|
|
raw[48..52].copy_from_slice(AsRef::<[f32; 4]>::as_ref(&view.col(3)));
|
|
|
|
|
raw
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl strafe_client::framework::Example for Skybox {
|
|
|
|
|
fn optional_features() -> wgpu::Features {
|
|
|
|
|
wgpu::Features::TEXTURE_COMPRESSION_ASTC
|
|
|
|
@ -163,7 +253,13 @@ impl strafe_client::framework::Example for Skybox {
|
|
|
|
|
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.));
|
|
|
|
|
modeldatas[2].transform=glam::Mat4::from_translation(glam::vec3(-10.,5.,10.));
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
@ -218,63 +314,29 @@ impl strafe_client::framework::Example for Skybox {
|
|
|
|
|
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let physics = strafe_client::body::PhysicsState {
|
|
|
|
|
body: strafe_client::body::Body::with_pva(glam::vec3(0.0,50.0,0.0),glam::vec3(0.0,0.0,0.0),glam::vec3(0.0,-100.0,0.0)),
|
|
|
|
|
time: 0,
|
|
|
|
|
tick: 0,
|
|
|
|
|
strafe_tick_num: 100,//100t
|
|
|
|
|
strafe_tick_den: 1_000_000_000,
|
|
|
|
|
gravity: glam::vec3(0.0,-100.0,0.0),
|
|
|
|
|
friction: 1.2,
|
|
|
|
|
walk_accel: 90.0,
|
|
|
|
|
let camera = Camera {
|
|
|
|
|
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::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,
|
|
|
|
|
grounded: false,
|
|
|
|
|
controls:0,
|
|
|
|
|
grounded: true,
|
|
|
|
|
walkspeed: 18.0,
|
|
|
|
|
contacts: std::collections::HashSet::new(),
|
|
|
|
|
models_cringe_clone: modeldatas.iter().map(|m|strafe_client::body::Model::new(m.transform)).collect(),
|
|
|
|
|
walk: strafe_client::body::WalkState::new(),
|
|
|
|
|
hitbox_halfsize: glam::vec3(1.0,2.5,1.0),
|
|
|
|
|
camera: strafe_client::body::Camera::from_offset(glam::vec3(0.0,4.5-2.5,0.0),(config.width as f32)/(config.height as f32)),
|
|
|
|
|
mouse_interpolation: strafe_client::body::MouseInterpolationState::new(),
|
|
|
|
|
controls: 0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let camera_uniforms = to_uniform_data(&physics.camera,physics.body.extrapolated_position(0));
|
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//drain the modeldata vec so entities can be /moved/ to models.entities
|
|
|
|
|
let mut models = Vec::<ModelGraphics>::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!("ModelGraphics{}",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!("ModelGraphics{}",i).as_str()),
|
|
|
|
|
});
|
|
|
|
|
//all of these are being moved here
|
|
|
|
|
models.push(ModelGraphics{
|
|
|
|
|
transform: modeldata.transform,
|
|
|
|
|
vertex_buf:modeldata.vertex_buf,
|
|
|
|
|
entities: modeldata.entities,
|
|
|
|
|
bind_group: model_bind_group,
|
|
|
|
|
model_buf,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
|
|
|
label: None,
|
|
|
|
|
bind_group_layouts: &[&main_bind_group_layout, &model_bind_group_layout],
|
|
|
|
@ -367,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,
|
|
|
|
@ -470,88 +586,115 @@ impl strafe_client::framework::Example for Skybox {
|
|
|
|
|
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 {
|
|
|
|
|
start_time: Instant::now(),
|
|
|
|
|
screen_size: (config.width,config.height),
|
|
|
|
|
physics,
|
|
|
|
|
camera,
|
|
|
|
|
sky_pipeline,
|
|
|
|
|
entity_pipeline,
|
|
|
|
|
ground_pipeline,
|
|
|
|
|
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(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(clippy::single_match)]
|
|
|
|
|
fn update(&mut self, event: winit::event::WindowEvent) {
|
|
|
|
|
//nothing atm
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn device_event(&mut self, event: winit::event::DeviceEvent) {
|
|
|
|
|
//there's no way this is the best way get a timestamp.
|
|
|
|
|
let time=self.start_time.elapsed().as_nanos() as i64;
|
|
|
|
|
match event {
|
|
|
|
|
winit::event::DeviceEvent::Key(winit::event::KeyboardInput {
|
|
|
|
|
state,
|
|
|
|
|
scancode: keycode,
|
|
|
|
|
winit::event::WindowEvent::KeyboardInput {
|
|
|
|
|
input:
|
|
|
|
|
winit::event::KeyboardInput {
|
|
|
|
|
state,
|
|
|
|
|
virtual_keycode: Some(keycode),
|
|
|
|
|
..
|
|
|
|
|
},
|
|
|
|
|
..
|
|
|
|
|
}) => {
|
|
|
|
|
let s=match state {
|
|
|
|
|
winit::event::ElementState::Pressed => true,
|
|
|
|
|
winit::event::ElementState::Released => false,
|
|
|
|
|
};
|
|
|
|
|
if let Some(input_instruction)=match keycode {
|
|
|
|
|
17 => Some(InputInstruction::MoveForward(s)),//W
|
|
|
|
|
30 => Some(InputInstruction::MoveLeft(s)),//A
|
|
|
|
|
31 => Some(InputInstruction::MoveBack(s)),//S
|
|
|
|
|
32 => Some(InputInstruction::MoveRight(s)),//D
|
|
|
|
|
18 => Some(InputInstruction::MoveUp(s)),//E
|
|
|
|
|
16 => Some(InputInstruction::MoveDown(s)),//Q
|
|
|
|
|
57 => Some(InputInstruction::Jump(s)),//Space
|
|
|
|
|
44 => Some(InputInstruction::Zoom(s)),//Z
|
|
|
|
|
19 => if s{Some(InputInstruction::Reset)}else{None},//R
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
self.physics.run(time);
|
|
|
|
|
self.physics.process_instruction(TimedInstruction{
|
|
|
|
|
time,
|
|
|
|
|
instruction:PhysicsInstruction::Input(input_instruction),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
winit::event::DeviceEvent::MouseMotion {
|
|
|
|
|
delta,//these (f64,f64) are integers on my machine
|
|
|
|
|
} => {
|
|
|
|
|
//do not step the physics because the mouse polling rate is higher than the physics can run.
|
|
|
|
|
//essentially the previous input will be overwritten until a true step runs
|
|
|
|
|
//which is fine because they run all the time.
|
|
|
|
|
self.physics.process_instruction(TimedInstruction{
|
|
|
|
|
time,
|
|
|
|
|
instruction:PhysicsInstruction::Input(InputInstruction::MoveMouse(glam::ivec2(delta.0 as i32,delta.1 as i32))),
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
winit::event::DeviceEvent::MouseWheel {
|
|
|
|
|
delta,
|
|
|
|
|
} => {
|
|
|
|
|
println!("mousewheel{:?}",delta);
|
|
|
|
|
if true{//self.physics.use_scroll
|
|
|
|
|
self.physics.run(time);
|
|
|
|
|
self.physics.process_instruction(TimedInstruction{
|
|
|
|
|
time,
|
|
|
|
|
instruction:PhysicsInstruction::Input(InputInstruction::Jump(true)),//activates the immediate jump path, but the style modifier prevents controls&CONTROL_JUMP bit from being set to auto jump
|
|
|
|
|
})
|
|
|
|
|
match (state,keycode) {
|
|
|
|
|
(k,winit::event::VirtualKeyCode::W) => match k {
|
|
|
|
|
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_MOVEFORWARD,
|
|
|
|
|
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_MOVEFORWARD,
|
|
|
|
|
}
|
|
|
|
|
(k,winit::event::VirtualKeyCode::A) => match k {
|
|
|
|
|
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_MOVELEFT,
|
|
|
|
|
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_MOVELEFT,
|
|
|
|
|
}
|
|
|
|
|
(k,winit::event::VirtualKeyCode::S) => match k {
|
|
|
|
|
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_MOVEBACK,
|
|
|
|
|
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_MOVEBACK,
|
|
|
|
|
}
|
|
|
|
|
(k,winit::event::VirtualKeyCode::D) => match k {
|
|
|
|
|
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_MOVERIGHT,
|
|
|
|
|
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_MOVERIGHT,
|
|
|
|
|
}
|
|
|
|
|
(k,winit::event::VirtualKeyCode::E) => match k {
|
|
|
|
|
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_MOVEUP,
|
|
|
|
|
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_MOVEUP,
|
|
|
|
|
}
|
|
|
|
|
(k,winit::event::VirtualKeyCode::Q) => match k {
|
|
|
|
|
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_MOVEDOWN,
|
|
|
|
|
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_MOVEDOWN,
|
|
|
|
|
}
|
|
|
|
|
(k,winit::event::VirtualKeyCode::Space) => match k {
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_=>(),
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn move_mouse(&mut self, delta: (f64,f64)) {
|
|
|
|
|
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(
|
|
|
|
|
&mut self,
|
|
|
|
|
config: &wgpu::SurfaceConfiguration,
|
|
|
|
@ -559,8 +702,7 @@ impl strafe_client::framework::Example for Skybox {
|
|
|
|
|
_queue: &wgpu::Queue,
|
|
|
|
|
) {
|
|
|
|
|
self.depth_view = Self::create_depth_texture(config, device);
|
|
|
|
|
self.screen_size = (config.width, config.height);
|
|
|
|
|
self.physics.camera.set_fov_aspect(1.0,(config.width as f32)/(config.height as f32));
|
|
|
|
|
self.camera.screen_size = (config.width, config.height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render(
|
|
|
|
@ -570,15 +712,49 @@ impl strafe_client::framework::Example for Skybox {
|
|
|
|
|
queue: &wgpu::Queue,
|
|
|
|
|
_spawner: &strafe_client::framework::Spawner,
|
|
|
|
|
) {
|
|
|
|
|
let time=self.start_time.elapsed().as_nanos() as i64;
|
|
|
|
|
let time = Instant::now();
|
|
|
|
|
|
|
|
|
|
self.physics.run(time);
|
|
|
|
|
//physique
|
|
|
|
|
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)).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;
|
|
|
|
|
}
|
|
|
|
|
self.camera.vel+=self.camera.gravity*dt;
|
|
|
|
|
self.camera.pos+=self.camera.vel*dt;
|
|
|
|
|
if self.camera.pos.y<0.0{
|
|
|
|
|
self.camera.pos.y=0.0;
|
|
|
|
|
self.camera.vel.y=0.0;
|
|
|
|
|
self.camera.grounded=true;
|
|
|
|
|
}
|
|
|
|
|
if self.camera.grounded&&(self.camera.controls&CONTROL_JUMP)!=0 {
|
|
|
|
|
self.camera.grounded=false;
|
|
|
|
|
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;
|
|
|
|
|
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=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 camera_uniforms = to_uniform_data(&self.physics.camera,self.physics.body.extrapolated_position(time));
|
|
|
|
|
let camera_uniforms = self.camera.to_uniform_data();
|
|
|
|
|
self.staging_belt
|
|
|
|
|
.write_buffer(
|
|
|
|
|
&mut encoder,
|
|
|
|
@ -588,9 +764,22 @@ impl strafe_client::framework::Example for Skybox {
|
|
|
|
|
device,
|
|
|
|
|
)
|
|
|
|
|
.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_transform_uniform_data(&model.transform);
|
|
|
|
|
let model_uniforms = get_model_uniform_data(&model);
|
|
|
|
|
self.staging_belt
|
|
|
|
|
.write_buffer(
|
|
|
|
|
&mut encoder,
|
|
|
|
@ -631,6 +820,23 @@ impl strafe_client::framework::Example for Skybox {
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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, &[]);
|
|
|
|
|