node_3d.cpp 53 KB

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