Simplify collision mesh of road-like system?

后端 未结 3 857

Given a tile-based road or sidewalk system in Unity in 3d like shown, I\'m getting mini-bumps of rigidbodies moving over their seams (even for same vertical position & s

3条回答
  •  半阙折子戏
    2021-01-13 22:09

    You can try Mesh.CombineMeshes to make a big collider out of all the small ones.

    [RequireComponent(typeof(MeshCollider))]
    public class MeshCompositeCollider : MonoBehaviour
    {
        void Start()
        {
            var meshColliders = GetComponentsInChildren();
            var combine = new CombineInstance[meshColliders.Length];
    
            for (int i = 0; i < meshColliders.Length; i++)
            {
                combine[i].mesh = meshColliders[i].sharedMesh;
                combine[i].transform = meshColliders[i].transform.localToWorldMatrix;
                meshColliders[i].enabled = false;
            }
    
            var compositeMesh = new Mesh();
            compositeMesh.CombineMeshes(combine);
            //WeldVertices(compositeMesh);
            GetComponent().sharedMesh = compositeMesh;
            GetComponent().enabled = true;
        }
    }
    

    One thing to note: the resulting combined mesh is in world space coordinates, so if the gameObject this gets attached to has any transform changes they will be applied too.

    This might be enough if you have the MeshCollider -> Cooking Options -> Weld Colocated Vertices enabled which should combine the vertices that have the same position.

    If not, you can try to weld the vertices yourself to see if that fixes the problem.

    public static void WeldVertices(Mesh aMesh, float aMaxDistDelta = 0.01f)
    {
        var aMaxDelta = aMaxDistDelta * aMaxDistDelta;
        var verts = aMesh.vertices;    
        List newVerts = new List();
        int[] map = new int[verts.Length];
        // create mapping and filter duplicates.
        for (int i = 0; i < verts.Length; i++)
        {
            var p = verts[i];
    
            bool duplicate = false;
            for (int i2 = 0; i2 < newVerts.Count; i2++)
            {
                int a = newVerts[i2];
                if ((verts[a] - p).sqrMagnitude <= aMaxDelta)
                {
                    map[i] = i2;
                    duplicate = true;
                    break;
                }
            }
            if (!duplicate)
            {
                map[i] = newVerts.Count;
                newVerts.Add(i);
            }
        }
        // create new vertices
        var verts2 = new Vector3[newVerts.Count];    
        for (int i = 0; i < newVerts.Count; i++)
        {
            int a = newVerts[i];
            verts2[i] = verts[a];        
        }
        // map the triangle to the new vertices
        var tris = aMesh.triangles;
        for (int i = 0; i < tris.Length; i++)
        {
            tris[i] = map[tris[i]];
        }
    
        aMesh.Clear();       
        aMesh.vertices = verts2;
        aMesh.triangles = tris;                
    }
    


    Here is an example (before and after): Above 6 colliders with clear gaps, below 1 collider with no gaps.

提交回复
热议问题