Ejemplo n.º 1
0
        public void build(VoxelProduct product)
        {
            Dictionary <VectorInt3, Dictionary <VectorInt3, VoxelHandler> > dict = new Dictionary <VectorInt3, Dictionary <VectorInt3, VoxelHandler> >();

            foreach (var kv in product.main.voxels)
            {
                VectorInt3 offset = new VectorInt3();
                offset.x = kv.Key.x / box_.x;
                offset.y = kv.Key.y / box_.y;
                offset.z = kv.Key.z / box_.z;
                if (!dict.ContainsKey(offset))
                {
                    dict [offset] = new Dictionary <VectorInt3, VoxelHandler> ();
                }
                dict [offset].Add(kv.Key, kv.Value);
            }
            List <VoxelProduct.Product> list = new List <VoxelProduct.Product>();

            foreach (var o in dict)
            {
                var p = new VoxelProduct.Product();
                p.voxels = o.Value;
                list.Add(p);
            }
            product.sub = list.ToArray();
        }
Ejemplo n.º 2
0
        private void toShadow(VectorInt3 position, VoxelHandler handler, bool spot = false)
        {
            VoxelShadowHandler vsh = null;

            bool glost = (handler.position.z != 0);

            position.z = 0;

            if (real_.ContainsKey(position))
            {
                vsh = real_ [position];
            }
            else
            {
                vsh              = new VoxelShadowHandler();
                vsh._position    = position;
                vsh._realSpot    = false;
                vsh._ghostSpot   = false;
                real_ [position] = vsh;
            }

            if (!glost)
            {
                vsh._realSpot  = vsh._realSpot || spot;
                vsh._ghostSpot = vsh._ghostSpot || spot;
            }
            else
            {
                vsh._ghostSpot = vsh._ghostSpot || spot;
            }
        }
Ejemplo n.º 3
0
 public void setup(VectorInt3 position, Color color, int id)
 {
     this.gameObject.SetActive(true);
     this.transform.localPosition = new Vector3(position.x, position.y, -position.z);
     this.color = color;
     this._id   = id;
 }
Ejemplo n.º 4
0
        public static VoxelNormal Normal(VoxelStruct st)
        {
            VoxelNormal normal = new VoxelNormal();
            VectorInt3  min    = new VectorInt3(9999, 9999, 9999);
            VectorInt3  max    = new VectorInt3(-9999, -9999, -9999);

            for (int i = 0; i < st.datas.Count; ++i)
            {
                VectorInt3 pos = st.datas [i].pos;
                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);
            }
            normal.vs = new VoxelStruct();
            for (int i = 0; i < st.datas.Count; ++i)
            {
                VoxelData data = st.datas [i];
                data.pos -= min;
                normal.vs.datas.Add(data);
            }

            normal.shifting = min;
            return(normal);
        }
Ejemplo n.º 5
0
		public void setup (VectorInt3 position, Color color, int id)
		{
			this.gameObject.SetActive (true);
			this.transform.localPosition = new Vector3(position.x, position.y, -position.z);
			this.color = color;
			this._id = id;
		}
Ejemplo n.º 6
0
		public void addVoxel(VoxelStruct vs, VectorInt3 offset){
//			Debug.Log (offset.x +","+ offset.y+","+ offset.z);
			Packed packed = new Packed ();
			packed.vs = vs;
			packed.offset = offset;
			list_.Add (packed);
		}
Ejemplo n.º 7
0
        public void addVoxel(VoxelStruct vs, VectorInt3 offset)
        {
            Packed packed = new Packed();

            packed.vs     = vs;
            packed.offset = offset;
            list_.Add(packed);
        }
Ejemplo n.º 8
0
 public static VectorInt3 operator +(VectorInt3 lhs, VectorInt3 rhs)
 {
     VectorInt3 result = new VectorInt3(lhs);
     result.x += rhs.x;
     result.y += rhs.y;
     result.z += rhs.z;
     return result;
 }
Ejemplo n.º 9
0
        public static VectorInt3 operator +(VectorInt3 lhs, VectorInt3 rhs)
        {
            VectorInt3 result = new VectorInt3(lhs);

            result.x += rhs.x;
            result.y += rhs.y;
            result.z += rhs.z;
            return(result);
        }
