public bool ModifyBlock(Vector3Int blockPosition, int value) { Vector3Int chunkCoord = blockPosition.ToChunkCoord(); Vector3Int localBlockPosition = blockPosition.ToChunkPosition(); if (ExistingChunks.TryGetValue(chunkCoord, out Chunk chunk)) { if (chunk.ModifyBlock(localBlockPosition, value)) { MarkForMeshGeneration(chunk); for (int i = 1; i < 8; i++) { int x = i & 1; int y = (i & 2) >> 1; int z = (i & 4) >> 2; if ((localBlockPosition.x * x == 0) && (localBlockPosition.y * y == 0) && (localBlockPosition.z * z == 0) && ExistingChunks.TryGetValue(chunkCoord - new Vector3Int(x, y, z), out chunk)) { MarkForMeshGeneration(chunk); } } return(true); } } return(false); }
public void GetBlockValueArray(out byte[] blocksArray, Vector3Int fromPosition, Vector3Int toPosition) { if (fromPosition.x > toPosition.x || fromPosition.y > toPosition.y || fromPosition.z > toPosition.z) { blocksArray = new byte[0]; return; } Vector3Int resultSize = toPosition - fromPosition + Vector3Int.one; blocksArray = new byte[resultSize.x * resultSize.y * resultSize.z]; Vector3Int fromCoord = fromPosition.ToChunkCoord(); Vector3Int toCoord = toPosition.ToChunkCoord(); // Go through required chunks and copy data for (int zCoord = fromCoord.z; zCoord <= toCoord.z; zCoord++) { for (int yCoord = fromCoord.y; yCoord <= toCoord.y; yCoord++) { for (int xCoord = fromCoord.x; xCoord <= toCoord.x; xCoord++) { Vector3Int chunkOrigin = (new Vector3Int(xCoord, yCoord, zCoord)).ToChunkOrigin(); Vector3Int chunkEnd = chunkOrigin + new Vector3Int(ChunkSize.Width - 1, ChunkSize.Height - 1, ChunkSize.Width - 1); Vector3Int from = Vector3Int.Max(fromPosition, chunkOrigin); Vector3Int to = Vector3Int.Min(toPosition, chunkEnd); Vector3Int startResultIndex = from - fromPosition; Vector3Int startChunkIndex = from.ToChunkPosition(); Vector3Int maxIndex = to - from; if (ExistingChunks.TryGetValue(new Vector3Int(xCoord, yCoord, zCoord), out Chunk chunk)) { for (int z = 0; z <= maxIndex.z; z++) { for (int y = 0; y <= maxIndex.y; y++) { for (int x = 0; x <= maxIndex.x; x++) { blocksArray[(startResultIndex.z + z) * resultSize.x * resultSize.y + (startResultIndex.y + y) * resultSize.x + startResultIndex.x + x] = chunk.Blocks[startChunkIndex.z + z, startChunkIndex.y + y, startChunkIndex.x + x]; } } } } else { for (int z = 0; z <= maxIndex.z; z++) { for (int y = 0; y <= maxIndex.y; y++) { for (int x = 0; x <= maxIndex.x; x++) { blocksArray[(startResultIndex.z + z) * resultSize.x * resultSize.y + (startResultIndex.y + y) * resultSize.x + startResultIndex.x + x] = 0; } } } } } } } }
public NativeArray <byte> GetBlockArrayReference(Vector3Int chunkCoord) { if (ExistingChunks.TryGetValue(chunkCoord, out Chunk chunk)) { return(chunk.Blocks.Native); } return(new NativeArray <byte>(0, Allocator.Persistent)); }
public void GetBlockValueArray(out byte[] blocksArray, Vector3Int chunkCoord) { blocksArray = new byte[ChunkSize.Length]; if (ExistingChunks.TryGetValue(chunkCoord, out Chunk chunk)) { chunk.Blocks.Native.CopyTo(blocksArray); } }
public byte GetBlockValue(Vector3Int localBlockPosition, Vector3Int chunkCoord) { if (ExistingChunks.TryGetValue(chunkCoord, out Chunk chunk)) { return(chunk.Blocks[localBlockPosition.z, localBlockPosition.y, localBlockPosition.x]); } return(0); }
public void GetBlockValueArray(out NativeArray <byte> blocksArray, Vector3Int chunkCoord) { if (ExistingChunks.TryGetValue(chunkCoord, out Chunk chunk)) { blocksArray = new NativeArray <byte>(chunk.Blocks.Native, Allocator.Persistent); return; } blocksArray = new NativeArray <byte>(ChunkSize.Length, Allocator.None); }
public void GetBlockValueArray(out byte[,,] blocksArray, Vector3Int chunkCoord) { blocksArray = new byte[ChunkSize.Width, ChunkSize.Height, ChunkSize.Width]; if (ExistingChunks.TryGetValue(chunkCoord, out Chunk chunk)) { byte[] tmp = new byte[blocksArray.Length]; chunk.Blocks.Native.CopyTo(tmp); Buffer.BlockCopy(tmp, 0, blocksArray, 0, blocksArray.Length); } }
/////////////// /* Interface */ /////////////// // Returns single block value public byte GetBlockValue(Vector3Int blockPosition) { Vector3Int localBlockPosition = blockPosition.ToChunkPosition(); Vector3Int chunkCoord = blockPosition.ToChunkCoord(); if (ExistingChunks.TryGetValue(chunkCoord, out Chunk chunk)) { return(chunk.Blocks[localBlockPosition.z, localBlockPosition.y, localBlockPosition.x]); } return(0); }