CinemachinePathEditor.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using UnityEditorInternal;
  6. using Cinemachine.Utility;
  7. using System.Linq;
  8. namespace Cinemachine.Editor
  9. {
  10. [CustomEditor(typeof(CinemachinePath))]
  11. sealed class CinemachinePathEditor : BaseEditor<CinemachinePath>
  12. {
  13. public static string kPreferTangentSelectionKey = "CinemachinePathEditor.PreferTangentSelection";
  14. public static bool PreferTangentSelection
  15. {
  16. get { return EditorPrefs.GetBool(kPreferTangentSelectionKey, false); }
  17. set
  18. {
  19. if (value != PreferTangentSelection)
  20. EditorPrefs.SetBool(kPreferTangentSelectionKey, value);
  21. }
  22. }
  23. private ReorderableList mWaypointList;
  24. static bool mWaypointsExpanded;
  25. bool mPreferTangentSelection;
  26. /// <summary>Get the property names to exclude in the inspector.</summary>
  27. /// <param name="excluded">Add the names to this list</param>
  28. protected override void GetExcludedPropertiesInInspector(List<string> excluded)
  29. {
  30. base.GetExcludedPropertiesInInspector(excluded);
  31. excluded.Add(FieldPath(x => x.m_Waypoints));
  32. }
  33. void OnEnable()
  34. {
  35. mWaypointList = null;
  36. mPreferTangentSelection = PreferTangentSelection;
  37. }
  38. // ReSharper disable once UnusedMember.Global - magic method called when doing Frame Selected
  39. public bool HasFrameBounds()
  40. {
  41. return Target.m_Waypoints != null && Target.m_Waypoints.Length > 0;
  42. }
  43. // ReSharper disable once UnusedMember.Global - magic method called when doing Frame Selected
  44. public Bounds OnGetFrameBounds()
  45. {
  46. Vector3[] wp;
  47. int selected = mWaypointList == null ? -1 : mWaypointList.index;
  48. if (selected >= 0 && selected < Target.m_Waypoints.Length)
  49. wp = new Vector3[1] { Target.m_Waypoints[selected].position };
  50. else
  51. wp = Target.m_Waypoints.Select(p => p.position).ToArray();
  52. return GeometryUtility.CalculateBounds(wp, Target.transform.localToWorldMatrix);
  53. }
  54. public override void OnInspectorGUI()
  55. {
  56. BeginInspector();
  57. if (mWaypointList == null)
  58. SetupWaypointList();
  59. if (mWaypointList.index >= mWaypointList.count)
  60. mWaypointList.index = mWaypointList.count - 1;
  61. // Ordinary properties
  62. DrawRemainingPropertiesInInspector();
  63. // Path length
  64. EditorGUILayout.LabelField("Path Length", Target.PathLength.ToString());
  65. GUILayout.Label(new GUIContent("Selected Waypoint:"));
  66. EditorGUILayout.BeginVertical(GUI.skin.box);
  67. Rect rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight * 3 + 10);
  68. if (mWaypointList.index >= 0)
  69. {
  70. DrawWaypointEditor(rect, mWaypointList.index);
  71. serializedObject.ApplyModifiedProperties();
  72. }
  73. else
  74. {
  75. if (Target.m_Waypoints.Length > 0)
  76. {
  77. EditorGUI.HelpBox(rect,
  78. "Click on a waypoint in the scene view\nor in the Path Details list",
  79. MessageType.Info);
  80. }
  81. else if (GUI.Button(rect, new GUIContent("Add a waypoint to the path")))
  82. {
  83. InsertWaypointAtIndex(mWaypointList.index);
  84. mWaypointList.index = 0;
  85. }
  86. }
  87. EditorGUILayout.EndVertical();
  88. if (mPreferTangentSelection != EditorGUILayout.Toggle(
  89. new GUIContent("Prefer Tangent Drag",
  90. "When editing the path, if waypoint position and tangent coincide, dragging will apply preferentially to the tangent"),
  91. mPreferTangentSelection))
  92. {
  93. PreferTangentSelection = mPreferTangentSelection = !mPreferTangentSelection;
  94. }
  95. mWaypointsExpanded = EditorGUILayout.Foldout(mWaypointsExpanded, "Path Details", true);
  96. if (mWaypointsExpanded)
  97. {
  98. EditorGUI.BeginChangeCheck();
  99. mWaypointList.DoLayoutList();
  100. if (EditorGUI.EndChangeCheck())
  101. serializedObject.ApplyModifiedProperties();
  102. }
  103. }
  104. void SetupWaypointList()
  105. {
  106. mWaypointList = new ReorderableList(
  107. serializedObject, FindProperty(x => x.m_Waypoints),
  108. true, true, true, true);
  109. mWaypointList.elementHeight *= 3;
  110. mWaypointList.drawHeaderCallback = (Rect rect) =>
  111. {
  112. EditorGUI.LabelField(rect, "Waypoints");
  113. };
  114. mWaypointList.drawElementCallback
  115. = (Rect rect, int index, bool isActive, bool isFocused) =>
  116. {
  117. DrawWaypointEditor(rect, index);
  118. };
  119. mWaypointList.onAddCallback = (ReorderableList l) =>
  120. {
  121. InsertWaypointAtIndex(l.index);
  122. };
  123. }
  124. void DrawWaypointEditor(Rect rect, int index)
  125. {
  126. // Needed for accessing string names of fields
  127. CinemachinePath.Waypoint def = new CinemachinePath.Waypoint();
  128. Vector2 numberDimension = GUI.skin.button.CalcSize(new GUIContent("999"));
  129. Vector2 labelDimension = GUI.skin.label.CalcSize(new GUIContent("Position"));
  130. Vector2 addButtonDimension = new Vector2(labelDimension.y + 5, labelDimension.y + 1);
  131. float vSpace = 2;
  132. float hSpace = 3;
  133. SerializedProperty element = mWaypointList.serializedProperty.GetArrayElementAtIndex(index);
  134. rect.y += vSpace / 2;
  135. Rect r = new Rect(rect.position, numberDimension);
  136. Color color = GUI.color;
  137. // GUI.color = Target.m_Appearance.pathColor;
  138. if (GUI.Button(r, new GUIContent(index.ToString(), "Go to the waypoint in the scene view")))
  139. {
  140. if (SceneView.lastActiveSceneView != null)
  141. {
  142. mWaypointList.index = index;
  143. SceneView.lastActiveSceneView.pivot = Target.EvaluatePosition(index);
  144. SceneView.lastActiveSceneView.size = 3;
  145. SceneView.lastActiveSceneView.Repaint();
  146. }
  147. }
  148. GUI.color = color;
  149. r = new Rect(rect.position, labelDimension);
  150. r.x += hSpace + numberDimension.x;
  151. EditorGUI.LabelField(r, "Position");
  152. r.x += hSpace + r.width;
  153. r.width = rect.width - (numberDimension.x + hSpace + r.width + hSpace + addButtonDimension.x + hSpace);
  154. EditorGUI.PropertyField(r, element.FindPropertyRelative(() => def.position), GUIContent.none);
  155. r.x += r.width + hSpace;
  156. r.size = addButtonDimension;
  157. GUIContent buttonContent = EditorGUIUtility.IconContent("d_RectTransform Icon");
  158. buttonContent.tooltip = "Set to scene-view camera position";
  159. GUIStyle style = new GUIStyle(GUI.skin.label);
  160. style.alignment = TextAnchor.MiddleCenter;
  161. if (GUI.Button(r, buttonContent, style) && SceneView.lastActiveSceneView != null)
  162. {
  163. Undo.RecordObject(Target, "Set waypoint");
  164. CinemachinePath.Waypoint wp = Target.m_Waypoints[index];
  165. Vector3 pos = SceneView.lastActiveSceneView.camera.transform.position;
  166. wp.position = Target.transform.InverseTransformPoint(pos);
  167. Target.m_Waypoints[index] = wp;
  168. }
  169. r = new Rect(rect.position, labelDimension);
  170. r.y += numberDimension.y + vSpace;
  171. r.x += hSpace + numberDimension.x; r.width = labelDimension.x;
  172. EditorGUI.LabelField(r, "Tangent");
  173. r.x += hSpace + r.width;
  174. r.width = rect.width - (numberDimension.x + hSpace + r.width + hSpace + addButtonDimension.x + hSpace);
  175. EditorGUI.PropertyField(r, element.FindPropertyRelative(() => def.tangent), GUIContent.none);
  176. r.x += r.width + hSpace;
  177. r.size = addButtonDimension;
  178. buttonContent = EditorGUIUtility.IconContent("ol minus@2x");
  179. buttonContent.tooltip = "Remove this waypoint";
  180. if (GUI.Button(r, buttonContent, style))
  181. {
  182. Undo.RecordObject(Target, "Delete waypoint");
  183. var list = new List<CinemachinePath.Waypoint>(Target.m_Waypoints);
  184. list.RemoveAt(index);
  185. Target.m_Waypoints = list.ToArray();
  186. if (index == Target.m_Waypoints.Length)
  187. mWaypointList.index = index - 1;
  188. }
  189. r = new Rect(rect.position, labelDimension);
  190. r.y += 2 * (numberDimension.y + vSpace);
  191. r.x += hSpace + numberDimension.x; r.width = labelDimension.x;
  192. EditorGUI.LabelField(r, "Roll");
  193. r.x += hSpace + labelDimension.x;
  194. r.width = rect.width
  195. - (numberDimension.x + hSpace)
  196. - (labelDimension.x + hSpace)
  197. - (addButtonDimension.x + hSpace);
  198. r.width /= 3;
  199. EditorGUI.MultiPropertyField(r, new GUIContent[] { new GUIContent(" ") },
  200. element.FindPropertyRelative(() => def.roll));
  201. r.x = rect.x + rect.width - addButtonDimension.x;
  202. r.size = addButtonDimension;
  203. buttonContent = EditorGUIUtility.IconContent("ol plus@2x");
  204. buttonContent.tooltip = "Add a new waypoint after this one";
  205. if (GUI.Button(r, buttonContent, style))
  206. {
  207. mWaypointList.index = index;
  208. InsertWaypointAtIndex(index);
  209. }
  210. }
  211. void InsertWaypointAtIndex(int indexA)
  212. {
  213. Vector3 pos = Vector3.forward;
  214. Vector3 tangent = Vector3.right;
  215. float roll = 0;
  216. // Get new values from the current indexA (if any)
  217. int numWaypoints = Target.m_Waypoints.Length;
  218. if (indexA < 0)
  219. indexA = numWaypoints - 1;
  220. if (indexA >= 0)
  221. {
  222. int indexB = indexA + 1;
  223. if (Target.m_Looped && indexB >= numWaypoints)
  224. indexB = 0;
  225. if (indexB >= numWaypoints)
  226. {
  227. // Extrapolate the end
  228. if (!Target.m_Waypoints[indexA].tangent.AlmostZero())
  229. tangent = Target.m_Waypoints[indexA].tangent;
  230. pos = Target.m_Waypoints[indexA].position + tangent;
  231. roll = Target.m_Waypoints[indexA].roll;
  232. }
  233. else
  234. {
  235. // Interpolate
  236. pos = Target.transform.InverseTransformPoint(
  237. Target.EvaluatePosition(0.5f + indexA));
  238. tangent = Target.transform.InverseTransformDirection(
  239. Target.EvaluateTangent(0.5f + indexA).normalized);
  240. roll = Mathf.Lerp(
  241. Target.m_Waypoints[indexA].roll, Target.m_Waypoints[indexB].roll, 0.5f);
  242. }
  243. }
  244. Undo.RecordObject(Target, "Add waypoint");
  245. var wp = new CinemachinePath.Waypoint();
  246. wp.position = pos;
  247. wp.tangent = tangent;
  248. wp.roll = roll;
  249. var list = new List<CinemachinePath.Waypoint>(Target.m_Waypoints);
  250. list.Insert(indexA + 1, wp);
  251. Target.m_Waypoints = list.ToArray();
  252. Target.InvalidateDistanceCache();
  253. mWaypointList.index = indexA + 1; // select it
  254. }
  255. void OnSceneGUI()
  256. {
  257. if (mWaypointList == null)
  258. SetupWaypointList();
  259. if (Tools.current == Tool.Move)
  260. {
  261. Color colorOld = Handles.color;
  262. var localToWorld = Target.transform.localToWorldMatrix;
  263. var localRotation = Target.transform.rotation;
  264. for (int i = 0; i < Target.m_Waypoints.Length; ++i)
  265. {
  266. DrawSelectionHandle(i, localToWorld);
  267. if (mWaypointList.index == i)
  268. {
  269. // Waypoint is selected
  270. if (PreferTangentSelection)
  271. {
  272. DrawPositionControl(i, localToWorld, localRotation);
  273. DrawTangentControl(i, localToWorld, localRotation);
  274. }
  275. else
  276. {
  277. DrawTangentControl(i, localToWorld, localRotation);
  278. DrawPositionControl(i, localToWorld, localRotation);
  279. }
  280. }
  281. }
  282. Handles.color = colorOld;
  283. }
  284. }
  285. void DrawSelectionHandle(int i, Matrix4x4 localToWorld)
  286. {
  287. if (Event.current.button != 1)
  288. {
  289. Vector3 pos = localToWorld.MultiplyPoint(Target.m_Waypoints[i].position);
  290. float size = HandleUtility.GetHandleSize(pos) * 0.2f;
  291. Handles.color = Color.white;
  292. if (Handles.Button(pos, Quaternion.identity, size, size, Handles.SphereHandleCap)
  293. && mWaypointList.index != i)
  294. {
  295. mWaypointList.index = i;
  296. InspectorUtility.RepaintGameView();
  297. }
  298. // Label it
  299. Handles.BeginGUI();
  300. Vector2 labelSize = new Vector2(
  301. EditorGUIUtility.singleLineHeight * 2, EditorGUIUtility.singleLineHeight);
  302. Vector2 labelPos = HandleUtility.WorldToGUIPoint(pos);
  303. labelPos.y -= labelSize.y / 2;
  304. labelPos.x -= labelSize.x / 2;
  305. GUILayout.BeginArea(new Rect(labelPos, labelSize));
  306. GUIStyle style = new GUIStyle();
  307. style.normal.textColor = Color.black;
  308. style.alignment = TextAnchor.MiddleCenter;
  309. GUILayout.Label(new GUIContent(i.ToString(), "Waypoint " + i), style);
  310. GUILayout.EndArea();
  311. Handles.EndGUI();
  312. }
  313. }
  314. void DrawTangentControl(int i, Matrix4x4 localToWorld, Quaternion localRotation)
  315. {
  316. CinemachinePath.Waypoint wp = Target.m_Waypoints[i];
  317. Vector3 hPos = localToWorld.MultiplyPoint(wp.position + wp.tangent);
  318. Handles.color = Color.yellow;
  319. Handles.DrawLine(localToWorld.MultiplyPoint(wp.position), hPos);
  320. EditorGUI.BeginChangeCheck();
  321. Quaternion rotation = (Tools.pivotRotation == PivotRotation.Local)
  322. ? localRotation : Quaternion.identity;
  323. float size = HandleUtility.GetHandleSize(hPos) * 0.1f;
  324. Handles.SphereHandleCap(0, hPos, rotation, size, EventType.Repaint);
  325. Vector3 newPos = Handles.PositionHandle(hPos, rotation);
  326. if (EditorGUI.EndChangeCheck())
  327. {
  328. Undo.RecordObject(target, "Change Waypoint Tangent");
  329. newPos = Matrix4x4.Inverse(localToWorld).MultiplyPoint(newPos);
  330. wp.tangent = newPos - wp.position;
  331. Target.m_Waypoints[i] = wp;
  332. Target.InvalidateDistanceCache();
  333. InspectorUtility.RepaintGameView();
  334. }
  335. }
  336. void DrawPositionControl(int i, Matrix4x4 localToWorld, Quaternion localRotation)
  337. {
  338. CinemachinePath.Waypoint wp = Target.m_Waypoints[i];
  339. Vector3 pos = localToWorld.MultiplyPoint(wp.position);
  340. EditorGUI.BeginChangeCheck();
  341. Handles.color = Target.m_Appearance.pathColor;
  342. Quaternion rotation = (Tools.pivotRotation == PivotRotation.Local)
  343. ? localRotation : Quaternion.identity;
  344. float size = HandleUtility.GetHandleSize(pos) * 0.1f;
  345. Handles.SphereHandleCap(0, pos, rotation, size, EventType.Repaint);
  346. pos = Handles.PositionHandle(pos, rotation);
  347. if (EditorGUI.EndChangeCheck())
  348. {
  349. Undo.RecordObject(target, "Move Waypoint");
  350. wp.position = Matrix4x4.Inverse(localToWorld).MultiplyPoint(pos);;
  351. Target.m_Waypoints[i] = wp;
  352. Target.InvalidateDistanceCache();
  353. InspectorUtility.RepaintGameView();
  354. }
  355. }
  356. [DrawGizmo(GizmoType.Active | GizmoType.NotInSelectionHierarchy
  357. | GizmoType.InSelectionHierarchy | GizmoType.Pickable, typeof(CinemachinePath))]
  358. static void DrawGizmos(CinemachinePath path, GizmoType selectionType)
  359. {
  360. var isActive = Selection.activeGameObject == path.gameObject;
  361. DrawPathGizmo(
  362. path, isActive ? path.m_Appearance.pathColor : path.m_Appearance.inactivePathColor,
  363. isActive, LocalSpaceSamplePath);
  364. }
  365. // same as Quaternion.AngleAxis(roll, Vector3.forward), just simplified
  366. static Quaternion RollAroundForward(float angle)
  367. {
  368. var halfAngle = angle * 0.5F * Mathf.Deg2Rad;
  369. return new Quaternion(0, 0, Mathf.Sin(halfAngle), Mathf.Cos(halfAngle));
  370. }
  371. static Vector4[] s_BezierWeightsCache;
  372. // Optimizer for gizmo drawing
  373. static int LocalSpaceSamplePath(CinemachinePathBase pathBase, int samplesPerSegment, Vector3[] positions, Quaternion[] rotations)
  374. {
  375. var path = pathBase as CinemachinePath;
  376. int numSegments = path.m_Waypoints.Length - (path.Looped ? 0 : 1);
  377. if (numSegments == 0)
  378. return 0;
  379. // Compute the bezier weights only once - this is shared by all segments
  380. if (s_BezierWeightsCache == null || s_BezierWeightsCache.Length < samplesPerSegment)
  381. s_BezierWeightsCache = new Vector4[samplesPerSegment];
  382. for (int i = 0; i < samplesPerSegment; ++i)
  383. {
  384. float t = ((float)i) / samplesPerSegment;
  385. float d = 1.0f - t;
  386. s_BezierWeightsCache[i] = new Vector4(d * d * d, 3f * d * d * t, 3f * d * t * t, t * t * t);
  387. }
  388. // Process the positions
  389. int numSamples = 0;
  390. for (int wp = 0; wp < numSegments && numSamples < positions.Length; ++wp)
  391. {
  392. int nextWp = (wp + 1) % path.m_Waypoints.Length;
  393. for (int i = 0; i < samplesPerSegment; ++i)
  394. {
  395. if (numSamples >= positions.Length)
  396. break;
  397. positions[numSamples++]
  398. = (s_BezierWeightsCache[i].x * path.m_Waypoints[wp].position)
  399. + (s_BezierWeightsCache[i].y * (path.m_Waypoints[wp].position + path.m_Waypoints[wp].tangent))
  400. + (s_BezierWeightsCache[i].z * (path.m_Waypoints[nextWp].position - path.m_Waypoints[nextWp].tangent))
  401. + (s_BezierWeightsCache[i].w * path.m_Waypoints[nextWp].position);
  402. }
  403. }
  404. if (numSamples < positions.Length)
  405. positions[numSamples++] = path.Looped ? positions[0] : path.m_Waypoints[path.m_Waypoints.Length - 1].position;
  406. // Process rotations
  407. if (rotations != null && rotations.Length >= numSamples)
  408. {
  409. int numRotSamples = 0;
  410. for (int wp = 0; wp < numSegments && numRotSamples < numSamples; ++wp)
  411. {
  412. int nextWp = (wp + 1) % path.m_Waypoints.Length;
  413. SplineHelpers.BezierTangentWeights3(
  414. path.m_Waypoints[wp].position, path.m_Waypoints[wp].position + path.m_Waypoints[wp].tangent,
  415. path.m_Waypoints[nextWp].position - path.m_Waypoints[nextWp].tangent, path.m_Waypoints[nextWp].position,
  416. out var w0, out var w1, out var w2);
  417. for (int i = 0; i < samplesPerSegment; ++i)
  418. {
  419. if (numRotSamples >= numSamples)
  420. break;
  421. float t = ((float)i) / samplesPerSegment;
  422. var fwd = (w0 * (t * t)) + (w1 * t) + w2; // from SplineHelpers.BezierTangent3()
  423. if (fwd.sqrMagnitude < 0.001f)
  424. fwd = Vector3.forward;
  425. rotations[numRotSamples++] = Quaternion.LookRotation(fwd) * RollAroundForward(path.GetRoll(wp, nextWp, t + wp));
  426. }
  427. }
  428. if (numRotSamples < numSamples)
  429. rotations[numRotSamples++] = path.Looped ? rotations[0] : path.EvaluateLocalOrientation(path.MaxPos);
  430. }
  431. return numSamples;
  432. }
  433. //===========================================
  434. // Generic service for drawing path gizmo
  435. static Vector3[] s_PositionsCache;
  436. static Quaternion[] s_RotationsCache;
  437. #if UNITY_2023_1_OR_NEWER
  438. static Vector3[] s_RightRailCache;
  439. static Vector3[] s_SleepersCache;
  440. #endif
  441. static ElementType[] EnsureArraySize<ElementType>(ElementType[] array, int requiredSize)
  442. {
  443. return ((array == null) || (array.Length < requiredSize)) ? new ElementType[requiredSize] : array;
  444. }
  445. /// <summary>
  446. /// Sample a path with a fixed number of samples per segment, in path-local space.
  447. /// Used for efficient gizmo drawing.
  448. /// </summary>
  449. /// <param name="samplesPerSegment">How many samples to take in each segment</param>
  450. /// <param name="positions">Output buffer for position samples</param>
  451. /// <param name="rotations">Output buffer for rotation samples. May be null if rotations are not needed.</param>
  452. /// <returns>Number of samples actually taken</returns>
  453. public delegate int BatchSamplerDelegate(CinemachinePathBase path, int samplesPerSegment, Vector3[] positions, Quaternion[] rotations);
  454. public static void DrawPathGizmo(CinemachinePathBase path, Color pathColor, bool isActive, BatchSamplerDelegate batchSampler = null)
  455. {
  456. var oldColor = Gizmos.color;
  457. Gizmos.color = pathColor;
  458. var oldMatrix = Gizmos.matrix;
  459. Gizmos.matrix = path.transform.localToWorldMatrix;
  460. if (batchSampler == null)
  461. batchSampler = DefaultBatchSampler;
  462. int numSegments = Mathf.CeilToInt(path.MaxPos - path.MinPos) + 1;
  463. int numSamples = numSegments * path.m_Resolution + 1;
  464. s_PositionsCache = EnsureArraySize(s_PositionsCache, numSamples);
  465. if (!isActive || path.m_Appearance.width < 0.01f)
  466. {
  467. numSamples = batchSampler(path, path.m_Resolution, s_PositionsCache, null);
  468. #if UNITY_2023_1_OR_NEWER
  469. Gizmos.DrawLineStrip(new Span<Vector3>(s_PositionsCache, 0, numSamples), false);
  470. #else
  471. for (int i = 1; i < numSamples; ++i)
  472. Gizmos.DrawLine(s_PositionsCache[i], s_PositionsCache[i-1]);
  473. #endif
  474. }
  475. else
  476. {
  477. s_RotationsCache = EnsureArraySize(s_RotationsCache, numSamples);
  478. numSamples = batchSampler(path, path.m_Resolution, s_PositionsCache, s_RotationsCache);
  479. // Draw the path
  480. var halfWidth = path.m_Appearance.width * 0.5f;
  481. #if UNITY_2023_1_OR_NEWER
  482. s_RightRailCache = EnsureArraySize(s_RightRailCache, numSamples);
  483. s_SleepersCache = EnsureArraySize(s_SleepersCache, numSamples * 2);
  484. for (int i = 0; i < numSamples; ++i)
  485. {
  486. var p = s_PositionsCache[i];
  487. var q = s_RotationsCache[i];
  488. var s = q * Vector3.right * halfWidth;
  489. s_RightRailCache[i] = p + s;
  490. s_PositionsCache[i] = p - s;
  491. s *= 1.2f;
  492. s_SleepersCache[i << 1] = p + s;
  493. s_SleepersCache[(i << 1) + 1] = p - s;
  494. }
  495. Gizmos.DrawLineStrip(new Span<Vector3>(s_PositionsCache, 0, numSamples), false);
  496. Gizmos.DrawLineStrip(new Span<Vector3>(s_RightRailCache, 0, numSamples), false);
  497. Gizmos.DrawLineList(new Span<Vector3>(s_SleepersCache, 0, numSamples * 2));
  498. #else
  499. var lastW = (s_RotationsCache[0] * Vector3.right) * halfWidth;
  500. for (int i = 1; i < numSamples; ++i)
  501. {
  502. var lastPos = s_PositionsCache[i-1];
  503. var p = s_PositionsCache[i];
  504. var q = s_RotationsCache[i];
  505. var w = (q * Vector3.right) * halfWidth;
  506. var w2 = w * 1.2f;
  507. var p0 = p - w2;
  508. var p1 = p + w2;
  509. Gizmos.DrawLine(p0, p1);
  510. Gizmos.DrawLine(lastPos - lastW, p - w);
  511. Gizmos.DrawLine(lastPos + lastW, p + w);
  512. lastW = w;
  513. }
  514. #endif
  515. }
  516. Gizmos.matrix = oldMatrix;
  517. Gizmos.color = oldColor;
  518. }
  519. // Fallback, used if a custom batch sampler is not provided to DrawPathGizmo()
  520. static int DefaultBatchSampler(CinemachinePathBase path, int samplesPerSegment, Vector3[] positions, Quaternion[] rotations)
  521. {
  522. int numSamples = 0;
  523. float step = 1f / samplesPerSegment;
  524. float tEnd = path.MaxPos + step / 2;
  525. for (float t = path.MinPos + step; t <= tEnd; t += step)
  526. {
  527. if (numSamples >= positions.Length || (rotations != null && numSamples >= rotations.Length))
  528. break;
  529. positions[numSamples] = path.EvaluateLocalPosition(t);
  530. if (rotations != null)
  531. rotations[numSamples] = path.EvaluateLocalOrientation(t);
  532. ++numSamples;
  533. }
  534. return numSamples;
  535. }
  536. }
  537. }