From 04edfc090b0e78f4f820977da8cb4eb2d015c5f7 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Tue, 14 Nov 2017 15:44:51 -0300 Subject: [PATCH] Finalized ability to convert from CanvasItem/Spatial/Particles materials to ShaderMaterial, closes #10242 --- editor/editor_node.cpp | 4 +++ editor/plugins/material_editor_plugin.cpp | 38 +++++++++++++++++++++++ editor/plugins/material_editor_plugin.h | 8 +++++ scene/2d/canvas_item.cpp | 6 ++++ scene/2d/canvas_item.h | 2 ++ 5 files changed, 58 insertions(+) diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index e0cae7de572..6a4e8793407 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -5533,6 +5533,10 @@ EditorNode::EditorNode() { spatial_mat_convert.instance(); resource_conversion_plugins.push_back(spatial_mat_convert); + Ref canvas_item_mat_convert; + canvas_item_mat_convert.instance(); + resource_conversion_plugins.push_back(canvas_item_mat_convert); + Ref particles_mat_convert; particles_mat_convert.instance(); resource_conversion_plugins.push_back(particles_mat_convert); diff --git a/editor/plugins/material_editor_plugin.cpp b/editor/plugins/material_editor_plugin.cpp index bd4891ccb75..1fc112896d9 100644 --- a/editor/plugins/material_editor_plugin.cpp +++ b/editor/plugins/material_editor_plugin.cpp @@ -503,3 +503,41 @@ Ref ParticlesMaterialConversionPlugin::convert(const Ref &p_ smat->set_render_priority(mat->get_render_priority()); return smat; } + +String CanvasItemMaterialConversionPlugin::converts_to() const { + + return "ShaderMaterial"; +} +bool CanvasItemMaterialConversionPlugin::handles(const Ref &p_resource) const { + + Ref mat = p_resource; + return mat.is_valid(); +} +Ref CanvasItemMaterialConversionPlugin::convert(const Ref &p_resource) { + + Ref mat = p_resource; + ERR_FAIL_COND_V(!mat.is_valid(), Ref()); + + Ref smat; + smat.instance(); + + Ref shader; + shader.instance(); + + String code = VS::get_singleton()->shader_get_code(mat->get_shader_rid()); + + shader->set_code(code); + + smat->set_shader(shader); + + List params; + VS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), ¶ms); + + for (List::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; +} diff --git a/editor/plugins/material_editor_plugin.h b/editor/plugins/material_editor_plugin.h index 52c73cb7d89..2cc24be33ab 100644 --- a/editor/plugins/material_editor_plugin.h +++ b/editor/plugins/material_editor_plugin.h @@ -119,4 +119,12 @@ public: virtual Ref convert(const Ref &p_resource); }; +class CanvasItemMaterialConversionPlugin : public EditorResourceConversionPlugin { + GDCLASS(CanvasItemMaterialConversionPlugin, EditorResourceConversionPlugin) +public: + virtual String converts_to() const; + virtual bool handles(const Ref &p_resource) const; + virtual Ref convert(const Ref &p_resource); +}; + #endif // MATERIAL_EDITOR_PLUGIN_H diff --git a/scene/2d/canvas_item.cpp b/scene/2d/canvas_item.cpp index b41ba7f590d..fa45c61f683 100644 --- a/scene/2d/canvas_item.cpp +++ b/scene/2d/canvas_item.cpp @@ -178,6 +178,12 @@ CanvasItemMaterial::LightMode CanvasItemMaterial::get_light_mode() const { void CanvasItemMaterial::_validate_property(PropertyInfo &property) const { } +RID CanvasItemMaterial::get_shader_rid() const { + + ERR_FAIL_COND_V(!shader_map.has(current_key), RID()); + return shader_map[current_key].shader; +} + void CanvasItemMaterial::_bind_methods() { ClassDB::bind_method(D_METHOD("set_blend_mode", "blend_mode"), &CanvasItemMaterial::set_blend_mode); diff --git a/scene/2d/canvas_item.h b/scene/2d/canvas_item.h index cb8ee761e6c..1a043c204f6 100644 --- a/scene/2d/canvas_item.h +++ b/scene/2d/canvas_item.h @@ -121,6 +121,8 @@ public: static void finish_shaders(); static void flush_changes(); + RID get_shader_rid() const; + CanvasItemMaterial(); virtual ~CanvasItemMaterial(); };