mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-01-20 04:34:48 +00:00
animation update
updated how animations are handled from assimp gltf timing now correct
This commit is contained in:
parent
5f1c2a63e5
commit
28fcb8d68b
|
|
@ -135,115 +135,104 @@ MatrixF AssimpAppNode::getTransform(F32 time)
|
|||
|
||||
void AssimpAppNode::getAnimatedTransform(MatrixF& mat, F32 t, aiAnimation* animSeq)
|
||||
{
|
||||
// Find the channel for this node
|
||||
// Convert time `t` (in seconds) to a frame index
|
||||
const F32 frameTime = (t * animSeq->mTicksPerSecond + 0.5f) + 1.0f;
|
||||
|
||||
// Loop through animation channels to find the matching node
|
||||
for (U32 k = 0; k < animSeq->mNumChannels; ++k)
|
||||
{
|
||||
if (dStrcmp(mName, animSeq->mChannels[k]->mNodeName.C_Str()) == 0)
|
||||
const aiNodeAnim* nodeAnim = animSeq->mChannels[k];
|
||||
if (dStrcmp(mName, nodeAnim->mNodeName.C_Str()) != 0)
|
||||
continue;
|
||||
|
||||
Point3F translation(Point3F::Zero);
|
||||
QuatF rotation(QuatF::Identity);
|
||||
Point3F scale(Point3F::One);
|
||||
|
||||
// Interpolate Translation Keys
|
||||
if (nodeAnim->mNumPositionKeys > 0)
|
||||
{
|
||||
aiNodeAnim *nodeAnim = animSeq->mChannels[k];
|
||||
Point3F trans(Point3F::Zero);
|
||||
Point3F scale(Point3F::One);
|
||||
QuatF rot;
|
||||
rot.identity();
|
||||
// T is in seconds, convert to frames.
|
||||
F32 frame = (t * animSeq->mTicksPerSecond + 0.5f) + 1.0f;
|
||||
translation = interpolateVectorKey(nodeAnim->mPositionKeys, nodeAnim->mNumPositionKeys, frameTime);
|
||||
}
|
||||
|
||||
// Transform
|
||||
if (nodeAnim->mNumPositionKeys == 1)
|
||||
trans.set(nodeAnim->mPositionKeys[0].mValue.x, nodeAnim->mPositionKeys[0].mValue.y, nodeAnim->mPositionKeys[0].mValue.z);
|
||||
else
|
||||
{
|
||||
Point3F curPos, lastPos;
|
||||
F32 lastT = 0.0;
|
||||
for (U32 key = 0; key < nodeAnim->mNumPositionKeys; ++key)
|
||||
{
|
||||
F32 curT = sTimeMultiplier * (F32)nodeAnim->mPositionKeys[key].mTime;
|
||||
curPos.set(nodeAnim->mPositionKeys[key].mValue.x, nodeAnim->mPositionKeys[key].mValue.y, nodeAnim->mPositionKeys[key].mValue.z);
|
||||
if ((curT > frame) && (key > 0))
|
||||
{
|
||||
F32 factor = (frame - lastT) / (curT - lastT);
|
||||
trans.interpolate(lastPos, curPos, factor);
|
||||
break;
|
||||
}
|
||||
else if ((curT >= frame) || (key == nodeAnim->mNumPositionKeys - 1))
|
||||
{
|
||||
trans = curPos;
|
||||
break;
|
||||
}
|
||||
// Interpolate Rotation Keys
|
||||
if (nodeAnim->mNumRotationKeys > 0)
|
||||
{
|
||||
rotation = interpolateQuaternionKey(nodeAnim->mRotationKeys, nodeAnim->mNumRotationKeys, frameTime);
|
||||
}
|
||||
|
||||
lastT = curT;
|
||||
lastPos = curPos;
|
||||
}
|
||||
}
|
||||
// Interpolate Scaling Keys
|
||||
if (nodeAnim->mNumScalingKeys > 0)
|
||||
{
|
||||
scale = interpolateVectorKey(nodeAnim->mScalingKeys, nodeAnim->mNumScalingKeys, frameTime);
|
||||
}
|
||||
|
||||
// Rotation
|
||||
if (nodeAnim->mNumRotationKeys == 1)
|
||||
rot.set(nodeAnim->mRotationKeys[0].mValue.x, nodeAnim->mRotationKeys[0].mValue.y,
|
||||
nodeAnim->mRotationKeys[0].mValue.z, nodeAnim->mRotationKeys[0].mValue.w);
|
||||
else
|
||||
{
|
||||
QuatF curRot, lastRot;
|
||||
F32 lastT = 0.0;
|
||||
for (U32 key = 0; key < nodeAnim->mNumRotationKeys; ++key)
|
||||
{
|
||||
F32 curT = sTimeMultiplier * (F32)nodeAnim->mRotationKeys[key].mTime;
|
||||
curRot.set(nodeAnim->mRotationKeys[key].mValue.x, nodeAnim->mRotationKeys[key].mValue.y,
|
||||
nodeAnim->mRotationKeys[key].mValue.z, nodeAnim->mRotationKeys[key].mValue.w);
|
||||
if ((curT > frame) && (key > 0))
|
||||
{
|
||||
F32 factor = (frame - lastT) / (curT - lastT);
|
||||
rot.interpolate(lastRot, curRot, factor);
|
||||
break;
|
||||
}
|
||||
else if ((curT >= frame) || (key == nodeAnim->mNumRotationKeys - 1))
|
||||
{
|
||||
rot = curRot;
|
||||
break;
|
||||
}
|
||||
// Apply the interpolated transform components to the matrix
|
||||
rotation.setMatrix(&mat);
|
||||
mat.inverse();
|
||||
mat.setPosition(translation);
|
||||
mat.scale(scale);
|
||||
|
||||
lastT = curT;
|
||||
lastRot = curRot;
|
||||
}
|
||||
}
|
||||
return; // Exit after processing the matching node
|
||||
}
|
||||
|
||||
// Scale
|
||||
if (nodeAnim->mNumScalingKeys == 1)
|
||||
scale.set(nodeAnim->mScalingKeys[0].mValue.x, nodeAnim->mScalingKeys[0].mValue.y, nodeAnim->mScalingKeys[0].mValue.z);
|
||||
else
|
||||
{
|
||||
Point3F curScale, lastScale;
|
||||
F32 lastT = 0.0;
|
||||
for (U32 key = 0; key < nodeAnim->mNumScalingKeys; ++key)
|
||||
{
|
||||
F32 curT = sTimeMultiplier * (F32)nodeAnim->mScalingKeys[key].mTime;
|
||||
curScale.set(nodeAnim->mScalingKeys[key].mValue.x, nodeAnim->mScalingKeys[key].mValue.y, nodeAnim->mScalingKeys[key].mValue.z);
|
||||
if ((curT > frame) && (key > 0))
|
||||
{
|
||||
F32 factor = (frame - lastT) / (curT - lastT);
|
||||
scale.interpolate(lastScale, curScale, factor);
|
||||
break;
|
||||
}
|
||||
else if ((curT >= frame) || (key == nodeAnim->mNumScalingKeys - 1))
|
||||
{
|
||||
scale = curScale;
|
||||
break;
|
||||
}
|
||||
// Default to the static node transformation if no animation data is found
|
||||
mat = mNodeTransform;
|
||||
}
|
||||
|
||||
lastT = curT;
|
||||
lastScale = curScale;
|
||||
}
|
||||
}
|
||||
Point3F AssimpAppNode::interpolateVectorKey(const aiVectorKey* keys, U32 numKeys, F32 frameTime)
|
||||
{
|
||||
if (numKeys == 1) // Single keyframe: use it directly
|
||||
return Point3F(keys[0].mValue.x, keys[0].mValue.y, keys[0].mValue.z);
|
||||
|
||||
rot.setMatrix(&mat);
|
||||
mat.inverse();
|
||||
mat.setPosition(trans);
|
||||
mat.scale(scale);
|
||||
return;
|
||||
// Clamp frameTime to the bounds of the keyframes
|
||||
if (frameTime <= keys[0].mTime) {
|
||||
// Before the first keyframe, return the first key
|
||||
return Point3F(keys[0].mValue.x, keys[0].mValue.y, keys[0].mValue.z);
|
||||
}
|
||||
if (frameTime >= keys[numKeys - 1].mTime) {
|
||||
// After the last keyframe, return the last key
|
||||
return Point3F(keys[numKeys - 1].mValue.x, keys[numKeys - 1].mValue.y, keys[numKeys - 1].mValue.z);
|
||||
}
|
||||
|
||||
// Interpolate between the two nearest keyframes
|
||||
for (U32 i = 1; i < numKeys; ++i)
|
||||
{
|
||||
if (frameTime < keys[i].mTime)
|
||||
{
|
||||
const F32 factor = (frameTime - keys[i - 1].mTime) / (keys[i].mTime - keys[i - 1].mTime);
|
||||
Point3F start(keys[i - 1].mValue.x, keys[i - 1].mValue.y, keys[i - 1].mValue.z);
|
||||
Point3F end(keys[i].mValue.x, keys[i].mValue.y, keys[i].mValue.z);
|
||||
Point3F result;
|
||||
result.interpolate(start, end, factor);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// Node not found in the animation channels
|
||||
mat = mNodeTransform;
|
||||
// Default to the last keyframe
|
||||
return Point3F(keys[numKeys - 1].mValue.x, keys[numKeys - 1].mValue.y, keys[numKeys - 1].mValue.z);
|
||||
}
|
||||
|
||||
QuatF AssimpAppNode::interpolateQuaternionKey(const aiQuatKey* keys, U32 numKeys, F32 frameTime)
|
||||
{
|
||||
if (numKeys == 1) // Single keyframe: use it directly
|
||||
return QuatF(keys[0].mValue.x, keys[0].mValue.y, keys[0].mValue.z, keys[0].mValue.w);
|
||||
|
||||
for (U32 i = 1; i < numKeys; ++i)
|
||||
{
|
||||
if (frameTime < keys[i].mTime)
|
||||
{
|
||||
const F32 factor = (frameTime - keys[i - 1].mTime) / (keys[i].mTime - keys[i - 1].mTime);
|
||||
QuatF start(keys[i - 1].mValue.x, keys[i - 1].mValue.y, keys[i - 1].mValue.z, keys[i - 1].mValue.w);
|
||||
QuatF end(keys[i].mValue.x, keys[i].mValue.y, keys[i].mValue.z, keys[i].mValue.w);
|
||||
QuatF result;
|
||||
result.interpolate(start, end, factor);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// Default to the last keyframe
|
||||
return QuatF(keys[numKeys - 1].mValue.x, keys[numKeys - 1].mValue.y, keys[numKeys - 1].mValue.z, keys[numKeys - 1].mValue.w);
|
||||
}
|
||||
|
||||
bool AssimpAppNode::animatesTransform(const AppSequence* appSeq)
|
||||
|
|
|
|||
|
|
@ -45,6 +45,8 @@ class AssimpAppNode : public AppNode
|
|||
|
||||
MatrixF getTransform(F32 time);
|
||||
void getAnimatedTransform(MatrixF& mat, F32 t, aiAnimation* animSeq);
|
||||
Point3F interpolateVectorKey(const aiVectorKey* keys, U32 numKeys, F32 frameTime);
|
||||
QuatF interpolateQuaternionKey(const aiQuatKey* keys, U32 numKeys, F32 frameTime);
|
||||
void buildMeshList() override;
|
||||
void buildChildList() override;
|
||||
|
||||
|
|
|
|||
|
|
@ -12,94 +12,103 @@
|
|||
#include "ts/assimp/assimpAppSequence.h"
|
||||
#include "ts/assimp/assimpAppNode.h"
|
||||
|
||||
AssimpAppSequence::AssimpAppSequence(aiAnimation *a) :
|
||||
seqStart(0.0f),
|
||||
seqEnd(0.0f)
|
||||
AssimpAppSequence::AssimpAppSequence(aiAnimation* a)
|
||||
: seqStart(0.0f), seqEnd(0.0f), mTimeMultiplier(1.0f)
|
||||
{
|
||||
fps = 30.0f;
|
||||
// Deep copy animation structure
|
||||
mAnim = new aiAnimation(*a);
|
||||
// Deep copy channels
|
||||
mAnim->mChannels = new aiNodeAnim * [a->mNumChannels];
|
||||
for (U32 i = 0; i < a->mNumChannels; ++i) {
|
||||
mAnim->mChannels[i] = new aiNodeAnim(*a->mChannels[i]);
|
||||
}
|
||||
|
||||
// Deep copy meshes
|
||||
mAnim->mMeshChannels = new aiMeshAnim * [a->mNumMeshChannels];
|
||||
for (U32 i = 0; i < a->mNumMeshChannels; ++i) {
|
||||
mAnim->mMeshChannels[i] = new aiMeshAnim(*a->mMeshChannels[i]);
|
||||
}
|
||||
|
||||
// Deep copy name
|
||||
mAnim->mName = a->mName;
|
||||
|
||||
mSequenceName = mAnim->mName.C_Str();
|
||||
if (mSequenceName.isEmpty())
|
||||
mSequenceName = "ambient";
|
||||
Con::printf("\n[Assimp] Adding %s animation", mSequenceName.c_str());
|
||||
|
||||
fps = (a->mTicksPerSecond > 0) ? a->mTicksPerSecond : 30.0f;
|
||||
Con::printf("\n[Assimp] Adding animation: %s", mSequenceName.c_str());
|
||||
|
||||
if (a->mDuration > 0)
|
||||
{
|
||||
// torques seqEnd is in seconds, this then gets generated into frames in generateSequences()
|
||||
seqEnd = (F32)a->mDuration / fps;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (U32 i = 0; i < a->mNumChannels; ++i)
|
||||
{
|
||||
aiNodeAnim* nodeAnim = a->mChannels[i];
|
||||
// Determine the maximum keyframe time for this animation
|
||||
F32 maxKeyTime = 0.0f;
|
||||
for (U32 k = 0; k < nodeAnim->mNumPositionKeys; k++) {
|
||||
maxKeyTime = getMax(maxKeyTime, (F32)nodeAnim->mPositionKeys[k].mTime);
|
||||
}
|
||||
for (U32 k = 0; k < nodeAnim->mNumRotationKeys; k++) {
|
||||
maxKeyTime = getMax(maxKeyTime, (F32)nodeAnim->mRotationKeys[k].mTime);
|
||||
}
|
||||
for (U32 k = 0; k < nodeAnim->mNumScalingKeys; k++) {
|
||||
maxKeyTime = getMax(maxKeyTime, (F32)nodeAnim->mScalingKeys[k].mTime);
|
||||
}
|
||||
|
||||
seqEnd = getMax(seqEnd, maxKeyTime);
|
||||
}
|
||||
}
|
||||
|
||||
mTimeMultiplier = 1.0f;
|
||||
|
||||
S32 timeFactor = ColladaUtils::getOptions().animTiming;
|
||||
S32 fpsRequest = (S32)a->mTicksPerSecond;
|
||||
if (timeFactor == 0)
|
||||
{ // Timing specified in frames
|
||||
fps = mClamp(fpsRequest, 5 /*TSShapeLoader::MinFrameRate*/, TSShapeLoader::MaxFrameRate);
|
||||
mTimeMultiplier = 1.0f / fps;
|
||||
}
|
||||
else
|
||||
{ // Timing specified in seconds or ms depending on format
|
||||
if (seqEnd > 1000.0f || a->mDuration > 1000.0f)
|
||||
timeFactor = 1000.0f; // If it's more than 1000 seconds, assume it's ms.
|
||||
|
||||
timeFactor = mClamp(timeFactor, 1, 1000);
|
||||
mTimeMultiplier = 1.0f / timeFactor;
|
||||
}
|
||||
// Determine the FPS and Time Multiplier
|
||||
determineTimeMultiplier(a);
|
||||
|
||||
// Calculate sequence end time based on keyframes and multiplier
|
||||
calculateSequenceEnd(a);
|
||||
}
|
||||
|
||||
AssimpAppSequence::~AssimpAppSequence()
|
||||
{
|
||||
}
|
||||
|
||||
void AssimpAppSequence::determineTimeMultiplier(aiAnimation* a)
|
||||
{
|
||||
// Set fps from the file or use default
|
||||
fps = (a->mTicksPerSecond > 0) ? a->mTicksPerSecond : 30.0f;
|
||||
|
||||
if (fps >= 1000.0f) { // Indicates milliseconds (GLTF or similar formats)
|
||||
mTimeMultiplier = 1.0f / 1000.0f; // Convert milliseconds to seconds
|
||||
Con::printf("[Assimp] Detected milliseconds timing (FPS >= 1000). Time Multiplier: %f", mTimeMultiplier);
|
||||
}
|
||||
else if (fps > 0.0f) { // Standard FPS
|
||||
fps = mClamp(fps, 5 /*TSShapeLoader::MinFrameRate*/, TSShapeLoader::MaxFrameRate);
|
||||
mTimeMultiplier = 1.0f / fps;
|
||||
Con::printf("[Assimp] Standard FPS detected. Time Multiplier: %f", mTimeMultiplier);
|
||||
}
|
||||
else {
|
||||
// Fall back to 30 FPS as default
|
||||
mTimeMultiplier = 1.0f / 30.0f;
|
||||
Con::printf("[Assimp] FPS not specified. Using default 30 FPS. Time Multiplier: %f", mTimeMultiplier);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void AssimpAppSequence::calculateSequenceEnd(aiAnimation* a)
|
||||
{
|
||||
for (U32 i = 0; i < a->mNumChannels; ++i) {
|
||||
aiNodeAnim* nodeAnim = a->mChannels[i];
|
||||
F32 maxKeyTime = 0.0f;
|
||||
|
||||
// Calculate the maximum time across all keyframes for this channel
|
||||
for (U32 k = 0; k < nodeAnim->mNumPositionKeys; ++k) {
|
||||
maxKeyTime = getMax(maxKeyTime, (F32)nodeAnim->mPositionKeys[k].mTime);
|
||||
}
|
||||
for (U32 k = 0; k < nodeAnim->mNumRotationKeys; ++k) {
|
||||
maxKeyTime = getMax(maxKeyTime, (F32)nodeAnim->mRotationKeys[k].mTime);
|
||||
}
|
||||
for (U32 k = 0; k < nodeAnim->mNumScalingKeys; ++k) {
|
||||
maxKeyTime = getMax(maxKeyTime, (F32)nodeAnim->mScalingKeys[k].mTime);
|
||||
}
|
||||
|
||||
// Use the multiplier to convert to real sequence time
|
||||
seqEnd = mTimeMultiplier * getMax(seqEnd, maxKeyTime);
|
||||
}
|
||||
|
||||
Con::printf("[Assimp] Sequence End Time: %f seconds", seqEnd);
|
||||
}
|
||||
|
||||
|
||||
void AssimpAppSequence::setActive(bool active)
|
||||
{
|
||||
if (active)
|
||||
{
|
||||
AssimpAppNode::sActiveSequence = mAnim;
|
||||
AssimpAppNode::sTimeMultiplier = mTimeMultiplier;
|
||||
Con::printf("[Assimp] Activating sequence: %s with Time Multiplier: %f", mSequenceName.c_str(), mTimeMultiplier);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (AssimpAppNode::sActiveSequence == mAnim)
|
||||
{
|
||||
AssimpAppNode::sActiveSequence = NULL;
|
||||
Con::printf("[Assimp] Deactivating sequence: %s", mSequenceName.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,11 +27,14 @@ class AssimpAppSequence : public AppSequence
|
|||
F32 seqEnd;
|
||||
F32 mTimeMultiplier; // The factor needed to convert the sequence data timestamp to seconds
|
||||
|
||||
void determineTimeMultiplier(aiAnimation* a);
|
||||
void calculateSequenceEnd(aiAnimation* a);
|
||||
|
||||
public:
|
||||
|
||||
AssimpAppSequence(aiAnimation *a);
|
||||
~AssimpAppSequence();
|
||||
|
||||
|
||||
aiAnimation *mAnim;
|
||||
|
||||
void setActive(bool active) override;
|
||||
|
|
|
|||
|
|
@ -349,12 +349,16 @@ void AssimpShapeLoader::processAnimations()
|
|||
|
||||
Vector<aiNodeAnim*> ambientChannels;
|
||||
F32 duration = 0.0f;
|
||||
F32 ticks = 0.0f;
|
||||
if (mScene->mNumAnimations > 0)
|
||||
{
|
||||
for (U32 i = 0; i < mScene->mNumAnimations; ++i)
|
||||
{
|
||||
aiAnimation* anim = mScene->mAnimations[i];
|
||||
duration = anim->mDuration;
|
||||
|
||||
ticks = anim->mTicksPerSecond;
|
||||
|
||||
duration = 0.0f;
|
||||
for (U32 j = 0; j < anim->mNumChannels; j++)
|
||||
{
|
||||
aiNodeAnim* nodeAnim = anim->mChannels[j];
|
||||
|
|
@ -379,7 +383,7 @@ void AssimpShapeLoader::processAnimations()
|
|||
ambientSeq->mNumChannels = ambientChannels.size();
|
||||
ambientSeq->mChannels = ambientChannels.address();
|
||||
ambientSeq->mDuration = duration;
|
||||
ambientSeq->mTicksPerSecond = 24.0;
|
||||
ambientSeq->mTicksPerSecond = ticks;
|
||||
|
||||
AssimpAppSequence* defaultAssimpSeq = new AssimpAppSequence(ambientSeq);
|
||||
appSequences.push_back(defaultAssimpSeq);
|
||||
|
|
|
|||
|
|
@ -57,9 +57,7 @@ IMPLEMENT_CALLBACK(TSShapeConstructor, onUnload, void, (), (),
|
|||
ImplementEnumType(TSShapeConstructorUpAxis,
|
||||
"Axis to use for upwards direction when importing from Collada.\n\n"
|
||||
"@ingroup TSShapeConstructor")
|
||||
{
|
||||
UPAXISTYPE_X_UP, "X_AXIS"
|
||||
},
|
||||
{ UPAXISTYPE_X_UP, "X_AXIS" },
|
||||
{ UPAXISTYPE_Y_UP, "Y_AXIS" },
|
||||
{ UPAXISTYPE_Z_UP, "Z_AXIS" },
|
||||
{ UPAXISTYPE_COUNT, "DEFAULT" }
|
||||
|
|
@ -68,9 +66,7 @@ EndImplementEnumType;
|
|||
ImplementEnumType(TSShapeConstructorLodType,
|
||||
"\n\n"
|
||||
"@ingroup TSShapeConstructor")
|
||||
{
|
||||
ColladaUtils::ImportOptions::DetectDTS, "DetectDTS"
|
||||
},
|
||||
{ ColladaUtils::ImportOptions::DetectDTS, "DetectDTS" },
|
||||
{ ColladaUtils::ImportOptions::SingleSize, "SingleSize" },
|
||||
{ ColladaUtils::ImportOptions::TrailingNumber, "TrailingNumber" },
|
||||
EndImplementEnumType;
|
||||
|
|
@ -78,9 +74,7 @@ ImplementEnumType(TSShapeConstructorLodType,
|
|||
ImplementEnumType(TSShapeConstructorAnimType,
|
||||
"\n\n"
|
||||
"@ingroup TSShapeConstructor")
|
||||
{
|
||||
ColladaUtils::ImportOptions::FrameCount, "Frames"
|
||||
},
|
||||
{ ColladaUtils::ImportOptions::FrameCount, "Frames" },
|
||||
{ ColladaUtils::ImportOptions::Seconds, "Seconds" },
|
||||
{ ColladaUtils::ImportOptions::Milliseconds, "Milliseconds" },
|
||||
EndImplementEnumType;
|
||||
|
|
|
|||
Loading…
Reference in a new issue