Gizmos.cs 14 KB

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