Merge pull request #32503 from Chaosus/vs_custom

Drag&drop custom nodes from filesystem to visual shader
This commit is contained in:
Yuri Roubinsky 2019-10-03 08:06:20 +03:00 committed by GitHub
commit 4f92dda815
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 5 deletions

View file

@ -88,7 +88,6 @@ void VisualShaderEditor::edit(VisualShader *p_visual_shader) {
} else { } else {
if (changed) { // to avoid tree collapse if (changed) { // to avoid tree collapse
_clear_buffer(); _clear_buffer();
_update_custom_nodes();
_update_options_menu(); _update_options_menu();
_update_preview(); _update_preview();
} }
@ -178,7 +177,7 @@ bool VisualShaderEditor::_is_available(int p_mode) {
return (p_mode == -1 || (p_mode & current_mode) != 0); return (p_mode == -1 || (p_mode & current_mode) != 0);
} }
void VisualShaderEditor::_update_custom_nodes() { void VisualShaderEditor::update_custom_nodes() {
clear_custom_types(); clear_custom_types();
List<StringName> class_list; List<StringName> class_list;
ScriptServer::get_global_class_list(&class_list); ScriptServer::get_global_class_list(&class_list);
@ -228,6 +227,7 @@ void VisualShaderEditor::_update_custom_nodes() {
add_custom_type(name, script, description, return_icon_type, category, sub_category); add_custom_type(name, script, description, return_icon_type, category, sub_category);
} }
} }
_update_options_menu();
} }
String VisualShaderEditor::_get_description(int p_idx) { String VisualShaderEditor::_get_description(int p_idx) {
@ -1219,6 +1219,23 @@ void VisualShaderEditor::_edit_port_default_input(Object *p_button, int p_node,
editing_port = p_port; editing_port = p_port;
} }
void VisualShaderEditor::_add_custom_node(const String &p_path) {
int idx = -1;
for (int i = custom_node_option_idx; i < add_options.size(); i++) {
if (add_options[i].script.is_valid()) {
if (add_options[i].script->get_path() == p_path) {
idx = i;
break;
}
}
}
if (idx != -1) {
_add_node(idx);
}
}
void VisualShaderEditor::_add_texture_node(const String &p_path) { void VisualShaderEditor::_add_texture_node(const String &p_path) {
VisualShaderNodeTexture *texture = (VisualShaderNodeTexture *)_add_node(texture_node_option_idx, -1); VisualShaderNodeTexture *texture = (VisualShaderNodeTexture *)_add_node(texture_node_option_idx, -1);
texture->set_texture(ResourceLoader::load(p_path)); texture->set_texture(ResourceLoader::load(p_path));
@ -2072,14 +2089,24 @@ void VisualShaderEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
} else if (d.has("files")) { } else if (d.has("files")) {
if (d["files"].get_type() == Variant::POOL_STRING_ARRAY) { if (d["files"].get_type() == Variant::POOL_STRING_ARRAY) {
int j = 0;
PoolStringArray arr = d["files"]; PoolStringArray arr = d["files"];
for (int i = 0; i < arr.size(); i++) { for (int i = 0; i < arr.size(); i++) {
String type = ResourceLoader::get_resource_type(arr[i]); String type = ResourceLoader::get_resource_type(arr[i]);
if (ClassDB::get_parent_class(type) == "Texture") { if (type == "GDScript") {
saved_node_pos = p_point + Vector2(0, i * 210 * EDSCALE); Ref<Script> script = ResourceLoader::load(arr[i]);
if (script->get_instance_base_type() == "VisualShaderNodeCustom") {
saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
saved_node_pos_dirty = true;
_add_custom_node(arr[i]);
j++;
}
} else if (ClassDB::get_parent_class(type) == "Texture") {
saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
saved_node_pos_dirty = true; saved_node_pos_dirty = true;
_add_texture_node(arr[i]); _add_texture_node(arr[i]);
j++;
} }
} }
} }
@ -2698,6 +2725,7 @@ VisualShaderEditor::VisualShaderEditor() {
add_options.push_back(AddOption("DdYS", "Special", "Derivative", "VisualShaderNodeScalarDerivativeFunc", TTR("(Fragment/Light mode only) (Scalar) Derivative in 'y' using local differencing."), VisualShaderNodeScalarDerivativeFunc::FUNC_Y, VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true)); add_options.push_back(AddOption("DdYS", "Special", "Derivative", "VisualShaderNodeScalarDerivativeFunc", TTR("(Fragment/Light mode only) (Scalar) Derivative in 'y' using local differencing."), VisualShaderNodeScalarDerivativeFunc::FUNC_Y, VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
add_options.push_back(AddOption("Sum", "Special", "Derivative", "VisualShaderNodeVectorDerivativeFunc", TTR("(Fragment/Light mode only) (Vector) Sum of absolute derivative in 'x' and 'y'."), VisualShaderNodeVectorDerivativeFunc::FUNC_SUM, VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true)); add_options.push_back(AddOption("Sum", "Special", "Derivative", "VisualShaderNodeVectorDerivativeFunc", TTR("(Fragment/Light mode only) (Vector) Sum of absolute derivative in 'x' and 'y'."), VisualShaderNodeVectorDerivativeFunc::FUNC_SUM, VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
add_options.push_back(AddOption("SumS", "Special", "Derivative", "VisualShaderNodeScalarDerivativeFunc", TTR("(Fragment/Light mode only) (Scalar) Sum of absolute derivative in 'x' and 'y'."), VisualShaderNodeScalarDerivativeFunc::FUNC_SUM, VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true)); add_options.push_back(AddOption("SumS", "Special", "Derivative", "VisualShaderNodeScalarDerivativeFunc", TTR("(Fragment/Light mode only) (Scalar) Sum of absolute derivative in 'x' and 'y'."), VisualShaderNodeScalarDerivativeFunc::FUNC_SUM, VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
custom_node_option_idx = add_options.size();
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
@ -2739,6 +2767,7 @@ void VisualShaderEditorPlugin::make_visible(bool p_visible) {
//editor->animation_panel_make_visible(true); //editor->animation_panel_make_visible(true);
button->show(); button->show();
editor->make_bottom_panel_item_visible(visual_shader_editor); editor->make_bottom_panel_item_visible(visual_shader_editor);
visual_shader_editor->update_custom_nodes();
visual_shader_editor->set_process_input(true); visual_shader_editor->set_process_input(true);
//visual_shader_editor->set_process(true); //visual_shader_editor->set_process(true);
} else { } else {

View file

@ -149,13 +149,14 @@ class VisualShaderEditor : public VBoxContainer {
Vector<AddOption> add_options; Vector<AddOption> add_options;
int texture_node_option_idx; int texture_node_option_idx;
int custom_node_option_idx;
List<String> keyword_list; List<String> keyword_list;
void _draw_color_over_button(Object *obj, Color p_color); void _draw_color_over_button(Object *obj, Color p_color);
void _add_custom_node(const String &p_path);
void _add_texture_node(const String &p_path); void _add_texture_node(const String &p_path);
VisualShaderNode *_add_node(int p_idx, int p_op_idx = -1); VisualShaderNode *_add_node(int p_idx, int p_op_idx = -1);
void _update_custom_nodes();
void _update_options_menu(); void _update_options_menu();
void _show_preview_text(); void _show_preview_text();
@ -255,6 +256,7 @@ protected:
static void _bind_methods(); static void _bind_methods();
public: public:
void update_custom_nodes();
void add_plugin(const Ref<VisualShaderNodePlugin> &p_plugin); void add_plugin(const Ref<VisualShaderNodePlugin> &p_plugin);
void remove_plugin(const Ref<VisualShaderNodePlugin> &p_plugin); void remove_plugin(const Ref<VisualShaderNodePlugin> &p_plugin);