bgfx/examples/15-shadowmaps-simple/fs_smsimple_draw.sc

97 lines
3.2 KiB
Python
Raw Normal View History

$input v_view, v_normal, v_shadowcoord
/*
2014-01-13 17:45:18 -05:00
* Copyright 2013-2014 Dario Manesku. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include "../common/common.sh"
uniform vec4 u_lightPos;
SAMPLER2D(u_shadowMap, 4);
vec2 lit(vec3 _ld, vec3 _n, vec3 _vd, float _exp)
{
//diff
float ndotl = dot(_n, _ld);
//spec
vec3 r = 2.0*ndotl*_n - _ld; //reflect(_ld, _n);
float rdotv = dot(r, _vd);
float spec = step(0.0, ndotl) * pow(max(0.0, rdotv), _exp) * (2.0 + _exp)/8.0;
return max(vec2(ndotl, spec), 0.0);
}
float hardShadow(sampler2D _sampler, vec4 _shadowCoord, float _bias)
{
vec2 texCoord = _shadowCoord.xy/_shadowCoord.w;
bool outside = any(greaterThan(texCoord, vec2_splat(1.0)))
|| any(lessThan (texCoord, vec2_splat(0.0)))
;
if (outside)
{
return 1.0;
}
float receiver = (_shadowCoord.z-_bias)/_shadowCoord.w;
float occluder = unpackRgbaToFloat(texture2D(_sampler, texCoord) );
float visibility = step(receiver, occluder);
return visibility;
}
float PCF(sampler2D _sampler, vec4 _shadowCoord, float _bias, vec2 _texelSize)
{
float result = 0.0;
vec2 offset = _texelSize * _shadowCoord.w;
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-1.5, -1.5) * offset, 0.0, 0.0), _bias);
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-1.5, -0.5) * offset, 0.0, 0.0), _bias);
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-1.5, 0.5) * offset, 0.0, 0.0), _bias);
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-1.5, 1.5) * offset, 0.0, 0.0), _bias);
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-0.5, -1.5) * offset, 0.0, 0.0), _bias);
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-0.5, -0.5) * offset, 0.0, 0.0), _bias);
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-0.5, 0.5) * offset, 0.0, 0.0), _bias);
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-0.5, 1.5) * offset, 0.0, 0.0), _bias);
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(0.5, -1.5) * offset, 0.0, 0.0), _bias);
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(0.5, -0.5) * offset, 0.0, 0.0), _bias);
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(0.5, 0.5) * offset, 0.0, 0.0), _bias);
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(0.5, 1.5) * offset, 0.0, 0.0), _bias);
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(1.5, -1.5) * offset, 0.0, 0.0), _bias);
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(1.5, -0.5) * offset, 0.0, 0.0), _bias);
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(1.5, 0.5) * offset, 0.0, 0.0), _bias);
result += hardShadow(_sampler, _shadowCoord + vec4(vec2(1.5, 1.5) * offset, 0.0, 0.0), _bias);
return result / 16.0;
}
void main()
{
const float shadowMapBias = 0.005;
const vec3 color = vec3_splat(1.0);
vec3 v = v_view;
vec3 vd = -normalize(v);
vec3 n = v_normal;
vec3 l = u_lightPos.xyz;
vec3 ld = -normalize(l);
vec2 lc = lit(ld, n, vd, 1.0);
vec2 texelSize = vec2_splat(1.0/512.0);
float visibility = PCF(u_shadowMap, v_shadowcoord, shadowMapBias, texelSize);
vec3 ambient = 0.1 * color;
vec3 brdf = (lc.x + lc.y) * color * visibility;
vec3 final = toGamma(abs(ambient + brdf) );
gl_FragColor.xyz = final;
gl_FragColor.w = 1.0;
}