In case anyone needs it, I managed to create the sphere, here’s the code (I’m actually using the ProceduralMeshComponent in this one, but the code stays basically the same):
void ASphereActor::GenerateSphereMesh()
{
mVertices.Empty();
mIndices.Empty();
mNormals.Empty();
mUvs.Empty();
mVertexColors.Empty();
mTangents.Empty();
CreateSphereWithTriangles(mVertices, mIndices, mNormals, SphereRadius, SphereRings, SphereSectors);
mMesh->CreateMeshSection(0, mVertices, mIndices, mNormals, mUvs, mVertexColors, mTangents, false);
}
void ASphereActor::CreateSphereWithTriangles(TArray<FVector> &vertices, TArray<int32> &indices, TArray<FVector> &normals, float radius, int stacks, int slices)
{
double stackAngle = PI / stacks;
double sliceAngle = 2.0 * PI / slices;
int i, j, v1_index, v2_index, v3_index;
double a = 0;
double b = a + stackAngle;
double r0 = radius * sin(a);
double r1 = radius * sin(b);
float z0 = radius * cos(a);
float z1 = radius * cos(b);
double c = 0;
float x = cos(c);
float y = sin(c);
vertices.Add(FVector(x * r0, y * r0, z0));
v1_index = vertices.Num();
vertices.Add(FVector(x * r1, y * r1, z1));
v2_index = vertices.Num();
for (i = 0; i < stacks; i++)
{
a = i * stackAngle;
b = a + stackAngle;
r0 = radius * sin(a);
r1 = radius * sin(b);
z0 = radius * cos(a);
z1 = radius * cos(b);
j = i ? 0 : 1;
for (; j <= slices; j++)
{
c = j * sliceAngle;
x = cos(c);
y = sin(c);
vertices.Add(FVector(x * r0, y * r0, z0));
v3_index = vertices.Num();
indices.Add(v1_index);
indices.Add(v2_index);
indices.Add(v3_index);
v1_index = v3_index;
vertices.Add(FVector(x * r1, y * r1, z1));
v3_index = vertices.Num();
indices.Add(v1_index);
indices.Add(v2_index);
indices.Add(v3_index);
v2_index = v3_index;
}
}
}
It’s not the prettiest code but it works: