| 1 | /**************************************************************************/ | 
|---|
| 2 | /*  gltf_document_extension.cpp                                           */ | 
|---|
| 3 | /**************************************************************************/ | 
|---|
| 4 | /*                         This file is part of:                          */ | 
|---|
| 5 | /*                             GODOT ENGINE                               */ | 
|---|
| 6 | /*                        https://godotengine.org                         */ | 
|---|
| 7 | /**************************************************************************/ | 
|---|
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
|---|
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
|---|
| 10 | /*                                                                        */ | 
|---|
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
|---|
| 12 | /* a copy of this software and associated documentation files (the        */ | 
|---|
| 13 | /* "Software"), to deal in the Software without restriction, including    */ | 
|---|
| 14 | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
|---|
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
|---|
| 16 | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
|---|
| 17 | /* the following conditions:                                              */ | 
|---|
| 18 | /*                                                                        */ | 
|---|
| 19 | /* The above copyright notice and this permission notice shall be         */ | 
|---|
| 20 | /* included in all copies or substantial portions of the Software.        */ | 
|---|
| 21 | /*                                                                        */ | 
|---|
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
|---|
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
|---|
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
|---|
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
|---|
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
|---|
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
|---|
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
|---|
| 29 | /**************************************************************************/ | 
|---|
| 30 |  | 
|---|
| 31 | #include "gltf_document_extension.h" | 
|---|
| 32 |  | 
|---|
| 33 | void GLTFDocumentExtension::_bind_methods() { | 
|---|
| 34 | // Import process. | 
|---|
| 35 | GDVIRTUAL_BIND(_import_preflight, "state", "extensions"); | 
|---|
| 36 | GDVIRTUAL_BIND(_get_supported_extensions); | 
|---|
| 37 | GDVIRTUAL_BIND(_parse_node_extensions, "state", "gltf_node", "extensions"); | 
|---|
| 38 | GDVIRTUAL_BIND(_parse_image_data, "state", "image_data", "mime_type", "ret_image"); | 
|---|
| 39 | GDVIRTUAL_BIND(_get_image_file_extension); | 
|---|
| 40 | GDVIRTUAL_BIND(_parse_texture_json, "state", "texture_json", "ret_gltf_texture"); | 
|---|
| 41 | GDVIRTUAL_BIND(_generate_scene_node, "state", "gltf_node", "scene_parent"); | 
|---|
| 42 | GDVIRTUAL_BIND(_import_post_parse, "state"); | 
|---|
| 43 | GDVIRTUAL_BIND(_import_node, "state", "gltf_node", "json", "node"); | 
|---|
| 44 | GDVIRTUAL_BIND(_import_post, "state", "root"); | 
|---|
| 45 | // Export process. | 
|---|
| 46 | GDVIRTUAL_BIND(_export_preflight, "state", "root"); | 
|---|
| 47 | GDVIRTUAL_BIND(_convert_scene_node, "state", "gltf_node", "scene_node"); | 
|---|
| 48 | GDVIRTUAL_BIND(_export_preserialize, "state"); | 
|---|
| 49 | GDVIRTUAL_BIND(_get_saveable_image_formats); | 
|---|
| 50 | GDVIRTUAL_BIND(_serialize_image_to_bytes, "state", "image", "image_dict", "image_format", "lossy_quality"); | 
|---|
| 51 | GDVIRTUAL_BIND(_save_image_at_path, "state", "image", "file_path", "image_format", "lossy_quality"); | 
|---|
| 52 | GDVIRTUAL_BIND(_serialize_texture_json, "state", "texture_json", "gltf_texture", "image_format"); | 
|---|
| 53 | GDVIRTUAL_BIND(_export_node, "state", "gltf_node", "json", "node"); | 
|---|
| 54 | GDVIRTUAL_BIND(_export_post, "state"); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | // Import process. | 
|---|
| 58 | Error GLTFDocumentExtension::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) { | 
|---|
| 59 | ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER); | 
|---|
| 60 | Error err = OK; | 
|---|
| 61 | GDVIRTUAL_CALL(_import_preflight, p_state, p_extensions, err); | 
|---|
| 62 | return err; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | Vector<String> GLTFDocumentExtension::get_supported_extensions() { | 
|---|
| 66 | Vector<String> ret; | 
|---|
| 67 | GDVIRTUAL_CALL(_get_supported_extensions, ret); | 
|---|
| 68 | return ret; | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | Error GLTFDocumentExtension::parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions) { | 
|---|
| 72 | ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER); | 
|---|
| 73 | ERR_FAIL_NULL_V(p_gltf_node, ERR_INVALID_PARAMETER); | 
|---|
| 74 | Error err = OK; | 
|---|
| 75 | GDVIRTUAL_CALL(_parse_node_extensions, p_state, p_gltf_node, p_extensions, err); | 
|---|
| 76 | return err; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | Error GLTFDocumentExtension::parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) { | 
|---|
| 80 | ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER); | 
|---|
| 81 | ERR_FAIL_NULL_V(r_image, ERR_INVALID_PARAMETER); | 
|---|
| 82 | Error err = OK; | 
|---|
| 83 | GDVIRTUAL_CALL(_parse_image_data, p_state, p_image_data, p_mime_type, r_image, err); | 
|---|
| 84 | return err; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | String GLTFDocumentExtension::get_image_file_extension() { | 
|---|
| 88 | String ret; | 
|---|
| 89 | GDVIRTUAL_CALL(_get_image_file_extension, ret); | 
|---|
| 90 | return ret; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | Error GLTFDocumentExtension::parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) { | 
|---|
| 94 | ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER); | 
|---|
| 95 | ERR_FAIL_NULL_V(r_gltf_texture, ERR_INVALID_PARAMETER); | 
|---|
| 96 | Error err = OK; | 
|---|
| 97 | GDVIRTUAL_CALL(_parse_texture_json, p_state, p_texture_json, r_gltf_texture, err); | 
|---|
| 98 | return err; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | Node3D *GLTFDocumentExtension::generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent) { | 
|---|
| 102 | ERR_FAIL_NULL_V(p_state, nullptr); | 
|---|
| 103 | ERR_FAIL_NULL_V(p_gltf_node, nullptr); | 
|---|
| 104 | ERR_FAIL_NULL_V(p_scene_parent, nullptr); | 
|---|
| 105 | Node3D *ret_node = nullptr; | 
|---|
| 106 | GDVIRTUAL_CALL(_generate_scene_node, p_state, p_gltf_node, p_scene_parent, ret_node); | 
|---|
| 107 | return ret_node; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | Error GLTFDocumentExtension::import_post_parse(Ref<GLTFState> p_state) { | 
|---|
| 111 | ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER); | 
|---|
| 112 | Error err = OK; | 
|---|
| 113 | GDVIRTUAL_CALL(_import_post_parse, p_state, err); | 
|---|
| 114 | return err; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | Error GLTFDocumentExtension::import_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_dict, Node *p_node) { | 
|---|
| 118 | ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER); | 
|---|
| 119 | ERR_FAIL_NULL_V(p_gltf_node, ERR_INVALID_PARAMETER); | 
|---|
| 120 | ERR_FAIL_NULL_V(p_node, ERR_INVALID_PARAMETER); | 
|---|
| 121 | Error err = OK; | 
|---|
| 122 | GDVIRTUAL_CALL(_import_node, p_state, p_gltf_node, r_dict, p_node, err); | 
|---|
| 123 | return err; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | Error GLTFDocumentExtension::import_post(Ref<GLTFState> p_state, Node *p_root) { | 
|---|
| 127 | ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER); | 
|---|
| 128 | ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER); | 
|---|
| 129 | Error err = OK; | 
|---|
| 130 | GDVIRTUAL_CALL(_import_post, p_state, p_root, err); | 
|---|
| 131 | return err; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | // Export process. | 
|---|
| 135 | Error GLTFDocumentExtension::export_preflight(Ref<GLTFState> p_state, Node *p_root) { | 
|---|
| 136 | ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER); | 
|---|
| 137 | Error err = OK; | 
|---|
| 138 | GDVIRTUAL_CALL(_export_preflight, p_state, p_root, err); | 
|---|
| 139 | return err; | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | void GLTFDocumentExtension::convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node) { | 
|---|
| 143 | ERR_FAIL_NULL(p_state); | 
|---|
| 144 | ERR_FAIL_NULL(p_gltf_node); | 
|---|
| 145 | ERR_FAIL_NULL(p_scene_node); | 
|---|
| 146 | GDVIRTUAL_CALL(_convert_scene_node, p_state, p_gltf_node, p_scene_node); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | Error GLTFDocumentExtension::export_preserialize(Ref<GLTFState> p_state) { | 
|---|
| 150 | ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER); | 
|---|
| 151 | Error err = OK; | 
|---|
| 152 | GDVIRTUAL_CALL(_export_preserialize, p_state, err); | 
|---|
| 153 | return err; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | Vector<String> GLTFDocumentExtension::get_saveable_image_formats() { | 
|---|
| 157 | Vector<String> ret; | 
|---|
| 158 | GDVIRTUAL_CALL(_get_saveable_image_formats, ret); | 
|---|
| 159 | return ret; | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | PackedByteArray GLTFDocumentExtension::serialize_image_to_bytes(Ref<GLTFState> p_state, Ref<Image> p_image, Dictionary p_image_dict, const String &p_image_format, float p_lossy_quality) { | 
|---|
| 163 | PackedByteArray ret; | 
|---|
| 164 | ERR_FAIL_NULL_V(p_state, ret); | 
|---|
| 165 | ERR_FAIL_NULL_V(p_image, ret); | 
|---|
| 166 | GDVIRTUAL_CALL(_serialize_image_to_bytes, p_state, p_image, p_image_dict, p_image_format, p_lossy_quality, ret); | 
|---|
| 167 | return ret; | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | Error GLTFDocumentExtension::save_image_at_path(Ref<GLTFState> p_state, Ref<Image> p_image, const String &p_file_path, const String &p_image_format, float p_lossy_quality) { | 
|---|
| 171 | ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER); | 
|---|
| 172 | ERR_FAIL_NULL_V(p_image, ERR_INVALID_PARAMETER); | 
|---|
| 173 | Error ret = OK; | 
|---|
| 174 | GDVIRTUAL_CALL(_save_image_at_path, p_state, p_image, p_file_path, p_image_format, p_lossy_quality, ret); | 
|---|
| 175 | return ret; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | Error GLTFDocumentExtension::serialize_texture_json(Ref<GLTFState> p_state, Dictionary p_texture_json, Ref<GLTFTexture> p_gltf_texture, const String &p_image_format) { | 
|---|
| 179 | ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER); | 
|---|
| 180 | ERR_FAIL_NULL_V(p_gltf_texture, ERR_INVALID_PARAMETER); | 
|---|
| 181 | Error err = OK; | 
|---|
| 182 | GDVIRTUAL_CALL(_serialize_texture_json, p_state, p_texture_json, p_gltf_texture, p_image_format, err); | 
|---|
| 183 | return err; | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | Error GLTFDocumentExtension::export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_dict, Node *p_node) { | 
|---|
| 187 | ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER); | 
|---|
| 188 | ERR_FAIL_NULL_V(p_gltf_node, ERR_INVALID_PARAMETER); | 
|---|
| 189 | ERR_FAIL_NULL_V(p_node, ERR_INVALID_PARAMETER); | 
|---|
| 190 | Error err = OK; | 
|---|
| 191 | GDVIRTUAL_CALL(_export_node, p_state, p_gltf_node, r_dict, p_node, err); | 
|---|
| 192 | return err; | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | Error GLTFDocumentExtension::export_post(Ref<GLTFState> p_state) { | 
|---|
| 196 | ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER); | 
|---|
| 197 | Error err = OK; | 
|---|
| 198 | GDVIRTUAL_CALL(_export_post, p_state, err); | 
|---|
| 199 | return err; | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|