Compare commits

..

5 Commits

Author SHA1 Message Date
18825ecedb move collision plane up 2 units 2023-09-05 17:46:17 -07:00
3f4c3c4710 load roblox map with epic cube code 2023-09-05 17:46:17 -07:00
3c583e9181 load_roblox module 2023-09-05 17:46:17 -07:00
6b3a5d3ba2 add roblox map 2023-09-05 17:46:17 -07:00
1570d1547d add roblox deps 2023-09-05 17:46:04 -07:00
5 changed files with 1239 additions and 4151 deletions

File diff suppressed because it is too large Load Diff

@ -11,18 +11,23 @@ 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>> {
fn recursive_collect_objects(objects: &mut std::vec::Vec<rbx_dom_weak::types::Ref>,dom: &rbx_dom_weak::WeakDom, instance: &rbx_dom_weak::Instance, superclass: &str){
for &referent in instance.children() {
if let Some(c) = dom.get_by_ref(referent) {
if class_is_a(c.class.as_str(), superclass) {
objects.push(c.referent());//copy ref
}
recursive_collect_objects(objects,dom,c,superclass);
}
}
}
pub fn get_objects(buf_thing: std::io::BufReader<&[u8]>, superclass: &str) -> Result<(rbx_dom_weak::WeakDom,std::vec::Vec<rbx_dom_weak::types::Ref>), Box<dyn std::error::Error>> {
// Using buffered I/O is recommended with rbx_binary
let dom = rbx_binary::from_reader(buf_thing)?;
let mut objects = std::vec::Vec::<rbx_dom_weak::Instance>::new();
//move matching instances into objects
let (_,mut instances) = dom.into_raw();
for (_,instance) in instances.drain() {
if class_is_a(instance.class.as_str(), superclass) {
objects.push(instance);
}
}
let mut objects = std::vec::Vec::<rbx_dom_weak::types::Ref>::new();
recursive_collect_objects(&mut objects, &dom, dom.root(), superclass);
return Ok(objects)
return Ok((dom,objects))
}

@ -8,25 +8,22 @@ const IMAGE_SIZE: u32 = 128;
#[repr(C)]
struct Vertex {
pos: [f32; 3],
texture: [f32; 2],
normal: [f32; 3],
}
struct Entity {
index_count: u32,
index_buf: wgpu::Buffer,
vertex_count: u32,
vertex_buf: wgpu::Buffer,
}
//temp?
struct ModelData {
transform: glam::Mat4,
vertex_buf: wgpu::Buffer,
transform: glam::Affine3A,
entities: Vec<Entity>,
}
struct Model {
transform: glam::Mat4,
vertex_buf: wgpu::Buffer,
transform: glam::Affine3A,
entities: Vec<Entity>,
bind_group: wgpu::BindGroup,
model_buf: wgpu::Buffer,
@ -107,16 +104,16 @@ impl Camera {
}else{
self.fov/5.0
};
let proj = perspective_rh(fov, aspect, 0.5, 1000.0);
let proj = perspective_rh(fov, aspect, 1.0, 200.0);
let view = (glam::Mat4::from_translation(self.pos+self.offset) * glam::Mat4::from_euler(glam::EulerRot::YXZ, self.yaw, self.pitch, 0f32)).inverse();
let 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[32..48].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view)[..]);
raw[48..51].copy_from_slice(AsRef::<[f32; 3]>::as_ref(&self.pos));
raw[51] = 1.0;//cam_pos is vec4
raw
}
}
@ -159,57 +156,43 @@ impl Skybox {
}
}
fn get_transform_uniform_data(transform:&glam::Mat4) -> [f32; 4*4] {
fn get_transform_uniform_data(transform:&glam::Affine3A) -> [f32; 4*4] {
let mut raw = [0f32; 4*4];
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(transform)[..]);
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&glam::Mat4::from(*transform))[..]);
raw
}
fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec<ModelData>,source:&[u8]){
let data = obj::ObjData::load_buf(&source[..]).unwrap();
let mut vertices = Vec::new();
let mut vertex_index = std::collections::HashMap::<obj::IndexTuple,u16>::new();
for object in data.objects {
let mut entities = Vec::<Entity>::new();
for group in object.groups {
let mut indices = Vec::new();
vertices.clear();
for poly in group.polys {
for end_index in 2..poly.0.len() {
for &index in &[0, end_index - 1, end_index] {
let vert = poly.0[index];
if let Some(&i)=vertex_index.get(&vert){
indices.push(i);
}else{
let i=vertices.len() as u16;
let obj::IndexTuple(position_id, _texture_id, normal_id) =
poly.0[index];
vertices.push(Vertex {
pos: data.position[vert.0],
texture: data.texture[vert.1.unwrap()],
normal: data.normal[vert.2.unwrap()],
});
vertex_index.insert(vert,i);
indices.push(i);
pos: data.position[position_id],
normal: data.normal[normal_id.unwrap()],
})
}
}
}
}
let index_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Index"),
contents: bytemuck::cast_slice(&indices),
usage: wgpu::BufferUsages::INDEX,
});
entities.push(Entity {
index_buf,
index_count: indices.len() as u32,
});
}
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Vertex"),
contents: bytemuck::cast_slice(&vertices),
usage: wgpu::BufferUsages::VERTEX,
});
modeldatas.push(ModelData {
transform: glam::Mat4::default(),
entities.push(Entity {
vertex_count: vertices.len() as u32,
vertex_buf,
});
}
modeldatas.push(ModelData {
transform: glam::Affine3A::default(),
entities,
})
}
@ -231,15 +214,13 @@ impl strafe_client::framework::Example for Skybox {
let mut modeldatas = Vec::<ModelData>::new();
add_obj(device,& mut modeldatas,include_bytes!("../models/teslacyberv3.0.obj"));
add_obj(device,& mut modeldatas,include_bytes!("../models/suzanne.obj"));
add_obj(device,& mut modeldatas,include_bytes!("../models/teapot.obj"));
println!("models.len = {:?}", modeldatas.len());
modeldatas[1].transform=glam::Mat4::from_translation(glam::vec3(10.,5.,10.));
modeldatas[2].transform=glam::Mat4::from_translation(glam::vec3(-10.,5.,10.));
modeldatas[1].transform=glam::Affine3A::from_translation(glam::vec3(10.,5.,10.));
let input = std::io::BufReader::new(&include_bytes!("../maps/bhop_easyhop.rbxm")[..]);
match strafe_client::load_roblox::get_objects(input, "BasePart") {
Ok(objects)=>{
for object in objects.iter() {
Ok((dom,objects))=>{
for &referent in objects.iter() {
if let Some(object) = dom.get_by_ref(referent) {
if let (
Some(rbx_dom_weak::types::Variant::CFrame(cf)),
Some(rbx_dom_weak::types::Variant::Vector3(size)),
@ -254,6 +235,7 @@ impl strafe_client::framework::Example for Skybox {
continue;
}
//simply draw a box
let mut vertices = Vec::new();
let face_data = [
[0.0f32, 0., 1.],
[0.0f32, 0., -1.],
@ -262,12 +244,6 @@ impl strafe_client::framework::Example for Skybox {
[0.0f32, 1., 0.],
[0.0f32, -1., 0.],
];
let texture_data = [
[1.0f32, 0.],
[0.0f32, 0.],
[0.0f32, 1.],
[1.0f32, 1.],
];
let vertex_data = [
// top (0, 0, 1)
[-1.0f32, -1., 1.],
@ -300,61 +276,43 @@ impl strafe_client::framework::Example for Skybox {
[-1.0f32, -1., -1.],
[1.0f32, -1., -1.],
];
let mut indices = Vec::new();
let mut vertices = Vec::new();
let mut vertex_index = std::collections::HashMap::<usize,u16>::new();
for face in 0..6 {
for end_index in 2..4 {
for &index in &[0, end_index - 1, end_index] {
let vert = face*4+index;
let unique_id=(vert * 1<<0) + (index * 1<<8) + (face * 1<<16);
if let Some(&i)=vertex_index.get(&unique_id){
indices.push(i);
}else{
let i=vertices.len() as u16;
vertices.push(Vertex {
pos: vertex_data[vert],
texture: texture_data[index],
pos: vertex_data[face*4+index],
normal: face_data[face],
});
vertex_index.insert(unique_id,i);
indices.push(i);
})
}
}
}
}
let index_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Index"),
contents: bytemuck::cast_slice(&indices),
usage: wgpu::BufferUsages::INDEX,
});
let mut entities = Vec::<Entity>::new();
entities.push(Entity {
index_buf,
index_count: indices.len() as u32,
});
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Vertex"),
contents: bytemuck::cast_slice(&vertices),
usage: wgpu::BufferUsages::VERTEX,
});
let mut entities = Vec::<Entity>::new();
entities.push(Entity {
vertex_count: vertices.len() as u32,
vertex_buf,
});
modeldatas.push(ModelData {
transform: glam::Mat4::from_translation(
glam::Vec3::new(cf.position.x,cf.position.y-12f32,cf.position.z)
transform: glam::Affine3A::from_translation(
glam::Vec3::new(cf.position.x,cf.position.y-13f32,cf.position.z)
)
* glam::Mat4::from_mat3(
* glam::Affine3A::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::Vec3::new(cf.orientation.x.x,cf.orientation.x.y,cf.orientation.x.z),
glam::Vec3::new(cf.orientation.y.x,cf.orientation.y.y,cf.orientation.y.z),
glam::Vec3::new(cf.orientation.z.x,cf.orientation.z.y,cf.orientation.z.z),
),
)
* glam::Mat4::from_scale(
* glam::Affine3A::from_scale(
glam::Vec3::new(size.x,size.y,size.z)/2.0
),
vertex_buf,
entities,
});
})
}
}
}
},
@ -480,7 +438,7 @@ impl strafe_client::framework::Example for Skybox {
buffers: &[wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x2, 2 => Float32x3],
attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3],
}],
},
fragment: Some(wgpu::FragmentState {
@ -633,7 +591,7 @@ impl strafe_client::framework::Example for Skybox {
});
//drain the modeldata vec so entities can be /moved/ to models.entities
let mut models = Vec::<Model>::with_capacity(modeldatas.len());
let mut models = Vec::<Model>::new();
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 {
@ -654,10 +612,9 @@ impl strafe_client::framework::Example for Skybox {
//all of these are being moved here
models.push(Model{
transform: modeldata.transform,
vertex_buf:modeldata.vertex_buf,
entities: modeldata.entities,
bind_group: model_bind_group,
model_buf,
model_buf: model_buf,
})
}
@ -729,8 +686,8 @@ impl strafe_client::framework::Example for Skybox {
}
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;
self.camera.pitch=(self.camera.pitch as f64+delta.1/-512.) as f32;
self.camera.yaw=(self.camera.yaw as f64+delta.0/-512.) as f32;
}
fn resize(
@ -763,8 +720,8 @@ impl strafe_client::framework::Example for Skybox {
}
self.camera.vel+=self.camera.gravity*dt;
self.camera.pos+=self.camera.vel*dt;
if self.camera.pos.y<3.0{
self.camera.pos.y=3.0;
if self.camera.pos.y<2.0{
self.camera.pos.y=2.0;
self.camera.vel.y=0.0;
self.camera.grounded=true;
}
@ -797,7 +754,6 @@ impl strafe_client::framework::Example for Skybox {
device,
)
.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_transform_uniform_data(&model.transform);
self.staging_belt
@ -843,11 +799,10 @@ impl strafe_client::framework::Example for Skybox {
rpass.set_pipeline(&self.entity_pipeline);
for model in self.models.iter() {
rpass.set_bind_group(1, &model.bind_group, &[]);
rpass.set_vertex_buffer(0, model.vertex_buf.slice(..));
for entity in model.entities.iter() {
rpass.set_index_buffer(entity.index_buf.slice(..), wgpu::IndexFormat::Uint16);
rpass.draw_indexed(0..entity.index_count, 0, 0..1);
rpass.set_vertex_buffer(0, entity.vertex_buf.slice(..));
rpass.draw(0..entity.vertex_count, 0..1);
}
}

@ -63,8 +63,7 @@ fn vs_ground(@builtin(vertex_index) vertex_index: u32) -> GroundOutput {
struct EntityOutput {
@builtin(position) position: vec4<f32>,
@location(1) texture: vec2<f32>,
@location(2) normal: vec3<f32>,
@location(1) normal: vec3<f32>,
@location(3) view: vec3<f32>,
};
@ -75,13 +74,11 @@ var<uniform> r_EntityTransform: mat4x4<f32>;
@vertex
fn vs_entity(
@location(0) pos: vec3<f32>,
@location(1) texture: vec2<f32>,
@location(2) normal: vec3<f32>,
@location(1) normal: vec3<f32>,
) -> EntityOutput {
var position: vec4<f32> = r_EntityTransform * vec4<f32>(pos, 1.0);
var result: EntityOutput;
result.normal = (r_EntityTransform * vec4<f32>(normal, 0.0)).xyz;
result.texture=texture;
result.view = position.xyz - r_data.cam_pos.xyz;
result.position = r_data.proj * r_data.view * position;
return result;
@ -103,13 +100,10 @@ fn fs_sky(vertex: SkyOutput) -> @location(0) vec4<f32> {
fn fs_entity(vertex: EntityOutput) -> @location(0) vec4<f32> {
let incident = normalize(vertex.view);
let normal = normalize(vertex.normal);
let d = dot(normal, incident);
let reflected = incident - 2.0 * d * normal;
let reflected = incident - 2.0 * dot(normal, incident) * normal;
let dir = vec3<f32>(-1.0)+2.0*vec3<f32>(vertex.texture.x,0.0,vertex.texture.y);
let texture_color = textureSample(r_texture, r_sampler, dir).rgb;
let reflected_color = textureSample(r_texture, r_sampler, reflected).rgb;
return vec4<f32>(mix(vec3<f32>(0.1) + 0.5 * reflected_color,texture_color,1.0-pow(1.0-abs(d),2.0)), 1.0);
return vec4<f32>(vec3<f32>(0.1) + 0.5 * reflected_color, 1.0);
}
fn modulo_euclidean (a: f32, b: f32) -> f32 {