docs/bevy/examples/shader/fallback_image.rs 2.2 K · 78 lines · raw · history

1 //! This example tests that all texture dimensions are supported by
2 //! `FallbackImage`.
3 //!
4 //! When running this example, you should expect to see a window that only draws
5 //! the clear color. The test material does not shade any geometry; this example
6 //! only tests that the images are initialized and bound so that the app does
7 //! not panic.
8 use bevy::{
9 prelude::*, reflect::TypePath, render::render_resource::AsBindGroup, shader::ShaderRef,
10 };
11
12 /// This example uses a shader source file from the assets subdirectory
13 const SHADER_ASSET_PATH: &str = "shaders/fallback_image_test.wesl";
14
15 fn main() {
16 App::new()
17 .add_plugins((
18 DefaultPlugins,
19 MaterialPlugin::<FallbackTestMaterial>::default(),
20 ))
21 .add_systems(Startup, setup)
22 .run();
23 }
24
25 fn setup(
26 mut commands: Commands,
27 mut meshes: ResMut<Assets<Mesh>>,
28 mut materials: ResMut<Assets<FallbackTestMaterial>>,
29 ) {
30 commands.spawn((
31 Mesh3d(meshes.add(Cuboid::default())),
32 MeshMaterial3d(materials.add(FallbackTestMaterial {
33 image_1d: None,
34 image_2d: None,
35 image_2d_array: None,
36 image_cube: None,
37 image_cube_array: None,
38 image_3d: None,
39 })),
40 ));
41 commands.spawn((
42 Camera3d::default(),
43 Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::new(1.5, 0.0, 0.0), Vec3::Y),
44 ));
45 }
46
47 #[derive(AsBindGroup, Debug, Clone, Asset, TypePath)]
48 struct FallbackTestMaterial {
49 #[texture(0, dimension = "1d")]
50 #[sampler(1)]
51 image_1d: Option<Handle<Image>>,
52
53 #[texture(2, dimension = "2d")]
54 #[sampler(3)]
55 image_2d: Option<Handle<Image>>,
56
57 #[texture(4, dimension = "2d_array")]
58 #[sampler(5)]
59 image_2d_array: Option<Handle<Image>>,
60
61 #[texture(6, dimension = "cube")]
62 #[sampler(7)]
63 image_cube: Option<Handle<Image>>,
64
65 #[texture(8, dimension = "cube_array")]
66 #[sampler(9)]
67 image_cube_array: Option<Handle<Image>>,
68
69 #[texture(10, dimension = "3d")]
70 #[sampler(11)]
71 image_3d: Option<Handle<Image>>,
72 }
73
74 impl Material for FallbackTestMaterial {
75 fn fragment_shader() -> ShaderRef {
76 SHADER_ASSET_PATH.into()
77 }
78 }