spatial.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*************************************************************************/
  2. /* spatial.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "engine.h"
  32. #include "message_queue.h"
  33. #include "scene/main/viewport.h"
  34. #include "scene/scene_string_names.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 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. SpatialGizmo::SpatialGizmo() {
  55. }
  56. void Spatial::_notify_dirty() {
  57. #ifdef TOOLS_ENABLED
  58. if ((data.gizmo.is_valid() || 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 Spatial::_update_local_transform() const {
  66. data.local_transform.basis = Basis();
  67. data.local_transform.basis.scale(data.scale);
  68. data.local_transform.basis.rotate(data.rotation);
  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. E->get()->_propagate_transform_changed(p_origin);
  84. }
  85. #ifdef TOOLS_ENABLED
  86. if ((data.gizmo.is_valid() || data.notify_transform) && !data.ignore_notification && !xform_change.in_list()) {
  87. #else
  88. if (data.notify_transform && !data.ignore_notification && !xform_change.in_list()) {
  89. #endif
  90. get_tree()->xform_change_list.add(&xform_change);
  91. }
  92. data.dirty |= DIRTY_GLOBAL;
  93. data.children_lock--;
  94. }
  95. void Spatial::_notification(int p_what) {
  96. switch (p_what) {
  97. case NOTIFICATION_ENTER_TREE: {
  98. Node *p = get_parent();
  99. if (p)
  100. data.parent = Object::cast_to<Spatial>(p);
  101. if (data.parent)
  102. data.C = data.parent->data.children.push_back(this);
  103. else
  104. data.C = NULL;
  105. if (data.toplevel && !Engine::get_singleton()->is_editor_hint()) {
  106. if (data.parent) {
  107. data.local_transform = data.parent->get_global_transform() * get_transform();
  108. data.dirty = DIRTY_VECTORS; //global is always dirty upon entering a scene
  109. }
  110. data.toplevel_active = true;
  111. }
  112. data.dirty |= DIRTY_GLOBAL; //global is always dirty upon entering a scene
  113. _notify_dirty();
  114. notification(NOTIFICATION_ENTER_WORLD);
  115. } break;
  116. case NOTIFICATION_EXIT_TREE: {
  117. notification(NOTIFICATION_EXIT_WORLD, true);
  118. if (xform_change.in_list())
  119. get_tree()->xform_change_list.remove(&xform_change);
  120. if (data.C)
  121. data.parent->data.children.erase(data.C);
  122. data.parent = NULL;
  123. data.C = NULL;
  124. data.toplevel_active = false;
  125. } break;
  126. case NOTIFICATION_ENTER_WORLD: {
  127. data.inside_world = true;
  128. data.viewport = NULL;
  129. Node *parent = get_parent();
  130. while (parent && !data.viewport) {
  131. data.viewport = Object::cast_to<Viewport>(parent);
  132. parent = parent->get_parent();
  133. }
  134. ERR_FAIL_COND(!data.viewport);
  135. if (get_script_instance()) {
  136. get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_enter_world, NULL, 0);
  137. }
  138. #ifdef TOOLS_ENABLED
  139. if (Engine::get_singleton()->is_editor_hint()) {
  140. //get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,SceneStringNames::get_singleton()->_spatial_editor_group,SceneStringNames::get_singleton()->_request_gizmo,this);
  141. get_tree()->call_group_flags(0, SceneStringNames::get_singleton()->_spatial_editor_group, SceneStringNames::get_singleton()->_request_gizmo, this);
  142. if (!data.gizmo_disabled) {
  143. if (data.gizmo.is_valid()) {
  144. data.gizmo->create();
  145. if (data.gizmo->can_draw()) {
  146. data.gizmo->redraw();
  147. }
  148. data.gizmo->transform();
  149. }
  150. }
  151. }
  152. #endif
  153. } break;
  154. case NOTIFICATION_EXIT_WORLD: {
  155. #ifdef TOOLS_ENABLED
  156. if (data.gizmo.is_valid()) {
  157. data.gizmo->free();
  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_deg");
  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. data.dirty &= ~DIRTY_GLOBAL;
  213. }
  214. return data.global_transform;
  215. }
  216. Spatial *Spatial::get_parent_spatial() const {
  217. return data.parent;
  218. }
  219. Transform Spatial::get_relative_transform(const Node *p_parent) const {
  220. if (p_parent == this)
  221. return Transform();
  222. ERR_FAIL_COND_V(!data.parent, Transform());
  223. if (p_parent == data.parent)
  224. return get_transform();
  225. else
  226. return data.parent->get_relative_transform(p_parent) * get_transform();
  227. }
  228. void Spatial::set_translation(const Vector3 &p_translation) {
  229. data.local_transform.origin = p_translation;
  230. _propagate_transform_changed(this);
  231. if (data.notify_local_transform) {
  232. notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  233. }
  234. }
  235. void Spatial::set_rotation(const Vector3 &p_euler_rad) {
  236. if (data.dirty & DIRTY_VECTORS) {
  237. data.scale = data.local_transform.basis.get_scale();
  238. data.dirty &= ~DIRTY_VECTORS;
  239. }
  240. data.rotation = p_euler_rad;
  241. data.dirty |= DIRTY_LOCAL;
  242. _propagate_transform_changed(this);
  243. if (data.notify_local_transform) {
  244. notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  245. }
  246. }
  247. void Spatial::set_rotation_in_degrees(const Vector3 &p_euler_deg) {
  248. set_rotation(p_euler_deg * Math_PI / 180.0);
  249. }
  250. void Spatial::_set_rotation_deg(const Vector3 &p_euler_deg) {
  251. WARN_PRINT("Deprecated method Spatial._set_rotation_deg(): This method was renamed to set_rotation_deg. Please adapt your code accordingly, as the old method will be obsoleted.");
  252. set_rotation_in_degrees(p_euler_deg);
  253. }
  254. void Spatial::set_scale(const Vector3 &p_scale) {
  255. if (data.dirty & DIRTY_VECTORS) {
  256. data.rotation = data.local_transform.basis.get_rotation();
  257. data.dirty &= ~DIRTY_VECTORS;
  258. }
  259. data.scale = p_scale;
  260. data.dirty |= DIRTY_LOCAL;
  261. _propagate_transform_changed(this);
  262. if (data.notify_local_transform) {
  263. notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  264. }
  265. }
  266. Vector3 Spatial::get_translation() const {
  267. return data.local_transform.origin;
  268. }
  269. Vector3 Spatial::get_rotation() const {
  270. if (data.dirty & DIRTY_VECTORS) {
  271. data.scale = data.local_transform.basis.get_scale();
  272. data.rotation = data.local_transform.basis.get_rotation();
  273. data.dirty &= ~DIRTY_VECTORS;
  274. }
  275. return data.rotation;
  276. }
  277. Vector3 Spatial::get_rotation_in_degrees() const {
  278. return get_rotation() * 180.0 / Math_PI;
  279. }
  280. // Kept for compatibility after rename to set_rotd.
  281. // Could be removed after a couple releases.
  282. Vector3 Spatial::_get_rotation_deg() const {
  283. WARN_PRINT("Deprecated method Spatial._get_rotation_deg(): This method was renamed to get_rotation_deg. Please adapt your code accordingly, as the old method will be obsoleted.");
  284. return get_rotation_in_degrees();
  285. }
  286. Vector3 Spatial::get_scale() const {
  287. if (data.dirty & DIRTY_VECTORS) {
  288. data.scale = data.local_transform.basis.get_scale();
  289. data.rotation = data.local_transform.basis.get_rotation();
  290. data.dirty &= ~DIRTY_VECTORS;
  291. }
  292. return data.scale;
  293. }
  294. void Spatial::update_gizmo() {
  295. #ifdef TOOLS_ENABLED
  296. if (!is_inside_world())
  297. return;
  298. if (!data.gizmo.is_valid())
  299. return;
  300. if (data.gizmo_dirty)
  301. return;
  302. data.gizmo_dirty = true;
  303. MessageQueue::get_singleton()->push_call(this, "_update_gizmo");
  304. #endif
  305. }
  306. void Spatial::set_gizmo(const Ref<SpatialGizmo> &p_gizmo) {
  307. #ifdef TOOLS_ENABLED
  308. if (data.gizmo_disabled)
  309. return;
  310. if (data.gizmo.is_valid() && is_inside_world())
  311. data.gizmo->free();
  312. data.gizmo = p_gizmo;
  313. if (data.gizmo.is_valid() && is_inside_world()) {
  314. data.gizmo->create();
  315. if (data.gizmo->can_draw()) {
  316. data.gizmo->redraw();
  317. }
  318. data.gizmo->transform();
  319. }
  320. #endif
  321. }
  322. Ref<SpatialGizmo> Spatial::get_gizmo() const {
  323. #ifdef TOOLS_ENABLED
  324. return data.gizmo;
  325. #else
  326. return Ref<SpatialGizmo>();
  327. #endif
  328. }
  329. #ifdef TOOLS_ENABLED
  330. void Spatial::_update_gizmo() {
  331. if (!is_inside_world())
  332. return;
  333. data.gizmo_dirty = false;
  334. if (data.gizmo.is_valid()) {
  335. if (data.gizmo->can_draw()) {
  336. if (is_visible_in_tree())
  337. data.gizmo->redraw();
  338. else
  339. data.gizmo->clear();
  340. }
  341. }
  342. }
  343. void Spatial::set_disable_gizmo(bool p_enabled) {
  344. data.gizmo_disabled = p_enabled;
  345. if (!p_enabled && data.gizmo.is_valid())
  346. data.gizmo = Ref<SpatialGizmo>();
  347. }
  348. #endif
  349. void Spatial::set_as_toplevel(bool p_enabled) {
  350. if (data.toplevel == p_enabled)
  351. return;
  352. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
  353. if (p_enabled)
  354. set_transform(get_global_transform());
  355. else if (data.parent)
  356. set_transform(data.parent->get_global_transform().affine_inverse() * get_global_transform());
  357. data.toplevel = p_enabled;
  358. data.toplevel_active = p_enabled;
  359. } else {
  360. data.toplevel = p_enabled;
  361. }
  362. }
  363. bool Spatial::is_set_as_toplevel() const {
  364. return data.toplevel;
  365. }
  366. Ref<World> Spatial::get_world() const {
  367. ERR_FAIL_COND_V(!is_inside_world(), Ref<World>());
  368. return data.viewport->find_world();
  369. }
  370. void Spatial::_propagate_visibility_changed() {
  371. notification(NOTIFICATION_VISIBILITY_CHANGED);
  372. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  373. _change_notify("visible");
  374. #ifdef TOOLS_ENABLED
  375. if (data.gizmo.is_valid())
  376. _update_gizmo();
  377. #endif
  378. for (List<Spatial *>::Element *E = data.children.front(); E; E = E->next()) {
  379. Spatial *c = E->get();
  380. if (!c || !c->data.visible)
  381. continue;
  382. c->_propagate_visibility_changed();
  383. }
  384. }
  385. void Spatial::show() {
  386. if (data.visible)
  387. return;
  388. data.visible = true;
  389. if (!is_inside_tree())
  390. return;
  391. _propagate_visibility_changed();
  392. }
  393. void Spatial::hide() {
  394. if (!data.visible)
  395. return;
  396. data.visible = false;
  397. if (!is_inside_tree())
  398. return;
  399. _propagate_visibility_changed();
  400. }
  401. bool Spatial::is_visible_in_tree() const {
  402. const Spatial *s = this;
  403. while (s) {
  404. if (!s->data.visible) {
  405. return false;
  406. }
  407. s = s->data.parent;
  408. }
  409. return true;
  410. }
  411. void Spatial::set_visible(bool p_visible) {
  412. if (p_visible)
  413. show();
  414. else
  415. hide();
  416. }
  417. bool Spatial::is_visible() const {
  418. return data.visible;
  419. }
  420. void Spatial::rotate(const Vector3 &p_normal, float p_radians) {
  421. Transform t = get_transform();
  422. t.basis.rotate(p_normal, p_radians);
  423. set_transform(t);
  424. }
  425. void Spatial::rotate_x(float p_radians) {
  426. Transform t = get_transform();
  427. t.basis.rotate(Vector3(1, 0, 0), p_radians);
  428. set_transform(t);
  429. }
  430. void Spatial::rotate_y(float p_radians) {
  431. Transform t = get_transform();
  432. t.basis.rotate(Vector3(0, 1, 0), p_radians);
  433. set_transform(t);
  434. }
  435. void Spatial::rotate_z(float p_radians) {
  436. Transform t = get_transform();
  437. t.basis.rotate(Vector3(0, 0, 1), p_radians);
  438. set_transform(t);
  439. }
  440. void Spatial::translate(const Vector3 &p_offset) {
  441. Transform t = get_transform();
  442. t.translate(p_offset);
  443. set_transform(t);
  444. }
  445. void Spatial::scale(const Vector3 &p_ratio) {
  446. Transform t = get_transform();
  447. t.basis.scale(p_ratio);
  448. set_transform(t);
  449. }
  450. void Spatial::global_rotate(const Vector3 &p_normal, float p_radians) {
  451. Basis rotation(p_normal, p_radians);
  452. Transform t = get_global_transform();
  453. t.basis = rotation * t.basis;
  454. set_global_transform(t);
  455. }
  456. void Spatial::global_translate(const Vector3 &p_offset) {
  457. Transform t = get_global_transform();
  458. t.origin += p_offset;
  459. set_global_transform(t);
  460. }
  461. void Spatial::orthonormalize() {
  462. Transform t = get_transform();
  463. t.orthonormalize();
  464. set_transform(t);
  465. }
  466. void Spatial::set_identity() {
  467. set_transform(Transform());
  468. }
  469. void Spatial::look_at(const Vector3 &p_target, const Vector3 &p_up_normal) {
  470. Transform lookat;
  471. lookat.origin = get_global_transform().origin;
  472. if (lookat.origin == p_target) {
  473. ERR_EXPLAIN("Node origin and target are in the same position, look_at() failed");
  474. ERR_FAIL();
  475. }
  476. if (p_up_normal.cross(p_target - lookat.origin) == Vector3()) {
  477. ERR_EXPLAIN("Up vector and direction between node origin and target are aligned, look_at() failed");
  478. ERR_FAIL();
  479. }
  480. lookat = lookat.looking_at(p_target, p_up_normal);
  481. set_global_transform(lookat);
  482. }
  483. void Spatial::look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up_normal) {
  484. Transform lookat;
  485. lookat.origin = p_pos;
  486. lookat = lookat.looking_at(p_target, p_up_normal);
  487. set_global_transform(lookat);
  488. }
  489. Vector3 Spatial::to_local(Vector3 p_global) const {
  490. return get_global_transform().affine_inverse().xform(p_global);
  491. }
  492. Vector3 Spatial::to_global(Vector3 p_local) const {
  493. return get_global_transform().xform(p_local);
  494. }
  495. void Spatial::set_notify_transform(bool p_enable) {
  496. data.notify_transform = p_enable;
  497. }
  498. bool Spatial::is_transform_notification_enabled() const {
  499. return data.notify_transform;
  500. }
  501. void Spatial::set_notify_local_transform(bool p_enable) {
  502. data.notify_local_transform = p_enable;
  503. }
  504. bool Spatial::is_local_transform_notification_enabled() const {
  505. return data.notify_local_transform;
  506. }
  507. void Spatial::_bind_methods() {
  508. ClassDB::bind_method(D_METHOD("set_transform", "local"), &Spatial::set_transform);
  509. ClassDB::bind_method(D_METHOD("get_transform"), &Spatial::get_transform);
  510. ClassDB::bind_method(D_METHOD("set_translation", "translation"), &Spatial::set_translation);
  511. ClassDB::bind_method(D_METHOD("get_translation"), &Spatial::get_translation);
  512. ClassDB::bind_method(D_METHOD("set_rotation", "rotation_rad"), &Spatial::set_rotation);
  513. ClassDB::bind_method(D_METHOD("get_rotation"), &Spatial::get_rotation);
  514. ClassDB::bind_method(D_METHOD("set_rotation_deg", "rotation_deg"), &Spatial::set_rotation_in_degrees);
  515. ClassDB::bind_method(D_METHOD("get_rotation_deg"), &Spatial::get_rotation_in_degrees);
  516. ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Spatial::set_scale);
  517. ClassDB::bind_method(D_METHOD("get_scale"), &Spatial::get_scale);
  518. ClassDB::bind_method(D_METHOD("set_global_transform", "global"), &Spatial::set_global_transform);
  519. ClassDB::bind_method(D_METHOD("get_global_transform"), &Spatial::get_global_transform);
  520. ClassDB::bind_method(D_METHOD("get_parent_spatial"), &Spatial::get_parent_spatial);
  521. ClassDB::bind_method(D_METHOD("set_ignore_transform_notification", "enabled"), &Spatial::set_ignore_transform_notification);
  522. ClassDB::bind_method(D_METHOD("set_as_toplevel", "enable"), &Spatial::set_as_toplevel);
  523. ClassDB::bind_method(D_METHOD("is_set_as_toplevel"), &Spatial::is_set_as_toplevel);
  524. ClassDB::bind_method(D_METHOD("get_world"), &Spatial::get_world);
  525. // TODO: Obsolete those two methods (old name) properly (GH-4397)
  526. ClassDB::bind_method(D_METHOD("_set_rotation_deg", "rotation_deg"), &Spatial::_set_rotation_deg);
  527. ClassDB::bind_method(D_METHOD("_get_rotation_deg"), &Spatial::_get_rotation_deg);
  528. #ifdef TOOLS_ENABLED
  529. ClassDB::bind_method(D_METHOD("_update_gizmo"), &Spatial::_update_gizmo);
  530. #endif
  531. ClassDB::bind_method(D_METHOD("update_gizmo"), &Spatial::update_gizmo);
  532. ClassDB::bind_method(D_METHOD("set_gizmo", "gizmo"), &Spatial::set_gizmo);
  533. ClassDB::bind_method(D_METHOD("get_gizmo"), &Spatial::get_gizmo);
  534. ClassDB::bind_method(D_METHOD("set_visible", "visible"), &Spatial::set_visible);
  535. ClassDB::bind_method(D_METHOD("is_visible"), &Spatial::is_visible);
  536. ClassDB::bind_method(D_METHOD("is_visible_in_tree"), &Spatial::is_visible_in_tree);
  537. ClassDB::bind_method(D_METHOD("show"), &Spatial::show);
  538. ClassDB::bind_method(D_METHOD("hide"), &Spatial::hide);
  539. ClassDB::bind_method(D_METHOD("set_notify_local_transform", "enable"), &Spatial::set_notify_local_transform);
  540. ClassDB::bind_method(D_METHOD("is_local_transform_notification_enabled"), &Spatial::is_local_transform_notification_enabled);
  541. ClassDB::bind_method(D_METHOD("set_notify_transform", "enable"), &Spatial::set_notify_transform);
  542. ClassDB::bind_method(D_METHOD("is_transform_notification_enabled"), &Spatial::is_transform_notification_enabled);
  543. void rotate(const Vector3 &p_normal, float p_radians);
  544. void rotate_x(float p_radians);
  545. void rotate_y(float p_radians);
  546. void rotate_z(float p_radians);
  547. void translate(const Vector3 &p_offset);
  548. void scale(const Vector3 &p_ratio);
  549. void global_rotate(const Vector3 &p_normal, float p_radians);
  550. void global_translate(const Vector3 &p_offset);
  551. ClassDB::bind_method(D_METHOD("rotate", "normal", "radians"), &Spatial::rotate);
  552. ClassDB::bind_method(D_METHOD("global_rotate", "normal", "radians"), &Spatial::global_rotate);
  553. ClassDB::bind_method(D_METHOD("rotate_x", "radians"), &Spatial::rotate_x);
  554. ClassDB::bind_method(D_METHOD("rotate_y", "radians"), &Spatial::rotate_y);
  555. ClassDB::bind_method(D_METHOD("rotate_z", "radians"), &Spatial::rotate_z);
  556. ClassDB::bind_method(D_METHOD("translate", "offset"), &Spatial::translate);
  557. ClassDB::bind_method(D_METHOD("global_translate", "offset"), &Spatial::global_translate);
  558. ClassDB::bind_method(D_METHOD("orthonormalize"), &Spatial::orthonormalize);
  559. ClassDB::bind_method(D_METHOD("set_identity"), &Spatial::set_identity);
  560. ClassDB::bind_method(D_METHOD("look_at", "target", "up"), &Spatial::look_at);
  561. ClassDB::bind_method(D_METHOD("look_at_from_position", "position", "target", "up"), &Spatial::look_at_from_position);
  562. ClassDB::bind_method(D_METHOD("to_local", "global_point"), &Spatial::to_local);
  563. ClassDB::bind_method(D_METHOD("to_global", "local_point"), &Spatial::to_global);
  564. BIND_CONSTANT(NOTIFICATION_TRANSFORM_CHANGED);
  565. BIND_CONSTANT(NOTIFICATION_ENTER_WORLD);
  566. BIND_CONSTANT(NOTIFICATION_EXIT_WORLD);
  567. BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
  568. //ADD_PROPERTY( PropertyInfo(Variant::TRANSFORM,"transform/global",PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR ), "set_global_transform", "get_global_transform") ;
  569. ADD_GROUP("Transform", "");
  570. ADD_PROPERTYNZ(PropertyInfo(Variant::TRANSFORM, "transform", PROPERTY_HINT_NONE, ""), "set_transform", "get_transform");
  571. ADD_PROPERTYNZ(PropertyInfo(Variant::TRANSFORM, "global_transform", PROPERTY_HINT_NONE, "", 0), "set_global_transform", "get_global_transform");
  572. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "translation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_translation", "get_translation");
  573. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "rotation_deg", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_rotation_deg", "get_rotation_deg");
  574. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "rotation", PROPERTY_HINT_NONE, "", 0), "set_rotation", "get_rotation");
  575. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "scale", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_scale", "get_scale");
  576. ADD_GROUP("Visibility", "");
  577. ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  578. //ADD_PROPERTY( PropertyInfo(Variant::TRANSFORM,"transform/local"), "set_transform", "get_transform") ;
  579. ADD_SIGNAL(MethodInfo("visibility_changed"));
  580. }
  581. Spatial::Spatial()
  582. : xform_change(this) {
  583. data.dirty = DIRTY_NONE;
  584. data.children_lock = 0;
  585. data.ignore_notification = false;
  586. data.toplevel = false;
  587. data.toplevel_active = false;
  588. data.scale = Vector3(1, 1, 1);
  589. data.viewport = NULL;
  590. data.inside_world = false;
  591. data.visible = true;
  592. #ifdef TOOLS_ENABLED
  593. data.gizmo_disabled = false;
  594. data.gizmo_dirty = false;
  595. #endif
  596. data.notify_local_transform = false;
  597. data.notify_transform = false;
  598. data.parent = NULL;
  599. data.C = NULL;
  600. }
  601. Spatial::~Spatial() {
  602. }