public void SetBlock(Vector3 pos, Block block) { Vector3 localPos = pos - transform.position; int blockX = Mathf.CeilToInt(localPos.x); int blockY = Mathf.CeilToInt(localPos.y); int blockZ = Mathf.CeilToInt(localPos.z); //print("pos: " + pos.x + ", " + pos.y + ", " + pos.z); //print("local pos: " + blockX + ", " + blockY + ", " + blockZ); _map[blockX, blockY, blockZ] = block; StartCoroutine(RebuildMesh()); if (block != null) { return; } #region 重构相邻的图块,补充未显示的面 // 右边 if (blockX == length - 1) { if (_rightChunk == null) { _rightChunk = ChunkMgr7Thread.GetChunkByChunkPos(blockX + 1, blockY, blockZ); } StartCoroutine(_rightChunk.RebuildMesh()); //Debug.Log("rihgt : " + _rightChunk.name); } // 左边 if (blockX == 0) { if (_leftChunk == null) { _leftChunk = ChunkMgr7Thread.GetChunkByChunkPos(chunkX - 1, chunkY, chunkZ); } StartCoroutine(_leftChunk.RebuildMesh()); //Debug.Log("left : " + _leftChunk.name); } // 前面 if (blockZ == 0) { if (_frontChunk == null) { _frontChunk = ChunkMgr7Thread.GetChunkByChunkPos(blockX, blockY, blockZ - 1); } StartCoroutine(_frontChunk.RebuildMesh()); //Debug.Log("front : " + _frontChunk.name); } // 后面 if (blockZ == width - 1) { if (_backChunk == null) { _backChunk = ChunkMgr7Thread.GetChunkByChunkPos(blockX, blockY, blockZ + 1); } StartCoroutine(_backChunk.RebuildMesh()); //Debug.Log("back : " + _backChunk.name); } // 上面 if (blockY == height - 1) { if (_topChunk == null) { _topChunk = ChunkMgr7Thread.GetChunkByChunkPos(blockX, blockY + 1, blockZ); } StartCoroutine(_topChunk.RebuildMesh()); //Debug.Log("top : " + _topChunk.name); } // 下面 if (blockY == 0) { if (_bottomChunk == null) { _bottomChunk = ChunkMgr7Thread.GetChunkByWorldPos(blockX, blockY - 1, blockZ); } StartCoroutine(_bottomChunk.RebuildMesh()); //Debug.Log("bottom : " + _bottomChunk.name); } #endregion }
/// <summary> /// 控制重建网格后,显示的面 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="z"></param> /// <returns>是否显示</returns> private bool IsRebuildBlockTransparent(int x, int y, int z) { #region 检查周围是否有图块,没有则显示该面 Vector3 worldPos = DataUtil.FloorToInt(new Vector3(x, y, z) + transform.position); // 右边 if (x >= length) { if (_rightChunk == null) { _rightChunk = ChunkMgr7Thread.GetChunkByChunkPos(chunkX + 1, chunkY, chunkZ); } if (_rightChunk != null && _rightChunk != this && _rightChunk.ready) { return(_rightChunk.GetBlock(worldPos) == null); } return(true); } // 左边 if (x < 0) { if (_leftChunk == null) { _leftChunk = ChunkMgr7Thread.GetChunkByChunkPos(chunkX - 1, chunkY, chunkZ); } if (_leftChunk != null && _leftChunk != this && _leftChunk.ready) { return(_leftChunk.GetBlock(worldPos) == null); } return(true); } // 前面 if (z < 0) { if (_frontChunk == null) { _frontChunk = ChunkMgr7Thread.GetChunkByWorldPos(worldPos); } if (_frontChunk != null && _frontChunk != this && _frontChunk.ready) { return(_frontChunk.GetBlock(worldPos) == null); } return(true); } // 后面 if (z >= width) { if (_backChunk == null) { _backChunk = ChunkMgr7Thread.GetChunkByWorldPos(worldPos); } if (_backChunk != null && _backChunk != this && _backChunk.ready) { return(_backChunk.GetBlock(worldPos) == null); } return(true); } // 上面 if (y >= height) { if (_topChunk == null) { _topChunk = ChunkMgr7Thread.GetChunkByWorldPos(worldPos); } if (_topChunk != null && _topChunk != this && _topChunk.ready) { return(_topChunk.GetBlock(worldPos) == null); } return(true); } // 下面 if (y < 0) { if (_bottomChunk == null) { _bottomChunk = ChunkMgr7Thread.GetChunkByWorldPos(worldPos); } if (_bottomChunk != null && _bottomChunk != this && _bottomChunk.ready) { return(_bottomChunk.GetBlock(worldPos) == null); } return(true); } #endregion if (_map[x, y, z] == null) { return(true); } return(false); }