diff --git a/Engine/source/CMakeLists.txt b/Engine/source/CMakeLists.txt index 78343a2e6..f1c243940 100644 --- a/Engine/source/CMakeLists.txt +++ b/Engine/source/CMakeLists.txt @@ -58,10 +58,12 @@ torqueAddSourceDirectories("platform" "platform/threads" "platform/async" torqueAddSourceDirectories("platform/nativeDialogs") # Handle T3D -torqueAddSourceDirectories("T3D/fps" "T3D/fx" "T3D/vehicles" "T3D/physics" - "T3D/decal" "T3D/sfx" "T3D/gameBase" "T3D/turret" - "T3D/lighting" "T3D/gameOBjects" "T3D/components" - "T3D/systems" "T3D/assets" "T3D" "T3D/gameBase/std") +torqueAddSourceDirectories( "T3D" "T3D/assets" "T3D/decal" "T3D/examples" "T3D/fps" "T3D/fx" + "T3D/gameBase" "T3D/gameBase/std" + "T3D/lighting" + "T3D/physics" +# "T3D/components" "T3D/sceneComponent" "T3D/systems" "T3D/gameOBjects" + "T3D/sfx" "T3D/turret" "T3D/vehicles") # Handle TS torqueAddSourceDirectories("ts" "ts/collada" "ts/assimp" "ts/loader" "ts/arch") diff --git a/Engine/source/ts/assimp/assimpAppNode.cpp b/Engine/source/ts/assimp/assimpAppNode.cpp index 093101521..024186255 100644 --- a/Engine/source/ts/assimp/assimpAppNode.cpp +++ b/Engine/source/ts/assimp/assimpAppNode.cpp @@ -122,15 +122,17 @@ MatrixF AssimpAppNode::getTransform(F32 time) void AssimpAppNode::getAnimatedTransform(MatrixF& mat, F32 t, aiAnimation* animSeq) { // Find the channel for this node - for (U32 i = 0; i < animSeq->mNumChannels; ++i) + for (U32 k = 0; k < animSeq->mNumChannels; ++k) { - if (strcmp(mName, animSeq->mChannels[i]->mNodeName.C_Str()) == 0) + if (dStrcmp(mName, animSeq->mChannels[k]->mNodeName.C_Str()) == 0) { - aiNodeAnim *nodeAnim = animSeq->mChannels[i]; + 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; // Transform if (nodeAnim->mNumPositionKeys == 1) @@ -143,13 +145,13 @@ void AssimpAppNode::getAnimatedTransform(MatrixF& mat, F32 t, aiAnimation* animS { 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 > t) && (key > 0)) + if ((curT > frame) && (key > 0)) { - F32 factor = (t - lastT) / (curT - lastT); + F32 factor = (frame - lastT) / (curT - lastT); trans.interpolate(lastPos, curPos, factor); break; } - else if ((curT >= t) || (key == nodeAnim->mNumPositionKeys - 1)) + else if ((curT >= frame) || (key == nodeAnim->mNumPositionKeys - 1)) { trans = curPos; break; @@ -173,13 +175,13 @@ void AssimpAppNode::getAnimatedTransform(MatrixF& mat, F32 t, aiAnimation* animS 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 > t) && (key > 0)) + if ((curT > frame) && (key > 0)) { - F32 factor = (t - lastT) / (curT - lastT); + F32 factor = (frame - lastT) / (curT - lastT); rot.interpolate(lastRot, curRot, factor); break; } - else if ((curT >= t) || (key == nodeAnim->mNumRotationKeys - 1)) + else if ((curT >= frame) || (key == nodeAnim->mNumRotationKeys - 1)) { rot = curRot; break; @@ -201,13 +203,13 @@ void AssimpAppNode::getAnimatedTransform(MatrixF& mat, F32 t, aiAnimation* animS { 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 > t) && (key > 0)) + if ((curT > frame) && (key > 0)) { - F32 factor = (t - lastT) / (curT - lastT); + F32 factor = (frame - lastT) / (curT - lastT); scale.interpolate(lastScale, curScale, factor); break; } - else if ((curT >= t) || (key == nodeAnim->mNumScalingKeys - 1)) + else if ((curT >= frame) || (key == nodeAnim->mNumScalingKeys - 1)) { scale = curScale; break; diff --git a/Engine/source/ts/assimp/assimpAppSequence.cpp b/Engine/source/ts/assimp/assimpAppSequence.cpp index e0a78175e..496d1fb09 100644 --- a/Engine/source/ts/assimp/assimpAppSequence.cpp +++ b/Engine/source/ts/assimp/assimpAppSequence.cpp @@ -14,72 +14,75 @@ AssimpAppSequence::AssimpAppSequence(aiAnimation *a) : seqStart(0.0f), - mAnim(a) + seqEnd(0.0f) { + 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 = (mAnim->mTicksPerSecond > 0) ? mAnim->mTicksPerSecond : 30.0f; + fps = (a->mTicksPerSecond > 0) ? a->mTicksPerSecond : 30.0f; - U32 maxKeys = 0; - F32 maxEndTime = 0; - F32 minFrameTime = 100000.0f; - // Detect the frame rate (minimum time between keyframes) and max sequence time - for (U32 i = 0; i < mAnim->mNumChannels; ++i) + if (a->mDuration > 0) { - aiNodeAnim *nodeAnim = mAnim->mChannels[i]; - maxKeys = getMax(maxKeys, nodeAnim->mNumPositionKeys); - maxKeys = getMax(maxKeys, nodeAnim->mNumRotationKeys); - maxKeys = getMax(maxKeys, nodeAnim->mNumScalingKeys); + // 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); + } - if (nodeAnim->mNumPositionKeys) - maxEndTime = getMax(maxEndTime, (F32) nodeAnim->mPositionKeys[nodeAnim->mNumPositionKeys-1].mTime); - if (nodeAnim->mNumRotationKeys) - maxEndTime = getMax(maxEndTime, (F32) nodeAnim->mRotationKeys[nodeAnim->mNumRotationKeys-1].mTime); - if (nodeAnim->mNumScalingKeys) - maxEndTime = getMax(maxEndTime, (F32) nodeAnim->mScalingKeys[nodeAnim->mNumScalingKeys-1].mTime); - - for (U32 key = 1; key < nodeAnim->mNumPositionKeys; ++key) - { - F32 deltaT = nodeAnim->mPositionKeys[key].mTime - nodeAnim->mPositionKeys[key-1].mTime; - minFrameTime = getMin(minFrameTime, deltaT); - } - for (U32 key = 1; key < nodeAnim->mNumRotationKeys; ++key) - { - F32 deltaT = nodeAnim->mRotationKeys[key].mTime - nodeAnim->mRotationKeys[key-1].mTime; - minFrameTime = getMin(minFrameTime, deltaT); - } - for (U32 key = 1; key < nodeAnim->mNumScalingKeys; ++key) - { - F32 deltaT = nodeAnim->mScalingKeys[key].mTime - nodeAnim->mScalingKeys[key-1].mTime; - minFrameTime = getMin(minFrameTime, deltaT); + seqEnd = getMax(seqEnd, maxKeyTime); } } + mTimeMultiplier = 1.0f; + S32 timeFactor = ColladaUtils::getOptions().animTiming; - S32 fpsRequest = ColladaUtils::getOptions().animFPS; + S32 fpsRequest = (S32)a->mTicksPerSecond; if (timeFactor == 0) { // Timing specified in frames fps = mClamp(fpsRequest, 5 /*TSShapeLoader::MinFrameRate*/, TSShapeLoader::MaxFrameRate); - maxKeys = getMax(maxKeys, (U32)maxEndTime); // Keys won't be assigned for every frame. - seqEnd = maxKeys / fps; mTimeMultiplier = 1.0f / fps; } else { // Timing specified in seconds or ms depending on format - if (maxEndTime > 1000.0f || mAnim->mDuration > 1000.0f) + 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); - minFrameTime /= (F32)timeFactor; - maxEndTime /= (F32)timeFactor; - fps = (minFrameTime > 0.0f) ? 1.0f / minFrameTime : fps; - fps = mClamp(fpsRequest, 5 /*TSShapeLoader::MinFrameRate*/, TSShapeLoader::MaxFrameRate); - seqEnd = maxEndTime; mTimeMultiplier = 1.0f / timeFactor; } + } AssimpAppSequence::~AssimpAppSequence() @@ -102,7 +105,7 @@ void AssimpAppSequence::setActive(bool active) U32 AssimpAppSequence::getFlags() const { - return TSShape::Blend; + return TSShape::Cyclic; } F32 AssimpAppSequence::getPriority() const { @@ -110,5 +113,5 @@ F32 AssimpAppSequence::getPriority() const } F32 AssimpAppSequence::getBlendRefTime() const { - return -1.0f; -} \ No newline at end of file + return 0.0f; +} diff --git a/Engine/source/ts/assimp/assimpAppSequence.h b/Engine/source/ts/assimp/assimpAppSequence.h index be5c11025..467aeb4a2 100644 --- a/Engine/source/ts/assimp/assimpAppSequence.h +++ b/Engine/source/ts/assimp/assimpAppSequence.h @@ -48,4 +48,4 @@ public: virtual U32 getFlags() const; virtual F32 getPriority() const; virtual F32 getBlendRefTime() const; -}; \ No newline at end of file +}; diff --git a/Engine/source/ts/assimp/assimpShapeLoader.cpp b/Engine/source/ts/assimp/assimpShapeLoader.cpp index 2a3735243..908e186f2 100644 --- a/Engine/source/ts/assimp/assimpShapeLoader.cpp +++ b/Engine/source/ts/assimp/assimpShapeLoader.cpp @@ -229,6 +229,18 @@ void AssimpShapeLoader::enumerateScene() if (!processNode(node)) delete node; + // add bounds node. + if (!boundsNode) + { + aiNode* req[1]; + req[0] = new aiNode("bounds"); + mScene->mRootNode->addChildren(1, req); + + AssimpAppNode* appBounds = new AssimpAppNode(mScene, req[0]); + if (!processNode(appBounds)) + delete appBounds; + } + // Check for animations and process those. processAnimations(); } @@ -243,13 +255,44 @@ void AssimpShapeLoader::enumerateScene() void AssimpShapeLoader::processAnimations() { - for(U32 n = 0; n < mScene->mNumAnimations; ++n) - { - Con::printf("[ASSIMP] Animation Found: %s", mScene->mAnimations[n]->mName.C_Str()); + // add all animations into 1 ambient animation. + aiAnimation* ambientSeq = new aiAnimation(); + ambientSeq->mName = "ambient"; - AssimpAppSequence* newAssimpSeq = new AssimpAppSequence(mScene->mAnimations[n]); - appSequences.push_back(newAssimpSeq); + Vector ambientChannels; + F32 duration = 0.0f; + + for (U32 i = 0; i < mScene->mNumAnimations; ++i) + { + aiAnimation* anim = mScene->mAnimations[i]; + for (U32 j = 0; j < anim->mNumChannels; j++) + { + aiNodeAnim* nodeAnim = anim->mChannels[j]; + // 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); + } + + ambientChannels.push_back(nodeAnim); + + duration = getMax(duration, maxKeyTime); + } } + + ambientSeq->mNumChannels = ambientChannels.size(); + ambientSeq->mChannels = ambientChannels.address(); + ambientSeq->mDuration = duration; + ambientSeq->mTicksPerSecond = 24.0; + + AssimpAppSequence* defaultAssimpSeq = new AssimpAppSequence(ambientSeq); + appSequences.push_back(defaultAssimpSeq); } void AssimpShapeLoader::computeBounds(Box3F& bounds) @@ -369,12 +412,16 @@ bool AssimpShapeLoader::fillGuiTreeView(const char* sourceShapePath, GuiTreeView tree->insertItem(matItem, String::ToString("%s", name.c_str()), String::ToString("%s", texName.c_str())); } - for (U32 i = 0; i < shapeScene->mNumAnimations; i++) + if (shapeScene->mNumAnimations == 0) { - String sequenceName = shapeScene->mAnimations[i]->mName.C_Str(); - if (sequenceName.isEmpty()) - sequenceName = "ambient"; - tree->insertItem(animItem, sequenceName.c_str()); + tree->insertItem(animItem, "ambient", "animation", "", 0, 0); + } + else + { + for (U32 i = 0; i < shapeScene->mNumAnimations; i++) + { + tree->insertItem(animItem, shapeScene->mAnimations[i]->mName.C_Str(), "animation", "", 0, 0); + } } U32 numNodes = 0;