spatial.cpp 24 KB

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