I have been making a 2D visiblity algorithm by following this tutorial http://simblob.blogspot.se/2012/07/2d-visibility.html

I have managed to create the light vertices and it works fine when I draw it in a single color, but I want to use a shadow map to get "faded" lights like this:

enter image description here

Mine looks like this now:

enter image description here

where the blue dot is the light center and the green is the lighted area.

How can I "apply" a sprite to this like this one?

enter image description here

My code lies here: http://github.com/doodlemeat/bugfree-octo-sansa

  • \$\begingroup\$ Have you considered using a shader and pass it the mentioned sprite? That way you can read from the fragment shader the pixel color of the sprite, and set the gree area color accordingly. \$\endgroup\$
    – Leo
    Commented Feb 18, 2015 at 16:32
  • \$\begingroup\$ I have thought about that, but I don't think it supports multiple overlapping lights. \$\endgroup\$
    – Doodlemeat
    Commented Feb 18, 2015 at 17:06
  • \$\begingroup\$ In that case, you can just do a simple lighting shader that loops through the lights and adds color. \$\endgroup\$
    – mklingen
    Commented Feb 18, 2015 at 20:10

1 Answer 1


Set the UVs from the (X,Z) or (X,Y) vertex coordinates of your light mesh (depending on your world setup) relative to the light center.

vertex.uv = vec2(0.5, 0.5) + (vertex.pos.xy - light_center.xy) * scaling;

Assign that fading texture to your light mesh and set the texture mapping to clamp (or clamp to edge)

Use the scaling value to control the fading distance.

Note that unless you want to add some 2D pattern to it you don't need a 2D texture for this. A 1D texture would do fine with the brightest at U=0 and darkest at U=1. Where you apply the distance from center to the U coordinate (with scaling).

Not all systems support 1D textures. Alternatively you can use a 2D (N x 4) texture in the same way. It will save on texture memory access.

You can also calculate the color fade directly in the shader without using a texture.

  • \$\begingroup\$ Hi, how would I use just a shader to create the fading effect on the mesh? Right now, I have the mesh, and I want to apply a shader to it so it fades off. I have a position, radius, color, and power of the light, I was just wondering how to make a shader to apply to my mesh to get a good effect. @StephaneHockenhull \$\endgroup\$ Commented Aug 30, 2015 at 22:36
  • \$\begingroup\$ pseudo-code for a 2d light effect: vector2d v = current_pixel_position_on_surface - light_position_on_surface; brightness = Pow(Max(1 - Length(v) / radius, 0), light_falloff_exponent); Where _position_on_surface could be in UV coordinates, world coordinates, or screen space coordinates, could be 2D or 3D. Pos( , light_falloff_exponent) is there to make the fading non-linear when light_falloff_exponent is not 1.0 The whole formula is "improper" lighting but I'm assuming you want a 2D (ish) artistic fading effect rather than physical accuracy. \$\endgroup\$ Commented Sep 5, 2015 at 15:30
  • 1
    \$\begingroup\$ Right, I've figured that out; can you take a look at my current question on how to blend that color with the current texture color? @StephaneHockenhull \$\endgroup\$ Commented Sep 6, 2015 at 1:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .