void TriangulateCube(int index) { Vector3 positon = allCubePoint[index]; CubeData data = CubeData.ToCubeData(datas[index]); data.orientate = data.orientate.AddOrientate(PreviewOrientate); Vector3[] cubeVertices = GetCubeVertices(positon, data.orientate); for (int i = 0; i < 6; i++) { CubeOrientate temp = (CubeOrientate)i; if (i < 4) { temp = temp.SubOrientate(PreviewOrientate); temp = temp.AddOrientate(data.orientate); } if (!CheckAdjacent(index, (CubeSurface)temp)) { TriangulateCubeSurface(cubeVertices, (CubeSurface)i, data); } } }
bool HasCube(CubeCoordinate coordinate) { byte cubeData = grid.GetCubeData(coordinate.ToVector3(CubeCoordinate.CoordinateType.cubeWorld)); CubeData data = CubeData.ToCubeData(cubeData); return(data.HasCube(false)); }
void DataTest() { for (int i = 0; i < cubes.Length; i++) { Debug.Log(i + ": " + CubeData.ToCubeData(cubes[i]).HasCube); } Debug.Log(grid.chunkDatas.ContainsKey(ChunkCoordinate.ToString())); }
void TriangualteAllCubes(CubeOrientate orientate) { GetCurrentCubePoints(orientate); for (int i = 0; i < datas.Length; i++) { CubeData data = CubeData.ToCubeData(datas[i]); if (data.active) { TriangulateCube(i); } } }
void SetDataFromPreviewBox(CubeOrientate orientate) { var allPoint = GetCurrentCubePoints(orientate); for (int i = 0; i < datas.Length; i++) { CubeData data = CubeData.ToCubeData(datas[i]); if (data.active) { data.orientate = data.orientate.AddOrientate(orientate); data.isTransparent = true; grid.SetCubeData(allPoint[i], data.ToByte()); BuilderAI.pendingPosList.Enqueue(allPoint[i]); } } }
void GetDataFromPreviewBox(CubeOrientate orientate) { var allPoint = GetCurrentCubePoints(orientate); for (int i = 0; i < allPoint.Length; i++) { CubeData data = CubeData.ToCubeData(grid.GetCubeData(allPoint[i])); if (!data.isTransparent && data.active) { data.orientate = data.orientate.SubOrientate(orientate); datas[i] = data.ToByte(); } else { datas[i] = byte.MinValue; } } }
void TriangulateByData() { Clear(); for (int y = 0; y < CubeMetrics.CHUNK_WIDTH; y++) { for (int z = 0; z < CubeMetrics.CHUNK_WIDTH; z++) { for (int x = 0; x < CubeMetrics.CHUNK_WIDTH; x++) { byte d = cubes[x + y * CubeMetrics.CHUNK_WIDTH * CubeMetrics.CHUNK_WIDTH + z * CubeMetrics.CHUNK_WIDTH]; CubeData data = CubeData.ToCubeData(d); if (data.active) { CubeCoordinate coordinate = new CubeCoordinate(x, y, z); TriangulateCube(x, y, z, data); } } } } Apply(); }
IEnumerator Building(Vector3 target) { Vector3 dir = (target - transform.position).normalized; dir.y = 0; transform.forward = dir; animator.SetTrigger("Build"); ParticleSystem partic = Instantiate(p); partic.transform.position = target; partic.Play(); CubeData data = CubeData.ToCubeData(grid.GetCubeData(target)); data.isTransparent = false; grid.SetCubeData(target, data.ToByte()); SetInfo(); yield return(new WaitForSeconds(0.5f)); }
bool CheckAdjacent(int x, int y, int z, CubeSurface surfaceTo) { int minValue = 0; int maxValue = CubeMetrics.CHUNK_WIDTH - 1; switch (surfaceTo) { case CubeSurface.front: if (z + 2 > CubeMetrics.CHUNK_WIDTH) { return(NeighborChunks[(int)AdjacentDirection.front] && CubeData.ToCubeData(NeighborChunks[(int)AdjacentDirection.front]. cubes[CubeMetrics.GetCubeIndexToChunk(x, y, minValue)]).HasCube); } return(CubeData.ToCubeData(cubes[CubeMetrics.GetCubeIndexToChunk(x, y, z + 1)]).HasCube); case CubeSurface.back: if (z - 1 < 0) { return(NeighborChunks[(int)AdjacentDirection.back] && CubeData.ToCubeData(NeighborChunks[(int)AdjacentDirection.back]. cubes[CubeMetrics.GetCubeIndexToChunk(x, y, maxValue)]).HasCube); } return(CubeData.ToCubeData(cubes[CubeMetrics.GetCubeIndexToChunk(x, y, z - 1)]).HasCube); case CubeSurface.left: if (x - 1 < 0) { return(NeighborChunks[(int)AdjacentDirection.left] && CubeData.ToCubeData(NeighborChunks[(int)AdjacentDirection.left].cubes[CubeMetrics.GetCubeIndexToChunk(maxValue, y, z)]).HasCube); } return(CubeData.ToCubeData(cubes[CubeMetrics.GetCubeIndexToChunk(x - 1, y, z)]).HasCube); case CubeSurface.right: if (x + 2 > CubeMetrics.CHUNK_WIDTH) { return(NeighborChunks[(int)AdjacentDirection.right] && CubeData.ToCubeData(NeighborChunks[(int)AdjacentDirection.right].cubes[CubeMetrics.GetCubeIndexToChunk(minValue, y, z)]).HasCube); } return(CubeData.ToCubeData(cubes[CubeMetrics.GetCubeIndexToChunk(x + 1, y, z)]).HasCube); case CubeSurface.up: if (y + 2 > CubeMetrics.CHUNK_WIDTH) { return(NeighborChunks[(int)AdjacentDirection.up] && CubeData.ToCubeData(NeighborChunks[(int)AdjacentDirection.up].cubes[CubeMetrics.GetCubeIndexToChunk(x, minValue, z)]).HasCube); } return(CubeData.ToCubeData(cubes[CubeMetrics.GetCubeIndexToChunk(x, y + 1, z)]).HasCube); case CubeSurface.down: if (y - 1 < 0) { return(NeighborChunks[(int)AdjacentDirection.down] && CubeData.ToCubeData(NeighborChunks[(int)AdjacentDirection.down].cubes[CubeMetrics.GetCubeIndexToChunk(x, maxValue, z)]).HasCube); } return(CubeData.ToCubeData(cubes[CubeMetrics.GetCubeIndexToChunk(x, y - 1, z)]).HasCube); default: return(false); } }
bool CheckAdjacent(int index, CubeSurface surfaceTo) { int tempIndex = index; int y = tempIndex / (localScale * localScale); int z = (tempIndex - (y * localScale * localScale)) / localScale; int x = tempIndex - (y * localScale * localScale) - (z * localScale); switch (surfaceTo) { case CubeSurface.front: if (z + 1 > 6) { return(false); } z += 1; break; case CubeSurface.back: if (z - 1 < 0) { return(false); } z -= 1; break; case CubeSurface.left: if (x - 1 < 0) { return(false); } x -= 1; break; case CubeSurface.right: if (x + 1 > 6) { return(false); } x += 1; break; case CubeSurface.up: if (y + 1 > 6) { return(false); } y += 1; break; case CubeSurface.down: if (y - 1 < 0) { return(false); } y -= 1; break; } int i = x + y * localScale * localScale + z * localScale; CubeData data = CubeData.ToCubeData(datas[i]); return(data.active); }
// //bool CheckAdjacent2(int x, int y, int z, CubeSurface surfaceTo) //{ // int minValue = 0; // int maxValue = CubeMetrics.CHUNK_WIDTH - 1; // CubeCoordinate c; // switch (surfaceTo) // { // case CubeSurface.front: // if (z + 2 > CubeMetrics.CHUNK_WIDTH) // { // c = ChunkCoordinate.GetAdjacentCoordinate(AdjacentDirection.front); // //return grid.chunkDatas.ContainsKey(c.ToString()) && // // CubeData.ToCubeData(grid.chunkDatas[c.ToString()][CubeMetrics.GetCubeIndexToChunk(x, y, minValue)]).HasCube; // //Debug.Log(grid.chunkDatas.ContainsKey(c.ToString())); // return NeighborChunks[(int)AdjacentDirection.front] && // CubeData.ToCubeData(NeighborChunks[(int)AdjacentDirection.front]. // cubes[CubeMetrics.GetCubeIndexToChunk(x, y, minValue)]).HasCube; // } // return CubeData.ToCubeData(cubes[CubeMetrics.GetCubeIndexToChunk(x, y, z + 1)]).HasCube; // case CubeSurface.back: // if (z - 1 < 0) // { // return NeighborChunks[(int)AdjacentDirection.back] && // CubeData.ToCubeData(NeighborChunks[(int)AdjacentDirection.back]. // cubes[CubeMetrics.GetCubeIndexToChunk(x, y, maxValue)]).HasCube; // } // return CubeData.ToCubeData(cubes[CubeMetrics.GetCubeIndexToChunk(x, y, z - 1)]).HasCube; // case CubeSurface.left: // if (x - 1 < 0) // { // return NeighborChunks[(int)AdjacentDirection.left] && // CubeData.ToCubeData(NeighborChunks[(int)AdjacentDirection.left].cubes[CubeMetrics.GetCubeIndexToChunk(maxValue, y, z)]).HasCube; // } // return CubeData.ToCubeData(cubes[CubeMetrics.GetCubeIndexToChunk(x - 1, y, z)]).HasCube; // case CubeSurface.right: // if (x + 2 > CubeMetrics.CHUNK_WIDTH) // { // return NeighborChunks[(int)AdjacentDirection.right] && // CubeData.ToCubeData(NeighborChunks[(int)AdjacentDirection.right].cubes[CubeMetrics.GetCubeIndexToChunk(minValue, y, z)]).HasCube; // } // return CubeData.ToCubeData(cubes[CubeMetrics.GetCubeIndexToChunk(x + 1, y, z)]).HasCube; // case CubeSurface.up: // if (y + 2 > CubeMetrics.CHUNK_WIDTH) // { // return NeighborChunks[(int)AdjacentDirection.up] && // CubeData.ToCubeData(NeighborChunks[(int)AdjacentDirection.up].cubes[CubeMetrics.GetCubeIndexToChunk(x, minValue, z)]).HasCube; // } // return CubeData.ToCubeData(cubes[CubeMetrics.GetCubeIndexToChunk(x, y + 1, z)]).HasCube; // case CubeSurface.down: // if (y - 1 < 0) // { // return NeighborChunks[(int)AdjacentDirection.down] && // CubeData.ToCubeData(NeighborChunks[(int)AdjacentDirection.down].cubes[CubeMetrics.GetCubeIndexToChunk(x, maxValue, z)]).HasCube; // } // return CubeData.ToCubeData(cubes[CubeMetrics.GetCubeIndexToChunk(x, y - 1, z)]).HasCube; // default: // return false; // } //} //考虑未实例化到场景中的chunk, 直接在数据表里核对是否需要隐藏 bool CheckAdjacent(int x, int y, int z, CubeSurface surfaceTo, bool isTransparent) { int tempX = x; int tempY = y; int tempZ = z; bool outOfRange = false; CubeCoordinate coordinate; switch (surfaceTo) { case CubeSurface.front: outOfRange = z + 2 > CubeMetrics.CHUNK_WIDTH; coordinate = ChunkCoordinate.GetAdjacentCoordinate(AdjacentDirection.front); tempZ = outOfRange ? 0 : z + 1; break; case CubeSurface.back: outOfRange = z - 1 < 0; coordinate = ChunkCoordinate.GetAdjacentCoordinate(AdjacentDirection.back); tempZ = outOfRange ? CubeMetrics.CHUNK_WIDTH - 1 : z - 1; break; case CubeSurface.left: outOfRange = x - 1 < 0; coordinate = ChunkCoordinate.GetAdjacentCoordinate(AdjacentDirection.left); tempX = outOfRange ? CubeMetrics.CHUNK_WIDTH - 1 : x - 1; break; case CubeSurface.right: outOfRange = x + 2 > CubeMetrics.CHUNK_WIDTH; coordinate = ChunkCoordinate.GetAdjacentCoordinate(AdjacentDirection.right); tempX = outOfRange ? 0 : x + 1; break; case CubeSurface.up: outOfRange = y + 2 > CubeMetrics.CHUNK_WIDTH; coordinate = ChunkCoordinate.GetAdjacentCoordinate(AdjacentDirection.up); tempY = outOfRange ? 0 : y + 1; break; case CubeSurface.down: outOfRange = y - 1 < 0; coordinate = ChunkCoordinate.GetAdjacentCoordinate(AdjacentDirection.down); tempY = outOfRange ? CubeMetrics.CHUNK_WIDTH - 1 : y - 1; break; default: coordinate = ChunkCoordinate; break; } try { return(outOfRange ? grid.chunkDatas.ContainsKey(coordinate.ToString()) && CubeData.ToCubeData(grid.chunkDatas[coordinate.ToString()][CubeMetrics.GetCubeIndexToChunk(tempX, tempY, tempZ)]).HasCube(isTransparent) : CubeData.ToCubeData(cubes[CubeMetrics.GetCubeIndexToChunk(tempX, tempY, tempZ)]).HasCube(isTransparent)); } catch { Debug.Log("chunk" + ChunkCoordinate.ToString() + ":Surface" + surfaceTo + "index is error"); return(false); } }