Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1#import bevy_pbr::{
2 mesh_bindings::mesh,
3 mesh_functions,
4 skinning,
5 morph::{morph_position, morph_normal, morph_tangent},
6 forward_io::{Vertex, VertexOutput},
7 view_transformations::position_world_to_clip,
8}
10#ifdef MORPH_TARGETS
11// The instance_index parameter must match vertex_in.instance_index. This is a work around for a wgpu dx12 bug.
12// See https://github.com/gfx-rs/naga/issues/2416
13fn morph_vertex(vertex_in: Vertex, instance_index: u32) -> Vertex {
14 var vertex = vertex_in;
15 let first_vertex = mesh[instance_index].first_vertex_index;
16 let vertex_index = vertex.index - first_vertex;
18 let weight_count = bevy_pbr::morph::layer_count(instance_index);
19 for (var i: u32 = 0u; i < weight_count; i ++) {
20 let weight = bevy_pbr::morph::weight_at(i, instance_index);
21 if weight == 0.0 {
22 continue;
23 }
24 vertex.position += weight * morph_position(vertex_index, i, instance_index);
25#ifdef VERTEX_NORMALS
26 vertex.normal += weight * morph_normal(vertex_index, i, instance_index);
27#endif
28#ifdef VERTEX_TANGENTS
29 vertex.tangent += vec4(weight * morph_tangent(vertex_index, i, instance_index), 0.0);
30#endif
31 }
32 return vertex;
34#endif
36@vertex
37fn vertex(vertex_no_morph: Vertex) -> VertexOutput {
38 var out: VertexOutput;
40#ifdef MORPH_TARGETS
41 var vertex = morph_vertex(vertex_no_morph, vertex_no_morph.instance_index);
42#else
43 var vertex = vertex_no_morph;
44#endif
46 let mesh_world_from_local = mesh_functions::get_world_from_local(vertex_no_morph.instance_index);
48#ifdef SKINNED
49 // Use vertex_no_morph.instance_index instead of vertex.instance_index to work around a wgpu dx12 bug.
50 // See https://github.com/gfx-rs/naga/issues/2416 .
51 var world_from_local = skinning::skin_model(
52 vertex.joint_indices,
53 vertex.joint_weights,
54 vertex_no_morph.instance_index
55 );
56#else
57 var world_from_local = mesh_world_from_local;
58#endif
60#ifdef VERTEX_NORMALS
61#ifdef SKINNED
62 out.world_normal = skinning::skin_normals(world_from_local, vertex.normal);
63#else
64 out.world_normal = mesh_functions::mesh_normal_local_to_world(
65 vertex.normal,
66 // Use vertex_no_morph.instance_index instead of vertex.instance_index to work around a wgpu dx12 bug.
67 // See https://github.com/gfx-rs/naga/issues/2416
68 vertex_no_morph.instance_index
69 );
70#endif
71#endif
73#ifdef VERTEX_POSITIONS
74 out.world_position = mesh_functions::mesh_position_local_to_world(world_from_local, vec4<f32>(vertex.position, 1.0));
75 out.position = position_world_to_clip(out.world_position.xyz);
76#endif
78#ifdef VERTEX_UVS_A
79 out.uv = vertex.uv;
80#endif
81#ifdef VERTEX_UVS_B
82 out.uv_b = vertex.uv_b;
83#endif
85#ifdef VERTEX_TANGENTS
86 out.world_tangent = mesh_functions::mesh_tangent_local_to_world(
87 world_from_local,
88 vertex.tangent,
89 // Use vertex_no_morph.instance_index instead of vertex.instance_index to work around a wgpu dx12 bug.
90 // See https://github.com/gfx-rs/naga/issues/2416
91 vertex_no_morph.instance_index
92 );
93#endif
95#ifdef VERTEX_COLORS
96 out.color = vertex.color;
97#endif
99#ifdef VERTEX_OUTPUT_INSTANCE_INDEX
100 // Use vertex_no_morph.instance_index instead of vertex.instance_index to work around a wgpu dx12 bug.
101 // See https://github.com/gfx-rs/naga/issues/2416
102 out.instance_index = vertex_no_morph.instance_index;
103#endif
105#ifdef VISIBILITY_RANGE_DITHER
106 out.visibility_range_dither = mesh_functions::get_visibility_range_dither_level(
107 vertex_no_morph.instance_index, mesh_world_from_local[3]);
108#endif
110 return out;
113@fragment
114fn fragment(
115 mesh: VertexOutput,
116) -> @location(0) vec4<f32> {
117#ifdef VERTEX_COLORS
118 return mesh.color;
119#else
120 return vec4<f32>(1.0, 0.0, 1.0, 1.0);
121#endif
Contents