node_3d.cpp 46 KB

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