diff options
Diffstat (limited to 'indra/newview/gltf/buffer_util.h')
-rw-r--r-- | indra/newview/gltf/buffer_util.h | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/indra/newview/gltf/buffer_util.h b/indra/newview/gltf/buffer_util.h index bd43f60d19..c26752a6b6 100644 --- a/indra/newview/gltf/buffer_util.h +++ b/indra/newview/gltf/buffer_util.h @@ -519,6 +519,104 @@ namespace LL return true; } + + // to/from extension + + // for internal use only, use copy_extensions instead + template<typename T> + inline bool _copy_extension(const boost::json::object& extensions, std::string_view member, T* dst) + { + if (extensions.contains(member)) + { + return copy(extensions.at(member), *dst); + } + + return false; + } + + // Copy all extensions from src.extensions to provided destinations + // Usage: + // copy_extensions(src, + // "KHR_materials_unlit", &mUnlit, + // "KHR_materials_pbrSpecularGlossiness", &mPbrSpecularGlossiness); + // returns true if any of the extensions are copied + template<class... Types> + inline bool copy_extensions(const boost::json::value& src, Types... args) + { + // extract the extensions object (don't assume it exists and verify that it is an object) + if (src.is_object()) + { + boost::json::object obj = src.get_object(); + if (obj.contains("extensions")) + { + const boost::json::value& extensions = obj.at("extensions"); + if (extensions.is_object()) + { + const boost::json::object& ext_obj = extensions.as_object(); + bool success = false; + // copy each extension, return true if any of them succeed, do not short circuit on success + U32 count = sizeof...(args); + for (U32 i = 0; i < count; i += 2) + { + if (_copy_extension(ext_obj, args...)) + { + success = true; + } + } + return success; + } + } + } + + return false; + } + + // internal use aonly, use write_extensions instead + template<typename T> + inline bool _write_extension(boost::json::object& extensions, const T* src, string_view member) + { + if (src->mPresent) + { + Value v; + if (write(*src, v)) + { + extensions[member] = v; + return true; + } + } + return false; + } + + // Write all extensions to dst.extensions + // Usage: + // write_extensions(dst, + // "KHR_materials_unlit", mUnlit, + // "KHR_materials_pbrSpecularGlossiness", mPbrSpecularGlossiness); + // returns true if any of the extensions are written + template<class... Types> + inline bool write_extensions(boost::json::object& dst, Types... args) + { + bool success = false; + + boost::json::object extensions; + U32 count = sizeof...(args) - 1; + + for (U32 i = 0; i < count; i += 2) + { + if (_write_extension(extensions, args...)) + { + success = true; + } + } + + if (success) + { + dst["extensions"] = extensions; + } + + return success; + } + // conditionally write a member to an object if the member // is not the default value template<typename T> |