瀏覽代碼

Add skeleton generation to aiScene

Kim Kulling 3 年之前
父節點
當前提交
64a6968254
共有 4 個文件被更改,包括 41 次插入3 次删除
  1. 38 0
      code/AssetLib/FBX/FBXConverter.cpp
  2. 1 0
      code/AssetLib/FBX/FBXConverter.h
  3. 1 1
      include/assimp/mesh.h
  4. 1 2
      include/assimp/scene.h

+ 38 - 0
code/AssetLib/FBX/FBXConverter.cpp

@@ -1030,6 +1030,34 @@ aiMesh *FBXConverter::SetupEmptyMesh(const Geometry &mesh, aiNode *parent) {
     return out_mesh;
     return out_mesh;
 }
 }
 
 
+static aiSkeleton *createAiSkeleton(SkeletonBoneContainer &sbc) {
+    if (sbc.MeshArray.empty() || sbc.SkeletonBoneToMeshLookup.empty()) {
+        return nullptr;
+    }
+
+    aiSkeleton *skeleton = new aiSkeleton;
+    for (auto *mesh : sbc.MeshArray) {
+        auto it = sbc.SkeletonBoneToMeshLookup.find(mesh);
+        if (it == sbc.SkeletonBoneToMeshLookup.end()) {
+            continue;
+        }
+        SkeletonBoneArray *ba = it->second;
+        if (ba == nullptr) {
+            continue;
+        }
+
+        skeleton->mNumBones = static_cast<unsigned int>(ba->size());
+        skeleton->mBones = new aiSkeletonBone*[skeleton->mNumBones];
+        size_t index = 0;
+        for (auto bone : (* ba)) {
+            skeleton->mBones[index] = bone;
+            ++index;
+        }
+    }
+
+    return skeleton;
+}
+
 unsigned int FBXConverter::ConvertMeshSingleMaterial(const MeshGeometry &mesh, const Model &model,
 unsigned int FBXConverter::ConvertMeshSingleMaterial(const MeshGeometry &mesh, const Model &model,
         const aiMatrix4x4 &absolute_transform, aiNode *parent,
         const aiMatrix4x4 &absolute_transform, aiNode *parent,
         aiNode *) {
         aiNode *) {
@@ -1155,6 +1183,10 @@ unsigned int FBXConverter::ConvertMeshSingleMaterial(const MeshGeometry &mesh, c
     } else if (doc.Settings().readWeights && mesh.DeformerSkin() != nullptr && doc.Settings().useSkeleton) {
     } else if (doc.Settings().readWeights && mesh.DeformerSkin() != nullptr && doc.Settings().useSkeleton) {
         SkeletonBoneContainer sbc;
         SkeletonBoneContainer sbc;
         ConvertWeightsToSkeleton(out_mesh, mesh, absolute_transform, parent, NO_MATERIAL_SEPARATION, nullptr, sbc);
         ConvertWeightsToSkeleton(out_mesh, mesh, absolute_transform, parent, NO_MATERIAL_SEPARATION, nullptr, sbc);
+        aiSkeleton *skeleton = createAiSkeleton(sbc);
+        if (skeleton != nullptr) {
+            mSkeletons.emplace_back(skeleton);
+        }
     }
     }
 
 
     std::vector<aiAnimMesh *> animMeshes;
     std::vector<aiAnimMesh *> animMeshes;
@@ -3657,6 +3689,12 @@ void FBXConverter::TransferDataToScene() {
 
 
         std::swap_ranges(textures.begin(), textures.end(), mSceneOut->mTextures);
         std::swap_ranges(textures.begin(), textures.end(), mSceneOut->mTextures);
     }
     }
+
+    if (!mSkeletons.empty()) {
+        mSceneOut->mSkeletons = new aiSkeleton *[mSkeletons.size()];
+        mSceneOut->mNumSkeletons = static_cast<unsigned int>(mSkeletons.size());
+        std::swap_ranges(mSkeletons.begin(), mSkeletons.end(), mSceneOut->mSkeletons);
+    }
 }
 }
 
 
 void FBXConverter::ConvertOrphanedEmbeddedTextures() {
 void FBXConverter::ConvertOrphanedEmbeddedTextures() {

+ 1 - 0
code/AssetLib/FBX/FBXConverter.h

@@ -467,6 +467,7 @@ private:
 
 
     double anim_fps;
     double anim_fps;
 
 
+    std::vector<aiSkeleton *> mSkeletons;
     aiScene* const mSceneOut;
     aiScene* const mSceneOut;
     const FBX::Document& doc;
     const FBX::Document& doc;
     bool mRemoveEmptyBones;
     bool mRemoveEmptyBones;

+ 1 - 1
include/assimp/mesh.h

@@ -1018,7 +1018,7 @@ struct aiSkeleton {
     /**
     /**
      *
      *
      */
      */
-    C_STRUCT aiSkeletonBone *mBones;
+    C_STRUCT aiSkeletonBone **mBones;
 
 
 #ifdef __cplusplus
 #ifdef __cplusplus
     /**
     /**

+ 1 - 2
include/assimp/scene.h

@@ -79,8 +79,7 @@ extern "C" {
  * the imported scene does consist of only a single root node without children.
  * the imported scene does consist of only a single root node without children.
  */
  */
 // -------------------------------------------------------------------------------
 // -------------------------------------------------------------------------------
-struct ASSIMP_API aiNode
-{
+struct ASSIMP_API aiNode {
     /** The name of the node.
     /** The name of the node.
      *
      *
      * The name might be empty (length of zero) but all nodes which
      * The name might be empty (length of zero) but all nodes which