Search Unity

Divide a Mesh with Overlap - Encountering Problems

Discussion in 'General Graphics' started by Mr_Admirals, Jan 25, 2019.

  1. Mr_Admirals

    Mr_Admirals

    Joined:
    May 13, 2017
    Posts:
    86
    Hello!

    For a snow deformation effect, I want to recursively divide up a planar mesh into quadrants. When the recursion is done, I want to take the tiles and add on an extra layer of vertices around the border where possible. This creates an area of overlap between the tiles that blends the snow deformation effect across the borders so there's not a noticeable gap/tear when stepping across the border.

    Now, dividing the mesh into tiles works flawlessly, but when I go to add the overlap, the vertices are correctly added for the majority of the tiles, which obviously isn't acceptable. I don't know why this is the case because the same code is applied to each tile.

    Code (CSharp):
    1. for (int i = 0; i < meshes.Count; i++)
    2.         {
    3.             Mesh currentMesh = meshes[i];
    4.             Vector3 center = currentMesh.bounds.center;
    5.             Vector3 extents = currentMesh.bounds.extents;
    6.             List<Vector3> vertList = new List<Vector3>();
    7.             List<Vector2> uvList = new List<Vector2>();
    8.  
    9.             float maxX = center.x + extents.x + vertDist;
    10.             float minX = center.x - extents.x - vertDist;
    11.             float maxZ = center.z + extents.z + vertDist;
    12.             float minZ = center.z - extents.z - vertDist;
    13.  
    14.             for (int j = 0; j < vertexCount; j++)
    15.             {
    16.                 if (vertices[j].x <= maxX &&
    17.                     vertices[j].x >= minX &&
    18.                     vertices[j].z <= maxZ &&
    19.                     vertices[j].z >= minZ)
    20.                 {
    21.                     vertList.Add(vertices[j]);
    22.                     uvList.Add(uv[j]);
    23.                 }
    24.             }
    25.  
    26.             currentMesh.vertices = vertList.ToArray();
    27.             currentMesh.uv = uvList.ToArray();
    28.  
    29.             GameObject tile = new GameObject("Tile");
    30.             tile.tag = "Tile";
    31.             MeshFilter meshFilter = tile.AddComponent<MeshFilter>();
    32.             meshFilter.mesh = InitializeTrianglesAndUVs(currentMesh);
    33.             tile.AddComponent<MeshRenderer>();
    34.             tile.AddComponent<MeshCollider>();
    35.             tile.transform.position = transform.position;
    36.             tile.transform.rotation = transform.rotation;
    37.         }
    Basically, I get the bounds of current mesh, and use the distance between each vertex to add onto the bounds to get the extra layer.

    Here's the result for a flat plane:

    upload_2019-1-25_14-38-41.png

    For some reason there's a diagonal gap missing, and even stranger is that somehow some of the vertices are being distorted along the Y axis.

    upload_2019-1-25_14-41-7.png

    Any thoughts or suggestions?
     
  2. Mr_Admirals

    Mr_Admirals

    Joined:
    May 13, 2017
    Posts:
    86
    I ended up solving it. The displaced vertices were a byproduct of the snow deformation code. The real problem was the triangle indices. I was using an algorithm that only accounted for the tiles being squares.