//Сгенерировать меш public MyQuadMesh MakeGroundMesh(LocationCreator lc, MyVector offset, int h) { var quadMesh = new MyQuadMesh(); //Проходим по всем клеткам блока for (int x = 0; x < SizeX; x++) { for (int z = 0; z < SizeZ; z++) { int totalX = OffsetX + x; int totalZ = OffsetZ + z; int currentH = h; //Проверяем цвет var color = Colors[x, z]; if (color == null) { continue; } var y = Land[x, z]; //Строим верхнюю грань { if (lc.Land[x, z] - 1 != y) { var colorUp = color; int v = y; var color1 = lc.GetColor(colorUp, x, z, v + 1); var color2 = lc.GetColor(colorUp, x + 1, z, v + 1); var color3 = lc.GetColor(colorUp, x, z + 1, v + 1); var color4 = lc.GetColor(colorUp, x + 1, z + 1, v + 1); quadMesh.CreateY1(x + offset.X, v + offset.Y, z + offset.Z, new[] { color1, color2, color3, color4 }); } } //Строим боковые грани X0 if (totalX > 0) { var v0 = lc.Land[totalX - 1, totalZ]; for (int v = Math.Max(v0 + 1, y - currentH); v <= y; v++) { var color1 = lc.GetColor(color, x, z, v); var color2 = lc.GetColor(color, x, z, v + 1); var color3 = lc.GetColor(color, x, z + 1, v); var color4 = lc.GetColor(color, x, z + 1, v + 1); quadMesh.CreateX0(x + offset.X, v + offset.Y, z + offset.Z, new[] { color1, color2, color3, color4 }); } } //Строим боковые грани X1 if (lc.GenerateVoxelSideX1 && totalX < SizeX - 1) { var v0 = lc.Land[totalX + 1, totalZ]; for (int v = Math.Max(v0 + 1, y - currentH); v <= y; v++) { var color1 = lc.GetColor(color, x + 1, z, v); var color2 = lc.GetColor(color, x + 1, z + 1, v); var color3 = lc.GetColor(color, x + 1, z, v + 1); var color4 = lc.GetColor(color, x + 1, z + 1, v + 1); quadMesh.CreateX1(x + offset.X, v + offset.Y, z + offset.Z, new[] { color1, color2, color3, color4 }); } } //Строим боковые грани Z0 if (lc.GenerateVoxelSideZ0 && totalZ > 0) { var v0 = lc.Land[totalX, totalZ - 1]; for (int v = Math.Max(v0 + 1, y - currentH); v <= y; v++) { var color1 = lc.GetColor(color, x, z, v); var color2 = lc.GetColor(color, x + 1, z, v); var color3 = lc.GetColor(color, x, z, v + 1); var color4 = lc.GetColor(color, x + 1, z, v + 1); quadMesh.CreateZ0(x + offset.X, v + offset.Y, z + offset.Z, new[] { color1, color2, color3, color4 }); } } //Строим боковые грани Z1 if (totalZ < SizeZ - 1) { var v0 = lc.Land[totalX, totalZ + 1]; for (int v = Math.Max(v0 + 1, y - currentH); v <= y; v++) { var color1 = lc.GetColor(color, x, z + 1, v); var color2 = lc.GetColor(color, x, z + 1, v + 1); var color3 = lc.GetColor(color, x + 1, z + 1, v); var color4 = lc.GetColor(color, x + 1, z + 1, v + 1); quadMesh.CreateZ1(x + offset.X, v + offset.Y, z + offset.Z, new[] { color1, color2, color3, color4 }); } } } } return(quadMesh); }
public MyMesh Build(LocationCreator lc, int k = 4, int m = 4) { var qy1 = QuadGroup(quadranglesY1, q => q.y); var qx0 = QuadGroup(quadranglesX0, q => q.x); var qx1 = QuadGroup(quadranglesX1, q => q.x); var qz0 = QuadGroup(quadranglesZ0, q => q.z); var qz1 = QuadGroup(quadranglesZ1, q => q.z); if (bOptimizeEd) { if (k > 1) { qy1 = qy1.Select(x => QuadrangleOptimizeX(x, k, k).ToList()); qy1 = qy1.Select(x => QuadrangleOptimizeZ(x, k, k).ToList()); } if (m > 1) { qx0 = qx0.Select(x => QuadrangleOptimizeY(x, m, m).ToList()); qx0 = qx0.Select(x => QuadrangleOptimizeZ(x, m, m).ToList()); qx1 = qx1.Select(x => QuadrangleOptimizeY(x, m, m).ToList()); qx1 = qx1.Select(x => QuadrangleOptimizeZ(x, m, m).ToList()); qz0 = qz0.Select(x => QuadrangleOptimizeX(x, m, m).ToList()); qz0 = qz0.Select(x => QuadrangleOptimizeY(x, m, m).ToList()); qz1 = qz1.Select(x => QuadrangleOptimizeX(x, m, m).ToList()); qz1 = qz1.Select(x => QuadrangleOptimizeY(x, m, m).ToList()); } } if (bOptimize3) { qy1 = qy1.Select(x => QuadrangleOptimizeX(x, 4, 4).ToList()); qy1 = qy1.Select(x => QuadrangleOptimizeZ(x, 4, 4).ToList()); qx0 = qx0.Select(x => QuadrangleOptimizeY(x, 4, 4).ToList()); qx0 = qx0.Select(x => QuadrangleOptimizeZ(x, 4, 4).ToList()); qx1 = qx1.Select(x => QuadrangleOptimizeY(x, 4, 4).ToList()); qx1 = qx1.Select(x => QuadrangleOptimizeZ(x, 4, 4).ToList()); qz0 = qz0.Select(x => QuadrangleOptimizeX(x, 4, 4).ToList()); qz0 = qz0.Select(x => QuadrangleOptimizeY(x, 4, 4).ToList()); qz1 = qz1.Select(x => QuadrangleOptimizeX(x, 4, 4).ToList()); qz1 = qz1.Select(x => QuadrangleOptimizeY(x, 4, 4).ToList()); } if (k > 4) { if (bOptimize3) { qy1 = qy1.Select(x => QuadrangleOptimizeX(x, k, k).ToList()); qy1 = qy1.Select(x => QuadrangleOptimizeZ(x, k, k).ToList()); qx0 = qx0.Select(x => QuadrangleOptimizeY(x, k, k).ToList()); qx0 = qx0.Select(x => QuadrangleOptimizeZ(x, k, k).ToList()); qx1 = qx1.Select(x => QuadrangleOptimizeY(x, k, k).ToList()); qx1 = qx1.Select(x => QuadrangleOptimizeZ(x, k, k).ToList()); qz0 = qz0.Select(x => QuadrangleOptimizeX(x, k, k).ToList()); qz0 = qz0.Select(x => QuadrangleOptimizeY(x, k, k).ToList()); qz1 = qz1.Select(x => QuadrangleOptimizeX(x, k, k).ToList()); qz1 = qz1.Select(x => QuadrangleOptimizeY(x, k, k).ToList()); } } if (bOptimize2 && k > 1) { qy1 = qy1.Select(x => QuadrangleOptimizeX(x, 1, k).ToList()); qy1 = qy1.Select(x => QuadrangleOptimizeZ(x, 1, k).ToList()); qx0 = qx0.Select(x => QuadrangleOptimizeY(x, 1, k).ToList()); qx0 = qx0.Select(x => QuadrangleOptimizeZ(x, 1, k).ToList()); qx1 = qx1.Select(x => QuadrangleOptimizeY(x, 1, k).ToList()); qx1 = qx1.Select(x => QuadrangleOptimizeZ(x, 1, k).ToList()); qz0 = qz0.Select(x => QuadrangleOptimizeX(x, 1, k).ToList()); qz0 = qz0.Select(x => QuadrangleOptimizeY(x, 1, k).ToList()); qz1 = qz1.Select(x => QuadrangleOptimizeX(x, 1, k).ToList()); qz1 = qz1.Select(x => QuadrangleOptimizeY(x, 1, k).ToList()); } var _qy1 = qy1.ToList(); var _qx0 = qx0.ToList(); var _qx1 = qx1.ToList(); var _qz0 = qz0.ToList(); var _qz1 = qz1.ToList(); var count = _qy1.Sum(x => x.Count) + _qx0.Sum(x => x.Count) + _qx1.Sum(x => x.Count) + _qz0.Sum(x => x.Count) + _qz1.Sum(x => x.Count); if (count == 0) { return(null); } var mesh = new MyMesh(count * 4, count); foreach (var quads in _qy1) { foreach (var quad in quads) { Create(mesh, quad, orientVertices[QuadOrient.y1]); } } foreach (var quads in _qx0) { foreach (var quad in quads) { Create(mesh, quad, orientVertices[QuadOrient.x0]); } } foreach (var quads in _qx1) { foreach (var quad in quads) { Create(mesh, quad, orientVertices[QuadOrient.x1]); } } foreach (var quads in _qz0) { foreach (var quad in quads) { Create(mesh, quad, orientVertices[QuadOrient.z0]); } } foreach (var quads in _qz1) { foreach (var quad in quads) { Create(mesh, quad, orientVertices[QuadOrient.z1]); } } mesh = mesh.Optimize(); return(mesh); }