Sign in to follow this  

HLSL Point Lights

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this