Bläddra i källkod

Changes to GLTF2 materials

pbrMetallicRoughness and pbrSpecularGlossiness as structs; persist textureinfo properties from start to finish; persist pbrSpecularGlossiness (via extensionsUsed) usage from start to finish
Daniel Hritzkiv 8 år sedan
förälder
incheckning
562920fbb8
6 ändrade filer med 199 tillägg och 91 borttagningar
  1. 19 8
      code/glTF2Asset.h
  2. 19 17
      code/glTF2Asset.inl
  3. 33 29
      code/glTF2AssetWriter.inl
  4. 95 14
      code/glTF2Exporter.cpp
  5. 8 0
      code/glTF2Exporter.h
  6. 25 23
      code/glTF2Importer.cpp

+ 19 - 8
code/glTF2Asset.h

@@ -692,15 +692,30 @@ namespace glTF2
         vec4 color;
         vec4 color;
     };*/
     };*/
 
 
-    //! The material appearance of a primitive.
-    struct Material : public Object
+    struct PbrMetallicRoughness
     {
     {
-        //PBR metallic roughness properties
         vec4 baseColorFactor;
         vec4 baseColorFactor;
         TextureInfo baseColorTexture;
         TextureInfo baseColorTexture;
         TextureInfo metallicRoughnessTexture;
         TextureInfo metallicRoughnessTexture;
         float metallicFactor;
         float metallicFactor;
         float roughnessFactor;
         float roughnessFactor;
+    };
+
+    struct PbrSpecularGlossiness
+    {
+        bool on = false;
+        vec4 diffuseFactor;
+        vec3 specularFactor;
+        float glossinessFactor;
+        TextureInfo diffuseTexture;
+        TextureInfo specularGlossinessTexture;
+    };
+
+    //! The material appearance of a primitive.
+    struct Material : public Object
+    {
+        //PBR metallic roughness properties
+        PbrMetallicRoughness pbrMetallicRoughness;
 
 
         //other basic material properties
         //other basic material properties
         NormalTextureInfo normalTexture;
         NormalTextureInfo normalTexture;
@@ -712,11 +727,7 @@ namespace glTF2
         bool doubleSided;
         bool doubleSided;
 
 
         //extension: KHR_materials_pbrSpecularGlossiness
         //extension: KHR_materials_pbrSpecularGlossiness
-        vec4 diffuseFactor;
-        vec3 specularFactor;
-        float glossinessFactor;
-        TextureInfo diffuseTexture;
-        TextureInfo specularGlossinessTexture;
+        PbrSpecularGlossiness pbrSpecularGlossiness;
 
 
         Material() { SetDefaults(); }
         Material() { SetDefaults(); }
         void Read(Value& obj, Asset& r);
         void Read(Value& obj, Asset& r);

+ 19 - 17
code/glTF2Asset.inl

@@ -758,11 +758,11 @@ inline void Material::Read(Value& material, Asset& r)
     SetDefaults();
     SetDefaults();
 
 
     if (Value* pbrMetallicRoughness = FindObject(material, "pbrMetallicRoughness")) {
     if (Value* pbrMetallicRoughness = FindObject(material, "pbrMetallicRoughness")) {
-        ReadMember(*pbrMetallicRoughness, "baseColorFactor", this->baseColorFactor);
-        ReadTextureProperty(r, *pbrMetallicRoughness, "baseColorTexture", this->baseColorTexture);
-        ReadTextureProperty(r, *pbrMetallicRoughness, "metallicRoughnessTexture", this->metallicRoughnessTexture);
-        ReadMember(*pbrMetallicRoughness, "metallicFactor", this->metallicFactor);
-        ReadMember(*pbrMetallicRoughness, "roughnessFactor", this->roughnessFactor);
+        ReadMember(*pbrMetallicRoughness, "baseColorFactor", this->pbrMetallicRoughness.baseColorFactor);
+        ReadTextureProperty(r, *pbrMetallicRoughness, "baseColorTexture", this->pbrMetallicRoughness.baseColorTexture);
+        ReadTextureProperty(r, *pbrMetallicRoughness, "metallicRoughnessTexture", this->pbrMetallicRoughness.metallicRoughnessTexture);
+        ReadMember(*pbrMetallicRoughness, "metallicFactor", this->pbrMetallicRoughness.metallicFactor);
+        ReadMember(*pbrMetallicRoughness, "roughnessFactor", this->pbrMetallicRoughness.roughnessFactor);
     }
     }
 
 
     ReadTextureProperty(r, material, "normalTexture", this->normalTexture);
     ReadTextureProperty(r, material, "normalTexture", this->normalTexture);
@@ -777,11 +777,13 @@ inline void Material::Read(Value& material, Asset& r)
     if (Value* extensions = FindObject(material, "extensions")) {
     if (Value* extensions = FindObject(material, "extensions")) {
         if (r.extensionsUsed.KHR_materials_pbrSpecularGlossiness) {
         if (r.extensionsUsed.KHR_materials_pbrSpecularGlossiness) {
             if (Value* pbrSpecularGlossiness = FindObject(*extensions, "KHR_materials_pbrSpecularGlossiness")) {
             if (Value* pbrSpecularGlossiness = FindObject(*extensions, "KHR_materials_pbrSpecularGlossiness")) {
-                ReadMember(*pbrSpecularGlossiness, "diffuseFactor", this->diffuseFactor);
-                ReadTextureProperty(r, *pbrSpecularGlossiness, "diffuseTexture", this->diffuseTexture);
-                ReadTextureProperty(r, *pbrSpecularGlossiness, "specularGlossinessTexture", this->specularGlossinessTexture);
-                ReadMember(*pbrSpecularGlossiness, "specularFactor", this->specularFactor);
-                ReadMember(*pbrSpecularGlossiness, "glossinessFactor", this->glossinessFactor);
+                this->pbrSpecularGlossiness.on = true;
+
+                ReadMember(*pbrSpecularGlossiness, "diffuseFactor", this->pbrSpecularGlossiness.diffuseFactor);
+                ReadTextureProperty(r, *pbrSpecularGlossiness, "diffuseTexture", this->pbrSpecularGlossiness.diffuseTexture);
+                ReadTextureProperty(r, *pbrSpecularGlossiness, "specularGlossinessTexture", this->pbrSpecularGlossiness.specularGlossinessTexture);
+                ReadMember(*pbrSpecularGlossiness, "specularFactor", this->pbrSpecularGlossiness.specularFactor);
+                ReadMember(*pbrSpecularGlossiness, "glossinessFactor", this->pbrSpecularGlossiness.glossinessFactor);
             }
             }
         }
         }
     }
     }
@@ -798,19 +800,19 @@ namespace {
 inline void Material::SetDefaults()
 inline void Material::SetDefaults()
 {
 {
     //pbr materials
     //pbr materials
-    SetVector(baseColorFactor, 1, 1, 1, 1);
-    SetVector(emissiveFactor, 0, 0, 0);
-    metallicFactor = 1.0;
-    roughnessFactor = 1.0;
+    SetVector(pbrMetallicRoughness.baseColorFactor, 1, 1, 1, 1);
+    pbrMetallicRoughness.metallicFactor = 1.0;
+    pbrMetallicRoughness.roughnessFactor = 1.0;
 
 
+    SetVector(emissiveFactor, 0, 0, 0);
     alphaMode = "OPAQUE";
     alphaMode = "OPAQUE";
     alphaCutoff = 0.5;
     alphaCutoff = 0.5;
     doubleSided = false;
     doubleSided = false;
 
 
     //pbrSpecularGlossiness properties
     //pbrSpecularGlossiness properties
-    SetVector(diffuseFactor, 1, 1, 1, 1);
-    SetVector(specularFactor, 1, 1, 1);
-    glossinessFactor = 1.0;
+    SetVector(pbrSpecularGlossiness.diffuseFactor, 1, 1, 1, 1);
+    SetVector(pbrSpecularGlossiness.specularFactor, 1, 1, 1);
+    pbrSpecularGlossiness.glossinessFactor = 1.0;
 }
 }
 
 
 namespace {
 namespace {

+ 33 - 29
code/glTF2AssetWriter.inl

@@ -287,19 +287,19 @@ namespace glTF2 {
         Value pbrMetallicRoughness;
         Value pbrMetallicRoughness;
         pbrMetallicRoughness.SetObject();
         pbrMetallicRoughness.SetObject();
         {
         {
-            WriteTex(pbrMetallicRoughness, m.baseColorTexture, "baseColorTexture", w.mAl);
-            WriteTex(pbrMetallicRoughness, m.metallicRoughnessTexture, "metallicRoughnessTexture", w.mAl);
+            WriteTex(pbrMetallicRoughness, m.pbrMetallicRoughness.baseColorTexture, "baseColorTexture", w.mAl);
+            WriteTex(pbrMetallicRoughness, m.pbrMetallicRoughness.metallicRoughnessTexture, "metallicRoughnessTexture", w.mAl);
 
 
             //@TODO: define this as a constant?
             //@TODO: define this as a constant?
             vec4 defaultEmissiveFactor = {1, 1, 1, 1};
             vec4 defaultEmissiveFactor = {1, 1, 1, 1};
-            WriteVec(pbrMetallicRoughness, m.baseColorFactor, "baseColorFactor", defaultEmissiveFactor, w.mAl);
+            WriteVec(pbrMetallicRoughness, m.pbrMetallicRoughness.baseColorFactor, "baseColorFactor", defaultEmissiveFactor, w.mAl);
 
 
-            if (m.metallicFactor != 1) {
-                WriteFloat(pbrMetallicRoughness, m.metallicFactor, "metallicFactor", w.mAl);
+            if (m.pbrMetallicRoughness.metallicFactor != 1) {
+                WriteFloat(pbrMetallicRoughness, m.pbrMetallicRoughness.metallicFactor, "metallicFactor", w.mAl);
             }
             }
 
 
-            if (m.roughnessFactor != 1) {
-                WriteFloat(pbrMetallicRoughness, m.roughnessFactor, "roughnessFactor", w.mAl);
+            if (m.pbrMetallicRoughness.roughnessFactor != 1) {
+                WriteFloat(pbrMetallicRoughness, m.pbrMetallicRoughness.roughnessFactor, "roughnessFactor", w.mAl);
             }
             }
         }
         }
 
 
@@ -327,34 +327,36 @@ namespace glTF2 {
             obj.AddMember("doubleSided", m.doubleSided, w.mAl);
             obj.AddMember("doubleSided", m.doubleSided, w.mAl);
         }
         }
 
 
-        Value pbrSpecularGlossiness;
-        pbrSpecularGlossiness.SetObject();
-        {
-            //pbrSpecularGlossiness
+        Value exts;
+        exts.SetObject();
 
 
-            vec4 defaultDiffuseFactor = {1, 1, 1, 1};
-            WriteVec(pbrSpecularGlossiness, m.diffuseFactor, "diffuseFactor", defaultDiffuseFactor, w.mAl);
+        if (m.pbrSpecularGlossiness.on) {
+            Value pbrSpecularGlossiness;
+            pbrSpecularGlossiness.SetObject();
+            {
+                //pbrSpecularGlossiness
 
 
-            vec3 defaultSpecularFactor = {1, 1, 1};
-            WriteVec(pbrSpecularGlossiness, m.specularFactor, "specularFactor", defaultSpecularFactor, w.mAl);
+                vec4 defaultDiffuseFactor = {1, 1, 1, 1};
+                WriteVec(pbrSpecularGlossiness, m.pbrSpecularGlossiness.diffuseFactor, "diffuseFactor", defaultDiffuseFactor, w.mAl);
 
 
-            if (m.glossinessFactor != 1) {
-                WriteFloat(obj, m.glossinessFactor, "glossinessFactor", w.mAl);
-            }
+                vec3 defaultSpecularFactor = {1, 1, 1};
+                WriteVec(pbrSpecularGlossiness, m.pbrSpecularGlossiness.specularFactor, "specularFactor", defaultSpecularFactor, w.mAl);
 
 
-            WriteTex(obj, m.diffuseTexture, "diffuseTexture", w.mAl);
-            WriteTex(obj, m.specularGlossinessTexture, "specularGlossinessTexture", w.mAl);
-        }
+                if (m.pbrSpecularGlossiness.glossinessFactor != 1) {
+                    WriteFloat(obj, m.pbrSpecularGlossiness.glossinessFactor, "glossinessFactor", w.mAl);
+                }
 
 
-        Value ext;
-        ext.SetObject();
+                WriteTex(obj, m.pbrSpecularGlossiness.diffuseTexture, "diffuseTexture", w.mAl);
+                WriteTex(obj, m.pbrSpecularGlossiness.specularGlossinessTexture, "specularGlossinessTexture", w.mAl);
+            }
 
 
-        if (!pbrSpecularGlossiness.ObjectEmpty()) {
-            ext.AddMember("KHR_materials_pbrSpecularGlossiness", pbrSpecularGlossiness, w.mAl);
+            if (!pbrSpecularGlossiness.ObjectEmpty()) {
+                exts.AddMember("KHR_materials_pbrSpecularGlossiness", pbrSpecularGlossiness, w.mAl);
+            }
         }
         }
 
 
-        if (!ext.ObjectEmpty()) {
-            obj.AddMember("extensions", ext, w.mAl);
+        if (!exts.ObjectEmpty()) {
+            obj.AddMember("extensions", exts, w.mAl);
         }
         }
     }
     }
 
 
@@ -719,8 +721,10 @@ namespace glTF2 {
             //if (false)
             //if (false)
             //    exts.PushBack(StringRef("KHR_binary_glTF"), mAl);
             //    exts.PushBack(StringRef("KHR_binary_glTF"), mAl);
 
 
-            // This is used to export common materials with GLTF 2.
-            //exts.PushBack(StringRef("KHR_materials_pbrSpecularGlossiness"), mAl);
+            // This is used to export pbrSpecularGlossiness materials with GLTF 2.
+            if (this->mAsset.extensionsUsed.KHR_materials_pbrSpecularGlossiness) {
+                exts.PushBack(StringRef("KHR_materials_pbrSpecularGlossiness"), mAl);
+            }
         }
         }
 
 
         if (!exts.Empty())
         if (!exts.Empty())

+ 95 - 14
code/glTF2Exporter.cpp

@@ -112,9 +112,9 @@ glTF2Exporter::glTF2Exporter(const char* filename, IOSystem* pIOSystem, const ai
 
 
     ExportMetadata();
     ExportMetadata();
 
 
-    //for (unsigned int i = 0; i < pScene->mNumCameras; ++i) {}
-
-    //for (unsigned int i = 0; i < pScene->mNumLights; ++i) {}
+    if (mScene->mRootNode) {
+        ExportExtensions(mScene->mRootNode);
+    }
 
 
     ExportMaterials();
     ExportMaterials();
 
 
@@ -124,8 +124,6 @@ glTF2Exporter::glTF2Exporter(const char* filename, IOSystem* pIOSystem, const ai
 
 
     ExportMeshes();
     ExportMeshes();
 
 
-    //for (unsigned int i = 0; i < pScene->mNumTextures; ++i) {}
-
     ExportScene();
     ExportScene();
 
 
     //ExportAnimations();
     //ExportAnimations();
@@ -280,10 +278,26 @@ void glTF2Exporter::GetTexSampler(const aiMaterial* mat, Ref<Texture> texture)
     texture->sampler->minFilter = SamplerMinFilter_Linear_Mipmap_Linear;
     texture->sampler->minFilter = SamplerMinFilter_Linear_Mipmap_Linear;
 }
 }
 
 
+void glTF2Exporter::GetMatTexProp(const aiMaterial* mat, unsigned int& prop, const char* propName, aiTextureType tt, unsigned int slot)
+{
+    const char* key = (std::string(_AI_MATKEY_TEXTURE_BASE) + "." + propName).c_str();
+
+    mat->Get(key, tt, slot, prop);
+}
+
+void glTF2Exporter::GetMatTexProp(const aiMaterial* mat, float& prop, const char* propName, aiTextureType tt, unsigned int slot)
+{
+    const char* key = (std::string(_AI_MATKEY_TEXTURE_BASE) + "." + propName).c_str();
+
+    mat->Get(key, tt, slot, prop);
+}
+
 void glTF2Exporter::GetMatTex(const aiMaterial* mat, Ref<Texture>& texture, aiTextureType tt, unsigned int slot = 0)
 void glTF2Exporter::GetMatTex(const aiMaterial* mat, Ref<Texture>& texture, aiTextureType tt, unsigned int slot = 0)
 {
 {
-    aiString tex;
+
     if (mat->GetTextureCount(tt) > 0) {
     if (mat->GetTextureCount(tt) > 0) {
+        aiString tex;
+
         if (mat->Get(AI_MATKEY_TEXTURE(tt, slot), tex) == AI_SUCCESS) {
         if (mat->Get(AI_MATKEY_TEXTURE(tt, slot), tex) == AI_SUCCESS) {
             std::string path = tex.C_Str();
             std::string path = tex.C_Str();
 
 
@@ -326,6 +340,41 @@ void glTF2Exporter::GetMatTex(const aiMaterial* mat, Ref<Texture>& texture, aiTe
     }
     }
 }
 }
 
 
+void glTF2Exporter::GetMatTex(const aiMaterial* mat, TextureInfo& prop, aiTextureType tt, unsigned int slot = 0)
+{
+    Ref<Texture>& texture = prop.texture;
+
+    GetMatTex(mat, texture, tt, slot);
+
+    if (texture) {
+        GetMatTexProp(mat, prop.texCoord, "texCoord", tt, slot);
+    }
+}
+
+void glTF2Exporter::GetMatTex(const aiMaterial* mat, NormalTextureInfo& prop, aiTextureType tt, unsigned int slot = 0)
+{
+    Ref<Texture>& texture = prop.texture;
+
+    GetMatTex(mat, texture, tt, slot);
+
+    if (texture) {
+        GetMatTexProp(mat, prop.texCoord, "texCoord", tt, slot);
+        GetMatTexProp(mat, prop.scale, "scale", tt, slot);
+    }
+}
+
+void glTF2Exporter::GetMatTex(const aiMaterial* mat, OcclusionTextureInfo& prop, aiTextureType tt, unsigned int slot = 0)
+{
+    Ref<Texture>& texture = prop.texture;
+
+    GetMatTex(mat, texture, tt, slot);
+
+    if (texture) {
+        GetMatTexProp(mat, prop.texCoord, "texCoord", tt, slot);
+        GetMatTexProp(mat, prop.strength, "strength", tt, slot);
+    }
+}
+
 void glTF2Exporter::GetMatColor(const aiMaterial* mat, vec4& prop, const char* propName, int type, int idx)
 void glTF2Exporter::GetMatColor(const aiMaterial* mat, vec4& prop, const char* propName, int type, int idx)
 {
 {
     aiColor4D col;
     aiColor4D col;
@@ -344,6 +393,8 @@ void glTF2Exporter::GetMatColor(const aiMaterial* mat, vec3& prop, const char* p
 
 
 void glTF2Exporter::ExportMaterials()
 void glTF2Exporter::ExportMaterials()
 {
 {
+    bool& KHR_materials_pbrSpecularGlossiness = mAsset->extensionsUsed.KHR_materials_pbrSpecularGlossiness;
+
     aiString aiName;
     aiString aiName;
     for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) {
     for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) {
         const aiMaterial* mat = mScene->mMaterials[i];
         const aiMaterial* mat = mScene->mMaterials[i];
@@ -356,20 +407,36 @@ void glTF2Exporter::ExportMaterials()
 
 
         Ref<Material> m = mAsset->materials.Create(name);
         Ref<Material> m = mAsset->materials.Create(name);
 
 
-        GetMatTex(mat, m->baseColorTexture.texture, aiTextureType_DIFFUSE);
-        GetMatTex(mat, m->metallicRoughnessTexture.texture, aiTextureType_UNKNOWN, 0);//get unknown slot
-        GetMatTex(mat, m->emissiveTexture.texture, aiTextureType_EMISSIVE);
-        GetMatTex(mat, m->normalTexture.texture, aiTextureType_NORMALS);
-        GetMatTex(mat, m->occlusionTexture.texture, aiTextureType_LIGHTMAP);
+        GetMatTex(mat, m->pbrMetallicRoughness.baseColorTexture, aiTextureType_DIFFUSE);
+        GetMatTex(mat, m->pbrMetallicRoughness.metallicRoughnessTexture, aiTextureType_UNKNOWN, 0);//get unknown slot
+        GetMatColor(mat, m->pbrMetallicRoughness.baseColorFactor, AI_MATKEY_COLOR_DIFFUSE);
 
 
-        GetMatColor(mat, m->baseColorFactor, AI_MATKEY_COLOR_DIFFUSE);
+        GetMatTex(mat, m->normalTexture, aiTextureType_NORMALS);
+        GetMatTex(mat, m->occlusionTexture, aiTextureType_LIGHTMAP);
+        GetMatTex(mat, m->emissiveTexture, aiTextureType_EMISSIVE);
         GetMatColor(mat, m->emissiveFactor, AI_MATKEY_COLOR_EMISSIVE);
         GetMatColor(mat, m->emissiveFactor, AI_MATKEY_COLOR_EMISSIVE);
 
 
         mat->Get(AI_MATKEY_TWOSIDED, m->doubleSided);
         mat->Get(AI_MATKEY_TWOSIDED, m->doubleSided);
         mat->Get("$mat.gltf.alphaCutoff", 0, 0, m->alphaCutoff);
         mat->Get("$mat.gltf.alphaCutoff", 0, 0, m->alphaCutoff);
-        mat->Get("$mat.gltf.metallicFactor", 0, 0, m->metallicFactor);
-        mat->Get("$mat.gltf.roughnessFactor", 0, 0, m->roughnessFactor);
+        mat->Get("$mat.gltf.metallicFactor", 0, 0, m->pbrMetallicRoughness.metallicFactor);
+        mat->Get("$mat.gltf.roughnessFactor", 0, 0, m->pbrMetallicRoughness.roughnessFactor);
         mat->Get("$mat.gltf.alphaMode", 0, 0, m->alphaMode);
         mat->Get("$mat.gltf.alphaMode", 0, 0, m->alphaMode);
+
+        bool hasPbrSpecularGlossiness;
+        mat->Get("$mat.gltf.pbrSpecularGlossiness.on", 0, 0, hasPbrSpecularGlossiness);
+
+        if (hasPbrSpecularGlossiness) {
+
+            if (!KHR_materials_pbrSpecularGlossiness) {
+                KHR_materials_pbrSpecularGlossiness = true;
+            }
+
+            GetMatColor(mat, m->pbrSpecularGlossiness.diffuseFactor, "$clr.diffuse", 0, 1);
+            GetMatColor(mat, m->pbrSpecularGlossiness.specularFactor, "$clr.specular", 0, 1);
+            mat->Get("$mat.gltf.glossinessFactor", 0, 0, m->pbrSpecularGlossiness.glossinessFactor);
+            GetMatTex(mat, m->pbrSpecularGlossiness.diffuseTexture, aiTextureType_DIFFUSE, 1);
+            GetMatTex(mat, m->pbrSpecularGlossiness.specularGlossinessTexture, aiTextureType_UNKNOWN, 1);
+        }
     }
     }
 }
 }
 
 
@@ -862,6 +929,20 @@ void glTF2Exporter::ExportMetadata()
     asset.generator = buffer;
     asset.generator = buffer;
 }
 }
 
 
+void glTF2Exporter::ExportExtensions(const aiNode* n)
+{
+    aiMetadata* mMetaData = n->mMetaData;
+
+    if (mMetaData != nullptr) {
+        bool pbrSpecularGlossiness;
+
+        if (mMetaData->Get("extensionsUsed.pbrSpecularGlossiness", pbrSpecularGlossiness)) {
+            mAsset->extensionsUsed.KHR_materials_pbrSpecularGlossiness = pbrSpecularGlossiness;
+        }
+    }
+
+}
+
 inline void ExtractAnimationData(Asset& mAsset, std::string& animId, Ref<Animation>& animRef, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond)
 inline void ExtractAnimationData(Asset& mAsset, std::string& animId, Ref<Animation>& animRef, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond)
 {
 {
     // Loop over the data and check to see if it exactly matches an existing buffer.
     // Loop over the data and check to see if it exactly matches an existing buffer.

+ 8 - 0
code/glTF2Exporter.h

@@ -67,6 +67,8 @@ namespace glTF2
     class Asset;
     class Asset;
     struct TexProperty;
     struct TexProperty;
     struct TextureInfo;
     struct TextureInfo;
+    struct NormalTextureInfo;
+    struct OcclusionTextureInfo;
     struct Node;
     struct Node;
     struct Texture;
     struct Texture;
 
 
@@ -107,10 +109,16 @@ namespace Assimp
         void WriteBinaryData(IOStream* outfile, std::size_t sceneLength);
         void WriteBinaryData(IOStream* outfile, std::size_t sceneLength);
 
 
         void GetTexSampler(const aiMaterial* mat, glTF2::Ref<glTF2::Texture> texture);
         void GetTexSampler(const aiMaterial* mat, glTF2::Ref<glTF2::Texture> texture);
+        void GetMatTexProp(const aiMaterial* mat, unsigned int& prop, const char* propName, aiTextureType tt, unsigned int idx);
+        void GetMatTexProp(const aiMaterial* mat, float& prop, const char* propName, aiTextureType tt, unsigned int idx);
         void GetMatTex(const aiMaterial* mat, glTF2::Ref<glTF2::Texture>& texture, aiTextureType tt, unsigned int slot);
         void GetMatTex(const aiMaterial* mat, glTF2::Ref<glTF2::Texture>& texture, aiTextureType tt, unsigned int slot);
+        void GetMatTex(const aiMaterial* mat, glTF2::TextureInfo& prop, aiTextureType tt, unsigned int slot);
+        void GetMatTex(const aiMaterial* mat, glTF2::NormalTextureInfo& prop, aiTextureType tt, unsigned int slot);
+        void GetMatTex(const aiMaterial* mat, glTF2::OcclusionTextureInfo& prop, aiTextureType tt, unsigned int slot);
         void GetMatColor(const aiMaterial* mat, glTF2::vec4& prop, const char* propName, int type, int idx);
         void GetMatColor(const aiMaterial* mat, glTF2::vec4& prop, const char* propName, int type, int idx);
         void GetMatColor(const aiMaterial* mat, glTF2::vec3& prop, const char* propName, int type, int idx);
         void GetMatColor(const aiMaterial* mat, glTF2::vec3& prop, const char* propName, int type, int idx);
         void ExportMetadata();
         void ExportMetadata();
+        void ExportExtensions(const aiNode* n);
         void ExportMaterials();
         void ExportMaterials();
         void ExportMeshes();
         void ExportMeshes();
         unsigned int ExportNodeHierarchy(const aiNode* n);
         unsigned int ExportNodeHierarchy(const aiNode* n);

+ 25 - 23
code/glTF2Importer.cpp

@@ -183,7 +183,7 @@ inline void SetMaterialColorProperty(Asset& r, vec3& prop, aiMaterial* mat, cons
     return SetMaterialColorProperty(r, prop4, mat, pKey, type, idx);
     return SetMaterialColorProperty(r, prop4, mat, pKey, type, idx);
 }
 }
 
 
-inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset& r, glTF2::TextureInfo prop, aiMaterial* mat, aiTextureType texType, int texSlot)
+inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset& r, glTF2::TextureInfo prop, aiMaterial* mat, aiTextureType texType, unsigned int texSlot = 0)
 {
 {
     if (prop.texture && prop.texture->source) {
     if (prop.texture && prop.texture->source) {
         aiString uri(prop.texture->source->uri);
         aiString uri(prop.texture->source->uri);
@@ -195,19 +195,11 @@ inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset&
             uri.length = 1 + ASSIMP_itoa10(uri.data + 1, MAXLEN - 1, texIdx);
             uri.length = 1 + ASSIMP_itoa10(uri.data + 1, MAXLEN - 1, texIdx);
         }
         }
 
 
-        if (texSlot < 0) {
-            mat->AddProperty(&uri, _AI_MATKEY_TEXTURE_BASE, texType, 0);
-        }
-        else {
-            mat->AddProperty(&uri, AI_MATKEY_TEXTURE(texType,
-                texSlot));
-        }
-    }
-}
+        mat->AddProperty(&uri, AI_MATKEY_TEXTURE(texType, texSlot));
 
 
-inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset& r, glTF2::TextureInfo prop, aiMaterial* mat, aiTextureType texType)
-{
-    return SetMaterialTextureProperty(embeddedTexIdxs, r, prop, mat, texType, -1);
+        const char *texCoordName = (std::string(_AI_MATKEY_TEXTURE_BASE) + ".texCoord").c_str();
+        mat->AddProperty(&prop.texCoord, 1, texCoordName, texType, texSlot);
+    }
 }
 }
 
 
 void glTF2Importer::ImportMaterials(glTF2::Asset& r)
 void glTF2Importer::ImportMaterials(glTF2::Asset& r)
