| 1 | /**************************************************************************/ |
| 2 | /* visual_instance_3d.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "visual_instance_3d.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | |
| 35 | AABB VisualInstance3D::get_aabb() const { |
| 36 | AABB ret; |
| 37 | GDVIRTUAL_CALL(_get_aabb, ret); |
| 38 | return ret; |
| 39 | } |
| 40 | |
| 41 | void VisualInstance3D::_update_visibility() { |
| 42 | if (!is_inside_tree()) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | bool already_visible = _is_vi_visible(); |
| 47 | bool visible = is_visible_in_tree(); |
| 48 | _set_vi_visible(visible); |
| 49 | |
| 50 | // If making visible, make sure the rendering server is up to date with the transform. |
| 51 | if (visible && !already_visible) { |
| 52 | if (!_is_using_identity_transform()) { |
| 53 | Transform3D gt = get_global_transform(); |
| 54 | RS::get_singleton()->instance_set_transform(instance, gt); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | RS::get_singleton()->instance_set_visible(instance, visible); |
| 59 | } |
| 60 | |
| 61 | void VisualInstance3D::set_instance_use_identity_transform(bool p_enable) { |
| 62 | // Prevent sending instance transforms when using global coordinates. |
| 63 | _set_use_identity_transform(p_enable); |
| 64 | |
| 65 | if (is_inside_tree()) { |
| 66 | if (p_enable) { |
| 67 | // Want to make sure instance is using identity transform. |
| 68 | RS::get_singleton()->instance_set_transform(instance, Transform3D()); |
| 69 | } else { |
| 70 | // Want to make sure instance is up to date. |
| 71 | RS::get_singleton()->instance_set_transform(instance, get_global_transform()); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void VisualInstance3D::fti_update_servers_xform() { |
| 77 | if (!_is_using_identity_transform()) { |
| 78 | RS::get_singleton()->instance_set_transform(get_instance(), _get_cached_global_transform_interpolated()); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void VisualInstance3D::_notification(int p_what) { |
| 83 | switch (p_what) { |
| 84 | case NOTIFICATION_ENTER_WORLD: { |
| 85 | ERR_FAIL_COND(get_world_3d().is_null()); |
| 86 | RenderingServer::get_singleton()->instance_set_scenario(instance, get_world_3d()->get_scenario()); |
| 87 | _update_visibility(); |
| 88 | } break; |
| 89 | |
| 90 | case NOTIFICATION_TRANSFORM_CHANGED: { |
| 91 | // ToDo : Can we turn off notify transform for physics interpolated cases? |
| 92 | if (_is_vi_visible() && !(is_inside_tree() && get_tree()->is_physics_interpolation_enabled()) && !_is_using_identity_transform()) { |
| 93 | // Physics interpolation global off, always send. |
| 94 | RenderingServer::get_singleton()->instance_set_transform(instance, get_global_transform()); |
| 95 | } |
| 96 | } break; |
| 97 | |
| 98 | case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: { |
| 99 | if (_is_vi_visible() && is_inside_tree()) { |
| 100 | // Allow resetting motion vectors etc |
| 101 | // at the same time as resetting physics interpolation, |
| 102 | // giving users one common interface. |
| 103 | RenderingServer::get_singleton()->instance_teleport(instance); |
| 104 | } |
| 105 | } break; |
| 106 | |
| 107 | case NOTIFICATION_EXIT_WORLD: { |
| 108 | RenderingServer::get_singleton()->instance_set_scenario(instance, RID()); |
| 109 | RenderingServer::get_singleton()->instance_attach_skeleton(instance, RID()); |
| 110 | _set_vi_visible(false); |
| 111 | } break; |
| 112 | |
| 113 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 114 | _update_visibility(); |
| 115 | } break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | RID VisualInstance3D::get_instance() const { |
| 120 | return instance; |
| 121 | } |
| 122 | |
| 123 | void VisualInstance3D::set_layer_mask(uint32_t p_mask) { |
| 124 | layers = p_mask; |
| 125 | RenderingServer::get_singleton()->instance_set_layer_mask(instance, p_mask); |
| 126 | } |
| 127 | |
| 128 | uint32_t VisualInstance3D::get_layer_mask() const { |
| 129 | return layers; |
| 130 | } |
| 131 | |
| 132 | void VisualInstance3D::set_layer_mask_value(int p_layer_number, bool p_value) { |
| 133 | ERR_FAIL_COND_MSG(p_layer_number < 1, "Render layer number must be between 1 and 20 inclusive."); |
| 134 | ERR_FAIL_COND_MSG(p_layer_number > 20, "Render layer number must be between 1 and 20 inclusive."); |
| 135 | uint32_t mask = get_layer_mask(); |
| 136 | if (p_value) { |
| 137 | mask |= 1 << (p_layer_number - 1); |
| 138 | } else { |
| 139 | mask &= ~(1 << (p_layer_number - 1)); |
| 140 | } |
| 141 | set_layer_mask(mask); |
| 142 | } |
| 143 | |
| 144 | bool VisualInstance3D::get_layer_mask_value(int p_layer_number) const { |
| 145 | ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Render layer number must be between 1 and 20 inclusive."); |
| 146 | ERR_FAIL_COND_V_MSG(p_layer_number > 20, false, "Render layer number must be between 1 and 20 inclusive."); |
| 147 | return layers & (1 << (p_layer_number - 1)); |
| 148 | } |
| 149 | |
| 150 | void VisualInstance3D::set_sorting_offset(float p_offset) { |
| 151 | sorting_offset = p_offset; |
| 152 | RenderingServer::get_singleton()->instance_set_pivot_data(instance, sorting_offset, sorting_use_aabb_center); |
| 153 | } |
| 154 | |
| 155 | float VisualInstance3D::get_sorting_offset() const { |
| 156 | return sorting_offset; |
| 157 | } |
| 158 | |
| 159 | void VisualInstance3D::set_sorting_use_aabb_center(bool p_enabled) { |
| 160 | sorting_use_aabb_center = p_enabled; |
| 161 | RenderingServer::get_singleton()->instance_set_pivot_data(instance, sorting_offset, sorting_use_aabb_center); |
| 162 | } |
| 163 | |
| 164 | bool VisualInstance3D::is_sorting_use_aabb_center() const { |
| 165 | return sorting_use_aabb_center; |
| 166 | } |
| 167 | |
| 168 | void VisualInstance3D::_bind_methods() { |
| 169 | ClassDB::bind_method(D_METHOD("set_base", "base"), &VisualInstance3D::set_base); |
| 170 | ClassDB::bind_method(D_METHOD("get_base"), &VisualInstance3D::get_base); |
| 171 | ClassDB::bind_method(D_METHOD("get_instance"), &VisualInstance3D::get_instance); |
| 172 | ClassDB::bind_method(D_METHOD("set_layer_mask", "mask"), &VisualInstance3D::set_layer_mask); |
| 173 | ClassDB::bind_method(D_METHOD("get_layer_mask"), &VisualInstance3D::get_layer_mask); |
| 174 | ClassDB::bind_method(D_METHOD("set_layer_mask_value", "layer_number", "value"), &VisualInstance3D::set_layer_mask_value); |
| 175 | ClassDB::bind_method(D_METHOD("get_layer_mask_value", "layer_number"), &VisualInstance3D::get_layer_mask_value); |
| 176 | ClassDB::bind_method(D_METHOD("set_sorting_offset", "offset"), &VisualInstance3D::set_sorting_offset); |
| 177 | ClassDB::bind_method(D_METHOD("get_sorting_offset"), &VisualInstance3D::get_sorting_offset); |
| 178 | ClassDB::bind_method(D_METHOD("set_sorting_use_aabb_center", "enabled"), &VisualInstance3D::set_sorting_use_aabb_center); |
| 179 | ClassDB::bind_method(D_METHOD("is_sorting_use_aabb_center"), &VisualInstance3D::is_sorting_use_aabb_center); |
| 180 | |
| 181 | GDVIRTUAL_BIND(_get_aabb); |
| 182 | ADD_PROPERTY(PropertyInfo(Variant::INT, "layers", PROPERTY_HINT_LAYERS_3D_RENDER), "set_layer_mask", "get_layer_mask"); |
| 183 | |
| 184 | ADD_GROUP("Sorting", "sorting_"); |
| 185 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sorting_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_sorting_offset", "get_sorting_offset"); |
| 186 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sorting_use_aabb_center", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_sorting_use_aabb_center", "is_sorting_use_aabb_center"); |
| 187 | } |
| 188 | |
| 189 | void VisualInstance3D::set_base(const RID &p_base) { |
| 190 | RenderingServer::get_singleton()->instance_set_base(instance, p_base); |
| 191 | base = p_base; |
| 192 | } |
| 193 | |
| 194 | RID VisualInstance3D::get_base() const { |
| 195 | return base; |
| 196 | } |
| 197 | |
| 198 | VisualInstance3D::VisualInstance3D() { |
| 199 | _define_ancestry(AncestralClass::VISUAL_INSTANCE_3D); |
| 200 | |
| 201 | instance = RenderingServer::get_singleton()->instance_create(); |
| 202 | RenderingServer::get_singleton()->instance_attach_object_instance_id(instance, get_instance_id()); |
| 203 | set_notify_transform(true); |
| 204 | } |
| 205 | |
| 206 | VisualInstance3D::~VisualInstance3D() { |
| 207 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 208 | RenderingServer::get_singleton()->free_rid(instance); |
| 209 | } |
| 210 | |
| 211 | void GeometryInstance3D::set_material_override(const Ref<Material> &p_material) { |
| 212 | if (material_override.is_valid()) { |
| 213 | material_override->disconnect(CoreStringName(property_list_changed), callable_mp((Object *)this, &Object::notify_property_list_changed)); |
| 214 | } |
| 215 | material_override = p_material; |
| 216 | if (material_override.is_valid()) { |
| 217 | material_override->connect(CoreStringName(property_list_changed), callable_mp((Object *)this, &Object::notify_property_list_changed)); |
| 218 | } |
| 219 | RS::get_singleton()->instance_geometry_set_material_override(get_instance(), p_material.is_valid() ? p_material->get_rid() : RID()); |
| 220 | } |
| 221 | |
| 222 | Ref<Material> GeometryInstance3D::get_material_override() const { |
| 223 | return material_override; |
| 224 | } |
| 225 | |
| 226 | void GeometryInstance3D::set_material_overlay(const Ref<Material> &p_material) { |
| 227 | material_overlay = p_material; |
| 228 | RS::get_singleton()->instance_geometry_set_material_overlay(get_instance(), p_material.is_valid() ? p_material->get_rid() : RID()); |
| 229 | } |
| 230 | |
| 231 | Ref<Material> GeometryInstance3D::get_material_overlay() const { |
| 232 | return material_overlay; |
| 233 | } |
| 234 | |
| 235 | void GeometryInstance3D::set_transparency(float p_transparency) { |
| 236 | transparency = CLAMP(p_transparency, 0.0f, 1.0f); |
| 237 | RS::get_singleton()->instance_geometry_set_transparency(get_instance(), transparency); |
| 238 | update_configuration_warnings(); |
| 239 | } |
| 240 | |
| 241 | float GeometryInstance3D::get_transparency() const { |
| 242 | return transparency; |
| 243 | } |
| 244 | |
| 245 | void GeometryInstance3D::set_visibility_range_begin(float p_dist) { |
| 246 | visibility_range_begin = p_dist; |
| 247 | 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); |
| 248 | update_configuration_warnings(); |
| 249 | } |
| 250 | |
| 251 | float GeometryInstance3D::get_visibility_range_begin() const { |
| 252 | return visibility_range_begin; |
| 253 | } |
| 254 | |
| 255 | void GeometryInstance3D::set_visibility_range_end(float p_dist) { |
| 256 | visibility_range_end = p_dist; |
| 257 | 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); |
| 258 | update_configuration_warnings(); |
| 259 | } |
| 260 | |
| 261 | float GeometryInstance3D::get_visibility_range_end() const { |
| 262 | return visibility_range_end; |
| 263 | } |
| 264 | |
| 265 | void GeometryInstance3D::set_visibility_range_begin_margin(float p_dist) { |
| 266 | visibility_range_begin_margin = p_dist; |
| 267 | 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); |
| 268 | update_configuration_warnings(); |
| 269 | } |
| 270 | |
| 271 | float GeometryInstance3D::get_visibility_range_begin_margin() const { |
| 272 | return visibility_range_begin_margin; |
| 273 | } |
| 274 | |
| 275 | void GeometryInstance3D::set_visibility_range_end_margin(float p_dist) { |
| 276 | visibility_range_end_margin = p_dist; |
| 277 | 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); |
| 278 | update_configuration_warnings(); |
| 279 | } |
| 280 | |
| 281 | float GeometryInstance3D::get_visibility_range_end_margin() const { |
| 282 | return visibility_range_end_margin; |
| 283 | } |
| 284 | |
| 285 | void GeometryInstance3D::set_visibility_range_fade_mode(VisibilityRangeFadeMode p_mode) { |
| 286 | visibility_range_fade_mode = p_mode; |
| 287 | 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); |
| 288 | update_configuration_warnings(); |
| 289 | } |
| 290 | |
| 291 | GeometryInstance3D::VisibilityRangeFadeMode GeometryInstance3D::get_visibility_range_fade_mode() const { |
| 292 | return visibility_range_fade_mode; |
| 293 | } |
| 294 | |
| 295 | const StringName *GeometryInstance3D::_instance_uniform_get_remap(const StringName &p_name) const { |
| 296 | StringName *r = instance_shader_parameter_property_remap.getptr(p_name); |
| 297 | if (!r) { |
| 298 | String s = p_name; |
| 299 | #ifndef DISABLE_DEPRECATED |
| 300 | if (s.begins_with("shader_uniforms/")) { |
| 301 | s = s.replace("shader_uniforms/", "instance_shader_parameters/"); |
| 302 | } |
| 303 | #endif // DISABLE_DEPRECATED |
| 304 | if (s.begins_with("instance_shader_parameters/")) { |
| 305 | StringName pname = StringName(s); |
| 306 | StringName name = s.replace("instance_shader_parameters/", ""); |
| 307 | instance_shader_parameter_property_remap[pname] = name; |
| 308 | return instance_shader_parameter_property_remap.getptr(pname); |
| 309 | } |
| 310 | return nullptr; |
| 311 | } |
| 312 | |
| 313 | return r; |
| 314 | } |
| 315 | |
| 316 | bool GeometryInstance3D::_set(const StringName &p_name, const Variant &p_value) { |
| 317 | const StringName *r = _instance_uniform_get_remap(p_name); |
| 318 | if (r) { |
| 319 | set_instance_shader_parameter(*r, p_value); |
| 320 | return true; |
| 321 | } |
| 322 | #ifndef DISABLE_DEPRECATED |
| 323 | if (p_name == SNAME("use_in_baked_light") && bool(p_value)) { |
| 324 | set_gi_mode(GI_MODE_STATIC); |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | if (p_name == SNAME("use_dynamic_gi") && bool(p_value)) { |
| 329 | set_gi_mode(GI_MODE_DYNAMIC); |
| 330 | return true; |
| 331 | } |
| 332 | #endif // DISABLE_DEPRECATED |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | bool GeometryInstance3D::_get(const StringName &p_name, Variant &r_ret) const { |
| 337 | const StringName *r = _instance_uniform_get_remap(p_name); |
| 338 | if (r) { |
| 339 | r_ret = get_instance_shader_parameter(*r); |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | void GeometryInstance3D::_get_property_list(List<PropertyInfo> *p_list) const { |
| 347 | List<PropertyInfo> pinfo; |
| 348 | RS::get_singleton()->instance_geometry_get_shader_parameter_list(get_instance(), &pinfo); |
| 349 | for (PropertyInfo &pi : pinfo) { |
| 350 | bool has_def_value = false; |
| 351 | Variant def_value = RS::get_singleton()->instance_geometry_get_shader_parameter_default_value(get_instance(), pi.name); |
| 352 | if (def_value.get_type() != Variant::NIL) { |
| 353 | has_def_value = true; |
| 354 | } |
| 355 | if (instance_shader_parameters.has(pi.name)) { |
| 356 | pi.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE | (has_def_value ? (PROPERTY_USAGE_CHECKABLE | PROPERTY_USAGE_CHECKED) : PROPERTY_USAGE_NONE); |
| 357 | } else { |
| 358 | pi.usage = PROPERTY_USAGE_EDITOR | (has_def_value ? PROPERTY_USAGE_CHECKABLE : PROPERTY_USAGE_NONE); //do not save if not changed |
| 359 | } |
| 360 | |
| 361 | pi.name = "instance_shader_parameters/" + pi.name; |
| 362 | p_list->push_back(pi); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | void GeometryInstance3D::set_cast_shadows_setting(ShadowCastingSetting p_shadow_casting_setting) { |
| 367 | shadow_casting_setting = p_shadow_casting_setting; |
| 368 | |
| 369 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting(get_instance(), (RS::ShadowCastingSetting)p_shadow_casting_setting); |
| 370 | } |
| 371 | |
| 372 | GeometryInstance3D::ShadowCastingSetting GeometryInstance3D::get_cast_shadows_setting() const { |
| 373 | return shadow_casting_setting; |
| 374 | } |
| 375 | |
| 376 | void GeometryInstance3D::set_extra_cull_margin(float p_margin) { |
| 377 | ERR_FAIL_COND(p_margin < 0); |
| 378 | extra_cull_margin = p_margin; |
| 379 | RS::get_singleton()->instance_set_extra_visibility_margin(get_instance(), extra_cull_margin); |
| 380 | } |
| 381 | |
| 382 | float GeometryInstance3D::get_extra_cull_margin() const { |
| 383 | return extra_cull_margin; |
| 384 | } |
| 385 | |
| 386 | void GeometryInstance3D::set_lod_bias(float p_bias) { |
| 387 | ERR_FAIL_COND(p_bias < 0.0); |
| 388 | lod_bias = p_bias; |
| 389 | RS::get_singleton()->instance_geometry_set_lod_bias(get_instance(), lod_bias); |
| 390 | } |
| 391 | |
| 392 | float GeometryInstance3D::get_lod_bias() const { |
| 393 | return lod_bias; |
| 394 | } |
| 395 | |
| 396 | void GeometryInstance3D::set_instance_shader_parameter(const StringName &p_name, const Variant &p_value) { |
| 397 | if (p_value.get_type() == Variant::NIL) { |
| 398 | Variant def_value = RS::get_singleton()->instance_geometry_get_shader_parameter_default_value(get_instance(), p_name); |
| 399 | RS::get_singleton()->instance_geometry_set_shader_parameter(get_instance(), p_name, def_value); |
| 400 | instance_shader_parameters.erase(p_value); |
| 401 | } else { |
| 402 | instance_shader_parameters[p_name] = p_value; |
| 403 | if (p_value.get_type() == Variant::OBJECT) { |
| 404 | RID tex_id = p_value; |
| 405 | RS::get_singleton()->instance_geometry_set_shader_parameter(get_instance(), p_name, tex_id); |
| 406 | } else { |
| 407 | RS::get_singleton()->instance_geometry_set_shader_parameter(get_instance(), p_name, p_value); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | Variant GeometryInstance3D::get_instance_shader_parameter(const StringName &p_name) const { |
| 413 | return RS::get_singleton()->instance_geometry_get_shader_parameter(get_instance(), p_name); |
| 414 | } |
| 415 | |
| 416 | void GeometryInstance3D::set_custom_aabb(AABB p_aabb) { |
| 417 | if (p_aabb == custom_aabb) { |
| 418 | return; |
| 419 | } |
| 420 | custom_aabb = p_aabb; |
| 421 | RS::get_singleton()->instance_set_custom_aabb(get_instance(), custom_aabb); |
| 422 | update_gizmos(); |
| 423 | } |
| 424 | |
| 425 | AABB GeometryInstance3D::get_custom_aabb() const { |
| 426 | return custom_aabb; |
| 427 | } |
| 428 | |
| 429 | void GeometryInstance3D::set_lightmap_texel_scale(float p_scale) { |
| 430 | lightmap_texel_scale = p_scale; |
| 431 | } |
| 432 | |
| 433 | float GeometryInstance3D::get_lightmap_texel_scale() const { |
| 434 | return lightmap_texel_scale; |
| 435 | } |
| 436 | |
| 437 | #ifndef DISABLE_DEPRECATED |
| 438 | void GeometryInstance3D::set_lightmap_scale(LightmapScale p_scale) { |
| 439 | ERR_FAIL_INDEX(p_scale, LIGHTMAP_SCALE_MAX); |
| 440 | switch (p_scale) { |
| 441 | case GeometryInstance3D::LIGHTMAP_SCALE_1X: |
| 442 | lightmap_texel_scale = 1.0f; |
| 443 | break; |
| 444 | case GeometryInstance3D::LIGHTMAP_SCALE_2X: |
| 445 | lightmap_texel_scale = 2.0f; |
| 446 | break; |
| 447 | case GeometryInstance3D::LIGHTMAP_SCALE_4X: |
| 448 | lightmap_texel_scale = 4.0f; |
| 449 | break; |
| 450 | case GeometryInstance3D::LIGHTMAP_SCALE_8X: |
| 451 | lightmap_texel_scale = 8.0f; |
| 452 | break; |
| 453 | case GeometryInstance3D::LIGHTMAP_SCALE_MAX: |
| 454 | break; // Can't happen, but silences warning. |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | GeometryInstance3D::LightmapScale GeometryInstance3D::get_lightmap_scale() const { |
| 459 | // Return closest approximation. |
| 460 | if (lightmap_texel_scale < 1.5f) { |
| 461 | return GeometryInstance3D::LIGHTMAP_SCALE_1X; |
| 462 | } else if (lightmap_texel_scale < 3.0f) { |
| 463 | return GeometryInstance3D::LIGHTMAP_SCALE_2X; |
| 464 | } else if (lightmap_texel_scale < 6.0f) { |
| 465 | return GeometryInstance3D::LIGHTMAP_SCALE_4X; |
| 466 | } |
| 467 | |
| 468 | return GeometryInstance3D::LIGHTMAP_SCALE_8X; |
| 469 | } |
| 470 | #endif // DISABLE_DEPRECATED |
| 471 | |
| 472 | void GeometryInstance3D::set_gi_mode(GIMode p_mode) { |
| 473 | switch (p_mode) { |
| 474 | case GI_MODE_DISABLED: { |
| 475 | RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
| 476 | RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_USE_DYNAMIC_GI, false); |
| 477 | } break; |
| 478 | case GI_MODE_STATIC: { |
| 479 | RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_USE_BAKED_LIGHT, true); |
| 480 | RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_USE_DYNAMIC_GI, false); |
| 481 | |
| 482 | } break; |
| 483 | case GI_MODE_DYNAMIC: { |
| 484 | RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
| 485 | RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_USE_DYNAMIC_GI, true); |
| 486 | } break; |
| 487 | } |
| 488 | |
| 489 | gi_mode = p_mode; |
| 490 | } |
| 491 | |
| 492 | GeometryInstance3D::GIMode GeometryInstance3D::get_gi_mode() const { |
| 493 | return gi_mode; |
| 494 | } |
| 495 | |
| 496 | void GeometryInstance3D::set_ignore_occlusion_culling(bool p_enabled) { |
| 497 | ignore_occlusion_culling = p_enabled; |
| 498 | RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, ignore_occlusion_culling); |
| 499 | } |
| 500 | |
| 501 | bool GeometryInstance3D::is_ignoring_occlusion_culling() { |
| 502 | return ignore_occlusion_culling; |
| 503 | } |
| 504 | |
| 505 | Ref<TriangleMesh> GeometryInstance3D::generate_triangle_mesh() const { |
| 506 | return Ref<TriangleMesh>(); |
| 507 | } |
| 508 | |
| 509 | PackedStringArray GeometryInstance3D::get_configuration_warnings() const { |
| 510 | PackedStringArray warnings = VisualInstance3D::get_configuration_warnings(); |
| 511 | |
| 512 | if (!Math::is_zero_approx(visibility_range_end) && visibility_range_end <= visibility_range_begin) { |
| 513 | warnings.push_back(RTR("The GeometryInstance3D visibility range's End distance is set to a non-zero value, but is lower than the Begin distance.\nThis means the GeometryInstance3D will never be visible.\nTo resolve this, set the End distance to 0 or to a value greater than the Begin distance.")); |
| 514 | } |
| 515 | |
| 516 | if ((visibility_range_fade_mode == VISIBILITY_RANGE_FADE_SELF || visibility_range_fade_mode == VISIBILITY_RANGE_FADE_DEPENDENCIES) && !Math::is_zero_approx(visibility_range_begin) && Math::is_zero_approx(visibility_range_begin_margin)) { |
| 517 | warnings.push_back(RTR("The GeometryInstance3D is configured to fade in smoothly over distance, but the fade transition distance is set to 0.\nTo resolve this, increase Visibility Range Begin Margin above 0.")); |
| 518 | } |
| 519 | |
| 520 | if ((visibility_range_fade_mode == VISIBILITY_RANGE_FADE_SELF || visibility_range_fade_mode == VISIBILITY_RANGE_FADE_DEPENDENCIES) && !Math::is_zero_approx(visibility_range_end) && Math::is_zero_approx(visibility_range_end_margin)) { |
| 521 | warnings.push_back(RTR("The GeometryInstance3D is configured to fade out smoothly over distance, but the fade transition distance is set to 0.\nTo resolve this, increase Visibility Range End Margin above 0.")); |
| 522 | } |
| 523 | |
| 524 | if (!Math::is_zero_approx(transparency) && OS::get_singleton()->get_current_rendering_method() != "forward_plus") { |
| 525 | warnings.push_back(RTR("GeometryInstance3D transparency is only available when using the Forward+ renderer.")); |
| 526 | } |
| 527 | |
| 528 | if ((visibility_range_fade_mode == VISIBILITY_RANGE_FADE_SELF || visibility_range_fade_mode == VISIBILITY_RANGE_FADE_DEPENDENCIES) && OS::get_singleton()->get_current_rendering_method() != "forward_plus") { |
| 529 | warnings.push_back(RTR("GeometryInstance3D visibility range transparency fade is only available when using the Forward+ renderer.")); |
| 530 | } |
| 531 | |
| 532 | return warnings; |
| 533 | } |
| 534 | |
| 535 | void GeometryInstance3D::_validate_property(PropertyInfo &p_property) const { |
| 536 | if (p_property.name == "sorting_offset" || p_property.name == "sorting_use_aabb_center") { |
| 537 | p_property.usage = PROPERTY_USAGE_DEFAULT; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | void GeometryInstance3D::_bind_methods() { |
| 542 | ClassDB::bind_method(D_METHOD("set_material_override", "material"), &GeometryInstance3D::set_material_override); |
| 543 | ClassDB::bind_method(D_METHOD("get_material_override"), &GeometryInstance3D::get_material_override); |
| 544 | |
| 545 | ClassDB::bind_method(D_METHOD("set_material_overlay", "material"), &GeometryInstance3D::set_material_overlay); |
| 546 | ClassDB::bind_method(D_METHOD("get_material_overlay"), &GeometryInstance3D::get_material_overlay); |
| 547 | |
| 548 | ClassDB::bind_method(D_METHOD("set_cast_shadows_setting", "shadow_casting_setting"), &GeometryInstance3D::set_cast_shadows_setting); |
| 549 | ClassDB::bind_method(D_METHOD("get_cast_shadows_setting"), &GeometryInstance3D::get_cast_shadows_setting); |
| 550 | |
| 551 | ClassDB::bind_method(D_METHOD("set_lod_bias", "bias"), &GeometryInstance3D::set_lod_bias); |
| 552 | ClassDB::bind_method(D_METHOD("get_lod_bias"), &GeometryInstance3D::get_lod_bias); |
| 553 | |
| 554 | ClassDB::bind_method(D_METHOD("set_transparency", "transparency"), &GeometryInstance3D::set_transparency); |
| 555 | ClassDB::bind_method(D_METHOD("get_transparency"), &GeometryInstance3D::get_transparency); |
| 556 | |
| 557 | ClassDB::bind_method(D_METHOD("set_visibility_range_end_margin", "distance"), &GeometryInstance3D::set_visibility_range_end_margin); |
| 558 | ClassDB::bind_method(D_METHOD("get_visibility_range_end_margin"), &GeometryInstance3D::get_visibility_range_end_margin); |
| 559 | |
| 560 | ClassDB::bind_method(D_METHOD("set_visibility_range_end", "distance"), &GeometryInstance3D::set_visibility_range_end); |
| 561 | ClassDB::bind_method(D_METHOD("get_visibility_range_end"), &GeometryInstance3D::get_visibility_range_end); |
| 562 | |
| 563 | ClassDB::bind_method(D_METHOD("set_visibility_range_begin_margin", "distance"), &GeometryInstance3D::set_visibility_range_begin_margin); |
| 564 | ClassDB::bind_method(D_METHOD("get_visibility_range_begin_margin"), &GeometryInstance3D::get_visibility_range_begin_margin); |
| 565 | |
| 566 | ClassDB::bind_method(D_METHOD("set_visibility_range_begin", "distance"), &GeometryInstance3D::set_visibility_range_begin); |
| 567 | ClassDB::bind_method(D_METHOD("get_visibility_range_begin"), &GeometryInstance3D::get_visibility_range_begin); |
| 568 | |
| 569 | ClassDB::bind_method(D_METHOD("set_visibility_range_fade_mode", "mode"), &GeometryInstance3D::set_visibility_range_fade_mode); |
| 570 | ClassDB::bind_method(D_METHOD("get_visibility_range_fade_mode"), &GeometryInstance3D::get_visibility_range_fade_mode); |
| 571 | |
| 572 | ClassDB::bind_method(D_METHOD("set_instance_shader_parameter", "name", "value"), &GeometryInstance3D::set_instance_shader_parameter); |
| 573 | ClassDB::bind_method(D_METHOD("get_instance_shader_parameter", "name"), &GeometryInstance3D::get_instance_shader_parameter); |
| 574 | |
| 575 | ClassDB::bind_method(D_METHOD("set_extra_cull_margin", "margin"), &GeometryInstance3D::set_extra_cull_margin); |
| 576 | ClassDB::bind_method(D_METHOD("get_extra_cull_margin"), &GeometryInstance3D::get_extra_cull_margin); |
| 577 | |
| 578 | ClassDB::bind_method(D_METHOD("set_lightmap_texel_scale", "scale"), &GeometryInstance3D::set_lightmap_texel_scale); |
| 579 | ClassDB::bind_method(D_METHOD("get_lightmap_texel_scale"), &GeometryInstance3D::get_lightmap_texel_scale); |
| 580 | |
| 581 | #ifndef DISABLE_DEPRECATED |
| 582 | ClassDB::bind_method(D_METHOD("set_lightmap_scale", "scale"), &GeometryInstance3D::set_lightmap_scale); |
| 583 | ClassDB::bind_method(D_METHOD("get_lightmap_scale"), &GeometryInstance3D::get_lightmap_scale); |
| 584 | #endif // DISABLE_DEPRECATED |
| 585 | |
| 586 | ClassDB::bind_method(D_METHOD("set_gi_mode", "mode"), &GeometryInstance3D::set_gi_mode); |
| 587 | ClassDB::bind_method(D_METHOD("get_gi_mode"), &GeometryInstance3D::get_gi_mode); |
| 588 | |
| 589 | ClassDB::bind_method(D_METHOD("set_ignore_occlusion_culling", "ignore_culling"), &GeometryInstance3D::set_ignore_occlusion_culling); |
| 590 | ClassDB::bind_method(D_METHOD("is_ignoring_occlusion_culling"), &GeometryInstance3D::is_ignoring_occlusion_culling); |
| 591 | |
| 592 | ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &GeometryInstance3D::set_custom_aabb); |
| 593 | ClassDB::bind_method(D_METHOD("get_custom_aabb"), &GeometryInstance3D::get_custom_aabb); |
| 594 | |
| 595 | ClassDB::bind_method(D_METHOD("get_aabb"), &GeometryInstance3D::get_aabb); |
| 596 | |
| 597 | ADD_GROUP("Geometry", ""); |
| 598 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material_override", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial", PROPERTY_USAGE_DEFAULT), "set_material_override", "get_material_override"); |
| 599 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material_overlay", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial", PROPERTY_USAGE_DEFAULT), "set_material_overlay", "get_material_overlay"); |
| 600 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "transparency", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_transparency", "get_transparency"); |
| 601 | ADD_PROPERTY(PropertyInfo(Variant::INT, "cast_shadow", PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"), "set_cast_shadows_setting", "get_cast_shadows_setting"); |
| 602 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "extra_cull_margin", PROPERTY_HINT_RANGE, "0,16384,0.01,suffix:m"), "set_extra_cull_margin", "get_extra_cull_margin"); |
| 603 | ADD_PROPERTY(PropertyInfo(Variant::AABB, "custom_aabb", PROPERTY_HINT_NONE, "suffix:m"), "set_custom_aabb", "get_custom_aabb"); |
| 604 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lod_bias", PROPERTY_HINT_RANGE, "0.001,128,0.001"), "set_lod_bias", "get_lod_bias"); |
| 605 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_occlusion_culling"), "set_ignore_occlusion_culling", "is_ignoring_occlusion_culling"); |
| 606 | |
| 607 | ADD_GROUP("Global Illumination", "gi_"); |
| 608 | ADD_PROPERTY(PropertyInfo(Variant::INT, "gi_mode", PROPERTY_HINT_ENUM, "Disabled,Static,Dynamic"), "set_gi_mode", "get_gi_mode"); |
| 609 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gi_lightmap_texel_scale", PROPERTY_HINT_RANGE, "0.01,10,0.0001,or_greater"), "set_lightmap_texel_scale", "get_lightmap_texel_scale"); |
| 610 | #ifndef DISABLE_DEPRECATED |
| 611 | ADD_PROPERTY(PropertyInfo(Variant::INT, "gi_lightmap_scale", PROPERTY_HINT_ENUM, String::utf8("1×,2×,4×,8×"), PROPERTY_USAGE_NONE), "set_lightmap_scale", "get_lightmap_scale"); |
| 612 | #endif // DISABLE_DEPRECATED |
| 613 | |
| 614 | ADD_GROUP("Visibility Range", "visibility_range_"); |
| 615 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visibility_range_begin", PROPERTY_HINT_RANGE, "0.0,4096.0,0.01,or_greater,suffix:m"), "set_visibility_range_begin", "get_visibility_range_begin"); |
| 616 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visibility_range_begin_margin", PROPERTY_HINT_RANGE, "0.0,4096.0,0.01,or_greater,suffix:m"), "set_visibility_range_begin_margin", "get_visibility_range_begin_margin"); |
| 617 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visibility_range_end", PROPERTY_HINT_RANGE, "0.0,4096.0,0.01,or_greater,suffix:m"), "set_visibility_range_end", "get_visibility_range_end"); |
| 618 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visibility_range_end_margin", PROPERTY_HINT_RANGE, "0.0,4096.0,0.01,or_greater,suffix:m"), "set_visibility_range_end_margin", "get_visibility_range_end_margin"); |
| 619 | 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"); |
| 620 | |
| 621 | BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_OFF); |
| 622 | BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_ON); |
| 623 | BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_DOUBLE_SIDED); |
| 624 | BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_SHADOWS_ONLY); |
| 625 | |
| 626 | BIND_ENUM_CONSTANT(GI_MODE_DISABLED); |
| 627 | BIND_ENUM_CONSTANT(GI_MODE_STATIC); |
| 628 | BIND_ENUM_CONSTANT(GI_MODE_DYNAMIC); |
| 629 | |
| 630 | BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_1X); |
| 631 | BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_2X); |
| 632 | BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_4X); |
| 633 | BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_8X); |
| 634 | BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_MAX); |
| 635 | |
| 636 | BIND_ENUM_CONSTANT(VISIBILITY_RANGE_FADE_DISABLED); |
| 637 | BIND_ENUM_CONSTANT(VISIBILITY_RANGE_FADE_SELF); |
| 638 | BIND_ENUM_CONSTANT(VISIBILITY_RANGE_FADE_DEPENDENCIES); |
| 639 | } |
| 640 | |
| 641 | GeometryInstance3D::GeometryInstance3D() { |
| 642 | _define_ancestry(AncestralClass::GEOMETRY_INSTANCE_3D); |
| 643 | } |
| 644 | |
| 645 | GeometryInstance3D::~GeometryInstance3D() { |
| 646 | if (material_overlay.is_valid()) { |
| 647 | set_material_overlay(Ref<Material>()); |
| 648 | } |
| 649 | if (material_override.is_valid()) { |
| 650 | set_material_override(Ref<Material>()); |
| 651 | } |
| 652 | } |