summaryrefslogtreecommitdiff
path: root/data/shaders/pbr-frag.glsl
blob: 4c569fc30571c5f5bcec28e8311f8966b56a6de8 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// -*- 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;
  int alphaMode;
  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;
  }
}

vec4 applyAlpha(vec4 color) {
  // Apply alpha mode.
  if(material.alphaMode == 0) { // opaque
    return vec4(color.xyz, 1.0);
  } else if(material.alphaMode == 1) { // mask
    if(color.a >= material.alphaCutoff) {
      return vec4(color.xyz, 1.0);
    } else {
      discard;
    }
  } else if(material.alphaMode == 2) { // blend
    if(color.a <= 0.005) {
      discard;
    } else {
      return color;
    }
  }
}

void main(void) {
  vec4 finalColor = sampleTexture(baseColorTexture,
                                  material.baseColorTextureEnabled,
                                  material.baseColorTexcoord,
                                  material.baseColorFactor,
                                  vec4(0.0, 0.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));
  finalColor = applyAlpha(finalColor);

#ifdef GLSL330
  fragColor = finalColor;
#else
  gl_FragColor = finalColor;
#endif
}