summaryrefslogtreecommitdiff
path: root/data
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2021-05-07 21:17:59 -0400
committerDavid Thompson <dthompson2@worcester.edu>2021-05-07 21:17:59 -0400
commit271a2bb9775dd8a4a6e5a232f27af86ca7ffd83e (patch)
treedebf24d4cf81d50cf60fea9619063fc6cd1a4094 /data
parentfeb3c58215960f2bac66922e8d1a1f8e32b7ff95 (diff)
model: Improve PBR material handling for glTF models.
Diffstat (limited to 'data')
-rw-r--r--data/shaders/pbr-frag.glsl73
1 files changed, 58 insertions, 15 deletions
diff --git a/data/shaders/pbr-frag.glsl b/data/shaders/pbr-frag.glsl
index 813f9a9..ce252ab 100644
--- a/data/shaders/pbr-frag.glsl
+++ b/data/shaders/pbr-frag.glsl
@@ -1,5 +1,23 @@
// -*- 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
@@ -14,26 +32,51 @@ in vec4 fragColor0;
out vec4 fragColor;
#endif
-uniform bool textured0;
-uniform bool textured1;
+uniform Material material;
uniform bool vertexColored;
-uniform vec3 baseColorFactor;
-uniform sampler2D texture0;
-uniform sampler2D texture1;
+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 = vec4(baseColorFactor, 1.0);
- // Vertex coloring.
+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;
}
- // Texture sampling.
- if(textured0) {
- finalColor *= texture(texture0, fragTexcoord0);
- }
- if(textured1) {
- finalColor += texture(texture1, fragTexcoord1);
- }
+
+ // 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