summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuri Rubinsky <chaosus89@gmail.com>2023-01-31 22:55:42 +0300
committerGitHub <noreply@github.com>2023-01-31 22:55:42 +0300
commit0810ecaafdbee3ea747219e6ab3a8de5d2216a09 (patch)
tree384e7008f35e63064f61eb3105e5687cc84d3203
parent935a6ef46b7c6af4239b6adce41b8198f051b57a (diff)
parentbcecb4fe37b250302ea78fb3991375ae9eb17f9d (diff)
Merge pull request #72436 from Chaosus/vs_switch_improvement
-rw-r--r--scene/resources/visual_shader_nodes.cpp35
1 files changed, 27 insertions, 8 deletions
diff --git a/scene/resources/visual_shader_nodes.cpp b/scene/resources/visual_shader_nodes.cpp
index 12be0f46a6..0695492e7f 100644
--- a/scene/resources/visual_shader_nodes.cpp
+++ b/scene/resources/visual_shader_nodes.cpp
@@ -6923,15 +6923,34 @@ void VisualShaderNodeSwitch::_bind_methods() { // static
}
String VisualShaderNodeSwitch::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 {
+ bool use_mix = false;
+ switch (op_type) {
+ case OP_TYPE_FLOAT: {
+ use_mix = true;
+ } break;
+ case OP_TYPE_VECTOR_2D: {
+ use_mix = true;
+ } break;
+ case OP_TYPE_VECTOR_3D: {
+ use_mix = true;
+ } break;
+ case OP_TYPE_VECTOR_4D: {
+ use_mix = true;
+ } break;
+ default: {
+ } break;
+ }
+
String code;
- code += " if(" + p_input_vars[0] + ")\n";
- code += " {\n";
- code += " " + p_output_vars[0] + " = " + p_input_vars[1] + ";\n";
- code += " }\n";
- code += " else\n";
- code += " {\n";
- code += " " + p_output_vars[0] + " = " + p_input_vars[2] + ";\n";
- code += " }\n";
+ if (use_mix) {
+ code += " " + p_output_vars[0] + " = mix(" + p_input_vars[2] + ", " + p_input_vars[1] + ", float(" + p_input_vars[0] + "));\n";
+ } else {
+ code += " if (" + p_input_vars[0] + ") {\n";
+ code += " " + p_output_vars[0] + " = " + p_input_vars[1] + ";\n";
+ code += " } else {\n";
+ code += " " + p_output_vars[0] + " = " + p_input_vars[2] + ";\n";
+ code += " }\n";
+ }
return code;
}