UltiDraw.cs 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  1. /** A commerical class which should not be public. Use PEDraw instead. **/
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace Ximmerse.XR.Utils
  6. {
  7. internal static class UltiDraw
  8. {
  9. public static Color White = Color.white;
  10. public static Color Black = Color.black;
  11. public static Color Red = Color.red;
  12. public static Color DarkRed = new Color(0.75f, 0f, 0f, 1f);
  13. public static Color Green = Color.green;
  14. public static Color DarkGreen = new Color(0f, 0.75f, 0f, 1f);
  15. public static Color Blue = Color.blue;
  16. public static Color Cyan = Color.cyan;
  17. public static Color Magenta = Color.magenta;
  18. public static Color Yellow = Color.yellow;
  19. public static Color Grey = Color.grey;
  20. public static Color LightGrey = new Color(0.75f, 0.75f, 0.75f, 1f);
  21. public static Color DarkGrey = new Color(0.25f, 0.25f, 0.25f, 1f);
  22. public static Color Orange = new Color(1f, 0.5f, 0f, 1f);
  23. public static Color Brown = new Color(0.5f, 0.25f, 0f, 1f);
  24. public static Color Mustard = new Color(1f, 0.75f, 0.25f, 1f);
  25. public static Color Teal = new Color(0f, 0.75f, 0.75f, 1f);
  26. public static Color Purple = new Color(0.5f, 0f, 0.5f, 1f);
  27. private static int Resolution = 30;
  28. private static Mesh Initialised;
  29. private static bool Active;
  30. private static Material GLMaterial;
  31. private static Material MeshMaterial;
  32. public static Material CustomMaterial;
  33. public static int CustomMaterialPass;
  34. private static float GUIOffset = 0.001f;
  35. private static Camera Camera;
  36. private static Vector3 ViewPosition;
  37. private static Quaternion ViewRotation;
  38. private static PROGRAM Program = PROGRAM.NONE;
  39. private enum PROGRAM
  40. {
  41. NONE,
  42. LINES,
  43. TRIANGLES,
  44. TRIANGLE_STRIP,
  45. QUADS}
  46. ;
  47. private static Mesh CircleMesh;
  48. private static Mesh QuadMesh;
  49. private static Mesh CubeMesh;
  50. private static Mesh SphereMesh;
  51. private static Mesh CylinderMesh;
  52. private static Mesh CapsuleMesh;
  53. private static Mesh ConeMesh;
  54. private static Mesh PyramidMesh;
  55. private static Mesh BoneMesh;
  56. private static Mesh PlaneMesh;
  57. private static Vector3[] CircleWire;
  58. private static Vector3[] QuadWire;
  59. private static Vector3[] CubeWire;
  60. private static Vector3[] SphereWire;
  61. private static Vector3[] CylinderWire;
  62. private static Vector3[] CapsuleWire;
  63. private static Vector3[] ConeWire;
  64. private static Vector3[] PyramidWire;
  65. private static Vector3[] BoneWire;
  66. private static Vector3[] PlaneWire;
  67. /// <summary>
  68. /// Should be called each time before actual drawing.
  69. /// Return false for fail drawing.
  70. /// </summary>
  71. public static bool Begin()
  72. {
  73. if (Active)
  74. {
  75. Debug.Log("Drawing is still active. Call 'End()' to stop.");
  76. return true;
  77. }
  78. else
  79. {
  80. Initialise();
  81. Camera = GetCamera();
  82. if(Camera != null)
  83. {
  84. ViewPosition = Camera.transform.position;
  85. ViewRotation = Camera.transform.rotation;
  86. Active = true;
  87. return true;
  88. }
  89. else
  90. {
  91. return false;
  92. }
  93. }
  94. }
  95. public static void End()
  96. {
  97. if (Active)
  98. {
  99. SetProgram(PROGRAM.NONE);
  100. Camera = null;
  101. ViewPosition = Vector3.zero;
  102. ViewRotation = Quaternion.identity;
  103. Active = false;
  104. }
  105. // else
  106. // {
  107. // Debug.Log("Drawing is not active. Call 'Begin()' to start.");
  108. // }
  109. }
  110. public static void SetDepthRendering(bool enabled)
  111. {
  112. Initialise();
  113. SetProgram(PROGRAM.NONE);
  114. GLMaterial.SetInt("_ZWrite", enabled ? 1 : 0);
  115. GLMaterial.SetInt("_ZTest", enabled ? (int)UnityEngine.Rendering.CompareFunction.LessEqual : (int)UnityEngine.Rendering.CompareFunction.Always);
  116. MeshMaterial.SetInt("_ZWrite", enabled ? 1 : 0);
  117. MeshMaterial.SetInt("_ZTest", enabled ? (int)UnityEngine.Rendering.CompareFunction.LessEqual : (int)UnityEngine.Rendering.CompareFunction.Always);
  118. }
  119. public static void SetCurvature(float value)
  120. {
  121. Initialise();
  122. SetProgram(PROGRAM.NONE);
  123. MeshMaterial.SetFloat("_Power", value);
  124. }
  125. public static void SetFilling(float value)
  126. {
  127. value = Mathf.Clamp(value, 0f, 1f);
  128. Initialise();
  129. SetProgram(PROGRAM.NONE);
  130. MeshMaterial.SetFloat("_Filling", value);
  131. }
  132. //------------------------------------------------------------------------------------------
  133. //2D SCENE DRAWING FUNCTIONS
  134. //------------------------------------------------------------------------------------------
  135. public static void DrawLine(Vector3 start, Vector3 end, Color color)
  136. {
  137. if (Return())
  138. {
  139. return;
  140. }
  141. ;
  142. SetProgram(PROGRAM.LINES);
  143. GL.Color(color);
  144. GL.Vertex(start);
  145. GL.Vertex(end);
  146. }
  147. public static void DrawLine(Vector3 start, Vector3 end, float thickness, Color color)
  148. {
  149. DrawLine(start, end, thickness, thickness, color);
  150. }
  151. public static void DrawLine(Vector3 start, Vector3 end, float startThickness, float endThickness, Color color)
  152. {
  153. if (Return())
  154. {
  155. return;
  156. }
  157. ;
  158. SetProgram(PROGRAM.QUADS);
  159. GL.Color(color);
  160. Vector3 dir = (end - start).normalized;
  161. Vector3 orthoStart = startThickness / 2f * (Quaternion.AngleAxis(90f, (start - ViewPosition)) * dir);
  162. Vector3 orthoEnd = endThickness / 2f * (Quaternion.AngleAxis(90f, (end - ViewPosition)) * dir);
  163. GL.Vertex(end + orthoEnd);
  164. GL.Vertex(end - orthoEnd);
  165. GL.Vertex(start - orthoStart);
  166. GL.Vertex(start + orthoStart);
  167. }
  168. public static void DrawTriangle(Vector3 a, Vector3 b, Vector3 c, Color color)
  169. {
  170. if (Return())
  171. {
  172. return;
  173. }
  174. ;
  175. SetProgram(PROGRAM.TRIANGLES);
  176. GL.Color(color);
  177. GL.Vertex(b);
  178. GL.Vertex(a);
  179. GL.Vertex(c);
  180. }
  181. public static void DrawTriangleWire(Vector3 a, Vector3 b, Vector3 c, Color color)
  182. {
  183. if (Return())
  184. {
  185. return;
  186. }
  187. ;
  188. SetProgram(PROGRAM.LINES);
  189. GL.Color(color);
  190. GL.Vertex(a);
  191. GL.Vertex(b);
  192. GL.Vertex(b);
  193. GL.Vertex(c);
  194. GL.Vertex(c);
  195. GL.Vertex(a);
  196. }
  197. public static void DrawCircle(Vector3 position, float size, Color color)
  198. {
  199. DrawMesh(CircleMesh, position, ViewRotation, size * Vector3.one, color);
  200. }
  201. public static void DrawCircle(Vector3 position, Quaternion rotation, float size, Color color)
  202. {
  203. DrawMesh(CircleMesh, position, rotation, size * Vector3.one, color);
  204. }
  205. public static void DrawWireCircle(Vector3 position, float size, Color color)
  206. {
  207. DrawWire(CircleWire, position, ViewRotation, size * Vector3.one, color);
  208. }
  209. public static void DrawWireCircle(Vector3 position, Quaternion rotation, float size, Color color)
  210. {
  211. DrawWire(CircleWire, position, rotation, size * Vector3.one, color);
  212. }
  213. public static void DrawWiredCircle(Vector3 position, float size, Color circleColor, Color wireColor)
  214. {
  215. DrawCircle(position, size, circleColor);
  216. DrawWireCircle(position, size, wireColor);
  217. }
  218. public static void DrawWiredCircle(Vector3 position, Quaternion rotation, float size, Color circleColor, Color wireColor)
  219. {
  220. DrawCircle(position, rotation, size, circleColor);
  221. DrawWireCircle(position, rotation, size, wireColor);
  222. }
  223. public static void DrawEllipse(Vector3 position, float width, float height, Color color)
  224. {
  225. DrawMesh(CircleMesh, position, ViewRotation, new Vector3(width, height, 1f), color);
  226. }
  227. public static void DrawEllipse(Vector3 position, Quaternion rotation, float width, float height, Color color)
  228. {
  229. DrawMesh(CircleMesh, position, rotation, new Vector3(width, height, 1f), color);
  230. }
  231. public static void DrawWireEllipse(Vector3 position, float width, float height, Color color)
  232. {
  233. DrawWire(CircleWire, position, ViewRotation, new Vector3(width, height, 1f), color);
  234. }
  235. public static void DrawWireEllipse(Vector3 position, Quaternion rotation, float width, float height, Color color)
  236. {
  237. DrawWire(CircleWire, position, rotation, new Vector3(width, height, 1f), color);
  238. }
  239. public static void DrawWiredEllipse(Vector3 position, float width, float height, Color ellipseColor, Color wireColor)
  240. {
  241. DrawEllipse(position, ViewRotation, width, height, ellipseColor);
  242. DrawWireEllipse(position, ViewRotation, width, height, wireColor);
  243. }
  244. public static void DrawWiredEllipse(Vector3 position, Quaternion rotation, float width, float height, Color ellipseColor, Color wireColor)
  245. {
  246. DrawEllipse(position, rotation, width, height, ellipseColor);
  247. DrawWireEllipse(position, rotation, width, height, wireColor);
  248. }
  249. public static void DrawArrow(Vector3 start, Vector3 end, float tipPivot, float shaftWidth, float tipWidth, Color color)
  250. {
  251. tipPivot = Mathf.Clamp(tipPivot, 0f, 1f);
  252. Vector3 pivot = start + tipPivot * (end - start);
  253. DrawLine(start, pivot, shaftWidth, color);
  254. DrawLine(pivot, end, tipWidth, 0f, color);
  255. }
  256. public static void DrawArrow(Vector3 start, Vector3 end, float tipPivot, float shaftWidth, float tipWidth, Color shaftColor, Color tipColor)
  257. {
  258. tipPivot = Mathf.Clamp(tipPivot, 0f, 1f);
  259. Vector3 pivot = start + tipPivot * (end - start);
  260. DrawLine(start, pivot, shaftWidth, shaftColor);
  261. DrawLine(pivot, end, tipWidth, 0f, tipColor);
  262. }
  263. public static void DrawGrid(Vector3 center, Quaternion rotation, int cellsX, int cellsY, float sizeX, float sizeY, Color color)
  264. {
  265. if (Return())
  266. {
  267. return;
  268. }
  269. float width = cellsX * sizeX;
  270. float height = cellsY * sizeY;
  271. Vector3 start = center - width / 2f * (rotation * Vector3.right) - height / 2f * (rotation * Vector3.forward);
  272. Vector3 dirX = rotation * Vector3.right;
  273. Vector3 dirY = rotation * Vector3.forward;
  274. for (int i = 0; i < cellsX + 1; i++)
  275. {
  276. DrawLine(start + i * sizeX * dirX, start + i * sizeX * dirX + height * dirY, color);
  277. }
  278. for (int i = 0; i < cellsY + 1; i++)
  279. {
  280. DrawLine(start + i * sizeY * dirY, start + i * sizeY * dirY + width * dirX, color);
  281. }
  282. }
  283. /// <summary>
  284. /// Draws the grid from origin point, where center, rotation is the left bottom point of the grid.
  285. /// </summary>
  286. /// <param name="center">Center.</param>
  287. /// <param name="rotation">Rotation.</param>
  288. /// <param name="cellsX">Cells x.</param>
  289. /// <param name="cellsY">Cells y.</param>
  290. /// <param name="sizeX">Size x.</param>
  291. /// <param name="sizeY">Size y.</param>
  292. /// <param name="color">Color.</param>
  293. public static void DrawGridLBOrigin(Vector3 center, Quaternion rotation, int cellsX, int cellsY, float sizeX, float sizeY, Color color)
  294. {
  295. if (Return())
  296. {
  297. return;
  298. }
  299. float width = cellsX * sizeX;
  300. float height = cellsY * sizeY;
  301. Vector3 start = center;
  302. Vector3 dirX = rotation * Vector3.right;
  303. Vector3 dirY = rotation * Vector3.forward;
  304. for (int i = 0; i < cellsX + 1; i++)
  305. {
  306. DrawLine(start + i * sizeX * dirX, start + i * sizeX * dirX + height * dirY, color);
  307. }
  308. for (int i = 0; i < cellsY + 1; i++)
  309. {
  310. DrawLine(start + i * sizeY * dirY, start + i * sizeY * dirY + width * dirX, color);
  311. }
  312. }
  313. /// <summary>
  314. /// Draws the grid from origin point, where center, rotation is the right top point of the grid.
  315. /// </summary>
  316. /// <param name="center">Center.</param>
  317. /// <param name="rotation">Rotation.</param>
  318. /// <param name="cellsX">Cells x.</param>
  319. /// <param name="cellsY">Cells y.</param>
  320. /// <param name="sizeX">Size x.</param>
  321. /// <param name="sizeY">Size y.</param>
  322. /// <param name="color">Color.</param>
  323. public static void DrawGridRTOrigin(Vector3 center, Quaternion rotation, int cellsX, int cellsY, float sizeX, float sizeY, Color color)
  324. {
  325. if (Return())
  326. {
  327. return;
  328. }
  329. float width = cellsX * sizeX;
  330. float height = cellsY * sizeY;
  331. Vector3 start = center;
  332. Vector3 dirX = rotation * Vector3.left;
  333. Vector3 dirY = rotation * -Vector3.forward;
  334. for (int i = 0; i < cellsX + 1; i++)
  335. {
  336. DrawLine(start + i * sizeX * dirX, start + i * sizeX * dirX + height * dirY, color);
  337. }
  338. for (int i = 0; i < cellsY + 1; i++)
  339. {
  340. DrawLine(start + i * sizeY * dirY, start + i * sizeY * dirY + width * dirX, color);
  341. }
  342. }
  343. public static void DrawQuad(Vector3 position, Quaternion rotation, float width, float height, Color color)
  344. {
  345. DrawMesh(QuadMesh, position, rotation, new Vector3(width, height, 1f), color);
  346. }
  347. public static void DrawWireQuad(Vector3 position, Quaternion rotation, float width, float height, Color color)
  348. {
  349. DrawWire(QuadWire, position, rotation, new Vector3(width, height, 1f), color);
  350. }
  351. public static void DrawWiredQuad(Vector3 position, Quaternion rotation, float width, float height, Color quadColor, Color wireColor)
  352. {
  353. DrawQuad(position, rotation, width, height, quadColor);
  354. DrawWireQuad(position, rotation, width, height, wireColor);
  355. }
  356. public static void DrawPlane(Vector3 position, Quaternion rotation, float width, float height, Color color)
  357. {
  358. DrawMesh(PlaneMesh, position, rotation, new Vector3(width / 10, 1, height / 10), color);
  359. }
  360. public static void DrawWirePlane(Vector3 position, Quaternion rotation, float width, float height, Color color)
  361. {
  362. DrawWire(PlaneWire, position, rotation, new Vector3(width, 1, height), color);
  363. }
  364. public static void DrawWiredPlane(Vector3 position, Quaternion rotation, float width, float height, Color color, Color wiredColor)
  365. {
  366. DrawWire(PlaneWire, position, rotation, new Vector3(width, 1, height), wiredColor);
  367. DrawMesh(PlaneMesh, position, rotation, new Vector3(width / 10, 1, height / 10), color);
  368. }
  369. public static void DrawCube(Vector3 position, Quaternion rotation, float size, Color color)
  370. {
  371. DrawMesh(CubeMesh, position, rotation, size * Vector3.one, color);
  372. }
  373. public static void DrawWireCube(Vector3 position, Quaternion rotation, float size, Color color)
  374. {
  375. DrawWire(CubeWire, position, rotation, size * Vector3.one, color);
  376. }
  377. public static void DrawWiredCube(Vector3 position, Quaternion rotation, float size, Color cubeColor, Color wireColor)
  378. {
  379. DrawCube(position, rotation, size, cubeColor);
  380. DrawWireCube(position, rotation, size, wireColor);
  381. }
  382. public static void DrawCuboid(Vector3 position, Quaternion rotation, Vector3 size, Color color)
  383. {
  384. DrawMesh(CubeMesh, position, rotation, size, color);
  385. }
  386. public static void DrawWireCuboid(Vector3 position, Quaternion rotation, Vector3 size, Color color)
  387. {
  388. DrawWire(CubeWire, position, rotation, size, color);
  389. }
  390. public static void DrawWiredCuboid(Vector3 position, Quaternion rotation, Vector3 size, Color cuboidColor, Color wireColor)
  391. {
  392. DrawCuboid(position, rotation, size, cuboidColor);
  393. DrawWireCuboid(position, rotation, size, wireColor);
  394. }
  395. public static void DrawSphere(Vector3 position, Quaternion rotation, float size, Color color)
  396. {
  397. DrawMesh(SphereMesh, position, rotation, size * Vector3.one, color);
  398. }
  399. public static void DrawWireSphere(Vector3 position, Quaternion rotation, float size, Color color)
  400. {
  401. DrawWire(SphereWire, position, rotation, size * Vector3.one, color);
  402. }
  403. public static void DrawWiredSphere(Vector3 position, Quaternion rotation, float size, Color sphereColor, Color wireColor)
  404. {
  405. DrawSphere(position, rotation, size, sphereColor);
  406. DrawWireSphere(position, rotation, size, wireColor);
  407. }
  408. public static void DrawEllipsoid(Vector3 position, Quaternion rotation, float width, float height, Color color)
  409. {
  410. DrawMesh(SphereMesh, position, rotation, new Vector3(width, height, width), color);
  411. }
  412. public static void DrawWireEllipsoid(Vector3 position, Quaternion rotation, float width, float height, Color color)
  413. {
  414. DrawWire(SphereWire, position, rotation, new Vector3(width, height, width), color);
  415. }
  416. public static void DrawWiredEllipsoid(Vector3 position, Quaternion rotation, float width, float height, Color ellipsoidColor, Color wireColor)
  417. {
  418. DrawEllipsoid(position, rotation, width, height, ellipsoidColor);
  419. DrawWireEllipsoid(position, rotation, width, height, wireColor);
  420. }
  421. public static void DrawCylinder(Vector3 position, Quaternion rotation, float width, float height, Color color)
  422. {
  423. DrawMesh(CylinderMesh, position, rotation, new Vector3(width, height / 2f, width), color);
  424. }
  425. public static void DrawWireCylinder(Vector3 position, Quaternion rotation, float width, float height, Color color)
  426. {
  427. DrawWire(CylinderWire, position, rotation, new Vector3(width, height / 2f, width), color);
  428. }
  429. public static void DrawWiredCylinder(Vector3 position, Quaternion rotation, float width, float height, Color cylinderColor, Color wireColor)
  430. {
  431. DrawCylinder(position, rotation, width, height, cylinderColor);
  432. DrawWireCylinder(position, rotation, width, height, wireColor);
  433. }
  434. public static void DrawCapsule(Vector3 position, Quaternion rotation, float width, float height, Color color)
  435. {
  436. DrawMesh(CapsuleMesh, position, rotation, new Vector3(width, height / 2f, width), color);
  437. }
  438. public static void DrawWireCapsule(Vector3 position, Quaternion rotation, float width, float height, Color color)
  439. {
  440. DrawWire(CapsuleWire, position, rotation, new Vector3(width, height / 2f, width), color);
  441. }
  442. public static void DrawWiredCapsule(Vector3 position, Quaternion rotation, float width, float height, Color capsuleColor, Color wireColor)
  443. {
  444. DrawCapsule(position, rotation, width, height, capsuleColor);
  445. DrawWireCapsule(position, rotation, width, height, wireColor);
  446. }
  447. public static void DrawCone(Vector3 position, Quaternion rotation, float width, float height, Color color)
  448. {
  449. DrawMesh(ConeMesh, position, rotation, new Vector3(width, height, width), color);
  450. }
  451. public static void DrawWireCone(Vector3 position, Quaternion rotation, float width, float height, Color color)
  452. {
  453. DrawWire(ConeWire, position, rotation, new Vector3(width, height, width), color);
  454. }
  455. public static void DrawWiredCone(Vector3 position, Quaternion rotation, float width, float height, Color coneColor, Color wireColor)
  456. {
  457. DrawCone(position, rotation, width, height, coneColor);
  458. DrawWireCone(position, rotation, width, height, wireColor);
  459. }
  460. public static void DrawPyramid(Vector3 position, Quaternion rotation, float width, float height, Color color)
  461. {
  462. DrawMesh(PyramidMesh, position, rotation, new Vector3(width, height, width), color);
  463. }
  464. public static void DrawWirePyramid(Vector3 position, Quaternion rotation, float width, float height, Color color)
  465. {
  466. DrawWire(PyramidWire, position, rotation, new Vector3(width, height, width), color);
  467. }
  468. public static void DrawWiredPyramid(Vector3 position, Quaternion rotation, float width, float height, Color pyramidColor, Color wireColor)
  469. {
  470. DrawPyramid(position, rotation, width, height, pyramidColor);
  471. DrawWirePyramid(position, rotation, width, height, wireColor);
  472. }
  473. public static void DrawBone(Vector3 position, Quaternion rotation, float width, float length, Color color)
  474. {
  475. DrawMesh(BoneMesh, position, rotation, new Vector3(width, width, length), color);
  476. }
  477. public static void DrawWireBone(Vector3 position, Quaternion rotation, float width, float length, Color color)
  478. {
  479. DrawWire(BoneWire, position, rotation, new Vector3(width, width, length), color);
  480. }
  481. public static void DrawWiredBone(Vector3 position, Quaternion rotation, float width, float length, Color boneColor, Color wireColor)
  482. {
  483. DrawBone(position, rotation, width, length, boneColor);
  484. DrawWireBone(position, rotation, width, length, wireColor);
  485. }
  486. public static void DrawTranslateGizmo(Vector3 position, Quaternion rotation, float size)
  487. {
  488. if (Return())
  489. {
  490. return;
  491. }
  492. DrawLine(position, position + 0.8f * size * (rotation * Vector3.right), Red);
  493. DrawCone(position + 0.8f * size * (rotation * Vector3.right), rotation * Quaternion.Euler(0f, 0f, -90f), 0.15f * size, 0.2f * size, Red);
  494. DrawLine(position, position + 0.8f * size * (rotation * Vector3.up), Green);
  495. DrawCone(position + 0.8f * size * (rotation * Vector3.up), rotation * Quaternion.Euler(0f, 0f, 0f), 0.15f * size, 0.2f * size, Green);
  496. DrawLine(position, position + 0.8f * size * (rotation * Vector3.forward), Blue);
  497. DrawCone(position + 0.8f * size * (rotation * Vector3.forward), rotation * Quaternion.Euler(90f, 0f, 0f), 0.15f * size, 0.2f * size, Blue);
  498. }
  499. public static void DrawRotateGizmo(Vector3 position, Quaternion rotation, float size)
  500. {
  501. if (Return())
  502. {
  503. return;
  504. }
  505. SetProgram(PROGRAM.NONE);
  506. DrawWireCircle(position, rotation * Quaternion.Euler(0f, 90f, 0f), 2f * size, Red);
  507. SetProgram(PROGRAM.NONE);
  508. DrawWireCircle(position, rotation * Quaternion.Euler(90f, 0f, 90f), 2f * size, Green);
  509. SetProgram(PROGRAM.NONE);
  510. DrawWireCircle(position, rotation * Quaternion.Euler(0f, 0f, 0f), 2f * size, Blue);
  511. SetProgram(PROGRAM.NONE);
  512. }
  513. public static void DrawScaleGizmo(Vector3 position, Quaternion rotation, float size)
  514. {
  515. if (Return())
  516. {
  517. return;
  518. }
  519. DrawLine(position, position + 0.85f * size * (rotation * Vector3.right), Red);
  520. DrawCube(position + 0.925f * size * (rotation * Vector3.right), rotation, 0.15f, Red);
  521. DrawLine(position, position + 0.85f * size * (rotation * Vector3.up), Green);
  522. DrawCube(position + 0.925f * size * (rotation * Vector3.up), rotation, 0.15f, Green);
  523. DrawLine(position, position + 0.85f * size * (rotation * Vector3.forward), Blue);
  524. DrawCube(position + 0.925f * size * (rotation * Vector3.forward), rotation, 0.15f, Blue);
  525. }
  526. public static void DrawMesh(Mesh mesh, Vector3 position, Quaternion rotation, Vector3 scale, Color color)
  527. {
  528. if (Return())
  529. {
  530. return;
  531. }
  532. SetProgram(PROGRAM.NONE);
  533. if(CustomMaterial != null)
  534. {
  535. CustomMaterial.color = color;
  536. CustomMaterial.SetPass (CustomMaterialPass);
  537. }
  538. else
  539. {
  540. MeshMaterial.color = color;
  541. MeshMaterial.SetPass(0);
  542. }
  543. Graphics.DrawMeshNow(mesh, Matrix4x4.TRS(position, rotation, scale));
  544. }
  545. //------------------------------------------------------------------------------------------
  546. //GUI DRAWING FUNCTIONS
  547. //------------------------------------------------------------------------------------------
  548. public static void DrawGUILine(Vector2 start, Vector2 end, Color color)
  549. {
  550. if (Camera != Camera.main)
  551. {
  552. return;
  553. }
  554. if (Return())
  555. {
  556. return;
  557. }
  558. SetProgram(PROGRAM.LINES);
  559. GL.Color(color);
  560. start.x *= Screen.width;
  561. start.y *= Screen.height;
  562. end.x *= Screen.width;
  563. end.y *= Screen.height;
  564. GL.Vertex(Camera.ScreenToWorldPoint(new Vector3(start.x, start.y, Camera.nearClipPlane + GUIOffset)));
  565. GL.Vertex(Camera.ScreenToWorldPoint(new Vector3(end.x, end.y, Camera.nearClipPlane + GUIOffset)));
  566. }
  567. public static void DrawGUILine(Vector2 start, Vector2 end, float thickness, Color color)
  568. {
  569. if (Camera != Camera.main)
  570. {
  571. return;
  572. }
  573. if (Return())
  574. {
  575. return;
  576. }
  577. SetProgram(PROGRAM.QUADS);
  578. GL.Color(color);
  579. start.x *= Screen.width;
  580. start.y *= Screen.height;
  581. end.x *= Screen.width;
  582. end.y *= Screen.height;
  583. thickness *= Screen.width;
  584. Vector3 p1 = new Vector3(start.x, start.y, Camera.nearClipPlane + GUIOffset);
  585. Vector3 p2 = new Vector3(end.x, end.y, Camera.nearClipPlane + GUIOffset);
  586. Vector3 dir = end - start;
  587. Vector3 ortho = thickness / 2f * (Quaternion.AngleAxis(90f, Vector3.forward) * dir).normalized;
  588. GL.Vertex(Camera.ScreenToWorldPoint(p1 - ortho));
  589. GL.Vertex(Camera.ScreenToWorldPoint(p1 + ortho));
  590. GL.Vertex(Camera.ScreenToWorldPoint(p2 + ortho));
  591. GL.Vertex(Camera.ScreenToWorldPoint(p2 - ortho));
  592. }
  593. public static void DrawGUIRectangle(Vector2 center, Vector2 size, Color color)
  594. {
  595. if (Camera != Camera.main)
  596. {
  597. return;
  598. }
  599. if (Return())
  600. {
  601. return;
  602. }
  603. SetProgram(PROGRAM.QUADS);
  604. GL.Color(color);
  605. center.x *= Screen.width;
  606. center.y *= Screen.height;
  607. size.x *= Screen.width;
  608. size.y *= Screen.height;
  609. GL.Vertex(Camera.ScreenToWorldPoint(new Vector3(center.x + size.x / 2f, center.y - size.y / 2f, Camera.nearClipPlane + GUIOffset)));
  610. GL.Vertex(Camera.ScreenToWorldPoint(new Vector3(center.x - size.x / 2f, center.y - size.y / 2f, Camera.nearClipPlane + GUIOffset)));
  611. GL.Vertex(Camera.ScreenToWorldPoint(new Vector3(center.x + -size.x / 2f, center.y + size.y / 2f, Camera.nearClipPlane + GUIOffset)));
  612. GL.Vertex(Camera.ScreenToWorldPoint(new Vector3(center.x + size.x / 2f, center.y + size.y / 2f, Camera.nearClipPlane + GUIOffset)));
  613. }
  614. public static void DrawGUITriangle(Vector2 a, Vector2 b, Vector2 c, Color color)
  615. {
  616. if (Camera != Camera.main)
  617. {
  618. return;
  619. }
  620. if (Return())
  621. {
  622. return;
  623. }
  624. SetProgram(PROGRAM.TRIANGLES);
  625. GL.Color(color);
  626. a.x *= Screen.width;
  627. a.y *= Screen.height;
  628. b.x *= Screen.width;
  629. b.y *= Screen.height;
  630. c.x *= Screen.width;
  631. c.y *= Screen.height;
  632. GL.Vertex(Camera.ScreenToWorldPoint(new Vector3(a.x, a.y, Camera.nearClipPlane + GUIOffset)));
  633. GL.Vertex(Camera.ScreenToWorldPoint(new Vector3(b.x, b.y, Camera.nearClipPlane + GUIOffset)));
  634. GL.Vertex(Camera.ScreenToWorldPoint(new Vector3(c.x, c.y, Camera.nearClipPlane + GUIOffset)));
  635. }
  636. public static void DrawGUICircle(Vector2 center, float size, Color color, Camera targetCamera)
  637. {
  638. if(targetCamera != null && Camera != targetCamera)
  639. {
  640. return;
  641. }
  642. if (targetCamera == null &&Camera != Camera.main)
  643. {
  644. return;
  645. }
  646. if (Return())
  647. {
  648. return;
  649. }
  650. // SetProgram(PROGRAM.TRIANGLE_STRIP);
  651. // GL.Color(color);
  652. // center.x *= Screen.width;
  653. // center.y *= Screen.height;
  654. // for (int i = 0; i < CircleWire.Length; i++)
  655. // {
  656. // GL.Vertex(Camera.ScreenToWorldPoint(new Vector3(center.x + size * CircleWire [i].x * Screen.width, center.y + size * CircleWire [i].y * Screen.width, Camera.nearClipPlane + GUIOffset)));
  657. // GL.Vertex(Camera.ScreenToWorldPoint(new Vector3(center.x, center.y, Camera.nearClipPlane + GUIOffset)));
  658. // }
  659. SetProgram(PROGRAM.TRIANGLES);
  660. GL.Color(color);
  661. center.x *= Screen.width;
  662. center.y *= Screen.height;
  663. for(int i=0; i<CircleWire.Length-1; i++) {
  664. GL.Vertex(Camera.ScreenToWorldPoint(new Vector3(center.x + size*CircleWire[i].x*Screen.width, center.y + size*CircleWire[i].y*Screen.width, Camera.nearClipPlane + GUIOffset)));
  665. GL.Vertex(Camera.ScreenToWorldPoint(new Vector3(center.x, center.y, Camera.nearClipPlane + GUIOffset)));
  666. GL.Vertex(Camera.ScreenToWorldPoint(new Vector3(center.x + size*CircleWire[i+1].x*Screen.width, center.y + size*CircleWire[i+1].y*Screen.width, Camera.nearClipPlane + GUIOffset)));
  667. }
  668. }
  669. //------------------------------------------------------------------------------------------
  670. //PLOTTING FUNCTIONS
  671. //------------------------------------------------------------------------------------------
  672. public static void DrawFunction(Vector2 center, Vector2 size, float[] values, float yMin, float yMax, Color background, Color line)
  673. {
  674. DrawGUIRectangle(center, size, background);
  675. float x = center.x - size.x / 2f;
  676. float y = center.y - size.y / 2f;
  677. float scale = yMax - yMin;
  678. for (int i = 0; i < values.Length - 1; i++)
  679. {
  680. DrawGUILine(
  681. new Vector2(x + (float)i / (float)(values.Length - 1) * size.x, y + Mathf.Clamp(values [i] / scale, 0f, 1f) * size.y),
  682. new Vector2(x + (float)(i + 1) / (float)(values.Length - 1) * size.x, y + Mathf.Clamp(values [i + 1] / scale, 0f, 1f) * size.y),
  683. line
  684. );
  685. }
  686. }
  687. public static void DrawFunction(Vector2 center, Vector2 size, float[] values, float yMin, float yMax, float thickness, Color background, Color line)
  688. {
  689. DrawGUIRectangle(center, size, background);
  690. float x = center.x - size.x / 2f;
  691. float y = center.y - size.y / 2f;
  692. float scale = yMax - yMin;
  693. for (int i = 0; i < values.Length - 1; i++)
  694. {
  695. DrawGUILine(
  696. new Vector2(x + (float)i / (float)(values.Length - 1) * size.x, y + Mathf.Clamp(values [i] / scale, 0f, 1f) * size.y),
  697. new Vector2(x + (float)(i + 1) / (float)(values.Length - 1) * size.x, y + Mathf.Clamp(values [i + 1] / scale, 0f, 1f) * size.y),
  698. thickness,
  699. line
  700. );
  701. }
  702. }
  703. public static void DrawFunctions(Vector2 center, Vector2 size, List<float[]> values, float yMin, float yMax, Color background, Color[] lines)
  704. {
  705. DrawGUIRectangle(center, size, background);
  706. float x = center.x - size.x / 2f;
  707. float y = center.y - size.y / 2f;
  708. float scale = yMax - yMin;
  709. for (int k = 0; k < values.Count; k++)
  710. {
  711. for (int i = 0; i < values [k].Length - 1; i++)
  712. {
  713. DrawGUILine(
  714. new Vector2(x + (float)i / (float)(values [k].Length - 1) * size.x, y + Mathf.Clamp(values [k] [i] / scale, 0f, 1f) * size.y),
  715. new Vector2(x + (float)(i + 1) / (float)(values [k].Length - 1) * size.x, y + Mathf.Clamp(values [k] [i + 1] / scale, 0f, 1f) * size.y),
  716. lines [k]
  717. );
  718. }
  719. }
  720. }
  721. public static void DrawFunctions(Vector2 center, Vector2 size, List<float[]> values, float yMin, float yMax, float thickness, Color background, Color[] lines)
  722. {
  723. DrawGUIRectangle(center, size, background);
  724. float x = center.x - size.x / 2f;
  725. float y = center.y - size.y / 2f;
  726. float scale = yMax - yMin;
  727. for (int k = 0; k < values.Count; k++)
  728. {
  729. for (int i = 0; i < values [k].Length - 1; i++)
  730. {
  731. DrawGUILine(
  732. new Vector2(x + (float)i / (float)(values [k].Length - 1) * size.x, y + Mathf.Clamp(values [k] [i] / scale, 0f, 1f) * size.y),
  733. new Vector2(x + (float)(i + 1) / (float)(values [k].Length - 1) * size.x, y + Mathf.Clamp(values [k] [i + 1] / scale, 0f, 1f) * size.y),
  734. thickness,
  735. lines [k]
  736. );
  737. }
  738. }
  739. }
  740. //------------------------------------------------------------------------------------------
  741. //UTILITY FUNCTIONS
  742. //------------------------------------------------------------------------------------------
  743. private static bool Return()
  744. {
  745. // if (!Active)
  746. // {
  747. // Debug.Log("Drawing is not active. Call 'Begin()' first.");
  748. // }
  749. return !Active;
  750. }
  751. static void Initialise()
  752. {
  753. if (Initialised != null)
  754. {
  755. return;
  756. }
  757. GLMaterial = new Material(Shader.Find("Hidden/Internal-Colored"));
  758. GLMaterial.hideFlags = HideFlags.HideAndDontSave;
  759. GLMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  760. GLMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  761. GLMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Back);
  762. GLMaterial.SetInt("_ZWrite", 1);
  763. GLMaterial.SetInt("_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always);
  764. Shader ultiDrawShader = Resources.Load<Shader>("Tag/Material/RxDraw");
  765. if (ultiDrawShader == null)
  766. {
  767. throw new UnityException("RxDraw.shader not in Resources folder.Call Tools/PolyEngine/PEDraw/Save Shader.");
  768. }
  769. MeshMaterial = new Material(ultiDrawShader);
  770. MeshMaterial.hideFlags = HideFlags.HideAndDontSave;
  771. MeshMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Back);
  772. MeshMaterial.SetInt("_ZWrite", 1);
  773. MeshMaterial.SetInt("_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always);
  774. MeshMaterial.SetFloat("_Power", 0.25f);
  775. //Meshes
  776. CircleMesh = CreateCircleMesh(Resolution);
  777. QuadMesh = GetPrimitiveMesh(PrimitiveType.Quad);
  778. CubeMesh = GetPrimitiveMesh(PrimitiveType.Cube);
  779. SphereMesh = GetPrimitiveMesh(PrimitiveType.Sphere);
  780. CylinderMesh = GetPrimitiveMesh(PrimitiveType.Cylinder);
  781. CapsuleMesh = GetPrimitiveMesh(PrimitiveType.Capsule);
  782. PlaneMesh = GetPrimitiveMesh(PrimitiveType.Plane);
  783. ConeMesh = CreateConeMesh(Resolution);
  784. PyramidMesh = CreatePyramidMesh();
  785. BoneMesh = CreateBoneMesh();
  786. //
  787. //Wires
  788. CircleWire = CreateCircleWire(Resolution);
  789. QuadWire = CreateQuadWire();
  790. CubeWire = CreateCubeWire();
  791. SphereWire = CreateSphereWire(Resolution);
  792. CylinderWire = CreateCylinderWire(Resolution);
  793. CapsuleWire = CreateCapsuleWire(Resolution);
  794. ConeWire = CreateConeWire(Resolution);
  795. PyramidWire = CreatePyramidWire();
  796. BoneWire = CreateBoneWire();
  797. PlaneWire = CreatePlaneWire();
  798. //
  799. Initialised = new Mesh();
  800. }
  801. private static void SetProgram(PROGRAM program)
  802. {
  803. if (Program != program)
  804. {
  805. Program = program;
  806. GL.End();
  807. if (Program != PROGRAM.NONE)
  808. {
  809. GLMaterial.SetPass(0);
  810. switch (Program)
  811. {
  812. case PROGRAM.LINES:
  813. GL.Begin(GL.LINES);
  814. break;
  815. case PROGRAM.TRIANGLES:
  816. GL.Begin(GL.TRIANGLES);
  817. break;
  818. case PROGRAM.TRIANGLE_STRIP:
  819. GL.Begin(GL.TRIANGLE_STRIP);
  820. break;
  821. case PROGRAM.QUADS:
  822. GL.Begin(GL.QUADS);
  823. break;
  824. }
  825. }
  826. }
  827. }
  828. private static void DrawWire(Vector3[] points, Vector3 position, Quaternion rotation, Vector3 scale, Color color)
  829. {
  830. if (Return())
  831. {
  832. return;
  833. }
  834. ;
  835. SetProgram(PROGRAM.LINES);
  836. GL.Color(color);
  837. for (int i = 0; i < points.Length; i += 2)
  838. {
  839. GL.Vertex(position + rotation * Vector3.Scale(scale, points [i]));
  840. GL.Vertex(position + rotation * Vector3.Scale(scale, points [i + 1]));
  841. }
  842. }
  843. static Camera sMainCamera;
  844. private static Camera GetCamera()
  845. {
  846. if (Camera.current != null)
  847. {
  848. return Camera.current;
  849. }
  850. else if(sMainCamera != null)
  851. {
  852. return sMainCamera;
  853. }
  854. else
  855. {
  856. var mainCam = Camera.main;
  857. if(mainCam != null)
  858. {
  859. sMainCamera = mainCam;
  860. return mainCam;
  861. }
  862. else
  863. {
  864. return null;
  865. }
  866. }
  867. }
  868. private static Mesh GetPrimitiveMesh(PrimitiveType type)
  869. {
  870. GameObject gameObject = GameObject.CreatePrimitive(type);
  871. gameObject.hideFlags = HideFlags.HideInHierarchy;
  872. gameObject.GetComponent<MeshRenderer>().enabled = false;
  873. Mesh mesh = gameObject.GetComponent<MeshFilter>().sharedMesh;
  874. if (Application.isPlaying)
  875. {
  876. GameObject.Destroy(gameObject);
  877. } else
  878. {
  879. GameObject.DestroyImmediate(gameObject);
  880. }
  881. return mesh;
  882. }
  883. private static Mesh CreateCircleMesh(int resolution)
  884. {
  885. List<Vector3> vertices = new List<Vector3>();
  886. List<int> triangles = new List<int>();
  887. float step = 360.0f / (float)resolution;
  888. Quaternion quaternion = Quaternion.Euler(0f, 0f, step);
  889. vertices.Add(new Vector3(0f, 0f, 0f));
  890. vertices.Add(new Vector3(0f, 0.5f, 0f));
  891. vertices.Add(quaternion * vertices [1]);
  892. triangles.Add(1);
  893. triangles.Add(0);
  894. triangles.Add(2);
  895. for (int i = 0; i < resolution - 1; i++)
  896. {
  897. triangles.Add(vertices.Count - 1);
  898. triangles.Add(0);
  899. triangles.Add(vertices.Count);
  900. vertices.Add(quaternion * vertices [vertices.Count - 1]);
  901. }
  902. Mesh mesh = new Mesh();
  903. mesh.vertices = vertices.ToArray();
  904. mesh.triangles = triangles.ToArray();
  905. return mesh;
  906. }
  907. private static Mesh CreateConeMesh(int resolution)
  908. {
  909. List<Vector3> vertices = new List<Vector3>();
  910. List<int> triangles = new List<int>();
  911. float step = 360.0f / (float)resolution;
  912. Quaternion quaternion = Quaternion.Euler(0f, step, 0f);
  913. vertices.Add(new Vector3(0f, 1f, 0f));
  914. vertices.Add(new Vector3(0f, 0f, 0f));
  915. vertices.Add(new Vector3(0f, 0f, 0.5f));
  916. vertices.Add(quaternion * vertices [2]);
  917. triangles.Add(2);
  918. triangles.Add(1);
  919. triangles.Add(3);
  920. triangles.Add(2);
  921. triangles.Add(3);
  922. triangles.Add(0);
  923. for (int i = 0; i < resolution - 1; i++)
  924. {
  925. triangles.Add(vertices.Count - 1);
  926. triangles.Add(1);
  927. triangles.Add(vertices.Count);
  928. triangles.Add(vertices.Count - 1);
  929. triangles.Add(vertices.Count);
  930. triangles.Add(0);
  931. vertices.Add(quaternion * vertices [vertices.Count - 1]);
  932. }
  933. Mesh mesh = new Mesh();
  934. mesh.vertices = vertices.ToArray();
  935. mesh.triangles = triangles.ToArray();
  936. mesh.RecalculateNormals();
  937. return mesh;
  938. }
  939. private static Mesh CreatePyramidMesh()
  940. {
  941. List<Vector3> vertices = new List<Vector3>();
  942. List<int> triangles = new List<int>();
  943. vertices.Add(new Vector3(-0.5f, 0f, -0.5f));
  944. vertices.Add(new Vector3(0.5f, 0f, -0.5f));
  945. vertices.Add(new Vector3(0.5f, 0f, 0.5f));
  946. vertices.Add(new Vector3(-0.5f, 0f, -0.5f));
  947. vertices.Add(new Vector3(0.5f, 0f, 0.5f));
  948. vertices.Add(new Vector3(-0.5f, 0f, 0.5f));
  949. vertices.Add(new Vector3(-0.5f, 0f, -0.5f));
  950. vertices.Add(new Vector3(0f, 1f, 0f));
  951. vertices.Add(new Vector3(0.5f, 0f, -0.5f));
  952. vertices.Add(new Vector3(0.5f, 0f, -0.5f));
  953. vertices.Add(new Vector3(0f, 1f, 0f));
  954. vertices.Add(new Vector3(0.5f, 0f, 0.5f));
  955. vertices.Add(new Vector3(0.5f, 0f, 0.5f));
  956. vertices.Add(new Vector3(0f, 1f, 0f));
  957. vertices.Add(new Vector3(-0.5f, 0f, 0.5f));
  958. vertices.Add(new Vector3(-0.5f, 0f, 0.5f));
  959. vertices.Add(new Vector3(0f, 1f, 0f));
  960. vertices.Add(new Vector3(-0.5f, 0f, -0.5f));
  961. for (int i = 0; i < 18; i++)
  962. {
  963. triangles.Add(i);
  964. }
  965. Mesh mesh = new Mesh();
  966. mesh.vertices = vertices.ToArray();
  967. mesh.triangles = triangles.ToArray();
  968. mesh.RecalculateNormals();
  969. return mesh;
  970. }
  971. private static Mesh CreateBoneMesh()
  972. {
  973. float size = 1f / 7f;
  974. List<Vector3> vertices = new List<Vector3>();
  975. List<int> triangles = new List<int>();
  976. vertices.Add(new Vector3(-size, -size, 0.200f));
  977. vertices.Add(new Vector3(-size, size, 0.200f));
  978. vertices.Add(new Vector3(0.000f, 0.000f, 0.000f));
  979. vertices.Add(new Vector3(size, size, 0.200f));
  980. vertices.Add(new Vector3(0.000f, 0.000f, 1.000f));
  981. vertices.Add(new Vector3(size, -size, 0.200f));
  982. vertices.Add(new Vector3(-size, size, 0.200f));
  983. vertices.Add(new Vector3(-size, -size, 0.200f));
  984. vertices.Add(new Vector3(0.000f, 0.000f, 1.000f));
  985. vertices.Add(new Vector3(size, -size, 0.200f));
  986. vertices.Add(new Vector3(0.000f, 0.000f, 1.000f));
  987. vertices.Add(new Vector3(-size, -size, 0.200f));
  988. vertices.Add(new Vector3(size, size, 0.200f));
  989. vertices.Add(new Vector3(-size, size, 0.200f));
  990. vertices.Add(new Vector3(0.000f, 0.000f, 1.000f));
  991. vertices.Add(new Vector3(size, size, 0.200f));
  992. vertices.Add(new Vector3(size, -size, 0.200f));
  993. vertices.Add(new Vector3(0.000f, 0.000f, 0.000f));
  994. vertices.Add(new Vector3(size, size, 0.200f));
  995. vertices.Add(new Vector3(0.000f, 0.000f, 0.000f));
  996. vertices.Add(new Vector3(-size, size, 0.200f));
  997. vertices.Add(new Vector3(size, -size, 0.200f));
  998. vertices.Add(new Vector3(-size, -size, 0.200f));
  999. vertices.Add(new Vector3(0.000f, 0.000f, 0.000f));
  1000. for (int i = 0; i < 24; i++)
  1001. {
  1002. triangles.Add(i);
  1003. }
  1004. Mesh mesh = new Mesh();
  1005. mesh.vertices = vertices.ToArray();
  1006. mesh.triangles = triangles.ToArray();
  1007. mesh.RecalculateNormals();
  1008. return mesh;
  1009. }
  1010. private static Vector3[] CreateCircleWire(int resolution)
  1011. {
  1012. List<Vector3> points = new List<Vector3>();
  1013. float step = 360.0f / (float)resolution;
  1014. for (int i = 0; i < resolution; i++)
  1015. {
  1016. points.Add(Quaternion.Euler(0f, 0f, i * step) * new Vector3(0f, 0.5f, 0f));
  1017. points.Add(Quaternion.Euler(0f, 0f, (i + 1) * step) * new Vector3(0f, 0.5f, 0f));
  1018. }
  1019. return points.ToArray();
  1020. }
  1021. private static Vector3[] CreateQuadWire()
  1022. {
  1023. List<Vector3> points = new List<Vector3>();
  1024. points.Add(new Vector3(-0.5f, -0.5f, 0f));
  1025. points.Add(new Vector3(0.5f, -0.5f, 0f));
  1026. points.Add(new Vector3(0.5f, -0.5f, 0f));
  1027. points.Add(new Vector3(0.5f, 0.5f, 0f));
  1028. points.Add(new Vector3(0.5f, 0.5f, 0f));
  1029. points.Add(new Vector3(-0.5f, 0.5f, 0f));
  1030. points.Add(new Vector3(-0.5f, 0.5f, 0f));
  1031. points.Add(new Vector3(-0.5f, -0.5f, 0f));
  1032. return points.ToArray();
  1033. }
  1034. private static Vector3[] CreatePlaneWire()
  1035. {
  1036. List<Vector3> points = new List<Vector3>();
  1037. points.Add(new Vector3(-0.5f, 0, -0.5f));
  1038. points.Add(new Vector3(0.5f, 0, -0.5f));
  1039. points.Add(new Vector3(0.5f, 0, -0.5f));
  1040. points.Add(new Vector3(0.5f, 0, 0.5f));
  1041. points.Add(new Vector3(0.5f, 0, 0.5f));
  1042. points.Add(new Vector3(-0.5f, 0, 0.5f));
  1043. points.Add(new Vector3(-0.5f, 0, 0.5f));
  1044. points.Add(new Vector3(-0.5f, 0, -0.5f));
  1045. return points.ToArray();
  1046. }
  1047. private static Vector3[] CreateCubeWire()
  1048. {
  1049. float size = 1f;
  1050. Vector3 A = new Vector3(-size / 2f, -size / 2f, -size / 2f);
  1051. Vector3 B = new Vector3(size / 2f, -size / 2f, -size / 2f);
  1052. Vector3 C = new Vector3(-size / 2f, -size / 2f, size / 2f);
  1053. Vector3 D = new Vector3(size / 2f, -size / 2f, size / 2f);
  1054. Vector3 p1 = A;
  1055. Vector3 p2 = B;
  1056. Vector3 p3 = C;
  1057. Vector3 p4 = D;
  1058. Vector3 p5 = -D;
  1059. Vector3 p6 = -C;
  1060. Vector3 p7 = -B;
  1061. Vector3 p8 = -A;
  1062. List<Vector3> points = new List<Vector3>();
  1063. points.Add(p1);
  1064. points.Add(p2);
  1065. points.Add(p2);
  1066. points.Add(p4);
  1067. points.Add(p4);
  1068. points.Add(p3);
  1069. points.Add(p3);
  1070. points.Add(p1);
  1071. points.Add(p5);
  1072. points.Add(p6);
  1073. points.Add(p6);
  1074. points.Add(p8);
  1075. points.Add(p5);
  1076. points.Add(p7);
  1077. points.Add(p7);
  1078. points.Add(p8);
  1079. points.Add(p1);
  1080. points.Add(p5);
  1081. points.Add(p2);
  1082. points.Add(p6);
  1083. points.Add(p3);
  1084. points.Add(p7);
  1085. points.Add(p4);
  1086. points.Add(p8);
  1087. return points.ToArray();
  1088. }
  1089. private static Vector3[] CreateSphereWire(int resolution)
  1090. {
  1091. List<Vector3> points = new List<Vector3>();
  1092. float step = 360.0f / (float)resolution;
  1093. for (int i = 0; i < resolution; i++)
  1094. {
  1095. points.Add(Quaternion.Euler(0f, 0f, i * step) * new Vector3(0f, 0.5f, 0f));
  1096. points.Add(Quaternion.Euler(0f, 0f, (i + 1) * step) * new Vector3(0f, 0.5f, 0f));
  1097. }
  1098. for (int i = 0; i < resolution; i++)
  1099. {
  1100. points.Add(Quaternion.Euler(0f, i * step, 0f) * new Vector3(0f, 0f, 0.5f));
  1101. points.Add(Quaternion.Euler(0f, (i + 1) * step, 0f) * new Vector3(0f, 0f, 0.5f));
  1102. }
  1103. for (int i = 0; i < resolution; i++)
  1104. {
  1105. points.Add(Quaternion.Euler(i * step, 0f, i * step) * new Vector3(0f, 0f, 0.5f));
  1106. points.Add(Quaternion.Euler((i + 1) * step, 0f, (i + 1) * step) * new Vector3(0f, 0f, 0.5f));
  1107. }
  1108. return points.ToArray();
  1109. }
  1110. private static Vector3[] CreateCylinderWire(int resolution)
  1111. {
  1112. List<Vector3> points = new List<Vector3>();
  1113. float step = 360.0f / (float)resolution;
  1114. for (int i = 0; i < resolution; i++)
  1115. {
  1116. points.Add(Quaternion.Euler(0f, i * step, 0f) * new Vector3(0f, 0f, 0.5f) + new Vector3(0f, 1f, 0f));
  1117. points.Add(Quaternion.Euler(0f, (i + 1) * step, 0f) * new Vector3(0f, 0f, 0.5f) + new Vector3(0f, 1f, 0f));
  1118. }
  1119. for (int i = 0; i < resolution; i++)
  1120. {
  1121. points.Add(Quaternion.Euler(0f, i * step, 0f) * new Vector3(0f, 0f, 0.5f) - new Vector3(0f, 1f, 0f));
  1122. points.Add(Quaternion.Euler(0f, (i + 1) * step, 0f) * new Vector3(0f, 0f, 0.5f) - new Vector3(0f, 1f, 0f));
  1123. }
  1124. points.Add(new Vector3(0f, -1f, -0.5f));
  1125. points.Add(new Vector3(0f, 1f, -0.5f));
  1126. points.Add(new Vector3(0f, -1f, 0.5f));
  1127. points.Add(new Vector3(0f, 1f, 0.5f));
  1128. points.Add(new Vector3(-0.5f, -1f, 0f));
  1129. points.Add(new Vector3(-0.5f, 1f, 0f));
  1130. points.Add(new Vector3(0.5f, -1f, 0f));
  1131. points.Add(new Vector3(0.5f, 1f, 0));
  1132. return points.ToArray();
  1133. }
  1134. private static Vector3[] CreateCapsuleWire(int resolution)
  1135. {
  1136. List<Vector3> points = new List<Vector3>();
  1137. float step = 360.0f / (float)resolution;
  1138. for (int i = -resolution / 4 - 1; i <= resolution / 4; i++)
  1139. {
  1140. points.Add(Quaternion.Euler(0f, 0f, i * step) * new Vector3(0f, 0.5f, 0f) + new Vector3(0f, 0.5f, 0f));
  1141. points.Add(Quaternion.Euler(0f, 0f, (i + 1) * step) * new Vector3(0f, 0.5f, 0f) + new Vector3(0f, 0.5f, 0f));
  1142. }
  1143. for (int i = resolution / 2; i < resolution; i++)
  1144. {
  1145. points.Add(Quaternion.Euler(i * step, 0f, i * step) * new Vector3(0f, 0f, 0.5f) + new Vector3(0f, 0.5f, 0f));
  1146. points.Add(Quaternion.Euler((i + 1) * step, 0f, (i + 1) * step) * new Vector3(0f, 0f, 0.5f) + new Vector3(0f, 0.5f, 0f));
  1147. }
  1148. for (int i = -resolution / 4 - 1; i <= resolution / 4; i++)
  1149. {
  1150. points.Add(Quaternion.Euler(0f, 0f, i * step) * new Vector3(0f, -0.5f, 0f) + new Vector3(0f, -0.5f, 0f));
  1151. points.Add(Quaternion.Euler(0f, 0f, (i + 1) * step) * new Vector3(0f, -0.5f, 0f) + new Vector3(0f, -0.5f, 0f));
  1152. }
  1153. for (int i = resolution / 2; i < resolution; i++)
  1154. {
  1155. points.Add(Quaternion.Euler(i * step, 0f, i * step) * new Vector3(0f, 0f, -0.5f) + new Vector3(0f, -0.5f, 0f));
  1156. points.Add(Quaternion.Euler((i + 1) * step, 0f, (i + 1) * step) * new Vector3(0f, 0f, -0.5f) + new Vector3(0f, -0.5f, 0f));
  1157. }
  1158. points.Add(new Vector3(0f, -0.5f, -0.5f));
  1159. points.Add(new Vector3(0f, 0.5f, -0.5f));
  1160. points.Add(new Vector3(0f, -0.5f, 0.5f));
  1161. points.Add(new Vector3(0f, 0.5f, 0.5f));
  1162. points.Add(new Vector3(-0.5f, -0.5f, 0f));
  1163. points.Add(new Vector3(-0.5f, 0.5f, 0f));
  1164. points.Add(new Vector3(0.5f, -0.5f, 0f));
  1165. points.Add(new Vector3(0.5f, 0.5f, 0));
  1166. return points.ToArray();
  1167. }
  1168. private static Vector3[] CreateConeWire(int resolution)
  1169. {
  1170. List<Vector3> points = new List<Vector3>();
  1171. float step = 360.0f / (float)resolution;
  1172. for (int i = 0; i < resolution; i++)
  1173. {
  1174. points.Add(Quaternion.Euler(0f, i * step, 0f) * new Vector3(0f, 0f, 0.5f));
  1175. points.Add(Quaternion.Euler(0f, (i + 1) * step, 0f) * new Vector3(0f, 0f, 0.5f));
  1176. }
  1177. points.Add(new Vector3(-0.5f, 0f, 0f));
  1178. points.Add(new Vector3(0f, 1f, 0f));
  1179. points.Add(new Vector3(0.5f, 0f, 0f));
  1180. points.Add(new Vector3(0f, 1f, 0f));
  1181. points.Add(new Vector3(0f, 0f, -0.5f));
  1182. points.Add(new Vector3(0f, 1f, 0f));
  1183. points.Add(new Vector3(0f, 0f, 0.5f));
  1184. points.Add(new Vector3(0f, 1f, 0f));
  1185. return points.ToArray();
  1186. }
  1187. private static Vector3[] CreatePyramidWire()
  1188. {
  1189. List<Vector3> points = new List<Vector3>();
  1190. points.Add(new Vector3(-0.5f, 0f, -0.5f));
  1191. points.Add(new Vector3(0.5f, 0f, -0.5f));
  1192. points.Add(new Vector3(0.5f, 0f, -0.5f));
  1193. points.Add(new Vector3(0.5f, 0f, 0.5f));
  1194. points.Add(new Vector3(0.5f, 0f, 0.5f));
  1195. points.Add(new Vector3(-0.5f, 0f, 0.5f));
  1196. points.Add(new Vector3(-0.5f, 0f, 0.5f));
  1197. points.Add(new Vector3(-0.5f, 0f, -0.5f));
  1198. points.Add(new Vector3(-0.5f, 0f, -0.5f));
  1199. points.Add(new Vector3(0f, 1f, 0f));
  1200. points.Add(new Vector3(0.5f, 0f, -0.5f));
  1201. points.Add(new Vector3(0f, 1f, 0f));
  1202. points.Add(new Vector3(-0.5f, 0f, 0.5f));
  1203. points.Add(new Vector3(0f, 1f, 0f));
  1204. points.Add(new Vector3(0.5f, 0f, 0.5f));
  1205. points.Add(new Vector3(0f, 1f, 0f));
  1206. return points.ToArray();
  1207. }
  1208. private static Vector3[] CreateBoneWire()
  1209. {
  1210. float size = 1f / 7f;
  1211. List<Vector3> points = new List<Vector3>();
  1212. points.Add(new Vector3(0.000f, 0.000f, 0.000f));
  1213. points.Add(new Vector3(-size, -size, 0.200f));
  1214. points.Add(new Vector3(0.000f, 0.000f, 0.000f));
  1215. points.Add(new Vector3(size, -size, 0.200f));
  1216. points.Add(new Vector3(0.000f, 0.000f, 0.000f));
  1217. points.Add(new Vector3(-size, size, 0.200f));
  1218. points.Add(new Vector3(0.000f, 0.000f, 0.000f));
  1219. points.Add(new Vector3(size, size, 0.200f));
  1220. points.Add(new Vector3(-size, -size, 0.200f));
  1221. points.Add(new Vector3(0.000f, 0.000f, 1.000f));
  1222. points.Add(new Vector3(size, -size, 0.200f));
  1223. points.Add(new Vector3(0.000f, 0.000f, 1.000f));
  1224. points.Add(new Vector3(-size, size, 0.200f));
  1225. points.Add(new Vector3(0.000f, 0.000f, 1.000f));
  1226. points.Add(new Vector3(size, size, 0.200f));
  1227. points.Add(new Vector3(0.000f, 0.000f, 1.000f));
  1228. points.Add(new Vector3(-size, -size, 0.200f));
  1229. points.Add(new Vector3(size, -size, 0.200f));
  1230. points.Add(new Vector3(size, -size, 0.200f));
  1231. points.Add(new Vector3(size, size, 0.200f));
  1232. points.Add(new Vector3(size, size, 0.200f));
  1233. points.Add(new Vector3(-size, size, 0.200f));
  1234. points.Add(new Vector3(-size, size, 0.200f));
  1235. points.Add(new Vector3(-size, -size, 0.200f));
  1236. return points.ToArray();
  1237. }
  1238. public static Color Transparent(this Color color, float opacity)
  1239. {
  1240. return new Color(color.r, color.g, color.b, Mathf.Clamp(opacity, 0f, 1f));
  1241. }
  1242. public static Color[] GetRainbowColors(int number)
  1243. {
  1244. Color[] colors = new Color[number];
  1245. for (int i = 0; i < number; i++)
  1246. {
  1247. float frequency = 5f / number;
  1248. colors [i].r = Normalise(Mathf.Sin(frequency * i + 0f) * (127f) + 128f, 0f, 255f, 0f, 1f);
  1249. colors [i].g = Normalise(Mathf.Sin(frequency * i + 2f) * (127f) + 128f, 0f, 255f, 0f, 1f);
  1250. colors [i].b = Normalise(Mathf.Sin(frequency * i + 4f) * (127f) + 128f, 0f, 255f, 0f, 1f);
  1251. colors [i].a = 1f;
  1252. }
  1253. return colors;
  1254. }
  1255. public static float Normalise(float value, float valueMin, float valueMax, float resultMin, float resultMax)
  1256. {
  1257. if (valueMax - valueMin != 0f)
  1258. {
  1259. return (value - valueMin) / (valueMax - valueMin) * (resultMax - resultMin) + resultMin;
  1260. } else
  1261. {
  1262. //Not possible to normalise input value.
  1263. return value;
  1264. }
  1265. }
  1266. }
  1267. }