Esempio n. 1
0
        private VoxelChunk CreateChunk(Vint3 intPos)
        {
            if (intPos.x > int.MaxValue - 100 || intPos.x < int.MinValue + 100 ||
                intPos.y > int.MaxValue - 100 || intPos.y < int.MinValue + 100 ||
                intPos.z > int.MaxValue - 100 || intPos.z < int.MinValue + 100)
            {
                Debug.LogError("Attempted to create chunk close to border: " + intPos);
                return(null);
            }

            var go    = new GameObject("Chunk" + intPos);
            var chunk = go.AddComponent <VoxelChunk>();

            chunk.Initialize(intPos.Vector * Size, intPos);

            if (!WebManager.IsServer)
            {
                var rend = go.AddComponent <MeshRenderer>();
                rend.sharedMaterial = material;
            }

            go.transform.SetParent(transform, false);

            chunks.Add(intPos, chunk);

            areas.Add(intPos * Size / 64);

            return(chunk);
        }
Esempio n. 2
0
        public void Draw(Vector3 position, float radius, byte color)
        {
            position = transform.InverseTransformPoint(position);

            var pos = new Vint3(position) / Size;

            var bounds = new Bounds(position, new Vector3(radius * 2, radius * 2, radius * 2));

            for (int i = 0; i < Vint3.Offset.Length; i++)
            {
                var v = pos + Vint3.Offset[i];
                if (!chunks.ContainsKey(v))
                {
                    CreateChunk(v);
                }

                if (chunks[v].bounds.Intersects(bounds))
                {
                    chunks[v].Draw(position, radius, color);
                    update.Add(v);
                    if (WebManager.IsConnected && !painted.Contains(v))
                    {
                        painted.Enqueue(v);
                    }
                }
            }
        }
Esempio n. 3
0
        public byte GetSingleVoxel(Vint3 pos)
        {
            var        logic = pos / Size;
            VoxelChunk chunk;

            if (debugCounter == 0 && chunks.TryGetValue(logic, out chunk))
            {
                debugCounter++;
                var found = chunk[pos - logic * Size];
                debugCounter = 0;
                return(found);
            }
            debugCounter = 0;
            return(0);
        }
Esempio n. 4
0
 public byte this[Vint3 pos]
 {
     get
     {
         //return data[x, y, z];
         var logic = pos / ChunkManager.Size;
         if (logic == Vint3.Zero)
         {
             return(data[pos.x, pos.y, pos.z]);
         }
         else
         {
             return(ChunkManager.Instance.GetSingleVoxel(iPos * ChunkManager.Size + pos));
         }
     }
 }
Esempio n. 5
0
        // Use this for initialization
        public void Initialize(Vector3 position, Vint3 intPos)
        {
            iPos = intPos;
            data = new byte[ChunkManager.Size, ChunkManager.Size, ChunkManager.Size];

            if (!WebManager.IsServer)
            {
                mesh = new Mesh();
                var filter = gameObject.AddComponent <MeshFilter>();
                filter.mesh         = mesh;
                collider            = gameObject.AddComponent <MeshCollider>();
                collider.sharedMesh = mesh;
            }

            transform.localPosition = position;
            bounds = new Bounds(position + new Vector3(ChunkManager.Size, ChunkManager.Size, ChunkManager.Size) * 0.5f,
                                new Vector3(ChunkManager.Size, ChunkManager.Size, ChunkManager.Size));
        }
Esempio n. 6
0
        public Vint3 LoadChunk(BinaryReader reader)
        {
            var v    = new Vint3(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32());
            int hash = reader.ReadInt32();

            if (hash != v.GetHashCode())
            {
                Debug.LogError("Chunk at " + v + " is malformed!");
                var ms = (MemoryStream)reader.BaseStream;

                //StringBuilder builder = new StringBuilder();
                //builder.AppendLine("Data:");
                //var array = WebClient.buffer;
                //for (int i = 0; i < array.Length; i++)
                //{
                //    builder.AppendLine(i.ToString("0000") + " " + array[i] + (i == ms.Position ? " <---" : ""));
                //}
                //Debug.Log(builder.ToString());

                ms.Position = ms.Length;
                return(v);
            }

            Debug.Log("Receiving chunk " + v);

            if (!chunks.ContainsKey(v))
            {
                CreateChunk(v);
            }

            chunks[v].LoadChunk(reader);

            for (int i = 0; i < offset.GetLength(0); i++)
            {
                var vo = v + new Vint3(offset[i, 0], offset[i, 1], offset[i, 2]);
                update.Add(vo);
            }

            return(v);
        }
Esempio n. 7
0
        public void SaveChunk(Vint3 pos, BinaryWriter writer)
        {
            var chunk = chunks[pos];

            SaveChunk(chunk, writer);
        }