Compare commits

..

14 Commits

5 changed files with 122 additions and 98 deletions

Binary file not shown.

@ -254,8 +254,7 @@ pub struct PhysicsState {
pub hitbox_halfsize: glam::Vec3,
pub contacts: std::collections::HashSet::<RelativeCollision>,
//pub intersections: Vec<ModelId>,
//temp
pub models_cringe_clone: Vec<Model>,
pub models: Vec<ModelPhysics>,
//camera must exist in state because wormholes modify the camera, also camera punch
pub camera: Camera,
pub mouse_interpolation: MouseInterpolationState,
@ -382,13 +381,13 @@ impl Aabb {
type TreyMeshFace = AabbFace;
type TreyMesh = Aabb;
pub struct Model {
pub struct ModelPhysics {
//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 {
impl ModelPhysics {
pub fn new(transform:glam::Mat4) -> Self {
Self{transform}
}
@ -432,10 +431,10 @@ pub struct RelativeCollision {
}
impl RelativeCollision {
pub fn mesh(&self,models:&Vec<Model>) -> TreyMesh {
pub fn mesh(&self,models:&Vec<ModelPhysics>) -> TreyMesh {
return models.get(self.model as usize).unwrap().face_mesh(self.face)
}
pub fn normal(&self,models:&Vec<Model>) -> glam::Vec3 {
pub fn normal(&self,models:&Vec<ModelPhysics>) -> glam::Vec3 {
return models.get(self.model as usize).unwrap().face_normal(self.face)
}
}
@ -494,7 +493,7 @@ impl PhysicsState {
fn contact_constrain_velocity(&self,velocity:&mut glam::Vec3){
for contact in self.contacts.iter() {
let n=contact.normal(&self.models_cringe_clone);
let n=contact.normal(&self.models);
let d=velocity.dot(n);
if d<0f32{
(*velocity)-=d/n.length_squared()*n;
@ -503,7 +502,7 @@ impl PhysicsState {
}
fn contact_constrain_acceleration(&self,acceleration:&mut glam::Vec3){
for contact in self.contacts.iter() {
let n=contact.normal(&self.models_cringe_clone);
let n=contact.normal(&self.models);
let d=acceleration.dot(n);
if d<0f32{
(*acceleration)-=d/n.length_squared()*n;
@ -603,7 +602,7 @@ impl PhysicsState {
let mut best_time=time_limit;
let mut exit_face:Option<TreyMeshFace>=None;
let mesh0=self.mesh();
let mesh1=self.models_cringe_clone.get(collision_data.model as usize).unwrap().mesh();
let mesh1=self.models.get(collision_data.model as usize).unwrap().mesh();
let (v,a)=(-self.body.velocity,self.body.acceleration);
//collect x
match collision_data.face {
@ -754,7 +753,7 @@ impl PhysicsState {
let mut best_time=time_limit;
let mut best_face:Option<TreyMeshFace>=None;
let mesh0=self.mesh();
let mesh1=self.models_cringe_clone.get(model_id as usize).unwrap().mesh();
let mesh1=self.models.get(model_id as usize).unwrap().mesh();
let (p,v,a)=(self.body.position,self.body.velocity,self.body.acceleration);
//collect x
for t in zeroes2(mesh0.max.x-mesh1.min.x,v.x,0.5*a.x) {
@ -879,7 +878,7 @@ impl crate::instruction::InstructionEmitter<PhysicsInstruction> for PhysicsState
collector.collect(self.predict_collision_end(self.time,time_limit,collision_data));
}
//check for collision start instructions (against every part in the game with no optimization!!)
for i in 0..self.models_cringe_clone.len() {
for i in 0..self.models.len() {
collector.collect(self.predict_collision_start(self.time,time_limit,i as u32));
}
if self.grounded {
@ -1018,4 +1017,4 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
},
}
}
}
}

@ -51,7 +51,7 @@ pub trait Example: 'static + Sized {
device: &wgpu::Device,
queue: &wgpu::Queue,
);
fn update(&mut self, event: WindowEvent);
fn update(&mut self, device: &wgpu::Device, event: WindowEvent);
fn device_event(&mut self, event: DeviceEvent);
fn render(
&mut self,
@ -344,7 +344,7 @@ fn start<E: Example>(
println!("{:#?}", instance.generate_report());
}
_ => {
example.update(event);
example.update(&device,event);
}
},
event::Event::DeviceEvent {

@ -10,8 +10,7 @@ fn class_is_a(class: &str, superclass: &str) -> bool {
}
return false
}
pub fn get_objects(buf_thing: std::io::BufReader<&[u8]>, superclass: &str) -> Result<std::vec::Vec<rbx_dom_weak::Instance>, Box<dyn std::error::Error>> {
pub fn get_objects<R: std::io::Read>(buf_thing: R, superclass: &str) -> Result<std::vec::Vec<rbx_dom_weak::Instance>, Box<dyn std::error::Error>> {
// Using buffered I/O is recommended with rbx_binary
let dom = rbx_binary::from_reader(buf_thing)?;

@ -31,6 +31,15 @@ struct ModelGraphics {
bind_group: wgpu::BindGroup,
model_buf: wgpu::Buffer,
}
pub struct GraphicsSamplers{
repeat: wgpu::Sampler,
}
pub struct GraphicsBindGroupLayouts{
model: wgpu::BindGroupLayout,
}
pub struct GraphicsBindGroups {
camera: wgpu::BindGroup,
skybox_texture: wgpu::BindGroup,
@ -47,6 +56,9 @@ pub struct GraphicsData {
physics: strafe_client::body::PhysicsState,
pipelines: GraphicsPipelines,
bind_groups: GraphicsBindGroups,
bind_group_layouts: GraphicsBindGroupLayouts,
samplers: GraphicsSamplers,
temp_squid_texture_view: wgpu::TextureView,
camera_buf: wgpu::Buffer,
models: Vec<ModelGraphics>,
depth_view: wgpu::TextureView,
@ -79,8 +91,8 @@ impl GraphicsData {
depth_texture.create_view(&wgpu::TextureViewDescriptor::default())
}
fn generate_modeldatas_roblox(&self,input:std::io::BufReader<&[u8]>) -> Vec<ModelData>{
let mut modeldata=generate_modeldatas(self.handy_unit_cube.clone())[0];
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() {
@ -88,18 +100,16 @@ 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::Color3(color3)),
) = (
object.properties.get("CFrame"),
object.properties.get("Size"),
object.properties.get("Transparency"),
object.properties.get("Color"),
)
{
if *transparency==1.0 {
continue;
}
modeldata.transforms.push(
modeldatas[0].transforms.push(
glam::Mat4::from_translation(
glam::Vec3::new(cf.position.x,cf.position.y,cf.position.z)
)
@ -119,10 +129,60 @@ impl GraphicsData {
},
Err(e) => println!("lmao err {:?}", e),
}
vec![modeldata]
modeldatas
}
fn generate_model_graphics(&mut self,modeldatas:Vec<ModelData>){
//
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_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::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let model_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &self.bind_group_layouts.model,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: model_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&self.temp_squid_texture_view),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::Sampler(&self.samplers.repeat),
},
],
label: Some(format!("ModelGraphics{}",i).as_str()),
});
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Vertex"),
contents: bytemuck::cast_slice(&modeldata.vertices),
usage: wgpu::BufferUsages::VERTEX,
});
//all of these are being moved here
self.models.push(ModelGraphics{
transforms:modeldata.transforms,
vertex_buf,
entities: modeldata.entities.iter().map(|indices|{
let index_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Index"),
contents: bytemuck::cast_slice(&indices),
usage: wgpu::BufferUsages::INDEX,
});
Entity {
index_buf,
index_count: indices.len() as u32,
}
}).collect(),
bind_group: model_bind_group,
model_buf,
})
}
}
}
@ -136,11 +196,13 @@ fn get_transform_uniform_data(transforms:&Vec<glam::Mat4>) -> Vec<f32> {
raw
}
fn generate_modeldatas<'a>(data:obj::ObjData) -> &'a mut 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();
for object in data.objects {
vertices.clear();
vertex_index.clear();
let mut entities = Vec::new();
for group in object.groups {
let mut indices = Vec::new();
@ -167,11 +229,11 @@ fn generate_modeldatas<'a>(data:obj::ObjData) -> &'a mut Vec<ModelData>{
}
modeldatas.push(ModelData {
transforms: vec![],
vertices,
vertices:vertices.clone(),
entities,
});
}
&mut modeldatas
modeldatas
}
@ -277,21 +339,21 @@ impl strafe_client::framework::Example for GraphicsData {
material_libs: Vec::new(),
};
let mut modeldatas = Vec::<ModelData>::new();
modeldatas.append(generate_modeldatas(obj::ObjData::load_buf(&include_bytes!("../models/teslacyberv3.0.obj")[..]).unwrap()));
modeldatas.append(generate_modeldatas(obj::ObjData::load_buf(&include_bytes!("../models/suzanne.obj")[..]).unwrap()));
modeldatas.append(generate_modeldatas(obj::ObjData::load_buf(&include_bytes!("../models/teapot.obj")[..]).unwrap()));
modeldatas.append(generate_modeldatas(unit_cube.clone()));
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].transforms[0]=glam::Mat4::from_translation(glam::vec3(10.,0.,-10.));
modeldatas[0].transforms.push(glam::Mat4::from_translation(glam::vec3(10.,0.,-10.)));
//quad monkeys
modeldatas[1].transforms[0]=glam::Mat4::from_translation(glam::vec3(10.,5.,10.));
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].transforms[0]=glam::Mat4::from_translation(glam::vec3(-10.,5.,10.));
modeldatas[2].transforms.push(glam::Mat4::from_translation(glam::vec3(-10.,5.,10.)));
//ground
modeldatas[3].transforms[0]=glam::Mat4::from_translation(glam::vec3(0.,0.,0.))*glam::Mat4::from_scale(glam::vec3(160.0, 1.0, 160.0));
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,
@ -401,7 +463,13 @@ impl strafe_client::framework::Example for GraphicsData {
grounded: false,
walkspeed: 18.0,
contacts: std::collections::HashSet::new(),
models_cringe_clone: modeldatas.iter().map(|m|m.transforms.iter().map(|t|strafe_client::body::Model::new(*t))).flatten().collect(),
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)),
@ -525,58 +593,6 @@ impl strafe_client::framework::Example for GraphicsData {
})
};
//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.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::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(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&squid_texture_view),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::Sampler(&repeat_sampler),
},
],
label: Some(format!("ModelGraphics{}",i).as_str()),
});
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Vertex"),
contents: bytemuck::cast_slice(&modeldata.vertices),
usage: wgpu::BufferUsages::VERTEX,
});
//all of these are being moved here
models.push(ModelGraphics{
transforms:modeldata.transforms,
vertex_buf,
entities: modeldata.entities.iter().map(|indices|{
let index_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Index"),
contents: bytemuck::cast_slice(&indices),
usage: wgpu::BufferUsages::INDEX,
});
Entity {
index_buf,
index_count: indices.len() as u32,
}
}).collect(),
bind_group: model_bind_group,
model_buf,
})
}
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[
@ -680,7 +696,7 @@ impl strafe_client::framework::Example for GraphicsData {
let depth_view = Self::create_depth_texture(config, device);
GraphicsData {
let mut graphics=GraphicsData {
handy_unit_cube:unit_cube,
start_time: Instant::now(),
screen_size: (config.width,config.height),
@ -694,23 +710,33 @@ impl strafe_client::framework::Example for GraphicsData {
skybox_texture:skybox_texture_bind_group,
},
camera_buf,
models,
models: Vec::new(),
depth_view,
staging_belt: wgpu::util::StagingBelt::new(0x100),
}
bind_group_layouts: GraphicsBindGroupLayouts { model: model_bind_group_layout },
samplers: GraphicsSamplers { repeat: repeat_sampler },
temp_squid_texture_view: squid_texture_view,
};
graphics.generate_model_graphics(&device,modeldatas);
return graphics;
}
#[allow(clippy::single_match)]
fn update(&mut self, event: winit::event::WindowEvent) {
//nothing atm
fn update(&mut self, device: &wgpu::Device, event: winit::event::WindowEvent) {
match event {
winit::event::WindowEvent::DroppedFile(path) => {
println!("opening file: {:?}", &path);
//oh boy! let's load the map!
let file=std::fs::File::open(path);
let input = std::io::BufReader::new(file);
let modeldatas=self.generate_modeldatas_roblox(input);
self.generate_model_graphics(modeldatas);
//also physics
if let Ok(file)=std::fs::File::open(path){
let input = std::io::BufReader::new(file);
let modeldatas=self.generate_modeldatas_roblox(input);
self.generate_model_graphics(device,modeldatas);
//also physics
}else{
println!("Could not open file");
}
},
_=>(),
}