Auto-set a first compatible uniform on dragging to create a UniformRef

This commit is contained in:
Yuri Roubinsky 2021-07-12 10:34:40 +03:00
parent b27683111b
commit 7d34701d63
3 changed files with 48 additions and 2 deletions

View File

@ -2274,6 +2274,8 @@ void VisualShaderEditor::_setup_node(VisualShaderNode *p_node, int p_op_idx) {
void VisualShaderEditor::_add_node(int p_idx, int p_op_idx, String p_resource_path, int p_node_idx) {
ERR_FAIL_INDEX(p_idx, add_options.size());
VisualShader::Type type = get_current_shader_type();
Ref<VisualShaderNode> vsnode;
bool is_custom = add_options[p_idx].is_custom;
@ -2300,6 +2302,29 @@ void VisualShaderEditor::_add_node(int p_idx, int p_op_idx, String p_resource_pa
}
}
VisualShaderNodeUniformRef *uniform_ref = Object::cast_to<VisualShaderNodeUniformRef>(vsn);
if (uniform_ref && to_node != -1 && to_slot != -1) {
VisualShaderNode::PortType input_port_type = visual_shader->get_node(type, to_node)->get_input_port_type(to_slot);
bool success = false;
for (int i = 0; i < uniform_ref->get_uniforms_count(); i++) {
if (uniform_ref->get_port_type_by_index(i) == input_port_type) {
uniform_ref->set_uniform_name(uniform_ref->get_uniform_name_by_index(i));
success = true;
break;
}
}
if (!success) {
for (int i = 0; i < uniform_ref->get_uniforms_count(); i++) {
if (visual_shader->is_port_types_compatible(uniform_ref->get_port_type_by_index(i), input_port_type)) {
uniform_ref->set_uniform_name(uniform_ref->get_uniform_name_by_index(i));
break;
}
}
}
}
vsnode = Ref<VisualShaderNode>(vsn);
} else {
ERR_FAIL_COND(add_options[p_idx].script.is_null());
@ -2320,8 +2345,6 @@ void VisualShaderEditor::_add_node(int p_idx, int p_op_idx, String p_resource_pa
}
saved_node_pos_dirty = false;
VisualShader::Type type = get_current_shader_type();
int id_to_use = visual_shader->get_valid_node_id(type);
if (p_resource_path.is_empty()) {

View File

@ -2708,6 +2708,28 @@ VisualShaderNodeUniformRef::UniformType VisualShaderNodeUniformRef::get_uniform_
return UniformType::UNIFORM_TYPE_FLOAT;
}
VisualShaderNodeUniformRef::PortType VisualShaderNodeUniformRef::get_port_type_by_index(int p_idx) const {
if (p_idx >= 0 && p_idx < uniforms.size()) {
switch (uniforms[p_idx].type) {
case UniformType::UNIFORM_TYPE_FLOAT:
return PORT_TYPE_SCALAR;
case UniformType::UNIFORM_TYPE_INT:
return PORT_TYPE_SCALAR_INT;
case UniformType::UNIFORM_TYPE_SAMPLER:
return PORT_TYPE_SAMPLER;
case UniformType::UNIFORM_TYPE_VECTOR:
return PORT_TYPE_VECTOR;
case UniformType::UNIFORM_TYPE_TRANSFORM:
return PORT_TYPE_TRANSFORM;
case UniformType::UNIFORM_TYPE_COLOR:
return PORT_TYPE_VECTOR;
default:
break;
}
}
return PORT_TYPE_SCALAR;
}
String VisualShaderNodeUniformRef::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 {
switch (uniform_type) {
case UniformType::UNIFORM_TYPE_FLOAT:

View File

@ -517,6 +517,7 @@ public:
String get_uniform_name_by_index(int p_idx) const;
UniformType get_uniform_type_by_name(const String &p_name) const;
UniformType get_uniform_type_by_index(int p_idx) const;
PortType get_port_type_by_index(int p_idx) const;
virtual Vector<StringName> get_editable_properties() const override;