2
0

Gizmos.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 bs;
  6. namespace bs.Editor
  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 hemisphere.
  121. /// </summary>
  122. /// <param name="position">World coordinates of the center of the hemisphere.</param>
  123. /// <param name="radius">Hemisphere radius.</param>
  124. public static void DrawWireHemisphere(Vector3 position, float radius)
  125. {
  126. Internal_DrawWireHemisphere(ref position, radius);
  127. }
  128. /// <summary>
  129. /// Draws a wireframe capsule. Capsule is assumed to be extending along the Y axis.
  130. /// </summary>
  131. /// <param name="position">World coordinates of the center of the capsule.</param>
  132. /// <param name="height">Distance between the centers of the capsule's hemispheres.</param>
  133. /// <param name="radius">Distance of each point from the capsule's center-line.</param>
  134. public static void DrawWireCapsule(Vector3 position, float height, float radius)
  135. {
  136. Internal_DrawWireCapsule(ref position, height, radius);
  137. }
  138. /// <summary>
  139. /// Draws a wireframe cone.
  140. /// </summary>
  141. /// <param name="coneBase">Position of the center of the base of the cone.</param>
  142. /// <param name="normal">Orientation of the cone, pointing from center base to the tip of the cone.</param>
  143. /// <param name="height">Height of the cone (along the normal).</param>
  144. /// <param name="radius">Radius of the base of the cone.</param>
  145. public static void DrawWireCone(Vector3 coneBase, Vector3 normal, float height, float radius)
  146. {
  147. Vector2 scale = Vector2.One;
  148. Internal_DrawWireCone(ref coneBase, ref normal, height, radius, ref scale);
  149. }
  150. /// <summary>
  151. /// Draws a wireframe cone.
  152. /// </summary>
  153. /// <param name="coneBase">Position of the center of the base of the cone.</param>
  154. /// <param name="normal">Orientation of the cone, pointing from center base to the tip of the cone.</param>
  155. /// <param name="height">Height of the cone (along the normal).</param>
  156. /// <param name="radius">Radius of the base of the cone.</param>
  157. /// <param name="scale">Scale applied to cone's disc width & height. Allows you to create elliptical cones.</param>
  158. public static void DrawWireCone(Vector3 coneBase, Vector3 normal, float height, float radius, Vector2 scale)
  159. {
  160. Internal_DrawWireCone(ref coneBase, ref normal, height, radius, ref scale);
  161. }
  162. /// <summary>
  163. /// Draws a 3D line.
  164. /// </summary>
  165. /// <param name="start">Starting point for the line.</param>
  166. /// <param name="end">Ending point for the line.</param>
  167. public static void DrawLine(Vector3 start, Vector3 end)
  168. {
  169. Internal_DrawLine(ref start, ref end);
  170. }
  171. /// <summary>
  172. /// Draws a list of 3D lines.
  173. /// </summary>
  174. /// <param name="linePoints">A list of line point pairs, start point followed by end point, and so on.</param>
  175. public static void DrawLineList(Vector3[] linePoints)
  176. {
  177. Internal_DrawLineList(linePoints);
  178. }
  179. /// <summary>
  180. /// Draws a wireframe disc.
  181. /// </summary>
  182. /// <param name="position">Center of the disc.</param>
  183. /// <param name="normal">Normal towards which to orient the disc.</param>
  184. /// <param name="radius">Radius of the disc.</param>
  185. public static void DrawWireDisc(Vector3 position, Vector3 normal, float radius)
  186. {
  187. Internal_DrawWireDisc(ref position, ref normal, radius);
  188. }
  189. /// <summary>
  190. /// Draws a wireframe arc.
  191. /// </summary>
  192. /// <param name="position">Center of the disc out of which the arc is cut out of.</param>
  193. /// <param name="normal">Normal towards which to orient the arc.</param>
  194. /// <param name="radius">Radius of the disc out of which the arc is cut out of.</param>
  195. /// <param name="startAngle">Angle at which the arc starts.</param>
  196. /// <param name="amountAngle">Length of the arc.</param>
  197. public static void DrawWireArc(Vector3 position, Vector3 normal, float radius, Degree startAngle, Degree amountAngle)
  198. {
  199. Internal_DrawWireArc(ref position, ref normal, radius, startAngle.Degrees, amountAngle.Degrees);
  200. }
  201. /// <summary>
  202. /// Draws a wireframe mesh.
  203. /// </summary>
  204. /// <param name="meshData">Object containing vertices and indices of the mesh.</param>
  205. public static void DrawWireMesh(MeshData meshData)
  206. {
  207. IntPtr meshDataPtr = IntPtr.Zero;
  208. if (meshData != null)
  209. meshDataPtr = meshData.GetCachedPtr();
  210. Internal_DrawWireMesh(meshDataPtr);
  211. }
  212. /// <summary>
  213. /// Draws a wireframe camera frustum.
  214. /// </summary>
  215. /// <param name="position">Origin of the frustum (place where the camera origin would normally be).</param>
  216. /// <param name="aspect">Aspect radio (width/height).</param>
  217. /// <param name="FOV">Horizontal field of view.</param>
  218. /// <param name="near">Distance from the origin to the near plane.</param>
  219. /// <param name="far">Distance from the origin to the far plane.</param>
  220. public static void DrawFrustum(Vector3 position, float aspect, Degree FOV, float near, float far)
  221. {
  222. Internal_DrawFrustum(ref position, aspect, ref FOV, near, far);
  223. }
  224. /// <summary>
  225. /// Draws a texture as a camera-facing quad at a specific position.
  226. /// </summary>
  227. /// <param name="position">World position of the center of the quad the texture will be drawn on.</param>
  228. /// <param name="image">Texture to draw.</param>
  229. /// <param name="fixedScale">If true the icon will remain consistent size regardless of distance from camera.
  230. /// If false normal perspective foreshortening effect will occurr.</param>
  231. public static void DrawIcon(Vector3 position, SpriteTexture image, bool fixedScale)
  232. {
  233. Internal_DrawIcon(ref position, image, fixedScale);
  234. }
  235. /// <summary>
  236. /// Draws camera aligned text at the specified position.
  237. /// </summary>
  238. /// <param name="position">World position to center the text on.</param>
  239. /// <param name="text">String to draw.</param>
  240. /// <param name="font">Font used for drawing the characters.</param>
  241. /// <param name="size">Size of the characters, in points.</param>
  242. public static void DrawText(Vector3 position, string text, Font font = null, int size = 16)
  243. {
  244. IntPtr scriptFont = IntPtr.Zero;
  245. if (font != null)
  246. scriptFont = font.GetCachedPtr();
  247. Internal_DrawText(ref position, text, scriptFont, size);
  248. }
  249. [MethodImpl(MethodImplOptions.InternalCall)]
  250. private static extern void Internal_SetColor(ref Color color);
  251. [MethodImpl(MethodImplOptions.InternalCall)]
  252. private static extern void Internal_GetColor(out Color color);
  253. [MethodImpl(MethodImplOptions.InternalCall)]
  254. private static extern void Internal_SetTransform(ref Matrix4 transform);
  255. [MethodImpl(MethodImplOptions.InternalCall)]
  256. private static extern void Internal_GetTransform(out Matrix4 transform);
  257. [MethodImpl(MethodImplOptions.InternalCall)]
  258. private static extern void Internal_DrawCube(ref Vector3 position, ref Vector3 extents);
  259. [MethodImpl(MethodImplOptions.InternalCall)]
  260. private static extern void Internal_DrawSphere(ref Vector3 position, float radius);
  261. [MethodImpl(MethodImplOptions.InternalCall)]
  262. private static extern void Internal_DrawCone(ref Vector3 coneBase, ref Vector3 normal, float height, float radius, ref Vector2 scale);
  263. [MethodImpl(MethodImplOptions.InternalCall)]
  264. private static extern void Internal_DrawDisc(ref Vector3 position, ref Vector3 normal, float radius);
  265. [MethodImpl(MethodImplOptions.InternalCall)]
  266. private static extern void Internal_DrawWireCube(ref Vector3 position, ref Vector3 extents);
  267. [MethodImpl(MethodImplOptions.InternalCall)]
  268. private static extern void Internal_DrawWireSphere(ref Vector3 position, float radius);
  269. [MethodImpl(MethodImplOptions.InternalCall)]
  270. private static extern void Internal_DrawWireHemisphere(ref Vector3 position, float radius);
  271. [MethodImpl(MethodImplOptions.InternalCall)]
  272. private static extern void Internal_DrawWireCapsule(ref Vector3 position, float height, float radius);
  273. [MethodImpl(MethodImplOptions.InternalCall)]
  274. private static extern void Internal_DrawWireCone(ref Vector3 coneBase, ref Vector3 normal, float height, float radius, ref Vector2 scale);
  275. [MethodImpl(MethodImplOptions.InternalCall)]
  276. private static extern void Internal_DrawLine(ref Vector3 start, ref Vector3 end);
  277. [MethodImpl(MethodImplOptions.InternalCall)]
  278. private static extern void Internal_DrawLineList(Vector3[] linePoints);
  279. [MethodImpl(MethodImplOptions.InternalCall)]
  280. private static extern void Internal_DrawWireDisc(ref Vector3 position, ref Vector3 normal, float radius);
  281. [MethodImpl(MethodImplOptions.InternalCall)]
  282. private static extern void Internal_DrawWireArc(ref Vector3 position, ref Vector3 normal, float radius,
  283. float startAngle, float amountAngle);
  284. [MethodImpl(MethodImplOptions.InternalCall)]
  285. private static extern void Internal_DrawWireMesh(IntPtr meshData);
  286. [MethodImpl(MethodImplOptions.InternalCall)]
  287. private static extern void Internal_DrawFrustum(ref Vector3 position, float aspect, ref Degree FOV, float near, float far);
  288. [MethodImpl(MethodImplOptions.InternalCall)]
  289. private static extern void Internal_DrawIcon(ref Vector3 position, SpriteTexture image, bool fixedScale);
  290. [MethodImpl(MethodImplOptions.InternalCall)]
  291. private static extern void Internal_DrawText(ref Vector3 position, string text, IntPtr font, int size);
  292. }
  293. /** @} */
  294. }