shader_material_bindless/
shader_material_bindless.rs1use bevy::prelude::*;
4use bevy::render::render_resource::{AsBindGroup, ShaderType};
5use bevy::shader::ShaderRef;
6
7const SHADER_ASSET_PATH: &str = "shaders/bindless_material.wgsl";
8
9#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
14#[uniform(0, BindlessMaterialUniform, binding_array(10))]
15#[bindless(limit(4))]
16struct BindlessMaterial {
17 color: LinearRgba,
18 #[texture(1)]
21 #[sampler(2)]
22 color_texture: Option<Handle<Image>>,
23}
24
25#[derive(ShaderType)]
27struct BindlessMaterialUniform {
28 color: LinearRgba,
29}
30
31impl<'a> From<&'a BindlessMaterial> for BindlessMaterialUniform {
32 fn from(material: &'a BindlessMaterial) -> Self {
33 BindlessMaterialUniform {
34 color: material.color,
35 }
36 }
37}
38
39fn main() {
41 App::new()
42 .add_plugins((
43 DefaultPlugins,
44 MaterialPlugin::<BindlessMaterial>::default(),
45 ))
46 .add_systems(Startup, setup)
47 .run();
48}
49
50fn setup(
52 mut commands: Commands,
53 mut meshes: ResMut<Assets<Mesh>>,
54 mut materials: ResMut<Assets<BindlessMaterial>>,
55 asset_server: Res<AssetServer>,
56) {
57 commands.spawn((
59 Mesh3d(meshes.add(Cuboid::default())),
60 MeshMaterial3d(materials.add(BindlessMaterial {
61 color: LinearRgba::BLUE,
62 color_texture: Some(asset_server.load("branding/bevy_logo_dark.png")),
63 })),
64 Transform::from_xyz(-2.0, 0.5, 0.0),
65 ));
66
67 commands.spawn((
69 Mesh3d(meshes.add(Cylinder::default())),
70 MeshMaterial3d(materials.add(BindlessMaterial {
71 color: LinearRgba::RED,
72 color_texture: Some(asset_server.load("branding/bevy_logo_light.png")),
73 })),
74 Transform::from_xyz(2.0, 0.5, 0.0),
75 ));
76
77 commands.spawn((
79 Camera3d::default(),
80 Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
81 ));
82}
83
84impl Material for BindlessMaterial {
85 fn fragment_shader() -> ShaderRef {
86 SHADER_ASSET_PATH.into()
87 }
88}