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

Backport material support to garden #2313

Merged
merged 3 commits into from
Mar 26, 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 @@ -46,6 +46,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 @@ -78,6 +83,7 @@ set (sources
${network_sources}
${comms_sources}
${component_sources}
${material_sources}
)

set (gtest_sources
Expand Down Expand Up @@ -186,7 +192,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 @@ -95,6 +95,8 @@
#include "gz/sim/components/World.hh"
#endif

#include "rendering/MaterialParser/MaterialParser.hh"

class gz::sim::SdfEntityCreatorPrivate
{
/// \brief Pointer to entity component manager. We don't assume ownership.
Expand All @@ -114,6 +116,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 @@ -204,6 +209,7 @@ SdfEntityCreator::SdfEntityCreator(EntityComponentManager &_ecm,
{
this->dataPtr->ecm = &_ecm;
this->dataPtr->eventManager = &_eventManager;
this->dataPtr->materialParser.Load();
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -834,8 +840,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 " <<
"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
Loading