I am making a game that's going to use a Class 1 (This is (n,0)) Goldberg Polyhedron as the map, that is I'm planning on using the hexagons and 12 pentagons as tiles for a tile-based game.
When it came to generate this shapes, I researched online and came to the conclusion that the best way to get there is to do an icosphere appropiately divided and then find it's dual shape so I got to work onto that and I came up with a code that will generate the vertices of an icosahedron divided just like I wanted and with zero duplicates.
Now my problem is, I'm having a lot of trouble finding a nice way to triangulate all this faces, because as some edges are shared the vertex index vary a lot. I'm gonna post some of my code and see if someone can help me out here cause i've been thinking about this for hours and I'm sure I'm just missing something.
void Subdivide(int frequency, List<Vector3> vertList, List<TriangleIndices> faces){
List<Vector3> workingList = new List<Vector3> ();
for (int i = 0; i < faces.Count; i++){
float div = frequency + 1;
Vector3 v1 = vertList [faces[i].v1];
Vector3 v2 = vertList [faces [i].v2];
Vector3 v3 = vertList [faces [i].v3];
for (float k =0; k <= (frequency + 1); k++) {
for (float j = 0; j <= div; j++){
if (div == 0) {
for (int c = 0; c < workingList.Count; c++) {
if (workingList [c] == ((v1 + (k / (frequency + 1f)) * (v3 - v1)) + 1f * ((v2 + (k / (frequency + 1f)) * (v3 - v2)) - (v1 + (k / (frequency + 1f)) * (v3 - v1)))).normalized) {
isDupe = true;
}
}
if (isDupe == false) {
workingList.Add (((v1 + (k / (frequency + 1f)) * (v3 - v1)) + 1f * ((v2 + (k / (frequency + 1f)) * (v3 - v2)) - (v1 + (k / (frequency + 1f)) * (v3 - v1)))).normalized);
} else {
isDupe = false;
}
} else {
for (int c = 0; c < workingList.Count; c++) {
if (workingList [c] == ((v1 + (k / (frequency + 1f)) * (v3 - v1)) + (j / div) * ((v2 + (k / (frequency + 1f)) * (v3 - v2)) - (v1 + (k / (frequency + 1f)) * (v3 - v1)))).normalized) {
isDupe = true;
}
}
if (isDupe == false) {
workingList.Add (((v1 + (k / (frequency + 1f)) * (v3 - v1)) + (j / div) * ((v2 + (k / (frequency + 1f)) * (v3 - v2)) - (v1 + (k / (frequency + 1f)) * (v3 - v1)))).normalized);
} else {
isDupe = false;
}
}
}
div --;
}
Triangulate (frequency, workingList);
}
This is a function that given the frequency, this is the number of vertices that are placed between two vertexes on the original triangle, a list of vertices, and a list of faces it will, without duplicates, subdivide the faces accordingly. Then it calls the triangulate function that is supposed to group this into actual faces.
This for a frequency 'f' means that now every edge will have 2+f vertices instead of 2. I use this definition of frequency because it's what it's used when talking about Goldberg polyhedra.
void Triangulate(int frequency, List<Vector3> vertList){
List<TriangleIndices> faces = new List<TriangleIndices> ();
int div = frequency + 1;
int divRecursive = 0;
for (int k = 0; k < frequency + 1; k++) {
for (int i = 0; i < div ; i++) {
faces.Add (new TriangleIndices (i + divRecursive, i + 1 + divRecursive, i + div + 1 + divRecursive));
}
for (int i = 0; i < div - 1; i++) {
faces.Add (new TriangleIndices (i + 1 + divRecursive, i + div + 2 + divRecursive, i + div + 1 + divRecursive));
}
divRecursive += div + 1;
div--;
}
List< int > triList = new List<int> ();
for (int i = 0; i < faces.Count; i++) {
triList.Add (faces [i].v1);
triList.Add (faces [i].v2);
triList.Add (faces [i].v3);
}
Debug.Log("There are " + faces.Count +" faces and " + vertList.Count + " vertices");
mesh.vertices = vertList.ToArray ();
mesh.triangles = triList.ToArray();
mesh.RecalculateNormals();
mesh.RecalculateBounds ();
}
This is my triangulate function, and at the moment, it only does the first face because i'm not sure how to do the other faces. Worth noting some stuff about it, divRecursive is a variable used to store the first vertex of the next line, div stands for divisions and is the number of divisions on each edge.
Now I need them to be grouped in faces to be able to then find the center of each face and apply a duality transformation to get the shape I actually want so if you have any tips for that too I'd be more than happy to hear them.