gltf_document.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*************************************************************************/
  2. /* gltf_document.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "gltf_defines.h"
  33. #include "structures/gltf_animation.h"
  34. #include "scene/3d/bone_attachment_3d.h"
  35. #include "scene/3d/importer_mesh_instance_3d.h"
  36. #include "scene/3d/mesh_instance_3d.h"
  37. #include "scene/animation/animation_player.h"
  38. #include "scene/resources/material.h"
  39. #include "modules/modules_enabled.gen.h" // For csg, gridmap.
  40. class GLTFDocument : public Resource {
  41. GDCLASS(GLTFDocument, Resource);
  42. TypedArray<GLTFDocumentExtension> document_extensions;
  43. private:
  44. const float BAKE_FPS = 30.0f;
  45. public:
  46. GLTFDocument();
  47. const int32_t JOINT_GROUP_SIZE = 4;
  48. enum {
  49. ARRAY_BUFFER = 34962,
  50. ELEMENT_ARRAY_BUFFER = 34963,
  51. TYPE_BYTE = 5120,
  52. TYPE_UNSIGNED_BYTE = 5121,
  53. TYPE_SHORT = 5122,
  54. TYPE_UNSIGNED_SHORT = 5123,
  55. TYPE_UNSIGNED_INT = 5125,
  56. TYPE_FLOAT = 5126,
  57. COMPONENT_TYPE_BYTE = 5120,
  58. COMPONENT_TYPE_UNSIGNED_BYTE = 5121,
  59. COMPONENT_TYPE_SHORT = 5122,
  60. COMPONENT_TYPE_UNSIGNED_SHORT = 5123,
  61. COMPONENT_TYPE_INT = 5125,
  62. COMPONENT_TYPE_FLOAT = 5126,
  63. };
  64. protected:
  65. static void _bind_methods();
  66. public:
  67. void set_extensions(TypedArray<GLTFDocumentExtension> p_extensions);
  68. TypedArray<GLTFDocumentExtension> get_extensions() const;
  69. private:
  70. void _build_parent_hierachy(Ref<GLTFState> state);
  71. double _filter_number(double p_float);
  72. String _get_component_type_name(const uint32_t p_component);
  73. int _get_component_type_size(const int component_type);
  74. Error _parse_scenes(Ref<GLTFState> state);
  75. Error _parse_nodes(Ref<GLTFState> state);
  76. String _get_type_name(const GLTFType p_component);
  77. String _get_accessor_type_name(const GLTFType p_type);
  78. String _gen_unique_name(Ref<GLTFState> state, const String &p_name);
  79. String _sanitize_animation_name(const String &name);
  80. String _gen_unique_animation_name(Ref<GLTFState> state, const String &p_name);
  81. String _sanitize_bone_name(const String &name);
  82. String _gen_unique_bone_name(Ref<GLTFState> state,
  83. const GLTFSkeletonIndex skel_i,
  84. const String &p_name);
  85. GLTFTextureIndex _set_texture(Ref<GLTFState> state, Ref<Texture2D> p_texture);
  86. Ref<Texture2D> _get_texture(Ref<GLTFState> state,
  87. const GLTFTextureIndex p_texture);
  88. Error _parse_json(const String &p_path, Ref<GLTFState> state);
  89. Error _parse_glb(Ref<FileAccess> f, Ref<GLTFState> state);
  90. void _compute_node_heights(Ref<GLTFState> state);
  91. Error _parse_buffers(Ref<GLTFState> state, const String &p_base_path);
  92. Error _parse_buffer_views(Ref<GLTFState> state);
  93. GLTFType _get_type_from_str(const String &p_string);
  94. Error _parse_accessors(Ref<GLTFState> state);
  95. Error _decode_buffer_view(Ref<GLTFState> state, double *dst,
  96. const GLTFBufferViewIndex p_buffer_view,
  97. const int skip_every, const int skip_bytes,
  98. const int element_size, const int count,
  99. const GLTFType type, const int component_count,
  100. const int component_type, const int component_size,
  101. const bool normalized, const int byte_offset,
  102. const bool for_vertex);
  103. Vector<double> _decode_accessor(Ref<GLTFState> state,
  104. const GLTFAccessorIndex p_accessor,
  105. const bool p_for_vertex);
  106. Vector<float> _decode_accessor_as_floats(Ref<GLTFState> state,
  107. const GLTFAccessorIndex p_accessor,
  108. const bool p_for_vertex);
  109. Vector<int> _decode_accessor_as_ints(Ref<GLTFState> state,
  110. const GLTFAccessorIndex p_accessor,
  111. const bool p_for_vertex);
  112. Vector<Vector2> _decode_accessor_as_vec2(Ref<GLTFState> state,
  113. const GLTFAccessorIndex p_accessor,
  114. const bool p_for_vertex);
  115. Vector<Vector3> _decode_accessor_as_vec3(Ref<GLTFState> state,
  116. const GLTFAccessorIndex p_accessor,
  117. const bool p_for_vertex);
  118. Vector<Color> _decode_accessor_as_color(Ref<GLTFState> state,
  119. const GLTFAccessorIndex p_accessor,
  120. const bool p_for_vertex);
  121. Vector<Quaternion> _decode_accessor_as_quaternion(Ref<GLTFState> state,
  122. const GLTFAccessorIndex p_accessor,
  123. const bool p_for_vertex);
  124. Vector<Transform2D> _decode_accessor_as_xform2d(Ref<GLTFState> state,
  125. const GLTFAccessorIndex p_accessor,
  126. const bool p_for_vertex);
  127. Vector<Basis> _decode_accessor_as_basis(Ref<GLTFState> state,
  128. const GLTFAccessorIndex p_accessor,
  129. const bool p_for_vertex);
  130. Vector<Transform3D> _decode_accessor_as_xform(Ref<GLTFState> state,
  131. const GLTFAccessorIndex p_accessor,
  132. const bool p_for_vertex);
  133. Error _parse_meshes(Ref<GLTFState> state);
  134. Error _serialize_textures(Ref<GLTFState> state);
  135. Error _serialize_images(Ref<GLTFState> state, const String &p_path);
  136. Error _serialize_lights(Ref<GLTFState> state);
  137. Error _parse_images(Ref<GLTFState> state, const String &p_base_path);
  138. Error _parse_textures(Ref<GLTFState> state);
  139. Error _parse_materials(Ref<GLTFState> state);
  140. void _set_texture_transform_uv1(const Dictionary &d, Ref<BaseMaterial3D> material);
  141. void spec_gloss_to_rough_metal(Ref<GLTFSpecGloss> r_spec_gloss,
  142. Ref<BaseMaterial3D> p_material);
  143. static void spec_gloss_to_metal_base_color(const Color &p_specular_factor,
  144. const Color &p_diffuse,
  145. Color &r_base_color,
  146. float &r_metallic);
  147. GLTFNodeIndex _find_highest_node(Ref<GLTFState> state,
  148. const Vector<GLTFNodeIndex> &subset);
  149. bool _capture_nodes_in_skin(Ref<GLTFState> state, Ref<GLTFSkin> skin,
  150. const GLTFNodeIndex node_index);
  151. void _capture_nodes_for_multirooted_skin(Ref<GLTFState> state, Ref<GLTFSkin> skin);
  152. Error _expand_skin(Ref<GLTFState> state, Ref<GLTFSkin> skin);
  153. Error _verify_skin(Ref<GLTFState> state, Ref<GLTFSkin> skin);
  154. Error _parse_skins(Ref<GLTFState> state);
  155. Error _determine_skeletons(Ref<GLTFState> state);
  156. Error _reparent_non_joint_skeleton_subtrees(
  157. Ref<GLTFState> state, Ref<GLTFSkeleton> skeleton,
  158. const Vector<GLTFNodeIndex> &non_joints);
  159. Error _determine_skeleton_roots(Ref<GLTFState> state,
  160. const GLTFSkeletonIndex skel_i);
  161. Error _create_skeletons(Ref<GLTFState> state);
  162. Error _map_skin_joints_indices_to_skeleton_bone_indices(Ref<GLTFState> state);
  163. Error _serialize_skins(Ref<GLTFState> state);
  164. Error _create_skins(Ref<GLTFState> state);
  165. bool _skins_are_same(const Ref<Skin> skin_a, const Ref<Skin> skin_b);
  166. void _remove_duplicate_skins(Ref<GLTFState> state);
  167. Error _serialize_cameras(Ref<GLTFState> state);
  168. Error _parse_cameras(Ref<GLTFState> state);
  169. Error _parse_lights(Ref<GLTFState> state);
  170. Error _parse_animations(Ref<GLTFState> state);
  171. Error _serialize_animations(Ref<GLTFState> state);
  172. BoneAttachment3D *_generate_bone_attachment(Ref<GLTFState> state,
  173. Skeleton3D *skeleton,
  174. const GLTFNodeIndex node_index,
  175. const GLTFNodeIndex bone_index);
  176. ImporterMeshInstance3D *_generate_mesh_instance(Ref<GLTFState> state, const GLTFNodeIndex node_index);
  177. Camera3D *_generate_camera(Ref<GLTFState> state, const GLTFNodeIndex node_index);
  178. Node3D *_generate_light(Ref<GLTFState> state, const GLTFNodeIndex node_index);
  179. Node3D *_generate_spatial(Ref<GLTFState> state, const GLTFNodeIndex node_index);
  180. void _assign_scene_names(Ref<GLTFState> state);
  181. template <class T>
  182. T _interpolate_track(const Vector<real_t> &p_times, const Vector<T> &p_values,
  183. const float p_time,
  184. const GLTFAnimation::Interpolation p_interp);
  185. GLTFAccessorIndex _encode_accessor_as_quaternions(Ref<GLTFState> state,
  186. const Vector<Quaternion> p_attribs,
  187. const bool p_for_vertex);
  188. GLTFAccessorIndex _encode_accessor_as_weights(Ref<GLTFState> state,
  189. const Vector<Color> p_attribs,
  190. const bool p_for_vertex);
  191. GLTFAccessorIndex _encode_accessor_as_joints(Ref<GLTFState> state,
  192. const Vector<Color> p_attribs,
  193. const bool p_for_vertex);
  194. GLTFAccessorIndex _encode_accessor_as_floats(Ref<GLTFState> state,
  195. const Vector<real_t> p_attribs,
  196. const bool p_for_vertex);
  197. GLTFAccessorIndex _encode_accessor_as_vec2(Ref<GLTFState> state,
  198. const Vector<Vector2> p_attribs,
  199. const bool p_for_vertex);
  200. void _calc_accessor_vec2_min_max(int i, const int element_count, Vector<double> &type_max, Vector2 attribs, Vector<double> &type_min) {
  201. if (i == 0) {
  202. for (int32_t type_i = 0; type_i < element_count; type_i++) {
  203. type_max.write[type_i] = attribs[(i * element_count) + type_i];
  204. type_min.write[type_i] = attribs[(i * element_count) + type_i];
  205. }
  206. }
  207. for (int32_t type_i = 0; type_i < element_count; type_i++) {
  208. type_max.write[type_i] = MAX(attribs[(i * element_count) + type_i], type_max[type_i]);
  209. type_min.write[type_i] = MIN(attribs[(i * element_count) + type_i], type_min[type_i]);
  210. type_max.write[type_i] = _filter_number(type_max.write[type_i]);
  211. type_min.write[type_i] = _filter_number(type_min.write[type_i]);
  212. }
  213. }
  214. GLTFAccessorIndex _encode_accessor_as_vec3(Ref<GLTFState> state,
  215. const Vector<Vector3> p_attribs,
  216. const bool p_for_vertex);
  217. GLTFAccessorIndex _encode_accessor_as_color(Ref<GLTFState> state,
  218. const Vector<Color> p_attribs,
  219. const bool p_for_vertex);
  220. 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);
  221. GLTFAccessorIndex _encode_accessor_as_ints(Ref<GLTFState> state,
  222. const Vector<int32_t> p_attribs,
  223. const bool p_for_vertex);
  224. GLTFAccessorIndex _encode_accessor_as_xform(Ref<GLTFState> state,
  225. const Vector<Transform3D> p_attribs,
  226. const bool p_for_vertex);
  227. Error _encode_buffer_view(Ref<GLTFState> state, const double *src,
  228. const int count, const GLTFType type,
  229. const int component_type, const bool normalized,
  230. const int byte_offset, const bool for_vertex,
  231. GLTFBufferViewIndex &r_accessor);
  232. Error _encode_accessors(Ref<GLTFState> state);
  233. Error _encode_buffer_views(Ref<GLTFState> state);
  234. Error _serialize_materials(Ref<GLTFState> state);
  235. Error _serialize_meshes(Ref<GLTFState> state);
  236. Error _serialize_nodes(Ref<GLTFState> state);
  237. Error _serialize_scenes(Ref<GLTFState> state);
  238. String interpolation_to_string(const GLTFAnimation::Interpolation p_interp);
  239. GLTFAnimation::Track _convert_animation_track(Ref<GLTFState> state,
  240. GLTFAnimation::Track p_track,
  241. Ref<Animation> p_animation,
  242. int32_t p_track_i,
  243. GLTFNodeIndex p_node_i);
  244. Error _encode_buffer_bins(Ref<GLTFState> state, const String &p_path);
  245. Error _encode_buffer_glb(Ref<GLTFState> state, const String &p_path);
  246. PackedByteArray _serialize_glb_buffer(Ref<GLTFState> state, Error *r_err);
  247. Dictionary _serialize_texture_transform_uv1(Ref<BaseMaterial3D> p_material);
  248. Dictionary _serialize_texture_transform_uv2(Ref<BaseMaterial3D> p_material);
  249. Error _serialize_version(Ref<GLTFState> state);
  250. Error _serialize_file(Ref<GLTFState> state, const String p_path);
  251. Error _serialize_extensions(Ref<GLTFState> state) const;
  252. public:
  253. // https://www.itu.int/rec/R-REC-BT.601
  254. // https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.601-7-201103-I!!PDF-E.pdf
  255. static constexpr float R_BRIGHTNESS_COEFF = 0.299f;
  256. static constexpr float G_BRIGHTNESS_COEFF = 0.587f;
  257. static constexpr float B_BRIGHTNESS_COEFF = 0.114f;
  258. private:
  259. // https://github.com/microsoft/glTF-SDK/blob/master/GLTFSDK/Source/PBRUtils.cpp#L9
  260. // https://bghgary.github.io/glTF/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
  261. static float solve_metallic(float p_dielectric_specular, float diffuse,
  262. float specular,
  263. float p_one_minus_specular_strength);
  264. static float get_perceived_brightness(const Color p_color);
  265. static float get_max_component(const Color &p_color);
  266. public:
  267. Error append_from_file(String p_path, Ref<GLTFState> r_state, uint32_t p_flags = 0, int32_t p_bake_fps = 30, String p_base_path = String());
  268. Error append_from_buffer(PackedByteArray p_bytes, String p_base_path, Ref<GLTFState> r_state, uint32_t p_flags = 0, int32_t p_bake_fps = 30);
  269. Error append_from_scene(Node *p_node, Ref<GLTFState> r_state, uint32_t p_flags = 0, int32_t p_bake_fps = 30);
  270. public:
  271. Node *generate_scene(Ref<GLTFState> state, int32_t p_bake_fps = 30.0f);
  272. PackedByteArray generate_buffer(Ref<GLTFState> state);
  273. Error write_to_filesystem(Ref<GLTFState> state, const String &p_path);
  274. public:
  275. Error _parse_gltf_state(Ref<GLTFState> state, const String &p_search_path, float p_bake_fps);
  276. Error _parse_gltf_extensions(Ref<GLTFState> state);
  277. void _process_mesh_instances(Ref<GLTFState> state, Node *scene_root);
  278. void _generate_scene_node(Ref<GLTFState> state, Node *scene_parent,
  279. Node3D *scene_root,
  280. const GLTFNodeIndex node_index);
  281. void _generate_skeleton_bone_node(Ref<GLTFState> state, Node *scene_parent, Node3D *scene_root, const GLTFNodeIndex node_index);
  282. void _import_animation(Ref<GLTFState> state, AnimationPlayer *ap,
  283. const GLTFAnimationIndex index, const int bake_fps);
  284. void _convert_mesh_instances(Ref<GLTFState> state);
  285. GLTFCameraIndex _convert_camera(Ref<GLTFState> state, Camera3D *p_camera);
  286. void _convert_light_to_gltf(Light3D *light, Ref<GLTFState> state, Ref<GLTFNode> gltf_node);
  287. GLTFLightIndex _convert_light(Ref<GLTFState> state, Light3D *p_light);
  288. void _convert_spatial(Ref<GLTFState> state, Node3D *p_spatial, Ref<GLTFNode> p_node);
  289. void _convert_scene_node(Ref<GLTFState> state, Node *p_current,
  290. const GLTFNodeIndex p_gltf_current,
  291. const GLTFNodeIndex p_gltf_root);
  292. #ifdef MODULE_CSG_ENABLED
  293. void _convert_csg_shape_to_gltf(CSGShape3D *p_current, GLTFNodeIndex p_gltf_parent, Ref<GLTFNode> gltf_node, Ref<GLTFState> state);
  294. #endif // MODULE_CSG_ENABLED
  295. void _create_gltf_node(Ref<GLTFState> state,
  296. Node *p_scene_parent,
  297. GLTFNodeIndex current_node_i,
  298. GLTFNodeIndex p_parent_node_index,
  299. GLTFNodeIndex p_root_gltf_node,
  300. Ref<GLTFNode> gltf_node);
  301. void _convert_animation_player_to_gltf(
  302. AnimationPlayer *animation_player, Ref<GLTFState> state,
  303. GLTFNodeIndex p_gltf_current,
  304. GLTFNodeIndex p_gltf_root_index,
  305. Ref<GLTFNode> p_gltf_node, Node *p_scene_parent);
  306. void _check_visibility(Node *p_node, bool &retflag);
  307. void _convert_camera_to_gltf(Camera3D *camera, Ref<GLTFState> state,
  308. Ref<GLTFNode> gltf_node);
  309. #ifdef MODULE_GRIDMAP_ENABLED
  310. void _convert_grid_map_to_gltf(
  311. GridMap *p_grid_map,
  312. GLTFNodeIndex p_parent_node_index,
  313. GLTFNodeIndex p_root_node_index,
  314. Ref<GLTFNode> gltf_node, Ref<GLTFState> state);
  315. #endif // MODULE_GRIDMAP_ENABLED
  316. void _convert_multi_mesh_instance_to_gltf(
  317. MultiMeshInstance3D *p_multi_mesh_instance,
  318. GLTFNodeIndex p_parent_node_index,
  319. GLTFNodeIndex p_root_node_index,
  320. Ref<GLTFNode> gltf_node, Ref<GLTFState> state);
  321. void _convert_skeleton_to_gltf(
  322. Skeleton3D *p_scene_parent, Ref<GLTFState> state,
  323. GLTFNodeIndex p_parent_node_index,
  324. GLTFNodeIndex p_root_node_index,
  325. Ref<GLTFNode> gltf_node);
  326. void _convert_bone_attachment_to_gltf(BoneAttachment3D *p_bone_attachment,
  327. Ref<GLTFState> state,
  328. GLTFNodeIndex p_parent_node_index,
  329. GLTFNodeIndex p_root_node_index,
  330. Ref<GLTFNode> gltf_node);
  331. void _convert_mesh_instance_to_gltf(MeshInstance3D *p_mesh_instance,
  332. Ref<GLTFState> state,
  333. Ref<GLTFNode> gltf_node);
  334. GLTFMeshIndex _convert_mesh_to_gltf(Ref<GLTFState> state,
  335. MeshInstance3D *p_mesh_instance);
  336. void _convert_animation(Ref<GLTFState> state, AnimationPlayer *ap,
  337. String p_animation_track_name);
  338. Error _serialize(Ref<GLTFState> state, const String &p_path);
  339. Error _parse(Ref<GLTFState> state, String p_path, Ref<FileAccess> f, int p_bake_fps);
  340. };
  341. #endif // GLTF_DOCUMENT_H