Ejemplo n.º 10
0
        public VoxelHandler getVoxel(VectorInt3 position)
        {
            var voxs = this.voxels;

            if (voxs.ContainsKey(position))
            {
                return(voxs [position]);
            }
            return(null);
        }
Ejemplo n.º 11
0
 private void shadowTest(VoxelHandler handler, VectorInt3 shadow, VectorInt3 light, byte index, Vector3 face)
 {
     if (voxels.ContainsKey(handler.position + shadow))
     {
         handler.shadowAdd(index, face);
     }
     else if (!voxels.ContainsKey(handler.position + light))
     {
         handler.lightAdd(index, face);
     }
 }
Ejemplo n.º 12
0
		public void addFile(VoxelModel model, VectorInt3 offset){

			for (int i = 0; i < model.data.Length; ++i) {
				VoxelData data = model.data [i];
				VectorInt3 p = new VectorInt3 (data.pos.x, data.pos.y, data.pos.z) + offset;
				if (!dictionary_.ContainsKey (p)) {
					dictionary_ [p] = data;
				}
			}

			Debug.Log (dictionary_.Count);
		}
Ejemplo n.º 13
0
        public static VoxelStruct Unusual(VectorInt3 shifting, VoxelStruct st)
        {
            VoxelStruct ret = new VoxelStruct();

            for (int i = 0; i < st.datas.Count; ++i)
            {
                VoxelData data = st.datas [i];
                data.pos += shifting;
                ret.datas.Add(data);
            }

            return(ret);
        }
Ejemplo n.º 14
0
            public bool contain(VectorInt3 pos)
            {
                if (pos.x >= _min.x &&
                    pos.y >= _min.y &&
                    pos.z >= _min.z &&
                    pos.x < _max.x &&
                    pos.y < _max.y &&
                    pos.z < _max.z
                    )
                {
                    return(true);
                }

                return(false);
            }
Ejemplo n.º 15
0
        public void doPowder(Color[] colors, Powder powder, VectorInt3 min, VectorInt3 max)
        {
            VectorInt3 mi = min + this._offset;

            //VectorInt3 mx = max + this._offset;
            foreach (VoxelFace.Rect r in powder.rect)
            {
                for (int x = r.min.x; x < r.max.x; ++x)
                {
                    for (int y = r.min.y; y < r.max.y; ++y)
                    {
                        for (int z = r.min.z; z < r.max.z; ++z)
                        {
                            VoxelHandler handler = _mesh.getVoxel(new VectorInt3(x, y, z) + mi);
//							Debug.Log ("x" + x + "y" +y +"z" + z);
                            if (handler != null)
                            {
                                foreach (VectorInt4 vertice in handler.vertices)
                                {
                                    colors[vertice.x] = r.color;
                                    colors[vertice.y] = r.color;
                                    colors[vertice.z] = r.color;
                                    colors[vertice.w] = r.color;
                                }
                            }
                        }
                    }
                }
            }

            foreach (VoxelFace.Point p in powder.point)
            {
                VoxelHandler handler = _mesh.getVoxel(p.position + mi);
                if (handler != null)
                {
                    foreach (VectorInt4 vertice in handler.vertices)
                    {
                        colors[vertice.x] = p.color;
                        colors[vertice.y] = p.color;
                        colors[vertice.z] = p.color;
                        colors[vertice.w] = p.color;
                    }
                }
            }
        }
Ejemplo n.º 16
0
        public static VoxelStruct Reversal(VoxelStruct st, int reversal)
        {
            if (reversal == 0)
            {
                return(st);
            }
            VectorInt3 min = new VectorInt3(9999, 9999, 9999);
            VectorInt3 max = new VectorInt3(-9999, -9999, -9999);

            for (int i = 0; i < st.datas.Count; ++i)
            {
                VectorInt3 pos = st.datas[i].pos;
                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);
            }


            VoxelStruct ret = new VoxelStruct();

            for (int i = 0; i < st.datas.Count; ++i)
            {
                var        data = st.datas[i];
                VectorInt3 pos  = data.pos;
                if ((reversal & (int)(ReversalAxis.XAxis)) != 0)
                {
                    pos.x = max.x - pos.x - 1 + min.x;
                }

                if ((reversal & (int)(ReversalAxis.YAxis)) != 0)
                {
                    pos.y = max.y - pos.y - 1 + min.y;
                }
                if ((reversal & (int)(ReversalAxis.ZAxis)) != 0)
                {
                    pos.z = max.z - pos.z - 1 + min.z;
                }

                ret.datas.Add(new VoxelData(pos, data.color));
            }
            return(ret);
        }
