Shows that multiple instances of a cube are automatically instanced in one draw call Try running this example in a graphics profiler and all the cubes should be only a single draw call. Also demonstrates how to use MeshTag
to use external data in a custom material.
use ;
const SHADER_ASSET_PATH: &str = "shaders/automatic_instancing.wgsl";
/// Sets up an instanced grid of cubes, where each cube is colored based on an image that is
/// sampled in the vertex shader. The cubes are then animated in a spiral pattern.
///
/// This example demonstrates one use of automatic instancing and how to use `MeshTag` to use
/// external data in a custom material. For example, here we use the "index" of each cube to
/// determine the texel coordinate to sample from the image in the shader.
// Animate the transform
// 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 texture: texture_2d<f32>;
(2) (1) var texture_sampler: sampler;
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;
// Lookup the tag for the given mesh
let tag = mesh_functions::;
var world_from_local = mesh_functions::;
out.world_position = mesh_functions::;
out.clip_position = ;
let tex_dim = ;
// Find the texel coordinate as derived from the tag
let texel_coord = vec2<u32>(tag % tex_dim.x, tag / tex_dim.x);
out.color = ;
return out;
}
fn -> (0) vec4<f32> {
return mesh.color;
}```