Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why are normals saved on a per-Vertex-basis?

Discussion in 'Shaders' started by frankfringe, Sep 9, 2019.

  1. frankfringe

    frankfringe

    Joined:
    Feb 9, 2019
    Posts:
    105
    If I understand it right, the normals are saved per-Vertex and are interpolated on the fragments.

    To calculate a normal on the vertex beforehand one has to probably do something like take the average of the normals of the adjacent triangles.

    Now I don't understand this: We have well-defined triangle normals, average them to get vertex normals and then interpolate between these vertex normals (also a kind of averaging) to get back normals on the triangles.

    Why can't we directly work with triangle normals?
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    Short answer: this is because we don't have this information. Vertex shaders operates on isolated vertices.
     
    frankfringe likes this.
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    To further explain @aleksandrk 's response, it's because that's not how GPUs were designed to work.

    From the earliest GPUs to now, they chose a design meant to reduce complexity and limit the amount of data required. The model they chose for mesh representation is triangles with a single set of data per vertex and using barycentric interpolation across the face of the triangle.

    That means all that's need for a single triangle is 3 vertices each with a position, and optionally a normal and one or more UV positions, and the current position within that triangle for the pixel you're rendering. The mesh's data representation is then a bunch of arrays of vertex data, and an array of what vertices each triangle uses, and you're done.

    To do triangle normal interpolation would require the vertex data for an arbitrary number of triangles at a time, and special case handling for when a triangle has a hard edge so there's no neighbor triangles. That's a lot more data, and a lot more math.
     
    frankfringe likes this.
  4. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    741
    If you want to calculate the normal on a vertex, you should do it mathematically.

    Any equation you have that displaces the vertex from its original position and original normal, will also be able to be derived in such a way to find its new normal vector. Its not easy math though. Depending on what you are doing most techniques have already figured out the math for you in papers.

    If not that use a height map and sample a small amount around it. And average it. Its not perfect but it should be sufficient in most cases. Water rendering does this to save performance.