Exemple #1
0
        private List <VoxelDesc> MakeVoxelsList(CubeArray3D _array)
        {
            List <VoxelDesc> list = new List <VoxelDesc>();
            SmallCube        cube;
            int x, y, z;

            for (x = 0; x < _array.CUBESIZEX; x++)
            {
                for (y = 0; y < _array.CUBESIZEY; y++)
                {
                    for (z = 0; z < _array.CUBESIZEZ; z++)
                    {
                        _array.GetCube(x, y, z, out cube);
                        if (cube.byMatL0 != 0)
                        {
                            var vd = new VoxelDesc();
                            vd.x = x; vd.y = y; vd.z = z;
                            vd.i = (int)cube.byMatL0;
                            list.Add(vd);
                        }
                    }
                }
            }
            return(list);
        }
        static public CubeArray3D ReSize(CubeArray3D _array, int _newsizeX, int _newsizeY, int _newsizeZ)
        {
            Debug.Assert(_newsizeX != _array.CUBESIZEX || _newsizeY == _array.CUBESIZEY || _newsizeZ == _array.CUBESIZEZ);

            CubeArray3D newCubeArray = new CubeArray3D();

            newCubeArray.SetSize(_newsizeX, _newsizeY, _newsizeZ);

            int middleX = (_array.CUBESIZEX >> 1);
            int middleY = (_array.CUBESIZEY >> 1);
            int middleZ = (_array.CUBESIZEZ >> 1);

            int newmiddleX = (_newsizeX >> 1);
            int newmiddleY = (_newsizeY >> 1);
            int newmiddleZ = (_newsizeZ >> 1);

            SmallCube c;
            int       x, y, z;
            int       x2, y2, z2;

            for (x = 0; x < _array.CUBESIZEX; x++)
            {
                for (z = 0; z < _array.CUBESIZEZ; z++)
                {
                    for (y = 0; y < _array.CUBESIZEY; y++)
                    {
                        x2 = (x - middleX) + newmiddleX;
                        y2 = (y - middleY) + newmiddleY;
                        z2 = (z - middleZ) + newmiddleZ;

                        if (x2 < 0 || y2 < 0 || z2 < 0)
                        {
                            continue;
                        }
                        if (x2 >= _newsizeX || y2 >= _newsizeY || z2 >= _newsizeZ)
                        {
                            continue;
                        }

                        _array.GetCube(x, y, z, out c); //Get Source
                        if (c.byMatL0 != 0)
                        {
                            //Set destination
                            newCubeArray.SetCube(x2, y2, z2, c);
                        }
                    }
                }
            }

            //Return new buffer
            return(newCubeArray);
        }
        public CubeBrush(Game _game, CubeArray3D _carray, Color[] _palette)
        {
            Game = _game;


            //Set Palette
            m_colPalette = _palette;

            //Set Array
            m_array = _carray;
            InitFromNewArray();

            //Meshes
            m_genMesh = new GenMesh();
        }
        static public void Copy(CubeArray3D _src, CubeArray3D _dst)
        {
            _dst.Clear();
            _dst.SetSize(_src.CUBESIZEX, _src.CUBESIZEY, _src.CUBESIZEZ);

            SmallCube c;
            int       x, y, z;

            for (x = 0; x < _src.CUBESIZEX; x++)
            {
                for (z = 0; z < _src.CUBESIZEZ; z++)
                {
                    for (y = 0; y < _src.CUBESIZEY; y++)
                    {
                        _src.GetCube(x, y, z, out c); //Get Source
                        _dst.SetCube(x, y, z, c);     //Set Destination
                    }
                }
            }
        }
        public void SetSize(int _sizeX, int _sizeY, int _sizeZ)
        {
            //Check if size have changed
            if (m_array != null && (_sizeX == m_array.CUBESIZEX && _sizeY == m_array.CUBESIZEY && _sizeZ == m_array.CUBESIZEZ))
            {
                return;
            }

            //3D Array
            if (m_array == null)
            {
                m_array = new CubeArray3D();
                m_array.SetSize(_sizeX, _sizeY, _sizeZ);
            }
            else
            {
                m_array = CubeArray3D.ReSize(m_array, _sizeX, _sizeY, _sizeZ);
            }

            //Init
            InitFromNewArray();
        }
