• News

• Industries

• Learning & Support

• Community

• Marketplace # Need help converting from c++

I’ve been following this tutorial and converting it to blueprint using the runtime mesh component, everything has been working up until the triangles bit, it’s creating the bottom+top without needing their own individual functions, along with some of the faces facing inwards and some strange connections between points on opposite ends.

(I did change the XYZ to the gridsize from the http://catlikecoding.com/unity/tutorials/cube-sphere/ cube sphere tutorial)

I checked and all the vertices are in a sphere shape, with a few overlapping coordinates which is probably causing the strange connections. I didn’t follow through on the part where he splits it into sub-meshes so the c++ code included in the project didn’t really help me find a solution.
Here’s the C++ from his example though:

``````
private void CreateTriangles () {
int] trianglesZ = new int(gridSize * gridSize) * 12];
int] trianglesX = new int(gridSize * gridSize) * 12];
int] trianglesY = new int(gridSize * gridSize) * 12];
int ring = (gridSize + gridSize) * 2;
int tZ = 0, tX = 0, tY = 0, v = 0;

for (int y = 0; y < gridSize; y++, v++) {
for (int q = 0; q < gridSize; q++, v++) {
tZ = SetQuad(trianglesZ, tZ, v, v + 1, v + ring, v + ring + 1);
}
for (int q = 0; q < gridSize; q++, v++) {
tX = SetQuad(trianglesX, tX, v, v + 1, v + ring, v + ring + 1);
}
for (int q = 0; q < gridSize; q++, v++) {
tZ = SetQuad(trianglesZ, tZ, v, v + 1, v + ring, v + ring + 1);
}
for (int q = 0; q < gridSize - 1; q++, v++) {
tX = SetQuad(trianglesX, tX, v, v + 1, v + ring, v + ring + 1);
}
tX = SetQuad(trianglesX, tX, v, v - ring + 1, v + ring, v + 1);
}

tY = CreateTopFace(trianglesY, tY, ring);
tY = CreateBottomFace(trianglesY, tY, ring);

mesh.subMeshCount = 3;
mesh.SetTriangles(trianglesZ, 0);
mesh.SetTriangles(trianglesX, 1);
mesh.SetTriangles(trianglesY, 2);
}

private int CreateTopFace (int] triangles, int t, int ring) {
int v = ring * gridSize;
for (int x = 0; x < gridSize - 1; x++, v++) {
t = SetQuad(triangles, t, v, v + 1, v + ring - 1, v + ring);
}
t = SetQuad(triangles, t, v, v + 1, v + ring - 1, v + 2);

int vMin = ring * (gridSize + 1) - 1;
int vMid = vMin + 1;
int vMax = v + 2;

for (int z = 1; z < gridSize - 1; z++, vMin--, vMid++, vMax++) {
t = SetQuad(triangles, t, vMin, vMid, vMin - 1, vMid + gridSize - 1);
for (int x = 1; x < gridSize - 1; x++, vMid++) {
triangles, t,
vMid, vMid + 1, vMid + gridSize - 1, vMid + gridSize);
}
t = SetQuad(triangles, t, vMid, vMax, vMid + gridSize - 1, vMax + 1);
}

int vTop = vMin - 2;
t = SetQuad(triangles, t, vMin, vMid, vTop + 1, vTop);
for (int x = 1; x < gridSize - 1; x++, vTop--, vMid++) {
t = SetQuad(triangles, t, vMid, vMid + 1, vTop, vTop - 1);
}
t = SetQuad(triangles, t, vMid, vTop - 2, vTop, vTop - 1);

return t;
}

private int CreateBottomFace (int] triangles, int t, int ring) {
int v = 1;
int vMid = vertices.Length - (gridSize - 1) * (gridSize - 1);
t = SetQuad(triangles, t, ring - 1, vMid, 0, 1);
for (int x = 1; x < gridSize - 1; x++, v++, vMid++) {
t = SetQuad(triangles, t, vMid, vMid + 1, v, v + 1);
}
t = SetQuad(triangles, t, vMid, v + 2, v, v + 1);

int vMin = ring - 2;
vMid -= gridSize - 2;
int vMax = v + 2;

for (int z = 1; z < gridSize - 1; z++, vMin--, vMid++, vMax++) {
t = SetQuad(triangles, t, vMin, vMid + gridSize - 1, vMin + 1, vMid);
for (int x = 1; x < gridSize - 1; x++, vMid++) {
triangles, t,
vMid + gridSize - 1, vMid + gridSize, vMid, vMid + 1);
}
t = SetQuad(triangles, t, vMid + gridSize - 1, vMax + 1, vMid, vMax);
}

int vTop = vMin - 1;
t = SetQuad(triangles, t, vTop + 1, vTop, vTop + 2, vMid);
for (int x = 1; x < gridSize - 1; x++, vTop--, vMid++) {
t = SetQuad(triangles, t, vTop, vTop - 1, vMid, vMid + 1);
}
t = SetQuad(triangles, t, vTop, vTop - 1, vMid, vTop - 2);

return t;

private static int
SetQuad (int] triangles, int i, int v00, int v10, int v01, int v11) {
triangles* = v00;
triangles[i + 1] = triangles[i + 4] = v01;
triangles[i + 2] = triangles[i + 3] = v10;
triangles[i + 5] = v11;
return i + 6;
}
}

``````

Would like some help ASAP, much appreciated