Last active
March 29, 2024 16:12
-
-
Save hariedo/dc63cbd0e4e07a9f5f619ab4dc434af5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.