-
-
Save lai3d/50304eadf5f2fa857ed0366862a364e6 to your computer and use it in GitHub Desktop.
Generation of a circle mesh (Unity)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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