docs/bevy/examples/shader/animate_shader.rs 1.2 K · 44 lines · raw · history

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
4 use 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
9 const SHADER_ASSET_PATH: &str = "shaders/animate_shader.wesl";
10
11 fn main() {
12 App::new()
13 .add_plugins((DefaultPlugins, MaterialPlugin::<CustomMaterial>::default()))
14 .add_systems(Startup, setup)
15 .run();
16 }
17
18 fn 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)]
38 struct CustomMaterial {}
39
40 impl Material for CustomMaterial {
41 fn fragment_shader() -> ShaderRef {
42 SHADER_ASSET_PATH.into()
43 }
44 }