BsPhysXMesh.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #include "BsPhysXMesh.h"
  2. #include "BsPhysXMeshRTTI.h"
  3. #include "BsMeshData.h"
  4. #include "BsVertexDataDesc.h"
  5. #include "BsPhysX.h"
  6. #include "BsAABox.h"
  7. #include "foundation\PxAllocatorCallback.h"
  8. #include "geometry\PxTriangleMesh.h"
  9. #include "geometry\PxConvexMesh.h"
  10. #include "cooking\PxConvexMeshDesc.h"
  11. #include "extensions\PxDefaultStreams.h"
  12. using namespace physx;
  13. namespace BansheeEngine
  14. {
  15. /**
  16. * Attempts to cook a convex mesh from the provided mesh data. Assumes the mesh data is not null and contains vertex
  17. * positions as well as face indices. If the method returns true the resulting convex mesh will be output in the @p
  18. * data buffer, and its size in @p size. The data buffer will be allocated used the generic allocator and is up to the
  19. * caller to free it.
  20. */
  21. bool cookConvex(PxCooking* cooking, const MeshDataPtr& meshData, UINT8** data, UINT32& size)
  22. {
  23. VertexDataDescPtr vertexDesc = meshData->getVertexDesc();
  24. // Generate hull polygons
  25. PxSimpleTriangleMesh meshDesc;
  26. meshDesc.points.count = meshData->getNumVertices();
  27. meshDesc.points.stride = vertexDesc->getVertexStride();
  28. meshDesc.points.data = meshData->getElementData(VES_POSITION);
  29. meshDesc.triangles.count = meshData->getNumIndices() / 3;
  30. meshDesc.triangles.stride = 3 * sizeof(PxU32);
  31. IndexType indexType = meshData->getIndexType();
  32. if (indexType == IT_32BIT)
  33. {
  34. meshDesc.triangles.stride = 4;
  35. meshDesc.triangles.data = meshData->getIndices32();
  36. }
  37. else
  38. {
  39. meshDesc.triangles.stride = 2;
  40. meshDesc.triangles.data = meshData->getIndices16();
  41. meshDesc.flags |= PxMeshFlag::e16_BIT_INDICES;
  42. }
  43. PxAllocatorCallback& allocator = PxGetFoundation().getAllocatorCallback();
  44. PxU32 numVertices = 0;
  45. PxU32 numIndices = 0;
  46. PxU32 numPolygons = 0;
  47. PxVec3* vertices = nullptr;
  48. PxU32* indices = nullptr;
  49. PxHullPolygon* polygons = nullptr;
  50. bool gotPolygons = cooking->computeHullPolygons(meshDesc, allocator,
  51. numVertices, vertices, numIndices, indices, numPolygons, polygons);
  52. // If we have polygons try to create hull directly from them
  53. if(gotPolygons)
  54. {
  55. PxConvexMeshDesc convexPolyDesc;
  56. convexPolyDesc.points.count = numVertices;
  57. convexPolyDesc.points.stride = sizeof(PxVec3);
  58. convexPolyDesc.points.data = vertices;
  59. convexPolyDesc.indices.count = numIndices;
  60. convexPolyDesc.indices.stride = sizeof(PxU32);
  61. convexPolyDesc.indices.data = indices;
  62. convexPolyDesc.polygons.count = numPolygons;
  63. convexPolyDesc.polygons.stride = sizeof(PxHullPolygon);
  64. convexPolyDesc.polygons.data = polygons;
  65. PxDefaultMemoryOutputStream output;
  66. if (cooking->cookConvexMesh(convexPolyDesc, output))
  67. {
  68. size = output.getSize();
  69. *data = (UINT8*)bs_alloc(size);
  70. memcpy(*data, output.getData(), size);
  71. allocator.deallocate(vertices);
  72. allocator.deallocate(indices);
  73. allocator.deallocate(polygons);
  74. return true;
  75. }
  76. allocator.deallocate(vertices);
  77. allocator.deallocate(indices);
  78. allocator.deallocate(polygons);
  79. }
  80. // Try to create hull from points
  81. PxConvexMeshDesc convexDesc;
  82. convexDesc.points.count = meshData->getNumVertices();
  83. convexDesc.points.stride = vertexDesc->getVertexStride();
  84. convexDesc.points.data = meshData->getElementData(VES_POSITION);
  85. convexDesc.flags |= PxConvexFlag::eCOMPUTE_CONVEX;
  86. PxDefaultMemoryOutputStream output;
  87. if (cooking->cookConvexMesh(convexDesc, output))
  88. {
  89. size = output.getSize();
  90. *data = (UINT8*)bs_alloc(size);
  91. memcpy(*data, output.getData(), size);
  92. return true;
  93. }
  94. // Try inflating the convex mesh
  95. convexDesc.flags |= PxConvexFlag::eINFLATE_CONVEX;
  96. if (cooking->cookConvexMesh(convexDesc, output))
  97. {
  98. size = output.getSize();
  99. *data = (UINT8*)bs_alloc(size);
  100. memcpy(*data, output.getData(), size);
  101. return true;
  102. }
  103. // Nothing works, just compute an AABB
  104. AABox box;
  105. auto vertIter = meshData->getVec3DataIter(VES_POSITION);
  106. do
  107. {
  108. box.merge(vertIter.getValue());
  109. }
  110. while (vertIter.moveNext());
  111. Vector3 aabbVerts[8];
  112. aabbVerts[0] = box.getCorner(AABox::FAR_LEFT_BOTTOM);
  113. aabbVerts[1] = box.getCorner(AABox::FAR_RIGHT_BOTTOM);
  114. aabbVerts[2] = box.getCorner(AABox::FAR_RIGHT_TOP);
  115. aabbVerts[3] = box.getCorner(AABox::FAR_LEFT_TOP);
  116. aabbVerts[4] = box.getCorner(AABox::NEAR_LEFT_BOTTOM);
  117. aabbVerts[5] = box.getCorner(AABox::NEAR_RIGHT_BOTTOM);
  118. aabbVerts[6] = box.getCorner(AABox::NEAR_RIGHT_TOP);
  119. aabbVerts[7] = box.getCorner(AABox::NEAR_LEFT_TOP);
  120. convexDesc.points.count = 8;
  121. convexDesc.points.stride = sizeof(Vector3);
  122. convexDesc.points.data = &aabbVerts[0];
  123. convexDesc.flags &= ~PxConvexFlag::eINFLATE_CONVEX;
  124. if (cooking->cookConvexMesh(convexDesc, output))
  125. {
  126. size = output.getSize();
  127. *data = (UINT8*)bs_alloc(size);
  128. memcpy(*data, output.getData(), size);
  129. return true;
  130. }
  131. return false;
  132. }
  133. /**
  134. * Attempts to cook a triangle or convex mesh from the provided mesh data. Will log a warning and return false if it is
  135. * unable to cook the mesh. If the method returns true the resulting convex mesh will be output in the @p data buffer,
  136. * and its size in @p size. The data buffer will be allocated used the generic allocator and is up to the caller to
  137. * free it.
  138. */
  139. bool cookMesh(const MeshDataPtr& meshData, PhysicsMeshType type, UINT8** data, UINT32& size)
  140. {
  141. if (meshData == nullptr)
  142. return false;
  143. PxCooking* cooking = gPhysX().getCooking();
  144. if (cooking == nullptr)
  145. {
  146. LOGWRN("Attempting to cook a physics mesh but cooking is not enabled globally.");
  147. return false;
  148. }
  149. VertexDataDescPtr vertexDesc = meshData->getVertexDesc();
  150. if (!vertexDesc->hasElement(VES_POSITION))
  151. {
  152. LOGWRN("Provided PhysicsMesh mesh data has no vertex positions.");
  153. return false;
  154. }
  155. if (type == PhysicsMeshType::Convex)
  156. {
  157. if(!cookConvex(cooking, meshData, data, size))
  158. {
  159. LOGWRN("Failed cooking a convex mesh. Perpahs it is too complex? Maximum number of convex vertices is 256.");
  160. return false;
  161. }
  162. }
  163. else
  164. {
  165. PxTriangleMeshDesc meshDesc;
  166. meshDesc.points.count = meshData->getNumVertices();
  167. meshDesc.points.stride = vertexDesc->getVertexStride();
  168. meshDesc.points.data = meshData->getElementData(VES_POSITION);
  169. meshDesc.triangles.count = meshData->getNumIndices() / 3;
  170. meshDesc.triangles.stride = 3 * sizeof(PxU32);
  171. IndexType indexType = meshData->getIndexType();
  172. if (indexType == IT_32BIT)
  173. {
  174. meshDesc.triangles.stride = 4;
  175. meshDesc.triangles.data = meshData->getIndices32();
  176. }
  177. else
  178. {
  179. meshDesc.triangles.stride = 2;
  180. meshDesc.triangles.data = meshData->getIndices16();
  181. meshDesc.flags |= PxMeshFlag::e16_BIT_INDICES;
  182. }
  183. PxDefaultMemoryOutputStream output;
  184. if (!cooking->cookTriangleMesh(meshDesc, output))
  185. return false;
  186. size = output.getSize();
  187. *data = (UINT8*)bs_alloc(size);
  188. memcpy(*data, output.getData(), size);
  189. }
  190. return true;
  191. }
  192. PhysXMesh::PhysXMesh(const MeshDataPtr& meshData, PhysicsMeshType type)
  193. :PhysicsMesh(meshData, type)
  194. { }
  195. void PhysXMesh::initialize()
  196. {
  197. // Perform cooking if needed
  198. if (mInitMeshData != nullptr)
  199. cookMesh(mInitMeshData, mType, &mCookedData, mCookedDataSize);
  200. if (mCookedData != nullptr && mCookedDataSize > 0)
  201. {
  202. PxPhysics* physx = gPhysX().getPhysX();
  203. PxDefaultMemoryInputData input(mCookedData, mCookedDataSize);
  204. if (mType == PhysicsMeshType::Convex)
  205. mConvexMesh = physx->createConvexMesh(input);
  206. else
  207. mTriangleMesh = physx->createTriangleMesh(input);
  208. }
  209. PhysicsMesh::initialize();
  210. }
  211. void PhysXMesh::destroy()
  212. {
  213. if(mCookedData != nullptr)
  214. {
  215. bs_free(mCookedData);
  216. mCookedData = nullptr;
  217. mCookedDataSize = 0;
  218. }
  219. if(mTriangleMesh != nullptr)
  220. {
  221. mTriangleMesh->release();
  222. mTriangleMesh = nullptr;
  223. }
  224. if (mConvexMesh != nullptr)
  225. {
  226. mConvexMesh->release();
  227. mConvexMesh = nullptr;
  228. }
  229. PhysicsMesh::destroy();
  230. }
  231. MeshDataPtr PhysXMesh::getMeshData() const
  232. {
  233. VertexDataDescPtr vertexDesc = VertexDataDesc::create();
  234. vertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  235. if (mConvexMesh == nullptr && mTriangleMesh == nullptr)
  236. return MeshData::create(0, 0, vertexDesc);
  237. UINT32 numVertices = 0;
  238. UINT32 numIndices = 0;
  239. if(mConvexMesh != nullptr)
  240. {
  241. numVertices = mConvexMesh->getNbVertices();
  242. UINT32 numPolygons = mConvexMesh->getNbPolygons();
  243. for (UINT32 i = 0; i < numPolygons; i++)
  244. {
  245. PxHullPolygon face;
  246. bool status = mConvexMesh->getPolygonData(i, face);
  247. assert(status);
  248. numIndices += (face.mNbVerts - 2) * 3;
  249. }
  250. }
  251. else // Triangle
  252. {
  253. numVertices = mTriangleMesh->getNbVertices();
  254. numIndices = mTriangleMesh->getNbTriangles() * 3;
  255. }
  256. MeshDataPtr meshData = MeshData::create(numVertices, numIndices, vertexDesc);
  257. auto posIter = meshData->getVec3DataIter(VES_POSITION);
  258. UINT32* outIndices = meshData->getIndices32();
  259. if (mConvexMesh != nullptr)
  260. {
  261. const PxVec3* convexVertices = mConvexMesh->getVertices();
  262. const UINT8* convexIndices = mConvexMesh->getIndexBuffer();
  263. UINT32 numPolygons = mConvexMesh->getNbPolygons();
  264. UINT32 offset = 0;
  265. for (UINT32 i = 0; i < numPolygons; i++)
  266. {
  267. PxHullPolygon face;
  268. bool status = mConvexMesh->getPolygonData(i, face);
  269. assert(status);
  270. const PxU8* faceIndices = convexIndices + face.mIndexBase;
  271. for (UINT32 j = 0; j < face.mNbVerts; j++)
  272. posIter.addValue(fromPxVector(convexVertices[faceIndices[j]]));
  273. for (UINT32 j = 2; j < face.mNbVerts; j++)
  274. {
  275. *outIndices++ = offset;
  276. *outIndices++ = offset + j;
  277. *outIndices++ = offset + j - 1;
  278. }
  279. offset += face.mNbVerts;
  280. }
  281. }
  282. else
  283. {
  284. const PxVec3* vertices = mTriangleMesh->getVertices();
  285. for (UINT32 i = 0; i < numVertices; i++)
  286. posIter.addValue(fromPxVector(vertices[i]));
  287. if(mTriangleMesh->getTriangleMeshFlags() & PxTriangleMeshFlag::e16_BIT_INDICES)
  288. {
  289. const UINT16* indices = (const UINT16*)mTriangleMesh->getTriangles();
  290. for (UINT32 i = 0; i < numIndices; i++)
  291. outIndices[i] = (UINT32)indices[i];
  292. }
  293. else
  294. {
  295. const UINT32* indices = (const UINT32*)mTriangleMesh->getTriangles();
  296. memcpy(outIndices, indices, numIndices * sizeof(UINT32));
  297. }
  298. }
  299. return meshData;
  300. }
  301. RTTITypeBase* PhysXMesh::getRTTIStatic()
  302. {
  303. return PhysXMeshRTTI::instance();
  304. }
  305. RTTITypeBase* PhysXMesh::getRTTI() const
  306. {
  307. return getRTTIStatic();
  308. }
  309. }