// -*- 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 }