node_3d.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  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. #endif
  762. }
  763. TypedArray<Node3DGizmo> Node3D::get_gizmos_bind() const {
  764. ERR_THREAD_GUARD_V(TypedArray<Node3DGizmo>());
  765. TypedArray<Node3DGizmo> ret;
  766. #ifdef TOOLS_ENABLED
  767. for (int i = 0; i < data.gizmos.size(); i++) {
  768. ret.push_back(Variant(data.gizmos[i].ptr()));
  769. }
  770. #endif
  771. return ret;
  772. }
  773. Vector<Ref<Node3DGizmo>> Node3D::get_gizmos() const {
  774. ERR_THREAD_GUARD_V(Vector<Ref<Node3DGizmo>>());
  775. #ifdef TOOLS_ENABLED
  776. return data.gizmos;
  777. #else
  778. return Vector<Ref<Node3DGizmo>>();
  779. #endif
  780. }
  781. void Node3D::_replace_dirty_mask(uint32_t p_mask) const {
  782. if (is_group_processing()) {
  783. data.dirty.mt.set(p_mask);
  784. } else {
  785. data.dirty.st = p_mask;
  786. }
  787. }
  788. void Node3D::_set_dirty_bits(uint32_t p_bits) const {
  789. if (is_group_processing()) {
  790. data.dirty.mt.bit_or(p_bits);
  791. } else {
  792. data.dirty.st |= p_bits;
  793. }
  794. }
  795. void Node3D::_clear_dirty_bits(uint32_t p_bits) const {
  796. if (is_group_processing()) {
  797. data.dirty.mt.bit_and(~p_bits);
  798. } else {
  799. data.dirty.st &= ~p_bits;
  800. }
  801. }
  802. void Node3D::_update_gizmos() {
  803. #ifdef TOOLS_ENABLED
  804. if (data.gizmos_disabled || !is_inside_world() || !data.gizmos_dirty) {
  805. data.gizmos_dirty = false;
  806. return;
  807. }
  808. data.gizmos_dirty = false;
  809. for (int i = 0; i < data.gizmos.size(); i++) {
  810. if (is_visible_in_tree()) {
  811. data.gizmos.write[i]->redraw();
  812. } else {
  813. data.gizmos.write[i]->clear();
  814. }
  815. }
  816. #endif
  817. }
  818. void Node3D::set_disable_gizmos(bool p_enabled) {
  819. ERR_THREAD_GUARD;
  820. #ifdef TOOLS_ENABLED
  821. data.gizmos_disabled = p_enabled;
  822. if (!p_enabled) {
  823. clear_gizmos();
  824. }
  825. #endif
  826. }
  827. void Node3D::reparent(Node *p_parent, bool p_keep_global_transform) {
  828. ERR_THREAD_GUARD;
  829. if (p_keep_global_transform) {
  830. Transform3D temp = get_global_transform();
  831. Node::reparent(p_parent, p_keep_global_transform);
  832. set_global_transform(temp);
  833. } else {
  834. Node::reparent(p_parent, p_keep_global_transform);
  835. }
  836. }
  837. void Node3D::set_disable_scale(bool p_enabled) {
  838. ERR_THREAD_GUARD;
  839. data.disable_scale = p_enabled;
  840. }
  841. bool Node3D::is_scale_disabled() const {
  842. ERR_READ_THREAD_GUARD_V(false);
  843. return data.disable_scale;
  844. }
  845. void Node3D::set_as_top_level(bool p_enabled) {
  846. ERR_THREAD_GUARD;
  847. if (data.top_level == p_enabled) {
  848. return;
  849. }
  850. if (is_inside_tree()) {
  851. if (p_enabled) {
  852. set_transform(get_global_transform());
  853. } else if (data.parent) {
  854. set_transform(data.parent->get_global_transform().affine_inverse() * get_global_transform());
  855. }
  856. }
  857. data.top_level = p_enabled;
  858. reset_physics_interpolation();
  859. }
  860. void Node3D::set_as_top_level_keep_local(bool p_enabled) {
  861. ERR_THREAD_GUARD;
  862. if (data.top_level == p_enabled) {
  863. return;
  864. }
  865. data.top_level = p_enabled;
  866. _propagate_transform_changed(this);
  867. reset_physics_interpolation();
  868. }
  869. bool Node3D::is_set_as_top_level() const {
  870. ERR_READ_THREAD_GUARD_V(false);
  871. return data.top_level;
  872. }
  873. Ref<World3D> Node3D::get_world_3d() const {
  874. ERR_READ_THREAD_GUARD_V(Ref<World3D>()); // World3D can only be set from main thread, so it's safe to obtain on threads.
  875. ERR_FAIL_COND_V(!is_inside_world(), Ref<World3D>());
  876. ERR_FAIL_NULL_V(data.viewport, Ref<World3D>());
  877. return data.viewport->find_world_3d();
  878. }
  879. void Node3D::_propagate_visibility_changed() {
  880. notification(NOTIFICATION_VISIBILITY_CHANGED);
  881. emit_signal(SceneStringName(visibility_changed));
  882. #ifdef TOOLS_ENABLED
  883. if (!data.gizmos.is_empty()) {
  884. data.gizmos_dirty = true;
  885. _update_gizmos();
  886. }
  887. #endif
  888. for (Node3D *c : data.children) {
  889. if (!c || !c->data.visible) {
  890. continue;
  891. }
  892. c->_propagate_visibility_changed();
  893. }
  894. }
  895. void Node3D::show() {
  896. ERR_MAIN_THREAD_GUARD;
  897. set_visible(true);
  898. }
  899. void Node3D::hide() {
  900. ERR_MAIN_THREAD_GUARD;
  901. set_visible(false);
  902. }
  903. void Node3D::set_visible(bool p_visible) {
  904. ERR_MAIN_THREAD_GUARD;
  905. if (data.visible == p_visible) {
  906. return;
  907. }
  908. data.visible = p_visible;
  909. if (!is_inside_tree()) {
  910. return;
  911. }
  912. _propagate_visibility_changed();
  913. }
  914. bool Node3D::is_visible() const {
  915. ERR_READ_THREAD_GUARD_V(false);
  916. return data.visible;
  917. }
  918. bool Node3D::is_visible_in_tree() const {
  919. ERR_READ_THREAD_GUARD_V(false); // Since visibility can only be changed from main thread, this is safe to call.
  920. const Node3D *s = this;
  921. while (s) {
  922. if (!s->data.visible) {
  923. return false;
  924. }
  925. s = s->data.parent;
  926. }
  927. return true;
  928. }
  929. void Node3D::rotate_object_local(const Vector3 &p_axis, real_t p_angle) {
  930. ERR_THREAD_GUARD;
  931. Transform3D t = get_transform();
  932. t.basis.rotate_local(p_axis, p_angle);
  933. set_transform(t);
  934. }
  935. void Node3D::rotate(const Vector3 &p_axis, real_t p_angle) {
  936. ERR_THREAD_GUARD;
  937. Transform3D t = get_transform();
  938. t.basis.rotate(p_axis, p_angle);
  939. set_transform(t);
  940. }
  941. void Node3D::rotate_x(real_t p_angle) {
  942. ERR_THREAD_GUARD;
  943. Transform3D t = get_transform();
  944. t.basis.rotate(Vector3(1, 0, 0), p_angle);
  945. set_transform(t);
  946. }
  947. void Node3D::rotate_y(real_t p_angle) {
  948. ERR_THREAD_GUARD;
  949. Transform3D t = get_transform();
  950. t.basis.rotate(Vector3(0, 1, 0), p_angle);
  951. set_transform(t);
  952. }
  953. void Node3D::rotate_z(real_t p_angle) {
  954. ERR_THREAD_GUARD;
  955. Transform3D t = get_transform();
  956. t.basis.rotate(Vector3(0, 0, 1), p_angle);
  957. set_transform(t);
  958. }
  959. void Node3D::translate(const Vector3 &p_offset) {
  960. ERR_THREAD_GUARD;
  961. Transform3D t = get_transform();
  962. t.translate_local(p_offset);
  963. set_transform(t);
  964. }
  965. void Node3D::translate_object_local(const Vector3 &p_offset) {
  966. ERR_THREAD_GUARD;
  967. Transform3D t = get_transform();
  968. Transform3D s;
  969. s.translate_local(p_offset);
  970. set_transform(t * s);
  971. }
  972. void Node3D::scale(const Vector3 &p_ratio) {
  973. ERR_THREAD_GUARD;
  974. Transform3D t = get_transform();
  975. t.basis.scale(p_ratio);
  976. set_transform(t);
  977. }
  978. void Node3D::scale_object_local(const Vector3 &p_scale) {
  979. ERR_THREAD_GUARD;
  980. Transform3D t = get_transform();
  981. t.basis.scale_local(p_scale);
  982. set_transform(t);
  983. }
  984. void Node3D::global_rotate(const Vector3 &p_axis, real_t p_angle) {
  985. ERR_THREAD_GUARD;
  986. Transform3D t = get_global_transform();
  987. t.basis.rotate(p_axis, p_angle);
  988. set_global_transform(t);
  989. }
  990. void Node3D::global_scale(const Vector3 &p_scale) {
  991. ERR_THREAD_GUARD;
  992. Transform3D t = get_global_transform();
  993. t.basis.scale(p_scale);
  994. set_global_transform(t);
  995. }
  996. void Node3D::global_translate(const Vector3 &p_offset) {
  997. ERR_THREAD_GUARD;
  998. Transform3D t = get_global_transform();
  999. t.origin += p_offset;
  1000. set_global_transform(t);
  1001. }
  1002. void Node3D::orthonormalize() {
  1003. ERR_THREAD_GUARD;
  1004. Transform3D t = get_transform();
  1005. t.orthonormalize();
  1006. set_transform(t);
  1007. }
  1008. void Node3D::set_identity() {
  1009. ERR_THREAD_GUARD;
  1010. set_transform(Transform3D());
  1011. }
  1012. void Node3D::look_at(const Vector3 &p_target, const Vector3 &p_up, bool p_use_model_front) {
  1013. ERR_THREAD_GUARD;
  1014. ERR_FAIL_COND_MSG(!is_inside_tree(), "Node not inside tree. Use look_at_from_position() instead.");
  1015. Vector3 origin = get_global_transform().origin;
  1016. look_at_from_position(origin, p_target, p_up, p_use_model_front);
  1017. }
  1018. void Node3D::look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up, bool p_use_model_front) {
  1019. ERR_THREAD_GUARD;
  1020. ERR_FAIL_COND_MSG(p_pos.is_equal_approx(p_target), "Node origin and target are in the same position, look_at() failed.");
  1021. ERR_FAIL_COND_MSG(p_up.is_zero_approx(), "The up vector can't be zero, look_at() failed.");
  1022. Vector3 forward = p_target - p_pos;
  1023. Basis lookat_basis = Basis::looking_at(forward, p_up, p_use_model_front);
  1024. Vector3 original_scale = get_scale();
  1025. set_global_transform(Transform3D(lookat_basis, p_pos));
  1026. set_scale(original_scale);
  1027. }
  1028. Vector3 Node3D::to_local(Vector3 p_global) const {
  1029. ERR_READ_THREAD_GUARD_V(Vector3());
  1030. return get_global_transform().affine_inverse().xform(p_global);
  1031. }
  1032. Vector3 Node3D::to_global(Vector3 p_local) const {
  1033. ERR_READ_THREAD_GUARD_V(Vector3());
  1034. return get_global_transform().xform(p_local);
  1035. }
  1036. void Node3D::set_notify_transform(bool p_enabled) {
  1037. ERR_THREAD_GUARD;
  1038. data.notify_transform = p_enabled;
  1039. }
  1040. bool Node3D::is_transform_notification_enabled() const {
  1041. ERR_READ_THREAD_GUARD_V(false);
  1042. return data.notify_transform;
  1043. }
  1044. void Node3D::set_notify_local_transform(bool p_enabled) {
  1045. ERR_THREAD_GUARD;
  1046. data.notify_local_transform = p_enabled;
  1047. }
  1048. bool Node3D::is_local_transform_notification_enabled() const {
  1049. ERR_READ_THREAD_GUARD_V(false);
  1050. return data.notify_local_transform;
  1051. }
  1052. void Node3D::force_update_transform() {
  1053. ERR_THREAD_GUARD;
  1054. ERR_FAIL_COND(!is_inside_tree());
  1055. if (!xform_change.in_list()) {
  1056. return; //nothing to update
  1057. }
  1058. get_tree()->xform_change_list.remove(&xform_change);
  1059. notification(NOTIFICATION_TRANSFORM_CHANGED);
  1060. }
  1061. void Node3D::_update_visibility_parent(bool p_update_root) {
  1062. RID new_parent;
  1063. if (!visibility_parent_path.is_empty()) {
  1064. if (!p_update_root) {
  1065. return;
  1066. }
  1067. Node *parent = get_node_or_null(visibility_parent_path);
  1068. ERR_FAIL_NULL_MSG(parent, "Can't find visibility parent node at path: " + String(visibility_parent_path));
  1069. ERR_FAIL_COND_MSG(parent == this, "The visibility parent can't be the same node.");
  1070. GeometryInstance3D *gi = Object::cast_to<GeometryInstance3D>(parent);
  1071. ERR_FAIL_NULL_MSG(gi, "The visibility parent node must be a GeometryInstance3D, at path: " + String(visibility_parent_path));
  1072. new_parent = gi ? gi->get_instance() : RID();
  1073. } else if (data.parent) {
  1074. new_parent = data.parent->data.visibility_parent;
  1075. }
  1076. if (new_parent == data.visibility_parent) {
  1077. return;
  1078. }
  1079. data.visibility_parent = new_parent;
  1080. VisualInstance3D *vi = Object::cast_to<VisualInstance3D>(this);
  1081. if (vi) {
  1082. RS::get_singleton()->instance_set_visibility_parent(vi->get_instance(), data.visibility_parent);
  1083. }
  1084. for (Node3D *c : data.children) {
  1085. c->_update_visibility_parent(false);
  1086. }
  1087. }
  1088. void Node3D::set_visibility_parent(const NodePath &p_path) {
  1089. ERR_MAIN_THREAD_GUARD;
  1090. visibility_parent_path = p_path;
  1091. if (is_inside_tree()) {
  1092. _update_visibility_parent(true);
  1093. }
  1094. }
  1095. NodePath Node3D::get_visibility_parent() const {
  1096. ERR_READ_THREAD_GUARD_V(NodePath());
  1097. return visibility_parent_path;
  1098. }
  1099. void Node3D::_validate_property(PropertyInfo &p_property) const {
  1100. if (data.rotation_edit_mode != ROTATION_EDIT_MODE_BASIS && p_property.name == "basis") {
  1101. p_property.usage = 0;
  1102. }
  1103. if (data.rotation_edit_mode == ROTATION_EDIT_MODE_BASIS && p_property.name == "scale") {
  1104. p_property.usage = 0;
  1105. }
  1106. if (data.rotation_edit_mode != ROTATION_EDIT_MODE_QUATERNION && p_property.name == "quaternion") {
  1107. p_property.usage = 0;
  1108. }
  1109. if (data.rotation_edit_mode != ROTATION_EDIT_MODE_EULER && p_property.name == "rotation") {
  1110. p_property.usage = 0;
  1111. }
  1112. if (data.rotation_edit_mode != ROTATION_EDIT_MODE_EULER && p_property.name == "rotation_order") {
  1113. p_property.usage = 0;
  1114. }
  1115. }
  1116. bool Node3D::_property_can_revert(const StringName &p_name) const {
  1117. const String sname = p_name;
  1118. if (sname == "basis") {
  1119. return true;
  1120. } else if (sname == "scale") {
  1121. return true;
  1122. } else if (sname == "quaternion") {
  1123. return true;
  1124. } else if (sname == "rotation") {
  1125. return true;
  1126. } else if (sname == "position") {
  1127. return true;
  1128. }
  1129. return false;
  1130. }
  1131. bool Node3D::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  1132. bool valid = false;
  1133. const String sname = p_name;
  1134. if (sname == "basis") {
  1135. Variant variant = PropertyUtils::get_property_default_value(this, "transform", &valid);
  1136. if (valid && variant.get_type() == Variant::Type::TRANSFORM3D) {
  1137. r_property = Transform3D(variant).get_basis();
  1138. } else {
  1139. r_property = Basis();
  1140. }
  1141. } else if (sname == "scale") {
  1142. Variant variant = PropertyUtils::get_property_default_value(this, "transform", &valid);
  1143. if (valid && variant.get_type() == Variant::Type::TRANSFORM3D) {
  1144. r_property = Transform3D(variant).get_basis().get_scale();
  1145. } else {
  1146. r_property = Vector3(1.0, 1.0, 1.0);
  1147. }
  1148. } else if (sname == "quaternion") {
  1149. Variant variant = PropertyUtils::get_property_default_value(this, "transform", &valid);
  1150. if (valid && variant.get_type() == Variant::Type::TRANSFORM3D) {
  1151. r_property = Quaternion(Transform3D(variant).get_basis().get_rotation_quaternion());
  1152. } else {
  1153. r_property = Quaternion();
  1154. }
  1155. } else if (sname == "rotation") {
  1156. Variant variant = PropertyUtils::get_property_default_value(this, "transform", &valid);
  1157. if (valid && variant.get_type() == Variant::Type::TRANSFORM3D) {
  1158. r_property = Transform3D(variant).get_basis().get_euler_normalized(data.euler_rotation_order);
  1159. } else {
  1160. r_property = Vector3();
  1161. }
  1162. } else if (sname == "position") {
  1163. Variant variant = PropertyUtils::get_property_default_value(this, "transform", &valid);
  1164. if (valid) {
  1165. r_property = Transform3D(variant).get_origin();
  1166. } else {
  1167. r_property = Vector3();
  1168. }
  1169. } else {
  1170. return false;
  1171. }
  1172. return true;
  1173. }
  1174. void Node3D::_bind_methods() {
  1175. ClassDB::bind_method(D_METHOD("set_transform", "local"), &Node3D::set_transform);
  1176. ClassDB::bind_method(D_METHOD("get_transform"), &Node3D::get_transform);
  1177. ClassDB::bind_method(D_METHOD("set_position", "position"), &Node3D::set_position);
  1178. ClassDB::bind_method(D_METHOD("get_position"), &Node3D::get_position);
  1179. ClassDB::bind_method(D_METHOD("set_rotation", "euler_radians"), &Node3D::set_rotation);
  1180. ClassDB::bind_method(D_METHOD("get_rotation"), &Node3D::get_rotation);
  1181. ClassDB::bind_method(D_METHOD("set_rotation_degrees", "euler_degrees"), &Node3D::set_rotation_degrees);
  1182. ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Node3D::get_rotation_degrees);
  1183. ClassDB::bind_method(D_METHOD("set_rotation_order", "order"), &Node3D::set_rotation_order);
  1184. ClassDB::bind_method(D_METHOD("get_rotation_order"), &Node3D::get_rotation_order);
  1185. ClassDB::bind_method(D_METHOD("set_rotation_edit_mode", "edit_mode"), &Node3D::set_rotation_edit_mode);
  1186. ClassDB::bind_method(D_METHOD("get_rotation_edit_mode"), &Node3D::get_rotation_edit_mode);
  1187. ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Node3D::set_scale);
  1188. ClassDB::bind_method(D_METHOD("get_scale"), &Node3D::get_scale);
  1189. ClassDB::bind_method(D_METHOD("set_quaternion", "quaternion"), &Node3D::set_quaternion);
  1190. ClassDB::bind_method(D_METHOD("get_quaternion"), &Node3D::get_quaternion);
  1191. ClassDB::bind_method(D_METHOD("set_basis", "basis"), &Node3D::set_basis);
  1192. ClassDB::bind_method(D_METHOD("get_basis"), &Node3D::get_basis);
  1193. ClassDB::bind_method(D_METHOD("set_global_transform", "global"), &Node3D::set_global_transform);
  1194. ClassDB::bind_method(D_METHOD("get_global_transform"), &Node3D::get_global_transform);
  1195. ClassDB::bind_method(D_METHOD("get_global_transform_interpolated"), &Node3D::get_global_transform_interpolated);
  1196. ClassDB::bind_method(D_METHOD("set_global_position", "position"), &Node3D::set_global_position);
  1197. ClassDB::bind_method(D_METHOD("get_global_position"), &Node3D::get_global_position);
  1198. ClassDB::bind_method(D_METHOD("set_global_basis", "basis"), &Node3D::set_global_basis);
  1199. ClassDB::bind_method(D_METHOD("get_global_basis"), &Node3D::get_global_basis);
  1200. ClassDB::bind_method(D_METHOD("set_global_rotation", "euler_radians"), &Node3D::set_global_rotation);
  1201. ClassDB::bind_method(D_METHOD("get_global_rotation"), &Node3D::get_global_rotation);
  1202. ClassDB::bind_method(D_METHOD("set_global_rotation_degrees", "euler_degrees"), &Node3D::set_global_rotation_degrees);
  1203. ClassDB::bind_method(D_METHOD("get_global_rotation_degrees"), &Node3D::get_global_rotation_degrees);
  1204. ClassDB::bind_method(D_METHOD("get_parent_node_3d"), &Node3D::get_parent_node_3d);
  1205. ClassDB::bind_method(D_METHOD("set_ignore_transform_notification", "enabled"), &Node3D::set_ignore_transform_notification);
  1206. ClassDB::bind_method(D_METHOD("set_as_top_level", "enable"), &Node3D::set_as_top_level);
  1207. ClassDB::bind_method(D_METHOD("is_set_as_top_level"), &Node3D::is_set_as_top_level);
  1208. ClassDB::bind_method(D_METHOD("set_disable_scale", "disable"), &Node3D::set_disable_scale);
  1209. ClassDB::bind_method(D_METHOD("is_scale_disabled"), &Node3D::is_scale_disabled);
  1210. ClassDB::bind_method(D_METHOD("get_world_3d"), &Node3D::get_world_3d);
  1211. ClassDB::bind_method(D_METHOD("force_update_transform"), &Node3D::force_update_transform);
  1212. ClassDB::bind_method(D_METHOD("set_visibility_parent", "path"), &Node3D::set_visibility_parent);
  1213. ClassDB::bind_method(D_METHOD("get_visibility_parent"), &Node3D::get_visibility_parent);
  1214. ClassDB::bind_method(D_METHOD("update_gizmos"), &Node3D::update_gizmos);
  1215. ClassDB::bind_method(D_METHOD("add_gizmo", "gizmo"), &Node3D::add_gizmo);
  1216. ClassDB::bind_method(D_METHOD("get_gizmos"), &Node3D::get_gizmos_bind);
  1217. ClassDB::bind_method(D_METHOD("clear_gizmos"), &Node3D::clear_gizmos);
  1218. ClassDB::bind_method(D_METHOD("set_subgizmo_selection", "gizmo", "id", "transform"), &Node3D::set_subgizmo_selection);
  1219. ClassDB::bind_method(D_METHOD("clear_subgizmo_selection"), &Node3D::clear_subgizmo_selection);
  1220. ClassDB::bind_method(D_METHOD("set_visible", "visible"), &Node3D::set_visible);
  1221. ClassDB::bind_method(D_METHOD("is_visible"), &Node3D::is_visible);
  1222. ClassDB::bind_method(D_METHOD("is_visible_in_tree"), &Node3D::is_visible_in_tree);
  1223. ClassDB::bind_method(D_METHOD("show"), &Node3D::show);
  1224. ClassDB::bind_method(D_METHOD("hide"), &Node3D::hide);
  1225. ClassDB::bind_method(D_METHOD("set_notify_local_transform", "enable"), &Node3D::set_notify_local_transform);
  1226. ClassDB::bind_method(D_METHOD("is_local_transform_notification_enabled"), &Node3D::is_local_transform_notification_enabled);
  1227. ClassDB::bind_method(D_METHOD("set_notify_transform", "enable"), &Node3D::set_notify_transform);
  1228. ClassDB::bind_method(D_METHOD("is_transform_notification_enabled"), &Node3D::is_transform_notification_enabled);
  1229. ClassDB::bind_method(D_METHOD("rotate", "axis", "angle"), &Node3D::rotate);
  1230. ClassDB::bind_method(D_METHOD("global_rotate", "axis", "angle"), &Node3D::global_rotate);
  1231. ClassDB::bind_method(D_METHOD("global_scale", "scale"), &Node3D::global_scale);
  1232. ClassDB::bind_method(D_METHOD("global_translate", "offset"), &Node3D::global_translate);
  1233. ClassDB::bind_method(D_METHOD("rotate_object_local", "axis", "angle"), &Node3D::rotate_object_local);
  1234. ClassDB::bind_method(D_METHOD("scale_object_local", "scale"), &Node3D::scale_object_local);
  1235. ClassDB::bind_method(D_METHOD("translate_object_local", "offset"), &Node3D::translate_object_local);
  1236. ClassDB::bind_method(D_METHOD("rotate_x", "angle"), &Node3D::rotate_x);
  1237. ClassDB::bind_method(D_METHOD("rotate_y", "angle"), &Node3D::rotate_y);
  1238. ClassDB::bind_method(D_METHOD("rotate_z", "angle"), &Node3D::rotate_z);
  1239. ClassDB::bind_method(D_METHOD("translate", "offset"), &Node3D::translate);
  1240. ClassDB::bind_method(D_METHOD("orthonormalize"), &Node3D::orthonormalize);
  1241. ClassDB::bind_method(D_METHOD("set_identity"), &Node3D::set_identity);
  1242. ClassDB::bind_method(D_METHOD("look_at", "target", "up", "use_model_front"), &Node3D::look_at, DEFVAL(Vector3(0, 1, 0)), DEFVAL(false));
  1243. 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));
  1244. ClassDB::bind_method(D_METHOD("to_local", "global_point"), &Node3D::to_local);
  1245. ClassDB::bind_method(D_METHOD("to_global", "local_point"), &Node3D::to_global);
  1246. BIND_CONSTANT(NOTIFICATION_TRANSFORM_CHANGED);
  1247. BIND_CONSTANT(NOTIFICATION_ENTER_WORLD);
  1248. BIND_CONSTANT(NOTIFICATION_EXIT_WORLD);
  1249. BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
  1250. BIND_CONSTANT(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  1251. BIND_ENUM_CONSTANT(ROTATION_EDIT_MODE_EULER);
  1252. BIND_ENUM_CONSTANT(ROTATION_EDIT_MODE_QUATERNION);
  1253. BIND_ENUM_CONSTANT(ROTATION_EDIT_MODE_BASIS);
  1254. ADD_GROUP("Transform", "");
  1255. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "transform", PROPERTY_HINT_NONE, "suffix:m", PROPERTY_USAGE_NO_EDITOR), "set_transform", "get_transform");
  1256. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "global_transform", PROPERTY_HINT_NONE, "suffix:m", PROPERTY_USAGE_NONE), "set_global_transform", "get_global_transform");
  1257. 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");
  1258. 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");
  1259. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_rotation_degrees", "get_rotation_degrees");
  1260. ADD_PROPERTY(PropertyInfo(Variant::QUATERNION, "quaternion", PROPERTY_HINT_HIDE_QUATERNION_EDIT, "", PROPERTY_USAGE_EDITOR), "set_quaternion", "get_quaternion");
  1261. ADD_PROPERTY(PropertyInfo(Variant::BASIS, "basis", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_basis", "get_basis");
  1262. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "scale", PROPERTY_HINT_LINK, "", PROPERTY_USAGE_EDITOR), "set_scale", "get_scale");
  1263. ADD_PROPERTY(PropertyInfo(Variant::INT, "rotation_edit_mode", PROPERTY_HINT_ENUM, "Euler,Quaternion,Basis"), "set_rotation_edit_mode", "get_rotation_edit_mode");
  1264. ADD_PROPERTY(PropertyInfo(Variant::INT, "rotation_order", PROPERTY_HINT_ENUM, "XYZ,XZY,YXZ,YZX,ZXY,ZYX"), "set_rotation_order", "get_rotation_order");
  1265. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "top_level"), "set_as_top_level", "is_set_as_top_level");
  1266. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "global_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_global_position", "get_global_position");
  1267. ADD_PROPERTY(PropertyInfo(Variant::BASIS, "global_basis", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_global_basis", "get_global_basis");
  1268. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "global_rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_global_rotation", "get_global_rotation");
  1269. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "global_rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_global_rotation_degrees", "get_global_rotation_degrees");
  1270. ADD_GROUP("Visibility", "");
  1271. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  1272. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "visibility_parent", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "GeometryInstance3D"), "set_visibility_parent", "get_visibility_parent");
  1273. ADD_SIGNAL(MethodInfo("visibility_changed"));
  1274. }
  1275. Node3D::Node3D() :
  1276. xform_change(this), _client_physics_interpolation_node_3d_list(this) {
  1277. // Default member initializer for bitfield is a C++20 extension, so:
  1278. data.top_level = false;
  1279. data.inside_world = false;
  1280. data.ignore_notification = false;
  1281. data.notify_local_transform = false;
  1282. data.notify_transform = false;
  1283. data.visible = true;
  1284. data.disable_scale = false;
  1285. data.vi_visible = true;
  1286. data.fti_on_frame_xform_list = false;
  1287. data.fti_on_frame_property_list = false;
  1288. data.fti_on_tick_xform_list = false;
  1289. data.fti_on_tick_property_list = false;
  1290. data.fti_global_xform_interp_set = false;
  1291. data.fti_frame_xform_force_update = false;
  1292. data.fti_is_identity_xform = false;
  1293. data.fti_processed = false;
  1294. #ifdef TOOLS_ENABLED
  1295. data.gizmos_requested = false;
  1296. data.gizmos_disabled = false;
  1297. data.gizmos_dirty = false;
  1298. data.transform_gizmo_visible = true;
  1299. #endif
  1300. }
  1301. Node3D::~Node3D() {
  1302. _disable_client_physics_interpolation();
  1303. if (is_inside_tree()) {
  1304. get_tree()->get_scene_tree_fti().node_3d_notify_delete(this);
  1305. }
  1306. }