This example is running in WebGL2 and should work in most browsers. You can check the WebGPU examples here.
//! This example demonstrates how to use a storage buffer with `AsBindGroup` in a custom material.
use ;
const SHADER_ASSET_PATH: &str = "shaders/storage_buffer.wgsl";
/// set up a simple 3D scene
// Update the material color by time
// Holds a handle to the custom material
;
// This struct defines the data that will be passed to your shader
#import bevy_pbr::{
mesh_functions,
view_transformations::position_world_to_clip
}
(2) (0) var<storage, read> colors: array<vec4<f32>, 5>;
struct Vertex {
(instance_index) instance_index: u32,
(0) position: vec3<f32>,
};
struct VertexOutput {
(position) clip_position: vec4<f32>,
(0) world_position: vec4<f32>,
(1) color: vec4<f32>,
};
fn -> VertexOutput {
var out: VertexOutput;
var world_from_local = mesh_functions::;
out.world_position = mesh_functions::;
out.clip_position = ;
// We have 5 colors in the storage buffer, but potentially many instances of the mesh, so
// we use the instance index to select a color from the storage buffer.
out.color = colors[vertex.instance_index % 5];
return out;
}
fn -> (0) vec4<f32> {
return mesh.color;
}```