Search Unity

Question Question about normal map

Discussion in 'Shader Graph' started by Kiupe, Apr 8, 2019.

  1. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    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
     
  2. Kink3d

    Kink3d

    Joined:
    Nov 16, 2015
    Posts:
    45
    Hi,

    Could you provide some images to help us work out whats going on here? Preferably of both the graph and actual/expected outcomes.
     
  3. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    Hello,

    Here some screenshots:

    Information : the sahder is applied on the background, the tree and the trunk. The light position is 0,0.

    The shader effect without the normal map take into account. The ramp texture works as expected
    without_normal.PNG

    The shader effect with the normal map (angle) take into account. Here the tree should have more dark areas respecting the ramp effect because of the normal map. This should affect where the tree leaves are close to the light but not facing the light direction.
    with_dot_product.PNG

    Here the screenshot of the shader graph, bot previous screenshot use the same shader graph but for the first one I set the offset that is multiplied by the dot product (angle) to zero to simulate no angle influence.

    At the bottom there is the part with the normal mal, how I split the vector and compute the dot production with the light direction and then use that value (that should be between -1 and 1) to a factor that is use to create an offset vector before sampling the ramp texture.

    As I said, if I do not inverse the normal map vector axis I get really weird result.

    dot_product_offset.PNG

    Now Im' wondering what do I do wrong ? Is that the angle computation ? How the angle affect the offset ? Should the angle affect the offset or should I use it to in other way to influence the result ?

    Thanks