From d0dc195da85ab0b9b3446a0d523bc4b086b2a5f4 Mon Sep 17 00:00:00 2001 From: Yuri Roubinski Date: Sat, 3 Aug 2019 18:12:33 +0300 Subject: [PATCH] Some improvements to Mix visual shader function --- .../plugins/visual_shader_editor_plugin.cpp | 1 + scene/register_scene_types.cpp | 1 + scene/resources/visual_shader_nodes.cpp | 78 ++++++++++++++++--- scene/resources/visual_shader_nodes.h | 21 +++++ 4 files changed, 90 insertions(+), 11 deletions(-) diff --git a/editor/plugins/visual_shader_editor_plugin.cpp b/editor/plugins/visual_shader_editor_plugin.cpp index 4376e3e68f0..f2d18d0f934 100644 --- a/editor/plugins/visual_shader_editor_plugin.cpp +++ b/editor/plugins/visual_shader_editor_plugin.cpp @@ -2425,6 +2425,7 @@ VisualShaderEditor::VisualShaderEditor() { add_options.push_back(AddOption("Max", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the greater of two values."), VisualShaderNodeVectorOp::OP_MAX, VisualShaderNode::PORT_TYPE_VECTOR)); add_options.push_back(AddOption("Min", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the lesser of two values."), VisualShaderNodeVectorOp::OP_MIN, VisualShaderNode::PORT_TYPE_VECTOR)); add_options.push_back(AddOption("Mix", "Vector", "Functions", "VisualShaderNodeVectorInterp", TTR("Linear interpolation between two vectors."), -1, VisualShaderNode::PORT_TYPE_VECTOR)); + add_options.push_back(AddOption("MixS", "Vector", "Functions", "VisualShaderNodeVectorScalarMix", TTR("Linear interpolation between two vectors using scalar."), -1, VisualShaderNode::PORT_TYPE_VECTOR)); add_options.push_back(AddOption("Negate", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the opposite value of the parameter."), VisualShaderNodeVectorFunc::FUNC_NEGATE, VisualShaderNode::PORT_TYPE_VECTOR)); add_options.push_back(AddOption("Normalize", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Calculates the normalize product of vector."), VisualShaderNodeVectorFunc::FUNC_NORMALIZE, VisualShaderNode::PORT_TYPE_VECTOR)); add_options.push_back(AddOption("OneMinus", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("1.0 - vector"), VisualShaderNodeVectorFunc::FUNC_ONEMINUS, VisualShaderNode::PORT_TYPE_VECTOR)); diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index 974c9771e08..fd7cd3c17b6 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -509,6 +509,7 @@ void register_scene_types() { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); diff --git a/scene/resources/visual_shader_nodes.cpp b/scene/resources/visual_shader_nodes.cpp index a1a29ec9504..9195d80cfcb 100644 --- a/scene/resources/visual_shader_nodes.cpp +++ b/scene/resources/visual_shader_nodes.cpp @@ -2399,34 +2399,38 @@ VisualShaderNodeVectorRefract::VisualShaderNodeVectorRefract() { set_input_port_default_value(2, 0.0); } -////////////// Scalar Interp +////////////// Scalar Mix String VisualShaderNodeScalarInterp::get_caption() const { - return "Mix"; + return "ScalarMix"; } int VisualShaderNodeScalarInterp::get_input_port_count() const { return 3; } + VisualShaderNodeScalarInterp::PortType VisualShaderNodeScalarInterp::get_input_port_type(int p_port) const { return PORT_TYPE_SCALAR; } + String VisualShaderNodeScalarInterp::get_input_port_name(int p_port) const { if (p_port == 0) { return "a"; } else if (p_port == 1) { return "b"; } else { - return "c"; + return "weight"; } } int VisualShaderNodeScalarInterp::get_output_port_count() const { return 1; } + VisualShaderNodeScalarInterp::PortType VisualShaderNodeScalarInterp::get_output_port_type(int p_port) const { return PORT_TYPE_SCALAR; } + String VisualShaderNodeScalarInterp::get_output_port_name(int p_port) const { return "mix"; } @@ -2437,38 +2441,42 @@ String VisualShaderNodeScalarInterp::generate_code(Shader::Mode p_mode, VisualSh VisualShaderNodeScalarInterp::VisualShaderNodeScalarInterp() { set_input_port_default_value(0, 0.0); - set_input_port_default_value(1, 0.0); - set_input_port_default_value(2, 0.0); + set_input_port_default_value(1, 1.0); + set_input_port_default_value(2, 0.5); } -////////////// Vector Interp +////////////// Vector Mix String VisualShaderNodeVectorInterp::get_caption() const { - return "Mix"; + return "VectorMix"; } int VisualShaderNodeVectorInterp::get_input_port_count() const { return 3; } + VisualShaderNodeVectorInterp::PortType VisualShaderNodeVectorInterp::get_input_port_type(int p_port) const { return PORT_TYPE_VECTOR; } + String VisualShaderNodeVectorInterp::get_input_port_name(int p_port) const { if (p_port == 0) { return "a"; } else if (p_port == 1) { return "b"; } else { - return "c"; + return "weight"; } } int VisualShaderNodeVectorInterp::get_output_port_count() const { return 1; } + VisualShaderNodeVectorInterp::PortType VisualShaderNodeVectorInterp::get_output_port_type(int p_port) const { return PORT_TYPE_VECTOR; } + String VisualShaderNodeVectorInterp::get_output_port_name(int p_port) const { return "mix"; } @@ -2478,9 +2486,57 @@ String VisualShaderNodeVectorInterp::generate_code(Shader::Mode p_mode, VisualSh } VisualShaderNodeVectorInterp::VisualShaderNodeVectorInterp() { - set_input_port_default_value(0, Vector3()); - set_input_port_default_value(1, Vector3()); - set_input_port_default_value(2, Vector3()); + set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0)); + set_input_port_default_value(1, Vector3(1.0, 1.0, 1.0)); + set_input_port_default_value(2, Vector3(0.5, 0.5, 0.5)); +} + +////////////// Vector Mix (by scalar) + +String VisualShaderNodeVectorScalarMix::get_caption() const { + return "VectorScalarMix"; +} + +int VisualShaderNodeVectorScalarMix::get_input_port_count() const { + return 3; +} + +VisualShaderNodeVectorScalarMix::PortType VisualShaderNodeVectorScalarMix::get_input_port_type(int p_port) const { + if (p_port == 2) + return PORT_TYPE_SCALAR; + return PORT_TYPE_VECTOR; +} + +String VisualShaderNodeVectorScalarMix::get_input_port_name(int p_port) const { + if (p_port == 0) { + return "a"; + } else if (p_port == 1) { + return "b"; + } else { + return "weight"; + } +} + +int VisualShaderNodeVectorScalarMix::get_output_port_count() const { + return 1; +} + +VisualShaderNodeVectorScalarMix::PortType VisualShaderNodeVectorScalarMix::get_output_port_type(int p_port) const { + return PORT_TYPE_VECTOR; +} + +String VisualShaderNodeVectorScalarMix::get_output_port_name(int p_port) const { + return "mix"; +} + +String VisualShaderNodeVectorScalarMix::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const { + return "\t" + p_output_vars[0] + " = mix( " + p_input_vars[0] + " , " + p_input_vars[1] + " , " + p_input_vars[2] + " );\n"; +} + +VisualShaderNodeVectorScalarMix::VisualShaderNodeVectorScalarMix() { + set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0)); + set_input_port_default_value(1, Vector3(1.0, 1.0, 1.0)); + set_input_port_default_value(2, 0.5); } ////////////// Vector Compose diff --git a/scene/resources/visual_shader_nodes.h b/scene/resources/visual_shader_nodes.h index 4585e7fdcc4..f7efa396dc8 100644 --- a/scene/resources/visual_shader_nodes.h +++ b/scene/resources/visual_shader_nodes.h @@ -1170,6 +1170,27 @@ public: VisualShaderNodeVectorInterp(); }; +/////////////////////////////////////// + +class VisualShaderNodeVectorScalarMix : public VisualShaderNode { + GDCLASS(VisualShaderNodeVectorScalarMix, VisualShaderNode); + +public: + virtual String get_caption() const; + + virtual int get_input_port_count() const; + virtual PortType get_input_port_type(int p_port) const; + virtual String get_input_port_name(int p_port) const; + + virtual int get_output_port_count() const; + virtual PortType get_output_port_type(int p_port) const; + virtual String get_output_port_name(int p_port) const; + + virtual String generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview = false) const; //if no output is connected, the output var passed will be empty. if no input is connected and input is NIL, the input var passed will be empty + + VisualShaderNodeVectorScalarMix(); +}; + /////////////////////////////////////// /// COMPOSE ///////////////////////////////////////