summaryrefslogtreecommitdiffstats
path: root/scene/resources/visual_shader.cpp
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2024-08-26 23:28:46 +0200
committerRémi Verschelde <rverschelde@gmail.com>2024-08-26 23:28:46 +0200
commit0125b0eb03f4db2527282664ab7a15de12248886 (patch)
treee9dbb93709e9959d9463f20c9a4de4108773a514 /scene/resources/visual_shader.cpp
parenta041772ecf1406586c62fc5b4608fb556acbbb84 (diff)
parentf202a3d24e2cea5d22f5b507c2c34311acd8de2b (diff)
downloadredot-engine-0125b0eb03f4db2527282664ab7a15de12248886.tar.gz
Merge pull request #94215 from Chaosus/vs_preview_and_param_list
Add a material preview to visual shader editor
Diffstat (limited to 'scene/resources/visual_shader.cpp')
-rw-r--r--scene/resources/visual_shader.cpp76
1 files changed, 74 insertions, 2 deletions
diff --git a/scene/resources/visual_shader.cpp b/scene/resources/visual_shader.cpp
index 3fabc867eb..a144c5ba83 100644
--- a/scene/resources/visual_shader.cpp
+++ b/scene/resources/visual_shader.cpp
@@ -32,6 +32,7 @@
#include "core/templates/rb_map.h"
#include "core/templates/vmap.h"
+#include "core/variant/variant_utility.h"
#include "servers/rendering/shader_types.h"
#include "visual_shader_nodes.h"
#include "visual_shader_particle_nodes.h"
@@ -897,6 +898,44 @@ VisualShader::VaryingType VisualShader::get_varying_type(const String &p_name) {
return varyings[p_name].type;
}
+void VisualShader::_set_preview_shader_parameter(const String &p_name, const Variant &p_value) {
+#ifdef TOOLS_ENABLED
+ if (Engine::get_singleton()->is_editor_hint()) {
+ if (p_value.get_type() == Variant::NIL) {
+ if (!preview_params.erase(p_name)) {
+ return;
+ }
+ } else {
+ Variant *var = preview_params.getptr(p_name);
+ if (var != nullptr && *var == p_value) {
+ return;
+ }
+ preview_params.insert(p_name, p_value);
+ }
+ emit_changed();
+ }
+#endif // TOOLS_ENABLED
+}
+
+Variant VisualShader::_get_preview_shader_parameter(const String &p_name) const {
+#ifdef TOOLS_ENABLED
+ if (Engine::get_singleton()->is_editor_hint()) {
+ ERR_FAIL_COND_V(!preview_params.has(p_name), Variant());
+ return preview_params.get(p_name);
+ }
+#endif // TOOLS_ENABLED
+ return Variant();
+}
+
+bool VisualShader::_has_preview_shader_parameter(const String &p_name) const {
+#ifdef TOOLS_ENABLED
+ if (Engine::get_singleton()->is_editor_hint()) {
+ return preview_params.has(p_name);
+ }
+#endif // TOOLS_ENABLED
+ return false;
+}
+
void VisualShader::add_node(Type p_type, const Ref<VisualShaderNode> &p_node, const Vector2 &p_position, int p_id) {
ERR_FAIL_COND(p_node.is_null());
ERR_FAIL_COND(p_id < 2);
@@ -1695,7 +1734,16 @@ bool VisualShader::_set(const StringName &p_name, const Variant &p_value) {
}
_queue_update();
return true;
- } else if (prop_name.begins_with("nodes/")) {
+ }
+#ifdef TOOLS_ENABLED
+ else if (prop_name.begins_with("preview_params/") && Engine::get_singleton()->is_editor_hint()) {
+ String param_name = prop_name.get_slicec('/', 1);
+ Variant value = VariantUtilityFunctions::str_to_var(p_value);
+ preview_params[param_name] = value;
+ return true;
+ }
+#endif
+ else if (prop_name.begins_with("nodes/")) {
String typestr = prop_name.get_slicec('/', 1);
Type type = TYPE_VERTEX;
for (int i = 0; i < TYPE_MAX; i++) {
@@ -1767,7 +1815,19 @@ bool VisualShader::_get(const StringName &p_name, Variant &r_ret) const {
r_ret = String();
}
return true;
- } else if (prop_name.begins_with("nodes/")) {
+ }
+#ifdef TOOLS_ENABLED
+ else if (prop_name.begins_with("preview_params/") && Engine::get_singleton()->is_editor_hint()) {
+ String param_name = prop_name.get_slicec('/', 1);
+ if (preview_params.has(param_name)) {
+ r_ret = VariantUtilityFunctions::var_to_str(preview_params[param_name]);
+ } else {
+ r_ret = String();
+ }
+ return true;
+ }
+#endif // TOOLS_ENABLED
+ else if (prop_name.begins_with("nodes/")) {
String typestr = prop_name.get_slicec('/', 1);
Type type = TYPE_VERTEX;
for (int i = 0; i < TYPE_MAX; i++) {
@@ -1864,6 +1924,14 @@ void VisualShader::_get_property_list(List<PropertyInfo> *p_list) const {
p_list->push_back(PropertyInfo(Variant::STRING, vformat("%s/%s", PNAME("varyings"), E.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
}
+#ifdef TOOLS_ENABLED
+ if (Engine::get_singleton()->is_editor_hint()) {
+ for (const KeyValue<String, Variant> &E : preview_params) {
+ p_list->push_back(PropertyInfo(Variant::STRING, vformat("%s/%s", PNAME("preview_params"), E.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
+ }
+ }
+#endif // TOOLS_ENABLED
+
for (int i = 0; i < TYPE_MAX; i++) {
for (const KeyValue<int, Node> &E : graph[i].nodes) {
String prop_name = "nodes/";
@@ -2943,6 +3011,10 @@ void VisualShader::_bind_methods() {
ClassDB::bind_method(D_METHOD("remove_varying", "name"), &VisualShader::remove_varying);
ClassDB::bind_method(D_METHOD("has_varying", "name"), &VisualShader::has_varying);
+ ClassDB::bind_method(D_METHOD("_set_preview_shader_parameter", "name", "value"), &VisualShader::_set_preview_shader_parameter);
+ ClassDB::bind_method(D_METHOD("_get_preview_shader_parameter", "name"), &VisualShader::_get_preview_shader_parameter);
+ ClassDB::bind_method(D_METHOD("_has_preview_shader_parameter", "name"), &VisualShader::_has_preview_shader_parameter);
+
ClassDB::bind_method(D_METHOD("_update_shader"), &VisualShader::_update_shader);
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_graph_offset", "get_graph_offset");