//! This example demonstrates how to use a storage buffer with `AsBindGroup` in a custom material. use std::array; use bevy::{ prelude::*, reflect::TypePath, render::{ render_resource::{AsBindGroup, ShaderRef}, storage::ShaderStorageBuffer, }, }; const SHADER_ASSET_PATH: &str = "shaders/storage_buffer.wgsl"; fn main() { App::new() .add_plugins((DefaultPlugins, MaterialPlugin::::default())) .add_systems(Startup, setup) .add_systems(Update, update) .run(); } /// set up a simple 3D scene fn setup( mut commands: Commands, mut meshes: ResMut>, mut buffers: ResMut>, mut materials: ResMut>, ) { // Example data for the storage buffer let color_data: Vec<[f32; 4]> = vec![ [1.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0], [1.0, 1.0, 0.0, 1.0], [0.0, 1.0, 1.0, 1.0], ]; let colors = buffers.add(ShaderStorageBuffer::from(color_data)); // Create the custom material with the storage buffer let material_handles: [Handle; 5] = array::from_fn(|color_id| { materials.add(CustomMaterial { colors: colors.clone(), color_id: color_id as u32, }) }); commands.insert_resource(CustomMaterialHandles(material_handles.clone())); // Spawn cubes with the custom material let mut current_color_id = 0; for i in -6..=6 { for j in -3..=3 { commands.spawn(( Mesh3d(meshes.add(Cuboid::from_size(Vec3::splat(0.3)))), MeshMaterial3d(material_handles[current_color_id % 5].clone()), Transform::from_xyz(i as f32, j as f32, 0.0), )); current_color_id += 1; } } // Camera commands.spawn(( Camera3d::default(), Transform::from_xyz(0.0, 0.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y), )); } // Update the material color by time fn update( time: Res