Compare commits

...

29 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
abd002e3bf optimize release builds a bit 2023-09-05 15:39:04 -07:00
1776dc4c39 add comments about moving data 2023-09-05 15:39:04 -07:00
946687639a v0.2.0 2023-09-05 15:39:04 -07:00
69c7896011 binding group per model 2023-09-05 14:58:55 -07:00
1a1509f315 try one entity, convert to mat4x4 2023-09-05 14:58:55 -07:00
87f674238d add model_bind_group 2023-09-05 14:58:55 -07:00
cce9800045 no fragment 2023-09-05 14:58:55 -07:00
8792b8e782 add entity CFrames 2023-09-05 14:58:55 -07:00
c9cb22be68 comment on value 2023-09-05 14:58:55 -07:00
d4172f5040 rename uniform_buf to camera_buf 2023-09-05 14:58:55 -07:00
df43dc6f6e add suzanne 2023-09-05 14:58:55 -07:00
c3290e2ce6 macro version into window title 2023-09-05 12:23:26 -07:00
e347d4fd51 vertex index buffer is very cool 2023-09-05 00:18:31 -07:00
ad46418e79 expand baseplate 2023-09-05 00:18:07 -07:00
82da9bf40f why not be accurate 2023-09-01 20:28:37 -07:00
bda898a1f1 struct style init straggler 2023-09-01 20:27:28 -07:00
ce5f6fceb9 modulo is implemented incorrectly by the shader language 2023-09-01 00:10:19 -07:00
7179f20e67 add zoom 2023-08-31 22:48:17 -07:00
e25a2d7a7a use slope fov 2023-08-31 22:47:55 -07:00
7c247b6949 fix strafe bug 2023-08-31 12:37:53 -07:00
49fde16c4f rbhop friction 2023-08-31 12:16:51 -07:00
403910d2d8 rbhop jump power 2023-08-31 12:10:49 -07:00
7a23ef2734 add walking 2023-08-31 12:04:18 -07:00
1e25f71618 brain lol 2023-08-31 02:21:39 -07:00
8 changed files with 3193 additions and 78 deletions

257
Cargo.lock generated

