From b217babca2374575a5b725c48e6dc044c1b78aab Mon Sep 17 00:00:00 2001 From: Yuri Roubinsky Date: Fri, 11 Oct 2019 10:26:57 +0300 Subject: [VShaders] Added sampler port to CubeMap, fixed parsing in expresssion s --- scene/resources/visual_shader.cpp | 20 +---- scene/resources/visual_shader.h | 11 --- scene/resources/visual_shader_nodes.cpp | 145 ++++++++++++++++++-------------- scene/resources/visual_shader_nodes.h | 10 +++ 4 files changed, 91 insertions(+), 95 deletions(-) diff --git a/scene/resources/visual_shader.cpp b/scene/resources/visual_shader.cpp index cd77c8a850..bd6835f816 100644 --- a/scene/resources/visual_shader.cpp +++ b/scene/resources/visual_shader.cpp @@ -1067,16 +1067,7 @@ Error VisualShader::_write_node(Type type, StringBuilder &global_code, StringBui VisualShaderNodeUniform *uniform = (VisualShaderNodeUniform *)graph[type].nodes[from_node].node.ptr(); if (uniform) { - inputs[i] = ""; - switch (uniform->get_uniform_type()) { - case VisualShaderNodeUniform::UTYPE_CUBEMAP: - inputs[i] += "cube_"; - break; - case VisualShaderNodeUniform::UTYPE_SAMPLER2D: - inputs[i] += "s2d_"; - break; - } - inputs[i] += uniform->get_uniform_name(); + inputs[i] = uniform->get_uniform_name(); } else { inputs[i] = ""; } @@ -1981,16 +1972,7 @@ void VisualShaderNodeUniform::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::STRING, "uniform_name"), "set_uniform_name", "get_uniform_name"); } -int VisualShaderNodeUniform::get_uniform_type() const { - return (int)uniform_type; -} - -void VisualShaderNodeUniform::set_uniform_type(int p_type) { - uniform_type = (UniformType)p_type; -} - VisualShaderNodeUniform::VisualShaderNodeUniform() { - uniform_type = UTYPE_NONE; } ////////////// GroupBase diff --git a/scene/resources/visual_shader.h b/scene/resources/visual_shader.h index 016213c399..4f73316404 100644 --- a/scene/resources/visual_shader.h +++ b/scene/resources/visual_shader.h @@ -353,16 +353,8 @@ public: class VisualShaderNodeUniform : public VisualShaderNode { GDCLASS(VisualShaderNodeUniform, VisualShaderNode); -public: - enum UniformType { - UTYPE_NONE, - UTYPE_CUBEMAP, - UTYPE_SAMPLER2D, - }; - private: String uniform_name; - UniformType uniform_type; protected: static void _bind_methods(); @@ -371,9 +363,6 @@ public: void set_uniform_name(const String &p_name); String get_uniform_name() const; - int get_uniform_type() const; - void set_uniform_type(int p_type); - VisualShaderNodeUniform(); }; diff --git a/scene/resources/visual_shader_nodes.cpp b/scene/resources/visual_shader_nodes.cpp index c56d5d7213..a7df736c78 100644 --- a/scene/resources/visual_shader_nodes.cpp +++ b/scene/resources/visual_shader_nodes.cpp @@ -386,7 +386,7 @@ String VisualShaderNodeTexture::get_input_port_name(int p_port) const { case 1: return "lod"; case 2: - return "sampler"; + return "sampler2D"; default: return ""; } @@ -474,62 +474,23 @@ String VisualShaderNodeTexture::generate_code(Shader::Mode p_mode, VisualShader: if (source == SOURCE_PORT) { String id = p_input_vars[2]; - VisualShaderNodeUniform::UniformType utype = VisualShaderNodeUniform::UTYPE_NONE; - if (id.begins_with("cube_")) { - utype = VisualShaderNodeUniform::UTYPE_CUBEMAP; - id = id.substr(5); - } else if (id.begins_with("s2d_")) { - utype = VisualShaderNodeUniform::UTYPE_SAMPLER2D; - id = id.substr(4); - } - String code; if (id == String()) { code += "\tvec4 " + id + "_tex_read = vec4(0.0);\n"; } else { if (p_input_vars[0] == String()) { // Use UV by default. - switch (utype) { - case VisualShaderNodeUniform::UTYPE_CUBEMAP: - if (p_input_vars[1] == String()) { - code += "\tvec4 " + id + "_tex_read = texture( " + id + " , vec3( UV, 0.0 ) );\n"; - } else { - code += "\tvec4 " + id + "_tex_read = textureLod( " + id + " , vec3( UV, 0.0 ) , " + p_input_vars[1] + " );\n"; - } - break; - case VisualShaderNodeUniform::UTYPE_SAMPLER2D: - if (p_input_vars[1] == String()) { - code += "\tvec4 " + id + "_tex_read = texture( " + id + " , UV.xy );\n"; - } else { - code += "\tvec4 " + id + "_tex_read = textureLod( " + id + " , UV.xy , " + p_input_vars[1] + " );\n"; - } - break; - default: - break; + + if (p_input_vars[1] == String()) { + code += "\tvec4 " + id + "_tex_read = texture( " + id + " , UV.xy );\n"; + } else { + code += "\tvec4 " + id + "_tex_read = textureLod( " + id + " , UV.xy , " + p_input_vars[1] + " );\n"; } + } else if (p_input_vars[1] == String()) { //no lod - - switch (utype) { - case VisualShaderNodeUniform::UTYPE_CUBEMAP: - code += "\tvec4 " + id + "_tex_read = texture( " + id + " , " + p_input_vars[0] + " );\n"; - break; - case VisualShaderNodeUniform::UTYPE_SAMPLER2D: - code += "\tvec4 " + id + "_tex_read = texture( " + id + " , " + p_input_vars[0] + ".xy );\n"; - break; - default: - break; - } + code += "\tvec4 " + id + "_tex_read = texture( " + id + " , " + p_input_vars[0] + ".xy );\n"; } else { - switch (utype) { - case VisualShaderNodeUniform::UTYPE_CUBEMAP: - code += "\tvec4 " + id + "_tex_read = textureLod( " + id + " , " + p_input_vars[0] + " , " + p_input_vars[1] + " );\n"; - break; - case VisualShaderNodeUniform::UTYPE_SAMPLER2D: - code += "\tvec4 " + id + "_tex_read = textureLod( " + id + " , " + p_input_vars[0] + ".xy , " + p_input_vars[1] + " );\n"; - break; - default: - break; - } + code += "\tvec4 " + id + "_tex_read = textureLod( " + id + " , " + p_input_vars[0] + ".xy , " + p_input_vars[1] + " );\n"; } code += "\t" + p_output_vars[0] + " = " + id + "_tex_read.rgb;\n"; @@ -769,15 +730,33 @@ String VisualShaderNodeCubeMap::get_caption() const { } int VisualShaderNodeCubeMap::get_input_port_count() const { - return 2; + return 3; } VisualShaderNodeCubeMap::PortType VisualShaderNodeCubeMap::get_input_port_type(int p_port) const { - return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR; + switch (p_port) { + case 0: + return PORT_TYPE_VECTOR; + case 1: + return PORT_TYPE_SCALAR; + case 2: + return PORT_TYPE_SAMPLER; + default: + return PORT_TYPE_SCALAR; + } } String VisualShaderNodeCubeMap::get_input_port_name(int p_port) const { - return p_port == 0 ? "uv" : "lod"; + switch (p_port) { + case 0: + return "uv"; + case 1: + return "lod"; + case 2: + return "samplerCube"; + default: + return ""; + } } int VisualShaderNodeCubeMap::get_output_port_count() const { @@ -803,19 +782,37 @@ Vector VisualShaderNodeCubeMap::get_default_t String VisualShaderNodeCubeMap::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const { - String u = "uniform samplerCube " + make_unique_id(p_type, p_id, "cube"); - switch (texture_type) { - case TYPE_DATA: break; - case TYPE_COLOR: u += " : hint_albedo"; break; - case TYPE_NORMALMAP: u += " : hint_normal"; break; + if (source == SOURCE_TEXTURE) { + String u = "uniform samplerCube " + make_unique_id(p_type, p_id, "cube"); + switch (texture_type) { + case TYPE_DATA: break; + case TYPE_COLOR: u += " : hint_albedo"; break; + case TYPE_NORMALMAP: u += " : hint_normal"; break; + } + return u + ";"; } - return u + ";"; + return String(); } String VisualShaderNodeCubeMap::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 { - String id = make_unique_id(p_type, p_id, "cube"); String code; + String id; + if (source == SOURCE_TEXTURE) { + id = make_unique_id(p_type, p_id, "cube"); + } else if (source == SOURCE_PORT) { + id = p_input_vars[2]; + } else { + return String(); + } + + if (id == String()) { + code += "\tvec4 " + id + "_read = vec4(0.0);\n"; + code += "\t" + p_output_vars[0] + " = " + id + "_read.rgb;\n"; + code += "\t" + p_output_vars[1] + " = " + id + "_read.a;\n"; + return code; + } + if (p_input_vars[0] == String()) { // Use UV by default. if (p_input_vars[1] == String()) { @@ -830,9 +827,9 @@ String VisualShaderNodeCubeMap::generate_code(Shader::Mode p_mode, VisualShader: } else { code += "\tvec4 " + id + "_read = textureLod( " + id + " , " + p_input_vars[0] + " , " + p_input_vars[1] + " );\n"; } - code += "\t" + p_output_vars[0] + " = " + id + "_read.rgb;\n"; code += "\t" + p_output_vars[1] + " = " + id + "_read.a;\n"; + return code; } @@ -843,6 +840,16 @@ String VisualShaderNodeCubeMap::get_input_port_default_hint(int p_port) const { return ""; } +void VisualShaderNodeCubeMap::set_source(Source p_source) { + source = p_source; + emit_changed(); + emit_signal("editor_refresh_request"); +} + +VisualShaderNodeCubeMap::Source VisualShaderNodeCubeMap::get_source() const { + return source; +} + void VisualShaderNodeCubeMap::set_cube_map(Ref p_value) { cube_map = p_value; @@ -865,22 +872,32 @@ VisualShaderNodeCubeMap::TextureType VisualShaderNodeCubeMap::get_texture_type() Vector VisualShaderNodeCubeMap::get_editable_properties() const { Vector props; - props.push_back("cube_map"); - props.push_back("texture_type"); + props.push_back("source"); + if (source == SOURCE_TEXTURE) { + props.push_back("cube_map"); + props.push_back("texture_type"); + } return props; } void VisualShaderNodeCubeMap::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_source", "value"), &VisualShaderNodeCubeMap::set_source); + ClassDB::bind_method(D_METHOD("get_source"), &VisualShaderNodeCubeMap::get_source); + ClassDB::bind_method(D_METHOD("set_cube_map", "value"), &VisualShaderNodeCubeMap::set_cube_map); ClassDB::bind_method(D_METHOD("get_cube_map"), &VisualShaderNodeCubeMap::get_cube_map); ClassDB::bind_method(D_METHOD("set_texture_type", "value"), &VisualShaderNodeCubeMap::set_texture_type); ClassDB::bind_method(D_METHOD("get_texture_type"), &VisualShaderNodeCubeMap::get_texture_type); + ADD_PROPERTY(PropertyInfo(Variant::INT, "source", PROPERTY_HINT_ENUM, "Texture,SamplerPort"), "set_source", "get_source"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "cube_map", PROPERTY_HINT_RESOURCE_TYPE, "CubeMap"), "set_cube_map", "get_cube_map"); ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_type", PROPERTY_HINT_ENUM, "Data,Color,Normalmap"), "set_texture_type", "get_texture_type"); + BIND_ENUM_CONSTANT(SOURCE_TEXTURE); + BIND_ENUM_CONSTANT(SOURCE_PORT); + BIND_ENUM_CONSTANT(TYPE_DATA); BIND_ENUM_CONSTANT(TYPE_COLOR); BIND_ENUM_CONSTANT(TYPE_NORMALMAP); @@ -3177,7 +3194,7 @@ String VisualShaderNodeTextureUniform::get_output_port_name(int p_port) const { case 1: return "alpha"; case 2: - return "sampler"; + return "sampler2D"; default: return ""; } @@ -3284,7 +3301,6 @@ String VisualShaderNodeTextureUniform::get_input_port_default_hint(int p_port) c VisualShaderNodeTextureUniform::VisualShaderNodeTextureUniform() { texture_type = TYPE_DATA; color_default = COLOR_DEFAULT_WHITE; - set_uniform_type(VisualShaderNodeUniform::UTYPE_SAMPLER2D); } ////////////// Texture Uniform (Triplanar) @@ -3403,7 +3419,7 @@ VisualShaderNodeCubeMapUniform::PortType VisualShaderNodeCubeMapUniform::get_out } String VisualShaderNodeCubeMapUniform::get_output_port_name(int p_port) const { - return "sampler"; + return "samplerCube"; } int VisualShaderNodeCubeMapUniform::get_input_port_count() const { @@ -3450,7 +3466,6 @@ String VisualShaderNodeCubeMapUniform::generate_code(Shader::Mode p_mode, Visual } VisualShaderNodeCubeMapUniform::VisualShaderNodeCubeMapUniform() { - set_uniform_type(VisualShaderNodeUniform::UTYPE_CUBEMAP); } ////////////// If diff --git a/scene/resources/visual_shader_nodes.h b/scene/resources/visual_shader_nodes.h index 350e73408c..339e59bda9 100644 --- a/scene/resources/visual_shader_nodes.h +++ b/scene/resources/visual_shader_nodes.h @@ -259,6 +259,11 @@ class VisualShaderNodeCubeMap : public VisualShaderNode { Ref cube_map; public: + enum Source { + SOURCE_TEXTURE, + SOURCE_PORT + }; + enum TextureType { TYPE_DATA, TYPE_COLOR, @@ -266,6 +271,7 @@ public: }; private: + Source source; TextureType texture_type; protected: @@ -287,6 +293,9 @@ public: virtual String generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) 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 + void set_source(Source p_source); + Source get_source() const; + void set_cube_map(Ref p_value); Ref get_cube_map() const; @@ -299,6 +308,7 @@ public: }; VARIANT_ENUM_CAST(VisualShaderNodeCubeMap::TextureType) +VARIANT_ENUM_CAST(VisualShaderNodeCubeMap::Source) /////////////////////////////////////// /// OPS -- cgit v1.2.3