Search Unity

Question Setting normals in world space inside a surface shader

Discussion in 'Shaders' started by LogTheDog, Jun 10, 2022.

  1. LogTheDog

    LogTheDog

    Joined:
    Aug 14, 2017
    Posts:
    1
    I'm creating a GPU terrain renderer. I have a plane that is an NxN uniform grid of vertices, filled with triangles (like you usually do). I have an NxN heightmap (procedurally generated from noise) and have generated a corresponding NxN normal map that contains the normals for each vertex in world space (packed to the 0 to +1 range).

    Now, I am displacing the vertices on the Z axis inside the vertex shader. If I set the v.normal inside the vertex shader, everything works great. But, I want to do it inside the surface shader since I don't want the normal map resolution to be tied to the mesh resolution (so I can use a more detailed normal map for example). The problem is, I have no idea how to do this. I don't really understand how tangent space works. Is it relative to the vertex normal? If that's the case, in theory, I should be able to set all of the vertex normals to (0,1,0) and then treat the o.Normal like it is in world space, but that doesn't seem to work. Does tangent space even use values between -1 and 1 or 0 and 1? I don't even know which direction is up; it seems like it's the z axis, whereas in the vertex shader it's the y axis. Also, why is (0,0,1) the up vector and not (0.5,0.5,1)? I'm so confused, any help is appreciated.