private void Update() { int3 newPlayerCoordinate = GetPlayerCoordinate(); if (!newPlayerCoordinate.Equals(_lastGenerationCoordinate)) { MoveVoxelData(_lastGenerationCoordinate, newPlayerCoordinate); var newlyFreedCoordinates = voxelWorld.ChunkStore.GetChunkCoordinatesOutsideOfRange(newPlayerCoordinate, renderDistance); int3 renderSize = new int3(renderDistance * 2 + 1); int3 oldPos = _lastGenerationCoordinate - new int3(renderDistance); BoundsInt oldCoords = new BoundsInt(oldPos.ToVectorInt(), renderSize.ToVectorInt()); int3 newPos = newPlayerCoordinate - new int3(renderDistance); BoundsInt newCoords = new BoundsInt(newPos.ToVectorInt(), renderSize.ToVectorInt()); int3[] coordinatesThatNeedChunks = CoordinateUtilities.GetCoordinatesThatNeedChunks(oldCoords, newCoords); int i = 0; foreach (int3 source in newlyFreedCoordinates) { int3 target = coordinatesThatNeedChunks[i]; // Move chunk gameobjects voxelWorld.ChunkStore.MoveChunk(source, target); // Move colors and generate new voxelWorld.VoxelColorStore.MoveChunk(source, target); chunkProvider.AddChunkToGenerationQueue(target); i++; } _lastGenerationCoordinate = newPlayerCoordinate; } }
/// <summary> /// Moves all of the voxel data that existed when the player was at the coordinate <paramref name="playerFromCoordinate"/> to the coordinates that don't yet have data but should have when the player is at coordinate <paramref name="playerToCoordinate"/> /// </summary> /// <param name="playerFromCoordinate">The old coordinate of the player</param> /// <param name="playerToCoordinate">The new coordinate of the player</param> private void MoveVoxelData(int3 playerFromCoordinate, int3 playerToCoordinate) { int range = renderDistance + loadingBufferSize; int3 renderSize = new int3(range * 2 + 1); int3 oldPos = playerFromCoordinate - new int3(range); BoundsInt oldCoords = new BoundsInt(oldPos.ToVectorInt(), renderSize.ToVectorInt()); int3 newPos = playerToCoordinate - new int3(range); BoundsInt newCoords = new BoundsInt(newPos.ToVectorInt(), renderSize.ToVectorInt()); int3[] coordinatesThatNeedData = CoordinateUtilities.GetCoordinatesThatNeedChunks(oldCoords, newCoords); var newlyFreedCoordinates = voxelWorld.VoxelDataStore.GetChunkCoordinatesOutsideOfRange(playerToCoordinate, range); int i = 0; foreach (int3 freeCoordinate in newlyFreedCoordinates) { var targetCoordinate = coordinatesThatNeedData[i]; voxelWorld.VoxelDataStore.MoveChunk(freeCoordinate, targetCoordinate); i++; } }