Ejemplo n.º 17
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            if (obj.GetType() != this.GetType())
            {
                return(false);
            }
            VectorInt3 v = (VectorInt3)(obj);// as VectorInt2;

            if (this.x != v.x || this.y != v.y || this.z != v.z)
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 18
0
        private void build(int from, int to, Dictionary <VectorInt3, VoxelHandler> voxs)
        {
//			Debug.Log ("from:" + from + ", to" + to);
            List <VectorInt3> keys = new List <VectorInt3> (voxs.Keys);

            for (int i = from; i < to; ++i)
            {
                VectorInt3 key = keys [i];

                VoxelHandler value = voxs [key];
                if (!voxs.ContainsKey(key + new VectorInt3(0, -1, 0)))
                {
                    addRect(Vector3.back, key, value.color);
                }

                if (!voxs.ContainsKey(key + new VectorInt3(0, 1, 0)))
                {
                    addRect(Vector3.forward, key, value.color);
                }

                if (!voxs.ContainsKey(key + new VectorInt3(0, 0, 1)))
                {
                    addRect(Vector3.up, key, value.color);
                }


                if (!voxs.ContainsKey(key + new VectorInt3(0, 0, -1)))
                {
                    addRect(Vector3.down, key, value.color);
                }


                if (!voxs.ContainsKey(key + new VectorInt3(1, 0, 0)))
                {
                    addRect(Vector3.left, key, value.color);
                }


                if (!voxs.ContainsKey(key + new VectorInt3(-1, 0, 0)))
                {
                    addRect(Vector3.right, key, value.color);
                }
            }
        }
Ejemplo n.º 19
0
        private void build(VoxelProduct.Product main, int from, int to, Dictionary <VectorInt3, VoxelHandler> voxs, Dictionary <VectorInt3, VoxelHandler> all)
        {
            List <VectorInt3> keys = new List <VectorInt3> (voxs.Keys);

            for (int i = from; i < to; ++i)
            {
                VectorInt3 key = keys [i];

                VoxelHandler value = voxs [key];
                if (!all.ContainsKey(key + new VectorInt3(0, -1, 0)))
                {
                    addRect(main, Vector3.back, key, value.color);
                }

                if (!all.ContainsKey(key + new VectorInt3(0, 1, 0)))
                {
                    addRect(main, Vector3.forward, key, value.color);
                }

                if (!all.ContainsKey(key + new VectorInt3(0, 0, 1)))
                {
                    addRect(main, Vector3.up, key, value.color);
                }


                if (!all.ContainsKey(key + new VectorInt3(0, 0, -1)))
                {
                    addRect(main, Vector3.down, key, value.color);
                }


                if (!all.ContainsKey(key + new VectorInt3(1, 0, 0)))
                {
                    addRect(main, Vector3.left, key, value.color);
                }


                if (!all.ContainsKey(key + new VectorInt3(-1, 0, 0)))
                {
                    addRect(main, Vector3.right, key, value.color);
                }
            }
        }
Ejemplo n.º 20
0
        static public Bounds CreateBounds(VoxelStruct st)
        {
            VectorInt3 min = new VectorInt3(9999, 9999, 9999);
            VectorInt3 max = new VectorInt3(-9999, -9999, -9999);

            for (int i = 0; i < st.datas.Count; ++i)
            {
                VectorInt3 pos = st.datas [i].pos;
                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);
            }
            Vector3 size   = new Vector3(max.x - min.x + 1, max.y - min.y + 1, max.z - min.z + 1);
            Bounds  bounds = new Bounds(size / 2, size);

            return(bounds);
        }
