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
}
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var texture_sampler: sampler;
struct Vertex {
@builtin(instance_index) instance_index: u32,
@location(0) position: vec3<f32>,
};
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) world_position: vec4<f32>,
@location(1) color: vec4<f32>,
};
@vertex
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 = ;
out.color = ;
return out;
}
@fragment
return mesh.color;
}```