2017-02-06 13:53:05 +00:00
|
|
|
#version 450
|
2016-12-24 15:03:37 +00:00
|
|
|
#ifdef GL_ES
|
|
|
|
// Set default precision to medium
|
|
|
|
precision mediump int;
|
|
|
|
precision mediump float;
|
|
|
|
#endif
|
|
|
|
|
2017-02-06 13:53:05 +00:00
|
|
|
uniform mat3 normalMatrix;
|
2017-01-19 16:57:50 +00:00
|
|
|
uniform vec3 cameraPosition;
|
|
|
|
|
2017-02-06 13:53:05 +00:00
|
|
|
uniform sampler2D tx0;
|
|
|
|
uniform sampler2D tx1;
|
2017-01-19 16:57:50 +00:00
|
|
|
|
2017-02-06 13:53:05 +00:00
|
|
|
uniform struct Material {
|
|
|
|
float shininess;
|
|
|
|
vec3 specularColor;
|
|
|
|
bool isTransparent;
|
|
|
|
bool hasSpecularmap;
|
|
|
|
bool hasNormalmap;
|
|
|
|
bool isGlow;
|
|
|
|
} material;
|
|
|
|
|
|
|
|
uniform bool useLight;
|
2016-12-24 15:03:37 +00:00
|
|
|
|
2017-01-17 10:32:06 +00:00
|
|
|
uniform struct Light {
|
2017-01-21 14:22:43 +00:00
|
|
|
vec4 position;
|
2017-01-17 16:48:54 +00:00
|
|
|
vec3 intensities;
|
2017-01-19 16:57:50 +00:00
|
|
|
float attenuationFactor;
|
|
|
|
float ambientCoefficient;
|
2017-01-17 10:32:06 +00:00
|
|
|
} light;
|
|
|
|
|
2017-01-18 16:01:43 +00:00
|
|
|
varying vec2 v_surfaceUV;
|
|
|
|
varying vec3 v_surfacePosition;
|
|
|
|
varying vec3 v_surfaceNormal;
|
2017-02-06 13:53:05 +00:00
|
|
|
varying vec3 v_polyNorm;
|
|
|
|
varying vec3 v_polyTan;
|
|
|
|
varying vec3 v_polyBiTan;
|
2016-12-24 15:03:37 +00:00
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2017-02-06 13:53:05 +00:00
|
|
|
if(useLight && !material.isGlow)
|
2017-01-17 19:18:04 +00:00
|
|
|
{
|
2017-02-06 13:53:05 +00:00
|
|
|
// get the color and undo gamma correction
|
|
|
|
vec4 surfaceColor = vec4(texture2D(tx0, v_surfaceUV));
|
|
|
|
surfaceColor.rgb = pow(surfaceColor.rgb, vec3(2.2));
|
2017-02-05 19:10:05 +00:00
|
|
|
|
2017-02-06 13:53:05 +00:00
|
|
|
// attenutation depending on the distance to the light
|
|
|
|
float distanceToLight = length(light.position.xyz - v_surfacePosition);
|
|
|
|
float attenuation = 1.0 / (1.0 + light.attenuationFactor * pow(distanceToLight, 2));
|
2017-02-05 19:10:05 +00:00
|
|
|
|
2017-02-06 13:53:05 +00:00
|
|
|
// normal vector
|
|
|
|
vec3 normal = normalize(normalMatrix * v_surfaceNormal);
|
2017-01-21 14:22:43 +00:00
|
|
|
|
2017-02-06 13:53:05 +00:00
|
|
|
// direction from surface to light depending on the light type
|
2017-01-21 14:22:43 +00:00
|
|
|
vec3 surfaceToLight;
|
2017-02-06 13:53:05 +00:00
|
|
|
if(light.position.w == 0.0) // directional light
|
2017-01-21 14:22:43 +00:00
|
|
|
surfaceToLight = normalize(light.position.xyz);
|
2017-02-06 13:53:05 +00:00
|
|
|
else // point light
|
2017-01-21 14:22:43 +00:00
|
|
|
surfaceToLight = normalize(light.position.xyz - v_surfacePosition);
|
|
|
|
|
2017-02-06 13:53:05 +00:00
|
|
|
// direction from surface to camera
|
2017-01-19 16:57:50 +00:00
|
|
|
vec3 surfaceToCamera = normalize(cameraPosition - v_surfacePosition);
|
2017-01-17 10:32:06 +00:00
|
|
|
|
2017-02-06 13:53:05 +00:00
|
|
|
// adjust the values if material has normal map
|
|
|
|
if(material.hasNormalmap)
|
|
|
|
{
|
|
|
|
vec3 surfaceTangent = normalize(normalMatrix * v_polyTan);
|
|
|
|
vec3 surfaceBitangent = normalize(normalMatrix * -v_polyBiTan);
|
|
|
|
vec3 surfaceNormal = normalize(normalMatrix * v_surfaceNormal);
|
|
|
|
mat3 tbn = transpose(mat3(surfaceTangent, surfaceBitangent, surfaceNormal));
|
|
|
|
normal = texture2D(tx1, v_surfaceUV).rgb;
|
|
|
|
normal = normalize(normal * 2.0 -1.0);
|
|
|
|
surfaceToLight = tbn * surfaceToLight;
|
|
|
|
surfaceToCamera = tbn * surfaceToCamera;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ambient component
|
|
|
|
|
2017-01-19 16:57:50 +00:00
|
|
|
vec3 ambient = light.ambientCoefficient * surfaceColor.rgb * light.intensities;
|
|
|
|
|
2017-02-06 13:53:05 +00:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// diffuse component
|
|
|
|
|
|
|
|
float diffuseCoefficient = max(0.0, dot(normal, surfaceToLight));
|
2017-01-19 16:57:50 +00:00
|
|
|
vec3 diffuse = diffuseCoefficient * surfaceColor.rgb * light.intensities;
|
|
|
|
|
2017-02-06 13:53:05 +00:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// specular component
|
|
|
|
|
2017-01-19 16:57:50 +00:00
|
|
|
float specularCoefficient = 0.0;
|
|
|
|
if(diffuseCoefficient > 0.0)
|
2017-02-06 13:53:05 +00:00
|
|
|
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normal))), material.shininess);
|
|
|
|
|
|
|
|
float specularWeight = 1;
|
|
|
|
if(material.hasSpecularmap)
|
|
|
|
specularWeight = surfaceColor.a;
|
|
|
|
vec3 specColor = specularWeight * material.specularColor;
|
|
|
|
|
2017-01-26 17:25:24 +00:00
|
|
|
vec3 specular = specularCoefficient * specColor * light.intensities;
|
2017-01-18 16:01:43 +00:00
|
|
|
|
2017-02-06 13:53:05 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// linear color before gamma correction
|
2017-01-19 16:57:50 +00:00
|
|
|
vec3 linearColor = ambient + attenuation * (diffuse + specular);
|
|
|
|
|
2017-02-06 13:53:05 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// gama correction
|
2017-01-19 16:57:50 +00:00
|
|
|
vec3 gamma = vec3(1.0/2.2);
|
2017-02-06 13:53:05 +00:00
|
|
|
|
|
|
|
if(!material.isTransparent)
|
|
|
|
surfaceColor.a = 1.0;
|
|
|
|
|
2017-01-19 16:57:50 +00:00
|
|
|
gl_FragColor = vec4(pow(linearColor, gamma), surfaceColor.a);
|
2017-01-17 19:18:04 +00:00
|
|
|
}
|
2017-02-06 13:53:05 +00:00
|
|
|
// don't use light
|
2017-01-17 19:18:04 +00:00
|
|
|
else
|
|
|
|
{
|
2017-02-06 13:53:05 +00:00
|
|
|
vec4 surfaceColor = vec4(texture2D(tx0, v_surfaceUV));
|
|
|
|
|
|
|
|
if(!material.isTransparent)
|
|
|
|
surfaceColor.a = 1.0;
|
2017-01-17 19:18:04 +00:00
|
|
|
|
2017-01-19 16:57:50 +00:00
|
|
|
gl_FragColor = surfaceColor;
|
|
|
|
}
|
2016-12-24 15:03:37 +00:00
|
|
|
}
|