@@ -223,11 +215,11 @@ void glTF2Importer::ImportMaterials(glTF2::Asset& r)
         aiString str(mat.id);
         aiString str(mat.id);
         aimat->AddProperty(&str, AI_MATKEY_NAME);
         aimat->AddProperty(&str, AI_MATKEY_NAME);
 
 
-        SetMaterialColorProperty(r, mat.baseColorFactor, aimat, AI_MATKEY_COLOR_DIFFUSE);
-        SetMaterialTextureProperty(embeddedTexIdxs, r, mat.baseColorTexture, aimat, aiTextureType_DIFFUSE);
-        SetMaterialTextureProperty(embeddedTexIdxs, r, mat.metallicRoughnessTexture, aimat, aiTextureType_UNKNOWN, 0);
-        aimat->AddProperty(&mat.metallicFactor, 1, "$mat.gltf.metallicFactor");
-        aimat->AddProperty(&mat.roughnessFactor, 1, "$mat.gltf.roughnessFactor");
+        SetMaterialColorProperty(r, mat.pbrMetallicRoughness.baseColorFactor, aimat, AI_MATKEY_COLOR_DIFFUSE);
+        SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrMetallicRoughness.baseColorTexture, aimat, aiTextureType_DIFFUSE);
+        SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrMetallicRoughness.metallicRoughnessTexture, aimat, aiTextureType_UNKNOWN);
+        aimat->AddProperty(&mat.pbrMetallicRoughness.metallicFactor, 1, "$mat.gltf.pbrMetallicRoughness.metallicFactor");
+        aimat->AddProperty(&mat.pbrMetallicRoughness.roughnessFactor, 1, "$mat.gltf.pbrMetallicRoughness.roughnessFactor");
 
 
         SetMaterialTextureProperty(embeddedTexIdxs, r, mat.normalTexture, aimat, aiTextureType_NORMALS);
         SetMaterialTextureProperty(embeddedTexIdxs, r, mat.normalTexture, aimat, aiTextureType_NORMALS);
         SetMaterialTextureProperty(embeddedTexIdxs, r, mat.occlusionTexture, aimat, aiTextureType_LIGHTMAP);
         SetMaterialTextureProperty(embeddedTexIdxs, r, mat.occlusionTexture, aimat, aiTextureType_LIGHTMAP);
