private static List <VoxelData> CreateVoxelDatas(Point[] points, VectorInt4[] palette)
        {
            List <VoxelData> datas = new List <VoxelData>();

            for (int i = 0; i < points.Length; ++i)
            {
                VoxelData data = new VoxelData();
                data.pos.x = points[i].x;
                data.pos.y = points[i].z;
                data.pos.z = points[i].y;

                if (palette == null)
                {
                    ushort c = palette_[points[i].i - 1];

                    data.color = Short2Color(c);
                }
                else
                {
                    VectorInt4 v = palette[points[i].i - 1];
                    data.color = Bytes2Color(v);;
                }

                datas.Add(data);
            }



            return(datas);
        }
Beispiel #2
0
 public VectorInt4(VectorInt4 v4)
 {
     this.x = v4.x;
     this.y = v4.y;
     this.z = v4.z;
     this.w = v4.w;
 }
Beispiel #3
0
 public VectorInt4(VectorInt4 v4)
 {
     this.x = v4.x;
     this.y = v4.y;
     this.z = v4.z;
     this.w = v4.w;
 }
Beispiel #4
0
        public void buildMesh(Vector3 position)
        {
            VoxelDrawMesh draw = new VoxelDrawMesh();

            Debug.Log(real_.Count + "count");
            foreach (KeyValuePair <VectorInt3, VoxelShadowHandler> kv in this.real_)
            {
                VectorInt4 v = draw.addRect(Vector3.up, kv.Key + new VectorInt3(0, 0, -1), VoxelShadowHandler.GetUV(kv.Value.real), VoxelShadowHandler.GetUV(kv.Value.ghost), Color.black);
            }

            _mesh = draw.crateMeshFilter("Shadow", _material);
            if (_offset != null)
            {
                _mesh.gameObject.transform.SetParent(this._offset.transform);
            }
            else
            {
                _mesh.gameObject.transform.SetParent(this.transform);
            }

            _mesh.gameObject.transform.localPosition = Vector3.zero;
            _mesh.gameObject.transform.localScale    = Vector3.one;
            _mesh.gameObject.transform.localRotation = Quaternion.Euler(Vector3.zero);

            _mesh.gameObject.SetActive(true);
            Renderer renderer = _mesh.GetComponent <Renderer> ();

            renderer.material = this._material;
            showMesh(position);
        }
        public static void WriteToBinary(VoxelStruct vs, System.IO.BinaryWriter bw)
        {
            bw.Write("VOX ".ToCharArray());
            MagicaVoxel magic = new MagicaVoxel(vs);

            bw.Write((int)magic.version);


            if (magic.main != null)
            {
                bw.Write(magic.main.name.ToCharArray());
                bw.Write((int)magic.main.size);
                bw.Write((int)magic.main.chunks);
            }

            if (magic.size != null)
            {
                bw.Write(magic.size.name.ToCharArray());
                bw.Write((int)magic.size.size);
                bw.Write((int)magic.size.chunks);
                bw.Write((int)magic.size.box.x);
                bw.Write((int)magic.size.box.y);
                bw.Write((int)magic.size.box.z);
            }


            if (magic.rgba != null && magic.rgba.palette != null)
            {
                int length = magic.rgba.palette.Length;
                bw.Write(magic.rgba.name.ToCharArray());
                bw.Write((int)(length * 4));
                bw.Write((int)magic.size.chunks);


                for (int i = 0; i < length; i++)
                {
                    VectorInt4 c = magic.rgba.palette [i];
                    bw.Write((byte)(c.x));
                    bw.Write((byte)(c.y));
                    bw.Write((byte)(c.z));
                    bw.Write((byte)(c.w));
                }
            }
            Point[] points = WritePoints(vs.datas, magic.rgba.palette);
            bw.Write("XYZI".ToCharArray());

            bw.Write((int)(points.Length * 4) + 4);
            bw.Write((int)0);

            bw.Write((int)points.Length);

            for (int i = 0; i < points.Length; ++i)
            {
                Point p = points[i];
                bw.Write((byte)(p.x));
                bw.Write((byte)(p.y));
                bw.Write((byte)(p.z));
                bw.Write((byte)(p.i));
            }
        }
