I am currently working on a project where I need to procedurally generate a 2D planet. My solution is to start from a point on the circumference of the circle and rotate in regular intervals, for each interval I would assign a new radius from an array of 1-dimensional Perlin noise (only using the x-axis of Perlin noise). Then I would calculate each vertex’s coordinates using x = radius+height*noiseMap[i,1])*Mathf.Sin(angle);
and y =(radius+height*noiseMap[i,1])*Mathf.Cos(angle);
My problem is I am not sure how to generate a mesh from this data, most guides I find are based on generating a rectangular mesh. I know that I must provide the mesh filter with an array of vertices, triangles and a UV, but I do not know how to generate the array of triangles or the UV.
Hopefully, this picture demonstrates what I am trying to achieve. Any help would be much appreciated.
Hmm, I think i don’t understand your problem. You have already explained everything. The triangles array is an array of vertex indices. Always 3 consecutive elements in the triangles array create one triangle. So usually when you create your vertices array you first put the center point in the first element. Then all your circle bounds points afterwards in order.
To form the triangles you just have to pick two consecutive points on the bounds as well as the center point. So it usually looks like this:
0,1,2, 0,2,3, 0,3,4, .... 0, n-1, 1
To create this triangle list you just iterate through your circle points like that
int[] tris = new int[numPoints * 3];
for(int i = 0; i < numPoints; i++)
{
tris[i*3] = 0;
tris[i*3 + 1] = i+1;
tris[i*3 + 2] = (i+1)%numPoints + 1;
}
Note the (i+1)%numPoints
just ensures when i is the last point we wrap around to 0. The “+1” is the offset since the first vertex is the center point.
We don’t know how you generate your “noiseMap”, so we assume it contains values in your desired range. Keep in mind that most noise functions return a value either between 0 and 1 or -1 and 1. Of course you mose likely want some minimal radius. If your noise value is between 0 and 1 you can do:
float radius = minRadius + noiseMap[i,1] * scale;
Vector2 p = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle)) * radius;
So here radius will always have a size of minRadius (when noiseMap is 0) or minRadius + scale (if noiseMap is 1). The same thing can be used for a range between -1 and 1 but minRadius would turn into the average radius since the noise value can add and subtract “scale”.