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

Edit material ambient/specular/diffuse/emissive in component inspector #1123

Merged
merged 11 commits into from
Oct 29, 2021
48 changes: 48 additions & 0 deletions include/ignition/gazebo/components/VisualCmd.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2021 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef IGNITION_GAZEBO_COMPONENTS_VISUALCMD_HH_
#define IGNITION_GAZEBO_COMPONENTS_VISUALCMD_HH_

#include <sdf/Visual.hh>

#include <ignition/gazebo/config.hh>
jennuine marked this conversation as resolved.
Show resolved Hide resolved
#include <ignition/gazebo/Export.hh>
#include <ignition/gazebo/components/Factory.hh>
#include <ignition/gazebo/components/Component.hh>
#include <ignition/gazebo/components/Serialization.hh>

#include <ignition/msgs/visual.pb.h>

namespace ignition
{
namespace gazebo
{
// Inline bracket to help doxygen filtering.
inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
namespace components
{
/// \brief A component type that contains commanded visual of an
/// entity in the world frame represented by msgs::Visual.
using VisualCmd = Component<ignition::msgs::Visual,
class VisualCmdTag, serializers::MsgSerializer>;
IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.VisualCmd",
VisualCmd)
}
}
}
}
#endif
81 changes: 81 additions & 0 deletions src/gui/plugins/component_inspector/ComponentInspector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "ignition/gazebo/components/LinearVelocitySeed.hh"
#include "ignition/gazebo/components/Link.hh"
#include "ignition/gazebo/components/MagneticField.hh"
#include "ignition/gazebo/components/Material.hh"
#include "ignition/gazebo/components/Model.hh"
#include "ignition/gazebo/components/Name.hh"
#include "ignition/gazebo/components/ParentEntity.hh"
Expand Down Expand Up @@ -259,6 +260,39 @@ void ignition::gazebo::setData(QStandardItem *_item, const sdf::Physics &_data)
}), ComponentsModel::RoleNames().key("data"));
}

//////////////////////////////////////////////////
template<>
void ignition::gazebo::setData(QStandardItem *_item,
const sdf::Material &_data)
{
if (nullptr == _item)
return;

_item->setData(QString("Material"),
ComponentsModel::RoleNames().key("dataType"));
_item->setData(QList({
QVariant(_data.Ambient().R()),
QVariant(_data.Ambient().G()),
QVariant(_data.Ambient().B()),
QVariant(_data.Ambient().A()),
QVariant(_data.Diffuse().R()),
QVariant(_data.Diffuse().G()),
QVariant(_data.Diffuse().B()),
QVariant(_data.Diffuse().A()),
QVariant(_data.Specular().R()),
QVariant(_data.Specular().G()),
QVariant(_data.Specular().B()),
QVariant(_data.Specular().A()),
QVariant(_data.Emissive().R()),
QVariant(_data.Emissive().G()),
QVariant(_data.Emissive().B()),
QVariant(_data.Emissive().A())
}), ComponentsModel::RoleNames().key("data"));

// TODO(anyone) Only shows colors of material,
// need to add others (e.g., pbr)
}

//////////////////////////////////////////////////
void ignition::gazebo::setUnit(QStandardItem *_item, const std::string &_unit)
{
Expand Down Expand Up @@ -697,6 +731,15 @@ void ComponentInspector::Update(const UpdateInfo &,
if (comp)
setData(item, comp->Data());
}
else if (typeId == components::Material::typeId)
{
auto comp = _ecm.Component<components::Material>(this->dataPtr->entity);
if (comp)
{
this->SetType("material");
setData(item, comp->Data());
}
}
}

// Remove components no longer present
Expand Down Expand Up @@ -906,6 +949,44 @@ void ComponentInspector::OnPhysics(double _stepSize, double _realTimeFactor)
this->dataPtr->node.Request(physicsCmdService, req, cb);
}

