Пример #1
0
    public void RefreshMesh()
    {
        // indices top and bottom
        Vector2[] temp = new Vector2[LocalPositions.Count];
        for (int i = 0; i < temp.Length; i++)
        {
            temp[i] = new Vector2(LocalPositions[i].x, LocalPositions[i].z);
        }

        int[] indices        = new Triangulator(temp).Triangulate();
        int[] reverseIndices = indices.Reverse().ToArray();
        int[] tempAllIndices = new int[indices.Length * 2];

        reverseIndices.CopyTo(tempAllIndices, 0);
        indices.CopyTo(tempAllIndices, indices.Length);

        for (int i = indices.Length; i < tempAllIndices.Length; i++)
        {
            tempAllIndices[i] += LocalPositions.Count;
        }

        // indices sides
        List <int> tempIntList = tempAllIndices.ToList();

        for (int i = 0; i < LocalPositions.Count; i++)
        {
            List <int> t = new List <int>();
            for (int j = 0; j < 6; j++)
            {
                if (j == 0)
                {
                    t.Add(i);
                }
                else if (j == 1)
                {
                    t.Add(i != LocalPositions.Count - 1 ? LocalPositions.Count + (i + 1) : LocalPositions.Count);
                }
                else if (j == 2)
                {
                    t.Add(LocalPositions.Count + i);
                }
                else if (j == 3)
                {
                    t.Add(i);
                }
                else if (j == 4)
                {
                    t.Add(i != LocalPositions.Count - 1 ? (i + 1) : 0);
                }
                else if (j == 5)
                {
                    t.Add(i != LocalPositions.Count - 1 ? LocalPositions.Count + (i + 1) : LocalPositions.Count);
                }
            }
            tempIntList.AddRange(t);
        }

        // Create the Vector3 vertices
        var vertices = new Vector3[temp.Length * 2];

        Vector2[] tempAllVertices = new Vector2[temp.Length * 2];
        temp.CopyTo(tempAllVertices, 0);
        temp.CopyTo(tempAllVertices, temp.Length);
        for (int i = 0; i < vertices.Length; i++)
        {
            vertices[i] = new Vector3(tempAllVertices[i].x, i < vertices.Length / 2 ? -RadarMeshHeight : RadarMeshHeight, tempAllVertices[i].y);
        }

        // Create the mesh
        Mesh mesh = new Mesh
        {
            vertices  = vertices,
            triangles = tempIntList.ToArray()
        };

        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        GetComponent <MeshFilter>().sharedMesh = mesh;

        var meshCollider = GetComponent <MeshCollider>();

        if (meshCollider == null)
        {
            meshCollider = gameObject.AddComponent <MeshCollider>();
        }
        meshCollider.sharedMesh = mesh;
        meshCollider.convex     = true;
    }