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

Triangles in mesh not recalculating correctly when calling RecalculateNormals()

Discussion in 'Scripting' started by discordbotstudio, Mar 2, 2021.

  1. discordbotstudio

    discordbotstudio

    Joined:
    Aug 2, 2020
    Posts:
    4
    I am generating a mesh with simplex noise via script. That works fine. I am then raycasting with the mouse to allow a vertex to be clicked, to raise that vertex. When I do so in code, and recalculate normals, the triangles surrounding that vertex are not recalculated correctly. Am I missing something?

    Here's the code in question:
    Code (CSharp):
    1. verts[closestIndex] += Vector3.up * 0.5f;
    2.             mesh.vertices = verts;
    3.             mesh.RecalculateNormals();
    4.  
    5.             meshCollider = GetComponent<MeshCollider>();
    6.             meshCollider.sharedMesh = mesh;
    Below is a picture of what is happening. Only one of the surrounding triangles is updated, so it looks wrong.

     
  2. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,748
    Looks like you not sharing vertices, so each tri in your mesh uses 3 unique vertices. The fact the faces are flat shaded and not smooth also indicates that. If you want the flat shaded/low poly look then you will have to find the vertices in your mesh that have the same value as the one you are about to move and then also move those.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Normals are stored in a single vertex. If you just raise a vertex and recalc normals, it won't look like a facet but rather mooshy smooth. To make it look like a facet you have to clone the verts so they are not shared between faces.

    You can see a running comparison of what I'm talking about in my MakeGeo project. Look for the
    SharedvsNonSharedVertices
    scene.

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo