Browse Source

Add a way to get the GLTF extensions supported by GLTFDocumentExtension

Aaron Franke 3 years ago
parent
commit
7097e8add7

+ 6 - 0
modules/gltf/doc_classes/GLTFDocumentExtension.xml

@@ -30,6 +30,12 @@
 			<description>
 			</description>
 		</method>
+		<method name="_get_supported_extensions" qualifiers="virtual">
+			<return type="PackedStringArray" />
+			<description>
+				Returns an array of the GLTF extensions supported by this GLTFDocumentExtension class. This is used to validate if a GLTF file with required extensions can be loaded.
+			</description>
+		</method>
 		<method name="_import_node" qualifiers="virtual">
 			<return type="int" />
 			<param index="0" name="state" type="GLTFState" />

+ 19 - 4
modules/gltf/gltf_document.cpp

@@ -6927,9 +6927,24 @@ Error GLTFDocument::_parse_gltf_extensions(Ref<GLTFState> state) {
 		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.");
-		return ERR_UNAVAILABLE;
+	HashSet<String> supported_extensions;
+	supported_extensions.insert("KHR_lights_punctual");
+	supported_extensions.insert("KHR_materials_pbrSpecularGlossiness");
+	supported_extensions.insert("KHR_texture_transform");
+	for (int ext_i = 0; ext_i < document_extensions.size(); ext_i++) {
+		Ref<GLTFDocumentExtension> ext = document_extensions[ext_i];
+		ERR_CONTINUE(ext.is_null());
+		Vector<String> ext_supported_extensions = ext->get_supported_extensions();
+		for (int i = 0; i < ext_supported_extensions.size(); ++i) {
+			supported_extensions.insert(ext_supported_extensions[i]);
+		}
 	}
-	return OK;
+	Error ret = Error::OK;
+	for (int i = 0; i < state->extensions_required.size(); i++) {
+		if (!supported_extensions.has(state->extensions_required[i])) {
+			ERR_PRINT("GLTF: Can't import file '" + state->filename + "', required extension '" + String(state->extensions_required[i]) + "' is not supported. Are you missing a GLTFDocumentExtension plugin?");
+			ret = ERR_UNAVAILABLE;
+		}
+	}
+	return ret;
 }

+ 7 - 0
modules/gltf/gltf_document_extension.cpp

@@ -31,6 +31,7 @@
 #include "gltf_document_extension.h"
 
 void GLTFDocumentExtension::_bind_methods() {
+	GDVIRTUAL_BIND(_get_supported_extensions);
 	GDVIRTUAL_BIND(_import_preflight, "state");
 	GDVIRTUAL_BIND(_import_post_parse, "state");
 	GDVIRTUAL_BIND(_import_node, "state", "gltf_node", "json", "node");
@@ -40,6 +41,12 @@ void GLTFDocumentExtension::_bind_methods() {
 	GDVIRTUAL_BIND(_export_post, "state");
 }
 
+Vector<String> GLTFDocumentExtension::get_supported_extensions() {
+	Vector<String> ret;
+	GDVIRTUAL_CALL(_get_supported_extensions, ret);
+	return ret;
+}
+
 Error GLTFDocumentExtension::import_post(Ref<GLTFState> p_state, Node *p_root) {
 	ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
 	ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER);

+ 2 - 0
modules/gltf/gltf_document_extension.h

@@ -41,6 +41,7 @@ protected:
 	static void _bind_methods();
 
 public:
+	virtual Vector<String> get_supported_extensions();
 	virtual Error import_preflight(Ref<GLTFState> p_state);
 	virtual Error import_post_parse(Ref<GLTFState> p_state);
 	virtual Error export_post(Ref<GLTFState> p_state);
@@ -48,6 +49,7 @@ public:
 	virtual Error export_preflight(Node *p_state);
 	virtual Error import_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_json, Node *p_node);
 	virtual Error export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_json, Node *p_node);
+	GDVIRTUAL0R(Vector<String>, _get_supported_extensions);
 	GDVIRTUAL1R(int, _import_preflight, Ref<GLTFState>);
 	GDVIRTUAL1R(int, _import_post_parse, Ref<GLTFState>);
 	GDVIRTUAL4R(int, _import_node, Ref<GLTFState>, Ref<GLTFNode>, Dictionary, Node *);