BoneDrawingUtility.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEngine;
  2. namespace UnityEditor.U2D.Animation
  3. {
  4. internal static class BoneDrawingUtility
  5. {
  6. public static float GetBoneRadius(Vector3 position, float scale = 1.0f)
  7. {
  8. if (Camera.current != null)
  9. {
  10. return 0.15f * scale * HandleUtility.GetHandleSize(position);
  11. }
  12. return 10f * scale / Handles.matrix.GetColumn(0).magnitude;
  13. }
  14. public static void DrawBoneNode(Vector3 position, Vector3 forward, Color color, float scale = 1.0f)
  15. {
  16. Color c = Handles.color;
  17. Handles.color = color;
  18. Handles.DrawSolidDisc(position, -forward, GetBoneRadius(position, scale) * 0.3f);
  19. Handles.color = c;
  20. }
  21. public static void DrawBone(Vector3 position, Vector3 endPosition, Vector3 forward, Color color, float scale = 1.0f)
  22. {
  23. Color c = Handles.color;
  24. Handles.color = color;
  25. var right = Vector3.right;
  26. var v = endPosition - position;
  27. if (v.sqrMagnitude != 0)
  28. right = v.normalized;
  29. var up = Vector3.Cross(right, forward).normalized;
  30. var radius = GetBoneRadius(position, scale) * 0.5f;
  31. var numSamples = 12;
  32. if (v.sqrMagnitude <= radius * radius)
  33. DrawingUtility.DrawSolidArc(position, -forward, up, 360f, radius, numSamples * 2);
  34. else
  35. {
  36. DrawingUtility.DrawSolidArc(position, -forward, up, 180f, radius, numSamples);
  37. DrawingUtility.DrawLine(position, endPosition, forward, radius * 2f, 0f);
  38. }
  39. Handles.color = c;
  40. }
  41. public static void DrawBoneOutline(Vector3 position, Vector3 endPosition, Vector3 forward, Color color, float outlineScale = 1.35f, float scale = 1.0f)
  42. {
  43. outlineScale = Mathf.Max(1f, outlineScale);
  44. Color c = Handles.color;
  45. Handles.color = color;
  46. var right = Vector3.right;
  47. var v = endPosition - position;
  48. if (v.sqrMagnitude != 0)
  49. right = v.normalized;
  50. var up = Vector3.Cross(right, forward).normalized;
  51. var radius = GetBoneRadius(position, scale) * 0.5f;
  52. var outlineWidth = radius * (outlineScale - 1f);
  53. var numSamples = 12;
  54. if (v.sqrMagnitude <= radius * radius)
  55. DrawingUtility.DrawSolidArc(position, -forward, up, 360f, radius, outlineScale, numSamples * 2);
  56. else
  57. {
  58. DrawingUtility.DrawSolidArc(position, -forward, up, 180f, radius, outlineScale, numSamples);
  59. DrawingUtility.DrawSolidArc(endPosition, -forward, -up, 180f, outlineWidth, 0f, numSamples);
  60. DrawingUtility.DrawLine(position + up * (radius + outlineWidth * 0.5f), endPosition + up * outlineWidth * 0.5f, forward, outlineWidth, outlineWidth);
  61. DrawingUtility.DrawLine(position - up * (radius + outlineWidth * 0.5f), endPosition - up * outlineWidth * 0.5f, forward, outlineWidth, outlineWidth);
  62. }
  63. Handles.color = c;
  64. }
  65. }
  66. }