2016-12-24 15:03:37 +00:00
|
|
|
#ifdef GL_ES
|
|
|
|
// Set default precision to medium
|
|
|
|
precision mediump int;
|
|
|
|
precision mediump float;
|
|
|
|
#endif
|
|
|
|
|
2017-01-17 19:18:04 +00:00
|
|
|
uniform mat3 n_matrix;
|
2017-01-19 16:57:50 +00:00
|
|
|
uniform vec3 cameraPosition;
|
|
|
|
|
2016-12-24 15:03:37 +00:00
|
|
|
uniform sampler2D texture;
|
2017-02-05 19:10:05 +00:00
|
|
|
uniform sampler2D secondTexture;
|
2017-01-19 16:57:50 +00:00
|
|
|
uniform float materialShininess;
|
|
|
|
uniform vec3 materialSpecularColor;
|
|
|
|
|
|
|
|
uniform bool b_transparent;
|
2017-01-22 13:37:06 +00:00
|
|
|
uniform bool b_specular;
|
2017-02-05 19:10:05 +00:00
|
|
|
uniform bool b_normalmap;
|
2017-01-19 16:57:50 +00:00
|
|
|
uniform bool b_light;
|
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-02-05 15:57:12 +00:00
|
|
|
attribute vec3 a_polyNorm;
|
|
|
|
attribute vec3 a_polyTan;
|
|
|
|
attribute vec3 a_polyBiTan;
|
|
|
|
|
2017-01-18 16:01:43 +00:00
|
|
|
varying vec2 v_surfaceUV;
|
|
|
|
varying vec3 v_surfacePosition;
|
|
|
|
varying vec3 v_surfaceNormal;
|
2016-12-24 15:03:37 +00:00
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2017-01-18 16:01:43 +00:00
|
|
|
if(b_light)
|
2017-01-17 19:18:04 +00:00
|
|
|
{
|
2017-01-19 16:57:50 +00:00
|
|
|
// some values
|
2017-02-05 19:10:05 +00:00
|
|
|
mat3 tbn = transpose(mat3(a_polyTan, a_polyBiTan, a_polyNorm));
|
|
|
|
|
|
|
|
vec3 finalNormal = normalize(n_matrix * v_surfaceNormal);
|
|
|
|
|
|
|
|
// if(b_normalmap)
|
|
|
|
// {
|
|
|
|
// finalNormal = texture2D(secondTexture, v_surfaceUV).rgb;
|
|
|
|
// finalNormal = normalize(finalNormal * 2.0 -1.0)
|
|
|
|
// }
|
2017-01-21 14:22:43 +00:00
|
|
|
|
2017-01-19 16:57:50 +00:00
|
|
|
vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));
|
2017-01-19 19:15:00 +00:00
|
|
|
surfaceColor.rgb = pow(surfaceColor.rgb, vec3(2.2));
|
2017-01-21 14:22:43 +00:00
|
|
|
|
|
|
|
vec3 surfaceToLight;
|
|
|
|
float attenuation;
|
|
|
|
// directional light
|
2017-01-26 17:25:24 +00:00
|
|
|
if(light.position.w == 0.0f)
|
2017-01-21 14:22:43 +00:00
|
|
|
{
|
|
|
|
surfaceToLight = normalize(light.position.xyz);
|
|
|
|
}
|
|
|
|
// point light
|
|
|
|
else
|
|
|
|
{
|
|
|
|
surfaceToLight = normalize(light.position.xyz - v_surfacePosition);
|
|
|
|
}
|
2017-01-23 15:09:06 +00:00
|
|
|
|
|
|
|
float distanceToLight = length(light.position.xyz - v_surfacePosition);
|
|
|
|
attenuation = 1.0 / (1.0 + light.attenuationFactor * pow(distanceToLight, 2));
|
2017-01-21 14:22:43 +00:00
|
|
|
|
2017-01-19 16:57:50 +00:00
|
|
|
vec3 surfaceToCamera = normalize(cameraPosition - v_surfacePosition);
|
2017-01-17 10:32:06 +00:00
|
|
|
|
2017-01-19 16:57:50 +00:00
|
|
|
// ambient
|
|
|
|
vec3 ambient = light.ambientCoefficient * surfaceColor.rgb * light.intensities;
|
|
|
|
|
|
|
|
// diffuse
|
2017-02-05 19:10:05 +00:00
|
|
|
float diffuseCoefficient = max(0.0, dot(finalNormal, surfaceToLight));
|
2017-01-19 16:57:50 +00:00
|
|
|
vec3 diffuse = diffuseCoefficient * surfaceColor.rgb * light.intensities;
|
|
|
|
|
|
|
|
// specular
|
|
|
|
float specularCoefficient = 0.0;
|
|
|
|
if(diffuseCoefficient > 0.0)
|
2017-02-05 19:10:05 +00:00
|
|
|
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, finalNormal))), materialShininess);
|
2017-01-26 17:25:24 +00:00
|
|
|
vec3 specColor;
|
2017-01-22 13:37:06 +00:00
|
|
|
if(b_specular)
|
2017-01-26 17:25:24 +00:00
|
|
|
specColor = vec3(surfaceColor.a);
|
|
|
|
else
|
|
|
|
specColor = materialSpecularColor;
|
|
|
|
vec3 specular = specularCoefficient * specColor * light.intensities;
|
2017-01-18 16:01:43 +00:00
|
|
|
|
2017-01-19 16:57:50 +00:00
|
|
|
// linear color before gamma correction)
|
|
|
|
vec3 linearColor = ambient + attenuation * (diffuse + specular);
|
|
|
|
|
|
|
|
// final color after gama correction
|
|
|
|
vec3 gamma = vec3(1.0/2.2);
|
2017-01-22 13:37:06 +00:00
|
|
|
if(!b_transparent)
|
|
|
|
surfaceColor.a = 1.0f;
|
2017-01-19 16:57:50 +00:00
|
|
|
gl_FragColor = vec4(pow(linearColor, gamma), surfaceColor.a);
|
2017-01-17 19:18:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-19 16:57:50 +00:00
|
|
|
vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));
|
|
|
|
if(!b_transparent)
|
|
|
|
surfaceColor.a = 1.0f;
|
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
|
|
|
}
|