Skip to main content

shader_material_2d_bindless/
shader_material_2d_bindless.rs

1//! Demonstrates bindless materials in 2D.
2
3use bevy::{
4    prelude::*,
5    reflect::TypePath,
6    render::render_resource::{AsBindGroup, ShaderType},
7    shader::ShaderRef,
8    sprite_render::{AlphaMode2d, Material2d, Material2dPlugin},
9};
10
11/// This example uses a shader source file from the `assets` subdirectory.
12const SHADER_ASSET_PATH: &str = "shaders/custom_material_2d_bindless.wesl";
13
14fn main() {
15    App::new()
16        .add_plugins((
17            DefaultPlugins,
18            Material2dPlugin::<BindlessMaterial>::default(),
19        ))
20        .add_systems(Startup, setup)
21        .run();
22}
23
24// Setup a simple 2D scene.
25fn setup(
26    mut commands: Commands,
27    mut meshes: ResMut<Assets<Mesh>>,
28    mut materials: ResMut<Assets<BindlessMaterial>>,
29    asset_server: Res<AssetServer>,
30) {
31    // Spawn the camera.
32    commands.spawn(Camera2d);
33
34    // Create the shared assets that the bindless material will use.
35    let dark_icon = asset_server.load("branding/bevy_logo_dark.png");
36    let light_icon = asset_server.load("branding/bevy_logo_light.png");
37    let mesh = meshes.add(Rectangle::default());
38
39    // Spawn the quads.
40    for (offset, color, image) in [
41        (vec2(-1.0, -1.0), LinearRgba::RED, dark_icon.clone()),
42        (
43            vec2(1.0, -1.0),
44            LinearRgba::new(1.0, 1.0, 0.0, 1.0),
45            light_icon.clone(),
46        ),
47        (vec2(-1.0, 1.0), LinearRgba::GREEN, dark_icon.clone()),
48        (vec2(1.0, 1.0), LinearRgba::BLUE, light_icon.clone()),
49    ] {
50        commands.spawn((
51            Mesh2d(mesh.clone()),
52            MeshMaterial2d(materials.add(BindlessMaterial {
53                color,
54                color_texture: Some(image),
55            })),
56            Transform::default()
57                .with_scale(vec3(128.0, 32.0, 1.0))
58                .with_translation((offset * 128.0).extend(0.0)),
59        ));
60    }
61}
62
63/// The CPU-side structure that's passed to the shader.
64#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
65#[uniform(0, BindlessMaterialUniform, binding_array(10))]
66#[bindless]
67struct BindlessMaterial {
68    color: LinearRgba,
69    #[texture(1)]
70    #[sampler(2)]
71    color_texture: Option<Handle<Image>>,
72}
73
74/// The GPU-side structure that's passed to the shader.
75#[derive(ShaderType)]
76struct BindlessMaterialUniform {
77    color: LinearRgba,
78}
79
80// The conversion function that Bevy calls to convert from the CPU-side material
81// structure to the GPU-side material structure.
82impl<'a> From<&'a BindlessMaterial> for BindlessMaterialUniform {
83    fn from(material: &'a BindlessMaterial) -> Self {
84        BindlessMaterialUniform {
85            color: material.color,
86        }
87    }
88}
89
90// The `Material2d`` trait is very configurable, but comes with sensible defaults
91// for all methods.
92//
93// You only need to implement functions for features that need non-default
94// behavior. See the `Material2d` API documentation for details.
95impl Material2d for BindlessMaterial {
96    fn fragment_shader() -> ShaderRef {
97        SHADER_ASSET_PATH.into()
98    }
99
100    fn alpha_mode(&self) -> AlphaMode2d {
101        AlphaMode2d::Mask(0.5)
102    }
103}