add mouse

This commit is contained in:
Matthew Deville 2025-08-31 19:22:17 +02:00
parent a8357a8876
commit cefcab280c
4 changed files with 19 additions and 1 deletions

View file

@ -2,6 +2,7 @@
@group(2) @binding(0) var<uniform> u_time: f32; @group(2) @binding(0) var<uniform> u_time: f32;
@group(2) @binding(1) var<uniform> u_resolution: vec2<f32>; @group(2) @binding(1) var<uniform> u_resolution: vec2<f32>;
@group(2) @binding(2) var<uniform> u_mouse_position: vec2<f32>;
@fragment @fragment
fn fragment(mesh: VertexOutput) -> @location(0) vec4<f32> { fn fragment(mesh: VertexOutput) -> @location(0) vec4<f32> {
@ -11,7 +12,7 @@ fn fragment(mesh: VertexOutput) -> @location(0) vec4<f32> {
let red = vec3<f32>(1, 0, 0); let red = vec3<f32>(1, 0, 0);
let blue = vec3<f32>(0, 0, 1.0); let blue = vec3<f32>(0, 0, 1.0);
let t = step(0.5, length(uv)); let t = step(0.2 + u_mouse_position.x / u_resolution.x, length(uv));
let color = mix(red, blue, t); let color = mix(red, blue, t);
return vec4<f32>(color, 1); return vec4<f32>(color, 1);

View file

@ -18,6 +18,7 @@ fn main() {
( (
material::update_material_time, material::update_material_time,
systems::screen_resized, systems::screen_resized,
systems::mouse_moved,
systems::exit_app.run_if(input_just_pressed(KeyCode::Escape)), systems::exit_app.run_if(input_just_pressed(KeyCode::Escape)),
systems::toggle_fullscreen.run_if(input_just_pressed(KeyCode::F11)), systems::toggle_fullscreen.run_if(input_just_pressed(KeyCode::F11)),
), ),

View file

@ -13,6 +13,8 @@ pub struct CustomMaterial {
pub time: f32, pub time: f32,
#[uniform(1)] #[uniform(1)]
pub resolution: Vec2, pub resolution: Vec2,
#[uniform(2)]
pub mouse_position: Vec2,
} }
pub fn update_material_time(mut materials: ResMut<Assets<CustomMaterial>>, time: Res<Time>) { pub fn update_material_time(mut materials: ResMut<Assets<CustomMaterial>>, time: Res<Time>) {

View file

@ -25,6 +25,7 @@ pub fn setup(
let material_handle = materials.add(CustomMaterial { let material_handle = materials.add(CustomMaterial {
time: 0.0, time: 0.0,
resolution: screen_size, resolution: screen_size,
mouse_position: Vec2::ZERO,
}); });
commands.spawn(( commands.spawn((
@ -35,6 +36,19 @@ pub fn setup(
)); ));
} }
pub fn mouse_moved(
mut events: EventReader<CursorMoved>,
shader_material: Single<&MeshMaterial2d<CustomMaterial>, With<FullscreenQuad>>,
mut materials: ResMut<Assets<CustomMaterial>>,
) {
if let Some(event) = events.read().last() {
materials
.get_mut(&shader_material.0)
.unwrap()
.mouse_position = Vec2::new(event.position.x, event.position.y);
}
}
pub fn screen_resized( pub fn screen_resized(
mut events: EventReader<WindowResized>, mut events: EventReader<WindowResized>,
shader_data: Single<(&mut Transform, &MeshMaterial2d<CustomMaterial>), With<FullscreenQuad>>, shader_data: Single<(&mut Transform, &MeshMaterial2d<CustomMaterial>), With<FullscreenQuad>>,