Implement NiLookAtController (#6938)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <osgParticle/Emitter>
|
||||
|
||||
#include <components/nif/data.hpp>
|
||||
#include <components/sceneutil/clone.hpp>
|
||||
#include <components/sceneutil/morphgeometry.hpp>
|
||||
|
||||
#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<LookAtController, NifOsg::MatrixTransform*>(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<osg::Group> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <type_traits>
|
||||
|
||||
#include <osg/Texture2D>
|
||||
#include <osg/observer_ptr>
|
||||
|
||||
#include <components/nif/controller.hpp>
|
||||
#include <components/nif/data.hpp>
|
||||
@@ -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<LookAtController, NifOsg::MatrixTransform*>,
|
||||
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<osg::Group> mTarget;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -331,6 +331,9 @@ namespace NifOsg
|
||||
// This is used to queue emitters that weren't attached to their node yet.
|
||||
std::vector<std::pair<unsigned int, osg::ref_ptr<Emitter>>> mEmitterQueue;
|
||||
|
||||
// This is used to queue look-at controllers whose target nodes may not have been created yet.
|
||||
mutable std::vector<std::pair<unsigned int, osg::ref_ptr<LookAtController>>> 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<const Nif::NiLookAtController*>(ctrl.getPtr());
|
||||
if (lookatctrl->mLookAt.empty())
|
||||
continue;
|
||||
|
||||
osg::ref_ptr<LookAtController> 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)
|
||||
{
|
||||
|
||||
@@ -1018,6 +1018,9 @@ namespace Resource
|
||||
}
|
||||
}
|
||||
osg::ref_ptr<osg::Node> cloned = static_cast<osg::Node*>(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
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <osgParticle/ParticleProcessor>
|
||||
#include <osgParticle/ParticleSystemUpdater>
|
||||
|
||||
#include <components/sceneutil/controller.hpp>
|
||||
#include <components/sceneutil/morphgeometry.hpp>
|
||||
#include <components/sceneutil/riggeometry.hpp>
|
||||
#include <components/sceneutil/riggeometryosgaextension.hpp>
|
||||
@@ -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<Controller*>(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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define OPENMW_COMPONENTS_SCENEUTIL_CLONE_H
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include <osg/CopyOp>
|
||||
|
||||
@@ -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<osgParticle::ParticleProcessor*, const osgParticle::ParticleSystem*> mProcessorToOldPs;
|
||||
mutable std::map<osgParticle::ParticleSystemUpdater*, const osgParticle::ParticleSystem*> mUpdaterToOldPs;
|
||||
mutable std::map<const osgParticle::ParticleSystem*, osgParticle::ParticleSystem*> mOldPsToNewPs;
|
||||
mutable std::map<const osg::Node*, osg::Node*> mClonedNodes;
|
||||
mutable std::vector<Controller*> mControllersToRemap;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -242,6 +242,7 @@ namespace SceneUtil
|
||||
"NifOsg::StaticBoundingBoxCallback",
|
||||
"NifOsg::GeomMorpherController",
|
||||
"NifOsg::UpdateMorphGeometry",
|
||||
"NifOsg::LookAtController",
|
||||
"NifOsg::UVController",
|
||||
"NifOsg::VisController",
|
||||
"osgMyGUI::Drawable",
|
||||
|
||||
Reference in New Issue
Block a user