Search Unity

How to calculate normal vectors in displacement shaders? (and use them for lighting)

Discussion in 'Shaders' started by xHammy, Dec 9, 2019.

  1. xHammy

    xHammy

    Joined:
    Aug 11, 2017
    Posts:
    27
    So pretend I have a brutally simple vertex displacement shader.

    I use this shader on some material applied to a flat plane in my scene, and it displaces it per a sine wave, pretty simple. Of course, then the PBR wants to light it, but as far as PBR knows it's a flat plane, not a wavy plane, so it lights it as such. Which obviously isn't something I want.

    To fix it, I know I need to get the vector tangent to the wave. In the case of the wave sin(x) our tangent slope should be -sec(x).

    But a slope isn't exactly what I want, I want a vector in world space. And I'm pretty sure I'm not even working in the right direction anymore.

    So, to ask an actual question:
    How do I actually get my normal vector in a format that unity agrees with?

    Once I have my normal vector how do I use it to inform PBR how to light my plane? As I'm using my own cell shader that expects, this isn't as important, or it's not an issue if the answer is "you can't". The shader takes the world space normal vector as an input, so regardless I'll be able to use it.
     
  2. xHammy

    xHammy

    Joined:
    Aug 11, 2017
    Posts:
    27
    I've fixed it at least in the X direction, I can probably do the cell shading stuff now. No idea on unitys lighting, but that's not a big deal.