mirror of
https://github.com/Xevion/Boids.git
synced 2025-12-09 10:06:34 -06:00
move arc drawing code into ShapeDraw, remove vertex count argument
This commit is contained in:
@@ -261,41 +261,4 @@ public class Boid : MonoBehaviour {
|
|||||||
// Add points to LineRenderer
|
// Add points to LineRenderer
|
||||||
line.SetPositions(points);
|
line.SetPositions(points);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawArcCentered(float radius, float centerAngle, float angleWidth, string childName) {
|
|
||||||
float half = angleWidth / 2f;
|
|
||||||
DrawArc(radius, Util.AddAngle(centerAngle, -half), Util.AddAngle(centerAngle, half), childName);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void DrawArc(float radius, float from, float to, string childName) {
|
|
||||||
// Create a new child GameObject to hold the LineRenderer Component
|
|
||||||
var child = new GameObject(childName);
|
|
||||||
child.transform.SetParent(transform);
|
|
||||||
child.transform.position = transform.position;
|
|
||||||
var line = child.AddComponent<LineRenderer>();
|
|
||||||
|
|
||||||
int vertexCount = _parent.arcVertexCount != -1 ? _parent.arcVertexCount : (int) Mathf.Abs(from - to) * 2;
|
|
||||||
|
|
||||||
// Setup LineRenderer properties
|
|
||||||
line.useWorldSpace = false;
|
|
||||||
line.startWidth = _parent.circleWidth;
|
|
||||||
line.endWidth = _parent.circleWidth;
|
|
||||||
line.positionCount = vertexCount + 1 + 2;
|
|
||||||
|
|
||||||
// Calculate points for circle
|
|
||||||
var pointCount = vertexCount + 1;
|
|
||||||
var points = new Vector3[pointCount + 2];
|
|
||||||
|
|
||||||
for (int i = 0; i < pointCount; i++) {
|
|
||||||
// Magic '180 - angle'
|
|
||||||
var rad = Mathf.Deg2Rad * Mathf.LerpAngle(from, to, i / (float) pointCount);
|
|
||||||
points[i + 1] = new Vector3(Mathf.Sin(rad), Mathf.Cos(rad), 0) * radius;
|
|
||||||
}
|
|
||||||
|
|
||||||
points[0] = new Vector3(0, 0, 0);
|
|
||||||
points[points.Length - 1] = points[0];
|
|
||||||
|
|
||||||
// Add points to LineRenderer
|
|
||||||
line.SetPositions(points);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,12 @@
|
|||||||
/// A simple static utility class that assists with drawing shapes using the <c>LineRenderer</c> class.
|
/// A simple static utility class that assists with drawing shapes using the <c>LineRenderer</c> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ShapeDraw {
|
public class ShapeDraw {
|
||||||
|
public static float CircleWidth = 1f;
|
||||||
|
public static float ArcWidth = 1f;
|
||||||
|
public static int CircleVertexCount = 360;
|
||||||
|
public static int ArcVertexCount = 77;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Draw a Arc aimed straight up with a certain angle width and radius.
|
/// Draw a Arc aimed straight up with a certain angle width and radius.
|
||||||
/// Use <see cref="RotateLineRenderer"/> to point the Arc at a certain direction.
|
/// Use <see cref="RotateLineRenderer"/> to point the Arc at a certain direction.
|
||||||
@@ -11,10 +17,29 @@ public class ShapeDraw {
|
|||||||
/// <param name="lineRenderer">The LineRenderer to draw the Arc upon.</param>
|
/// <param name="lineRenderer">The LineRenderer to draw the Arc upon.</param>
|
||||||
/// <param name="angle">Angle of the Arc</param>
|
/// <param name="angle">Angle of the Arc</param>
|
||||||
/// <param name="radius">Radius of the Arc</param>
|
/// <param name="radius">Radius of the Arc</param>
|
||||||
/// <param name="vertexCount">Number of vertexes to be used in the arc, clamp minimum 3 </param>
|
|
||||||
/// <seealso cref="RotateLineRenderer"/>
|
/// <seealso cref="RotateLineRenderer"/>
|
||||||
public static void DrawArc(LineRenderer lineRenderer, float angle, float radius, int vertexCount) {
|
public static void DrawArc(LineRenderer lineRenderer, float angle, float radius) {
|
||||||
|
// Setup LineRenderer properties
|
||||||
|
lineRenderer.useWorldSpace = false;
|
||||||
|
lineRenderer.startWidth = ArcWidth;
|
||||||
|
lineRenderer.endWidth = ArcWidth;
|
||||||
|
lineRenderer.positionCount = ArcVertexCount + 1 + 2;
|
||||||
|
|
||||||
|
// Calculate points for circle
|
||||||
|
var pointCount = ArcVertexCount + 1;
|
||||||
|
var points = new Vector3[pointCount + 2];
|
||||||
|
|
||||||
|
for (int i = 0; i < pointCount; i++) {
|
||||||
|
// Magic '180 - angle'
|
||||||
|
var rad = Mathf.Deg2Rad * Mathf.LerpAngle(0, angle, i / (float) pointCount);
|
||||||
|
points[i + 1] = new Vector3(Mathf.Sin(rad), Mathf.Cos(rad), 0) * radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
points[0] = new Vector3(0, 0, 0);
|
||||||
|
points[points.Length - 1] = points[0];
|
||||||
|
|
||||||
|
// Add points to LineRenderer
|
||||||
|
lineRenderer.SetPositions(points);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user