1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//! This example tests that all texture dimensions are supported by
//! `FallbackImage`.
//!
//! When running this example, you should expect to see a window that only draws
//! the clear color. The test material does not shade any geometry; this example
//! only tests that the images are initialized and bound so that the app does
//! not panic.
use bevy::{
    prelude::*,
    reflect::TypePath,
    render::render_resource::{AsBindGroup, ShaderRef},
};

/// This example uses a shader source file from the assets subdirectory
const SHADER_ASSET_PATH: &str = "shaders/fallback_image_test.wgsl";

fn main() {
    App::new()
        .add_plugins((
            DefaultPlugins,
            MaterialPlugin::<FallbackTestMaterial>::default(),
        ))
        .add_systems(Startup, setup)
        .run();
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<FallbackTestMaterial>>,
) {
    commands.spawn(MaterialMeshBundle {
        mesh: meshes.add(Cuboid::default()),
        material: materials.add(FallbackTestMaterial {
            image_1d: None,
            image_2d: None,
            image_2d_array: None,
            image_cube: None,
            image_cube_array: None,
            image_3d: None,
        }),
        ..default()
    });
    commands.spawn(Camera3dBundle {
        transform: Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::new(1.5, 0.0, 0.0), Vec3::Y),
        ..default()
    });
}

#[derive(AsBindGroup, Debug, Clone, Asset, TypePath)]
struct FallbackTestMaterial {
    #[texture(0, dimension = "1d")]
    #[sampler(1)]
    image_1d: Option<Handle<Image>>,

    #[texture(2, dimension = "2d")]
    #[sampler(3)]
    image_2d: Option<Handle<Image>>,

    #[texture(4, dimension = "2d_array")]
    #[sampler(5)]
    image_2d_array: Option<Handle<Image>>,

    #[texture(6, dimension = "cube")]
    #[sampler(7)]
    image_cube: Option<Handle<Image>>,

    #[texture(8, dimension = "cube_array")]
    #[sampler(9)]
    image_cube_array: Option<Handle<Image>>,

    #[texture(10, dimension = "3d")]
    #[sampler(11)]
    image_3d: Option<Handle<Image>>,
}

impl Material for FallbackTestMaterial {
    fn fragment_shader() -> ShaderRef {
        SHADER_ASSET_PATH.into()
    }
}