From d1eb768de6bca8cecdc4a506ae90b4e0cefcabf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Garc=C3=ADa=20Li=C3=B1=C3=A1n?= Date: Sun, 3 Nov 2019 20:53:55 +0100 Subject: [PATCH] Disabled CASTSHADOW_BIT on most of the scene graph geometry to optimize performance --- simgear/scene/sky/newcloud.cxx | 2 +- simgear/scene/tgdb/SGOceanTile.cxx | 2 +- simgear/scene/tgdb/SGTileDetailsCallback.hxx | 2 +- simgear/scene/tgdb/obj.cxx | 2 +- simgear/scene/viewer/CompositorPass.cxx | 33 ++++++++++++++++---- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/simgear/scene/sky/newcloud.cxx b/simgear/scene/sky/newcloud.cxx index 6791b370..8e9eb714 100644 --- a/simgear/scene/sky/newcloud.cxx +++ b/simgear/scene/sky/newcloud.cxx @@ -256,7 +256,7 @@ osg::ref_ptr SGNewCloud::genCloud() { geode->addDrawable(sg); geode->setName("3D cloud"); geode->setEffect(effect.get()); - geode->setNodeMask( ~simgear::MODELLIGHT_BIT ); + geode->setNodeMask( ~(simgear::CASTSHADOW_BIT | simgear::MODELLIGHT_BIT) ); return geode; } diff --git a/simgear/scene/tgdb/SGOceanTile.cxx b/simgear/scene/tgdb/SGOceanTile.cxx index f4636159..29839b29 100644 --- a/simgear/scene/tgdb/SGOceanTile.cxx +++ b/simgear/scene/tgdb/SGOceanTile.cxx @@ -338,7 +338,7 @@ osg::Node* SGOceanTile(const SGBucket& b, SGMaterialLib *matlib, int latPoints, transform->setMatrix(osg::Matrix::rotate(toOsg(hlOr))* osg::Matrix::translate(toOsg(cartCenter))); transform->addChild(geode); - transform->setNodeMask( ~simgear::MODELLIGHT_BIT ); + transform->setNodeMask( ~(simgear::CASTSHADOW_BIT | simgear::MODELLIGHT_BIT) ); return transform; } diff --git a/simgear/scene/tgdb/SGTileDetailsCallback.hxx b/simgear/scene/tgdb/SGTileDetailsCallback.hxx index 6e22c8cd..58792a68 100644 --- a/simgear/scene/tgdb/SGTileDetailsCallback.hxx +++ b/simgear/scene/tgdb/SGTileDetailsCallback.hxx @@ -1099,7 +1099,7 @@ public: if (forestNode.valid()) objectLOD->addChild(forestNode.get(), 0, 2.0 * object_range + SG_TILE_RADIUS); if (buildingNode.valid()) objectLOD->addChild(buildingNode.get(), 0, 2.0 * object_range + SG_TILE_RADIUS); - unsigned nodeMask = SG_NODEMASK_CASTSHADOW_BIT | SG_NODEMASK_RECEIVESHADOW_BIT | SG_NODEMASK_TERRAIN_BIT; + unsigned nodeMask = SG_NODEMASK_RECEIVESHADOW_BIT | SG_NODEMASK_TERRAIN_BIT; objectLOD->setNodeMask(nodeMask); } diff --git a/simgear/scene/tgdb/obj.cxx b/simgear/scene/tgdb/obj.cxx index 2efe814e..5d3461f5 100644 --- a/simgear/scene/tgdb/obj.cxx +++ b/simgear/scene/tgdb/obj.cxx @@ -159,6 +159,6 @@ SGLoadBTG(const std::string& path, const simgear::SGReaderWriterOptions* options transform->addChild(pagedLOD); } - transform->setNodeMask( ~simgear::MODELLIGHT_BIT ); + transform->setNodeMask( ~(simgear::CASTSHADOW_BIT | simgear::MODELLIGHT_BIT) ); return transform; } diff --git a/simgear/scene/viewer/CompositorPass.cxx b/simgear/scene/viewer/CompositorPass.cxx index d2d7a6db..2a67a6d4 100644 --- a/simgear/scene/viewer/CompositorPass.cxx +++ b/simgear/scene/viewer/CompositorPass.cxx @@ -36,6 +36,25 @@ #include "Compositor.hxx" #include "CompositorUtil.hxx" +namespace { +osgUtil::RenderBin::RenderBinList +removeTransparentBins(simgear::EffectCullVisitor *cv) +{ + osgUtil::RenderBin::RenderBinList transparent_bins; + osgUtil::RenderStage *stage = cv->getRenderStage(); + osgUtil::RenderBin::RenderBinList &rbl = stage->getRenderBinList(); + for (auto rbi = rbl.begin(); rbi != rbl.end(); ) { + if (rbi->second->getSortMode() == osgUtil::RenderBin::SORT_BACK_TO_FRONT) { + transparent_bins.insert(std::make_pair(rbi->first, rbi->second)); + rbl.erase(rbi++); + } else { + ++rbi; + } + } + return transparent_bins; +} +} // anonymous namespace + namespace simgear { namespace compositor { @@ -384,9 +403,12 @@ public: virtual void operator()(osg::Node *node, osg::NodeVisitor *nv) { osg::Camera *camera = static_cast(node); + EffectCullVisitor *cv = dynamic_cast(nv); traverse(node, nv); + removeTransparentBins(cv); + // The light matrix uniform is updated after the traverse in case the // OSG near/far plane calculations were enabled osg::Matrixf light_matrix = @@ -497,19 +519,18 @@ public: osg::Vec4 aim4 = osg::Vec4(bs.center(), 1.0) * view_inverse; osg::Vec3 aim(aim4.x(), aim4.y(), aim4.z()); - osg::Vec3 up(0.0f, 1.0f, 0.0f); osg::Matrixd &light_view_matrix = camera->getViewMatrix(); light_view_matrix.makeLookAt( - aim + (light_dir * bs.radius() * 2.0f), + aim + light_dir * (bs.radius() + 10.0f), aim, - aim); + osg::Vec3(0.0f, 0.0f, 1.0f)); osg::Matrixd &light_proj_matrix = camera->getProjectionMatrix(); light_proj_matrix.makeOrtho( -bs.radius(), bs.radius(), -bs.radius(), bs.radius(), - -bs.radius() * 6.0f, bs.radius() * 6.0f); + 1.0, bs.radius() * 10.0); // Do texel snapping to prevent flickering or shimmering. // We are using double precision vectors and matrices because in FG @@ -520,8 +541,8 @@ public: osg::Vec2d shadow_origin(shadow_origin4.x(), shadow_origin4.y()); shadow_origin = osg::Vec2d(shadow_origin.x() * _half_sm_size.x(), shadow_origin.y() * _half_sm_size.y()); - osg::Vec2d rounded_origin(std::round(shadow_origin.x()), - std::round(shadow_origin.y())); + osg::Vec2d rounded_origin(std::floor(shadow_origin.x()), + std::floor(shadow_origin.y())); osg::Vec2d rounding = rounded_origin - shadow_origin; rounding = osg::Vec2d(rounding.x() / _half_sm_size.x(), rounding.y() / _half_sm_size.y());