Browse Source

Ability to convert from SpatialMaterial to ShaderMaterial

Juan Linietsky 8 years ago
parent
commit
3237e05c36

+ 27 - 0
editor/editor_node.cpp

@@ -3247,6 +3247,7 @@ void EditorNode::register_editor_types() {
 	ClassDB::register_virtual_class<ScriptEditor>();
 	ClassDB::register_virtual_class<EditorInterface>();
 	ClassDB::register_class<EditorExportPlugin>();
+	ClassDB::register_class<EditorResourceConversionPlugin>();
 
 	// FIXME: Is this stuff obsolete, or should it be ported to new APIs?
 	//ClassDB::register_class<EditorScenePostImport>();
@@ -4431,6 +4432,27 @@ void EditorNode::open_export_template_manager() {
 	export_template_manager->popup_manager();
 }
 
+void EditorNode::add_resource_conversion_plugin(const Ref<EditorResourceConversionPlugin> &p_plugin) {
+	resource_conversion_plugins.push_back(p_plugin);
+}
+
+void EditorNode::remove_resource_conversion_plugin(const Ref<EditorResourceConversionPlugin> &p_plugin) {
+	resource_conversion_plugins.erase(p_plugin);
+}
+
+Vector<Ref<EditorResourceConversionPlugin> > EditorNode::find_resource_conversion_plugin(const Ref<Resource> &p_for_resource) {
+
+	Vector<Ref<EditorResourceConversionPlugin> > ret;
+
+	for (int i = 0; i < resource_conversion_plugins.size(); i++) {
+		if (resource_conversion_plugins[i].is_valid() && resource_conversion_plugins[i]->handles(p_for_resource)) {
+			ret.push_back(resource_conversion_plugins[i]);
+		}
+	}
+
+	return ret;
+}
+
 void EditorNode::_bind_methods() {
 
 	ClassDB::bind_method("_menu_option", &EditorNode::_menu_option);
@@ -5497,6 +5519,11 @@ EditorNode::EditorNode() {
 	resource_preview->add_preview_generator(Ref<EditorMeshPreviewPlugin>(memnew(EditorMeshPreviewPlugin)));
 	resource_preview->add_preview_generator(Ref<EditorBitmapPreviewPlugin>(memnew(EditorBitmapPreviewPlugin)));
 
+	{
+		Ref<SpatialMaterialConversionPlugin> spatial_mat_convert;
+		spatial_mat_convert.instance();
+		resource_conversion_plugins.push_back(spatial_mat_convert);
+	}
 	circle_step_msec = OS::get_singleton()->get_ticks_msec();
 	circle_step_frame = Engine::get_singleton()->get_frames_drawn();
 	circle_step = 0;

+ 6 - 0
editor/editor_node.h

@@ -608,6 +608,8 @@ private:
 
 	void _license_tree_selected();
 
+	Vector<Ref<EditorResourceConversionPlugin> > resource_conversion_plugins;
+
 protected:
 	void _notification(int p_what);
 	static void _bind_methods();
@@ -776,6 +778,10 @@ public:
 	~EditorNode();
 	void get_singleton(const char *arg1, bool arg2);
 
+	void add_resource_conversion_plugin(const Ref<EditorResourceConversionPlugin> &p_plugin);
+	void remove_resource_conversion_plugin(const Ref<EditorResourceConversionPlugin> &p_plugin);
+	Vector<Ref<EditorResourceConversionPlugin> > find_resource_conversion_plugin(const Ref<Resource> &p_for_resource);
+
 	static void add_init_callback(EditorNodeInitCallback p_callback) { _init_callbacks.push_back(p_callback); }
 	static void add_build_callback(EditorBuildCallback p_callback);
 };

+ 41 - 1
editor/plugins/material_editor_plugin.cpp

@@ -30,9 +30,11 @@
 
 // FIXME: Disabled as (according to reduz) users were complaining that it gets in the way
 // Waiting for PropertyEditor rewrite (planned for 3.1) to be refactored.
-#if 0
+
 #include "material_editor_plugin.h"
 
+#if 0
+
 #include "scene/main/viewport.h"
 
 void MaterialEditor::_gui_input(InputEvent p_event) {
@@ -416,3 +418,41 @@ MaterialEditorPlugin::~MaterialEditorPlugin()
 }
 
 #endif
+
+String SpatialMaterialConversionPlugin::converts_to() const {
+
+	return "ShaderMaterial";
+}
+bool SpatialMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
+
+	Ref<SpatialMaterial> mat = p_resource;
+	return mat.is_valid();
+}
+Ref<Resource> SpatialMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) {
+
+	Ref<SpatialMaterial> mat = p_resource;
+	ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
+
+	Ref<ShaderMaterial> smat;
+	smat.instance();
+
+	Ref<Shader> shader;
+	shader.instance();
+
+	String code = VS::get_singleton()->shader_get_code(mat->get_shader_rid());
+
+	shader->set_code(code);
+
+	smat->set_shader(shader);
+
+	List<PropertyInfo> params;
+	VS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
+
+	for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
+		Variant value = VS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
+		smat->set_shader_param(E->get().name, value);
+	}
+
+	smat->set_render_priority(mat->get_render_priority());
+	return smat;
+}

