浏览代码

completed scene importing (I hope?)

Juan Linietsky 8 年之前
父节点
当前提交
331a4d8078

+ 39 - 1
tools/editor/editor_node.cpp

@@ -3304,7 +3304,10 @@ void EditorNode::fix_dependencies(const String& p_for_file) {
 	dependency_fixer->edit(p_for_file);
 }
 
-Error EditorNode::load_scene(const String& p_scene, bool p_ignore_broken_deps,bool p_set_inherited,bool p_clear_errors) {
+
+
+
+Error EditorNode::load_scene(const String& p_scene, bool p_ignore_broken_deps, bool p_set_inherited, bool p_clear_errors, bool p_force_open_imported) {
 
 	if (!is_inside_tree()) {
 		defer_load_scene = p_scene;
@@ -3313,6 +3316,8 @@ Error EditorNode::load_scene(const String& p_scene, bool p_ignore_broken_deps,bo
 
 
 	if(!p_set_inherited) {
+
+
 		for(int i=0;i<editor_data.get_edited_scene_count();i++) {
 
 			if (editor_data.get_scene_path(i)==p_scene) {
@@ -3320,9 +3325,19 @@ Error EditorNode::load_scene(const String& p_scene, bool p_ignore_broken_deps,bo
 				return OK;
 			}
 		}
+
+		if (!p_force_open_imported && FileAccess::exists(p_scene+".import")) {
+			open_imported->set_text(vformat(TTR("Scene '%s' was automatically imported, so it can't be modified.\nTo make changes to it, a new inherited scene can be created."),p_scene.get_file()));
+			open_imported->popup_centered_minsize();
+			new_inherited_button->grab_focus();
+			open_import_request=p_scene;
+			return OK;
+		}
+
 	}
 
 
+
 	if (p_clear_errors)
 		load_errors->clear();
 
@@ -4958,6 +4973,19 @@ void EditorNode::_call_build() {
 	}
 }
 
+
+void EditorNode::_inherit_imported(const String& p_action) {
+
+	open_imported->hide();
+	load_scene(open_import_request,true,true);
+
+}
+
+void EditorNode::_open_imported() {
+
+	load_scene(open_import_request,true,false,true,true);
+}
+
 void EditorNode::_bind_methods() {
 
 
@@ -5035,6 +5063,8 @@ void EditorNode::_bind_methods() {
 	ClassDB::bind_method(_MD("get_gui_base"), &EditorNode::get_gui_base);
 	ClassDB::bind_method(_MD("_bottom_panel_switch"), &EditorNode::_bottom_panel_switch);
 
+	ClassDB::bind_method(_MD("_open_imported"), &EditorNode::_open_imported);
+	ClassDB::bind_method(_MD("_inherit_imported"), &EditorNode::_inherit_imported);
 
 	ADD_SIGNAL( MethodInfo("play_pressed") );
 	ADD_SIGNAL( MethodInfo("pause_pressed") );
@@ -6357,6 +6387,14 @@ EditorNode::EditorNode() {
 		}
 	}
 
+	open_imported = memnew( ConfirmationDialog );
+	open_imported->get_ok()->set_text(TTR("Open Anyway"));
+	new_inherited_button=open_imported->add_button("New Inherited",!OS::get_singleton()->get_swap_ok_cancel(),"inherit");
+	open_imported->connect("confirmed",this,"_open_imported");
+	open_imported->connect("custom_action",this,"_inherit_imported");
+	gui_base->add_child(open_imported);
+
+
 
 	//edited_scene=NULL;
 	saved_version=1;

+ 7 - 1
tools/editor/editor_node.h

@@ -346,6 +346,10 @@ private:
 	DependencyErrorDialog *dependency_error;
 	DependencyEditor *dependency_fixer;
 	OrphanResourcesDialog *orphan_resources;
+	ConfirmationDialog *open_imported;
+	Button *new_inherited_button;
+	String open_import_request;
+
 
 	TabContainer *dock_slot[DOCK_SLOT_MAX];
 	Rect2 dock_select_rect[DOCK_SLOT_MAX];
@@ -584,6 +588,8 @@ private:
 		MAX_BUILD_CALLBACKS=128
 	};
 
+	void _inherit_imported(const String &p_action);
+	void _open_imported();
 
 
 	static int plugin_init_callback_count;
@@ -684,7 +690,7 @@ public:
 
 	void fix_dependencies(const String& p_for_file);
 	void clear_scene() { _cleanup_scene(); }
-	Error load_scene(const String& p_scene, bool p_ignore_broken_deps=false, bool p_set_inherited=false, bool p_clear_errors=true);
+	Error load_scene(const String& p_scene, bool p_ignore_broken_deps=false, bool p_set_inherited=false, bool p_clear_errors=true,bool p_force_open_imported=false);
 	Error load_resource(const String& p_scene);
 
 	bool is_scene_open(const String& p_path);

+ 36 - 4
tools/editor/import/editor_import_collada.cpp

@@ -65,6 +65,7 @@ struct ColladaImport {
 	bool found_directional;
 	bool force_make_tangents;
 	bool apply_mesh_xform_to_vertices;
+	bool use_mesh_builtin_materials;
 	float bake_fps;
 
 
@@ -87,7 +88,7 @@ struct ColladaImport {
 	Error _create_scene(Collada::Node *p_node, Spatial *p_parent);
 	Error _create_resources(Collada::Node *p_node);
 	Error _create_material(const String& p_material);
-	Error _create_mesh_surfaces(bool p_optimize, Ref<Mesh>& p_mesh, const Map<String,Collada::NodeGeometry::Material>& p_material_map, const Collada::MeshData &meshdata, const Transform& p_local_xform, const Vector<int> &bone_remap, const Collada::SkinControllerData *p_skin_data, const Collada::MorphControllerData *p_morph_data, Vector<Ref<Mesh> > p_morph_meshes=Vector<Ref<Mesh> >(), bool p_for_morph=false);
+	Error _create_mesh_surfaces(bool p_optimize, Ref<Mesh>& p_mesh, const Map<String,Collada::NodeGeometry::Material>& p_material_map, const Collada::MeshData &meshdata, const Transform& p_local_xform, const Vector<int> &bone_remap, const Collada::SkinControllerData *p_skin_data, const Collada::MorphControllerData *p_morph_data, Vector<Ref<Mesh> > p_morph_meshes=Vector<Ref<Mesh> >(), bool p_for_morph=false, bool p_use_mesh_material=false);
 	Error load(const String& p_path, int p_flags, bool p_force_make_tangents=false);
 	void _fix_param_animation_tracks();
 	void create_animation(int p_clip,bool p_make_tracks_in_all_bones, bool p_import_value_tracks);
@@ -610,7 +611,7 @@ static void _generate_tangents_and_binormals(const PoolVector<int>& p_indices,co
 	}
 }
 
-Error ColladaImport::_create_mesh_surfaces(bool p_optimize,Ref<Mesh>& p_mesh,const Map<String,Collada::NodeGeometry::Material>& p_material_map,const Collada::MeshData &meshdata,const Transform& p_local_xform,const Vector<int> &bone_remap, const Collada::SkinControllerData *skin_controller, const Collada::MorphControllerData *p_morph_data,Vector<Ref<Mesh> > p_morph_meshes,bool p_for_morph) {
+Error ColladaImport::_create_mesh_surfaces(bool p_optimize,Ref<Mesh>& p_mesh,const Map<String,Collada::NodeGeometry::Material>& p_material_map,const Collada::MeshData &meshdata,const Transform& p_local_xform,const Vector<int> &bone_remap, const Collada::SkinControllerData *skin_controller, const Collada::MorphControllerData *p_morph_data,Vector<Ref<Mesh> > p_morph_meshes,bool p_for_morph,bool p_use_mesh_material) {
 
 
 	bool local_xform_mirror=p_local_xform.basis.determinant() < 0;
@@ -1494,7 +1495,9 @@ Error ColladaImport::_create_mesh_surfaces(bool p_optimize,Ref<Mesh>& p_mesh,con
 			p_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES,d,mr,p_for_morph?0:Mesh::ARRAY_COMPRESS_DEFAULT);
 
 			if (material.is_valid()) {
-				p_mesh->surface_set_material(surface, material);
+				if (p_use_mesh_material) {
+					p_mesh->surface_set_material(surface, material);
+				}
 				p_mesh->surface_set_name(surface, material->get_name());
 			}
 		}
@@ -1753,7 +1756,7 @@ Error ColladaImport::_create_resources(Collada::Node *p_node) {
 					mesh=Ref<Mesh>(memnew( Mesh ));
 					const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid];
 					mesh->set_name( meshdata.name );
-					Error err = _create_mesh_surfaces(morphs.size()==0,mesh,ng->material_map,meshdata,apply_xform,bone_remap,skin,morph,morphs);
+					Error err = _create_mesh_surfaces(morphs.size()==0,mesh,ng->material_map,meshdata,apply_xform,bone_remap,skin,morph,morphs,false,use_mesh_builtin_materials);
 					ERR_FAIL_COND_V(err,err);
 
 					mesh_cache[meshid]=mesh;
@@ -1764,7 +1767,33 @@ Error ColladaImport::_create_resources(Collada::Node *p_node) {
 			}
 
 			if (!mesh.is_null()) {
+
 				mi->set_mesh(mesh);
+				if (!use_mesh_builtin_materials) {
+					const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid];
+
+					for(int i=0;i<meshdata.primitives.size();i++) {
+
+						String matname=meshdata.primitives[i].material;
+
+						if (ng->material_map.has(matname)) {
+							String target=ng->material_map[matname].target;
+
+							Ref<Material> material;
+							if (!material_cache.has(target)) {
+								Error err = _create_material(target);
+								if (!err)
+									material=material_cache[target];
+							} else
+								material=material_cache[target];
+
+							mi->set_surface_material(i,material);
+						} else if (matname!=""){
+							print_line("Warning, unreferenced material in geometry instance: "+matname);
+						}
+
+					}
+				}
 			}
 		}
 	}
@@ -2375,6 +2404,7 @@ Node* EditorSceneImporterCollada::import_scene(const String& p_path, uint32_t p_
 	if (p_flags&IMPORT_ANIMATION)
 		flags|=Collada::IMPORT_FLAG_ANIMATION;
 
+	state.use_mesh_builtin_materials=!(p_flags&IMPORT_MATERIALS_IN_INSTANCES);
 	state.bake_fps=p_bake_fps;
 
 	Error err = state.load(p_path,flags,p_flags&EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS);
@@ -2435,6 +2465,8 @@ Ref<Animation> EditorSceneImporterCollada::import_animation(const String& p_path
 	ColladaImport state;
 
 
+	state.use_mesh_builtin_materials=false;
+
 	Error err = state.load(p_path,Collada::IMPORT_FLAG_ANIMATION,p_flags&EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS);
 	ERR_FAIL_COND_V(err!=OK,RES());
 

+ 138 - 3
tools/editor/import/resource_importer_scene.cpp

@@ -979,6 +979,131 @@ void ResourceImporterScene::_optimize_animations(Node *scene, float p_max_lin_er
 }
 
 
+static String _make_extname(const String& p_str) {
+
+	String ext_name=p_str.replace(".","_");
+	ext_name=ext_name.replace(":","_");
+	ext_name=ext_name.replace("\"","_");
+	ext_name=ext_name.replace("<","_");
+	ext_name=ext_name.replace(">","_");
+	ext_name=ext_name.replace("/","_");
+	ext_name=ext_name.replace("|","_");
+	ext_name=ext_name.replace("\\","_");
+	ext_name=ext_name.replace("?","_");
+	ext_name=ext_name.replace("*","_");
+
+	return ext_name;
+}
+
+void ResourceImporterScene::_make_external_resources(Node* p_node,const String& p_base_path, bool p_make_materials, bool p_make_meshes, Map<Ref<Material>,Ref<Material> >& p_materials, Map<Ref<Mesh>,Ref<Mesh> >& p_meshes) {
+
+	List<PropertyInfo> pi;
+
+	p_node->get_property_list(&pi);
+
+	for (List<PropertyInfo>::Element *E=pi.front();E;E=E->next()) {
+
+		if (E->get().type==Variant::OBJECT) {
+
+			Ref<Material> mat = p_node->get(E->get().name);
+			if (p_make_materials && mat.is_valid() && mat->get_name()!="") {
+
+
+				if (!p_materials.has(mat)) {
+
+					String ext_name = p_base_path+"."+_make_extname(mat->get_name())+".mtl";
+					if (FileAccess::exists(ext_name)) {
+						//if exists, use it
+						Ref<Material> existing = ResourceLoader::load(ext_name);
+						p_materials[mat]=existing;
+					} else {
+
+						ResourceSaver::save(ext_name,mat,ResourceSaver::FLAG_CHANGE_PATH);
+						p_materials[mat]=mat;
+					}
+				}
+
+				if (p_materials[mat]!=mat) {
+
+					p_node->set(E->get().name,p_materials[mat]);
+				}
+			} else {
+
+				Ref<Mesh> mesh = p_node->get(E->get().name);
+
+				if (mesh.is_valid()) {
+
+					bool mesh_just_added=false;
+
+					if (p_make_meshes) {
+
+						if (!p_meshes.has(mesh)) {
+
+							String ext_name = p_base_path+"."+_make_extname(mesh->get_name())+".msh";
+							if (FileAccess::exists(ext_name)) {
+								//if exists, use it
+								Ref<Mesh> existing = ResourceLoader::load(ext_name);
+								p_meshes[mesh]=existing;
+							} else {
+
+								ResourceSaver::save(ext_name,mesh,ResourceSaver::FLAG_CHANGE_PATH);
+								p_meshes[mesh]=mesh;
+								mesh_just_added=true;
+							}
+
+
+						}
+					}
+
+
+					if (p_make_materials){
+
+						if (mesh_just_added || !p_meshes.has(mesh)) {
+
+
+							for(int i=0;i<mesh->get_surface_count();i++) {
+								mat=mesh->surface_get_material(i);
+								if (!mat.is_valid() || mat->get_name()=="")
+									continue;
+
+								if (!p_materials.has(mat)) {
+
+									String ext_name = p_base_path+"."+_make_extname(mat->get_name())+".mtl";
+									if (FileAccess::exists(ext_name)) {
+										//if exists, use it
+										Ref<Material> existing = ResourceLoader::load(ext_name);
+										p_materials[mat]=existing;
+									} else {
+
+										ResourceSaver::save(ext_name,mat,ResourceSaver::FLAG_CHANGE_PATH);
+										p_materials[mat]=mat;
+									}
+								}
+
+								if (p_materials[mat]!=mat) {
+
+									mesh->surface_set_material(i,p_materials[mat]);
+								}
+
+							}
+
+							if(!p_make_meshes) {
+								p_meshes[mesh]=Ref<Mesh>(); //save it anyway, so it won't be checked again
+							}
+						}
+					}
+				}
+			}
+		}
+	}
+
+	for(int i=0;i<p_node->get_child_count();i++) {
+
+		_make_external_resources(p_node->get_child(i),p_base_path,p_make_materials,p_make_meshes,p_materials,p_meshes);
+	}
+}
+
+
 void ResourceImporterScene::get_import_options(List<ImportOption> *r_options,int p_preset) const {
 
 
@@ -998,7 +1123,7 @@ void ResourceImporterScene::get_import_options(List<ImportOption> *r_options,int
 
 	r_options->push_back(ImportOption(PropertyInfo(Variant::STRING,"nodes/custom_script",PROPERTY_HINT_FILE,script_ext_hint),""));
 	r_options->push_back(ImportOption(PropertyInfo(Variant::INT,"materials/location",PROPERTY_HINT_ENUM,"Node,Mesh"),0));
-	r_options->push_back(ImportOption(PropertyInfo(Variant::INT,"materials/storage",PROPERTY_HINT_ENUM,"Bult-In,Files"),1));
+	r_options->push_back(ImportOption(PropertyInfo(Variant::INT,"materials/storage",PROPERTY_HINT_ENUM,"Bult-In,Files"),0));
 	r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL,"geometry/compress"),true));
 	r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL,"geometry/ensure_tangents"),true));
 	r_options->push_back(ImportOption(PropertyInfo(Variant::INT,"geometry/storage",PROPERTY_HINT_ENUM,"Built-In,Files"),0));
@@ -1018,6 +1143,7 @@ void ResourceImporterScene::get_import_options(List<ImportOption> *r_options,int
 		r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL,"animation/clip_"+itos(i+1)+"/loops"),false));
 	}
 }
