I am building a game in XNA, but this question could apply to most 3D frameworks.
My scene contains several point lights. Each light has a position, intensity, color and radius. It also contains several objects that can be lit by the point lights.
I have successfully used a simple forward renderer to light objects these with up to 8 point lights at a time.
However, I also want to try some other effects, such as lit objects where the light slowly fades away each frame.
I want to use a multipass approach:
- Each lit object has a texture and a lightmap (a rendertexture)
Then, each frame and for each object:
- Clear the lightmap (or fade it)
- Draw each light to the lightmap, with falloff etc.
- Render the object using texture * lightmap in the pixel shader
My problem is related to texture coordinates. The lightmap should use the same UV mapping as the texture map, so that when I draw the objects, the color is:
tex2D(TextureSampler, uv) * tex2D(LightMapSampler, uv)
Or, put another way, I need to compute the world position of each pixel in the lightmap in the lightmap pixel shader for computing its distance to each light.
What coordinate transforms are required to achieve this?