private void CreateBodiesForEachFace(DoublyConnectedEdgeList dcel, Material material, Vector3 originalPosition)
    {
        float vWidth  = dcel.ux - dcel.lx;
        float vHeight = dcel.uy - dcel.ly;
        float ox      = originalPosition.x - width / 2;
        float oy      = originalPosition.z - height / 2;

        //TODO hardcoded for now to figure out what the problem is
        ox = -9.45f;
        oy = -7.92f;
        foreach (Face face in dcel.faceList)
        {
            float x = face.siteEvent.x * width / (vWidth * 4);
            float y = face.siteEvent.y * height / (vHeight * 3);

            GameObject fragment = new GameObject();
            fragment.AddComponent <Rigidbody>();
            MeshFilter   meshFilter   = fragment.AddComponent <MeshFilter>() as MeshFilter;
            MeshRenderer meshRenderer = fragment.AddComponent <MeshRenderer>() as MeshRenderer;
            meshRenderer.material = material;

            meshFilter.mesh = GetMeshFor(face, dcel);

            MeshCollider meshCollider = fragment.AddComponent <MeshCollider>() as MeshCollider;
            meshCollider.convex     = true;
            meshCollider.sharedMesh = meshFilter.mesh;

            fragment.transform.position = new Vector3(ox + x, originalPosition.y, oy + y);
        }
    }
    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == "Projectile")
        {
            Debug.Log("Entered collision with Projectile");
            //remove this game object's mesh renderer,box collider,Mesh filter and rigid body
            Vector3  originalPosition = transform.position;
            Material material         = this.RemoveAllComponents();
            //create two game objects of half the the width as this cube
            //add them as child to this cube
//			GameObject first = Instantiate(Resources.Load("SplitHalf 1", typeof(GameObject))) as GameObject;
//			GameObject second = Instantiate(Resources.Load("SplitHalf 2", typeof(GameObject))) as GameObject;
            DoublyConnectedEdgeList dcel = new VoronoiFracture().GetVoronoiDiagram();
            CreateBodiesForEachFace(dcel, material, originalPosition);
            DoublyConnectedEdgeList d = null;
//			d.ToString();//intentional program crash for pausing
        }
    }
