MeshData.cs 9.1 KB

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