fix builds, improve movement

This commit is contained in:
zack 2024-12-28 22:21:15 -05:00
parent a19661daa0
commit 94432b0c0d
No known key found for this signature in database
GPG key ID: 5F873416BCF59F35
4 changed files with 63 additions and 22 deletions

1
Cargo.lock generated
View file

@ -875,6 +875,7 @@ dependencies = [
[[package]] [[package]]
name = "gpu-profiler" name = "gpu-profiler"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zackartz/gpu-profiler#cb5b9d74a9ea25d1f27e4be7949403296e351370"
dependencies = [ dependencies = [
"ash", "ash",
"once_cell", "once_cell",

View file

@ -19,7 +19,9 @@ ash-window = "0.12.0"
shaders-shared = { path = "../shaders-shared" } shaders-shared = { path = "../shaders-shared" }
puffin = { git = "https://github.com/EmbarkStudios/puffin", rev = "5ac4e54164ee89bd68c29f288c2b5613afc2c929" } puffin = { git = "https://github.com/EmbarkStudios/puffin", rev = "5ac4e54164ee89bd68c29f288c2b5613afc2c929" }
puffin_egui = { git = "https://github.com/EmbarkStudios/puffin", rev = "5ac4e54164ee89bd68c29f288c2b5613afc2c929" } puffin_egui = { git = "https://github.com/EmbarkStudios/puffin", rev = "5ac4e54164ee89bd68c29f288c2b5613afc2c929" }
gpu-profiler = { path = "../../../gpu-profiler", features = ["use-ash"] } gpu-profiler = { git = "https://github.com/zackartz/gpu-profiler", features = [
"use-ash",
] }
[build-dependencies] [build-dependencies]
spirv-builder.workspace = true spirv-builder.workspace = true

View file

@ -45,6 +45,7 @@ struct Game {
camera_position: Vec3, camera_position: Vec3,
camera_yaw: f32, camera_yaw: f32,
camera_pitch: f32, camera_pitch: f32,
camera_fov: f32,
right_mouse_pressed: bool, right_mouse_pressed: bool,
last_mouse_pos: Option<(f32, f32)>, last_mouse_pos: Option<(f32, f32)>,
@ -105,6 +106,12 @@ impl App for Game {
ui.add(egui::DragValue::new(&mut self.camera_position.z).speed(0.1)); ui.add(egui::DragValue::new(&mut self.camera_position.z).speed(0.1));
}); });
ui.separator(); ui.separator();
ui.label("FOV");
ui.add(egui::widgets::Slider::new(
&mut self.camera_fov,
10.0..=150.0,
));
ui.separator();
if ui.button("Show Profiler").clicked() { if ui.button("Show Profiler").clicked() {
self.show_profiler = !self.show_profiler; self.show_profiler = !self.show_profiler;
} }
@ -114,12 +121,19 @@ impl App for Game {
if !ctx.wants_keyboard_input() { if !ctx.wants_keyboard_input() {
let movement_speed = 0.1; let movement_speed = 0.1;
let forward = Vec3::new(self.camera_yaw.sin(), 0.0, self.camera_yaw.cos()).normalize(); // Calculate forward direction using yaw
let forward = Vec3::new(
self.camera_yaw.sin(),
self.camera_pitch.sin(),
self.camera_yaw.cos(),
)
.normalize();
// Calculate right direction
let right = Vec3::new( let right = Vec3::new(
(self.camera_yaw + 90.0_f32.to_radians()).sin(), (self.camera_yaw + std::f32::consts::FRAC_PI_2).sin(),
0.0, 0.0,
(self.camera_yaw + 90.0_f32.to_radians()).cos(), (self.camera_yaw + std::f32::consts::FRAC_PI_2).cos(),
) )
.normalize(); .normalize();
@ -141,6 +155,7 @@ impl App for Game {
// Handle mouse input for camera rotation // Handle mouse input for camera rotation
let is_right_mouse_down = ctx.input(|i| i.pointer.secondary_down()); let is_right_mouse_down = ctx.input(|i| i.pointer.secondary_down());
let is_middle_mouse_down = ctx.input(|i| i.pointer.middle_down());
let hover_pos = ctx.input(|i| i.pointer.hover_pos()); let hover_pos = ctx.input(|i| i.pointer.hover_pos());
// Set cursor visibility based on right mouse button // Set cursor visibility based on right mouse button
@ -154,22 +169,40 @@ impl App for Game {
self.right_mouse_pressed = is_right_mouse_down; self.right_mouse_pressed = is_right_mouse_down;
if self.right_mouse_pressed { match (self.right_mouse_pressed, is_middle_mouse_down) {
if let Some(pos) = hover_pos { (true, false) => {
if let Some((last_x, last_y)) = self.last_mouse_pos { if let Some(pos) = hover_pos {
let delta_x = pos.x - last_x; if let Some((last_x, last_y)) = self.last_mouse_pos {
let delta_y = pos.y - last_y; let delta_x = pos.x - last_x;
let delta_y = pos.y - last_y;
// Update camera rotation // Update camera rotation
let rotation_speed = 0.002; let rotation_speed = 0.002;
self.camera_yaw -= delta_x * rotation_speed; self.camera_yaw -= delta_x * rotation_speed;
self.camera_pitch = (self.camera_pitch + delta_y * rotation_speed) self.camera_pitch = (self.camera_pitch + delta_y * rotation_speed)
.clamp(-89.0_f32.to_radians(), 89.0_f32.to_radians()); .clamp(-89.0_f32.to_radians(), 89.0_f32.to_radians());
}
self.last_mouse_pos = Some((pos.x, pos.y));
} }
self.last_mouse_pos = Some((pos.x, pos.y));
} }
} else { (false, true) => {
self.last_mouse_pos = None; if let Some(pos) = hover_pos {
if let Some((last_x, last_y)) = self.last_mouse_pos {
let delta_x = pos.x - last_x;
let delta_y = pos.y - last_y;
let move_speed = 0.005;
self.camera_position.x -= delta_x * move_speed;
self.camera_position.y += delta_y * move_speed;
}
self.last_mouse_pos = Some((pos.x, pos.y))
}
}
_ => {
self.last_mouse_pos = None;
}
} }
match self.theme { match self.theme {
@ -186,9 +219,10 @@ impl App for Game {
let camera_position = self.camera_position; let camera_position = self.camera_position;
let camera_yaw = self.camera_yaw; let camera_yaw = self.camera_yaw;
let camera_pitch = self.camera_pitch; let camera_pitch = self.camera_pitch;
let camera_fov = self.camera_fov;
move |size, egui_cmd| { move |size, egui_cmd| {
let mut renderer = renderer.inner.lock().unwrap(); let mut renderer = renderer.inner.lock().unwrap();
renderer.update_camera(camera_position, camera_yaw, camera_pitch); renderer.update_camera(camera_position, camera_yaw, camera_pitch, camera_fov);
renderer.render(size.width, size.height, egui_cmd, rotate_y) renderer.render(size.width, size.height, egui_cmd, rotate_y)
} }
})) }))
@ -521,7 +555,7 @@ impl AppCreator<Arc<Mutex<Allocator>>> for MyAppCreator {
} }
let ( let (
physical_device, physical_device,
physical_device_properties, _physical_device_properties,
_physical_device_memory_properties, _physical_device_memory_properties,
queue_family_index, queue_family_index,
) = Self::create_physical_device(&instance, &surface_loader, surface, &req_ext); ) = Self::create_physical_device(&instance, &surface_loader, surface, &req_ext);
@ -591,6 +625,7 @@ impl AppCreator<Arc<Mutex<Allocator>>> for MyAppCreator {
camera_position: Vec3::new(0.0, 0.0, -5.0), camera_position: Vec3::new(0.0, 0.0, -5.0),
camera_pitch: 0., camera_pitch: 0.,
camera_yaw: 0., camera_yaw: 0.,
camera_fov: 45.,
last_mouse_pos: None, last_mouse_pos: None,
right_mouse_pressed: false, right_mouse_pressed: false,
last_fps_update: std::time::Instant::now(), last_fps_update: std::time::Instant::now(),

View file

@ -3,7 +3,6 @@ use ash::{
vk::{self, Buffer}, vk::{self, Buffer},
Device, Device,
}; };
use cgmath::num_traits::Float;
use egui_ash::EguiCommand; use egui_ash::EguiCommand;
use glam::{Mat4, Vec3}; use glam::{Mat4, Vec3};
use gpu_allocator::vulkan::{Allocation, Allocator}; use gpu_allocator::vulkan::{Allocation, Allocator};
@ -102,6 +101,7 @@ pub struct RendererInner {
frame_counter: u32, frame_counter: u32,
camera_position: Vec3, camera_position: Vec3,
camera_fov: f32,
camera_yaw: f32, camera_yaw: f32,
camera_pitch: f32, camera_pitch: f32,
accumulation_reset_needed: bool, accumulation_reset_needed: bool,
@ -1082,18 +1082,21 @@ impl RendererInner {
camera_position: Vec3::new(0.0, 0.0, -5.0), camera_position: Vec3::new(0.0, 0.0, -5.0),
camera_yaw: 0., camera_yaw: 0.,
camera_pitch: 0., camera_pitch: 0.,
camera_fov: 45.,
accumulation_reset_needed: true, accumulation_reset_needed: true,
} }
} }
pub fn update_camera(&mut self, new_position: Vec3, yaw: f32, pitch: f32) { pub fn update_camera(&mut self, new_position: Vec3, yaw: f32, pitch: f32, fov: f32) {
if (new_position - self.camera_position).length() > 0.0001 if (new_position - self.camera_position).length() > 0.0001
|| (yaw - self.camera_yaw).abs() > 0.001 || (yaw - self.camera_yaw).abs() > 0.001
|| (pitch - self.camera_pitch).abs() > 0.001 || (pitch - self.camera_pitch).abs() > 0.001
|| (fov - self.camera_fov).abs() > 0.001
{ {
self.camera_position = new_position; self.camera_position = new_position;
self.camera_yaw = yaw; self.camera_yaw = yaw;
self.camera_pitch = pitch; self.camera_pitch = pitch;
self.camera_fov = fov;
self.accumulation_reset_needed = true; self.accumulation_reset_needed = true;
} }
} }
@ -1172,7 +1175,7 @@ impl RendererInner {
model: Mat4::from_rotation_y(rotate_y.to_radians()), model: Mat4::from_rotation_y(rotate_y.to_radians()),
view, view,
proj: Mat4::perspective_rh( proj: Mat4::perspective_rh(
45.0_f32.to_radians(), self.camera_fov.to_radians(),
width as f32 / height as f32, width as f32 / height as f32,
0.1, 0.1,
10.0, 10.0,