コード例 #1
0
        /// <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);
        }
コード例 #2
0
 public bool Contains(VoxelTerrainChunk chunk)
 {
     lock (_cache)
     {
         return(_cache.Count(x => x.Chunk == chunk) != 0);
     }
 }
コード例 #3
0
 public void Remove(VoxelTerrainChunk chunk)
 {
     lock (_cache)
     {
         var entry = _cache.FirstOrDefault(x => x.Chunk == chunk);
         _cache.Remove(entry);
     }
 }
コード例 #4
0
        internal void UpdateNeighbors()
        {
            Neighbors = new VoxelTerrainChunk[NeighborChunkDirections.Length];

            for (var i = 0; i < NeighborChunkDirections.Length; i++)
            {
                Neighbors[i] = _terrainMap.FindChunk(WorldPosition + NeighborChunkDirections[i] * Width);
            }
        }
コード例 #5
0
        /// <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);
        }
コード例 #6
0
 public void Add(VoxelTerrainChunk chunk)
 {
     lock (_cache)
     {
         _cache.Add(new CacheEntry
         {
             CacheTime = DateTime.Now,
             Chunk     = chunk
         });
     }
 }
コード例 #7
0
        /// <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);
        }
コード例 #8
0
            /// <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;
            }