Example #1
0
        private void arrange(VoxelStruct st, bool normal = false)
        {
            structure_ = st;
            HashSet <Color> palette = new HashSet <Color>();

            Vector3Int min = new Vector3Int(9999, 9999, 9999);
            Vector3Int max = new Vector3Int(-9999, -9999, -9999);

            for (int i = 0; i < st.count; ++i)
            {
                palette.Add(st.getData(i).color);

                Vector3Int pos = st.getData(i).position;

                min.x = Mathf.Min(pos.x, min.x);
                min.y = Mathf.Min(pos.y, min.y);
                min.z = Mathf.Min(pos.z, min.z);
                max.x = Mathf.Max(pos.x, max.x);
                max.y = Mathf.Max(pos.y, max.y);
                max.z = Mathf.Max(pos.z, max.z);
            }

            if (normal)
            {
                max = max - min;
                for (int i = 0; i < st.count; ++i)
                {
                    palette.Add(st.getData(i).color);
                    var data = st.getData(i);
                    data.position -= min;
                    st.setData(i, data);                   //.pos = pos - min;
                }
                min = new Vector3Int(0, 0, 0);
            }

            this.main      = new MagicaVoxel.Main();
            this.main.name = "MAIN";
            this.main.size = 0;


            this.size        = new MagicaVoxel.Size();
            this.size.name   = "SIZE";
            this.size.size   = 12;
            this.size.chunks = 0;

            this.size.box = new Vector3Int();


            this.size.box.x = max.x - min.x + 1;
            this.size.box.y = max.y - min.y + 1;
            this.size.box.z = max.z - min.z + 1;


            this.rgba = new MagicaVoxel.Rgba();

            int size = Mathf.Max(palette.Count, 256);

            this.rgba.palette = new Vector4Int[size];
            int n = 0;

            foreach (Color c in palette)
            {
                this.rgba.palette [n] = MagicaVoxelFormater.Color2Bytes(c);
                ++n;
            }



            this.rgba.size   = this.rgba.palette.Length * 4;
            this.rgba.name   = "RGBA";
            this.rgba.chunks = 0;

            this.version = 150;

            this.main.chunks = 52 + this.rgba.palette.Length * 4 + st.count * 4;
        }