MeshData.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 System.Runtime.InteropServices;
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup Utility
  9. * @{
  10. */
  11. /// <summary>
  12. /// Contains mesh vertex and index data used for initializing, updating and reading mesh data from Mesh.
  13. /// </summary>
  14. public class MeshData : ScriptObject
  15. {
  16. /// <summary>
  17. /// Constructor for internal runtime use only.
  18. /// </summary>
  19. private MeshData()
  20. { }
  21. /// <summary>
  22. /// Creates a new mesh data that can hold number of vertices described by the provided vertex layout as well as a
  23. /// number of indices of the provided type.
  24. /// </summary>
  25. /// <param name="numVertices">Number of vertices in the mesh.</param>
  26. /// <param name="numIndices">Number of indices in the mesh. </param>
  27. /// <param name="vertex">Controls how are vertices organized in the vertex buffer and what data they contain.</param>
  28. /// <param name="index">Size of indices, use smaller size for better performance, however be careful not to go over
  29. /// the number of vertices limited by the size.</param>
  30. public MeshData(int numVertices, int numIndices, VertexType vertex = VertexType.Position,
  31. IndexType index = IndexType.Index32)
  32. {
  33. Internal_CreateInstance(this, numVertices, numIndices, vertex, index);
  34. }
  35. /// <summary>
  36. /// An array of all vertex positions. Only valid if the vertex layout contains vertex positions.
  37. /// </summary>
  38. public Vector3[] Positions
  39. {
  40. get { return Internal_GetPositions(mCachedPtr); }
  41. set { Internal_SetPositions(mCachedPtr, value); }
  42. }
  43. /// <summary>
  44. /// An array of all vertex normals. Only valid if the vertex layout contains vertex normals.
  45. /// </summary>
  46. public Vector3[] Normals
  47. {
  48. get { return Internal_GetNormals(mCachedPtr); }
  49. set { Internal_SetNormals(mCachedPtr, value); }
  50. }
  51. /// <summary>
  52. /// An array of all vertex tangents. Only valid if the vertex layout contains vertex tangents.
  53. /// </summary>
  54. public Vector4[] Tangents
  55. {
  56. get { return Internal_GetTangents(mCachedPtr); }
  57. set { Internal_SetTangents(mCachedPtr, value); }
  58. }
  59. /// <summary>
  60. /// An array of all vertex colors. Only valid if the vertex layout contains vertex colors.
  61. /// </summary>
  62. public Color[] Colors
  63. {
  64. get { return Internal_GetColors(mCachedPtr); }
  65. set { Internal_SetColors(mCachedPtr, value); }
  66. }
  67. /// <summary>
  68. /// An array of all vertex texture coordinates in the UV0 channel. Only valid if the vertex layout contains UV0
  69. /// coordinates.
  70. /// </summary>
  71. public Vector2[] UV
  72. {
  73. get { return UV0; }
  74. set { UV0 = value; }
  75. }
  76. /// <summary>
  77. /// An array of all vertex texture coordinates in the UV0 channel. Only valid if the vertex layout contains UV0
  78. /// coordinates.
  79. /// </summary>
  80. public Vector2[] UV0
  81. {
  82. get { return Internal_GetUV0(mCachedPtr); }
  83. set { Internal_SetUV0(mCachedPtr, value); }
  84. }
  85. /// <summary>
  86. /// An array of all vertex texture coordinates in the UV1 channel. Only valid if the vertex layout contains UV1
  87. /// coordinates.
  88. /// </summary>
  89. public Vector2[] UV1
  90. {
  91. get { return Internal_GetUV1(mCachedPtr); }
  92. set { Internal_SetUV0(mCachedPtr, value); }
  93. }
  94. /// <summary>
  95. /// An array of all vertex bone weights. Only valid if the vertex layout contains bone weights.
  96. /// </summary>
  97. public BoneWeight[] BoneWeights
  98. {
  99. get { return Internal_GetBoneWeights(mCachedPtr); }
  100. set { Internal_SetBoneWeights(mCachedPtr, value); }
  101. }
  102. /// <summary>
  103. /// An array of all indices. Make sure that individual entries do not go over the index count as required by
  104. /// active index type.
  105. /// </summary>
  106. public int[] Indices
  107. {
  108. get { return Internal_GetIndices(mCachedPtr); }
  109. set { Internal_SetIndices(mCachedPtr, value); }
  110. }
  111. /// <summary>
  112. /// Number of vertices contained by this object.
  113. /// </summary>
  114. public int VertexCount
  115. {
  116. get { return Internal_GetVertexCount(mCachedPtr); }
  117. }
  118. /// <summary>
  119. /// Number of indices contained by this object.
  120. /// </summary>
  121. public int IndexCount
  122. {
  123. get { return Internal_GetIndexCount(mCachedPtr); }
  124. }
  125. [MethodImpl(MethodImplOptions.InternalCall)]
  126. private static extern void Internal_CreateInstance(MeshData instance, int numVertices,
  127. int numIndices, VertexType vertex, IndexType index);
  128. [MethodImpl(MethodImplOptions.InternalCall)]
  129. private static extern Vector3[] Internal_GetPositions(IntPtr thisPtr);
  130. [MethodImpl(MethodImplOptions.InternalCall)]
  131. private static extern void Internal_SetPositions(IntPtr thisPtr, Vector3[] value);
  132. [MethodImpl(MethodImplOptions.InternalCall)]
  133. private static extern Vector3[] Internal_GetNormals(IntPtr thisPtr);
  134. [MethodImpl(MethodImplOptions.InternalCall)]
  135. private static extern void Internal_SetNormals(IntPtr thisPtr, Vector3[] value);
  136. [MethodImpl(MethodImplOptions.InternalCall)]
  137. private static extern Vector4[] Internal_GetTangents(IntPtr thisPtr);
  138. [MethodImpl(MethodImplOptions.InternalCall)]
  139. private static extern void Internal_SetTangents(IntPtr thisPtr, Vector4[] value);
  140. [MethodImpl(MethodImplOptions.InternalCall)]
  141. private static extern Color[] Internal_GetColors(IntPtr thisPtr);
  142. [MethodImpl(MethodImplOptions.InternalCall)]
  143. private static extern void Internal_SetColors(IntPtr thisPtr, Color[] value);
  144. [MethodImpl(MethodImplOptions.InternalCall)]
  145. private static extern Vector2[] Internal_GetUV0(IntPtr thisPtr);
  146. [MethodImpl(MethodImplOptions.InternalCall)]
  147. private static extern void Internal_SetUV0(IntPtr thisPtr, Vector2[] value);
  148. [MethodImpl(MethodImplOptions.InternalCall)]
  149. private static extern Vector2[] Internal_GetUV1(IntPtr thisPtr);
  150. [MethodImpl(MethodImplOptions.InternalCall)]
  151. private static extern void Internal_SetUV1(IntPtr thisPtr, Vector2[] value);
  152. [MethodImpl(MethodImplOptions.InternalCall)]
  153. private static extern BoneWeight[] Internal_GetBoneWeights(IntPtr thisPtr);
  154. [MethodImpl(MethodImplOptions.InternalCall)]
  155. private static extern void Internal_SetBoneWeights(IntPtr thisPtr, BoneWeight[] value);
  156. [MethodImpl(MethodImplOptions.InternalCall)]
  157. private static extern int[] Internal_GetIndices(IntPtr thisPtr);
  158. [MethodImpl(MethodImplOptions.InternalCall)]
  159. private static extern void Internal_SetIndices(IntPtr thisPtr, int[] value);
  160. [MethodImpl(MethodImplOptions.InternalCall)]
  161. private static extern int Internal_GetVertexCount(IntPtr thisPtr);
  162. [MethodImpl(MethodImplOptions.InternalCall)]
  163. private static extern int Internal_GetIndexCount(IntPtr thisPtr);
  164. }
  165. /// <summary>
  166. /// Available vertex layouts that specify what data is provided per-vertex in a mesh. Combinations other than those
  167. /// provided are allowed.
  168. /// </summary>
  169. public enum VertexType // Note: Must match C++ enum VertexLayout
  170. {
  171. Position = 0x01,
  172. Color = 0x02,
  173. Normal = 0x04,
  174. Tangent = 0x08,
  175. BlendWeights = 0x10,
  176. UV0 = 0x20,
  177. UV1 = 0x40,
  178. PC = Position | Color,
  179. PU = Position | UV0,
  180. PCU = Position | Color | UV0,
  181. PCN = Position | Color | Normal,
  182. PCNU = Position | Color | Normal | UV0,
  183. PCNT = Position | Color | Normal | Tangent,
  184. PCNTU = Position | Color | Normal | Tangent | UV0,
  185. PN = Position | Normal,
  186. PNU = Position | Normal | UV0,
  187. PNT = Position | Normal | Tangent,
  188. PNTU = Position | Normal | Tangent | UV0,
  189. }
  190. /// <summary>
  191. /// Determines the size of a single index in a mesh.
  192. /// </summary>
  193. public enum IndexType // Note: Must match C++ enum ScriptIndexType
  194. {
  195. Index16,
  196. Index32
  197. }
  198. /// <summary>
  199. /// Contains per-vertex bone weights and indexes used for skinning, for up to four bones.
  200. /// </summary>
  201. [StructLayout(LayoutKind.Sequential)]
  202. public struct BoneWeight // Note: Must match C++ class BoneWeight
  203. {
  204. public int index0;
  205. public int index1;
  206. public int index2;
  207. public int index3;
  208. public float weight0;
  209. public float weight1;
  210. public float weight2;
  211. public float weight3;
  212. }
  213. /** @} */
  214. }