From 383c97e380517303ef4755a8ba6039e4dbabe0fd Mon Sep 17 00:00:00 2001 From: Danny Kabrane Date: Wed, 23 Dec 2020 10:43:21 +0100 Subject: [PATCH] Add KHR_materials_transmission during export --- code/AssetLib/glTF2/glTF2AssetWriter.h | 1 + code/AssetLib/glTF2/glTF2AssetWriter.inl | 20 ++++++++++++++++++++ code/AssetLib/glTF2/glTF2Exporter.cpp | 14 ++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.h b/code/AssetLib/glTF2/glTF2AssetWriter.h index a31d79312..bf4f80dbe 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.h +++ b/code/AssetLib/glTF2/glTF2AssetWriter.h @@ -48,6 +48,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * KHR_materials_unlit: full * KHR_materials_sheen: full * KHR_materials_clearcoat: full + * KHR_materials_transmission: full */ #ifndef GLTF2ASSETWRITER_H_INC #define GLTF2ASSETWRITER_H_INC diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.inl b/code/AssetLib/glTF2/glTF2AssetWriter.inl index 81ce1fd6e..65fddaec5 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.inl +++ b/code/AssetLib/glTF2/glTF2AssetWriter.inl @@ -458,6 +458,22 @@ namespace glTF2 { } } + if (m.materialTransmission.isPresent) { + Value materialTransmission(rapidjson::Type::kObjectType); + + MaterialTransmission &transmission = m.materialTransmission.value; + + if (transmission.transmissionFactor != 0.f) { + WriteFloat(materialTransmission, transmission.transmissionFactor, "transmissionFactor", w.mAl); + } + + WriteTex(materialTransmission, transmission.transmissionTexture, "transmissionTexture", w.mAl); + + if (!materialTransmission.ObjectEmpty()) { + exts.AddMember("KHR_materials_transmission", materialTransmission, w.mAl); + } + } + if (!exts.ObjectEmpty()) { obj.AddMember("extensions", exts, w.mAl); } @@ -857,6 +873,10 @@ namespace glTF2 { if (this->mAsset.extensionsUsed.KHR_materials_clearcoat) { exts.PushBack(StringRef("KHR_materials_clearcoat"), mAl); } + + if (this->mAsset.extensionsUsed.KHR_materials_transmission) { + exts.PushBack(StringRef("KHR_materials_transmission"), mAl); + } } if (!exts.Empty()) diff --git a/code/AssetLib/glTF2/glTF2Exporter.cpp b/code/AssetLib/glTF2/glTF2Exporter.cpp index ba60be919..596ea6d28 100644 --- a/code/AssetLib/glTF2/glTF2Exporter.cpp +++ b/code/AssetLib/glTF2/glTF2Exporter.cpp @@ -747,6 +747,20 @@ void glTF2Exporter::ExportMaterials() m->materialClearcoat = Nullable(clearcoat); } + + bool hasMaterialTransmission = false; + mat->Get(AI_MATKEY_GLTF_MATERIAL_TRANSMISSION, hasMaterialTransmission); + + if (hasMaterialTransmission) { + mAsset->extensionsUsed.KHR_materials_transmission = true; + + MaterialTransmission transmission; + + mat->Get(AI_MATKEY_GLTF_MATERIAL_TRANSMISSION_FACTOR, transmission.transmissionFactor); + GetMatTex(mat, transmission.transmissionTexture, AI_MATKEY_GLTF_MATERIAL_TRANSMISSION_TEXTURE); + + m->materialTransmission = Nullable(transmission); + } } }