2016-07-25 01:39:50 +03:00
|
|
|
$input v_lightCenterScale, v_color0
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2016 Joseph Cherlin. All rights reserved.
|
|
|
|
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../common/common.sh"
|
|
|
|
|
|
|
|
SAMPLER2D(s_normal, 0); // Normal output from gbuffer
|
|
|
|
SAMPLER2D(s_depth, 1); // Depth output from gbuffer
|
|
|
|
|
|
|
|
uniform mat4 u_invMvp;
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2020-12-28 22:24:49 +03:00
|
|
|
#if BGFX_SHADER_LANGUAGE_HLSL && (BGFX_SHADER_LANGUAGE_HLSL < 400)
|
2016-07-25 02:07:09 +03:00
|
|
|
vec2 texCoord = gl_FragCoord.xy * u_viewTexel.xy + u_viewTexel.xy * vec2_splat(0.5);
|
2016-07-25 01:39:50 +03:00
|
|
|
#else
|
2016-07-25 02:07:09 +03:00
|
|
|
vec2 texCoord = gl_FragCoord.xy * u_viewTexel.xy;
|
2016-07-25 01:39:50 +03:00
|
|
|
#endif
|
|
|
|
|
2016-07-25 02:07:09 +03:00
|
|
|
// Get world position
|
|
|
|
float deviceDepth = texture2D(s_depth, texCoord).x;
|
|
|
|
float depth = toClipSpaceDepth(deviceDepth);
|
2016-07-25 01:39:50 +03:00
|
|
|
|
2016-07-25 02:07:09 +03:00
|
|
|
vec3 clip = vec3(texCoord * 2.0 - 1.0, depth);
|
2021-04-07 05:46:26 +03:00
|
|
|
#if !BGFX_SHADER_LANGUAGE_GLSL
|
2016-07-25 02:07:09 +03:00
|
|
|
clip.y = -clip.y;
|
2021-04-07 05:46:26 +03:00
|
|
|
#endif // !BGFX_SHADER_LANGUAGE_GLSL
|
2016-07-25 02:07:09 +03:00
|
|
|
vec3 wpos = clipToWorld(u_invMvp, clip);
|
|
|
|
|
|
|
|
// Get normal from its map, and decompress
|
|
|
|
vec3 n = texture2D(s_normal, texCoord).xyz*2.0-1.0;
|
|
|
|
|
|
|
|
// Do lighting
|
|
|
|
vec3 pointToLight = v_lightCenterScale.xyz-wpos;
|
|
|
|
float lightLen = sqrt(dot(pointToLight, pointToLight));
|
|
|
|
|
|
|
|
float lightFalloff;
|
|
|
|
|
|
|
|
if (lightLen > v_lightCenterScale.w)
|
|
|
|
lightFalloff = 0.0;
|
|
|
|
else
|
|
|
|
lightFalloff = 1.0-(lightLen/v_lightCenterScale.w); // Linear falloff for light (could use dist sq if you want)
|
|
|
|
|
|
|
|
vec3 l = normalize(pointToLight)*lightFalloff;
|
|
|
|
|
|
|
|
gl_FragColor.xyz = v_color0.xyz * max(0.0, dot(n,l));
|
|
|
|
|
|
|
|
gl_FragColor.w = 1.0;
|
2016-07-25 01:39:50 +03:00
|
|
|
}
|