Hello,
I’m working on a fake 2D toon light shader. Basically, in a 2D game I have an object acting as a light. It has a position, a range, a color and a ramp texture (white, grey and black). Those properties exist shader side and during the test phase are initialized directly in the shader, except for the position which is set by code. So, depending of the distance, a pixel in the ramp texture is sampled and used in order to have a “cartoon” effect. It works great.
I decided to use a normal map in order to take into account the angle, point is to have a pixel less “bright” the less is facing the light direction. In order to achieve that I do the following step (spoiler, it does not work):
- Substract the current “pixel” position and the light position in order to have the light direction
- Normalise the light direction
- Compute the dot product of the pixel normal vector and the light direction vector
- use the dot product as a factor to offset the ramp texture so simulate a more lighter or darker area on the ramp texture according to the angle (the ramp texture is clamped)
This does not work and it seems that the dot product does not always returned values between -1 and 1 whereas vectors are normalized.
- I’m not quite sure that the normal vector and the light direction vector use the same axis orientation. After some test I ended up by inverting x and z axis and multiply the y axis by -1. Does not fixed everything but the result is less “weird”.
- I don’t know how to debug the dot product, so I have no idea what it returns
- I’m not sure the angle (dot product) should influence the ramp offset, but I did see shader doing that but in 3D.
Does anyone have any advice or help regarding my issues ?
Thanks