From 20705f51af0bf281a8eee99452ed20a33cc41368 Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Wed, 20 Jul 2022 10:10:38 -0700 Subject: [PATCH] LensFlare: parameterize number of occlusion steps (#3234) There is a significant amount of CPU time taken when checking for occlusions in the LensFlare compositor listener. This exposes the number of steps taken when checking occlusions to allow users to attempt to speed up this process. * LensFlarePlugin: call SetCamera last The SetCamera call passes the current parameters to the compositor listener, so make sure that it is called last. Signed-off-by: Steve Peters --- gazebo/rendering/LensFlare.cc | 24 +++++++++++++++++++++++- gazebo/rendering/LensFlare.hh | 12 ++++++++++++ plugins/LensFlareSensorPlugin.cc | 23 ++++++++++++++++++++++- plugins/LensFlareSensorPlugin.hh | 13 ++++++++++--- 4 files changed, 67 insertions(+), 5 deletions(-) diff --git a/gazebo/rendering/LensFlare.cc b/gazebo/rendering/LensFlare.cc index e5440a3495..dfa74fd046 100644 --- a/gazebo/rendering/LensFlare.cc +++ b/gazebo/rendering/LensFlare.cc @@ -55,6 +55,10 @@ namespace gazebo /// \brief Color of lens flare. public: ignition::math::Vector3d color = ignition::math::Vector3d(1.0, 1.0, 1.0); + + /// \brief Number of steps to take in each direction when checking for + /// occlusion. + public: double occlusionSteps = 10.0; }; ////////////////////////////////////////////////// @@ -95,6 +99,13 @@ namespace gazebo this->dataPtr->color = _color; } + ////////////////////////////////////////////////// + void LensFlareCompositorListener::SetOcclusionSteps( + double _occlusionSteps) + { + this->dataPtr->occlusionSteps = _occlusionSteps; + } + ////////////////////////////////////////////////// void LensFlareCompositorListener::notifyMaterialRender(unsigned int _passId, Ogre::MaterialPtr &_mat) @@ -313,7 +324,7 @@ namespace gazebo // work in normalized device coordinates // lens flare's halfSize is just an approximated value double halfSize = 0.05 * this->dataPtr->scale; - double steps = 10; + double steps = this->dataPtr->occlusionSteps; double stepSize = halfSize * 2 / steps; double cx = _imgPos.X(); double cy = _imgPos.Y(); @@ -356,6 +367,9 @@ namespace gazebo public: ignition::math::Vector3d lensFlareColor = ignition::math::Vector3d(1.0, 1.0, 1.0); + /// \brief Color of lens flare. + public: double lensFlareOcclusionSteps = 10.0; + /// \brief Compositor name to be used for lens flare public: std::string compositorName = "CameraLensFlare/Default"; @@ -423,6 +437,8 @@ void LensFlare::SetCamera(CameraPtr _camera) this->dataPtr->lensFlareScale); this->dataPtr->lensFlareCompositorListener->SetColor( this->dataPtr->lensFlareColor); + this->dataPtr->lensFlareCompositorListener->SetOcclusionSteps( + this->dataPtr->lensFlareOcclusionSteps); this->dataPtr->lensFlareInstance = Ogre::CompositorManager::getSingleton().addCompositor( @@ -463,6 +479,12 @@ void LensFlare::SetColor(const ignition::math::Vector3d &_color) } } +////////////////////////////////////////////////// +void LensFlare::SetOcclusionSteps(double _occlusionSteps) +{ + this->dataPtr->lensFlareOcclusionSteps = _occlusionSteps; +} + ////////////////////////////////////////////////// void LensFlare::SetCompositorName(const std::string &_name) { diff --git a/gazebo/rendering/LensFlare.hh b/gazebo/rendering/LensFlare.hh index 058e0a2da0..04165751d4 100644 --- a/gazebo/rendering/LensFlare.hh +++ b/gazebo/rendering/LensFlare.hh @@ -59,6 +59,12 @@ namespace gazebo /// \param[in] _color Color of lens flare public: void SetColor(const ignition::math::Vector3d &_color); + /// \brief Set the number of steps to take in each direction when + /// checking for occlusions. + /// \param[in] _occlusionSteps number of steps to take in each direction + /// when checking for occlusion. + public: void SetOcclusionSteps(double _occlusionSteps); + /// \brief Callback that OGRE will invoke for us on each render call /// \param[in] _passID OGRE material pass ID. /// \param[in] _mat Pointer to OGRE material. @@ -123,6 +129,12 @@ namespace gazebo /// \param[in] _color Color of lens flare public: void SetColor(const ignition::math::Vector3d &_color); + /// \brief Set the number of steps to take in each direction when + /// checking for occlusions. + /// \param[in] _occlusionSteps number of steps to take in each direction + /// when checking for occlusion. + public: void SetOcclusionSteps(double _occlusionSteps); + /// \brief Set the name of the lens flare compositor to use the next /// time SetCamera is called. /// \param[in] _name Name of the compositor to use diff --git a/plugins/LensFlareSensorPlugin.cc b/plugins/LensFlareSensorPlugin.cc index 93db560415..d2309c7946 100644 --- a/plugins/LensFlareSensorPlugin.cc +++ b/plugins/LensFlareSensorPlugin.cc @@ -40,6 +40,9 @@ namespace gazebo public: ignition::math::Vector3d color = ignition::math::Vector3d(1.4, 1.2, 1.0); + /// \brief Lens flare occlusion steps + public: double occlusionSteps = 10.0; + /// \brief Lens flare compositor name public: std::string compositorName; }; @@ -87,6 +90,11 @@ void LensFlareSensorPlugin::Load(sensors::SensorPtr _sensor, this->dataPtr->color = _sdf->Get("color"); } + if (_sdf->HasElement("occlusion_steps")) + { + this->dataPtr->occlusionSteps = _sdf->Get("occlusion_steps"); + } + const std::string compositorName = "compositor"; if (_sdf->HasElement(compositorName)) { @@ -141,6 +149,17 @@ void LensFlareSensorPlugin::SetColor(const ignition::math::Vector3d &_color) } } +///////////////////////////////////////////////// +void LensFlareSensorPlugin::SetOcclusionSteps(double _occlusionSteps) +{ + this->dataPtr->occlusionSteps = _occlusionSteps; + + for (auto flare : this->dataPtr->lensFlares) + { + flare->SetOcclusionSteps(_occlusionSteps); + } +} + ///////////////////////////////////////////////// void LensFlareSensorPlugin::AddLensFlare(rendering::CameraPtr _camera) { @@ -153,8 +172,10 @@ void LensFlareSensorPlugin::AddLensFlare(rendering::CameraPtr _camera) { lensFlare->SetCompositorName(this->dataPtr->compositorName); } - lensFlare->SetCamera(_camera); lensFlare->SetScale(this->dataPtr->scale); lensFlare->SetColor(this->dataPtr->color); + lensFlare->SetOcclusionSteps(this->dataPtr->occlusionSteps); + // SetCamera must be called last + lensFlare->SetCamera(_camera); this->dataPtr->lensFlares.push_back(lensFlare); } diff --git a/plugins/LensFlareSensorPlugin.hh b/plugins/LensFlareSensorPlugin.hh index 54aa04b587..7120af5405 100644 --- a/plugins/LensFlareSensorPlugin.hh +++ b/plugins/LensFlareSensorPlugin.hh @@ -28,9 +28,10 @@ namespace gazebo /// \brief Plugin that adds lens flare effect to a camera or multicamera /// sensor /// The plugin has the following optional parameter: - /// Name of the lens flare compositor to use. - /// Scale of lens flare. Must be greater than 0 - /// Color of lens flare. + /// Color of lens flare. + /// Name of the lens flare compositor to use. + /// Number of steps used when checking for occlusions. + /// Scale of lens flare. Must be greater than 0. /// \todo A potentially useful feature would be an option for constantly /// updating the flare color to match the light source color. class GZ_PLUGIN_VISIBLE LensFlareSensorPlugin : public SensorPlugin @@ -53,6 +54,12 @@ namespace gazebo /// \param[in] _color Color of lens flare public: void SetColor(const ignition::math::Vector3d &_color); + /// \brief Set the number of steps to take in each direction when + /// checking for occlusions. + /// \param[in] _occlusionSteps number of steps to take in each direction + /// when checking for occlusion. + public: void SetOcclusionSteps(double _occlusionSteps); + /// \brief Add lens flare effect to a camera /// \param[in] _camera Camera to add the lens flare effect to. private: void AddLensFlare(rendering::CameraPtr _camera);