Beispiel #6
0
 public static Color Bytes2Color(VectorInt4 v)
 {
     Color c = new Color ();
     c.r  = ((float)(v.x))/255.0f;
     c.g  = ((float)(v.y))/255.0f;
     c.b  = ((float)(v.z))/255.0f;
     c.a  = ((float)(v.w))/255.0f;
     return c;
 }
Beispiel #7
0
 public static VectorInt4 operator +(VectorInt4 lhs, VectorInt4 rhs)
 {
     VectorInt4 result = new VectorInt4(lhs);
     result.x += rhs.x;
     result.y += rhs.y;
     result.z += rhs.z;
     result.w += rhs.w;
     return result;
 }
Beispiel #8
0
        public static VectorInt4 operator +(VectorInt4 lhs, VectorInt4 rhs)
        {
            VectorInt4 result = new VectorInt4(lhs);

            result.x += rhs.x;
            result.y += rhs.y;
            result.z += rhs.z;
            result.w += rhs.w;
            return(result);
        }
        public static Color Bytes2Color(VectorInt4 v)
        {
            Color c = new Color();

            c.r = ((float)(v.x)) / 255.0f;
            c.g = ((float)(v.y)) / 255.0f;
            c.b = ((float)(v.z)) / 255.0f;
            c.a = ((float)(v.w)) / 255.0f;
            return(c);
        }
Beispiel #10
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            if (obj.GetType() != this.GetType())
            {
                return(false);
            }
            VectorInt4 v = (VectorInt4)(obj);// as VectorInt2;

            if (this.x != v.x || this.y != v.y || this.z != v.z || this.w != v.w)
            {
                return(false);
            }
            return(true);
        }
Beispiel #11
0
        private static Point[] WritePoints(List <VoxelData> datas, VectorInt4[] palette)
        {
            Point[] points = new Point[datas.Count];

            for (int i = 0; i < datas.Count; ++i)
            {
                var data = datas [i];
                points[i]   = new Point();
                points[i].x = (byte)data.pos.x;
                points[i].y = (byte)data.pos.z;
                points[i].z = (byte)data.pos.y;

                Color color = datas [i].color;
                if (palette == null)
                {
                    ushort s = Color2Short(color);
                    for (int x = 0; x < palette_.Length; ++x)
                    {
                        if (palette_ [x] == s)
                        {
                            points [i].i = (byte)(x + 1);
                            break;
                        }
                    }
                }
                else
                {
                    VectorInt4 v = Color2Bytes(color);
                    for (int x = 0; x < palette.Length; ++x)
                    {
                        if (palette [x] == v)
                        {
                            points [i].i = (byte)(x + 1);
                            break;
                        }
                    }
                }
            }

            return(points);
        }
Beispiel #12
0
        private static List <VoxelData> CreateVoxelDatas(Point[] points, VectorInt4[] palette)
        {
            //VoxelStruct vs = new VoxelStruct ();



            List <VoxelData> datas = new List <VoxelData>();

            for (int i = 0; i < points.Length; ++i)
            {
                VoxelData data = new VoxelData();
                data.pos.x = points[i].x;
                data.pos.y = points[i].y;
                data.pos.z = points[i].z;
                data.id    = i;

                //ushort c =  (colors == null ? voxColors[voxelData[i].color - 1] : colors[voxelData[i].color - 1]);
                if (palette == null)
                {
                    ushort c = palette_[points[i].i - 1];

                    data.color = Short2Color(c);
                }
                else
                {
                    VectorInt4 v = palette[points[i].i - 1];
                    data.color = Bytes2Color(v);;
                }

                datas.Add(data);
                //	Debug.Log( "r:" + (c >> 10 & 0x1f)+ ",g:"+ (float)(c >> 5 & 0x1f) + ",b:"+ (float)(c & 0x1f));
                //datas[i].cindex = voxelData[i].color;
            }



            return(datas);
        }
