diff options
author | JFonS <joan.fonssanchez@gmail.com> | 2021-07-20 15:17:34 -0300 |
---|---|---|
committer | jfons <joan.fonssanchez@gmail.com> | 2021-10-25 11:39:34 +0200 |
commit | c571e4a7f4a5cc34fa9b7efeff37ee69fcf6249d (patch) | |
tree | a4c2c52f3f5bc8f53526b91b04845496bec2b30b /scene/3d | |
parent | 88d9914519bd2016bd5181a7aa4c00f8063ad940 (diff) |
Implement distance fade and transparency
The built-in ALPHA in spatial shaders comes pre-set with a per-instance
transparency value. Multiply by it if you want to keep it.
The transparency value of any given GeometryInstance3D is affected by:
- Its new "transparency" property.
- Its own visiblity range when the new "visibility_range_fade_mode"
property is set to "Self".
- Its parent visibility range when the parent's fade mode is
set to "Dependencies".
The "Self" mode will fade-out the instance when reaching the visibility
range limits, while the "Dependencies" mode will fade-in its
dependencies.
Per-instance transparency is only implemented in the forward clustered
renderer, support for mobile should be added in the future.
Co-authored-by: reduz <reduzio@gmail.com>
Diffstat (limited to 'scene/3d')
-rw-r--r-- | scene/3d/visual_instance_3d.cpp | 39 | ||||
-rw-r--r-- | scene/3d/visual_instance_3d.h | 17 |
2 files changed, 48 insertions, 8 deletions
diff --git a/scene/3d/visual_instance_3d.cpp b/scene/3d/visual_instance_3d.cpp index 73c2887983..d407592376 100644 --- a/scene/3d/visual_instance_3d.cpp +++ b/scene/3d/visual_instance_3d.cpp @@ -152,9 +152,18 @@ Ref<Material> GeometryInstance3D::get_material_override() const { return material_override; } +void GeometryInstance3D::set_transparecy(float p_transparency) { + transparency = CLAMP(p_transparency, 0.0f, 1.0f); + RS::get_singleton()->instance_geometry_set_transparency(get_instance(), transparency); +} + +float GeometryInstance3D::get_transparency() const { + return transparency; +} + void GeometryInstance3D::set_visibility_range_begin(float p_dist) { visibility_range_begin = p_dist; - RS::get_singleton()->instance_geometry_set_visibility_range(get_instance(), visibility_range_begin, visibility_range_end, visibility_range_begin_margin, visibility_range_end_margin); + RS::get_singleton()->instance_geometry_set_visibility_range(get_instance(), visibility_range_begin, visibility_range_end, visibility_range_begin_margin, visibility_range_end_margin, (RS::VisibilityRangeFadeMode)visibility_range_fade_mode); update_configuration_warnings(); } @@ -164,7 +173,7 @@ float GeometryInstance3D::get_visibility_range_begin() const { void GeometryInstance3D::set_visibility_range_end(float p_dist) { visibility_range_end = p_dist; - RS::get_singleton()->instance_geometry_set_visibility_range(get_instance(), visibility_range_begin, visibility_range_end, visibility_range_begin_margin, visibility_range_end_margin); + RS::get_singleton()->instance_geometry_set_visibility_range(get_instance(), visibility_range_begin, visibility_range_end, visibility_range_begin_margin, visibility_range_end_margin, (RS::VisibilityRangeFadeMode)visibility_range_fade_mode); update_configuration_warnings(); } @@ -174,7 +183,7 @@ float GeometryInstance3D::get_visibility_range_end() const { void GeometryInstance3D::set_visibility_range_begin_margin(float p_dist) { visibility_range_begin_margin = p_dist; - RS::get_singleton()->instance_geometry_set_visibility_range(get_instance(), visibility_range_begin, visibility_range_end, visibility_range_begin_margin, visibility_range_end_margin); + RS::get_singleton()->instance_geometry_set_visibility_range(get_instance(), visibility_range_begin, visibility_range_end, visibility_range_begin_margin, visibility_range_end_margin, (RS::VisibilityRangeFadeMode)visibility_range_fade_mode); } float GeometryInstance3D::get_visibility_range_begin_margin() const { @@ -183,13 +192,22 @@ float GeometryInstance3D::get_visibility_range_begin_margin() const { void GeometryInstance3D::set_visibility_range_end_margin(float p_dist) { visibility_range_end_margin = p_dist; - RS::get_singleton()->instance_geometry_set_visibility_range(get_instance(), visibility_range_begin, visibility_range_end, visibility_range_begin_margin, visibility_range_end_margin); + RS::get_singleton()->instance_geometry_set_visibility_range(get_instance(), visibility_range_begin, visibility_range_end, visibility_range_begin_margin, visibility_range_end_margin, (RS::VisibilityRangeFadeMode)visibility_range_fade_mode); } float GeometryInstance3D::get_visibility_range_end_margin() const { return visibility_range_end_margin; } +void GeometryInstance3D::set_visibility_range_fade_mode(VisibilityRangeFadeMode p_mode) { + visibility_range_fade_mode = p_mode; + RS::get_singleton()->instance_geometry_set_visibility_range(get_instance(), visibility_range_begin, visibility_range_end, visibility_range_begin_margin, visibility_range_end_margin, (RS::VisibilityRangeFadeMode)visibility_range_fade_mode); +} + +GeometryInstance3D::VisibilityRangeFadeMode GeometryInstance3D::get_visibility_range_fade_mode() const { + return visibility_range_fade_mode; +} + void GeometryInstance3D::_notification(int p_what) { } @@ -375,6 +393,9 @@ void GeometryInstance3D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_lod_bias", "bias"), &GeometryInstance3D::set_lod_bias); ClassDB::bind_method(D_METHOD("get_lod_bias"), &GeometryInstance3D::get_lod_bias); + ClassDB::bind_method(D_METHOD("set_transparency", "transparency"), &GeometryInstance3D::set_transparecy); + ClassDB::bind_method(D_METHOD("get_transparency"), &GeometryInstance3D::get_transparency); + ClassDB::bind_method(D_METHOD("set_visibility_range_end_margin", "distance"), &GeometryInstance3D::set_visibility_range_end_margin); ClassDB::bind_method(D_METHOD("get_visibility_range_end_margin"), &GeometryInstance3D::get_visibility_range_end_margin); @@ -387,6 +408,9 @@ void GeometryInstance3D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_visibility_range_begin", "distance"), &GeometryInstance3D::set_visibility_range_begin); ClassDB::bind_method(D_METHOD("get_visibility_range_begin"), &GeometryInstance3D::get_visibility_range_begin); + ClassDB::bind_method(D_METHOD("set_visibility_range_fade_mode", "mode"), &GeometryInstance3D::set_visibility_range_fade_mode); + ClassDB::bind_method(D_METHOD("get_visibility_range_fade_mode"), &GeometryInstance3D::get_visibility_range_fade_mode); + 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); @@ -408,6 +432,7 @@ void GeometryInstance3D::_bind_methods() { ADD_GROUP("Geometry", ""); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material_override", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DEFERRED_SET_RESOURCE), "set_material_override", "get_material_override"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "transparency", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_transparency", "get_transparency"); ADD_PROPERTY(PropertyInfo(Variant::INT, "cast_shadow", PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"), "set_cast_shadows_setting", "get_cast_shadows_setting"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "extra_cull_margin", PROPERTY_HINT_RANGE, "0,16384,0.01"), "set_extra_cull_margin", "get_extra_cull_margin"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lod_bias", PROPERTY_HINT_RANGE, "0.001,128,0.001"), "set_lod_bias", "get_lod_bias"); @@ -421,7 +446,7 @@ void GeometryInstance3D::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visibility_range_begin_margin", PROPERTY_HINT_RANGE, "0.0,4096.0,0.01"), "set_visibility_range_begin_margin", "get_visibility_range_begin_margin"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visibility_range_end", PROPERTY_HINT_RANGE, "0.0,4096.0,0.01"), "set_visibility_range_end", "get_visibility_range_end"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visibility_range_end_margin", PROPERTY_HINT_RANGE, "0.0,4096.0,0.01"), "set_visibility_range_end_margin", "get_visibility_range_end_margin"); - + ADD_PROPERTY(PropertyInfo(Variant::INT, "visibility_range_fade_mode", PROPERTY_HINT_ENUM, "Disabled,Self,Dependencies"), "set_visibility_range_fade_mode", "get_visibility_range_fade_mode"); //ADD_SIGNAL( MethodInfo("visibility_changed")); BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_OFF); @@ -438,6 +463,10 @@ void GeometryInstance3D::_bind_methods() { BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_4X); BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_8X); BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_MAX); + + BIND_ENUM_CONSTANT(VISIBILITY_RANGE_FADE_DISABLED); + BIND_ENUM_CONSTANT(VISIBILITY_RANGE_FADE_SELF); + BIND_ENUM_CONSTANT(VISIBILITY_RANGE_FADE_DEPENDENCIES); } GeometryInstance3D::GeometryInstance3D() { diff --git a/scene/3d/visual_instance_3d.h b/scene/3d/visual_instance_3d.h index 8d24e13d47..acdbc4666a 100644 --- a/scene/3d/visual_instance_3d.h +++ b/scene/3d/visual_instance_3d.h @@ -101,6 +101,12 @@ public: LIGHTMAP_SCALE_MAX, }; + enum VisibilityRangeFadeMode { + VISIBILITY_RANGE_FADE_DISABLED = RS::VISIBILITY_RANGE_FADE_DISABLED, + VISIBILITY_RANGE_FADE_SELF = RS::VISIBILITY_RANGE_FADE_SELF, + VISIBILITY_RANGE_FADE_DEPENDENCIES = RS::VISIBILITY_RANGE_FADE_DEPENDENCIES, + }; + private: ShadowCastingSetting shadow_casting_setting = SHADOW_CASTING_SETTING_ON; Ref<Material> material_override; @@ -109,8 +115,9 @@ private: float visibility_range_end = 0.0; float visibility_range_begin_margin = 0.0; float visibility_range_end_margin = 0.0; + VisibilityRangeFadeMode visibility_range_fade_mode = VISIBILITY_RANGE_FADE_DISABLED; - Vector<NodePath> visibility_range_children; + float transparency = 0.0f; float lod_bias = 1.0; @@ -136,6 +143,9 @@ public: void set_cast_shadows_setting(ShadowCastingSetting p_shadow_casting_setting); ShadowCastingSetting get_cast_shadows_setting() const; + void set_transparecy(float p_transparency); + float get_transparency() const; + void set_visibility_range_begin(float p_dist); float get_visibility_range_begin() const; @@ -148,8 +158,8 @@ public: void set_visibility_range_end_margin(float p_dist); float get_visibility_range_end_margin() const; - void set_visibility_range_parent(const Node *p_parent); - void clear_visibility_range_parent(); + void set_visibility_range_fade_mode(VisibilityRangeFadeMode p_mode); + VisibilityRangeFadeMode get_visibility_range_fade_mode() const; void set_material_override(const Ref<Material> &p_material); Ref<Material> get_material_override() const; @@ -181,5 +191,6 @@ public: VARIANT_ENUM_CAST(GeometryInstance3D::ShadowCastingSetting); VARIANT_ENUM_CAST(GeometryInstance3D::LightmapScale); VARIANT_ENUM_CAST(GeometryInstance3D::GIMode); +VARIANT_ENUM_CAST(GeometryInstance3D::VisibilityRangeFadeMode); #endif |