/////////////////////////////////////////////////
void ComponentInspector::OnMaterialColor(
double _rAmbient, double _gAmbient, double _bAmbient, double _aAmbient,
double _rDiffuse, double _gDiffuse, double _bDiffuse, double _aDiffuse,
double _rSpecular, double _gSpecular, double _bSpecular, double _aSpecular,
double _rEmissive, double _gEmissive, double _bEmissive, double _aEmissive)
{
std::function<void(const ignition::msgs::Boolean &, const bool)> cb =
[](const ignition::msgs::Boolean &/*_rep*/, const bool _result)
{
if (!_result)
ignerr << "Error setting material color configuration"
<< " on visual" << std::endl;
};

msgs::Visual req;
req.set_id(this->dataPtr->entity);

msgs::Set(req.mutable_material()->mutable_ambient(),
math::Color(_rAmbient, _gAmbient, _bAmbient, _aAmbient));
msgs::Set(req.mutable_material()->mutable_diffuse(),
math::Color(_rDiffuse, _gDiffuse, _bDiffuse, _aDiffuse));
msgs::Set(req.mutable_material()->mutable_specular(),
math::Color(_rSpecular, _gSpecular, _bSpecular, _aSpecular));
msgs::Set(req.mutable_material()->mutable_emissive(),
math::Color(_rEmissive, _gEmissive, _bEmissive, _aEmissive));

auto materialCmdService = "/world/" + this->dataPtr->worldName
+ "/visual_config";
materialCmdService = transport::TopicUtils::AsValidTopic(materialCmdService);
if (materialCmdService.empty())
{
ignerr << "Invalid material command service topic provided" << std::endl;
return;
}
this->dataPtr->node.Request(materialCmdService, req, cb);
}

/////////////////////////////////////////////////
bool ComponentInspector::NestedModel() const
{
Expand Down
32 changes: 32 additions & 0 deletions src/gui/plugins/component_inspector/ComponentInspector.hh
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ namespace gazebo
template<>
void setData(QStandardItem *_item, const std::ostream &_data);

/// \brief Specialized to set material data.
/// \param[in] _item Item whose data will be set.
/// \param[in] _data Data to set.
template<>
void setData(QStandardItem *_item, const sdf::Material &_data);


/// \brief Set the unit of a given item.
/// \param[in] _item Item whose unit will be set.
/// \param[in] _unit Unit to be displayed, such as 'm' for meters.
Expand Down Expand Up @@ -259,6 +266,31 @@ namespace gazebo
public: Q_INVOKABLE void OnPhysics(double _stepSize,
double _realTimeFactor);

// \brief Callback in Qt thread when material color changes for a visual
/// \param[in] _rAmbient ambient red
/// \param[in] _gAmbient ambient green
/// \param[in] _bAmbient ambient blue
/// \param[in] _aAmbient ambient alpha
/// \param[in] _rDiffuse diffuse red
/// \param[in] _gDiffuse diffuse green
/// \param[in] _bDiffuse diffuse blue
/// \param[in] _aDiffuse diffuse alpha
/// \param[in] _rSpecular specular red
/// \param[in] _gSpecular specular green
/// \param[in] _bSpecular specular blue
/// \param[in] _aSpecular specular alpha
/// \param[in] _rEmissive emissive red
/// \param[in] _gEmissive emissive green
/// \param[in] _bEmissive emissive blue
/// \param[in] _aEmissive emissive alpha
public: Q_INVOKABLE void OnMaterialColor(
double _rAmbient, double _gAmbient, double _bAmbient,
double _aAmbient, double _rDiffuse, double _gDiffuse,
double _bDiffuse, double _aDiffuse, double _rSpecular,
double _gSpecular, double _bSpecular, double _aSpecular,
double _rEmissive, double _gEmissive, double _bEmissive, double _aEmissive
);

/// \brief Get whether the entity is a nested model or not
/// \return True if the entity is a nested model, false otherwise
public: Q_INVOKABLE bool NestedModel() const;
Expand Down
14 changes: 14 additions & 0 deletions src/gui/plugins/component_inspector/ComponentInspector.qml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ Rectangle {
ComponentInspector.OnPhysics(_stepSize, _realTimeFactor)
}

/**
* Forward material color changes to C++
*/
function onMaterialColor(_rAmbient, _gAmbient, _bAmbient, _aAmbient,
_rDiffuse, _gDiffuse, _bDiffuse, _aDiffuse,
_rSpecular, _gSpecular, _bSpecular, _aSpecular,
_rEmissive, _gEmissive, _bEmissive, _aEmissive) {
ComponentInspector.OnMaterialColor(
_rAmbient, _gAmbient, _bAmbient, _aAmbient,
_rDiffuse, _gDiffuse, _bDiffuse, _aDiffuse,
_rSpecular, _gSpecular, _bSpecular, _aSpecular,
_rEmissive, _gEmissive, _bEmissive, _aEmissive)
}

Rectangle {
id: header
height: lockButton.height
Expand Down
1 change: 1 addition & 0 deletions src/gui/plugins/component_inspector/ComponentInspector.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<file>ComponentInspector.qml</file>
<file>Light.qml</file>
<file>NoData.qml</file>
<file>Material.qml</file>
<file>Physics.qml</file>
<file>Pose3d.qml</file>
<file>String.qml</file>
Expand Down
Loading