Set uniform default values in inspector

This commit is contained in:
JFonS 2018-09-05 00:54:35 +02:00
parent ae8f984cf3
commit 3cedec5f75
14 changed files with 148 additions and 1 deletions

View File

@ -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) {}

View File

@ -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<ShaderLanguage::ConstantNode::Value> 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();
}

View File

@ -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);

View File

@ -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<ShaderLanguage::ConstantNode::Value> 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();
}

View File

@ -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);

View File

@ -145,6 +145,31 @@ void ShaderMaterial::_get_property_list(List<PropertyInfo> *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<Shader> &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");
}

View File

@ -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<PropertyInfo> *p_list) const;
bool property_can_revert(const String &p_name);
Variant property_get_revert(const String &p_name);
static void _bind_methods();

View File

@ -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;

View File

@ -2273,6 +2273,90 @@ bool ShaderLanguage::is_sampler_type(DataType p_type) {
p_type == TYPE_SAMPLERCUBE;
}
Variant ShaderLanguage::constant_value_to_variant(const Vector<ShaderLanguage::ConstantNode::Value> &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<String> *r_keywords) {
Set<String> kws;

View File

@ -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<ShaderLanguage::ConstantNode::Value> &p_value, DataType p_type);
static void get_keyword_list(List<String> *r_keywords);
static void get_builtin_funcs(List<String> *r_keywords);

View File

@ -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)

View File

@ -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)

View File

@ -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);

View File

@ -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;