@ -157,6 +157,12 @@ dependencies = [
"rustc-demangle",
]
[[package]]
name = "base64"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "bit-set"
version = "0.5.3"
@ -184,12 +190,35 @@ version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635"
[[package]]
name = "blake3"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "199c42ab6972d92c9f8995f086273d25c42fc0f7b2a1fcefba465c1352d25ba5"
dependencies = [
"arrayref",
"arrayvec",
"cc",
"cfg-if",
"constant_time_eq",
"digest",
]
[[package]]
name = "block"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "block-sys"
version = "0.1.0-beta.1"
@ -302,6 +331,12 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "constant_time_eq"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2"
[[package]]
name = "core-foundation"
version = "0.9.3"
@ -360,6 +395,16 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "d3d12"
version = "0.7.0"
@ -382,6 +427,17 @@ dependencies = [
"enum_primitive",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
"subtle",
]
[[package]]
name = "dispatch"
version = "0.2.0"
@ -555,6 +611,16 @@ dependencies = [
"waker-fn",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.10"
@ -822,6 +888,26 @@ version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "lz4"
version = "1.24.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e9e2dd86df36ce760a60f6ff6ad526f7ba1f14ba0356f8254fb6905e6494df1"
dependencies = [
"libc",
"lz4-sys",
]
[[package]]
name = "lz4-sys"
version = "1.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57d27b317e207b10f69f5e75494119e391a96f48861ae870d1da6edac98ca900"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "malloc_buf"
version = "0.0.6"
@ -1198,6 +1284,12 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22686f4785f02a4fcc856d3b3bb19bf6c8160d103f7a99cc258bddd0251dc7f2"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro-crate"
version = "1.3.1"
@ -1222,6 +1314,19 @@ name = "profiling"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46b2164ebdb1dfeec5e337be164292351e11daf63a05174c6776b2f47460f0c9"
dependencies = [
"profiling-procmacros",
]
[[package]]
name = "profiling-procmacros"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74c55e9e629af5298a40e0fa106435b2da30484c4ec76b41d19bc4d00dd8b903"
dependencies = [
"quote",
"syn 2.0.29",
]
[[package]]
name = "quote"
@ -1232,6 +1337,36 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "range-alloc"
version = "0.1.3"
@ -1244,6 +1379,69 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9"
[[package]]
name = "rbx_binary"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e50573021d04b680018955662eba7dc4aac3de92219231798f6c9b41e38ab01"
dependencies = [
"log",
"lz4",
"profiling",
"rbx_dom_weak",
"rbx_reflection",
"rbx_reflection_database",
"thiserror",
]
[[package]]
name = "rbx_dom_weak"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "161729449bbb0cfa657ce7bcca6a160d0af06d8b8d9efdc9abe14735dccacdb9"
dependencies = [
"rbx_types",
"serde",
]
[[package]]
name = "rbx_reflection"
version = "4.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08bd48487192046fec8f805f3fa29f3d7d5beb9890b0859b1a92bd8aff580343"
dependencies = [
"rbx_types",
"serde",
"thiserror",
]
[[package]]
name = "rbx_reflection_database"
version = "0.2.7+roblox-588"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1be6cf674182806f11ad4899dd1feafe977591f1ae035ae05a58d4b74e487276"
dependencies = [
"lazy_static",
"rbx_reflection",
"rmp-serde",
"serde",
]
[[package]]
name = "rbx_types"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "070106e926b8ae54c7bc443e5db4d868d7f0af51c1d7cfd7efe1364c1753d8a3"
dependencies = [
"base64",
"bitflags 1.3.2",
"blake3",
"lazy_static",
"rand",
"serde",
"thiserror",
]
[[package]]
name = "redox_syscall"
version = "0.3.5"
@ -1288,6 +1486,28 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "216080ab382b992234dda86873c18d4c48358f5cfcb70fd693d7f6f2131b628b"
[[package]]
name = "rmp"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f9860a6cc38ed1da53456442089b4dfa35e7cedaa326df63017af88385e6b20"
dependencies = [
"byteorder",
"num-traits 0.2.16",
"paste",
]
[[package]]
name = "rmp-serde"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bffea85eea980d8a74453e5d02a8d93028f3c34725de143085a844ebe953258a"
dependencies = [
"byteorder",
"rmp",
"serde",
]
[[package]]
name = "rustc-demangle"
version = "0.1.23"
@ -1338,6 +1558,26 @@ dependencies = [
"tiny-skia",
]
[[package]]
name = "serde"
version = "1.0.188"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.188"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.29",
]
[[package]]
name = "simd-adler32"
version = "0.3.7"
@ -1405,7 +1645,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "strafe-client"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"async-executor",
"bytemuck",
@ -1415,6 +1655,9 @@ dependencies = [
"log",
"obj",
"pollster",
"rbx_binary",
"rbx_dom_weak",
"rbx_reflection_database",
"wgpu",
"winit",
]
@ -1425,6 +1668,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731"
[[package]]
name = "subtle"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
[[package]]
name = "syn"
version = "1.0.109"
@ -1524,6 +1773,12 @@ version = "0.19.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a464a4b34948a5f67fddd2b823c62d9d92e44be75058b99939eae6c5b6960b33"
[[package]]
name = "typenum"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
[[package]]
name = "unicode-ident"
version = "1.0.11"

@ -1,6 +1,6 @@
[package]
name = "strafe-client"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -14,5 +14,13 @@ glam = "0.24.1"
log = "0.4.20"
obj = "0.10.2"
pollster = "0.3.0"
rbx_binary = "0.7.1"
rbx_dom_weak = "2.5.0"
rbx_reflection_database = "0.2.7"
wgpu = "0.17.0"
winit = "0.28.6"
[profile.release]
lto = true
strip = true
codegen-units = 1

BIN
maps/bhop_easyhop.rbxm Normal file

Binary file not shown.

2580
models/suzanne.obj Normal file

File diff suppressed because it is too large Load Diff

@ -1 +1,2 @@
pub mod framework;
pub mod load_roblox;

33
src/load_roblox.rs Normal file

@ -0,0 +1,33 @@
fn class_is_a(class: &str, superclass: &str) -> bool {
if class==superclass {
return true
}
let class_descriptor=rbx_reflection_database::get().classes.get(class);
if let Some(descriptor) = &class_descriptor {
if let Some(class_super) = &descriptor.superclass {
return class_is_a(&class_super, superclass)
}
}
return false
}
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::types::Ref>::new();
recursive_collect_objects(&mut objects, &dom, dom.root(), superclass);
return Ok((dom,objects))
}