Ejemplo n.º 21
0
        private void shadowTest(VectorInt3 offset, VoxelShadowHandler handler, byte index)
        {
            var voxs = this.real_;

            if (handler._realSpot)
            {
                handler.realAdd(index);
            }

            VectorInt3 key = offset;

            if (voxs.ContainsKey(key))
            {
                VoxelShadowHandler o = voxs[key];
                if (o._realSpot)
                {
                    handler.realAdd(index);
                }
            }

            if (handler._ghostSpot)
            {
                if (!voxs.ContainsKey(key))
                {
                    handler.ghostAdd(index);
                }
                else
                {
                    VoxelShadowHandler o = voxs [key];
                    if (!o._ghostSpot)
                    {
                        handler.ghostAdd(index);
                    }
                }
            }
            else
            {
                handler.ghostAdd(index);
            }
        }
Ejemplo n.º 22
0
        public void shadowBuild(Dictionary <VectorInt3, VoxelHandler> voxs)
        {
            this.real_.Clear();
            foreach (KeyValuePair <VectorInt3, VoxelHandler> kv in voxs)
            {
                VectorInt3 pos = kv.Value.position;
                toShadow(pos, kv.Value, true);
                if (kv.Value.position.z == 0)
                {
                    toShadow(pos + new VectorInt3(0, -1, 0), kv.Value);
                    toShadow(pos + new VectorInt3(0, 1, 0), kv.Value);
                    toShadow(pos + new VectorInt3(1, 0, 0), kv.Value);
                    toShadow(pos + new VectorInt3(1, -1, 0), kv.Value);
                    toShadow(pos + new VectorInt3(1, 1, 0), kv.Value);
                    toShadow(pos + new VectorInt3(-1, 0, 0), kv.Value);
                    toShadow(pos + new VectorInt3(-1, -1, 0), kv.Value);
                    toShadow(pos + new VectorInt3(-1, 1, 0), kv.Value);
                }
            }

            foreach (KeyValuePair <VectorInt3, VoxelShadowHandler> kv in this.real_)
            {
                VectorInt3 pos = kv.Key;

                kv.Value.shadowBegin();

                shadowTest(pos + new VectorInt3(-1, 1, 0), kv.Value, 0);
                shadowTest(pos + new VectorInt3(0, 1, 0), kv.Value, 1);
                shadowTest(pos + new VectorInt3(1, 1, 0), kv.Value, 2);
                shadowTest(pos + new VectorInt3(-1, 0, 0), kv.Value, 3);
                shadowTest(pos + new VectorInt3(1, 0, 0), kv.Value, 4);
                shadowTest(pos + new VectorInt3(-1, -1, 0), kv.Value, 5);
                shadowTest(pos + new VectorInt3(0, -1, 0), kv.Value, 6);
                shadowTest(pos + new VectorInt3(1, -1, 0), kv.Value, 7);

                kv.Value.shadowEnd();
            }
        }
Ejemplo n.º 23
0
        public void arrange(bool normal = false)
        {
            HashSet<Color> palette = new HashSet<Color>();

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

            for (int i = 0; i < this.datas.Count; ++i) {
                palette.Add (this.datas[i].color);

                VectorInt3 pos = this.datas [i].pos;

                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 < this.datas.Count; ++i) {
                    palette.Add (this.datas[i].color);

                    VectorInt3 pos = this.datas [i].pos;
                    this.datas [i].pos = pos - min;

                }
                min = new VectorInt3 (0, 0, 0);
            }

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

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

            this.size.box = new VectorInt3 ();

            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 VoxelStruct.Rgba ();

            int size = Mathf.Max (palette.Count, 256);
            this.rgba.palette = new VectorInt4[size];
            int n = 0;
            foreach (Color c in palette)
            {
                this.rgba.palette [n] = VoxelFormater.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 + this.datas.Count *4;
        }
Ejemplo n.º 24
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;
        }
