From 4ac9639a52c3635215299202c74254fd900db267 Mon Sep 17 00:00:00 2001 From: marauder2k7 Date: Wed, 11 Dec 2024 15:38:17 +0000 Subject: [PATCH] bkup commit fbx details matching for skinned meshes, gltf still offset --- Engine/source/ts/assimp/assimpAppNode.cpp | 53 +++++-------------- Engine/source/ts/assimp/assimpAppNode.h | 24 +++++---- Engine/source/ts/assimp/assimpShapeLoader.cpp | 49 +++++++++++++++-- Engine/source/ts/assimp/assimpShapeLoader.h | 8 +++ Engine/source/ts/loader/appNode.h | 4 +- Engine/source/ts/loader/tsShapeLoader.h | 2 +- 6 files changed, 83 insertions(+), 57 deletions(-) diff --git a/Engine/source/ts/assimp/assimpAppNode.cpp b/Engine/source/ts/assimp/assimpAppNode.cpp index 32c783868..f7bccd7ab 100644 --- a/Engine/source/ts/assimp/assimpAppNode.cpp +++ b/Engine/source/ts/assimp/assimpAppNode.cpp @@ -34,17 +34,14 @@ aiAnimation* AssimpAppNode::sActiveSequence = NULL; F32 AssimpAppNode::sTimeMultiplier = 1.0f; -AssimpAppNode::AssimpAppNode(const struct aiScene* scene, const struct aiNode* node, AssimpAppNode* parent) +AssimpAppNode::AssimpAppNode(const aiScene* scene, const aiNode* node, AssimpAppNode* parentNode) : mScene(scene), mNode(node ? node : scene->mRootNode), - appParent(parent), mInvertMeshes(false), mLastTransformTime(TSShapeLoader::DefaultTime - 1), mDefaultTransformValid(false) { - - mScene = scene; - mNode = node ? node : scene->mRootNode; + appParent = parentNode; // Initialize node and parent names. mName = dStrdup(mNode->mName.C_Str()); if ( dStrlen(mName) == 0 ) @@ -52,46 +49,12 @@ AssimpAppNode::AssimpAppNode(const struct aiScene* scene, const struct aiNode* n const char* defaultName = "null"; mName = dStrdup(defaultName); } - - mParentName = dStrdup(parent ? parent->getName() : "ROOT"); - + mParentName = dStrdup(parentNode ? parentNode->mName : "ROOT"); // Convert transformation matrix assimpToTorqueMat(node->mTransformation, mNodeTransform); Con::printf("[ASSIMP] Node Created: %s, Parent: %s", mName, mParentName); } -// Get all child nodes -void AssimpAppNode::buildChildList() -{ - // Ensure mNode is valid - if (!mNode) { - Con::errorf("[ASSIMP] Error: mNode is null in buildChildList"); - return; - } - - if (!mNode->mChildren) - return; - - for (U32 n = 0; n < mNode->mNumChildren; ++n) { - if (!mNode->mChildren[n]) { - Con::errorf("[ASSIMP] Warning: Null child node at index %d", n); - continue; - } - - mChildNodes.push_back(new AssimpAppNode(mScene, mNode->mChildren[n], this)); - } -} - -// Get all geometry attached to this node -void AssimpAppNode::buildMeshList() -{ - for (U32 n = 0; n < mNode->mNumMeshes; ++n) - { - const struct aiMesh* mesh = mScene->mMeshes[mNode->mMeshes[n]]; - mMeshes.push_back(new AssimpAppMesh(mesh, this)); - } -} - MatrixF AssimpAppNode::getTransform(F32 time) { // Check if we can use the last computed transform @@ -338,3 +301,13 @@ aiNode* AssimpAppNode::findChildNodeByName(const char* nodeName, aiNode* rootNod } return nullptr; } + +void AssimpAppNode::addChild(AssimpAppNode* child) +{ + mChildNodes.push_back(child); +} + +void AssimpAppNode::addMesh(AssimpAppMesh* child) +{ + mMeshes.push_back(child); +} diff --git a/Engine/source/ts/assimp/assimpAppNode.h b/Engine/source/ts/assimp/assimpAppNode.h index e4a4c9d47..788f0c763 100644 --- a/Engine/source/ts/assimp/assimpAppNode.h +++ b/Engine/source/ts/assimp/assimpAppNode.h @@ -47,25 +47,24 @@ class AssimpAppNode : public AppNode 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; - + void buildMeshList() override {}; + void buildChildList() override {}; protected: const aiScene* mScene; const aiNode* mNode; ///< Pointer to the assimp scene node - AssimpAppNode* appParent; ///< Parent node - MatrixF mNodeTransform; ///< Scene node transform converted to TorqueSpace (filled for ALL nodes) + AssimpAppNode* appParent; ///< Parent node + MatrixF mNodeTransform; ///< Scene node transform converted to TorqueSpace (filled for ALL nodes) - bool mInvertMeshes; ///< True if this node's coordinate space is inverted (left handed) - F32 mLastTransformTime; ///< Time of the last transform lookup (getTransform) - MatrixF mLastTransform; ///< Last transform lookup (getTransform) (Only Non-Dummy Nodes) - bool mDefaultTransformValid; ///< Flag indicating whether the defaultNodeTransform is valid - MatrixF mDefaultNodeTransform; ///< Transform at DefaultTime (Only Non-Dummy Nodes) + bool mInvertMeshes; ///< True if this node's coordinate space is inverted (left handed) + F32 mLastTransformTime; ///< Time of the last transform lookup (getTransform) + MatrixF mLastTransform; ///< Last transform lookup (getTransform) (Only Non-Dummy Nodes) + bool mDefaultTransformValid; ///< Flag indicating whether the defaultNodeTransform is valid + MatrixF mDefaultNodeTransform; ///< Transform at DefaultTime (Only Non-Dummy Nodes) public: - AssimpAppNode(const struct aiScene* scene, const struct aiNode* node, AssimpAppNode* parent = 0); + AssimpAppNode(const aiScene* scene, const aiNode* node, AssimpAppNode* parentNode = nullptr); virtual ~AssimpAppNode() { // @@ -115,6 +114,9 @@ public: static void assimpToTorqueMat(const aiMatrix4x4& inAssimpMat, MatrixF& outMat); static void convertMat(MatrixF& outMat); static aiNode* findChildNodeByName(const char* nodeName, aiNode* rootNode); + + void AssimpAppNode::addChild(AssimpAppNode* child); + void AssimpAppNode::addMesh(AssimpAppMesh* child); }; #endif // _ASSIMP_APPNODE_H_ diff --git a/Engine/source/ts/assimp/assimpShapeLoader.cpp b/Engine/source/ts/assimp/assimpShapeLoader.cpp index 71d896124..4a2ecc9f0 100644 --- a/Engine/source/ts/assimp/assimpShapeLoader.cpp +++ b/Engine/source/ts/assimp/assimpShapeLoader.cpp @@ -182,7 +182,7 @@ void AssimpShapeLoader::enumerateScene() Con::printf("[ASSIMP] Attempting to load file: %s", shapePath.getFullPath().c_str()); // Define post-processing steps - unsigned int ppsteps = aiProcess_Triangulate | aiProcess_ConvertToLeftHanded & ~aiProcess_FlipWindingOrder; + unsigned int ppsteps = aiProcess_Triangulate | aiProcess_ConvertToLeftHanded & ~aiProcess_MakeLeftHanded; const auto& options = ColladaUtils::getOptions(); if (options.reverseWindingOrder) ppsteps |= aiProcess_FlipWindingOrder; @@ -248,7 +248,13 @@ void AssimpShapeLoader::enumerateScene() } } - // Extract embedded textures + aiMatrix4x4 rotationMatrix; + rotationMatrix = aiMatrix4x4::RotationX(-AI_MATH_PI / 2, rotationMatrix); + applyTransformation(mScene->mRootNode, rotationMatrix); + + rotationMatrix = aiMatrix4x4::RotationY(AI_MATH_PI, rotationMatrix); + applyTransformation(mScene->mRootNode, rotationMatrix); + for (unsigned int i = 0; i < mScene->mNumTextures; ++i) { extractTexture(i, mScene->mTextures[i]); } @@ -268,12 +274,18 @@ void AssimpShapeLoader::enumerateScene() delete rootNode; } + processAssimpNode(mScene->mRootNode, mScene, rootNode); + // Add a bounds node if none exists if (!boundsNode) { auto* reqNode = new aiNode("bounds"); mScene->mRootNode->addChildren(1, &reqNode); + rotationMatrix = aiMatrix4x4::RotationX(-AI_MATH_PI / 2, rotationMatrix); + applyTransformation(reqNode, rotationMatrix); + /*rotationMatrix = aiMatrix4x4::RotationY(-AI_MATH_PI, rotationMatrix); + applyTransformation(reqNode, rotationMatrix);*/ - auto* appBoundsNode = new AssimpAppNode(mScene, reqNode); + auto* appBoundsNode = new AssimpAppNode(mScene, reqNode, rootNode); if (!processNode(appBoundsNode)) { delete appBoundsNode; } @@ -306,6 +318,37 @@ void AssimpShapeLoader::configureImportUnits() { } } +void AssimpShapeLoader::processAssimpNode(const aiNode* node, const aiScene* scene, AssimpAppNode* parentNode) +{ + AssimpAppNode* currNode; + if (node == scene->mRootNode) + { + currNode = parentNode; + } + else + { + currNode = new AssimpAppNode(scene, node, parentNode); + + if (parentNode) + { + parentNode->addChild(currNode); + } + + for (U32 i = 0; i < node->mNumMeshes; i++) + { + U32 meshIdx = node->mMeshes[i]; + const aiMesh* mesh = scene->mMeshes[meshIdx]; + AssimpAppMesh* curMesh = new AssimpAppMesh(mesh, currNode); + currNode->addMesh(curMesh); + } + } + // Recursively process child nodes + for (U32 i = 0; i < node->mNumChildren; i++) + { + processAssimpNode(node->mChildren[i], scene, currNode); + } +} + void AssimpShapeLoader::processAnimations() { // add all animations into 1 ambient animation. diff --git a/Engine/source/ts/assimp/assimpShapeLoader.h b/Engine/source/ts/assimp/assimpShapeLoader.h index 9d32db6f3..d5daff3af 100644 --- a/Engine/source/ts/assimp/assimpShapeLoader.h +++ b/Engine/source/ts/assimp/assimpShapeLoader.h @@ -26,6 +26,12 @@ #ifndef _TSSHAPELOADER_H_ #include "ts/loader/tsShapeLoader.h" #endif + + +#ifndef _ASSIMP_APPNODE_H_ +#include "ts/assimp/assimpAppNode.h" +#endif + #include #include @@ -41,12 +47,14 @@ protected: Assimp::Importer mImporter; const aiScene* mScene; + //bool processNode(AppNode* node) override; bool ignoreNode(const String& name) override; bool ignoreMesh(const String& name) override; void detectDetails(); void extractTexture(U32 index, aiTexture* pTex); private: + void processAssimpNode(const aiNode* node, const aiScene* scene, AssimpAppNode* parentNode = nullptr); void addNodeToTree(S32 parentItem, aiNode* node, GuiTreeViewCtrl* tree, U32& nodeCount); void addMetaDataToTree(const aiMetadata* metaData, GuiTreeViewCtrl* tree); bool getMetabool(const char* key, bool& boolVal); diff --git a/Engine/source/ts/loader/appNode.h b/Engine/source/ts/loader/appNode.h index 5b6e707b5..65ef92e40 100644 --- a/Engine/source/ts/loader/appNode.h +++ b/Engine/source/ts/loader/appNode.h @@ -41,8 +41,8 @@ class AppNode // the reason these are tracked by AppNode is that // AppNode is responsible for deleting all it's children // and attached meshes. - virtual void buildMeshList() = 0; - virtual void buildChildList() = 0; + virtual void buildMeshList() {}; + virtual void buildChildList() {}; protected: diff --git a/Engine/source/ts/loader/tsShapeLoader.h b/Engine/source/ts/loader/tsShapeLoader.h index bcb35624a..bda7fa34b 100644 --- a/Engine/source/ts/loader/tsShapeLoader.h +++ b/Engine/source/ts/loader/tsShapeLoader.h @@ -123,7 +123,7 @@ protected: // Collect the nodes, objects and sequences for the scene virtual void enumerateScene() = 0; - bool processNode(AppNode* node); + virtual bool processNode(AppNode* node); virtual bool ignoreNode(const String& name) { return false; } virtual bool ignoreMesh(const String& name) { return false; }