gltf_state.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**************************************************************************/
  2. /* gltf_state.h */
  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. #ifndef GLTF_STATE_H
  31. #define GLTF_STATE_H
  32. #include "extensions/gltf_light.h"
  33. #include "structures/gltf_accessor.h"
  34. #include "structures/gltf_animation.h"
  35. #include "structures/gltf_buffer_view.h"
  36. #include "structures/gltf_camera.h"
  37. #include "structures/gltf_mesh.h"
  38. #include "structures/gltf_node.h"
  39. #include "structures/gltf_object_model_property.h"
  40. #include "structures/gltf_skeleton.h"
  41. #include "structures/gltf_skin.h"
  42. #include "structures/gltf_texture.h"
  43. #include "structures/gltf_texture_sampler.h"
  44. #include "scene/3d/importer_mesh_instance_3d.h"
  45. #include "scene/animation/animation_player.h"
  46. class GLTFState : public Resource {
  47. GDCLASS(GLTFState, Resource);
  48. friend class GLTFDocument;
  49. friend class GLTFNode;
  50. protected:
  51. String base_path;
  52. String extract_path;
  53. String extract_prefix;
  54. String filename;
  55. Dictionary json;
  56. int major_version = 0;
  57. int minor_version = 0;
  58. String copyright;
  59. Vector<uint8_t> glb_data;
  60. double bake_fps = 30.0;
  61. bool use_named_skin_binds = false;
  62. bool use_khr_texture_transform = false;
  63. bool discard_meshes_and_materials = false;
  64. bool force_generate_tangents = false;
  65. bool create_animations = true;
  66. bool force_disable_compression = false;
  67. bool import_as_skeleton_bones = false;
  68. int handle_binary_image = HANDLE_BINARY_EXTRACT_TEXTURES;
  69. Vector<Ref<GLTFNode>> nodes;
  70. Vector<Vector<uint8_t>> buffers;
  71. Vector<Ref<GLTFBufferView>> buffer_views;
  72. Vector<Ref<GLTFAccessor>> accessors;
  73. Vector<Ref<GLTFMesh>> meshes; // Meshes are loaded directly, no reason not to.
  74. Vector<AnimationPlayer *> animation_players;
  75. HashMap<Ref<Material>, GLTFMaterialIndex> material_cache;
  76. Vector<Ref<Material>> materials;
  77. String scene_name;
  78. Vector<int> root_nodes;
  79. Vector<Ref<GLTFTexture>> textures;
  80. Vector<Ref<GLTFTextureSampler>> texture_samplers;
  81. Ref<GLTFTextureSampler> default_texture_sampler;
  82. Vector<Ref<Texture2D>> images;
  83. Vector<String> extensions_used;
  84. Vector<String> extensions_required;
  85. Vector<Ref<Image>> source_images;
  86. Vector<Ref<GLTFSkin>> skins;
  87. Vector<Ref<GLTFCamera>> cameras;
  88. Vector<Ref<GLTFLight>> lights;
  89. HashSet<String> unique_names;
  90. HashSet<String> unique_animation_names;
  91. Vector<Ref<GLTFSkeleton>> skeletons;
  92. Vector<Ref<GLTFAnimation>> animations;
  93. HashMap<GLTFNodeIndex, Node *> scene_nodes;
  94. HashMap<GLTFNodeIndex, ImporterMeshInstance3D *> scene_mesh_instances;
  95. HashMap<String, Ref<GLTFObjectModelProperty>> object_model_properties;
  96. HashMap<ObjectID, GLTFSkeletonIndex> skeleton3d_to_gltf_skeleton;
  97. HashMap<ObjectID, HashMap<ObjectID, GLTFSkinIndex>> skin_and_skeleton3d_to_gltf_skin;
  98. Dictionary additional_data;
  99. protected:
  100. static void _bind_methods();
  101. public:
  102. double get_bake_fps() const {
  103. return bake_fps;
  104. }
  105. void set_bake_fps(double value) {
  106. bake_fps = value;
  107. }
  108. void add_used_extension(const String &p_extension, bool p_required = false);
  109. GLTFBufferViewIndex append_data_to_buffers(const Vector<uint8_t> &p_data, const bool p_deduplication);
  110. GLTFNodeIndex append_gltf_node(Ref<GLTFNode> p_gltf_node, Node *p_godot_scene_node, GLTFNodeIndex p_parent_node_index);
  111. enum GLTFHandleBinary {
  112. HANDLE_BINARY_DISCARD_TEXTURES = 0,
  113. HANDLE_BINARY_EXTRACT_TEXTURES,
  114. HANDLE_BINARY_EMBED_AS_BASISU,
  115. HANDLE_BINARY_EMBED_AS_UNCOMPRESSED, // If this value changes from 3, ResourceImporterScene::pre_import must be changed as well.
  116. };
  117. int32_t get_handle_binary_image() {
  118. return handle_binary_image;
  119. }
  120. void set_handle_binary_image(int32_t p_handle_binary_image) {
  121. handle_binary_image = p_handle_binary_image;
  122. }
  123. Dictionary get_json();
  124. void set_json(Dictionary p_json);
  125. int get_major_version();
  126. void set_major_version(int p_major_version);
  127. int get_minor_version();
  128. void set_minor_version(int p_minor_version);
  129. String get_copyright() const;
  130. void set_copyright(const String &p_copyright);
  131. Vector<uint8_t> get_glb_data();
  132. void set_glb_data(Vector<uint8_t> p_glb_data);
  133. bool get_use_named_skin_binds();
  134. void set_use_named_skin_binds(bool p_use_named_skin_binds);
  135. bool get_discard_textures();
  136. void set_discard_textures(bool p_discard_textures);
  137. bool get_embed_as_basisu();
  138. void set_embed_as_basisu(bool p_embed_as_basisu);
  139. bool get_extract_textures();
  140. void set_extract_textures(bool p_extract_textures);
  141. bool get_discard_meshes_and_materials();
  142. void set_discard_meshes_and_materials(bool p_discard_meshes_and_materials);
  143. TypedArray<GLTFNode> get_nodes();
  144. void set_nodes(TypedArray<GLTFNode> p_nodes);
  145. TypedArray<PackedByteArray> get_buffers();
  146. void set_buffers(TypedArray<PackedByteArray> p_buffers);
  147. TypedArray<GLTFBufferView> get_buffer_views();
  148. void set_buffer_views(TypedArray<GLTFBufferView> p_buffer_views);
  149. TypedArray<GLTFAccessor> get_accessors();
  150. void set_accessors(TypedArray<GLTFAccessor> p_accessors);
  151. TypedArray<GLTFMesh> get_meshes();
  152. void set_meshes(TypedArray<GLTFMesh> p_meshes);
  153. TypedArray<Material> get_materials();
  154. void set_materials(TypedArray<Material> p_materials);
  155. String get_scene_name();
  156. void set_scene_name(String p_scene_name);
  157. String get_base_path();
  158. void set_base_path(const String &p_base_path);
  159. String get_extract_path();
  160. void set_extract_path(const String &p_extract_path);
  161. String get_extract_prefix();
  162. void set_extract_prefix(const String &p_extract_prefix);
  163. String get_filename() const;
  164. void set_filename(const String &p_filename);
  165. PackedInt32Array get_root_nodes();
  166. void set_root_nodes(PackedInt32Array p_root_nodes);
  167. TypedArray<GLTFTexture> get_textures();
  168. void set_textures(TypedArray<GLTFTexture> p_textures);
  169. TypedArray<GLTFTextureSampler> get_texture_samplers();
  170. void set_texture_samplers(TypedArray<GLTFTextureSampler> p_texture_samplers);
  171. TypedArray<Texture2D> get_images();
  172. void set_images(TypedArray<Texture2D> p_images);
  173. TypedArray<GLTFSkin> get_skins();
  174. void set_skins(TypedArray<GLTFSkin> p_skins);
  175. TypedArray<GLTFCamera> get_cameras();
  176. void set_cameras(TypedArray<GLTFCamera> p_cameras);
  177. TypedArray<GLTFLight> get_lights();
  178. void set_lights(TypedArray<GLTFLight> p_lights);
  179. TypedArray<String> get_unique_names();
  180. void set_unique_names(TypedArray<String> p_unique_names);
  181. TypedArray<String> get_unique_animation_names();
  182. void set_unique_animation_names(TypedArray<String> p_unique_names);
  183. TypedArray<GLTFSkeleton> get_skeletons();
  184. void set_skeletons(TypedArray<GLTFSkeleton> p_skeletons);
  185. bool get_create_animations();
  186. void set_create_animations(bool p_create_animations);
  187. bool get_import_as_skeleton_bones();
  188. void set_import_as_skeleton_bones(bool p_import_as_skeleton_bones);
  189. TypedArray<GLTFAnimation> get_animations();
  190. void set_animations(TypedArray<GLTFAnimation> p_animations);
  191. Node *get_scene_node(GLTFNodeIndex idx);
  192. GLTFNodeIndex get_node_index(Node *p_node);
  193. int get_animation_players_count(int idx);
  194. AnimationPlayer *get_animation_player(int idx);
  195. Variant get_additional_data(const StringName &p_extension_name);
  196. void set_additional_data(const StringName &p_extension_name, Variant p_additional_data);
  197. };
  198. #endif // GLTF_STATE_H