Exemple #1
0
        private int[,,] VoxelizeMesh(Vector3[] vertices, int[] triangles, Box3 bounds, int size)
        {
            MeshVoxelizer m_voxelizer = new MeshVoxelizer(size, size, size);

            m_voxelizer.Voxelize(vertices, triangles, bounds);

            return(m_voxelizer.Voxels);
        }
    /// <summary>
    /// Uses <see cref="MeshVoxelizer"/> to build voxel object collection from the source mesh.
    /// </summary>
    /// <param name="size">xyz size of the voxel object collection</param>
    public void GenerateBlocks(int size)
    {
        Color defaultColor = gameObject.GetComponent <BlockDrawer>().selectedColor;

        voxelObject.SetActive(true);
        var bounds = srcMesh.bounds;
        var max    = bounds.extents.x;

        if (max < bounds.extents.y)
        {
            max = bounds.extents.y;
        }
        if (max < bounds.extents.z)
        {
            max = bounds.extents.z;
        }
        var  center = srcMesh.bounds.center;
        Box3 box    = new Box3(center - new Vector3(max, max, max), center + new Vector3(max, max, max));

        meshVoxelizer = new MeshVoxelizer(size, size, size);
        meshVoxelizer.Voxelize(srcMesh.vertices, srcMesh.triangles, box);

        for (int i = 0; i < meshVoxelizer.Voxels.GetLength(0); i++)
        {
            for (int j = 0; j < meshVoxelizer.Voxels.GetLength(1); j++)
            {
                for (int k = 0; k < meshVoxelizer.Voxels.GetLength(2); k++)
                {
                    if (meshVoxelizer.Voxels[i, j, k] == 1)
                    {
                        GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                        cube.transform.parent        = voxelObject.transform;
                        cube.transform.localPosition = new Vector3(i, j, k);
                        cube.tag = "Voxel";
                        cube.GetComponent <MeshRenderer>().material.color = defaultColor;
                    }
                }
            }
        }
        sourceObject.SetActive(false);
    }