From 271a2bb9775dd8a4a6e5a232f27af86ca7ffd83e Mon Sep 17 00:00:00 2001 From: David Thompson Date: Fri, 7 May 2021 21:17:59 -0400 Subject: model: Improve PBR material handling for glTF models. --- data/shaders/pbr-frag.glsl | 73 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 15 deletions(-) (limited to 'data/shaders/pbr-frag.glsl') 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 -- cgit v1.2.3