+ 10 - 0
editor/plugins/material_editor_plugin.h

@@ -30,6 +30,7 @@
 #ifndef MATERIAL_EDITOR_PLUGIN_H
 #define MATERIAL_EDITOR_PLUGIN_H
 
+#include "editor/property_editor.h"
 // FIXME: Disabled as (according to reduz) users were complaining that it gets in the way
 // Waiting for PropertyEditor rewrite (planned for 3.1) to be refactored.
 #if 0
@@ -101,4 +102,13 @@ public:
 };
 
 #endif
+
+class SpatialMaterialConversionPlugin : public EditorResourceConversionPlugin {
+	GDCLASS(SpatialMaterialConversionPlugin, EditorResourceConversionPlugin)
+public:
+	virtual String converts_to() const;
+	virtual bool handles(const Ref<Resource> &p_resource) const;
+	virtual Ref<Resource> convert(const Ref<Resource> &p_resource);
+};
+
 #endif // MATERIAL_EDITOR_PLUGIN_H

+ 78 - 0
editor/property_editor.cpp

@@ -53,6 +53,49 @@
 #include "scene/resources/packed_scene.h"
 #include "scene/scene_string_names.h"
 
+void EditorResourceConversionPlugin::_bind_methods() {
+
+	MethodInfo mi;
+	mi.name = "_convert";
+	mi.return_val.type = Variant::OBJECT;
+	mi.return_val.class_name = "Resource";
+	mi.return_val.hint = PROPERTY_HINT_RESOURCE_TYPE;
+	mi.return_val.hint_string = "Resource";
+	mi.arguments.push_back(mi.return_val);
+	mi.arguments[0].name = "resource";
+
+	BIND_VMETHOD(mi)
+
+	mi.name = "_handles";
+	mi.return_val = PropertyInfo(Variant::BOOL, "");
+
+	BIND_VMETHOD(MethodInfo(Variant::BOOL, "_converts_to"));
+}
+
+String EditorResourceConversionPlugin::converts_to() const {
+
+	if (get_script_instance())
+		return get_script_instance()->call("_converts_to");
+
+	return "";
+}
+
+bool EditorResourceConversionPlugin::handles(const Ref<Resource> &p_resource) const {
+
+	if (get_script_instance())
+		return get_script_instance()->call("_handles", p_resource);
+
+	return false;
+}
+
+Ref<Resource> EditorResourceConversionPlugin::convert(const Ref<Resource> &p_resource) {
+
+	if (get_script_instance())
+		return get_script_instance()->call("_convert", p_resource);
+
+	return Ref<Resource>();
+}
+
 void CustomPropertyEditor::_notification(int p_what) {
 
 	if (p_what == NOTIFICATION_DRAW) {
@@ -213,6 +256,20 @@ void CustomPropertyEditor::_menu_option(int p_which) {
 				} break;
 				default: {
 
+					if (p_which >= CONVERT_BASE_ID) {
+
+						int to_type = p_which - CONVERT_BASE_ID;
+
+						Vector<Ref<EditorResourceConversionPlugin> > conversions = EditorNode::get_singleton()->find_resource_conversion_plugin(RES(v));
+
+						ERR_FAIL_INDEX(to_type, conversions.size());
+
+						Ref<Resource> new_res = conversions[to_type]->convert(v);
+
+						v = new_res;
+						emit_signal("variant_changed");
+						break;
+					}
 					ERR_FAIL_COND(inheritors_array.empty());
 
 					String intype = inheritors_array[p_which - TYPE_BASE_ID];
@@ -903,6 +960,27 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant::
 				}
 			}
 
+			if (!RES(v).is_null()) {
+
+				Vector<Ref<EditorResourceConversionPlugin> > conversions = EditorNode::get_singleton()->find_resource_conversion_plugin(RES(v));
+				if (conversions.size()) {
+					menu->add_separator();
+				}
+				for (int i = 0; i < conversions.size(); i++) {
+					String what = conversions[i]->converts_to();
+					Ref<Texture> icon;
+					if (has_icon(what, "EditorIcons")) {
+
+						icon = get_icon(what, "EditorIcons");
+					} else {
+
+						icon = get_icon(what, "Resource");
+					}
+
+					menu->add_icon_item(icon, vformat(TTR("Convert To %s"), what), CONVERT_BASE_ID + i);
+				}
+			}
+
 			menu->set_position(get_position());
 			menu->popup();
 			hide();

+ 15 - 1
editor/property_editor.h

@@ -53,6 +53,19 @@ class PropertyValueEvaluator;
 class CreateDialog;
 class PropertySelector;
 
+class EditorResourceConversionPlugin : public Reference {
+
+	GDCLASS(EditorResourceConversionPlugin, Reference)
+
+protected:
+	static void _bind_methods();
+
+public:
+	virtual String converts_to() const;
+	virtual bool handles(const Ref<Resource> &p_resource) const;
+	virtual Ref<Resource> convert(const Ref<Resource> &p_resource);
+};
+
 class CustomPropertyEditor : public Popup {
 
 	GDCLASS(CustomPropertyEditor, Popup);
@@ -68,7 +81,8 @@ class CustomPropertyEditor : public Popup {
 		OBJ_MENU_PASTE = 5,
 		OBJ_MENU_NEW_SCRIPT = 6,
 		OBJ_MENU_SHOW_IN_FILE_SYSTEM = 7,
-		TYPE_BASE_ID = 100
+		TYPE_BASE_ID = 100,
+		CONVERT_BASE_ID = 1000
 	};
 
 	enum {

+ 6 - 0
scene/resources/material.cpp

@@ -1611,6 +1611,12 @@ float SpatialMaterial::get_distance_fade_min_distance() const {
 	return distance_fade_min_distance;
 }
 
+RID SpatialMaterial::get_shader_rid() const {
+
+	ERR_FAIL_COND_V(!shader_map.has(current_key), RID());
+	return shader_map[current_key].shader;
+}
+
 void SpatialMaterial::_bind_methods() {
 
 	ClassDB::bind_method(D_METHOD("set_albedo", "albedo"), &SpatialMaterial::set_albedo);

+ 2 - 0
scene/resources/material.h

@@ -579,6 +579,8 @@ public:
 
 	static RID get_material_rid_for_2d(bool p_shaded, bool p_transparent, bool p_double_sided, bool p_cut_alpha, bool p_opaque_prepass);
 
+	RID get_shader_rid() const;
+
 	SpatialMaterial();
 	virtual ~SpatialMaterial();
 };