Exemple #6
0
 public MagicaVoxelLoader()
 {
     m_carray  = new CubeArray3D();
     m_palette = new Color[256];
 }
Exemple #7
0
        public bool WriteFile(BinaryWriter _bw, CubeArray3D _array, Color[] _palette)
        {
            int i;

            //Make voxel array
            var list = MakeVoxelsList(_array);

            //////////////////////////////////////////////////
            //Write Header
            //4 bytes: magic number ('V' 'O' 'X' 'space' )
            //4 bytes: version number (current version is 150 )
            _bw.Write((UInt32)0x20584F56);
            _bw.Write((UInt32)150);


            //////////////////////////////////////////////////
            // header
            //4 bytes: chunk id
            //4 bytes: size of chunk contents (n)
            //4 bytes: total size of children chunks(m)

            //// chunk content
            //n bytes: chunk contents

            //// children chunks : m bytes
            //{ child chunk 0 }
            //{ child chunk 1 }

            ///////////////////////////////////////////////////
            //Chunk MAIN
            UInt32 chunkLen_SIZE = 4 * 3;
            UInt32 chunkLen_XYZI = (UInt32)(list.Count * 4) + 4;
            UInt32 chunkLen_RGBA = (UInt32)(256 * 4);

            UInt32 chunkTotalChildSize = chunkLen_SIZE + chunkLen_XYZI + chunkLen_RGBA + (4 * 3 * 3);

            _bw.Write(new char[] { 'M', 'A', 'I', 'N' }); //chunkName

            _bw.Write((UInt32)0);                         //chunkSize
            _bw.Write(chunkTotalChildSize);               //chunkTotalChildSize = 0 ?????


            ///////////////////////////////////////////////////
            //Chunk SIZE
            _bw.Write(new char[] { 'S', 'I', 'Z', 'E' }); //chunkName

            _bw.Write(chunkLen_SIZE);                     //chunkSize = (4 bytes x 3 : x, y, z )
            _bw.Write((UInt32)0);                         //chunkTotalChildSize = 0

            _bw.Write((Int32)m_array.CUBESIZEX);
            _bw.Write((Int32)m_array.CUBESIZEZ); //Reversed y-z
            _bw.Write((Int32)m_array.CUBESIZEY); //Reversed y-z

            ///////////////////////////////////////////////////
            //Chunk XYZI
            _bw.Write(new char[] { 'X', 'Y', 'Z', 'I' });

            _bw.Write(chunkLen_XYZI); //chunkSize
            _bw.Write((UInt32)0);     //chunkTotalChildSize = 0

            //(numVoxels : 4 bytes )
            _bw.Write(list.Count);

            //(each voxel: 1 byte x 4 : x, y, z, colorIndex ) x numVoxels
            for (i = 0; i < list.Count; i++)
            {
                var vox = list[i];
                _bw.Write((byte)vox.x);
                _bw.Write((byte)(m_array.CUBESIZEZ - vox.z - 1)); //Reversed y-z
                _bw.Write((byte)vox.y);                           //Reversed y-z
                _bw.Write((byte)vox.i);
            }

            ///////////////////////////////////////////////////
            //Chunk RGBA
            _bw.Write(new char[] { 'R', 'G', 'B', 'A' });
            _bw.Write(chunkLen_RGBA); //chunkSize
            _bw.Write((UInt32)0);     //chunkTotalChildSize = 0

            //(each pixel: 1 byte x 4 : r, g, b, a ) x 256
            for (i = 0; i < 256; i++)
            {
                var col = _palette[i]; // first color ( at position 0 ) is corresponding to color index 1.
                _bw.Write((byte)col.R);
                _bw.Write((byte)col.G);
                _bw.Write((byte)col.B);
                _bw.Write((byte)col.A);
            }


            return(true);
        }
Exemple #8
0
 public MagicaVoxelSaver()
 {
     m_array = null;
 }