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;
|
2016-12-24 15:03:37 +00:00
|
|
|
uniform sampler2D texture;
|
|
|
|
|
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-17 10:32:06 +00:00
|
|
|
} light;
|
|
|
|
|
2017-01-15 14:51:12 +00:00
|
|
|
uniform bool b_transparent;
|
2017-01-17 19:18:04 +00:00
|
|
|
uniform bool b_light;
|
2017-01-15 14:51:12 +00:00
|
|
|
|
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-17 19:18:04 +00:00
|
|
|
// variables
|
2017-01-18 16:01:43 +00:00
|
|
|
vec3 diffuse;
|
2017-01-17 10:32:06 +00:00
|
|
|
|
2017-01-18 16:01:43 +00:00
|
|
|
// get fragment color from texture
|
|
|
|
vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));
|
|
|
|
|
|
|
|
// if not transparent, ignore alpha value and set it to 1
|
|
|
|
if(!b_transparent)
|
|
|
|
surfaceColor.a = 1.0f;
|
|
|
|
|
|
|
|
if(b_light)
|
2017-01-17 19:18:04 +00:00
|
|
|
{
|
|
|
|
// calculate normals in worldspace
|
2017-01-18 16:01:43 +00:00
|
|
|
vec3 normalWorld = normalize(n_matrix * v_surfaceNormal);
|
2017-01-17 16:48:54 +00:00
|
|
|
|
2017-01-18 16:01:43 +00:00
|
|
|
//get the surface - light vector (cause this is a point light)
|
|
|
|
vec3 surfaceToLight = normalize(light.position - v_surfacePosition);
|
2017-01-17 10:32:06 +00:00
|
|
|
|
2017-01-17 19:18:04 +00:00
|
|
|
// calculate the brightness depending on the angle
|
2017-01-18 16:01:43 +00:00
|
|
|
float diffuseCoefficient = max(0.0, dot(normalWorld, surfaceToLight));
|
|
|
|
|
|
|
|
// result diffuse color
|
|
|
|
diffuse = diffuseCoefficient * surfaceColor.rgb * light.intensities;
|
2017-01-17 19:18:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-18 16:01:43 +00:00
|
|
|
diffuse = surfaceColor.rgb;
|
2017-01-17 19:18:04 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 16:01:43 +00:00
|
|
|
// put all together
|
|
|
|
gl_FragColor = vec4(diffuse, surfaceColor.a);
|
2016-12-24 15:03:37 +00:00
|
|
|
}
|