WebGPU Voxel Game
at main 4.2 kB view raw
1#![allow(rust_analyzer::inactive_code)] 2 3mod app; 4mod gfx; 5mod gui; 6mod world; 7 8use glam::{ivec3, IVec3, Mat3, Quat, Vec3}; 9#[cfg(target_arch = "wasm32")] 10use wasm_bindgen::prelude::*; 11use wasm_bindgen::UnwrapThrowExt; 12use winit::event_loop::EventLoop; 13 14#[derive(Copy, Clone, Debug)] 15struct Instance { 16 position: Vec3, 17 rotation: Quat, 18} 19 20#[cfg(not(target_arch = "wasm32"))] 21type ConnectionOnlyOnNative = rusqlite::Connection; 22 23#[cfg(target_arch = "wasm32")] 24type ConnectionOnlyOnNative = (); 25 26impl Instance { 27 fn as_raw(&self) -> InstanceRaw { 28 InstanceRaw { 29 model: (glam::Mat4::from_translation(self.position) 30 * glam::Mat4::from_quat(self.rotation)) 31 .to_cols_array_2d(), 32 normal: Mat3::from_quat(self.rotation).to_cols_array_2d(), 33 } 34 } 35} 36#[repr(C)] 37#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] 38struct InstanceRaw { 39 model: [[f32; 4]; 4], 40 normal: [[f32; 3]; 3], 41} 42 43impl InstanceRaw { 44 fn desc() -> wgpu::VertexBufferLayout<'static> { 45 wgpu::VertexBufferLayout { 46 array_stride: size_of::<InstanceRaw>() as wgpu::BufferAddress, 47 step_mode: wgpu::VertexStepMode::Instance, 48 attributes: &[ 49 wgpu::VertexAttribute { 50 offset: 0, 51 shader_location: 5, 52 format: wgpu::VertexFormat::Float32x4, 53 }, 54 wgpu::VertexAttribute { 55 offset: size_of::<[f32; 4]>() as wgpu::BufferAddress, 56 shader_location: 6, 57 format: wgpu::VertexFormat::Float32x4, 58 }, 59 wgpu::VertexAttribute { 60 offset: size_of::<[f32; 8]>() as wgpu::BufferAddress, 61 shader_location: 7, 62 format: wgpu::VertexFormat::Float32x4, 63 }, 64 wgpu::VertexAttribute { 65 offset: size_of::<[f32; 12]>() as wgpu::BufferAddress, 66 shader_location: 8, 67 format: wgpu::VertexFormat::Float32x4, 68 }, 69 wgpu::VertexAttribute { 70 offset: size_of::<[f32; 16]>() as wgpu::BufferAddress, 71 shader_location: 9, 72 format: wgpu::VertexFormat::Float32x3, 73 }, 74 wgpu::VertexAttribute { 75 offset: size_of::<[f32; 19]>() as wgpu::BufferAddress, 76 shader_location: 10, 77 format: wgpu::VertexFormat::Float32x3, 78 }, 79 wgpu::VertexAttribute { 80 offset: size_of::<[f32; 22]>() as wgpu::BufferAddress, 81 shader_location: 11, 82 format: wgpu::VertexFormat::Float32x3, 83 }, 84 ], 85 } 86 } 87} 88 89fn init_logger() { 90 cfg_if::cfg_if! { 91 if #[cfg(target_arch = "wasm32")] { 92 std::panic::set_hook(Box::new(console_error_panic_hook::hook)); 93 console_log::init_with_level(log::Level::Debug).expect("Couldn't initialize logger"); 94 } else { 95 //env_logger::init(); 96 env_logger::builder().filter_level(log::LevelFilter::Warn).init(); 97 } 98 } 99} 100#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))] 101pub async fn run() { 102 init_logger(); 103 104 log::info!("Hello world!"); 105 // preload_chunk_cache(); 106 107 let conn = rusqlite::Connection::open("./save.sqlite").unwrap(); 108 conn.execute( 109 r#" 110 CREATE TABLE IF NOT EXISTS chunks ( 111 x INTEGER, 112 y INTEGER, 113 z INTEGER, 114 data BLOB, 115 PRIMARY KEY (x,y,z) 116 ) 117 "#, 118 (), 119 ) 120 .unwrap(); 121 122 let event_loop = EventLoop::with_user_event().build().unwrap_throw(); 123 124 let mut app = app::Application::new(&event_loop, "BL0CK", conn); 125 event_loop.run_app(&mut app).unwrap(); 126} 127 128fn ivec3_product(lower: IVec3, higher: IVec3) -> impl Iterator<Item = IVec3> { 129 let IVec3 { 130 x: ax, 131 y: ay, 132 z: az, 133 } = lower; 134 let IVec3 { 135 x: bx, 136 y: by, 137 z: bz, 138 } = higher; 139 itertools::iproduct!(ax..=bx, ay..=by, az..=bz).map(|(i, j, k)| ivec3(i, j, k)) 140}