1 /*************************************************************************/
2 /*  import_state.h                                                       */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #ifndef EDITOR_SCENE_IMPORT_STATE_H
32 #define EDITOR_SCENE_IMPORT_STATE_H
33 
34 #include "core/bind/core_bind.h"
35 #include "core/io/resource_importer.h"
36 #include "core/vector.h"
37 #include "editor/import/resource_importer_scene.h"
38 #include "editor/project_settings_editor.h"
39 #include "scene/3d/mesh_instance.h"
40 #include "scene/3d/skeleton.h"
41 #include "scene/3d/spatial.h"
42 #include "scene/animation/animation_player.h"
43 #include "scene/resources/animation.h"
44 #include "scene/resources/surface_tool.h"
45 
46 #include <assimp/matrix4x4.h>
47 #include <assimp/scene.h>
48 #include <assimp/types.h>
49 #include <assimp/DefaultLogger.hpp>
50 #include <assimp/LogStream.hpp>
51 #include <assimp/Logger.hpp>
52 
53 namespace AssimpImporter {
54 /** Import state is for global scene import data
55  * This makes the code simpler and contains useful lookups.
56  */
57 struct ImportState {
58 
59 	String path;
60 	Spatial *root;
61 	const aiScene *assimp_scene;
62 	uint32_t max_bone_weights;
63 
64 	Map<String, Ref<Mesh> > mesh_cache;
65 	Map<int, Ref<Material> > material_cache;
66 	Map<String, int> light_cache;
67 	Map<String, int> camera_cache;
68 
69 	// very useful for when you need to ask assimp for the bone mesh
70 
71 	Map<const aiNode *, Node *> assimp_node_map;
72 	Map<String, Ref<Image> > path_to_image_cache;
73 
74 	// Generation 3 - determinisitic iteration
75 	// to lower potential recursion errors
76 	List<const aiNode *> nodes;
77 	Map<const aiNode *, Spatial *> flat_node_map;
78 	AnimationPlayer *animation_player;
79 
80 	// Generation 3 - deterministic armatures
81 	// list of armature nodes - flat and simple to parse
82 	// assimp node, node in godot
83 	List<aiNode *> armature_nodes;
84 	Map<const aiNode *, Skeleton *> armature_skeletons;
85 	Map<aiBone *, Skeleton *> skeleton_bone_map;
86 	// Generation 3 - deterministic bone handling
87 	// bones from the stack are popped when found
88 	// this means we can detect
89 	// what bones are for other armatures
90 	List<aiBone *> bone_stack;
91 
92 	// EditorSceneImporter::ImportFlags
93 	uint32_t import_flags;
94 };
95 
96 struct AssimpImageData {
97 	Ref<Image> raw_image;
98 	Ref<ImageTexture> texture;
99 	aiTextureMapMode map_mode[2];
100 };
101 
102 /** Recursive state is used to push state into functions instead of specifying them
103 	* This makes the code easier to handle too and add extra arguments without breaking things
104 	*/
105 struct RecursiveState {
RecursiveStateRecursiveState106 	RecursiveState() {} // do not construct :)
RecursiveStateRecursiveState107 	RecursiveState(
108 			Transform &_node_transform,
109 			Skeleton *_skeleton,
110 			Spatial *_new_node,
111 			String &_node_name,
112 			aiNode *_assimp_node,
113 			Node *_parent_node,
114 			aiBone *_bone) :
115 			node_transform(_node_transform),
116 			skeleton(_skeleton),
117 			new_node(_new_node),
118 			node_name(_node_name),
119 			assimp_node(_assimp_node),
120 			parent_node(_parent_node),
121 			bone(_bone) {}
122 
123 	Transform node_transform;
124 	Skeleton *skeleton = NULL;
125 	Spatial *new_node = NULL;
126 	String node_name;
127 	aiNode *assimp_node = NULL;
128 	Node *parent_node = NULL;
129 	aiBone *bone = NULL;
130 };
131 } // namespace AssimpImporter
132 
133 #endif // EDITOR_SCENE_IMPORT_STATE_H
134