Exemple #1
0
        private void readVoxFile(TextAsset voxFile)
        {
            Stream sw = new MemoryStream(voxFile.bytes);

            System.IO.BinaryReader br = new System.IO.BinaryReader(sw);
            var        vs             = VoxelFormater.ReadFromMagicaVoxel(br);
            SplitVoxel split          = new SplitVoxel(vs);

            for (int i = 0; i < _boxes.Count; ++i)
            {
                split.addBox(_boxes[i]);
            }
            VoxelStruct[] vses = split.doIt();
            _items.Clear();
            for (int i = 0; i < vses.Length; ++i)
            {
                vses [i].arrange();
                string md5  = VoxelFormater.GetMd5(vses [i]);
                Item   item = new Item();
                item.voxel = vses [i];
                item._MD5  = md5;
                item.data  = CreateData(md5, vses [i]);
                _items.Add(item);
            }
        }
Exemple #2
0
        public Task buildTask(string name, VoxelStruct vs, GeometryResult cb)
        {
            VoxelGeometry.MeshData data = null;
            TaskPack tp = new TaskPack(delegate(){
                vs.arrange();
                string md5 = VoxelFormater.GetMd5(vs);
                data       = this.LoadFromFile(GetKey(md5));
                if (data == null)
                {
                    return(buildData(vs, delegate(VoxelGeometry.MeshData result) {
                        data = result;
//						Debug.Log(md5);

                        this.SaveToFile(GetKey(md5), data);
                    }));
                }
                return(new Task());
            });


            TaskManager.PushBack(tp, delegate {
                if (this.gameObject.GetComponent <VoxelMesh>() == null)
                {
                    this.gameObject.AddComponent <VoxelMesh>();
                }
                VoxelMesh mesh = VoxelGeometry.Draw(name, data, this.gameObject, this._material);
                mesh.vs        = vs;
                cb(mesh);
            });
            return(tp);
        }
Exemple #3
0
 private void readVoxFile(TextAsset voxFile)
 {
     vs_ = createStruct(voxFile);
     vs_.arrange();
     this._md5            = VoxelFormater.GetMd5(vs_);
     this.data_           = createMeshData(this._md5);
     this.gameObject.name = voxFile.name;
 }
Exemple #4
0
        public VoxelStruct createStruct(TextAsset voxFile)
        {
            Stream sw = new MemoryStream(voxFile.bytes);

            System.IO.BinaryReader br = new System.IO.BinaryReader(sw);
            var vs = VoxelFormater.ReadFromMagicaVoxel(br);

            return(vs);
        }
Exemple #5
0
        public static VoxelGeometry.MeshData BuildMeshData(VoxelStruct vs)
        {
            vs.arrange();
            string md5 = VoxelFormater.GetMd5(vs);

            VoxelGeometry.MeshData data = LoadFromFile(GetKey(md5));
            if (data == null)
            {
                data = CreateMeshData(vs);
                SaveToFile(GetKey(md5), data);
            }
            return(data);
        }
Exemple #6
0
 public override void read()
 {
     if (_voxFile != null)
     {
         Stream sw = new MemoryStream(_voxFile.bytes);
         System.IO.BinaryReader br = new System.IO.BinaryReader(sw);
         if (_model != null)
         {
             VoxelStruct vs = VoxelFormater.ReadFromMagicaVoxel(br);
             _model.data = vs.datas.ToArray();
             _model.vs   = vs;
         }
     }
 }
Exemple #7
0
        public void arrange(bool normal = false)
        {
            HashSet <Color> palette = new HashSet <Color>();

            VectorInt3 min = new VectorInt3(9999, 9999, 9999);
            VectorInt3 max = new VectorInt3(-9999, -9999, -9999);

            for (int i = 0; i < this.datas.Count; ++i)
            {
                palette.Add(this.datas[i].color);

                VectorInt3 pos = this.datas [i].pos;

                min.x = Mathf.Min(pos.x, min.x);
                min.y = Mathf.Min(pos.y, min.y);
                min.z = Mathf.Min(pos.z, min.z);
                max.x = Mathf.Max(pos.x, max.x);
                max.y = Mathf.Max(pos.y, max.y);
                max.z = Mathf.Max(pos.z, max.z);
            }

            if (normal)
            {
                max = max - min;
                for (int i = 0; i < this.datas.Count; ++i)
                {
                    palette.Add(this.datas[i].color);

                    VectorInt3 pos = this.datas [i].pos;
                    this.datas [i].pos = pos - min;
                }
                min = new VectorInt3(0, 0, 0);
            }

            this.main      = new VoxelStruct.Main();
            this.main.name = "MAIN";
            this.main.size = 0;


            this.size        = new VoxelStruct.Size();
            this.size.name   = "SIZE";
            this.size.size   = 12;
            this.size.chunks = 0;

            this.size.box = new VectorInt3();


            this.size.box.x = max.x - min.x + 1;
            this.size.box.y = max.y - min.y + 1;
            this.size.box.z = max.z - min.z + 1;


            this.rgba = new VoxelStruct.Rgba();

            int size = Mathf.Max(palette.Count, 256);

            this.rgba.palette = new VectorInt4[size];
            int n = 0;

            foreach (Color c in palette)
            {
                this.rgba.palette [n] = VoxelFormater.Color2Bytes(c);
                ++n;
            }



            this.rgba.size   = this.rgba.palette.Length * 4;
            this.rgba.name   = "RGBA";
            this.rgba.chunks = 0;

            this.version = 150;

            this.main.chunks = 52 + this.rgba.palette.Length * 4 + this.datas.Count * 4;
        }
