MeshData.cs 9.1 KB

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