Exemplo n.º 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);
            }
        }
Exemplo n.º 2
0
        public static void WriteToBinary(VoxelStruct vs, System.IO.BinaryWriter bw)
        {
            bw.Write("VOX ".ToCharArray());
            MagicaVoxel magic = new MagicaVoxel(vs);

            bw.Write((int)magic.version);


            if (magic.main != null)
            {
                bw.Write(magic.main.name.ToCharArray());
                bw.Write((int)magic.main.size);
                bw.Write((int)magic.main.chunks);
            }

            if (magic.size != null)
            {
                bw.Write(magic.size.name.ToCharArray());
                bw.Write((int)magic.size.size);
                bw.Write((int)magic.size.chunks);
                bw.Write((int)magic.size.box.x);
                bw.Write((int)magic.size.box.y);
                bw.Write((int)magic.size.box.z);
            }


            if (magic.rgba != null && magic.rgba.palette != null)
            {
                int length = magic.rgba.palette.Length;
                bw.Write(magic.rgba.name.ToCharArray());
                bw.Write((int)(length * 4));
                bw.Write((int)magic.size.chunks);


                for (int i = 0; i < length; i++)
                {
                    VectorInt4 c = magic.rgba.palette [i];
                    bw.Write((byte)(c.x));
                    bw.Write((byte)(c.y));
                    bw.Write((byte)(c.z));
                    bw.Write((byte)(c.w));
                }
            }
            Point[] points = WritePoints(vs.datas, magic.rgba.palette);
            bw.Write("XYZI".ToCharArray());

            bw.Write((int)(points.Length * 4) + 4);
            bw.Write((int)0);

            bw.Write((int)points.Length);

            for (int i = 0; i < points.Length; ++i)
            {
                Point p = points[i];
                bw.Write((byte)(p.x));
                bw.Write((byte)(p.y));
                bw.Write((byte)(p.z));
                bw.Write((byte)(p.i));
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 4
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);
        }