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

            System.IO.BinaryReader br    = new System.IO.BinaryReader(sw);
            MagicaVoxel            magic = null;
            var        vs    = MagicaVoxelFormater.ReadFromBinary(br).vs;
            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  = MagicaVoxelFormater.GetMd5(vses [i]);
                Item   item = new Item();
                item.voxel = vses [i];
                item._MD5  = md5;
                //item.data = CreateData (md5, vses [i]);
                _items.Add(item);
            }
        }
示例#2
0
        public static VoxelMeshData Struct2DataInCache(VoxelStruct vs)
        {
            string        md5  = MagicaVoxelFormater.GetMd5(vs);
            VoxelMeshData data = LoadFromCache(GetKey(md5));

            if (data == null)
            {
                data = VoxelBuilder.Struct2Data(vs);
                SaveToCache(GetKey(md5), data);
            }
            return(data);
        }
示例#3
0
        public static GameObject Building(GameObject obj, System.IO.BinaryReader br, Material material, int reversal = 0)
        {
            MagicaVoxel magica = MagicaVoxelFormater.ReadFromBinary(br);
            VoxelStruct vs     = VoxelStruct.Reversal(magica.vs, reversal); // VoxelBuilder.Reversal(magica.vs, reversal);
            var         data   = VoxelBuilder.Struct2Data(vs);              // VoxelBuilderHelper.Struct2DataInCache (vs);
            var         mesh   = VoxelBuilder.Data2Mesh(data);
            var         filter = VoxelBuilder.Mesh2Filter(mesh);

            VoxelBuilder.FilterAddRenderer(filter, material);


            filter.transform.SetParent(obj.transform);
            filter.transform.localEulerAngles = Vector3.zero;
            filter.transform.localPosition    = data.offset;
            filter.gameObject.layer           = obj.layer;
            filter.name = "Voxel";
            return(filter.gameObject);
        }
示例#4
0
 // Update is called once per frame
 void Update()
 {
     if (_building == true && _voxFile != null)
     {
         init();
         if (_voxFile != null)
         {
             var vs     = MagicaVoxelFormater.ReadFromFile(_voxFile).vs;
             var data   = VoxelBuilder.Struct2Data(vs);
             var mesh   = VoxelBuilder.Data2Mesh(data);
             var filter = VoxelBuilder.Mesh2Filter(mesh);
             VoxelBuilder.FilterAddRenderer(filter, this._material);
             filter.transform.SetParent(this.transform);
             filter.transform.localEulerAngles = Vector3.zero;
             filter.transform.localPosition    = data.offset;
             filter.gameObject.layer           = this.gameObject.layer;
             filter.name = "Voxel";
         }
         _building = false;
     }
 }
示例#5
0
        /*public static MagicaVoxel ReadFromFile(TextAsset file){
         *      Stream sw = new MemoryStream(file.bytes);
         *      System.IO.BinaryReader br = new System.IO.BinaryReader (sw);
         *      return ReadFromBinary(br);
         *
         * }
         * public static MagicaVoxel ReadFromUrl(string url){
         *      return null;
         * }*/
        public static MagicaVoxel ReadFromBinary(System.IO.BinaryReader br)
        {
            MagicaVoxel magic = new MagicaVoxel(new VoxelStruct());

            VectorInt4[] palette = null;
            Point[]      points  = null;
            string       vox     = new string(br.ReadChars(4));

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

            int version = br.ReadInt32();

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


            while (br.BaseStream.Position + 12 < br.BaseStream.Length)
            {
                string name   = new string(br.ReadChars(4));
                int    size   = br.ReadInt32();
                int    chunks = br.ReadInt32();


                if (name == "MAIN")
                {
                    magic.main        = new MagicaVoxel.Main();
                    magic.main.size   = size;
                    magic.main.name   = name;
                    magic.main.chunks = chunks;
                }
                else if (name == "SIZE")
                {
                    box.x = br.ReadInt32();
                    box.y = br.ReadInt32();
                    box.z = br.ReadInt32();


                    magic.size        = new MagicaVoxel.Size();
                    magic.size.size   = 12;
                    magic.size.name   = name;
                    magic.size.chunks = chunks;
                    magic.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] = MagicaVoxelFormater.ReadPoint(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;
                    }

                    magic.rgba         = new MagicaVoxel.Rgba();
                    magic.rgba.size    = size;
                    magic.rgba.name    = name;
                    magic.rgba.chunks  = chunks;
                    magic.rgba.palette = palette;
                }
                else
                {
                    if (br.BaseStream.Position + size >= br.BaseStream.Length)
                    {
                        break;
                    }
                    else
                    {
                        br.ReadBytes(size);
                    }
                }
            }
            magic.vs.datas = CreateVoxelDatas(points, palette);
            return(magic);
        }
示例#6
0
        private void arrange(VoxelStruct st, bool normal = false)
        {
            vs_ = st;
            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 < st.datas.Count; ++i)
            {
                palette.Add(st.datas[i].color);

                VectorInt3 pos = st.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 < st.datas.Count; ++i)
                {
                    palette.Add(st.datas[i].color);
                    var data = st.datas [i];
                    data.pos    -= min;
                    st.datas [i] = data;                   //.pos = pos - min;
                }
                min = new VectorInt3(0, 0, 0);
            }

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


            this.size        = new MagicaVoxel.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 MagicaVoxel.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] = MagicaVoxelFormater.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 + st.datas.Count * 4;
        }
示例#7
0
 public static MagicaVoxel ReadFromFile(TextAsset file)
 {
     System.IO.BinaryReader br = GDGeek.VoxelReader.ReadFromFile(file);
     return(MagicaVoxelFormater.ReadFromBinary(br));
 }