Example #1
0
    private Transform CreateHoleObject(List <Vector3> shapePoints)
    {
        var startIndex = 1;
        var endIndex   = shapePoints.Count - 1;

        Vector3[] vertices;
        Vector2[] uv;
        Mesh      mesh;

        var hole = Instantiate(_dynamicHolePrefab, Vector3.zero, Quaternion.identity, transform);

        hole.GetComponent <MeshFilter>().mesh = mesh = new Mesh();
        hole.gameObject.name = mesh.name = $"Hole{_holeId}";
        var num = endIndex - startIndex;

        vertices = new Vector3[num];
        uv       = new Vector2[num];

        for (int i = startIndex, k = 0; i < endIndex; i++, k++)
        {
            vertices[k] = shapePoints[i];
            uv[k]       = MyMaths.squashVector(vertices[k]);
        }

        mesh.vertices = vertices;
        mesh.uv       = uv;

        var triangulator = new Triangulator(vertices);

        mesh.triangles = triangulator.Triangulate();
        mesh.RecalculateNormals();

        var normals = mesh.normals;

        for (var i = 0; i < normals.Length; i++)
        {
            if (normals[i].normalized != Vector3.up)
            {
                // HAXXX: if you retrace your steps and finish a shape, it comes out all messed up, this kind of helps
                // Debug.Log("Fixed inverse hole");
                MyMaths.InverseTriangles(mesh);
                break;
            }
        }

        return(hole);
    }