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-01-19 16:57:50 +00:00
|
|
|
uniform float materialShininess;
|
|
|
|
uniform vec3 materialSpecularColor;
|
|
|
|
|
|
|
|
uniform bool b_transparent;
|
|
|
|
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-17 16:48:54 +00:00
|
|
|
vec3 position;
|
|
|
|
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;
|
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-01-18 16:01:43 +00:00
|
|
|
vec3 normalWorld = normalize(n_matrix * v_surfaceNormal);
|
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-19 16:57:50 +00:00
|
|
|
if(!b_transparent)
|
|
|
|
surfaceColor.a = 1.0f;
|
2017-01-18 16:01:43 +00:00
|
|
|
vec3 surfaceToLight = normalize(light.position - v_surfacePosition);
|
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-01-18 16:01:43 +00:00
|
|
|
float diffuseCoefficient = max(0.0, dot(normalWorld, 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)
|
|
|
|
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normalWorld))), materialShininess);
|
|
|
|
vec3 specular = specularCoefficient * materialSpecularColor * light.intensities;
|
2017-01-18 16:01:43 +00:00
|
|
|
|
2017-01-19 16:57:50 +00:00
|
|
|
// attenuation
|
|
|
|
float distanceToLight = length(light.position - v_surfacePosition);
|
|
|
|
float attenuation = 1.0 / (1.0 + light.attenuationFactor * pow(distanceToLight, 2));
|
|
|
|
|
|
|
|
// linear color before gamma correction)
|
|
|
|
vec3 linearColor = ambient + attenuation * (diffuse + specular);
|
|
|
|
|
|
|
|
// final color after gama correction
|
|
|
|
vec3 gamma = vec3(1.0/2.2);
|
|
|
|
gl_FragColor = vec4(pow(linearColor, gamma), surfaceColor.a);
|
2017-01-19 19:15:00 +00:00
|
|
|
//gl_FragColor = vec4(linearColor, 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
|
|
|
}
|