From 200bf8df7a6879b5c0840cba0b9d620f6b4221b1 Mon Sep 17 00:00:00 2001 From: diharaw Date: Wed, 29 Sep 2021 17:05:17 +0100 Subject: [PATCH 1/7] Added gltf2 KHR_materials_volume import support. --- code/AssetLib/glTF2/glTF2Asset.h | 16 ++++++++++++++++ code/AssetLib/glTF2/glTF2Asset.inl | 21 +++++++++++++++++++++ code/AssetLib/glTF2/glTF2Importer.cpp | 10 ++++++++++ include/assimp/material.h | 17 +++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/code/AssetLib/glTF2/glTF2Asset.h b/code/AssetLib/glTF2/glTF2Asset.h index 9a50ede99..fb3100b2c 100644 --- a/code/AssetLib/glTF2/glTF2Asset.h +++ b/code/AssetLib/glTF2/glTF2Asset.h @@ -49,6 +49,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * KHR_materials_sheen full * KHR_materials_clearcoat full * KHR_materials_transmission full + * KHR_materials_volume full */ #ifndef GLTF2ASSET_H_INC #define GLTF2ASSET_H_INC @@ -713,6 +714,7 @@ const vec3 defaultEmissiveFactor = { 0, 0, 0 }; const vec4 defaultDiffuseFactor = { 1, 1, 1, 1 }; const vec3 defaultSpecularFactor = { 1, 1, 1 }; const vec3 defaultSheenFactor = { 0, 0, 0 }; +const vec3 defaultAttenuationColor = { 1, 1, 1 }; struct TextureInfo { Ref texture; @@ -777,6 +779,16 @@ struct MaterialTransmission { float transmissionFactor = 0.f; }; +struct MaterialVolume { + float thicknessFactor = 0.f; + TextureInfo thicknessTexture; + float attenuationDistance = 0.f; + vec3 attenuationColor; + + MaterialVolume() { SetDefaults(); } + void SetDefaults(); +}; + //! The material appearance of a primitive. struct Material : public Object { //PBR metallic roughness properties @@ -803,6 +815,9 @@ struct Material : public Object { //extension: KHR_materials_transmission Nullable materialTransmission; + //extension: KHR_materials_volume + Nullable materialVolume; + //extension: KHR_materials_unlit bool unlit; @@ -1091,6 +1106,7 @@ public: bool KHR_materials_sheen; bool KHR_materials_clearcoat; bool KHR_materials_transmission; + bool KHR_materials_volume; bool KHR_draco_mesh_compression; bool FB_ngon_encoding; bool KHR_texture_basisu; diff --git a/code/AssetLib/glTF2/glTF2Asset.inl b/code/AssetLib/glTF2/glTF2Asset.inl index 4832995cc..ff71ff53b 100644 --- a/code/AssetLib/glTF2/glTF2Asset.inl +++ b/code/AssetLib/glTF2/glTF2Asset.inl @@ -1215,6 +1215,19 @@ inline void Material::Read(Value &material, Asset &r) { } } + if (r.extensionsUsed.KHR_materials_volume) { + if (Value *curMaterialVolume = FindObject(*extensions, "KHR_materials_volume")) { + MaterialVolume volume; + + ReadMember(*curMaterialVolume, "thicknessFactor", volume.thicknessFactor); + ReadTextureProperty(r, *curMaterialVolume, "thicknessTexture", volume.thicknessTexture); + ReadMember(*curMaterialVolume, "attenuationDistance", volume.attenuationDistance); + ReadMember(*curMaterialVolume, "attenuationColor", volume.attenuationColor); + + this->materialVolume = Nullable(volume); + } + } + unlit = nullptr != FindObject(*extensions, "KHR_materials_unlit"); } } @@ -1260,6 +1273,13 @@ inline void MaterialSheen::SetDefaults() { sheenRoughnessFactor = 0.f; } +inline void MaterialVolume::SetDefaults() { + //KHR_materials_volume properties + thicknessFactor = 0.f; + attenuationDistance = INFINITY; + SetVector(attenuationColor, defaultAttenuationColor); +} + namespace { template @@ -1931,6 +1951,7 @@ inline void Asset::ReadExtensionsUsed(Document &doc) { CHECK_EXT(KHR_materials_sheen); CHECK_EXT(KHR_materials_clearcoat); CHECK_EXT(KHR_materials_transmission); + CHECK_EXT(KHR_materials_volume); CHECK_EXT(KHR_draco_mesh_compression); CHECK_EXT(KHR_texture_basisu); diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 6c92bdc87..136b01d07 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -341,6 +341,16 @@ static aiMaterial *ImportMaterial(std::vector &embeddedTexIdxs, Asset &r, M SetMaterialTextureProperty(embeddedTexIdxs, r, transmission.transmissionTexture, aimat, AI_MATKEY_TRANSMISSION_TEXTURE); } + // KHR_materials_volume + if (mat.materialVolume.isPresent) { + MaterialVolume &volume = mat.materialVolume.value; + + aimat->AddProperty(&volume.thicknessFactor, 1, AI_MATKEY_VOLUME_THICKNESS_FACTOR); + SetMaterialTextureProperty(embeddedTexIdxs, r, volume.thicknessTexture, aimat, AI_MATKEY_VOLUME_THICKNESS_TEXTURE); + aimat->AddProperty(&volume.attenuationDistance, 1, AI_MATKEY_VOLUME_ATTENUATION_DISTANCE); + SetMaterialColorProperty(r, volume.attenuationColor, aimat, AI_MATKEY_VOLUME_ATTENUATION_COLOR); + } + return aimat; } catch (...) { delete aimat; diff --git a/include/assimp/material.h b/include/assimp/material.h index 250ad90d5..2373185fc 100644 --- a/include/assimp/material.h +++ b/include/assimp/material.h @@ -318,6 +318,12 @@ enum aiTextureType { */ aiTextureType_TRANSMISSION = 21, + /** Volume + * Simulates transmission through the surface + * May include further information such as wall thickness + */ + aiTextureType_VOLUME = 22, + /** Unknown texture * * A texture reference that does not match any of the definitions @@ -1028,6 +1034,17 @@ extern "C" { // Multiplied by AI_MATKEY_TRANSMISSION_FACTOR #define AI_MATKEY_TRANSMISSION_TEXTURE aiTextureType_TRANSMISSION, 0 +// Volume +// ------------ +// https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_volume +// Base percentage of light transmitted through the surface. 0.0 = Opaque, 1.0 = Fully transparent +#define AI_MATKEY_VOLUME_THICKNESS_FACTOR "$mat.volume.thicknessFactor", 0, 0 +// Texture defining percentage of light transmitted through the surface. +// Multiplied by AI_MATKEY_THICKNESS_FACTOR +#define AI_MATKEY_VOLUME_THICKNESS_TEXTURE aiTextureType_VOLUME, 0 +#define AI_MATKEY_VOLUME_ATTENUATION_DISTANCE "$mat.volume.attenuationDistance", 0, 0 +#define AI_MATKEY_VOLUME_ATTENUATION_COLOR "$mat.volume.attenuationColor", 0, 0 + // Emissive // -------- #define AI_MATKEY_USE_EMISSIVE_MAP "$mat.useEmissiveMap", 0, 0 From d3276de47da869417b1a619640f534d90e66a6dc Mon Sep 17 00:00:00 2001 From: diharaw Date: Thu, 30 Sep 2021 08:10:02 +0100 Subject: [PATCH 2/7] Added gltf2 KHR_materials_volume write support. --- code/AssetLib/glTF2/glTF2AssetWriter.h | 1 + code/AssetLib/glTF2/glTF2AssetWriter.inl | 26 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.h b/code/AssetLib/glTF2/glTF2AssetWriter.h index 8244a14b9..9be3fdf0c 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.h +++ b/code/AssetLib/glTF2/glTF2AssetWriter.h @@ -49,6 +49,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * KHR_materials_sheen: full * KHR_materials_clearcoat: full * KHR_materials_transmission: full + * KHR_materials_volume: full */ #ifndef GLTF2ASSETWRITER_H_INC #define GLTF2ASSETWRITER_H_INC diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.inl b/code/AssetLib/glTF2/glTF2AssetWriter.inl index 115cdf903..03b8476dc 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.inl +++ b/code/AssetLib/glTF2/glTF2AssetWriter.inl @@ -474,6 +474,28 @@ namespace glTF2 { } } + if (m.materialVolume.isPresent) { + Value materialVolume(rapidjson::Type::kObjectType); + + MaterialVolume &volume = m.materialVolume.value; + + if (volume.thicknessFactor != 0.f) { + WriteFloat(materialVolume, volume.thicknessFactor, "thicknessFactor", w.mAl); + } + + WriteTex(materialVolume, volume.thicknessTexture, "thicknessTexture", w.mAl); + + if (volume.attenuationDistance != INFINITY) { + WriteFloat(materialVolume, volume.attenuationDistance, "attenuationDistance", w.mAl); + } + + WriteVec(materialVolume, volume.attenuationColor, "attenuationColor", defaultAttenuationColor, w.mAl); + + if (!materialVolume.ObjectEmpty()) { + exts.AddMember("KHR_materials_volume", materialVolume, w.mAl); + } + } + if (!exts.ObjectEmpty()) { obj.AddMember("extensions", exts, w.mAl); } @@ -890,6 +912,10 @@ namespace glTF2 { exts.PushBack(StringRef("KHR_materials_transmission"), mAl); } + if (this->mAsset.extensionsUsed.KHR_materials_volume) { + exts.PushBack(StringRef("KHR_materials_volume"), mAl); + } + if (this->mAsset.extensionsUsed.FB_ngon_encoding) { exts.PushBack(StringRef("FB_ngon_encoding"), mAl); } From addd54125133c590bcf1dda68ca78f9952651f33 Mon Sep 17 00:00:00 2001 From: diharaw Date: Thu, 30 Sep 2021 08:47:53 +0100 Subject: [PATCH 3/7] Added gltf2 KHR_materials_ior support. --- code/AssetLib/glTF2/glTF2Asset.h | 12 ++++++++++ code/AssetLib/glTF2/glTF2Asset.inl | 16 ++++++++++++++ code/AssetLib/glTF2/glTF2AssetWriter.h | 1 + code/AssetLib/glTF2/glTF2AssetWriter.inl | 18 +++++++++++++++ code/AssetLib/glTF2/glTF2Exporter.cpp | 28 ++++++++++++++++++++++++ code/AssetLib/glTF2/glTF2Exporter.h | 4 ++++ code/AssetLib/glTF2/glTF2Importer.cpp | 7 ++++++ include/assimp/material.h | 12 ++++++++-- 8 files changed, 96 insertions(+), 2 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Asset.h b/code/AssetLib/glTF2/glTF2Asset.h index fb3100b2c..c790a1374 100644 --- a/code/AssetLib/glTF2/glTF2Asset.h +++ b/code/AssetLib/glTF2/glTF2Asset.h @@ -50,6 +50,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * KHR_materials_clearcoat full * KHR_materials_transmission full * KHR_materials_volume full + * KHR_materials_ior full */ #ifndef GLTF2ASSET_H_INC #define GLTF2ASSET_H_INC @@ -789,6 +790,13 @@ struct MaterialVolume { void SetDefaults(); }; +struct MaterialIOR { + float ior = 0.f; + + MaterialIOR() { SetDefaults(); } + void SetDefaults(); +}; + //! The material appearance of a primitive. struct Material : public Object { //PBR metallic roughness properties @@ -817,6 +825,9 @@ struct Material : public Object { //extension: KHR_materials_volume Nullable materialVolume; + + //extension: KHR_materials_ior + Nullable materialIOR; //extension: KHR_materials_unlit bool unlit; @@ -1107,6 +1118,7 @@ public: bool KHR_materials_clearcoat; bool KHR_materials_transmission; bool KHR_materials_volume; + bool KHR_materials_ior; bool KHR_draco_mesh_compression; bool FB_ngon_encoding; bool KHR_texture_basisu; diff --git a/code/AssetLib/glTF2/glTF2Asset.inl b/code/AssetLib/glTF2/glTF2Asset.inl index ff71ff53b..d65b4132b 100644 --- a/code/AssetLib/glTF2/glTF2Asset.inl +++ b/code/AssetLib/glTF2/glTF2Asset.inl @@ -1228,6 +1228,16 @@ inline void Material::Read(Value &material, Asset &r) { } } + if (r.extensionsUsed.KHR_materials_ior) { + if (Value *curMaterialIOR = FindObject(*extensions, "KHR_materials_ior")) { + MaterialIOR ior; + + ReadMember(*curMaterialIOR, "ior", ior.ior); + + this->materialIOR = Nullable(ior); + } + } + unlit = nullptr != FindObject(*extensions, "KHR_materials_unlit"); } } @@ -1280,6 +1290,11 @@ inline void MaterialVolume::SetDefaults() { SetVector(attenuationColor, defaultAttenuationColor); } +inline void MaterialIOR::SetDefaults() { + //KHR_materials_ior properties + ior = 1.5f; +} + namespace { template @@ -1952,6 +1967,7 @@ inline void Asset::ReadExtensionsUsed(Document &doc) { CHECK_EXT(KHR_materials_clearcoat); CHECK_EXT(KHR_materials_transmission); CHECK_EXT(KHR_materials_volume); + CHECK_EXT(KHR_materials_ior); CHECK_EXT(KHR_draco_mesh_compression); CHECK_EXT(KHR_texture_basisu); diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.h b/code/AssetLib/glTF2/glTF2AssetWriter.h index 9be3fdf0c..ce8b4d65e 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.h +++ b/code/AssetLib/glTF2/glTF2AssetWriter.h @@ -50,6 +50,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * KHR_materials_clearcoat: full * KHR_materials_transmission: full * KHR_materials_volume: full + * KHR_materials_ior: full */ #ifndef GLTF2ASSETWRITER_H_INC #define GLTF2ASSETWRITER_H_INC diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.inl b/code/AssetLib/glTF2/glTF2AssetWriter.inl index 03b8476dc..b91553fbe 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.inl +++ b/code/AssetLib/glTF2/glTF2AssetWriter.inl @@ -496,6 +496,20 @@ namespace glTF2 { } } + if (m.materialIOR.isPresent) { + Value materialIOR(rapidjson::Type::kObjectType); + + MaterialIOR &ior = m.materialIOR.value; + + if (ior.ior != 1.5f) { + WriteFloat(materialIOR, ior.ior, "ior", w.mAl); + } + + if (!materialIOR.ObjectEmpty()) { + exts.AddMember("KHR_materials_ior", materialIOR, w.mAl); + } + } + if (!exts.ObjectEmpty()) { obj.AddMember("extensions", exts, w.mAl); } @@ -916,6 +930,10 @@ namespace glTF2 { exts.PushBack(StringRef("KHR_materials_volume"), mAl); } + if (this->mAsset.extensionsUsed.KHR_materials_ior) { + exts.PushBack(StringRef("KHR_materials_ior"), mAl); + } + if (this->mAsset.extensionsUsed.FB_ngon_encoding) { exts.PushBack(StringRef("FB_ngon_encoding"), mAl); } diff --git a/code/AssetLib/glTF2/glTF2Exporter.cpp b/code/AssetLib/glTF2/glTF2Exporter.cpp index 22db4b26d..8327f8c54 100644 --- a/code/AssetLib/glTF2/glTF2Exporter.cpp +++ b/code/AssetLib/glTF2/glTF2Exporter.cpp @@ -704,6 +704,22 @@ bool glTF2Exporter::GetMatTransmission(const aiMaterial &mat, glTF2::MaterialTra return result || transmission.transmissionTexture.texture; } +bool glTF2Exporter::GetMatVolume(const aiMaterial &mat, glTF2::MaterialVolume &volume) { + bool result = mat.Get(AI_MATKEY_VOLUME_THICKNESS_FACTOR, volume.thicknessFactor) != aiReturn_SUCCESS; + + GetMatTex(mat, volume.thicknessTexture, AI_MATKEY_VOLUME_THICKNESS_TEXTURE); + + result = result || mat.Get(AI_MATKEY_VOLUME_ATTENUATION_DISTANCE, volume.attenuationDistance); + result = result || GetMatColor(mat, volume.attenuationColor, AI_MATKEY_VOLUME_ATTENUATION_COLOR) != aiReturn_SUCCESS; + + // Valid if any of these properties are available + return result || volume.thicknessTexture.texture; +} + +bool glTF2Exporter::GetMatIOR(const aiMaterial &mat, glTF2::MaterialIOR &ior) { + return mat.Get(AI_MATKEY_IOR, ior.ior) == aiReturn_SUCCESS; +} + void glTF2Exporter::ExportMaterials() { aiString aiName; for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) { @@ -824,6 +840,18 @@ void glTF2Exporter::ExportMaterials() { mAsset->extensionsUsed.KHR_materials_transmission = true; m->materialTransmission = Nullable(transmission); } + + MaterialVolume volume; + if (GetMatVolume(mat, volume)) { + mAsset->extensionsUsed.KHR_materials_volume = true; + m->materialVolume = Nullable(volume); + } + + MaterialIOR ior; + if (GetMatIOR(mat, ior)) { + mAsset->extensionsUsed.KHR_materials_ior = true; + m->materialIOR = Nullable(ior); + } } } } diff --git a/code/AssetLib/glTF2/glTF2Exporter.h b/code/AssetLib/glTF2/glTF2Exporter.h index 842d29815..6d70d915e 100644 --- a/code/AssetLib/glTF2/glTF2Exporter.h +++ b/code/AssetLib/glTF2/glTF2Exporter.h @@ -77,6 +77,8 @@ struct PbrSpecularGlossiness; struct MaterialSheen; struct MaterialClearcoat; struct MaterialTransmission; +struct MaterialVolume; +struct MaterialIOR; // Vec/matrix types, as raw float arrays typedef float(vec2)[2]; @@ -114,6 +116,8 @@ protected: bool GetMatSheen(const aiMaterial &mat, glTF2::MaterialSheen &sheen); bool GetMatClearcoat(const aiMaterial &mat, glTF2::MaterialClearcoat &clearcoat); bool GetMatTransmission(const aiMaterial &mat, glTF2::MaterialTransmission &transmission); + bool GetMatVolume(const aiMaterial &mat, glTF2::MaterialVolume &volume); + bool GetMatIOR(const aiMaterial &mat, glTF2::MaterialIOR &ior); void ExportMetadata(); void ExportMaterials(); void ExportMeshes(); diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 136b01d07..c7de2af4f 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -351,6 +351,13 @@ static aiMaterial *ImportMaterial(std::vector &embeddedTexIdxs, Asset &r, M SetMaterialColorProperty(r, volume.attenuationColor, aimat, AI_MATKEY_VOLUME_ATTENUATION_COLOR); } + // KHR_materials_ior + if (mat.materialIOR.isPresent) { + MaterialIOR &ior = mat.materialIOR.value; + + aimat->AddProperty(&ior.ior, 1, AI_MATKEY_IOR); + } + return aimat; } catch (...) { delete aimat; diff --git a/include/assimp/material.h b/include/assimp/material.h index 2373185fc..c86848993 100644 --- a/include/assimp/material.h +++ b/include/assimp/material.h @@ -1037,14 +1037,22 @@ extern "C" { // Volume // ------------ // https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_volume -// Base percentage of light transmitted through the surface. 0.0 = Opaque, 1.0 = Fully transparent +// The thickness of the volume beneath the surface. If the value is 0 the material is thin-walled. Otherwise the material is a volume boundary. #define AI_MATKEY_VOLUME_THICKNESS_FACTOR "$mat.volume.thicknessFactor", 0, 0 -// Texture defining percentage of light transmitted through the surface. +// Texture that defines the thickness. // Multiplied by AI_MATKEY_THICKNESS_FACTOR #define AI_MATKEY_VOLUME_THICKNESS_TEXTURE aiTextureType_VOLUME, 0 +// Density of the medium given as the average distance that light travels in the medium before interacting with a particle. #define AI_MATKEY_VOLUME_ATTENUATION_DISTANCE "$mat.volume.attenuationDistance", 0, 0 +// The color that white light turns into due to absorption when reaching the attenuation distance. #define AI_MATKEY_VOLUME_ATTENUATION_COLOR "$mat.volume.attenuationColor", 0, 0 +// IOR +// ------------ +// https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_ior +// Index of Refraction. +#define AI_MATKEY_IOR "$mat.ior", 0, 0 + // Emissive // -------- #define AI_MATKEY_USE_EMISSIVE_MAP "$mat.useEmissiveMap", 0, 0 From 46a7de7a0b21ebf03c8128fb3952db4c4d670228 Mon Sep 17 00:00:00 2001 From: diharaw Date: Thu, 30 Sep 2021 08:56:16 +0100 Subject: [PATCH 4/7] Moved KHR_material_volume thickness texture under aiTextureType_TRANSMISSION. --- include/assimp/material.h | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/include/assimp/material.h b/include/assimp/material.h index c86848993..5499bd916 100644 --- a/include/assimp/material.h +++ b/include/assimp/material.h @@ -318,12 +318,6 @@ enum aiTextureType { */ aiTextureType_TRANSMISSION = 21, - /** Volume - * Simulates transmission through the surface - * May include further information such as wall thickness - */ - aiTextureType_VOLUME = 22, - /** Unknown texture * * A texture reference that does not match any of the definitions @@ -1041,7 +1035,7 @@ extern "C" { #define AI_MATKEY_VOLUME_THICKNESS_FACTOR "$mat.volume.thicknessFactor", 0, 0 // Texture that defines the thickness. // Multiplied by AI_MATKEY_THICKNESS_FACTOR -#define AI_MATKEY_VOLUME_THICKNESS_TEXTURE aiTextureType_VOLUME, 0 +#define AI_MATKEY_VOLUME_THICKNESS_TEXTURE aiTextureType_TRANSMISSION, 1 // Density of the medium given as the average distance that light travels in the medium before interacting with a particle. #define AI_MATKEY_VOLUME_ATTENUATION_DISTANCE "$mat.volume.attenuationDistance", 0, 0 // The color that white light turns into due to absorption when reaching the attenuation distance. From 0738742611dba94d9adef998e59df963ba8c4115 Mon Sep 17 00:00:00 2001 From: diharaw Date: Thu, 7 Oct 2021 09:30:22 +0100 Subject: [PATCH 5/7] Removed KHR_materials_ior support. --- code/AssetLib/glTF2/glTF2Asset.h | 12 ------------ code/AssetLib/glTF2/glTF2Asset.inl | 16 ---------------- code/AssetLib/glTF2/glTF2AssetWriter.h | 1 - code/AssetLib/glTF2/glTF2AssetWriter.inl | 18 ------------------ code/AssetLib/glTF2/glTF2Exporter.cpp | 10 ---------- code/AssetLib/glTF2/glTF2Exporter.h | 2 -- code/AssetLib/glTF2/glTF2Importer.cpp | 7 ------- include/assimp/material.h | 6 ------ 8 files changed, 72 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Asset.h b/code/AssetLib/glTF2/glTF2Asset.h index c790a1374..e97523bbe 100644 --- a/code/AssetLib/glTF2/glTF2Asset.h +++ b/code/AssetLib/glTF2/glTF2Asset.h @@ -50,7 +50,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * KHR_materials_clearcoat full * KHR_materials_transmission full * KHR_materials_volume full - * KHR_materials_ior full */ #ifndef GLTF2ASSET_H_INC #define GLTF2ASSET_H_INC @@ -790,13 +789,6 @@ struct MaterialVolume { void SetDefaults(); }; -struct MaterialIOR { - float ior = 0.f; - - MaterialIOR() { SetDefaults(); } - void SetDefaults(); -}; - //! The material appearance of a primitive. struct Material : public Object { //PBR metallic roughness properties @@ -826,9 +818,6 @@ struct Material : public Object { //extension: KHR_materials_volume Nullable materialVolume; - //extension: KHR_materials_ior - Nullable materialIOR; - //extension: KHR_materials_unlit bool unlit; @@ -1118,7 +1107,6 @@ public: bool KHR_materials_clearcoat; bool KHR_materials_transmission; bool KHR_materials_volume; - bool KHR_materials_ior; bool KHR_draco_mesh_compression; bool FB_ngon_encoding; bool KHR_texture_basisu; diff --git a/code/AssetLib/glTF2/glTF2Asset.inl b/code/AssetLib/glTF2/glTF2Asset.inl index d65b4132b..ff71ff53b 100644 --- a/code/AssetLib/glTF2/glTF2Asset.inl +++ b/code/AssetLib/glTF2/glTF2Asset.inl @@ -1228,16 +1228,6 @@ inline void Material::Read(Value &material, Asset &r) { } } - if (r.extensionsUsed.KHR_materials_ior) { - if (Value *curMaterialIOR = FindObject(*extensions, "KHR_materials_ior")) { - MaterialIOR ior; - - ReadMember(*curMaterialIOR, "ior", ior.ior); - - this->materialIOR = Nullable(ior); - } - } - unlit = nullptr != FindObject(*extensions, "KHR_materials_unlit"); } } @@ -1290,11 +1280,6 @@ inline void MaterialVolume::SetDefaults() { SetVector(attenuationColor, defaultAttenuationColor); } -inline void MaterialIOR::SetDefaults() { - //KHR_materials_ior properties - ior = 1.5f; -} - namespace { template @@ -1967,7 +1952,6 @@ inline void Asset::ReadExtensionsUsed(Document &doc) { CHECK_EXT(KHR_materials_clearcoat); CHECK_EXT(KHR_materials_transmission); CHECK_EXT(KHR_materials_volume); - CHECK_EXT(KHR_materials_ior); CHECK_EXT(KHR_draco_mesh_compression); CHECK_EXT(KHR_texture_basisu); diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.h b/code/AssetLib/glTF2/glTF2AssetWriter.h index ce8b4d65e..9be3fdf0c 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.h +++ b/code/AssetLib/glTF2/glTF2AssetWriter.h @@ -50,7 +50,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * KHR_materials_clearcoat: full * KHR_materials_transmission: full * KHR_materials_volume: full - * KHR_materials_ior: full */ #ifndef GLTF2ASSETWRITER_H_INC #define GLTF2ASSETWRITER_H_INC diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.inl b/code/AssetLib/glTF2/glTF2AssetWriter.inl index b91553fbe..03b8476dc 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.inl +++ b/code/AssetLib/glTF2/glTF2AssetWriter.inl @@ -496,20 +496,6 @@ namespace glTF2 { } } - if (m.materialIOR.isPresent) { - Value materialIOR(rapidjson::Type::kObjectType); - - MaterialIOR &ior = m.materialIOR.value; - - if (ior.ior != 1.5f) { - WriteFloat(materialIOR, ior.ior, "ior", w.mAl); - } - - if (!materialIOR.ObjectEmpty()) { - exts.AddMember("KHR_materials_ior", materialIOR, w.mAl); - } - } - if (!exts.ObjectEmpty()) { obj.AddMember("extensions", exts, w.mAl); } @@ -930,10 +916,6 @@ namespace glTF2 { exts.PushBack(StringRef("KHR_materials_volume"), mAl); } - if (this->mAsset.extensionsUsed.KHR_materials_ior) { - exts.PushBack(StringRef("KHR_materials_ior"), mAl); - } - if (this->mAsset.extensionsUsed.FB_ngon_encoding) { exts.PushBack(StringRef("FB_ngon_encoding"), mAl); } diff --git a/code/AssetLib/glTF2/glTF2Exporter.cpp b/code/AssetLib/glTF2/glTF2Exporter.cpp index 8327f8c54..46433fc95 100644 --- a/code/AssetLib/glTF2/glTF2Exporter.cpp +++ b/code/AssetLib/glTF2/glTF2Exporter.cpp @@ -716,10 +716,6 @@ bool glTF2Exporter::GetMatVolume(const aiMaterial &mat, glTF2::MaterialVolume &v return result || volume.thicknessTexture.texture; } -bool glTF2Exporter::GetMatIOR(const aiMaterial &mat, glTF2::MaterialIOR &ior) { - return mat.Get(AI_MATKEY_IOR, ior.ior) == aiReturn_SUCCESS; -} - void glTF2Exporter::ExportMaterials() { aiString aiName; for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) { @@ -846,12 +842,6 @@ void glTF2Exporter::ExportMaterials() { mAsset->extensionsUsed.KHR_materials_volume = true; m->materialVolume = Nullable(volume); } - - MaterialIOR ior; - if (GetMatIOR(mat, ior)) { - mAsset->extensionsUsed.KHR_materials_ior = true; - m->materialIOR = Nullable(ior); - } } } } diff --git a/code/AssetLib/glTF2/glTF2Exporter.h b/code/AssetLib/glTF2/glTF2Exporter.h index 6d70d915e..c00faafb9 100644 --- a/code/AssetLib/glTF2/glTF2Exporter.h +++ b/code/AssetLib/glTF2/glTF2Exporter.h @@ -78,7 +78,6 @@ struct MaterialSheen; struct MaterialClearcoat; struct MaterialTransmission; struct MaterialVolume; -struct MaterialIOR; // Vec/matrix types, as raw float arrays typedef float(vec2)[2]; @@ -117,7 +116,6 @@ protected: bool GetMatClearcoat(const aiMaterial &mat, glTF2::MaterialClearcoat &clearcoat); bool GetMatTransmission(const aiMaterial &mat, glTF2::MaterialTransmission &transmission); bool GetMatVolume(const aiMaterial &mat, glTF2::MaterialVolume &volume); - bool GetMatIOR(const aiMaterial &mat, glTF2::MaterialIOR &ior); void ExportMetadata(); void ExportMaterials(); void ExportMeshes(); diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index c7de2af4f..136b01d07 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -351,13 +351,6 @@ static aiMaterial *ImportMaterial(std::vector &embeddedTexIdxs, Asset &r, M SetMaterialColorProperty(r, volume.attenuationColor, aimat, AI_MATKEY_VOLUME_ATTENUATION_COLOR); } - // KHR_materials_ior - if (mat.materialIOR.isPresent) { - MaterialIOR &ior = mat.materialIOR.value; - - aimat->AddProperty(&ior.ior, 1, AI_MATKEY_IOR); - } - return aimat; } catch (...) { delete aimat; diff --git a/include/assimp/material.h b/include/assimp/material.h index 5499bd916..4e55e4894 100644 --- a/include/assimp/material.h +++ b/include/assimp/material.h @@ -1041,12 +1041,6 @@ extern "C" { // The color that white light turns into due to absorption when reaching the attenuation distance. #define AI_MATKEY_VOLUME_ATTENUATION_COLOR "$mat.volume.attenuationColor", 0, 0 -// IOR -// ------------ -// https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_ior -// Index of Refraction. -#define AI_MATKEY_IOR "$mat.ior", 0, 0 - // Emissive // -------- #define AI_MATKEY_USE_EMISSIVE_MAP "$mat.useEmissiveMap", 0, 0 From b01d008bc0d8042bd2395be4b0a8b0bb2eaaa0c6 Mon Sep 17 00:00:00 2001 From: diharaw Date: Thu, 7 Oct 2021 09:36:53 +0100 Subject: [PATCH 6/7] Revert "Removed KHR_materials_ior support." This reverts commit 0738742611dba94d9adef998e59df963ba8c4115. --- code/AssetLib/glTF2/glTF2Asset.h | 12 ++++++++++++ code/AssetLib/glTF2/glTF2Asset.inl | 16 ++++++++++++++++ code/AssetLib/glTF2/glTF2AssetWriter.h | 1 + code/AssetLib/glTF2/glTF2AssetWriter.inl | 18 ++++++++++++++++++ code/AssetLib/glTF2/glTF2Exporter.cpp | 10 ++++++++++ code/AssetLib/glTF2/glTF2Exporter.h | 2 ++ code/AssetLib/glTF2/glTF2Importer.cpp | 7 +++++++ include/assimp/material.h | 6 ++++++ 8 files changed, 72 insertions(+) diff --git a/code/AssetLib/glTF2/glTF2Asset.h b/code/AssetLib/glTF2/glTF2Asset.h index e97523bbe..c790a1374 100644 --- a/code/AssetLib/glTF2/glTF2Asset.h +++ b/code/AssetLib/glTF2/glTF2Asset.h @@ -50,6 +50,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * KHR_materials_clearcoat full * KHR_materials_transmission full * KHR_materials_volume full + * KHR_materials_ior full */ #ifndef GLTF2ASSET_H_INC #define GLTF2ASSET_H_INC @@ -789,6 +790,13 @@ struct MaterialVolume { void SetDefaults(); }; +struct MaterialIOR { + float ior = 0.f; + + MaterialIOR() { SetDefaults(); } + void SetDefaults(); +}; + //! The material appearance of a primitive. struct Material : public Object { //PBR metallic roughness properties @@ -818,6 +826,9 @@ struct Material : public Object { //extension: KHR_materials_volume Nullable materialVolume; + //extension: KHR_materials_ior + Nullable materialIOR; + //extension: KHR_materials_unlit bool unlit; @@ -1107,6 +1118,7 @@ public: bool KHR_materials_clearcoat; bool KHR_materials_transmission; bool KHR_materials_volume; + bool KHR_materials_ior; bool KHR_draco_mesh_compression; bool FB_ngon_encoding; bool KHR_texture_basisu; diff --git a/code/AssetLib/glTF2/glTF2Asset.inl b/code/AssetLib/glTF2/glTF2Asset.inl index ff71ff53b..d65b4132b 100644 --- a/code/AssetLib/glTF2/glTF2Asset.inl +++ b/code/AssetLib/glTF2/glTF2Asset.inl @@ -1228,6 +1228,16 @@ inline void Material::Read(Value &material, Asset &r) { } } + if (r.extensionsUsed.KHR_materials_ior) { + if (Value *curMaterialIOR = FindObject(*extensions, "KHR_materials_ior")) { + MaterialIOR ior; + + ReadMember(*curMaterialIOR, "ior", ior.ior); + + this->materialIOR = Nullable(ior); + } + } + unlit = nullptr != FindObject(*extensions, "KHR_materials_unlit"); } } @@ -1280,6 +1290,11 @@ inline void MaterialVolume::SetDefaults() { SetVector(attenuationColor, defaultAttenuationColor); } +inline void MaterialIOR::SetDefaults() { + //KHR_materials_ior properties + ior = 1.5f; +} + namespace { template @@ -1952,6 +1967,7 @@ inline void Asset::ReadExtensionsUsed(Document &doc) { CHECK_EXT(KHR_materials_clearcoat); CHECK_EXT(KHR_materials_transmission); CHECK_EXT(KHR_materials_volume); + CHECK_EXT(KHR_materials_ior); CHECK_EXT(KHR_draco_mesh_compression); CHECK_EXT(KHR_texture_basisu); diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.h b/code/AssetLib/glTF2/glTF2AssetWriter.h index 9be3fdf0c..ce8b4d65e 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.h +++ b/code/AssetLib/glTF2/glTF2AssetWriter.h @@ -50,6 +50,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * KHR_materials_clearcoat: full * KHR_materials_transmission: full * KHR_materials_volume: full + * KHR_materials_ior: full */ #ifndef GLTF2ASSETWRITER_H_INC #define GLTF2ASSETWRITER_H_INC diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.inl b/code/AssetLib/glTF2/glTF2AssetWriter.inl index 03b8476dc..b91553fbe 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.inl +++ b/code/AssetLib/glTF2/glTF2AssetWriter.inl @@ -496,6 +496,20 @@ namespace glTF2 { } } + if (m.materialIOR.isPresent) { + Value materialIOR(rapidjson::Type::kObjectType); + + MaterialIOR &ior = m.materialIOR.value; + + if (ior.ior != 1.5f) { + WriteFloat(materialIOR, ior.ior, "ior", w.mAl); + } + + if (!materialIOR.ObjectEmpty()) { + exts.AddMember("KHR_materials_ior", materialIOR, w.mAl); + } + } + if (!exts.ObjectEmpty()) { obj.AddMember("extensions", exts, w.mAl); } @@ -916,6 +930,10 @@ namespace glTF2 { exts.PushBack(StringRef("KHR_materials_volume"), mAl); } + if (this->mAsset.extensionsUsed.KHR_materials_ior) { + exts.PushBack(StringRef("KHR_materials_ior"), mAl); + } + if (this->mAsset.extensionsUsed.FB_ngon_encoding) { exts.PushBack(StringRef("FB_ngon_encoding"), mAl); } diff --git a/code/AssetLib/glTF2/glTF2Exporter.cpp b/code/AssetLib/glTF2/glTF2Exporter.cpp index 46433fc95..8327f8c54 100644 --- a/code/AssetLib/glTF2/glTF2Exporter.cpp +++ b/code/AssetLib/glTF2/glTF2Exporter.cpp @@ -716,6 +716,10 @@ bool glTF2Exporter::GetMatVolume(const aiMaterial &mat, glTF2::MaterialVolume &v return result || volume.thicknessTexture.texture; } +bool glTF2Exporter::GetMatIOR(const aiMaterial &mat, glTF2::MaterialIOR &ior) { + return mat.Get(AI_MATKEY_IOR, ior.ior) == aiReturn_SUCCESS; +} + void glTF2Exporter::ExportMaterials() { aiString aiName; for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) { @@ -842,6 +846,12 @@ void glTF2Exporter::ExportMaterials() { mAsset->extensionsUsed.KHR_materials_volume = true; m->materialVolume = Nullable(volume); } + + MaterialIOR ior; + if (GetMatIOR(mat, ior)) { + mAsset->extensionsUsed.KHR_materials_ior = true; + m->materialIOR = Nullable(ior); + } } } } diff --git a/code/AssetLib/glTF2/glTF2Exporter.h b/code/AssetLib/glTF2/glTF2Exporter.h index c00faafb9..6d70d915e 100644 --- a/code/AssetLib/glTF2/glTF2Exporter.h +++ b/code/AssetLib/glTF2/glTF2Exporter.h @@ -78,6 +78,7 @@ struct MaterialSheen; struct MaterialClearcoat; struct MaterialTransmission; struct MaterialVolume; +struct MaterialIOR; // Vec/matrix types, as raw float arrays typedef float(vec2)[2]; @@ -116,6 +117,7 @@ protected: bool GetMatClearcoat(const aiMaterial &mat, glTF2::MaterialClearcoat &clearcoat); bool GetMatTransmission(const aiMaterial &mat, glTF2::MaterialTransmission &transmission); bool GetMatVolume(const aiMaterial &mat, glTF2::MaterialVolume &volume); + bool GetMatIOR(const aiMaterial &mat, glTF2::MaterialIOR &ior); void ExportMetadata(); void ExportMaterials(); void ExportMeshes(); diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 136b01d07..c7de2af4f 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -351,6 +351,13 @@ static aiMaterial *ImportMaterial(std::vector &embeddedTexIdxs, Asset &r, M SetMaterialColorProperty(r, volume.attenuationColor, aimat, AI_MATKEY_VOLUME_ATTENUATION_COLOR); } + // KHR_materials_ior + if (mat.materialIOR.isPresent) { + MaterialIOR &ior = mat.materialIOR.value; + + aimat->AddProperty(&ior.ior, 1, AI_MATKEY_IOR); + } + return aimat; } catch (...) { delete aimat; diff --git a/include/assimp/material.h b/include/assimp/material.h index 4e55e4894..5499bd916 100644 --- a/include/assimp/material.h +++ b/include/assimp/material.h @@ -1041,6 +1041,12 @@ extern "C" { // The color that white light turns into due to absorption when reaching the attenuation distance. #define AI_MATKEY_VOLUME_ATTENUATION_COLOR "$mat.volume.attenuationColor", 0, 0 +// IOR +// ------------ +// https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_ior +// Index of Refraction. +#define AI_MATKEY_IOR "$mat.ior", 0, 0 + // Emissive // -------- #define AI_MATKEY_USE_EMISSIVE_MAP "$mat.useEmissiveMap", 0, 0 From 0fb66f84378ef441977fd552b6cda2ff3dd7216d Mon Sep 17 00:00:00 2001 From: diharaw Date: Thu, 7 Oct 2021 09:39:08 +0100 Subject: [PATCH 7/7] Replaced AI_MATKEY_IOR with AI_MATKEY_REFRACTI. --- code/AssetLib/glTF2/glTF2Exporter.cpp | 2 +- code/AssetLib/glTF2/glTF2Importer.cpp | 2 +- include/assimp/material.h | 6 ------ 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Exporter.cpp b/code/AssetLib/glTF2/glTF2Exporter.cpp index 8327f8c54..572c61f2b 100644 --- a/code/AssetLib/glTF2/glTF2Exporter.cpp +++ b/code/AssetLib/glTF2/glTF2Exporter.cpp @@ -717,7 +717,7 @@ bool glTF2Exporter::GetMatVolume(const aiMaterial &mat, glTF2::MaterialVolume &v } bool glTF2Exporter::GetMatIOR(const aiMaterial &mat, glTF2::MaterialIOR &ior) { - return mat.Get(AI_MATKEY_IOR, ior.ior) == aiReturn_SUCCESS; + return mat.Get(AI_MATKEY_REFRACTI, ior.ior) == aiReturn_SUCCESS; } void glTF2Exporter::ExportMaterials() { diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index c7de2af4f..87f2604ca 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -355,7 +355,7 @@ static aiMaterial *ImportMaterial(std::vector &embeddedTexIdxs, Asset &r, M if (mat.materialIOR.isPresent) { MaterialIOR &ior = mat.materialIOR.value; - aimat->AddProperty(&ior.ior, 1, AI_MATKEY_IOR); + aimat->AddProperty(&ior.ior, 1, AI_MATKEY_REFRACTI); } return aimat; diff --git a/include/assimp/material.h b/include/assimp/material.h index 5499bd916..4e55e4894 100644 --- a/include/assimp/material.h +++ b/include/assimp/material.h @@ -1041,12 +1041,6 @@ extern "C" { // The color that white light turns into due to absorption when reaching the attenuation distance. #define AI_MATKEY_VOLUME_ATTENUATION_COLOR "$mat.volume.attenuationColor", 0, 0 -// IOR -// ------------ -// https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_ior -// Index of Refraction. -#define AI_MATKEY_IOR "$mat.ior", 0, 0 - // Emissive // -------- #define AI_MATKEY_USE_EMISSIVE_MAP "$mat.useEmissiveMap", 0, 0