This commit is contained in:
Matthew Deville 2025-08-31 17:25:38 +02:00
parent 999e86224a
commit a8357a8876
3 changed files with 12 additions and 17 deletions

View file

@ -17,7 +17,7 @@ fn main() {
Update,
(
material::update_material_time,
systems::resize_fullscreen_quad,
systems::screen_resized,
systems::exit_app.run_if(input_just_pressed(KeyCode::Escape)),
systems::toggle_fullscreen.run_if(input_just_pressed(KeyCode::F11)),
),

View file

@ -7,7 +7,6 @@ use bevy::{
const DEFAULT_SHADER_PATH: &str = "shaders/default.wgsl";
// This is the struct that will be passed to your shader
#[derive(Asset, TypePath, AsBindGroup, Clone)]
pub struct CustomMaterial {
#[uniform(0)]

View file

@ -1,23 +1,19 @@
use bevy::{
prelude::*,
window::{PrimaryWindow, WindowMode},
window::{PrimaryWindow, WindowMode, WindowResized},
};
use crate::material::CustomMaterial;
/// Marker component for the fullscreen quad so we can update it on window resize
#[derive(Component)]
pub struct FullscreenQuad;
/// set up a simple 2D-screen-like surface
pub fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<CustomMaterial>>,
mut primary_window: Single<&mut Window, With<PrimaryWindow>>,
primary_window: Single<&Window, With<PrimaryWindow>>,
mut commands: Commands,
) {
primary_window.decorations = false;
commands.spawn(Camera2d);
// quad that fills the whole window
@ -39,16 +35,16 @@ pub fn setup(
));
}
/// Update fullscreen quad transforms when the primary window is resized.
pub fn resize_fullscreen_quad(
primary_window: Single<&Window, With<PrimaryWindow>>,
mut transform: Single<&mut Transform, With<FullscreenQuad>>,
pub fn screen_resized(
mut events: EventReader<WindowResized>,
shader_data: Single<(&mut Transform, &MeshMaterial2d<CustomMaterial>), With<FullscreenQuad>>,
mut materials: ResMut<Assets<CustomMaterial>>,
) {
let size = Vec2::new(
primary_window.resolution.width(),
primary_window.resolution.height(),
);
transform.scale = Vec3::new(size.x, size.y, 1.0);
let (mut transform, material) = shader_data.into_inner();
if let Some(event) = events.read().last() {
transform.scale = Vec3::new(event.width, event.height, 1.0);
materials.get_mut(&material.0).unwrap().resolution = Vec2::new(event.width, event.height);
}
}
pub fn exit_app(mut exit: EventWriter<AppExit>) {