summaryrefslogtreecommitdiff
path: root/data/shaders/pbr-frag.glsl
blob: b025bb79a13ccb68fd88d8c66bc3c3e9bdec52db (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// -*- mode: c -*-

// Heavily based upon: https://learnopengl.com/PBR/Lighting

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

struct Light {
  bool enabled;
  int type;
  vec3 position;
  vec3 direction;
  vec4 color;
  float cutOff;
};

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

#ifdef GLSL330
out vec4 fragColor;
#endif

uniform Material material;
uniform Light lights[4];
uniform vec4 ambientLightColor;
uniform bool vertexColored;
uniform vec3 cameraPosition;
uniform sampler2D baseColorTexture;
uniform sampler2D metallicRoughnessTexture;
uniform sampler2D normalTexture;
uniform sampler2D occlusionTexture;
uniform sampler2D emissiveTexture;

const float PI = 3.14159265359;

vec3 fresnelSchlick(float cosTheta, vec3 F0)
{
    return F0 + (1.0 - F0) * pow(max(1.0 - cosTheta, 0.0), 5.0);
}

float DistributionGGX(vec3 N, vec3 H, float roughness)
{
    float a      = roughness*roughness;
    float a2     = a*a;
    float NdotH  = max(dot(N, H), 0.0);
    float NdotH2 = NdotH*NdotH;

    float num   = a2;
    float denom = (NdotH2 * (a2 - 1.0) + 1.0);
    denom = PI * denom * denom;

    return num / denom;
}

float GeometrySchlickGGX(float NdotV, float roughness)
{
    float r = (roughness + 1.0);
    float k = (r*r) / 8.0;

    float num   = NdotV;
    float denom = NdotV * (1.0 - k) + k;

    return num / denom;
}

float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
    float NdotV = max(dot(N, V), 0.0);
    float NdotL = max(dot(N, L), 0.0);
    float ggx2  = GeometrySchlickGGX(NdotV, roughness);
    float ggx1  = GeometrySchlickGGX(NdotL, roughness);

    return ggx1 * ggx2;
}

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

vec2 texcoord(int i) {
  if(i == 0) {
    return fragTexcoord0;
  } else {
    return fragTexcoord1;
  }
}

vec4 sRGBtoLinear(vec4 srgb) {
  return vec4(pow(srgb.r, 2.2),
              pow(srgb.g, 2.2),
              pow(srgb.b, 2.2),
              srgb.a);
}

float materialMetallic() {
  float m = material.metallicFactor;

  if(material.metallicRoughnessTextureEnabled) {
    m *= texture(metallicRoughnessTexture,
                 texcoord(material.metallicRoughnessTexcoord)).b;
  }

  return m;
}

float materialRoughness() {
  float r = material.roughnessFactor;

  if(material.metallicRoughnessTextureEnabled) {
    r *= texture(metallicRoughnessTexture,
                 texcoord(material.metallicRoughnessTexcoord)).g;
  }

  return r;
}

vec4 materialAlbedo() {
  vec4 color = vec4(0.0, 0.0, 1.0, 1.0);

  if(material.baseColorTextureEnabled) {
    vec4 texColor = texture(baseColorTexture,
                            texcoord(material.baseColorTexcoord));
    color = sRGBtoLinear(texColor);
  }

  color *= vec4(material.baseColorFactor, 1.0);

  if(vertexColored) {
    color *= fragColor0;
  }

  return color;
}

vec4 materialEmissive() {
  vec4 color = vec4(0.0);

  if(material.emissiveTextureEnabled) {
    vec4 texColor = texture(emissiveTexture,
                            texcoord(material.emissiveTexcoord));
    color = sRGBtoLinear(texColor);
  }

  return color * vec4(material.emissiveFactor, 1.0);
}

vec3 materialOcclusion() {
  if(material.occlusionTextureEnabled) {
    return vec3(texture(occlusionTexture,
                        texcoord(material.occlusionTexcoord)).r);
  } else {
    return vec3(1.0);
  }
}

vec3 materialNormal() {
  vec3 normal;

  if(material.normalTextureEnabled) {
    normal = texture(normalTexture,
                     texcoord(material.normalTexcoord)).rgb * 2.0 - 1.0;
  } else {
    normal = fragNormal;
  }

  return normalize(normal);
}

void main(void) {
  float metallic = materialMetallic();
  float roughness = materialRoughness();
  vec3 N = materialNormal();
  vec3 V = normalize(cameraPosition - fragWorldPos);
  vec4 rawAlbedo = materialAlbedo();
  vec3 albedo = rawAlbedo.rgb;
  vec3 ao = materialOcclusion();
  vec3 F0 = mix(vec3(0.4), albedo, metallic);

  // reflectance equation
  vec3 Lo = vec3(0.0);
  for(int i = 0; i < 4; ++i)
  {
    Light light = lights[i];

    if(!light.enabled) {
      continue;
    }

    vec3 L;
    vec3 radiance;

    // calculate per-light radiance
    if(light.type == 0) { // point light
      L = normalize(light.position - fragWorldPos);
      float distance = length(light.position - fragWorldPos);
      float attenuation = 1.0 / (distance * distance);
      radiance = light.color.rgb * attenuation;
    } else if(light.type == 1) { // directional light
      L = normalize(-light.direction);
      radiance = light.color.rgb;
    } else if(light.type == 2) { // spotlight
      L = normalize(light.position - fragWorldPos);
      float theta = dot(L, normalize(-light.direction));

      if(theta > light.cutOff) {
        float distance = length(light.position - fragWorldPos);
        float attenuation = 1.0 / (distance * distance);
        radiance = light.color.rgb * attenuation;
      } else {
        continue;
      }
    }

    vec3 H = normalize(V + L);

    // cook-torrance brdf
    float NDF = DistributionGGX(N, H, roughness);
    float G = GeometrySmith(N, V, L, roughness);
    vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);

    vec3 kS = F;
    vec3 kD = vec3(1.0) - kS;
    kD *= 1.0 - metallic;

    vec3 numerator = NDF * G * F;
    float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
    vec3 specular = numerator / max(denominator, 0.001);

    // add to outgoing radiance Lo
    float NdotL = max(dot(N, L), 0.0);
    Lo += (kD * albedo / PI + specular) * radiance * NdotL;
  }

  // Add emissive color.
  Lo += materialEmissive().rgb;

  // Apply ambient lighting.
  vec3 ambient = ambientLightColor.xyz * albedo * ao;
  vec3 color = ambient + Lo;

  // Apply HDR.
  color = color / (color + vec3(1.0));
  color = pow(color, vec3(1.0 / 2.2));

  vec4 finalColor = applyAlpha(vec4(color, rawAlbedo.a));

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