Make used extensions stored in GLTFState

This allows GLTFDocumentExtension classes to add to the used extensions array.
This commit is contained in:
Aaron Franke 2022-09-17 22:06:23 -05:00
parent ca1ebf9fee
commit b72dc0de89
No known key found for this signature in database
GPG key ID: 40A1750B977E56BF
5 changed files with 42 additions and 11 deletions

View file

@ -7,6 +7,14 @@
<tutorials> <tutorials>
</tutorials> </tutorials>
<methods> <methods>
<method name="add_used_extension">
<return type="void" />
<param index="0" name="extension_name" type="String" />
<param index="1" name="required" type="bool" />
<description>
Appends an extension to the list of extensions used by this GLTF file during serialization. If [param required] is true, the extension will also be added to the list of required extensions. Do not run this in [method GLTFDocumentExtension._export_post], as that stage is too late to add extensions. The final list is sorted alphabetically.
</description>
</method>
<method name="get_accessors"> <method name="get_accessors">
<return type="GLTFAccessor[]" /> <return type="GLTFAccessor[]" />
<description> <description>

View file

@ -198,7 +198,7 @@ Error GLTFDocument::_serialize(Ref<GLTFState> state, const String &p_path) {
} }
/* STEP SERIALIZE EXTENSIONS */ /* STEP SERIALIZE EXTENSIONS */
err = _serialize_extensions(state); err = _serialize_gltf_extensions(state);
if (err != OK) { if (err != OK) {
return Error::FAILED; return Error::FAILED;
} }
@ -219,9 +219,9 @@ Error GLTFDocument::_serialize(Ref<GLTFState> state, const String &p_path) {
return OK; return OK;
} }
Error GLTFDocument::_serialize_extensions(Ref<GLTFState> state) const { Error GLTFDocument::_serialize_gltf_extensions(Ref<GLTFState> state) const {
Array extensions_used; Vector<String> extensions_used = state->extensions_used;
Array extensions_required; Vector<String> extensions_required = state->extensions_required;
if (!state->lights.is_empty()) { if (!state->lights.is_empty()) {
extensions_used.push_back("KHR_lights_punctual"); extensions_used.push_back("KHR_lights_punctual");
} }
@ -230,9 +230,11 @@ Error GLTFDocument::_serialize_extensions(Ref<GLTFState> state) const {
extensions_required.push_back("KHR_texture_transform"); extensions_required.push_back("KHR_texture_transform");
} }
if (!extensions_used.is_empty()) { if (!extensions_used.is_empty()) {
extensions_used.sort();
state->json["extensionsUsed"] = extensions_used; state->json["extensionsUsed"] = extensions_used;
} }
if (!extensions_required.is_empty()) { if (!extensions_required.is_empty()) {
extensions_required.sort();
state->json["extensionsRequired"] = extensions_required; state->json["extensionsRequired"] = extensions_required;
} }
return OK; return OK;
@ -6917,12 +6919,17 @@ Error GLTFDocument::append_from_file(String p_path, Ref<GLTFState> r_state, uint
Error GLTFDocument::_parse_gltf_extensions(Ref<GLTFState> state) { Error GLTFDocument::_parse_gltf_extensions(Ref<GLTFState> state) {
ERR_FAIL_NULL_V(state, ERR_PARSE_ERROR); ERR_FAIL_NULL_V(state, ERR_PARSE_ERROR);
if (state->json.has("extensionsRequired") && state->json["extensionsRequired"].get_type() == Variant::ARRAY) { if (state->json.has("extensionsUsed")) {
Array extensions_required = state->json["extensionsRequired"]; Vector<String> ext_array = state->json["extensionsUsed"];
if (extensions_required.find("KHR_draco_mesh_compression") != -1) { state->extensions_used = ext_array;
}
if (state->json.has("extensionsRequired")) {
Vector<String> ext_array = state->json["extensionsRequired"];
state->extensions_required = ext_array;
}
if (state->extensions_required.find("KHR_draco_mesh_compression") != -1) {
ERR_PRINT("glTF2 extension KHR_draco_mesh_compression is not supported."); ERR_PRINT("glTF2 extension KHR_draco_mesh_compression is not supported.");
return ERR_UNAVAILABLE; return ERR_UNAVAILABLE;
} }
}
return OK; return OK;
} }

View file

@ -265,7 +265,7 @@ private:
Dictionary _serialize_texture_transform_uv2(Ref<BaseMaterial3D> p_material); Dictionary _serialize_texture_transform_uv2(Ref<BaseMaterial3D> p_material);
Error _serialize_version(Ref<GLTFState> state); Error _serialize_version(Ref<GLTFState> state);
Error _serialize_file(Ref<GLTFState> state, const String p_path); Error _serialize_file(Ref<GLTFState> state, const String p_path);
Error _serialize_extensions(Ref<GLTFState> state) const; Error _serialize_gltf_extensions(Ref<GLTFState> state) const;
public: public:
// https://www.itu.int/rec/R-REC-BT.601 // https://www.itu.int/rec/R-REC-BT.601

View file

@ -31,6 +31,7 @@
#include "gltf_state.h" #include "gltf_state.h"
void GLTFState::_bind_methods() { void GLTFState::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_used_extension", "extension_name", "required"), &GLTFState::add_used_extension);
ClassDB::bind_method(D_METHOD("get_json"), &GLTFState::get_json); ClassDB::bind_method(D_METHOD("get_json"), &GLTFState::get_json);
ClassDB::bind_method(D_METHOD("set_json", "json"), &GLTFState::set_json); ClassDB::bind_method(D_METHOD("set_json", "json"), &GLTFState::set_json);
ClassDB::bind_method(D_METHOD("get_major_version"), &GLTFState::get_major_version); ClassDB::bind_method(D_METHOD("get_major_version"), &GLTFState::get_major_version);
@ -112,6 +113,17 @@ void GLTFState::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "animations", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_animations", "get_animations"); // Vector<Ref<GLTFAnimation>> ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "animations", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_animations", "get_animations"); // Vector<Ref<GLTFAnimation>>
} }
void GLTFState::add_used_extension(const String &p_extension_name, bool p_required) {
if (!extensions_used.has(p_extension_name)) {
extensions_used.push_back(p_extension_name);
}
if (p_required) {
if (!extensions_required.has(p_extension_name)) {
extensions_required.push_back(p_extension_name);
}
}
}
Dictionary GLTFState::get_json() { Dictionary GLTFState::get_json() {
return json; return json;
} }

View file

@ -78,6 +78,8 @@ class GLTFState : public Resource {
Vector<int> root_nodes; Vector<int> root_nodes;
Vector<Ref<GLTFTexture>> textures; Vector<Ref<GLTFTexture>> textures;
Vector<Ref<Texture2D>> images; Vector<Ref<Texture2D>> images;
Vector<String> extensions_used;
Vector<String> extensions_required;
Vector<Ref<GLTFSkin>> skins; Vector<Ref<GLTFSkin>> skins;
Vector<Ref<GLTFCamera>> cameras; Vector<Ref<GLTFCamera>> cameras;
@ -97,6 +99,8 @@ protected:
static void _bind_methods(); static void _bind_methods();
public: public:
void add_used_extension(const String &p_extension, bool p_required = false);
Dictionary get_json(); Dictionary get_json();
void set_json(Dictionary p_json); void set_json(Dictionary p_json);