From f8c63d874b99751280553e1b36523e55d3370622 Mon Sep 17 00:00:00 2001 From: Danny Kabrane Date: Wed, 23 Dec 2020 08:12:09 +0100 Subject: [PATCH] support KHR_materials_clearcoat during import --- code/AssetLib/glTF2/glTF2Asset.h | 13 +++++++++++++ code/AssetLib/glTF2/glTF2Asset.inl | 15 +++++++++++++++ code/AssetLib/glTF2/glTF2Importer.cpp | 11 +++++++++++ include/assimp/pbrmaterial.h | 6 ++++++ 4 files changed, 45 insertions(+) diff --git a/code/AssetLib/glTF2/glTF2Asset.h b/code/AssetLib/glTF2/glTF2Asset.h index c67f6b6bb..3d439d622 100644 --- a/code/AssetLib/glTF2/glTF2Asset.h +++ b/code/AssetLib/glTF2/glTF2Asset.h @@ -47,6 +47,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * KHR_materials_unlit full * KHR_lights_punctual full * KHR_materials_sheen full + * KHR_materials_clearcoat full */ #ifndef GLTF2ASSET_H_INC #define GLTF2ASSET_H_INC @@ -730,6 +731,14 @@ struct MaterialSheen { void SetDefaults(); }; +struct MaterialClearcoat { + float clearcoatFactor = 0.f; + float clearcoatRoughnessFactor = 0.f; + TextureInfo clearcoatTexture; + TextureInfo clearcoatRoughnessTexture; + NormalTextureInfo clearcoatNormalTexture; +}; + //! The material appearance of a primitive. struct Material : public Object { //PBR metallic roughness properties @@ -750,6 +759,9 @@ struct Material : public Object { //extension: KHR_materials_sheen Nullable materialSheen; + //extension: KHR_materials_clearcoat + Nullable materialClearcoat; + //extension: KHR_materials_unlit bool unlit; @@ -1069,6 +1081,7 @@ public: bool KHR_lights_punctual; bool KHR_texture_transform; bool KHR_materials_sheen; + bool KHR_materials_clearcoat; } extensionsUsed; //! Keeps info about the required extensions diff --git a/code/AssetLib/glTF2/glTF2Asset.inl b/code/AssetLib/glTF2/glTF2Asset.inl index ecc2fd407..8a5a697fa 100644 --- a/code/AssetLib/glTF2/glTF2Asset.inl +++ b/code/AssetLib/glTF2/glTF2Asset.inl @@ -1055,6 +1055,20 @@ inline void Material::Read(Value &material, Asset &r) { } } + if (r.extensionsUsed.KHR_materials_clearcoat) { + if (Value *curMaterialClearcoat = FindObject(*extensions, "KHR_materials_clearcoat")) { + MaterialClearcoat clearcoat; + + ReadMember(*curMaterialClearcoat, "clearcoatFactor", clearcoat.clearcoatFactor); + ReadTextureProperty(r, *curMaterialClearcoat, "clearcoatTexture", clearcoat.clearcoatTexture); + ReadMember(*curMaterialClearcoat, "clearcoatRoughnessFactor", clearcoat.clearcoatRoughnessFactor); + ReadTextureProperty(r, *curMaterialClearcoat, "clearcoatRoughnessTexture", clearcoat.clearcoatRoughnessTexture); + ReadTextureProperty(r, *curMaterialClearcoat, "clearcoatNormalTexture", clearcoat.clearcoatNormalTexture); + + this->materialClearcoat = Nullable(clearcoat); + } + } + unlit = nullptr != FindObject(*extensions, "KHR_materials_unlit"); } } @@ -1751,6 +1765,7 @@ inline void Asset::ReadExtensionsUsed(Document &doc) { CHECK_EXT(KHR_lights_punctual); CHECK_EXT(KHR_texture_transform); CHECK_EXT(KHR_materials_sheen); + CHECK_EXT(KHR_materials_clearcoat); #undef CHECK_EXT } diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 6dc6a072d..180cedfab 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -290,6 +290,17 @@ static aiMaterial *ImportMaterial(std::vector &embeddedTexIdxs, Asset &r, M SetMaterialTextureProperty(embeddedTexIdxs, r, sheen.sheenColorTexture, aimat, AI_MATKEY_GLTF_MATERIAL_SHEEN_COLOR_TEXTURE); SetMaterialTextureProperty(embeddedTexIdxs, r, sheen.sheenRoughnessTexture, aimat, AI_MATKEY_GLTF_MATERIAL_SHEEN_ROUGHNESS_TEXTURE); } + //KHR_materials_clearcoat + if (mat.materialClearcoat.isPresent) { + MaterialClearcoat &clearcoat = mat.materialClearcoat.value; + + aimat->AddProperty(&mat.materialClearcoat.isPresent, 1, AI_MATKEY_GLTF_MATERIAL_CLEARCOAT); + aimat->AddProperty(&clearcoat.clearcoatFactor, 1, AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_FACTOR); + aimat->AddProperty(&clearcoat.clearcoatRoughnessFactor, 1, AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_ROUGHNESS_FACTOR); + SetMaterialTextureProperty(embeddedTexIdxs, r, clearcoat.clearcoatTexture, aimat, AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_TEXTURE); + SetMaterialTextureProperty(embeddedTexIdxs, r, clearcoat.clearcoatRoughnessTexture, aimat, AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_ROUGHNESS_TEXTURE); + SetMaterialTextureProperty(embeddedTexIdxs, r, clearcoat.clearcoatNormalTexture, aimat, AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_NORMAL_TEXTURE); + } return aimat; } diff --git a/include/assimp/pbrmaterial.h b/include/assimp/pbrmaterial.h index 7f8441572..d6ba34aef 100644 --- a/include/assimp/pbrmaterial.h +++ b/include/assimp/pbrmaterial.h @@ -65,6 +65,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define AI_MATKEY_GLTF_MATERIAL_SHEEN_ROUGHNESS_FACTOR "$mat.gltf.materialSheen.sheenRoughnessFactor", 0, 0 #define AI_MATKEY_GLTF_MATERIAL_SHEEN_COLOR_TEXTURE aiTextureType_UNKNOWN, 1 #define AI_MATKEY_GLTF_MATERIAL_SHEEN_ROUGHNESS_TEXTURE aiTextureType_UNKNOWN, 2 +#define AI_MATKEY_GLTF_MATERIAL_CLEARCOAT "$mat.gltf.materialClearcoat", 0, 0 +#define AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_FACTOR "$mat.gltf.materialClearcoat.clearcoatFactor", 0, 0 +#define AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_ROUGHNESS_FACTOR "$mat.gltf.materialClearcoat.clearcoatRoughnessFactor", 0, 0 +#define AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_TEXTURE aiTextureType_UNKNOWN, 3 +#define AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_ROUGHNESS_TEXTURE aiTextureType_UNKNOWN, 4 +#define AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_NORMAL_TEXTURE aiTextureType_NORMALS, 1 #define _AI_MATKEY_GLTF_TEXTURE_TEXCOORD_BASE "$tex.file.texCoord" #define _AI_MATKEY_GLTF_MAPPINGNAME_BASE "$tex.mappingname"