diff --git a/src/SdfEntityCreator.cc b/src/SdfEntityCreator.cc index ef60860691..f4b693533b 100644 --- a/src/SdfEntityCreator.cc +++ b/src/SdfEntityCreator.cc @@ -817,17 +817,20 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual) std::optional parsed = this->dataPtr->materialParser.GetMaterialValues(scriptName); - if(!parsed.has_value()) { + 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, using default." << std::endl; + " not recognized or supported, using default." << std::endl; } - - visualMaterial.SetAmbient - (parsed->ambient.value_or(visualMaterial.Ambient())); - visualMaterial.SetDiffuse - (parsed->diffuse.value_or(visualMaterial.Diffuse())); - visualMaterial.SetSpecular - (parsed->specular.value_or(visualMaterial.Specular())); } } this->dataPtr->ecm->CreateComponent(visualEntity, diff --git a/src/rendering/MaterialParser/MaterialParser.cc b/src/rendering/MaterialParser/MaterialParser.cc index 36bf006306..7983d25a68 100644 --- a/src/rendering/MaterialParser/MaterialParser.cc +++ b/src/rendering/MaterialParser/MaterialParser.cc @@ -42,7 +42,7 @@ void MaterialParser::Load() std::optional MaterialParser::GetMaterialValues( const std::string& material) { - MaterialValues values; + std::optional values = std::nullopt; std::map scripts = this->configLoader.getAllConfigScripts(); @@ -51,31 +51,29 @@ std::optional MaterialParser::GetMaterialValues( for (it = scripts.begin(); it != scripts.end(); ++it) { std::string name = it->first; if (name.find(material) != std::string::npos) { + if(!values) { + values = MaterialValues(); + } ConfigNode * node = it->second; ConfigNode * techniqueNode = node->findChild("technique"); if (techniqueNode) { ConfigNode * passNode = techniqueNode->findChild("pass"); if (passNode) { - std::size_t index = name.rfind(" "); - if (index != std::string::npos) { - name = name.substr(index + 1); - } - ConfigNode * ambientNode = passNode->findChild("ambient"); if (ambientNode) { std::vector ambientValues; ambientNode->getValuesInFloat(ambientValues); - values.ambient = gz::math::Color(ambientValues[0], - ambientValues[1], ambientValues[2]); + values->ambient.emplace(gz::math::Color(ambientValues[0], + ambientValues[1], ambientValues[2])); } ConfigNode * diffuseNode = passNode->findChild("diffuse"); if (diffuseNode) { std::vector diffuseValues; diffuseNode->getValuesInFloat(diffuseValues); - values.diffuse = gz::math::Color(diffuseValues[0], - diffuseValues[1], diffuseValues[2]); + values->diffuse.emplace(gz::math::Color(diffuseValues[0], + diffuseValues[1], diffuseValues[2])); } ConfigNode * specularNode = passNode->findChild("specular"); @@ -84,8 +82,8 @@ std::optional MaterialParser::GetMaterialValues( specularNode->getValuesInFloat(specularValues); // Using first four values for specular as // Gazebo doesn't support shininess - values.specular = gz::math::Color(specularValues[0], - specularValues[1], specularValues[2], specularValues[3]); + values->specular.emplace(gz::math::Color(specularValues[0], + specularValues[1], specularValues[2], specularValues[3])); } } }