/// <summary> /// Creates chunk at given world position, if chunk already exists, the current chunk is being returned /// and no voxel/mesh generation is required. /// </summary> /// <param name="worldPosition">The world position.</param> /// <returns>The created/found chunk.</returns> public VoxelTerrainChunk CreateChunk(Vector3Int worldPosition) { var chunkMap = FindOrAddChunkMap(worldPosition); // Try to find chunk first var chunk = chunkMap.GetChunk(worldPosition); if (chunk != null) { // Queue for mesh generation if (chunk.HasVoxels && !chunk.HasMesh) { VoxelTerrainChunkGenerator.EnqueueMeshGeneration(chunk); } return(chunk); } // Create temporary test chunk chunk = new VoxelTerrainChunk(this, worldPosition); chunkMap.SetChunk(chunk, worldPosition); // Generate whole chunk, this probably will also generate all neighbors VoxelTerrainChunkGenerator.EnqueueGeneration(chunk); return(chunk); }
public bool Contains(VoxelTerrainChunk chunk) { lock (_cache) { return(_cache.Count(x => x.Chunk == chunk) != 0); } }
public void Remove(VoxelTerrainChunk chunk) { lock (_cache) { var entry = _cache.FirstOrDefault(x => x.Chunk == chunk); _cache.Remove(entry); } }
internal void UpdateNeighbors() { Neighbors = new VoxelTerrainChunk[NeighborChunkDirections.Length]; for (var i = 0; i < NeighborChunkDirections.Length; i++) { Neighbors[i] = _terrainMap.FindChunk(WorldPosition + NeighborChunkDirections[i] * Width); } }
/// <summary> /// Destroys given chunk. Must be called from main thread. /// </summary> /// <param name="chunk">The chunk which will be destroyed.</param> public void DestroyChunkNow(VoxelTerrainChunk chunk) { // Check if this chunk should be always loaded, if true, leave it as-is. if (!chunk.CanUnload) { return; } Object.Destroy(chunk.Actor); }
public void Add(VoxelTerrainChunk chunk) { lock (_cache) { _cache.Add(new CacheEntry { CacheTime = DateTime.Now, Chunk = chunk }); } }
/// <summary> /// Destroys given chunk after cache time. Must be called from main thread. /// </summary> /// <param name="chunk">The chunk which will be destroyed.</param> public void DestroyChunk(VoxelTerrainChunk chunk) { // Destroy chunk now, when cache time less or equal to 0. if (VoxelTerrainManager.Current.MaxChunkCacheTime <= 0) { DestroyChunkNow(chunk); return; } // Hide chunk chunk.Hide(); // Check if this chunk should be always loaded, if true, skip caching and leave it as-is. if (!chunk.CanUnload) { return; } // Add chunk to cache _chunkCache.Add(chunk); }
/// <summary> /// Sets chunk at given world position (will be snapped to chunk offset position). /// </summary> /// <param name="chunk">The chunk instance.</param> /// <param name="worldPosition">The world position.</param> public void SetChunk(VoxelTerrainChunk chunk, Vector3Int worldPosition) { var offset = WorldToLocalChunk(worldPosition); Chunks[offset.X, offset.Y, offset.Z] = chunk; }