Mesh.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. namespace BansheeEngine
  5. {
  6. public class Mesh : Resource
  7. {
  8. public Mesh(int numVertices, int numIndices, MeshTopology topology = MeshTopology.TriangleList,
  9. MeshUsage usage = MeshUsage.Default, VertexType vertex = VertexType.Position,
  10. IndexType index = IndexType.Index32)
  11. {
  12. SubMesh[] subMeshes = {new SubMesh(0, numIndices, topology)};
  13. Internal_CreateInstance(this, numVertices, numIndices, subMeshes, usage, vertex, index);
  14. }
  15. public Mesh(int numVertices, int numIndices, SubMesh[] subMeshes, MeshUsage usage = MeshUsage.Default,
  16. VertexType vertex = VertexType.Position, IndexType index = IndexType.Index32)
  17. {
  18. Internal_CreateInstance(this, numVertices, numIndices, subMeshes, usage, vertex, index);
  19. }
  20. public Mesh(MeshData data, MeshTopology topology = MeshTopology.TriangleList, MeshUsage usage = MeshUsage.Default)
  21. {
  22. int numIndices = 0;
  23. IntPtr dataPtr = IntPtr.Zero;
  24. if (data != null)
  25. {
  26. numIndices = data.IndexCount;
  27. dataPtr = data.GetCachedPtr();
  28. }
  29. SubMesh[] subMeshes = { new SubMesh(0, numIndices, topology) };
  30. Internal_CreateInstanceMeshData(this, dataPtr, subMeshes, usage);
  31. }
  32. public Mesh(MeshData data, SubMesh[] subMeshes, MeshUsage usage = MeshUsage.Default)
  33. {
  34. IntPtr dataPtr = IntPtr.Zero;
  35. if (data != null)
  36. dataPtr = data.GetCachedPtr();
  37. Internal_CreateInstanceMeshData(this, dataPtr, subMeshes, usage);
  38. }
  39. public int SubMeshCount
  40. {
  41. get { return Internal_GetSubMeshCount(mCachedPtr); }
  42. }
  43. public SubMesh[] SubMeshes
  44. {
  45. get { return Internal_GetSubMeshes(mCachedPtr); }
  46. }
  47. public Bounds Bounds
  48. {
  49. get
  50. {
  51. AABox box;
  52. Sphere sphere;
  53. Internal_GetBounds(mCachedPtr, out box, out sphere);
  54. return new Bounds(box, sphere);
  55. }
  56. }
  57. public MeshData GetMeshData()
  58. {
  59. return Internal_GetMeshData(mCachedPtr);
  60. }
  61. public void SetMeshData(MeshData data)
  62. {
  63. IntPtr dataPtr = IntPtr.Zero;
  64. if (data != null)
  65. dataPtr = data.GetCachedPtr();
  66. Internal_SetMeshData(mCachedPtr, dataPtr);
  67. }
  68. [MethodImpl(MethodImplOptions.InternalCall)]
  69. private static extern void Internal_CreateInstance(Mesh instance, int numVertices,
  70. int numIndices, SubMesh[] subMeshes, MeshUsage usage, VertexType vertex, IndexType index);
  71. [MethodImpl(MethodImplOptions.InternalCall)]
  72. private static extern void Internal_CreateInstanceMeshData(Mesh instance, IntPtr data, SubMesh[] subMeshes,
  73. MeshUsage usage);
  74. [MethodImpl(MethodImplOptions.InternalCall)]
  75. private static extern SubMesh[] Internal_GetSubMeshes(IntPtr thisPtr);
  76. [MethodImpl(MethodImplOptions.InternalCall)]
  77. private static extern int Internal_GetSubMeshCount(IntPtr thisPtr);
  78. [MethodImpl(MethodImplOptions.InternalCall)]
  79. private static extern void Internal_GetBounds(IntPtr thisPtr, out AABox box, out Sphere sphere);
  80. [MethodImpl(MethodImplOptions.InternalCall)]
  81. private static extern MeshData Internal_GetMeshData(IntPtr thisPtr);
  82. [MethodImpl(MethodImplOptions.InternalCall)]
  83. private static extern void Internal_SetMeshData(IntPtr thisPtr, IntPtr value);
  84. }
  85. [StructLayout(LayoutKind.Sequential)]
  86. public struct SubMesh
  87. {
  88. public SubMesh(int indexOffset, int indexCount, MeshTopology topology = MeshTopology.TriangleList)
  89. {
  90. IndexOffset = indexOffset;
  91. IndexCount = indexCount;
  92. Topology = topology;
  93. }
  94. public int IndexOffset;
  95. public int IndexCount;
  96. public MeshTopology Topology;
  97. }
  98. // Note: Values must match C++ enum MeshTopology
  99. public enum MeshTopology
  100. {
  101. PointList = 1,
  102. LineList = 2,
  103. LineStrip = 3,
  104. TriangleList = 4,
  105. TriangleStrip = 5,
  106. TriangleFan = 6
  107. }
  108. // Note: Do not modify IDs as they must match TextureUsage C++ enum
  109. public enum MeshUsage
  110. {
  111. Default = 0x1,
  112. Dynamic = 0x2,
  113. CPUCached = 0x1000
  114. }
  115. }