
# Objective Because of mesh preprocessing, users cannot rely on `@builtin(instance_index)` in order to reference external data, as the instance index is not stable, either from frame to frame or relative to the total spawn order of mesh instances. ## Solution Add a user supplied mesh index that can be used for referencing external data when drawing instanced meshes. Closes #13373 ## Testing Benchmarked `many_cubes` showing no difference in total frame time. ## Showcase https://github.com/user-attachments/assets/80620147-aafc-4d9d-a8ee-e2149f7c8f3b --------- Co-authored-by: IceSentry <IceSentry@users.noreply.github.com>
36 lines
993 B
WebGPU Shading Language
36 lines
993 B
WebGPU Shading Language
#import bevy_pbr::{
|
|
mesh_functions,
|
|
view_transformations::position_world_to_clip
|
|
}
|
|
|
|
@group(2) @binding(0) var<storage, read> colors: array<vec4<f32>, 5>;
|
|
|
|
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
|
|
fn vertex(vertex: Vertex) -> VertexOutput {
|
|
var out: VertexOutput;
|
|
let tag = mesh_functions::get_tag(vertex.instance_index);
|
|
var world_from_local = mesh_functions::get_world_from_local(vertex.instance_index);
|
|
out.world_position = mesh_functions::mesh_position_local_to_world(world_from_local, vec4(vertex.position, 1.0));
|
|
out.clip_position = position_world_to_clip(out.world_position.xyz);
|
|
|
|
out.color = colors[tag];
|
|
return out;
|
|
}
|
|
|
|
@fragment
|
|
fn fragment(
|
|
mesh: VertexOutput,
|
|
) -> @location(0) vec4<f32> {
|
|
return mesh.color;
|
|
} |