| 1 | /**************************************************************************/ |
| 2 | /* node_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 "node_3d.h" |
| 32 | |
| 33 | #include "core/math/transform_interpolator.h" |
| 34 | #include "scene/3d/visual_instance_3d.h" |
| 35 | #include "scene/main/viewport.h" |
| 36 | #include "scene/property_utils.h" |
| 37 | |
| 38 | /* |
| 39 | |
| 40 | possible algorithms: |
| 41 | |
| 42 | Algorithm 1: (current) |
| 43 | |
| 44 | definition of invalidation: global is invalid |
| 45 | |
| 46 | 1) If a node sets a LOCAL, it produces an invalidation of everything above |
| 47 | . a) If above is invalid, don't keep invalidating upwards |
| 48 | 2) If a node sets a GLOBAL, it is converted to LOCAL (and forces validation of everything pending below) |
| 49 | |
| 50 | drawback: setting/reading globals is useful and used very often, and using affine inverses is slow |
| 51 | |
| 52 | --- |
| 53 | |
| 54 | Algorithm 2: (no longer current) |
| 55 | |
| 56 | definition of invalidation: NONE dirty, LOCAL dirty, GLOBAL dirty |
| 57 | |
| 58 | 1) If a node sets a LOCAL, it must climb the tree and set it as GLOBAL dirty |
| 59 | . a) marking GLOBALs as dirty up all the tree must be done always |
| 60 | 2) If a node sets a GLOBAL, it marks local as dirty, and that's all? |
| 61 | |
| 62 | //is clearing the dirty state correct in this case? |
| 63 | |
| 64 | drawback: setting a local down the tree forces many tree walks often |
| 65 | |
| 66 | -- |
| 67 | |
| 68 | future: no idea |
| 69 | |
| 70 | */ |
| 71 | |
| 72 | Node3DGizmo::Node3DGizmo() { |
| 73 | } |
| 74 | |
| 75 | void Node3D::_notify_dirty() { |
| 76 | #ifdef TOOLS_ENABLED |
| 77 | if ((!data.gizmos.is_empty() || data.notify_transform) && !data.ignore_notification && !xform_change.in_list()) { |
| 78 | #else |
| 79 | if (data.notify_transform && !data.ignore_notification && !xform_change.in_list()) { |
| 80 | |
| 81 | #endif |
| 82 | get_tree()->xform_change_list.add(&xform_change); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void Node3D::_update_local_transform() const { |
| 87 | // This function is called when the local transform (data.local_transform) is dirty and the right value is contained in the Euler rotation and scale. |
| 88 | data.local_transform.basis.set_euler_scale(data.euler_rotation, data.scale, data.euler_rotation_order); |
| 89 | _clear_dirty_bits(DIRTY_LOCAL_TRANSFORM); |
| 90 | } |
| 91 | |
| 92 | void Node3D::_update_rotation_and_scale() const { |
| 93 | // This function is called when the Euler rotation (data.euler_rotation) is dirty and the right value is contained in the local transform |
| 94 | |
| 95 | data.scale = data.local_transform.basis.get_scale(); |
| 96 | data.euler_rotation = data.local_transform.basis.get_euler_normalized(data.euler_rotation_order); |
| 97 | _clear_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE); |
| 98 | } |
| 99 | |
| 100 | void Node3D::_propagate_transform_changed_deferred() { |
| 101 | if (is_inside_tree() && !xform_change.in_list()) { |
| 102 | get_tree()->xform_change_list.add(&xform_change); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void Node3D::_propagate_transform_changed(Node3D *p_origin) { |
| 107 | if (!is_inside_tree()) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | for (uint32_t n = 0; n < data.node3d_children.size(); n++) { |
| 112 | Node3D *s = data.node3d_children[n]; |
| 113 | |
| 114 | // Don't propagate to a toplevel. |
| 115 | if (!s->data.top_level) { |
| 116 | s->_propagate_transform_changed(p_origin); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | #ifdef TOOLS_ENABLED |
| 121 | if ((!data.gizmos.is_empty() || data.notify_transform) && !data.ignore_notification && !xform_change.in_list()) { |
| 122 | #else |
| 123 | if (data.notify_transform && !data.ignore_notification && !xform_change.in_list()) { |
| 124 | #endif |
| 125 | if (likely(is_accessible_from_caller_thread())) { |
| 126 | get_tree()->xform_change_list.add(&xform_change); |
| 127 | } else { |
| 128 | // This should very rarely happen, but if it does at least make sure the notification is received eventually. |
| 129 | callable_mp(this, &Node3D::_propagate_transform_changed_deferred).call_deferred(); |
| 130 | } |
| 131 | } |
| 132 | _set_dirty_bits(DIRTY_GLOBAL_TRANSFORM | DIRTY_GLOBAL_INTERPOLATED_TRANSFORM); |
| 133 | } |
| 134 | |
| 135 | void Node3D::_notification(int p_what) { |
| 136 | switch (p_what) { |
| 137 | case NOTIFICATION_ACCESSIBILITY_UPDATE: { |
| 138 | RID ae = get_accessibility_element(); |
| 139 | ERR_FAIL_COND(ae.is_null()); |
| 140 | |
| 141 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_CONTAINER); |
| 142 | } break; |
| 143 | |
| 144 | case NOTIFICATION_ENTER_TREE: { |
| 145 | ERR_MAIN_THREAD_GUARD; |
| 146 | ERR_FAIL_NULL(get_tree()); |
| 147 | |
| 148 | Node *p = get_parent(); |
| 149 | if (p) { |
| 150 | data.parent = Object::cast_to<Node3D>(p); |
| 151 | } |
| 152 | |
| 153 | if (data.parent) { |
| 154 | data.index_in_parent = data.parent->data.node3d_children.size(); |
| 155 | data.parent->data.node3d_children.push_back(this); |
| 156 | } else if (data.index_in_parent != UINT32_MAX) { |
| 157 | data.index_in_parent = UINT32_MAX; |
| 158 | ERR_PRINT("Node3D ENTER_TREE detected without EXIT_TREE, recovering."); |
| 159 | } |
| 160 | |
| 161 | if (data.top_level && !Engine::get_singleton()->is_editor_hint()) { |
| 162 | if (data.parent) { |
| 163 | if (!data.top_level) { |
| 164 | data.local_transform = data.parent->get_global_transform() * get_transform(); |
| 165 | } else { |
| 166 | data.local_transform = get_transform(); |
| 167 | } |
| 168 | _replace_dirty_mask(DIRTY_EULER_ROTATION_AND_SCALE); // As local transform was updated, rot/scale should be dirty. |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | _set_dirty_bits(DIRTY_GLOBAL_TRANSFORM | DIRTY_GLOBAL_INTERPOLATED_TRANSFORM); // Global is always dirty upon entering a scene. |
| 173 | _notify_dirty(); |
| 174 | |
| 175 | notification(NOTIFICATION_ENTER_WORLD); |
| 176 | _update_visibility_parent(true); |
| 177 | |
| 178 | if (is_inside_tree() && get_tree()->is_physics_interpolation_enabled()) { |
| 179 | // Always reset FTI when entering tree and update the servers, |
| 180 | // both for interpolated and non-interpolated nodes, |
| 181 | // to ensure the server xforms are up to date. |
| 182 | fti_pump_xform(); |
| 183 | |
| 184 | // No need to interpolate as we are doing a reset. |
| 185 | data.global_transform_interpolated = get_global_transform(); |
| 186 | |
| 187 | // Make sure servers are up to date. |
| 188 | fti_update_servers_xform(); |
| 189 | |
| 190 | // As well as a reset based on the transform when adding, the user may change |
| 191 | // the transform during the first tick / frame, and we want to reset automatically |
| 192 | // at the end of the frame / tick (unless the user manually called `reset_physics_interpolation()`). |
| 193 | if (is_physics_interpolated()) { |
| 194 | get_tree()->get_scene_tree_fti().node_3d_request_reset(this); |
| 195 | } |
| 196 | } |
| 197 | } break; |
| 198 | |
| 199 | case NOTIFICATION_EXIT_TREE: { |
| 200 | ERR_MAIN_THREAD_GUARD; |
| 201 | |
| 202 | if (is_inside_tree()) { |
| 203 | get_tree()->get_scene_tree_fti().node_3d_notify_delete(this); |
| 204 | } |
| 205 | |
| 206 | notification(NOTIFICATION_EXIT_WORLD, true); |
| 207 | if (xform_change.in_list()) { |
| 208 | get_tree()->xform_change_list.remove(&xform_change); |
| 209 | } |
| 210 | |
| 211 | if (data.parent) { |
| 212 | if (data.index_in_parent != UINT32_MAX) { |
| 213 | // Aliases |
| 214 | uint32_t c = data.index_in_parent; |
| 215 | LocalVector<Node3D *> &parent_children = data.parent->data.node3d_children; |
| 216 | |
| 217 | parent_children.remove_at_unordered(c); |
| 218 | |
| 219 | // After unordered remove, we need to inform the moved child |
| 220 | // what their new id is in the parent children list. |
| 221 | if (parent_children.size() > c) { |
| 222 | parent_children[c]->data.index_in_parent = c; |
| 223 | } |
| 224 | } else { |
| 225 | ERR_PRINT("Node3D index_in_parent unset at EXIT_TREE."); |
| 226 | } |
| 227 | } |
| 228 | data.index_in_parent = UINT32_MAX; |
| 229 | |
| 230 | data.parent = nullptr; |
| 231 | _update_visibility_parent(true); |
| 232 | _disable_client_physics_interpolation(); |
| 233 | } break; |
| 234 | |
| 235 | case NOTIFICATION_ENTER_WORLD: { |
| 236 | ERR_MAIN_THREAD_GUARD; |
| 237 | |
| 238 | data.inside_world = true; |
| 239 | data.viewport = nullptr; |
| 240 | Node *parent = get_parent(); |
| 241 | while (parent && !data.viewport) { |
| 242 | data.viewport = Object::cast_to<Viewport>(parent); |
| 243 | parent = parent->get_parent(); |
| 244 | } |
| 245 | |
| 246 | ERR_FAIL_NULL(data.viewport); |
| 247 | |
| 248 | if (get_script_instance()) { |
| 249 | get_script_instance()->call(SNAME("_enter_world")); |
| 250 | } |
| 251 | |
| 252 | #ifdef TOOLS_ENABLED |
| 253 | if (is_part_of_edited_scene() && !data.gizmos_requested) { |
| 254 | data.gizmos_requested = true; |
| 255 | get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, SceneStringName(_spatial_editor_group), SNAME("_request_gizmo_for_id"), get_instance_id()); |
| 256 | } |
| 257 | #endif |
| 258 | } break; |
| 259 | |
| 260 | case NOTIFICATION_EXIT_WORLD: { |
| 261 | ERR_MAIN_THREAD_GUARD; |
| 262 | |
| 263 | #ifdef TOOLS_ENABLED |
| 264 | clear_gizmos(); |
| 265 | #endif |
| 266 | |
| 267 | if (get_script_instance()) { |
| 268 | get_script_instance()->call(SNAME("_exit_world")); |
| 269 | } |
| 270 | |
| 271 | data.viewport = nullptr; |
| 272 | data.inside_world = false; |
| 273 | } break; |
| 274 | |
| 275 | case NOTIFICATION_TRANSFORM_CHANGED: { |
| 276 | ERR_THREAD_GUARD; |
| 277 | |
| 278 | #ifdef TOOLS_ENABLED |
| 279 | for (int i = 0; i < data.gizmos.size(); i++) { |
| 280 | data.gizmos.write[i]->transform(); |
| 281 | } |
| 282 | #endif |
| 283 | } break; |
| 284 | |
| 285 | case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: { |
| 286 | if (data.client_physics_interpolation_data) { |
| 287 | data.client_physics_interpolation_data->global_xform_prev = data.client_physics_interpolation_data->global_xform_curr; |
| 288 | } |
| 289 | |
| 290 | // In most cases, nodes derived from Node3D will have to already have reset code available for SceneTreeFTI, |
| 291 | // so it makes sense for them to reuse this method rather than respond individually to NOTIFICATION_RESET_PHYSICS_INTERPOLATION, |
| 292 | // unless they need to perform specific tasks (like changing process modes). |
| 293 | fti_pump_xform(); |
| 294 | fti_pump_property(); |
| 295 | |
| 296 | // Detect whether we are using an identity transform. |
| 297 | // This is an optimization for faster tree transform concatenation. |
| 298 | data.fti_is_identity_xform = data.local_transform == Transform3D(); |
| 299 | } break; |
| 300 | case NOTIFICATION_SUSPENDED: |
| 301 | case NOTIFICATION_PAUSED: { |
| 302 | if (is_physics_interpolated_and_enabled()) { |
| 303 | data.local_transform_prev = get_transform(); |
| 304 | } |
| 305 | } break; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void Node3D::set_basis(const Basis &p_basis) { |
| 310 | ERR_THREAD_GUARD; |
| 311 | |
| 312 | set_transform(Transform3D(p_basis, data.local_transform.origin)); |
| 313 | } |
| 314 | void Node3D::set_quaternion(const Quaternion &p_quaternion) { |
| 315 | ERR_THREAD_GUARD; |
| 316 | if (_test_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE)) { |
| 317 | // We need the scale part, so if these are dirty, update it |
| 318 | data.scale = data.local_transform.basis.get_scale(); |
| 319 | _clear_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE); |
| 320 | } |
| 321 | data.local_transform.basis = Basis(p_quaternion, data.scale); |
| 322 | // Rotscale should not be marked dirty because that would cause precision loss issues with the scale. Instead reconstruct rotation now. |
| 323 | data.euler_rotation = data.local_transform.basis.get_euler_normalized(data.euler_rotation_order); |
| 324 | |
| 325 | _replace_dirty_mask(DIRTY_NONE); |
| 326 | |
| 327 | _propagate_transform_changed(this); |
| 328 | if (data.notify_local_transform) { |
| 329 | notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED); |
| 330 | } |
| 331 | fti_notify_node_changed(); |
| 332 | } |
| 333 | |
| 334 | Vector3 Node3D::get_global_position() const { |
| 335 | ERR_READ_THREAD_GUARD_V(Vector3()); |
| 336 | return get_global_transform().get_origin(); |
| 337 | } |
| 338 | |
| 339 | Basis Node3D::get_global_basis() const { |
| 340 | ERR_READ_THREAD_GUARD_V(Basis()); |
| 341 | return get_global_transform().get_basis(); |
| 342 | } |
| 343 | |
| 344 | void Node3D::set_global_position(const Vector3 &p_position) { |
| 345 | ERR_THREAD_GUARD; |
| 346 | Transform3D transform = get_global_transform(); |
| 347 | transform.set_origin(p_position); |
| 348 | set_global_transform(transform); |
| 349 | } |
| 350 | |
| 351 | void Node3D::set_global_basis(const Basis &p_basis) { |
| 352 | ERR_THREAD_GUARD; |
| 353 | Transform3D transform = get_global_transform(); |
| 354 | transform.set_basis(p_basis); |
| 355 | set_global_transform(transform); |
| 356 | } |
| 357 | |
| 358 | Vector3 Node3D::get_global_rotation() const { |
| 359 | ERR_READ_THREAD_GUARD_V(Vector3()); |
| 360 | return get_global_transform().get_basis().get_euler_normalized(); |
| 361 | } |
| 362 | |
| 363 | Vector3 Node3D::get_global_rotation_degrees() const { |
| 364 | ERR_READ_THREAD_GUARD_V(Vector3()); |
| 365 | Vector3 radians = get_global_rotation(); |
| 366 | return Vector3(Math::rad_to_deg(radians.x), Math::rad_to_deg(radians.y), Math::rad_to_deg(radians.z)); |
| 367 | } |
| 368 | |
| 369 | void Node3D::set_global_rotation(const Vector3 &p_euler_rad) { |
| 370 | ERR_THREAD_GUARD; |
| 371 | Transform3D transform = get_global_transform(); |
| 372 | transform.basis = Basis::from_euler(p_euler_rad) * Basis::from_scale(transform.basis.get_scale()); |
| 373 | set_global_transform(transform); |
| 374 | } |
| 375 | |
| 376 | void Node3D::set_global_rotation_degrees(const Vector3 &p_euler_degrees) { |
| 377 | ERR_THREAD_GUARD; |
| 378 | Vector3 radians(Math::deg_to_rad(p_euler_degrees.x), Math::deg_to_rad(p_euler_degrees.y), Math::deg_to_rad(p_euler_degrees.z)); |
| 379 | set_global_rotation(radians); |
| 380 | } |
| 381 | |
| 382 | void Node3D::fti_pump_xform() { |
| 383 | data.local_transform_prev = get_transform(); |
| 384 | } |
| 385 | |
| 386 | void Node3D::fti_notify_node_changed(bool p_transform_changed) { |
| 387 | if (is_inside_tree()) { |
| 388 | get_tree()->get_scene_tree_fti().node_3d_notify_changed(*this, p_transform_changed); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | void Node3D::set_transform(const Transform3D &p_transform) { |
| 393 | ERR_THREAD_GUARD; |
| 394 | data.local_transform = p_transform; |
| 395 | _replace_dirty_mask(DIRTY_EULER_ROTATION_AND_SCALE); // Make rot/scale dirty. |
| 396 | |
| 397 | _propagate_transform_changed(this); |
| 398 | if (data.notify_local_transform) { |
| 399 | notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED); |
| 400 | } |
| 401 | fti_notify_node_changed(); |
| 402 | } |
| 403 | |
| 404 | Basis Node3D::get_basis() const { |
| 405 | ERR_READ_THREAD_GUARD_V(Basis()); |
| 406 | return get_transform().basis; |
| 407 | } |
| 408 | |
| 409 | Quaternion Node3D::get_quaternion() const { |
| 410 | return get_transform().basis.get_rotation_quaternion(); |
| 411 | } |
| 412 | |
| 413 | void Node3D::set_global_transform(const Transform3D &p_transform) { |
| 414 | ERR_THREAD_GUARD; |
| 415 | Transform3D xform = (data.parent && !data.top_level) |
| 416 | ? data.parent->get_global_transform().affine_inverse() * p_transform |
| 417 | : p_transform; |
| 418 | |
| 419 | set_transform(xform); |
| 420 | } |
| 421 | |
| 422 | Transform3D Node3D::get_transform() const { |
| 423 | ERR_READ_THREAD_GUARD_V(Transform3D()); |
| 424 | if (_test_dirty_bits(DIRTY_LOCAL_TRANSFORM)) { |
| 425 | // This update can happen if needed over multiple threads. |
| 426 | _update_local_transform(); |
| 427 | } |
| 428 | |
| 429 | return data.local_transform; |
| 430 | } |
| 431 | |
| 432 | // Return false to timeout and remove from the client interpolation list. |
| 433 | bool Node3D::update_client_physics_interpolation_data() { |
| 434 | if (!is_inside_tree() || !_is_physics_interpolated_client_side()) { |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | ERR_FAIL_NULL_V(data.client_physics_interpolation_data, false); |
| 439 | ClientPhysicsInterpolationData &pid = *data.client_physics_interpolation_data; |
| 440 | |
| 441 | uint64_t tick = Engine::get_singleton()->get_physics_frames(); |
| 442 | |
| 443 | // Has this update been done already this tick? |
| 444 | // (For instance, get_global_transform_interpolated() could be called multiple times.) |
| 445 | if (pid.current_physics_tick != tick) { |
| 446 | // Timeout? |
| 447 | if (tick >= pid.timeout_physics_tick) { |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | if (pid.current_physics_tick == (tick - 1)) { |
| 452 | // Normal interpolation situation, there is a continuous flow of data |
| 453 | // from one tick to the next... |
| 454 | pid.global_xform_prev = pid.global_xform_curr; |
| 455 | } else { |
| 456 | // There has been a gap, we cannot sensibly offer interpolation over |
| 457 | // a multitick gap, so we will teleport. |
| 458 | pid.global_xform_prev = get_global_transform(); |
| 459 | } |
| 460 | pid.current_physics_tick = tick; |
| 461 | } |
| 462 | |
| 463 | pid.global_xform_curr = get_global_transform(); |
| 464 | return true; |
| 465 | } |
| 466 | |
| 467 | void Node3D::_disable_client_physics_interpolation() { |
| 468 | // Disable any current client side interpolation. |
| 469 | // (This can always restart as normal if you later re-attach the node to the SceneTree.) |
| 470 | if (data.client_physics_interpolation_data) { |
| 471 | memdelete(data.client_physics_interpolation_data); |
| 472 | data.client_physics_interpolation_data = nullptr; |
| 473 | |
| 474 | SceneTree *tree = get_tree(); |
| 475 | if (tree && _client_physics_interpolation_node_3d_list.in_list()) { |
| 476 | tree->client_physics_interpolation_remove_node_3d(&_client_physics_interpolation_node_3d_list); |
| 477 | } |
| 478 | } |
| 479 | _set_physics_interpolated_client_side(false); |
| 480 | } |
| 481 | |
| 482 | Transform3D Node3D::_get_global_transform_interpolated(real_t p_interpolation_fraction) { |
| 483 | ERR_FAIL_COND_V(!is_inside_tree(), Transform3D()); |
| 484 | |
| 485 | // Set in motion the mechanisms for client side interpolation if not already active. |
| 486 | if (!_is_physics_interpolated_client_side()) { |
| 487 | _set_physics_interpolated_client_side(true); |
| 488 | |
| 489 | ERR_FAIL_COND_V(data.client_physics_interpolation_data != nullptr, Transform3D()); |
| 490 | data.client_physics_interpolation_data = memnew(ClientPhysicsInterpolationData); |
| 491 | data.client_physics_interpolation_data->global_xform_curr = get_global_transform(); |
| 492 | data.client_physics_interpolation_data->global_xform_prev = data.client_physics_interpolation_data->global_xform_curr; |
| 493 | data.client_physics_interpolation_data->current_physics_tick = Engine::get_singleton()->get_physics_frames(); |
| 494 | } |
| 495 | |
| 496 | // Storing the last tick we requested client interpolation allows us to timeout |
| 497 | // and remove client interpolated nodes from the list to save processing. |
| 498 | // We use some arbitrary timeout here, but this could potentially be user defined. |
| 499 | |
| 500 | // Note: This timeout has to be larger than the number of ticks in a frame, otherwise the interpolated |
| 501 | // data will stop flowing before the next frame is drawn. This should only be relevant at high tick rates. |
| 502 | // We could alternatively do this by frames rather than ticks and avoid this problem, but then the behavior |
| 503 | // would be machine dependent. |
| 504 | data.client_physics_interpolation_data->timeout_physics_tick = Engine::get_singleton()->get_physics_frames() + 256; |
| 505 | |
| 506 | // Make sure data is up to date. |
| 507 | update_client_physics_interpolation_data(); |
| 508 | |
| 509 | // Interpolate the current data. |
| 510 | const Transform3D &xform_curr = data.client_physics_interpolation_data->global_xform_curr; |
| 511 | const Transform3D &xform_prev = data.client_physics_interpolation_data->global_xform_prev; |
| 512 | |
| 513 | Transform3D res; |
| 514 | TransformInterpolator::interpolate_transform_3d(xform_prev, xform_curr, res, p_interpolation_fraction); |
| 515 | |
| 516 | SceneTree *tree = get_tree(); |
| 517 | |
| 518 | // This should not happen, as is_inside_tree() is checked earlier. |
| 519 | ERR_FAIL_NULL_V(tree, res); |
| 520 | if (!_client_physics_interpolation_node_3d_list.in_list()) { |
| 521 | tree->client_physics_interpolation_add_node_3d(&_client_physics_interpolation_node_3d_list); |
| 522 | } |
| 523 | |
| 524 | return res; |
| 525 | } |
| 526 | |
| 527 | // Visible nodes - get_global_transform_interpolated is cheap. |
| 528 | // Invisible nodes - get_global_transform_interpolated is expensive, try to avoid. |
| 529 | Transform3D Node3D::get_global_transform_interpolated() { |
| 530 | #if 1 |
| 531 | // Pass through if physics interpolation is switched off. |
| 532 | // This is a convenience, as it allows you to easy turn off interpolation |
| 533 | // without changing any code. |
| 534 | if (SceneTree::is_fti_enabled() && is_inside_tree() && !Engine::get_singleton()->is_in_physics_frame()) { |
| 535 | // Note that with SceneTreeFTI, we may want to calculate interpolated transform for a node |
| 536 | // with physics interpolation set to OFF, if it has a parent that is ON. |
| 537 | |
| 538 | // Cheap case. |
| 539 | // We already pre-cache the visible_in_tree for VisualInstances, but NOT for Node3Ds, so we have to |
| 540 | // deal with non-VIs the slow way. |
| 541 | if (Object::cast_to<VisualInstance3D>(this) && _is_vi_visible() && data.fti_global_xform_interp_set) { |
| 542 | return data.global_transform_interpolated; |
| 543 | } |
| 544 | |
| 545 | // Find out if visible in tree. |
| 546 | // If not visible in tree, find the FIRST ancestor that is visible in tree. |
| 547 | const Node3D *visible_parent = nullptr; |
| 548 | const Node3D *s = this; |
| 549 | bool visible = true; |
| 550 | bool visible_in_tree = true; |
| 551 | |
| 552 | while (s) { |
| 553 | if (!s->data.visible) { |
| 554 | visible_in_tree = false; |
| 555 | visible = false; |
| 556 | } else { |
| 557 | if (!visible) { |
| 558 | visible_parent = s; |
| 559 | visible = true; |
| 560 | } |
| 561 | } |
| 562 | s = s->data.parent; |
| 563 | } |
| 564 | |
| 565 | // Simplest case, we can return the interpolated xform calculated by SceneTreeFTI. |
| 566 | if (visible_in_tree) { |
| 567 | return data.fti_global_xform_interp_set ? data.global_transform_interpolated : get_global_transform(); |
| 568 | } else if (visible_parent) { |
| 569 | // INVISIBLE case. Not visible, but there is a visible ancestor somewhere in the chain. |
| 570 | if (_get_scene_tree_depth() < 1) { |
| 571 | // This should not happen unless there a problem has been introduced in the scene tree depth code. |
| 572 | // Print a non-spammy error and return something reasonable. |
| 573 | ERR_PRINT_ONCE("depth is < 1."); |
| 574 | return get_global_transform(); |
| 575 | } |
| 576 | |
| 577 | // The interpolated xform is not already calculated for invisible nodes, but we can calculate this |
| 578 | // manually on demand if there is a visible parent. |
| 579 | // First create the chain (backwards), from the node up to first visible parent. |
| 580 | const Node3D **parents = (const Node3D **)alloca((sizeof(const Node3D *) * _get_scene_tree_depth())); |
| 581 | int32_t num_parents = 0; |
| 582 | |
| 583 | s = this; |
| 584 | while (s) { |
| 585 | if (s == visible_parent) { |
| 586 | // Finished. |
| 587 | break; |
| 588 | } |
| 589 | |
| 590 | parents[num_parents++] = s; |
| 591 | s = s->data.parent; |
| 592 | } |
| 593 | |
| 594 | // Now calculate the interpolated chain forwards. |
| 595 | float interpolation_fraction = Engine::get_singleton()->get_physics_interpolation_fraction(); |
| 596 | |
| 597 | // Seed the xform with the visible parent. |
| 598 | Transform3D xform = visible_parent->data.fti_global_xform_interp_set ? visible_parent->data.global_transform_interpolated : visible_parent->get_global_transform(); |
| 599 | Transform3D local_interp; |
| 600 | |
| 601 | // Backwards through the list is forwards through the chain through the tree. |
| 602 | for (int32_t n = num_parents - 1; n >= 0; n--) { |
| 603 | s = parents[n]; |
| 604 | |
| 605 | if (s->is_physics_interpolated()) { |
| 606 | // Make sure to call `get_transform()` rather than using local_transform directly, because |
| 607 | // local_transform may be dirty and need updating from rotation / scale. |
| 608 | TransformInterpolator::interpolate_transform_3d(s->data.local_transform_prev, s->get_transform(), local_interp, interpolation_fraction); |
| 609 | } else { |
| 610 | local_interp = s->get_transform(); |
| 611 | } |
| 612 | xform *= local_interp; |
| 613 | } |
| 614 | |
| 615 | // We could save this in case of multiple calls, |
| 616 | // but probably not necessary. |
| 617 | return xform; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | return get_global_transform(); |
| 622 | #else |
| 623 | // OLD METHOD - deprecated since moving to SceneTreeFTI, |
| 624 | // but leaving for reference and comparison for debugging. |
| 625 | |
| 626 | if (!is_physics_interpolated_and_enabled()) { |
| 627 | return get_global_transform(); |
| 628 | } |
| 629 | |
| 630 | // If we are in the physics frame, the interpolated global transform is meaningless. |
| 631 | // However, there is an exception, we may want to use this as a means of starting off the client |
| 632 | // interpolation pump if not already started (when _is_physics_interpolated_client_side() is false). |
| 633 | if (Engine::get_singleton()->is_in_physics_frame() && _is_physics_interpolated_client_side()) { |
| 634 | return get_global_transform(); |
| 635 | } |
| 636 | |
| 637 | return _get_global_transform_interpolated(Engine::get_singleton()->get_physics_interpolation_fraction()); |
| 638 | #endif |
| 639 | } |
| 640 | |
| 641 | Transform3D Node3D::get_global_transform() const { |
| 642 | ERR_FAIL_COND_V(!is_inside_tree(), Transform3D()); |
| 643 | |
| 644 | /* Due to how threads work at scene level, while this global transform won't be able to be changed from outside a thread, |
| 645 | * it is possible that multiple threads can access it while it's dirty from previous work. Due to this, we must ensure that |
| 646 | * the dirty/update process is thread safe by utilizing atomic copies. |
| 647 | */ |
| 648 | |
| 649 | uint32_t dirty = _read_dirty_mask(); |
| 650 | if (dirty & DIRTY_GLOBAL_TRANSFORM) { |
| 651 | if (dirty & DIRTY_LOCAL_TRANSFORM) { |
| 652 | _update_local_transform(); // Update local transform atomically. |
| 653 | } |
| 654 | |
| 655 | Transform3D new_global; |
| 656 | if (data.parent && !data.top_level) { |
| 657 | new_global = data.parent->get_global_transform() * data.local_transform; |
| 658 | } else { |
| 659 | new_global = data.local_transform; |
| 660 | } |
| 661 | |
| 662 | if (data.disable_scale) { |
| 663 | new_global.basis.orthonormalize(); |
| 664 | } |
| 665 | |
| 666 | data.global_transform = new_global; |
| 667 | _clear_dirty_bits(DIRTY_GLOBAL_TRANSFORM); |
| 668 | } |
| 669 | |
| 670 | return data.global_transform; |
| 671 | } |
| 672 | |
| 673 | #ifdef TOOLS_ENABLED |
| 674 | Transform3D Node3D::get_global_gizmo_transform() const { |
| 675 | return get_global_transform(); |
| 676 | } |
| 677 | |
| 678 | Transform3D Node3D::get_local_gizmo_transform() const { |
| 679 | return get_transform(); |
| 680 | } |
| 681 | #endif |
| 682 | |
| 683 | Node3D *Node3D::get_parent_node_3d() const { |
| 684 | ERR_READ_THREAD_GUARD_V(nullptr); // This can't be changed on threads anyway. |
| 685 | if (data.top_level) { |
| 686 | return nullptr; |
| 687 | } |
| 688 | |
| 689 | return Object::cast_to<Node3D>(get_parent()); |
| 690 | } |
| 691 | |
| 692 | Transform3D Node3D::get_relative_transform(const Node *p_parent) const { |
| 693 | ERR_READ_THREAD_GUARD_V(Transform3D()); |
| 694 | if (p_parent == this) { |
| 695 | return Transform3D(); |
| 696 | } |
| 697 | |
| 698 | ERR_FAIL_NULL_V(data.parent, Transform3D()); |
| 699 | |
| 700 | if (p_parent == data.parent) { |
| 701 | return get_transform(); |
| 702 | } else { |
| 703 | return data.parent->get_relative_transform(p_parent) * get_transform(); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | void Node3D::set_position(const Vector3 &p_position) { |
| 708 | ERR_THREAD_GUARD; |
| 709 | data.local_transform.origin = p_position; |
| 710 | _propagate_transform_changed(this); |
| 711 | if (data.notify_local_transform) { |
| 712 | notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED); |
| 713 | } |
| 714 | fti_notify_node_changed(); |
| 715 | } |
| 716 | |
| 717 | void Node3D::set_rotation_edit_mode(RotationEditMode p_mode) { |
| 718 | ERR_THREAD_GUARD; |
| 719 | if (data.rotation_edit_mode == p_mode) { |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | bool transform_changed = false; |
| 724 | if (data.rotation_edit_mode == ROTATION_EDIT_MODE_BASIS && !_test_dirty_bits(DIRTY_LOCAL_TRANSFORM)) { |
| 725 | data.local_transform.orthogonalize(); |
| 726 | transform_changed = true; |
| 727 | } |
| 728 | |
| 729 | data.rotation_edit_mode = p_mode; |
| 730 | |
| 731 | if (p_mode == ROTATION_EDIT_MODE_EULER && _test_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE)) { |
| 732 | // If going to Euler mode, ensure that vectors are _not_ dirty, else the retrieved value may be wrong. |
| 733 | // Otherwise keep what is there, so switching back and forth between modes does not break the vectors. |
| 734 | |
| 735 | _update_rotation_and_scale(); |
| 736 | } |
| 737 | |
| 738 | if (transform_changed) { |
| 739 | _propagate_transform_changed(this); |
| 740 | if (data.notify_local_transform) { |
| 741 | notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | notify_property_list_changed(); |
| 746 | } |
| 747 | |
| 748 | Node3D::RotationEditMode Node3D::get_rotation_edit_mode() const { |
| 749 | ERR_READ_THREAD_GUARD_V(ROTATION_EDIT_MODE_EULER); |
| 750 | return data.rotation_edit_mode; |
| 751 | } |
| 752 | |
| 753 | void Node3D::set_rotation_order(EulerOrder p_order) { |
| 754 | ERR_THREAD_GUARD; |
| 755 | if (data.euler_rotation_order == p_order) { |
| 756 | return; |
| 757 | } |
| 758 | |
| 759 | ERR_FAIL_INDEX(int32_t(p_order), 6); |
| 760 | bool transform_changed = false; |
| 761 | |
| 762 | uint32_t dirty = _read_dirty_mask(); |
| 763 | if ((dirty & DIRTY_EULER_ROTATION_AND_SCALE)) { |
| 764 | _update_rotation_and_scale(); |
| 765 | } else if ((dirty & DIRTY_LOCAL_TRANSFORM)) { |
| 766 | data.euler_rotation = Basis::from_euler(data.euler_rotation, data.euler_rotation_order).get_euler_normalized(p_order); |
| 767 | transform_changed = true; |
| 768 | } else { |
| 769 | _set_dirty_bits(DIRTY_LOCAL_TRANSFORM); |
| 770 | transform_changed = true; |
| 771 | } |
| 772 | |
| 773 | data.euler_rotation_order = p_order; |
| 774 | |
| 775 | if (transform_changed) { |
| 776 | _propagate_transform_changed(this); |
| 777 | if (data.notify_local_transform) { |
| 778 | notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED); |
| 779 | } |
| 780 | } |
| 781 | notify_property_list_changed(); // Will change the rotation property. |
| 782 | } |
| 783 | |
| 784 | EulerOrder Node3D::get_rotation_order() const { |
| 785 | ERR_READ_THREAD_GUARD_V(EulerOrder::XYZ); |
| 786 | return data.euler_rotation_order; |
| 787 | } |
| 788 | |
| 789 | void Node3D::set_rotation(const Vector3 &p_euler_rad) { |
| 790 | ERR_THREAD_GUARD; |
| 791 | if (_test_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE)) { |
| 792 | // Update scale only if rotation and scale are dirty, as rotation will be overridden. |
| 793 | data.scale = data.local_transform.basis.get_scale(); |
| 794 | _clear_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE); |
| 795 | } |
| 796 | |
| 797 | data.euler_rotation = p_euler_rad; |
| 798 | _replace_dirty_mask(DIRTY_LOCAL_TRANSFORM); |
| 799 | _propagate_transform_changed(this); |
| 800 | if (data.notify_local_transform) { |
| 801 | notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED); |
| 802 | } |
| 803 | fti_notify_node_changed(); |
| 804 | } |
| 805 | |
| 806 | void Node3D::set_rotation_degrees(const Vector3 &p_euler_degrees) { |
| 807 | ERR_THREAD_GUARD; |
| 808 | Vector3 radians(Math::deg_to_rad(p_euler_degrees.x), Math::deg_to_rad(p_euler_degrees.y), Math::deg_to_rad(p_euler_degrees.z)); |
| 809 | set_rotation(radians); |
| 810 | } |
| 811 | |
| 812 | void Node3D::set_scale(const Vector3 &p_scale) { |
| 813 | ERR_THREAD_GUARD; |
| 814 | if (_test_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE)) { |
| 815 | // Update rotation only if rotation and scale are dirty, as scale will be overridden. |
| 816 | data.euler_rotation = data.local_transform.basis.get_euler_normalized(data.euler_rotation_order); |
| 817 | _clear_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE); |
| 818 | } |
| 819 | |
| 820 | data.scale = p_scale; |
| 821 | _replace_dirty_mask(DIRTY_LOCAL_TRANSFORM); |
| 822 | _propagate_transform_changed(this); |
| 823 | if (data.notify_local_transform) { |
| 824 | notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED); |
| 825 | } |
| 826 | fti_notify_node_changed(); |
| 827 | } |
| 828 | |
| 829 | Vector3 Node3D::get_position() const { |
| 830 | ERR_READ_THREAD_GUARD_V(Vector3()); |
| 831 | return data.local_transform.origin; |
| 832 | } |
| 833 | |
| 834 | Vector3 Node3D::get_rotation() const { |
| 835 | ERR_READ_THREAD_GUARD_V(Vector3()); |
| 836 | if (_test_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE)) { |
| 837 | _update_rotation_and_scale(); |
| 838 | } |
| 839 | |
| 840 | return data.euler_rotation; |
| 841 | } |
| 842 | |
| 843 | Vector3 Node3D::get_rotation_degrees() const { |
| 844 | ERR_READ_THREAD_GUARD_V(Vector3()); |
| 845 | Vector3 radians = get_rotation(); |
| 846 | return Vector3(Math::rad_to_deg(radians.x), Math::rad_to_deg(radians.y), Math::rad_to_deg(radians.z)); |
| 847 | } |
| 848 | |
| 849 | Vector3 Node3D::get_scale() const { |
| 850 | ERR_READ_THREAD_GUARD_V(Vector3()); |
| 851 | if (_test_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE)) { |
| 852 | _update_rotation_and_scale(); |
| 853 | } |
| 854 | |
| 855 | return data.scale; |
| 856 | } |
| 857 | |
| 858 | void Node3D::update_gizmos() { |
| 859 | ERR_THREAD_GUARD; |
| 860 | #ifdef TOOLS_ENABLED |
| 861 | if (!is_inside_world()) { |
| 862 | return; |
| 863 | } |
| 864 | |
| 865 | if (data.gizmos.is_empty()) { |
| 866 | if (!data.gizmos_requested) { |
| 867 | data.gizmos_requested = true; |
| 868 | get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, SceneStringName(_spatial_editor_group), SNAME("_request_gizmo_for_id"), get_instance_id()); |
| 869 | } |
| 870 | return; |
| 871 | } |
| 872 | if (data.gizmos_dirty) { |
| 873 | return; |
| 874 | } |
| 875 | data.gizmos_dirty = true; |
| 876 | callable_mp(this, &Node3D::_update_gizmos).call_deferred(); |
| 877 | #endif |
| 878 | } |
| 879 | |
| 880 | void Node3D::set_subgizmo_selection(Ref<Node3DGizmo> p_gizmo, int p_id, Transform3D p_transform) { |
| 881 | ERR_THREAD_GUARD; |
| 882 | #ifdef TOOLS_ENABLED |
| 883 | if (!is_inside_world()) { |
| 884 | return; |
| 885 | } |
| 886 | |
| 887 | if (is_part_of_edited_scene()) { |
| 888 | get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, SceneStringName(_spatial_editor_group), SNAME("_set_subgizmo_selection"), this, p_gizmo, p_id, p_transform); |
| 889 | } |
| 890 | #endif |
| 891 | } |
| 892 | |
| 893 | void Node3D::clear_subgizmo_selection() { |
| 894 | ERR_THREAD_GUARD; |
| 895 | #ifdef TOOLS_ENABLED |
| 896 | if (!is_inside_world()) { |
| 897 | return; |
| 898 | } |
| 899 | |
| 900 | if (data.gizmos.is_empty()) { |
| 901 | return; |
| 902 | } |
| 903 | |
| 904 | if (is_part_of_edited_scene()) { |
| 905 | get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, SceneStringName(_spatial_editor_group), SNAME("_clear_subgizmo_selection"), this); |
| 906 | } |
| 907 | #endif |
| 908 | } |
| 909 | |
| 910 | void Node3D::add_gizmo(Ref<Node3DGizmo> p_gizmo) { |
| 911 | ERR_THREAD_GUARD; |
| 912 | #ifdef TOOLS_ENABLED |
| 913 | if (data.gizmos_disabled || p_gizmo.is_null()) { |
| 914 | return; |
| 915 | } |
| 916 | data.gizmos.push_back(p_gizmo); |
| 917 | |
| 918 | if (p_gizmo.is_valid() && is_inside_world()) { |
| 919 | p_gizmo->create(); |
| 920 | if (is_visible_in_tree()) { |
| 921 | p_gizmo->redraw(); |
| 922 | } |
| 923 | p_gizmo->transform(); |
| 924 | } |
| 925 | #endif |
| 926 | } |
| 927 | |
| 928 | void Node3D::remove_gizmo(Ref<Node3DGizmo> p_gizmo) { |
| 929 | ERR_THREAD_GUARD; |
| 930 | #ifdef TOOLS_ENABLED |
| 931 | int idx = data.gizmos.find(p_gizmo); |
| 932 | if (idx != -1) { |
| 933 | p_gizmo->free(); |
| 934 | data.gizmos.remove_at(idx); |
| 935 | } |
| 936 | #endif |
| 937 | } |
| 938 | |
| 939 | void Node3D::clear_gizmos() { |
| 940 | ERR_THREAD_GUARD; |
| 941 | #ifdef TOOLS_ENABLED |
| 942 | for (int i = 0; i < data.gizmos.size(); i++) { |
| 943 | data.gizmos.write[i]->free(); |
| 944 | } |
| 945 | data.gizmos.clear(); |
| 946 | data.gizmos_requested = false; |
| 947 | #endif |
| 948 | } |
| 949 | |
| 950 | TypedArray<Node3DGizmo> Node3D::get_gizmos_bind() const { |
| 951 | ERR_THREAD_GUARD_V(TypedArray<Node3DGizmo>()); |
| 952 | TypedArray<Node3DGizmo> ret; |
| 953 | |
| 954 | #ifdef TOOLS_ENABLED |
| 955 | for (int i = 0; i < data.gizmos.size(); i++) { |
| 956 | ret.push_back(Variant(data.gizmos[i].ptr())); |
| 957 | } |
| 958 | #endif |
| 959 | |
| 960 | return ret; |
| 961 | } |
| 962 | |
| 963 | Vector<Ref<Node3DGizmo>> Node3D::get_gizmos() const { |
| 964 | ERR_THREAD_GUARD_V(Vector<Ref<Node3DGizmo>>()); |
| 965 | #ifdef TOOLS_ENABLED |
| 966 | return data.gizmos; |
| 967 | #else |
| 968 | return Vector<Ref<Node3DGizmo>>(); |
| 969 | #endif |
| 970 | } |
| 971 | |
| 972 | void Node3D::_replace_dirty_mask(uint32_t p_mask) const { |
| 973 | if (is_group_processing()) { |
| 974 | data.dirty.mt.set(p_mask); |
| 975 | } else { |
| 976 | data.dirty.st = p_mask; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | void Node3D::_set_dirty_bits(uint32_t p_bits) const { |
| 981 | if (is_group_processing()) { |
| 982 | data.dirty.mt.bit_or(p_bits); |
| 983 | } else { |
| 984 | data.dirty.st |= p_bits; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | void Node3D::_clear_dirty_bits(uint32_t p_bits) const { |
| 989 | if (is_group_processing()) { |
| 990 | data.dirty.mt.bit_and(~p_bits); |
| 991 | } else { |
| 992 | data.dirty.st &= ~p_bits; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | void Node3D::_update_gizmos() { |
| 997 | #ifdef TOOLS_ENABLED |
| 998 | if (data.gizmos_disabled || !is_inside_world() || !data.gizmos_dirty) { |
| 999 | data.gizmos_dirty = false; |
| 1000 | return; |
| 1001 | } |
| 1002 | data.gizmos_dirty = false; |
| 1003 | for (int i = 0; i < data.gizmos.size(); i++) { |
| 1004 | if (is_visible_in_tree()) { |
| 1005 | data.gizmos.write[i]->redraw(); |
| 1006 | } else { |
| 1007 | data.gizmos.write[i]->clear(); |
| 1008 | } |
| 1009 | } |
| 1010 | #endif |
| 1011 | } |
| 1012 | |
| 1013 | void Node3D::set_disable_gizmos(bool p_enabled) { |
| 1014 | ERR_THREAD_GUARD; |
| 1015 | #ifdef TOOLS_ENABLED |
| 1016 | data.gizmos_disabled = p_enabled; |
| 1017 | if (!p_enabled) { |
| 1018 | clear_gizmos(); |
| 1019 | } |
| 1020 | #endif |
| 1021 | } |
| 1022 | |
| 1023 | void Node3D::reparent(RequiredParam<Node> p_parent, bool p_keep_global_transform) { |
| 1024 | ERR_THREAD_GUARD; |
| 1025 | if (p_keep_global_transform) { |
| 1026 | Transform3D temp = get_global_transform(); |
| 1027 | Node::reparent(p_parent, p_keep_global_transform); |
| 1028 | set_global_transform(temp); |
| 1029 | } else { |
| 1030 | Node::reparent(p_parent, p_keep_global_transform); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | void Node3D::set_disable_scale(bool p_enabled) { |
| 1035 | ERR_THREAD_GUARD; |
| 1036 | data.disable_scale = p_enabled; |
| 1037 | } |
| 1038 | |
| 1039 | bool Node3D::is_scale_disabled() const { |
| 1040 | ERR_READ_THREAD_GUARD_V(false); |
| 1041 | return data.disable_scale; |
| 1042 | } |
| 1043 | |
| 1044 | void Node3D::set_as_top_level(bool p_enabled) { |
| 1045 | ERR_THREAD_GUARD; |
| 1046 | if (data.top_level == p_enabled) { |
| 1047 | return; |
| 1048 | } |
| 1049 | if (is_inside_tree()) { |
| 1050 | if (p_enabled) { |
| 1051 | set_transform(get_global_transform()); |
| 1052 | } else if (data.parent) { |
| 1053 | set_transform(data.parent->get_global_transform().affine_inverse() * get_global_transform()); |
| 1054 | } |
| 1055 | } |
| 1056 | data.top_level = p_enabled; |
| 1057 | reset_physics_interpolation(); |
| 1058 | } |
| 1059 | |
| 1060 | void Node3D::set_as_top_level_keep_local(bool p_enabled) { |
| 1061 | ERR_THREAD_GUARD; |
| 1062 | if (data.top_level == p_enabled) { |
| 1063 | return; |
| 1064 | } |
| 1065 | data.top_level = p_enabled; |
| 1066 | _propagate_transform_changed(this); |
| 1067 | reset_physics_interpolation(); |
| 1068 | } |
| 1069 | |
| 1070 | bool Node3D::is_set_as_top_level() const { |
| 1071 | ERR_READ_THREAD_GUARD_V(false); |
| 1072 | return data.top_level; |
| 1073 | } |
| 1074 | |
| 1075 | Ref<World3D> Node3D::get_world_3d() const { |
| 1076 | ERR_READ_THREAD_GUARD_V(Ref<World3D>()); // World3D can only be set from main thread, so it's safe to obtain on threads. |
| 1077 | ERR_FAIL_COND_V(!is_inside_world(), Ref<World3D>()); |
| 1078 | ERR_FAIL_NULL_V(data.viewport, Ref<World3D>()); |
| 1079 | |
| 1080 | return data.viewport->find_world_3d(); |
| 1081 | } |
| 1082 | |
| 1083 | void Node3D::_propagate_visibility_changed() { |
| 1084 | notification(NOTIFICATION_VISIBILITY_CHANGED); |
| 1085 | emit_signal(SceneStringName(visibility_changed)); |
| 1086 | |
| 1087 | #ifdef TOOLS_ENABLED |
| 1088 | if (!data.gizmos.is_empty()) { |
| 1089 | data.gizmos_dirty = true; |
| 1090 | _update_gizmos(); |
| 1091 | } |
| 1092 | #endif |
| 1093 | |
| 1094 | for (uint32_t n = 0; n < data.node3d_children.size(); n++) { |
| 1095 | Node3D *s = data.node3d_children[n]; |
| 1096 | |
| 1097 | if (s->data.visible) { |
| 1098 | s->_propagate_visibility_changed(); |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | void Node3D::show() { |
| 1104 | ERR_MAIN_THREAD_GUARD; |
| 1105 | set_visible(true); |
| 1106 | } |
| 1107 | |
| 1108 | void Node3D::hide() { |
| 1109 | ERR_MAIN_THREAD_GUARD; |
| 1110 | set_visible(false); |
| 1111 | } |
| 1112 | |
| 1113 | void Node3D::set_visible(bool p_visible) { |
| 1114 | ERR_MAIN_THREAD_GUARD; |
| 1115 | if (data.visible == p_visible) { |
| 1116 | return; |
| 1117 | } |
| 1118 | |
| 1119 | data.visible = p_visible; |
| 1120 | |
| 1121 | if (!is_inside_tree()) { |
| 1122 | return; |
| 1123 | } |
| 1124 | _propagate_visibility_changed(); |
| 1125 | } |
| 1126 | |
| 1127 | bool Node3D::is_visible() const { |
| 1128 | ERR_READ_THREAD_GUARD_V(false); |
| 1129 | return data.visible; |
| 1130 | } |
| 1131 | |
| 1132 | bool Node3D::is_visible_in_tree() const { |
| 1133 | ERR_READ_THREAD_GUARD_V(false); // Since visibility can only be changed from main thread, this is safe to call. |
| 1134 | const Node3D *s = this; |
| 1135 | |
| 1136 | while (s) { |
| 1137 | if (!s->data.visible) { |
| 1138 | return false; |
| 1139 | } |
| 1140 | s = s->data.parent; |
| 1141 | } |
| 1142 | |
| 1143 | return true; |
| 1144 | } |
| 1145 | |
| 1146 | void Node3D::rotate_object_local(const Vector3 &p_axis, real_t p_angle) { |
| 1147 | ERR_THREAD_GUARD; |
| 1148 | Transform3D t = get_transform(); |
| 1149 | t.basis.rotate_local(p_axis, p_angle); |
| 1150 | set_transform(t); |
| 1151 | } |
| 1152 | |
| 1153 | void Node3D::rotate(const Vector3 &p_axis, real_t p_angle) { |
| 1154 | ERR_THREAD_GUARD; |
| 1155 | Transform3D t = get_transform(); |
| 1156 | t.basis.rotate(p_axis, p_angle); |
| 1157 | set_transform(t); |
| 1158 | } |
| 1159 | |
| 1160 | void Node3D::rotate_x(real_t p_angle) { |
| 1161 | ERR_THREAD_GUARD; |
| 1162 | Transform3D t = get_transform(); |
| 1163 | t.basis.rotate(Vector3(1, 0, 0), p_angle); |
| 1164 | set_transform(t); |
| 1165 | } |
| 1166 | |
| 1167 | void Node3D::rotate_y(real_t p_angle) { |
| 1168 | ERR_THREAD_GUARD; |
| 1169 | Transform3D t = get_transform(); |
| 1170 | t.basis.rotate(Vector3(0, 1, 0), p_angle); |
| 1171 | set_transform(t); |
| 1172 | } |
| 1173 | |
| 1174 | void Node3D::rotate_z(real_t p_angle) { |
| 1175 | ERR_THREAD_GUARD; |
| 1176 | Transform3D t = get_transform(); |
| 1177 | t.basis.rotate(Vector3(0, 0, 1), p_angle); |
| 1178 | set_transform(t); |
| 1179 | } |
| 1180 | |
| 1181 | void Node3D::translate(const Vector3 &p_offset) { |
| 1182 | ERR_THREAD_GUARD; |
| 1183 | Transform3D t = get_transform(); |
| 1184 | t.translate_local(p_offset); |
| 1185 | set_transform(t); |
| 1186 | } |
| 1187 | |
| 1188 | void Node3D::translate_object_local(const Vector3 &p_offset) { |
| 1189 | ERR_THREAD_GUARD; |
| 1190 | Transform3D t = get_transform(); |
| 1191 | |
| 1192 | Transform3D s; |
| 1193 | s.translate_local(p_offset); |
| 1194 | set_transform(t * s); |
| 1195 | } |
| 1196 | |
| 1197 | void Node3D::scale(const Vector3 &p_ratio) { |
| 1198 | ERR_THREAD_GUARD; |
| 1199 | Transform3D t = get_transform(); |
| 1200 | t.basis.scale(p_ratio); |
| 1201 | set_transform(t); |
| 1202 | } |
| 1203 | |
| 1204 | void Node3D::scale_object_local(const Vector3 &p_scale) { |
| 1205 | ERR_THREAD_GUARD; |
| 1206 | Transform3D t = get_transform(); |
| 1207 | t.basis.scale_local(p_scale); |
| 1208 | set_transform(t); |
| 1209 | } |
| 1210 | |
| 1211 | void Node3D::global_rotate(const Vector3 &p_axis, real_t p_angle) { |
| 1212 | ERR_THREAD_GUARD; |
| 1213 | Transform3D t = get_global_transform(); |
| 1214 | t.basis.rotate(p_axis, p_angle); |
| 1215 | set_global_transform(t); |
| 1216 | } |
| 1217 | |
| 1218 | void Node3D::global_scale(const Vector3 &p_scale) { |
| 1219 | ERR_THREAD_GUARD; |
| 1220 | Transform3D t = get_global_transform(); |
| 1221 | t.basis.scale(p_scale); |
| 1222 | set_global_transform(t); |
| 1223 | } |
| 1224 | |
| 1225 | void Node3D::global_translate(const Vector3 &p_offset) { |
| 1226 | ERR_THREAD_GUARD; |
| 1227 | Transform3D t = get_global_transform(); |
| 1228 | t.origin += p_offset; |
| 1229 | set_global_transform(t); |
| 1230 | } |
| 1231 | |
| 1232 | void Node3D::orthonormalize() { |
| 1233 | ERR_THREAD_GUARD; |
| 1234 | Transform3D t = get_transform(); |
| 1235 | t.orthonormalize(); |
| 1236 | set_transform(t); |
| 1237 | } |
| 1238 | |
| 1239 | void Node3D::set_identity() { |
| 1240 | ERR_THREAD_GUARD; |
| 1241 | set_transform(Transform3D()); |
| 1242 | } |
| 1243 | |
| 1244 | void Node3D::look_at(const Vector3 &p_target, const Vector3 &p_up, bool p_use_model_front) { |
| 1245 | ERR_THREAD_GUARD; |
| 1246 | ERR_FAIL_COND_MSG(!is_inside_tree(), "Node not inside tree. Use look_at_from_position() instead."); |
| 1247 | Vector3 origin = get_global_transform().origin; |
| 1248 | look_at_from_position(origin, p_target, p_up, p_use_model_front); |
| 1249 | } |
| 1250 | |
| 1251 | void Node3D::look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up, bool p_use_model_front) { |
| 1252 | ERR_THREAD_GUARD; |
| 1253 | ERR_FAIL_COND_MSG(p_pos.is_equal_approx(p_target), "Node origin and target are in the same position, look_at() failed."); |
| 1254 | ERR_FAIL_COND_MSG(p_up.is_zero_approx(), "The up vector can't be zero, look_at() failed."); |
| 1255 | |
| 1256 | Vector3 forward = p_target - p_pos; |
| 1257 | Basis lookat_basis = Basis::looking_at(forward, p_up, p_use_model_front); |
| 1258 | Vector3 original_scale = get_scale(); |
| 1259 | set_global_transform(Transform3D(lookat_basis, p_pos)); |
| 1260 | set_scale(original_scale); |
| 1261 | } |
| 1262 | |
| 1263 | Vector3 Node3D::to_local(Vector3 p_global) const { |
| 1264 | ERR_READ_THREAD_GUARD_V(Vector3()); |
| 1265 | return get_global_transform().affine_inverse().xform(p_global); |
| 1266 | } |
| 1267 | |
| 1268 | Vector3 Node3D::to_global(Vector3 p_local) const { |
| 1269 | ERR_READ_THREAD_GUARD_V(Vector3()); |
| 1270 | return get_global_transform().xform(p_local); |
| 1271 | } |
| 1272 | |
| 1273 | void Node3D::set_notify_transform(bool p_enabled) { |
| 1274 | ERR_THREAD_GUARD; |
| 1275 | data.notify_transform = p_enabled; |
| 1276 | } |
| 1277 | |
| 1278 | bool Node3D::is_transform_notification_enabled() const { |
| 1279 | ERR_READ_THREAD_GUARD_V(false); |
| 1280 | return data.notify_transform; |
| 1281 | } |
| 1282 | |
| 1283 | void Node3D::set_notify_local_transform(bool p_enabled) { |
| 1284 | ERR_THREAD_GUARD; |
| 1285 | data.notify_local_transform = p_enabled; |
| 1286 | } |
| 1287 | |
| 1288 | bool Node3D::is_local_transform_notification_enabled() const { |
| 1289 | ERR_READ_THREAD_GUARD_V(false); |
| 1290 | return data.notify_local_transform; |
| 1291 | } |
| 1292 | |
| 1293 | void Node3D::force_update_transform() { |
| 1294 | ERR_THREAD_GUARD; |
| 1295 | ERR_FAIL_COND(!is_inside_tree()); |
| 1296 | if (!xform_change.in_list()) { |
| 1297 | return; //nothing to update |
| 1298 | } |
| 1299 | get_tree()->xform_change_list.remove(&xform_change); |
| 1300 | |
| 1301 | notification(NOTIFICATION_TRANSFORM_CHANGED); |
| 1302 | } |
| 1303 | |
| 1304 | void Node3D::_update_visibility_parent(bool p_update_root) { |
| 1305 | RID new_parent; |
| 1306 | |
| 1307 | if (!visibility_parent_path.is_empty()) { |
| 1308 | if (!p_update_root) { |
| 1309 | return; |
| 1310 | } |
| 1311 | Node *parent = get_node_or_null(visibility_parent_path); |
| 1312 | ERR_FAIL_NULL_MSG(parent, "Can't find visibility parent node at path: " + String(visibility_parent_path)); |
| 1313 | ERR_FAIL_COND_MSG(parent == this, "The visibility parent can't be the same node."); |
| 1314 | GeometryInstance3D *gi = Object::cast_to<GeometryInstance3D>(parent); |
| 1315 | ERR_FAIL_NULL_MSG(gi, "The visibility parent node must be a GeometryInstance3D, at path: " + String(visibility_parent_path)); |
| 1316 | new_parent = gi ? gi->get_instance() : RID(); |
| 1317 | } else if (data.parent) { |
| 1318 | new_parent = data.parent->data.visibility_parent; |
| 1319 | } |
| 1320 | |
| 1321 | if (new_parent == data.visibility_parent) { |
| 1322 | return; |
| 1323 | } |
| 1324 | |
| 1325 | data.visibility_parent = new_parent; |
| 1326 | |
| 1327 | VisualInstance3D *vi = Object::cast_to<VisualInstance3D>(this); |
| 1328 | if (vi) { |
| 1329 | RS::get_singleton()->instance_set_visibility_parent(vi->get_instance(), data.visibility_parent); |
| 1330 | } |
| 1331 | |
| 1332 | for (Node3D *c : data.node3d_children) { |
| 1333 | c->_update_visibility_parent(false); |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | void Node3D::set_visibility_parent(const NodePath &p_path) { |
| 1338 | ERR_MAIN_THREAD_GUARD; |
| 1339 | visibility_parent_path = p_path; |
| 1340 | if (is_inside_tree()) { |
| 1341 | _update_visibility_parent(true); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | NodePath Node3D::get_visibility_parent() const { |
| 1346 | ERR_READ_THREAD_GUARD_V(NodePath()); |
| 1347 | return visibility_parent_path; |
| 1348 | } |
| 1349 | |
| 1350 | void Node3D::_validate_property(PropertyInfo &p_property) const { |
| 1351 | if (data.rotation_edit_mode != ROTATION_EDIT_MODE_BASIS && p_property.name == "basis") { |
| 1352 | p_property.usage = 0; |
| 1353 | } |
| 1354 | if (data.rotation_edit_mode == ROTATION_EDIT_MODE_BASIS && p_property.name == "scale") { |
| 1355 | p_property.usage = 0; |
| 1356 | } |
| 1357 | if (data.rotation_edit_mode != ROTATION_EDIT_MODE_QUATERNION && p_property.name == "quaternion") { |
| 1358 | p_property.usage = 0; |
| 1359 | } |
| 1360 | if (data.rotation_edit_mode != ROTATION_EDIT_MODE_EULER && p_property.name == "rotation") { |
| 1361 | p_property.usage = 0; |
| 1362 | } |
| 1363 | if (data.rotation_edit_mode != ROTATION_EDIT_MODE_EULER && p_property.name == "rotation_order") { |
| 1364 | p_property.usage = 0; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | bool Node3D::_property_can_revert(const StringName &p_name) const { |
| 1369 | const String sname = p_name; |
| 1370 | if (sname == "basis") { |
| 1371 | return true; |
| 1372 | } else if (sname == "scale") { |
| 1373 | return true; |
| 1374 | } else if (sname == "quaternion") { |
| 1375 | return true; |
| 1376 | } else if (sname == "rotation") { |
| 1377 | return true; |
| 1378 | } else if (sname == "position") { |
| 1379 | return true; |
| 1380 | } |
| 1381 | return false; |
| 1382 | } |
| 1383 | |
| 1384 | bool Node3D::_property_get_revert(const StringName &p_name, Variant &r_property) const { |
| 1385 | bool valid = false; |
| 1386 | |
| 1387 | const String sname = p_name; |
| 1388 | if (sname == "basis") { |
| 1389 | Variant variant = PropertyUtils::get_property_default_value(this, "transform", &valid); |
| 1390 | if (valid && variant.get_type() == Variant::Type::TRANSFORM3D) { |
| 1391 | r_property = Transform3D(variant).get_basis(); |
| 1392 | } else { |
| 1393 | r_property = Basis(); |
| 1394 | } |
| 1395 | } else if (sname == "scale") { |
| 1396 | Variant variant = PropertyUtils::get_property_default_value(this, "transform", &valid); |
| 1397 | if (valid && variant.get_type() == Variant::Type::TRANSFORM3D) { |
| 1398 | r_property = Transform3D(variant).get_basis().get_scale(); |
| 1399 | } else { |
| 1400 | r_property = Vector3(1.0, 1.0, 1.0); |
| 1401 | } |
| 1402 | } else if (sname == "quaternion") { |
| 1403 | Variant variant = PropertyUtils::get_property_default_value(this, "transform", &valid); |
| 1404 | if (valid && variant.get_type() == Variant::Type::TRANSFORM3D) { |
| 1405 | r_property = Quaternion(Transform3D(variant).get_basis().get_rotation_quaternion()); |
| 1406 | } else { |
| 1407 | r_property = Quaternion(); |
| 1408 | } |
| 1409 | } else if (sname == "rotation") { |
| 1410 | Variant variant = PropertyUtils::get_property_default_value(this, "transform", &valid); |
| 1411 | if (valid && variant.get_type() == Variant::Type::TRANSFORM3D) { |
| 1412 | r_property = Transform3D(variant).get_basis().get_euler_normalized(data.euler_rotation_order); |
| 1413 | } else { |
| 1414 | r_property = Vector3(); |
| 1415 | } |
| 1416 | } else if (sname == "position") { |
| 1417 | Variant variant = PropertyUtils::get_property_default_value(this, "transform", &valid); |
| 1418 | if (valid) { |
| 1419 | r_property = Transform3D(variant).get_origin(); |
| 1420 | } else { |
| 1421 | r_property = Vector3(); |
| 1422 | } |
| 1423 | } else { |
| 1424 | return false; |
| 1425 | } |
| 1426 | return true; |
| 1427 | } |
| 1428 | |
| 1429 | void Node3D::_bind_methods() { |
| 1430 | ClassDB::bind_method(D_METHOD("set_transform", "local"), &Node3D::set_transform); |
| 1431 | ClassDB::bind_method(D_METHOD("get_transform"), &Node3D::get_transform); |
| 1432 | ClassDB::bind_method(D_METHOD("set_position", "position"), &Node3D::set_position); |
| 1433 | ClassDB::bind_method(D_METHOD("get_position"), &Node3D::get_position); |
| 1434 | ClassDB::bind_method(D_METHOD("set_rotation", "euler_radians"), &Node3D::set_rotation); |
| 1435 | ClassDB::bind_method(D_METHOD("get_rotation"), &Node3D::get_rotation); |
| 1436 | ClassDB::bind_method(D_METHOD("set_rotation_degrees", "euler_degrees"), &Node3D::set_rotation_degrees); |
| 1437 | ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Node3D::get_rotation_degrees); |
| 1438 | ClassDB::bind_method(D_METHOD("set_rotation_order", "order"), &Node3D::set_rotation_order); |
| 1439 | ClassDB::bind_method(D_METHOD("get_rotation_order"), &Node3D::get_rotation_order); |
| 1440 | ClassDB::bind_method(D_METHOD("set_rotation_edit_mode", "edit_mode"), &Node3D::set_rotation_edit_mode); |
| 1441 | ClassDB::bind_method(D_METHOD("get_rotation_edit_mode"), &Node3D::get_rotation_edit_mode); |
| 1442 | ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Node3D::set_scale); |
| 1443 | ClassDB::bind_method(D_METHOD("get_scale"), &Node3D::get_scale); |
| 1444 | ClassDB::bind_method(D_METHOD("set_quaternion", "quaternion"), &Node3D::set_quaternion); |
| 1445 | ClassDB::bind_method(D_METHOD("get_quaternion"), &Node3D::get_quaternion); |
| 1446 | ClassDB::bind_method(D_METHOD("set_basis", "basis"), &Node3D::set_basis); |
| 1447 | ClassDB::bind_method(D_METHOD("get_basis"), &Node3D::get_basis); |
| 1448 | |
| 1449 | ClassDB::bind_method(D_METHOD("set_global_transform", "global"), &Node3D::set_global_transform); |
| 1450 | ClassDB::bind_method(D_METHOD("get_global_transform"), &Node3D::get_global_transform); |
| 1451 | ClassDB::bind_method(D_METHOD("get_global_transform_interpolated"), &Node3D::get_global_transform_interpolated); |
| 1452 | ClassDB::bind_method(D_METHOD("set_global_position", "position"), &Node3D::set_global_position); |
| 1453 | ClassDB::bind_method(D_METHOD("get_global_position"), &Node3D::get_global_position); |
| 1454 | ClassDB::bind_method(D_METHOD("set_global_basis", "basis"), &Node3D::set_global_basis); |
| 1455 | ClassDB::bind_method(D_METHOD("get_global_basis"), &Node3D::get_global_basis); |
| 1456 | ClassDB::bind_method(D_METHOD("set_global_rotation", "euler_radians"), &Node3D::set_global_rotation); |
| 1457 | ClassDB::bind_method(D_METHOD("get_global_rotation"), &Node3D::get_global_rotation); |
| 1458 | ClassDB::bind_method(D_METHOD("set_global_rotation_degrees", "euler_degrees"), &Node3D::set_global_rotation_degrees); |
| 1459 | ClassDB::bind_method(D_METHOD("get_global_rotation_degrees"), &Node3D::get_global_rotation_degrees); |
| 1460 | |
| 1461 | ClassDB::bind_method(D_METHOD("get_parent_node_3d"), &Node3D::get_parent_node_3d); |
| 1462 | ClassDB::bind_method(D_METHOD("set_ignore_transform_notification", "enabled"), &Node3D::set_ignore_transform_notification); |
| 1463 | ClassDB::bind_method(D_METHOD("set_as_top_level", "enable"), &Node3D::set_as_top_level); |
| 1464 | ClassDB::bind_method(D_METHOD("is_set_as_top_level"), &Node3D::is_set_as_top_level); |
| 1465 | ClassDB::bind_method(D_METHOD("set_disable_scale", "disable"), &Node3D::set_disable_scale); |
| 1466 | ClassDB::bind_method(D_METHOD("is_scale_disabled"), &Node3D::is_scale_disabled); |
| 1467 | ClassDB::bind_method(D_METHOD("get_world_3d"), &Node3D::get_world_3d); |
| 1468 | |
| 1469 | ClassDB::bind_method(D_METHOD("force_update_transform"), &Node3D::force_update_transform); |
| 1470 | |
| 1471 | ClassDB::bind_method(D_METHOD("set_visibility_parent", "path"), &Node3D::set_visibility_parent); |
| 1472 | ClassDB::bind_method(D_METHOD("get_visibility_parent"), &Node3D::get_visibility_parent); |
| 1473 | |
| 1474 | ClassDB::bind_method(D_METHOD("update_gizmos"), &Node3D::update_gizmos); |
| 1475 | ClassDB::bind_method(D_METHOD("add_gizmo", "gizmo"), &Node3D::add_gizmo); |
| 1476 | ClassDB::bind_method(D_METHOD("get_gizmos"), &Node3D::get_gizmos_bind); |
| 1477 | ClassDB::bind_method(D_METHOD("clear_gizmos"), &Node3D::clear_gizmos); |
| 1478 | ClassDB::bind_method(D_METHOD("set_subgizmo_selection", "gizmo", "id", "transform"), &Node3D::set_subgizmo_selection); |
| 1479 | ClassDB::bind_method(D_METHOD("clear_subgizmo_selection"), &Node3D::clear_subgizmo_selection); |
| 1480 | |
| 1481 | ClassDB::bind_method(D_METHOD("set_visible", "visible"), &Node3D::set_visible); |
| 1482 | ClassDB::bind_method(D_METHOD("is_visible"), &Node3D::is_visible); |
| 1483 | ClassDB::bind_method(D_METHOD("is_visible_in_tree"), &Node3D::is_visible_in_tree); |
| 1484 | ClassDB::bind_method(D_METHOD("show"), &Node3D::show); |
| 1485 | ClassDB::bind_method(D_METHOD("hide"), &Node3D::hide); |
| 1486 | |
| 1487 | ClassDB::bind_method(D_METHOD("set_notify_local_transform", "enable"), &Node3D::set_notify_local_transform); |
| 1488 | ClassDB::bind_method(D_METHOD("is_local_transform_notification_enabled"), &Node3D::is_local_transform_notification_enabled); |
| 1489 | |
| 1490 | ClassDB::bind_method(D_METHOD("set_notify_transform", "enable"), &Node3D::set_notify_transform); |
| 1491 | ClassDB::bind_method(D_METHOD("is_transform_notification_enabled"), &Node3D::is_transform_notification_enabled); |
| 1492 | |
| 1493 | ClassDB::bind_method(D_METHOD("rotate", "axis", "angle"), &Node3D::rotate); |
| 1494 | ClassDB::bind_method(D_METHOD("global_rotate", "axis", "angle"), &Node3D::global_rotate); |
| 1495 | ClassDB::bind_method(D_METHOD("global_scale", "scale"), &Node3D::global_scale); |
| 1496 | ClassDB::bind_method(D_METHOD("global_translate", "offset"), &Node3D::global_translate); |
| 1497 | ClassDB::bind_method(D_METHOD("rotate_object_local", "axis", "angle"), &Node3D::rotate_object_local); |
| 1498 | ClassDB::bind_method(D_METHOD("scale_object_local", "scale"), &Node3D::scale_object_local); |
| 1499 | ClassDB::bind_method(D_METHOD("translate_object_local", "offset"), &Node3D::translate_object_local); |
| 1500 | ClassDB::bind_method(D_METHOD("rotate_x", "angle"), &Node3D::rotate_x); |
| 1501 | ClassDB::bind_method(D_METHOD("rotate_y", "angle"), &Node3D::rotate_y); |
| 1502 | ClassDB::bind_method(D_METHOD("rotate_z", "angle"), &Node3D::rotate_z); |
| 1503 | ClassDB::bind_method(D_METHOD("translate", "offset"), &Node3D::translate); |
| 1504 | ClassDB::bind_method(D_METHOD("orthonormalize"), &Node3D::orthonormalize); |
| 1505 | ClassDB::bind_method(D_METHOD("set_identity"), &Node3D::set_identity); |
| 1506 | |
| 1507 | ClassDB::bind_method(D_METHOD("look_at", "target", "up", "use_model_front"), &Node3D::look_at, DEFVAL(Vector3::UP), DEFVAL(false)); |
| 1508 | ClassDB::bind_method(D_METHOD("look_at_from_position", "position", "target", "up", "use_model_front"), &Node3D::look_at_from_position, DEFVAL(Vector3::UP), DEFVAL(false)); |
| 1509 | |
| 1510 | ClassDB::bind_method(D_METHOD("to_local", "global_point"), &Node3D::to_local); |
| 1511 | ClassDB::bind_method(D_METHOD("to_global", "local_point"), &Node3D::to_global); |
| 1512 | |
| 1513 | BIND_CONSTANT(NOTIFICATION_TRANSFORM_CHANGED); |
| 1514 | BIND_CONSTANT(NOTIFICATION_ENTER_WORLD); |
| 1515 | BIND_CONSTANT(NOTIFICATION_EXIT_WORLD); |
| 1516 | BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED); |
| 1517 | BIND_CONSTANT(NOTIFICATION_LOCAL_TRANSFORM_CHANGED); |
| 1518 | |
| 1519 | BIND_ENUM_CONSTANT(ROTATION_EDIT_MODE_EULER); |
| 1520 | BIND_ENUM_CONSTANT(ROTATION_EDIT_MODE_QUATERNION); |
| 1521 | BIND_ENUM_CONSTANT(ROTATION_EDIT_MODE_BASIS); |
| 1522 | |
| 1523 | ADD_GROUP("Transform", ""); |
| 1524 | ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "transform", PROPERTY_HINT_NONE, "suffix:m", PROPERTY_USAGE_NO_EDITOR), "set_transform", "get_transform"); |
| 1525 | ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "global_transform", PROPERTY_HINT_NONE, "suffix:m", PROPERTY_USAGE_NONE), "set_global_transform", "get_global_transform"); |
| 1526 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "position", PROPERTY_HINT_RANGE, "-99999,99999,or_greater,or_less,hide_control,suffix:m", PROPERTY_USAGE_EDITOR), "set_position", "get_position"); |
| 1527 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "rotation", PROPERTY_HINT_RANGE, "-360,360,0.1,or_less,or_greater,radians_as_degrees", PROPERTY_USAGE_EDITOR), "set_rotation", "get_rotation"); |
| 1528 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_rotation_degrees", "get_rotation_degrees"); |
| 1529 | ADD_PROPERTY(PropertyInfo(Variant::QUATERNION, "quaternion", PROPERTY_HINT_HIDE_QUATERNION_EDIT, "", PROPERTY_USAGE_EDITOR), "set_quaternion", "get_quaternion"); |
| 1530 | ADD_PROPERTY(PropertyInfo(Variant::BASIS, "basis", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_basis", "get_basis"); |
| 1531 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "scale", PROPERTY_HINT_LINK, "", PROPERTY_USAGE_EDITOR), "set_scale", "get_scale"); |
| 1532 | ADD_PROPERTY(PropertyInfo(Variant::INT, "rotation_edit_mode", PROPERTY_HINT_ENUM, "Euler,Quaternion,Basis"), "set_rotation_edit_mode", "get_rotation_edit_mode"); |
| 1533 | ADD_PROPERTY(PropertyInfo(Variant::INT, "rotation_order", PROPERTY_HINT_ENUM, "XYZ,XZY,YXZ,YZX,ZXY,ZYX"), "set_rotation_order", "get_rotation_order"); |
| 1534 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "top_level"), "set_as_top_level", "is_set_as_top_level"); |
| 1535 | |
| 1536 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "global_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_global_position", "get_global_position"); |
| 1537 | ADD_PROPERTY(PropertyInfo(Variant::BASIS, "global_basis", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_global_basis", "get_global_basis"); |
| 1538 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "global_rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_global_rotation", "get_global_rotation"); |
| 1539 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "global_rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_global_rotation_degrees", "get_global_rotation_degrees"); |
| 1540 | ADD_GROUP("Visibility", ""); |
| 1541 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible"); |
| 1542 | ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "visibility_parent", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "GeometryInstance3D"), "set_visibility_parent", "get_visibility_parent"); |
| 1543 | |
| 1544 | ADD_SIGNAL(MethodInfo("visibility_changed")); |
| 1545 | } |
| 1546 | |
| 1547 | Node3D::Node3D() : |
| 1548 | xform_change(this), _client_physics_interpolation_node_3d_list(this) { |
| 1549 | _define_ancestry(AncestralClass::NODE_3D); |
| 1550 | |
| 1551 | // Default member initializer for bitfield is a C++20 extension, so: |
| 1552 | |
| 1553 | data.top_level = false; |
| 1554 | data.inside_world = false; |
| 1555 | |
| 1556 | data.ignore_notification = false; |
| 1557 | data.notify_local_transform = false; |
| 1558 | data.notify_transform = false; |
| 1559 | |
| 1560 | data.visible = true; |
| 1561 | data.disable_scale = false; |
| 1562 | data.vi_visible = true; |
| 1563 | |
| 1564 | data.fti_on_frame_xform_list = false; |
| 1565 | data.fti_on_frame_property_list = false; |
| 1566 | data.fti_on_tick_xform_list = false; |
| 1567 | data.fti_on_tick_property_list = false; |
| 1568 | data.fti_global_xform_interp_set = false; |
| 1569 | data.fti_frame_xform_force_update = false; |
| 1570 | data.fti_is_identity_xform = false; |
| 1571 | data.fti_processed = false; |
| 1572 | |
| 1573 | #ifdef TOOLS_ENABLED |
| 1574 | data.gizmos_requested = false; |
| 1575 | data.gizmos_disabled = false; |
| 1576 | data.gizmos_dirty = false; |
| 1577 | data.transform_gizmo_visible = true; |
| 1578 | #endif |
| 1579 | } |
| 1580 | |
| 1581 | Node3D::~Node3D() { |
| 1582 | _disable_client_physics_interpolation(); |
| 1583 | |
| 1584 | if (is_inside_tree()) { |
| 1585 | get_tree()->get_scene_tree_fti().node_3d_notify_delete(this); |
| 1586 | } |
| 1587 | } |