// build the "physical" mesh for display // build the "physical" mesh for display public static Node[] BuildWorld(int sideLength, out TriangleGrid[] segment) { Node[] map; // make twenty segments, which are each a grid of triangles segment = new TriangleGrid[20]; // this part is somewhat complicated // luckily there are 'only' 20 of them segment[0] = new TriangleGrid(sideLength); segment[1] = new TriangleGrid(sideLength, rightEdge: segment[0].LeftEdge(), startIndex: segment[0].lastVertex); segment[2] = new TriangleGrid(sideLength, rightEdge: segment[1].LeftEdge(), startIndex: segment[1].lastVertex); segment[3] = new TriangleGrid(sideLength, rightEdge: segment[2].LeftEdge(), startIndex: segment[2].lastVertex); segment[4] = new TriangleGrid(sideLength, rightEdge: segment[3].LeftEdge(), leftEdge: segment[0].RightEdge(), startIndex: segment[3].lastVertex); // middle segment segment[5] = new TriangleGrid(sideLength, bottomEdge: Reversed(segment[4].BottomEdge()), startIndex: segment[4].lastVertex); segment[6] = new TriangleGrid(sideLength, bottomEdge: Reversed(segment[3].BottomEdge()), startIndex: segment[5].lastVertex); segment[7] = new TriangleGrid(sideLength, bottomEdge: Reversed(segment[2].BottomEdge()), startIndex: segment[6].lastVertex); segment[8] = new TriangleGrid(sideLength, bottomEdge: Reversed(segment[1].BottomEdge()), startIndex: segment[7].lastVertex); segment[9] = new TriangleGrid(sideLength, bottomEdge: Reversed(segment[0].BottomEdge()), startIndex: segment[8].lastVertex); // second middle segment segment[10] = new TriangleGrid(sideLength, rightEdge: segment[5].LeftEdge(), bottomEdge: Reversed(segment[6].RightEdge()), startIndex: segment[9].lastVertex); segment[11] = new TriangleGrid(sideLength, rightEdge: segment[6].LeftEdge(), bottomEdge: Reversed(segment[7].RightEdge()), startIndex: segment[10].lastVertex); segment[12] = new TriangleGrid(sideLength, rightEdge: segment[7].LeftEdge(), bottomEdge: Reversed(segment[8].RightEdge()), startIndex: segment[11].lastVertex); segment[13] = new TriangleGrid(sideLength, rightEdge: segment[8].LeftEdge(), bottomEdge: Reversed(segment[9].RightEdge()), startIndex: segment[12].lastVertex); segment[14] = new TriangleGrid(sideLength, rightEdge: segment[9].LeftEdge(), bottomEdge: Reversed(segment[5].RightEdge()), startIndex: segment[13].lastVertex); // bottom segment segment[15] = new TriangleGrid(sideLength, leftEdge: Reversed(segment[10].LeftEdge()), startIndex: segment[14].lastVertex); segment[16] = new TriangleGrid(sideLength, leftEdge: Reversed(segment[11].LeftEdge()), bottomEdge: Reversed(segment[15].RightEdge()), startIndex: segment[15].lastVertex); segment[17] = new TriangleGrid(sideLength, leftEdge: Reversed(segment[12].LeftEdge()), bottomEdge: Reversed(segment[16].RightEdge()), startIndex: segment[16].lastVertex); segment[18] = new TriangleGrid(sideLength, leftEdge: Reversed(segment[13].LeftEdge()), bottomEdge: Reversed(segment[17].RightEdge()), startIndex: segment[17].lastVertex); segment[19] = new TriangleGrid(sideLength, leftEdge: Reversed(segment[14].LeftEdge()), rightEdge: Reversed(segment[15].BottomEdge()), bottomEdge: Reversed(segment[18].RightEdge()), startIndex: segment[18].lastVertex); for (int n = 0; n < 20; n++) { segment[n].index = n; } // how many is that? int totalVertices = segment[19].lastVertex; map = new Node[totalVertices]; // assemble mesh- and map-vertices together into single arrays for (int i = 0; i < 20; i++) { foreach (Node n in segment[i].nodes) { // map vertex. there's a one-to-one correspondence map[n.index] = n; } } // put the triangles together as well List <int> mapTriangles = new List <int>(); for (int i = 0; i < 20; i++) { mapTriangles.AddRange(segment[i].triangles); } return(map); }
public TriangleGrid[] Split(int subGrids = 1) { // the grid needs to divide correctly for this to work if (vertsPerSide % subGrids != 1) { return(null); } int i = 0; TriangleGrid[] subs = new TriangleGrid[subGrids * subGrids]; // split into multiple grids int vertsPerSub = vertsPerSide / subGrids; for (int x = 0; x < subGrids; x++) { for (int y = 0; y <= x; y++) { // x2 and y2 are the coordinates within the sub-grid int x2 = 0, y2 = 0; // x1 and y1 are coordinates within the major grid, // while x and y designate the current sub-grid int z = 0; // z is the index of the current mapvertex within the sub-grid // here we construct a sub-grid, which will become a map region subs[i] = new TriangleGrid(vertsPerSub + 1); for (int x1 = x * vertsPerSub; x1 <= (x + 1) * vertsPerSub; x1++) { for (int y1 = y * vertsPerSub; y1 <= y * vertsPerSub + x2; y1++) { // this works for "up-facing" grid segments subs[i].nodes[z] = nodes[IndexOf(x1, y1)]; // add any triangles connected to this vertex // any triangles that should be anchored here if (y2 > 0) { // one triangle subs[i].triangles.Add(z); subs[i].triangles.Add(z - x2 - 1); subs[i].triangles.Add(z - 1); // in most cases, there are two triangles per vertex // but if it's on the right edge, where y = x, there's only one if (x2 > y2) { // another one subs[i].triangles.Add(z); subs[i].triangles.Add(z - x2); subs[i].triangles.Add(z - x2 - 1); } } // increment mapvertex index z += 1; // increment y coordinate y2 += 1; } x2 += 1; y2 = 0; } // count last index subs[i].lastVertex = z - 1; // increment sub-grid index i += 1; if (y < x) { // here we'll do a "down pointing" region // again, x2 and y2 are the coordinates within the sub-grid x2 = 0; y2 = 0; // x1 and y1 are coordinates within the major grid, // while x and y designate the current sub-grid z = 0; // and z is the index of the current mapvertex within the sub-grid // initialize sub-grid subs[i] = new TriangleGrid(vertsPerSub + 1); for (int x1 = x * vertsPerSub; x1 <= (x + 1) * vertsPerSub; x1++) { for (int y1 = y * vertsPerSub + x2; y1 <= (y + 1) * vertsPerSub; y1++) { subs[i].nodes[z] = nodes[IndexOf(x1, y1)]; // add any triangles connected to this vertex // any triangles that should be anchored here if (x2 > 0) { // one triangle subs[i].triangles.Add(z); subs[i].triangles.Add(z - vertsPerSub + x2 - 1); subs[i].triangles.Add(z - vertsPerSub + x2 - 2); // do a second triangle sometimes if (y2 > 0) { subs[i].triangles.Add(z); subs[i].triangles.Add(z - vertsPerSub + x2 - 2); subs[i].triangles.Add(z - 1); } } // increment mapvertex index z += 1; // increment y coordinate y2 += 1; } x2 += 1; y2 = 0; } // count last index subs[i].lastVertex = z - 1; // increment sub-grid index i += 1; } } } return(subs); }