# 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.

## Recommended Posts

Hello, I was wondering how do you calculate a point light in HLSL? Thanks in Advance

##### Share on other sites
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 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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634065
• Total Posts
3015313
×