Advertisement
Guest User

Untitled

a guest
Dec 27th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace De.Bug
  6. {
  7. public class Draw : MonoBehaviour {
  8.  
  9. public List<DebugVO> Vos = new List<DebugVO>();
  10.  
  11. private static Draw _instance;
  12. public static Draw Instance
  13. {
  14. get{
  15. if(_instance == null)
  16. {
  17. _instance = FindObjectOfType<Draw>();
  18. }
  19. if(_instance == null)
  20. {
  21. GameObject go = new GameObject();
  22. _instance = go.AddComponent<Draw>();
  23. go.name = "Debug Draw";
  24. }
  25. return _instance;
  26. }
  27. }
  28.  
  29. public static void Line(Color color, params Vector3[] point)
  30. {
  31. DebugVO vo = ScriptableObject.CreateInstance<DebugVO>();
  32. List<Vector3> p = new List<Vector3>();
  33. int i = 0, l = point.Length;
  34. for(;i<l;++i)
  35. {
  36. p.Add(point[i]);
  37. }
  38. vo.Points = p;
  39. vo.Color = color;
  40. vo.AutoDelete = true;
  41. Draw.Instance.Vos.Add(vo);
  42. }
  43.  
  44. public static void Line(Color color, List<Vector3> points, bool autoDelete = true)
  45. {
  46. DebugVO vo = ScriptableObject.CreateInstance<DebugVO>();
  47. vo.Points = points;
  48. vo.Color = color;
  49. vo.AutoDelete = autoDelete;
  50. Draw.Instance.Vos.Add(vo);
  51. }
  52.  
  53. public static void AABB(Color color, GameObject gameObject, bool autoDelete = true)
  54. {
  55. if(gameObject.transform.renderer == null)
  56. {
  57. return;
  58. }
  59.  
  60. DebugVO vo = ScriptableObject.CreateInstance<DebugVO>();
  61. Vector3 min = gameObject.transform.renderer.bounds.min;
  62. Vector3 max = gameObject.transform.renderer.bounds.max;
  63.  
  64. List<Vector3> p = new List<Vector3>();
  65. p.Add(new Vector3(min.x,min.y,min.z));
  66. p.Add(new Vector3(min.x,min.y,max.z));
  67.  
  68. p.Add(new Vector3(min.x,min.y,max.z));
  69. p.Add(new Vector3(max.x,min.y,max.z));
  70.  
  71. p.Add(new Vector3(max.x,min.y,max.z));
  72. p.Add(new Vector3(max.x,min.y,min.z));
  73.  
  74. p.Add(new Vector3(max.x,min.y,min.z));
  75. p.Add(new Vector3(min.x,min.y,min.z));
  76.  
  77. p.Add(new Vector3(min.x,max.y,min.z));
  78. p.Add(new Vector3(min.x,max.y,max.z));
  79.  
  80. p.Add(new Vector3(min.x,max.y,max.z));
  81. p.Add(new Vector3(max.x,max.y,max.z));
  82.  
  83. p.Add(new Vector3(max.x,max.y,max.z));
  84. p.Add(new Vector3(max.x,max.y,min.z));
  85.  
  86. p.Add(new Vector3(max.x,max.y,min.z));
  87. p.Add(new Vector3(min.x,max.y,min.z));
  88.  
  89. p.Add(new Vector3(max.x,max.y,min.z));
  90. p.Add(new Vector3(max.x,min.y,min.z));
  91.  
  92. p.Add(new Vector3(max.x,min.y,max.z));
  93. p.Add(new Vector3(max.x,max.y,max.z));
  94.  
  95. p.Add(new Vector3(min.x,max.y,max.z));
  96. p.Add(new Vector3(min.x,min.y,max.z));
  97.  
  98. vo.Points = p;
  99. vo.Color = color;
  100. vo.AutoDelete = autoDelete;
  101. Draw.Instance.Vos.Add(vo);
  102. }
  103.  
  104. public static void Circle(Color color, Vector3 point, float radius, Quaternion rotation, bool autoDelete = true)
  105. {
  106. Circle(color,point,radius,rotation.eulerAngles,autoDelete);
  107. }
  108.  
  109. public static void Circle(Color color, Vector3 point, float radius, Vector3 rotation, bool autoDelete = true)
  110. {
  111. int segments = (int)(radius*50);
  112. float angle = (360f) / (float)segments;
  113. float a = angle;
  114. List<Vector3> points = new List<Vector3>();
  115.  
  116. int i = 0, l = segments+1;
  117.  
  118. Matrix4x4 m = Matrix4x4.TRS(Vector3.zero,Quaternion.Euler(rotation),Vector3.one);
  119.  
  120. for (; i<l; ++i)
  121. {
  122. Vector3 pos = Vector3.zero;
  123. float ca = radius * Mathf.Sin(angle * Mathf.Deg2Rad);
  124. float cb = radius * Mathf.Cos(angle * Mathf.Deg2Rad);
  125. pos = new Vector3(ca,cb,0);
  126. pos = m.MultiplyPoint3x4(pos);
  127. pos.x += point.x;
  128. pos.y += point.y;
  129. pos.z += point.z;
  130.  
  131. points.Add(pos);
  132.  
  133. angle += a;
  134. }
  135.  
  136. DebugVO vo = ScriptableObject.CreateInstance<DebugVO>();
  137. vo.Points = points;
  138. vo.Color = color;
  139. vo.AutoDelete = autoDelete;
  140. Draw.Instance.Vos.Add(vo);
  141. }
  142.  
  143. public static void Sphere(Color color, Vector3 point, float radius, bool autoDelete = true)
  144. {
  145. int segments = (int)(radius*50);
  146. float angle = (360f) / (float)segments;
  147. float a = angle;
  148. List<Vector3> pointsa = new List<Vector3>();
  149. List<Vector3> pointsb = new List<Vector3>();
  150. List<Vector3> pointsc = new List<Vector3>();
  151.  
  152. int i = 0, l = (segments*3)+3;
  153. for (; i<l; ++i)
  154. {
  155. Vector3 pos = Vector3.zero;
  156.  
  157. float ca = radius * Mathf.Sin(angle * Mathf.Deg2Rad);
  158. float cb = radius * Mathf.Cos(angle * Mathf.Deg2Rad);
  159.  
  160. if(i <= segments)
  161. {
  162. pos = new Vector3(point.x + ca,point.y + cb, point.z);
  163. pointsa.Add(pos);
  164. }
  165. else if (i <= (segments * 2)+1)
  166. {
  167. pos = new Vector3(point.x + ca,point.y, point.z + cb);
  168. pointsb.Add(pos);
  169. }
  170. else
  171. {
  172. pos = new Vector3(point.x,point.y + ca, point.z + cb);
  173. pointsc.Add(pos);
  174. }
  175.  
  176.  
  177. angle += a;
  178. }
  179. DebugVO vo = ScriptableObject.CreateInstance<DebugVO>();
  180. vo.Points = pointsa;
  181. vo.Color = color;
  182. vo.AutoDelete = autoDelete;
  183. Draw.Instance.Vos.Add(vo);
  184.  
  185. vo = ScriptableObject.CreateInstance<DebugVO>();
  186. vo.Points = pointsb;
  187. vo.Color = color;
  188. vo.AutoDelete = autoDelete;
  189. Draw.Instance.Vos.Add(vo);
  190.  
  191. vo = ScriptableObject.CreateInstance<DebugVO>();
  192. vo.Points = pointsc;
  193. vo.Color = color;
  194. vo.AutoDelete = autoDelete;
  195. Draw.Instance.Vos.Add(vo);
  196. }
  197.  
  198. //Do the actual debug stuff in gizmos
  199. void OnDrawGizmos()
  200. {
  201. int i = 0, l = Vos.Count;
  202. List<DebugVO> removing = new List<DebugVO>();
  203.  
  204. for(;i<l;++i)
  205. {
  206. DebugVO vo = Vos[i];
  207. if(vo.Points != null)
  208. {
  209. Gizmos.color = vo.Color;
  210. int j = 1, k = vo.Points.Count;
  211. for(;j<k;++j)
  212. {
  213. Gizmos.DrawLine(vo.Points[j-1],vo.Points[j]);
  214. }
  215. }
  216. if(vo.AutoDelete)
  217. {
  218. removing.Add(vo);
  219. }
  220. }
  221.  
  222. i = 0; l = removing.Count;
  223. for(;i<l;++i)
  224. {
  225. int index = Vos.IndexOf(removing[i]);
  226. Destroy(Vos[index]);
  227. Vos.RemoveAt(index);
  228. }
  229. }
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement