From 71c1b536b4709e416bb6da05733d6fbda80339cf Mon Sep 17 00:00:00 2001 From: Alexei Kotov Date: Sat, 1 Jun 2024 21:37:53 +0300 Subject: [PATCH] Discard culled lights specifically for post-processing light buffer --- components/sceneutil/lightmanager.cpp | 19 +++++++++++++++---- components/sceneutil/lightmanager.hpp | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/components/sceneutil/lightmanager.cpp b/components/sceneutil/lightmanager.cpp index 0377905c9d..66fe231e11 100644 --- a/components/sceneutil/lightmanager.cpp +++ b/components/sceneutil/lightmanager.cpp @@ -1122,8 +1122,10 @@ namespace SceneUtil } const std::vector& LightManager::getLightsInViewSpace( - osg::Camera* camera, const osg::RefMatrix* viewMatrix, size_t frameNum) + osgUtil::CullVisitor* cv, const osg::RefMatrix* viewMatrix, size_t frameNum) { + osg::Camera* camera = cv->getCurrentCamera(); + osg::observer_ptr camPtr(camera); auto it = mLightsInViewSpace.find(camPtr); @@ -1175,13 +1177,22 @@ namespace SceneUtil if (fillPPLights) { + osg::CullingSet& cullingSet = cv->getModelViewCullingStack().front(); for (const auto& bound : it->second) { if (bound.mLightSource->getEmpty()) continue; const auto* light = bound.mLightSource->getLight(frameNum); - if (light->getDiffuse().x() >= 0.f) - mPPLightBuffer->setLight(frameNum, light, bound.mLightSource->getRadius()); + // Ignore negative lights + if (light->getDiffuse().x() < 0.f) + continue; + const float radius = bound.mLightSource->getRadius(); + osg::BoundingSphere frustumBound = bound.mViewBound; + frustumBound.radius() = radius * 2.f; + // Ignore culled lights + if (cullingSet.isCulled(frustumBound)) + continue; + mPPLightBuffer->setLight(frameNum, light, radius); } } @@ -1291,7 +1302,7 @@ namespace SceneUtil transformBoundingSphere(*cv->getModelViewMatrix(), nodeBound); const std::vector& lights - = mLightManager->getLightsInViewSpace(cv->getCurrentCamera(), viewMatrix, mLastFrameNumber); + = mLightManager->getLightsInViewSpace(cv, viewMatrix, mLastFrameNumber); mLightList.clear(); for (const LightManager::LightSourceViewBound& light : lights) diff --git a/components/sceneutil/lightmanager.hpp b/components/sceneutil/lightmanager.hpp index ee7dc5b0af..0b30a77e5c 100644 --- a/components/sceneutil/lightmanager.hpp +++ b/components/sceneutil/lightmanager.hpp @@ -238,7 +238,7 @@ namespace SceneUtil void addLight(LightSource* lightSource, const osg::Matrixf& worldMat, size_t frameNum); const std::vector& getLightsInViewSpace( - osg::Camera* camera, const osg::RefMatrix* viewMatrix, size_t frameNum); + osgUtil::CullVisitor* cv, const osg::RefMatrix* viewMatrix, size_t frameNum); osg::ref_ptr getLightListStateSet( const LightList& lightList, size_t frameNum, const osg::RefMatrix* viewMatrix);