Advertisement Jump to content
Sign in to follow this  

HLSL Point Lights

This topic is 4823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

2D or 3D?

3D -
In the vertex shader's output, you include the untransformed position of the vertex too. It will linearly interpolate it between vertices, which is just fine as the pixels are also linearly interpolated in the same place. So, in the vertex shader, output the transformed position (untransformed position multiplied by the view matrix), the texcoord and the untransformed position.

In the pixel shader, sample a texture using the input texcoord, and multiply it based on the distance between the untransformed position and the light.

Example hlsl shader, also includes bumpmapping

2D - If your using 2D, which chances are you aren't, post here and I'll tell you how.

Hope this helps :)

Share this post

Link to post
Share on other sites
You can do this all in the vertex shader, or partially in both the vertex and pixel shader.

For per pixel light, typically you pass vertex position as a UV from the vertex shader to the pixel shader, as the previous poster said. You'll also need to pass the normal. In the pixel shader you can compute the direction and distance:

Regardless of pixel or vertex shader, you'd do this:

lightvec = (vertexpos - lightpos);
lightdist = length(lightvec);
lightvec = normalize(lightvec);

You can then apply attenutation based on the distance, do a directional light calculation based on the lightvector and the normal. The pointlight color value is (lightvec dot normal) * attenuation * lightcolor * materialcolor.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!