Esempio n. 1
0
 public static void WriteVOXChunk(this BinaryWriter bin_writer, VOXChunk chunk)
 {
     bin_writer.Write(chunk.ID);
     bin_writer.Write(chunk.Content_size);
     bin_writer.Write(chunk.Children_size);
     bin_writer.Write(chunk.content);
     foreach (VOXChunk child in chunk.Childrens)
     {
         bin_writer.WriteVOXChunk(child);
     }
 }
Esempio n. 2
0
        public void CreateFile(VOXObject obj, string path = "Assets/NewModel.vox")
        {
            BinaryWriter bin_writer;

            try {
                bin_writer = new BinaryWriter(System.IO.File.Create(path));
            } catch (IOException e) {
                Debug.Log(e.Message + "\nCannot create file");
                return;
            }
            //MAIN
            VOXChunk main_chunk = new VOXChunk(MAIN_ID);
            //SIZE
            VOXChunk size_chunk = new VOXChunk(SIZE_ID);

            byte[] size_x_byte = BitConverter.GetBytes(obj.size_x);
            byte[] size_y_byte = BitConverter.GetBytes(obj.size_y);
            byte[] size_z_byte = BitConverter.GetBytes(obj.size_z);
            byte[] sizes       = new byte[12];

            for (int i = 0; i < 4; i++)
            {
                sizes[i + 0] = size_x_byte[i];
                sizes[i + 4] = size_y_byte[i];
                sizes[i + 8] = size_z_byte[i];
            }
            size_chunk.content = sizes;
            main_chunk.children.Add(size_chunk);

            //XYZI
            VOXChunk     xyzi_chunk = new VOXChunk(XYZI_ID);
            List <Voxel> voxels     = obj.NonEmptyVoxels;
            uint         nb_voxels  = (uint)voxels.Count;

            byte[] voxels_byte = new byte[4 * (nb_voxels + 1)];
            BitConverter.GetBytes(nb_voxels).CopyTo(voxels_byte, 0);
            for (int i = 0; i < nb_voxels; i++)
            {
                voxels[i].ToBytes().CopyTo(voxels_byte, 4 * (i + 1));
            }
            xyzi_chunk.content = voxels_byte;
            main_chunk.children.Add(xyzi_chunk);

            Color[] palette = obj.Palette;
            if (palette != null)
            {
                VOXChunk palette_chunk = new VOXChunk(PALETTE_ID);
                byte[]   colors        = new byte[VOXObject.PALETTE_LENGTH * 4];
                byte[]   byte_color;
                for (int i = 0; i < VOXObject.PALETTE_LENGTH; i++)
                {
                    byte_color        = palette[i].ToBytes();
                    colors[4 * i + 0] = byte_color[0];
                    colors[4 * i + 1] = byte_color[1];
                    colors[4 * i + 2] = byte_color[2];
                    colors[4 * i + 3] = byte_color[3];
                }
                palette_chunk.content = colors;
                main_chunk.children.Add(palette_chunk);
            }

            try {
                bin_writer.Write(System.Text.Encoding.ASCII.GetBytes(HEADER_ID));
                bin_writer.Write(VERSION);
                bin_writer.WriteVOXChunk(main_chunk);
            } catch (IOException e) {
                Debug.Log(e.Message + "\nError writing to file");
            }

            /* DEPLETED
             * try {
             *      bin_writer.Write(System.Text.Encoding.ASCII.GetBytes(header_id));		//"VOX "
             *      bin_writer.Write(version);			//version
             *
             *      bin_writer.Write(System.Text.Encoding.ASCII.GetBytes(main_id));			//"MAIN"
             *      bin_writer.Write((uint)0);			//Content size of main
             *      bin_writer.Write((uint)
             *              4 +			//SIZE header
             *              4 +			//SIZE content size
             *              4 +			//SIZE children size
             *              12 +        //SIZE content
             *              4 +         //XYZI header
             *              4 +         //XYZI content size
             *              4 +         //XYZI children size
             *              4*(nb_voxels+1)
             *      );
             *
             *      bin_writer.Write(System.Text.Encoding.ASCII.GetBytes(size_id));			//"SIZE"
             *      bin_writer.Write((uint)12);
             *      bin_writer.Write(empty4);           //Children size of SIZE
             *      //SIZE content: 3 x uint representing dimension length of the model
             *      bin_writer.Write((uint)obj.size_x);
             *      bin_writer.Write((uint)obj.size_y);
             *      bin_writer.Write((uint)obj.size_z);
             *
             *      bin_writer.Write(System.Text.Encoding.ASCII.GetBytes(xyzi_id));			//"XYZI"
             *      bin_writer.Write(4*(nb_voxels+1));	//Content size of XYZI : (Nb de voxel*4) + 4
             *      bin_writer.Write((uint)0);			//Children size of XYZI
             *      bin_writer.Write(nb_voxels);
             *
             *      for(int i = 0; i < nb_voxels; i++) {
             *              bin_writer.Write(voxels[i].pos_x);
             *              bin_writer.Write(voxels[i].pos_y);
             *              bin_writer.Write(voxels[i].pos_z);
             *              bin_writer.Write(voxels[i].color);
             *      }
             *
             * } catch(IOException e) {
             *      Debug.Log(e.Message + "\nError writing to file");
             * }*/

            bin_writer.Close();
        }