gltf_state.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**************************************************************************/
  2. /* gltf_state.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_state.h"
  31. #include "gltf_state.compat.inc"
  32. #include "gltf_template_convert.h"
  33. void GLTFState::_bind_methods() {
  34. ClassDB::bind_method(D_METHOD("add_used_extension", "extension_name", "required"), &GLTFState::add_used_extension);
  35. ClassDB::bind_method(D_METHOD("append_data_to_buffers", "data", "deduplication"), &GLTFState::append_data_to_buffers);
  36. ClassDB::bind_method(D_METHOD("append_gltf_node", "gltf_node", "godot_scene_node", "parent_node_index"), &GLTFState::append_gltf_node);
  37. ClassDB::bind_method(D_METHOD("get_json"), &GLTFState::get_json);
  38. ClassDB::bind_method(D_METHOD("set_json", "json"), &GLTFState::set_json);
  39. ClassDB::bind_method(D_METHOD("get_major_version"), &GLTFState::get_major_version);
  40. ClassDB::bind_method(D_METHOD("set_major_version", "major_version"), &GLTFState::set_major_version);
  41. ClassDB::bind_method(D_METHOD("get_minor_version"), &GLTFState::get_minor_version);
  42. ClassDB::bind_method(D_METHOD("set_minor_version", "minor_version"), &GLTFState::set_minor_version);
  43. ClassDB::bind_method(D_METHOD("get_copyright"), &GLTFState::get_copyright);
  44. ClassDB::bind_method(D_METHOD("set_copyright", "copyright"), &GLTFState::set_copyright);
  45. ClassDB::bind_method(D_METHOD("get_glb_data"), &GLTFState::get_glb_data);
  46. ClassDB::bind_method(D_METHOD("set_glb_data", "glb_data"), &GLTFState::set_glb_data);
  47. ClassDB::bind_method(D_METHOD("get_use_named_skin_binds"), &GLTFState::get_use_named_skin_binds);
  48. ClassDB::bind_method(D_METHOD("set_use_named_skin_binds", "use_named_skin_binds"), &GLTFState::set_use_named_skin_binds);
  49. ClassDB::bind_method(D_METHOD("get_nodes"), &GLTFState::get_nodes_bind);
  50. ClassDB::bind_method(D_METHOD("set_nodes", "nodes"), &GLTFState::set_nodes_bind);
  51. ClassDB::bind_method(D_METHOD("get_buffers"), &GLTFState::get_buffers_bind);
  52. ClassDB::bind_method(D_METHOD("set_buffers", "buffers"), &GLTFState::set_buffers_bind);
  53. ClassDB::bind_method(D_METHOD("get_buffer_views"), &GLTFState::get_buffer_views_bind);
  54. ClassDB::bind_method(D_METHOD("set_buffer_views", "buffer_views"), &GLTFState::set_buffer_views_bind);
  55. ClassDB::bind_method(D_METHOD("get_accessors"), &GLTFState::get_accessors_bind);
  56. ClassDB::bind_method(D_METHOD("set_accessors", "accessors"), &GLTFState::set_accessors_bind);
  57. ClassDB::bind_method(D_METHOD("get_meshes"), &GLTFState::get_meshes_bind);
  58. ClassDB::bind_method(D_METHOD("set_meshes", "meshes"), &GLTFState::set_meshes_bind);
  59. ClassDB::bind_method(D_METHOD("get_animation_players_count", "anim_player_index"), &GLTFState::get_animation_players_count);
  60. ClassDB::bind_method(D_METHOD("get_animation_player", "anim_player_index"), &GLTFState::get_animation_player);
  61. ClassDB::bind_method(D_METHOD("get_materials"), &GLTFState::get_materials_bind);
  62. ClassDB::bind_method(D_METHOD("set_materials", "materials"), &GLTFState::set_materials_bind);
  63. ClassDB::bind_method(D_METHOD("get_scene_name"), &GLTFState::get_scene_name);
  64. ClassDB::bind_method(D_METHOD("set_scene_name", "scene_name"), &GLTFState::set_scene_name);
  65. ClassDB::bind_method(D_METHOD("get_base_path"), &GLTFState::get_base_path);
  66. ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &GLTFState::set_base_path);
  67. ClassDB::bind_method(D_METHOD("get_filename"), &GLTFState::get_filename);
  68. ClassDB::bind_method(D_METHOD("set_filename", "filename"), &GLTFState::set_filename);
  69. ClassDB::bind_method(D_METHOD("get_root_nodes"), &GLTFState::get_root_nodes);
  70. ClassDB::bind_method(D_METHOD("set_root_nodes", "root_nodes"), &GLTFState::set_root_nodes);
  71. ClassDB::bind_method(D_METHOD("get_textures"), &GLTFState::get_textures_bind);
  72. ClassDB::bind_method(D_METHOD("set_textures", "textures"), &GLTFState::set_textures_bind);
  73. ClassDB::bind_method(D_METHOD("get_texture_samplers"), &GLTFState::get_texture_samplers_bind);
  74. ClassDB::bind_method(D_METHOD("set_texture_samplers", "texture_samplers"), &GLTFState::set_texture_samplers_bind);
  75. ClassDB::bind_method(D_METHOD("get_images"), &GLTFState::get_images_bind);
  76. ClassDB::bind_method(D_METHOD("set_images", "images"), &GLTFState::set_images_bind);
  77. ClassDB::bind_method(D_METHOD("get_skins"), &GLTFState::get_skins_bind);
  78. ClassDB::bind_method(D_METHOD("set_skins", "skins"), &GLTFState::set_skins_bind);
  79. ClassDB::bind_method(D_METHOD("get_cameras"), &GLTFState::get_cameras_bind);
  80. ClassDB::bind_method(D_METHOD("set_cameras", "cameras"), &GLTFState::set_cameras_bind);
  81. ClassDB::bind_method(D_METHOD("get_lights"), &GLTFState::get_lights_bind);
  82. ClassDB::bind_method(D_METHOD("set_lights", "lights"), &GLTFState::set_lights_bind);
  83. ClassDB::bind_method(D_METHOD("get_unique_names"), &GLTFState::get_unique_names_bind);
  84. ClassDB::bind_method(D_METHOD("set_unique_names", "unique_names"), &GLTFState::set_unique_names_bind);
  85. ClassDB::bind_method(D_METHOD("get_unique_animation_names"), &GLTFState::get_unique_animation_names_bind);
  86. ClassDB::bind_method(D_METHOD("set_unique_animation_names", "unique_animation_names"), &GLTFState::set_unique_animation_names_bind);
  87. ClassDB::bind_method(D_METHOD("get_skeletons"), &GLTFState::get_skeletons_bind);
  88. ClassDB::bind_method(D_METHOD("set_skeletons", "skeletons"), &GLTFState::set_skeletons_bind);
  89. ClassDB::bind_method(D_METHOD("get_create_animations"), &GLTFState::get_create_animations);
  90. ClassDB::bind_method(D_METHOD("set_create_animations", "create_animations"), &GLTFState::set_create_animations);
  91. ClassDB::bind_method(D_METHOD("get_import_as_skeleton_bones"), &GLTFState::get_import_as_skeleton_bones);
  92. ClassDB::bind_method(D_METHOD("set_import_as_skeleton_bones", "import_as_skeleton_bones"), &GLTFState::set_import_as_skeleton_bones);
  93. ClassDB::bind_method(D_METHOD("get_animations"), &GLTFState::get_animations_bind);
  94. ClassDB::bind_method(D_METHOD("set_animations", "animations"), &GLTFState::set_animations_bind);
  95. ClassDB::bind_method(D_METHOD("get_scene_node", "gltf_node_index"), &GLTFState::get_scene_node);
  96. ClassDB::bind_method(D_METHOD("get_node_index", "scene_node"), &GLTFState::get_node_index);
  97. ClassDB::bind_method(D_METHOD("get_additional_data", "extension_name"), &GLTFState::get_additional_data);
  98. ClassDB::bind_method(D_METHOD("set_additional_data", "extension_name", "additional_data"), &GLTFState::set_additional_data);
  99. ClassDB::bind_method(D_METHOD("get_handle_binary_image_mode"), &GLTFState::get_handle_binary_image_mode);
  100. ClassDB::bind_method(D_METHOD("set_handle_binary_image_mode", "method"), &GLTFState::set_handle_binary_image_mode);
  101. ClassDB::bind_method(D_METHOD("set_bake_fps", "value"), &GLTFState::set_bake_fps);
  102. ClassDB::bind_method(D_METHOD("get_bake_fps"), &GLTFState::get_bake_fps);
  103. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "json"), "set_json", "get_json"); // Dictionary
  104. ADD_PROPERTY(PropertyInfo(Variant::INT, "major_version"), "set_major_version", "get_major_version"); // int
  105. ADD_PROPERTY(PropertyInfo(Variant::INT, "minor_version"), "set_minor_version", "get_minor_version"); // int
  106. ADD_PROPERTY(PropertyInfo(Variant::STRING, "copyright"), "set_copyright", "get_copyright"); // String
  107. ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "glb_data"), "set_glb_data", "get_glb_data"); // Vector<uint8_t>
  108. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_named_skin_binds"), "set_use_named_skin_binds", "get_use_named_skin_binds"); // bool
  109. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "nodes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_nodes", "get_nodes"); // Vector<Ref<GLTFNode>>
  110. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "buffers"), "set_buffers", "get_buffers"); // Vector<Vector<uint8_t>
  111. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "buffer_views", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_buffer_views", "get_buffer_views"); // Vector<Ref<GLTFBufferView>>
  112. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "accessors", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_accessors", "get_accessors"); // Vector<Ref<GLTFAccessor>>
  113. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "meshes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_meshes", "get_meshes"); // Vector<Ref<GLTFMesh>>
  114. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "materials", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_materials", "get_materials"); // Vector<Ref<Material>
  115. ADD_PROPERTY(PropertyInfo(Variant::STRING, "scene_name"), "set_scene_name", "get_scene_name"); // String
  116. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_path"), "set_base_path", "get_base_path"); // String
  117. ADD_PROPERTY(PropertyInfo(Variant::STRING, "filename"), "set_filename", "get_filename"); // String
  118. ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "root_nodes"), "set_root_nodes", "get_root_nodes"); // Vector<int>
  119. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "textures", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_textures", "get_textures"); // Vector<Ref<GLTFTexture>>
  120. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "texture_samplers", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_texture_samplers", "get_texture_samplers"); //Vector<Ref<GLTFTextureSampler>>
  121. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "images", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_images", "get_images"); // Vector<Ref<Texture>
  122. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "skins", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_skins", "get_skins"); // Vector<Ref<GLTFSkin>>
  123. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "cameras", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_cameras", "get_cameras"); // Vector<Ref<GLTFCamera>>
  124. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "lights", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_lights", "get_lights"); // Vector<Ref<GLTFLight>>
  125. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "unique_names", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_unique_names", "get_unique_names"); // Set<String>
  126. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "unique_animation_names", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_unique_animation_names", "get_unique_animation_names"); // Set<String>
  127. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "skeletons", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_skeletons", "get_skeletons"); // Vector<Ref<GLTFSkeleton>>
  128. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "create_animations"), "set_create_animations", "get_create_animations"); // bool
  129. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "import_as_skeleton_bones"), "set_import_as_skeleton_bones", "get_import_as_skeleton_bones"); // bool
  130. 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>>
  131. ADD_PROPERTY(PropertyInfo(Variant::INT, "handle_binary_image_mode", PROPERTY_HINT_ENUM, "Discard All Textures,Extract Textures,Embed as Basis Universal,Embed as Uncompressed"), "set_handle_binary_image_mode", "get_handle_binary_image_mode"); // enum
  132. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bake_fps", PROPERTY_HINT_RANGE, "0.001,120,0.0001,or_greater"), "set_bake_fps", "get_bake_fps");
  133. BIND_ENUM_CONSTANT(HANDLE_BINARY_IMAGE_MODE_DISCARD_TEXTURES);
  134. BIND_ENUM_CONSTANT(HANDLE_BINARY_IMAGE_MODE_EXTRACT_TEXTURES);
  135. BIND_ENUM_CONSTANT(HANDLE_BINARY_IMAGE_MODE_EMBED_AS_BASISU);
  136. BIND_ENUM_CONSTANT(HANDLE_BINARY_IMAGE_MODE_EMBED_AS_UNCOMPRESSED);
  137. #ifndef DISABLE_DEPRECATED
  138. // Deprecated non-type-safe versions for backward compatibility, remove in Godot 5.0.
  139. ClassDB::bind_method(D_METHOD("get_handle_binary_image"), &GLTFState::get_handle_binary_image);
  140. ClassDB::bind_method(D_METHOD("set_handle_binary_image", "method"), &GLTFState::set_handle_binary_image);
  141. ADD_PROPERTY(PropertyInfo(Variant::INT, "handle_binary_image", PROPERTY_HINT_ENUM, "Discard All Textures,Extract Textures,Embed as Basis Universal,Embed as Uncompressed", PROPERTY_USAGE_INTERNAL), "set_handle_binary_image", "get_handle_binary_image"); // enum as int
  142. BIND_CONSTANT(HANDLE_BINARY_DISCARD_TEXTURES);
  143. BIND_CONSTANT(HANDLE_BINARY_EXTRACT_TEXTURES);
  144. BIND_CONSTANT(HANDLE_BINARY_EMBED_AS_BASISU);
  145. BIND_CONSTANT(HANDLE_BINARY_EMBED_AS_UNCOMPRESSED);
  146. #endif // DISABLE_DEPRECATED
  147. }
  148. void GLTFState::add_used_extension(const String &p_extension_name, bool p_required) {
  149. if (!extensions_used.has(p_extension_name)) {
  150. extensions_used.push_back(p_extension_name);
  151. }
  152. if (p_required) {
  153. if (!extensions_required.has(p_extension_name)) {
  154. extensions_required.push_back(p_extension_name);
  155. }
  156. }
  157. }
  158. Dictionary GLTFState::get_json() const {
  159. return Dictionary(json);
  160. }
  161. void GLTFState::set_json(const Dictionary &p_json) {
  162. json = Dictionary(p_json);
  163. }
  164. int GLTFState::get_major_version() const {
  165. return major_version;
  166. }
  167. void GLTFState::set_major_version(int p_major_version) {
  168. major_version = p_major_version;
  169. }
  170. int GLTFState::get_minor_version() const {
  171. return minor_version;
  172. }
  173. void GLTFState::set_minor_version(int p_minor_version) {
  174. minor_version = p_minor_version;
  175. }
  176. String GLTFState::get_copyright() const {
  177. return copyright;
  178. }
  179. void GLTFState::set_copyright(const String &p_copyright) {
  180. copyright = p_copyright;
  181. }
  182. Vector<uint8_t> GLTFState::get_glb_data() const {
  183. return Vector<uint8_t>(glb_data);
  184. }
  185. void GLTFState::set_glb_data(const Vector<uint8_t> &p_glb_data) {
  186. glb_data = Vector<uint8_t>(p_glb_data);
  187. }
  188. bool GLTFState::get_use_named_skin_binds() const {
  189. return use_named_skin_binds;
  190. }
  191. void GLTFState::set_use_named_skin_binds(bool p_use_named_skin_binds) {
  192. use_named_skin_binds = p_use_named_skin_binds;
  193. }
  194. TypedArray<GLTFNode> GLTFState::get_nodes_bind() const {
  195. return GLTFTemplateConvert::to_array(nodes);
  196. }
  197. void GLTFState::set_nodes_bind(const TypedArray<GLTFNode> &p_nodes) {
  198. GLTFTemplateConvert::set_from_array(nodes, p_nodes);
  199. }
  200. TypedArray<PackedByteArray> GLTFState::get_buffers_bind() const {
  201. return GLTFTemplateConvert::to_array(buffers);
  202. }
  203. void GLTFState::set_buffers_bind(const TypedArray<PackedByteArray> &p_buffers) {
  204. GLTFTemplateConvert::set_from_array(buffers, p_buffers);
  205. }
  206. TypedArray<GLTFBufferView> GLTFState::get_buffer_views_bind() const {
  207. return GLTFTemplateConvert::to_array(buffer_views);
  208. }
  209. void GLTFState::set_buffer_views_bind(const TypedArray<GLTFBufferView> &p_buffer_views) {
  210. GLTFTemplateConvert::set_from_array(buffer_views, p_buffer_views);
  211. }
  212. TypedArray<GLTFAccessor> GLTFState::get_accessors_bind() const {
  213. return GLTFTemplateConvert::to_array(accessors);
  214. }
  215. void GLTFState::set_accessors_bind(const TypedArray<GLTFAccessor> &p_accessors) {
  216. GLTFTemplateConvert::set_from_array(accessors, p_accessors);
  217. }
  218. TypedArray<GLTFMesh> GLTFState::get_meshes_bind() const {
  219. return GLTFTemplateConvert::to_array(meshes);
  220. }
  221. void GLTFState::set_meshes_bind(const TypedArray<GLTFMesh> &p_meshes) {
  222. GLTFTemplateConvert::set_from_array(meshes, p_meshes);
  223. }
  224. TypedArray<Material> GLTFState::get_materials_bind() const {
  225. return GLTFTemplateConvert::to_array(materials);
  226. }
  227. void GLTFState::set_materials_bind(const TypedArray<Material> &p_materials) {
  228. GLTFTemplateConvert::set_from_array(materials, p_materials);
  229. }
  230. String GLTFState::get_scene_name() const {
  231. return scene_name;
  232. }
  233. void GLTFState::set_scene_name(const String &p_scene_name) {
  234. scene_name = p_scene_name;
  235. }
  236. PackedInt32Array GLTFState::get_root_nodes() const {
  237. return root_nodes;
  238. }
  239. void GLTFState::set_root_nodes(const PackedInt32Array &p_root_nodes) {
  240. root_nodes = PackedInt32Array(p_root_nodes);
  241. }
  242. TypedArray<GLTFTexture> GLTFState::get_textures_bind() const {
  243. return GLTFTemplateConvert::to_array(textures);
  244. }
  245. void GLTFState::set_textures_bind(const TypedArray<GLTFTexture> &p_textures) {
  246. GLTFTemplateConvert::set_from_array(textures, p_textures);
  247. }
  248. TypedArray<GLTFTextureSampler> GLTFState::get_texture_samplers_bind() const {
  249. return GLTFTemplateConvert::to_array(texture_samplers);
  250. }
  251. void GLTFState::set_texture_samplers_bind(const TypedArray<GLTFTextureSampler> &p_texture_samplers) {
  252. GLTFTemplateConvert::set_from_array(texture_samplers, p_texture_samplers);
  253. }
  254. TypedArray<Texture2D> GLTFState::get_images_bind() const {
  255. return GLTFTemplateConvert::to_array(images);
  256. }
  257. void GLTFState::set_images_bind(const TypedArray<Texture2D> &p_images) {
  258. GLTFTemplateConvert::set_from_array(images, p_images);
  259. }
  260. TypedArray<GLTFSkin> GLTFState::get_skins_bind() const {
  261. return GLTFTemplateConvert::to_array(skins);
  262. }
  263. void GLTFState::set_skins_bind(const TypedArray<GLTFSkin> &p_skins) {
  264. GLTFTemplateConvert::set_from_array(skins, p_skins);
  265. }
  266. TypedArray<GLTFCamera> GLTFState::get_cameras_bind() const {
  267. return GLTFTemplateConvert::to_array(cameras);
  268. }
  269. void GLTFState::set_cameras_bind(const TypedArray<GLTFCamera> &p_cameras) {
  270. GLTFTemplateConvert::set_from_array(cameras, p_cameras);
  271. }
  272. TypedArray<GLTFLight> GLTFState::get_lights_bind() const {
  273. return GLTFTemplateConvert::to_array(lights);
  274. }
  275. void GLTFState::set_lights_bind(const TypedArray<GLTFLight> &p_lights) {
  276. GLTFTemplateConvert::set_from_array(lights, p_lights);
  277. }
  278. TypedArray<String> GLTFState::get_unique_names_bind() const {
  279. return GLTFTemplateConvert::to_array(unique_names);
  280. }
  281. void GLTFState::set_unique_names_bind(const TypedArray<String> &p_unique_names) {
  282. GLTFTemplateConvert::set_from_array(unique_names, p_unique_names);
  283. }
  284. TypedArray<String> GLTFState::get_unique_animation_names_bind() const {
  285. return GLTFTemplateConvert::to_array(unique_animation_names);
  286. }
  287. void GLTFState::set_unique_animation_names_bind(const TypedArray<String> &p_unique_animation_names) {
  288. GLTFTemplateConvert::set_from_array(unique_animation_names, p_unique_animation_names);
  289. }
  290. TypedArray<GLTFSkeleton> GLTFState::get_skeletons_bind() const {
  291. return GLTFTemplateConvert::to_array(skeletons);
  292. }
  293. void GLTFState::set_skeletons_bind(const TypedArray<GLTFSkeleton> &p_skeletons) {
  294. GLTFTemplateConvert::set_from_array(skeletons, p_skeletons);
  295. }
  296. bool GLTFState::get_create_animations() const {
  297. return create_animations;
  298. }
  299. void GLTFState::set_create_animations(bool p_create_animations) {
  300. create_animations = p_create_animations;
  301. }
  302. bool GLTFState::get_import_as_skeleton_bones() const {
  303. return import_as_skeleton_bones;
  304. }
  305. void GLTFState::set_import_as_skeleton_bones(bool p_import_as_skeleton_bones) {
  306. import_as_skeleton_bones = p_import_as_skeleton_bones;
  307. }
  308. TypedArray<GLTFAnimation> GLTFState::get_animations_bind() const {
  309. return GLTFTemplateConvert::to_array(animations);
  310. }
  311. void GLTFState::set_animations_bind(const TypedArray<GLTFAnimation> &p_animations) {
  312. GLTFTemplateConvert::set_from_array(animations, p_animations);
  313. }
  314. Node *GLTFState::get_scene_node(GLTFNodeIndex p_gltf_node_index) const {
  315. if (!scene_nodes.has(p_gltf_node_index)) {
  316. return nullptr;
  317. }
  318. return scene_nodes[p_gltf_node_index];
  319. }
  320. GLTFNodeIndex GLTFState::get_node_index(Node *p_node) const {
  321. for (KeyValue<GLTFNodeIndex, Node *> x : scene_nodes) {
  322. if (x.value == p_node) {
  323. return x.key;
  324. }
  325. }
  326. return -1;
  327. }
  328. int GLTFState::get_animation_players_count(int p_anim_player_index) const {
  329. return animation_players.size();
  330. }
  331. AnimationPlayer *GLTFState::get_animation_player(int p_anim_player_index) const {
  332. ERR_FAIL_INDEX_V(p_anim_player_index, animation_players.size(), nullptr);
  333. return animation_players[p_anim_player_index];
  334. }
  335. void GLTFState::set_discard_meshes_and_materials(bool p_discard_meshes_and_materials) {
  336. discard_meshes_and_materials = p_discard_meshes_and_materials;
  337. }
  338. bool GLTFState::get_discard_meshes_and_materials() const {
  339. return discard_meshes_and_materials;
  340. }
  341. String GLTFState::get_base_path() const {
  342. return base_path;
  343. }
  344. void GLTFState::set_base_path(const String &p_base_path) {
  345. base_path = p_base_path;
  346. if (extract_path.is_empty()) {
  347. extract_path = p_base_path;
  348. }
  349. }
  350. String GLTFState::get_extract_path() const {
  351. return extract_path;
  352. }
  353. void GLTFState::set_extract_path(const String &p_extract_path) {
  354. extract_path = p_extract_path;
  355. }
  356. String GLTFState::get_extract_prefix() const {
  357. return extract_prefix;
  358. }
  359. void GLTFState::set_extract_prefix(const String &p_extract_prefix) {
  360. extract_prefix = p_extract_prefix;
  361. }
  362. String GLTFState::get_filename() const {
  363. return filename;
  364. }
  365. void GLTFState::set_filename(const String &p_filename) {
  366. filename = p_filename;
  367. if (extract_prefix.is_empty()) {
  368. extract_prefix = p_filename.get_basename();
  369. }
  370. }
  371. Variant GLTFState::get_additional_data(const StringName &p_extension_name) const {
  372. return additional_data[p_extension_name];
  373. }
  374. void GLTFState::set_additional_data(const StringName &p_extension_name, Variant p_additional_data) {
  375. additional_data[p_extension_name] = p_additional_data;
  376. }
  377. GLTFBufferViewIndex GLTFState::append_data_to_buffers(const Vector<uint8_t> &p_data, const bool p_deduplication = false) {
  378. if (p_deduplication) {
  379. for (int i = 0; i < buffer_views.size(); i++) {
  380. Ref<GLTFBufferView> buffer_view = buffer_views[i];
  381. Vector<uint8_t> buffer_view_data = buffer_view->load_buffer_view_data(this);
  382. if (buffer_view_data == p_data) {
  383. return i;
  384. }
  385. }
  386. }
  387. // Append the given data to a buffer and create a buffer view for it.
  388. if (unlikely(buffers.is_empty())) {
  389. buffers.push_back(Vector<uint8_t>());
  390. }
  391. Vector<uint8_t> &destination_buffer = buffers.write[0];
  392. Ref<GLTFBufferView> buffer_view;
  393. buffer_view.instantiate();
  394. buffer_view->set_buffer(0);
  395. buffer_view->set_byte_offset(destination_buffer.size());
  396. buffer_view->set_byte_length(p_data.size());
  397. destination_buffer.append_array(p_data);
  398. const int new_index = buffer_views.size();
  399. buffer_views.push_back(buffer_view);
  400. return new_index;
  401. }
  402. GLTFNodeIndex GLTFState::append_gltf_node(Ref<GLTFNode> p_gltf_node, Node *p_godot_scene_node, GLTFNodeIndex p_parent_node_index) {
  403. p_gltf_node->set_parent(p_parent_node_index);
  404. const GLTFNodeIndex new_index = nodes.size();
  405. nodes.append(p_gltf_node);
  406. scene_nodes.insert(new_index, p_godot_scene_node);
  407. if (p_parent_node_index == -1) {
  408. root_nodes.append(new_index);
  409. } else if (p_parent_node_index < new_index) {
  410. nodes.write[p_parent_node_index]->append_child_index(new_index);
  411. }
  412. return new_index;
  413. }