Hey guys, hopefully someone smarter than me can help me out.
I have some point lights. They have a radius. However, the light they emit doesn't seem to be constrained by this radius like I want, see this image:
(The red circles are what their radius should be. You can see how the light continues though, producing a very visible square shape.)
Here's the shader I use where I pass in each light one by one and render it to the scene:
#version 330
in vec4 v_color;
in vec2 v_texCoord; // UV for the current fragment
uniform sampler2D u_texture;
uniform sampler2D u_normal;
uniform float lightRadius; // Radius of light (pixels)
uniform vec3 lightPos; // Position of point light (UV)
uniform vec4 lightColor; // Color of light, a is intensity
uniform vec2 screenSize; // Screen resolution (pixels)
layout(location = 0) out vec4 lightBuffer;
void main()
{
// RGBA of diffuse color.
vec4 diffuseRGBA = texture2D(u_texture, v_texCoord);
// RGB of normal map, invert the y-axis for accuracy.
vec3 normalRGB = texture2D(u_normal, v_texCoord).rgb;
normalRGB.g = 1.0 - normalRGB.g;
// The delta position of light
vec3 lightDir = vec3(lightPos.xy - (gl_FragCoord.xy / screenSize.xy), lightPos.z);
// Correct for aspect ratio and set size of light.
float lightDiameter = lightRadius * 2;
lightDir.x /= (lightDiameter / screenSize.x);
lightDir.y /= (lightDiameter / screenSize.y);
// Determine distance (used for attenuation) before normalizing lightDir.
float distance = length(lightDir);
// Normalize vectors.
vec3 N = normalize(normalRGB * 2.0 - 1.0);
vec3 L = normalize(lightDir);
// Pre-multiply light color with intensity then perform "N dot L" to determine diffuse term.
vec3 diffuse = (lightColor.rgb * lightColor.a) * max(dot(N, L), 0.0);
// Calculate attenuation.
float attenuation = 1.0 - (distance * distance);
// The calculation which brings it all together.
vec3 intensity = diffuse * attenuation;
vec3 finalColor = diffuseRGBA.rgb * intensity;
// Done.
lightBuffer = v_color * vec4(finalColor, diffuseRGBA.a);
}
Can anyone shed some light on what's going on, appreciate it!