#import bevy_pbr::{
mesh_functions,
forward_io::{Vertex , VertexOutput},
mesh_view_bindings::view,
}
const FLAGS_TEXTURE_BIT: u32 = 1u;
struct ShaderModel {
color: vec4<f32>,
flags: u32,
};
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var<uniform> model: ShaderModel;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(2) var texture_sampler: sampler;
@vertex
fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;
// Use vertex.instance_index instead of vertex.instance_index to work around a wgpu dx12 bug.
// See https://github.com/gfx-rs/naga/issues/2416 .
var world_from_local = mesh_functions::get_world_from_local(vertex.instance_index);
#ifdef VERTEX_NORMALS
out.world_normal = mesh_functions::mesh_normal_local_to_world(
vertex.normal,
// Use vertex.instance_index instead of vertex.instance_index to work around a wgpu dx12 bug.
// See https://github.com/gfx-rs/naga/issues/2416
vertex.instance_index
);
#endif
#ifdef VERTEX_POSITIONS
out.world_position = mesh_functions::mesh_position_local_to_world(world_from_local, vec4<f32>(vertex.position, 1.0));
out.position = view.clip_from_world * vec4(out.world_position.xyz, 1.0);
#endif
#ifdef VERTEX_UVS_A
out.uv = vertex.uv;
#endif
#ifdef VERTEX_UVS_B
out.uv_b = vertex.uv_b;
#endif
#ifdef VERTEX_TANGENTS
out.world_tangent = mesh_functions::mesh_tangent_local_to_world(
world_from_local,
vertex.tangent,
// Use vertex.instance_index instead of vertex.instance_index to work around a wgpu dx12 bug.
// See https://github.com/gfx-rs/naga/issues/2416
vertex.instance_index
);
#endif
#ifdef VERTEX_COLORS
out.color = vertex.color;
#endif
#ifdef VERTEX_OUTPUT_INSTANCE_INDEX
// Use vertex.instance_index instead of vertex.instance_index to work around a wgpu dx12 bug.
// See https://github.com/gfx-rs/naga/issues/2416
out.instance_index = vertex.instance_index;
#endif
return out;
}
@fragment
fn fragment(
mesh: VertexOutput,
) -> @location(0) vec4<f32> {
var output_color: vec4<f32> = model.color;
#ifdef VERTEX_COLORS
output_color = output_color * mesh.color;
#endif
if ((model.flags & FLAGS_TEXTURE_BIT) != 0u) {
output_color = output_color * textureSample(texture, texture_sampler, mesh.uv);
}
return output_color;
}