Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Gazebo materials #2269

Merged
merged 21 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/gz/sim/InstallationDirectories.hh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ namespace gz

/// \brief getWorldInstallDir return the world install dir
GZ_SIM_VISIBLE std::string getWorldInstallDir();

/// \brief getMediaInstallDir return the media install dir
GZ_SIM_VISIBLE std::string getMediaInstallDir();
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ set(cli_sources
cmd/ModelCommandAPI.cc
)

set(material_sources
rendering/MaterialParser/MaterialParser.cc
rendering/MaterialParser/ConfigLoader.cc
)

set (sources
Actor.cc
Barrier.cc
Expand Down Expand Up @@ -92,6 +97,7 @@ set (sources
${network_sources}
${comms_sources}
${msgs_sources}
${material_sources}
)

set (gtest_sources
Expand Down Expand Up @@ -200,7 +206,9 @@ set_property(
GZ_SIM_PLUGIN_RELATIVE_INSTALL_DIR="${GZ_LIB_INSTALL_DIR}/gz-${GZ_DESIGNATION}-${PROJECT_VERSION_MAJOR}/plugins"
GZ_SIM_GUI_PLUGIN_RELATIVE_INSTALL_DIR="${GZ_LIB_INSTALL_DIR}/gz-${GZ_DESIGNATION}-${PROJECT_VERSION_MAJOR}/plugins/gui"
GZ_SIM_WORLD_RELATIVE_INSTALL_DIR="${GZ_DATA_INSTALL_DIR}/worlds"
GZ_SIM_MEDIA_RELATIVE_INSTALL_DIR="${GZ_DATA_INSTALL_DIR}/media"
)
install(FILES "rendering/MaterialParser/gazebo.material" DESTINATION ${GZ_DATA_INSTALL_DIR}/media)

target_link_libraries(${PROJECT_LIBRARY_TARGET_NAME}
PUBLIC
Expand Down
6 changes: 6 additions & 0 deletions src/InstallationDirectories.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ std::string getWorldInstallDir()
getInstallPrefix(), GZ_SIM_WORLD_RELATIVE_INSTALL_DIR);
}

std::string getMediaInstallDir()
{
return gz::common::joinPaths(
getInstallPrefix(), GZ_SIM_MEDIA_RELATIVE_INSTALL_DIR);
}

}
}
}
47 changes: 46 additions & 1 deletion src/SdfEntityCreator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
#include "gz/sim/components/WindMode.hh"
#include "gz/sim/components/World.hh"

#include "rendering/MaterialParser/MaterialParser.hh"

class gz::sim::SdfEntityCreatorPrivate
{
/// \brief Pointer to entity component manager. We don't assume ownership.
Expand All @@ -103,6 +105,9 @@ class gz::sim::SdfEntityCreatorPrivate
/// \brief Keep track of new visuals being added, so we load their plugins
/// only after we have their scoped name.
public: std::map<Entity, sdf::Plugins> newVisuals;

/// \brief Parse Gazebo defined materials for visuals
public: MaterialParser materialParser;
};

using namespace gz;
Expand Down Expand Up @@ -193,6 +198,7 @@ SdfEntityCreator::SdfEntityCreator(EntityComponentManager &_ecm,
{
this->dataPtr->ecm = &_ecm;
this->dataPtr->eventManager = &_eventManager;
this->dataPtr->materialParser.Load();
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -788,8 +794,47 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual)
// \todo(louise) Populate with default material if undefined
if (_visual->Material())
{
sdf::Material visualMaterial = *_visual->Material();
if (!_visual->Material()->ScriptUri().empty())
{
gzwarn << "Gazebo does not support Ogre material scripts. See " <<
"https://gazebosim.org/api/sim/8/migrationsdf.html#:~:text=Materials " <<
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the #:~:text=Materials part work? It takes me to the migration page but not the Materials section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its using text fragments, and mostly browser dependent. For me, it highlights and scrolls to the Material section but scrolls back up the page. Do you think it would be useful?

Copy link
Contributor

@iche033 iche033 Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok I thought it would jump to that section. Yes I see it highlighted. We can leave the text fragment here.

"for details." << std::endl;
std::string scriptUri = visualMaterial.ScriptUri();
if (scriptUri != "file://media/materials/scripts/gazebo.material") {
gzwarn << "Custom material scripts are not supported."
<< std::endl;
}
}
if (!_visual->Material()->ScriptName().empty())
{
std::string scriptName = visualMaterial.ScriptName();

if ((scriptName.find("Gazebo/") == 0u))
{
gzwarn << "Using an internal gazebo.material to parse "
<< scriptName << std::endl;
std::optional<MaterialParser::MaterialValues> parsed =
this->dataPtr->materialParser.GetMaterialValues(scriptName);

if(parsed.has_value())
{
visualMaterial.SetAmbient
(parsed->ambient.value_or(visualMaterial.Ambient()));
visualMaterial.SetDiffuse
(parsed->diffuse.value_or(visualMaterial.Diffuse()));
visualMaterial.SetSpecular
(parsed->specular.value_or(visualMaterial.Specular()));
}
else
{
gzwarn << "Material " << scriptName <<
" not recognized or supported, using default." << std::endl;
}
}
}
this->dataPtr->ecm->CreateComponent(visualEntity,
components::Material(*_visual->Material()));
components::Material(visualMaterial));
}

// store the plugin in a component
Expand Down
Loading