spatial.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*************************************************************************/
  2. /* spatial.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "spatial.h"
  31. #include "core/engine.h"
  32. #include "core/message_queue.h"
  33. #include "scene/main/scene_tree.h"
  34. #include "scene/main/viewport.h"
  35. #include "scene/scene_string_names.h"
  36. #include "servers/visual_server_callbacks.h"
  37. /*
  38. possible algorithms:
  39. Algorithm 1: (current)
  40. definition of invalidation: global is invalid
  41. 1) If a node sets a LOCAL, it produces an invalidation of everything above
  42. . a) If above is invalid, don't keep invalidating upwards
  43. 2) If a node sets a GLOBAL, it is converted to LOCAL (and forces validation of everything pending below)
  44. drawback: setting/reading globals is useful and used very very often, and using affine inverses is slow
  45. ---
  46. Algorithm 2: (no longer current)
  47. definition of invalidation: NONE dirty, LOCAL dirty, GLOBAL dirty
  48. 1) If a node sets a LOCAL, it must climb the tree and set it as GLOBAL dirty
  49. . a) marking GLOBALs as dirty up all the tree must be done always
  50. 2) If a node sets a GLOBAL, it marks local as dirty, and that's all?
  51. //is clearing the dirty state correct in this case?
  52. drawback: setting a local down the tree forces many tree walks often
  53. --
  54. future: no idea
  55. */
  56. SpatialGizmo::SpatialGizmo() {
  57. }
  58. void Spatial::_notify_dirty() {
  59. #ifdef TOOLS_ENABLED
  60. if ((data.gizmo.is_valid() || data.notify_transform) && !data.ignore_notification && !xform_change.in_list()) {
  61. #else
  62. if (data.notify_transform && !data.ignore_notification && !xform_change.in_list()) {
  63. #endif
  64. get_tree()->xform_change_list.add(&xform_change);
  65. }
  66. }
  67. void Spatial::_update_local_transform() const {
  68. data.local_transform.basis.set_euler_scale(data.rotation, data.scale);
  69. data.dirty &= ~DIRTY_LOCAL;
  70. }
  71. void Spatial::_propagate_transform_changed(Spatial *p_origin) {
  72. if (!is_inside_tree()) {
  73. return;
  74. }
  75. /*
  76. if (data.dirty&DIRTY_GLOBAL)
  77. return; //already dirty
  78. */
  79. data.children_lock++;
  80. for (List<Spatial *>::Element *E = data.children.front(); E; E = E->next()) {
  81. if (E->get()->data.toplevel_active) {
  82. continue; //don't propagate to a toplevel
  83. }
  84. E->get()->_propagate_transform_changed(p_origin);
  85. }
  86. #ifdef TOOLS_ENABLED
  87. if ((data.gizmo.is_valid() || data.notify_transform) && !data.ignore_notification && !xform_change.in_list()) {
  88. #else
  89. if (data.notify_transform && !data.ignore_notification && !xform_change.in_list()) {
  90. #endif
  91. get_tree()->xform_change_list.add(&xform_change);
  92. }
  93. data.dirty |= DIRTY_GLOBAL;
  94. data.children_lock--;
  95. }
  96. void Spatial::notification_callback(int p_message_type) {
  97. switch (p_message_type) {
  98. default:
  99. break;
  100. case VisualServerCallbacks::CALLBACK_NOTIFICATION_ENTER_GAMEPLAY: {
  101. notification(NOTIFICATION_ENTER_GAMEPLAY);
  102. } break;
  103. case VisualServerCallbacks::CALLBACK_NOTIFICATION_EXIT_GAMEPLAY: {
  104. notification(NOTIFICATION_EXIT_GAMEPLAY);
  105. } break;
  106. case VisualServerCallbacks::CALLBACK_SIGNAL_ENTER_GAMEPLAY: {
  107. emit_signal("gameplay_entered");
  108. } break;
  109. case VisualServerCallbacks::CALLBACK_SIGNAL_EXIT_GAMEPLAY: {
  110. emit_signal("gameplay_exited");
  111. } break;
  112. }
  113. }
  114. void Spatial::_notification(int p_what) {
  115. switch (p_what) {
  116. case NOTIFICATION_ENTER_TREE: {
  117. ERR_FAIL_COND(!get_tree());
  118. Node *p = get_parent();
  119. if (p) {
  120. data.parent = Object::cast_to<Spatial>(p);
  121. }
  122. if (data.parent) {
  123. data.C = data.parent->data.children.push_back(this);
  124. } else {
  125. data.C = nullptr;
  126. }
  127. if (data.toplevel && !Engine::get_singleton()->is_editor_hint()) {
  128. if (data.parent) {
  129. data.local_transform = data.parent->get_global_transform() * get_transform();
  130. data.dirty = DIRTY_VECTORS; //global is always dirty upon entering a scene
  131. }
  132. data.toplevel_active = true;
  133. }
  134. data.dirty |= DIRTY_GLOBAL; //global is always dirty upon entering a scene
  135. _notify_dirty();
  136. notification(NOTIFICATION_ENTER_WORLD);
  137. } break;
  138. case NOTIFICATION_EXIT_TREE: {
  139. notification(NOTIFICATION_EXIT_WORLD, true);
  140. if (xform_change.in_list()) {
  141. get_tree()->xform_change_list.remove(&xform_change);
  142. }
  143. if (data.C) {
  144. data.parent->data.children.erase(data.C);
  145. }
  146. data.parent = nullptr;
  147. data.C = nullptr;
  148. data.toplevel_active = false;
  149. } break;
  150. case NOTIFICATION_ENTER_WORLD: {
  151. data.inside_world = true;
  152. data.viewport = nullptr;
  153. Node *parent = get_parent();
  154. while (parent && !data.viewport) {
  155. data.viewport = Object::cast_to<Viewport>(parent);
  156. parent = parent->get_parent();
  157. }
  158. ERR_FAIL_COND(!data.viewport);
  159. if (get_script_instance()) {
  160. get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_enter_world, nullptr, 0);
  161. }
  162. #ifdef TOOLS_ENABLED
  163. if (Engine::get_singleton()->is_editor_hint() && get_tree()->is_node_being_edited(this)) {
  164. //get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,SceneStringNames::get_singleton()->_spatial_editor_group,SceneStringNames::get_singleton()->_request_gizmo,this);
  165. get_tree()->call_group_flags(0, SceneStringNames::get_singleton()->_spatial_editor_group, SceneStringNames::get_singleton()->_request_gizmo, this);
  166. if (!data.gizmo_disabled) {
  167. if (data.gizmo.is_valid()) {
  168. data.gizmo->create();
  169. if (is_visible_in_tree()) {
  170. data.gizmo->redraw();
  171. }
  172. data.gizmo->transform();
  173. }
  174. }
  175. }
  176. #endif
  177. } break;
  178. case NOTIFICATION_EXIT_WORLD: {
  179. #ifdef TOOLS_ENABLED
  180. if (data.gizmo.is_valid()) {
  181. data.gizmo->free();
  182. data.gizmo.unref();
  183. }
  184. #endif
  185. if (get_script_instance()) {
  186. get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_exit_world, nullptr, 0);
  187. }
  188. data.viewport = nullptr;
  189. data.inside_world = false;
  190. } break;
  191. case NOTIFICATION_TRANSFORM_CHANGED: {
  192. #ifdef TOOLS_ENABLED
  193. if (data.gizmo.is_valid()) {
  194. data.gizmo->transform();
  195. }
  196. #endif
  197. } break;
  198. default: {
  199. }
  200. }
  201. }
  202. void Spatial::set_transform(const Transform &p_transform) {
  203. data.local_transform = p_transform;
  204. data.dirty |= DIRTY_VECTORS;
  205. _change_notify("translation");
  206. _change_notify("rotation");
  207. _change_notify("rotation_degrees");
  208. _change_notify("scale");
  209. _propagate_transform_changed(this);
  210. if (data.notify_local_transform) {
  211. notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  212. }
  213. }
  214. void Spatial::set_global_transform(const Transform &p_transform) {
  215. Transform xform = (data.parent && !data.toplevel_active) ? data.parent->get_global_transform().affine_inverse() * p_transform : p_transform;
  216. set_transform(xform);
  217. }
  218. Transform Spatial::get_transform() const {
  219. if (data.dirty & DIRTY_LOCAL) {
  220. _update_local_transform();
  221. }
  222. return data.local_transform;
  223. }
  224. Transform Spatial::get_global_transform() const {
  225. ERR_FAIL_COND_V(!is_inside_tree(), Transform());
  226. if (data.dirty & DIRTY_GLOBAL) {
  227. if (data.dirty & DIRTY_LOCAL) {
  228. _update_local_transform();
  229. }
  230. if (data.parent && !data.toplevel_active) {
  231. data.global_transform = data.parent->get_global_transform() * data.local_transform;
  232. } else {
  233. data.global_transform = data.local_transform;
  234. }
  235. if (data.disable_scale) {
  236. data.global_transform.basis.orthonormalize();
  237. }
  238. data.dirty &= ~DIRTY_GLOBAL;
  239. }
  240. return data.global_transform;
  241. }
  242. #ifdef TOOLS_ENABLED
  243. Transform Spatial::get_global_gizmo_transform() const {
  244. return get_global_transform();
  245. }
  246. Transform Spatial::get_local_gizmo_transform() const {
  247. return get_transform();
  248. }
  249. #endif
  250. Spatial *Spatial::get_parent_spatial() const {
  251. return data.parent;
  252. }
  253. Transform Spatial::get_relative_transform(const Node *p_parent) const {
  254. if (p_parent == this) {
  255. return Transform();
  256. }
  257. ERR_FAIL_COND_V(!data.parent, Transform());
  258. if (p_parent == data.parent) {
  259. return get_transform();
  260. } else {
  261. return data.parent->get_relative_transform(p_parent) * get_transform();
  262. }
  263. }
  264. void Spatial::set_translation(const Vector3 &p_translation) {
  265. data.local_transform.origin = p_translation;
  266. _change_notify("transform");
  267. _propagate_transform_changed(this);
  268. if (data.notify_local_transform) {
  269. notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  270. }
  271. }
  272. void Spatial::set_rotation(const Vector3 &p_euler_rad) {
  273. if (data.dirty & DIRTY_VECTORS) {
  274. data.scale = data.local_transform.basis.get_scale();
  275. data.dirty &= ~DIRTY_VECTORS;
  276. }
  277. data.rotation = p_euler_rad;
  278. data.dirty |= DIRTY_LOCAL;
  279. _change_notify("transform");
  280. _propagate_transform_changed(this);
  281. if (data.notify_local_transform) {
  282. notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  283. }
  284. }
  285. void Spatial::set_rotation_degrees(const Vector3 &p_euler_deg) {
  286. set_rotation(p_euler_deg * Math_PI / 180.0);
  287. }
  288. void Spatial::set_scale(const Vector3 &p_scale) {
  289. if (data.dirty & DIRTY_VECTORS) {
  290. data.rotation = data.local_transform.basis.get_rotation();
  291. data.dirty &= ~DIRTY_VECTORS;
  292. }
  293. data.scale = p_scale;
  294. data.dirty |= DIRTY_LOCAL;
  295. _change_notify("transform");
  296. _propagate_transform_changed(this);
  297. if (data.notify_local_transform) {
  298. notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  299. }
  300. }
  301. Vector3 Spatial::get_translation() const {
  302. return data.local_transform.origin;
  303. }
  304. Vector3 Spatial::get_rotation() const {
  305. if (data.dirty & DIRTY_VECTORS) {
  306. data.scale = data.local_transform.basis.get_scale();
  307. data.rotation = data.local_transform.basis.get_rotation();
  308. data.dirty &= ~DIRTY_VECTORS;
  309. }
  310. return data.rotation;
  311. }
  312. Vector3 Spatial::get_rotation_degrees() const {
  313. return get_rotation() * 180.0 / Math_PI;
  314. }
  315. Vector3 Spatial::get_scale() const {
  316. if (data.dirty & DIRTY_VECTORS) {
  317. data.scale = data.local_transform.basis.get_scale();
  318. data.rotation = data.local_transform.basis.get_rotation();
  319. data.dirty &= ~DIRTY_VECTORS;
  320. }
  321. return data.scale;
  322. }
  323. void Spatial::update_gizmo() {
  324. #ifdef TOOLS_ENABLED
  325. if (!is_inside_world()) {
  326. return;
  327. }
  328. if (!data.gizmo.is_valid()) {
  329. get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, SceneStringNames::get_singleton()->_spatial_editor_group, SceneStringNames::get_singleton()->_request_gizmo, this);
  330. }
  331. if (!data.gizmo.is_valid()) {
  332. return;
  333. }
  334. if (data.gizmo_dirty) {
  335. return;
  336. }
  337. data.gizmo_dirty = true;
  338. MessageQueue::get_singleton()->push_call(this, "_update_gizmo");
  339. #endif
  340. }
  341. void Spatial::set_gizmo(const Ref<SpatialGizmo> &p_gizmo) {
  342. #ifdef TOOLS_ENABLED
  343. if (data.gizmo_disabled) {
  344. return;
  345. }
  346. if (data.gizmo.is_valid() && is_inside_world()) {
  347. data.gizmo->free();
  348. }
  349. data.gizmo = p_gizmo;
  350. if (data.gizmo.is_valid() && is_inside_world()) {
  351. data.gizmo->create();
  352. if (is_visible_in_tree()) {
  353. data.gizmo->redraw();
  354. }
  355. data.gizmo->transform();
  356. }
  357. #endif
  358. }
  359. Ref<SpatialGizmo> Spatial::get_gizmo() const {
  360. #ifdef TOOLS_ENABLED
  361. return data.gizmo;
  362. #else
  363. return Ref<SpatialGizmo>();
  364. #endif
  365. }
  366. void Spatial::_update_gizmo() {
  367. #ifdef TOOLS_ENABLED
  368. if (!is_inside_world()) {
  369. return;
  370. }
  371. data.gizmo_dirty = false;
  372. if (data.gizmo.is_valid()) {
  373. if (is_visible_in_tree()) {
  374. data.gizmo->redraw();
  375. } else {
  376. data.gizmo->clear();
  377. }
  378. }
  379. #endif
  380. }
  381. #ifdef TOOLS_ENABLED
  382. void Spatial::set_disable_gizmo(bool p_enabled) {
  383. data.gizmo_disabled = p_enabled;
  384. if (!p_enabled && data.gizmo.is_valid()) {
  385. data.gizmo = Ref<SpatialGizmo>();
  386. }
  387. }
  388. #endif
  389. void Spatial::set_disable_scale(bool p_enabled) {
  390. data.disable_scale = p_enabled;
  391. }
  392. bool Spatial::is_scale_disabled() const {
  393. return data.disable_scale;
  394. }
  395. void Spatial::set_as_toplevel(bool p_enabled) {
  396. if (data.toplevel == p_enabled) {
  397. return;
  398. }
  399. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
  400. if (p_enabled) {
  401. set_transform(get_global_transform());
  402. } else if (data.parent) {
  403. set_transform(data.parent->get_global_transform().affine_inverse() * get_global_transform());
  404. }
  405. data.toplevel = p_enabled;
  406. data.toplevel_active = p_enabled;
  407. } else {
  408. data.toplevel = p_enabled;
  409. }
  410. }
  411. bool Spatial::is_set_as_toplevel() const {
  412. return data.toplevel;
  413. }
  414. Ref<World> Spatial::get_world() const {
  415. ERR_FAIL_COND_V(!is_inside_world(), Ref<World>());
  416. ERR_FAIL_COND_V(!data.viewport, Ref<World>());
  417. return data.viewport->find_world();
  418. }
  419. void Spatial::_propagate_visibility_changed() {
  420. notification(NOTIFICATION_VISIBILITY_CHANGED);
  421. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  422. _change_notify("visible");
  423. #ifdef TOOLS_ENABLED
  424. if (data.gizmo.is_valid()) {
  425. _update_gizmo();
  426. }
  427. #endif
  428. for (List<Spatial *>::Element *E = data.children.front(); E; E = E->next()) {
  429. Spatial *c = E->get();
  430. if (!c || !c->data.visible) {
  431. continue;
  432. }
  433. c->_propagate_visibility_changed();
  434. }
  435. }
  436. void Spatial::show() {
  437. if (data.visible) {
  438. return;
  439. }
  440. data.visible = true;
  441. if (!is_inside_tree()) {
  442. return;
  443. }
  444. _propagate_visibility_changed();
  445. }
  446. void Spatial::hide() {
  447. if (!data.visible) {
  448. return;
  449. }
  450. data.visible = false;
  451. if (!is_inside_tree()) {
  452. return;
  453. }
  454. _propagate_visibility_changed();
  455. }
  456. bool Spatial::is_visible_in_tree() const {
  457. const Spatial *s = this;
  458. while (s) {
  459. if (!s->data.visible) {
  460. return false;
  461. }
  462. s = s->data.parent;
  463. }
  464. return true;
  465. }
  466. void Spatial::set_visible(bool p_visible) {
  467. if (p_visible) {
  468. show();
  469. } else {
  470. hide();
  471. }
  472. }
  473. bool Spatial::is_visible() const {
  474. return data.visible;
  475. }
  476. void Spatial::rotate_object_local(const Vector3 &p_axis, float p_angle) {
  477. Transform t = get_transform();
  478. t.basis.rotate_local(p_axis, p_angle);
  479. set_transform(t);
  480. }
  481. void Spatial::rotate(const Vector3 &p_axis, float p_angle) {
  482. Transform t = get_transform();
  483. t.basis.rotate(p_axis, p_angle);
  484. set_transform(t);
  485. }
  486. void Spatial::rotate_x(float p_angle) {
  487. Transform t = get_transform();
  488. t.basis.rotate(Vector3(1, 0, 0), p_angle);
  489. set_transform(t);
  490. }
  491. void Spatial::rotate_y(float p_angle) {
  492. Transform t = get_transform();
  493. t.basis.rotate(Vector3(0, 1, 0), p_angle);
  494. set_transform(t);
  495. }
  496. void Spatial::rotate_z(float p_angle) {
  497. Transform t = get_transform();
  498. t.basis.rotate(Vector3(0, 0, 1), p_angle);
  499. set_transform(t);
  500. }
  501. void Spatial::translate(const Vector3 &p_offset) {
  502. Transform t = get_transform();
  503. t.translate(p_offset);
  504. set_transform(t);
  505. }
  506. void Spatial::translate_object_local(const Vector3 &p_offset) {
  507. Transform t = get_transform();
  508. Transform s;
  509. s.translate(p_offset);
  510. set_transform(t * s);
  511. }
  512. void Spatial::scale(const Vector3 &p_ratio) {
  513. Transform t = get_transform();
  514. t.basis.scale(p_ratio);
  515. set_transform(t);
  516. }
  517. void Spatial::scale_object_local(const Vector3 &p_scale) {
  518. Transform t = get_transform();
  519. t.basis.scale_local(p_scale);
  520. set_transform(t);
  521. }
  522. void Spatial::global_rotate(const Vector3 &p_axis, float p_angle) {
  523. Transform t = get_global_transform();
  524. t.basis.rotate(p_axis, p_angle);
  525. set_global_transform(t);
  526. }
  527. void Spatial::global_scale(const Vector3 &p_scale) {
  528. Transform t = get_global_transform();
  529. t.basis.scale(p_scale);
  530. set_global_transform(t);
  531. }
  532. void Spatial::global_translate(const Vector3 &p_offset) {
  533. Transform t = get_global_transform();
  534. t.origin += p_offset;
  535. set_global_transform(t);
  536. }
  537. void Spatial::orthonormalize() {
  538. Transform t = get_transform();
  539. t.orthonormalize();
  540. set_transform(t);
  541. }
  542. void Spatial::set_identity() {
  543. set_transform(Transform());
  544. }
  545. void Spatial::look_at(const Vector3 &p_target, const Vector3 &p_up) {
  546. Vector3 origin(get_global_transform().origin);
  547. look_at_from_position(origin, p_target, p_up);
  548. }
  549. void Spatial::look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up) {
  550. ERR_FAIL_COND_MSG(p_pos == p_target, "Node origin and target are in the same position, look_at() failed.");
  551. ERR_FAIL_COND_MSG(p_up == Vector3(), "The up vector can't be zero, look_at() failed.");
  552. ERR_FAIL_COND_MSG(p_up.cross(p_target - p_pos) == Vector3(), "Up vector and direction between node origin and target are aligned, look_at() failed.");
  553. Transform lookat;
  554. lookat.origin = p_pos;
  555. Vector3 original_scale(get_scale());
  556. lookat = lookat.looking_at(p_target, p_up);
  557. set_global_transform(lookat);
  558. set_scale(original_scale);
  559. }
  560. Vector3 Spatial::to_local(Vector3 p_global) const {
  561. return get_global_transform().affine_inverse().xform(p_global);
  562. }
  563. Vector3 Spatial::to_global(Vector3 p_local) const {
  564. return get_global_transform().xform(p_local);
  565. }
  566. void Spatial::set_notify_transform(bool p_enable) {
  567. data.notify_transform = p_enable;
  568. }
  569. bool Spatial::is_transform_notification_enabled() const {
  570. return data.notify_transform;
  571. }
  572. void Spatial::set_notify_local_transform(bool p_enable) {
  573. data.notify_local_transform = p_enable;
  574. }
  575. bool Spatial::is_local_transform_notification_enabled() const {
  576. return data.notify_local_transform;
  577. }
  578. void Spatial::force_update_transform() {
  579. ERR_FAIL_COND(!is_inside_tree());
  580. if (!xform_change.in_list()) {
  581. return; //nothing to update
  582. }
  583. get_tree()->xform_change_list.remove(&xform_change);
  584. notification(NOTIFICATION_TRANSFORM_CHANGED);
  585. }
  586. void Spatial::_bind_methods() {
  587. ClassDB::bind_method(D_METHOD("set_transform", "local"), &Spatial::set_transform);
  588. ClassDB::bind_method(D_METHOD("get_transform"), &Spatial::get_transform);
  589. ClassDB::bind_method(D_METHOD("set_translation", "translation"), &Spatial::set_translation);
  590. ClassDB::bind_method(D_METHOD("get_translation"), &Spatial::get_translation);
  591. ClassDB::bind_method(D_METHOD("set_rotation", "euler"), &Spatial::set_rotation);
  592. ClassDB::bind_method(D_METHOD("get_rotation"), &Spatial::get_rotation);
  593. ClassDB::bind_method(D_METHOD("set_rotation_degrees", "euler_degrees"), &Spatial::set_rotation_degrees);
  594. ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Spatial::get_rotation_degrees);
  595. ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Spatial::set_scale);
  596. ClassDB::bind_method(D_METHOD("get_scale"), &Spatial::get_scale);
  597. ClassDB::bind_method(D_METHOD("set_global_transform", "global"), &Spatial::set_global_transform);
  598. ClassDB::bind_method(D_METHOD("get_global_transform"), &Spatial::get_global_transform);
  599. ClassDB::bind_method(D_METHOD("get_parent_spatial"), &Spatial::get_parent_spatial);
  600. ClassDB::bind_method(D_METHOD("set_ignore_transform_notification", "enabled"), &Spatial::set_ignore_transform_notification);
  601. ClassDB::bind_method(D_METHOD("set_as_toplevel", "enable"), &Spatial::set_as_toplevel);
  602. ClassDB::bind_method(D_METHOD("is_set_as_toplevel"), &Spatial::is_set_as_toplevel);
  603. ClassDB::bind_method(D_METHOD("set_disable_scale", "disable"), &Spatial::set_disable_scale);
  604. ClassDB::bind_method(D_METHOD("is_scale_disabled"), &Spatial::is_scale_disabled);
  605. ClassDB::bind_method(D_METHOD("get_world"), &Spatial::get_world);
  606. ClassDB::bind_method(D_METHOD("force_update_transform"), &Spatial::force_update_transform);
  607. ClassDB::bind_method(D_METHOD("_update_gizmo"), &Spatial::_update_gizmo);
  608. ClassDB::bind_method(D_METHOD("update_gizmo"), &Spatial::update_gizmo);
  609. ClassDB::bind_method(D_METHOD("set_gizmo", "gizmo"), &Spatial::set_gizmo);
  610. ClassDB::bind_method(D_METHOD("get_gizmo"), &Spatial::get_gizmo);
  611. ClassDB::bind_method(D_METHOD("set_visible", "visible"), &Spatial::set_visible);
  612. ClassDB::bind_method(D_METHOD("is_visible"), &Spatial::is_visible);
  613. ClassDB::bind_method(D_METHOD("is_visible_in_tree"), &Spatial::is_visible_in_tree);
  614. ClassDB::bind_method(D_METHOD("show"), &Spatial::show);
  615. ClassDB::bind_method(D_METHOD("hide"), &Spatial::hide);
  616. ClassDB::bind_method(D_METHOD("set_notify_local_transform", "enable"), &Spatial::set_notify_local_transform);
  617. ClassDB::bind_method(D_METHOD("is_local_transform_notification_enabled"), &Spatial::is_local_transform_notification_enabled);
  618. ClassDB::bind_method(D_METHOD("set_notify_transform", "enable"), &Spatial::set_notify_transform);
  619. ClassDB::bind_method(D_METHOD("is_transform_notification_enabled"), &Spatial::is_transform_notification_enabled);
  620. ClassDB::bind_method(D_METHOD("rotate", "axis", "angle"), &Spatial::rotate);
  621. ClassDB::bind_method(D_METHOD("global_rotate", "axis", "angle"), &Spatial::global_rotate);
  622. ClassDB::bind_method(D_METHOD("global_scale", "scale"), &Spatial::global_scale);
  623. ClassDB::bind_method(D_METHOD("global_translate", "offset"), &Spatial::global_translate);
  624. ClassDB::bind_method(D_METHOD("rotate_object_local", "axis", "angle"), &Spatial::rotate_object_local);
  625. ClassDB::bind_method(D_METHOD("scale_object_local", "scale"), &Spatial::scale_object_local);
  626. ClassDB::bind_method(D_METHOD("translate_object_local", "offset"), &Spatial::translate_object_local);
  627. ClassDB::bind_method(D_METHOD("rotate_x", "angle"), &Spatial::rotate_x);
  628. ClassDB::bind_method(D_METHOD("rotate_y", "angle"), &Spatial::rotate_y);
  629. ClassDB::bind_method(D_METHOD("rotate_z", "angle"), &Spatial::rotate_z);
  630. ClassDB::bind_method(D_METHOD("translate", "offset"), &Spatial::translate);
  631. ClassDB::bind_method(D_METHOD("orthonormalize"), &Spatial::orthonormalize);
  632. ClassDB::bind_method(D_METHOD("set_identity"), &Spatial::set_identity);
  633. ClassDB::bind_method(D_METHOD("look_at", "target", "up"), &Spatial::look_at);
  634. ClassDB::bind_method(D_METHOD("look_at_from_position", "position", "target", "up"), &Spatial::look_at_from_position);
  635. ClassDB::bind_method(D_METHOD("to_local", "global_point"), &Spatial::to_local);
  636. ClassDB::bind_method(D_METHOD("to_global", "local_point"), &Spatial::to_global);
  637. BIND_CONSTANT(NOTIFICATION_TRANSFORM_CHANGED);
  638. BIND_CONSTANT(NOTIFICATION_ENTER_WORLD);
  639. BIND_CONSTANT(NOTIFICATION_EXIT_WORLD);
  640. BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
  641. BIND_CONSTANT(NOTIFICATION_ENTER_GAMEPLAY);
  642. BIND_CONSTANT(NOTIFICATION_EXIT_GAMEPLAY);
  643. //ADD_PROPERTY( PropertyInfo(Variant::TRANSFORM,"transform/global",PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR ), "set_global_transform", "get_global_transform") ;
  644. ADD_GROUP("Transform", "");
  645. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "global_transform", PROPERTY_HINT_NONE, "", 0), "set_global_transform", "get_global_transform");
  646. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "translation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_translation", "get_translation");
  647. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_rotation_degrees", "get_rotation_degrees");
  648. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "rotation", PROPERTY_HINT_NONE, "", 0), "set_rotation", "get_rotation");
  649. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "scale", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_scale", "get_scale");
  650. ADD_GROUP("Matrix", "");
  651. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "transform", PROPERTY_HINT_NONE, ""), "set_transform", "get_transform");
  652. ADD_GROUP("Visibility", "");
  653. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  654. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gizmo", PROPERTY_HINT_RESOURCE_TYPE, "SpatialGizmo", 0), "set_gizmo", "get_gizmo");
  655. ADD_SIGNAL(MethodInfo("visibility_changed"));
  656. ADD_SIGNAL(MethodInfo("gameplay_entered"));
  657. ADD_SIGNAL(MethodInfo("gameplay_exited"));
  658. }
  659. Spatial::Spatial() :
  660. xform_change(this) {
  661. data.dirty = DIRTY_NONE;
  662. data.children_lock = 0;
  663. data.ignore_notification = false;
  664. data.toplevel = false;
  665. data.toplevel_active = false;
  666. data.scale = Vector3(1, 1, 1);
  667. data.viewport = nullptr;
  668. data.inside_world = false;
  669. data.visible = true;
  670. data.disable_scale = false;
  671. data.spatial_flags = SPATIAL_FLAG_VI_VISIBLE;
  672. #ifdef TOOLS_ENABLED
  673. data.gizmo_disabled = false;
  674. data.gizmo_dirty = false;
  675. #endif
  676. data.notify_local_transform = false;
  677. data.notify_transform = false;
  678. data.parent = nullptr;
  679. data.C = nullptr;
  680. }
  681. Spatial::~Spatial() {
  682. }