Example #1
0
    // Refresh chunk and neighbors mesh by a given world position
    public IEnumerator RefreshChunkMesh(Vector3i worldPos, bool async = false)
    {
        ChunkRenderer.UpdateHeightMap(this, worldPos, _worldVisibleSizeY);

        if (_lightAttenuation == true)
        {
            ChunkRenderer.LightningFloodArea(this, worldPos, _worldVisibleSizeY);
        }

        Vector3i blockPosInsideChunk = WorldToChunkPosition(worldPos);

        // Refresh Main Chunk (the chunk that contains the worldPosition)
        Vector3i chunkIndex = GetChunkIndex(worldPos);

        RefreshChunkMeshByIndex(chunkIndex);
        if (async)
        {
            yield return(null);
        }

        // Refresh chunk neighbors if needed
        #region RefreshChunkNeighbors
        if (blockPosInsideChunk.y >= Chunk.SizeY - 1)
        {
            Vector3i neighborChunkIndex = chunkIndex;
            neighborChunkIndex.y++;
            RefreshChunkMeshByIndex(neighborChunkIndex);
            if (async)
            {
                yield return(null);
            }
        }
        if (blockPosInsideChunk.y <= 0)
        {
            Vector3i neighborChunkIndex = chunkIndex;
            neighborChunkIndex.y--;
            RefreshChunkMeshByIndex(neighborChunkIndex);
            if (async)
            {
                yield return(null);
            }
        }

        if (blockPosInsideChunk.x >= Chunk.SizeX - 1)
        {
            Vector3i neighborChunkIndex = chunkIndex;
            neighborChunkIndex.x++;
            RefreshChunkMeshByIndex(neighborChunkIndex);
            if (async)
            {
                yield return(null);
            }
        }
        if (blockPosInsideChunk.x <= 0)
        {
            Vector3i neighborChunkIndex = chunkIndex;
            neighborChunkIndex.x--;
            RefreshChunkMeshByIndex(neighborChunkIndex);
            if (async)
            {
                yield return(null);
            }
        }

        if (blockPosInsideChunk.z >= Chunk.SizeZ - 1)
        {
            Vector3i neighborChunkIndex = chunkIndex;
            neighborChunkIndex.z++;
            RefreshChunkMeshByIndex(neighborChunkIndex);
            if (async)
            {
                yield return(null);
            }
        }
        if (blockPosInsideChunk.z <= 0)
        {
            Vector3i neighborChunkIndex = chunkIndex;
            neighborChunkIndex.z--;
            RefreshChunkMeshByIndex(neighborChunkIndex);
            if (async)
            {
                yield return(null);
            }
        }
        #endregion
    }