From 7d4dd3f8fa5df244db6acbb47c12ef90dc4f8cae Mon Sep 17 00:00:00 2001 From: sean Date: Thu, 5 Oct 2023 17:02:14 +0200 Subject: [PATCH] Add all missing material fields --- src/fastgltf.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 4 deletions(-) diff --git a/src/fastgltf.cpp b/src/fastgltf.cpp index 200ab7c2c..3e726e6ef 100644 --- a/src/fastgltf.cpp +++ b/src/fastgltf.cpp @@ -314,6 +314,20 @@ namespace fastgltf { return Error::None; } + + void writeTextureInfo(std::string& json, const TextureInfo* info, TextureInfoType type = TextureInfoType::Standard) { + json += '{'; + json += "\"index\":" + std::to_string(info->textureIndex); + if (info->texCoordIndex != 0) { + json += ",\"texCoord\":" + std::to_string(info->texCoordIndex); + } + if (type == TextureInfoType::NormalTexture) { + json += ",\"scale\":" + std::to_string(reinterpret_cast(info)->scale); + } else if (type == TextureInfoType::OcclusionTexture) { + json += ",\"strength\":" + std::to_string(reinterpret_cast(info)->strength); + } + json += '}'; + } } // namespace fastgltf #pragma region URI @@ -3652,8 +3666,65 @@ void fg::Composer::writeMaterials(std::string& json) { for (auto it = asset->materials.begin(); it != asset->materials.end(); ++it) { json += '{'; - if (it->alphaMode != AlphaMode::Opaque) { + json += "\"pbrMetallicRoughness\":{"; + if (it->pbrData.baseColorFactor != std::array{{1.0f, 1.0f, 1.0f, 1.0f}}) { + json += R"("baseColorFactor":[)"; + json += std::to_string(it->pbrData.baseColorFactor[0]) + ',' + std::to_string(it->pbrData.baseColorFactor[1]) + ',' + + std::to_string(it->pbrData.baseColorFactor[2]) + ',' + std::to_string(it->pbrData.baseColorFactor[3]); + json += "],"; + } + + if (it->pbrData.baseColorTexture.has_value()) { + if (json.back() != '{') json += ','; + json += "\"baseColorTexture\":"; + writeTextureInfo(json, &it->pbrData.baseColorTexture.value()); + } + + if (it->pbrData.metallicFactor != 1.0f) { + if (json.back() != '{') json += ','; + json += "\"metallicFactor\":" + std::to_string(it->pbrData.metallicFactor); + } + + if (it->pbrData.roughnessFactor != 1.0f) { + if (json.back() != '{') json += ','; + json += "\"roughnessFactor\":" + std::to_string(it->pbrData.roughnessFactor); + } + + if (it->pbrData.metallicRoughnessTexture.has_value()) { if (json.back() != '{') json += ','; + json += "\"metallicRoughnessTexture\":"; + writeTextureInfo(json, &it->pbrData.metallicRoughnessTexture.value()); + } + + json += '}'; + + if (it->normalTexture.has_value()) { + if (json.back() != ',') json += ','; + json += "\"normalTexture\":"; + writeTextureInfo(json, &it->normalTexture.value(), TextureInfoType::NormalTexture); + } + + if (it->occlusionTexture.has_value()) { + if (json.back() != ',') json += ','; + json += "\"occlusionTexture\":"; + writeTextureInfo(json, &it->occlusionTexture.value(), TextureInfoType::OcclusionTexture); + } + + if (it->emissiveTexture.has_value()) { + if (json.back() != ',') json += ','; + json += "\"emissiveTexture\":"; + writeTextureInfo(json, &it->emissiveTexture.value()); + } + + if (it->emissiveFactor != std::array{{.0f, .0f, .0f}}) { + if (json.back() != ',') json += ','; + json += R"("emissiveFactor":[)"; + json += std::to_string(it->emissiveFactor[0]) + ',' + std::to_string(it->emissiveFactor[1]) + ',' + std::to_string(it->emissiveFactor[2]); + json += "],"; + } + + if (it->alphaMode != AlphaMode::Opaque) { + if (json.back() != ',') json += ','; json += R"("alphaMode":)"; if (it->alphaMode == AlphaMode::Blend) { json += "\"BLEND\""; @@ -3663,17 +3734,17 @@ void fg::Composer::writeMaterials(std::string& json) { } if (it->alphaCutoff != 0.5f) { - if (json.back() != '{') json += ','; + if (json.back() != ',') json += ','; json += R"("alphaCutoff":)" + std::to_string(it->alphaCutoff); } if (it->doubleSided) { - if (json.back() != '{') json += ','; + if (json.back() != ',') json += ','; json += R"("doubleSided":true)"; } if (!it->name.empty()) { - if (json.back() != '{') json += ','; + if (json.back() != ',') json += ','; json += R"("name":")" + it->name + '"'; } json += '}';