gltf_document.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**************************************************************************/
  2. /* gltf_document.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_DOCUMENT_H
  31. #define GLTF_DOCUMENT_H
  32. #include "extensions/gltf_document_extension.h"
  33. #include "gltf_defines.h"
  34. #include "structures/gltf_animation.h"
  35. #include "scene/3d/bone_attachment.h"
  36. #include "scene/3d/camera.h"
  37. #include "scene/3d/light.h"
  38. #include "scene/3d/mesh_instance.h"
  39. #include "scene/3d/skeleton.h"
  40. #include "scene/3d/spatial.h"
  41. #include "scene/animation/animation_player.h"
  42. #include "scene/resources/material.h"
  43. #include "scene/resources/texture.h"
  44. #include "modules/modules_enabled.gen.h" // For csg, gridmap.
  45. #ifdef MODULE_CSG_ENABLED
  46. class CSGShape;
  47. #endif // MODULE_CSG_ENABLED
  48. #ifdef MODULE_GRIDMAP_ENABLED
  49. class GridMap;
  50. #endif // MODULE_GRIDMAP_ENABLED
  51. class GLTFDocument : public Resource {
  52. GDCLASS(GLTFDocument, Resource);
  53. static Vector<Ref<GLTFDocumentExtension>> all_document_extensions;
  54. Vector<Ref<GLTFDocumentExtension>> document_extensions;
  55. private:
  56. const float BAKE_FPS = 30.0f;
  57. public:
  58. const int32_t JOINT_GROUP_SIZE = 4;
  59. enum {
  60. ARRAY_BUFFER = 34962,
  61. ELEMENT_ARRAY_BUFFER = 34963,
  62. TYPE_BYTE = 5120,
  63. TYPE_UNSIGNED_BYTE = 5121,
  64. TYPE_SHORT = 5122,
  65. TYPE_UNSIGNED_SHORT = 5123,
  66. TYPE_UNSIGNED_INT = 5125,
  67. TYPE_FLOAT = 5126,
  68. COMPONENT_TYPE_BYTE = 5120,
  69. COMPONENT_TYPE_UNSIGNED_BYTE = 5121,
  70. COMPONENT_TYPE_SHORT = 5122,
  71. COMPONENT_TYPE_UNSIGNED_SHORT = 5123,
  72. COMPONENT_TYPE_INT = 5125,
  73. COMPONENT_TYPE_FLOAT = 5126,
  74. };
  75. protected:
  76. static void _bind_methods();
  77. public:
  78. void _register_gltf_document_extension(Ref<GLTFDocumentExtension> p_extension, bool p_first_priority = false);
  79. void _unregister_gltf_document_extension(Ref<GLTFDocumentExtension> p_extension);
  80. void _unregister_all_gltf_document_extensions();
  81. static void register_gltf_document_extension(Ref<GLTFDocumentExtension> p_extension, bool p_first_priority = false);
  82. static void unregister_gltf_document_extension(Ref<GLTFDocumentExtension> p_extension);
  83. static void unregister_all_gltf_document_extensions();
  84. private:
  85. double _filter_number(double p_float);
  86. String _get_component_type_name(const uint32_t p_component);
  87. int _get_component_type_size(const int p_component_type);
  88. Error _parse_scenes(Ref<GLTFState> p_state);
  89. Error _parse_nodes(Ref<GLTFState> p_state);
  90. String _get_type_name(const GLTFType p_component);
  91. String _get_accessor_type_name(const GLTFType p_type);
  92. String _gen_unique_name(Ref<GLTFState> p_state, const String &p_name);
  93. String _sanitize_animation_name(const String &p_name);
  94. String _gen_unique_animation_name(Ref<GLTFState> p_state, const String &p_name);
  95. String _sanitize_bone_name(Ref<GLTFState> p_state, const String &p_name);
  96. String _gen_unique_bone_name(Ref<GLTFState> p_state,
  97. const GLTFSkeletonIndex p_skel_i,
  98. const String &p_name);
  99. GLTFTextureIndex _set_texture(Ref<GLTFState> p_state, Ref<Texture> p_texture);
  100. Ref<Texture> _get_texture(Ref<GLTFState> p_state,
  101. const GLTFTextureIndex p_texture);
  102. GLTFTextureSamplerIndex _set_sampler_for_mode(Ref<GLTFState> p_state,
  103. uint32_t p_mode);
  104. Ref<GLTFTextureSampler> _get_sampler_for_texture(Ref<GLTFState> p_state,
  105. const GLTFTextureIndex p_texture);
  106. Error _parse_json(const String &p_path, Ref<GLTFState> p_state);
  107. Error _parse_glb(const String &p_path, Ref<GLTFState> p_state);
  108. void _compute_node_heights(Ref<GLTFState> p_state);
  109. Error _parse_buffers(Ref<GLTFState> p_state, const String &p_base_path);
  110. Error _parse_buffer_views(Ref<GLTFState> p_state);
  111. GLTFType _get_type_from_str(const String &p_string);
  112. Error _parse_accessors(Ref<GLTFState> p_state);
  113. Error _decode_buffer_view(Ref<GLTFState> p_state, double *p_dst,
  114. const GLTFBufferViewIndex p_buffer_view,
  115. const int p_skip_every, const int p_skip_bytes,
  116. const int p_element_size, const int p_count,
  117. const GLTFType p_type, const int p_component_count,
  118. const int p_component_type, const int p_component_size,
  119. const bool p_normalized, const int p_byte_offset,
  120. const bool p_for_vertex);
  121. Vector<double> _decode_accessor(Ref<GLTFState> p_state,
  122. const GLTFAccessorIndex p_accessor,
  123. const bool p_for_vertex);
  124. Vector<float> _decode_accessor_as_floats(Ref<GLTFState> p_state,
  125. const GLTFAccessorIndex p_accessor,
  126. const bool p_for_vertex);
  127. Vector<int> _decode_accessor_as_ints(Ref<GLTFState> p_state,
  128. const GLTFAccessorIndex p_accessor,
  129. const bool p_for_vertex);
  130. Vector<Vector2> _decode_accessor_as_vec2(Ref<GLTFState> p_state,
  131. const GLTFAccessorIndex p_accessor,
  132. const bool p_for_vertex);
  133. Vector<Vector3> _decode_accessor_as_vec3(Ref<GLTFState> p_state,
  134. const GLTFAccessorIndex p_accessor,
  135. const bool p_for_vertex);
  136. Vector<Color> _decode_accessor_as_color(Ref<GLTFState> p_state,
  137. const GLTFAccessorIndex p_accessor,
  138. const bool p_for_vertex);
  139. Vector<Quat> _decode_accessor_as_quat(Ref<GLTFState> p_state,
  140. const GLTFAccessorIndex p_accessor,
  141. const bool p_for_vertex);
  142. Vector<Transform2D> _decode_accessor_as_xform2d(Ref<GLTFState> p_state,
  143. const GLTFAccessorIndex p_accessor,
  144. const bool p_for_vertex);
  145. Vector<Basis> _decode_accessor_as_basis(Ref<GLTFState> p_state,
  146. const GLTFAccessorIndex p_accessor,
  147. const bool p_for_vertex);
  148. Vector<Transform> _decode_accessor_as_xform(Ref<GLTFState> p_state,
  149. const GLTFAccessorIndex p_accessor,
  150. const bool p_for_vertex);
  151. Error _parse_meshes(Ref<GLTFState> p_state);
  152. Error _serialize_textures(Ref<GLTFState> p_state);
  153. Error _serialize_texture_samplers(Ref<GLTFState> p_state);
  154. Error _serialize_images(Ref<GLTFState> p_state, const String &p_path);
  155. Error _serialize_lights(Ref<GLTFState> p_state);
  156. Error _parse_images(Ref<GLTFState> p_state, const String &p_base_path);
  157. Error _parse_textures(Ref<GLTFState> p_state);
  158. Error _parse_texture_samplers(Ref<GLTFState> p_state);
  159. Error _parse_materials(Ref<GLTFState> p_state);
  160. void _set_texture_transform_uv1(const Dictionary &p_dict, Ref<Material3D> p_material);
  161. void spec_gloss_to_rough_metal(Ref<GLTFSpecGloss> r_spec_gloss,
  162. Ref<Material3D> p_material);
  163. static void spec_gloss_to_metal_base_color(const Color &p_specular_factor,
  164. const Color &p_diffuse,
  165. Color &r_base_color,
  166. float &r_metallic);
  167. GLTFNodeIndex _find_highest_node(Ref<GLTFState> p_state,
  168. const Vector<GLTFNodeIndex> &p_subset);
  169. bool _capture_nodes_in_skin(Ref<GLTFState> p_state, Ref<GLTFSkin> p_skin,
  170. const GLTFNodeIndex p_node_index);
  171. void _capture_nodes_for_multirooted_skin(Ref<GLTFState> p_state, Ref<GLTFSkin> p_skin);
  172. Error _expand_skin(Ref<GLTFState> p_state, Ref<GLTFSkin> p_skin);
  173. Error _verify_skin(Ref<GLTFState> p_state, Ref<GLTFSkin> p_skin);
  174. Error _parse_skins(Ref<GLTFState> p_state);
  175. Error _determine_skeletons(Ref<GLTFState> p_state);
  176. Error _reparent_non_joint_skeleton_subtrees(
  177. Ref<GLTFState> p_state, Ref<GLTFSkeleton> p_skeleton,
  178. const Vector<GLTFNodeIndex> &p_non_joints);
  179. Error _determine_skeleton_roots(Ref<GLTFState> p_state,
  180. const GLTFSkeletonIndex p_skel_i);
  181. Error _create_skeletons(Ref<GLTFState> p_state);
  182. Error _map_skin_joints_indices_to_skeleton_bone_indices(Ref<GLTFState> p_state);
  183. Error _serialize_skins(Ref<GLTFState> p_state);
  184. Error _create_skins(Ref<GLTFState> p_state);
  185. bool _skins_are_same(const Ref<Skin> p_skin_a, const Ref<Skin> p_skin_b);
  186. void _remove_duplicate_skins(Ref<GLTFState> p_state);
  187. Error _serialize_cameras(Ref<GLTFState> p_state);
  188. Error _parse_cameras(Ref<GLTFState> p_state);
  189. Error _parse_lights(Ref<GLTFState> p_state);
  190. Error _parse_animations(Ref<GLTFState> p_state);
  191. Error _serialize_animations(Ref<GLTFState> p_state);
  192. BoneAttachment *_generate_bone_attachment(Ref<GLTFState> p_state,
  193. Skeleton *p_skeleton,
  194. const GLTFNodeIndex p_node_index,
  195. const GLTFNodeIndex p_bone_index);
  196. Spatial *_generate_mesh_instance(Ref<GLTFState> p_state, Node *p_scene_parent, const GLTFNodeIndex p_node_index);
  197. Camera *_generate_camera(Ref<GLTFState> p_state, Node *p_scene_parent,
  198. const GLTFNodeIndex p_node_index);
  199. Spatial *_generate_light(Ref<GLTFState> p_state, Node *p_scene_parent, const GLTFNodeIndex p_node_index);
  200. Spatial *_generate_spatial(Ref<GLTFState> p_state, Node *p_scene_parent,
  201. const GLTFNodeIndex p_node_index);
  202. void _assign_scene_names(Ref<GLTFState> p_state);
  203. template <class T>
  204. T _interpolate_track(const Vector<float> &p_times, const Vector<T> &p_values,
  205. const float p_time,
  206. const GLTFAnimation::Interpolation p_interp);
  207. GLTFAccessorIndex _encode_accessor_as_quats(Ref<GLTFState> p_state,
  208. const Vector<Quat> p_attribs,
  209. const bool p_for_vertex);
  210. GLTFAccessorIndex _encode_accessor_as_weights(Ref<GLTFState> p_state,
  211. const Vector<Color> p_attribs,
  212. const bool p_for_vertex);
  213. GLTFAccessorIndex _encode_accessor_as_joints(Ref<GLTFState> p_state,
  214. const Vector<Color> p_attribs,
  215. const bool p_for_vertex);
  216. GLTFAccessorIndex _encode_accessor_as_floats(Ref<GLTFState> p_state,
  217. const Vector<real_t> p_attribs,
  218. const bool p_for_vertex);
  219. GLTFAccessorIndex _encode_accessor_as_vec2(Ref<GLTFState> p_state,
  220. const Vector<Vector2> p_attribs,
  221. const bool p_for_vertex);
  222. void _calc_accessor_vec2_min_max(int p_i, const int p_element_count, Vector<double> &p_type_max, Vector2 p_attribs, Vector<double> &p_type_min) {
  223. if (p_i == 0) {
  224. for (int32_t type_i = 0; type_i < p_element_count; type_i++) {
  225. p_type_max.write[type_i] = p_attribs[(p_i * p_element_count) + type_i];
  226. p_type_min.write[type_i] = p_attribs[(p_i * p_element_count) + type_i];
  227. }
  228. }
  229. for (int32_t type_i = 0; type_i < p_element_count; type_i++) {
  230. p_type_max.write[type_i] = MAX(p_attribs[(p_i * p_element_count) + type_i], p_type_max[type_i]);
  231. p_type_min.write[type_i] = MIN(p_attribs[(p_i * p_element_count) + type_i], p_type_min[type_i]);
  232. p_type_max.write[type_i] = _filter_number(p_type_max.write[type_i]);
  233. p_type_min.write[type_i] = _filter_number(p_type_min.write[type_i]);
  234. }
  235. }
  236. GLTFAccessorIndex _encode_accessor_as_vec3(Ref<GLTFState> p_state,
  237. const Vector<Vector3> p_attribs,
  238. const bool p_for_vertex);
  239. GLTFAccessorIndex _encode_accessor_as_color(Ref<GLTFState> p_state,
  240. const Vector<Color> p_attribs,
  241. const bool p_for_vertex);
  242. void _calc_accessor_min_max(int p_i, const int p_element_count, Vector<double> &p_type_max, Vector<double> p_attribs, Vector<double> &p_type_min);
  243. GLTFAccessorIndex _encode_accessor_as_ints(Ref<GLTFState> p_state,
  244. const Vector<int32_t> p_attribs,
  245. const bool p_for_vertex);
  246. GLTFAccessorIndex _encode_accessor_as_xform(Ref<GLTFState> p_state,
  247. const Vector<Transform> p_attribs,
  248. const bool p_for_vertex);
  249. Error _encode_buffer_view(Ref<GLTFState> p_state, const double *p_src,
  250. const int p_count, const GLTFType p_type,
  251. const int p_component_type, const bool p_normalized,
  252. const int p_byte_offset, const bool p_for_vertex,
  253. GLTFBufferViewIndex &r_accessor);
  254. Error _encode_accessors(Ref<GLTFState> p_state);
  255. Error _encode_buffer_views(Ref<GLTFState> p_state);
  256. Error _serialize_materials(Ref<GLTFState> p_state);
  257. Error _serialize_meshes(Ref<GLTFState> p_state);
  258. Error _serialize_nodes(Ref<GLTFState> p_state);
  259. Error _serialize_scenes(Ref<GLTFState> p_state);
  260. String interpolation_to_string(const GLTFAnimation::Interpolation p_interp);
  261. GLTFAnimation::Track _convert_animation_track(Ref<GLTFState> p_state,
  262. GLTFAnimation::Track p_track,
  263. Ref<Animation> p_animation, Transform p_bone_rest,
  264. int32_t p_track_i,
  265. GLTFNodeIndex p_node_i);
  266. Error _encode_buffer_bins(Ref<GLTFState> p_state, const String &p_path);
  267. Error _encode_buffer_glb(Ref<GLTFState> p_state, const String &p_path);
  268. Dictionary _serialize_texture_transform_uv1(Ref<Material3D> p_material);
  269. Dictionary _serialize_texture_transform_uv2(Ref<Material3D> p_material);
  270. Error _serialize_version(Ref<GLTFState> p_state);
  271. Error _serialize_file(Ref<GLTFState> p_state, const String p_path);
  272. Error _serialize_gltf_extensions(Ref<GLTFState> p_state) const;
  273. public:
  274. // http://www.itu.int/rec/R-REC-BT.601
  275. // http://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.601-7-201103-I!!PDF-E.pdf
  276. static constexpr float R_BRIGHTNESS_COEFF = 0.299f;
  277. static constexpr float G_BRIGHTNESS_COEFF = 0.587f;
  278. static constexpr float B_BRIGHTNESS_COEFF = 0.114f;
  279. private:
  280. // https://github.com/microsoft/glTF-SDK/blob/master/GLTFSDK/Source/PBRUtils.cpp#L9
  281. // https://bghgary.github.io/glTF/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
  282. static float solve_metallic(float p_dielectric_specular, float p_diffuse,
  283. float p_specular,
  284. float p_one_minus_specular_strength);
  285. static float get_perceived_brightness(const Color p_color);
  286. static float get_max_component(const Color &p_color);
  287. public:
  288. void extension_generate_scene(Ref<GLTFState> p_state);
  289. String _sanitize_scene_name(Ref<GLTFState> p_state, const String &p_name);
  290. String _legacy_validate_node_name(const String &p_name);
  291. Error _parse_gltf_extensions(Ref<GLTFState> p_state);
  292. void _process_mesh_instances(Ref<GLTFState> p_state, Node *p_scene_root);
  293. void _generate_scene_node(Ref<GLTFState> p_state, Node *p_scene_parent,
  294. Spatial *p_scene_root,
  295. const GLTFNodeIndex p_node_index);
  296. void _generate_skeleton_bone_node(Ref<GLTFState> p_state, Node *p_scene_parent, Spatial *p_scene_root, const GLTFNodeIndex p_node_index);
  297. void _import_animation(Ref<GLTFState> p_state, AnimationPlayer *p_animation_player,
  298. const GLTFAnimationIndex p_index, const int p_bake_fps);
  299. void _convert_mesh_instances(Ref<GLTFState> p_state);
  300. GLTFCameraIndex _convert_camera(Ref<GLTFState> p_state, Camera *p_camera);
  301. void _convert_light_to_gltf(Light *p_light, Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node);
  302. GLTFLightIndex _convert_light(Ref<GLTFState> p_state, Light *p_light);
  303. void _convert_spatial(Ref<GLTFState> p_state, Spatial *p_spatial, Ref<GLTFNode> p_node);
  304. void _convert_scene_node(Ref<GLTFState> p_state, Node *p_current,
  305. const GLTFNodeIndex p_gltf_current,
  306. const GLTFNodeIndex p_gltf_root);
  307. #ifdef MODULE_CSG_ENABLED
  308. void _convert_csg_shape_to_gltf(CSGShape *p_current, GLTFNodeIndex p_gltf_parent, Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
  309. #endif // MODULE_CSG_ENABLED
  310. void _create_gltf_node(Ref<GLTFState> p_state,
  311. Node *p_scene_parent,
  312. GLTFNodeIndex p_current_node_i,
  313. GLTFNodeIndex p_parent_node_index,
  314. GLTFNodeIndex p_root_gltf_node,
  315. Ref<GLTFNode> p_gltf_node);
  316. void _convert_animation_player_to_gltf(
  317. AnimationPlayer *p_animation_player, Ref<GLTFState> p_state,
  318. GLTFNodeIndex p_gltf_current,
  319. GLTFNodeIndex p_gltf_root_index,
  320. Ref<GLTFNode> p_gltf_node, Node *p_scene_parent);
  321. void _check_visibility(Node *p_node, bool &r_retflag);
  322. void _convert_camera_to_gltf(Camera *p_camera, Ref<GLTFState> p_state,
  323. Ref<GLTFNode> p_gltf_node);
  324. #ifdef MODULE_GRIDMAP_ENABLED
  325. void _convert_grid_map_to_gltf(
  326. GridMap *p_grid_map,
  327. GLTFNodeIndex p_parent_node_index,
  328. GLTFNodeIndex p_root_node_index,
  329. Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
  330. #endif // MODULE_GRIDMAP_ENABLED
  331. void _convert_multi_mesh_instance_to_gltf(
  332. MultiMeshInstance *p_scene_parent,
  333. GLTFNodeIndex p_parent_node_index,
  334. GLTFNodeIndex p_root_node_index,
  335. Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
  336. void _convert_skeleton_to_gltf(
  337. Skeleton *p_scene_parent, Ref<GLTFState> p_state,
  338. GLTFNodeIndex p_parent_node_index,
  339. GLTFNodeIndex p_root_node_index,
  340. Ref<GLTFNode> p_gltf_node);
  341. void _convert_bone_attachment_to_gltf(BoneAttachment *p_bone_attachment,
  342. Ref<GLTFState> p_state,
  343. GLTFNodeIndex p_parent_node_index,
  344. GLTFNodeIndex p_root_node_index,
  345. Ref<GLTFNode> p_gltf_node);
  346. void _convert_mesh_instance_to_gltf(MeshInstance *p_mesh_instance,
  347. Ref<GLTFState> p_state,
  348. Ref<GLTFNode> p_gltf_node);
  349. GLTFMeshIndex _convert_mesh_to_gltf(Ref<GLTFState> p_state,
  350. MeshInstance *p_mesh_instance);
  351. void _convert_animation(Ref<GLTFState> p_state, AnimationPlayer *p_animation_player,
  352. String p_animation_track_name);
  353. Error serialize(Ref<GLTFState> p_state, Node *p_root, const String &p_path);
  354. Error parse(Ref<GLTFState> p_state, String p_paths, bool p_read_binary = false);
  355. };
  356. #endif // GLTF_DOCUMENT_H