diff --git a/graphics/include/gz/common/MeshManager.hh b/graphics/include/gz/common/MeshManager.hh index 5185dd4c7..07e770cce 100644 --- a/graphics/include/gz/common/MeshManager.hh +++ b/graphics/include/gz/common/MeshManager.hh @@ -45,7 +45,11 @@ namespace gz class SubMesh; /// \class MeshManager MeshManager.hh gz/common/MeshManager.hh - /// \brief Maintains and manages all meshes + /// \brief Maintains and manages all meshes. Supported mesh formats are + /// STL (STLA, STLB), COLLADA, OBJ, GLTF (GLB) and FBX. By default only GLTF + /// and FBX are loaded using assimp loader, however if GZ_MESH_FORCE_ASSIMP + /// environment variable is set, then MeshManager will use assimp loader for + /// all supported mesh formats. class GZ_COMMON_GRAPHICS_VISIBLE MeshManager : public SingletonT { @@ -240,8 +244,12 @@ namespace gz const gz::math::Vector2d &_segments, const gz::math::Vector2d &_uvTile); + /// \brief Sets the forceAssimp flag by reading the GZ_MESH_FORCE_ASSIMP + /// environment variable. If forceAssimp true, MeshManager uses Assimp + /// for loading all mesh formats, otherwise only for GLTF and FBX. + public: void SetAssimpEnvs(); + /// \brief Tesselate a 2D mesh - /// /// Makes a zigzag pattern compatible with strips /// \param[in] _sm the mesh to tesselate /// \param[in] _meshWith mesh width diff --git a/graphics/src/MeshManager.cc b/graphics/src/MeshManager.cc index 5148b78cd..085dbb623 100644 --- a/graphics/src/MeshManager.cc +++ b/graphics/src/MeshManager.cc @@ -71,6 +71,9 @@ class gz::common::MeshManager::Implementation /// \brief Mutex to protect the mesh map public: std::mutex mutex; + + /// \brief True if assimp is used for loading all supported mesh formats + public: bool forceAssimp; #ifdef _WIN32 #pragma warning(pop) #endif @@ -146,19 +149,26 @@ const Mesh *MeshManager::Load(const std::string &_filename) std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower); MeshLoader *loader = nullptr; - - if (extension == "stl" || extension == "stlb" || extension == "stla") - loader = &this->dataPtr->stlLoader; - else if (extension == "dae") - loader = &this->dataPtr->colladaLoader; - else if (extension == "obj") - loader = &this->dataPtr->objLoader; - else if (extension == "gltf" || extension == "glb" || extension == "fbx") + this->SetAssimpEnvs(); + if (this->dataPtr->forceAssimp) + { loader = &this->dataPtr->assimpLoader; + } else { - gzerr << "Unsupported mesh format for file[" << _filename << "]\n"; - return nullptr; + if (extension == "stl" || extension == "stlb" || extension == "stla") + loader = &this->dataPtr->stlLoader; + else if (extension == "dae") + loader = &this->dataPtr->colladaLoader; + else if (extension == "obj") + loader = &this->dataPtr->objLoader; + else if (extension == "gltf" || extension == "glb" || extension == "fbx") + loader = &this->dataPtr->assimpLoader; + else + { + gzerr << "Unsupported mesh format for file[" << _filename << "]\n"; + return nullptr; + } } // This mutex prevents two threads from loading the same mesh at the // same time. @@ -1619,3 +1629,16 @@ void MeshManager::ConvertPolylinesToVerticesAndEdges( } } } + +////////////////////////////////////////////////// +void MeshManager::SetAssimpEnvs() +{ + std::string forceAssimpEnv; + common::env("GZ_MESH_FORCE_ASSIMP", forceAssimpEnv); + this->dataPtr->forceAssimp = false; + if (forceAssimpEnv == "true") + { + gzmsg << "Using assimp to load all mesh formats" << std::endl; + this->dataPtr->forceAssimp = true; + } +}