Search Unity

Custom Mesh Lighting

Discussion in 'General Graphics' started by Deleted User, Dec 16, 2018.

  1. Deleted User

    Deleted User

    Guest

    Hello, I am generating a custom mesh and calculating its normals using this built in function - RecalculateNormals (); Everything works great, but betwen some vertices there are "lines" in lighting.




    The green mesh is separate from the brown one.

    How to fix this? Thanks.
     
  2. Deleted User

    Deleted User

    Guest

  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    I assume this is actually several copies of the same wave mesh, not one long mesh with multiple waves? Then this is working exactly as it should, even if it's not how you want.

    Lets go over roughly how normals are calculated on a mesh using RecalculateNormals().

    Lets pretend we're looking at a portion of your above mesh from the side. Here we have a couple of vertices, and the polygon faces between them. Each face has an implicit normal, which is the cross product of the three vertices that make up the face. This is represented below by the fat, short arrows. If you were to use those normals, the mesh would be flat shaded and not smooth, so instead the vertices use an average of the faces' normals that are connected to that vertex. This is the thin, tall arrows below. The result is nice smooth shading.
    upload_2018-12-17_23-53-32.png
    Now lets break up that curve into two meshes. A green one and a red one.
    upload_2018-12-18_0-1-14.png
    Now that vertex in the middle of the image is actually two separate vertices in two different meshes. The green mesh's vertices don't know about the red mesh's vertices, so in this case the average normal of the faces using that normal is the same as the normal of those faces. The result is exactly what you're seeing above.

    The solution is to not use the RecalculateNormals() function. It only knows about what's in the mesh you're providing it, and has no knowledge of what intended usage is. I.e.: it has no idea that it's supposed to be able to seamlessly continue if you put multiple meshes together. So instead you'll need to calculate the normals yourself, either using that knowledge of the mesh effectively wrapping, or by calculating the normal from whatever wave function you're using to generate the mesh.
     
    XJonOneX, blayzenw, abob101 and 2 others like this.
  4. Deleted User

    Deleted User

    Guest

    @bgolus thanks for the detailed explanation! Turns out I had multiple vertices of the same position and those vertices were messing up everything else.
     
    bgolus likes this.