MeshData.cs 8.7 KB

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