//! A shader that uses the GLSL shading language. use bevy::{ prelude::*, reflect::TypePath, render::render_resource::{AsBindGroup, ShaderRef}, sprite::{AlphaMode2d, Material2d, Material2dPlugin}, window::PrimaryWindow, }; const SHADER_ASSET_PATH: &str = "shaders/default.wgsl"; fn main() { App::new() .add_plugins(( DefaultPlugins, Material2dPlugin::::default(), )) .add_systems(Startup, setup) .add_systems(Update, resize_fullscreen_quad) .run(); } /// set up a simple 2D-screen-like surface fn setup( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, primary_window: Single<&Window, With>, ) { // camera commands.spawn(Camera2d); // quad that fills the whole window let size = Vec2::new( primary_window.resolution.width(), primary_window.resolution.height(), ); commands.spawn(( Mesh2d(meshes.add(Rectangle::default())), MeshMaterial2d(materials.add(CustomMaterial {})), Transform::from_scale(Vec3::new(size.x, size.y, 1.0)), FullscreenQuad, )); } /// Marker component for the fullscreen quad so we can update it on window resize #[derive(Component)] struct FullscreenQuad; /// Update fullscreen quad transforms when the primary window is resized. fn resize_fullscreen_quad( primary_window: Single<&Window, With>, mut transform: Single<&mut Transform, With>, ) { let size = Vec2::new( primary_window.resolution.width(), primary_window.resolution.height(), ); transform.scale = Vec3::new(size.x, size.y, 1.0); } // This is the struct that will be passed to your shader #[derive(Asset, TypePath, AsBindGroup, Clone)] struct CustomMaterial {} /// The Material2d trait is very configurable, but comes with sensible defaults for all methods. /// You only need to implement functions for features that need non-default behavior. See the Material2d api docs for details! impl Material2d for CustomMaterial { fn fragment_shader() -> ShaderRef { SHADER_ASSET_PATH.into() } fn alpha_mode(&self) -> AlphaMode2d { AlphaMode2d::Opaque } }