Gizmos.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Provides functionality for drawing gizmos. This class should only be used in methods defined with
  8. /// <see cref="DrawGizmo"/> attribute.
  9. /// </summary>
  10. public class Gizmos
  11. {
  12. /// <summary>
  13. /// Determines the color that will be used on any following draw method.
  14. /// </summary>
  15. public static Color Color
  16. {
  17. get
  18. {
  19. Color value;
  20. Internal_GetColor(out value);
  21. return value;
  22. }
  23. set
  24. {
  25. Internal_SetColor(ref value);
  26. }
  27. }
  28. /// <summary>
  29. /// Determines the world transform that will be applied to any following draw method.
  30. /// </summary>
  31. public static Matrix4 Transform
  32. {
  33. get
  34. {
  35. Matrix4 value;
  36. Internal_GetTransform(out value);
  37. return value;
  38. }
  39. set
  40. {
  41. Internal_SetTransform(ref value);
  42. }
  43. }
  44. /// <summary>
  45. /// Draws an axis aligned solid cube.
  46. /// </summary>
  47. /// <param name="position">World coordinates of the center of the cube.</param>
  48. /// <param name="extents">Extents defining the half-size of the cube in each dimension.</param>
  49. public static void DrawCube(Vector3 position, Vector3 extents)
  50. {
  51. Internal_DrawCube(ref position, ref extents);
  52. }
  53. /// <summary>
  54. /// Draws a solid sphere.
  55. /// </summary>
  56. /// <param name="position">World coordinates of the center of the sphere.</param>
  57. /// <param name="radius">Sphere radius.</param>
  58. public static void DrawSphere(Vector3 position, float radius)
  59. {
  60. Internal_DrawSphere(ref position, radius);
  61. }
  62. /// <summary>
  63. /// Draws an axis aligned wireframe cube.
  64. /// </summary>
  65. /// <param name="position">World coordinates of the center of the cube.</param>
  66. /// <param name="extents">Extents defining the half-size of the cube in each dimension.</param>
  67. public static void DrawWireCube(Vector3 position, Vector3 extents)
  68. {
  69. Internal_DrawWireCube(ref position, ref extents);
  70. }
  71. /// <summary>
  72. /// Draws a wireframe sphere.
  73. /// </summary>
  74. /// <param name="position">World coordinates of the center of the sphere.</param>
  75. /// <param name="radius">Sphere radius.</param>
  76. public static void DrawWireSphere(Vector3 position, float radius)
  77. {
  78. Internal_DrawWireSphere(ref position, radius);
  79. }
  80. /// <summary>
  81. /// Draws a 3D line.
  82. /// </summary>
  83. /// <param name="start">Starting point for the line.</param>
  84. /// <param name="end">Ending point for the line.</param>
  85. public static void DrawLine(Vector3 start, Vector3 end)
  86. {
  87. Internal_DrawLine(ref start, ref end);
  88. }
  89. /// <summary>
  90. /// Draws a wireframe disc.
  91. /// </summary>
  92. /// <param name="position">Center of the disc.</param>
  93. /// <param name="normal">Normal towards which to orient the disc.</param>
  94. /// <param name="radius">Radius of the disc.</param>
  95. public static void DrawWireDisc(Vector3 position, Vector3 normal, float radius)
  96. {
  97. Internal_DrawWireDisc(ref position, ref normal, radius);
  98. }
  99. /// <summary>
  100. /// Draws a wireframe arc.
  101. /// </summary>
  102. /// <param name="position">Center of the disc out of which the arc is cut out of.</param>
  103. /// <param name="normal">Normal towards which to orient the arc.</param>
  104. /// <param name="radius">Radius of the disc out of which the arc is cut out of.</param>
  105. /// <param name="startAngle">Angle at which the arc starts.</param>
  106. /// <param name="amountAngle">Length of the arc.</param>
  107. public static void DrawWireArc(Vector3 position, Vector3 normal, float radius, Degree startAngle, Degree amountAngle)
  108. {
  109. Internal_DrawWireArc(ref position, ref normal, radius, startAngle.Degrees, amountAngle.Degrees);
  110. }
  111. /// <summary>
  112. /// Draws a wireframe camera frustum.
  113. /// </summary>
  114. /// <param name="position">Origin of the frustum (place where the camera origin would normally be).</param>
  115. /// <param name="aspect">Aspect radio (width/height).</param>
  116. /// <param name="FOV">Horizontal field of view.</param>
  117. /// <param name="near">Distance from the origin to the near plane.</param>
  118. /// <param name="far">Distance from the origin to the far plane.</param>
  119. public static void DrawFrustum(Vector3 position, float aspect, Degree FOV, float near, float far)
  120. {
  121. Internal_DrawFrustum(ref position, aspect, ref FOV, near, far);
  122. }
  123. /// <summary>
  124. /// Draws a texture as a camera-facing quad at a specific position.
  125. /// </summary>
  126. /// <param name="position">World position of the center of the quad the texture will be drawn on.</param>
  127. /// <param name="image">Texture to draw.</param>
  128. /// <param name="fixedScale">If true the icon will remain consistent size regardless of distance from camera.
  129. /// If false normal perspective foreshortening effect will occurr.</param>
  130. public static void DrawIcon(Vector3 position, SpriteTexture image, bool fixedScale)
  131. {
  132. Internal_DrawIcon(ref position, image, fixedScale);
  133. }
  134. /// <summary>
  135. /// Draws camera aligned text at the specified position.
  136. /// </summary>
  137. /// <param name="position">World position to center the text on.</param>
  138. /// <param name="text">String to draw.</param>
  139. /// <param name="font">Font used for drawing the characters.</param>
  140. /// <param name="size">Size of the characters, in points.</param>
  141. public static void DrawText(Vector3 position, string text, Font font = null, int size = 16)
  142. {
  143. IntPtr scriptFont = IntPtr.Zero;
  144. if (font != null)
  145. scriptFont = font.GetCachedPtr();
  146. Internal_DrawText(ref position, text, scriptFont, size);
  147. }
  148. [MethodImpl(MethodImplOptions.InternalCall)]
  149. private static extern void Internal_SetColor(ref Color color);
  150. [MethodImpl(MethodImplOptions.InternalCall)]
  151. private static extern void Internal_GetColor(out Color color);
  152. [MethodImpl(MethodImplOptions.InternalCall)]
  153. private static extern void Internal_SetTransform(ref Matrix4 transform);
  154. [MethodImpl(MethodImplOptions.InternalCall)]
  155. private static extern void Internal_GetTransform(out Matrix4 transform);
  156. [MethodImpl(MethodImplOptions.InternalCall)]
  157. private static extern void Internal_DrawCube(ref Vector3 position, ref Vector3 extents);
  158. [MethodImpl(MethodImplOptions.InternalCall)]
  159. private static extern void Internal_DrawSphere(ref Vector3 position, float radius);
  160. [MethodImpl(MethodImplOptions.InternalCall)]
  161. private static extern void Internal_DrawWireCube(ref Vector3 position, ref Vector3 extents);
  162. [MethodImpl(MethodImplOptions.InternalCall)]
  163. private static extern void Internal_DrawWireSphere(ref Vector3 position, float radius);
  164. [MethodImpl(MethodImplOptions.InternalCall)]
  165. private static extern void Internal_DrawLine(ref Vector3 start, ref Vector3 end);
  166. [MethodImpl(MethodImplOptions.InternalCall)]
  167. private static extern void Internal_DrawWireDisc(ref Vector3 position, ref Vector3 normal, float radius);
  168. [MethodImpl(MethodImplOptions.InternalCall)]
  169. private static extern void Internal_DrawWireArc(ref Vector3 position, ref Vector3 normal, float radius,
  170. float startAngle, float amountAngle);
  171. [MethodImpl(MethodImplOptions.InternalCall)]
  172. private static extern void Internal_DrawFrustum(ref Vector3 position, float aspect, ref Degree FOV, float near, float far);
  173. [MethodImpl(MethodImplOptions.InternalCall)]
  174. private static extern void Internal_DrawIcon(ref Vector3 position, SpriteTexture image, bool fixedScale);
  175. [MethodImpl(MethodImplOptions.InternalCall)]
  176. private static extern void Internal_DrawText(ref Vector3 position, string text, IntPtr font, int size);
  177. }
  178. }