Ejemplo n.º 25
0
        private void addRect(Vector3 direction, VectorInt3 position, Color color)
        {
            Vector3 offset = new Vector3(position.x, position.z, position.y);

            product_.draw.triangles.Add(product_.draw.vertices.Count + 0);
            product_.draw.triangles.Add(product_.draw.vertices.Count + 1);
            product_.draw.triangles.Add(product_.draw.vertices.Count + 2);
            product_.draw.triangles.Add(product_.draw.vertices.Count + 1);
            product_.draw.triangles.Add(product_.draw.vertices.Count + 3);
            product_.draw.triangles.Add(product_.draw.vertices.Count + 2);



            Vector3 p0 = new Vector3();             //
            Vector3 p1 = new Vector3();             //
            Vector3 p2 = new Vector3();             //
            Vector3 p3 = new Vector3();             //

            if (direction == Vector3.up)
            {
                p0 = (new Vector3(-0.5f, 0.5f, 0.5f) + offset);
                p1 = (new Vector3(0.5f, 0.5f, 0.5f) + offset);
                p2 = (new Vector3(-0.5f, 0.5f, -0.5f) + offset);
                p3 = (new Vector3(0.5f, 0.5f, -0.5f) + offset);
            }
            if (direction == Vector3.down)
            {
                p0 = (new Vector3(-0.5f, -0.5f, -0.5f) + offset);
                p1 = (new Vector3(0.5f, -0.5f, -0.5f) + offset);
                p2 = (new Vector3(-0.5f, -0.5f, 0.5f) + offset);
                p3 = (new Vector3(0.5f, -0.5f, 0.5f) + offset);
            }
            else if (direction == Vector3.back)
            {
                p0 = (new Vector3(-0.5f, 0.5f, -0.5f) + offset);
                p1 = (new Vector3(0.5f, 0.5f, -0.5f) + offset);
                p2 = (new Vector3(-0.5f, -0.5f, -0.5f) + offset);
                p3 = (new Vector3(0.5f, -0.5f, -0.5f) + offset);
            }
            else if (direction == Vector3.forward)
            {
                p0 = (new Vector3(-0.5f, -0.5f, 0.5f) + offset);
                p1 = (new Vector3(0.5f, -0.5f, 0.5f) + offset);
                p2 = (new Vector3(-0.5f, 0.5f, 0.5f) + offset);
                p3 = (new Vector3(0.5f, 0.5f, 0.5f) + offset);
            }
            else if (direction == Vector3.left)
            {
                p0 = (new Vector3(0.5f, -0.5f, 0.5f) + offset);
                p1 = (new Vector3(0.5f, -0.5f, -0.5f) + offset);
                p2 = (new Vector3(0.5f, 0.5f, 0.5f) + offset);
                p3 = (new Vector3(0.5f, 0.5f, -0.5f) + offset);
            }
            else if (direction == Vector3.right)
            {
                p0 = (new Vector3(-0.5f, -0.5f, -0.5f) + offset);
                p1 = (new Vector3(-0.5f, -0.5f, 0.5f) + offset);
                p2 = (new Vector3(-0.5f, 0.5f, -0.5f) + offset);
                p3 = (new Vector3(-0.5f, 0.5f, 0.5f) + offset);
            }

            this.addVertix(p0, color, direction.normalized);
            this.addVertix(p1, color, direction.normalized);
            this.addVertix(p2, color, direction.normalized);
            this.addVertix(p3, color, direction.normalized);
        }
Ejemplo n.º 26
0
        public void doPowder(Color[] colors, Powder powder, VectorInt3 min, VectorInt3 max)
        {
            VectorInt3 mi = min + this._offset;
            //VectorInt3 mx = max + this._offset;
            foreach (VoxelFace.Rect r in powder.rect) {

                for (int x = r.min.x; x<r.max.x; ++x) {
                    for(int y = r.min.y; y< r.max.y; ++y){
                        for(int z = r.min.z;z<r.max.z; ++z ){
                            VoxelHandler handler = _mesh.getVoxel(new VectorInt3(x, y, z) + mi);
            //							Debug.Log ("x" + x + "y" +y +"z" + z);
                            if(handler != null){
                                foreach(VectorInt4 vertice in handler.vertices){

                                    colors[vertice.x] = r.color;
                                    colors[vertice.y] = r.color;
                                    colors[vertice.z] = r.color;
                                    colors[vertice.w] = r.color;
                                }
                            }
                        }
                    }
                }
            }

            foreach (VoxelFace.Point p in powder.point) {

                VoxelHandler handler = _mesh.getVoxel(p.position + mi);
                if(handler != null){
                    foreach(VectorInt4 vertice in handler.vertices){

                        colors[vertice.x] = p.color;
                        colors[vertice.y] = p.color;
                        colors[vertice.z] = p.color;
                        colors[vertice.w] = p.color;
                    }
                }

            }
        }
