Skip to content

Commit 4adb72d

Browse files
authored
Merge pull request #6 from LykenSol/mouse
Shadertoy-like mouse support.
2 parents 8e57169 + 0136319 commit 4adb72d

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

shaders/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![register_attr(spirv)]
55

66
use shared::*;
7-
use spirv_std::glam::{vec2, vec3, Vec2, Vec3, Vec4};
7+
use spirv_std::glam::{vec2, vec3, vec4, Vec2, Vec3, Vec4};
88
use spirv_std::storage_class::{Input, Output, PushConstant};
99

1010
pub mod a_lot_of_spheres;
@@ -58,7 +58,17 @@ pub fn fs(constants: &ShaderConstants, mut frag_coord: Vec2) -> Vec4 {
5858
0.0,
5959
);
6060
let time = constants.time;
61-
let mouse = Vec4::zero();
61+
let mouse = vec4(
62+
constants.last_lmb_down_x / COLS as f32,
63+
constants.last_lmb_down_y / ROWS as f32,
64+
constants.last_click_x / COLS as f32
65+
* if constants.mouse_left_pressed {
66+
1.0
67+
} else {
68+
-1.0
69+
},
70+
constants.last_click_y / ROWS as f32 * if constants.mouse_clicked { 1.0 } else { -1.0 },
71+
);
6272

6373
let col = (frag_coord.x / resolution.x) as usize;
6474
let row = (frag_coord.y / resolution.y) as usize;

shared/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ pub struct ShaderConstants {
2121
pub width: u32,
2222
pub height: u32,
2323
pub time: f32,
24+
pub last_lmb_down_x: f32,
25+
pub last_lmb_down_y: f32,
26+
pub last_click_x: f32,
27+
pub last_click_y: f32,
28+
pub mouse_left_pressed: bool,
29+
pub mouse_clicked: bool,
2430
}
2531

2632
pub fn saturate(x: f32) -> f32 {

src/main.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use shared::ShaderConstants;
22
use winit::{
3-
event::{Event, KeyboardInput, VirtualKeyCode, WindowEvent},
3+
event::{ElementState, Event, KeyboardInput, MouseButton, VirtualKeyCode, WindowEvent},
44
event_loop::{ControlFlow, EventLoop},
55
window::Window,
66
};
@@ -101,6 +101,11 @@ async fn run(
101101
.map(|surface| device.create_swap_chain(&surface, &sc_desc));
102102

103103
let start = std::time::Instant::now();
104+
let mut mouse_left_pressed = false;
105+
let (mut cursor_x, mut cursor_y) = (0.0, 0.0);
106+
let (mut last_lmb_down_x, mut last_lmb_down_y) = (0.0, 0.0);
107+
let mut mouse_clicked = false;
108+
let (mut last_click_x, mut last_click_y) = (0.0, 0.0);
104109

105110
event_loop.run(move |event, _, control_flow| {
106111
// Have the closure take ownership of the resources.
@@ -157,7 +162,14 @@ async fn run(
157162
width: window.inner_size().width,
158163
height: window.inner_size().height,
159164
time: start.elapsed().as_secs_f32(),
165+
last_lmb_down_x,
166+
last_lmb_down_y,
167+
last_click_x,
168+
last_click_y,
169+
mouse_left_pressed,
170+
mouse_clicked,
160171
};
172+
mouse_clicked = false;
161173
rpass.set_pipeline(&render_pipeline);
162174
rpass.set_push_constants(wgpu::ShaderStage::all(), 0, unsafe {
163175
any_as_u32_slice(&push_constants)
@@ -184,6 +196,35 @@ async fn run(
184196
},
185197
..
186198
} => *control_flow = ControlFlow::Exit,
199+
Event::WindowEvent {
200+
event:
201+
WindowEvent::MouseInput {
202+
state,
203+
button: MouseButton::Left,
204+
..
205+
},
206+
..
207+
} => {
208+
mouse_left_pressed = state == ElementState::Pressed;
209+
if mouse_left_pressed {
210+
last_lmb_down_x = cursor_x;
211+
last_lmb_down_y = cursor_y;
212+
last_click_x = cursor_x;
213+
last_click_y = cursor_y;
214+
mouse_clicked = true;
215+
}
216+
}
217+
Event::WindowEvent {
218+
event: WindowEvent::CursorMoved { position, .. },
219+
..
220+
} => {
221+
cursor_x = position.x as f32;
222+
cursor_y = position.y as f32;
223+
if mouse_left_pressed {
224+
last_lmb_down_x = cursor_x;
225+
last_lmb_down_y = cursor_y;
226+
}
227+
}
187228
_ => {}
188229
}
189230
});

0 commit comments

Comments
 (0)