Skip to content
Snippets Groups Projects
Commit a839f200 authored by Tobias Schöner's avatar Tobias Schöner
Browse files

Improved CircleGenerator

CircleGenerator now generates triangles around midPoint instead of using CreatePlane of ShapeGenerator
parent 5fe9841e
Branches
No related tags found
1 merge request!1feat: added visualisation for CircleFact; added Torus- and ConeGenerator
......@@ -21,11 +21,34 @@ public class CircleGenerator : ShapeGenerator
#region Implementation
protected override void GenerateShape()
{
Vector3[] circle = GetCirclePoints(radius, sideCount, Vector3.zero);
var circle = CreateCircle(radius, sideCount);
if (circleMesh.sharedMesh != null)
circleMesh.sharedMesh.Clear();
circleMesh.mesh = CreateMesh(CreatePlane(circle));
circleMesh.mesh = CreateMesh(circle);
if (circleMesh.transform.TryGetComponent(out MeshCollider meshCol))
meshCol.sharedMesh = circleMesh.sharedMesh;
}
/// <summary>
/// Creates circle vertecies and triangles around the midPoint at (0,0,0)
/// </summary>
/// <param name="points"></param>
/// <param name="invert"></param>
/// <returns></returns>
static (Vector3[], int[]) CreateCircle(float radius, int sideCount, bool invert = false)
{
Vector3[] vertices = GetCirclePoints(radius, sideCount).Union(new Vector3[] { Vector3.zero }).ToArray();
int[] triangles = new int[(vertices.Length - 1) * 3];
int vertLen = vertices.Length;
for (int i = 0; i < vertLen-1; i++)
{
triangles[i * 3 + 0] = vertLen-1; // midPoint
triangles[i * 3 + 1] = i;
triangles[i * 3 + 2] = (i + 1) % (vertLen-1);
}
return (vertices, invert ? triangles.Reverse().ToArray() : triangles);
}
#endregion Implementation
}
......@@ -33,6 +33,12 @@ protected static Vector3[] GetCirclePoints(float circleRadius, int pointCount, V
return circle;
}
/// <summary>
/// Creates triangles for a set of vertecies of a flat, convex shape
/// </summary>
/// <param name="points"></param>
/// <param name="invert"></param>
/// <returns></returns>
protected static (Vector3[], int[]) CreatePlane(Vector3[] points, bool invert = false)
{
Vector3[] vertices = points;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment