-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
skinning.wgsl
68 lines (59 loc) · 1.86 KB
/
skinning.wgsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#define_import_path bevy_pbr::skinning
#import bevy_pbr::mesh_types::SkinnedMesh
#ifdef SKINNED
@group(1) @binding(1) var<uniform> joint_matrices: SkinnedMesh;
// An array of matrices specifying the joint positions from the previous frame.
//
// This is used for motion vector computation.
//
// If this is the first frame, or we're otherwise prevented from using data from
// the previous frame, this is simply the same as `joint_matrices` above.
@group(1) @binding(6) var<uniform> prev_joint_matrices: SkinnedMesh;
fn skin_model(
indexes: vec4<u32>,
weights: vec4<f32>,
) -> mat4x4<f32> {
return weights.x * joint_matrices.data[indexes.x]
+ weights.y * joint_matrices.data[indexes.y]
+ weights.z * joint_matrices.data[indexes.z]
+ weights.w * joint_matrices.data[indexes.w];
}
// Returns the skinned position of a vertex with the given weights from the
// previous frame.
//
// This is used for motion vector computation.
fn skin_prev_model(
indexes: vec4<u32>,
weights: vec4<f32>,
) -> mat4x4<f32> {
return weights.x * prev_joint_matrices.data[indexes.x]
+ weights.y * prev_joint_matrices.data[indexes.y]
+ weights.z * prev_joint_matrices.data[indexes.z]
+ weights.w * prev_joint_matrices.data[indexes.w];
}
fn inverse_transpose_3x3m(in: mat3x3<f32>) -> mat3x3<f32> {
let x = cross(in[1], in[2]);
let y = cross(in[2], in[0]);
let z = cross(in[0], in[1]);
let det = dot(in[2], z);
return mat3x3<f32>(
x / det,
y / det,
z / det
);
}
fn skin_normals(
world_from_local: mat4x4<f32>,
normal: vec3<f32>,
) -> vec3<f32> {
return normalize(
inverse_transpose_3x3m(
mat3x3<f32>(
world_from_local[0].xyz,
world_from_local[1].xyz,
world_from_local[2].xyz
)
) * normal
);
}
#endif