From b6566a7891dd320bb3e39b1b781b461f13992171 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Fri, 18 Feb 2022 00:13:45 +0100 Subject: [PATCH 1/2] Added light to threadsafe branch Signed-off-by: ahcorde --- source/ignition_live/Scene.cpp | 90 +++++++++++++++++++++++++++++++--- source/ignition_live/Scene.hpp | 2 + 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/source/ignition_live/Scene.cpp b/source/ignition_live/Scene.cpp index d153300..3833f8e 100644 --- a/source/ignition_live/Scene.cpp +++ b/source/ignition_live/Scene.cpp @@ -24,6 +24,9 @@ #include #include +#include +#include +#include #include #include @@ -296,6 +299,16 @@ bool Scene::UpdateLink(const ignition::msgs::Link &_link, return false; } } + + for (const auto &light : _link.light()) + { + if (!this->UpdateLights(light, usdLinkPath + "/" + light.name())) + { + ignerr << "Failed to add light [" << usdLinkPath + "/" + light.name() << "]" << std::endl; + return false; + } + } + return true; } @@ -381,17 +394,82 @@ bool Scene::UpdateScene(const ignition::msgs::Scene &_scene) for (const auto &light : _scene.light()) { - // TODO: This is just a stub remove warnings when updating poses - auto stage = this->stage.Lock(); - auto xform = pxr::UsdGeomXform::Define( - *stage, pxr::SdfPath("/" + worldName + "/" + light.name())); - pxr::UsdGeomXformCommonAPI xformApi(xform); - this->entities[light.id()] = xform.GetPrim(); + if (!this->UpdateLights(light, "/" + worldName + "/" + light.name())) + { + ignerr << "Failed to add light [" << light.name() << "]" << std::endl; + return false; + } } return true; } +////////////////////////////////////////////////// +bool Scene::UpdateLights(const ignition::msgs::Light &_light, + const std::string &_usdLightPath) +{ + auto stage = this->stage.Lock(); + + const pxr::SdfPath sdfLightPath(_usdLightPath); + switch (_light.type()) + { + case ignition::msgs::Light::POINT: + { + auto pointLight = + pxr::UsdLuxSphereLight::Define(*stage, sdfLightPath); + pointLight.CreateTreatAsPointAttr().Set(true); + this->entities[_light.id()] = pointLight.GetPrim(); + pointLight.CreateRadiusAttr(pxr::VtValue(0.1f)); + pointLight.CreateColorAttr( + pxr::VtValue( + pxr::GfVec3f( + _light.diffuse().r(), + _light.diffuse().g(), + _light.diffuse().b()))); + break; + } + case ignition::msgs::Light::SPOT: + { + auto diskLight = pxr::UsdLuxDiskLight::Define(*stage, sdfLightPath); + this->entities[_light.id()] = diskLight.GetPrim(); + diskLight.CreateColorAttr( + pxr::VtValue( + pxr::GfVec3f( + _light.diffuse().r(), + _light.diffuse().g(), + _light.diffuse().b()))); + break; + } + case ignition::msgs::Light::DIRECTIONAL: + { + auto directionalLight = + pxr::UsdLuxDistantLight::Define(*stage, sdfLightPath); + this->entities[_light.id()] = directionalLight.GetPrim(); + directionalLight.CreateColorAttr( + pxr::VtValue( + pxr::GfVec3f( + _light.diffuse().r(), + _light.diffuse().g(), + _light.diffuse().b()))); + break; + } + default: + return false; + } + + // This is a workaround to set the light's intensity attribute. Using the + // UsdLuxLightAPI sets the light's "inputs:intensity" attribute, but isaac + // sim reads the light's "intensity" attribute. Both inputs:intensity and + // intensity are set to provide flexibility with other USD renderers + const float usdLightIntensity = + static_cast(_light.intensity()) * 1000.0f; + auto lightPrim = stage->GetPrimAtPath(sdfLightPath); + lightPrim.CreateAttribute(pxr::TfToken("intensity"), + pxr::SdfValueTypeNames->Float, false).Set(usdLightIntensity); + + return true; +} + ////////////////////////////////////////////////// bool Scene::Init() { diff --git a/source/ignition_live/Scene.hpp b/source/ignition_live/Scene.hpp index 2749574..a30f7c0 100644 --- a/source/ignition_live/Scene.hpp +++ b/source/ignition_live/Scene.hpp @@ -71,6 +71,8 @@ class Scene ignition::transport::Node node; std::unordered_map entities; + bool UpdateLights(const ignition::msgs::Light &_light, + const std::string &_usdLightPath); bool UpdateScene(const ignition::msgs::Scene &_scene); bool UpdateVisual(const ignition::msgs::Visual &_visual, const std::string &_usdPath); From c7db785d5c221730f1b5b53892dddb4034f58b5f Mon Sep 17 00:00:00 2001 From: Teo Koon Peng Date: Fri, 18 Feb 2022 17:01:50 +0800 Subject: [PATCH 2/2] add todo to remove duplicate code Signed-off-by: Teo Koon Peng --- source/ignition_live/Scene.cpp | 62 +++++++++++++++------------------- 1 file changed, 27 insertions(+), 35 deletions(-) diff --git a/source/ignition_live/Scene.cpp b/source/ignition_live/Scene.cpp index 3833f8e..3756f98 100644 --- a/source/ignition_live/Scene.cpp +++ b/source/ignition_live/Scene.cpp @@ -304,7 +304,8 @@ bool Scene::UpdateLink(const ignition::msgs::Link &_link, { if (!this->UpdateLights(light, usdLinkPath + "/" + light.name())) { - ignerr << "Failed to add light [" << usdLinkPath + "/" + light.name() << "]" << std::endl; + ignerr << "Failed to add light [" << usdLinkPath + "/" + light.name() + << "]" << std::endl; return false; } } @@ -408,6 +409,8 @@ bool Scene::UpdateScene(const ignition::msgs::Scene &_scene) bool Scene::UpdateLights(const ignition::msgs::Light &_light, const std::string &_usdLightPath) { + // TODO: We can probably re-use code from sdformat + auto stage = this->stage.Lock(); const pxr::SdfPath sdfLightPath(_usdLightPath); @@ -415,46 +418,33 @@ bool Scene::UpdateLights(const ignition::msgs::Light &_light, { case ignition::msgs::Light::POINT: { - auto pointLight = - pxr::UsdLuxSphereLight::Define(*stage, sdfLightPath); + auto pointLight = pxr::UsdLuxSphereLight::Define(*stage, sdfLightPath); pointLight.CreateTreatAsPointAttr().Set(true); this->entities[_light.id()] = pointLight.GetPrim(); pointLight.CreateRadiusAttr(pxr::VtValue(0.1f)); - pointLight.CreateColorAttr( - pxr::VtValue( - pxr::GfVec3f( - _light.diffuse().r(), - _light.diffuse().g(), - _light.diffuse().b()))); + pointLight.CreateColorAttr(pxr::VtValue(pxr::GfVec3f( + _light.diffuse().r(), _light.diffuse().g(), _light.diffuse().b()))); + break; + } + case ignition::msgs::Light::SPOT: + { + auto diskLight = pxr::UsdLuxDiskLight::Define(*stage, sdfLightPath); + this->entities[_light.id()] = diskLight.GetPrim(); + diskLight.CreateColorAttr(pxr::VtValue(pxr::GfVec3f( + _light.diffuse().r(), _light.diffuse().g(), _light.diffuse().b()))); break; } - case ignition::msgs::Light::SPOT: - { - auto diskLight = pxr::UsdLuxDiskLight::Define(*stage, sdfLightPath); - this->entities[_light.id()] = diskLight.GetPrim(); - diskLight.CreateColorAttr( - pxr::VtValue( - pxr::GfVec3f( - _light.diffuse().r(), - _light.diffuse().g(), - _light.diffuse().b()))); - break; - } - case ignition::msgs::Light::DIRECTIONAL: - { + case ignition::msgs::Light::DIRECTIONAL: + { auto directionalLight = - pxr::UsdLuxDistantLight::Define(*stage, sdfLightPath); + pxr::UsdLuxDistantLight::Define(*stage, sdfLightPath); this->entities[_light.id()] = directionalLight.GetPrim(); - directionalLight.CreateColorAttr( - pxr::VtValue( - pxr::GfVec3f( - _light.diffuse().r(), - _light.diffuse().g(), - _light.diffuse().b()))); + directionalLight.CreateColorAttr(pxr::VtValue(pxr::GfVec3f( + _light.diffuse().r(), _light.diffuse().g(), _light.diffuse().b()))); break; } - default: - return false; + default: + return false; } // This is a workaround to set the light's intensity attribute. Using the @@ -462,10 +452,12 @@ bool Scene::UpdateLights(const ignition::msgs::Light &_light, // sim reads the light's "intensity" attribute. Both inputs:intensity and // intensity are set to provide flexibility with other USD renderers const float usdLightIntensity = - static_cast(_light.intensity()) * 1000.0f; + static_cast(_light.intensity()) * 1000.0f; auto lightPrim = stage->GetPrimAtPath(sdfLightPath); - lightPrim.CreateAttribute(pxr::TfToken("intensity"), - pxr::SdfValueTypeNames->Float, false).Set(usdLightIntensity); + lightPrim + .CreateAttribute(pxr::TfToken("intensity"), pxr::SdfValueTypeNames->Float, + false) + .Set(usdLightIntensity); return true; }