CoreLightEditorUtilities.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. using System;
  2. using UnityEditor.IMGUI.Controls;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. namespace UnityEditor.Rendering
  6. {
  7. /// <summary>Utility class for drawing light Editor gizmos</summary>
  8. public static class CoreLightEditorUtilities
  9. {
  10. [Flags]
  11. enum HandleDirections
  12. {
  13. Left = 1 << 0,
  14. Up = 1 << 1,
  15. Right = 1 << 2,
  16. Down = 1 << 3,
  17. All = Left | Up | Right | Down
  18. }
  19. static readonly Vector3[] directionalLightHandlesRayPositions =
  20. {
  21. new Vector3(1, 0, 0),
  22. new Vector3(-1, 0, 0),
  23. new Vector3(0, 1, 0),
  24. new Vector3(0, -1, 0),
  25. new Vector3(1, 1, 0).normalized,
  26. new Vector3(1, -1, 0).normalized,
  27. new Vector3(-1, 1, 0).normalized,
  28. new Vector3(-1, -1, 0).normalized
  29. };
  30. /// <summary>
  31. /// Draw a gizmo for a directional light.
  32. /// </summary>
  33. /// <param name="light">The light that is used for this gizmo.</param>
  34. public static void DrawDirectionalLightGizmo(Light light)
  35. {
  36. // Sets the color of the Gizmo.
  37. Color outerColor = GetLightAboveObjectWireframeColor(light.color);
  38. Vector3 lightPos = light.transform.position;
  39. float lightSize;
  40. using (new Handles.DrawingScope(Matrix4x4.identity)) //be sure no matrix affect the size computation
  41. {
  42. lightSize = HandleUtility.GetHandleSize(lightPos);
  43. }
  44. float radius = lightSize * 0.2f;
  45. using (new Handles.DrawingScope(outerColor))
  46. {
  47. Handles.DrawWireDisc(Vector3.zero, Vector3.forward, radius);
  48. foreach (Vector3 normalizedPos in directionalLightHandlesRayPositions)
  49. {
  50. Vector3 pos = normalizedPos * radius;
  51. Handles.DrawLine(pos, pos + new Vector3(0, 0, lightSize));
  52. }
  53. }
  54. }
  55. /// <summary>
  56. /// Draw a gizmo for a point light.
  57. /// </summary>
  58. /// <param name="light">The light that is used for this gizmo.</param>
  59. public static void DrawPointLightGizmo(Light light)
  60. {
  61. // Sets the color of the Gizmo.
  62. Color outerColor = GetLightAboveObjectWireframeColor(light.color);
  63. // Drawing the point light
  64. DrawPointLight(light, outerColor);
  65. // Draw the handles and labels
  66. DrawPointHandlesAndLabels(light);
  67. }
  68. static void DrawPointLight(Light light, Color outerColor)
  69. {
  70. float range = light.range;
  71. using (new Handles.DrawingScope(outerColor))
  72. {
  73. EditorGUI.BeginChangeCheck();
  74. //range = Handles.RadiusHandle(Quaternion.identity, light.transform.position, range, false, true);
  75. range = DoPointHandles(range);
  76. if (EditorGUI.EndChangeCheck())
  77. {
  78. Undo.RecordObject(light, "Adjust Point Light");
  79. light.range = range;
  80. }
  81. }
  82. }
  83. static void DrawPointHandlesAndLabels(Light light)
  84. {
  85. // Getting the first control on point handle
  86. var firstControl = GUIUtility.GetControlID(s_PointLightHandle.GetHashCode(), FocusType.Passive) -6; // BoxBoundsHandle allocates 6 control IDs
  87. if (Event.current.type != EventType.Repaint)
  88. return;
  89. // var firstControl = GUIUtility.GetControlID(k_RadiusHandleHash, FocusType.Passive) - 6;
  90. // if (Event.current.type != EventType.Repaint)
  91. // return;
  92. // Adding label /////////////////////////////////////
  93. Vector3 labelPosition = Vector3.zero;
  94. if (GUIUtility.hotControl != 0)
  95. {
  96. switch (GUIUtility.hotControl - firstControl)
  97. {
  98. case 0:
  99. labelPosition = Vector3.right * light.range;
  100. break;
  101. case 1:
  102. labelPosition = Vector3.left * light.range;
  103. break;
  104. case 2:
  105. labelPosition = Vector3.up * light.range;
  106. break;
  107. case 3:
  108. labelPosition = Vector3.down * light.range;
  109. break;
  110. case 4:
  111. labelPosition = Vector3.forward * light.range;
  112. break;
  113. case 5:
  114. labelPosition = Vector3.back * light.range;
  115. break;
  116. default:
  117. return;
  118. }
  119. string labelText = FormattableString.Invariant($"Range: {light.range:0.00}");
  120. DrawHandleLabel(labelPosition, labelText);
  121. }
  122. }
  123. /// <summary>
  124. /// Draw a gizmo for an area/rectangle light.
  125. /// </summary>
  126. /// <param name="light">The light that is used for this gizmo.</param>
  127. public static void DrawRectangleLightGizmo(Light light)
  128. {
  129. // Color to use for gizmo drawing
  130. Color outerColor = GetLightAboveObjectWireframeColor(light.color);
  131. // Drawing the gizmo
  132. DrawRectangleLight(light, outerColor);
  133. // Draw the handles and labels
  134. DrawRectangleHandlesAndLabels(light);
  135. }
  136. static void DrawRectangleLight(Light light, Color outerColor)
  137. {
  138. Vector2 size = light.areaSize;
  139. var range = light.range;
  140. var innerColor = GetLightBehindObjectWireframeColor(light.color);
  141. DrawZTestedLine(range, outerColor, innerColor);
  142. using (new Handles.DrawingScope(outerColor))
  143. {
  144. EditorGUI.BeginChangeCheck();
  145. size = DoRectHandles(size);
  146. if (EditorGUI.EndChangeCheck())
  147. {
  148. Undo.RecordObject(light, "Adjust Area Rectangle Light");
  149. light.areaSize = size;
  150. }
  151. }
  152. }
  153. static void DrawRectangleHandlesAndLabels(Light light)
  154. {
  155. // Getting the first control on radius handle
  156. var firstControl = GUIUtility.GetControlID(s_AreaLightHandle.GetHashCode(), FocusType.Passive) -6; // BoxBoundsHandle allocates 6 control IDs
  157. if (Event.current.type != EventType.Repaint)
  158. return;
  159. // Adding label /////////////////////////////////////
  160. Vector3 labelPosition = Vector3.zero;
  161. if (GUIUtility.hotControl != 0)
  162. {
  163. switch (GUIUtility.hotControl - firstControl)
  164. {
  165. case 0: // PositiveX
  166. labelPosition = Vector3.right * (light.areaSize.x / 2);
  167. break;
  168. case 1: // NegativeX
  169. labelPosition = Vector3.left * (light.areaSize.x / 2);
  170. break;
  171. case 2: // PositiveY
  172. labelPosition = Vector3.up * (light.areaSize.y / 2);
  173. break;
  174. case 3: // NegativeY
  175. labelPosition = Vector3.down * (light.areaSize.y / 2);
  176. break;
  177. default:
  178. return;
  179. }
  180. string labelText = FormattableString.Invariant($"w:{light.areaSize.x:0.00} x h:{light.areaSize.y:0.00}");
  181. DrawHandleLabel(labelPosition, labelText);
  182. }
  183. }
  184. /// <summary>
  185. /// Draw a gizmo for a disc light.
  186. /// </summary>
  187. /// <param name="light">The light that is used for this gizmo.</param>
  188. public static void DrawDiscLightGizmo(Light light)
  189. {
  190. // Color to use for gizmo drawing.
  191. Color outerColor = GetLightAboveObjectWireframeColor(light.color);
  192. // Drawing before objects
  193. DrawDiscLight(light, outerColor);
  194. // Draw handles
  195. DrawDiscHandlesAndLabels(light);
  196. }
  197. static void DrawDiscLight(Light light, Color outerColor)
  198. {
  199. float radius = light.areaSize.x;
  200. var range = light.range;
  201. var innerColor = GetLightBehindObjectWireframeColor(light.color);
  202. DrawZTestedLine(range, outerColor, innerColor);
  203. using (new Handles.DrawingScope(outerColor))
  204. {
  205. EditorGUI.BeginChangeCheck();
  206. radius = DoDiscHandles(radius);
  207. if (EditorGUI.EndChangeCheck())
  208. {
  209. Undo.RecordObject(light, "Adjust Area Disc Light");
  210. light.areaSize = new Vector2(radius, light.areaSize.y);
  211. }
  212. }
  213. }
  214. static void DrawDiscHandlesAndLabels(Light light)
  215. {
  216. // Getting the first control on radius handle
  217. var firstControl = GUIUtility.GetControlID(s_DiscLightHandle.GetHashCode(), FocusType.Passive) -6; // BoxBoundsHandle allocates 6 control IDs
  218. if (Event.current.type != EventType.Repaint)
  219. return;
  220. Vector3 labelPosition = Vector3.zero;
  221. if (GUIUtility.hotControl != 0)
  222. {
  223. switch (GUIUtility.hotControl - firstControl)
  224. {
  225. case 0: // PositiveX
  226. labelPosition = Vector3.right * light.areaSize.x;
  227. break;
  228. case 1: // NegativeX
  229. labelPosition = Vector3.left * light.areaSize.x;
  230. break;
  231. case 2: // PositiveY
  232. labelPosition = Vector3.up * light.areaSize.x;
  233. break;
  234. case 3: // NegativeY
  235. labelPosition = Vector3.down * light.areaSize.x;
  236. break;
  237. default:
  238. return;
  239. }
  240. string labelText = FormattableString.Invariant($"Radius: {light.areaSize.x:0.00}");
  241. DrawHandleLabel(labelPosition, labelText);
  242. }
  243. }
  244. static void DrawWithZTest(PrimitiveBoundsHandle primitiveHandle, float alpha = 0.2f)
  245. {
  246. primitiveHandle.center = Vector3.zero;
  247. primitiveHandle.handleColor = Color.clear;
  248. primitiveHandle.wireframeColor = Color.white;
  249. Handles.zTest = CompareFunction.LessEqual;
  250. primitiveHandle.DrawHandle();
  251. primitiveHandle.wireframeColor = new Color(1f, 1f, 1f, alpha);
  252. Handles.zTest = CompareFunction.Greater;
  253. primitiveHandle.DrawHandle();
  254. primitiveHandle.handleColor = Color.white;
  255. primitiveHandle.wireframeColor = Color.clear;
  256. Handles.zTest = CompareFunction.Always;
  257. primitiveHandle.DrawHandle();
  258. }
  259. static void DrawZTestedLine(float range, Color outerColor, Color innerColor)
  260. {
  261. using (new Handles.DrawingScope(outerColor))
  262. {
  263. Handles.zTest = CompareFunction.LessEqual;
  264. Handles.DrawLine(Vector3.zero, Vector3.forward * range);
  265. }
  266. using (new Handles.DrawingScope(innerColor))
  267. {
  268. Handles.zTest = CompareFunction.Greater;
  269. Handles.DrawLine(Vector3.zero, Vector3.forward * range);
  270. }
  271. Handles.zTest = CompareFunction.Always;
  272. }
  273. static void DrawHandleLabel(Vector3 handlePosition, string labelText, float offsetFromHandle = 0.3f)
  274. {
  275. Vector3 labelPosition = Vector3.zero;
  276. var style = new GUIStyle{normal = {background = Texture2D.whiteTexture}};
  277. GUI.color = new Color(0.82f, 0.82f, 0.82f, 1);
  278. labelPosition = handlePosition + Handles.inverseMatrix.MultiplyVector(Vector3.up) * HandleUtility.GetHandleSize(handlePosition) * offsetFromHandle;
  279. Handles.Label(labelPosition, labelText, style);
  280. }
  281. static readonly BoxBoundsHandle s_AreaLightHandle =
  282. new BoxBoundsHandle { axes = PrimitiveBoundsHandle.Axes.X | PrimitiveBoundsHandle.Axes.Y };
  283. static Vector2 DoRectHandles(Vector2 size)
  284. {
  285. s_AreaLightHandle.center = Vector3.zero;
  286. s_AreaLightHandle.size = size;
  287. DrawWithZTest(s_AreaLightHandle);
  288. return s_AreaLightHandle.size;
  289. }
  290. static readonly SphereBoundsHandle s_DiscLightHandle =
  291. new SphereBoundsHandle { axes = PrimitiveBoundsHandle.Axes.X | PrimitiveBoundsHandle.Axes.Y } ;
  292. static float DoDiscHandles(float radius)
  293. {
  294. s_DiscLightHandle.center = Vector3.zero;
  295. s_DiscLightHandle.radius = radius;
  296. DrawWithZTest(s_DiscLightHandle);
  297. return s_DiscLightHandle.radius;
  298. }
  299. static readonly SphereBoundsHandle s_PointLightHandle =
  300. new SphereBoundsHandle { axes = PrimitiveBoundsHandle.Axes.All };
  301. static float DoPointHandles(float range)
  302. {
  303. s_PointLightHandle.radius = range;
  304. DrawWithZTest(s_PointLightHandle);
  305. return s_PointLightHandle.radius;
  306. }
  307. static bool drawInnerConeAngle = true;
  308. /// <summary>
  309. /// Draw a gizmo for a spot light.
  310. /// </summary>
  311. /// <param name="light">The light that is used for this gizmo.</param>
  312. public static void DrawSpotLightGizmo(Light light)
  313. {
  314. // Saving the default colors
  315. var defColor = Handles.color;
  316. var defZTest = Handles.zTest;
  317. // Default Color for outer cone will be Yellow if nothing has been provided.
  318. Color outerColor = GetLightAboveObjectWireframeColor(light.color);
  319. // The default z-test outer color will be 20% opacity of the outer color
  320. Color outerColorZTest = GetLightBehindObjectWireframeColor(outerColor);
  321. // Default Color for inner cone will be Yellow-ish if nothing has been provided.
  322. Color innerColor = GetLightInnerConeColor(light.color);
  323. // The default z-test outer color will be 20% opacity of the inner color
  324. Color innerColorZTest = GetLightBehindObjectWireframeColor(innerColor);
  325. // Drawing before objects
  326. Handles.zTest = CompareFunction.LessEqual;
  327. DrawSpotlightWireframe(light, outerColor, innerColor);
  328. // Drawing behind objects
  329. Handles.zTest = CompareFunction.Greater;
  330. DrawSpotlightWireframe(light, outerColorZTest, innerColorZTest);
  331. // Resets the compare function to always
  332. Handles.zTest = CompareFunction.Always;
  333. // Draw handles
  334. if (!Event.current.alt)
  335. {
  336. DrawHandlesAndLabels(light, outerColor);
  337. }
  338. // Resets the handle colors
  339. Handles.color = defColor;
  340. Handles.zTest = defZTest;
  341. }
  342. static void DrawHandlesAndLabels(Light light, Color color)
  343. {
  344. // Zero position vector3
  345. Vector3 zeroPos = Vector3.zero;
  346. // Variable for which direction to draw the handles
  347. HandleDirections DrawHandleDirections;
  348. // Draw the handles ///////////////////////////////
  349. Handles.color = color;
  350. // Draw Center Handle
  351. float range = light.range;
  352. var id = GUIUtility.GetControlID(FocusType.Passive);
  353. EditorGUI.BeginChangeCheck();
  354. range = SliderLineHandle(id, Vector3.zero, Vector3.forward, range, "Range: ");
  355. if (EditorGUI.EndChangeCheck())
  356. {
  357. Undo.RecordObjects(new[] { light }, "Undo range change.");
  358. }
  359. // Draw outer handles
  360. DrawHandleDirections = HandleDirections.Down | HandleDirections.Up;
  361. const string outerLabel = "Outer Angle: ";
  362. EditorGUI.BeginChangeCheck();
  363. float outerAngle = DrawConeHandles(zeroPos, light.spotAngle, range, DrawHandleDirections, outerLabel);
  364. if (EditorGUI.EndChangeCheck())
  365. {
  366. Undo.RecordObjects(new[] { light }, "Undo outer angle change.");
  367. }
  368. // Draw inner handles
  369. float innerAngle = 0;
  370. const string innerLabel = "Inner Angle: ";
  371. if (light.innerSpotAngle > 0f && drawInnerConeAngle)
  372. {
  373. DrawHandleDirections = HandleDirections.Left | HandleDirections.Right;
  374. EditorGUI.BeginChangeCheck();
  375. innerAngle = DrawConeHandles(zeroPos, light.innerSpotAngle, range, DrawHandleDirections, innerLabel);
  376. if (EditorGUI.EndChangeCheck())
  377. {
  378. Undo.RecordObjects(new[] { light }, "Undo inner angle change.");
  379. }
  380. }
  381. // Draw Near Plane Handle
  382. float nearPlaneRange = light.shadowNearPlane;
  383. if(light.shadows != LightShadows.None && light.lightmapBakeType != LightmapBakeType.Baked)
  384. {
  385. EditorGUI.BeginChangeCheck();
  386. nearPlaneRange = SliderLineHandle(GUIUtility.GetControlID(FocusType.Passive), Vector3.zero, Vector3.forward, nearPlaneRange, "Near Plane: ");
  387. if (EditorGUI.EndChangeCheck())
  388. {
  389. Undo.RecordObjects(new[] { light }, "Undo shadow near plane change.");
  390. nearPlaneRange = Mathf.Clamp(nearPlaneRange, 0.1f, light.range);
  391. }
  392. }
  393. // If changes has been made we update the corresponding property
  394. if (GUI.changed)
  395. {
  396. light.spotAngle = outerAngle;
  397. light.innerSpotAngle = innerAngle;
  398. light.range = Math.Max(range, 0.01f);
  399. light.shadowNearPlane = Mathf.Clamp(nearPlaneRange, 0.1f, light.range);
  400. }
  401. }
  402. static Color GetLightInnerConeColor(Color wireframeColor)
  403. {
  404. Color color = wireframeColor;
  405. color.a = 0.4f;
  406. return RemapLightColor(CoreUtils.ConvertLinearToActiveColorSpace(color.linear));
  407. }
  408. static Color GetLightAboveObjectWireframeColor(Color wireframeColor)
  409. {
  410. Color color = wireframeColor;
  411. color.a = 1f;
  412. return RemapLightColor(CoreUtils.ConvertLinearToActiveColorSpace(color.linear));
  413. }
  414. static Color GetLightBehindObjectWireframeColor(Color wireframeColor)
  415. {
  416. Color color = wireframeColor;
  417. color.a = 0.2f;
  418. return RemapLightColor(CoreUtils.ConvertLinearToActiveColorSpace(color.linear));
  419. }
  420. static Color RemapLightColor(Color src)
  421. {
  422. Color color = src;
  423. float max = Mathf.Max( Mathf.Max(color.r, color.g), color.b);
  424. if (max > 0f)
  425. {
  426. float mult = 1f / max;
  427. color.r *= mult;
  428. color.g *= mult;
  429. color.b *= mult;
  430. }
  431. else
  432. {
  433. color = Color.white;
  434. }
  435. return color;
  436. }
  437. static void DrawSpotlightWireframe(Light spotlight, Color outerColor, Color innerColor)
  438. {
  439. // Variable for which direction to draw the handles
  440. HandleDirections DrawHandleDirections;
  441. float outerAngle = spotlight.spotAngle;
  442. float innerAngle = spotlight.innerSpotAngle;
  443. float range = spotlight.range;
  444. var outerDiscRadius = range * Mathf.Sin(outerAngle * Mathf.Deg2Rad * 0.5f);
  445. var outerDiscDistance = Mathf.Cos(Mathf.Deg2Rad * outerAngle * 0.5f) * range;
  446. var vectorLineUp = Vector3.Normalize(Vector3.forward * outerDiscDistance + Vector3.up * outerDiscRadius);
  447. var vectorLineLeft = Vector3.Normalize(Vector3.forward * outerDiscDistance + Vector3.left * outerDiscRadius);
  448. // Need to check if we need to draw inner angle
  449. // Need to disable this for now until we get all the inner angle baking working.
  450. if(innerAngle > 0f && drawInnerConeAngle)
  451. {
  452. DrawHandleDirections = HandleDirections.Up | HandleDirections.Down;
  453. var innerDiscRadius = range * Mathf.Sin(innerAngle * Mathf.Deg2Rad * 0.5f);
  454. var innerDiscDistance = Mathf.Cos(Mathf.Deg2Rad * innerAngle * 0.5f) * range;
  455. // Drawing the inner Cone and also z-testing it to draw another color if behind
  456. Handles.color = innerColor;
  457. DrawConeWireframe(innerDiscRadius, innerDiscDistance, DrawHandleDirections);
  458. }
  459. // Draw range line
  460. Handles.color = innerColor;
  461. var rangeCenter = Vector3.forward * range;
  462. Handles.DrawLine(Vector3.zero, rangeCenter);
  463. // Drawing the outer Cone and also z-testing it to draw another color if behind
  464. Handles.color = outerColor;
  465. DrawHandleDirections = HandleDirections.Left | HandleDirections.Right;
  466. DrawConeWireframe(outerDiscRadius, outerDiscDistance, DrawHandleDirections);
  467. // Bottom arcs, making a nice rounded shape
  468. Handles.DrawWireArc(Vector3.zero, Vector3.right, vectorLineUp, outerAngle, range);
  469. Handles.DrawWireArc(Vector3.zero, Vector3.up, vectorLineLeft, outerAngle, range);
  470. // If we are using shadows we draw the near plane for shadows
  471. if(spotlight.shadows != LightShadows.None && spotlight.lightmapBakeType != LightmapBakeType.Baked)
  472. {
  473. DrawShadowNearPlane(spotlight, innerColor);
  474. }
  475. }
  476. static void DrawShadowNearPlane(Light spotlight, Color color)
  477. {
  478. Color previousColor = Handles.color;
  479. Handles.color = color;
  480. var shadowDiscRadius = Mathf.Tan(spotlight.spotAngle * Mathf.Deg2Rad * 0.5f) * spotlight.shadowNearPlane;
  481. var shadowDiscDistance = spotlight.shadowNearPlane ;
  482. Handles.DrawWireDisc(Vector3.forward * shadowDiscDistance, Vector3.forward, shadowDiscRadius);
  483. Handles.DrawLine(Vector3.forward * shadowDiscDistance, (Vector3.right * shadowDiscRadius) + (Vector3.forward * shadowDiscDistance));
  484. Handles.DrawLine(Vector3.forward * shadowDiscDistance, (-Vector3.right * shadowDiscRadius) + (Vector3.forward * shadowDiscDistance));
  485. Handles.color = previousColor;
  486. }
  487. static void DrawConeWireframe(float radius, float height, HandleDirections handleDirections)
  488. {
  489. var rangeCenter = Vector3.forward * height;
  490. if (handleDirections.HasFlag(HandleDirections.Up))
  491. {
  492. var rangeUp = rangeCenter + Vector3.up * radius;
  493. Handles.DrawLine(Vector3.zero, rangeUp);
  494. }
  495. if (handleDirections.HasFlag(HandleDirections.Down))
  496. {
  497. var rangeDown = rangeCenter - Vector3.up * radius;
  498. Handles.DrawLine(Vector3.zero, rangeDown);
  499. }
  500. if (handleDirections.HasFlag(HandleDirections.Right))
  501. {
  502. var rangeRight = rangeCenter + Vector3.right * radius;
  503. Handles.DrawLine(Vector3.zero, rangeRight);
  504. }
  505. if (handleDirections.HasFlag(HandleDirections.Left))
  506. {
  507. var rangeLeft = rangeCenter - Vector3.right * radius;
  508. Handles.DrawLine(Vector3.zero, rangeLeft);
  509. }
  510. //Draw Circle
  511. Handles.DrawWireDisc(rangeCenter, Vector3.forward, radius);
  512. }
  513. static float DrawConeHandles(Vector3 position, float angle, float range, HandleDirections handleDirections, string controlName)
  514. {
  515. if(handleDirections.HasFlag(HandleDirections.Left))
  516. {
  517. angle = SizeSliderSpotAngle(position, Vector3.forward, -Vector3.right, range, angle, controlName);
  518. }
  519. if(handleDirections.HasFlag(HandleDirections.Up))
  520. {
  521. angle = SizeSliderSpotAngle(position, Vector3.forward, Vector3.up, range, angle, controlName);
  522. }
  523. if(handleDirections.HasFlag(HandleDirections.Right))
  524. {
  525. angle = SizeSliderSpotAngle(position, Vector3.forward, Vector3.right, range, angle, controlName);
  526. }
  527. if(handleDirections.HasFlag(HandleDirections.Down))
  528. {
  529. angle = SizeSliderSpotAngle(position, Vector3.forward, -Vector3.up, range, angle, controlName);
  530. }
  531. return angle;
  532. }
  533. static float SliderLineHandle(int id, Vector3 position, Vector3 direction, float value, string labelText = "")
  534. {
  535. Vector3 pos = position + direction * value;
  536. float sizeHandle = HandleUtility.GetHandleSize(pos);
  537. bool temp = GUI.changed;
  538. GUI.changed = false;
  539. pos = Handles.Slider(id, pos, direction, sizeHandle * 0.03f, Handles.DotHandleCap, 0f);
  540. if (GUI.changed)
  541. {
  542. value = Vector3.Dot(pos - position, direction);
  543. }
  544. GUI.changed |= temp;
  545. if (GUIUtility.hotControl == id && !String.IsNullOrEmpty(labelText))
  546. {
  547. labelText += FormattableString.Invariant($"{value:0.00}");
  548. DrawHandleLabel(pos, labelText);
  549. }
  550. return value;
  551. }
  552. static float SizeSliderSpotAngle(Vector3 position, Vector3 forward, Vector3 axis, float range, float spotAngle, string controlName)
  553. {
  554. if (Math.Abs(spotAngle) <= 0.05f)
  555. return spotAngle;
  556. var angledForward = Quaternion.AngleAxis(Mathf.Max(spotAngle, 0.05f) * 0.5f, axis) * forward;
  557. var centerToLeftOnSphere = (angledForward * range + position) - (position + forward * range);
  558. bool temp = GUI.changed;
  559. GUI.changed = false;
  560. var handlePosition = position + forward * range;
  561. var id = GUIUtility.GetControlID(FocusType.Passive);
  562. var newMagnitude = Mathf.Max(0f, SliderLineHandle(id, handlePosition, centerToLeftOnSphere.normalized, centerToLeftOnSphere.magnitude));
  563. if (GUI.changed)
  564. {
  565. centerToLeftOnSphere = centerToLeftOnSphere.normalized * newMagnitude;
  566. angledForward = (centerToLeftOnSphere + (position + forward * range) - position).normalized;
  567. spotAngle = Mathf.Clamp(Mathf.Acos(Vector3.Dot(forward, angledForward)) * Mathf.Rad2Deg * 2, 0f, 179f);
  568. if (spotAngle <= 0.05f || float.IsNaN(spotAngle))
  569. spotAngle = 0f;
  570. }
  571. GUI.changed |= temp;
  572. if (GUIUtility.hotControl == id)
  573. {
  574. var pos = handlePosition + centerToLeftOnSphere.normalized * newMagnitude;
  575. string labelText = FormattableString.Invariant($"{controlName} {spotAngle:0.00}");
  576. DrawHandleLabel(pos, labelText);
  577. }
  578. return spotAngle;
  579. }
  580. }
  581. }