TrafficSystemGizmos.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Traffic Simulation
  2. // https://github.com/mchrbn/unity-traffic-simulation
  3. using System;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace TrafficSimulation {
  8. public static class TrafficSystemGizmos {
  9. //Custom Gizmo function
  10. [DrawGizmo(GizmoType.Selected | GizmoType.NonSelected | GizmoType.Active)]
  11. private static void DrawGizmo(TrafficSystem script, GizmoType gizmoType) {
  12. //Don't go further if we hide gizmos
  13. if (script.hideGuizmos) {
  14. return;
  15. }
  16. foreach (Segment segment in script.segments) {
  17. //Draw segment names
  18. GUIStyle style = new GUIStyle {normal = {textColor = new Color(1, 0, 0)}, fontSize = 15};
  19. Handles.Label(segment.transform.position, segment.name, style);
  20. //Draw waypoint
  21. for (int j = 0; j < segment.waypoints.Count; j++) {
  22. //Get current waypoint position
  23. Vector3 p = segment.waypoints[j].GetVisualPos();
  24. //Draw sphere, increase color to show the direction
  25. Gizmos.color = new Color(0f, 0f, 1f, (j + 1) / (float) segment.waypoints.Count);
  26. Gizmos.DrawSphere(p, script.waypointSize);
  27. //Get next waypoint position
  28. Vector3 pNext = Vector3.zero;
  29. if (j < segment.waypoints.Count - 1 && segment.waypoints[j + 1] != null) {
  30. pNext = segment.waypoints[j + 1].GetVisualPos();
  31. }
  32. if (pNext != Vector3.zero) {
  33. if (segment == script.curSegment) {
  34. Gizmos.color = new Color(1f, .3f, .1f);
  35. } else {
  36. Gizmos.color = new Color(1f, 0f, 0f);
  37. }
  38. //Draw connection line of the two waypoints
  39. Gizmos.DrawLine(p, pNext);
  40. //Set arrow count based on arrowDrawType
  41. int arrows = GetArrowCount(p, pNext, script);
  42. //Draw arrows
  43. for (int i = 1; i < arrows + 1; i++) {
  44. Vector3 point = Vector3.Lerp(p, pNext, (float) i / (arrows + 1));
  45. DrawArrow(point, p - pNext, script.arrowSizeWaypoint);
  46. }
  47. }
  48. }
  49. //Draw line linking segments
  50. foreach (Segment nextSegment in segment.nextSegments) {
  51. if (nextSegment != null){
  52. Vector3 p1 = segment.waypoints.Last().GetVisualPos();
  53. Vector3 p2 = nextSegment.waypoints.First().GetVisualPos();
  54. Gizmos.color = new Color(1f, 1f, 0f);
  55. Gizmos.DrawLine(p1, p2);
  56. if (script.arrowDrawType != ArrowDraw.Off) {
  57. DrawArrow((p1 + p2) / 2f, p1 - p2, script.arrowSizeIntersection);
  58. }
  59. }
  60. }
  61. }
  62. }
  63. private static void DrawArrow(Vector3 point, Vector3 forward, float size) {
  64. forward = forward.normalized * size;
  65. Vector3 left = Quaternion.Euler(0, 45, 0) * forward;
  66. Vector3 right = Quaternion.Euler(0, -45, 0) * forward;
  67. Gizmos.DrawLine(point, point + left);
  68. Gizmos.DrawLine(point, point + right);
  69. }
  70. private static int GetArrowCount(Vector3 pointA, Vector3 pointB, TrafficSystem script) {
  71. switch (script.arrowDrawType) {
  72. case ArrowDraw.FixedCount:
  73. return script.arrowCount;
  74. case ArrowDraw.ByLength:
  75. //Minimum of one arrow
  76. return Mathf.Max(1, (int) (Vector3.Distance(pointA, pointB) / script.arrowDistance));
  77. case ArrowDraw.Off:
  78. return 0;
  79. default:
  80. throw new ArgumentOutOfRangeException();
  81. }
  82. }
  83. }
  84. }