stk-code_catmod/data/shaders/pointlight.frag

61 lines
1.6 KiB
GLSL
Raw Normal View History

uniform sampler2D ntex;
2014-01-27 13:10:34 -05:00
uniform sampler2D dtex;
uniform float spec;
2014-03-10 18:29:37 -04:00
uniform vec2 screen;
#ifdef UBO_DISABLED
uniform mat4 ViewMatrix;
uniform mat4 ProjectionMatrix;
uniform mat4 InverseViewMatrix;
uniform mat4 InverseProjectionMatrix;
#else
layout (std140) uniform MatrixesData
{
mat4 ViewMatrix;
mat4 ProjectionMatrix;
mat4 InverseViewMatrix;
mat4 InverseProjectionMatrix;
mat4 ShadowViewProjMatrixes[4];
};
#endif
2014-03-10 18:29:37 -04:00
flat in vec3 center;
flat in float energy;
flat in vec3 col;
2014-05-12 13:36:45 -04:00
flat in float radius;
out vec4 Diffuse;
out vec4 Specular;
vec3 DecodeNormal(vec2 n);
2014-04-06 17:57:54 -04:00
vec3 getSpecular(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
2014-04-06 15:23:09 -04:00
void main()
{
vec2 texc = gl_FragCoord.xy / screen;
float z = texture(dtex, texc).x;
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, texc).xy - 1.));
2014-04-06 16:19:10 -04:00
float roughness = texture(ntex, texc).z;
2014-04-06 15:23:09 -04:00
vec4 xpos = getPosFromUVDepth(vec3(texc, z), InverseProjectionMatrix);
2014-04-06 15:23:09 -04:00
vec3 eyedir = -normalize(xpos.xyz);
vec4 pseudocenter = ViewMatrix * vec4(center.xyz, 1.0);
pseudocenter /= pseudocenter.w;
vec3 light_pos = pseudocenter.xyz;
vec3 light_col = col.xyz;
float d = distance(light_pos, xpos.xyz);
float att = energy * 20. / (1. + d * d);
2014-05-12 13:36:45 -04:00
att *= (radius - d) / radius;
2014-05-03 17:44:27 -04:00
if (att <= 0.) discard;
2014-04-06 15:23:09 -04:00
// Light Direction
vec3 L = -normalize(xpos.xyz - light_pos);
float NdotL = max(0., dot(norm, L));
Diffuse = vec4(NdotL * light_col * att, 1.);
Specular = vec4(getSpecular(norm, eyedir, L, light_col, roughness) * NdotL * att, 1.);
}