2013-01-06 10:34:31 +04:00
|
|
|
$input v_wpos, v_view, v_normal, v_tangent, v_bitangent, v_texcoord0
|
2012-10-28 08:34:41 +04:00
|
|
|
|
|
|
|
/*
|
2013-01-13 22:57:24 +04:00
|
|
|
* Copyright 2011-2013 Branimir Karadzic. All rights reserved.
|
2012-10-28 08:34:41 +04:00
|
|
|
* License: http://www.opensource.org/licenses/BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../common/common.sh"
|
|
|
|
|
|
|
|
SAMPLER2D(u_texColor, 0);
|
|
|
|
SAMPLER2D(u_texNormal, 1);
|
|
|
|
uniform vec4 u_lightPosRadius[4];
|
|
|
|
uniform vec4 u_lightRgbInnerR[4];
|
|
|
|
|
|
|
|
vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir)
|
|
|
|
{
|
|
|
|
float ndotl = dot(_normal, _lightDir);
|
|
|
|
vec3 reflected = _lightDir - 2.0*ndotl*_normal; // reflect(_lightDir, _normal);
|
|
|
|
float rdotv = dot(reflected, _viewDir);
|
|
|
|
return vec2(ndotl, rdotv);
|
|
|
|
}
|
|
|
|
|
|
|
|
float fresnel(float _ndotl, float _bias, float _pow)
|
|
|
|
{
|
|
|
|
float facing = (1.0 - _ndotl);
|
|
|
|
return max(_bias + (1.0 - _bias) * pow(facing, _pow), 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
vec4 lit(float _ndotl, float _rdotv, float _m)
|
|
|
|
{
|
|
|
|
float diff = max(0.0, _ndotl);
|
|
|
|
float spec = step(0.0, _ndotl) * max(0.0, _rdotv * _m);
|
|
|
|
return vec4(1.0, diff, spec, 1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
vec4 powRgba(vec4 _rgba, float _pow)
|
|
|
|
{
|
|
|
|
vec4 result;
|
|
|
|
result.xyz = pow(_rgba.xyz, vec3_splat(_pow) );
|
|
|
|
result.w = _rgba.w;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3 calcLight(int _idx, mat3 _tbn, vec3 _wpos, vec3 _normal, vec3 _view)
|
|
|
|
{
|
|
|
|
vec3 lp = u_lightPosRadius[_idx].xyz - _wpos;
|
|
|
|
float attn = 1.0 - smoothstep(u_lightRgbInnerR[_idx].w, 1.0, length(lp) / u_lightPosRadius[_idx].w);
|
|
|
|
vec3 lightDir = mul(_tbn, normalize(lp) );
|
|
|
|
vec2 bln = blinn(lightDir, _normal, _view);
|
|
|
|
vec4 lc = lit(bln.x, bln.y, 1.0);
|
|
|
|
vec3 rgb = u_lightRgbInnerR[_idx].xyz*max(0.0, saturate(lc.y) ) * attn;
|
|
|
|
return rgb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2013-01-06 10:34:31 +04:00
|
|
|
mat3 tbn = mat3(
|
|
|
|
normalize(v_tangent),
|
|
|
|
normalize(v_bitangent),
|
|
|
|
normalize(v_normal)
|
|
|
|
);
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2013-01-21 00:50:59 +04:00
|
|
|
vec3 normal;
|
|
|
|
normal.xy = texture2D(u_texNormal, v_texcoord0).xy * 2.0 - 1.0;
|
2013-01-21 00:47:29 +04:00
|
|
|
normal.z = sqrt(1.0 - dot(normal.xy, normal.xy) );
|
2013-01-06 10:34:31 +04:00
|
|
|
vec3 view = -normalize(v_view);
|
2012-10-28 08:34:41 +04:00
|
|
|
|
|
|
|
vec3 lightColor;
|
2013-01-06 10:34:31 +04:00
|
|
|
lightColor = calcLight(0, tbn, v_wpos, normal, view);
|
|
|
|
lightColor += calcLight(1, tbn, v_wpos, normal, view);
|
|
|
|
lightColor += calcLight(2, tbn, v_wpos, normal, view);
|
|
|
|
lightColor += calcLight(3, tbn, v_wpos, normal, view);
|
2012-10-28 08:34:41 +04:00
|
|
|
|
|
|
|
vec4 color = toLinear(texture2D(u_texColor, v_texcoord0) );
|
|
|
|
|
2013-01-03 09:59:06 +04:00
|
|
|
gl_FragColor.xyz = max(vec3_splat(0.05), lightColor.xyz)*color.xyz;
|
2012-10-28 08:34:41 +04:00
|
|
|
gl_FragColor.w = 1.0;
|
|
|
|
gl_FragColor = toGamma(gl_FragColor);
|
|
|
|
}
|