2013-10-18 01:55:43 -04:00
|
|
|
$input v_normal, v_view, v_texcoord0
|
|
|
|
|
|
|
|
/*
|
2014-01-13 17:45:18 -05:00
|
|
|
* Copyright 2013-2014 Dario Manesku. All rights reserved.
|
2013-10-18 01:55:43 -04:00
|
|
|
* License: http://www.opensource.org/licenses/BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../common/common.sh"
|
2013-10-20 16:07:18 -04:00
|
|
|
|
|
|
|
#define MAX_NUM_LIGHTS 5
|
|
|
|
|
2013-10-18 01:55:43 -04:00
|
|
|
uniform vec4 u_params;
|
2015-05-28 18:27:00 -04:00
|
|
|
uniform vec4 u_ambient;
|
|
|
|
uniform vec4 u_diffuse;
|
2013-10-18 01:55:43 -04:00
|
|
|
uniform vec4 u_color;
|
|
|
|
uniform vec4 u_specular_shininess;
|
2013-10-20 16:07:18 -04:00
|
|
|
uniform vec4 u_lightPosRadius[MAX_NUM_LIGHTS];
|
|
|
|
uniform vec4 u_lightRgbInnerR[MAX_NUM_LIGHTS];
|
2015-10-25 01:34:11 -04:00
|
|
|
SAMPLER2D(s_texColor, 0);
|
2013-10-18 01:55:43 -04:00
|
|
|
|
2015-04-30 07:45:29 -04:00
|
|
|
#define u_ambientPass u_params.x
|
|
|
|
#define u_lightingPass u_params.y
|
|
|
|
#define u_lightCount u_params.z
|
|
|
|
#define u_lightIndex u_params.w
|
|
|
|
#define u_specular u_specular_shininess.xyz
|
|
|
|
#define u_shininess u_specular_shininess.w
|
2013-10-18 01:55:43 -04:00
|
|
|
|
|
|
|
vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir)
|
|
|
|
{
|
|
|
|
float ndotl = dot(_normal, _lightDir);
|
|
|
|
vec3 reflected = 2.0*ndotl*_normal - _lightDir; // reflect(_lightDir, _normal);
|
|
|
|
float rdotv = dot(reflected, _viewDir);
|
|
|
|
return vec2(ndotl, rdotv);
|
|
|
|
}
|
|
|
|
|
|
|
|
vec4 lit(float _ndotl, float _rdotv, float _m)
|
|
|
|
{
|
|
|
|
float diff = max(0.0, _ndotl);
|
|
|
|
float spec = step(0.0, _ndotl) * pow(max(0.0, _rdotv), _m);
|
|
|
|
return vec4(1.0, diff, spec, 1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3 calcLight(int _idx, vec3 _view, vec3 _normal, vec3 _viewDir)
|
|
|
|
{
|
|
|
|
vec3 lightPos = mul(u_view, vec4(u_lightPosRadius[_idx].xyz, 1.0)).xyz;
|
|
|
|
vec3 toLight = lightPos - _view;
|
|
|
|
vec3 lightDir = normalize(toLight);
|
|
|
|
|
|
|
|
vec2 bln = blinn(lightDir, _normal, _viewDir);
|
|
|
|
vec4 lc = lit(bln.x, bln.y, u_shininess);
|
|
|
|
|
|
|
|
float dist = max(length(toLight), u_lightPosRadius[_idx].w);
|
2013-11-06 11:47:03 -05:00
|
|
|
float attn = 250.0 * pow(dist, -2.0);
|
2015-05-28 18:27:00 -04:00
|
|
|
vec3 rgb = (lc.y * u_diffuse.xyz + lc.z * u_specular) * u_lightRgbInnerR[_idx].rgb * attn;
|
2013-10-18 01:55:43 -04:00
|
|
|
|
|
|
|
return rgb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
vec3 normal = normalize(v_normal);
|
|
|
|
vec3 viewDir = -normalize(v_view);
|
|
|
|
|
2015-05-28 18:27:00 -04:00
|
|
|
vec3 ambientColor = u_ambient.xyz * u_ambientPass;
|
2013-10-18 01:55:43 -04:00
|
|
|
|
|
|
|
vec3 lightColor = vec3_splat(0.0);
|
2013-10-20 16:07:18 -04:00
|
|
|
for(int ii = 0; ii < MAX_NUM_LIGHTS; ++ii)
|
2013-10-18 01:55:43 -04:00
|
|
|
{
|
2013-10-20 16:07:18 -04:00
|
|
|
float condition = 0.0;
|
2015-04-30 07:45:29 -04:00
|
|
|
if (u_lightCount > 1.0) // Stencil Reflection Scene.
|
2013-10-20 16:07:18 -04:00
|
|
|
{
|
2015-04-30 07:45:29 -04:00
|
|
|
condition = 1.0 - step(u_lightCount, float(ii)); // True for every light up to u_lightCount.
|
2013-10-20 16:07:18 -04:00
|
|
|
}
|
2015-04-30 07:45:29 -04:00
|
|
|
else // Projection Shadows Scene.
|
2013-10-20 16:07:18 -04:00
|
|
|
{
|
2015-04-30 07:45:29 -04:00
|
|
|
condition = float(float(ii) == u_lightIndex); // True only for current light.
|
2013-10-20 16:07:18 -04:00
|
|
|
}
|
2015-04-30 07:45:29 -04:00
|
|
|
|
2013-10-20 16:07:18 -04:00
|
|
|
lightColor += calcLight(ii, v_view, normal, viewDir) * condition;
|
2013-10-18 01:55:43 -04:00
|
|
|
}
|
2015-04-30 07:45:29 -04:00
|
|
|
lightColor *= u_lightingPass;
|
2013-10-18 01:55:43 -04:00
|
|
|
|
2015-10-25 01:34:11 -04:00
|
|
|
vec3 color = toLinear(texture2D(s_texColor, v_texcoord0)).xyz;
|
2013-10-18 01:55:43 -04:00
|
|
|
|
|
|
|
vec3 ambient = toGamma(ambientColor * color);
|
|
|
|
vec3 diffuse = toGamma(lightColor * color);
|
|
|
|
gl_FragColor.xyz = clamp(ambient + diffuse, 0.0, 1.0);
|
|
|
|
|
2013-11-06 11:47:03 -05:00
|
|
|
gl_FragColor.w = u_color.w;
|
2013-10-18 01:55:43 -04:00
|
|
|
}
|