Преглед на файлове

Changed chunk system to work with Vector3 instead of Vector3Int

James Peret преди 2 години
родител
ревизия
91876c9051
променени са 2 файла, в които са добавени 48 реда и са изтрити 23 реда
  1. 3 1
      Runtime/Chunk.cs
  2. 45 22
      Runtime/ChunkSystem.cs

+ 3 - 1
Runtime/Chunk.cs

@@ -12,13 +12,15 @@ namespace KairoEngine.Chunks
         public bool isInitialized() => initialized;
         private Vector3Int size;
         public Vector3Int position { get; private set; }
+        public Vector3 globalPosition { get; private set; }
         public TChunkBlock[ , , ] blocks;
 
-        public Chunk(Vector3Int size, Vector3Int position, Func<Chunk<TChunkBlock>, Vector3Int, TChunkBlock> createChunkBlock, 
+        public Chunk(Vector3Int size, Vector3 globalPosition, Vector3Int position, Func<Chunk<TChunkBlock>, Vector3Int, TChunkBlock> createChunkBlock, 
             bool debug = false)
         {
             this.size = size;
             this.position = position;
+            this.globalPosition = globalPosition;
             this.blocks = new TChunkBlock[size.x, size.y, size.z];
             this.initialized = true;
 

+ 45 - 22
Runtime/ChunkSystem.cs

@@ -9,52 +9,75 @@ namespace KairoEngine.Chunks
     public class ChunkSystem<TChunkBlock>
     {
         public Vector3Int chunkSize = new Vector3Int(16, 16, 16);
+        public Vector3 voxelSize = new Vector3(1f, 1f, 1f);
         public Dictionary<Vector3Int, Chunk<TChunkBlock>> chunks;
-        public List<Vector3Int> chunkList = new List<Vector3Int>();
+        public List<Vector3> chunkList = new List<Vector3>();
 
-        public ChunkSystem(Vector3Int chunkSize, bool debug = false)
+        public ChunkSystem(Vector3Int chunkSize, Vector3 voxelSize, bool debug = false)
         {
             this.chunkSize = chunkSize;
+            this.voxelSize = voxelSize;
             this.chunks = new Dictionary<Vector3Int, Chunk<TChunkBlock>>();
         }
 
-        public void CreateChunk(Vector3Int position, Func<Chunk<TChunkBlock>, Vector3Int, TChunkBlock> createChunkBlock)
+        public void CreateChunk(Vector3 globalPosition, Vector3Int position, Func<Chunk<TChunkBlock>, Vector3Int, TChunkBlock> createChunkBlock)
         {
-            Chunk<TChunkBlock> chunk = new Chunk<TChunkBlock>(chunkSize, position, createChunkBlock);
+            Chunk<TChunkBlock> chunk = new Chunk<TChunkBlock>(chunkSize, globalPosition, position, createChunkBlock);
             chunks.Add(position, chunk);
-            chunkList.Add(position);
+            chunkList.Add(globalPosition);
         }
 
-        public TChunkBlock GetBlock(Vector3Int position)
+        public Vector3Int GetVoxelFromPosition(Vector3 position, Chunk<TChunkBlock> chunk)
+        {
+            Vector3 voxelPos = new Vector3();
+            voxelPos = position - chunk.globalPosition;
+            // voxelPos.x = position.x - (Mathf.Floor(position.x / chunkSize.x * voxelSize.x) * chunkSize.x * voxelSize.x);
+            // voxelPos.y = position.y - (Mathf.Floor(position.y / chunkSize.y * voxelSize.y) * chunkSize.y * voxelSize.y);
+            // voxelPos.z = position.z - (Mathf.Floor(position.z / chunkSize.z * voxelSize.z) * chunkSize.z * voxelSize.z);
+            voxelPos = new Vector3(voxelPos.x / voxelSize.x, voxelPos.y / voxelSize.y, voxelPos.z / voxelSize.z);
+            //voxelPos = new Vector3(voxelPos.x + 0.1f, voxelPos.y + 0.1f, voxelPos.z + 0.1f);
+            Vector3Int pos = new Vector3Int(Mathf.RoundToInt(voxelPos.x), Mathf.RoundToInt(voxelPos.y), Mathf.RoundToInt(voxelPos.z));
+            if(pos.x >= chunkSize.x || pos.y >= chunkSize.y || pos.z >= chunkSize.z) Debug.LogError($"Position is out of chunk bounds ({pos} | {position} | {chunk.position})");
+            pos = new Vector3Int(Mathf.Clamp(pos.x, 0, chunkSize.x - 1), Mathf.Clamp(pos.y, 0, chunkSize.y - 1), Mathf.Clamp(pos.z, 0, chunkSize.z - 1));
+            return pos;
+        }
+
+        public TChunkBlock GetBlock(Vector3 position)
         {
             Chunk<TChunkBlock> chunk = GetChunk(position);
             if(!chunk.isInitialized()) return default(TChunkBlock);
-            Vector3Int pos = new Vector3Int();
-            pos.x = position.x - (Mathf.FloorToInt(position.x / chunkSize.x ) * chunkSize.x);
-            pos.y = position.y - (Mathf.FloorToInt(position.y / chunkSize.y ) * chunkSize.y);
-            pos.z = position.z - (Mathf.FloorToInt(position.z / chunkSize.z ) * chunkSize.z);
-            return chunk.GetBlock(pos);
+            return chunk.GetBlock(GetVoxelFromPosition(position, chunk));
         }
 
-        public void SetBlock(Vector3Int position, TChunkBlock data)
+        public void SetBlock(Vector3 position, TChunkBlock data)
         {
             Chunk<TChunkBlock> chunk = GetChunk(position);
             if(!chunk.isInitialized()) return;
-            Vector3Int pos = new Vector3Int();
-            pos.x = position.x - (Mathf.FloorToInt(position.x / chunkSize.x ) * chunkSize.x);
-            pos.y = position.y - (Mathf.FloorToInt(position.y / chunkSize.y ) * chunkSize.y);
-            pos.z = position.z - (Mathf.FloorToInt(position.z / chunkSize.z ) * chunkSize.z);
+            Vector3Int pos = GetVoxelFromPosition(position, chunk);
+            if(pos.x >= chunkSize.x || pos.y >= chunkSize.y || pos.z >= chunkSize.z) return;
             chunk.SetBlock(pos, data);
         }
 
-        public Chunk<TChunkBlock> GetChunk(Vector3Int position)
+        public Chunk<TChunkBlock> GetChunk(Vector3 position)
         {
-            Vector3Int pos = new Vector3Int();
-            pos.x = Mathf.FloorToInt(position.x / chunkSize.x ) * chunkSize.x;
-            pos.y = Mathf.FloorToInt(position.y / chunkSize.y ) * chunkSize.y;
-            pos.z = Mathf.FloorToInt(position.z / chunkSize.z ) * chunkSize.z;
+            // Vector3Int pos = new Vector3Int();
+            // pos.x = Mathf.FloorToInt(position.x / chunkSize.x ) * chunkSize.x;
+            // pos.y = Mathf.FloorToInt(position.y / chunkSize.y ) * chunkSize.y;
+            // pos.z = Mathf.FloorToInt(position.z / chunkSize.z ) * chunkSize.z;
+            // Vector3Int pos = new Vector3Int();
+            // pos.x = Mathf.FloorToInt(position.x / chunkSize.x * voxelSize.x ) * chunkSize.x;
+            // pos.y = Mathf.FloorToInt(position.y / chunkSize.y * voxelSize.y) * chunkSize.y;
+            // pos.z = Mathf.FloorToInt(position.z / chunkSize.z * voxelSize.z) * chunkSize.z;
+
+            Vector3 pos = new Vector3(position.x / voxelSize.x, position.y / voxelSize.y, position.z / voxelSize.z);
+            Vector3Int chunkPos = new Vector3Int();
+            chunkPos.x = Mathf.FloorToInt(pos.x / chunkSize.x ) * chunkSize.x;
+            chunkPos.y = Mathf.FloorToInt(pos.y / chunkSize.y ) * chunkSize.y;
+            chunkPos.z = Mathf.FloorToInt(pos.z / chunkSize.z ) * chunkSize.z;
+            //Vector3Int chunkPos = new Vector3Int(Mathf.RoundToInt(pos.x), Mathf.RoundToInt(pos.y), Mathf.RoundToInt(pos.z));
             Chunk<TChunkBlock> chunk;
-            chunks.TryGetValue(pos, out chunk);
+            //if(!chunks.TryGetValue(chunkPos, out chunk)) Debug.LogWarning($"Chunk not found for position {chunkPos}");
+            chunks.TryGetValue(chunkPos, out chunk);
             return chunk;
         }
     }