private void GetNextChunkGroup() { currentChunkGroup = nextChunkGroup; if (nextNonConnectorGroup == null) { var nextGroup = chunks.chunkGroups[Random.Range(0, chunks.chunkGroups.Count)]; nextNonConnectorGroup = nextGroup; } if (isNextGroupConnector) { var nextGroup = chunks.chunkGroups[Random.Range(0, chunks.chunkGroups.Count)]; if (nextGroup.name == nextChunkGroup.name) { return; } nextNonConnectorGroup = nextGroup; nextChunkGroup = chunks.GetChunkConnectorGroup(nextChunkGroup, nextGroup); } else { nextChunkGroup = nextNonConnectorGroup; } isNextGroupConnector = !isNextGroupConnector; }
public ChunkGroup GetChunkConnectorGroup(ChunkGroup previousChunkGroup, ChunkGroup nextChunkGroup) { foreach (var chunkConnectorGroup in chunkConnectorGroups) { if (chunkConnectorGroup.from == previousChunkGroup.name && chunkConnectorGroup.to == nextChunkGroup.name) { return(chunkConnectorGroup.chunkGroup); } } return(null); }
/// <inheritdoc cref="VoxelChunkMesher"/> protected override bool CanPlaceQuad(ChunkGroup chunkProperties, BlockPosition pos, int side) { var block = chunkProperties.GetBlock(pos); if (!block.Solid) { return(false); } var nextPos = pos; nextPos = nextPos.ShiftAlongDirection(side); if (!nextPos.IsWithinGrid(chunkProperties.ChunkSize)) { return(true); } var next = chunkProperties.GetBlock(nextPos); return(!next.Solid); }
/// <inheritdoc cref="VoxelChunkMesher"/> internal VisualRemeshTask(ChunkGroup chunkProperties, int materialID, GreedyMesher mesher) : base(chunkProperties, mesher) => MaterialID = materialID;
/// <inheritdoc cref="VoxelChunkMesher"/> internal CollisionRemeshTask(ChunkGroup chunkProperties, GreedyMesher mesher) : base(chunkProperties, mesher) { }
public static Mesh Mesh(Chunk chunk, ChunkGroup chunkGroup) { currentChunk = chunk; Clear(); /* * Vector3 scl = new Vector3( * chunk.ResolutionX / chunk.LengthX, * chunk.ResolutionY / chunk.LengthY, * chunk.ResolutionZ / chunk.LengthZ); */ for (int x = 0; x < chunk.typeGrid.GetLength(0); x++) { for (int y = 0; y < chunk.typeGrid.GetLength(1); y++) { for (int z = 0; z < chunk.typeGrid.GetLength(2); z++) { int voxelValue = chunk.typeGrid[x, y, z]; bool voxelIsMass = voxelValue > 0; //Color color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)); ushort colorVal = chunk.colorGrid[x, y, z]; ushort mask = 0xf; int r, g, b, a; r = (colorVal >> 12) & mask; g = (colorVal >> 8) & mask; b = (colorVal >> 4) & mask; a = (colorVal) & mask; Color color = new Color(r / 15f, g / 15f, b / 15f, a / 15f); Vector3 off = new Vector3(x, y, z); Vector3 orig = new Vector3(chunk.indexX * chunk.SizeX, chunk.indexY * chunk.SizeY, chunk.indexZ * chunk.SizeZ) + off; Vector3 forward = Vector3.forward; Vector3 right = Vector3.right; Vector3 up = Vector3.up; bool drawLeft = (CoordinateInBounds(x - 1, y, z) && chunk.typeGrid[x - 1, y, z] == 0) || (!CoordinateInBounds(x - 1, y, z)); bool drawRight = (CoordinateInBounds(x + 1, y, z) && chunk.typeGrid[x + 1, y, z] == 0) || (!CoordinateInBounds(x + 1, y, z)); bool drawBottom = (CoordinateInBounds(x, y - 1, z) && chunk.typeGrid[x, y - 1, z] == 0) || (!CoordinateInBounds(x, y - 1, z)); bool drawTop = (CoordinateInBounds(x, y + 1, z) && chunk.typeGrid[x, y + 1, z] == 0) || (!CoordinateInBounds(x, y + 1, z)); bool drawBack = (CoordinateInBounds(x, y, z - 1) && chunk.typeGrid[x, y, z - 1] == 0) || (!CoordinateInBounds(x, y, z - 1)); bool drawFront = (CoordinateInBounds(x, y, z + 1) && chunk.typeGrid[x, y, z + 1] == 0) || (!CoordinateInBounds(x, y, z + 1)); // left if (voxelIsMass && drawLeft) { DrawFace(orig + forward, -forward, up, -right, color); } // right if (voxelIsMass && drawRight) { DrawFace(orig + right, forward, up, right, color); } // bottom if (voxelIsMass && drawBottom) { DrawFace(orig + right, -right, forward, -up, color); } // top if (voxelIsMass && drawTop) { DrawFace(orig + up, right, forward, up, color); } // back if (voxelIsMass && drawBack) { DrawFace(orig, right, up, -forward, color); } // front if (voxelIsMass && drawFront) { DrawFace(orig + forward + right, -right, up, -forward, color); } } } } Mesh mesh = new Mesh(); mesh.vertices = vertices.ToArray(); mesh.triangles = triangles.ToArray(); mesh.normals = normals.ToArray(); mesh.colors = colors.ToArray(); mesh.RecalculateBounds(); //mesh.Optimize(); return(mesh); }
public static Mesh MarchingCubes(Chunk chunk, ChunkGroup chunkGroup) { currentChunk = chunk; Clear(); /* * Vector3 scl = new Vector3( * chunk.ResolutionX / chunk.LengthX, * chunk.ResolutionY / chunk.LengthY, * chunk.ResolutionZ / chunk.LengthZ); */ int ntriang; Vector3[] vertList = new Vector3[12]; float[,,] grid = PaddedChunkIsoGrid(chunk); /* * float[,,] grid = new float[chunk.SizeX + 2, chunk.SizeY + 2, chunk.SizeZ + 2]; * for (int x = 0; x < chunk.SizeX; x++) * { * for (int y = 0; y < chunk.SizeY; y++) * { * for (int z = 0; z < chunk.SizeZ; z++) * { * grid[x + 1, y + 1, z + 1] = chunk.isoGrid[x, y, z]; * } * } * } */ for (int x = 0; x < chunk.SizeX; x++) { for (int y = 0; y < chunk.SizeY; y++) { for (int z = 0; z < chunk.SizeZ; z++) { uint cubeIndex = 0; float v0, v1, v2, v3, v4, v5, v6, v7; v0 = z < chunk.SizeZ - 1 ? chunk.isoGrid[x, y, z + 1] : chunk.zPlusChunk != null ? chunk.zPlusChunk.isoGrid[x, y, 0] : 0f; v1 = chunk.isoGrid[x + 1, y, z + 1]; v2 = chunk.isoGrid[x + 1, y, z]; v3 = chunk.isoGrid[x, y, z]; v4 = chunk.isoGrid[x, y + 1, z + 1]; v5 = chunk.isoGrid[x + 1, y + 1, z + 1]; v6 = chunk.isoGrid[x + 1, y + 1, z]; v7 = chunk.isoGrid[x, y + 1, z]; Vector3 p0, p1, p2, p3, p4, p5, p6, p7; Vector3 o = new Vector3(x, y, z); p0 = o + Vector3.forward; p1 = o + Vector3.right + Vector3.forward; p2 = o + Vector3.right; p3 = o; p4 = o + Vector3.forward + Vector3.up; p5 = o + Vector3.right + Vector3.forward + Vector3.up; p6 = o + Vector3.right + Vector3.up; p7 = o + Vector3.up; if (v0 < isoVal) { cubeIndex |= 1; } if (v1 < isoVal) { cubeIndex |= 2; } if (v2 < isoVal) { cubeIndex |= 4; } if (v3 < isoVal) { cubeIndex |= 8; } if (v4 < isoVal) { cubeIndex |= 16; } if (v5 < isoVal) { cubeIndex |= 32; } if (v6 < isoVal) { cubeIndex |= 64; } if (v7 < isoVal) { cubeIndex |= 128; } if (edgeTable[cubeIndex] == 0) { continue; } /* * int voxelValue = chunk.typeGrid[x, y, z]; * bool voxelIsMass = voxelValue > 0; * * //Color color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)); * ushort colorVal = chunk.colorGrid[x, y, z]; * ushort mask = 0xf; * int r, g, b, a; * r = (colorVal >> 12) & mask; * g = (colorVal >> 8) & mask; * b = (colorVal >> 4) & mask; * a = (colorVal) & mask; * Color color = new Color(r / 15f, g / 15f, b / 15f, a / 15f); * * Vector3 off = new Vector3(x, y, z); * * Vector3 orig = new Vector3(chunk.indexX * chunk.SizeX, chunk.indexY * chunk.SizeY, chunk.indexZ * chunk.SizeZ) + off; * Vector3 forward = Vector3.forward; * Vector3 right = Vector3.right; * Vector3 up = Vector3.up; * * bool drawLeft = * (CoordinateInBounds(x - 1, y, z) && chunk.typeGrid[x - 1, y, z] == 0) || * (!CoordinateInBounds(x - 1, y, z)); * * bool drawRight = * (CoordinateInBounds(x + 1, y, z) && chunk.typeGrid[x + 1, y, z] == 0) || * (!CoordinateInBounds(x + 1, y, z)); * * bool drawBottom = * (CoordinateInBounds(x, y - 1, z) && chunk.typeGrid[x, y - 1, z] == 0) || * (!CoordinateInBounds(x, y - 1, z)); * * bool drawTop = * (CoordinateInBounds(x, y + 1, z) && chunk.typeGrid[x, y + 1, z] == 0) || * (!CoordinateInBounds(x, y + 1, z)); * * bool drawBack = * (CoordinateInBounds(x, y, z - 1) && chunk.typeGrid[x, y, z - 1] == 0) || * (!CoordinateInBounds(x, y, z - 1)); * * bool drawFront = * (CoordinateInBounds(x, y, z + 1) && chunk.typeGrid[x, y, z + 1] == 0) || * (!CoordinateInBounds(x, y, z + 1)); * * * // left * if (voxelIsMass && drawLeft) * { * DrawFace(orig + forward, -forward, up, -right, color); * } * // right * if (voxelIsMass && drawRight) * { * DrawFace(orig + right, forward, up, right, color); * } * // bottom * if (voxelIsMass && drawBottom) * { * DrawFace(orig + right, -right, forward, -up, color); * } * // top * if (voxelIsMass && drawTop) * { * DrawFace(orig + up, right, forward, up, color); * } * // back * if (voxelIsMass && drawBack) * { * DrawFace(orig, right, up, -forward, color); * } * // front * if (voxelIsMass && drawFront) * { * DrawFace(orig + forward + right, -right, up, -forward, color); * } */ } } } Mesh mesh = new Mesh(); mesh.vertices = vertices.ToArray(); mesh.triangles = triangles.ToArray(); mesh.normals = normals.ToArray(); mesh.colors = colors.ToArray(); mesh.RecalculateBounds(); //mesh.Optimize(); return(mesh); }