From 3cedec5f754b08d04095d5ebe0cd9532de011268 Mon Sep 17 00:00:00 2001 From: JFonS Date: Wed, 5 Sep 2018 00:54:35 +0200 Subject: [PATCH] Set uniform default values in inspector --- drivers/dummy/rasterizer_dummy.h | 1 + drivers/gles2/rasterizer_storage_gles2.cpp | 13 ++++ drivers/gles2/rasterizer_storage_gles2.h | 1 + drivers/gles3/rasterizer_storage_gles3.cpp | 14 +++- drivers/gles3/rasterizer_storage_gles3.h | 1 + scene/resources/material.cpp | 27 +++++++ scene/resources/material.h | 2 + servers/visual/rasterizer.h | 1 + servers/visual/shader_language.cpp | 84 ++++++++++++++++++++++ servers/visual/shader_language.h | 1 + servers/visual/visual_server_raster.h | 1 + servers/visual/visual_server_wrap_mt.h | 1 + servers/visual_server.cpp | 1 + servers/visual_server.h | 1 + 14 files changed, 148 insertions(+), 1 deletion(-) diff --git a/drivers/dummy/rasterizer_dummy.h b/drivers/dummy/rasterizer_dummy.h index bb4511ecc3a..126f23feebd 100644 --- a/drivers/dummy/rasterizer_dummy.h +++ b/drivers/dummy/rasterizer_dummy.h @@ -267,6 +267,7 @@ public: void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) {} Variant material_get_param(RID p_material, const StringName &p_param) const { return Variant(); } + Variant material_get_param_default(RID p_material, const StringName &p_param) const { return Variant(); } void material_set_line_width(RID p_material, float p_width) {} diff --git a/drivers/gles2/rasterizer_storage_gles2.cpp b/drivers/gles2/rasterizer_storage_gles2.cpp index fdc110be9e3..d945132dc2d 100644 --- a/drivers/gles2/rasterizer_storage_gles2.cpp +++ b/drivers/gles2/rasterizer_storage_gles2.cpp @@ -1419,6 +1419,19 @@ Variant RasterizerStorageGLES2::material_get_param(RID p_material, const StringN return material->params[p_param]; } + return material_get_param_default(p_material, p_param); +} + +Variant RasterizerStorageGLES2::material_get_param_default(RID p_material, const StringName &p_param) const { + const Material *material = material_owner.get(p_material); + ERR_FAIL_COND_V(!material, Variant()); + + if (material->shader) { + if (material->shader->uniforms.has(p_param)) { + Vector default_value = material->shader->uniforms[p_param].default_value; + return ShaderLanguage::constant_value_to_variant(default_value, material->shader->uniforms[p_param].type); + } + } return Variant(); } diff --git a/drivers/gles2/rasterizer_storage_gles2.h b/drivers/gles2/rasterizer_storage_gles2.h index e42eb67d3da..d9bf6b3ccb0 100644 --- a/drivers/gles2/rasterizer_storage_gles2.h +++ b/drivers/gles2/rasterizer_storage_gles2.h @@ -563,6 +563,7 @@ public: virtual void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value); virtual Variant material_get_param(RID p_material, const StringName &p_param) const; + virtual Variant material_get_param_default(RID p_material, const StringName &p_param) const; virtual void material_set_line_width(RID p_material, float p_width); virtual void material_set_next_pass(RID p_material, RID p_next_material); diff --git a/drivers/gles3/rasterizer_storage_gles3.cpp b/drivers/gles3/rasterizer_storage_gles3.cpp index c05f4cfbe32..25e7bd0424f 100644 --- a/drivers/gles3/rasterizer_storage_gles3.cpp +++ b/drivers/gles3/rasterizer_storage_gles3.cpp @@ -29,7 +29,6 @@ /*************************************************************************/ #include "rasterizer_storage_gles3.h" - #include "core/engine.h" #include "core/project_settings.h" #include "rasterizer_canvas_gles3.h" @@ -2140,6 +2139,19 @@ Variant RasterizerStorageGLES3::material_get_param(RID p_material, const StringN if (material->params.has(p_param)) return material->params[p_param]; + return material_get_param_default(p_material, p_param); +} + +Variant RasterizerStorageGLES3::material_get_param_default(RID p_material, const StringName &p_param) const { + const Material *material = material_owner.get(p_material); + ERR_FAIL_COND_V(!material, Variant()); + + if (material->shader) { + if (material->shader->uniforms.has(p_param)) { + Vector default_value = material->shader->uniforms[p_param].default_value; + return ShaderLanguage::constant_value_to_variant(default_value, material->shader->uniforms[p_param].type); + } + } return Variant(); } diff --git a/drivers/gles3/rasterizer_storage_gles3.h b/drivers/gles3/rasterizer_storage_gles3.h index 33f0853d601..0bd9c22be5f 100644 --- a/drivers/gles3/rasterizer_storage_gles3.h +++ b/drivers/gles3/rasterizer_storage_gles3.h @@ -583,6 +583,7 @@ public: virtual void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value); virtual Variant material_get_param(RID p_material, const StringName &p_param) const; + virtual Variant material_get_param_default(RID p_material, const StringName &p_param) const; virtual void material_set_line_width(RID p_material, float p_width); virtual void material_set_next_pass(RID p_material, RID p_next_material); diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp index 4727526b68f..2cf802a2da2 100644 --- a/scene/resources/material.cpp +++ b/scene/resources/material.cpp @@ -145,6 +145,31 @@ void ShaderMaterial::_get_property_list(List *p_list) const { } } +bool ShaderMaterial::property_can_revert(const String &p_name) { + if (shader.is_valid()) { + + StringName pr = shader->remap_param(p_name); + if (pr) { + Variant default_value = VisualServer::get_singleton()->material_get_param_default(_get_material(), pr); + Variant current_value; + _get(p_name, current_value); + return default_value.get_type() != Variant::NIL && default_value != current_value; + } + } + return false; +} + +Variant ShaderMaterial::property_get_revert(const String &p_name) { + Variant r_ret; + if (shader.is_valid()) { + StringName pr = shader->remap_param(p_name); + if (pr) { + r_ret = VisualServer::get_singleton()->material_get_param_default(_get_material(), pr); + } + } + return r_ret; +} + void ShaderMaterial::set_shader(const Ref &p_shader) { if (shader.is_valid()) { @@ -190,6 +215,8 @@ void ShaderMaterial::_bind_methods() { ClassDB::bind_method(D_METHOD("set_shader_param", "param", "value"), &ShaderMaterial::set_shader_param); ClassDB::bind_method(D_METHOD("get_shader_param", "param"), &ShaderMaterial::get_shader_param); ClassDB::bind_method(D_METHOD("_shader_changed"), &ShaderMaterial::_shader_changed); + ClassDB::bind_method(D_METHOD("property_can_revert", "name"), &ShaderMaterial::property_can_revert); + ClassDB::bind_method(D_METHOD("property_get_revert", "name"), &ShaderMaterial::property_get_revert); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shader", PROPERTY_HINT_RESOURCE_TYPE, "Shader"), "set_shader", "get_shader"); } diff --git a/scene/resources/material.h b/scene/resources/material.h index 078649e7b0d..4a2a8133416 100644 --- a/scene/resources/material.h +++ b/scene/resources/material.h @@ -85,6 +85,8 @@ protected: bool _set(const StringName &p_name, const Variant &p_value); bool _get(const StringName &p_name, Variant &r_ret) const; void _get_property_list(List *p_list) const; + bool property_can_revert(const String &p_name); + Variant property_get_revert(const String &p_name); static void _bind_methods(); diff --git a/servers/visual/rasterizer.h b/servers/visual/rasterizer.h index c26f02f0874..6eeaf12dfc9 100644 --- a/servers/visual/rasterizer.h +++ b/servers/visual/rasterizer.h @@ -249,6 +249,7 @@ public: virtual void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) = 0; virtual Variant material_get_param(RID p_material, const StringName &p_param) const = 0; + virtual Variant material_get_param_default(RID p_material, const StringName &p_param) const = 0; virtual void material_set_line_width(RID p_material, float p_width) = 0; diff --git a/servers/visual/shader_language.cpp b/servers/visual/shader_language.cpp index 8a8b8af2678..35236b23f12 100644 --- a/servers/visual/shader_language.cpp +++ b/servers/visual/shader_language.cpp @@ -2273,6 +2273,90 @@ bool ShaderLanguage::is_sampler_type(DataType p_type) { p_type == TYPE_SAMPLERCUBE; } +Variant ShaderLanguage::constant_value_to_variant(const Vector &p_value, DataType p_type) { + if (p_value.size() > 0) { + Variant value; + switch (p_type) { + case ShaderLanguage::TYPE_BOOL: + value = Variant(p_value[0].boolean); + break; + case ShaderLanguage::TYPE_BVEC2: + case ShaderLanguage::TYPE_BVEC3: + case ShaderLanguage::TYPE_BVEC4: + case ShaderLanguage::TYPE_INT: + value = Variant(p_value[0].sint); + break; + case ShaderLanguage::TYPE_IVEC2: + value = Variant(Vector2(p_value[0].sint, p_value[1].sint)); + break; + case ShaderLanguage::TYPE_IVEC3: + value = Variant(Vector3(p_value[0].sint, p_value[1].sint, p_value[2].sint)); + break; + case ShaderLanguage::TYPE_IVEC4: + value = Variant(Plane(p_value[0].sint, p_value[1].sint, p_value[2].sint, p_value[3].sint)); + break; + case ShaderLanguage::TYPE_UINT: + value = Variant(p_value[0].uint); + break; + case ShaderLanguage::TYPE_UVEC2: + value = Variant(Vector2(p_value[0].uint, p_value[1].uint)); + break; + case ShaderLanguage::TYPE_UVEC3: + value = Variant(Vector3(p_value[0].uint, p_value[1].uint, p_value[2].uint)); + break; + case ShaderLanguage::TYPE_UVEC4: + value = Variant(Plane(p_value[0].uint, p_value[1].uint, p_value[2].uint, p_value[3].uint)); + break; + case ShaderLanguage::TYPE_FLOAT: + value = Variant(p_value[0].real); + break; + case ShaderLanguage::TYPE_VEC2: + value = Variant(Vector2(p_value[0].real, p_value[1].real)); + break; + case ShaderLanguage::TYPE_VEC3: + value = Variant(Vector3(p_value[0].real, p_value[1].real, p_value[2].real)); + break; + case ShaderLanguage::TYPE_VEC4: + value = Variant(Plane(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real)); + break; + case ShaderLanguage::TYPE_MAT2: + value = Variant(Transform2D(p_value[0].real, p_value[2].real, p_value[1].real, p_value[3].real, 0.0, 0.0)); + break; + case ShaderLanguage::TYPE_MAT3: { + Basis p; + p[0][0] = p_value[0].real; + p[0][1] = p_value[1].real; + p[0][2] = p_value[2].real; + p[1][0] = p_value[3].real; + p[1][1] = p_value[4].real; + p[1][2] = p_value[5].real; + p[2][0] = p_value[6].real; + p[2][1] = p_value[7].real; + p[2][2] = p_value[8].real; + value = Variant(p); + break; + } + case ShaderLanguage::TYPE_MAT4: { + Basis p; + p[0][0] = p_value[0].real; + p[0][1] = p_value[1].real; + p[0][2] = p_value[2].real; + p[1][0] = p_value[4].real; + p[1][1] = p_value[5].real; + p[1][2] = p_value[6].real; + p[2][0] = p_value[8].real; + p[2][1] = p_value[9].real; + p[2][2] = p_value[10].real; + Transform t = Transform(p, Vector3(p_value[3].real, p_value[7].real, p_value[11].real)); + value = Variant(t); + break; + } + } + return value; + } + return Variant(); +} + void ShaderLanguage::get_keyword_list(List *r_keywords) { Set kws; diff --git a/servers/visual/shader_language.h b/servers/visual/shader_language.h index e8ae0993020..08c4d069928 100644 --- a/servers/visual/shader_language.h +++ b/servers/visual/shader_language.h @@ -548,6 +548,7 @@ public: static int get_cardinality(DataType p_type); static bool is_scalar_type(DataType p_type); static bool is_sampler_type(DataType p_type); + static Variant constant_value_to_variant(const Vector &p_value, DataType p_type); static void get_keyword_list(List *r_keywords); static void get_builtin_funcs(List *r_keywords); diff --git a/servers/visual/visual_server_raster.h b/servers/visual/visual_server_raster.h index f25973c4e2d..b54e150656a 100644 --- a/servers/visual/visual_server_raster.h +++ b/servers/visual/visual_server_raster.h @@ -203,6 +203,7 @@ public: BIND3(material_set_param, RID, const StringName &, const Variant &) BIND2RC(Variant, material_get_param, RID, const StringName &) + BIND2RC(Variant, material_get_param_default, RID, const StringName &) BIND2(material_set_render_priority, RID, int) BIND2(material_set_line_width, RID, float) diff --git a/servers/visual/visual_server_wrap_mt.h b/servers/visual/visual_server_wrap_mt.h index 85205e5132c..b8f86d7123c 100644 --- a/servers/visual/visual_server_wrap_mt.h +++ b/servers/visual/visual_server_wrap_mt.h @@ -137,6 +137,7 @@ public: FUNC3(material_set_param, RID, const StringName &, const Variant &) FUNC2RC(Variant, material_get_param, RID, const StringName &) + FUNC2RC(Variant, material_get_param_default, RID, const StringName &) FUNC2(material_set_render_priority, RID, int) FUNC2(material_set_line_width, RID, float) diff --git a/servers/visual_server.cpp b/servers/visual_server.cpp index d4e96bb173e..7240266133f 100644 --- a/servers/visual_server.cpp +++ b/servers/visual_server.cpp @@ -1695,6 +1695,7 @@ void VisualServer::_bind_methods() { ClassDB::bind_method(D_METHOD("material_get_shader", "shader_material"), &VisualServer::material_get_shader); ClassDB::bind_method(D_METHOD("material_set_param", "material", "parameter", "value"), &VisualServer::material_set_param); ClassDB::bind_method(D_METHOD("material_get_param", "material", "parameter"), &VisualServer::material_get_param); + ClassDB::bind_method(D_METHOD("material_get_param_default", "material", "parameter"), &VisualServer::material_get_param_default); ClassDB::bind_method(D_METHOD("material_set_render_priority", "material", "priority"), &VisualServer::material_set_render_priority); ClassDB::bind_method(D_METHOD("material_set_line_width", "material", "width"), &VisualServer::material_set_line_width); ClassDB::bind_method(D_METHOD("material_set_next_pass", "material", "next_material"), &VisualServer::material_set_next_pass); diff --git a/servers/visual_server.h b/servers/visual_server.h index 7c21d138e06..6a1f2c3550f 100644 --- a/servers/visual_server.h +++ b/servers/visual_server.h @@ -209,6 +209,7 @@ public: virtual void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) = 0; virtual Variant material_get_param(RID p_material, const StringName &p_param) const = 0; + virtual Variant material_get_param_default(RID p_material, const StringName &p_param) const = 0; virtual void material_set_render_priority(RID p_material, int priority) = 0;