spatial.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*************************************************************************/
  2. /* spatial.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "spatial.h"
  30. #include "scene/main/viewport.h"
  31. #include "message_queue.h"
  32. #include "scene/scene_string_names.h"
  33. /*
  34. possible algorithms:
  35. Algorithm 1: (current)
  36. definition of invalidation: global is invalid
  37. 1) If a node sets a LOCAL, it produces an invalidation of everything above
  38. a) If above is invalid, don't keep invalidating upwards
  39. 2) If a node sets a GLOBAL, it is converted to LOCAL (and forces validation of everything pending below)
  40. drawback: setting/reading globals is useful and used very very often, and using affine inverses is slow
  41. ---
  42. Algorithm 2: (no longer current)
  43. definition of invalidation: NONE dirty, LOCAL dirty, GLOBAL dirty
  44. 1) If a node sets a LOCAL, it must climb the tree and set it as GLOBAL dirty
  45. a) marking GLOBALs as dirty up all the tree must be done always
  46. 2) If a node sets a GLOBAL, it marks local as dirty, and that's all?
  47. //is clearing the dirty state correct in this case?
  48. drawback: setting a local down the tree forces many tree walks often
  49. --
  50. future: no idea
  51. */
  52. SpatialGizmo::SpatialGizmo() {
  53. }
  54. void Spatial::_notify_dirty() {
  55. if (!data.ignore_notification && !xform_change.in_list()) {
  56. get_tree()->xform_change_list.add(&xform_change);
  57. }
  58. }
  59. void Spatial::_update_local_transform() const {
  60. data.local_transform.basis.set_euler(data.rotation);
  61. data.local_transform.basis.scale(data.scale);
  62. data.dirty&=~DIRTY_LOCAL;
  63. }
  64. void Spatial::_propagate_transform_changed(Spatial *p_origin) {
  65. if (!is_inside_tree()) {
  66. return;
  67. }
  68. // if (data.dirty&DIRTY_GLOBAL)
  69. // return; //already dirty
  70. data.children_lock++;
  71. for (List<Spatial*>::Element *E=data.children.front();E;E=E->next()) {
  72. if (E->get()->data.toplevel_active)
  73. continue; //don't propagate to a toplevel
  74. E->get()->_propagate_transform_changed(p_origin);
  75. }
  76. if (!data.ignore_notification && !xform_change.in_list()) {
  77. get_tree()->xform_change_list.add(&xform_change);
  78. }
  79. data.dirty|=DIRTY_GLOBAL;
  80. data.children_lock--;
  81. }
  82. void Spatial::_notification(int p_what) {
  83. switch(p_what) {
  84. case NOTIFICATION_ENTER_TREE: {
  85. Node *p = get_parent();
  86. if (p)
  87. data.parent=p->cast_to<Spatial>();
  88. if (data.parent)
  89. data.C=data.parent->data.children.push_back(this);
  90. else
  91. data.C=NULL;
  92. if (data.toplevel && !get_tree()->is_editor_hint()) {
  93. if (data.parent) {
  94. data.local_transform = data.parent->get_global_transform() * get_transform();
  95. data.dirty=DIRTY_VECTORS; //global is always dirty upon entering a scene
  96. }
  97. data.toplevel_active=true;
  98. }
  99. data.dirty|=DIRTY_GLOBAL; //global is always dirty upon entering a scene
  100. _notify_dirty();
  101. notification(NOTIFICATION_ENTER_WORLD);
  102. } break;
  103. case NOTIFICATION_EXIT_TREE: {
  104. notification(NOTIFICATION_EXIT_WORLD,true);
  105. if (xform_change.in_list())
  106. get_tree()->xform_change_list.remove(&xform_change);
  107. if (data.C)
  108. data.parent->data.children.erase(data.C);
  109. data.parent=NULL;
  110. data.C=NULL;
  111. data.toplevel_active=false;
  112. } break;
  113. case NOTIFICATION_ENTER_WORLD: {
  114. data.inside_world=true;
  115. data.viewport=NULL;
  116. Node *parent = get_parent();
  117. while(parent && !data.viewport) {
  118. data.viewport=parent->cast_to<Viewport>();
  119. parent=parent->get_parent();
  120. }
  121. ERR_FAIL_COND(!data.viewport);
  122. if (get_script_instance()) {
  123. Variant::CallError err;
  124. get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_enter_world,NULL,0);
  125. }
  126. #ifdef TOOLS_ENABLED
  127. if (get_tree()->is_editor_hint()) {
  128. // get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,SceneStringNames::get_singleton()->_spatial_editor_group,SceneStringNames::get_singleton()->_request_gizmo,this);
  129. get_tree()->call_group(0,SceneStringNames::get_singleton()->_spatial_editor_group,SceneStringNames::get_singleton()->_request_gizmo,this);
  130. if (!data.gizmo_disabled) {
  131. if (data.gizmo.is_valid())
  132. data.gizmo->create();
  133. }
  134. }
  135. #endif
  136. } break;
  137. case NOTIFICATION_EXIT_WORLD: {
  138. #ifdef TOOLS_ENABLED
  139. if (data.gizmo.is_valid()) {
  140. data.gizmo->free();
  141. }
  142. #endif
  143. if (get_script_instance()) {
  144. Variant::CallError err;
  145. get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_exit_world,NULL,0);
  146. }
  147. data.viewport=NULL;
  148. data.inside_world=false;
  149. } break;
  150. case NOTIFICATION_TRANSFORM_CHANGED: {
  151. #ifdef TOOLS_ENABLED
  152. if (data.gizmo.is_valid()) {
  153. data.gizmo->transform();
  154. }
  155. #endif
  156. } break;
  157. default: {}
  158. }
  159. }
  160. void Spatial::set_transform(const Transform& p_transform) {
  161. data.local_transform=p_transform;
  162. data.dirty|=DIRTY_VECTORS;
  163. _change_notify("transform/translation");
  164. _change_notify("transform/rotation");
  165. _change_notify("transform/scale");
  166. _propagate_transform_changed(this);
  167. if (data.notify_local_transform) {
  168. notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  169. }
  170. }
  171. void Spatial::set_global_transform(const Transform& p_transform) {
  172. Transform xform =
  173. (data.parent && !data.toplevel_active) ?
  174. data.parent->get_global_transform().affine_inverse() * p_transform :
  175. p_transform;
  176. set_transform(xform);
  177. }
  178. Transform Spatial::get_transform() const {
  179. if (data.dirty & DIRTY_LOCAL) {
  180. _update_local_transform();
  181. }
  182. return data.local_transform;
  183. }
  184. Transform Spatial::get_global_transform() const {
  185. ERR_FAIL_COND_V(!is_inside_tree(), Transform());
  186. if (data.dirty & DIRTY_GLOBAL) {
  187. if (data.dirty & DIRTY_LOCAL) {
  188. _update_local_transform();
  189. }
  190. if (data.parent && !data.toplevel_active) {
  191. data.global_transform=data.parent->get_global_transform() * data.local_transform;
  192. } else {
  193. data.global_transform=data.local_transform;
  194. }
  195. data.dirty&=~DIRTY_GLOBAL;
  196. }
  197. return data.global_transform;
  198. }
  199. #if 0
  200. void Spatial::add_child_notify(Node *p_child) {
  201. /*
  202. Spatial *s=p_child->cast_to<Spatial>();
  203. if (!s)
  204. return;
  205. ERR_FAIL_COND(data.children_lock>0);
  206. s->data.dirty=DIRTY_GLOBAL; // don't allow global transform to be valid
  207. s->data.parent=this;
  208. data.children.push_back(s);
  209. s->data.C=data.children.back();
  210. */
  211. }
  212. void Spatial::remove_child_notify(Node *p_child) {
  213. /*
  214. Spatial *s=p_child->cast_to<Spatial>();
  215. if (!s)
  216. return;
  217. ERR_FAIL_COND(data.children_lock>0);
  218. if (s->data.C)
  219. data.children.erase(s->data.C);
  220. s->data.parent=NULL;
  221. s->data.C=NULL;
  222. */
  223. }
  224. #endif
  225. Spatial *Spatial::get_parent_spatial() const {
  226. return data.parent;
  227. }
  228. Transform Spatial::get_relative_transform(const Node *p_parent) const {
  229. if (p_parent==this)
  230. return Transform();
  231. ERR_FAIL_COND_V(!data.parent,Transform());
  232. if (p_parent==data.parent)
  233. return get_transform();
  234. else
  235. return data.parent->get_relative_transform(p_parent) * get_transform();
  236. }
  237. void Spatial::set_translation(const Vector3& p_translation) {
  238. data.local_transform.origin=p_translation;
  239. _propagate_transform_changed(this);
  240. if (data.notify_local_transform) {
  241. notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  242. }
  243. }
  244. void Spatial::set_rotation(const Vector3& p_euler_rad){
  245. if (data.dirty&DIRTY_VECTORS) {
  246. data.scale=data.local_transform.basis.get_scale();
  247. data.dirty&=~DIRTY_VECTORS;
  248. }
  249. data.rotation=p_euler_rad;
  250. data.dirty|=DIRTY_LOCAL;
  251. _propagate_transform_changed(this);
  252. if (data.notify_local_transform) {
  253. notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  254. }
  255. }
  256. void Spatial::set_rotation_deg(const Vector3& p_euler_deg) {
  257. set_rotation(p_euler_deg * Math_PI / 180.0);
  258. }
  259. void Spatial::_set_rotation_deg(const Vector3& p_euler_deg) {
  260. 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.");
  261. set_rotation_deg(p_euler_deg);
  262. }
  263. void Spatial::set_scale(const Vector3& p_scale){
  264. if (data.dirty&DIRTY_VECTORS) {
  265. data.rotation=data.local_transform.basis.get_euler();
  266. data.dirty&=~DIRTY_VECTORS;
  267. }
  268. data.scale=p_scale;
  269. data.dirty|=DIRTY_LOCAL;
  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_euler();
  282. data.dirty&=~DIRTY_VECTORS;
  283. }
  284. return data.rotation;
  285. }
  286. Vector3 Spatial::get_rotation_deg() const {
  287. return get_rotation() * 180.0 / Math_PI;
  288. }
  289. // Kept for compatibility after rename to set_rotd.
  290. // Could be removed after a couple releases.
  291. Vector3 Spatial::_get_rotation_deg() const {
  292. 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.");
  293. return get_rotation_deg();
  294. }
  295. Vector3 Spatial::get_scale() const{
  296. if (data.dirty&DIRTY_VECTORS) {
  297. data.scale=data.local_transform.basis.get_scale();
  298. data.rotation=data.local_transform.basis.get_euler();
  299. data.dirty&=~DIRTY_VECTORS;
  300. }
  301. return data.scale;
  302. }
  303. void Spatial::update_gizmo() {
  304. #ifdef TOOLS_ENABLED
  305. if (!is_inside_world())
  306. return;
  307. if (!data.gizmo.is_valid())
  308. return;
  309. if (data.gizmo_dirty)
  310. return;
  311. data.gizmo_dirty=true;
  312. MessageQueue::get_singleton()->push_call(this,"_update_gizmo");
  313. #endif
  314. }
  315. void Spatial::set_gizmo(const Ref<SpatialGizmo>& p_gizmo) {
  316. #ifdef TOOLS_ENABLED
  317. if (data.gizmo_disabled)
  318. return;
  319. if (data.gizmo.is_valid() && is_inside_world())
  320. data.gizmo->free();
  321. data.gizmo=p_gizmo;
  322. if (data.gizmo.is_valid() && is_inside_world()) {
  323. data.gizmo->create();
  324. data.gizmo->redraw();
  325. data.gizmo->transform();
  326. }
  327. #endif
  328. }
  329. Ref<SpatialGizmo> Spatial::get_gizmo() const {
  330. #ifdef TOOLS_ENABLED
  331. return data.gizmo;
  332. #else
  333. return Ref<SpatialGizmo>();
  334. #endif
  335. }
  336. #ifdef TOOLS_ENABLED
  337. void Spatial::_update_gizmo() {
  338. data.gizmo_dirty=false;
  339. if (data.gizmo.is_valid()) {
  340. if (is_visible())
  341. data.gizmo->redraw();
  342. else
  343. data.gizmo->clear();
  344. }
  345. }
  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_as_toplevel(bool p_enabled) {
  353. if (data.toplevel==p_enabled)
  354. return;
  355. if (is_inside_tree() && !get_tree()->is_editor_hint()) {
  356. if (p_enabled)
  357. set_transform(get_global_transform());
  358. else if (data.parent)
  359. set_transform(data.parent->get_global_transform().affine_inverse() * get_global_transform());
  360. data.toplevel=p_enabled;
  361. data.toplevel_active=p_enabled;
  362. } else {
  363. data.toplevel=p_enabled;
  364. }
  365. }
  366. bool Spatial::is_set_as_toplevel() const{
  367. return data.toplevel;
  368. }
  369. Ref<World> Spatial::get_world() const {
  370. ERR_FAIL_COND_V(!is_inside_world(),Ref<World>());
  371. return data.viewport->find_world();
  372. }
  373. #ifdef TOOLS_ENABLED
  374. void Spatial::set_import_transform(const Transform& p_transform) {
  375. data.import_transform=p_transform;
  376. }
  377. Transform Spatial::get_import_transform() const {
  378. return data.import_transform;
  379. }
  380. #endif
  381. void Spatial::_propagate_visibility_changed() {
  382. notification(NOTIFICATION_VISIBILITY_CHANGED);
  383. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  384. _change_notify("visibility/visible");
  385. #ifdef TOOLS_ENABLED
  386. if (data.gizmo.is_valid())
  387. _update_gizmo();
  388. #endif
  389. for (List<Spatial*>::Element*E=data.children.front();E;E=E->next()) {
  390. Spatial *c=E->get();
  391. if (!c || !c->data.visible)
  392. continue;
  393. c->_propagate_visibility_changed();
  394. }
  395. }
  396. void Spatial::show() {
  397. if (data.visible)
  398. return;
  399. data.visible=true;
  400. if (!is_inside_tree())
  401. return;
  402. if (!data.parent || is_visible()) {
  403. _propagate_visibility_changed();
  404. }
  405. }
  406. void Spatial::hide(){
  407. if (!data.visible)
  408. return;
  409. bool was_visible = is_visible();
  410. data.visible=false;
  411. if (!data.parent || was_visible) {
  412. _propagate_visibility_changed();
  413. }
  414. }
  415. bool Spatial::is_visible() 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. bool Spatial::is_hidden() const{
  426. return !data.visible;
  427. }
  428. void Spatial::set_hidden(bool p_hidden) {
  429. if (data.visible != p_hidden) {
  430. return;
  431. }
  432. _set_visible_(!p_hidden);
  433. }
  434. void Spatial::_set_visible_(bool p_visible) {
  435. if (p_visible)
  436. show();
  437. else
  438. hide();
  439. }
  440. bool Spatial::_is_visible_() const {
  441. return !is_hidden();
  442. }
  443. void Spatial::rotate(const Vector3& p_normal,float p_radians) {
  444. Transform t =get_transform();
  445. t.basis.rotate(p_normal,p_radians);
  446. set_transform(t);
  447. }
  448. void Spatial::rotate_x(float p_radians) {
  449. Transform t =get_transform();
  450. t.basis.rotate(Vector3(1,0,0),p_radians);
  451. set_transform(t);
  452. }
  453. void Spatial::rotate_y(float p_radians){
  454. Transform t =get_transform();
  455. t.basis.rotate(Vector3(0,1,0),p_radians);
  456. set_transform(t);
  457. }
  458. void Spatial::rotate_z(float p_radians){
  459. Transform t =get_transform();
  460. t.basis.rotate(Vector3(0,0,1),p_radians);
  461. set_transform(t);
  462. }
  463. void Spatial::translate(const Vector3& p_offset){
  464. Transform t =get_transform();
  465. t.translate(p_offset);
  466. set_transform(t);
  467. }
  468. void Spatial::scale(const Vector3& p_ratio){
  469. Transform t =get_transform();
  470. t.basis.scale(p_ratio);
  471. set_transform(t);
  472. }
  473. void Spatial::global_rotate(const Vector3& p_normal,float p_radians){
  474. Matrix3 rotation(p_normal,p_radians);
  475. Transform t = get_global_transform();
  476. t.basis= rotation * t.basis;
  477. set_global_transform(t);
  478. }
  479. void Spatial::global_translate(const Vector3& p_offset){
  480. Transform t = get_global_transform();
  481. t.origin+=p_offset;
  482. set_global_transform(t);
  483. }
  484. void Spatial::orthonormalize() {
  485. Transform t = get_transform();
  486. t.orthonormalize();
  487. set_transform(t);
  488. }
  489. void Spatial::set_identity() {
  490. set_transform(Transform());
  491. }
  492. void Spatial::look_at(const Vector3& p_target, const Vector3& p_up_normal) {
  493. Transform lookat;
  494. lookat.origin=get_global_transform().origin;
  495. if (lookat.origin==p_target) {
  496. ERR_EXPLAIN("Node origin and target are in the same position, look_at() failed");
  497. ERR_FAIL();
  498. }
  499. if (p_up_normal.cross(p_target-lookat.origin)==Vector3()) {
  500. ERR_EXPLAIN("Up vector and direction between node origin and target are aligned, look_at() failed");
  501. ERR_FAIL();
  502. }
  503. lookat=lookat.looking_at(p_target,p_up_normal);
  504. set_global_transform(lookat);
  505. }
  506. void Spatial::look_at_from_pos(const Vector3& p_pos,const Vector3& p_target, const Vector3& p_up_normal) {
  507. Transform lookat;
  508. lookat.origin=p_pos;
  509. lookat=lookat.looking_at(p_target,p_up_normal);
  510. set_global_transform(lookat);
  511. }
  512. void Spatial::set_notify_local_transform(bool p_enable) {
  513. data.notify_local_transform=p_enable;
  514. }
  515. bool Spatial::is_local_transform_notification_enabled() const {
  516. return data.notify_local_transform;
  517. }
  518. void Spatial::_bind_methods() {
  519. ObjectTypeDB::bind_method(_MD("set_transform","local"), &Spatial::set_transform);
  520. ObjectTypeDB::bind_method(_MD("get_transform"), &Spatial::get_transform);
  521. ObjectTypeDB::bind_method(_MD("set_translation","translation"), &Spatial::set_translation);
  522. ObjectTypeDB::bind_method(_MD("get_translation"), &Spatial::get_translation);
  523. ObjectTypeDB::bind_method(_MD("set_rotation","rotation_rad"), &Spatial::set_rotation);
  524. ObjectTypeDB::bind_method(_MD("get_rotation"), &Spatial::get_rotation);
  525. ObjectTypeDB::bind_method(_MD("set_rotation_deg","rotation_deg"), &Spatial::set_rotation_deg);
  526. ObjectTypeDB::bind_method(_MD("get_rotation_deg"), &Spatial::get_rotation_deg);
  527. ObjectTypeDB::bind_method(_MD("set_scale","scale"), &Spatial::set_scale);
  528. ObjectTypeDB::bind_method(_MD("get_scale"), &Spatial::get_scale);
  529. ObjectTypeDB::bind_method(_MD("set_global_transform","global"), &Spatial::set_global_transform);
  530. ObjectTypeDB::bind_method(_MD("get_global_transform"), &Spatial::get_global_transform);
  531. ObjectTypeDB::bind_method(_MD("get_parent_spatial"), &Spatial::get_parent_spatial);
  532. ObjectTypeDB::bind_method(_MD("set_ignore_transform_notification","enabled"), &Spatial::set_ignore_transform_notification);
  533. ObjectTypeDB::bind_method(_MD("set_as_toplevel","enable"), &Spatial::set_as_toplevel);
  534. ObjectTypeDB::bind_method(_MD("is_set_as_toplevel"), &Spatial::is_set_as_toplevel);
  535. ObjectTypeDB::bind_method(_MD("get_world:World"), &Spatial::get_world);
  536. // TODO: Obsolete those two methods (old name) properly (GH-4397)
  537. ObjectTypeDB::bind_method(_MD("_set_rotation_deg","rotation_deg"), &Spatial::_set_rotation_deg);
  538. ObjectTypeDB::bind_method(_MD("_get_rotation_deg"), &Spatial::_get_rotation_deg);
  539. #ifdef TOOLS_ENABLED
  540. ObjectTypeDB::bind_method(_MD("_update_gizmo"), &Spatial::_update_gizmo);
  541. ObjectTypeDB::bind_method(_MD("_set_import_transform"), &Spatial::set_import_transform);
  542. ObjectTypeDB::bind_method(_MD("_get_import_transform"), &Spatial::get_import_transform);
  543. ADD_PROPERTY( PropertyInfo(Variant::TRANSFORM,"_import_transform",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("_set_import_transform"),_SCS("_get_import_transform"));
  544. #endif
  545. ObjectTypeDB::bind_method(_MD("update_gizmo"), &Spatial::update_gizmo);
  546. ObjectTypeDB::bind_method(_MD("set_gizmo","gizmo:SpatialGizmo"), &Spatial::set_gizmo);
  547. ObjectTypeDB::bind_method(_MD("get_gizmo:SpatialGizmo"), &Spatial::get_gizmo);
  548. ObjectTypeDB::bind_method(_MD("show"), &Spatial::show);
  549. ObjectTypeDB::bind_method(_MD("hide"), &Spatial::hide);
  550. ObjectTypeDB::bind_method(_MD("is_visible"), &Spatial::is_visible);
  551. ObjectTypeDB::bind_method(_MD("is_hidden"), &Spatial::is_hidden);
  552. ObjectTypeDB::bind_method(_MD("set_hidden","hidden"), &Spatial::set_hidden);
  553. ObjectTypeDB::bind_method(_MD("_set_visible_"), &Spatial::_set_visible_);
  554. ObjectTypeDB::bind_method(_MD("_is_visible_"), &Spatial::_is_visible_);
  555. ObjectTypeDB::bind_method(_MD("set_notify_local_transform","enable"), &Spatial::set_notify_local_transform);
  556. ObjectTypeDB::bind_method(_MD("is_local_transform_notification_enabled"), &Spatial::is_local_transform_notification_enabled);
  557. void rotate(const Vector3& p_normal,float p_radians);
  558. void rotate_x(float p_radians);
  559. void rotate_y(float p_radians);
  560. void rotate_z(float p_radians);
  561. void translate(const Vector3& p_offset);
  562. void scale(const Vector3& p_ratio);
  563. void global_rotate(const Vector3& p_normal,float p_radians);
  564. void global_translate(const Vector3& p_offset);
  565. ObjectTypeDB::bind_method( _MD("rotate","normal","radians"),&Spatial::rotate );
  566. ObjectTypeDB::bind_method( _MD("global_rotate","normal","radians"),&Spatial::global_rotate );
  567. ObjectTypeDB::bind_method( _MD("rotate_x","radians"),&Spatial::rotate_x );
  568. ObjectTypeDB::bind_method( _MD("rotate_y","radians"),&Spatial::rotate_y );
  569. ObjectTypeDB::bind_method( _MD("rotate_z","radians"),&Spatial::rotate_z );
  570. ObjectTypeDB::bind_method( _MD("translate","offset"),&Spatial::translate );
  571. ObjectTypeDB::bind_method( _MD("global_translate","offset"),&Spatial::global_translate );
  572. ObjectTypeDB::bind_method( _MD("orthonormalize"),&Spatial::orthonormalize );
  573. ObjectTypeDB::bind_method( _MD("set_identity"),&Spatial::set_identity );
  574. ObjectTypeDB::bind_method( _MD("look_at","target","up"),&Spatial::look_at );
  575. ObjectTypeDB::bind_method( _MD("look_at_from_pos","pos","target","up"),&Spatial::look_at_from_pos );
  576. BIND_CONSTANT( NOTIFICATION_TRANSFORM_CHANGED );
  577. BIND_CONSTANT( NOTIFICATION_ENTER_WORLD );
  578. BIND_CONSTANT( NOTIFICATION_EXIT_WORLD );
  579. BIND_CONSTANT( NOTIFICATION_VISIBILITY_CHANGED );
  580. //ADD_PROPERTY( PropertyInfo(Variant::TRANSFORM,"transform/global",PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR ), _SCS("set_global_transform"), _SCS("get_global_transform") );
  581. ADD_PROPERTYNZ( PropertyInfo(Variant::TRANSFORM,"transform/local",PROPERTY_HINT_NONE,""), _SCS("set_transform"), _SCS("get_transform") );
  582. ADD_PROPERTY( PropertyInfo(Variant::VECTOR3,"transform/translation",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_EDITOR), _SCS("set_translation"), _SCS("get_translation") );
  583. ADD_PROPERTY( PropertyInfo(Variant::VECTOR3,"transform/rotation",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_EDITOR), _SCS("set_rotation_deg"), _SCS("get_rotation_deg") );
  584. ADD_PROPERTY( PropertyInfo(Variant::VECTOR3,"transform/rotation_rad",PROPERTY_HINT_NONE,"",0), _SCS("set_rotation"), _SCS("get_rotation") );
  585. ADD_PROPERTY( PropertyInfo(Variant::VECTOR3,"transform/scale",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_EDITOR), _SCS("set_scale"), _SCS("get_scale") );
  586. ADD_PROPERTYNO( PropertyInfo(Variant::BOOL,"visibility/visible"), _SCS("_set_visible_"), _SCS("_is_visible_") );
  587. //ADD_PROPERTY( PropertyInfo(Variant::TRANSFORM,"transform/local"), _SCS("set_transform"), _SCS("get_transform") );
  588. ADD_SIGNAL( MethodInfo("visibility_changed" ) );
  589. }
  590. Spatial::Spatial() : xform_change(this)
  591. {
  592. data.dirty=DIRTY_NONE;
  593. data.children_lock=0;
  594. data.ignore_notification=false;
  595. data.toplevel=false;
  596. data.toplevel_active=false;
  597. data.scale=Vector3(1,1,1);
  598. data.viewport=NULL;
  599. data.inside_world=false;
  600. data.visible=true;
  601. #ifdef TOOLS_ENABLED
  602. data.gizmo_disabled=false;
  603. data.gizmo_dirty=false;
  604. #endif
  605. data.notify_local_transform=false;
  606. data.parent=NULL;
  607. data.C=NULL;
  608. }
  609. Spatial::~Spatial() {
  610. }