Search Unity

Auto weld (or connect) vertices after merge

Discussion in 'World Building' started by tr0ma, Mar 30, 2020.

  1. tr0ma

    tr0ma

    Joined:
    Dec 4, 2012
    Posts:
    19
    Hi there,

    Trying out the runtime api, I've been successfully merging two cubes meshes together through script.
    During that process is there any way to optimize duplicate vertices?
    What would be the easiest way to clean them up?

    Thanks
     
  2. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
  3. tr0ma

    tr0ma

    Joined:
    Dec 4, 2012
    Posts:
    19
    Sorry, I forgot to mention that, I was using Combine

    Code (CSharp):
    1. var list = new List<ProBuilderMesh> { meshA, meshB };
    2. CombineMeshes.Combine(list, meshA);
    which doesn't seem to go through any collapsing of coincident vertices.

    Maybe MergeElements is the way to go then...
     
  4. stychu

    stychu

    Joined:
    May 9, 2016
    Posts:
    62
    I bump this question.
    There seems no way to collapse coincident vertices automatically it seems or I wrongly interpreting docs and examples.

    I just want to collapse all coincident vertices so only one vertex per world position would be left. Isin't tehre any easy way to do it @kaarrrllll ?/
     
  5. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    In the Editor ProBuilder handles this automatically through EditorMeshUtility.Optimize call. At runtime you will need to use MeshUtility.CollapseSharedVertices directly.
     
  6. stychu

    stychu

    Joined:
    May 9, 2016
    Posts:
    62
    After hours of debugging, I found some answers to my problem.
    The first mistake was that I assumed when merging/collapsing shared vertices will transform probuilder mesh vertices data. So there will be fewer vertices. Instead what is happening it does not change the vertices data but it does change the sharedvertices. So if I do


    Code (CSharp):
    1. SharedVertex.GetSharedVerticesWithPositions(mesh.VerticesInWorldSpace())
    2.      .Where(vert => vert.Count > 1)
    3.      .ToList()
    4.      .ForEach(vertex => mesh.MergeVertices(vertex.ToArray(), true));
    5.  
    6.     MeshUtility.CollapseSharedVertices(mesh.GetComponent<MeshFilter>().sharedMesh);
    It will indeed merge the vertices into shared vertex but that vertex will contain all indexes of vertices being at the same position.

    Not sure if the collapsesharedvertices there is needed? I think it would be needed if i moved the vertices itself??
     
  7. stychu

    stychu

    Joined:
    May 9, 2016
    Posts:
    62
    Code (CSharp):
    1. int[] faces = mesh.faces.SelectMany(x => x.indexes).ToArray();
    2.     mesh.WeldVertices(faces, Mathf.Epsilon);
    I'm stupid and this actually works. But because of confusion that the vertices will be removed I though it doesn't work..
     
  8. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    Glad you got it working. Was the point of confusion that CollapseSharedVertices applies to the `MeshFilter.sharedMesh` rather than `ProBuilderMesh` component?