@@ -239,11 +231,14 @@ void glTF2Importer::ImportMaterials(glTF2::Asset& r)
         aimat->AddProperty(&mat.alphaCutoff, 1, "$mat.gltf.alphaCutoff");
         aimat->AddProperty(&mat.alphaCutoff, 1, "$mat.gltf.alphaCutoff");
 
 
         //pbrSpecularGlossiness
         //pbrSpecularGlossiness
-        SetMaterialColorProperty(r, mat.diffuseFactor, aimat, "$clr.diffuse", 0, 1);
-        SetMaterialColorProperty(r, mat.specularFactor, aimat, "$clr.specular", 0, 1);
-        aimat->AddProperty(&mat.glossinessFactor, 1, "$mat.gltf.glossinessFactor");
-        SetMaterialTextureProperty(embeddedTexIdxs, r, mat.diffuseTexture, aimat, aiTextureType_DIFFUSE, 1);
-        SetMaterialTextureProperty(embeddedTexIdxs, r, mat.specularGlossinessTexture, aimat, aiTextureType_UNKNOWN, 1);
+        if (mat.pbrSpecularGlossiness.on) {
+            aimat->AddProperty(&mat.pbrSpecularGlossiness.on, 1, "$mat.gltf.pbrSpecularGlossiness.on");
+            SetMaterialColorProperty(r, mat.pbrSpecularGlossiness.diffuseFactor, aimat, "$clr.diffuse", 0, 1);
+            SetMaterialColorProperty(r, mat.pbrSpecularGlossiness.specularFactor, aimat, "$clr.specular", 0, 1);
+            aimat->AddProperty(&mat.pbrSpecularGlossiness.glossinessFactor, 1, "$mat.gltf.pbrSpecularGlossiness.glossinessFactor");
+            SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrSpecularGlossiness.diffuseTexture, aimat, aiTextureType_DIFFUSE, 1);
+            SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrSpecularGlossiness.specularGlossinessTexture, aimat, aiTextureType_UNKNOWN, 1);
+        }
     }
     }
 }
 }
 
 
@@ -590,6 +585,13 @@ void glTF2Importer::ImportNodes(glTF2::Asset& r)
     //if (!mScene->mRootNode) {
     //if (!mScene->mRootNode) {
     //  mScene->mRootNode = new aiNode("EMPTY");
     //  mScene->mRootNode = new aiNode("EMPTY");
     //}
     //}
+
+    //initialize mMetaData;
+    aiMetadata* mMetaData = new aiMetadata();
+
+    //store used glTF extensions on the root node, for a lack of a better place.
+    mMetaData->Add("extensionsUsed.pbrSpecularGlossiness", r.extensionsUsed.KHR_materials_pbrSpecularGlossiness);
+    mScene->mRootNode->mMetaData = mMetaData;
 }
 }
 
 
 void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset& r)
 void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset& r)