public static void CreateVoxels(MVVoxel[] voxels, MVVoxModel voxModel)
    {
        MVVoxelChunk chunk = voxModel.vox.voxelChunk;

        float sizePerVox = voxModel.sizePerVox;

        float cx = sizePerVox * chunk.sizeX / 2;
        float cy = sizePerVox * chunk.sizeY / 2;
        float cz = sizePerVox * chunk.sizeZ / 2;

        Material mat = (voxModel.voxMaterial != null) ? voxModel.voxMaterial : MVImporter.DefaultMaterial;

        List <GameObject> objects = new List <GameObject> ();

        foreach (MVVoxel voxel in voxels)
        {
            float px = voxel.x * sizePerVox - cx, py = voxel.y * sizePerVox - cy, pz = voxel.z * sizePerVox - cz;

            GameObject go = MVImporter.CreateGameObject(voxModel.gameObject.gameObject.transform,
                                                        new Vector3(px, py, pz),
                                                        string.Format("Voxel ({0}, {1}, {2})", voxel.x, voxel.y, voxel.z),
                                                        MVImporter.CubeMeshWithColor(sizePerVox, voxModel.vox.palatte [chunk.voxels [voxel.x, voxel.y, voxel.z] - 1]),
                                                        mat);

            MVVoxModelVoxel v = go.AddComponent <MVVoxModelVoxel> ();
            v.voxel = new MVVoxel()
            {
                x = voxel.x, y = voxel.y, z = voxel.z, colorIndex = chunk.voxels [voxel.x, voxel.y, voxel.z]
            };

            objects.Add(go);
        }

        Selection.objects = objects.ToArray();
    }
Esempio n. 2
0
    public static void CombineVoxels(MVVoxModelVoxel[] voxels)
    {
        if (voxels != null && voxels.Length > 0)
        {
            MVVoxelChunk chunk     = new MVVoxelChunk();
            MVVoxModel   model     = voxels [0].parentVoxModel;
            MVVoxelChunk origChunk = model.vox.voxelChunk;

            chunk.voxels = new byte[origChunk.sizeX, origChunk.sizeY, origChunk.sizeZ];
            foreach (MVVoxModelVoxel v in voxels)
            {
                chunk.voxels [v.voxel.x, v.voxel.y, v.voxel.z] = v.voxel.colorIndex;
            }

            MVImporter.GenerateFaces(chunk);
            Mesh[] meshes = MVImporter.CreateMeshesFromChunk(chunk, model.vox.palatte, model.sizePerVox);

            if (meshes.Length > 1)
            {
                Debug.LogError("[MVCombine] Currently does not support combining voxels into multiple meshes, please reduce the number of voxels you are trying to combine");
                return;
            }

            Material mat = (model.voxMaterial != null) ? model.voxMaterial : MVImporter.DefaultMaterial;

            int index = 0;
            foreach (Mesh mesh in meshes)
            {
                GameObject go = MVImporter.CreateGameObject(model.gameObject.transform, Vector3.zero, string.Format("VoxMesh ({0})", index), mesh, mat);

                MVVoxModelMesh voxMesh = go.AddComponent <MVVoxModelMesh> ();
                voxMesh.voxels = voxels.Select(o => o.voxel).ToArray();

                Selection.activeGameObject = go;

                index++;
            }

            foreach (MVVoxModelVoxel v in voxels)
            {
                GameObject.DestroyImmediate(v.gameObject);
            }
        }
        else
        {
            Debug.LogError("[MVCombine] Invalid voxels");
        }
    }