// Update is called once per frame
        private void Update()
        {
            if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1f)
            {
                if ((voxelObject == null && !isBakedVolume) || (NumFrames <= 0) || !IsPlaying)
                {
                    return;
                }

                currentFrameTime += Time.deltaTime;
                if (currentFrameTime >= Interval)
                {
                    currentFrameTime = 0;

                    CurrentFrame += dir;
                    if (dir == 1 && CurrentFrame == NumFrames)
                    {
                        if (PingPong)
                        {
                            CurrentFrame--;
                            dir = -1;
                        }
                        else
                        {
                            CurrentFrame = 0;
                        }
                    }
                    else if (dir == -1 && CurrentFrame == 0)
                    {
                        dir = 1;
                    }

                    if (!isBakedVolume)
                    {
                        voxelObject.SetFrame(CurrentFrame);
                    }
                    else
                    {
                        int thisFrame = 0;
                        for (int i = 0; i < transform.childCount; i++)
                        {
                            if (transform.GetChild(i).name.ToLower().StartsWith("frame"))
                            {
                                transform.GetChild(i).gameObject.SetActive(false);
                                if (CurrentFrame == thisFrame)
                                {
                                    transform.GetChild(i).gameObject.SetActive(true);
                                }

                                thisFrame++;
                            }
                        }
                    }
                }
            }
        }
        public static void FromMagica(BinaryReader stream, GameObject root, float voxelSize, bool centerPivot)
        {
            // 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

            Color32[] colors = new Color32[256];

            for (int i = 1; i < 256; i++)
            {
                uint hexval = defaultColors[i];
                byte cb     = (byte)((hexval >> 16) & 0xFF);
                byte cg     = (byte)((hexval >> 8) & 0xFF);
                byte cr     = (byte)((hexval >> 0) & 0xFF);

                colors[i - 1] = new Color32(cr, cg, cb, 255);
            }

            MagicaVoxelData[]        voxelData = null;
            List <MagicaVoxelData[]> frames    = new List <MagicaVoxelData[]>();

            //int numFrames = 1;


            string magic = new string(stream.ReadChars(4));

            //int version =
            stream.ReadInt32();


            // a MagicaVoxel .vox file starts with a 'magic' 4 character 'VOX ' identifier
            if (magic == "VOX ")
            {
                int  sizex = 0, sizey = 0, sizez = 0;
                bool subsample = false;

                while (stream.BaseStream.Position < stream.BaseStream.Length)
                {
                    // each chunk has an ID, size and child chunks
                    char[] chunkId   = stream.ReadChars(4);
                    int    chunkSize = stream.ReadInt32();
                    //int childChunks =
                    stream.ReadInt32();
                    string chunkName = new string(chunkId);

                    if (chunkName == "PACK")
                    {
                        stream.ReadInt32();
                    }
                    else if (chunkName == "SIZE")
                    {
                        sizex = stream.ReadInt32();
                        sizez = stream.ReadInt32();
                        sizey = stream.ReadInt32();

                        //if (sizex > 32 || sizey > 32) subsample = true;

                        stream.ReadBytes(chunkSize - 4 * 3);
                    }
                    else if (chunkName == "XYZI")
                    {
                        // XYZI contains n voxels
                        int numVoxels = stream.ReadInt32();
                        // int div = (subsample ? 2 : 1);

                        // each voxel has x, y, z and color index values
                        voxelData = new MagicaVoxelData[numVoxels];
                        for (int i = 0; i < voxelData.Length; i++)
                        {
                            voxelData[i] = new MagicaVoxelData(stream, subsample);
                        }

                        frames.Add(voxelData);
                    }
                    else if (chunkName == "RGBA")
                    {
                        for (int i = 0; i < 256; i++)
                        {
                            byte r = stream.ReadByte();
                            byte g = stream.ReadByte();
                            byte b = stream.ReadByte();
                            byte a = stream.ReadByte();

                            // convert RGBA to our custom voxel format (16 bits, 0RRR RRGG GGGB BBBB)
                            colors[i] = new Color32(r, g, b, a);
                        }
                    }
                    else
                    {
                        stream.ReadBytes(chunkSize);    // read any excess bytes
                    }
                }

                if (voxelData.Length == 0)
                {
                    return;                        // failed to read any valid voxel data
                }
                if (root != null)
                {
                    Volume voxelVolume = root.GetComponent <Volume>();

                    voxelVolume.XSize = sizex;
                    voxelVolume.YSize = sizey;
                    voxelVolume.ZSize = sizez;
                    voxelVolume.Frames[voxelVolume.CurrentFrame].XSize  = sizex;
                    voxelVolume.Frames[voxelVolume.CurrentFrame].YSize  = sizey;
                    voxelVolume.Frames[voxelVolume.CurrentFrame].ZSize  = sizez;
                    voxelVolume.Frames[voxelVolume.CurrentFrame].Voxels = new Voxel[sizex * sizey * sizez];
                    for (int i = 0; i < voxelVolume.Frames[voxelVolume.CurrentFrame].Voxels.Length; i++)
                    {
                        voxelVolume.Frames[voxelVolume.CurrentFrame].Voxels[i].Value = 128;
                    }
                    voxelVolume.VoxelSize = voxelSize;

                    if (centerPivot)
                    {
                        voxelVolume.Pivot = (new Vector3(voxelVolume.XSize, voxelVolume.YSize, voxelVolume.ZSize) * voxelVolume.VoxelSize) / 2f;
                        voxelVolume.UpdatePivot();
                    }

                    foreach (MagicaVoxelData[] d in frames)
                    {
                        foreach (MagicaVoxelData v in d)
                        {
                            try
                            {
                                voxelVolume.Frames[voxelVolume.CurrentFrame].Voxels[v.x + sizex * (v.z + sizey * v.y)] = new Voxel()
                                {
                                    State = VoxelState.Active,
                                    Color = colors[v.color - 1],
                                    Value = 128
                                };
                            }
                            catch (Exception)
                            {
                                Debug.Log(v.x + " " + v.y + " " + v.z);
                            }
                        }

                        if (frames.IndexOf(d) < frames.Count - 1)
                        {
                            voxelVolume.AddFrame(voxelVolume.CurrentFrame + 1);
                            voxelVolume.Frames[voxelVolume.CurrentFrame].Voxels = new Voxel[sizex * sizey * sizez];
                        }
                    }

                    voxelVolume.SetFrame(0);
                    voxelVolume.CreateChunks();
                    voxelVolume.SaveForSerialize();
                }
            }
        }