Ejemplo n.º 3
0
    public DoublyConnectedEdgeList GetVoronoiDiagram()
    {
        //input
        float [,] input = new float[, ] {
            { 20, 80 }, { 40, 60 }, { 20, 30 }, { 70, 70 }, { 60, 50 }
        };

        DoublyConnectedEdgeList dcel = new DoublyConnectedEdgeList(0, 0, 100, 100);


        SiteEvent siteEvent;
        Face      face;

        //20,30
        siteEvent      = new SiteEvent(20, 20);
        face           = new Face(siteEvent);
        siteEvent.face = face;
        dcel.faceList.Add(face);

        face.AddEdge(new Edge(new Vertex(50, 0)));
        face.AddEdge(new Edge(new Vertex(48, 15)));
        face.AddEdge(new Edge(new Vertex(15, 45)));
        face.AddEdge(new Edge(new Vertex(0, 45)));
        face.AddEdge(new Edge(new Vertex(0, 0)));

        //70,30
        siteEvent      = new SiteEvent(70, 30);
        face           = new Face(siteEvent);
        siteEvent.face = face;
        dcel.faceList.Add(face);

        face.AddEdge(new Edge(new Vertex(50, 0)));
        face.AddEdge(new Edge(new Vertex(100, 0)));
        face.AddEdge(new Edge(new Vertex(100, 58)));
        face.AddEdge(new Edge(new Vertex(55, 35)));
        face.AddEdge(new Edge(new Vertex(48, 15)));

        //40,60
        siteEvent      = new SiteEvent(40, 40);
        face           = new Face(siteEvent);
        siteEvent.face = face;
        dcel.faceList.Add(face);

        face.AddEdge(new Edge(new Vertex(48, 15)));
        face.AddEdge(new Edge(new Vertex(55, 35)));
        face.AddEdge(new Edge(new Vertex(43, 65)));
        face.AddEdge(new Edge(new Vertex(15, 45)));

        //60,50
        siteEvent      = new SiteEvent(60, 50);
        face           = new Face(siteEvent);
        siteEvent.face = face;
        dcel.faceList.Add(face);

        face.AddEdge(new Edge(new Vertex(55, 35)));
        face.AddEdge(new Edge(new Vertex(100, 58)));
        face.AddEdge(new Edge(new Vertex(100, 100)));
        face.AddEdge(new Edge(new Vertex(50, 100)));
        face.AddEdge(new Edge(new Vertex(43, 65)));

        //20,70
        siteEvent      = new SiteEvent(20, 70);
        face           = new Face(siteEvent);
        siteEvent.face = face;
        dcel.faceList.Add(face);

        face.AddEdge(new Edge(new Vertex(0, 45)));
        face.AddEdge(new Edge(new Vertex(15, 45)));
        face.AddEdge(new Edge(new Vertex(43, 65)));
        face.AddEdge(new Edge(new Vertex(50, 100)));
        face.AddEdge(new Edge(new Vertex(0, 100)));

        return(dcel);
    }
    private Mesh GetMeshFor(Face face, DoublyConnectedEdgeList dcel)
    {
        float vWidth  = dcel.ux - dcel.lx;
        float vHeight = dcel.uy - dcel.ly;

        List <Vector3> vertexList = new List <Vector3>();
        List <Vector2> uvList     = new List <Vector2>();
        Edge           t          = face.GetStartingEdge();

        do
        {
            float   x      = t.origin.x * width / vWidth;
            float   y      = t.origin.y * height / vHeight;
            Vector3 vertex = new Vector3(x, thickness, y);
            vertexList.Add(vertex);
            uvList.Add(new Vector2(t.origin.x / vWidth, t.origin.y / vHeight));
            t = t.next;
        }while(t != face.GetStartingEdge());

        //repeat for lower side
        t = face.GetStartingEdge();
        do
        {
            float   x      = t.origin.x * width / vWidth;
            float   y      = t.origin.y * height / vHeight;
            Vector3 vertex = new Vector3(x, -thickness, y);
            vertexList.Add(vertex);
            uvList.Add(new Vector2(t.origin.x / vWidth, t.origin.y / vHeight));
            t = t.next;
        }while(t != face.GetStartingEdge());

        Vector3 [] vertices = vertexList.ToArray();

        //there are n-2 triangles on this convex polygon
        List <int> triangles = new List <int>();

        //polyogn on upper side
        for (int i = 1; i < vertices.Length / 2 - 1; i++)
        {
            //always add group of 3
            triangles.Add(0);
            triangles.Add(i + 1);
            triangles.Add(i);
        }

        //polyogn on lower side
        int resetIndex = vertices.Length / 2;

        for (int i = 1; i < vertices.Length / 2 - 1; i++)
        {
            //always add group of 3
            triangles.Add(resetIndex + 0);
            triangles.Add(resetIndex + i);
            triangles.Add(resetIndex + i + 1);
        }

        //triangles for thickness sides
        for (int i = 0; i < vertices.Length / 2 - 1; i++)
        {
            // add group of 3 for one triangle of rectangle
            triangles.Add(i);
            triangles.Add(i + 1);
            triangles.Add(resetIndex + i + 1);

            triangles.Add(i);
            triangles.Add(resetIndex + i + 1);
            triangles.Add(resetIndex + i);
        }

        //handle the last case seperately because its wrapping around
        // add group of 3 for one triangle of rectangle (using exisitng i)
        int j = vertices.Length / 2 - 1;

        triangles.Add(j);
        triangles.Add(0);
        triangles.Add(resetIndex + 0);

        triangles.Add(j);
        triangles.Add(resetIndex + 0);
        triangles.Add(resetIndex + j);

        int [] trianglesArray = triangles.ToArray();
        Mesh   mesh           = new Mesh();

        mesh.vertices  = vertices;
        mesh.triangles = trianglesArray;
        mesh.uv        = uvList.ToArray();
        return(mesh);
    }