Compare commits

..

6 Commits

Author SHA1 Message Date
c8e4a5d2aa do not step physics on mouse input, only update pos (overwriting previous pos) 2023-09-21 00:42:09 -07:00
005d0fdd58 fix angles 2023-09-21 00:42:09 -07:00
c28b5177f8 accumulate deltas 2023-09-21 00:42:09 -07:00
8a3918deb9 implement jump() + remove jump_trying + prevent air jumping 2023-09-21 00:42:09 -07:00
6c99e493c6 wip 2 2023-09-21 00:42:09 -07:00
b727736152 wip 2023-09-21 00:25:41 -07:00
5 changed files with 75 additions and 153 deletions

2
Cargo.lock generated

@ -1645,7 +1645,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "strafe-client"
version = "0.5.0"
version = "0.4.0"
dependencies = [
"async-executor",
"bytemuck",

@ -1,6 +1,6 @@
[package]
name = "strafe-client"
version = "0.5.0"
version = "0.4.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

@ -12,8 +12,6 @@ pub enum PhysicsInstruction {
// bool,//true = Trigger; false = teleport
// bool,//true = Force
// )
//temp
SetPosition(glam::Vec3),
//Both of these conditionally activate RefreshWalkTarget (by doing what SetWalkTargetVelocity used to do and then flagging it)
Input(InputInstruction),
}
@ -904,7 +902,6 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
//selectively update body
match &ins.instruction {
PhysicsInstruction::Input(InputInstruction::MoveMouse(_)) => (),//dodge time for mouse movement
PhysicsInstruction::SetPosition(_) => self.time=ins.time,//TODO: queue instructions
PhysicsInstruction::Input(_)
|PhysicsInstruction::ReachWalkTargetVelocity
|PhysicsInstruction::CollisionStart(_)
@ -912,15 +909,6 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
|PhysicsInstruction::StrafeTick => self.advance_time(ins.time),
}
match ins.instruction {
PhysicsInstruction::SetPosition(position)=>{
//temp
self.body.position=position;
//manual clear //for c in self.contacts{process_instruction(CollisionEnd(c))}
self.contacts.clear();
self.body.acceleration=self.gravity;
self.walk.state=WalkEnum::Reached;
self.grounded=false;
}
PhysicsInstruction::CollisionStart(c) => {
//check ground
match &c.face {

@ -11,7 +11,6 @@ struct Vertex {
pos: [f32; 3],
texture: [f32; 2],
normal: [f32; 3],
color: [f32; 4],
}
struct Entity {
@ -19,24 +18,14 @@ struct Entity {
index_buf: wgpu::Buffer,
}
struct ModelInstance {
transform: glam::Mat4,
color: glam::Vec4,
}
struct ModelData {
instances: Vec<ModelInstance>,
transforms: Vec<glam::Mat4>,
vertices: Vec<Vertex>,
entities: Vec<Vec<u16>>,
}
impl ModelData {
const COLOR_FLOATS_WHITE: [f32;4] = [1.0,1.0,1.0,1.0];
const COLOR_VEC4_WHITE: glam::Vec4 = glam::vec4(1.0,1.0,1.0,1.0);
}
struct ModelGraphics {
instances: Vec<ModelInstance>,
transforms: Vec<glam::Mat4>,
vertex_buf: wgpu::Buffer,
entities: Vec<Entity>,
bind_group: wgpu::BindGroup,
@ -102,9 +91,8 @@ impl GraphicsData {
depth_texture.create_view(&wgpu::TextureViewDescriptor::default())
}
fn generate_modeldatas_roblox<R: std::io::Read>(&self,input:R) -> (Vec<ModelData>,glam::Vec3){
let mut spawn_point=glam::Vec3::ZERO;
let mut modeldatas=generate_modeldatas(self.handy_unit_cube.clone(),ModelData::COLOR_FLOATS_WHITE);
fn generate_modeldatas_roblox<R: std::io::Read>(&self,input:R) -> Vec<ModelData>{
let mut modeldatas=generate_modeldatas(self.handy_unit_cube.clone());
match strafe_client::load_roblox::get_objects(input, "BasePart") {
Ok(objects)=>{
for object in objects.iter() {
@ -112,62 +100,46 @@ impl GraphicsData {
Some(rbx_dom_weak::types::Variant::CFrame(cf)),
Some(rbx_dom_weak::types::Variant::Vector3(size)),
Some(rbx_dom_weak::types::Variant::Float32(transparency)),
Some(rbx_dom_weak::types::Variant::Color3uint8(color3)),
Some(rbx_dom_weak::types::Variant::Enum(shape)),
) = (
object.properties.get("CFrame"),
object.properties.get("Size"),
object.properties.get("Transparency"),
object.properties.get("Color"),
object.properties.get("Shape"),//this will also skip unions
)
{
let transform=glam::Mat4::from_translation(
glam::Vec3::new(cf.position.x,cf.position.y,cf.position.z)
)
* glam::Mat4::from_mat3(
glam::Mat3::from_cols(
glam::Vec3::new(cf.orientation.x.x,cf.orientation.y.x,cf.orientation.z.x),
glam::Vec3::new(cf.orientation.x.y,cf.orientation.y.y,cf.orientation.z.y),
glam::Vec3::new(cf.orientation.x.z,cf.orientation.y.z,cf.orientation.z.z),
),
)
* glam::Mat4::from_scale(
glam::Vec3::new(size.x,size.y,size.z)/2.0
);
if object.name=="MapStart"{
spawn_point=glam::Vec4Swizzles::xyz(transform*glam::Vec3::Y.extend(1.0))+glam::vec3(0.0,2.5,0.0);
println!("Found MapStart{:?}",spawn_point);
}
if *transparency==1.0||shape.to_u32()!=1 {
if *transparency==1.0 {
continue;
}
modeldatas[0].instances.push(ModelInstance {
transform,
color: glam::vec4(color3.r as f32/255f32, color3.g as f32/255f32, color3.b as f32/255f32, 1.0-*transparency),
})
modeldatas[0].transforms.push(
glam::Mat4::from_translation(
glam::Vec3::new(cf.position.x,cf.position.y,cf.position.z)
)
* glam::Mat4::from_mat3(
glam::Mat3::from_cols(
glam::Vec3::new(cf.orientation.x.x,cf.orientation.y.x,cf.orientation.z.x),
glam::Vec3::new(cf.orientation.x.y,cf.orientation.y.y,cf.orientation.z.y),
glam::Vec3::new(cf.orientation.x.z,cf.orientation.y.z,cf.orientation.z.z),
),
)
* glam::Mat4::from_scale(
glam::Vec3::new(size.x,size.y,size.z)/2.0
)
)
}
}
},
Err(e) => println!("lmao err {:?}", e),
}
(modeldatas,spawn_point)
}
fn generate_model_physics(&mut self,modeldatas:&Vec<ModelData>){
self.physics.models.append(&mut modeldatas.iter().map(|m|
//make aabb and run vertices to get realistic bounds
m.instances.iter().map(|t|strafe_client::body::ModelPhysics::new(t.transform))
).flatten().collect());
modeldatas
}
fn generate_model_graphics(&mut self,device:&wgpu::Device,mut modeldatas:Vec<ModelData>){
//drain the modeldata vec so entities can be /moved/ to models.entities
self.models.reserve(modeldatas.len());
for (i,modeldata) in modeldatas.drain(..).enumerate() {
let model_uniforms = get_instances_buffer_data(&modeldata.instances);
let model_uniforms = get_transform_uniform_data(&modeldata.transforms);
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::STORAGE | wgpu::BufferUsages::COPY_DST,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let model_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &self.bind_group_layouts.model,
@ -194,7 +166,7 @@ impl GraphicsData {
});
//all of these are being moved here
self.models.push(ModelGraphics{
instances:modeldata.instances,
transforms:modeldata.transforms,
vertex_buf,
entities: modeldata.entities.iter().map(|indices|{
let index_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
@ -214,19 +186,17 @@ impl GraphicsData {
}
}
fn get_instances_buffer_data(instances:&Vec<ModelInstance>) -> Vec<f32> {
const SIZE: usize=4*4+4;//let size=std::mem::size_of::<ModelInstance>();
let mut raw = Vec::with_capacity(SIZE*instances.len());
for (i,mi) in instances.iter().enumerate(){
let mut v = raw.split_off(SIZE*i);
raw.extend_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&mi.transform)[..]);
raw.extend_from_slice(AsRef::<[f32; 4]>::as_ref(&mi.color));
fn get_transform_uniform_data(transforms:&Vec<glam::Mat4>) -> Vec<f32> {
let mut raw = Vec::with_capacity(4*4*transforms.len());
for (i,t) in transforms.iter().enumerate(){
let mut v = raw.split_off(4*4*i);
raw.extend_from_slice(&AsRef::<[f32; 4*4]>::as_ref(t)[..]);
raw.append(&mut v);
}
raw
}
fn generate_modeldatas(data:obj::ObjData,color:[f32;4]) -> Vec<ModelData>{
fn generate_modeldatas(data:obj::ObjData) -> Vec<ModelData>{
let mut modeldatas=Vec::new();
let mut vertices = Vec::new();
let mut vertex_index = std::collections::HashMap::<obj::IndexTuple,u16>::new();
@ -248,7 +218,6 @@ fn generate_modeldatas(data:obj::ObjData,color:[f32;4]) -> Vec<ModelData>{
pos: data.position[vert.0],
texture: data.texture[vert.1.unwrap()],
normal: data.normal[vert.2.unwrap()],
color,
});
vertex_index.insert(vert,i);
indices.push(i);
@ -259,7 +228,7 @@ fn generate_modeldatas(data:obj::ObjData,color:[f32;4]) -> Vec<ModelData>{
entities.push(indices);
}
modeldatas.push(ModelData {
instances: Vec::new(),
transforms: vec![],
vertices:vertices.clone(),
entities,
});
@ -288,12 +257,7 @@ impl strafe_client::framework::Example for GraphicsData {
| wgpu::Features::TEXTURE_COMPRESSION_ETC2
| wgpu::Features::TEXTURE_COMPRESSION_BC
}
fn required_features() -> wgpu::Features {
wgpu::Features::STORAGE_RESOURCE_BINDING_ARRAY
}
fn required_limits() -> wgpu::Limits {
wgpu::Limits::default() //framework.rs was using goofy limits that caused me a multi-day headache
}
fn init(
config: &wgpu::SurfaceConfiguration,
_adapter: &wgpu::Adapter,
@ -375,42 +339,21 @@ impl strafe_client::framework::Example for GraphicsData {
material_libs: Vec::new(),
};
let mut modeldatas = Vec::<ModelData>::new();
modeldatas.append(&mut generate_modeldatas(obj::ObjData::load_buf(&include_bytes!("../models/teslacyberv3.0.obj")[..]).unwrap(),ModelData::COLOR_FLOATS_WHITE));
modeldatas.append(&mut generate_modeldatas(obj::ObjData::load_buf(&include_bytes!("../models/suzanne.obj")[..]).unwrap(),ModelData::COLOR_FLOATS_WHITE));
modeldatas.append(&mut generate_modeldatas(obj::ObjData::load_buf(&include_bytes!("../models/teapot.obj")[..]).unwrap(),ModelData::COLOR_FLOATS_WHITE));
modeldatas.append(&mut generate_modeldatas(unit_cube.clone(),ModelData::COLOR_FLOATS_WHITE));
modeldatas.append(&mut generate_modeldatas(obj::ObjData::load_buf(&include_bytes!("../models/teslacyberv3.0.obj")[..]).unwrap()));
modeldatas.append(&mut generate_modeldatas(obj::ObjData::load_buf(&include_bytes!("../models/suzanne.obj")[..]).unwrap()));
modeldatas.append(&mut generate_modeldatas(obj::ObjData::load_buf(&include_bytes!("../models/teapot.obj")[..]).unwrap()));
modeldatas.append(&mut generate_modeldatas(unit_cube.clone()));
println!("models.len = {:?}", modeldatas.len());
modeldatas[0].instances.push(ModelInstance{
transform:glam::Mat4::from_translation(glam::vec3(10.,0.,-10.)),
color:ModelData::COLOR_VEC4_WHITE,
});
modeldatas[0].transforms.push(glam::Mat4::from_translation(glam::vec3(10.,0.,-10.)));
//quad monkeys
modeldatas[1].instances.push(ModelInstance{
transform:glam::Mat4::from_translation(glam::vec3(10.,5.,10.)),
color:ModelData::COLOR_VEC4_WHITE,
});
modeldatas[1].instances.push(ModelInstance{
transform:glam::Mat4::from_translation(glam::vec3(20.,5.,10.)),
color:glam::vec4(1.0,0.0,0.0,1.0),
});
modeldatas[1].instances.push(ModelInstance{
transform:glam::Mat4::from_translation(glam::vec3(10.,5.,20.)),
color:glam::vec4(0.0,1.0,0.0,1.0),
});
modeldatas[1].instances.push(ModelInstance{
transform:glam::Mat4::from_translation(glam::vec3(20.,5.,20.)),
color:glam::vec4(0.0,0.0,1.0,1.0),
});
modeldatas[1].transforms.push(glam::Mat4::from_translation(glam::vec3(10.,5.,10.)));
modeldatas[1].transforms.push(glam::Mat4::from_translation(glam::vec3(20.,5.,10.)));
modeldatas[1].transforms.push(glam::Mat4::from_translation(glam::vec3(10.,5.,20.)));
modeldatas[1].transforms.push(glam::Mat4::from_translation(glam::vec3(20.,5.,20.)));
//teapot
modeldatas[2].instances.push(ModelInstance{
transform:glam::Mat4::from_translation(glam::vec3(-10.,5.,10.)),
color:ModelData::COLOR_VEC4_WHITE,
});
modeldatas[2].transforms.push(glam::Mat4::from_translation(glam::vec3(-10.,5.,10.)));
//ground
modeldatas[3].instances.push(ModelInstance{
transform:glam::Mat4::from_translation(glam::vec3(0.,0.,0.))*glam::Mat4::from_scale(glam::vec3(160.0, 1.0, 160.0)),
color:ModelData::COLOR_VEC4_WHITE,
});
modeldatas[3].transforms.push(glam::Mat4::from_translation(glam::vec3(0.,0.,0.))*glam::Mat4::from_scale(glam::vec3(160.0, 1.0, 160.0)));
let camera_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
@ -455,7 +398,7 @@ impl strafe_client::framework::Example for GraphicsData {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
@ -520,7 +463,13 @@ impl strafe_client::framework::Example for GraphicsData {
grounded: false,
walkspeed: 18.0,
contacts: std::collections::HashSet::new(),
models: Vec::new(),
models: modeldatas.iter().map(|m|
//make aabb and run vertices to get realistic bounds
//this needs to be a function generate_model_physics
m.transforms.iter().map(|t|
strafe_client::body::ModelPhysics::new(*t)
)
).flatten().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)),
@ -606,13 +555,9 @@ impl strafe_client::framework::Example for GraphicsData {
//squid
let squid_texture_view={
let bytes = &include_bytes!("../images/squid.dds")[..];
let image = ddsfile::Dds::read(&mut std::io::Cursor::new(&bytes)).unwrap();
let size = wgpu::Extent3d {
width: image.get_width(),
height: image.get_height(),
width: 1076,
height: 1076,
depth_or_array_layers: 1,
};
@ -622,6 +567,10 @@ impl strafe_client::framework::Example for GraphicsData {
};
let max_mips = layer_size.max_mips(wgpu::TextureDimension::D2);
let bytes = &include_bytes!("../images/squid.dds")[..];
let image = ddsfile::Dds::read(&mut std::io::Cursor::new(&bytes)).unwrap();
let texture = device.create_texture_with_data(
queue,
&wgpu::TextureDescriptor {
@ -691,7 +640,7 @@ impl strafe_client::framework::Example for GraphicsData {
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 => Float32x2, 2 => Float32x3, 3 => Float32x4],
attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x2, 2 => Float32x3],
}],
},
fragment: Some(wgpu::FragmentState {
@ -769,7 +718,6 @@ impl strafe_client::framework::Example for GraphicsData {
temp_squid_texture_view: squid_texture_view,
};
graphics.generate_model_physics(&modeldatas);
graphics.generate_model_graphics(&device,modeldatas);
return graphics;
@ -783,18 +731,9 @@ impl strafe_client::framework::Example for GraphicsData {
//oh boy! let's load the map!
if let Ok(file)=std::fs::File::open(path){
let input = std::io::BufReader::new(file);
let (modeldatas,spawn_point)=self.generate_modeldatas_roblox(input);
//if generate_modeldatas succeeds, clear the previous ones
self.models.clear();
self.physics.models.clear();
self.generate_model_physics(&modeldatas);
let modeldatas=self.generate_modeldatas_roblox(input);
self.generate_model_graphics(device,modeldatas);
//manual reset
let time=self.physics.time;
strafe_client::instruction::InstructionConsumer::process_instruction(&mut self.physics, strafe_client::instruction::TimedInstruction{
time,
instruction: strafe_client::body::PhysicsInstruction::SetPosition(spawn_point),
})
//also physics
}else{
println!("Could not open file");
}
@ -901,7 +840,7 @@ impl strafe_client::framework::Example for GraphicsData {
.copy_from_slice(bytemuck::cast_slice(&camera_uniforms));
//This code only needs to run when the uniforms change
for model in self.models.iter() {
let model_uniforms = get_instances_buffer_data(&model.instances);
let model_uniforms = get_transform_uniform_data(&model.transforms);
self.staging_belt
.write_buffer(
&mut encoder,
@ -950,7 +889,7 @@ impl strafe_client::framework::Example for GraphicsData {
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..model.instances.len() as u32);
rpass.draw_indexed(0..entity.index_count, 0, 0..model.transforms.len() as u32);
}
}

@ -41,17 +41,15 @@ fn vs_sky(@builtin(vertex_index) vertex_index: u32) -> SkyOutput {
return result;
}
struct ModelInstance{
transform:mat4x4<f32>,
//texture_transform:mat3x3<f32>,
color:vec4<f32>,
}
//my fancy idea is to create a megatexture for each model that includes all the textures each intance will need
//the texture transform then maps the texture coordinates to the location of the specific texture
const MAX_ENTITY_INSTANCES=1024;
//group 1 is the model
@group(1)
@binding(0)
var<storage> model_instances: array<ModelInstance>;
var<uniform> entity_transforms: array<mat4x4<f32>,MAX_ENTITY_INSTANCES>;
//var<uniform> entity_texture_transforms: array<mat3x3<f32>,MAX_ENTITY_INSTANCES>;
//my fancy idea is to create a megatexture for each model that includes all the textures each intance will need
//the texture transform then maps the texture coordinates to the location of the specific texture
//how to do no texture?
@group(1)
@binding(1)
var model_texture: texture_2d<f32>;
@ -64,7 +62,6 @@ struct EntityOutputTexture {
@location(1) texture: vec2<f32>,
@location(2) normal: vec3<f32>,
@location(3) view: vec3<f32>,
@location(4) color: vec4<f32>,
};
@vertex
fn vs_entity_texture(
@ -72,13 +69,11 @@ fn vs_entity_texture(
@location(0) pos: vec3<f32>,
@location(1) texture: vec2<f32>,
@location(2) normal: vec3<f32>,
@location(3) color: vec4<f32>,
) -> EntityOutputTexture {
var position: vec4<f32> = model_instances[instance].transform * vec4<f32>(pos, 1.0);
var position: vec4<f32> = entity_transforms[instance] * vec4<f32>(pos, 1.0);
var result: EntityOutputTexture;
result.normal = (model_instances[instance].transform * vec4<f32>(normal, 0.0)).xyz;
result.texture=texture;//(model_instances[instance].texture_transform * vec3<f32>(texture, 1.0)).xy;
result.color=model_instances[instance].color * color;
result.normal = (entity_transforms[instance] * vec4<f32>(normal, 0.0)).xyz;
result.texture=texture;//(entity_texture_transforms[instance] * vec3<f32>(texture, 1.0)).xy;
result.view = position.xyz - camera.cam_pos.xyz;
result.position = camera.proj * camera.view * position;
return result;
@ -104,7 +99,7 @@ fn fs_entity_texture(vertex: EntityOutputTexture) -> @location(0) vec4<f32> {
let d = dot(normal, incident);
let reflected = incident - 2.0 * d * normal;
let fragment_color = textureSample(model_texture, model_sampler, vertex.texture)*vertex.color;
let fragment_color = textureSample(model_texture, model_sampler, vertex.texture).rgb;
let reflected_color = textureSample(cube_texture, cube_sampler, reflected).rgb;
return mix(vec4<f32>(vec3<f32>(0.1) + 0.5 * reflected_color,1.0),fragment_color,1.0-pow(1.0-abs(d),2.0));
return vec4<f32>(mix(vec3<f32>(0.1) + 0.5 * reflected_color,fragment_color,1.0-pow(1.0-abs(d),2.0)), 1.0);
}