Beispiel #1
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);
        }
Beispiel #2
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);
        }