Skip to content

Instantly share code, notes, and snippets.

@lai3d
Forked from olegknyazev/GenerateCircleMesh.cs
Created January 21, 2019 04:24
Show Gist options
  • Save lai3d/50304eadf5f2fa857ed0366862a364e6 to your computer and use it in GitHub Desktop.
Save lai3d/50304eadf5f2fa857ed0366862a364e6 to your computer and use it in GitHub Desktop.
Generation of a circle mesh (Unity)
private const int CircleSegmentCount = 64;
private const int CircleVertexCount = CircleSegmentCount + 2;
private const int CircleIndexCount = CircleSegmentCount * 3;
private static Mesh GenerateCircleMesh()
{
var circle = new Mesh();
var vertices = new List<Vector3>(CircleVertexCount);
var indices = new int[CircleIndexCount];
var segmentWidth = Mathf.PI * 2f / CircleSegmentCount;
var angle = 0f;
vertices.Add(Vector3.zero);
for (int i = 1; i < CircleVertexCount; ++i)
{
vertices.Add(new Vector3(Mathf.Cos(angle), 0f, Mathf.Sin(angle)));
angle -= segmentWidth;
if (i > 1)
{
var j = (i - 2) * 3;
indices[j + 0] = 0;
indices[j + 1] = i - 1;
indices[j + 2] = i;
}
}
circle.SetVertices(vertices);
circle.SetIndices(indices, MeshTopology.Triangles, 0);
circle.RecalculateBounds();
return circle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment