editor_scene_importer_gltf.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*************************************************************************/
  2. /* editor_scene_importer_gltf.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 EDITOR_SCENE_IMPORTER_GLTF_H
  31. #define EDITOR_SCENE_IMPORTER_GLTF_H
  32. #include "editor/import/resource_importer_scene.h"
  33. #include "scene/3d/skeleton.h"
  34. #include "scene/3d/spatial.h"
  35. class AnimationPlayer;
  36. class EditorSceneImporterGLTF : public EditorSceneImporter {
  37. GDCLASS(EditorSceneImporterGLTF, EditorSceneImporter);
  38. enum {
  39. ARRAY_BUFFER = 34962,
  40. ELEMENT_ARRAY_BUFFER = 34963,
  41. TYPE_BYTE = 5120,
  42. TYPE_UNSIGNED_BYTE = 5121,
  43. TYPE_SHORT = 5122,
  44. TYPE_UNSIGNED_SHORT = 5123,
  45. TYPE_UNSIGNED_INT = 5125,
  46. TYPE_FLOAT = 5126,
  47. COMPONENT_TYPE_BYTE = 5120,
  48. COMPONENT_TYPE_UNSIGNED_BYTE = 5121,
  49. COMPONENT_TYPE_SHORT = 5122,
  50. COMPONENT_TYPE_UNSIGNED_SHORT = 5123,
  51. COMPONENT_TYPE_INT = 5125,
  52. COMPONENT_TYPE_FLOAT = 5126,
  53. };
  54. String _get_component_type_name(uint32_t p_component);
  55. int _get_component_type_size(int component_type);
  56. enum GLTFType {
  57. TYPE_SCALAR,
  58. TYPE_VEC2,
  59. TYPE_VEC3,
  60. TYPE_VEC4,
  61. TYPE_MAT2,
  62. TYPE_MAT3,
  63. TYPE_MAT4,
  64. };
  65. String _get_type_name(GLTFType p_component);
  66. struct GLTFNode {
  67. //matrices need to be transformed to this
  68. int parent;
  69. Transform xform;
  70. String name;
  71. //Node *godot_node;
  72. //int godot_bone_index;
  73. int mesh;
  74. int camera;
  75. int skin;
  76. //int skeleton_skin;
  77. //int child_of_skeleton; // put as children of skeleton
  78. //Vector<int> skeleton_children; //skeleton put as children of this
  79. struct Joint {
  80. int skin;
  81. int bone;
  82. int godot_bone_index;
  83. Joint() {
  84. skin = -1;
  85. bone = -1;
  86. godot_bone_index = -1;
  87. }
  88. };
  89. Vector<Joint> joints;
  90. //keep them for animation
  91. Vector3 translation;
  92. Quat rotation;
  93. Vector3 scale;
  94. Vector<int> children;
  95. Vector<Node *> godot_nodes;
  96. GLTFNode() :
  97. parent(-1),
  98. mesh(-1),
  99. camera(-1),
  100. skin(-1),
  101. //skeleton_skin(-1),
  102. //child_of_skeleton(-1),
  103. scale(Vector3(1, 1, 1)) {
  104. }
  105. };
  106. struct GLTFBufferView {
  107. int buffer;
  108. int byte_offset;
  109. int byte_length;
  110. int byte_stride;
  111. bool indices;
  112. //matrices need to be transformed to this
  113. GLTFBufferView() :
  114. buffer(0),
  115. byte_offset(0),
  116. byte_length(0),
  117. byte_stride(0),
  118. indices(false) {
  119. }
  120. };
  121. struct GLTFAccessor {
  122. int buffer_view;
  123. int byte_offset;
  124. int component_type;
  125. bool normalized;
  126. int count;
  127. GLTFType type;
  128. float min;
  129. float max;
  130. int sparse_count;
  131. int sparse_indices_buffer_view;
  132. int sparse_indices_byte_offset;
  133. int sparse_indices_component_type;
  134. int sparse_values_buffer_view;
  135. int sparse_values_byte_offset;
  136. //matrices need to be transformed to this
  137. GLTFAccessor() {
  138. buffer_view = 0;
  139. byte_offset = 0;
  140. component_type = 0;
  141. normalized = false;
  142. count = 0;
  143. min = 0;
  144. max = 0;
  145. sparse_count = 0;
  146. sparse_indices_byte_offset = 0;
  147. sparse_values_byte_offset = 0;
  148. }
  149. };
  150. struct GLTFTexture {
  151. int src_image;
  152. };
  153. struct GLTFSkin {
  154. String name;
  155. struct Bone {
  156. Transform inverse_bind;
  157. int node;
  158. };
  159. int skeleton;
  160. Vector<Bone> bones;
  161. //matrices need to be transformed to this
  162. GLTFSkin() {
  163. skeleton = -1;
  164. }
  165. };
  166. struct GLTFMesh {
  167. Ref<ArrayMesh> mesh;
  168. Vector<float> blend_weights;
  169. };
  170. struct GLTFCamera {
  171. bool perspective;
  172. float fov_size;
  173. float zfar;
  174. float znear;
  175. GLTFCamera() {
  176. perspective = true;
  177. fov_size = 65;
  178. zfar = 500;
  179. znear = 0.1;
  180. }
  181. };
  182. struct GLTFAnimation {
  183. enum Interpolation {
  184. INTERP_LINEAR,
  185. INTERP_STEP,
  186. INTERP_CATMULLROMSPLINE,
  187. INTERP_CUBIC_SPLINE
  188. };
  189. template <class T>
  190. struct Channel {
  191. Interpolation interpolation;
  192. Vector<float> times;
  193. Vector<T> values;
  194. };
  195. struct Track {
  196. Channel<Vector3> translation_track;
  197. Channel<Quat> rotation_track;
  198. Channel<Vector3> scale_track;
  199. Vector<Channel<float> > weight_tracks;
  200. };
  201. String name;
  202. Map<int, Track> tracks;
  203. };
  204. struct GLTFState {
  205. Dictionary json;
  206. int major_version;
  207. int minor_version;
  208. Vector<uint8_t> glb_data;
  209. Vector<GLTFNode *> nodes;
  210. Vector<Vector<uint8_t> > buffers;
  211. Vector<GLTFBufferView> buffer_views;
  212. Vector<GLTFAccessor> accessors;
  213. Vector<GLTFMesh> meshes; //meshes are loaded directly, no reason not to.
  214. Vector<Ref<Material> > materials;
  215. String scene_name;
  216. Vector<int> root_nodes;
  217. Vector<GLTFTexture> textures;
  218. Vector<Ref<Texture> > images;
  219. Vector<GLTFSkin> skins;
  220. Vector<GLTFCamera> cameras;
  221. Set<String> unique_names;
  222. Vector<GLTFAnimation> animations;
  223. Map<int, Vector<int> > skeleton_nodes;
  224. //Map<int, Vector<int> > skin_users; //cache skin users
  225. ~GLTFState() {
  226. for (int i = 0; i < nodes.size(); i++) {
  227. memdelete(nodes[i]);
  228. }
  229. }
  230. };
  231. String _gen_unique_name(GLTFState &state, const String &p_name);
  232. Ref<Texture> _get_texture(GLTFState &state, int p_texture);
  233. Error _parse_json(const String &p_path, GLTFState &state);
  234. Error _parse_glb(const String &p_path, GLTFState &state);
  235. Error _parse_scenes(GLTFState &state);
  236. Error _parse_nodes(GLTFState &state);
  237. Error _parse_buffers(GLTFState &state, const String &p_base_path);
  238. Error _parse_buffer_views(GLTFState &state);
  239. GLTFType _get_type_from_str(const String &p_string);
  240. Error _parse_accessors(GLTFState &state);
  241. Error _decode_buffer_view(GLTFState &state, int p_buffer_view, double *dst, int skip_every, int skip_bytes, int element_size, int count, GLTFType type, int component_count, int component_type, int component_size, bool normalized, int byte_offset, bool for_vertex);
  242. Vector<double> _decode_accessor(GLTFState &state, int p_accessor, bool p_for_vertex);
  243. PoolVector<float> _decode_accessor_as_floats(GLTFState &state, int p_accessor, bool p_for_vertex);
  244. PoolVector<int> _decode_accessor_as_ints(GLTFState &state, int p_accessor, bool p_for_vertex);
  245. PoolVector<Vector2> _decode_accessor_as_vec2(GLTFState &state, int p_accessor, bool p_for_vertex);
  246. PoolVector<Vector3> _decode_accessor_as_vec3(GLTFState &state, int p_accessor, bool p_for_vertex);
  247. PoolVector<Color> _decode_accessor_as_color(GLTFState &state, int p_accessor, bool p_for_vertex);
  248. Vector<Quat> _decode_accessor_as_quat(GLTFState &state, int p_accessor, bool p_for_vertex);
  249. Vector<Transform2D> _decode_accessor_as_xform2d(GLTFState &state, int p_accessor, bool p_for_vertex);
  250. Vector<Basis> _decode_accessor_as_basis(GLTFState &state, int p_accessor, bool p_for_vertex);
  251. Vector<Transform> _decode_accessor_as_xform(GLTFState &state, int p_accessor, bool p_for_vertex);
  252. void _generate_bone(GLTFState &state, int p_node, Vector<Skeleton *> &skeletons, Node *p_parent_node);
  253. void _generate_node(GLTFState &state, int p_node, Node *p_parent, Node *p_owner, Vector<Skeleton *> &skeletons);
  254. void _import_animation(GLTFState &state, AnimationPlayer *ap, int index, int bake_fps, Vector<Skeleton *> skeletons);
  255. Spatial *_generate_scene(GLTFState &state, int p_bake_fps);
  256. Error _parse_meshes(GLTFState &state);
  257. Error _parse_images(GLTFState &state, const String &p_base_path);
  258. Error _parse_textures(GLTFState &state);
  259. Error _parse_materials(GLTFState &state);
  260. Error _parse_skins(GLTFState &state);
  261. Error _parse_cameras(GLTFState &state);
  262. Error _parse_animations(GLTFState &state);
  263. void _assign_scene_names(GLTFState &state);
  264. template <class T>
  265. T _interpolate_track(const Vector<float> &p_times, const Vector<T> &p_values, float p_time, GLTFAnimation::Interpolation p_interp);
  266. public:
  267. virtual uint32_t get_import_flags() const;
  268. virtual void get_extensions(List<String> *r_extensions) const;
  269. virtual Node *import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps = NULL, Error *r_err = NULL);
  270. virtual Ref<Animation> import_animation(const String &p_path, uint32_t p_flags, int p_bake_fps);
  271. EditorSceneImporterGLTF();
  272. };
  273. #endif // EDITOR_SCENE_IMPORTER_GLTF_H