示例#1
0
    private static MeshWithCenter ConvertFromDirectedGraph(MeshGraph graph)
    {
        Vector3[] verticies = new Vector3[graph.allVerts.Length];
        Vector3[] normals   = new Vector3[graph.allVerts.Length];
        Vector4[] tangents  = new Vector4[graph.allVerts.Length];
        Vector2[] uv        = new Vector2[graph.allVerts.Length];

        Bounds b = new Bounds(graph.allVerts[0].pos, Vector3.zero);

        for (int i = 0; i < graph.allVerts.Length; i++)
        {
            normals[i]  = graph.allVerts[i].normal;
            tangents[i] = graph.allVerts[i].tangent;
            uv[i]       = graph.allVerts[i].uv;

            b.Encapsulate(graph.allVerts[i].pos);
        }

        // put the verticie position in as an offset pos so the brick'll
        // be centered yo.
        for (int i = 0; i < graph.allVerts.Length; i++)
        {
            verticies[i] = graph.allVerts[i].pos - b.center;
        }


        List <int> triangles = new List <int>();

        for (int i = 0; i < graph.edges.Count; i++)
        {
            Triangle tri = graph.edges[i];

            triangles.Add(Array.FindIndex(graph.allVerts, s => s.isEqual(tri.a)));
            triangles.Add(Array.FindIndex(graph.allVerts, s => s.isEqual(tri.b)));
            triangles.Add(Array.FindIndex(graph.allVerts, s => s.isEqual(tri.c)));
        }

        Mesh mesh = new Mesh();

        mesh.SetVertices(verticies);
        mesh.SetUVs(0, uv);
        mesh.SetNormals(normals);
        mesh.SetTangents(tangents);
        mesh.SetTriangles(triangles, 0);
        mesh.RecalculateBounds();

        MeshWithCenter data = new MeshWithCenter();

        data.center = b.center;
        data.mesh   = mesh;
        return(data);
    }
示例#2
0
    public static MeshWithCenter[] splitMesh(Mesh mesh)
    {
        MeshGraph graph = ConvertToDirectedGraph(mesh);

        MeshGraph[] graphs = SplitGraph(graph);

        MeshWithCenter[] meshes = new MeshWithCenter[graphs.Length];
        for (int i = 0; i < graphs.Length; i++)
        {
            meshes[i] = ConvertFromDirectedGraph(graphs[i]);
        }

        return(meshes);
    }
    private void CreateChildObject(MeshWithCenter mesh, MeshRenderer renderer, GameObject parent, int num)
    {
        GameObject child = new GameObject(gameObject.name + "_brick" + num, typeof(MeshRenderer), typeof(MeshFilter));

        MeshFilter childFilter = child.GetComponent <MeshFilter>();

        childFilter.sharedMesh = mesh.mesh;

        MeshRenderer childRenderer = child.GetComponent <MeshRenderer>();

        childRenderer.sharedMaterials = renderer.sharedMaterials;
        child.transform.SetParent(parent.transform, false);
        child.transform.position = mesh.center;
    }