diff --git a/cocos/3d/CCBundle3D.cpp b/cocos/3d/CCBundle3D.cpp index 22b4346aee..247cf35e29 100644 --- a/cocos/3d/CCBundle3D.cpp +++ b/cocos/3d/CCBundle3D.cpp @@ -2111,6 +2111,29 @@ Reference* Bundle3D::seekToFirstType(unsigned int type, const std::string& id) return nullptr; } +std::vector Bundle3D::getTrianglesList(const std::string& path) +{ + std::vector trianglesList; + auto bundle = Bundle3D::createBundle(); + if (!bundle->load(path)) + { + Bundle3D::destroyBundle(bundle); + return trianglesList; + } + MeshDatas meshs; + bundle->loadMeshDatas(meshs); + Bundle3D::destroyBundle(bundle); + for (auto iter : meshs.meshDatas){ + int preVertexSize = iter->getPerVertexSize() / sizeof(float); + for (auto indexArray : iter->subMeshIndices){ + for (auto i : indexArray){ + trianglesList.push_back(Vec3(iter->vertex[i * preVertexSize], iter->vertex[i * preVertexSize + 1], iter->vertex[i * preVertexSize + 2])); + } + } + } + return trianglesList; +} + Bundle3D::Bundle3D() : _modelPath(""), _path(""), diff --git a/cocos/3d/CCBundle3D.h b/cocos/3d/CCBundle3D.h index f9927eeac3..60d681931a 100644 --- a/cocos/3d/CCBundle3D.h +++ b/cocos/3d/CCBundle3D.h @@ -85,6 +85,12 @@ public: //since 3.3, to support reskin virtual bool loadMaterials(MaterialDatas& materialdatas); + /** + * load triangle list + * @param path the file path to load + */ + static std::vector getTrianglesList(const std::string& path); + //load .obj file static bool loadObj(MeshDatas& meshdatas, MaterialDatas& materialdatas, NodeDatas& nodedatas, const std::string& fullPath, const char* mtl_basepath = nullptr); diff --git a/tests/cpp-tests/Classes/Physics3DTest/Physics3DTest.cpp b/tests/cpp-tests/Classes/Physics3DTest/Physics3DTest.cpp index 9a44f80a64..4ffd804477 100644 --- a/tests/cpp-tests/Classes/Physics3DTest/Physics3DTest.cpp +++ b/tests/cpp-tests/Classes/Physics3DTest/Physics3DTest.cpp @@ -626,20 +626,7 @@ bool Physics3DTerrainDemo::init() } //create mesh - std::vector trianglesList; - auto bundle = Bundle3D::createBundle(); - bundle->load("Sprite3DTest/boss.c3b"); - MeshDatas meshs; - bundle->loadMeshDatas(meshs); - Bundle3D::destroyBundle(bundle); - for (auto iter : meshs.meshDatas){ - int preVertexSize = iter->getPerVertexSize() / sizeof(float); - for (auto indexArray : iter->subMeshIndices){ - for (auto i : indexArray){ - trianglesList.push_back(Vec3(iter->vertex[i * preVertexSize], iter->vertex[i * preVertexSize + 1], iter->vertex[i * preVertexSize + 2])); - } - } - } + std::vector trianglesList = Bundle3D::getTrianglesList("Sprite3DTest/boss.c3b"); rbDes.mass = 0.0f; rbDes.shape = Physics3DShape::createMesh(&trianglesList[0], (int)trianglesList.size() / 3); @@ -707,20 +694,7 @@ bool Physics3DCollisionCallbackDemo::init() Physics3DRigidBodyDes rbDes; float scale = 2.0f; - std::vector trianglesList; - auto bundle = Bundle3D::createBundle(); - bundle->load("Sprite3DTest/boss.c3b"); - MeshDatas meshs; - bundle->loadMeshDatas(meshs); - Bundle3D::destroyBundle(bundle); - for (auto iter : meshs.meshDatas){ - int preVertexSize = iter->getPerVertexSize() / sizeof(float); - for (auto indexArray : iter->subMeshIndices){ - for (auto i : indexArray){ - trianglesList.push_back(Vec3(iter->vertex[i * preVertexSize], iter->vertex[i * preVertexSize + 1], iter->vertex[i * preVertexSize + 2]) * scale); - } - } - } + std::vector trianglesList = Bundle3D::getTrianglesList("Sprite3DTest/boss.c3b"); rbDes.mass = 0.0f; rbDes.shape = Physics3DShape::createMesh(&trianglesList[0], (int)trianglesList.size() / 3);