-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
260 additions
and
65 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use winit::event::MouseButton; | ||
|
||
use crate::mouse_input::MouseState; | ||
|
||
// Naive look-at camera. | ||
// This version removes the use of quaternion to avoid adding a dependency. | ||
// To avoid having to do linear algebra ourselves, most computations are done in the shader. | ||
// This is sub-optimal. Improving this is left as an exercise to the reader. | ||
#[derive(Debug)] | ||
pub struct CameraLookAt { | ||
// Object the camera is looking at. | ||
pub center: [f32; 3], | ||
// Angle around the object on the horizontal plane, in radians. | ||
pub angle: f32, | ||
// Height between -1 and 1, 0 is flat, 1 is zenith, -1 is nadir | ||
pub height: f32, | ||
// Distance from center | ||
pub distance: f32, | ||
} | ||
|
||
impl Default for CameraLookAt { | ||
fn default() -> Self { | ||
// See object in 0,0,0 from the front top left | ||
CameraLookAt { | ||
center: [0.0, 0.0, 0.0], | ||
angle: 2.0 * std::f32::consts::FRAC_PI_3, | ||
height: 0.3, | ||
distance: f32::sqrt(72.0), | ||
} | ||
} | ||
} | ||
|
||
impl CameraLookAt { | ||
/// Pan the camera with middle mouse click, zoom with scroll wheel, orbit with right mouse click. | ||
pub fn update(&mut self, mouse_state: &MouseState, window_size: [f32; 2]) { | ||
// change input mapping for orbit and panning here | ||
let orbit_button = MouseButton::Right; | ||
let translation_button = MouseButton::Middle; | ||
|
||
if mouse_state.position_delta[0] != 0.0 || mouse_state.position_delta[1] != 0.0 { | ||
if mouse_state.pressed(orbit_button) { | ||
let delta_x = | ||
mouse_state.position_delta[0] / window_size[0] * std::f32::consts::PI * 2.0; | ||
let delta_y = mouse_state.position_delta[1] / window_size[1] * std::f32::consts::PI; | ||
self.angle += delta_x; | ||
self.height += delta_y; | ||
self.height = self | ||
.height | ||
.max(-std::f32::consts::FRAC_PI_2 + 0.001) | ||
.min(std::f32::consts::FRAC_PI_2 - 0.001); | ||
} | ||
|
||
if mouse_state.pressed(translation_button) { | ||
let dir = [self.angle.cos(), self.angle.sin()]; | ||
let translation_dir = [-dir[1], dir[0]]; | ||
let translation_weight = | ||
mouse_state.position_delta[0] / window_size[0] * self.distance; | ||
|
||
self.center[0] += translation_dir[0] * translation_weight; | ||
self.center[2] += translation_dir[1] * translation_weight; | ||
self.center[1] += mouse_state.position_delta[1] / window_size[1] * self.distance; | ||
} | ||
} | ||
|
||
if mouse_state.scroll_delta != 0.0 { | ||
self.distance -= mouse_state.scroll_delta * self.distance * 0.2; | ||
// Don't allow zoom to reach 0 or 1e6 to avoid getting stuck / in float precision issue realm. | ||
self.distance = self.distance.max(0.05).min(1e6); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use winit::event::{ElementState, MouseButton, MouseScrollDelta, WindowEvent}; | ||
|
||
// Naive mouse state. | ||
#[derive(Debug, Default)] | ||
pub struct MouseState { | ||
pub position: [f32; 2], | ||
pub position_delta: [f32; 2], | ||
pub left: bool, | ||
pub right: bool, | ||
pub middle: bool, | ||
pub scroll_delta: f32, | ||
} | ||
|
||
impl MouseState { | ||
pub fn pressed(&self, button: MouseButton) -> bool { | ||
match button { | ||
MouseButton::Left => self.left, | ||
MouseButton::Right => self.right, | ||
MouseButton::Middle => self.middle, | ||
_ => false, | ||
} | ||
} | ||
|
||
// Called on Event::RedrawRequested since it's the start of a new frame. | ||
pub fn clear_deltas(&mut self) { | ||
self.position_delta = [0.0, 0.0]; | ||
self.scroll_delta = 0.0; | ||
} | ||
|
||
// Called on relevant window events. | ||
pub fn on_window_event(&mut self, window_event: WindowEvent) { | ||
self.position_delta = [0.0, 0.0]; | ||
self.scroll_delta = 0.0; | ||
match window_event { | ||
WindowEvent::MouseInput { button, state, .. } => match button { | ||
MouseButton::Left => self.left = state == ElementState::Pressed, | ||
MouseButton::Right => self.right = state == ElementState::Pressed, | ||
MouseButton::Middle => self.middle = state == ElementState::Pressed, | ||
_ => (), | ||
}, | ||
WindowEvent::CursorMoved { position, .. } => { | ||
self.position_delta = [ | ||
position.x as f32 - self.position[0], | ||
position.y as f32 - self.position[1], | ||
]; | ||
self.position = [position.x as f32, position.y as f32]; | ||
} | ||
WindowEvent::MouseWheel { delta, .. } => { | ||
log::info!("delta: {:?}", delta); | ||
match delta { | ||
// native mode: line delta should be 1 or -1 | ||
MouseScrollDelta::LineDelta(_, y) => self.scroll_delta = y, | ||
// wasm: pixel delta is around 100 * display_scale | ||
MouseScrollDelta::PixelDelta(pos) => self.scroll_delta = pos.y as f32 / 100.0, | ||
} | ||
} | ||
_ => (), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
fn get_camera_ray(x: f32, y: f32) -> vec3<f32> { | ||
let fov = 60.0; | ||
let xy: vec2<f32> = vec2(x, y) - vec2(uniforms.width, uniforms.height) / 2.0; | ||
let z: f32 = uniforms.height / tan(radians(fov) / 2.0); | ||
return normalize(vec3(xy.x, -xy.y, -z)); | ||
} | ||
|
||
fn get_view_matrix(eye: vec3<f32>, center: vec3<f32>, up: vec3<f32>) -> mat4x4<f32> { | ||
let f = normalize(center - eye); | ||
let s = normalize(cross(f, up)); | ||
let u = cross(s, f); | ||
return mat4x4( | ||
vec4(s, 0.0), | ||
vec4(u, 0.0), | ||
vec4(-f, 0.0), | ||
vec4(-dot(eye, s), -dot(eye, u), dot(eye, f), 1.0) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.