+
 Error ResourceImporterScene::import(const String& p_source_file, const String& p_save_path, const Map<StringName,Variant>& p_options, List<String>* r_platform_variants, List<String> *r_gen_files) {
 
 	String src_path=p_source_file;
@@ -1063,8 +1189,8 @@ Error ResourceImporterScene::import(const String& p_source_file, const String& p
 	if (bool(p_options["geometry/ensure_tangents"]))
 		import_flags|=EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS;
 
-
-
+	if (int(p_options["materials/location"])==0)
+		import_flags|=EditorSceneImporter::IMPORT_MATERIALS_IN_INSTANCES;
 
 
 	Error err=OK;
@@ -1138,6 +1264,15 @@ Error ResourceImporterScene::import(const String& p_source_file, const String& p
 	}
 
 
+	bool external_materials = p_options["materials/storage"];
+	bool external_meshes = p_options["geometry/storage"];
+
+	if (external_materials || external_meshes) {
+		Map<Ref<Material>, Ref<Material> > mat_map;
+		Map<Ref<Mesh>, Ref<Mesh> > mesh_map;
+		_make_external_resources(scene,p_source_file.get_basename(),external_materials,external_meshes,mat_map,mesh_map);
+	}
+
 	progress.step(TTR("Running Custom Script.."),2);
 
 	String post_import_script_path = p_options["nodes/custom_script"];

+ 7 - 1
tools/editor/import/resource_importer_scene.h

@@ -5,6 +5,8 @@
 #include "scene/resources/animation.h"
 #include "scene/resources/shape.h"
 
+class Material;
+
 class EditorSceneImporter : public Reference {
 
 	GDCLASS(EditorSceneImporter,Reference );
@@ -18,7 +20,8 @@ public:
 		IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS=16,
 		IMPORT_ANIMATION_KEEP_VALUE_TRACKS=32,
 		IMPORT_GENERATE_TANGENT_ARRAYS=256,
-		IMPORT_FAIL_ON_MISSING_DEPENDENCIES=512
+		IMPORT_FAIL_ON_MISSING_DEPENDENCIES=512,
+		IMPORT_MATERIALS_IN_INSTANCES=1024
 
 	};
 
@@ -72,6 +75,8 @@ public:
 	virtual void get_import_options(List<ImportOption> *r_options,int p_preset=0) const;
 	virtual bool get_option_visibility(const String& p_option,const Map<StringName,Variant>& p_options) const;
 
+	void _make_external_resources(Node* p_node,const String& p_base_path, bool p_make_materials, bool p_make_meshes, Map<Ref<Material>, Ref<Material> > &p_materials, Map<Ref<Mesh>, Ref<Mesh> > &p_meshes);
+
 	Node* _fix_node(Node *p_node,Node *p_root,Map<Ref<Mesh>,Ref<Shape> > &collision_map);
 
 	void _create_clips(Node *scene, const Array& p_clips,bool p_bake_all);
@@ -84,4 +89,5 @@ public:
 	ResourceImporterScene();
 };
 
+
 #endif // RESOURCEIMPORTERSCENE_H