예제 #1
0
    void Fracture(Vector2 center)
    {
        vec3center = new Vector3(center.x, center.y);
        print("Generating sites around " + center.ToString("F3"));
        var sites = VoronoiHelpers.GenerateSites(canvasSize, 30, center);

        drawSites(sites);

        var points = new List <VoronoiLib.Structures.FortuneSite>();

        foreach (var site in sites)
        {
            // move all input points to Quadrant 1
            var offsetSite = site;
            points.Add(new VoronoiLib.Structures.FortuneSite(offsetSite.x, offsetSite.y));
        }

        //FortunesAlgorithm.Run(points, min x, min y, max x, max y)
        LinkedList <VoronoiLib.Structures.VEdge> cuttingEdges = VoronoiLib.FortunesAlgorithm.Run(points, 0, 0, 800, 800);

        // HERE !@!@$@!%$@!%

        var delaunay = VoronoiHelpers.GenerateDelaunay(points);

        StartCoroutine(DrawVoronoiCoroutine(cuttingEdges));
        // StartCoroutine(DrawDelaunayCoroutine(delaunay));

        // Make a copy of the original mesh
        var workingMesh = MeshTools.Clone(mesh);
        // Disable the orginal mesh
        // mesh.Clear();
        // this.GetComponent<Renderer>().enabled = false;

        int i = 0;

        // Sutherland–Hodgman algorithm for slicing concave shapes
        foreach (var point in points)
        {
            // Duplicate the working mesh
            var piece = MeshTools.Clone(workingMesh);
            print("Cutting piece " + i + ", " + point.Neighbors.Count + " neighbors.");

            foreach (var neighbor in point.Neighbors)
            {
                // Find the perpendicular bisector and slice
                var a        = new Vector2((float)point.X, (float)point.Y);
                var b        = new Vector2((float)neighbor.X, (float)neighbor.Y);
                var bisector = VoronoiHelpers.PerpendicularBisector(a, b);

                // Keep vertices from this side of the bisector line
                try {
                    TempMesh temp = MeshTools.Cut(piece, bisector, center);
                    MeshTools.ReplaceMesh(mesh, temp);
                    print("SUCCESS");
                } catch (UnityException e) {
                    print(e.ToString());
                }
                // Fill the hole
                // MeshTools.FillHoles(piece);

                // Now we have the final chunk, save it to a new GameObject
                // var g = MeshTools.Export(temp, "Piece " + i);
                // MeshTools.ReplaceMesh(mesh, temp);

                // break;
            }

            // Instantiate(g, g.transform.position, Quaternion.identity);
            i++;
            break;
        }

        mesh = workingMesh;

        print("length: " + points.Count);
    }