Compare commits
24 Commits
depth-fudg
...
timelines
Author | SHA1 | Date | |
---|---|---|---|
53d2b5b3f5 | |||
c565120ea7 | |||
4f5c9afed3 | |||
53605746d4 | |||
cead05b08b | |||
949897a558 | |||
a4ed50fc38 | |||
846f681648 | |||
ff54a03487 | |||
8c2dda5205 | |||
addde65caa | |||
2c4e6f642b | |||
f11742ef3b | |||
e6862b5bad | |||
e18e8a9a7d | |||
f600092f13 | |||
1f1ef5d3ad | |||
54bf0358d6 | |||
6caa273623 | |||
101f0f8d12 | |||
fc751a9fe8 | |||
5391b635fb | |||
230469496e | |||
ea3134de51 |
321
src/body.rs
Normal file
321
src/body.rs
Normal file
@ -0,0 +1,321 @@
|
||||
use crate::instruction::TimedInstruction;
|
||||
|
||||
pub enum PhysicsInstruction {
|
||||
CollisionStart(RelativeCollision),
|
||||
CollisionEnd(RelativeCollision),
|
||||
StrafeTick,
|
||||
Jump,
|
||||
SetWalkTargetVelocity(glam::Vec3),
|
||||
ReachWalkTargetVelocity,
|
||||
// Water,
|
||||
// Spawn(
|
||||
// Option<SpawnId>,
|
||||
// bool,//true = Trigger; false = teleport
|
||||
// bool,//true = Force
|
||||
// )
|
||||
}
|
||||
|
||||
pub struct Body {
|
||||
pub position: glam::Vec3,//I64 where 2^32 = 1 u
|
||||
pub velocity: glam::Vec3,//I64 where 2^32 = 1 u/s
|
||||
pub time: TIME,//nanoseconds x xxxxD!
|
||||
}
|
||||
|
||||
pub enum MoveRestriction {
|
||||
Air,
|
||||
Water,
|
||||
Ground,
|
||||
Ladder,//multiple ladders how
|
||||
}
|
||||
|
||||
pub struct PhysicsState {
|
||||
pub body: Body,
|
||||
pub contacts: Vec<RelativeCollision>,
|
||||
pub models_cringe_clone: Vec<Model>,
|
||||
pub time: TIME,
|
||||
pub strafe_tick_num: TIME,
|
||||
pub strafe_tick_den: TIME,
|
||||
pub tick: u32,
|
||||
pub mv: f32,
|
||||
pub walkspeed: f32,
|
||||
pub friction: f32,
|
||||
pub gravity: glam::Vec3,
|
||||
pub grounded: bool,
|
||||
pub jump_trying: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone,Copy)]
|
||||
pub enum AabbFace{
|
||||
Right,//+X
|
||||
Top,
|
||||
Back,
|
||||
Left,
|
||||
Bottom,
|
||||
Front,
|
||||
}
|
||||
|
||||
pub struct Aabb {
|
||||
min: glam::Vec3,
|
||||
max: glam::Vec3,
|
||||
}
|
||||
|
||||
impl Aabb {
|
||||
// const FACE_DATA: [[f32; 3]; 6] = [
|
||||
// [0.0f32, 0., 1.],
|
||||
// [0.0f32, 0., -1.],
|
||||
// [1.0f32, 0., 0.],
|
||||
// [-1.0f32, 0., 0.],
|
||||
// [0.0f32, 1., 0.],
|
||||
// [0.0f32, -1., 0.],
|
||||
// ];
|
||||
const VERTEX_DATA_RIGHT: [glam::Vec3; 4] = [
|
||||
glam::vec3(1., -1., -1.),
|
||||
glam::vec3(1., 1., -1.),
|
||||
glam::vec3(1., 1., 1.),
|
||||
glam::vec3(1., -1., 1.),
|
||||
];
|
||||
const VERTEX_DATA_TOP: [glam::Vec3; 4] = [
|
||||
glam::vec3(1., 1., -1.),
|
||||
glam::vec3(-1., 1., -1.),
|
||||
glam::vec3(-1., 1., 1.),
|
||||
glam::vec3(1., 1., 1.),
|
||||
];
|
||||
const VERTEX_DATA_BACK: [glam::Vec3; 4] = [
|
||||
glam::vec3(-1., -1., 1.),
|
||||
glam::vec3(1., -1., 1.),
|
||||
glam::vec3(1., 1., 1.),
|
||||
glam::vec3(-1., 1., 1.),
|
||||
];
|
||||
const VERTEX_DATA_LEFT: [glam::Vec3; 4] = [
|
||||
glam::vec3(-1., -1., 1.),
|
||||
glam::vec3(-1., 1., 1.),
|
||||
glam::vec3(-1., 1., -1.),
|
||||
glam::vec3(-1., -1., -1.),
|
||||
];
|
||||
const VERTEX_DATA_BOTTOM: [glam::Vec3; 4] = [
|
||||
glam::vec3(1., -1., 1.),
|
||||
glam::vec3(-1., -1., 1.),
|
||||
glam::vec3(-1., -1., -1.),
|
||||
glam::vec3(1., -1., -1.),
|
||||
];
|
||||
const VERTEX_DATA_FRONT: [glam::Vec3; 4] = [
|
||||
glam::vec3(-1., 1., -1.),
|
||||
glam::vec3(1., 1., -1.),
|
||||
glam::vec3(1., -1., -1.),
|
||||
glam::vec3(-1., -1., -1.),
|
||||
];
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self {min: glam::Vec3::INFINITY,max: glam::Vec3::NEG_INFINITY}
|
||||
}
|
||||
|
||||
pub fn grow(&mut self, point:glam::Vec3){
|
||||
self.min=self.min.min(point);
|
||||
self.max=self.max.max(point);
|
||||
}
|
||||
|
||||
pub fn normal(face:AabbFace) -> glam::Vec3 {
|
||||
match face {
|
||||
AabbFace::Right => glam::vec3(1.,0.,0.),
|
||||
AabbFace::Top => glam::vec3(0.,1.,0.),
|
||||
AabbFace::Back => glam::vec3(0.,0.,1.),
|
||||
AabbFace::Left => glam::vec3(-1.,0.,0.),
|
||||
AabbFace::Bottom => glam::vec3(0.,-1.,0.),
|
||||
AabbFace::Front => glam::vec3(0.,0.,-1.),
|
||||
}
|
||||
}
|
||||
pub fn face_vertices(face:AabbFace) -> [glam::Vec3;4] {
|
||||
match face {
|
||||
AabbFace::Right => Self::VERTEX_DATA_RIGHT,
|
||||
AabbFace::Top => Self::VERTEX_DATA_TOP,
|
||||
AabbFace::Back => Self::VERTEX_DATA_BACK,
|
||||
AabbFace::Left => Self::VERTEX_DATA_LEFT,
|
||||
AabbFace::Bottom => Self::VERTEX_DATA_BOTTOM,
|
||||
AabbFace::Front => Self::VERTEX_DATA_FRONT,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type Face = AabbFace;
|
||||
type TreyMesh = Aabb;
|
||||
|
||||
pub struct Model {
|
||||
//A model is a thing that has a hitbox. can be represented by a list of TreyMesh-es
|
||||
//in this iteration, all it needs is extents.
|
||||
transform: glam::Mat4,
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub fn new(transform:glam::Mat4) -> Self {
|
||||
Self{transform}
|
||||
}
|
||||
pub fn face_vertices(&self,face:Face) -> [glam::Vec3;4] {
|
||||
Aabb::face_vertices(face)
|
||||
}
|
||||
pub fn face_mesh(&self,face:Face) -> TreyMesh {
|
||||
let mut aabb=Aabb::new();
|
||||
for &vertex in self.face_vertices(face).iter() {
|
||||
aabb.grow(vertex);
|
||||
}
|
||||
return aabb;
|
||||
}
|
||||
pub fn face_normal(&self,face:Face) -> glam::Vec3 {
|
||||
let mut n=glam::Vec3Swizzles::xyzz(Aabb::normal(face));
|
||||
n.w=0.0;//what a man will do to avoid writing out the components
|
||||
glam::Vec4Swizzles::xyz(self.transform*n)//this is wrong for scale
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RelativeCollision {
|
||||
face: Face,//just an id
|
||||
model: u32,//using id to avoid lifetimes
|
||||
}
|
||||
|
||||
impl RelativeCollision {
|
||||
pub fn mesh(&self,models:&Vec<Model>) -> TreyMesh {
|
||||
return models.get(self.model as usize).unwrap().face_mesh(self.face)
|
||||
}
|
||||
pub fn normal(&self,models:&Vec<Model>) -> glam::Vec3 {
|
||||
return models.get(self.model as usize).unwrap().face_normal(self.face)
|
||||
}
|
||||
}
|
||||
|
||||
pub type TIME = i64;
|
||||
|
||||
const CONTROL_JUMP:u32 = 0b01000000;//temp DATA NORMALIZATION!@#$
|
||||
impl PhysicsState {
|
||||
//delete this, we are tickless gamers
|
||||
pub fn run(&mut self, time: TIME){
|
||||
//prepare is ommitted - everything is done via instructions.
|
||||
while let Some(instruction) = self.next_instruction() {//collect
|
||||
if time<instruction.time {
|
||||
break;
|
||||
}
|
||||
//advance
|
||||
self.advance_time(instruction.time);
|
||||
//process
|
||||
self.process_instruction(instruction);
|
||||
//write hash lol
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//delete this
|
||||
pub fn extrapolate_position(&self, time: TIME) -> glam::Vec3 {
|
||||
let dt=(time-self.body.time) as f64/1_000_000_000f64;
|
||||
self.body.position+self.body.velocity*(dt as f32)+self.gravity*((0.5*dt*dt) as f32)
|
||||
}
|
||||
|
||||
fn next_strafe_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
|
||||
return Some(TimedInstruction{
|
||||
time:(self.time*self.strafe_tick_num/self.strafe_tick_den+1)*self.strafe_tick_den/self.strafe_tick_num,
|
||||
//only poll the physics if there is a before and after mouse event
|
||||
instruction:PhysicsInstruction::StrafeTick
|
||||
});
|
||||
}
|
||||
|
||||
//state mutated on collision:
|
||||
//Accelerator
|
||||
//stair step-up
|
||||
|
||||
//state mutated on instruction
|
||||
//change fly acceleration (fly_sustain)
|
||||
//change fly velocity
|
||||
|
||||
//generic event emmiters
|
||||
//PlatformStandTime
|
||||
//walk/swim/air/ladder sounds
|
||||
//VState?
|
||||
|
||||
//falling under the map
|
||||
// fn next_respawn_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
|
||||
// if self.body.position<self.world.min_y {
|
||||
// return Some(TimedInstruction{
|
||||
// time:self.time,
|
||||
// instruction:PhysicsInstruction::Trigger(None)
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
|
||||
// fn next_water_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
|
||||
// return Some(TimedInstruction{
|
||||
// time:(self.time*self.strafe_tick_num/self.strafe_tick_den+1)*self.strafe_tick_den/self.strafe_tick_num,
|
||||
// //only poll the physics if there is a before and after mouse event
|
||||
// instruction:PhysicsInstruction::Water
|
||||
// });
|
||||
// }
|
||||
|
||||
fn next_walk_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
|
||||
//check if you are accelerating towards a walk target velocity and create an instruction
|
||||
return None;
|
||||
}
|
||||
fn predict_collision_end(&self,model:&Model) -> Option<TimedInstruction<PhysicsInstruction>> {
|
||||
//must treat cancollide false objects differently: you may not exit through the same face you entered.
|
||||
None
|
||||
}
|
||||
fn predict_collision_start(&self,model:&Model) -> Option<TimedInstruction<PhysicsInstruction>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::instruction::InstructionEmitter<PhysicsInstruction> for PhysicsState {
|
||||
//this little next instruction function can cache its return value and invalidate the cached value by watching the State.
|
||||
fn next_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
|
||||
//JUST POLLING!!! NO MUTATION
|
||||
let mut collector = crate::instruction::InstructionCollector::new();
|
||||
//autohop (already pressing spacebar; the signal to begin trying to jump is different)
|
||||
if self.grounded&&self.jump_trying {
|
||||
//scroll will be implemented with InputInstruction::Jump(true) but it blocks setting self.jump_trying=true
|
||||
collector.collect(Some(TimedInstruction{
|
||||
time:self.time,
|
||||
instruction:PhysicsInstruction::Jump
|
||||
}));
|
||||
}
|
||||
//check for collision stop instructions with curent contacts
|
||||
for collision_data in self.contacts.iter() {
|
||||
collector.collect(self.predict_collision_end(self.models_cringe_clone.get(collision_data.model as usize).unwrap()));
|
||||
}
|
||||
//check for collision start instructions (against every part in the game with no optimization!!)
|
||||
for model in &self.models_cringe_clone {
|
||||
collector.collect(self.predict_collision_start(model));
|
||||
}
|
||||
if self.grounded {
|
||||
//walk maintenance
|
||||
collector.collect(self.next_walk_instruction());
|
||||
}else{
|
||||
//check to see when the next strafe tick is
|
||||
collector.collect(self.next_strafe_instruction());
|
||||
}
|
||||
collector.instruction()
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsState {
|
||||
fn process_instruction(&mut self, ins:TimedInstruction<PhysicsInstruction>) {
|
||||
//mutate position and velocity and time
|
||||
self.body.advance_time(ins.time);//should this be in a separate function: self.advance_time?
|
||||
match ins.instruction {
|
||||
PhysicsInstruction::CollisionStart(_) => todo!(),
|
||||
PhysicsInstruction::CollisionEnd(_) => todo!(),
|
||||
PhysicsInstruction::StrafeTick => {
|
||||
let control_dir=self.get_control_dir();//this respects your mouse interpolation settings
|
||||
let d=self.body.velocity.dot(control_dir);
|
||||
if d<self.mv {
|
||||
self.body.velocity+=(self.mv-d)*control_dir;
|
||||
}
|
||||
}
|
||||
PhysicsInstruction::Jump => {
|
||||
self.grounded=false;//do I need this?
|
||||
self.body.velocity+=glam::Vec3::new(0.0,0.715588/2.0*100.0,0.0);
|
||||
}
|
||||
PhysicsInstruction::ReachWalkTargetVelocity => {
|
||||
//precisely set velocity
|
||||
self.body.velocity=self.walk_target_velocity;
|
||||
}
|
||||
PhysicsInstruction::SetWalkTargetVelocity(v) => {
|
||||
self.walk_target_velocity=v;
|
||||
//calculate acceleration yada yada
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
37
src/instruction.rs
Normal file
37
src/instruction.rs
Normal file
@ -0,0 +1,37 @@
|
||||
pub struct TimedInstruction<I> {
|
||||
pub time: crate::body::TIME,
|
||||
pub instruction: I,
|
||||
}
|
||||
|
||||
pub trait InstructionEmitter<I> {
|
||||
fn next_instruction(&self) -> Option<TimedInstruction<I>>;
|
||||
}
|
||||
pub trait InstructionConsumer<I> {
|
||||
fn process_instruction(&mut self, instruction:TimedInstruction<I>);
|
||||
}
|
||||
|
||||
//PROPER PRIVATE FIELDS!!!
|
||||
pub struct InstructionCollector<I> {
|
||||
instruction: Option<TimedInstruction<I>>,
|
||||
}
|
||||
impl<I> InstructionCollector<I> {
|
||||
pub fn new() -> Self {
|
||||
Self{instruction:None}
|
||||
}
|
||||
|
||||
pub fn collect(&mut self,instruction:Option<TimedInstruction<I>>){
|
||||
match &instruction {
|
||||
Some(unwrap_instruction) => match &self.instruction {
|
||||
Some(unwrap_best_instruction) => if unwrap_instruction.time<unwrap_best_instruction.time {
|
||||
self.instruction=instruction;
|
||||
},
|
||||
None => self.instruction=instruction,
|
||||
},
|
||||
None => (),
|
||||
}
|
||||
}
|
||||
pub fn instruction(self) -> Option<TimedInstruction<I>> {
|
||||
//STEAL INSTRUCTION AND DESTROY INSTRUCTIONCOLLECTOR
|
||||
return self.instruction
|
||||
}
|
||||
}
|
@ -1 +1,4 @@
|
||||
pub mod framework;
|
||||
pub mod body;
|
||||
pub mod instruction;
|
||||
pub mod timelines;
|
||||
|
272
src/main.rs
272
src/main.rs
@ -20,16 +20,12 @@ struct Entity {
|
||||
//temp?
|
||||
struct ModelData {
|
||||
transform: glam::Mat4,
|
||||
transform_depth: glam::Mat4,
|
||||
use_depth: glam::Vec4,
|
||||
vertex_buf: wgpu::Buffer,
|
||||
entities: Vec<Entity>,
|
||||
}
|
||||
|
||||
struct Model {
|
||||
struct ModelGraphics {
|
||||
transform: glam::Mat4,
|
||||
transform_depth: glam::Mat4,
|
||||
use_depth: glam::Vec4,
|
||||
vertex_buf: wgpu::Buffer,
|
||||
entities: Vec<Entity>,
|
||||
bind_group: wgpu::BindGroup,
|
||||
@ -38,20 +34,12 @@ struct Model {
|
||||
|
||||
// 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),
|
||||
offset: glam::Vec3,
|
||||
fov: f32,
|
||||
yaw: f32,
|
||||
pitch: f32,
|
||||
controls: u32,
|
||||
mv: f32,
|
||||
grounded: bool,
|
||||
walkspeed: f32,
|
||||
}
|
||||
|
||||
const CONTROL_MOVEFORWARD:u32 = 0b00000001;
|
||||
@ -104,7 +92,7 @@ fn get_control_dir(controls: u32) -> glam::Vec3{
|
||||
}
|
||||
|
||||
impl Camera {
|
||||
fn to_uniform_data(&self) -> [f32; 16 * 3 + 4] {
|
||||
fn to_uniform_data(&self, pos: glam::Vec3) -> [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
|
||||
@ -113,7 +101,7 @@ impl Camera {
|
||||
};
|
||||
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 = glam::Mat4::from_translation(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];
|
||||
@ -126,19 +114,17 @@ impl Camera {
|
||||
}
|
||||
|
||||
pub struct Skybox {
|
||||
start_time: std::time::Instant,
|
||||
camera: Camera,
|
||||
physics: strafe_client::body::PhysicsState,
|
||||
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<Model>,
|
||||
models: Vec<ModelGraphics>,
|
||||
depth_view: wgpu::TextureView,
|
||||
staging_belt: wgpu::util::StagingBelt,
|
||||
start_time: Instant,
|
||||
}
|
||||
|
||||
impl Skybox {
|
||||
@ -167,18 +153,9 @@ 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));
|
||||
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)[..]);
|
||||
raw
|
||||
}
|
||||
|
||||
@ -226,8 +203,6 @@ 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,
|
||||
})
|
||||
@ -253,13 +228,7 @@ 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.));
|
||||
|
||||
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;
|
||||
modeldatas[2].transform=glam::Mat4::from_translation(glam::vec3(-10.,5.,10.));
|
||||
|
||||
let main_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: None,
|
||||
@ -315,28 +284,69 @@ impl strafe_client::framework::Example for Skybox {
|
||||
});
|
||||
|
||||
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,
|
||||
controls:0,
|
||||
grounded: true,
|
||||
walkspeed: 18.0,
|
||||
};
|
||||
let camera_uniforms = camera.to_uniform_data();
|
||||
let physics = strafe_client::body::PhysicsState {
|
||||
body: strafe_client::body::Body {
|
||||
position: glam::Vec3::new(5.0,0.0,5.0),
|
||||
velocity: glam::Vec3::new(0.0,0.0,0.0),
|
||||
time: 0,
|
||||
},
|
||||
time: 0,
|
||||
tick: 0,
|
||||
strafe_tick_num: 100,//100t
|
||||
strafe_tick_den: 1_000_000_000,
|
||||
gravity: glam::Vec3::new(0.0,-100.0,0.0),
|
||||
friction: 90.0,
|
||||
mv: 2.7,
|
||||
grounded: true,
|
||||
jump_trying: false,
|
||||
walkspeed: 18.0,
|
||||
contacts: Vec::<strafe_client::body::RelativeCollision>::new(),
|
||||
models_cringe_clone: modeldatas.iter().map(|m|strafe_client::body::Model::new(m.transform)).collect(),
|
||||
};
|
||||
|
||||
let camera_uniforms = camera.to_uniform_data(physics.extrapolate_position(0));
|
||||
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],
|
||||
@ -429,60 +439,6 @@ 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,
|
||||
@ -586,55 +542,20 @@ 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(),
|
||||
camera,
|
||||
physics,
|
||||
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(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -712,49 +633,18 @@ impl strafe_client::framework::Example for Skybox {
|
||||
queue: &wgpu::Queue,
|
||||
_spawner: &strafe_client::framework::Spawner,
|
||||
) {
|
||||
let time = Instant::now();
|
||||
|
||||
//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);
|
||||
let time=self.start_time.elapsed().as_nanos() as i64;
|
||||
|
||||
self.special_teapot.transform_depth=proj1 * model1;
|
||||
self.physics.run(time,control_dir,self.camera.controls);
|
||||
|
||||
let mut encoder =
|
||||
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
|
||||
|
||||
// update rotation
|
||||
let camera_uniforms = self.camera.to_uniform_data();
|
||||
let camera_uniforms = self.camera.to_uniform_data(self.physics.extrapolate_position(time));
|
||||
self.staging_belt
|
||||
.write_buffer(
|
||||
&mut encoder,
|
||||
@ -764,22 +654,9 @@ 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_model_uniform_data(&model);
|
||||
let model_uniforms = get_transform_uniform_data(&model.transform);
|
||||
self.staging_belt
|
||||
.write_buffer(
|
||||
&mut encoder,
|
||||
@ -820,23 +697,6 @@ 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, &[]);
|
||||
|
@ -68,15 +68,9 @@ struct EntityOutput {
|
||||
@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;
|
||||
var<uniform> r_EntityTransform: mat4x4<f32>;
|
||||
|
||||
@vertex
|
||||
fn vs_entity(
|
||||
@ -84,42 +78,12 @@ fn vs_entity(
|
||||
@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 position: vec4<f32> = r_EntityTransform * vec4<f32>(pos, 1.0);
|
||||
var result: EntityOutput;
|
||||
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.normal = (r_EntityTransform * vec4<f32>(normal, 0.0)).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);
|
||||
result.position = r_data.proj * r_data.view * position;
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -165,24 +129,3 @@ fn fs_ground(vertex: GroundOutput) -> @location(0) vec4<f32> {
|
||||
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) {}
|
8
src/sweep.rs
Normal file
8
src/sweep.rs
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
//something that implements body + hitbox + transform can predict collision
|
||||
impl crate::sweep::PredictCollision for Model {
|
||||
fn predict_collision(&self,other:&Model) -> Option<crate::event::EventStruct> {
|
||||
//math!
|
||||
None
|
||||
}
|
||||
}
|
47
src/timelines.rs
Normal file
47
src/timelines.rs
Normal file
@ -0,0 +1,47 @@
|
||||
type ORDER = u32;
|
||||
|
||||
pub struct Tracker {
|
||||
order: ORDER,
|
||||
}
|
||||
|
||||
pub struct TimelineInstruction<I>{
|
||||
pub order: ORDER,//absolute ordering of instructions which can be used for sorting even when there are multiple simultaneous timestamps
|
||||
pub instruction: crate::instruction::TimedInstruction<I>,
|
||||
}
|
||||
|
||||
pub struct Timeline<I>{
|
||||
instructions: std::collections::VecDeque<TimelineInstruction<I>>,
|
||||
trackers: Vec<Tracker>,//wrong
|
||||
}
|
||||
|
||||
impl<I> Timeline<I>{
|
||||
pub fn new() -> Self {
|
||||
Self{
|
||||
instructions:std::collections::VecDeque::<TimelineInstruction<I>>::new(),
|
||||
trackers:Vec::<Tracker>::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
return self.instructions.len()
|
||||
}
|
||||
pub fn first(&self) -> Option<&TimelineInstruction<I>> {
|
||||
return self.instructions.get(0)
|
||||
}
|
||||
pub fn last(&self) -> Option<&TimelineInstruction<I>> {
|
||||
return self.instructions.get(self.instructions.len()-1)
|
||||
}
|
||||
pub fn append(&mut self,instruction:TimelineInstruction<I>){
|
||||
let i=self.instructions.len();
|
||||
self.instructions.push_back(instruction);
|
||||
for tracker in self.trackers.iter() {
|
||||
tracker.set_active(true);
|
||||
}
|
||||
}
|
||||
pub fn get_index_after_time(&mut self,time:crate::body::TIME) -> usize{
|
||||
self.instructions.partition_point(|ins|ins.instruction.time<time)
|
||||
}
|
||||
pub fn get_index_after_order(&mut self,order:ORDER) -> usize{
|
||||
self.instructions.partition_point(|ins|ins.order<order)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user