1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| public class GreedyMeshBuilder { private struct FaceInfo { public BlockType blockType; public bool merged; }
public void GenerateSliceMesh( ChunkData chunk, int sliceCoord, // 切片在 axis 方向的坐标 int axis, bool positive, List<Vector3> vertices, List<int> triangles, List<Vector2> uvs, List<Color32> colors) { int uSize = axis == 0 ? VoxelConfig.CHUNK_HEIGHT : VoxelConfig.CHUNK_SIZE; int vSize = axis == 2 ? VoxelConfig.CHUNK_HEIGHT : VoxelConfig.CHUNK_SIZE;
var faceGrid = new FaceInfo[uSize, vSize];
for (int u = 0; u < uSize; u++) { for (int v = 0; v < vSize; v++) { int x = axis == 0 ? sliceCoord : (axis == 2 ? v : u); int y = axis == 1 ? sliceCoord : (axis == 2 ? sliceCoord : v); int z = axis == 2 ? sliceCoord : (axis == 0 ? v : u);
BlockType current = chunk.GetBlock(x, y, z); int nx = x + (axis == 0 ? (positive ? 1 : -1) : 0); int ny = y + (axis == 1 ? (positive ? 1 : -1) : 0); int nz = z + (axis == 2 ? (positive ? 1 : -1) : 0); BlockType neighbor = chunk.GetBlock(nx, ny, nz);
if (current != BlockType.Air && neighbor == BlockType.Air) faceGrid[u, v] = new FaceInfo { blockType = current, merged = false }; else faceGrid[u, v] = new FaceInfo { blockType = BlockType.Air, merged = true }; } }
for (int u = 0; u < uSize; u++) { for (int v = 0; v < vSize; v++) { if (faceGrid[u, v].merged) continue; BlockType type = faceGrid[u, v].blockType;
int vEnd = v + 1; while (vEnd < vSize && !faceGrid[u, vEnd].merged && faceGrid[u, vEnd].blockType == type) vEnd++;
int uEnd = u + 1; while (uEnd < uSize) { bool canExpand = true; for (int vi = v; vi < vEnd; vi++) { if (faceGrid[uEnd, vi].merged || faceGrid[uEnd, vi].blockType != type) { canExpand = false; break; } } if (!canExpand) break; uEnd++; }
for (int ui = u; ui < uEnd; ui++) for (int vi = v; vi < vEnd; vi++) faceGrid[ui, vi].merged = true;
AddFace(vertices, triangles, uvs, colors, u, v, uEnd - u, vEnd - v, sliceCoord, axis, positive, type); } } }
void AddFace(List<Vector3> verts, List<int> tris, List<Vector2> uvs, List<Color32> colors, int u, int v, int uSize, int vSize, int sliceCoord, int axis, bool positive, BlockType type) { int baseIdx = verts.Count;
Vector3[] faceVerts = new Vector3[4]; Vector3[] positions = { ToWorldPos(u, v, sliceCoord, axis, positive), ToWorldPos(u + uSize,v, sliceCoord, axis, positive), ToWorldPos(u + uSize,v + vSize,sliceCoord, axis, positive), ToWorldPos(u, v + vSize,sliceCoord, axis, positive), };
verts.AddRange(positions);
if (positive) { tris.AddRange(new[] { baseIdx, baseIdx+1, baseIdx+2, baseIdx, baseIdx+2, baseIdx+3 }); } else { tris.AddRange(new[] { baseIdx, baseIdx+2, baseIdx+1, baseIdx, baseIdx+3, baseIdx+2 }); }
var atlasUV = GetAtlasUV(type, axis, positive, uSize, vSize); uvs.AddRange(atlasUV);
colors.AddRange(new Color32[] { new Color32(255, 255, 255, 255), new Color32(255, 255, 255, 255), new Color32(255, 255, 255, 255), new Color32(255, 255, 255, 255) }); }
Vector3 ToWorldPos(int u, int v, int s, int axis, bool positive) { float offset = positive ? 1 : 0; switch (axis) { case 0: return new Vector3(s + offset, u, v); case 1: return new Vector3(u, s + offset, v); default: return new Vector3(u, v, s + offset); } }
Vector2[] GetAtlasUV(BlockType type, int axis, bool positive, int uSize, int vSize) { return new Vector2[4] { new Vector2(0, 0), new Vector2(uSize, 0), new Vector2(uSize, vSize), new Vector2(0, vSize) }; } }
|