Ejemplo n.º 27
0
 private void shadowTest(VoxelHandler handler, VectorInt3 shadow, VectorInt3 light, byte index, Vector3 face)
 {
     if (voxels.ContainsKey (handler.position + shadow)) {
         handler.shadowAdd (index, face);
     } else 	if (!voxels.ContainsKey (handler.position + light)) {
         handler.lightAdd (index, face);
     }
 }
Ejemplo n.º 28
0
 public VoxelData(VectorInt3 p, Color c)
 {
     pos   = p;
     color = c;
 }
Ejemplo n.º 29
0
        public static VectorInt3 operator-(VectorInt3 lhs, VectorInt3 rhs)
        {
            VectorInt3 to = new VectorInt3(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);

            return(to);
        }
Ejemplo n.º 30
0
 public VoxelSplitSmall(VectorInt3 box)
 {
     box_ = box;
 }
Ejemplo n.º 31
0
 public VectorInt3(VectorInt3 v3)
 {
     this.x = v3.x;
     this.y = v3.y;
     this.z = v3.z;
 }
Ejemplo n.º 32
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);
        }
Ejemplo n.º 33
0
		private void shadowTest(VectorInt3 offset, VoxelShadowHandler handler, byte index){


			var voxs = this.real_;
			if (handler._realSpot) {
				handler.realAdd (index);
			}
		
			VectorInt3 key = offset;
			if (voxs.ContainsKey (key)) {
				VoxelShadowHandler o = voxs[key];
				if(o._realSpot){
					handler.realAdd (index);	
				}

			} 

			if (handler._ghostSpot) {
					if (!voxs.ContainsKey (key)) {
							handler.ghostAdd (index);	
					} else {
							VoxelShadowHandler o = voxs [key];
							if (!o._ghostSpot) {
									handler.ghostAdd (index);	
							}

					}

			} else {
				handler.ghostAdd (index);	
			}
		}
Ejemplo n.º 34
0
        private void arrange(VoxelStruct st, bool normal = false)
        {
            vs_ = st;
            HashSet <Color> palette = new HashSet <Color>();

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

            for (int i = 0; i < st.datas.Count; ++i)
            {
                palette.Add(st.datas[i].color);

                VectorInt3 pos = st.datas [i].pos;

                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.datas.Count; ++i)
                {
                    palette.Add(st.datas[i].color);
                    var data = st.datas [i];
                    data.pos    -= min;
                    st.datas [i] = data;                   //.pos = pos - min;
                }
                min = new VectorInt3(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 VectorInt3();


            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 VectorInt4[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.datas.Count * 4;
        }
Ejemplo n.º 35
0
 public void addBox(VectorInt3 min, VectorInt3 size)
 {
     list_.Add(new Box(min, size));
 }
Ejemplo n.º 36
0
 public Box(VectorInt3 min, VectorInt3 size)
 {
     _min = min;
     _max = min + size;
 }
Ejemplo n.º 37
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);
        }
Ejemplo n.º 38
0
 public VectorInt3(VectorInt3 v3)
 {
     this.x = v3.x;
     this.y = v3.y;
     this.z = v3.z;
 }
Ejemplo n.º 39
0
 public VoxelHandler getVoxel(VectorInt3 position)
 {
     var voxs = this.voxels;
     if (voxs.ContainsKey (position)) {
         return voxs [position];
     }
     return null;
 }
