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() && get_tree()->is_node_being_edited(this)) {
  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. }
  177. void Spatial::set_transform(const Transform &p_transform) {
  178. data.local_transform = p_transform;
  179. data.dirty |= DIRTY_VECTORS;
  180. _change_notify("translation");
  181. _change_notify("rotation");
  182. _change_notify("rotation_degrees");
  183. _change_notify("scale");
  184. _propagate_transform_changed(this);
  185. if (data.notify_local_transform) {
  186. notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  187. }
  188. }
  189. void Spatial::set_global_transform(const Transform &p_transform) {
  190. Transform xform =
  191. (data.parent && !data.toplevel_active) ?
  192. data.parent->get_global_transform().affine_inverse() * p_transform :
  193. p_transform;
  194. set_transform(xform);
  195. }
  196. Transform Spatial::get_transform() const {
  197. if (data.dirty & DIRTY_LOCAL) {
  198. _update_local_transform();
  199. }
  200. return data.local_transform;
  201. }
  202. Transform Spatial::get_global_transform() const {
  203. ERR_FAIL_COND_V(!is_inside_tree(), Transform());
  204. if (data.dirty & DIRTY_GLOBAL) {
  205. if (data.dirty & DIRTY_LOCAL) {
  206. _update_local_transform();
  207. }
  208. if (data.parent && !data.toplevel_active) {
  209. data.global_transform = data.parent->get_global_transform() * data.local_transform;
  210. } else {
  211. data.global_transform = data.local_transform;
  212. }
  213. if (data.disable_scale) {
  214. data.global_transform.basis.orthonormalize();
  215. }
  216. data.dirty &= ~DIRTY_GLOBAL;
  217. }
  218. return data.global_transform;
  219. }
  220. #ifdef TOOLS_ENABLED
  221. Transform Spatial::get_global_gizmo_transform() const {
  222. return get_global_transform();
  223. }
  224. Transform Spatial::get_local_gizmo_transform() const {
  225. return get_transform();
  226. }
  227. #endif
  228. Spatial *Spatial::get_parent_spatial() const {
  229. return data.parent;
  230. }
  231. Transform Spatial::get_relative_transform(const Node *p_parent) const {
  232. if (p_parent == this)
  233. return Transform();
  234. ERR_FAIL_COND_V(!data.parent, Transform());
  235. if (p_parent == data.parent)
  236. return get_transform();
  237. else
  238. return data.parent->get_relative_transform(p_parent) * get_transform();
  239. }
  240. void Spatial::set_translation(const Vector3 &p_translation) {
  241. data.local_transform.origin = p_translation;
  242. _propagate_transform_changed(this);
  243. if (data.notify_local_transform) {
  244. notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  245. }
  246. }
  247. void Spatial::set_rotation(const Vector3 &p_euler_rad) {
  248. if (data.dirty & DIRTY_VECTORS) {
  249. data.scale = data.local_transform.basis.get_scale();
  250. data.dirty &= ~DIRTY_VECTORS;
  251. }
  252. data.rotation = p_euler_rad;
  253. data.dirty |= DIRTY_LOCAL;
  254. _propagate_transform_changed(this);
  255. if (data.notify_local_transform) {
  256. notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  257. }
  258. }
  259. void Spatial::set_rotation_degrees(const Vector3 &p_euler_deg) {
  260. set_rotation(p_euler_deg * Math_PI / 180.0);
  261. }
  262. void Spatial::set_scale(const Vector3 &p_scale) {
  263. if (data.dirty & DIRTY_VECTORS) {
  264. data.rotation = data.local_transform.basis.get_rotation();
  265. data.dirty &= ~DIRTY_VECTORS;
  266. }
  267. data.scale = p_scale;
  268. data.dirty |= DIRTY_LOCAL;
  269. _propagate_transform_changed(this);
  270. if (data.notify_local_transform) {
  271. notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  272. }
  273. }
  274. Vector3 Spatial::get_translation() const {
  275. return data.local_transform.origin;
  276. }
  277. Vector3 Spatial::get_rotation() const {
  278. if (data.dirty & DIRTY_VECTORS) {
  279. data.scale = data.local_transform.basis.get_scale();
  280. data.rotation = data.local_transform.basis.get_rotation();
  281. data.dirty &= ~DIRTY_VECTORS;
  282. }
  283. return data.rotation;
  284. }
  285. Vector3 Spatial::get_rotation_degrees() const {
  286. return get_rotation() * 180.0 / Math_PI;
  287. }
  288. Vector3 Spatial::get_scale() const {
  289. if (data.dirty & DIRTY_VECTORS) {
  290. data.scale = data.local_transform.basis.get_scale();
  291. data.rotation = data.local_transform.basis.get_rotation();
  292. data.dirty &= ~DIRTY_VECTORS;
  293. }
  294. return data.scale;
  295. }
  296. void Spatial::update_gizmo() {
  297. #ifdef TOOLS_ENABLED
  298. if (!is_inside_world())
  299. return;
  300. if (!data.gizmo.is_valid())
  301. get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, SceneStringNames::get_singleton()->_spatial_editor_group, SceneStringNames::get_singleton()->_request_gizmo, this);
  302. if (!data.gizmo.is_valid())
  303. return;
  304. if (data.gizmo_dirty)
  305. return;
  306. data.gizmo_dirty = true;
  307. MessageQueue::get_singleton()->push_call(this, "_update_gizmo");
  308. #endif
  309. }
  310. void Spatial::set_gizmo(const Ref<SpatialGizmo> &p_gizmo) {
  311. #ifdef TOOLS_ENABLED
  312. if (data.gizmo_disabled)
  313. return;
  314. if (data.gizmo.is_valid() && is_inside_world())
  315. data.gizmo->free();
  316. data.gizmo = p_gizmo;
  317. if (data.gizmo.is_valid() && is_inside_world()) {
  318. data.gizmo->create();
  319. if (is_visible_in_tree()) {
  320. data.gizmo->redraw();
  321. }
  322. data.gizmo->transform();
  323. }
  324. #endif
  325. }
  326. Ref<SpatialGizmo> Spatial::get_gizmo() const {
  327. #ifdef TOOLS_ENABLED
  328. return data.gizmo;
  329. #else
  330. return Ref<SpatialGizmo>();
  331. #endif
  332. }
  333. void Spatial::_update_gizmo() {
  334. #ifdef TOOLS_ENABLED
  335. if (!is_inside_world())
  336. return;
  337. data.gizmo_dirty = false;
  338. if (data.gizmo.is_valid()) {
  339. if (is_visible_in_tree())
  340. data.gizmo->redraw();
  341. else
  342. data.gizmo->clear();
  343. }
  344. #endif
  345. }
  346. #ifdef TOOLS_ENABLED
  347. void Spatial::set_disable_gizmo(bool p_enabled) {
  348. data.gizmo_disabled = p_enabled;
  349. if (!p_enabled && data.gizmo.is_valid())
  350. data.gizmo = Ref<SpatialGizmo>();
  351. }
  352. #endif
  353. void Spatial::set_disable_scale(bool p_enabled) {
  354. data.disable_scale = p_enabled;
  355. }
  356. bool Spatial::is_scale_disabled() const {
  357. return data.disable_scale;
  358. }
  359. void Spatial::set_as_toplevel(bool p_enabled) {
  360. if (data.toplevel == p_enabled)
  361. return;
  362. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
  363. if (p_enabled)
  364. set_transform(get_global_transform());
  365. else if (data.parent)
  366. set_transform(data.parent->get_global_transform().affine_inverse() * get_global_transform());
  367. data.toplevel = p_enabled;
  368. data.toplevel_active = p_enabled;
  369. } else {
  370. data.toplevel = p_enabled;
  371. }
  372. }
  373. bool Spatial::is_set_as_toplevel() const {
  374. return data.toplevel;
  375. }
  376. Ref<World> Spatial::get_world() const {
  377. ERR_FAIL_COND_V(!is_inside_world(), Ref<World>());
  378. return data.viewport->find_world();
  379. }
  380. void Spatial::_propagate_visibility_changed() {
  381. notification(NOTIFICATION_VISIBILITY_CHANGED);
  382. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  383. _change_notify("visible");
  384. #ifdef TOOLS_ENABLED
  385. if (data.gizmo.is_valid())
  386. _update_gizmo();
  387. #endif
  388. for (List<Spatial *>::Element *E = data.children.front(); E; E = E->next()) {
  389. Spatial *c = E->get();
  390. if (!c || !c->data.visible)
  391. continue;
  392. c->_propagate_visibility_changed();
  393. }
  394. }
  395. void Spatial::show() {
  396. if (data.visible)
  397. return;
  398. data.visible = true;
  399. if (!is_inside_tree())
  400. return;
  401. _propagate_visibility_changed();
  402. }
  403. void Spatial::hide() {
  404. if (!data.visible)
  405. return;
  406. data.visible = false;
  407. if (!is_inside_tree())
  408. return;
  409. _propagate_visibility_changed();
  410. }
  411. bool Spatial::is_visible_in_tree() const {
  412. const Spatial *s = this;
  413. while (s) {
  414. if (!s->data.visible) {
  415. return false;
  416. }
  417. s = s->data.parent;
  418. }
  419. return true;
  420. }
  421. void Spatial::set_visible(bool p_visible) {
  422. if (p_visible)
  423. show();
  424. else
  425. hide();
  426. }
  427. bool Spatial::is_visible() const {
  428. return data.visible;
  429. }
  430. void Spatial::rotate_object_local(const Vector3 &p_axis, float p_angle) {
  431. Transform t = get_transform();
  432. t.basis.rotate_local(p_axis, p_angle);
  433. set_transform(t);
  434. }
  435. void Spatial::rotate(const Vector3 &p_axis, float p_angle) {
  436. Transform t = get_transform();
  437. t.basis.rotate(p_axis, p_angle);
  438. set_transform(t);
  439. }
  440. void Spatial::rotate_x(float p_angle) {
  441. Transform t = get_transform();
  442. t.basis.rotate(Vector3(1, 0, 0), p_angle);
  443. set_transform(t);
  444. }
  445. void Spatial::rotate_y(float p_angle) {
  446. Transform t = get_transform();
  447. t.basis.rotate(Vector3(0, 1, 0), p_angle);
  448. set_transform(t);
  449. }
  450. void Spatial::rotate_z(float p_angle) {
  451. Transform t = get_transform();
  452. t.basis.rotate(Vector3(0, 0, 1), p_angle);
  453. set_transform(t);
  454. }
  455. void Spatial::translate(const Vector3 &p_offset) {
  456. Transform t = get_transform();
  457. t.translate(p_offset);
  458. set_transform(t);
  459. }
  460. void Spatial::translate_object_local(const Vector3 &p_offset) {
  461. Transform t = get_transform();
  462. Transform s;
  463. s.translate(p_offset);
  464. set_transform(t * s);
  465. }
  466. void Spatial::scale(const Vector3 &p_ratio) {
  467. Transform t = get_transform();
  468. t.basis.scale(p_ratio);
  469. set_transform(t);
  470. }
  471. void Spatial::scale_object_local(const Vector3 &p_scale) {
  472. Transform t = get_transform();
  473. t.basis.scale_local(p_scale);
  474. set_transform(t);
  475. }
  476. void Spatial::global_rotate(const Vector3 &p_axis, float p_angle) {
  477. Transform t = get_global_transform();
  478. t.basis.rotate(p_axis, p_angle);
  479. set_global_transform(t);
  480. }
  481. void Spatial::global_scale(const Vector3 &p_scale) {
  482. Transform t = get_global_transform();
  483. t.basis.scale(p_scale);
  484. set_global_transform(t);
  485. }
  486. void Spatial::global_translate(const Vector3 &p_offset) {
  487. Transform t = get_global_transform();
  488. t.origin += p_offset;
  489. set_global_transform(t);
  490. }
  491. void Spatial::orthonormalize() {
  492. Transform t = get_transform();
  493. t.orthonormalize();
  494. set_transform(t);
  495. }
  496. void Spatial::set_identity() {
  497. set_transform(Transform());
  498. }
  499. void Spatial::look_at(const Vector3 &p_target, const Vector3 &p_up) {
  500. Transform lookat;
  501. lookat.origin = get_global_transform().origin;
  502. if (lookat.origin == p_target) {
  503. ERR_EXPLAIN("Node origin and target are in the same position, look_at() failed");
  504. ERR_FAIL();
  505. }
  506. if (p_up.cross(p_target - lookat.origin) == Vector3()) {
  507. ERR_EXPLAIN("Up vector and direction between node origin and target are aligned, look_at() failed");
  508. ERR_FAIL();
  509. }
  510. lookat = lookat.looking_at(p_target, p_up);
  511. set_global_transform(lookat);
  512. }
  513. void Spatial::look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up) {
  514. Transform lookat;
  515. lookat.origin = p_pos;
  516. lookat = lookat.looking_at(p_target, p_up);
  517. set_global_transform(lookat);
  518. }
  519. Vector3 Spatial::to_local(Vector3 p_global) const {
  520. return get_global_transform().affine_inverse().xform(p_global);
  521. }
  522. Vector3 Spatial::to_global(Vector3 p_local) const {
  523. return get_global_transform().xform(p_local);
  524. }
  525. void Spatial::set_notify_transform(bool p_enable) {
  526. data.notify_transform = p_enable;
  527. }
  528. bool Spatial::is_transform_notification_enabled() const {
  529. return data.notify_transform;
  530. }
  531. void Spatial::set_notify_local_transform(bool p_enable) {
  532. data.notify_local_transform = p_enable;
  533. }
  534. bool Spatial::is_local_transform_notification_enabled() const {
  535. return data.notify_local_transform;
  536. }
  537. void Spatial::force_update_transform() {
  538. ERR_FAIL_COND(!is_inside_tree());
  539. if (!xform_change.in_list()) {
  540. return; //nothing to update
  541. }
  542. get_tree()->xform_change_list.remove(&xform_change);
  543. notification(NOTIFICATION_TRANSFORM_CHANGED);
  544. }
  545. void Spatial::_bind_methods() {
  546. ClassDB::bind_method(D_METHOD("set_transform", "local"), &Spatial::set_transform);
  547. ClassDB::bind_method(D_METHOD("get_transform"), &Spatial::get_transform);
  548. ClassDB::bind_method(D_METHOD("set_translation", "translation"), &Spatial::set_translation);
  549. ClassDB::bind_method(D_METHOD("get_translation"), &Spatial::get_translation);
  550. ClassDB::bind_method(D_METHOD("set_rotation", "euler"), &Spatial::set_rotation);
  551. ClassDB::bind_method(D_METHOD("get_rotation"), &Spatial::get_rotation);
  552. ClassDB::bind_method(D_METHOD("set_rotation_degrees", "euler_degrees"), &Spatial::set_rotation_degrees);
  553. ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Spatial::get_rotation_degrees);
  554. ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Spatial::set_scale);
  555. ClassDB::bind_method(D_METHOD("get_scale"), &Spatial::get_scale);
  556. ClassDB::bind_method(D_METHOD("set_global_transform", "global"), &Spatial::set_global_transform);
  557. ClassDB::bind_method(D_METHOD("get_global_transform"), &Spatial::get_global_transform);
  558. ClassDB::bind_method(D_METHOD("get_parent_spatial"), &Spatial::get_parent_spatial);
  559. ClassDB::bind_method(D_METHOD("set_ignore_transform_notification", "enabled"), &Spatial::set_ignore_transform_notification);
  560. ClassDB::bind_method(D_METHOD("set_as_toplevel", "enable"), &Spatial::set_as_toplevel);
  561. ClassDB::bind_method(D_METHOD("is_set_as_toplevel"), &Spatial::is_set_as_toplevel);
  562. ClassDB::bind_method(D_METHOD("set_disable_scale", "disable"), &Spatial::set_disable_scale);
  563. ClassDB::bind_method(D_METHOD("is_scale_disabled"), &Spatial::is_scale_disabled);
  564. ClassDB::bind_method(D_METHOD("get_world"), &Spatial::get_world);
  565. ClassDB::bind_method(D_METHOD("force_update_transform"), &Spatial::force_update_transform);
  566. ClassDB::bind_method(D_METHOD("_update_gizmo"), &Spatial::_update_gizmo);
  567. ClassDB::bind_method(D_METHOD("update_gizmo"), &Spatial::update_gizmo);
  568. ClassDB::bind_method(D_METHOD("set_gizmo", "gizmo"), &Spatial::set_gizmo);
  569. ClassDB::bind_method(D_METHOD("get_gizmo"), &Spatial::get_gizmo);
  570. ClassDB::bind_method(D_METHOD("set_visible", "visible"), &Spatial::set_visible);
  571. ClassDB::bind_method(D_METHOD("is_visible"), &Spatial::is_visible);
  572. ClassDB::bind_method(D_METHOD("is_visible_in_tree"), &Spatial::is_visible_in_tree);
  573. ClassDB::bind_method(D_METHOD("show"), &Spatial::show);
  574. ClassDB::bind_method(D_METHOD("hide"), &Spatial::hide);
  575. ClassDB::bind_method(D_METHOD("set_notify_local_transform", "enable"), &Spatial::set_notify_local_transform);
  576. ClassDB::bind_method(D_METHOD("is_local_transform_notification_enabled"), &Spatial::is_local_transform_notification_enabled);
  577. ClassDB::bind_method(D_METHOD("set_notify_transform", "enable"), &Spatial::set_notify_transform);
  578. ClassDB::bind_method(D_METHOD("is_transform_notification_enabled"), &Spatial::is_transform_notification_enabled);
  579. ClassDB::bind_method(D_METHOD("rotate", "axis", "angle"), &Spatial::rotate);
  580. ClassDB::bind_method(D_METHOD("global_rotate", "axis", "angle"), &Spatial::global_rotate);
  581. ClassDB::bind_method(D_METHOD("global_scale", "scale"), &Spatial::global_scale);
  582. ClassDB::bind_method(D_METHOD("global_translate", "offset"), &Spatial::global_translate);
  583. ClassDB::bind_method(D_METHOD("rotate_object_local", "axis", "angle"), &Spatial::rotate_object_local);
  584. ClassDB::bind_method(D_METHOD("scale_object_local", "scale"), &Spatial::scale_object_local);
  585. ClassDB::bind_method(D_METHOD("translate_object_local", "offset"), &Spatial::translate_object_local);
  586. ClassDB::bind_method(D_METHOD("rotate_x", "angle"), &Spatial::rotate_x);
  587. ClassDB::bind_method(D_METHOD("rotate_y", "angle"), &Spatial::rotate_y);
  588. ClassDB::bind_method(D_METHOD("rotate_z", "angle"), &Spatial::rotate_z);
  589. ClassDB::bind_method(D_METHOD("translate", "offset"), &Spatial::translate);
  590. ClassDB::bind_method(D_METHOD("orthonormalize"), &Spatial::orthonormalize);
  591. ClassDB::bind_method(D_METHOD("set_identity"), &Spatial::set_identity);
  592. ClassDB::bind_method(D_METHOD("look_at", "target", "up"), &Spatial::look_at);
  593. ClassDB::bind_method(D_METHOD("look_at_from_position", "position", "target", "up"), &Spatial::look_at_from_position);
  594. ClassDB::bind_method(D_METHOD("to_local", "global_point"), &Spatial::to_local);
  595. ClassDB::bind_method(D_METHOD("to_global", "local_point"), &Spatial::to_global);
  596. BIND_CONSTANT(NOTIFICATION_TRANSFORM_CHANGED);
  597. BIND_CONSTANT(NOTIFICATION_ENTER_WORLD);
  598. BIND_CONSTANT(NOTIFICATION_EXIT_WORLD);
  599. BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
  600. //ADD_PROPERTY( PropertyInfo(Variant::TRANSFORM,"transform/global",PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR ), "set_global_transform", "get_global_transform") ;
  601. ADD_GROUP("Transform", "");
  602. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "global_transform", PROPERTY_HINT_NONE, "", 0), "set_global_transform", "get_global_transform");
  603. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "translation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_translation", "get_translation");
  604. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_rotation_degrees", "get_rotation_degrees");
  605. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "rotation", PROPERTY_HINT_NONE, "", 0), "set_rotation", "get_rotation");
  606. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "scale", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_scale", "get_scale");
  607. ADD_GROUP("Matrix", "");
  608. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "transform", PROPERTY_HINT_NONE, ""), "set_transform", "get_transform");
  609. ADD_GROUP("Visibility", "");
  610. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  611. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gizmo", PROPERTY_HINT_RESOURCE_TYPE, "SpatialGizmo", 0), "set_gizmo", "get_gizmo");
  612. ADD_SIGNAL(MethodInfo("visibility_changed"));
  613. }
  614. Spatial::Spatial() :
  615. xform_change(this) {
  616. data.dirty = DIRTY_NONE;
  617. data.children_lock = 0;
  618. data.ignore_notification = false;
  619. data.toplevel = false;
  620. data.toplevel_active = false;
  621. data.scale = Vector3(1, 1, 1);
  622. data.viewport = NULL;
  623. data.inside_world = false;
  624. data.visible = true;
  625. data.disable_scale = false;
  626. #ifdef TOOLS_ENABLED
  627. data.gizmo_disabled = false;
  628. data.gizmo_dirty = false;
  629. #endif
  630. data.notify_local_transform = false;
  631. data.notify_transform = false;
  632. data.parent = NULL;
  633. data.C = NULL;
  634. }
  635. Spatial::~Spatial() {
  636. }