Beispiel #13
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);
        }
Beispiel #14
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 #15
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 #16
0
        /*public static MagicaVoxel ReadFromFile(TextAsset file){
         *      Stream sw = new MemoryStream(file.bytes);
         *      System.IO.BinaryReader br = new System.IO.BinaryReader (sw);
         *      return ReadFromBinary(br);
         *
         * }
         * public static MagicaVoxel ReadFromUrl(string url){
         *      return null;
         * }*/
        public static MagicaVoxel ReadFromBinary(System.IO.BinaryReader br)
        {
            MagicaVoxel magic = new MagicaVoxel(new VoxelStruct());

            VectorInt4[] palette = null;
            Point[]      points  = null;
            string       vox     = new string(br.ReadChars(4));

            if (vox != "VOX ")
            {
                return(magic);
            }

            int version = br.ReadInt32();

            magic.version = version;
            VectorInt3 box       = new VectorInt3();
            bool       subsample = false;


            while (br.BaseStream.Position + 12 < br.BaseStream.Length)
            {
                string name   = new string(br.ReadChars(4));
                int    size   = br.ReadInt32();
                int    chunks = br.ReadInt32();


                if (name == "MAIN")
                {
                    magic.main        = new MagicaVoxel.Main();
                    magic.main.size   = size;
                    magic.main.name   = name;
                    magic.main.chunks = chunks;
                }
                else if (name == "SIZE")
                {
                    box.x = br.ReadInt32();
                    box.y = br.ReadInt32();
                    box.z = br.ReadInt32();


                    magic.size        = new MagicaVoxel.Size();
                    magic.size.size   = 12;
                    magic.size.name   = name;
                    magic.size.chunks = chunks;
                    magic.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] = MagicaVoxelFormater.ReadPoint(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;
                    }

                    magic.rgba         = new MagicaVoxel.Rgba();
                    magic.rgba.size    = size;
                    magic.rgba.name    = name;
                    magic.rgba.chunks  = chunks;
                    magic.rgba.palette = palette;
                }
                else
                {
                    if (br.BaseStream.Position + size >= br.BaseStream.Length)
                    {
                        break;
                    }
                    else
                    {
                        br.ReadBytes(size);
                    }
                }
            }
            magic.vs.datas = CreateVoxelDatas(points, palette);
            return(magic);
        }
Beispiel #17
0
        private static List<VoxelData> CreateVoxelDatas(Point[] points, VectorInt4[] palette)
        {
            //VoxelStruct vs = new VoxelStruct ();

            List<VoxelData> datas = new List<VoxelData>();

            for(int i=0; i < points.Length; ++i){
                VoxelData data = new VoxelData();
                data.pos.x = points[i].x;
                data.pos.y = points[i].y;
                data.pos.z = points[i].z;
                data.id = i;

                //ushort c =  (colors == null ? voxColors[voxelData[i].color - 1] : colors[voxelData[i].color - 1]);
                if(palette == null){

                    ushort c = palette_[points[i].i - 1];

                    data.color = Short2Color (c);

                }else{
                    VectorInt4 v = palette[points[i].i - 1];
                    data.color = Bytes2Color (v);;

                }

                datas.Add (data);
                //	Debug.Log( "r:" + (c >> 10 & 0x1f)+ ",g:"+ (float)(c >> 5 & 0x1f) + ",b:"+ (float)(c & 0x1f));
                //datas[i].cindex = voxelData[i].color;
            }

            return datas;
        }
