Gizmos.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using BansheeEngine;
  6. namespace BansheeEditor
  7. {
  8. /// <summary>
  9. /// Provides functionality for drawing gizmos. This class should only be used in methods defined with
  10. /// <see cref="DrawGizmo"/> attribute.
  11. /// </summary>
  12. public class Gizmos
  13. {
  14. /// <summary>
  15. /// Determines the color that will be used on any following draw method.
  16. /// </summary>
  17. public static Color Color
  18. {
  19. get
  20. {
  21. Color value;
  22. Internal_GetColor(out value);
  23. return value;
  24. }
  25. set
  26. {
  27. Internal_SetColor(ref value);
  28. }
  29. }
  30. /// <summary>
  31. /// Determines the world transform that will be applied to any following draw method.
  32. /// </summary>
  33. public static Matrix4 Transform
  34. {
  35. get
  36. {
  37. Matrix4 value;
  38. Internal_GetTransform(out value);
  39. return value;
  40. }
  41. set
  42. {
  43. Internal_SetTransform(ref value);
  44. }
  45. }
  46. /// <summary>
  47. /// Draws an axis aligned solid cube.
  48. /// </summary>
  49. /// <param name="position">World coordinates of the center of the cube.</param>
  50. /// <param name="extents">Extents defining the half-size of the cube in each dimension.</param>
  51. public static void DrawCube(Vector3 position, Vector3 extents)
  52. {
  53. Internal_DrawCube(ref position, ref extents);
  54. }
  55. /// <summary>
  56. /// Draws a solid sphere.
  57. /// </summary>
  58. /// <param name="position">World coordinates of the center of the sphere.</param>
  59. /// <param name="radius">Sphere radius.</param>
  60. public static void DrawSphere(Vector3 position, float radius)
  61. {
  62. Internal_DrawSphere(ref position, radius);
  63. }
  64. /// <summary>
  65. /// Draws a solid cone.
  66. /// </summary>
  67. /// <param name="coneBase">Position of the center of the base of the cone.</param>
  68. /// <param name="normal">Orientation of the cone, pointing from center base to the tip of the cone.</param>
  69. /// <param name="height">Height of the cone (along the normal).</param>
  70. /// <param name="radius">Radius of the base of the cone.</param>
  71. public static void DrawCone(Vector3 coneBase, Vector3 normal, float height, float radius)
  72. {
  73. Vector2 scale = Vector2.One;
  74. Internal_DrawCone(ref coneBase, ref normal, height, radius, ref scale);
  75. }
  76. /// <summary>
  77. /// Draws a solid cone.
  78. /// </summary>
  79. /// <param name="coneBase">Position of the center of the base of the cone.</param>
  80. /// <param name="normal">Orientation of the cone, pointing from center base to the tip of the cone.</param>
  81. /// <param name="height">Height of the cone (along the normal).</param>
  82. /// <param name="radius">Radius of the base of the cone.</param>
  83. /// <param name="scale">Scale applied to cone's disc width & height. Allows you to create elliptical cones.</param>
  84. public static void DrawCone(Vector3 coneBase, Vector3 normal, float height, float radius, Vector2 scale)
  85. {
  86. Internal_DrawCone(ref coneBase, ref normal, height, radius, ref scale);
  87. }
  88. /// <summary>
  89. /// Draws an axis aligned wireframe cube.
  90. /// </summary>
  91. /// <param name="position">World coordinates of the center of the cube.</param>
  92. /// <param name="extents">Extents defining the half-size of the cube in each dimension.</param>
  93. public static void DrawWireCube(Vector3 position, Vector3 extents)
  94. {
  95. Internal_DrawWireCube(ref position, ref extents);
  96. }
  97. /// <summary>
  98. /// Draws a wireframe sphere.
  99. /// </summary>
  100. /// <param name="position">World coordinates of the center of the sphere.</param>
  101. /// <param name="radius">Sphere radius.</param>
  102. public static void DrawWireSphere(Vector3 position, float radius)
  103. {
  104. Internal_DrawWireSphere(ref position, radius);
  105. }
  106. /// <summary>
  107. /// Draws a wireframe capsule. Capsule is assumed to be extending along the Y axis.
  108. /// </summary>
  109. /// <param name="position">World coordinates of the center of the capsule.</param>
  110. /// <param name="height">Distance between the centers of the capsule's hemispheres.</param>
  111. /// <param name="radius">Distance of each point from the capsule's center-line.</param>
  112. public static void DrawWireCapsule(Vector3 position, float height, float radius)
  113. {
  114. Internal_DrawWireCapsule(ref position, height, radius);
  115. }
  116. /// <summary>
  117. /// Draws a wireframe cone.
  118. /// </summary>
  119. /// <param name="coneBase">Position of the center of the base of the cone.</param>
  120. /// <param name="normal">Orientation of the cone, pointing from center base to the tip of the cone.</param>
  121. /// <param name="height">Height of the cone (along the normal).</param>
  122. /// <param name="radius">Radius of the base of the cone.</param>
  123. public static void DrawWireCone(Vector3 coneBase, Vector3 normal, float height, float radius)
  124. {
  125. Vector2 scale = Vector2.One;
  126. Internal_DrawWireCone(ref coneBase, ref normal, height, radius, ref scale);
  127. }
  128. /// <summary>
  129. /// Draws a wireframe cone.
  130. /// </summary>
  131. /// <param name="coneBase">Position of the center of the base of the cone.</param>
  132. /// <param name="normal">Orientation of the cone, pointing from center base to the tip of the cone.</param>
  133. /// <param name="height">Height of the cone (along the normal).</param>
  134. /// <param name="radius">Radius of the base of the cone.</param>
  135. /// <param name="scale">Scale applied to cone's disc width & height. Allows you to create elliptical cones.</param>
  136. public static void DrawWireCone(Vector3 coneBase, Vector3 normal, float height, float radius, Vector2 scale)
  137. {
  138. Internal_DrawWireCone(ref coneBase, ref normal, height, radius, ref scale);
  139. }
  140. /// <summary>
  141. /// Draws a 3D line.
  142. /// </summary>
  143. /// <param name="start">Starting point for the line.</param>
  144. /// <param name="end">Ending point for the line.</param>
  145. public static void DrawLine(Vector3 start, Vector3 end)
  146. {
  147. Internal_DrawLine(ref start, ref end);
  148. }
  149. /// <summary>
  150. /// Draws a list of 3D lines.
  151. /// </summary>
  152. /// <param name="linePoints">A list of line point pairs, start point followed by end point, and so on.</param>
  153. public static void DrawLineList(Vector3[] linePoints)
  154. {
  155. Internal_DrawLineList(linePoints);
  156. }
  157. /// <summary>
  158. /// Draws a wireframe disc.
  159. /// </summary>
  160. /// <param name="position">Center of the disc.</param>
  161. /// <param name="normal">Normal towards which to orient the disc.</param>
  162. /// <param name="radius">Radius of the disc.</param>
  163. public static void DrawWireDisc(Vector3 position, Vector3 normal, float radius)
  164. {
  165. Internal_DrawWireDisc(ref position, ref normal, radius);
  166. }
  167. /// <summary>
  168. /// Draws a wireframe arc.
  169. /// </summary>
  170. /// <param name="position">Center of the disc out of which the arc is cut out of.</param>
  171. /// <param name="normal">Normal towards which to orient the arc.</param>
  172. /// <param name="radius">Radius of the disc out of which the arc is cut out of.</param>
  173. /// <param name="startAngle">Angle at which the arc starts.</param>
  174. /// <param name="amountAngle">Length of the arc.</param>
  175. public static void DrawWireArc(Vector3 position, Vector3 normal, float radius, Degree startAngle, Degree amountAngle)
  176. {
  177. Internal_DrawWireArc(ref position, ref normal, radius, startAngle.Degrees, amountAngle.Degrees);
  178. }
  179. /// <summary>
  180. /// Draws a wireframe mesh.
  181. /// </summary>
  182. /// <param name="meshData">Object containing vertices and indices of the mesh.</param>
  183. public static void DrawWireMesh(MeshData meshData)
  184. {
  185. IntPtr meshDataPtr = IntPtr.Zero;
  186. if (meshData != null)
  187. meshDataPtr = meshData.GetCachedPtr();
  188. Internal_DrawWireMesh(meshDataPtr);
  189. }
  190. /// <summary>
  191. /// Draws a wireframe camera frustum.
  192. /// </summary>
  193. /// <param name="position">Origin of the frustum (place where the camera origin would normally be).</param>
  194. /// <param name="aspect">Aspect radio (width/height).</param>
  195. /// <param name="FOV">Horizontal field of view.</param>
  196. /// <param name="near">Distance from the origin to the near plane.</param>
  197. /// <param name="far">Distance from the origin to the far plane.</param>
  198. public static void DrawFrustum(Vector3 position, float aspect, Degree FOV, float near, float far)
  199. {
  200. Internal_DrawFrustum(ref position, aspect, ref FOV, near, far);
  201. }
  202. /// <summary>
  203. /// Draws a texture as a camera-facing quad at a specific position.
  204. /// </summary>
  205. /// <param name="position">World position of the center of the quad the texture will be drawn on.</param>
  206. /// <param name="image">Texture to draw.</param>
  207. /// <param name="fixedScale">If true the icon will remain consistent size regardless of distance from camera.
  208. /// If false normal perspective foreshortening effect will occurr.</param>
  209. public static void DrawIcon(Vector3 position, SpriteTexture image, bool fixedScale)
  210. {
  211. Internal_DrawIcon(ref position, image, fixedScale);
  212. }
  213. /// <summary>
  214. /// Draws camera aligned text at the specified position.
  215. /// </summary>
  216. /// <param name="position">World position to center the text on.</param>
  217. /// <param name="text">String to draw.</param>
  218. /// <param name="font">Font used for drawing the characters.</param>
  219. /// <param name="size">Size of the characters, in points.</param>
  220. public static void DrawText(Vector3 position, string text, Font font = null, int size = 16)
  221. {
  222. IntPtr scriptFont = IntPtr.Zero;
  223. if (font != null)
  224. scriptFont = font.GetCachedPtr();
  225. Internal_DrawText(ref position, text, scriptFont, size);
  226. }
  227. [MethodImpl(MethodImplOptions.InternalCall)]
  228. private static extern void Internal_SetColor(ref Color color);
  229. [MethodImpl(MethodImplOptions.InternalCall)]
  230. private static extern void Internal_GetColor(out Color color);
  231. [MethodImpl(MethodImplOptions.InternalCall)]
  232. private static extern void Internal_SetTransform(ref Matrix4 transform);
  233. [MethodImpl(MethodImplOptions.InternalCall)]
  234. private static extern void Internal_GetTransform(out Matrix4 transform);
  235. [MethodImpl(MethodImplOptions.InternalCall)]
  236. private static extern void Internal_DrawCube(ref Vector3 position, ref Vector3 extents);
  237. [MethodImpl(MethodImplOptions.InternalCall)]
  238. private static extern void Internal_DrawSphere(ref Vector3 position, float radius);
  239. [MethodImpl(MethodImplOptions.InternalCall)]
  240. private static extern void Internal_DrawCone(ref Vector3 coneBase, ref Vector3 normal, float height, float radius, ref Vector2 scale);
  241. [MethodImpl(MethodImplOptions.InternalCall)]
  242. private static extern void Internal_DrawWireCube(ref Vector3 position, ref Vector3 extents);
  243. [MethodImpl(MethodImplOptions.InternalCall)]
  244. private static extern void Internal_DrawWireSphere(ref Vector3 position, float radius);
  245. [MethodImpl(MethodImplOptions.InternalCall)]
  246. private static extern void Internal_DrawWireCapsule(ref Vector3 position, float height, float radius);
  247. [MethodImpl(MethodImplOptions.InternalCall)]
  248. private static extern void Internal_DrawWireCone(ref Vector3 coneBase, ref Vector3 normal, float height, float radius, ref Vector2 scale);
  249. [MethodImpl(MethodImplOptions.InternalCall)]
  250. private static extern void Internal_DrawLine(ref Vector3 start, ref Vector3 end);
  251. [MethodImpl(MethodImplOptions.InternalCall)]
  252. private static extern void Internal_DrawLineList(Vector3[] linePoints);
  253. [MethodImpl(MethodImplOptions.InternalCall)]
  254. private static extern void Internal_DrawWireDisc(ref Vector3 position, ref Vector3 normal, float radius);
  255. [MethodImpl(MethodImplOptions.InternalCall)]
  256. private static extern void Internal_DrawWireArc(ref Vector3 position, ref Vector3 normal, float radius,
  257. float startAngle, float amountAngle);
  258. [MethodImpl(MethodImplOptions.InternalCall)]
  259. private static extern void Internal_DrawWireMesh(IntPtr meshData);
  260. [MethodImpl(MethodImplOptions.InternalCall)]
  261. private static extern void Internal_DrawFrustum(ref Vector3 position, float aspect, ref Degree FOV, float near, float far);
  262. [MethodImpl(MethodImplOptions.InternalCall)]
  263. private static extern void Internal_DrawIcon(ref Vector3 position, SpriteTexture image, bool fixedScale);
  264. [MethodImpl(MethodImplOptions.InternalCall)]
  265. private static extern void Internal_DrawText(ref Vector3 position, string text, IntPtr font, int size);
  266. }
  267. }