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 void LoadVOXData(byte[] data, bool asIndividualVoxels, byte[] alphaMaskData = null)
    {
        ClearVoxMeshes();

        MVVoxelChunk alphaMask = null;

        if (alphaMaskData != null)
        {
            MVMainChunk mc = MVImporter.LoadVOXFromData(alphaMaskData, generateFaces: false);
            if (mc != null)
            {
                alphaMask = mc.voxelChunk;
            }
        }
        MVMainChunk v = MVImporter.LoadVOXFromData(data, alphaMask);

        if (v != null)
        {
            Material mat = (this.voxMaterial != null) ? this.voxMaterial : MVImporter.DefaultMaterial;
            if (!asIndividualVoxels)
            {
                MVImporter.CreateVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox);
            }
            else
            {
                MVImporter.CreateIndividualVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox);
            }

            this.vox = v;
        }
    }
Esempio n. 3
0
    public void LoadVOXData(byte[] data, bool asIndividualVoxels)
    {
        ClearVoxMeshes();

        MVMainChunk v = MVImporter.LoadVOXFromData(data);

        if (v != null)
        {
            Material mat = (this.voxMaterial != null) ? this.voxMaterial : MVImporter.DefaultMaterial;

            if (paletteToTex)
            {
                mat.mainTexture = v.PaletteToTexture();
            }

            if (!asIndividualVoxels)
            {
                MVImporter.CreateVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox);
            }
            else
            {
                MVImporter.CreateIndividualVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox);
            }

            this.vox = v;
        }
    }
Esempio n. 4
0
    public void LoadVOXFile(string path, string alphaMaskPath, bool asIndividualVoxels)
    {
        ClearVoxMeshes();

        if (path != null && path.Length > 0)
        {
            MVVoxelChunk alphaChunk = null;
            if (!string.IsNullOrEmpty(alphaMaskPath))
            {
                MVMainChunk mc = MVImporter.LoadVOX(alphaMaskPath, generateFaces: false);
                if (mc != null)
                {
                    alphaChunk = mc.voxelChunk;
                }
            }

            MVMainChunk v = MVImporter.LoadVOX(path, alphaChunk);

            if (v != null)
            {
                Material mat = (this.voxMaterial != null) ? this.voxMaterial : MVImporter.DefaultMaterial;

                if (!asIndividualVoxels)
                {
                    if (meshOrigin != null)
                    {
                        MVImporter.CreateVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox, meshOrigin.localPosition);
                    }
                    else
                    {
                        MVImporter.CreateVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox);
                    }
                }
                else
                {
                    if (meshOrigin != null)
                    {
                        MVImporter.CreateIndividualVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox, meshOrigin.localPosition);
                    }
                    else
                    {
                        MVImporter.CreateIndividualVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox);
                    }
                }

                this.vox = v;
            }
        }
        else
        {
            Debug.LogError("[MVVoxModel] Invalid file path");
        }
    }
Esempio n. 5
0
    public static MVMainChunk LoadVOX(string path, bool generateFaces = true)
    {
        byte[] bytes = File.ReadAllBytes(path);
        if (bytes [0] != 'V' ||
            bytes [1] != 'O' ||
            bytes [2] != 'X' ||
            bytes [3] != ' ')
        {
            Debug.LogError("Invalid VOX file, magic number mismatch");
            return(null);
        }

        return(MVImporter.LoadVOXFromData(bytes, generateFaces));
    }
Esempio n. 6
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");
        }
    }
Esempio n. 7
0
    public static GameObject[] CreateIndividualVoxelGameObjectsForChunk(MVVoxelChunk chunk, Color[] palatte, Transform parent, Material mat, float sizePerVox, MVVoxelChunk alphaMask, Vector3 origin)
    {
        List <GameObject> result = new List <GameObject> ();

        if (alphaMask != null && (alphaMask.sizeX != chunk.sizeX || alphaMask.sizeY != chunk.sizeY || alphaMask.sizeZ != chunk.sizeZ))
        {
            Debug.LogErrorFormat("Unable to create meshes from chunk : Chunk's size ({0},{1},{2}) differs from alphaMask chunk's size ({3},{4},{5})", chunk.sizeX, chunk.sizeY, chunk.sizeZ, alphaMask.sizeX, alphaMask.sizeY, alphaMask.sizeZ);
            return(result.ToArray());
        }

        for (int x = 0; x < chunk.sizeX; ++x)
        {
            for (int y = 0; y < chunk.sizeY; ++y)
            {
                for (int z = 0; z < chunk.sizeZ; ++z)
                {
                    if (chunk.voxels [x, y, z] != 0)
                    {
                        float px = (x - origin.x + 0.5f) * sizePerVox, py = (y - origin.y + 0.5f) * sizePerVox, pz = (z - origin.z + 0.5f) * sizePerVox;
                        Color c = palatte [chunk.voxels [x, y, z] - 1];

                        if (alphaMask != null && alphaMask.voxels[x, y, z] != 0)
                        {
                            c.a = (float)(alphaMask.voxels [x, y, z] - 1) / 255;
                        }

                        GameObject go = CreateGameObject(
                            parent,
                            new Vector3(px, py, pz),
                            string.Format("Voxel ({0}, {1}, {2})", x, y, z),
                            MVImporter.CubeMeshWithColor(sizePerVox, c),
                            mat);

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

                        result.Add(go);
                    }
                }
            }
        }

        return(result.ToArray());
    }
Esempio n. 8
0
    public void LoadVOXFile(string path, bool asIndividualVoxels)
    {
        ClearVoxMeshes();

        if (path != null && path.Length > 0)
        {
            MVMainChunk v = MVImporter.LoadVOX(path);

            if (v != null)
            {
                Material mat = (this.voxMaterial != null) ? this.voxMaterial : MVImporter.DefaultMaterial;
                if (paletteToTex)
                {
                    mat.mainTexture = v.PaletteToTexture();
                }

                if (!asIndividualVoxels)
                {
                    if (meshOrigin != null)
                    {
                        MVImporter.CreateVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox, meshOrigin.localPosition);
                    }
                    else
                    {
                        MVImporter.CreateVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox);
                    }
                }
                else
                {
                    if (meshOrigin != null)
                    {
                        MVImporter.CreateIndividualVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox, meshOrigin.localPosition);
                    }
                    else
                    {
                        MVImporter.CreateIndividualVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox);
                    }
                }

                this.vox = v;
            }
        }
        else
        {
            Debug.LogError("[MVVoxModel] Invalid file path");
        }
    }
Esempio n. 9
0
    public static GameObject[] CreateIndividualVoxelGameObjectsForChunk(MVVoxelChunk chunk, Color[] palatte, Transform parent, Material mat, float sizePerVox, Vector3 origin)
    {
        List <GameObject> result = new List <GameObject> ();

        for (int x = 0; x < chunk.sizeX; ++x)
        {
            for (int y = 0; y < chunk.sizeY; ++y)
            {
                for (int z = 0; z < chunk.sizeZ; ++z)
                {
                    if (chunk.voxels [x, y, z] != 0)
                    {
                        float px = (x - origin.x + 0.5f) * sizePerVox, py = (y - origin.y + 0.5f) * sizePerVox, pz = (z - origin.z + 0.5f) * sizePerVox;
                        int   cidx = chunk.voxels[x, y, z];

                        GameObject go = CreateGameObject(
                            parent,
                            new Vector3(px, py, pz),
                            string.Format("Voxel ({0}, {1}, {2})", x, y, z),
                            MVImporter.CubeMeshWithColor(sizePerVox, palatte[cidx - 1], cidx),
                            mat);

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

                        result.Add(go);
                    }
                }
            }
        }

        return(result.ToArray());
    }