RxDraw.cs 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Ximmerse.XR.Utils
  5. {
  6. /// <summary>
  7. /// GL draw helper.
  8. /// </summary>
  9. public class RxDraw : MonoBehaviour
  10. {
  11. public enum RxDrawAlignment
  12. {
  13. LeftBottom = 0,
  14. Center = 1,
  15. RightTop = 2,
  16. }
  17. public enum RxCameraSpaceType
  18. {
  19. ViewPort = 0,
  20. ScreenSpace = 1,
  21. WorldSpace = 2,
  22. }
  23. private struct RxDrawRequest
  24. {
  25. internal PEDrawType drawType;
  26. internal Vector3 position;
  27. internal Vector3 position2;
  28. internal Quaternion rotation;
  29. internal float scale;
  30. internal Color color;
  31. internal Color wireColor;
  32. internal bool noDepth;
  33. internal Vector3 v3Size;
  34. internal float endTime;
  35. internal Material customMaterial;
  36. internal int customMaterialPass;
  37. internal Vector2Int scaleInt;
  38. internal Mesh meshReference;
  39. internal RxDrawAlignment alignment;
  40. internal Camera targetCamera;
  41. internal RxCameraSpaceType GUISpaceType;
  42. /// <summary>
  43. /// UI Verts : data structure wrap for UI/Text drawing
  44. /// </summary>
  45. internal UIVertex[] uiVerts;
  46. }
  47. private enum PEDrawType
  48. {
  49. Line,
  50. Sphere,
  51. WireSphere,
  52. WiredSphere,
  53. Cube,
  54. WireCube,
  55. WiredCube,
  56. Cuboid,
  57. WireCuboid,
  58. WiredCuboid,
  59. Quad,
  60. WireQuad,
  61. WiredQuad,
  62. TranslateGizmos,
  63. RotationGizmos,
  64. ScalingGizmos,
  65. Plane,
  66. WirePlane,
  67. WiredPlane,
  68. Grids,
  69. Arrow,
  70. Cone,
  71. Cylinder,
  72. Pyramid,
  73. Mesh,
  74. /// <summary>
  75. /// Draw a human skeleton, where original model height = 1m.
  76. /// </summary>
  77. HumanSkeleton,
  78. /// <summary>
  79. /// Draw a human skeleton line, where original model height = 1m
  80. /// </summary>
  81. HumanSkeletonLine,
  82. /// <summary>
  83. /// Draw a human skeleton, where original model width = 1m
  84. /// </summary>
  85. DogSkeleton,
  86. /// <summary>
  87. /// Draw a human skeleton line, where original model width = 1m
  88. /// </summary>
  89. DogSkeletonLine,
  90. MeshWireFrame,
  91. /// <summary>
  92. /// Draws a GUI circle.
  93. /// </summary>
  94. GUICircle,
  95. /// <summary>
  96. /// Draws full screen rect.
  97. /// </summary>
  98. ScreenRect,
  99. /// <summary>
  100. /// Draws 3d text mesh
  101. /// </summary>
  102. TextMesh3D,
  103. WireCone,
  104. }
  105. static List<RxDrawRequest> sDrawRequests = new List<RxDrawRequest>();
  106. static bool m_DepthRendering = false;
  107. public static bool DepthRendering
  108. {
  109. get
  110. {
  111. return m_DepthRendering;
  112. }
  113. set
  114. {
  115. m_DepthRendering = value;
  116. }
  117. }
  118. static TextGenerator sTextGen = null;
  119. static TextGenerator TextGen
  120. {
  121. get
  122. {
  123. if (sTextGen == null)
  124. sTextGen = new TextGenerator();
  125. return sTextGen;
  126. }
  127. }
  128. static List<UIVertex> sTextVertices = new List<UIVertex>();
  129. static List<Vector3> sTextMeshVertices = new List<Vector3>();
  130. static List<Vector3> sTextMeshNormals = new List<Vector3>();
  131. static List<Vector4> sTextMeshTangents = new List<Vector4>();
  132. static List<int> sTextMeshTris = new List<int>();
  133. static List<Vector2> sTextMeshUVs = new List<Vector2>();
  134. static List<Color> sTextMeshColor = new List<Color>();
  135. static Material matUI = null;
  136. static Material MaterialUI
  137. {
  138. get
  139. {
  140. if (matUI == null)
  141. matUI = new Material(Shader.Find("UI/Default"));
  142. return matUI;
  143. }
  144. }
  145. static Mesh sTextMesh = null;
  146. static Mesh TextMesh
  147. {
  148. get
  149. {
  150. if (sTextMesh == null)
  151. {
  152. sTextMesh = new Mesh();
  153. sTextMesh.name = "PEDrawTextMesh";
  154. TextMesh.MarkDynamic();
  155. }
  156. return sTextMesh;
  157. }
  158. }
  159. Coroutine clearQueueCoroutine = null;
  160. private void OnEnable()
  161. {
  162. clearQueueCoroutine = StartCoroutine(ClearQueue());
  163. }
  164. private void OnDisable()
  165. {
  166. if(clearQueueCoroutine != null)
  167. {
  168. StopCoroutine(clearQueueCoroutine);
  169. clearQueueCoroutine = null;
  170. }
  171. }
  172. /// <summary>
  173. /// Sets the custom material for previous draw command.
  174. /// </summary>
  175. /// <param name="CustomMaterial">Custom material.</param>
  176. /// <param name="Pass">Pass.</param>
  177. public static void SetCustomMaterial(Material CustomMaterial, int Pass = 0)
  178. {
  179. if (sDrawRequests.Count == 0)
  180. {
  181. Debug.Log("Draw command queue is empty, can't set custom material.");
  182. return;
  183. }
  184. var drawCommand = sDrawRequests[sDrawRequests.Count - 1];
  185. drawCommand.customMaterial = CustomMaterial;
  186. drawCommand.customMaterialPass = Pass;
  187. sDrawRequests[sDrawRequests.Count - 1] = drawCommand;
  188. }
  189. /// <summary>
  190. /// Draws a line.
  191. /// </summary>
  192. /// <param name="start">Start.</param>
  193. /// <param name="end">End.</param>
  194. /// <param name="color">Color.</param>
  195. public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration = 0)
  196. {
  197. //For editor call, direct draw:
  198. if (Application.isEditor)
  199. {
  200. UltiDraw.SetDepthRendering(DepthRendering);
  201. UltiDraw.Begin();
  202. UltiDraw.DrawLine(start, end, color);
  203. UltiDraw.End();
  204. }
  205. if (!Application.isPlaying) return;
  206. sDrawRequests.Add(new RxDrawRequest()
  207. {
  208. drawType = PEDrawType.Line,
  209. position = start,
  210. position2 = end,
  211. color = color,
  212. endTime = Time.time + duration,
  213. }
  214. );
  215. if (singleton == null)
  216. {
  217. InitializeBehavior();
  218. }
  219. }
  220. /// <summary>
  221. /// Draws a line.
  222. /// </summary>
  223. /// <param name="start">Start.</param>
  224. /// <param name="end">End.</param>
  225. /// <param name="color">Color.</param>
  226. public static void DrawLine(Vector3 start, Vector3 end, Color color, float lineWidth, float duration = 0)
  227. {
  228. //For editor call, direct draw:
  229. if (Application.isEditor)
  230. {
  231. UltiDraw.SetDepthRendering(DepthRendering);
  232. UltiDraw.Begin();
  233. UltiDraw.DrawLine(start, end, lineWidth, color);
  234. UltiDraw.End();
  235. }
  236. if (!Application.isPlaying) return;
  237. sDrawRequests.Add(new RxDrawRequest()
  238. {
  239. drawType = PEDrawType.Line,
  240. position = start,
  241. position2 = end,
  242. color = color,
  243. scale = lineWidth,
  244. endTime = Time.time + duration,
  245. }
  246. );
  247. if (singleton == null)
  248. {
  249. InitializeBehavior();
  250. }
  251. }
  252. /// <summary>
  253. /// Draws a ray.
  254. /// </summary>
  255. /// <param name="start">Start.</param>
  256. /// <param name="direction">Direction.</param>
  257. /// <param name="color">Color.</param>
  258. public static void DrawRay(Vector3 start, Vector3 direction, Color color, float duration = 0)
  259. {
  260. //For editor call, direct draw:
  261. if (Application.isEditor)
  262. {
  263. UltiDraw.SetDepthRendering(DepthRendering);
  264. UltiDraw.Begin();
  265. UltiDraw.DrawLine(start, start + direction, color);
  266. UltiDraw.End();
  267. }
  268. if (!Application.isPlaying) return;
  269. sDrawRequests.Add(new RxDrawRequest()
  270. {
  271. drawType = PEDrawType.Line,
  272. position = start,
  273. position2 = start + direction,
  274. color = color,
  275. endTime = Time.time + duration,
  276. }
  277. );
  278. if (singleton == null)
  279. {
  280. InitializeBehavior();
  281. }
  282. }
  283. /// <summary>
  284. /// Draws a ray.
  285. /// </summary>
  286. /// <param name="start">Start.</param>
  287. /// <param name="direction">Direction.</param>
  288. /// <param name="color">Color.</param>
  289. public static void DrawRay(Vector3 start, Vector3 direction, Color color, float lineWidth, float duration = 0)
  290. {
  291. //For editor call, direct draw:
  292. if (Application.isEditor)
  293. {
  294. UltiDraw.SetDepthRendering(DepthRendering);
  295. UltiDraw.Begin();
  296. UltiDraw.DrawLine(start, start + direction, lineWidth, color);
  297. UltiDraw.End();
  298. }
  299. if (!Application.isPlaying) return;
  300. sDrawRequests.Add(new RxDrawRequest()
  301. {
  302. drawType = PEDrawType.Line,
  303. position = start,
  304. position2 = start + direction,
  305. color = color,
  306. scale = lineWidth,
  307. endTime = Time.time + duration,
  308. }
  309. );
  310. if (singleton == null)
  311. {
  312. InitializeBehavior();
  313. }
  314. }
  315. /// <summary>
  316. /// Draws a sphere.
  317. /// </summary>
  318. /// <param name="center">Center.</param>
  319. /// <param name="radius">Radius.</param>
  320. /// <param name="color">Color.</param>
  321. public static void DrawSphere(Vector3 center, float radius, Color color, float duration = 0)
  322. {
  323. //For editor call, direct draw:
  324. if (Application.isEditor)
  325. {
  326. UltiDraw.SetDepthRendering(DepthRendering);
  327. UltiDraw.Begin();
  328. UltiDraw.DrawSphere(center, Quaternion.identity, radius, color);
  329. UltiDraw.End();
  330. }
  331. if (!Application.isPlaying) return;
  332. sDrawRequests.Add(new RxDrawRequest()
  333. {
  334. drawType = PEDrawType.Sphere,
  335. position = center,
  336. scale = radius,
  337. color = color,
  338. endTime = Time.time + duration,
  339. }
  340. );
  341. if (singleton == null)
  342. {
  343. InitializeBehavior();
  344. }
  345. }
  346. /// <summary>
  347. /// Draws a wire sphere.
  348. /// </summary>
  349. /// <param name="center">Center.</param>
  350. /// <param name="radius">Radius.</param>
  351. /// <param name="color">Color.</param>
  352. public static void DrawWireSphere(Vector3 center, float radius, Color wireColor, float duration = 0)
  353. {
  354. //For editor call, direct draw:
  355. if (Application.isEditor)
  356. {
  357. UltiDraw.SetDepthRendering(DepthRendering);
  358. UltiDraw.Begin();
  359. UltiDraw.DrawWireSphere(center, Quaternion.identity, radius, wireColor);
  360. UltiDraw.End();
  361. }
  362. if (!Application.isPlaying) return;
  363. sDrawRequests.Add(new RxDrawRequest()
  364. {
  365. drawType = PEDrawType.WireSphere,
  366. position = center,
  367. scale = radius,
  368. wireColor = wireColor,
  369. endTime = Time.time + duration,
  370. }
  371. );
  372. if (singleton == null)
  373. {
  374. InitializeBehavior();
  375. }
  376. }
  377. /// <summary>
  378. /// Draws a sphere with wired.
  379. /// </summary>
  380. /// <param name="center">Center.</param>
  381. /// <param name="radius">Radius.</param>
  382. /// <param name="color">Color.</param>
  383. public static void DrawWiredSphere(Vector3 center, float radius, Color color, Color wireColor, float duration = 0)
  384. {
  385. //For editor call, direct draw:
  386. if (Application.isEditor)
  387. {
  388. UltiDraw.SetDepthRendering(DepthRendering);
  389. UltiDraw.Begin();
  390. UltiDraw.DrawWiredSphere(center, Quaternion.identity, radius, color, wireColor);
  391. UltiDraw.End();
  392. }
  393. if (!Application.isPlaying) return;
  394. sDrawRequests.Add(new RxDrawRequest()
  395. {
  396. drawType = PEDrawType.WiredSphere,
  397. position = center,
  398. scale = radius,
  399. color = color,
  400. wireColor = wireColor,
  401. endTime = Time.time + duration,
  402. }
  403. );
  404. if (singleton == null)
  405. {
  406. InitializeBehavior();
  407. }
  408. }
  409. /// <summary>
  410. /// Draws an arrow.
  411. /// </summary>
  412. /// <param name="center">Center.</param>
  413. /// <param name="radius">Radius.</param>
  414. /// <param name="color">Color.</param>
  415. public static void DrawArrow(Vector3 center, Quaternion rotation, float tipPivot, float shaftWidth, float tipWidth, Color color, float duration = 0)
  416. {
  417. //For editor call, direct draw:
  418. if (Application.isEditor)
  419. {
  420. UltiDraw.SetDepthRendering(DepthRendering);
  421. UltiDraw.Begin();
  422. UltiDraw.DrawArrow(center, rotation * Vector3.forward, tipPivot, shaftWidth, tipWidth, color);
  423. UltiDraw.End();
  424. }
  425. if (!Application.isPlaying) return;
  426. sDrawRequests.Add(new RxDrawRequest()
  427. {
  428. drawType = PEDrawType.Arrow,
  429. position = center,
  430. rotation = rotation,
  431. v3Size = new Vector3(tipPivot, shaftWidth, tipWidth),
  432. color = color,
  433. endTime = Time.time + duration,
  434. }
  435. );
  436. if (singleton == null)
  437. {
  438. InitializeBehavior();
  439. }
  440. }
  441. /// <summary>
  442. /// Draws mesh directly
  443. /// </summary>
  444. /// <param name="center">Center.</param>
  445. /// <param name="radius">Radius.</param>
  446. /// <param name="color">Color.</param>
  447. public static void DrawMesh(Mesh m, Vector3 center, Quaternion rotation, Vector3 scale, Color color, float duration = 0)
  448. {
  449. //For editor call, direct draw:
  450. if (Application.isEditor)
  451. {
  452. UltiDraw.SetDepthRendering(DepthRendering);
  453. UltiDraw.Begin();
  454. UltiDraw.DrawMesh(m, center, rotation, scale, color);
  455. UltiDraw.End();
  456. }
  457. if (!Application.isPlaying) return;
  458. sDrawRequests.Add(new RxDrawRequest()
  459. {
  460. drawType = PEDrawType.Mesh,
  461. position = center,
  462. rotation = rotation,
  463. v3Size = scale,
  464. color = color,
  465. endTime = Time.time + duration,
  466. meshReference = m,
  467. }
  468. );
  469. if (singleton == null)
  470. {
  471. InitializeBehavior();
  472. }
  473. }
  474. /// <summary>
  475. /// Draws mesh wire frame
  476. /// </summary>
  477. /// <param name="center">Center.</param>
  478. /// <param name="radius">Radius.</param>
  479. /// <param name="color">Color.</param>
  480. public static void DrawMeshWireframe(Mesh m, Vector3 center, Quaternion rotation, Vector3 scale, Color color, float duration = 0)
  481. {
  482. //For editor call, direct draw:
  483. if (Application.isEditor)
  484. {
  485. UltiDraw.SetDepthRendering(DepthRendering);
  486. UltiDraw.Begin();
  487. UltiDrawExt.DrawMeshWireFrame(center, rotation, scale, m, color);
  488. UltiDraw.End();
  489. }
  490. if (!Application.isPlaying) return;
  491. sDrawRequests.Add(new RxDrawRequest()
  492. {
  493. drawType = PEDrawType.MeshWireFrame,
  494. position = center,
  495. rotation = rotation,
  496. v3Size = scale,
  497. color = color,
  498. endTime = Time.time + duration,
  499. meshReference = m,
  500. }
  501. );
  502. if (singleton == null)
  503. {
  504. InitializeBehavior();
  505. }
  506. }
  507. /// <summary>
  508. /// Draws a cube.
  509. /// </summary>
  510. /// <param name="center">Center.</param>
  511. /// <param name="radius">Radius.</param>
  512. /// <param name="color">Color.</param>
  513. public static void DrawCube(Vector3 center, Quaternion rotation, float size, Color color, float duration = 0)
  514. {
  515. //For editor call, direct draw:
  516. if (Application.isEditor)
  517. {
  518. UltiDraw.SetDepthRendering(DepthRendering);
  519. UltiDraw.Begin();
  520. UltiDraw.DrawCube(center, rotation, size, color);
  521. UltiDraw.End();
  522. }
  523. if (!Application.isPlaying) return;
  524. sDrawRequests.Add(new RxDrawRequest()
  525. {
  526. drawType = PEDrawType.Cube,
  527. position = center,
  528. rotation = rotation,
  529. scale = size,
  530. color = color,
  531. endTime = Time.time + duration,
  532. }
  533. );
  534. if (singleton == null)
  535. {
  536. InitializeBehavior();
  537. }
  538. }
  539. /// <summary>
  540. /// Draws a cone.
  541. /// </summary>
  542. /// <param name="center">Center.</param>
  543. /// <param name="radius">Radius.</param>
  544. /// <param name="color">Color.</param>
  545. public static void DrawCone(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
  546. {
  547. //For editor call, direct draw:
  548. if (Application.isEditor)
  549. {
  550. UltiDraw.SetDepthRendering(DepthRendering);
  551. UltiDraw.Begin();
  552. UltiDraw.DrawCone(center, rotation, width, height, color);
  553. UltiDraw.End();
  554. }
  555. if (!Application.isPlaying) return;
  556. sDrawRequests.Add(new RxDrawRequest()
  557. {
  558. drawType = PEDrawType.Cone,
  559. position = center,
  560. rotation = rotation,
  561. v3Size = new Vector3(width, height),
  562. color = color,
  563. endTime = Time.time + duration,
  564. }
  565. );
  566. if (singleton == null)
  567. {
  568. InitializeBehavior();
  569. }
  570. }
  571. /// <summary>
  572. /// Draws a wire cone.
  573. /// </summary>
  574. public static void DrawWireCone(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
  575. {
  576. //For editor call, direct draw:
  577. if (Application.isEditor)
  578. {
  579. UltiDraw.SetDepthRendering(DepthRendering);
  580. UltiDraw.Begin();
  581. UltiDraw.DrawWireCone(center, rotation, width, height, color);
  582. UltiDraw.End();
  583. }
  584. if (!Application.isPlaying) return;
  585. sDrawRequests.Add(new RxDrawRequest()
  586. {
  587. drawType = PEDrawType.WireCone,
  588. position = center,
  589. rotation = rotation,
  590. v3Size = new Vector3(width, height),
  591. wireColor = color,
  592. endTime = Time.time + duration,
  593. }
  594. );
  595. if (singleton == null)
  596. {
  597. InitializeBehavior();
  598. }
  599. }
  600. /// <summary>
  601. /// Draws a cylinder.
  602. /// </summary>
  603. /// <param name="center">Center.</param>
  604. /// <param name="radius">Radius.</param>
  605. /// <param name="color">Color.</param>
  606. public static void DrawCylinder(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
  607. {
  608. //For editor call, direct draw:
  609. if (Application.isEditor)
  610. {
  611. UltiDraw.SetDepthRendering(DepthRendering);
  612. UltiDraw.Begin();
  613. UltiDraw.DrawCylinder(center, rotation, width, height, color);
  614. UltiDraw.End();
  615. }
  616. if (!Application.isPlaying) return;
  617. sDrawRequests.Add(new RxDrawRequest()
  618. {
  619. drawType = PEDrawType.Cylinder,
  620. position = center,
  621. rotation = rotation,
  622. v3Size = new Vector3(width, height),
  623. color = color,
  624. endTime = Time.time + duration,
  625. }
  626. );
  627. if (singleton == null)
  628. {
  629. InitializeBehavior();
  630. }
  631. }
  632. /// <summary>
  633. /// Draws a pyramid.
  634. /// </summary>
  635. /// <param name="center">Center.</param>
  636. /// <param name="radius">Radius.</param>
  637. /// <param name="color">Color.</param>
  638. public static void DrawPyramid(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
  639. {
  640. //For editor call, direct draw:
  641. if (Application.isEditor)
  642. {
  643. UltiDraw.SetDepthRendering(DepthRendering);
  644. UltiDraw.Begin();
  645. UltiDraw.DrawPyramid(center, rotation, width, height, color);
  646. UltiDraw.End();
  647. }
  648. if (!Application.isPlaying) return;
  649. sDrawRequests.Add(new RxDrawRequest()
  650. {
  651. drawType = PEDrawType.Pyramid,
  652. position = center,
  653. rotation = rotation,
  654. v3Size = new Vector3(width, height),
  655. color = color,
  656. endTime = Time.time + duration,
  657. }
  658. );
  659. if (singleton == null)
  660. {
  661. InitializeBehavior();
  662. }
  663. }
  664. /// <summary>
  665. /// Draws a wire cube.
  666. /// </summary>
  667. /// <param name="center">Center.</param>
  668. /// <param name="radius">Radius.</param>
  669. /// <param name="color">Color.</param>
  670. public static void DrawWireCube(Vector3 center, Quaternion rotation, float size, Color color, float duration = 0)
  671. {
  672. //For editor call, direct draw:
  673. if (Application.isEditor)
  674. {
  675. UltiDraw.SetDepthRendering(DepthRendering);
  676. UltiDraw.Begin();
  677. UltiDraw.DrawWireCube(center, rotation, size, color);
  678. UltiDraw.End();
  679. }
  680. if (!Application.isPlaying) return;
  681. sDrawRequests.Add(new RxDrawRequest()
  682. {
  683. drawType = PEDrawType.WireCube,
  684. position = center,
  685. rotation = rotation,
  686. scale = size,
  687. wireColor = color,
  688. endTime = Time.time + duration,
  689. }
  690. );
  691. if (singleton == null)
  692. {
  693. InitializeBehavior();
  694. }
  695. }
  696. /// <summary>
  697. /// Draws a wired cube.
  698. /// </summary>
  699. /// <param name="center">Center.</param>
  700. /// <param name="radius">Radius.</param>
  701. /// <param name="color">Color.</param>
  702. public static void DrawWiredCube(Vector3 center, Quaternion rotation, float size, Color color, Color wireColor, float duration = 0)
  703. {
  704. //For editor call, direct draw:
  705. if (Application.isEditor)
  706. {
  707. UltiDraw.SetDepthRendering(DepthRendering);
  708. UltiDraw.Begin();
  709. UltiDraw.DrawWiredCube(center, rotation, size, color, wireColor);
  710. UltiDraw.End();
  711. }
  712. if (!Application.isPlaying) return;
  713. sDrawRequests.Add(new RxDrawRequest()
  714. {
  715. drawType = PEDrawType.WiredCube,
  716. position = center,
  717. rotation = rotation,
  718. scale = size,
  719. wireColor = wireColor,
  720. color = color,
  721. endTime = Time.time + duration,
  722. }
  723. );
  724. if (singleton == null)
  725. {
  726. InitializeBehavior();
  727. }
  728. }
  729. /// <summary>
  730. /// Draws a cuboid.
  731. /// </summary>
  732. /// <param name="center">Center.</param>
  733. /// <param name="radius">Radius.</param>
  734. /// <param name="color">Color.</param>
  735. public static void DrawCuboid(Vector3 center, Quaternion rotation, Vector3 size, Color color, float duration = 0)
  736. {
  737. //For editor call, direct draw:
  738. if (Application.isEditor)
  739. {
  740. UltiDraw.SetDepthRendering(DepthRendering);
  741. UltiDraw.Begin();
  742. UltiDraw.DrawCuboid(center, rotation, size, color);
  743. UltiDraw.End();
  744. }
  745. if (!Application.isPlaying) return;
  746. sDrawRequests.Add(new RxDrawRequest()
  747. {
  748. drawType = PEDrawType.Cuboid,
  749. position = center,
  750. rotation = rotation,
  751. v3Size = size,
  752. color = color,
  753. endTime = Time.time + duration,
  754. }
  755. );
  756. if (singleton == null)
  757. {
  758. InitializeBehavior();
  759. }
  760. }
  761. /// <summary>
  762. /// Draws a wire Cuboid.
  763. /// </summary>
  764. /// <param name="center">Center.</param>
  765. /// <param name="radius">Radius.</param>
  766. /// <param name="color">Color.</param>
  767. public static void DrawWireCuboid(Vector3 center, Quaternion rotation, Vector3 size, Color color, float duration = 0)
  768. {
  769. //For editor call, direct draw:
  770. if (Application.isEditor)
  771. {
  772. UltiDraw.SetDepthRendering(DepthRendering);
  773. UltiDraw.Begin();
  774. UltiDraw.DrawWireCuboid(center, rotation, size, color);
  775. UltiDraw.End();
  776. }
  777. if (!Application.isPlaying) return;
  778. sDrawRequests.Add(new RxDrawRequest()
  779. {
  780. drawType = PEDrawType.WireCuboid,
  781. position = center,
  782. rotation = rotation,
  783. v3Size = size,
  784. wireColor = color,
  785. endTime = Time.time + duration,
  786. }
  787. );
  788. if (singleton == null)
  789. {
  790. InitializeBehavior();
  791. }
  792. }
  793. /// <summary>
  794. /// Draws a wired Cuboid.
  795. /// </summary>
  796. /// <param name="center">Center.</param>
  797. /// <param name="radius">Radius.</param>
  798. /// <param name="color">Color.</param>
  799. public static void DrawWiredCuboid(Vector3 center, Quaternion rotation, Vector3 size, Color color, Color wireColor, float duration = 0)
  800. {
  801. //For editor call, direct draw:
  802. if (Application.isEditor)
  803. {
  804. UltiDraw.SetDepthRendering(DepthRendering);
  805. UltiDraw.Begin();
  806. UltiDraw.DrawWiredCuboid(center, rotation, size, color, wireColor);
  807. UltiDraw.End();
  808. }
  809. if (!Application.isPlaying) return;
  810. sDrawRequests.Add(new RxDrawRequest()
  811. {
  812. drawType = PEDrawType.WiredCuboid,
  813. position = center,
  814. rotation = rotation,
  815. v3Size = size,
  816. wireColor = wireColor,
  817. color = color,
  818. endTime = Time.time + duration,
  819. }
  820. );
  821. if (singleton == null)
  822. {
  823. InitializeBehavior();
  824. }
  825. }
  826. /// <summary>
  827. /// Draws a quad.
  828. /// </summary>
  829. /// <param name="center">Center.</param>
  830. /// <param name="radius">Radius.</param>
  831. /// <param name="color">Color.</param>
  832. public static void DrawQuad(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
  833. {
  834. //For editor call, direct draw:
  835. if (Application.isEditor)
  836. {
  837. UltiDraw.SetDepthRendering(DepthRendering);
  838. UltiDraw.Begin();
  839. UltiDraw.DrawQuad(center, rotation, width, height, color);
  840. UltiDraw.End();
  841. }
  842. if (!Application.isPlaying) return;
  843. sDrawRequests.Add(new RxDrawRequest()
  844. {
  845. drawType = PEDrawType.Quad,
  846. position = center,
  847. rotation = rotation,
  848. v3Size = new Vector3(width, height, 0),
  849. color = color,
  850. endTime = Time.time + duration,
  851. }
  852. );
  853. if (singleton == null)
  854. {
  855. InitializeBehavior();
  856. }
  857. }
  858. /// <summary>
  859. /// Draws a quad of wire.
  860. /// </summary>
  861. /// <param name="center">Center.</param>
  862. /// <param name="radius">Radius.</param>
  863. /// <param name="color">Color.</param>
  864. public static void DrawWireQuad(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
  865. {
  866. //For editor call, direct draw:
  867. if (Application.isEditor)
  868. {
  869. UltiDraw.SetDepthRendering(DepthRendering);
  870. UltiDraw.Begin();
  871. UltiDraw.DrawWireQuad(center, rotation, width, height, color);
  872. UltiDraw.End();
  873. }
  874. if (!Application.isPlaying) return;
  875. sDrawRequests.Add(new RxDrawRequest()
  876. {
  877. drawType = PEDrawType.WireQuad,
  878. position = center,
  879. rotation = rotation,
  880. v3Size = new Vector3(width, height, 0),
  881. wireColor = color,
  882. endTime = Time.time + duration,
  883. }
  884. );
  885. if (singleton == null)
  886. {
  887. InitializeBehavior();
  888. }
  889. }
  890. /// <summary>
  891. /// Draws a wired and filled quad.
  892. /// </summary>
  893. /// <param name="center">Center.</param>
  894. /// <param name="radius">Radius.</param>
  895. /// <param name="color">Color.</param>
  896. public static void DrawWiredQuad(Vector3 center, Quaternion rotation, float width, float height, Color color, Color wireColor, float duration = 0)
  897. {
  898. //For editor call, direct draw:
  899. if (Application.isEditor)
  900. {
  901. UltiDraw.SetDepthRendering(DepthRendering);
  902. UltiDraw.Begin();
  903. UltiDraw.DrawWiredQuad(center, rotation, width, height, color, wireColor);
  904. UltiDraw.End();
  905. }
  906. if (!Application.isPlaying) return;
  907. sDrawRequests.Add(new RxDrawRequest()
  908. {
  909. drawType = PEDrawType.WiredQuad,
  910. position = center,
  911. rotation = rotation,
  912. v3Size = new Vector3(width, height, 0),
  913. color = color,
  914. wireColor = wireColor,
  915. endTime = Time.time + duration,
  916. }
  917. );
  918. if (singleton == null)
  919. {
  920. InitializeBehavior();
  921. }
  922. }
  923. /// <summary>
  924. /// Draws a plane.
  925. /// </summary>
  926. /// <param name="center">Center.</param>
  927. /// <param name="radius">Radius.</param>
  928. /// <param name="color">Color.</param>
  929. public static void DrawPlane(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
  930. {
  931. //For editor call, direct draw:
  932. if (Application.isEditor)
  933. {
  934. UltiDraw.SetDepthRendering(DepthRendering);
  935. UltiDraw.Begin();
  936. UltiDraw.DrawPlane(center, rotation, width, height, color);
  937. UltiDraw.End();
  938. }
  939. if (!Application.isPlaying) return;
  940. sDrawRequests.Add(new RxDrawRequest()
  941. {
  942. drawType = PEDrawType.Plane,
  943. position = center,
  944. rotation = rotation,
  945. v3Size = new Vector3(width, height, 0),
  946. color = color,
  947. endTime = Time.time + duration,
  948. }
  949. );
  950. if (singleton == null)
  951. {
  952. InitializeBehavior();
  953. }
  954. }
  955. /// <summary>
  956. /// Draws a plane of wire.
  957. /// </summary>
  958. /// <param name="center">Center.</param>
  959. /// <param name="radius">Radius.</param>
  960. /// <param name="color">Color.</param>
  961. public static void DrawWirePlane(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
  962. {
  963. //For editor call, direct draw:
  964. if (Application.isEditor)
  965. {
  966. UltiDraw.SetDepthRendering(DepthRendering);
  967. UltiDraw.Begin();
  968. UltiDraw.DrawWirePlane(center, rotation, width, height, color);
  969. UltiDraw.End();
  970. }
  971. if (!Application.isPlaying) return;
  972. sDrawRequests.Add(new RxDrawRequest()
  973. {
  974. drawType = PEDrawType.WirePlane,
  975. position = center,
  976. rotation = rotation,
  977. v3Size = new Vector3(width, height, 0),
  978. wireColor = color,
  979. endTime = Time.time + duration,
  980. }
  981. );
  982. if (singleton == null)
  983. {
  984. InitializeBehavior();
  985. }
  986. }
  987. /// <summary>
  988. /// Draws a wired and filled plane.
  989. /// </summary>
  990. /// <param name="center">Center.</param>
  991. /// <param name="radius">Radius.</param>
  992. /// <param name="color">Color.</param>
  993. public static void DrawWiredPlane(Vector3 center, Quaternion rotation, float width, float height, Color color, Color wireColor, float duration = 0)
  994. {
  995. //For editor call, direct draw:
  996. if (Application.isEditor)
  997. {
  998. UltiDraw.SetDepthRendering(DepthRendering);
  999. UltiDraw.Begin();
  1000. UltiDraw.DrawWiredPlane(center, rotation, width, height, color, wireColor);
  1001. UltiDraw.End();
  1002. }
  1003. if (!Application.isPlaying) return;
  1004. sDrawRequests.Add(new RxDrawRequest()
  1005. {
  1006. drawType = PEDrawType.WiredPlane,
  1007. position = center,
  1008. rotation = rotation,
  1009. v3Size = new Vector3(width, height, 0),
  1010. color = color,
  1011. wireColor = wireColor,
  1012. endTime = Time.time + duration,
  1013. }
  1014. );
  1015. if (singleton == null)
  1016. {
  1017. InitializeBehavior();
  1018. }
  1019. }
  1020. /// <summary>
  1021. /// Draws the translate(positional) gizmos.
  1022. /// </summary>
  1023. /// <param name="position">Position.</param>
  1024. /// <param name="rotation">Rotation.</param>
  1025. /// <param name="scale">Scale.</param>
  1026. public static void DrawTranslateGizmos(Vector3 position, Quaternion rotation, float scale, float duration = 0)
  1027. {
  1028. //For editor call, direct draw:
  1029. if (Application.isEditor)
  1030. {
  1031. UltiDraw.SetDepthRendering(DepthRendering);
  1032. UltiDraw.Begin();
  1033. UltiDraw.DrawTranslateGizmo(position, rotation, scale);
  1034. UltiDraw.End();
  1035. }
  1036. if (!Application.isPlaying) return;
  1037. sDrawRequests.Add(new RxDrawRequest()
  1038. {
  1039. drawType = PEDrawType.TranslateGizmos,
  1040. position = position,
  1041. rotation = rotation,
  1042. scale = scale,
  1043. endTime = Time.time + duration,
  1044. }
  1045. );
  1046. if (singleton == null)
  1047. {
  1048. InitializeBehavior();
  1049. }
  1050. }
  1051. /// <summary>
  1052. /// Draws a 3D text mesh.
  1053. /// </summary>
  1054. /// <param name="position">Position.</param>
  1055. /// <param name="rotation">Rotation.</param>
  1056. /// <param name="scale">Scale.</param>
  1057. /// <param name="text">Text.</param>
  1058. /// <param name="Duration">Duration.</param>
  1059. /// <param name="color">Color.</param>
  1060. /// <param name="font">Font.If null, the default arial is used.</param>
  1061. /// <param name="Style">Style.</param>
  1062. public static void Text3D(Vector3 position, Quaternion rotation, float scale, string text, Color color, float Duration = 0, Font font = null, FontStyle style = default(FontStyle))
  1063. {
  1064. Font drawFont = font != null ? font : Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
  1065. Material drawMaterial = drawFont.material;
  1066. TextGenerationSettings setting = new TextGenerationSettings();
  1067. setting.textAnchor = TextAnchor.MiddleCenter;
  1068. setting.color = color;
  1069. setting.generationExtents = Vector2.zero;
  1070. setting.pivot = new Vector2(0.5f, 0.5f);
  1071. setting.richText = false;
  1072. setting.font = drawFont;
  1073. setting.fontStyle = style;
  1074. setting.fontSize = 1;
  1075. setting.horizontalOverflow = HorizontalWrapMode.Overflow;
  1076. setting.verticalOverflow = VerticalWrapMode.Overflow;
  1077. setting.alignByGeometry = true;
  1078. setting.lineSpacing = 1;
  1079. setting.scaleFactor = 120;
  1080. setting.resizeTextForBestFit = false;
  1081. setting.resizeTextMaxSize = 1;
  1082. setting.resizeTextMinSize = 1;
  1083. TextGen.Invalidate();
  1084. TextGen.Populate(text, setting);
  1085. sTextVertices.Clear();
  1086. // TextGen.GetVertices(sTextVertices);
  1087. UIVertex[] _uiVerts = TextGen.GetVerticesArray();
  1088. float rescaleFactor = scale * 0.01f;
  1089. RxDrawRequest drawTextReq = new RxDrawRequest()
  1090. {
  1091. drawType = PEDrawType.TextMesh3D,
  1092. uiVerts = _uiVerts,
  1093. position = position,
  1094. rotation = rotation,
  1095. scale = rescaleFactor,
  1096. color = color,
  1097. customMaterial = drawMaterial,
  1098. customMaterialPass = 0,
  1099. endTime = Time.time + Duration,
  1100. };
  1101. //For editor call, direct draw:
  1102. if (Application.isEditor)
  1103. {
  1104. UIVerticesToTextMesh(new List<UIVertex>(_uiVerts), TextMesh);
  1105. UltiDraw.SetDepthRendering(DepthRendering);
  1106. UltiDraw.CustomMaterial = drawMaterial;
  1107. UltiDraw.Begin();
  1108. UltiDraw.DrawMesh(TextMesh, position, rotation, Vector3.one * rescaleFactor, setting.color);
  1109. UltiDraw.End();
  1110. UltiDraw.CustomMaterial = null;
  1111. }
  1112. if (!Application.isPlaying) return;
  1113. sDrawRequests.Add(drawTextReq);
  1114. if (singleton == null)
  1115. {
  1116. InitializeBehavior();
  1117. }
  1118. }
  1119. ///// <summary>
  1120. ///// Draws text using default setting:
  1121. ///// alignment = middle center,
  1122. ///// rich text = true,
  1123. ///// Best fit = false,
  1124. ///// Vertical and horizontal wrap mode = Overflow
  1125. ///// </summary>
  1126. ///// <param name="position">Position.</param>
  1127. ///// <param name="rotation">Rotation.</param>
  1128. ///// <param name="scale">Scale.</param>
  1129. ///// <param name="text">Text.</param>
  1130. ///// <param name="setting">Setting.</param>
  1131. ///// <param name="duration">Duration.</param>
  1132. //[System.Obsolete ("Deprecated method, call PEDraw.Text3D()")]
  1133. //public static void DrawText(Vector3 position, Quaternion rotation, Vector3 scale, string text, Font font, FontStyle fontStyle, Color color, float duration = 0)
  1134. //{
  1135. // TextGenerationSettings setting = new TextGenerationSettings();
  1136. // setting.textAnchor = TextAnchor.MiddleCenter;
  1137. // setting.color = color;
  1138. // setting.generationExtents = Vector2.zero;
  1139. // setting.pivot = new Vector2(0.5f, 0.5f);
  1140. // setting.richText = false;
  1141. // setting.font = font;
  1142. // setting.fontStyle = fontStyle;
  1143. // setting.fontSize = 1;
  1144. // setting.horizontalOverflow = HorizontalWrapMode.Overflow;
  1145. // setting.verticalOverflow = VerticalWrapMode.Overflow;
  1146. // setting.alignByGeometry = true;
  1147. // setting.lineSpacing = 1;
  1148. // setting.scaleFactor = 120;
  1149. // setting.resizeTextForBestFit = false;
  1150. // setting.resizeTextMaxSize = 1;
  1151. // setting.resizeTextMinSize = 1;
  1152. // DrawText(position, rotation, scale, text, setting, duration);
  1153. //}
  1154. ///// <summary>
  1155. ///// Draws text using custom setting.
  1156. ///// </summary>
  1157. ///// <param name="position">Position.</param>
  1158. ///// <param name="rotation">Rotation.</param>
  1159. ///// <param name="scale">Scale.</param>
  1160. ///// <param name="text">Text.</param>
  1161. ///// <param name="setting">Setting.</param>
  1162. ///// <param name="color">Color.</param>
  1163. ///// <param name="duration">Duration.</param>
  1164. //[System.Obsolete ("Deprecated method, call PEDraw.Text3D()")]
  1165. //public static void DrawText(Vector3 position, Quaternion rotation, Vector3 scale, string text, TextGenerationSettings setting, float duration = 0)
  1166. //{
  1167. // TextGen.Invalidate();
  1168. // TextGen.Populate(text, setting);
  1169. // sTextVertices.Clear();
  1170. // TextGen.GetVertices(sTextVertices);
  1171. // //Convert UIVertices to Mesh:
  1172. // if (sTextVertices.Count == 0)
  1173. // return;
  1174. // //UIVertice to text mesh:
  1175. // if ((sTextVertices.Count % 4) != 0)
  1176. // {
  1177. // Debug.LogErrorFormat("Error drawing text:{0}. Reason : UIVertices to mesh error: {1} not multiplier of 4 !", text, sTextVertices.Count);
  1178. // return;
  1179. // }
  1180. // if (setting.font == null || setting.font.material == null)
  1181. // {
  1182. // Debug.LogError("Font's invalid !");
  1183. // return;
  1184. // }
  1185. // //Fill up text mesh:
  1186. // UIVerticesToTextMesh(sTextVertices, TextMesh);
  1187. // //For editor call, direct draw:
  1188. // if (Application.isEditor)
  1189. // {
  1190. // UltiDraw.SetDepthRendering(DepthRendering);
  1191. // UltiDraw.CustomMaterial = setting.font.material;
  1192. // UltiDraw.Begin();
  1193. // UltiDraw.DrawMesh(TextMesh, position, rotation, scale * 0.01f, setting.color);
  1194. // UltiDraw.End();
  1195. // UltiDraw.CustomMaterial = null;
  1196. // }
  1197. // if (!Application.isPlaying) return;
  1198. // sDrawRequests.Add(new PEDrawRequest()
  1199. // {
  1200. // drawType = PEDrawType.Mesh,
  1201. // position = position,
  1202. // rotation = rotation,
  1203. // v3Size = scale * 0.01f,
  1204. // endTime = Time.time + duration,
  1205. // meshReference = TextMesh,
  1206. // color = setting.color,
  1207. // customMaterial = setting.font.material,
  1208. // }
  1209. // );
  1210. // if (singleton == null)
  1211. // {
  1212. // InitializeBehavior();
  1213. // }
  1214. //}
  1215. /// <summary>
  1216. /// Convert UI vertices to mesh
  1217. /// </summary>
  1218. /// <param name="verticesLst">Vertices lst.</param>
  1219. /// <param name="TextMesh">Text mesh.</param>
  1220. static void UIVerticesToTextMesh(List<UIVertex> verticesLst, Mesh TextMesh)
  1221. {
  1222. int charCount = (verticesLst.Count / 4);
  1223. sTextMeshVertices.Clear();
  1224. sTextMeshVertices.Capacity = verticesLst.Count;
  1225. sTextMeshTris.Clear();
  1226. sTextMeshTris.Capacity = charCount * 2 * 3;
  1227. sTextMeshUVs.Clear();
  1228. sTextMeshUVs.Capacity = verticesLst.Count;
  1229. sTextMeshColor.Clear();
  1230. sTextMeshColor.Capacity = verticesLst.Count;
  1231. sTextMeshNormals.Clear();
  1232. sTextMeshNormals.Capacity = verticesLst.Count;
  1233. sTextMeshTangents.Clear();
  1234. sTextMeshTangents.Capacity = verticesLst.Count;
  1235. for (int c = 0; c < charCount; c++)
  1236. {
  1237. var c1 = verticesLst[c * 4];
  1238. var c2 = verticesLst[c * 4 + 1];
  1239. var c3 = verticesLst[c * 4 + 2];
  1240. var c4 = verticesLst[c * 4 + 3];
  1241. Vector3 v1 = c1.position;
  1242. Vector3 v2 = c2.position;
  1243. Vector3 v3 = c3.position;
  1244. Vector3 v4 = c4.position;
  1245. sTextMeshVertices.Add(v1);
  1246. sTextMeshVertices.Add(v2);
  1247. sTextMeshVertices.Add(v3);
  1248. sTextMeshVertices.Add(v4);
  1249. int t1 = sTextMeshVertices.Count - 4;
  1250. int t2 = sTextMeshVertices.Count - 3;
  1251. int t3 = sTextMeshVertices.Count - 2;
  1252. int t4 = sTextMeshVertices.Count - 1;
  1253. sTextMeshTris.Add(t1);
  1254. sTextMeshTris.Add(t2);
  1255. sTextMeshTris.Add(t3);
  1256. sTextMeshTris.Add(t1);
  1257. sTextMeshTris.Add(t3);
  1258. sTextMeshTris.Add(t4);
  1259. sTextMeshUVs.Add(c1.uv0);
  1260. sTextMeshUVs.Add(c2.uv0);
  1261. sTextMeshUVs.Add(c3.uv0);
  1262. sTextMeshUVs.Add(c4.uv0);
  1263. sTextMeshColor.Add(c1.color);
  1264. sTextMeshColor.Add(c2.color);
  1265. sTextMeshColor.Add(c3.color);
  1266. sTextMeshColor.Add(c4.color);
  1267. sTextMeshNormals.Add(c1.normal);
  1268. sTextMeshNormals.Add(c2.normal);
  1269. sTextMeshNormals.Add(c3.normal);
  1270. sTextMeshNormals.Add(c4.normal);
  1271. sTextMeshTangents.Add(c1.tangent);
  1272. sTextMeshTangents.Add(c2.tangent);
  1273. sTextMeshTangents.Add(c3.tangent);
  1274. sTextMeshTangents.Add(c4.tangent);
  1275. }
  1276. TextMesh.Clear();
  1277. TextMesh.SetVertices(sTextMeshVertices);
  1278. TextMesh.SetUVs(0, sTextMeshUVs);
  1279. TextMesh.SetColors(sTextMeshColor);
  1280. TextMesh.SetNormals(sTextMeshNormals);
  1281. TextMesh.SetTangents(sTextMeshTangents);
  1282. TextMesh.SetTriangles(sTextMeshTris, 0);
  1283. // TextMesh.UploadMeshData (false);
  1284. }
  1285. /// <summary>
  1286. /// Draws the rotation gizmos.
  1287. /// </summary>
  1288. /// <param name="position">Position.</param>
  1289. /// <param name="rotation">Rotation.</param>
  1290. /// <param name="scale">Scale.</param>
  1291. public static void DrawRotationGizmos(Vector3 position, Quaternion rotation, float scale, float duration = 0)
  1292. {
  1293. //For editor call, direct draw:
  1294. if (Application.isEditor)
  1295. {
  1296. UltiDraw.SetDepthRendering(DepthRendering);
  1297. UltiDraw.Begin();
  1298. UltiDraw.DrawRotateGizmo(position, rotation, scale);
  1299. UltiDraw.End();
  1300. }
  1301. if (!Application.isPlaying) return;
  1302. sDrawRequests.Add(new RxDrawRequest()
  1303. {
  1304. drawType = PEDrawType.RotationGizmos,
  1305. position = position,
  1306. rotation = rotation,
  1307. scale = scale,
  1308. endTime = Time.time + duration,
  1309. }
  1310. );
  1311. if (singleton == null)
  1312. {
  1313. InitializeBehavior();
  1314. }
  1315. }
  1316. /// <summary>
  1317. /// Draws the grid gizmos.
  1318. /// </summary>
  1319. /// <param name="position">Position.</param>
  1320. /// <param name="rotation">Rotation.</param>
  1321. /// <param name="scale">Scale.</param>
  1322. public static void DrawGrids(Vector3 position, Quaternion rotation, int gridX, int gridY,
  1323. float gridXSize, float gridYSize, Color gridColor, float duration = 0, RxDrawAlignment drawAlignment = RxDrawAlignment.LeftBottom)
  1324. {
  1325. //For editor call, direct draw:
  1326. if (Application.isEditor)
  1327. {
  1328. UltiDraw.SetDepthRendering(DepthRendering);
  1329. UltiDraw.Begin();
  1330. switch (drawAlignment)
  1331. {
  1332. case RxDrawAlignment.Center:
  1333. UltiDraw.DrawGrid(position, rotation, gridX, gridY, gridXSize, gridYSize, color: gridColor);
  1334. break;
  1335. case RxDrawAlignment.LeftBottom:
  1336. UltiDraw.DrawGridLBOrigin(position, rotation, gridX, gridY, gridXSize, gridYSize, color: gridColor);
  1337. break;
  1338. case RxDrawAlignment.RightTop:
  1339. UltiDraw.DrawGridRTOrigin(position, rotation, gridX, gridY, gridXSize, gridYSize, color: gridColor);
  1340. break;
  1341. }
  1342. UltiDraw.End();
  1343. }
  1344. if (!Application.isPlaying) return;
  1345. sDrawRequests.Add(new RxDrawRequest()
  1346. {
  1347. drawType = PEDrawType.Grids,
  1348. position = position,
  1349. rotation = rotation,
  1350. scaleInt = new Vector2Int(gridX, gridY),
  1351. v3Size = new Vector3(gridXSize, gridYSize, 0),
  1352. color = gridColor,
  1353. endTime = Time.time + duration,
  1354. alignment = drawAlignment,
  1355. }
  1356. );
  1357. if (singleton == null)
  1358. {
  1359. InitializeBehavior();
  1360. }
  1361. }
  1362. /// <summary>
  1363. /// Draws a human skeleton.
  1364. /// </summary>
  1365. /// <param name="position">Position.</param>
  1366. /// <param name="rotation">Rotation.</param>
  1367. /// <param name="scale">Scale.</param>
  1368. public static void DrawHumanSkeleton(Vector3 position, Quaternion rotation, Vector3 scale, Color color, float duration = 0)
  1369. {
  1370. //For editor call, direct draw:
  1371. if (Application.isEditor)
  1372. {
  1373. UltiDraw.SetDepthRendering(DepthRendering);
  1374. UltiDraw.Begin();
  1375. UltiDrawExt.DrawHumanSkeleton(position, rotation, scale, color);
  1376. UltiDraw.End();
  1377. }
  1378. if (!Application.isPlaying) return;
  1379. sDrawRequests.Add(new RxDrawRequest()
  1380. {
  1381. drawType = PEDrawType.HumanSkeleton,
  1382. position = position,
  1383. rotation = rotation,
  1384. v3Size = scale,
  1385. color = color,
  1386. endTime = Time.time + duration,
  1387. }
  1388. );
  1389. if (singleton == null)
  1390. {
  1391. InitializeBehavior();
  1392. }
  1393. }
  1394. /// <summary>
  1395. /// Draws a human skeleton line.
  1396. /// </summary>
  1397. /// <param name="position">Position.</param>
  1398. /// <param name="rotation">Rotation.</param>
  1399. /// <param name="scale">Scale.</param>
  1400. public static void DrawHumanSkeletonLine(Vector3 position, Quaternion rotation, Vector3 scale, Color color, float duration = 0)
  1401. {
  1402. //For editor call, direct draw:
  1403. if (Application.isEditor)
  1404. {
  1405. UltiDraw.SetDepthRendering(DepthRendering);
  1406. UltiDraw.Begin();
  1407. UltiDrawExt.DrawHumanSkeletonLine(position, rotation, scale, color);
  1408. UltiDraw.End();
  1409. }
  1410. if (!Application.isPlaying) return;
  1411. sDrawRequests.Add(new RxDrawRequest()
  1412. {
  1413. drawType = PEDrawType.HumanSkeletonLine,
  1414. position = position,
  1415. rotation = rotation,
  1416. v3Size = scale,
  1417. color = color,
  1418. endTime = Time.time + duration,
  1419. }
  1420. );
  1421. if (singleton == null)
  1422. {
  1423. InitializeBehavior();
  1424. }
  1425. }
  1426. /// <summary>
  1427. /// Draws a GUI circle at the screen point.
  1428. /// If drawSpace = ViewPort, Position and Radius is measured in screen coordination.
  1429. /// Else If drawSpace = ScreenSpace, Screen point and Radius is measured in pixel coordination.
  1430. /// Else If drawSpace = WorldSpace, Screen point and Radius is measured in 3D global coordination.
  1431. /// </summary>
  1432. /// <param name="ScreenPoint">Screen point.</param>
  1433. /// <param name="Radius">Radius.</param>
  1434. /// <param name="color">Color.</param>
  1435. /// <param name="cameraTarget">Camera target.</param>
  1436. /// <param name="duration">Duration.</param>
  1437. public static void DrawGUICircle(Vector3 Position, float Radius, Color color, Camera cameraTarget = null, float duration = 0)
  1438. {
  1439. if (!Application.isPlaying)
  1440. {
  1441. return;
  1442. }
  1443. var drawReq = new RxDrawRequest()
  1444. {
  1445. drawType = PEDrawType.GUICircle,
  1446. position = Position,
  1447. scale = Radius,
  1448. color = color,
  1449. endTime = Time.time + duration,
  1450. targetCamera = cameraTarget,
  1451. GUISpaceType = RxCameraSpaceType.ScreenSpace,
  1452. };
  1453. ConvertPixelSpace(ref drawReq);
  1454. sDrawRequests.Add(drawReq);
  1455. if (singleton == null)
  1456. {
  1457. InitializeBehavior();
  1458. }
  1459. }
  1460. /// <summary>
  1461. /// Draws a dog skeleton.
  1462. /// </summary>
  1463. /// <param name="position">Position.</param>
  1464. /// <param name="rotation">Rotation.</param>
  1465. /// <param name="scale">Scale.</param>
  1466. public static void DrawDogSkeleton(Vector3 position, Quaternion rotation, Vector3 scale, Color color, float duration = 0)
  1467. {
  1468. //For editor call, direct draw:
  1469. if (Application.isEditor)
  1470. {
  1471. UltiDraw.SetDepthRendering(DepthRendering);
  1472. UltiDraw.Begin();
  1473. UltiDrawExt.DrawDogSkeleton(position, rotation, scale, color);
  1474. UltiDraw.End();
  1475. }
  1476. if (!Application.isPlaying) return;
  1477. sDrawRequests.Add(new RxDrawRequest()
  1478. {
  1479. drawType = PEDrawType.DogSkeleton,
  1480. position = position,
  1481. rotation = rotation,
  1482. v3Size = scale,
  1483. color = color,
  1484. endTime = Time.time + duration,
  1485. }
  1486. );
  1487. if (singleton == null)
  1488. {
  1489. InitializeBehavior();
  1490. }
  1491. }
  1492. /// <summary>
  1493. /// Draws a dog skeleton line.
  1494. /// </summary>
  1495. /// <param name="position">Position.</param>
  1496. /// <param name="rotation">Rotation.</param>
  1497. /// <param name="scale">Scale.</param>
  1498. public static void DrawDogSkeletonLine(Vector3 position, Quaternion rotation, Vector3 scale, Color color, float duration = 0)
  1499. {
  1500. //For editor call, direct draw:
  1501. if (Application.isEditor)
  1502. {
  1503. UltiDraw.SetDepthRendering(DepthRendering);
  1504. UltiDraw.Begin();
  1505. UltiDrawExt.DrawDogSkeletonLine(position, rotation, scale, color);
  1506. UltiDraw.End();
  1507. }
  1508. if (!Application.isPlaying) return;
  1509. sDrawRequests.Add(new RxDrawRequest()
  1510. {
  1511. drawType = PEDrawType.DogSkeletonLine,
  1512. position = position,
  1513. rotation = rotation,
  1514. v3Size = scale,
  1515. color = color,
  1516. endTime = Time.time + duration,
  1517. }
  1518. );
  1519. if (singleton == null)
  1520. {
  1521. InitializeBehavior();
  1522. }
  1523. }
  1524. /// <summary>
  1525. /// Draws a scale gizmos.
  1526. /// </summary>
  1527. /// <param name="position">Position.</param>
  1528. /// <param name="rotation">Rotation.</param>
  1529. /// <param name="scale">Scale.</param>
  1530. public static void DrawScaleGizmos(Vector3 position, Quaternion rotation, float scale, float duration = 0)
  1531. {
  1532. //For editor call, direct draw:
  1533. if (Application.isEditor)
  1534. {
  1535. UltiDraw.SetDepthRendering(DepthRendering);
  1536. UltiDraw.Begin();
  1537. UltiDraw.DrawScaleGizmo(position, rotation, scale);
  1538. UltiDraw.End();
  1539. }
  1540. if (!Application.isPlaying) return;
  1541. sDrawRequests.Add(new RxDrawRequest()
  1542. {
  1543. drawType = PEDrawType.ScalingGizmos,
  1544. position = position,
  1545. rotation = rotation,
  1546. scale = scale,
  1547. endTime = Time.time + duration,
  1548. }
  1549. );
  1550. if (singleton == null)
  1551. {
  1552. InitializeBehavior();
  1553. }
  1554. }
  1555. /// <summary>
  1556. /// Draws a screen rect, when scale = 1, the rect fully covers screen viewport.
  1557. /// </summary>
  1558. /// <param name="color">Color.</param>
  1559. /// <param name="scale">Scale.</param>
  1560. /// <param name="duration">Duration.</param>
  1561. public static void DrawScreenRect (Color Color, float Scale = 1, float Duration = 0)
  1562. {
  1563. sDrawRequests.Add(new RxDrawRequest()
  1564. {
  1565. drawType = PEDrawType.ScreenRect,
  1566. scale = Scale,
  1567. color = Color,
  1568. endTime = Time.time + Duration,
  1569. }
  1570. );
  1571. if (singleton == null)
  1572. {
  1573. InitializeBehavior();
  1574. }
  1575. }
  1576. static RxDraw singleton;
  1577. /// <summary>
  1578. /// Initializes the behavior.
  1579. /// </summary>
  1580. static void InitializeBehavior()
  1581. {
  1582. if (singleton == null)
  1583. {
  1584. singleton = FindObjectOfType<RxDraw>();
  1585. if (!singleton)
  1586. {
  1587. var go = new GameObject("RxDraw");
  1588. singleton = go.AddComponent<RxDraw>();
  1589. //go.hideFlags = HideFlags.HideAndDontSave;
  1590. if (Application.isPlaying)
  1591. {
  1592. DontDestroyOnLoad(go);
  1593. }
  1594. }
  1595. }
  1596. }
  1597. void OnRenderObject()
  1598. {
  1599. if (sDrawRequests.Count > 0)
  1600. {
  1601. UltiDraw.Begin();
  1602. // UltiDraw.SetCurvature(0f);
  1603. UltiDraw.SetDepthRendering(DepthRendering);
  1604. for (int i = sDrawRequests.Count - 1; i >= 0; i--)
  1605. {
  1606. var dReq = sDrawRequests[i];
  1607. // Debug.Log ("Draw: " + dReq.drawType.ToString());
  1608. if (dReq.customMaterial != null)
  1609. {
  1610. UltiDraw.CustomMaterial = dReq.customMaterial;
  1611. UltiDraw.CustomMaterialPass = dReq.customMaterialPass;
  1612. }
  1613. switch (dReq.drawType)
  1614. {
  1615. case PEDrawType.Line:
  1616. if (dReq.scale <= 0)
  1617. {
  1618. UltiDraw.DrawLine(dReq.position, dReq.position2, dReq.color);
  1619. }
  1620. else
  1621. {
  1622. UltiDraw.DrawLine(dReq.position, dReq.position2, dReq.scale, dReq.color);
  1623. }
  1624. break;
  1625. case PEDrawType.Sphere:
  1626. UltiDraw.DrawSphere(dReq.position, Quaternion.identity, dReq.scale, dReq.color);
  1627. break;
  1628. case PEDrawType.WireSphere:
  1629. UltiDraw.DrawWireSphere(dReq.position, Quaternion.identity, dReq.scale, dReq.wireColor);
  1630. break;
  1631. case PEDrawType.WiredSphere:
  1632. UltiDraw.DrawWiredSphere(dReq.position, Quaternion.identity, dReq.scale, dReq.color, dReq.wireColor);
  1633. break;
  1634. case PEDrawType.Cube:
  1635. UltiDraw.DrawCube(dReq.position, dReq.rotation, dReq.scale, dReq.color);
  1636. break;
  1637. case PEDrawType.WireCube:
  1638. UltiDraw.DrawWireCube(dReq.position, dReq.rotation, dReq.scale, dReq.wireColor);
  1639. break;
  1640. case PEDrawType.WiredCube:
  1641. UltiDraw.DrawWiredCube(dReq.position, dReq.rotation, dReq.scale, dReq.color, dReq.wireColor);
  1642. break;
  1643. case PEDrawType.Cuboid:
  1644. UltiDraw.DrawCuboid(dReq.position, dReq.rotation, dReq.v3Size, dReq.color);
  1645. break;
  1646. case PEDrawType.WireCuboid:
  1647. UltiDraw.DrawWireCuboid(dReq.position, dReq.rotation, dReq.v3Size, dReq.wireColor);
  1648. break;
  1649. case PEDrawType.WiredCuboid:
  1650. UltiDraw.DrawWiredCuboid(dReq.position, dReq.rotation, dReq.v3Size, dReq.color, dReq.wireColor);
  1651. break;
  1652. case PEDrawType.Quad:
  1653. UltiDraw.DrawQuad(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
  1654. break;
  1655. case PEDrawType.WireQuad:
  1656. UltiDraw.DrawWireQuad(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.wireColor);
  1657. break;
  1658. case PEDrawType.WiredQuad:
  1659. UltiDraw.DrawWiredQuad(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.color, dReq.wireColor);
  1660. break;
  1661. case PEDrawType.Plane:
  1662. UltiDraw.DrawPlane(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
  1663. break;
  1664. case PEDrawType.WirePlane:
  1665. UltiDraw.DrawWirePlane(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.wireColor);
  1666. break;
  1667. case PEDrawType.WiredPlane:
  1668. UltiDraw.DrawWiredPlane(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.color, dReq.wireColor);
  1669. break;
  1670. case PEDrawType.TranslateGizmos:
  1671. UltiDraw.DrawTranslateGizmo(dReq.position, dReq.rotation, dReq.scale);
  1672. break;
  1673. case PEDrawType.RotationGizmos:
  1674. UltiDraw.DrawRotateGizmo(dReq.position, dReq.rotation, dReq.scale);
  1675. break;
  1676. case PEDrawType.ScalingGizmos:
  1677. UltiDraw.DrawScaleGizmo(dReq.position, dReq.rotation, dReq.scale);
  1678. break;
  1679. case PEDrawType.Grids:
  1680. switch (dReq.alignment)
  1681. {
  1682. case RxDrawAlignment.Center:
  1683. UltiDraw.DrawGrid(dReq.position, dReq.rotation, dReq.scaleInt.x, dReq.scaleInt.y, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
  1684. break;
  1685. case RxDrawAlignment.LeftBottom:
  1686. UltiDraw.DrawGridLBOrigin(dReq.position, dReq.rotation, dReq.scaleInt.x, dReq.scaleInt.y, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
  1687. break;
  1688. case RxDrawAlignment.RightTop:
  1689. UltiDraw.DrawGridRTOrigin(dReq.position, dReq.rotation, dReq.scaleInt.x, dReq.scaleInt.y, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
  1690. break;
  1691. }
  1692. break;
  1693. case PEDrawType.Arrow:
  1694. UltiDraw.DrawArrow(dReq.position, dReq.rotation * Vector3.forward, dReq.v3Size.x, dReq.v3Size.y, dReq.v3Size.z, dReq.color);
  1695. break;
  1696. case PEDrawType.Cone:
  1697. UltiDraw.DrawCone(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
  1698. break;
  1699. case PEDrawType.Cylinder:
  1700. UltiDraw.DrawCylinder(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
  1701. break;
  1702. case PEDrawType.Pyramid:
  1703. UltiDraw.DrawPyramid(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
  1704. break;
  1705. case PEDrawType.Mesh:
  1706. UltiDraw.DrawMesh(dReq.meshReference, dReq.position, dReq.rotation, dReq.v3Size, dReq.color);
  1707. break;
  1708. case PEDrawType.MeshWireFrame:
  1709. UltiDrawExt.DrawMeshWireFrame(dReq.position, dReq.rotation, dReq.v3Size, dReq.meshReference, dReq.color);
  1710. break;
  1711. case PEDrawType.HumanSkeleton:
  1712. UltiDrawExt.DrawHumanSkeleton(dReq.position, dReq.rotation, dReq.v3Size, dReq.color);
  1713. break;
  1714. case PEDrawType.HumanSkeletonLine:
  1715. UltiDrawExt.DrawHumanSkeletonLine(dReq.position, dReq.rotation, dReq.v3Size, dReq.color);
  1716. break;
  1717. case PEDrawType.DogSkeleton:
  1718. UltiDrawExt.DrawDogSkeleton(dReq.position, dReq.rotation, dReq.v3Size, dReq.color);
  1719. break;
  1720. case PEDrawType.DogSkeletonLine:
  1721. UltiDrawExt.DrawDogSkeletonLine(dReq.position, dReq.rotation, dReq.v3Size, dReq.color);
  1722. break;
  1723. case PEDrawType.GUICircle:
  1724. UltiDraw.DrawGUICircle((Vector2)dReq.position, dReq.scale, dReq.color, dReq.targetCamera);
  1725. break;
  1726. case PEDrawType.ScreenRect:
  1727. GL.PushMatrix();
  1728. MaterialUI.SetPass(0);
  1729. MaterialUI.color = dReq.color;
  1730. GL.LoadOrtho();
  1731. GL.Begin(GL.TRIANGLE_STRIP);
  1732. if (dReq.scale != 1)
  1733. {
  1734. GL.Vertex(new Vector3(0.5f - 0.5f * dReq.scale, 0.5f * dReq.scale + 0.5f, 0));
  1735. GL.Vertex(new Vector3(0.5f * dReq.scale + 0.5f, 0.5f * dReq.scale + 0.5f, 0));
  1736. GL.Vertex(new Vector3(0.5f - 0.5f * dReq.scale, 0.5f - 0.5f * dReq.scale, 0));
  1737. GL.Vertex(new Vector3(0.5f * dReq.scale + 0.5f, 0.5f - 0.5f * dReq.scale, 0));
  1738. }
  1739. else
  1740. {
  1741. GL.Vertex(new Vector3(0, 1, 0));
  1742. GL.Vertex(new Vector3(1, 1, 0));
  1743. GL.Vertex(new Vector3(0, 0, 0));
  1744. GL.Vertex(new Vector3(1, 0, 0));
  1745. }
  1746. GL.End();
  1747. GL.PopMatrix();
  1748. break;
  1749. case PEDrawType.TextMesh3D:
  1750. GL.PushMatrix();
  1751. GL.MultMatrix(Matrix4x4.TRS(dReq.position, dReq.rotation, dReq.scale * Vector3.one));
  1752. dReq.customMaterial.SetPass(dReq.customMaterialPass);
  1753. var vLst = dReq.uiVerts;
  1754. int charCount = (vLst.Length / 4); //每个character占据4个vertices
  1755. GL.Begin(GL.TRIANGLES);
  1756. //draw characters by characters
  1757. for (int c = 0; c < charCount; c++)
  1758. {
  1759. //draw triangle first
  1760. {
  1761. var ct11 = vLst[c * 4];
  1762. var ct12 = vLst[c * 4 + 1];
  1763. var ct13 = vLst[c * 4 + 2];
  1764. GL.Color(dReq.color);GL.MultiTexCoord(0,ct11.uv0);GL.Vertex(ct11.position);
  1765. GL.Color(dReq.color);GL.MultiTexCoord(0,ct12.uv0);GL.Vertex(ct12.position);
  1766. GL.Color(dReq.color);GL.MultiTexCoord(0,ct13.uv0);GL.Vertex(ct13.position);
  1767. }
  1768. //draw triangle second
  1769. {
  1770. var ct21 = vLst[c * 4];
  1771. var ct22 = vLst[c * 4 + 2];
  1772. var ct23 = vLst[c * 4 + 3];
  1773. GL.Color(dReq.color);GL.TexCoord(ct21.uv0);GL.Vertex(ct21.position);
  1774. GL.Color(dReq.color);GL.TexCoord(ct22.uv0);GL.Vertex(ct22.position);
  1775. GL.Color(dReq.color);GL.TexCoord(ct23.uv0);GL.Vertex(ct23.position);
  1776. }
  1777. }
  1778. GL.End();
  1779. GL.PopMatrix();
  1780. break;
  1781. case PEDrawType.WireCone:
  1782. UltiDraw.DrawWireCone(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.wireColor);
  1783. break;
  1784. default:
  1785. break;
  1786. }
  1787. UltiDraw.CustomMaterial = null;//Clear after draw
  1788. }
  1789. UltiDraw.End();
  1790. }
  1791. }
  1792. /// <summary>
  1793. /// Converts the position from screen normalize space to pixel space.
  1794. /// </summary>
  1795. /// <param name="drawRequest">Draw request.</param>
  1796. static void ConvertPixelSpace(ref RxDrawRequest drawRequest)
  1797. {
  1798. var _Camera = drawRequest.targetCamera ?? Camera.main;
  1799. if (_Camera)
  1800. {
  1801. if (drawRequest.GUISpaceType == RxCameraSpaceType.ScreenSpace)
  1802. {
  1803. drawRequest.position = _Camera.ScreenToViewportPoint(drawRequest.position);
  1804. }
  1805. else if (drawRequest.GUISpaceType == RxCameraSpaceType.WorldSpace)
  1806. {
  1807. drawRequest.position = _Camera.WorldToViewportPoint(drawRequest.position);
  1808. }
  1809. }
  1810. }
  1811. void OnDestroy()
  1812. {
  1813. sDrawRequests.Clear();
  1814. if (singleton)
  1815. {
  1816. if (Application.isEditor && Application.isPlaying == false)
  1817. {
  1818. DestroyImmediate(singleton.gameObject);
  1819. }
  1820. else
  1821. {
  1822. Destroy(singleton.gameObject);
  1823. }
  1824. }
  1825. singleton = null;
  1826. }
  1827. IEnumerator ClearQueue ()
  1828. {
  1829. var waitEndFrame = new WaitForEndOfFrame();
  1830. while (true)
  1831. {
  1832. yield return waitEndFrame;
  1833. if (sDrawRequests.Count > 0)
  1834. {
  1835. for (int i = sDrawRequests.Count - 1; i >= 0; i--)
  1836. {
  1837. if (sDrawRequests[i].endTime < Time.time)
  1838. {
  1839. sDrawRequests.RemoveAt(i);
  1840. }
  1841. }
  1842. }
  1843. }
  1844. }
  1845. }
  1846. }