@ -1,5 +1,5 @@
use bytemuck::{Pod, Zeroable};
use std::{borrow::Cow, f32::consts,time::Instant};
use std::{borrow::Cow, time::Instant};
use wgpu::{util::DeviceExt, AstcBlock, AstcChannel};
const IMAGE_SIZE: u32 = 128;
@ -16,6 +16,19 @@ struct Entity {
vertex_buf: wgpu::Buffer,
}
//temp?
struct ModelData {
transform: glam::Affine3A,
entities: Vec<Entity>,
}
struct Model {
transform: glam::Affine3A,
entities: Vec<Entity>,
bind_group: wgpu::BindGroup,
model_buf: wgpu::Buffer,
}
// Note: we use the Y=up coordinate space in this example.
struct Camera {
time: Instant,
@ -25,11 +38,13 @@ struct Camera {
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;
@ -39,7 +54,7 @@ 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 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);
@ -69,10 +84,27 @@ fn get_control_dir(controls: u32) -> glam::Vec3{
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 proj = glam::Mat4::perspective_rh(consts::FRAC_PI_2, aspect, 1.0, 200.0);
let fov = if self.controls&CONTROL_ZOOM==0 {
self.fov
}else{
self.fov/5.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();
@ -81,7 +113,7 @@ impl Camera {
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)[..]);
raw[48..51].copy_from_slice(AsRef::<[f32; 3]>::as_ref(&self.pos));
raw[51] = 1.0;
raw[51] = 1.0;//cam_pos is vec4
raw
}
}
@ -91,9 +123,9 @@ pub struct Skybox {
sky_pipeline: wgpu::RenderPipeline,
entity_pipeline: wgpu::RenderPipeline,
ground_pipeline: wgpu::RenderPipeline,
bind_group: wgpu::BindGroup,
uniform_buf: wgpu::Buffer,
entities: Vec<Entity>,
main_bind_group: wgpu::BindGroup,
camera_buf: wgpu::Buffer,
models: Vec<Model>,
depth_view: wgpu::TextureView,
staging_belt: wgpu::util::StagingBelt,
}
@ -124,6 +156,48 @@ impl Skybox {
}
}
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(&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();
for object in data.objects {
let mut entities = Vec::<Entity>::new();
for group in object.groups {
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 obj::IndexTuple(position_id, _texture_id, normal_id) =
poly.0[index];
vertices.push(Vertex {
pos: data.position[position_id],
normal: data.normal[normal_id.unwrap()],
})
}
}
}
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Vertex"),
contents: bytemuck::cast_slice(&vertices),
usage: wgpu::BufferUsages::VERTEX,
});
entities.push(Entity {
vertex_count: vertices.len() as u32,
vertex_buf,
});
}
modeldatas.push(ModelData {
transform: glam::Affine3A::default(),
entities,
})
}
}
impl strafe_client::framework::Example for Skybox {
fn optional_features() -> wgpu::Features {
wgpu::Features::TEXTURE_COMPRESSION_ASTC
@ -137,45 +211,120 @@ impl strafe_client::framework::Example for Skybox {
device: &wgpu::Device,
queue: &wgpu::Queue,
) -> Self {
let mut entities = Vec::new();
{
let source = include_bytes!("../models/teslacyberv3.0.obj");
let data = obj::ObjData::load_buf(&source[..]).unwrap();
let mut vertices = Vec::new();
for object in data.objects {
for group in object.groups {
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 obj::IndexTuple(position_id, _texture_id, normal_id) =
poly.0[index];
vertices.push(Vertex {
pos: data.position[position_id],
normal: data.normal[normal_id.unwrap()],
})
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"));
println!("models.len = {:?}", modeldatas.len());
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((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)),
Some(rbx_dom_weak::types::Variant::Float32(transparency)),
) = (
object.properties.get("CFrame"),
object.properties.get("Size"),
object.properties.get("Transparency"),
)
{
if *transparency==1.0 {
continue;
}
//simply draw a box
let mut vertices = Vec::new();
let face_data = [
[0.0f32, 0., 1.],
[0.0f32, 0., -1.],
[1.0f32, 0., 0.],
[-1.0f32, 0., 0.],
[0.0f32, 1., 0.],
[0.0f32, -1., 0.],
];
let vertex_data = [
// top (0, 0, 1)
[-1.0f32, -1., 1.],
[1.0f32, -1., 1.],
[1.0f32, 1., 1.],
[-1.0f32, 1., 1.],
// bottom (0, 0, -1)
[-1.0f32, 1., -1.],
[1.0f32, 1., -1.],
[1.0f32, -1., -1.],
[-1.0f32, -1., -1.],
// right (1, 0, 0)
[1.0f32, -1., -1.],
[1.0f32, 1., -1.],
[1.0f32, 1., 1.],
[1.0f32, -1., 1.],
// left (-1, 0, 0)
[-1.0f32, -1., 1.],
[-1.0f32, 1., 1.],
[-1.0f32, 1., -1.],
[-1.0f32, -1., -1.],
// front (0, 1, 0)
[1.0f32, 1., -1.],
[-1.0f32, 1., -1.],
[-1.0f32, 1., 1.],
[1.0f32, 1., 1.],
// back (0, -1, 0)
[1.0f32, -1., 1.],
[-1.0f32, -1., 1.],
[-1.0f32, -1., -1.],
[1.0f32, -1., -1.],
];
for face in 0..6 {
for end_index in 2..4 {
for &index in &[0, end_index - 1, end_index] {
vertices.push(Vertex {
pos: vertex_data[face*4+index],
normal: face_data[face],
})
}
}
}
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::Affine3A::from_translation(
glam::Vec3::new(cf.position.x,cf.position.y-13f32,cf.position.z)
)
* glam::Affine3A::from_mat3(
glam::Mat3::from_cols(
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::Affine3A::from_scale(
glam::Vec3::new(size.x,size.y,size.z)/2.0
),
entities,
})
}
}
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Vertex"),
contents: bytemuck::cast_slice(&vertices),
usage: wgpu::BufferUsages::VERTEX,
});
entities.push(Entity {
vertex_count: vertices.len() as u32,
vertex_buf,
});
}
}
},
Err(e) => println!("lmao err {:?}", e),
}
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
let main_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
@ -201,6 +350,21 @@ impl strafe_client::framework::Example for Skybox {
},
],
});
let model_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
],
});
// Create the render pipeline
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
@ -212,26 +376,28 @@ impl strafe_client::framework::Example for Skybox {
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 { x: 0.0, y: -100.0, z: 0.0 },
friction: 80.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 raw_uniforms = camera.to_uniform_data();
let uniform_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Buffer"),
contents: bytemuck::cast_slice(&raw_uniforms),
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,
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[&bind_group_layout],
bind_group_layouts: &[&main_bind_group_layout, &model_bind_group_layout],
push_constant_ranges: &[],
});
@ -405,12 +571,12 @@ impl strafe_client::framework::Example for Skybox {
dimension: Some(wgpu::TextureViewDimension::Cube),
..wgpu::TextureViewDescriptor::default()
});
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &bind_group_layout,
let main_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &main_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buf.as_entire_binding(),
resource: camera_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
@ -421,9 +587,37 @@ impl strafe_client::framework::Example for Skybox {
resource: wgpu::BindingResource::Sampler(&sampler),
},
],
label: None,
label: Some("Camera"),
});
//drain the modeldata vec so entities can be /moved/ to models.entities
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 {
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,
entities: modeldata.entities,
bind_group: model_bind_group,
model_buf: model_buf,
})
}
let depth_view = Self::create_depth_texture(config, device);
Skybox {
@ -431,9 +625,9 @@ impl strafe_client::framework::Example for Skybox {
sky_pipeline,
entity_pipeline,
ground_pipeline,
bind_group,
uniform_buf,
entities,
main_bind_group,
camera_buf,
models,
depth_view,
staging_belt: wgpu::util::StagingBelt::new(0x100),
}
@ -480,6 +674,10 @@ impl strafe_client::framework::Example for Skybox {
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,
}
_ => (),
}
}
@ -488,8 +686,8 @@ impl strafe_client::framework::Example for Skybox {
}
fn move_mouse(&mut self, delta: (f64,f64)) {
self.camera.pitch+=(delta.1/-512.) as f32;
self.camera.yaw+=(delta.0/-512.) 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(
@ -515,28 +713,30 @@ impl strafe_client::framework::Example for Skybox {
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));
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;
if self.camera.pos.y<2.0{
self.camera.pos.y=2.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,50.0,0.0);
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;
if applied_friction*applied_friction<self.camera.vel.length_squared() {
self.camera.vel-=applied_friction*self.camera.vel.normalize();
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=glam::Vec3::new(0.0,0.0,0.0);
self.camera.vel=targetv;
}
}
@ -544,17 +744,28 @@ impl strafe_client::framework::Example for Skybox {
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
// update rotation
let raw_uniforms = self.camera.to_uniform_data();
let camera_uniforms = self.camera.to_uniform_data();
self.staging_belt
.write_buffer(
&mut encoder,
&self.uniform_buf,
&self.camera_buf,
0,
wgpu::BufferSize::new((raw_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
wgpu::BufferSize::new((camera_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
device,
)
.copy_from_slice(bytemuck::cast_slice(&raw_uniforms));
.copy_from_slice(bytemuck::cast_slice(&camera_uniforms));
for model in self.models.iter() {
let model_uniforms = get_transform_uniform_data(&model.transform);
self.staging_belt
.write_buffer(
&mut encoder,
&model.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));
}
self.staging_belt.finish();
{
@ -583,15 +794,21 @@ impl strafe_client::framework::Example for Skybox {
}),
});
rpass.set_bind_group(0, &self.bind_group, &[]);
rpass.set_bind_group(0, &self.main_bind_group, &[]);
rpass.set_pipeline(&self.entity_pipeline);
for entity in self.entities.iter() {
rpass.set_vertex_buffer(0, entity.vertex_buf.slice(..));
rpass.draw(0..entity.vertex_count, 0..1);
for model in self.models.iter() {
rpass.set_bind_group(1, &model.bind_group, &[]);
for entity in model.entities.iter() {
rpass.set_vertex_buffer(0, entity.vertex_buf.slice(..));
rpass.draw(0..entity.vertex_count, 0..1);
}
}
rpass.set_pipeline(&self.ground_pipeline);
//rpass.set_index_buffer(&[0u16,1,2,1,2,3][..] as wgpu::BufferSlice, wgpu::IndexFormat::Uint16);
//rpass.draw_indexed(0..4, 0, 0..1);
rpass.draw(0..6, 0..1);
rpass.set_pipeline(&self.sky_pipeline);
@ -605,5 +822,9 @@ impl strafe_client::framework::Example for Skybox {
}
fn main() {
strafe_client::framework::run::<Skybox>("Strafe Client v0.1");
strafe_client::framework::run::<Skybox>(
format!("Strafe Client v{}",
env!("CARGO_PKG_VERSION")
).as_str()
);
}

@ -47,13 +47,13 @@ struct GroundOutput {
@vertex
fn vs_ground(@builtin(vertex_index) vertex_index: u32) -> GroundOutput {
// hacky way to draw two triangles that make a square
let tmp1 = (i32(vertex_index)-i32(vertex_index)/3*2) / 2;
let tmp2 = (i32(vertex_index)-i32(vertex_index)/3*2) & 1;
let tmp1 = i32(vertex_index)/2-i32(vertex_index)/3;
let tmp2 = i32(vertex_index)&1;
let pos = vec3<f32>(
f32(tmp1) * 2.0 - 1.0,
0.0,
f32(tmp2) * 2.0 - 1.0
) * 100.0;
) * 160.0;
var result: GroundOutput;
result.pos = pos;
@ -67,15 +67,20 @@ struct EntityOutput {
@location(3) view: vec3<f32>,
};
@group(1)
@binding(0)
var<uniform> r_EntityTransform: mat4x4<f32>;
@vertex
fn vs_entity(
@location(0) pos: vec3<f32>,
@location(1) normal: vec3<f32>,
) -> EntityOutput {
var position: vec4<f32> = r_EntityTransform * vec4<f32>(pos, 1.0);
var result: EntityOutput;
result.normal = normal;
result.view = pos - r_data.cam_pos.xyz;
result.position = r_data.proj * r_data.view * vec4<f32>(pos, 1.0);
result.normal = (r_EntityTransform * vec4<f32>(normal, 0.0)).xyz;
result.view = position.xyz - r_data.cam_pos.xyz;
result.position = r_data.proj * r_data.view * position;
return result;
}
@ -101,8 +106,20 @@ fn fs_entity(vertex: EntityOutput) -> @location(0) vec4<f32> {
return vec4<f32>(vec3<f32>(0.1) + 0.5 * reflected_color, 1.0);
}
fn modulo_euclidean (a: f32, b: f32) -> f32 {
var m = a % b;
if (m < 0.0) {
if (b < 0.0) {
m -= b;
} else {
m += b;
}
}
return m;
}
@fragment
fn fs_ground(vertex: GroundOutput) -> @location(0) vec4<f32> {
let dir = vec3<f32>(-1.0)+vec3<f32>(vertex.pos.x/16.%1.0,0.0,vertex.pos.z/16.%1.0)*2.0;
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);
}