Skip to content

Instantly share code, notes, and snippets.

@hariedo
Last active March 29, 2024 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hariedo/dc63cbd0e4e07a9f5f619ab4dc434af5 to your computer and use it in GitHub Desktop.
Save hariedo/dc63cbd0e4e07a9f5f619ab4dc434af5 to your computer and use it in GitHub Desktop.
public static void DrawPolygon(Vector3 center, Vector3 axis, float radius,
int sides, Color color=default, float duration=0f, bool depthTest=true)
{
//duration = Mathf.Max(0.001f, duration);
Vector3 start = Limb(axis);
Vector3 prior = start * radius;
if (sides < 3) sides = 3;
float step = 360f/sides;
for (float f = step; f <= 360f; f += step)
{
Vector3 next = Quaternion.AngleAxis(step, axis) * prior;
Debug.DrawLine(center+prior, center+next, color, duration, depthTest);
prior = next;
}
}
public static void DrawWireDisc(Vector3 center, Vector3 axis, float radius,
Color color=default, float duration=0f, bool depthTest=true)
{
DrawPolygon(center, axis, radius, 16, color, duration, depthTest);
}
public static void DrawWireSphere(Vector3 center, float radius,
Color color=default, float duration=0f, bool depthTest=true)
{
DrawWireDisc(center, Vector3.right, radius, color, duration, depthTest);
DrawWireDisc(center, Vector3.up, radius, color, duration, depthTest);
DrawWireDisc(center, Vector3.forward, radius, color, duration, depthTest);
// Draw a tangent circle facing viewer.
Camera camera = Camera.current;
if (camera != null && !camera.orthographic)
{
#if UNITY_EDITOR
Vector3 pivot = camera.transform.position;
Vector3 axis = (pivot - center);
float d = axis.magnitude;
if (d != 0f)
{
float length = Mathf.Sqrt(d*d - radius*radius);
float theta = Mathf.Asin(radius / d);
float height = length * Mathf.Cos(theta);
float r = length * Mathf.Sin(theta);
Vector3 c = axis.normalized * height;
DrawWireDisc(pivot - c, axis, r, color, duration, depthTest);
}
#endif
}
}
@hariedo
Copy link
Author

hariedo commented Mar 29, 2024

Sometimes you don't want to rewrite your debugging code to put it into the OnDrawGizmos methods, and wish that Debug.DrawLine had more powerful friends that Gizmos and Handles do.

Also, the SphereCollider's gizmos draw with a really nice tangent circle to tie all the three axis planes' circles together and make it feel more like a sphere. The trigonometry to do this is not immediately obvious, so I include it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment