spatial.cpp 24 KB

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