node_3d.cpp 53 KB

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