gltf_document_extension.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #include "gltf_document_extension.h"
  31. void GLTFDocumentExtension::_bind_methods() {
  32. // Import process.
  33. GDVIRTUAL_BIND(_import_preflight, "state", "extensions");
  34. GDVIRTUAL_BIND(_get_supported_extensions);
  35. GDVIRTUAL_BIND(_parse_node_extensions, "state", "gltf_node", "extensions");
  36. GDVIRTUAL_BIND(_parse_image_data, "state", "image_data", "mime_type", "ret_image");
  37. GDVIRTUAL_BIND(_get_image_file_extension);
  38. GDVIRTUAL_BIND(_parse_texture_json, "state", "texture_json", "ret_gltf_texture");
  39. GDVIRTUAL_BIND(_import_object_model_property, "state", "split_json_pointer", "partial_paths");
  40. GDVIRTUAL_BIND(_import_post_parse, "state");
  41. GDVIRTUAL_BIND(_import_pre_generate, "state");
  42. GDVIRTUAL_BIND(_generate_scene_node, "state", "gltf_node", "scene_parent");
  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_post_convert, "state", "root");
  49. GDVIRTUAL_BIND(_export_preserialize, "state");
  50. GDVIRTUAL_BIND(_export_object_model_property, "state", "node_path", "godot_node", "gltf_node_index", "target_object", "target_depth");
  51. GDVIRTUAL_BIND(_get_saveable_image_formats);
  52. GDVIRTUAL_BIND(_serialize_image_to_bytes, "state", "image", "image_dict", "image_format", "lossy_quality");
  53. GDVIRTUAL_BIND(_save_image_at_path, "state", "image", "file_path", "image_format", "lossy_quality");
  54. GDVIRTUAL_BIND(_serialize_texture_json, "state", "texture_json", "gltf_texture", "image_format");
  55. GDVIRTUAL_BIND(_export_node, "state", "gltf_node", "json", "node");
  56. GDVIRTUAL_BIND(_export_post, "state");
  57. }
  58. // Import process.
  59. Error GLTFDocumentExtension::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {
  60. ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
  61. Error err = OK;
  62. GDVIRTUAL_CALL(_import_preflight, p_state, p_extensions, err);
  63. return err;
  64. }
  65. Vector<String> GLTFDocumentExtension::get_supported_extensions() {
  66. Vector<String> ret;
  67. GDVIRTUAL_CALL(_get_supported_extensions, ret);
  68. return ret;
  69. }
  70. Error GLTFDocumentExtension::parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions) {
  71. ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
  72. ERR_FAIL_COND_V(p_gltf_node.is_null(), ERR_INVALID_PARAMETER);
  73. Error err = OK;
  74. GDVIRTUAL_CALL(_parse_node_extensions, p_state, p_gltf_node, p_extensions, err);
  75. return err;
  76. }
  77. Error GLTFDocumentExtension::parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) {
  78. ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
  79. ERR_FAIL_COND_V(r_image.is_null(), ERR_INVALID_PARAMETER);
  80. Error err = OK;
  81. GDVIRTUAL_CALL(_parse_image_data, p_state, p_image_data, p_mime_type, r_image, err);
  82. return err;
  83. }
  84. String GLTFDocumentExtension::get_image_file_extension() {
  85. String ret;
  86. GDVIRTUAL_CALL(_get_image_file_extension, ret);
  87. return ret;
  88. }
  89. Error GLTFDocumentExtension::parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) {
  90. ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
  91. ERR_FAIL_COND_V(r_gltf_texture.is_null(), ERR_INVALID_PARAMETER);
  92. Error err = OK;
  93. GDVIRTUAL_CALL(_parse_texture_json, p_state, p_texture_json, r_gltf_texture, err);
  94. return err;
  95. }
  96. Ref<GLTFObjectModelProperty> GLTFDocumentExtension::import_object_model_property(Ref<GLTFState> p_state, const PackedStringArray &p_split_json_pointer, const TypedArray<NodePath> &p_partial_paths) {
  97. Ref<GLTFObjectModelProperty> ret;
  98. ERR_FAIL_COND_V(p_state.is_null(), ret);
  99. GDVIRTUAL_CALL(_import_object_model_property, p_state, p_split_json_pointer, p_partial_paths, ret);
  100. return ret;
  101. }
  102. Error GLTFDocumentExtension::import_post_parse(Ref<GLTFState> p_state) {
  103. ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
  104. Error err = OK;
  105. GDVIRTUAL_CALL(_import_post_parse, p_state, err);
  106. return err;
  107. }
  108. Error GLTFDocumentExtension::import_pre_generate(Ref<GLTFState> p_state) {
  109. ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
  110. Error err = OK;
  111. GDVIRTUAL_CALL(_import_pre_generate, p_state, err);
  112. return err;
  113. }
  114. Node3D *GLTFDocumentExtension::generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent) {
  115. ERR_FAIL_COND_V(p_state.is_null(), nullptr);
  116. ERR_FAIL_COND_V(p_gltf_node.is_null(), nullptr);
  117. Node3D *ret_node = nullptr;
  118. GDVIRTUAL_CALL(_generate_scene_node, p_state, p_gltf_node, p_scene_parent, ret_node);
  119. return ret_node;
  120. }
  121. Error GLTFDocumentExtension::import_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_dict, Node *p_node) {
  122. ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
  123. ERR_FAIL_COND_V(p_gltf_node.is_null(), ERR_INVALID_PARAMETER);
  124. ERR_FAIL_NULL_V(p_node, ERR_INVALID_PARAMETER);
  125. Error err = OK;
  126. GDVIRTUAL_CALL(_import_node, p_state, p_gltf_node, r_dict, p_node, err);
  127. return err;
  128. }
  129. Error GLTFDocumentExtension::import_post(Ref<GLTFState> p_state, Node *p_root) {
  130. ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
  131. ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
  132. Error err = OK;
  133. GDVIRTUAL_CALL(_import_post, p_state, p_root, err);
  134. return err;
  135. }
  136. // Export process.
  137. Error GLTFDocumentExtension::export_preflight(Ref<GLTFState> p_state, Node *p_root) {
  138. ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
  139. Error err = OK;
  140. GDVIRTUAL_CALL(_export_preflight, p_state, p_root, err);
  141. return err;
  142. }
  143. void GLTFDocumentExtension::convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node) {
  144. ERR_FAIL_COND(p_state.is_null());
  145. ERR_FAIL_COND(p_gltf_node.is_null());
  146. ERR_FAIL_NULL(p_scene_node);
  147. GDVIRTUAL_CALL(_convert_scene_node, p_state, p_gltf_node, p_scene_node);
  148. }
  149. Error GLTFDocumentExtension::export_post_convert(Ref<GLTFState> p_state, Node *p_root) {
  150. ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
  151. ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
  152. Error err = OK;
  153. GDVIRTUAL_CALL(_export_post_convert, p_state, p_root, err);
  154. return err;
  155. }
  156. Error GLTFDocumentExtension::export_preserialize(Ref<GLTFState> p_state) {
  157. ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
  158. Error err = OK;
  159. GDVIRTUAL_CALL(_export_preserialize, p_state, err);
  160. return err;
  161. }
  162. Ref<GLTFObjectModelProperty> GLTFDocumentExtension::export_object_model_property(Ref<GLTFState> p_state, const NodePath &p_node_path, const Node *p_godot_node, GLTFNodeIndex p_gltf_node_index, const Object *p_target_object, int p_target_depth) {
  163. Ref<GLTFObjectModelProperty> ret;
  164. ERR_FAIL_COND_V(p_state.is_null(), ret);
  165. ERR_FAIL_NULL_V(p_godot_node, ret);
  166. ERR_FAIL_NULL_V(p_target_object, ret);
  167. GDVIRTUAL_CALL(_export_object_model_property, p_state, p_node_path, p_godot_node, p_gltf_node_index, p_target_object, p_target_depth, ret);
  168. return ret;
  169. }
  170. Vector<String> GLTFDocumentExtension::get_saveable_image_formats() {
  171. Vector<String> ret;
  172. GDVIRTUAL_CALL(_get_saveable_image_formats, ret);
  173. return ret;
  174. }
  175. 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) {
  176. PackedByteArray ret;
  177. ERR_FAIL_COND_V(p_state.is_null(), ret);
  178. ERR_FAIL_COND_V(p_image.is_null(), ret);
  179. GDVIRTUAL_CALL(_serialize_image_to_bytes, p_state, p_image, p_image_dict, p_image_format, p_lossy_quality, ret);
  180. return ret;
  181. }
  182. 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) {
  183. ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
  184. ERR_FAIL_COND_V(p_image.is_null(), ERR_INVALID_PARAMETER);
  185. Error ret = OK;
  186. GDVIRTUAL_CALL(_save_image_at_path, p_state, p_image, p_file_path, p_image_format, p_lossy_quality, ret);
  187. return ret;
  188. }
  189. Error GLTFDocumentExtension::serialize_texture_json(Ref<GLTFState> p_state, Dictionary p_texture_json, Ref<GLTFTexture> p_gltf_texture, const String &p_image_format) {
  190. ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
  191. ERR_FAIL_COND_V(p_gltf_texture.is_null(), ERR_INVALID_PARAMETER);
  192. Error err = OK;
  193. GDVIRTUAL_CALL(_serialize_texture_json, p_state, p_texture_json, p_gltf_texture, p_image_format, err);
  194. return err;
  195. }
  196. Error GLTFDocumentExtension::export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_dict, Node *p_node) {
  197. ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
  198. ERR_FAIL_COND_V(p_gltf_node.is_null(), ERR_INVALID_PARAMETER);
  199. Error err = OK;
  200. GDVIRTUAL_CALL(_export_node, p_state, p_gltf_node, r_dict, p_node, err);
  201. return err;
  202. }
  203. Error GLTFDocumentExtension::export_post(Ref<GLTFState> p_state) {
  204. ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
  205. Error err = OK;
  206. GDVIRTUAL_CALL(_export_post, p_state, err);
  207. return err;
  208. }