123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- // Traffic Simulation
- // https://github.com/mchrbn/unity-traffic-simulation
- using System;
- using System.Linq;
- using UnityEditor;
- using UnityEngine;
- namespace TrafficSimulation {
- public static class TrafficSystemGizmos {
- //Custom Gizmo function
- [DrawGizmo(GizmoType.Selected | GizmoType.NonSelected | GizmoType.Active)]
- private static void DrawGizmo(TrafficSystem script, GizmoType gizmoType) {
- //Don't go further if we hide gizmos
- if (script.hideGuizmos) {
- return;
- }
- foreach (Segment segment in script.segments) {
- //Draw segment names
- GUIStyle style = new GUIStyle {normal = {textColor = new Color(1, 0, 0)}, fontSize = 15};
- Handles.Label(segment.transform.position, segment.name, style);
- //Draw waypoint
- for (int j = 0; j < segment.waypoints.Count; j++) {
- //Get current waypoint position
- Vector3 p = segment.waypoints[j].GetVisualPos();
- //Draw sphere, increase color to show the direction
- Gizmos.color = new Color(0f, 0f, 1f, (j + 1) / (float) segment.waypoints.Count);
- Gizmos.DrawSphere(p, script.waypointSize);
-
- //Get next waypoint position
- Vector3 pNext = Vector3.zero;
- if (j < segment.waypoints.Count - 1 && segment.waypoints[j + 1] != null) {
- pNext = segment.waypoints[j + 1].GetVisualPos();
- }
- if (pNext != Vector3.zero) {
- if (segment == script.curSegment) {
- Gizmos.color = new Color(1f, .3f, .1f);
- } else {
- Gizmos.color = new Color(1f, 0f, 0f);
- }
- //Draw connection line of the two waypoints
- Gizmos.DrawLine(p, pNext);
- //Set arrow count based on arrowDrawType
- int arrows = GetArrowCount(p, pNext, script);
- //Draw arrows
- for (int i = 1; i < arrows + 1; i++) {
- Vector3 point = Vector3.Lerp(p, pNext, (float) i / (arrows + 1));
- DrawArrow(point, p - pNext, script.arrowSizeWaypoint);
- }
- }
- }
- //Draw line linking segments
- foreach (Segment nextSegment in segment.nextSegments) {
- if (nextSegment != null){
- Vector3 p1 = segment.waypoints.Last().GetVisualPos();
- Vector3 p2 = nextSegment.waypoints.First().GetVisualPos();
- Gizmos.color = new Color(1f, 1f, 0f);
- Gizmos.DrawLine(p1, p2);
- if (script.arrowDrawType != ArrowDraw.Off) {
- DrawArrow((p1 + p2) / 2f, p1 - p2, script.arrowSizeIntersection);
- }
- }
- }
- }
- }
- private static void DrawArrow(Vector3 point, Vector3 forward, float size) {
- forward = forward.normalized * size;
- Vector3 left = Quaternion.Euler(0, 45, 0) * forward;
- Vector3 right = Quaternion.Euler(0, -45, 0) * forward;
- Gizmos.DrawLine(point, point + left);
- Gizmos.DrawLine(point, point + right);
- }
- private static int GetArrowCount(Vector3 pointA, Vector3 pointB, TrafficSystem script) {
- switch (script.arrowDrawType) {
- case ArrowDraw.FixedCount:
- return script.arrowCount;
- case ArrowDraw.ByLength:
- //Minimum of one arrow
- return Mathf.Max(1, (int) (Vector3.Distance(pointA, pointB) / script.arrowDistance));
- case ArrowDraw.Off:
- return 0;
- default:
- throw new ArgumentOutOfRangeException();
- }
- }
- }
- }
|