From 664df72de0f7d3c7c2f81ced8c356c1a5fa90d7d Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Wed, 2 Mar 2022 18:18:53 -0800 Subject: [PATCH 1/2] fix shaders when there is lidar in the scene Signed-off-by: Ian Chen --- ogre2/src/Ogre2GpuRays.cc | 35 +++++++++++++++++++++++++----- ogre2/src/Ogre2MaterialSwitcher.cc | 11 ++++++++-- test/integration/camera.cc | 21 ++++++++++++++++++ 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/ogre2/src/Ogre2GpuRays.cc b/ogre2/src/Ogre2GpuRays.cc index 0cb21195a..8fa4e7251 100644 --- a/ogre2/src/Ogre2GpuRays.cc +++ b/ogre2/src/Ogre2GpuRays.cc @@ -198,6 +198,11 @@ class ignition::rendering::Ogre2GpuRaysPrivate using namespace ignition; using namespace rendering; +/// \brief A map of ogre sub item pointer to their original low level material +/// \todo(anyone) Added here for ABI compatibity. Move to private class +/// in ign-rendering7 +std::map> laserRetroMaterialMap; ////////////////////////////////////////////////// Ogre2LaserRetroMaterialSwitcher::Ogre2LaserRetroMaterialSwitcher( @@ -230,7 +235,6 @@ void Ogre2LaserRetroMaterialSwitcher::cameraPreRenderScene( // swap item to use v1 shader material // Note: keep an eye out for performance impact on switching materials // on the fly. We are not doing this often so should be ok. - this->datablockMap.clear(); auto itor = this->scene->OgreSceneManager()->getMovableObjectIterator( Ogre::ItemFactory::FACTORY_TYPE_NAME); while (itor.hasMoreElements()) @@ -304,9 +308,18 @@ void Ogre2LaserRetroMaterialSwitcher::cameraPreRenderScene( subItem->setCustomParameter(this->customParamIdx, Ogre::Vector4(color, color, color, 1.0)); - Ogre::HlmsDatablock *datablock = subItem->getDatablock(); - this->datablockMap[subItem] = datablock; - + // case when item is using low level materials + // e.g. shaders + if (!subItem->getMaterial().isNull()) + { + laserRetroMaterialMap[this][subItem] = subItem->getMaterial(); + } + // regular Pbs Hlms datablock + else + { + Ogre::HlmsDatablock *datablock = subItem->getDatablock(); + this->datablockMap[subItem] = datablock; + } subItem->setMaterial(this->laserRetroSourceMaterial); } itor.moveNext(); @@ -324,12 +337,24 @@ void Ogre2LaserRetroMaterialSwitcher::cameraPostRenderScene( subItem->setDatablock(it.second); } + auto lIt = laserRetroMaterialMap.find(this); + if (lIt != laserRetroMaterialMap.end()) + { + for (auto it : lIt->second) + { + Ogre::SubItem *subItem = it.first; + subItem->setMaterial(it.second); + } + } + Ogre::Pass *pass = this->laserRetroSourceMaterial->getBestTechnique()->getPass(0u); pass->getVertexProgramParameters()->setNamedConstant( "ignMinClipDistance", 0.0f ); -} + this->datablockMap.clear(); + laserRetroMaterialMap[this].clear(); +} ////////////////////////////////////////////////// Ogre2GpuRays::Ogre2GpuRays() diff --git a/ogre2/src/Ogre2MaterialSwitcher.cc b/ogre2/src/Ogre2MaterialSwitcher.cc index 45c38e88b..352cc78c7 100644 --- a/ogre2/src/Ogre2MaterialSwitcher.cc +++ b/ogre2/src/Ogre2MaterialSwitcher.cc @@ -37,8 +37,8 @@ using namespace rendering; /// \brief A map of ogre sub item pointer to their original low level material -/// \todo(anyone) Added here for ABI compatibity This can be removed once -/// ign-rendering7 switches to Hlms customization for "switching" materials +/// \todo(anyone) Added here for ABI compatibity. Move to private class +/// in ign-rendering7 std::map> materialMap; @@ -110,6 +110,13 @@ void Ogre2MaterialSwitcher::cameraPreRenderScene( { materialMap[this][subItem] = subItem->getMaterial(); subItem->setMaterial(this->plainMaterial); + auto technique = subItem->getMaterial()->getTechnique(0); + + if (technique && !technique->isDepthWriteEnabled() && + !technique->isDepthCheckEnabled()) + subItem->setMaterial(this->plainOverlayMaterial); + else + subItem->setMaterial(this->plainMaterial); } // regular Pbs Hlms datablock else diff --git a/test/integration/camera.cc b/test/integration/camera.cc index 92fd0f252..8c14beaf5 100644 --- a/test/integration/camera.cc +++ b/test/integration/camera.cc @@ -22,6 +22,7 @@ #include "test_config.h" // NOLINT(build/include) #include "ignition/rendering/Camera.hh" +#include "ignition/rendering/GpuRays.hh" #include "ignition/rendering/RenderEngine.hh" #include "ignition/rendering/RenderingIface.hh" #include "ignition/rendering/Scene.hh" @@ -663,6 +664,25 @@ void CameraTest::ShaderSelection(const std::string &_renderEngine) camera->SetHFOV(IGN_PI / 2); root->AddChild(camera); + // Create a gpu ray + // laser retro material switching may also affect shader materials + const double hMinAngle = -IGN_PI/2.0; + const double hMaxAngle = IGN_PI/2.0; + const double minRange = 0.1; + const double maxRange = 10.0; + const int hRayCount = 320; + const int vRayCount = 1; + GpuRaysPtr gpuRays = scene->CreateGpuRays("gpu_rays"); + gpuRays->SetWorldPosition(0, 0, 0); + gpuRays->SetNearClipPlane(minRange); + gpuRays->SetFarClipPlane(maxRange); + gpuRays->SetAngleMin(hMinAngle); + gpuRays->SetAngleMax(hMaxAngle); + gpuRays->SetRayCount(hRayCount); + + gpuRays->SetVerticalRayCount(vRayCount); + root->AddChild(gpuRays); + if (_renderEngine == "ogre2") { // worldviewproj_matrix is a constant defined by ogre. @@ -681,6 +701,7 @@ void CameraTest::ShaderSelection(const std::string &_renderEngine) for (auto i = 0; i < 30; ++i) { camera->Update(); + gpuRays->Update(); } // capture a frame From a6c13de99012a85048edbd411959cd36cfff932c Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Fri, 4 Mar 2022 11:31:48 -0800 Subject: [PATCH 2/2] style Signed-off-by: Ian Chen --- ogre2/src/Ogre2GpuRays.cc | 6 +++--- ogre2/src/Ogre2MaterialSwitcher.cc | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ogre2/src/Ogre2GpuRays.cc b/ogre2/src/Ogre2GpuRays.cc index 8fa4e7251..1821b121b 100644 --- a/ogre2/src/Ogre2GpuRays.cc +++ b/ogre2/src/Ogre2GpuRays.cc @@ -337,10 +337,10 @@ void Ogre2LaserRetroMaterialSwitcher::cameraPostRenderScene( subItem->setDatablock(it.second); } - auto lIt = laserRetroMaterialMap.find(this); - if (lIt != laserRetroMaterialMap.end()) + auto laserRetroIt = laserRetroMaterialMap.find(this); + if (laserRetroIt != laserRetroMaterialMap.end()) { - for (auto it : lIt->second) + for (auto it : laserRetroIt->second) { Ogre::SubItem *subItem = it.first; subItem->setMaterial(it.second); diff --git a/ogre2/src/Ogre2MaterialSwitcher.cc b/ogre2/src/Ogre2MaterialSwitcher.cc index 352cc78c7..905684f91 100644 --- a/ogre2/src/Ogre2MaterialSwitcher.cc +++ b/ogre2/src/Ogre2MaterialSwitcher.cc @@ -114,9 +114,13 @@ void Ogre2MaterialSwitcher::cameraPreRenderScene( if (technique && !technique->isDepthWriteEnabled() && !technique->isDepthCheckEnabled()) + { subItem->setMaterial(this->plainOverlayMaterial); + } else + { subItem->setMaterial(this->plainMaterial); + } } // regular Pbs Hlms datablock else