RuntimeGizmoDrawer.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Leap.Unity.RuntimeGizmos
  5. {
  6. public class RuntimeGizmoDrawer
  7. {
  8. public Shader gizmoShader
  9. {
  10. get
  11. {
  12. if (this._gizmoMaterial == null)
  13. {
  14. return null;
  15. }
  16. return this._gizmoMaterial.shader;
  17. }
  18. set
  19. {
  20. if (this._gizmoMaterial == null)
  21. {
  22. this._gizmoMaterial = new Material(value);
  23. this._gizmoMaterial.name = "Runtime Gizmo Material";
  24. this._gizmoMaterial.hideFlags = HideFlags.HideAndDontSave;
  25. }
  26. else
  27. {
  28. this._gizmoMaterial.shader = value;
  29. }
  30. }
  31. }
  32. public void BeginGuard()
  33. {
  34. this._operationCountOnGuard = this._operations.Count;
  35. }
  36. public void EndGuard()
  37. {
  38. bool flag = this._operations.Count > this._operationCountOnGuard;
  39. this._operationCountOnGuard = -1;
  40. if (flag)
  41. {
  42. Debug.LogError("New gizmos were drawn to the front buffer! Make sure to never keep a reference to a Drawer, always get a new one every time you want to start drawing.");
  43. }
  44. }
  45. public void RelativeTo(Transform transform)
  46. {
  47. this.matrix = transform.localToWorldMatrix;
  48. }
  49. public void PushMatrix()
  50. {
  51. this._matrixStack.Push(this._currMatrix);
  52. }
  53. public void PopMatrix()
  54. {
  55. this.matrix = this._matrixStack.Pop();
  56. }
  57. public void ResetMatrixAndColorState()
  58. {
  59. this.matrix = Matrix4x4.identity;
  60. this.color = Color.white;
  61. }
  62. public Color color
  63. {
  64. get
  65. {
  66. return this._currColor;
  67. }
  68. set
  69. {
  70. if (this._currColor == value)
  71. {
  72. return;
  73. }
  74. this._currColor = value;
  75. this._operations.Add(RuntimeGizmoDrawer.OperationType.SetColor);
  76. this._colors.Add(this._currColor);
  77. }
  78. }
  79. public Matrix4x4 matrix
  80. {
  81. get
  82. {
  83. return this._currMatrix;
  84. }
  85. set
  86. {
  87. if (this._currMatrix == value)
  88. {
  89. return;
  90. }
  91. this._currMatrix = value;
  92. this._operations.Add(RuntimeGizmoDrawer.OperationType.SetMatrix);
  93. this._matrices.Add(this._currMatrix);
  94. }
  95. }
  96. public void DrawMesh(Mesh mesh, Matrix4x4 matrix)
  97. {
  98. this.setWireMode(false);
  99. this.drawMeshInternal(mesh, matrix);
  100. }
  101. public void DrawMesh(Mesh mesh, Vector3 position, Quaternion rotation, Vector3 scale)
  102. {
  103. this.DrawMesh(mesh, Matrix4x4.TRS(position, rotation, scale));
  104. }
  105. public void DrawWireMesh(Mesh mesh, Matrix4x4 matrix)
  106. {
  107. this.setWireMode(true);
  108. this.drawMeshInternal(mesh, matrix);
  109. }
  110. public void DrawWireMesh(Mesh mesh, Vector3 position, Quaternion rotation, Vector3 scale)
  111. {
  112. this.DrawWireMesh(mesh, Matrix4x4.TRS(position, rotation, scale));
  113. }
  114. public void DrawLine(Vector3 a, Vector3 b)
  115. {
  116. this._operations.Add(RuntimeGizmoDrawer.OperationType.DrawLine);
  117. this._lines.Add(new RuntimeGizmoDrawer.Line(a, b));
  118. }
  119. public void DrawCube(Vector3 position, Vector3 size)
  120. {
  121. this.DrawMesh(this.cubeMesh, position, Quaternion.identity, size);
  122. }
  123. public void DrawWireCube(Vector3 position, Vector3 size)
  124. {
  125. this.DrawWireMesh(this.wireCubeMesh, position, Quaternion.identity, size);
  126. }
  127. public void DrawSphere(Vector3 center, float radius)
  128. {
  129. this.DrawMesh(this.sphereMesh, center, Quaternion.identity, Vector3.one * radius * 2f);
  130. }
  131. public void DrawWireSphere(Vector3 center, float radius)
  132. {
  133. this.DrawWireMesh(this.wireSphereMesh, center, Quaternion.identity, Vector3.one * radius * 2f);
  134. }
  135. public void DrawWireCirlce(Vector3 center, Vector3 direction, float radius)
  136. {
  137. this.PushMatrix();
  138. this.matrix = Matrix4x4.TRS(center, Quaternion.LookRotation(direction), new Vector3(1f, 1f, 0f)) * this.matrix;
  139. this.DrawWireSphere(Vector3.zero, radius);
  140. this.PopMatrix();
  141. }
  142. public void DrawColliders(GameObject gameObject, bool useWireframe = true, bool traverseHierarchy = true)
  143. {
  144. this.PushMatrix();
  145. if (traverseHierarchy)
  146. {
  147. gameObject.GetComponentsInChildren<Collider>(this._colliderList);
  148. }
  149. else
  150. {
  151. gameObject.GetComponents<Collider>(this._colliderList);
  152. }
  153. for (int i = 0; i < this._colliderList.Count; i++)
  154. {
  155. Collider collider = this._colliderList[i];
  156. this.RelativeTo(collider.transform);
  157. if (collider is BoxCollider)
  158. {
  159. BoxCollider boxCollider = collider as BoxCollider;
  160. if (useWireframe)
  161. {
  162. this.DrawWireCube(boxCollider.center, boxCollider.size);
  163. }
  164. else
  165. {
  166. this.DrawCube(boxCollider.center, boxCollider.size);
  167. }
  168. }
  169. else if (collider is SphereCollider)
  170. {
  171. SphereCollider sphereCollider = collider as SphereCollider;
  172. if (useWireframe)
  173. {
  174. this.DrawWireSphere(sphereCollider.center, sphereCollider.radius);
  175. }
  176. else
  177. {
  178. this.DrawSphere(sphereCollider.center, sphereCollider.radius);
  179. }
  180. }
  181. else if (collider is CapsuleCollider)
  182. {
  183. CapsuleCollider capsuleCollider = collider as CapsuleCollider;
  184. Vector3 vector = Vector3.zero;
  185. vector += Vector3.one * capsuleCollider.radius * 2f;
  186. vector += new Vector3((float)((capsuleCollider.direction != 0) ? 0 : 1), (float)((capsuleCollider.direction != 1) ? 0 : 1), (float)((capsuleCollider.direction != 2) ? 0 : 1)) * (capsuleCollider.height - capsuleCollider.radius * 2f);
  187. if (useWireframe)
  188. {
  189. this.DrawWireCube(capsuleCollider.center, vector);
  190. }
  191. else
  192. {
  193. this.DrawCube(capsuleCollider.center, vector);
  194. }
  195. }
  196. else if (collider is MeshCollider)
  197. {
  198. MeshCollider meshCollider = collider as MeshCollider;
  199. if (meshCollider.sharedMesh != null)
  200. {
  201. if (useWireframe)
  202. {
  203. this.DrawWireMesh(meshCollider.sharedMesh, Matrix4x4.identity);
  204. }
  205. else
  206. {
  207. this.DrawMesh(meshCollider.sharedMesh, Matrix4x4.identity);
  208. }
  209. }
  210. }
  211. }
  212. this.PopMatrix();
  213. }
  214. public void ClearAllGizmos()
  215. {
  216. this._operations.Clear();
  217. this._matrices.Clear();
  218. this._colors.Clear();
  219. this._lines.Clear();
  220. this._meshes.Clear();
  221. this._isInWireMode = false;
  222. this._currMatrix = Matrix4x4.identity;
  223. this._currColor = Color.white;
  224. }
  225. public void DrawAllGizmosToScreen()
  226. {
  227. try
  228. {
  229. int num = 0;
  230. int num2 = 0;
  231. int num3 = 0;
  232. int num4 = 0;
  233. int num5 = -1;
  234. this._currMatrix = Matrix4x4.identity;
  235. this._currColor = Color.white;
  236. GL.wireframe = false;
  237. for (int i = 0; i < this._operations.Count; i++)
  238. {
  239. RuntimeGizmoDrawer.OperationType operationType = this._operations[i];
  240. switch (operationType)
  241. {
  242. case RuntimeGizmoDrawer.OperationType.SetMatrix:
  243. this._currMatrix = this._matrices[num++];
  244. break;
  245. case RuntimeGizmoDrawer.OperationType.ToggleWireframe:
  246. GL.wireframe = !GL.wireframe;
  247. break;
  248. case RuntimeGizmoDrawer.OperationType.SetColor:
  249. this._currColor = this._colors[num2++];
  250. num5 = -1;
  251. break;
  252. case RuntimeGizmoDrawer.OperationType.DrawLine:
  253. {
  254. this.setPass(ref num5, true);
  255. GL.Begin(1);
  256. RuntimeGizmoDrawer.Line line = this._lines[num3++];
  257. GL.Vertex(this._currMatrix.MultiplyPoint(line.a));
  258. GL.Vertex(this._currMatrix.MultiplyPoint(line.b));
  259. GL.End();
  260. break;
  261. }
  262. case RuntimeGizmoDrawer.OperationType.DrawMesh:
  263. if (GL.wireframe)
  264. {
  265. this.setPass(ref num5, true);
  266. }
  267. else
  268. {
  269. this.setPass(ref num5, false);
  270. }
  271. Graphics.DrawMeshNow(this._meshes[num4++], this._currMatrix * this._matrices[num++]);
  272. break;
  273. default:
  274. throw new InvalidOperationException("Unexpected operation type " + operationType);
  275. }
  276. }
  277. }
  278. finally
  279. {
  280. GL.wireframe = false;
  281. }
  282. }
  283. private void setPass(ref int currPass, bool isUnlit)
  284. {
  285. int num;
  286. if (isUnlit)
  287. {
  288. if (this._currColor.a < 1f)
  289. {
  290. num = 1;
  291. }
  292. else
  293. {
  294. num = 0;
  295. }
  296. }
  297. else if (this._currColor.a < 1f)
  298. {
  299. num = 3;
  300. }
  301. else
  302. {
  303. num = 2;
  304. }
  305. if (currPass != num)
  306. {
  307. currPass = num;
  308. this._gizmoMaterial.color = this._currColor;
  309. this._gizmoMaterial.SetPass(currPass);
  310. }
  311. }
  312. private void drawMeshInternal(Mesh mesh, Matrix4x4 matrix)
  313. {
  314. if (mesh == null)
  315. {
  316. throw new InvalidOperationException("Mesh cannot be null!");
  317. }
  318. this._operations.Add(RuntimeGizmoDrawer.OperationType.DrawMesh);
  319. this._meshes.Add(mesh);
  320. this._matrices.Add(matrix);
  321. }
  322. private void setWireMode(bool wireMode)
  323. {
  324. if (this._isInWireMode != wireMode)
  325. {
  326. this._operations.Add(RuntimeGizmoDrawer.OperationType.ToggleWireframe);
  327. this._isInWireMode = wireMode;
  328. }
  329. }
  330. public const int UNLIT_SOLID_PASS = 0;
  331. public const int UNLIT_TRANSPARENT_PASS = 1;
  332. public const int SHADED_SOLID_PASS = 2;
  333. public const int SHADED_TRANSPARENT_PASS = 3;
  334. private List<RuntimeGizmoDrawer.OperationType> _operations = new List<RuntimeGizmoDrawer.OperationType>();
  335. private List<Matrix4x4> _matrices = new List<Matrix4x4>();
  336. private List<Color> _colors = new List<Color>();
  337. private List<RuntimeGizmoDrawer.Line> _lines = new List<RuntimeGizmoDrawer.Line>();
  338. private List<Mesh> _meshes = new List<Mesh>();
  339. private Color _currColor = Color.white;
  340. private Matrix4x4 _currMatrix = Matrix4x4.identity;
  341. private Stack<Matrix4x4> _matrixStack = new Stack<Matrix4x4>();
  342. private bool _isInWireMode;
  343. private Material _gizmoMaterial;
  344. private int _operationCountOnGuard = -1;
  345. public Mesh cubeMesh;
  346. public Mesh wireCubeMesh;
  347. public Mesh sphereMesh;
  348. public Mesh wireSphereMesh;
  349. private List<Collider> _colliderList = new List<Collider>();
  350. private enum OperationType
  351. {
  352. SetMatrix,
  353. ToggleWireframe,
  354. SetColor,
  355. DrawLine,
  356. DrawMesh
  357. }
  358. private struct Line
  359. {
  360. public Line(Vector3 a, Vector3 b)
  361. {
  362. this.a = a;
  363. this.b = b;
  364. }
  365. public Vector3 a;
  366. public Vector3 b;
  367. }
  368. }
  369. }