소스 검색

Merge pull request #3969 from Promit/promit/gltf-extras

Added support for custom properties ("extras") in glTF2 importer
Kim Kulling 4 년 전
부모
커밋
1d33131e90
3개의 변경된 파일27개의 추가작업 그리고 5개의 파일을 삭제
  1. 2 0
      code/AssetLib/glTF2/glTF2Asset.h
  2. 10 3
      code/AssetLib/glTF2/glTF2Asset.inl
  3. 15 2
      code/AssetLib/glTF2/glTF2Importer.cpp

+ 2 - 0
code/AssetLib/glTF2/glTF2Asset.h

@@ -411,6 +411,7 @@ struct Object {
     std::string name; //!< The user-defined name of this object
     std::string name; //!< The user-defined name of this object
 
 
     CustomExtension customExtensions;
     CustomExtension customExtensions;
+    CustomExtension extras;
 
 
     //! Objects marked as special are not exported (used to emulate the binary body buffer)
     //! Objects marked as special are not exported (used to emulate the binary body buffer)
     virtual bool IsSpecial() const { return false; }
     virtual bool IsSpecial() const { return false; }
@@ -428,6 +429,7 @@ struct Object {
     inline Value *FindExtension(Value &val, const char *extensionId);
     inline Value *FindExtension(Value &val, const char *extensionId);
 
 
     inline void ReadExtensions(Value &val);
     inline void ReadExtensions(Value &val);
+    inline void ReadExtras(Value &val);
 };
 };
 
 
 //
 //

+ 10 - 3
code/AssetLib/glTF2/glTF2Asset.inl

@@ -372,6 +372,12 @@ inline void Object::ReadExtensions(Value &val) {
     }
     }
 }
 }
 
 
+inline void Object::ReadExtras(Value &val) {
+    if (Value *curExtras = FindObject(val, "extras")) {
+        this->extras = glTF2::ReadExtensions("extras", *curExtras);
+    }
+}
+
 #ifdef ASSIMP_ENABLE_DRACO
 #ifdef ASSIMP_ENABLE_DRACO
 
 
 template <typename T>
 template <typename T>
@@ -612,6 +618,7 @@ Ref<T> LazyDict<T>::Retrieve(unsigned int i) {
     ReadMember(obj, "name", inst->name);
     ReadMember(obj, "name", inst->name);
     inst->Read(obj, mAsset);
     inst->Read(obj, mAsset);
     inst->ReadExtensions(obj);
     inst->ReadExtensions(obj);
+    inst->ReadExtras(obj);
 
 
     Ref<T> result = Add(inst.release());
     Ref<T> result = Add(inst.release());
     mRecursiveReferenceCheck.erase(i);
     mRecursiveReferenceCheck.erase(i);
@@ -1661,9 +1668,9 @@ inline void Mesh::Read(Value &pJSON_Object, Asset &pAsset_Root) {
         }
         }
     }
     }
 
 
-    Value *extras = FindObject(pJSON_Object, "extras");
-    if (nullptr != extras) {
-        if (Value *curTargetNames = FindArray(*extras, "targetNames")) {
+    Value *curExtras = FindObject(pJSON_Object, "extras");
+    if (nullptr != curExtras) {
+        if (Value *curTargetNames = FindArray(*curExtras, "targetNames")) {
             this->targetNames.resize(curTargetNames->Size());
             this->targetNames.resize(curTargetNames->Size());
             for (unsigned int i = 0; i < curTargetNames->Size(); ++i) {
             for (unsigned int i = 0; i < curTargetNames->Size(); ++i) {
                 Value &targetNameValue = (*curTargetNames)[i];
                 Value &targetNameValue = (*curTargetNames)[i];

+ 15 - 2
code/AssetLib/glTF2/glTF2Importer.cpp

@@ -999,6 +999,14 @@ void ParseExtensions(aiMetadata *metadata, const CustomExtension &extension) {
     }
     }
 }
 }
 
 
+void ParseExtras(aiMetadata *metadata, const CustomExtension &extension) {
+    if (extension.mValues.isPresent) {
+        for (size_t i = 0; i < extension.mValues.value.size(); ++i) {
+            ParseExtensions(metadata, extension.mValues.value[i]);
+        }
+    }
+}
+
 aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector<unsigned int> &meshOffsets, glTF2::Ref<glTF2::Node> &ptr) {
 aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector<unsigned int> &meshOffsets, glTF2::Ref<glTF2::Node> &ptr) {
     Node &node = *ptr;
     Node &node = *ptr;
 
 
@@ -1017,9 +1025,14 @@ aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector<unsigned int> &
             }
             }
         }
         }
 
 
-        if (node.customExtensions) {
+        if (node.customExtensions || node.extras) {
             ainode->mMetaData = new aiMetadata;
             ainode->mMetaData = new aiMetadata;
-            ParseExtensions(ainode->mMetaData, node.customExtensions);
+            if (node.customExtensions) {
+                ParseExtensions(ainode->mMetaData, node.customExtensions);
+            }
+            if (node.extras) {
+                ParseExtras(ainode->mMetaData, node.extras);
+            }
         }
         }
 
 
         GetNodeTransform(ainode->mTransformation, node);
         GetNodeTransform(ainode->mTransformation, node);