I'm implementing point lights in my Voxel engine, and I'm really struggling to get a good flow of light, from 100% near the light source to 0% at the light radius.
I have 5 arguments for the function:
- Light color (Vec3)
- Light intensity (distance from the light till the distance where falloff is 100%)
- Distance from the light to the fragment
- The angle from the fragment normal to the light
- The position of the light
Can anyone push me in the right direction to create a function for the calculating of the fragment color?
Image of one of my experiments:
Edit (current code requested by Byte) Note that this is just some experiment code from my side. I got the float att from a website, and it kinds works, but far from perfect.:
void main()
{
// Light color
vec3 torchColor = vec3(1.0f, 1.0f, 1.0f);
float lightAdd = 0.0f;
for (int i=0; i<5; i++) {
vec3 pos = lights[i];
if (pos.x == 0.0f) continue;
float dist = distance(vertex_pos, pos);
if (dist < 9) {
float att=1.0/(1.0+0.1*dist+0.01*dist*dist);
vec3 surf2light = normalize(pos - vertex_pos);
vec3 norm = normalize(normal);
float dcont=max(0.0,dot(norm,surf2light));
lightAdd += att*(dcont+0.4);
}
}
vec3 textureColor = texture2D(texture, texture_coordinate).rgb;
vec3 torch_output = lightAdd * torchColor;
vec3 final_color = ((0.1+torch_output) * textureColor);
gl_FragColor = vec4(final_color, 1.0f);
}
if (dist < 9)
? Alternatively you could calculateatt
with a function that returns 1 when distance is 0 and 0 when distance is 9. E.g.mix(1.0, 0.0, dist / 9.0)
\$\endgroup\$