summaryrefslogtreecommitdiff
path: root/data/shaders/pbr-frag.glsl
blob: ce252ab7df3bb90585b25de2870572b31a9c4695 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// -*- mode: c -*-

struct Material {
  vec3 baseColorFactor;
  bool baseColorTextureEnabled;
  int baseColorTexcoord;
  float metallicFactor;
  float roughnessFactor;
  vec3 normalFactor;
  bool normalTextureEnabled;
  int normalTexcoord;
  vec3 occlusionFactor;
  bool occlusionTextureEnabled;
  int occlusionTexcoord;
  vec3 emissiveFactor;
  bool emissiveTextureEnabled;
  int emissiveTexcoord;
  float alphaCutoff;
};

#ifdef GLSL120
attribute vec2 fragTexcoord0;
attribute vec2 fragTexcoord1
attribute vec4 fragColor0;
#else
in vec2 fragTexcoord0;
in vec2 fragTexcoord1;
in vec4 fragColor0;
#endif

#ifdef GLSL330
out vec4 fragColor;
#endif

uniform Material material;
uniform bool vertexColored;
uniform sampler2D baseColorTexture;
uniform sampler2D normalTexture;
uniform sampler2D occlusionTexture;
uniform sampler2D emissiveTexture;

vec4 sampleTexture(sampler2D tex, bool enabled, int texcoord, vec3 factor, vec4 defaultColor) {
  if(enabled && texcoord == 0) {
    return texture(tex, fragTexcoord0) * vec4(factor, 1.0);
  } else if(enabled && texcoord == 1) {
    return texture(tex, fragTexcoord1) * vec4(factor, 1.0);
  } else {
    return defaultColor;
  }
}

void main(void) {
  vec4 finalColor = sampleTexture(baseColorTexture,
                                  material.baseColorTextureEnabled,
                                  material.baseColorTexcoord,
                                  material.baseColorFactor,
                                  vec4(1.0, 1.0, 1.0, 1.0));

  // Mix in vertex color, if present.
  if(vertexColored) {
    finalColor *= fragColor0;
  }

  // TODO: Actually apply PBR calculations.
  finalColor += sampleTexture(normalTexture,
                              material.normalTextureEnabled,
                              material.normalTexcoord,
                              material.normalFactor,
                              vec4(0.0, 0.0, 0.0, 0.0)) * 0.1;
  finalColor += sampleTexture(occlusionTexture,
                              material.occlusionTextureEnabled,
                              material.occlusionTexcoord,
                              material.occlusionFactor,
                              vec4(0.0, 0.0, 0.0, 0.0)) * 0.1;
  finalColor += sampleTexture(emissiveTexture,
                              material.emissiveTextureEnabled,
                              material.emissiveTexcoord,
                              material.emissiveFactor,
                              vec4(0.0, 0.0, 0.0, 0.0));
#ifdef GLSL330
  fragColor = finalColor;
#else
  gl_FragColor = finalColor;
#endif
}