summaryrefslogtreecommitdiff
path: root/scene/3d
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2020-04-17 18:36:06 +0200
committerGitHub <noreply@github.com>2020-04-17 18:36:06 +0200
commit17304f1aaed434b0547606ff22721d65292d8cf1 (patch)
tree8b52f567244fe41a2776a49099ee8c7f300bfe40 /scene/3d
parentcfda0e377004a9399255105132ccb1a906e9f12e (diff)
parent0e1c66d9fced8f9d3a8fbd87ddfd2c29a9ed2679 (diff)
Merge pull request #37949 from reduz/implement-global-shader-uniforms
Implement global and per instance shader uniforms.
Diffstat (limited to 'scene/3d')
-rw-r--r--scene/3d/visual_instance_3d.cpp75
-rw-r--r--scene/3d/visual_instance_3d.h12
2 files changed, 87 insertions, 0 deletions
diff --git a/scene/3d/visual_instance_3d.cpp b/scene/3d/visual_instance_3d.cpp
index 775a9b76e2..604bc53422 100644
--- a/scene/3d/visual_instance_3d.cpp
+++ b/scene/3d/visual_instance_3d.cpp
@@ -217,6 +217,62 @@ float GeometryInstance3D::get_lod_max_hysteresis() const {
void GeometryInstance3D::_notification(int p_what) {
}
+const StringName *GeometryInstance3D::_instance_uniform_get_remap(const StringName p_name) const {
+ StringName *r = instance_uniform_property_remap.getptr(p_name);
+ if (!r) {
+ String s = p_name;
+ if (s.begins_with("shader_params/")) {
+ StringName name = s.replace("shader_params/", "");
+ instance_uniform_property_remap[p_name] = name;
+ return instance_uniform_property_remap.getptr(p_name);
+ }
+
+ return nullptr;
+ }
+
+ return r;
+}
+
+bool GeometryInstance3D::_set(const StringName &p_name, const Variant &p_value) {
+ const StringName *r = _instance_uniform_get_remap(p_name);
+ if (r) {
+ set_shader_instance_uniform(*r, p_value);
+ return true;
+ }
+
+ return false;
+}
+
+bool GeometryInstance3D::_get(const StringName &p_name, Variant &r_ret) const {
+ const StringName *r = _instance_uniform_get_remap(p_name);
+ if (r) {
+ r_ret = get_shader_instance_uniform(*r);
+ return true;
+ }
+
+ return false;
+}
+void GeometryInstance3D::_get_property_list(List<PropertyInfo> *p_list) const {
+ List<PropertyInfo> pinfo;
+ RS::get_singleton()->instance_geometry_get_shader_parameter_list(get_instance(), &pinfo);
+ for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
+ PropertyInfo pi = E->get();
+ bool has_def_value = false;
+ Variant def_value = RS::get_singleton()->instance_geometry_get_shader_parameter_default_value(get_instance(), pi.name);
+ if (def_value.get_type() != Variant::NIL) {
+ has_def_value = true;
+ }
+ if (instance_uniforms.has(pi.name)) {
+ pi.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE | (has_def_value ? (PROPERTY_USAGE_CHECKABLE | PROPERTY_USAGE_CHECKED) : 0);
+ } else {
+ pi.usage = PROPERTY_USAGE_EDITOR | (has_def_value ? PROPERTY_USAGE_CHECKABLE : 0); //do not save if not changed
+ }
+
+ pi.name = "shader_params/" + pi.name;
+ p_list->push_back(pi);
+ }
+}
+
void GeometryInstance3D::set_flag(Flags p_flag, bool p_value) {
ERR_FAIL_INDEX(p_flag, FLAG_MAX);
@@ -258,6 +314,22 @@ float GeometryInstance3D::get_extra_cull_margin() const {
return extra_cull_margin;
}
+void GeometryInstance3D::set_shader_instance_uniform(const StringName &p_uniform, const Variant &p_value) {
+
+ if (p_value.get_type() == Variant::NIL) {
+ Variant def_value = RS::get_singleton()->instance_geometry_get_shader_parameter_default_value(get_instance(), p_uniform);
+ RS::get_singleton()->instance_geometry_set_shader_parameter(get_instance(), p_uniform, def_value);
+ instance_uniforms.erase(p_value);
+ } else {
+ instance_uniforms[p_uniform] = p_value;
+ RS::get_singleton()->instance_geometry_set_shader_parameter(get_instance(), p_uniform, p_value);
+ }
+}
+
+Variant GeometryInstance3D::get_shader_instance_uniform(const StringName &p_uniform) const {
+
+ return RS::get_singleton()->instance_geometry_get_shader_parameter(get_instance(), p_uniform);
+}
void GeometryInstance3D::set_custom_aabb(AABB aabb) {
RS::get_singleton()->instance_set_custom_aabb(get_instance(), aabb);
@@ -280,6 +352,9 @@ void GeometryInstance3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_lod_max_distance", "mode"), &GeometryInstance3D::set_lod_max_distance);
ClassDB::bind_method(D_METHOD("get_lod_max_distance"), &GeometryInstance3D::get_lod_max_distance);
+ ClassDB::bind_method(D_METHOD("set_shader_instance_uniform", "uniform", "value"), &GeometryInstance3D::set_shader_instance_uniform);
+ ClassDB::bind_method(D_METHOD("get_shader_instance_uniform", "uniform"), &GeometryInstance3D::get_shader_instance_uniform);
+
ClassDB::bind_method(D_METHOD("set_lod_min_hysteresis", "mode"), &GeometryInstance3D::set_lod_min_hysteresis);
ClassDB::bind_method(D_METHOD("get_lod_min_hysteresis"), &GeometryInstance3D::get_lod_min_hysteresis);
diff --git a/scene/3d/visual_instance_3d.h b/scene/3d/visual_instance_3d.h
index 9476c28848..cc5f92066f 100644
--- a/scene/3d/visual_instance_3d.h
+++ b/scene/3d/visual_instance_3d.h
@@ -108,9 +108,18 @@ private:
float lod_min_hysteresis;
float lod_max_hysteresis;
+ mutable HashMap<StringName, Variant> instance_uniforms;
+ mutable HashMap<StringName, StringName> instance_uniform_property_remap;
+
float extra_cull_margin;
+ const StringName *_instance_uniform_get_remap(const StringName p_name) const;
+
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;
+
void _notification(int p_what);
static void _bind_methods();
@@ -139,6 +148,9 @@ public:
void set_extra_cull_margin(float p_margin);
float get_extra_cull_margin() const;
+ void set_shader_instance_uniform(const StringName &p_uniform, const Variant &p_value);
+ Variant get_shader_instance_uniform(const StringName &p_uniform) const;
+
void set_custom_aabb(AABB aabb);
GeometryInstance3D();