Skip to content

Commit

Permalink
Support for Gazebo materials (#2269)
Browse files Browse the repository at this point in the history
* test script tag

Signed-off-by: Dharini Dutia <[email protected]>

* add dark grey before creating component

Signed-off-by: Dharini Dutia <[email protected]>

* material parser

Signed-off-by: Dharini Dutia <[email protected]>

* linters

Signed-off-by: Dharini Dutia <[email protected]>

* Update src/rendering/MaterialParser/MaterialParser.cc

Co-authored-by: Alejandro Hernández Cordero <[email protected]>
Signed-off-by: Dharini Dutia <[email protected]>
Signed-off-by: Dharini Dutia <[email protected]>

* fix configLoader, material struct and feedback

Signed-off-by: Dharini Dutia <[email protected]>

* default color and todos

Signed-off-by: Dharini Dutia <[email protected]>

* install/load one file, range based loop, hardcode dependent solid colors

Signed-off-by: Dharini Dutia <[email protected]>

* fix install_dir property

Signed-off-by: Dharini Dutia <[email protected]>

* credits, initializing cleanup

Signed-off-by: Dharini Dutia <[email protected]>

* eof

Signed-off-by: Dharini Dutia <[email protected]>

* reformat

Signed-off-by: Dharini Dutia <[email protected]>

* add integration test

Signed-off-by: Dharini Dutia <[email protected]>

* migration note

Signed-off-by: Dharini Dutia <[email protected]>

* intends, default case, invalid color

Signed-off-by: Dharini Dutia <[email protected]>

* optional materialValues, typo

Signed-off-by: Dharini Dutia <[email protected]>

* feedback

Signed-off-by: Dharini Dutia <[email protected]>

* size check

Signed-off-by: Dharini Dutia <[email protected]>

* get color values

Signed-off-by: Dharini Dutia <[email protected]>

* migration doc update

Signed-off-by: Dharini Dutia <[email protected]>

---------

Signed-off-by: Dharini Dutia <[email protected]>
Signed-off-by: Dharini Dutia <[email protected]>
Co-authored-by: Alejandro Hernández Cordero <[email protected]>
  • Loading branch information
quarkytale and ahcorde authored Jan 12, 2024
1 parent 82969d5 commit c6aaaf2
Show file tree
Hide file tree
Showing 12 changed files with 2,832 additions and 11 deletions.
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 " <<
"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

0 comments on commit c6aaaf2

Please sign in to comment.