Exemple #8
0
        public static VoxelStruct ReadFromMagicaVoxel(System.IO.BinaryReader br)
        {
            VoxelStruct vs = new VoxelStruct();


            // check out http://voxel.codeplex.com/wikipage?title=VOX%20Format&referringTitle=Home for the file format used below
            // we're going to return a voxel chunk worth of data
            //ushort[] data = new ushort[32 * 128 * 32];
            VectorInt4[] palette = null;
            Point[]      points  = null;


            string vox = new string(br.ReadChars(4));

            if (vox != "VOX ")
            {
                Debug.Log(vox);
                return(vs);
            }

            int version = br.ReadInt32();

            vs.version = version;
            VectorInt3 box       = new VectorInt3();
            bool       subsample = false;


            while (br.BaseStream.Position < br.BaseStream.Length)
            {
                // each chunk has an ID, size and child chunks
                string name   = new string(br.ReadChars(4));
                int    size   = br.ReadInt32();
                int    chunks = br.ReadInt32();
                // Debug.LogError (chunkName);
                // there are only 2 chunks we only care about, and they are SIZE and XYZI
                if (name == "MAIN")
                {
                    vs.main        = new VoxelStruct.Main();
                    vs.main.size   = size;
                    vs.main.name   = name;
                    vs.main.chunks = chunks;
                }
                if (name == "SIZE")
                {
                    box.x = br.ReadInt32();
                    box.y = br.ReadInt32();
                    box.z = br.ReadInt32();


                    vs.size        = new VoxelStruct.Size();
                    vs.size.size   = 12;
                    vs.size.name   = name;
                    vs.size.chunks = chunks;
                    vs.size.box    = box;

                    if (box.x > 32 || box.y > 32)
                    {
                        subsample = true;
                    }

                    br.ReadBytes(size - 4 * 3);
                }
                else if (name == "XYZI")
                {
                    // XYZI contains n voxels
                    int count = br.ReadInt32();
                    //int div = (subsample ? 2 : 1);
                    // each voxel has x, y, z and color index values
                    points = new Point[count];
                    for (int i = 0; i < points.Length; i++)
                    {
                        points [i] = VoxelFormater.CreatePoint(br, subsample);                         //new Data (stream, subsample);
                    }
                }
                else if (name == "RGBA")
                {
                    int n = size / 4;
                    palette = new VectorInt4[n];
                    for (int i = 0; i < n; i++)
                    {
                        byte r = br.ReadByte();
                        byte g = br.ReadByte();
                        byte b = br.ReadByte();
                        byte a = br.ReadByte();
                        palette[i].x = r;
                        palette[i].y = g;
                        palette[i].z = b;
                        palette[i].w = a;
                    }

                    vs.rgba         = new VoxelStruct.Rgba();
                    vs.rgba.size    = size;
                    vs.rgba.name    = name;
                    vs.rgba.chunks  = chunks;
                    vs.rgba.palette = palette;
                }
                else
                {
                    br.ReadBytes(size);                       // read any excess bytes
                }
            }
            vs.datas = CreateVoxelDatas(points, palette);
            return(vs);
        }
Exemple #9
0
        public static VoxelStruct ReadFromMagicaVoxel(System.IO.BinaryReader br)
        {
            VoxelStruct vs = new VoxelStruct();

            VectorInt4[] palette = null;
            Point[]      points  = null;

            string vox = new string(br.ReadChars(4));

            if (vox != "VOX ")
            {
                return(vs);
            }

            int version = br.ReadInt32();

            vs.version = version;
            VectorInt3 box       = new VectorInt3();
            bool       subsample = false;


            while (br.BaseStream.Position < br.BaseStream.Length)
            {
                // each chunk has an ID, size and child chunks
                string name   = new string(br.ReadChars(4));
                int    size   = br.ReadInt32();
                int    chunks = br.ReadInt32();
                // Debug.LogError (chunkName);
                // there are only 2 chunks we only care about, and they are SIZE and XYZI
                if (name == "MAIN")
                {
                    vs.main        = new VoxelStruct.Main();
                    vs.main.size   = size;
                    vs.main.name   = name;
                    vs.main.chunks = chunks;
                }
                if (name == "SIZE")
                {
                    box.x = br.ReadInt32();
                    box.y = br.ReadInt32();
                    box.z = br.ReadInt32();


                    vs.size        = new VoxelStruct.Size();
                    vs.size.size   = 12;
                    vs.size.name   = name;
                    vs.size.chunks = chunks;
                    vs.size.box    = box;

                    if (box.x > 32 || box.y > 32)
                    {
                        subsample = true;
                    }

                    br.ReadBytes(size - 4 * 3);
                }
                else if (name == "XYZI")
                {
                    int count = br.ReadInt32();
                    points = new Point[count];
                    for (int i = 0; i < points.Length; i++)
                    {
                        points [i] = VoxelFormater.CreatePoint(br, subsample);                         //new Data (stream, subsample);
                    }
                }
                else if (name == "RGBA")
                {
                    int n = size / 4;
                    palette = new VectorInt4[n];
                    for (int i = 0; i < n; i++)
                    {
                        byte r = br.ReadByte();
                        byte g = br.ReadByte();
                        byte b = br.ReadByte();
                        byte a = br.ReadByte();
                        palette[i].x = r;
                        palette[i].y = g;
                        palette[i].z = b;
                        palette[i].w = a;
                    }

                    vs.rgba         = new VoxelStruct.Rgba();
                    vs.rgba.size    = size;
                    vs.rgba.name    = name;
                    vs.rgba.chunks  = chunks;
                    vs.rgba.palette = palette;
                }
                else
                {
                    br.ReadBytes(size);                       // read any excess bytes
                }
            }
            vs.datas = CreateVoxelDatas(points, palette);
            return(vs);
        }