diff --git a/src/SdfEntityCreator.cc b/src/SdfEntityCreator.cc index 082f42db11..6985f292d9 100644 --- a/src/SdfEntityCreator.cc +++ b/src/SdfEntityCreator.cc @@ -85,6 +85,7 @@ #include "gz/sim/components/World.hh" #include "rendering/MaterialParser/MaterialParser.hh" +#include "ServerPrivate.hh" class gz::sim::SdfEntityCreatorPrivate { @@ -808,7 +809,8 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual) "https://gazebosim.org/api/sim/8/migrationsdf.html#:~:text=Materials " << "for details." << std::endl; std::string scriptUri = visualMaterial.ScriptUri(); - if (scriptUri != "file://media/materials/scripts/gazebo.material") { + if (scriptUri != ServerPrivate::kClassicMaterialScriptUri) + { gzwarn << "Custom material scripts are not supported." << std::endl; } diff --git a/src/Server.cc b/src/Server.cc index e2286945e7..86b8b7d1cc 100644 --- a/src/Server.cc +++ b/src/Server.cc @@ -159,7 +159,8 @@ Server::Server(const ServerConfig &_config) // 'src/gui_main.cc'. errors = sdfRoot.Load(filePath, sdfParserConfig); if (errors.empty() || _config.BehaviorOnSdfErrors() != - ServerConfig::SdfErrorBehavior::EXIT_IMMEDIATELY) { + ServerConfig::SdfErrorBehavior::EXIT_IMMEDIATELY) + { if (sdfRoot.Model() == nullptr) { this->dataPtr->sdfRoot = std::move(sdfRoot); } diff --git a/src/ServerPrivate.cc b/src/ServerPrivate.cc index 80df07819e..208b582806 100644 --- a/src/ServerPrivate.cc +++ b/src/ServerPrivate.cc @@ -37,6 +37,9 @@ using namespace gz; using namespace sim; +const char ServerPrivate::kClassicMaterialScriptUri[] = + "file://media/materials/scripts/gazebo.material"; + /// \brief This struct provides access to the record plugin SDF string struct LoggingPlugin { @@ -546,6 +549,12 @@ bool ServerPrivate::ResourcePathsResolveService( ////////////////////////////////////////////////// std::string ServerPrivate::FetchResource(const std::string &_uri) { + // Handle gazebo classic material URIs. + // Return original URI string as the SdfEntityCreator checks for this URI + if (_uri == kClassicMaterialScriptUri) + return _uri; + + // Fetch resource from fuel auto path = fuel_tools::fetchResourceWithClient(_uri, *this->fuelClient.get()); diff --git a/src/ServerPrivate.hh b/src/ServerPrivate.hh index 03128c56e8..cbb298db3f 100644 --- a/src/ServerPrivate.hh +++ b/src/ServerPrivate.hh @@ -187,6 +187,11 @@ namespace gz /// Server. It is used in the SDFormat world generator when saving worlds public: std::unordered_map fuelUriMap; + /// \brief Gazebo classic material URI string + /// A URI matching this string indicates that it is a gazebo classic + /// material. + public: static const char kClassicMaterialScriptUri[]; + /// \brief List of names for all worlds loaded in this server. private: std::vector worldNames; diff --git a/test/integration/material.cc b/test/integration/material.cc index 6d8ca1599f..22567a8a87 100644 --- a/test/integration/material.cc +++ b/test/integration/material.cc @@ -296,3 +296,30 @@ TEST_F(MaterialTest, InvalidColor) EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f), boxVisualComp->Data().Specular()); } + +TEST_F(MaterialTest, WorldWithClassicMaterial) +{ + ServerConfig serverConfig; + serverConfig.SetSdfFile(common::joinPaths(PROJECT_SOURCE_PATH, + "test", "worlds", "classic_material.sdf")); + + std::cout << "Loading: " << serverConfig.SdfFile() << std::endl; + this->StartServer(serverConfig); + + auto model = this->GetModel("box"); + ASSERT_TRUE(model.Valid(*this->ecm)); + + auto boxVisualEntity = + this->ecm->EntityByComponents(components::Name("box_visual")); + ASSERT_NE(kNullEntity, boxVisualEntity); + + // Blue color + auto boxVisualComp = + this->ecm->Component(boxVisualEntity); + EXPECT_EQ(math::Color(0.0f, 0.0f, 1.0f, 1.0f), + boxVisualComp->Data().Ambient()); + EXPECT_EQ(math::Color(0.0f, 0.0f, 1.0f, 1.0f), + boxVisualComp->Data().Diffuse()); + EXPECT_EQ(math::Color(0.1f, 0.1f, 0.1f, 1.0f), + boxVisualComp->Data().Specular()); +} diff --git a/test/worlds/classic_material.sdf b/test/worlds/classic_material.sdf new file mode 100644 index 0000000000..3cf61e6109 --- /dev/null +++ b/test/worlds/classic_material.sdf @@ -0,0 +1,32 @@ + + + + + + + + + + 1 1 1 + + + + + + + + 1 1 1 + + + + + + + + + + +