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#define_import_path bevy_pbr::skinning
3#import bevy_pbr::mesh_types::SkinnedMesh
4#import bevy_pbr::mesh_bindings::mesh
6#ifdef SKINNED
8#ifdef SKINS_USE_UNIFORM_BUFFERS
9@group(2) @binding(1) var<uniform> joint_matrices: SkinnedMesh;
10#else // SKINS_USE_UNIFORM_BUFFERS
11@group(2) @binding(1) var<storage> joint_matrices: array<mat4x4<f32>>;
12#endif // SKINS_USE_UNIFORM_BUFFERS
14// An array of matrices specifying the joint positions from the previous frame.
15//
16// This is used for motion vector computation.
17//
18// If this is the first frame, or we're otherwise prevented from using data from
19// the previous frame, this is simply the same as `joint_matrices` above.
20#ifdef SKINS_USE_UNIFORM_BUFFERS
21@group(2) @binding(6) var<uniform> prev_joint_matrices: SkinnedMesh;
22#else // SKINS_USE_UNIFORM_BUFFERS
23@group(2) @binding(6) var<storage> prev_joint_matrices: array<mat4x4<f32>>;
24#endif // SKINS_USE_UNIFORM_BUFFERS
26fn skin_model(
27 indexes: vec4<u32>,
28 weights: vec4<f32>,
29 instance_index: u32,
30) -> mat4x4<f32> {
31#ifdef SKINS_USE_UNIFORM_BUFFERS
32 return weights.x * joint_matrices.data[indexes.x]
33 + weights.y * joint_matrices.data[indexes.y]
34 + weights.z * joint_matrices.data[indexes.z]
35 + weights.w * joint_matrices.data[indexes.w];
36#else // SKINS_USE_UNIFORM_BUFFERS
37 var skin_index = mesh[instance_index].current_skin_index;
38 return weights.x * joint_matrices[skin_index + indexes.x]
39 + weights.y * joint_matrices[skin_index + indexes.y]
40 + weights.z * joint_matrices[skin_index + indexes.z]
41 + weights.w * joint_matrices[skin_index + indexes.w];
42#endif // SKINS_USE_UNIFORM_BUFFERS
45// Returns the skinned position of a vertex with the given weights from the
46// previous frame.
47//
48// This is used for motion vector computation.
49fn skin_prev_model(
50 indexes: vec4<u32>,
51 weights: vec4<f32>,
52 instance_index: u32,
53) -> mat4x4<f32> {
54#ifdef SKINS_USE_UNIFORM_BUFFERS
55 return weights.x * prev_joint_matrices.data[indexes.x]
56 + weights.y * prev_joint_matrices.data[indexes.y]
57 + weights.z * prev_joint_matrices.data[indexes.z]
58 + weights.w * prev_joint_matrices.data[indexes.w];
59#else // SKINS_USE_UNIFORM_BUFFERS
60 let skin_index = mesh[instance_index].current_skin_index;
61 return weights.x * prev_joint_matrices[skin_index + indexes.x]
62 + weights.y * prev_joint_matrices[skin_index + indexes.y]
63 + weights.z * prev_joint_matrices[skin_index + indexes.z]
64 + weights.w * prev_joint_matrices[skin_index + indexes.w];
65#endif // SKINS_USE_UNIFORM_BUFFERS
68fn inverse_transpose_3x3m(in: mat3x3<f32>) -> mat3x3<f32> {
69 let x = cross(in[1], in[2]);
70 let y = cross(in[2], in[0]);
71 let z = cross(in[0], in[1]);
72 let det = dot(in[2], z);
73 return mat3x3<f32>(
74 x / det,
75 y / det,
76 z / det
77 );
80fn skin_normals(
81 world_from_local: mat4x4<f32>,
82 normal: vec3<f32>,
83) -> vec3<f32> {
84 return normalize(
85 inverse_transpose_3x3m(
86 mat3x3<f32>(
87 world_from_local[0].xyz,
88 world_from_local[1].xyz,
89 world_from_local[2].xyz
90 )
91 ) * normal
92 );
95#endif
Contents