From a5aed7b00fe3fe792f51bc26a58b234f2f0022dc Mon Sep 17 00:00:00 2001 From: Alexei Kotov Date: Sat, 27 Jun 2026 19:18:33 +0300 Subject: [PATCH] Implement NiLookAtController (#6938) --- components/nif/controller.cpp | 3 + components/nifosg/controller.cpp | 120 +++++++++++++++++++++++++++ components/nifosg/controller.hpp | 23 +++++ components/nifosg/nifloader.cpp | 39 +++++++++ components/resource/scenemanager.cpp | 3 + components/sceneutil/clone.cpp | 31 ++++++- components/sceneutil/clone.hpp | 8 ++ components/sceneutil/controller.hpp | 3 + components/sceneutil/serialize.cpp | 1 + 9 files changed, 230 insertions(+), 1 deletion(-) diff --git a/components/nif/controller.cpp b/components/nif/controller.cpp index 90f6af0d95..04346ef5d6 100644 --- a/components/nif/controller.cpp +++ b/components/nif/controller.cpp @@ -277,6 +277,9 @@ namespace Nif if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 0)) nif->read(mLookAtFlags); + else + mLookAtFlags = (mFlags >> 4) & 7; + mLookAt.read(nif); } diff --git a/components/nifosg/controller.cpp b/components/nifosg/controller.cpp index 116a256846..a62f77f571 100644 --- a/components/nifosg/controller.cpp +++ b/components/nifosg/controller.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include "matrixtransform.hpp" @@ -653,4 +654,123 @@ namespace NifOsg traverse(node, nv); } + LookAtController::LookAtController(const Nif::NiLookAtController& ctrl) + : mFlags(ctrl.mLookAtFlags) + { + } + + LookAtController::LookAtController(const LookAtController& copy, const osg::CopyOp& copyop) + : SceneUtil::NodeCallback(copy, copyop) + , SceneUtil::Controller(copy) + , mFlags(copy.mFlags) + , mTarget(copy.mTarget) + { + } + + void LookAtController::setTarget(osg::Group* target) + { + mTarget = target; + } + + void LookAtController::operator()(NifOsg::MatrixTransform* node, osg::NodeVisitor* nv) + { + osg::ref_ptr target; + if (!mTarget.lock(target)) + { + traverse(node, nv); + return; + } + + osg::MatrixList targetMats = target->getWorldMatrices(); + if (targetMats.empty()) + { + traverse(node, nv); + return; + } + + const osg::NodePath& nodePath = nv->getNodePath(); + const osg::Matrixf nodeWorldMat = osg::computeLocalToWorld(nodePath); + osg::Vec3f worldDir = targetMats.front().getTrans() - nodeWorldMat.getTrans(); + float len = worldDir.length(); + if (len < 1e-6f) + { + traverse(node, nv); + return; + } + + worldDir /= len; + + osg::Matrixf parentWorldInv; + if (nodePath.size() > 1) + { + osg::NodePath parentPath(nodePath.begin(), nodePath.end() - 1); + osg::Matrixf parentWorld = osg::computeLocalToWorld(parentPath); + if (!parentWorldInv.invert(parentWorld)) + { + traverse(node, nv); + return; + } + } + + // Build a constrained look-at rotation matrix. + // Which axis is "forward" depends on the flag; preferred up is world Z. + // Per MWSE: negate forward when flip is NOT set. No idea why. + osg::Vec3f forward = osg::Matrixf::transform3x3(worldDir, parentWorldInv); + forward.normalize(); + if (!(mFlags & Nif::NiLookAtController::Flag_Flip)) + forward = -forward; + + osg::Vec3f up = osg::Matrixf::transform3x3(osg::Vec3f(0.f, 0.f, 1.f), parentWorldInv); + up.normalize(); + + osg::Vec3f left = up ^ forward; + float leftLen = left.length(); + if (leftLen < 1e-6f) + { + traverse(node, nv); + return; + } + + left /= leftLen; + up = forward ^ left; + + osg::Vec3f rowX, rowY, rowZ; + if (mFlags & Nif::NiLookAtController::Flag_LookZAxis) + { + rowX = left; + rowY = up; + rowZ = forward; + } + else if (mFlags & Nif::NiLookAtController::Flag_LookYAxis) + { + rowX = forward ^ up; + rowY = forward; + rowZ = up; + } + else + { + rowX = forward; + rowY = left; + rowZ = up; + } + + osg::Matrixf rotMat(rowX.x(), rowX.y(), rowX.z(), 0.0, rowY.x(), rowY.y(), rowY.z(), 0.0, rowZ.x(), rowZ.y(), + rowZ.z(), 0.0, 0.0, 0.0, 0.0, 1.0); + + osg::Quat rotation; + rotation.set(rotMat); + node->setRotation(rotation); + + traverse(node, nv); + } + + void LookAtController::remapTargets(const SceneUtil::CopyOp& copyop) + { + if (!mTarget) + return; + + if (osg::Node* clonedTarget = copyop.getClonedNode(mTarget.get())) + mTarget = clonedTarget->asGroup(); + } + } diff --git a/components/nifosg/controller.hpp b/components/nifosg/controller.hpp index cceec279b2..05dee4f6ad 100644 --- a/components/nifosg/controller.hpp +++ b/components/nifosg/controller.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -26,6 +27,7 @@ namespace osgParticle namespace SceneUtil { + class CopyOp; class MorphGeometry; } @@ -420,6 +422,27 @@ namespace NifOsg float getPercent(float time) const; }; + class LookAtController : public SceneUtil::NodeCallback, + public SceneUtil::Controller + { + public: + LookAtController(const Nif::NiLookAtController& ctrl); + LookAtController() = default; + LookAtController(const LookAtController& copy, const osg::CopyOp& copyop); + + META_Object(NifOsg, LookAtController) + + void operator()(NifOsg::MatrixTransform*, osg::NodeVisitor*); + + void setTarget(osg::Group* target); + + void remapTargets(const SceneUtil::CopyOp& copyop) override; + + private: + uint16_t mFlags{ 0 }; + osg::observer_ptr mTarget; + }; + } #endif diff --git a/components/nifosg/nifloader.cpp b/components/nifosg/nifloader.cpp index 53f14aab22..b6e4b17642 100644 --- a/components/nifosg/nifloader.cpp +++ b/components/nifosg/nifloader.cpp @@ -331,6 +331,9 @@ namespace NifOsg // This is used to queue emitters that weren't attached to their node yet. std::vector>> mEmitterQueue; + // This is used to queue look-at controllers whose target nodes may not have been created yet. + mutable std::vector>> mLookAtQueue; + void loadKf(Nif::FileView nif, SceneUtil::KeyframeHolder& target) const { const Nif::NiSequenceStreamHelper* seq = nullptr; @@ -457,6 +460,9 @@ namespace NifOsg // Attach particle emitters to their nodes which should all be loaded by now. handleQueuedParticleEmitters(created, nif); + // Resolve look-at controller targets now that all nodes are loaded. + handleQueuedLookAtControllers(created, nif); + RemoveLodOverlapVisitor removeLodOverlaps; created->accept(removeLodOverlaps); @@ -1048,6 +1054,19 @@ namespace NifOsg node->addUpdateCallback(callback); isAnimated = true; } + else if (ctrl->mRecordType == Nif::RC_NiLookAtController) + { + const auto lookatctrl = static_cast(ctrl.getPtr()); + if (lookatctrl->mLookAt.empty()) + continue; + + osg::ref_ptr callback(new LookAtController(*lookatctrl)); + setupController(lookatctrl, callback, animflags); + node->addUpdateCallback(callback); + isAnimated = true; + + mLookAtQueue.emplace_back(lookatctrl->mLookAt->mRecordIndex, callback); + } else if (ctrl->mRecordType == Nif::RC_NiGeomMorpherController || ctrl->mRecordType == Nif::RC_NiParticleSystemController || ctrl->mRecordType == Nif::RC_NiBSPArrayController || ctrl->mRecordType == Nif::RC_NiUVController) @@ -1410,6 +1429,26 @@ namespace NifOsg mEmitterQueue.clear(); } + void handleQueuedLookAtControllers(osg::Group* rootNode, Nif::FileView nif) + { + for (const auto& [recordIndex, controller] : mLookAtQueue) + { + FindGroupByRecordIndex findTargetNode(recordIndex); + rootNode->accept(findTargetNode); + osg::Group* targetNode = findTargetNode.mFound; + if (!targetNode) + { + Log(Debug::Warning) << "NIFFile Warning: Failed to find look-at target node (node record index " + << recordIndex << "). File: " << nif.getFilename(); + continue; + } + + controller->setTarget(targetNode); + targetNode->setDataVariance(osg::Object::DYNAMIC); + } + mLookAtQueue.clear(); + } + void handleParticleSystem(const Nif::NiAVObject* nifNode, const Nif::Parent* parent, osg::Group* parentNode, SceneUtil::CompositeStateSetUpdater* composite, int animflags) { diff --git a/components/resource/scenemanager.cpp b/components/resource/scenemanager.cpp index 21b9f6de1c..a1b6454ff3 100644 --- a/components/resource/scenemanager.cpp +++ b/components/resource/scenemanager.cpp @@ -1018,6 +1018,9 @@ namespace Resource } } osg::ref_ptr cloned = static_cast(base->clone(copyop)); + + copyop.remapControllerTargets(); + // add a ref to the original template to help verify the safety of shallow cloning operations // in addition, if this node is managed by a cache, we hint to the cache that it's still being used and should // be kept in cache diff --git a/components/sceneutil/clone.cpp b/components/sceneutil/clone.cpp index f07a1bfaae..18b1b46bcb 100644 --- a/components/sceneutil/clone.cpp +++ b/components/sceneutil/clone.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -35,7 +36,23 @@ namespace SceneUtil mUpdaterToOldPs[cloned] = updater->getParticleSystem(0); return cloned; } - return osg::CopyOp::operator()(node); + + osg::Node* cloned = osg::CopyOp::operator()(node); + if (cloned) + mClonedNodes[node] = cloned; + + return cloned; + } + + osg::Callback* CopyOp::operator()(const osg::Callback* callback) const + { + osg::Callback* cloned = osg::CopyOp::operator()(callback); + for (osg::Callback* cb = cloned; cb != nullptr; cb = cb->getNestedCallback()) + { + if (auto* ctrl = dynamic_cast(cb)) + mControllersToRemap.push_back(ctrl); + } + return cloned; } osg::Drawable* CopyOp::operator()(const osg::Drawable* drawable) const @@ -98,4 +115,16 @@ namespace SceneUtil return cloned; } + osg::Node* CopyOp::getClonedNode(const osg::Node* original) const + { + auto it = mClonedNodes.find(original); + return it != mClonedNodes.end() ? it->second : nullptr; + } + + void CopyOp::remapControllerTargets() const + { + for (Controller* ctrl : mControllersToRemap) + ctrl->remapTargets(*this); + } + } diff --git a/components/sceneutil/clone.hpp b/components/sceneutil/clone.hpp index b755280b61..e2a4b71092 100644 --- a/components/sceneutil/clone.hpp +++ b/components/sceneutil/clone.hpp @@ -2,6 +2,7 @@ #define OPENMW_COMPONENTS_SCENEUTIL_CLONE_H #include +#include #include @@ -14,6 +15,7 @@ namespace osgParticle namespace SceneUtil { + class Controller; /// @par Defines the cloning behaviour we need: /// * Assigns updated ParticleSystem pointers on cloned emitters and programs. @@ -31,6 +33,10 @@ namespace SceneUtil osg::Node* operator()(const osg::Node* node) const override; osg::Drawable* operator()(const osg::Drawable* drawable) const override; + osg::Callback* operator()(const osg::Callback* callback) const override; + + osg::Node* getClonedNode(const osg::Node* original) const; + void remapControllerTargets() const; private: // maps new pointers to their old pointers @@ -38,6 +44,8 @@ namespace SceneUtil mutable std::map mProcessorToOldPs; mutable std::map mUpdaterToOldPs; mutable std::map mOldPsToNewPs; + mutable std::map mClonedNodes; + mutable std::vector mControllersToRemap; }; } diff --git a/components/sceneutil/controller.hpp b/components/sceneutil/controller.hpp index 3f30e4ea03..695079d41b 100644 --- a/components/sceneutil/controller.hpp +++ b/components/sceneutil/controller.hpp @@ -6,6 +6,7 @@ namespace SceneUtil { + class CopyOp; class ControllerSource { @@ -41,6 +42,8 @@ namespace SceneUtil Controller(); virtual ~Controller() {} + virtual void remapTargets(const CopyOp& copyop) {} + bool hasInput() const; float getInputValue(osg::NodeVisitor* nv); diff --git a/components/sceneutil/serialize.cpp b/components/sceneutil/serialize.cpp index befc7fed5d..ef2858c59b 100644 --- a/components/sceneutil/serialize.cpp +++ b/components/sceneutil/serialize.cpp @@ -242,6 +242,7 @@ namespace SceneUtil "NifOsg::StaticBoundingBoxCallback", "NifOsg::GeomMorpherController", "NifOsg::UpdateMorphGeometry", + "NifOsg::LookAtController", "NifOsg::UVController", "NifOsg::VisController", "osgMyGUI::Drawable",