Beispiel #18
0
        private static Point[] CreatePoint(List<VoxelData> datas, VectorInt4[] palette)
        {
            Point[] points = new Point[datas.Count];
            for (int i = 0; i < datas.Count; ++i) {
                points[i] = new Point();
                points[i].x = (byte)datas[i].pos.x;
                points[i].y = (byte)datas[i].pos.y;
                points[i].z = (byte)datas[i].pos.z;
                Color color = datas [i].color;
                if (palette == null) {
                    ushort s = Color2Short (color);
                    for (int x = 0; x < palette_.Length; ++x) {
                        if (palette_ [x] == s) {
                            points [i].i = (byte)(x + 1);
                            break;
                        }
                    }
                } else {
                    VectorInt4 v = Color2Bytes (color);
                    for (int x = 0; x < palette.Length; ++x) {
                        if (palette [x] == v) {
                            points [i].i =  (byte)(x + 1);
                            break;
                        }
                    }
                }
            }

            return points;
        }
Beispiel #19
0
        public void buildMesh()
        {
            VoxelDrawMesh draw = new VoxelDrawMesh();

            Dictionary <VectorInt3, VoxelHandler> voxs = this.voxels;

            foreach (KeyValuePair <VectorInt3, VoxelHandler> kv in voxs)
            {
                if (!voxs.ContainsKey(kv.Key + new VectorInt3(0, -1, 0)))
                {
                    VectorInt4 v = draw.addRect(Vector3.back, kv.Key, VoxelHandler.GetUV(kv.Value.back), VoxelHandler.GetUV(kv.Value.lback), kv.Value.color);
                    kv.Value.vertices.Add(v);
                }

                if (!voxs.ContainsKey(kv.Key + new VectorInt3(0, 1, 0)))
                {
                    VectorInt4 v = draw.addRect(Vector3.forward, kv.Key, VoxelHandler.GetUV(kv.Value.front), VoxelHandler.GetUV(kv.Value.lfront), kv.Value.color);
                    kv.Value.vertices.Add(v);
                }

                if (!voxs.ContainsKey(kv.Key + new VectorInt3(0, 0, 1)))
                {
                    VectorInt4 v = draw.addRect(Vector3.up, kv.Key, VoxelHandler.GetUV(kv.Value.up), VoxelHandler.GetUV(kv.Value.lup), kv.Value.color);
                    kv.Value.vertices.Add(v);
                }


                if (!voxs.ContainsKey(kv.Key + new VectorInt3(0, 0, -1)))
                {
                    VectorInt4 v = draw.addRect(Vector3.down, kv.Key, VoxelHandler.GetUV(kv.Value.down), VoxelHandler.GetUV(kv.Value.ldown), kv.Value.color);
                    kv.Value.vertices.Add(v);
                }


                if (!voxs.ContainsKey(kv.Key + new VectorInt3(1, 0, 0)))
                {
                    VectorInt4 v = draw.addRect(Vector3.left, kv.Key, VoxelHandler.GetUV(kv.Value.left), VoxelHandler.GetUV(kv.Value.lleft), kv.Value.color);
                    kv.Value.vertices.Add(v);
                }


                if (!voxs.ContainsKey(kv.Key + new VectorInt3(-1, 0, 0)))
                {
                    VectorInt4 v = draw.addRect(Vector3.right, kv.Key, VoxelHandler.GetUV(kv.Value.right), VoxelHandler.GetUV(kv.Value.lright), kv.Value.color);
                    kv.Value.vertices.Add(v);
                }
            }
            //Resources.
            _mesh = draw.crateMeshFilter("VoxelMesh", _material);
            if (_offset != null)
            {
                _mesh.gameObject.transform.SetParent(this._offset.transform);
            }
            else
            {
                _mesh.gameObject.transform.SetParent(this.transform);
            }

            _mesh.gameObject.transform.localPosition = Vector3.zero;
            _mesh.gameObject.transform.localScale    = Vector3.one;
            _mesh.gameObject.transform.localRotation = Quaternion.Euler(Vector3.zero);

            _mesh.gameObject.SetActive(true);
            Renderer renderer = _mesh.GetComponent <Renderer> ();

            renderer.material = this._material;
            showMesh();
        }