2020-05-10 12:53:46 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* shader_globals_editor.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2020-05-10 12:53:46 +02:00
|
|
|
/* */
|
|
|
|
/* 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 04:52:00 +02:00
|
|
|
#include "shader_globals_editor.h"
|
2022-02-12 02:46:22 +01:00
|
|
|
|
|
|
|
#include "core/config/project_settings.h"
|
|
|
|
#include "editor/editor_node.h"
|
2022-03-25 18:06:46 +01:00
|
|
|
#include "editor/editor_undo_redo_manager.h"
|
2022-01-10 19:46:41 +01:00
|
|
|
#include "servers/rendering/shader_language.h"
|
2020-04-17 04:52:00 +02: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)
|
|
|
|
|
|
|
|
void _var_changed() {
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("var_changed"));
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-08-27 11:22:43 +02:00
|
|
|
Variant existing = RS::get_singleton()->global_shader_parameter_get(p_name);
|
2020-04-17 04:52:00 +02:00
|
|
|
|
|
|
|
if (existing.get_type() == Variant::NIL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-29 12:10:32 +02:00
|
|
|
Ref<EditorUndoRedoManager> &undo_redo = EditorNode::get_undo_redo();
|
2020-04-17 04:52:00 +02:00
|
|
|
|
2021-07-19 21:10:05 +02:00
|
|
|
undo_redo->create_action(TTR("Set Shader Global Variable"));
|
2022-08-27 11:22:43 +02:00
|
|
|
undo_redo->add_do_method(RS::get_singleton(), "global_shader_parameter_set", p_name, p_value);
|
|
|
|
undo_redo->add_undo_method(RS::get_singleton(), "global_shader_parameter_set", p_name, existing);
|
|
|
|
RS::GlobalShaderParameterType type = RS::get_singleton()->global_shader_parameter_get_type(p_name);
|
2020-04-17 04:52:00 +02:00
|
|
|
Dictionary gv;
|
|
|
|
gv["type"] = global_var_type_names[type];
|
|
|
|
if (type >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
|
2022-05-03 01:43:50 +02:00
|
|
|
Ref<Resource> res = p_value;
|
2020-04-17 04:52:00 +02: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 16:43:37 +02:00
|
|
|
undo_redo->add_undo_property(ProjectSettings::get_singleton(), path, GLOBAL_GET(path));
|
2020-04-17 04:52:00 +02:00
|
|
|
undo_redo->add_do_method(this, "_var_changed");
|
|
|
|
undo_redo->add_undo_method(this, "_var_changed");
|
|
|
|
block_update = true;
|
|
|
|
undo_redo->commit_action();
|
|
|
|
block_update = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _get(const StringName &p_name, Variant &r_ret) const {
|
2022-08-27 11:22:43 +02:00
|
|
|
r_ret = RS::get_singleton()->global_shader_parameter_get(p_name);
|
2020-04-17 04:52:00 +02:00
|
|
|
return r_ret.get_type() != Variant::NIL;
|
|
|
|
}
|
|
|
|
void _get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
Vector<StringName> variables;
|
2022-08-27 11:22:43 +02:00
|
|
|
variables = RS::get_singleton()->global_shader_parameter_get_list();
|
2020-04-17 04:52:00 +02:00
|
|
|
for (int i = 0; i < variables.size(); i++) {
|
|
|
|
PropertyInfo pinfo;
|
|
|
|
pinfo.name = variables[i];
|
|
|
|
|
2022-08-27 11:22:43 +02:00
|
|
|
switch (RS::get_singleton()->global_shader_parameter_get_type(variables[i])) {
|
2020-04-17 04:52:00 +02: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-20 01:11:13 +02:00
|
|
|
pinfo.type = Variant::VECTOR4I;
|
2020-04-17 04:52:00 +02: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-20 01:11:13 +02:00
|
|
|
pinfo.type = Variant::VECTOR4I;
|
2020-04-17 04:52:00 +02: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-20 01:11:13 +02:00
|
|
|
pinfo.type = Variant::VECTOR4;
|
2020-04-17 04:52:00 +02: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: {
|
|
|
|
pinfo.type = Variant::PACKED_INT32_ARRAY;
|
|
|
|
} 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 09:36:08 +02:00
|
|
|
pinfo.type = Variant::TRANSFORM3D;
|
2020-04-17 04:52:00 +02: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-20 01:11:13 +02:00
|
|
|
pinfo.type = Variant::PROJECTION;
|
2020-04-17 04:52:00 +02: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;
|
|
|
|
pinfo.hint_string = "Texture2DArray";
|
|
|
|
} 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;
|
|
|
|
pinfo.hint_string = "Cubemap";
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_list->push_back(pinfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool block_update = false;
|
|
|
|
|
|
|
|
ShaderGlobalsEditorInterface() {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-27 11:22:43 +02:00
|
|
|
static Variant create_var(RS::GlobalShaderParameterType p_type) {
|
2020-04-17 04:52:00 +02: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: {
|
|
|
|
Vector<int> v4;
|
|
|
|
v4.resize(4);
|
|
|
|
v4.write[0] = 0;
|
|
|
|
v4.write[1] = 0;
|
|
|
|
v4.write[2] = 0;
|
|
|
|
v4.write[3] = 0;
|
|
|
|
return v4;
|
|
|
|
}
|
|
|
|
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: {
|
2020-10-21 01:34:25 +02:00
|
|
|
Vector<int> v4;
|
|
|
|
v4.resize(4);
|
|
|
|
v4.write[0] = 0;
|
|
|
|
v4.write[1] = 0;
|
|
|
|
v4.write[2] = 0;
|
|
|
|
v4.write[3] = 0;
|
|
|
|
return v4;
|
2020-04-17 04:52:00 +02: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: {
|
2022-04-12 19:09:29 +02:00
|
|
|
return Quaternion();
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_RECT2: {
|
|
|
|
return Rect2();
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_COLOR: {
|
|
|
|
return Color();
|
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_MAT2: {
|
|
|
|
Vector<real_t> xform;
|
|
|
|
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 07:08:21 +02:00
|
|
|
return Transform3D();
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
case RS::GLOBAL_VAR_TYPE_MAT4: {
|
|
|
|
Vector<real_t> xform;
|
2020-10-21 01:34:25 +02:00
|
|
|
xform.resize(16);
|
2020-04-17 04:52:00 +02:00
|
|
|
xform.write[0] = 1;
|
|
|
|
xform.write[1] = 0;
|
|
|
|
xform.write[2] = 0;
|
|
|
|
xform.write[3] = 0;
|
|
|
|
|
|
|
|
xform.write[4] = 0;
|
|
|
|
xform.write[5] = 1;
|
|
|
|
xform.write[6] = 0;
|
|
|
|
xform.write[7] = 0;
|
|
|
|
|
|
|
|
xform.write[8] = 0;
|
|
|
|
xform.write[9] = 0;
|
|
|
|
xform.write[10] = 1;
|
|
|
|
xform.write[11] = 0;
|
|
|
|
|
|
|
|
xform.write[12] = 0;
|
|
|
|
xform.write[13] = 0;
|
|
|
|
xform.write[14] = 0;
|
|
|
|
xform.write[15] = 1;
|
|
|
|
|
|
|
|
return xform;
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderGlobalsEditor::_variable_added() {
|
|
|
|
String var = variable_name->get_text().strip_edges();
|
2021-12-09 10:42:46 +01:00
|
|
|
if (var.is_empty() || !var.is_valid_identifier()) {
|
2022-04-03 18:56:43 +02:00
|
|
|
EditorNode::get_singleton()->show_warning(TTR("Please specify a valid shader uniform identifier name."));
|
2020-04-17 04:52:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-27 11:22:43 +02:00
|
|
|
if (RenderingServer::get_singleton()->global_shader_parameter_get(var).get_type() != Variant::NIL) {
|
|
|
|
EditorNode::get_singleton()->show_warning(vformat(TTR("Global shader parameter '%s' already exists'"), var));
|
2020-04-17 04:52:00 +02: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-03-25 18:06:46 +01:00
|
|
|
Ref<EditorUndoRedoManager> undo_redo = EditorNode::get_singleton()->get_undo_redo();
|
2020-04-17 04:52:00 +02:00
|
|
|
|
2022-08-27 11:22:43 +02:00
|
|
|
Variant value = create_var(RS::GlobalShaderParameterType(variable_type->get_selected()));
|
2020-04-17 04:52:00 +02:00
|
|
|
|
2022-08-27 11:22:43 +02: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 04:52:00 +02: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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderGlobalsEditor::_variable_deleted(const String &p_variable) {
|
2022-03-25 18:06:46 +01:00
|
|
|
Ref<EditorUndoRedoManager> undo_redo = EditorNode::get_singleton()->get_undo_redo();
|
2020-04-17 04:52:00 +02:00
|
|
|
|
2022-08-27 11:22:43 +02: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 04:52:00 +02:00
|
|
|
|
|
|
|
undo_redo->add_do_property(ProjectSettings::get_singleton(), "shader_globals/" + p_variable, Variant());
|
2022-10-18 16:43:37 +02:00
|
|
|
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "shader_globals/" + p_variable, GLOBAL_GET("shader_globals/" + p_variable));
|
2020-04-17 04:52:00 +02: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 23:22:52 +02:00
|
|
|
emit_signal(SNAME("globals_changed"));
|
2020-04-17 04:52:00 +02:00
|
|
|
if (!interface->block_update) {
|
2021-02-10 21:18:45 +01:00
|
|
|
interface->notify_property_list_changed();
|
2020-04-17 04:52:00 +02: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 15:17:55 +01:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
|
|
|
if (is_visible_in_tree()) {
|
|
|
|
inspector->edit(interface);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_PREDELETE: {
|
|
|
|
inspector->edit(nullptr);
|
|
|
|
} break;
|
2020-10-13 10:27:49 +02:00
|
|
|
}
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ShaderGlobalsEditor::ShaderGlobalsEditor() {
|
|
|
|
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);
|
|
|
|
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")));
|
|
|
|
add_menu_hb->add_child(variable_add);
|
|
|
|
variable_add->connect("pressed", callable_mp(this, &ShaderGlobalsEditor::_variable_added));
|
|
|
|
|
|
|
|
inspector = memnew(EditorInspector);
|
|
|
|
inspector->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
add_child(inspector);
|
|
|
|
inspector->set_use_wide_editors(true);
|
2022-03-23 02:46:59 +01:00
|
|
|
inspector->set_property_name_style(EditorPropertyNameProcessor::STYLE_RAW);
|
2020-04-17 04:52:00 +02:00
|
|
|
inspector->set_use_deletable_properties(true);
|
2022-07-28 22:56:41 +02:00
|
|
|
inspector->connect("property_deleted", callable_mp(this, &ShaderGlobalsEditor::_variable_deleted), CONNECT_DEFERRED);
|
2020-04-17 04:52:00 +02:00
|
|
|
|
|
|
|
interface = memnew(ShaderGlobalsEditorInterface);
|
2020-12-06 16:09:18 +01:00
|
|
|
interface->connect("var_changed", callable_mp(this, &ShaderGlobalsEditor::_changed));
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-04-17 04:52:00 +02:00
|
|
|
ShaderGlobalsEditor::~ShaderGlobalsEditor() {
|
|
|
|
memdelete(interface);
|
|
|
|
}
|