Ejemplo n.º 40
0
	public VectorInt4 addRect(Vector3 direction, VectorInt3 position, Vector2 shadow, Vector2 light, Color color){


		Vector3 offset = new Vector3(position.x, position.z, position.y);

		
		triangles_.Add (vertices_.Count + 0);
		triangles_.Add (vertices_.Count + 1);
		triangles_.Add (vertices_.Count + 2);
		triangles_.Add (vertices_.Count + 1);
		triangles_.Add (vertices_.Count + 3);
		triangles_.Add (vertices_.Count + 2);


		colors_.Add (color);
		colors_.Add (color);
		colors_.Add (color);
		colors_.Add (color);

		if (direction == Vector3.up) {
			vertices_.Add (new Vector3 (-0.5f, 0.5f, 0.5f) + offset);
			vertices_.Add (new Vector3 (0.5f, 0.5f, 0.5f) + offset);
			vertices_.Add (new Vector3 (-0.5f, 0.5f, -0.5f) + offset);
			vertices_.Add (new Vector3 (0.5f, 0.5f, -0.5f) + offset);
			
		}if (direction == Vector3.down) {
			vertices_.Add (new Vector3 (-0.5f, -0.5f, -0.5f) + offset);
			vertices_.Add (new Vector3 (0.5f, -0.5f, -0.5f) + offset);
			vertices_.Add (new Vector3 (-0.5f, -0.5f, 0.5f) + offset);
			vertices_.Add (new Vector3 (0.5f, -0.5f, 0.5f) + offset);
			
		}else if (direction == Vector3.back) {
			vertices_.Add (new Vector3 (-0.5f, 0.5f, -0.5f) + offset);
			vertices_.Add (new Vector3 (0.5f, 0.5f, -0.5f) + offset);
			vertices_.Add (new Vector3 (-0.5f, -0.5f, -0.5f) + offset);
			vertices_.Add (new Vector3 (0.5f, -0.5f, -0.5f) + offset);
			
			
		}else if (direction == Vector3.forward) {
			vertices_.Add (new Vector3 (-0.5f, -0.5f, 0.5f) + offset);
			vertices_.Add (new Vector3 (0.5f, -0.5f, 0.5f) + offset);
			vertices_.Add (new Vector3 (-0.5f, 0.5f, 0.5f) + offset);
			vertices_.Add (new Vector3 (0.5f, 0.5f, 0.5f) + offset);
			
			
		}else if (direction == Vector3.left) {
			vertices_.Add (new Vector3 (0.5f, -0.5f, 0.5f) + offset);
			vertices_.Add (new Vector3 (0.5f, -0.5f, -0.5f) + offset);
			vertices_.Add (new Vector3 (0.5f, 0.5f, 0.5f) + offset);
			vertices_.Add (new Vector3 (0.5f, 0.5f, -0.5f) + offset);
		}else if (direction == Vector3.right) {

			vertices_.Add (new Vector3 (-0.5f, -0.5f, -0.5f) + offset);
			vertices_.Add (new Vector3 (-0.5f, -0.5f, 0.5f) + offset);
			vertices_.Add (new Vector3 (-0.5f, 0.5f, -0.5f) + offset);
			vertices_.Add (new Vector3 (-0.5f, 0.5f, 0.5f) + offset);
		}
		float magic = 0.03125f;//
		this.uv_.Add (new Vector2 (light.x +magic, light.y +magic));
		this.uv_.Add (new Vector2 (light.x, light.y +magic));
		this.uv_.Add (new Vector2 (light.x +magic, light.y+0.0625f));
		this.uv_.Add (new Vector2 (light.x, light.y+0.0625f));

		this.uv1_.Add (new Vector2 (shadow.x +magic, shadow.y +magic));
		this.uv1_.Add (new Vector2 (shadow.x, shadow.y +magic));
		this.uv1_.Add (new Vector2 (shadow.x +magic, shadow.y+0.0625f));
		this.uv1_.Add (new Vector2 (shadow.x, shadow.y+0.0625f));


		return new VectorInt4 (vertices_.Count-4, vertices_.Count-3, vertices_.Count-2, vertices_.Count-1);

	}
Ejemplo n.º 41
0
 public void doPowder(Powder powder, VectorInt3 min, VectorInt3 max)
 {
     Color[] colors = this.beginEmotions();
     doPowder (colors, powder, min, max);
     this.endEmotions (colors);
 }
Ejemplo n.º 42
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);
        }
Ejemplo n.º 43
0
		private void toShadow(VectorInt3 position, VoxelHandler handler, bool spot = false){
			
			VoxelShadowHandler vsh = null;

			bool glost = (handler.position.z != 0);
			position.z = 0;

			if (real_.ContainsKey (position)) {
				vsh = real_ [position];
			} else {
				vsh = new VoxelShadowHandler ();
				vsh._position = position;
				vsh._realSpot = false;
				vsh._ghostSpot = false;
				real_ [position] = vsh;	
			}

			if (!glost) 
			{
				vsh._realSpot = vsh._realSpot || spot;	
				vsh._ghostSpot = vsh._ghostSpot || spot;
			} else 
			{
				vsh._ghostSpot = vsh._ghostSpot || spot;
			}
		

		}