How to link up vertices to form the triangles in a Fibonacci Sphere?
I have been working on generating my own Sphere meshes lately. The easiest method to achieve this, is by creating a cube first and then normalize the vertices so they are equidistant from a certain origin point. This works fine but there are a couple of problems with it.
Because I am generating the Cube first and the turning it into a Sphere, the Sphere is made of 6 sides (6 sides of the cube). This makes it difficult to distort the shape of the Sphere in any form as you have to make sure the edges of the faces stay lined up.
So I looked online for other ways of generating a Sphere mesh, and I came across the Fibonacci Sphere found in this article.
It is a pretty cool way but also very efficient as the vertices are spread fairly evenly over the surface of the sphere. The problem with this though is that I have no clue what the logic behind the triangles is going to be in this one. The cube one is fairly straightforward because I am essentially only making 6 planes which are simply a grid of vertices.
In this one, I can't really understand how everything is going to link up together. I can't really think of any algorithm that can create the outside triangles for any arbitrary number of vertices on the sphere and I couldn't find anything online about it.
For reference, here is how the vertices are spread out when I try to spread 500 points over the surface of the sphere: https://prnt.sc/ulzwdb
I used Gizmos to draw spheres at the points in which the vertices are located just to see if I could come up with a way to find the right vertex indexes for my triangles, but I couldn't.
Any help would be appreciated
Thank you in advance.
Here is the code that I am currently using for the vertices (and also a mess of triangles):
public void createCircle(){
Mesh mesh = new Mesh();
vertices = new Vector3[resolution];
triangles = new int[resolution*3];
for(int i=0; i<resolution; i++){
float theta = 2 * Mathf.PI * i / goldenRatio;
float phi = Mathf.Acos((float)(1 - 2*(i+0.5)/resolution));
Vector3 pointOnSphere = (new Vector3(Mathf.Cos(theta) * Mathf.Sin(phi),
Mathf.Sin(theta) * Mathf.Sin(phi), Mathf.Cos(phi)) * radius) + transform.position;
vertices[i] = pointOnSphere;
}
int vertexCount = 0;
int triangleCount = 0;
for(int i=0; i<resolution-2; i++){
triangles[triangleCount] = vertexCount;
triangles[triangleCount+1] = vertexCount+1;
triangles[triangleCount+2] = vertexCount+2;
vertexCount++;
triangleCount+=3;
}
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
if(sphereMeshFilter != null) sphereMeshFilter = new MeshFilter();
MeshRenderer meshRenderer = this.gameObject.GetComponent<MeshRenderer>();
MeshFilter meshFilter = this.gameObject.GetComponent<MeshFilter>();
if(meshFilter == null){
this.gameObject.AddComponent<MeshFilter>();
}
if(meshRenderer == null){
this.gameObject.AddComponent<MeshRenderer>().sharedMaterial = new Material(shader);
}
meshFilter.mesh = mesh;
}
Did you manage to do this. I'm at exactly the same point you are here but would love to see if you managed it.
Your answer
Follow this Question
Related Questions
How to place a mesh on a sphere at a certain point? 0 Answers
Seamless Terrain on Quad Sphere 0 Answers
Why is my mesh not showing outside of an area? 0 Answers
Texture not Drawn on all uv Coordinates 0 Answers
Code generated mesh not visible 1 Answer