From 45cb3eb081d8281a0f5cdd6ff38eea58aef60844 Mon Sep 17 00:00:00 2001 From: Cody Glassman Date: Wed, 3 Jul 2024 13:28:49 -0700 Subject: [PATCH] lua - use loaded node when possible when calculating bounding box --- apps/openmw/mwrender/renderingmanager.cpp | 40 ++++++++++++++--------- files/lua_api/openmw/core.lua | 2 +- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/apps/openmw/mwrender/renderingmanager.cpp b/apps/openmw/mwrender/renderingmanager.cpp index 469d71fc4f..db5b01913c 100644 --- a/apps/openmw/mwrender/renderingmanager.cpp +++ b/apps/openmw/mwrender/renderingmanager.cpp @@ -1659,24 +1659,32 @@ namespace MWRender osg::BoundingBox RenderingManager::getCullSafeBoundingBox(const MWWorld::Ptr& ptr) const { - const std::string model = ptr.getClass().getCorrectedModel(ptr); - if (model.empty()) - return {}; + osg::ref_ptr rootNode = ptr.getRefData().getBaseNode(); - osg::ref_ptr rootNode = new SceneUtil::PositionAttitudeTransform; - // Hack even used by osg internally, osg's NodeVisitor won't accept const qualified nodes - rootNode->addChild(const_cast(mResourceSystem->getSceneManager()->getTemplate(model).get())); - - const float refScale = ptr.getCellRef().getScale(); - rootNode->setScale({ refScale, refScale, refScale }); - rootNode->setPosition(osg::Vec3(0, 0, 0)); - - osg::ref_ptr animation = nullptr; - - if (ptr.getClass().isNpc()) + // Recalculate bounds on the ptr's template when the object is not loaded or is loaded but paged + MWWorld::Scene* worldScene = MWBase::Environment::get().getWorldScene(); + if (!rootNode || worldScene->isPagedRef(ptr)) { - rootNode->setNodeMask(Mask_Actor); - animation = new NpcAnimation(ptr, osg::ref_ptr(rootNode), mResourceSystem); + const std::string model = ptr.getClass().getCorrectedModel(ptr); + + if (model.empty()) + return {}; + + rootNode = new SceneUtil::PositionAttitudeTransform; + // Hack even used by osg internally, osg's NodeVisitor won't accept const qualified nodes + rootNode->addChild(const_cast(mResourceSystem->getSceneManager()->getTemplate(model).get())); + + const float refScale = ptr.getCellRef().getScale(); + rootNode->setScale({ refScale, refScale, refScale }); + rootNode->setPosition(ptr.getCellRef().getPosition().asVec3()); + + osg::ref_ptr animation = nullptr; + + if (ptr.getClass().isNpc()) + { + rootNode->setNodeMask(Mask_Actor); + animation = new NpcAnimation(ptr, osg::ref_ptr(rootNode), mResourceSystem); + } } SceneUtil::CullSafeBoundsVisitor computeBounds; diff --git a/files/lua_api/openmw/core.lua b/files/lua_api/openmw/core.lua index b75f22748b..5a92f92f89 100644 --- a/files/lua_api/openmw/core.lua +++ b/files/lua_api/openmw/core.lua @@ -277,7 +277,7 @@ -- money:split(50):moveInto(types.Container.content(cont)) --- --- The axis aligned bounding box in local coordinates. +-- The axis aligned bounding box in world coordinates. -- @function [parent=#GameObject] getBoundingBox -- @param self -- @return openmw.util#Box