animate_shader/
animate_shader.rs

1//! A shader that uses dynamic data like the time since startup.
2//! The time data is in the globals binding which is part of the `mesh_view_bindings` shader import.
3
4use bevy::{
5    prelude::*, reflect::TypePath, render::render_resource::AsBindGroup, shader::ShaderRef,
6};
7
8/// This example uses a shader source file from the assets subdirectory
9const SHADER_ASSET_PATH: &str = "shaders/animate_shader.wgsl";
10
11fn main() {
12    App::new()
13        .add_plugins((DefaultPlugins, MaterialPlugin::<CustomMaterial>::default()))
14        .add_systems(Startup, setup)
15        .run();
16}
17
18fn setup(
19    mut commands: Commands,
20    mut meshes: ResMut<Assets<Mesh>>,
21    mut materials: ResMut<Assets<CustomMaterial>>,
22) {
23    // cube
24    commands.spawn((
25        Mesh3d(meshes.add(Cuboid::default())),
26        MeshMaterial3d(materials.add(CustomMaterial {})),
27        Transform::from_xyz(0.0, 0.5, 0.0),
28    ));
29
30    // camera
31    commands.spawn((
32        Camera3d::default(),
33        Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
34    ));
35}
36
37#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
38struct CustomMaterial {}
39
40impl Material for CustomMaterial {
41    fn fragment_shader() -> ShaderRef {
42        SHADER_ASSET_PATH.into()
43    }
44}