2019-02-17 18:50:26 +03:00
|
|
|
/*
|
2019-02-18 08:48:20 +03:00
|
|
|
* Copyright 2011-2019 Branimir Karadzic. All rights reserved.
|
2019-02-17 18:50:26 +03:00
|
|
|
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../common/common.sh"
|
|
|
|
|
|
|
|
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(vec3 _wpos, vec3 _normal, vec3 _view, vec3 _lightPos, float _lightRadius, vec3 _lightRgb, float _lightInner)
|
|
|
|
{
|
|
|
|
vec3 lp = _lightPos - _wpos;
|
|
|
|
float attn = 1.0 - smoothstep(_lightInner, 1.0, length(lp) / _lightRadius);
|
|
|
|
vec3 lightDir = normalize(lp);
|
|
|
|
vec2 bln = blinn(lightDir, _normal, _view);
|
|
|
|
vec4 lc = lit(bln.x, bln.y, 1.0);
|
|
|
|
vec3 rgb = _lightRgb * saturate(lc.y) * attn;
|
|
|
|
return rgb;
|
|
|
|
}
|
|
|
|
|
|
|
|
float toClipSpaceDepth(float _depthTextureZ)
|
|
|
|
{
|
2019-02-19 00:40:41 +03:00
|
|
|
#if BGFX_SHADER_LANGUAGE_GLSL
|
2019-02-17 18:50:26 +03:00
|
|
|
return _depthTextureZ * 2.0 - 1.0;
|
2019-02-19 00:40:41 +03:00
|
|
|
#else
|
|
|
|
return _depthTextureZ;
|
|
|
|
#endif // BGFX_SHADER_LANGUAGE_GLSL
|
2019-02-17 18:50:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
vec3 clipToWorld(mat4 _invViewProj, vec3 _clipPos)
|
|
|
|
{
|
|
|
|
vec4 wpos = mul(_invViewProj, vec4(_clipPos, 1.0) );
|
|
|
|
return wpos.xyz / wpos.w;
|
|
|
|
}
|
|
|
|
|