~vladh/peony

peony/src_shaders/standard.vert -rw-r--r-- 1.2 KiB
7fc9922bVlad-Stefan Harbuz changes shaders namespace into class a month ago
                                                                                
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
/*
  Peony Game Engine
  Copyright (C) 2020 Vlad-Stefan Harbuz <vlad@vladh.net>
  All rights reserved.
*/

uniform mat4 model_matrix;
uniform mat3 model_normal_matrix;
uniform mat4 bone_matrices[MAX_N_BONES];

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 tex_coords;
layout (location = 3) in uvec4 bone_idxs;
layout (location = 4) in vec4 bone_weights;

out BLOCK {
  vec3 world_position;
  vec3 normal;
  vec2 tex_coords;
} vs_out;

void main() {
  mat4 bone_transform = mat4(0.0);
  bool did_find_bones = false;

  for (int idx = 0; idx < 4; idx++) {
    if (bone_weights[idx] > 0) {
      did_find_bones = true;
      bone_transform += bone_matrices[bone_idxs[idx]] * bone_weights[idx];
    }
  }

  if (did_find_bones) {
    vs_out.world_position = vec3(model_matrix * bone_transform * vec4(position, 1.0));
    vs_out.normal = normalize(
      model_normal_matrix *
      mat3(transpose(inverse(bone_transform))) *
      normal
    );
  } else {
    vs_out.world_position = vec3(model_matrix * vec4(position, 1.0));
    vs_out.normal = normalize(model_normal_matrix * normal);
  }
  vs_out.tex_coords = tex_coords;
  gl_Position = projection * view * vec4(vs_out.world_position, 1.0);
}