From a1a17c1dda127adbd571fb063af9309eebaac4bd Mon Sep 17 00:00:00 2001 From: Stanlo Slasinski Date: Fri, 18 May 2018 14:01:25 -0700 Subject: [PATCH] Read and write the KHR_materials_unlit glTF/2.0 extension. --- code/glTF2Asset.h | 5 +++++ code/glTF2Asset.inl | 4 ++++ code/glTF2AssetWriter.h | 1 + code/glTF2AssetWriter.inl | 10 ++++++++++ code/glTF2Exporter.cpp | 6 ++++++ code/glTF2Importer.cpp | 3 +++ include/assimp/pbrmaterial.h | 1 + 7 files changed, 30 insertions(+) diff --git a/code/glTF2Asset.h b/code/glTF2Asset.h index dd9b11df5..0ce843d09 100644 --- a/code/glTF2Asset.h +++ b/code/glTF2Asset.h @@ -45,6 +45,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * glTF Extensions Support: * KHR_materials_pbrSpecularGlossiness full + * KHR_materials_unlit full */ #ifndef GLTF2ASSET_H_INC #define GLTF2ASSET_H_INC @@ -741,6 +742,9 @@ namespace glTF2 //extension: KHR_materials_pbrSpecularGlossiness Nullable pbrSpecularGlossiness; + //extension: KHR_materials_unlit + bool unlit; + Material() { SetDefaults(); } void Read(Value& obj, Asset& r); void SetDefaults(); @@ -1037,6 +1041,7 @@ namespace glTF2 struct Extensions { bool KHR_materials_pbrSpecularGlossiness; + bool KHR_materials_unlit; } extensionsUsed; diff --git a/code/glTF2Asset.inl b/code/glTF2Asset.inl index d96db6d74..acf8cb331 100644 --- a/code/glTF2Asset.inl +++ b/code/glTF2Asset.inl @@ -860,6 +860,8 @@ inline void Material::Read(Value& material, Asset& r) this->pbrSpecularGlossiness = Nullable(pbrSG); } } + + unlit = nullptr != FindObject(*extensions, "KHR_materials_unlit"); } } @@ -882,6 +884,7 @@ inline void Material::SetDefaults() alphaMode = "OPAQUE"; alphaCutoff = 0.5; doubleSided = false; + unlit = false; } inline void PbrSpecularGlossiness::SetDefaults() @@ -1253,6 +1256,7 @@ inline void Asset::ReadExtensionsUsed(Document& doc) if (exts.find(#EXT) != exts.end()) extensionsUsed.EXT = true; CHECK_EXT(KHR_materials_pbrSpecularGlossiness); + CHECK_EXT(KHR_materials_unlit); #undef CHECK_EXT } diff --git a/code/glTF2AssetWriter.h b/code/glTF2AssetWriter.h index e2b97e8c4..493ca1c0a 100644 --- a/code/glTF2AssetWriter.h +++ b/code/glTF2AssetWriter.h @@ -45,6 +45,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * glTF Extensions Support: * KHR_materials_pbrSpecularGlossiness: full + * KHR_materials_unlit: full */ #ifndef GLTF2ASSETWRITER_H_INC #define GLTF2ASSETWRITER_H_INC diff --git a/code/glTF2AssetWriter.inl b/code/glTF2AssetWriter.inl index 6be012676..0579dfdac 100644 --- a/code/glTF2AssetWriter.inl +++ b/code/glTF2AssetWriter.inl @@ -343,6 +343,12 @@ namespace glTF2 { } } + if (m.unlit) { + Value unlit; + unlit.SetObject(); + exts.AddMember("KHR_materials_unlit", unlit, w.mAl); + } + if (!exts.ObjectEmpty()) { obj.AddMember("extensions", exts, w.mAl); } @@ -683,6 +689,10 @@ namespace glTF2 { if (this->mAsset.extensionsUsed.KHR_materials_pbrSpecularGlossiness) { exts.PushBack(StringRef("KHR_materials_pbrSpecularGlossiness"), mAl); } + + if (this->mAsset.extensionsUsed.KHR_materials_unlit) { + exts.PushBack(StringRef("KHR_materials_unlit"), mAl); + } } if (!exts.Empty()) diff --git a/code/glTF2Exporter.cpp b/code/glTF2Exporter.cpp index d4b3d67de..9e8dfc822 100644 --- a/code/glTF2Exporter.cpp +++ b/code/glTF2Exporter.cpp @@ -526,6 +526,12 @@ void glTF2Exporter::ExportMaterials() m->pbrSpecularGlossiness = Nullable(pbrSG); } + + bool unlit; + if (mat->Get(AI_MATKEY_GLTF_UNLIT, unlit) == AI_SUCCESS && unlit) { + mAsset->extensionsUsed.KHR_materials_unlit = true; + m->unlit = true; + } } } diff --git a/code/glTF2Importer.cpp b/code/glTF2Importer.cpp index 2bcf8b5de..f478ca487 100644 --- a/code/glTF2Importer.cpp +++ b/code/glTF2Importer.cpp @@ -281,6 +281,9 @@ static aiMaterial* ImportMaterial(std::vector& embeddedTexIdxs, Asset& r, M SetMaterialTextureProperty(embeddedTexIdxs, r, pbrSG.specularGlossinessTexture, aimat, aiTextureType_SPECULAR); } + if (mat.unlit) { + aimat->AddProperty(&mat.unlit, 1, AI_MATKEY_GLTF_UNLIT); + } return aimat; } diff --git a/include/assimp/pbrmaterial.h b/include/assimp/pbrmaterial.h index cd9b5e2bf..723957300 100644 --- a/include/assimp/pbrmaterial.h +++ b/include/assimp/pbrmaterial.h @@ -56,6 +56,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define AI_MATKEY_GLTF_ALPHACUTOFF "$mat.gltf.alphaCutoff", 0, 0 #define AI_MATKEY_GLTF_PBRSPECULARGLOSSINESS "$mat.gltf.pbrSpecularGlossiness", 0, 0 #define AI_MATKEY_GLTF_PBRSPECULARGLOSSINESS_GLOSSINESS_FACTOR "$mat.gltf.pbrMetallicRoughness.glossinessFactor", 0, 0 +#define AI_MATKEY_GLTF_UNLIT "$mat.gltf.unlit", 0, 0 #define _AI_MATKEY_GLTF_TEXTURE_TEXCOORD_BASE "$tex.file.texCoord" #define _AI_MATKEY_GLTF_MAPPINGNAME_BASE "$tex.mappingname"