skeleton.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*************************************************************************/
  2. /* skeleton.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 "skeleton.h"
  30. #include "message_queue.h"
  31. #include "scene/resources/surface_tool.h"
  32. #include "core/globals.h"
  33. bool Skeleton::_set(const StringName& p_path, const Variant& p_value) {
  34. String path = p_path;
  35. if (!path.begins_with("bones/"))
  36. return false;
  37. int which=path.get_slicec('/',1).to_int();
  38. String what=path.get_slicec('/',2);
  39. if (which==bones.size() && what=="name") {
  40. add_bone(p_value);
  41. return true;
  42. }
  43. ERR_FAIL_INDEX_V( which, bones.size(), false );
  44. if (what=="parent")
  45. set_bone_parent(which, p_value );
  46. else if (what=="rest")
  47. set_bone_rest(which, p_value);
  48. else if (what=="enabled")
  49. set_bone_enabled(which, p_value);
  50. else if (what=="pose")
  51. set_bone_pose(which, p_value);
  52. else if (what=="bound_childs") {
  53. Array children=p_value;
  54. bones[which].nodes_bound.clear();
  55. for (int i=0;i<children.size();i++) {
  56. NodePath path=children[i];
  57. ERR_CONTINUE( path.operator String()=="" );
  58. Node *node = get_node(path);
  59. ERR_CONTINUE(!node);
  60. bind_child_node_to_bone(which,node);
  61. }
  62. } else {
  63. return false;
  64. }
  65. return true;
  66. }
  67. bool Skeleton::_get(const StringName& p_name,Variant &r_ret) const {
  68. String path=p_name;
  69. if (!path.begins_with("bones/"))
  70. return false;
  71. int which=path.get_slicec('/',1).to_int();
  72. String what=path.get_slicec('/',2);
  73. ERR_FAIL_INDEX_V( which, bones.size(), false );
  74. if (what=="name")
  75. r_ret=get_bone_name(which);
  76. else if (what=="parent")
  77. r_ret=get_bone_parent(which);
  78. else if (what=="rest")
  79. r_ret=get_bone_rest(which);
  80. else if (what=="enabled")
  81. r_ret=is_bone_enabled(which);
  82. else if (what=="pose")
  83. r_ret=get_bone_pose(which);
  84. else if (what=="bound_childs") {
  85. Array children;
  86. for (const List<uint32_t>::Element *E=bones[which].nodes_bound.front();E;E=E->next()) {
  87. Object *obj=ObjectDB::get_instance(E->get());
  88. ERR_CONTINUE(!obj);
  89. Node *node=obj->cast_to<Node>();
  90. ERR_CONTINUE(!node);
  91. NodePath path=get_path_to(node);
  92. children.push_back(path);
  93. }
  94. r_ret=children;
  95. } else
  96. return false;
  97. return true;
  98. }
  99. void Skeleton::_get_property_list( List<PropertyInfo>* p_list ) const {
  100. for (int i=0;i<bones.size();i++) {
  101. String prep="bones/"+itos(i)+"/";
  102. p_list->push_back( PropertyInfo( Variant::STRING, prep+"name" ) );
  103. p_list->push_back( PropertyInfo( Variant::INT, prep+"parent" , PROPERTY_HINT_RANGE,"-1,"+itos(i-1)+",1") );
  104. p_list->push_back( PropertyInfo( Variant::TRANSFORM, prep+"rest" ) );
  105. p_list->push_back( PropertyInfo( Variant::BOOL, prep+"enabled" ) );
  106. p_list->push_back( PropertyInfo( Variant::TRANSFORM, prep+"pose", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR ) );
  107. p_list->push_back( PropertyInfo( Variant::ARRAY, prep+"bound_childs" ) );
  108. }
  109. }
  110. void Skeleton::_notification(int p_what) {
  111. switch(p_what) {
  112. case NOTIFICATION_ENTER_WORLD: {
  113. if (dirty) {
  114. dirty=false;
  115. _make_dirty(); // property make it dirty
  116. }
  117. } break;
  118. case NOTIFICATION_EXIT_WORLD: {
  119. } break;
  120. case NOTIFICATION_UPDATE_SKELETON: {
  121. VisualServer *vs=VisualServer::get_singleton();
  122. Bone *bonesptr=&bones[0];
  123. int len=bones.size();
  124. vs->skeleton_resize( skeleton, len ); // if same size, nothin really happens
  125. // pose changed, rebuild cache of inverses
  126. if (rest_global_inverse_dirty) {
  127. // calculate global rests and invert them
  128. for (int i=0;i<len;i++) {
  129. Bone &b=bonesptr[i];
  130. if (b.parent>=0)
  131. b.rest_global_inverse=bonesptr[b.parent].rest_global_inverse * b.rest;
  132. else
  133. b.rest_global_inverse=b.rest;
  134. }
  135. for (int i=0;i<len;i++) {
  136. Bone &b=bonesptr[i];
  137. b.rest_global_inverse.affine_invert();
  138. }
  139. rest_global_inverse_dirty=false;
  140. }
  141. for (int i=0;i<len;i++) {
  142. Bone &b=bonesptr[i];
  143. if (b.disable_rest) {
  144. if (b.enabled) {
  145. Transform pose=b.pose;
  146. if (b.custom_pose_enable) {
  147. pose = b.custom_pose * pose;
  148. }
  149. if (b.parent>=0) {
  150. b.pose_global=bonesptr[b.parent].pose_global * pose;
  151. } else {
  152. b.pose_global=pose;
  153. }
  154. } else {
  155. if (b.parent>=0) {
  156. b.pose_global=bonesptr[b.parent].pose_global;
  157. } else {
  158. b.pose_global=Transform();
  159. }
  160. }
  161. } else {
  162. if (b.enabled) {
  163. Transform pose=b.pose;
  164. if (b.custom_pose_enable) {
  165. pose = b.custom_pose * pose;
  166. }
  167. if (b.parent>=0) {
  168. b.pose_global=bonesptr[b.parent].pose_global * (b.rest * pose);
  169. } else {
  170. b.pose_global=b.rest * pose;
  171. }
  172. } else {
  173. if (b.parent>=0) {
  174. b.pose_global=bonesptr[b.parent].pose_global * b.rest;
  175. } else {
  176. b.pose_global=b.rest;
  177. }
  178. }
  179. }
  180. vs->skeleton_bone_set_transform( skeleton, i, b.pose_global * b.rest_global_inverse );
  181. for(List<uint32_t>::Element *E=b.nodes_bound.front();E;E=E->next()) {
  182. Object *obj=ObjectDB::get_instance(E->get());
  183. ERR_CONTINUE(!obj);
  184. Spatial *sp = obj->cast_to<Spatial>();
  185. ERR_CONTINUE(!sp);
  186. sp->set_transform(b.pose_global);
  187. }
  188. }
  189. dirty=false;
  190. } break;
  191. }
  192. }
  193. Transform Skeleton::get_bone_transform(int p_bone) const {
  194. ERR_FAIL_INDEX_V(p_bone,bones.size(),Transform());
  195. if (dirty)
  196. const_cast<Skeleton*>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
  197. return bones[p_bone].pose_global * bones[p_bone].rest_global_inverse;
  198. }
  199. void Skeleton::set_bone_global_pose(int p_bone,const Transform& p_pose) {
  200. ERR_FAIL_INDEX(p_bone,bones.size());
  201. if (bones[p_bone].parent==-1) {
  202. set_bone_pose(p_bone,bones[p_bone].rest_global_inverse * p_pose); //fast
  203. } else {
  204. set_bone_pose(p_bone, bones[p_bone].rest.affine_inverse() * (get_bone_global_pose(bones[p_bone].parent).affine_inverse() * p_pose)); //slow
  205. }
  206. }
  207. Transform Skeleton::get_bone_global_pose(int p_bone) const {
  208. ERR_FAIL_INDEX_V(p_bone,bones.size(),Transform());
  209. if (dirty)
  210. const_cast<Skeleton*>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
  211. return bones[p_bone].pose_global;
  212. }
  213. RID Skeleton::get_skeleton() const {
  214. return skeleton;
  215. }
  216. // skeleton creation api
  217. void Skeleton::add_bone(const String& p_name) {
  218. ERR_FAIL_COND( p_name=="" || p_name.find(":")!=-1 || p_name.find("/")!=-1 );
  219. for (int i=0;i<bones.size();i++) {
  220. ERR_FAIL_COND( bones[i].name=="p_name");
  221. }
  222. Bone b;
  223. b.name=p_name;
  224. bones.push_back(b);
  225. rest_global_inverse_dirty=true;
  226. _make_dirty();
  227. update_gizmo();
  228. }
  229. int Skeleton::find_bone(String p_name) const {
  230. for (int i=0;i<bones.size();i++) {
  231. if (bones[i].name==p_name)
  232. return i;
  233. }
  234. return -1;
  235. }
  236. String Skeleton::get_bone_name(int p_bone) const {
  237. ERR_FAIL_INDEX_V( p_bone, bones.size(), "" );
  238. return bones[p_bone].name;
  239. }
  240. int Skeleton::get_bone_count() const {
  241. return bones.size();
  242. }
  243. void Skeleton::set_bone_parent(int p_bone, int p_parent) {
  244. ERR_FAIL_INDEX( p_bone, bones.size() );
  245. ERR_FAIL_COND( p_parent!=-1 && (p_parent<0 || p_parent>=p_bone));
  246. bones[p_bone].parent=p_parent;
  247. rest_global_inverse_dirty=true;
  248. _make_dirty();
  249. }
  250. void Skeleton::unparent_bone_and_rest(int p_bone) {
  251. ERR_FAIL_INDEX( p_bone, bones.size() );
  252. int parent=bones[p_bone].parent;
  253. while(parent>=0) {
  254. bones[p_bone].rest = bones[parent].rest * bones[p_bone].rest;
  255. parent=bones[parent].parent;
  256. }
  257. bones[p_bone].parent=-1;
  258. bones[p_bone].rest_global_inverse=bones[p_bone].rest.affine_inverse(); //same thing
  259. _make_dirty();
  260. }
  261. void Skeleton::set_bone_disable_rest(int p_bone, bool p_disable) {
  262. ERR_FAIL_INDEX( p_bone, bones.size() );
  263. bones[p_bone].disable_rest=p_disable;
  264. }
  265. bool Skeleton::is_bone_rest_disabled(int p_bone) const {
  266. ERR_FAIL_INDEX_V( p_bone, bones.size(), false );
  267. return bones[p_bone].disable_rest;
  268. }
  269. int Skeleton::get_bone_parent(int p_bone) const {
  270. ERR_FAIL_INDEX_V( p_bone, bones.size(), -1 );
  271. return bones[p_bone].parent;
  272. }
  273. void Skeleton::set_bone_rest(int p_bone, const Transform& p_rest) {
  274. ERR_FAIL_INDEX( p_bone, bones.size() );
  275. bones[p_bone].rest=p_rest;
  276. rest_global_inverse_dirty=true;
  277. _make_dirty();
  278. }
  279. Transform Skeleton::get_bone_rest(int p_bone) const {
  280. ERR_FAIL_INDEX_V( p_bone, bones.size(), Transform() );
  281. return bones[p_bone].rest;
  282. }
  283. void Skeleton::set_bone_enabled(int p_bone, bool p_enabled) {
  284. ERR_FAIL_INDEX( p_bone, bones.size() );
  285. bones[p_bone].enabled=p_enabled;
  286. rest_global_inverse_dirty=true;
  287. _make_dirty();
  288. }
  289. bool Skeleton::is_bone_enabled(int p_bone) const {
  290. ERR_FAIL_INDEX_V( p_bone, bones.size(), false );
  291. return bones[p_bone].enabled;
  292. }
  293. void Skeleton::bind_child_node_to_bone(int p_bone,Node *p_node) {
  294. ERR_FAIL_NULL(p_node);
  295. ERR_FAIL_INDEX( p_bone, bones.size() );
  296. uint32_t id=p_node->get_instance_ID();
  297. for (List<uint32_t>::Element *E=bones[p_bone].nodes_bound.front();E;E=E->next()) {
  298. if (E->get()==id)
  299. return; // already here
  300. }
  301. bones[p_bone].nodes_bound.push_back(id);
  302. }
  303. void Skeleton::unbind_child_node_from_bone(int p_bone,Node *p_node) {
  304. ERR_FAIL_NULL(p_node);
  305. ERR_FAIL_INDEX( p_bone, bones.size() );
  306. uint32_t id=p_node->get_instance_ID();
  307. bones[p_bone].nodes_bound.erase(id);
  308. }
  309. void Skeleton::get_bound_child_nodes_to_bone(int p_bone,List<Node*> *p_bound) const {
  310. ERR_FAIL_INDEX( p_bone, bones.size() );
  311. for (const List<uint32_t>::Element *E=bones[p_bone].nodes_bound.front();E;E=E->next()) {
  312. Object *obj=ObjectDB::get_instance(E->get());
  313. ERR_CONTINUE(!obj);
  314. p_bound->push_back(obj->cast_to<Node>());
  315. }
  316. }
  317. void Skeleton::clear_bones() {
  318. bones.clear();
  319. rest_global_inverse_dirty=true;
  320. _make_dirty();
  321. }
  322. // posing api
  323. void Skeleton::set_bone_pose(int p_bone, const Transform& p_pose) {
  324. ERR_FAIL_INDEX( p_bone, bones.size() );
  325. ERR_FAIL_COND( !is_inside_tree() );
  326. bones[p_bone].pose=p_pose;
  327. _make_dirty();
  328. }
  329. Transform Skeleton::get_bone_pose(int p_bone) const {
  330. ERR_FAIL_INDEX_V( p_bone, bones.size(), Transform() );
  331. return bones[p_bone].pose;
  332. }
  333. void Skeleton::set_bone_custom_pose(int p_bone, const Transform& p_custom_pose) {
  334. ERR_FAIL_INDEX( p_bone, bones.size() );
  335. // ERR_FAIL_COND( !is_inside_scene() );
  336. bones[p_bone].custom_pose_enable=(p_custom_pose!=Transform());
  337. bones[p_bone].custom_pose=p_custom_pose;
  338. _make_dirty();
  339. }
  340. Transform Skeleton::get_bone_custom_pose(int p_bone) const {
  341. ERR_FAIL_INDEX_V( p_bone, bones.size(), Transform() );
  342. return bones[p_bone].custom_pose;
  343. }
  344. void Skeleton::_make_dirty() {
  345. if (dirty)
  346. return;
  347. if (!is_inside_tree()) {
  348. dirty=true;
  349. return;
  350. }
  351. MessageQueue::get_singleton()->push_notification( this, NOTIFICATION_UPDATE_SKELETON );
  352. dirty=true;
  353. }
  354. RES Skeleton::_get_gizmo_geometry() const {
  355. if (!GLOBAL_DEF("debug/draw_skeleton", true))
  356. return RES();
  357. if (bones.size()==0)
  358. return RES();
  359. Ref<SurfaceTool> surface_tool( memnew( SurfaceTool ));
  360. Ref<FixedMaterial> mat( memnew( FixedMaterial ));
  361. mat->set_parameter( FixedMaterial::PARAM_DIFFUSE,Color(0.6,1.0,0.3,0.1) );
  362. mat->set_line_width(4);
  363. mat->set_flag(Material::FLAG_DOUBLE_SIDED,true);
  364. mat->set_flag(Material::FLAG_UNSHADED,true);
  365. mat->set_flag(Material::FLAG_ONTOP,true);
  366. // mat->set_hint(Material::HINT_NO_DEPTH_DRAW,true);
  367. surface_tool->begin(Mesh::PRIMITIVE_LINES);
  368. surface_tool->set_material(mat);
  369. const Bone *bonesptr=&bones[0];
  370. int len=bones.size();
  371. for (int i=0;i<len;i++) {
  372. const Bone &b=bonesptr[i];
  373. Transform t;
  374. if (b.parent<0)
  375. continue;
  376. Vector3 v1=(bonesptr[b.parent].pose_global * bonesptr[b.parent].rest_global_inverse).xform(bonesptr[b.parent].rest_global_inverse.affine_inverse().origin);
  377. Vector3 v2=(b.pose_global * b.rest_global_inverse).xform(b.rest_global_inverse.affine_inverse().origin);
  378. surface_tool->add_vertex(v1);
  379. surface_tool->add_vertex(v2);
  380. }
  381. return surface_tool->commit();
  382. }
  383. void Skeleton::localize_rests() {
  384. for(int i=bones.size()-1;i>=0;i--) {
  385. if (bones[i].parent>=0)
  386. set_bone_rest(i,bones[bones[i].parent].rest.affine_inverse() * bones[i].rest);
  387. }
  388. }
  389. void Skeleton::_bind_methods() {
  390. ObjectTypeDB::bind_method(_MD("add_bone","name"),&Skeleton::add_bone);
  391. ObjectTypeDB::bind_method(_MD("find_bone","name"),&Skeleton::find_bone);
  392. ObjectTypeDB::bind_method(_MD("get_bone_name","bone_idx"),&Skeleton::get_bone_name);
  393. ObjectTypeDB::bind_method(_MD("get_bone_parent","bone_idx"),&Skeleton::get_bone_parent);
  394. ObjectTypeDB::bind_method(_MD("set_bone_parent","bone_idx","parent_idx"),&Skeleton::set_bone_parent);
  395. ObjectTypeDB::bind_method(_MD("get_bone_count"),&Skeleton::get_bone_count);
  396. ObjectTypeDB::bind_method(_MD("unparent_bone_and_rest","bone_idx"),&Skeleton::unparent_bone_and_rest);
  397. ObjectTypeDB::bind_method(_MD("get_bone_rest","bone_idx"),&Skeleton::get_bone_rest);
  398. ObjectTypeDB::bind_method(_MD("set_bone_rest","bone_idx","rest"),&Skeleton::set_bone_rest);
  399. ObjectTypeDB::bind_method(_MD("set_bone_disable_rest","bone_idx","disable"),&Skeleton::set_bone_disable_rest);
  400. ObjectTypeDB::bind_method(_MD("is_bone_rest_disabled","bone_idx"),&Skeleton::is_bone_rest_disabled);
  401. ObjectTypeDB::bind_method(_MD("bind_child_node_to_bone","bone_idx","node:Node"),&Skeleton::bind_child_node_to_bone);
  402. ObjectTypeDB::bind_method(_MD("unbind_child_node_from_bone","bone_idx","node:Node"),&Skeleton::unbind_child_node_from_bone);
  403. ObjectTypeDB::bind_method(_MD("get_bound_child_nodes_to_bone","bone_idx"),&Skeleton::_get_bound_child_nodes_to_bone);
  404. ObjectTypeDB::bind_method(_MD("clear_bones"),&Skeleton::clear_bones);
  405. ObjectTypeDB::bind_method(_MD("get_bone_pose","bone_idx"),&Skeleton::get_bone_pose);
  406. ObjectTypeDB::bind_method(_MD("set_bone_pose","bone_idx","pose"),&Skeleton::set_bone_pose);
  407. ObjectTypeDB::bind_method(_MD("set_bone_global_pose","bone_idx","pose"),&Skeleton::set_bone_global_pose);
  408. ObjectTypeDB::bind_method(_MD("get_bone_global_pose","bone_idx"),&Skeleton::get_bone_global_pose);
  409. ObjectTypeDB::bind_method(_MD("get_bone_custom_pose","bone_idx"),&Skeleton::get_bone_custom_pose);
  410. ObjectTypeDB::bind_method(_MD("set_bone_custom_pose","bone_idx","custom_pose"),&Skeleton::set_bone_custom_pose);
  411. ObjectTypeDB::bind_method(_MD("get_bone_transform","bone_idx"),&Skeleton::get_bone_transform);
  412. BIND_CONSTANT( NOTIFICATION_UPDATE_SKELETON );
  413. }
  414. Skeleton::Skeleton() {
  415. rest_global_inverse_dirty=true;
  416. dirty=false;
  417. skeleton=VisualServer::get_singleton()->skeleton_create();
  418. }
  419. Skeleton::~Skeleton() {
  420. VisualServer::get_singleton()->free( skeleton );
  421. }