2020-05-10 10:53:46 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* shader_globals_editor.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2020-04-17 02:52:00 +00:00
|
|
|
#include "shader_globals_editor.h"
|
2022-02-12 01:46:22 +00:00
|
|
|
|
|
|
|
#include "core/config/project_settings.h"
|
|
|
|
#include "editor/editor_node.h"
|
2022-03-25 17:06:46 +00:00
|
|
|
#include "editor/editor_undo_redo_manager.h"
|
2022-01-10 18:46:41 +00:00
|
|
|
#include "servers/rendering/shader_language.h"
|
2020-04-17 02:52:00 +00:00
|
|
|
|
|
|
|
static const char *global_var_type_names[RS::GLOBAL_VAR_TYPE_MAX] = {
|
|
|
|
"bool",
|
|
|
|
"bvec2",
|
|
|
|
"bvec3",
|
|
|
|
"bvec4",
|
|
|
|
"int",
|
|
|
|
"ivec2",
|
|
|
|
"ivec3",
|
|
|
|
"ivec4",
|
|
|
|
"rect2i",
|
|
|
|
"uint",
|
|
|
|
"uvec2",
|
|
|
|
"uvec3",
|
|
|
|
"uvec4",
|
|
|
|
"float",
|
|
|
|
"vec2",
|
|
|
|
"vec3",
|
|
|
|
"vec4",
|
|
|
|
"color",
|
|
|
|
"rect2",
|
|
|
|
"mat2",
|
|
|
|
"mat3",
|
|
|
|
"mat4",
|
|
|
|
"transform_2d",
|
|
|
|
"transform",
|
|
|
|
"sampler2D",
|
|
|
|
"sampler2DArray",
|
|
|
|
"sampler3D",
|
|
|
|
"samplerCube",
|
|
|
|
};
|
|
|
|
|
|
|
|
class ShaderGlobalsEditorInterface : public Object {
|
|
|
|
GDCLASS(ShaderGlobalsEditorInterface, Object)
|
|
|
|
|
2022-12-05 10:46:16 +00:00
|
|
|
void _set_var(const StringName &p_name, const Variant &p_value, const Variant &p_prev_value) {
|
2022-12-23 22:53:16 +00:00
|
|
|
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
|
2020-04-17 02:52:00 +00:00
|
|
|
|
2021-07-19 19:10:05 +00:00
|
|
|
undo_redo->create_action(TTR("Set Shader Global Variable"));
|
2022-08-27 09:22:43 +00:00
|
|
|
undo_redo->add_do_method(RS::get_singleton(), "global_shader_parameter_set", p_name, p_value);
|
2022-12-05 10:46:16 +00:00
|
|
|
undo_redo->add_undo_method(RS::get_singleton(), "global_shader_parameter_set", p_name, p_prev_value);
|
2022-08-27 09:22:43 +00:00
|
|
|
RS::GlobalShaderParameterType type = RS::get_singleton()->global_shader_parameter_get_type(p_name);
|
2020-04-17 02:52:00 +00:00
|
|
|
Dictionary gv;
|
|
|
|
gv["type"] = global_var_type_names[type];
|
|
|
|
if (type >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
|
2022-05-02 23:43:50 +00:00
|
|
|
Ref<Resource> res = p_value;
|
2020-04-17 02:52:00 +00:00
|
|
|
if (res.is_valid()) {
|
|
|
|
gv["value"] = res->get_path();
|
|
|
|
} else {
|
|
|
|
gv["value"] = "";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gv["value"] = p_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
String path = "shader_globals/" + String(p_name);
|
|
|
|
undo_redo->add_do_property(ProjectSettings::get_singleton(), path, gv);
|
2022-10-18 14:43:37 +00:00
|
|
|
undo_redo->add_undo_property(ProjectSettings::get_singleton(), path, GLOBAL_GET(path));
|
2020-04-17 02:52:00 +00:00
|
|
|
undo_redo->add_do_method(this, "_var_changed");
|
|
|
|
undo_redo->add_undo_method(this, "_var_changed");
|
|
|
|
block_update = true;
|
2022-12-05 10:46:16 +00:00
|
|
|
undo_redo->commit_action();
|
2020-04-17 02:52:00 +00:00
|
|
|
block_update = false;
|
2022-12-05 10:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _var_changed() {
|
|
|
|
emit_signal(SNAME("var_changed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods() {
|
|
|
|
ClassDB::bind_method("_var_changed", &ShaderGlobalsEditorInterface::_var_changed);
|
|
|
|
ADD_SIGNAL(MethodInfo("var_changed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _set(const StringName &p_name, const Variant &p_value) {
|
|
|
|
Variant existing = RS::get_singleton()->global_shader_parameter_get(p_name);
|
|
|
|
|
|
|
|
if (existing.get_type() == Variant::NIL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-18 14:46:56 +00:00
|
|
|
callable_mp(this, &ShaderGlobalsEditorInterface::_set_var).call_deferred(p_name, p_value, existing);
|
2020-04-17 02:52:00 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _get(const StringName &p_name, Variant &r_ret) const {
|
2022-08-27 09:22:43 +00:00
|
|
|
r_ret = RS::get_singleton()->global_shader_parameter_get(p_name);
|
2020-04-17 02:52:00 +00:00
|
|
|
return r_ret.get_type() != Variant::NIL;
|
|
|
|
}
|
|
|
|
void _get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
Vector<StringName> variables;
|
2022-08-27 09:22:43 +00:00
|
|
|
variables = RS::get_singleton()->global_shader_parameter_get_list();
|
2020-04-17 02:52:00 +00:00
|
|
|
for (int i = 0; i < variables.size(); i++) {
|
|
|
|
PropertyInfo pinfo;
|
|
|
|
pinfo.name = variables[i];
|
|
|
|
|
2022-08-27 09:22:43 +00:00
|
|
|
switch (RS::get_singleton()->global_shader_parameter_get_type(variables[i])) {
|
2020-04-17 02:52:00 +00:00
|
|
|
case RS::GLOBAL_VAR_TYPE_BOOL: {
|
|
|
|
pinfo.type = Variant::BOOL;
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_BVEC2: {
|
|
|
|
pinfo.type = Variant::INT;
|
|
|
|
pinfo.hint = PROPERTY_HINT_FLAGS;
|
|
|
|
pinfo.hint_string = "x,y";
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_BVEC3: {
|
|
|
|
pinfo.type = Variant::INT;
|
|
|
|
pinfo.hint = PROPERTY_HINT_FLAGS;
|
|
|
|
pinfo.hint_string = "x,y,z";
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_BVEC4: {
|
|
|
|
pinfo.type = Variant::INT;
|
|
|
|
pinfo.hint = PROPERTY_HINT_FLAGS;
|
|
|
|
pinfo.hint_string = "x,y,z,w";
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_INT: {
|
|
|
|
pinfo.type = Variant::INT;
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_IVEC2: {
|
|
|
|
pinfo.type = Variant::VECTOR2I;
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_IVEC3: {
|
|
|
|
pinfo.type = Variant::VECTOR3I;
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_IVEC4: {
|
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
|
|
|
pinfo.type = Variant::VECTOR4I;
|
2020-04-17 02:52:00 +00:00
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_RECT2I: {
|
|
|
|
pinfo.type = Variant::RECT2I;
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_UINT: {
|
|
|
|
pinfo.type = Variant::INT;
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_UVEC2: {
|
|
|
|
pinfo.type = Variant::VECTOR2I;
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_UVEC3: {
|
|
|
|
pinfo.type = Variant::VECTOR3I;
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_UVEC4: {
|
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
|
|
|
pinfo.type = Variant::VECTOR4I;
|
2020-04-17 02:52:00 +00:00
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_FLOAT: {
|
|
|
|
pinfo.type = Variant::FLOAT;
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_VEC2: {
|
|
|
|
pinfo.type = Variant::VECTOR2;
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_VEC3: {
|
|
|
|
pinfo.type = Variant::VECTOR3;
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_VEC4: {
|
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
|
|
|
pinfo.type = Variant::VECTOR4;
|
2020-04-17 02:52:00 +00:00
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_RECT2: {
|
|
|
|
pinfo.type = Variant::RECT2;
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_COLOR: {
|
|
|
|
pinfo.type = Variant::COLOR;
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_MAT2: {
|
2023-03-15 06:40:06 +00:00
|
|
|
pinfo.type = Variant::PACKED_FLOAT32_ARRAY;
|
2020-04-17 02:52:00 +00:00
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_MAT3: {
|
|
|
|
pinfo.type = Variant::BASIS;
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
|
|
|
|
pinfo.type = Variant::TRANSFORM2D;
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
|
2021-04-28 07:36:08 +00:00
|
|
|
pinfo.type = Variant::TRANSFORM3D;
|
2020-04-17 02:52:00 +00:00
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_MAT4: {
|
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
|
|
|
pinfo.type = Variant::PROJECTION;
|
2020-04-17 02:52:00 +00:00
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_SAMPLER2D: {
|
|
|
|
pinfo.type = Variant::OBJECT;
|
|
|
|
pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
|
|
|
|
pinfo.hint_string = "Texture2D";
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_SAMPLER2DARRAY: {
|
|
|
|
pinfo.type = Variant::OBJECT;
|
|
|
|
pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
|
2024-04-25 15:36:31 +00:00
|
|
|
pinfo.hint_string = "Texture2DArray,CompressedTexture2DArray";
|
2020-04-17 02:52:00 +00:00
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_SAMPLER3D: {
|
|
|
|
pinfo.type = Variant::OBJECT;
|
|
|
|
pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
|
|
|
|
pinfo.hint_string = "Texture3D";
|
|
|
|
} break;
|
|
|
|
case RS::GLOBAL_VAR_TYPE_SAMPLERCUBE: {
|
|
|
|
pinfo.type = Variant::OBJECT;
|
|
|
|
pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
|
2024-04-25 15:36:31 +00:00
|
|
|
pinfo.hint_string = "Cubemap,CompressedCubemap";
|
2020-04-17 02:52:00 +00:00
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_list->push_back(pinfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool block_update = false;
|
|
|
|
|
|
|
|
ShaderGlobalsEditorInterface() {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-27 09:22:43 +00:00
|
|
|
static Variant create_var(RS::GlobalShaderParameterType p_type) {
|
2020-04-17 02:52:00 +00:00
|
|
|
switch (p_type) {
|
|
|
|
case RS::GLOBAL_VAR_TYPE_BOOL: {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_BVEC2: {
|
|
|
|
return 0; //bits
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_BVEC3: {
|
|
|
|
return 0; //bits
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_BVEC4: {
|
|
|
|
return 0; //bits
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_INT: {
|
|
|
|
return 0; //bits
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_IVEC2: {
|
|
|
|
return Vector2i();
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_IVEC3: {
|
|
|
|
return Vector3i();
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_IVEC4: {
|
2023-03-15 06:40:06 +00:00
|
|
|
return Vector4i();
|
2020-04-17 02:52:00 +00:00
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_RECT2I: {
|
|
|
|
return Rect2i();
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_UINT: {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_UVEC2: {
|
|
|
|
return Vector2i();
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_UVEC3: {
|
|
|
|
return Vector3i();
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_UVEC4: {
|
2023-03-15 06:40:06 +00:00
|
|
|
return Vector4i();
|
2020-04-17 02:52:00 +00:00
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_FLOAT: {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_VEC2: {
|
|
|
|
return Vector2();
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_VEC3: {
|
|
|
|
return Vector3();
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_VEC4: {
|
2023-03-15 06:40:06 +00:00
|
|
|
return Vector4();
|
2020-04-17 02:52:00 +00:00
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_RECT2: {
|
|
|
|
return Rect2();
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_COLOR: {
|
|
|
|
return Color();
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_MAT2: {
|
2023-03-15 06:40:06 +00:00
|
|
|
Vector<float> xform;
|
2020-04-17 02:52:00 +00:00
|
|
|
xform.resize(4);
|
|
|
|
xform.write[0] = 1;
|
|
|
|
xform.write[1] = 0;
|
|
|
|
xform.write[2] = 0;
|
|
|
|
xform.write[3] = 1;
|
|
|
|
return xform;
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_MAT3: {
|
|
|
|
return Basis();
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
|
|
|
|
return Transform2D();
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
|
2020-10-17 05:08:21 +00:00
|
|
|
return Transform3D();
|
2020-04-17 02:52:00 +00:00
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_MAT4: {
|
2023-03-15 06:40:06 +00:00
|
|
|
return Projection();
|
2020-04-17 02:52:00 +00:00
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_SAMPLER2D: {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_SAMPLER2DARRAY: {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_SAMPLER3D: {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_SAMPLERCUBE: {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-11 20:13:23 +00:00
|
|
|
String ShaderGlobalsEditor::_check_new_variable_name(const String &p_variable_name) {
|
|
|
|
if (p_variable_name.is_empty()) {
|
|
|
|
return TTR("Name cannot be empty.");
|
|
|
|
}
|
|
|
|
|
2024-08-23 06:30:51 +00:00
|
|
|
if (!p_variable_name.is_valid_ascii_identifier()) {
|
2024-01-11 20:13:23 +00:00
|
|
|
return TTR("Name must be a valid identifier.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2023-08-30 18:11:21 +00:00
|
|
|
LineEdit *ShaderGlobalsEditor::get_name_box() const {
|
|
|
|
return variable_name;
|
|
|
|
}
|
|
|
|
|
2024-01-11 20:13:23 +00:00
|
|
|
void ShaderGlobalsEditor::_variable_name_text_changed(const String &p_variable_name) {
|
|
|
|
const String &warning = _check_new_variable_name(p_variable_name.strip_edges());
|
|
|
|
variable_add->set_tooltip_text(warning);
|
|
|
|
variable_add->set_disabled(!warning.is_empty());
|
|
|
|
}
|
|
|
|
|
2020-04-17 02:52:00 +00:00
|
|
|
void ShaderGlobalsEditor::_variable_added() {
|
|
|
|
String var = variable_name->get_text().strip_edges();
|
|
|
|
|
2022-08-27 09:22:43 +00:00
|
|
|
if (RenderingServer::get_singleton()->global_shader_parameter_get(var).get_type() != Variant::NIL) {
|
2024-01-11 20:13:23 +00:00
|
|
|
EditorNode::get_singleton()->show_warning(vformat(TTR("Global shader parameter '%s' already exists."), var));
|
2020-04-17 02:52:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> keywords;
|
|
|
|
ShaderLanguage::get_keyword_list(&keywords);
|
|
|
|
|
|
|
|
if (keywords.find(var) != nullptr || var == "script") {
|
|
|
|
EditorNode::get_singleton()->show_warning(vformat(TTR("Name '%s' is a reserved shader language keyword."), var));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-23 22:53:16 +00:00
|
|
|
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
|
2020-04-17 02:52:00 +00:00
|
|
|
|
2022-08-27 09:22:43 +00:00
|
|
|
Variant value = create_var(RS::GlobalShaderParameterType(variable_type->get_selected()));
|
2020-04-17 02:52:00 +00:00
|
|
|
|
2022-08-27 09:22:43 +00:00
|
|
|
undo_redo->create_action(TTR("Add Shader Global Parameter"));
|
|
|
|
undo_redo->add_do_method(RS::get_singleton(), "global_shader_parameter_add", var, RS::GlobalShaderParameterType(variable_type->get_selected()), value);
|
|
|
|
undo_redo->add_undo_method(RS::get_singleton(), "global_shader_parameter_remove", var);
|
2020-04-17 02:52:00 +00:00
|
|
|
Dictionary gv;
|
|
|
|
gv["type"] = global_var_type_names[variable_type->get_selected()];
|
|
|
|
gv["value"] = value;
|
|
|
|
|
|
|
|
undo_redo->add_do_property(ProjectSettings::get_singleton(), "shader_globals/" + var, gv);
|
|
|
|
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "shader_globals/" + var, Variant());
|
|
|
|
undo_redo->add_do_method(this, "_changed");
|
|
|
|
undo_redo->add_undo_method(this, "_changed");
|
|
|
|
undo_redo->commit_action();
|
2024-03-05 15:04:13 +00:00
|
|
|
|
|
|
|
variable_name->clear();
|
2020-04-17 02:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderGlobalsEditor::_variable_deleted(const String &p_variable) {
|
2022-12-23 22:53:16 +00:00
|
|
|
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
|
2020-04-17 02:52:00 +00:00
|
|
|
|
2022-08-27 09:22:43 +00:00
|
|
|
undo_redo->create_action(TTR("Add Shader Global Parameter"));
|
|
|
|
undo_redo->add_do_method(RS::get_singleton(), "global_shader_parameter_remove", p_variable);
|
|
|
|
undo_redo->add_undo_method(RS::get_singleton(), "global_shader_parameter_add", p_variable, RS::get_singleton()->global_shader_parameter_get_type(p_variable), RS::get_singleton()->global_shader_parameter_get(p_variable));
|
2020-04-17 02:52:00 +00:00
|
|
|
|
|
|
|
undo_redo->add_do_property(ProjectSettings::get_singleton(), "shader_globals/" + p_variable, Variant());
|
2022-10-18 14:43:37 +00:00
|
|
|
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "shader_globals/" + p_variable, GLOBAL_GET("shader_globals/" + p_variable));
|
2020-04-17 02:52:00 +00:00
|
|
|
undo_redo->add_do_method(this, "_changed");
|
|
|
|
undo_redo->add_undo_method(this, "_changed");
|
|
|
|
undo_redo->commit_action();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderGlobalsEditor::_changed() {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("globals_changed"));
|
2020-04-17 02:52:00 +00:00
|
|
|
if (!interface->block_update) {
|
2021-02-10 20:18:45 +00:00
|
|
|
interface->notify_property_list_changed();
|
2020-04-17 02:52:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderGlobalsEditor::_bind_methods() {
|
|
|
|
ClassDB::bind_method("_changed", &ShaderGlobalsEditor::_changed);
|
|
|
|
ADD_SIGNAL(MethodInfo("globals_changed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderGlobalsEditor::_notification(int p_what) {
|
2022-02-16 14:17:55 +00:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
|
|
|
if (is_visible_in_tree()) {
|
|
|
|
inspector->edit(interface);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
2024-01-11 20:13:23 +00:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
|
|
|
variable_add->set_icon(get_editor_theme_icon(SNAME("Add")));
|
|
|
|
} break;
|
|
|
|
|
2022-02-16 14:17:55 +00:00
|
|
|
case NOTIFICATION_PREDELETE: {
|
|
|
|
inspector->edit(nullptr);
|
|
|
|
} break;
|
2020-10-13 08:27:49 +00:00
|
|
|
}
|
2020-04-17 02:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ShaderGlobalsEditor::ShaderGlobalsEditor() {
|
2022-06-08 15:52:19 +00:00
|
|
|
ProjectSettings::get_singleton()->add_hidden_prefix("shader_globals/");
|
|
|
|
|
2020-04-17 02:52:00 +00:00
|
|
|
HBoxContainer *add_menu_hb = memnew(HBoxContainer);
|
|
|
|
add_child(add_menu_hb);
|
|
|
|
|
|
|
|
add_menu_hb->add_child(memnew(Label(TTR("Name:"))));
|
|
|
|
variable_name = memnew(LineEdit);
|
|
|
|
variable_name->set_h_size_flags(SIZE_EXPAND_FILL);
|
2024-01-11 20:13:23 +00:00
|
|
|
variable_name->set_clear_button_enabled(true);
|
2024-05-14 09:42:00 +00:00
|
|
|
variable_name->connect(SceneStringName(text_changed), callable_mp(this, &ShaderGlobalsEditor::_variable_name_text_changed));
|
2024-03-05 15:04:13 +00:00
|
|
|
variable_name->connect("text_submitted", callable_mp(this, &ShaderGlobalsEditor::_variable_added).unbind(1));
|
2024-01-11 20:13:23 +00:00
|
|
|
|
2020-04-17 02:52:00 +00:00
|
|
|
add_menu_hb->add_child(variable_name);
|
|
|
|
|
|
|
|
add_menu_hb->add_child(memnew(Label(TTR("Type:"))));
|
|
|
|
variable_type = memnew(OptionButton);
|
|
|
|
variable_type->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
add_menu_hb->add_child(variable_type);
|
|
|
|
|
|
|
|
for (int i = 0; i < RS::GLOBAL_VAR_TYPE_MAX; i++) {
|
|
|
|
variable_type->add_item(global_var_type_names[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
variable_add = memnew(Button(TTR("Add")));
|
2024-01-11 20:13:23 +00:00
|
|
|
variable_add->set_disabled(true);
|
2020-04-17 02:52:00 +00:00
|
|
|
add_menu_hb->add_child(variable_add);
|
2024-05-14 07:40:21 +00:00
|
|
|
variable_add->connect(SceneStringName(pressed), callable_mp(this, &ShaderGlobalsEditor::_variable_added));
|
2020-04-17 02:52:00 +00:00
|
|
|
|
|
|
|
inspector = memnew(EditorInspector);
|
|
|
|
inspector->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
add_child(inspector);
|
|
|
|
inspector->set_use_wide_editors(true);
|
2022-03-23 01:46:59 +00:00
|
|
|
inspector->set_property_name_style(EditorPropertyNameProcessor::STYLE_RAW);
|
2020-04-17 02:52:00 +00:00
|
|
|
inspector->set_use_deletable_properties(true);
|
2022-07-28 20:56:41 +00:00
|
|
|
inspector->connect("property_deleted", callable_mp(this, &ShaderGlobalsEditor::_variable_deleted), CONNECT_DEFERRED);
|
2020-04-17 02:52:00 +00:00
|
|
|
|
|
|
|
interface = memnew(ShaderGlobalsEditorInterface);
|
2020-12-06 15:09:18 +00:00
|
|
|
interface->connect("var_changed", callable_mp(this, &ShaderGlobalsEditor::_changed));
|
2020-04-17 02:52:00 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-04-17 02:52:00 +00:00
|
|
|
ShaderGlobalsEditor::~ShaderGlobalsEditor() {
|
|
|
|
memdelete(interface);
|
|
|
|
}
|