skeleton.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*************************************************************************/
  2. /* skeleton.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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_slice("/",1).to_int();
  38. String what=path.get_slice("/",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_slice("/",1).to_int();
  72. String what=path.get_slice("/",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.enabled) {
  144. Transform pose=b.pose;
  145. if (b.custom_pose_enable) {
  146. pose = b.custom_pose * pose;
  147. }
  148. if (b.parent>=0) {
  149. b.pose_global=bonesptr[b.parent].pose_global * (b.rest * pose);
  150. } else {
  151. b.pose_global=b.rest * pose;
  152. }
  153. } else {
  154. if (b.parent>=0) {
  155. b.pose_global=bonesptr[b.parent].pose_global * b.rest;
  156. } else {
  157. b.pose_global=b.rest;
  158. }
  159. }
  160. vs->skeleton_bone_set_transform( skeleton, i, b.pose_global * b.rest_global_inverse );
  161. for(List<uint32_t>::Element *E=b.nodes_bound.front();E;E=E->next()) {
  162. Object *obj=ObjectDB::get_instance(E->get());
  163. ERR_CONTINUE(!obj);
  164. Spatial *sp = obj->cast_to<Spatial>();
  165. ERR_CONTINUE(!sp);
  166. sp->set_transform(b.pose_global * b.rest_global_inverse);
  167. }
  168. }
  169. dirty=false;
  170. } break;
  171. }
  172. }
  173. Transform Skeleton::get_bone_transform(int p_bone) const {
  174. ERR_FAIL_INDEX_V(p_bone,bones.size(),Transform());
  175. if (dirty)
  176. const_cast<Skeleton*>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
  177. return bones[p_bone].pose_global * bones[p_bone].rest_global_inverse;
  178. }
  179. Transform Skeleton::get_bone_global_pose(int p_bone) const {
  180. ERR_FAIL_INDEX_V(p_bone,bones.size(),Transform());
  181. if (dirty)
  182. const_cast<Skeleton*>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
  183. return bones[p_bone].pose_global;
  184. }
  185. RID Skeleton::get_skeleton() const {
  186. return skeleton;
  187. }
  188. // skeleton creation api
  189. void Skeleton::add_bone(const String& p_name) {
  190. ERR_FAIL_COND( p_name=="" || p_name.find(":")!=-1 || p_name.find("/")!=-1 );
  191. for (int i=0;i<bones.size();i++) {
  192. ERR_FAIL_COND( bones[i].name=="p_name");
  193. }
  194. Bone b;
  195. b.name=p_name;
  196. bones.push_back(b);
  197. rest_global_inverse_dirty=true;
  198. _make_dirty();
  199. update_gizmo();
  200. }
  201. int Skeleton::find_bone(String p_name) const {
  202. for (int i=0;i<bones.size();i++) {
  203. if (bones[i].name==p_name)
  204. return i;
  205. }
  206. return -1;
  207. }
  208. String Skeleton::get_bone_name(int p_bone) const {
  209. ERR_FAIL_INDEX_V( p_bone, bones.size(), "" );
  210. return bones[p_bone].name;
  211. }
  212. int Skeleton::get_bone_count() const {
  213. return bones.size();
  214. }
  215. void Skeleton::set_bone_parent(int p_bone, int p_parent) {
  216. ERR_FAIL_INDEX( p_bone, bones.size() );
  217. ERR_FAIL_COND( p_parent!=-1 && (p_parent<0 || p_parent>=p_bone));
  218. bones[p_bone].parent=p_parent;
  219. rest_global_inverse_dirty=true;
  220. _make_dirty();
  221. }
  222. int Skeleton::get_bone_parent(int p_bone) const {
  223. ERR_FAIL_INDEX_V( p_bone, bones.size(), -1 );
  224. return bones[p_bone].parent;
  225. }
  226. void Skeleton::set_bone_rest(int p_bone, const Transform& p_rest) {
  227. ERR_FAIL_INDEX( p_bone, bones.size() );
  228. bones[p_bone].rest=p_rest;
  229. rest_global_inverse_dirty=true;
  230. _make_dirty();
  231. }
  232. Transform Skeleton::get_bone_rest(int p_bone) const {
  233. ERR_FAIL_INDEX_V( p_bone, bones.size(), Transform() );
  234. return bones[p_bone].rest;
  235. }
  236. void Skeleton::set_bone_enabled(int p_bone, bool p_enabled) {
  237. ERR_FAIL_INDEX( p_bone, bones.size() );
  238. bones[p_bone].enabled=p_enabled;
  239. rest_global_inverse_dirty=true;
  240. _make_dirty();
  241. }
  242. bool Skeleton::is_bone_enabled(int p_bone) const {
  243. ERR_FAIL_INDEX_V( p_bone, bones.size(), false );
  244. return bones[p_bone].enabled;
  245. }
  246. void Skeleton::bind_child_node_to_bone(int p_bone,Node *p_node) {
  247. ERR_FAIL_NULL(p_node);
  248. ERR_FAIL_INDEX( p_bone, bones.size() );
  249. uint32_t id=p_node->get_instance_ID();
  250. for (List<uint32_t>::Element *E=bones[p_bone].nodes_bound.front();E;E=E->next()) {
  251. if (E->get()==id)
  252. return; // already here
  253. }
  254. bones[p_bone].nodes_bound.push_back(id);
  255. }
  256. void Skeleton::unbind_child_node_from_bone(int p_bone,Node *p_node) {
  257. ERR_FAIL_NULL(p_node);
  258. ERR_FAIL_INDEX( p_bone, bones.size() );
  259. uint32_t id=p_node->get_instance_ID();
  260. bones[p_bone].nodes_bound.erase(id);
  261. }
  262. void Skeleton::get_bound_child_nodes_to_bone(int p_bone,List<Node*> *p_bound) const {
  263. ERR_FAIL_INDEX( p_bone, bones.size() );
  264. for (const List<uint32_t>::Element *E=bones[p_bone].nodes_bound.front();E;E=E->next()) {
  265. Object *obj=ObjectDB::get_instance(E->get());
  266. ERR_CONTINUE(!obj);
  267. p_bound->push_back(obj->cast_to<Node>());
  268. }
  269. }
  270. void Skeleton::clear_bones() {
  271. bones.clear();
  272. rest_global_inverse_dirty=true;
  273. _make_dirty();
  274. }
  275. // posing api
  276. void Skeleton::set_bone_pose(int p_bone, const Transform& p_pose) {
  277. ERR_FAIL_INDEX( p_bone, bones.size() );
  278. ERR_FAIL_COND( !is_inside_scene() );
  279. bones[p_bone].pose=p_pose;
  280. _make_dirty();
  281. }
  282. Transform Skeleton::get_bone_pose(int p_bone) const {
  283. ERR_FAIL_INDEX_V( p_bone, bones.size(), Transform() );
  284. return bones[p_bone].pose;
  285. }
  286. void Skeleton::set_bone_custom_pose(int p_bone, const Transform& p_custom_pose) {
  287. ERR_FAIL_INDEX( p_bone, bones.size() );
  288. // ERR_FAIL_COND( !is_inside_scene() );
  289. bones[p_bone].custom_pose_enable=(p_custom_pose!=Transform());
  290. bones[p_bone].custom_pose=p_custom_pose;
  291. _make_dirty();
  292. }
  293. Transform Skeleton::get_bone_custom_pose(int p_bone) const {
  294. ERR_FAIL_INDEX_V( p_bone, bones.size(), Transform() );
  295. return bones[p_bone].custom_pose;
  296. }
  297. void Skeleton::_make_dirty() {
  298. if (dirty)
  299. return;
  300. if (!is_inside_scene()) {
  301. dirty=true;
  302. return;
  303. }
  304. MessageQueue::get_singleton()->push_notification( this, NOTIFICATION_UPDATE_SKELETON );
  305. dirty=true;
  306. }
  307. RES Skeleton::_get_gizmo_geometry() const {
  308. if (!GLOBAL_DEF("debug/draw_skeleton", true))
  309. return RES();
  310. if (bones.size()==0)
  311. return RES();
  312. Ref<SurfaceTool> surface_tool( memnew( SurfaceTool ));
  313. Ref<FixedMaterial> mat( memnew( FixedMaterial ));
  314. mat->set_parameter( FixedMaterial::PARAM_DIFFUSE,Color(0.6,1.0,0.3,0.1) );
  315. mat->set_line_width(4);
  316. mat->set_flag(Material::FLAG_DOUBLE_SIDED,true);
  317. mat->set_flag(Material::FLAG_UNSHADED,true);
  318. mat->set_flag(Material::FLAG_ONTOP,true);
  319. // mat->set_hint(Material::HINT_NO_DEPTH_DRAW,true);
  320. surface_tool->begin(Mesh::PRIMITIVE_LINES);
  321. surface_tool->set_material(mat);
  322. const Bone *bonesptr=&bones[0];
  323. int len=bones.size();
  324. for (int i=0;i<len;i++) {
  325. const Bone &b=bonesptr[i];
  326. Transform t;
  327. if (b.parent<0)
  328. continue;
  329. Vector3 v1=(bonesptr[b.parent].pose_global * bonesptr[b.parent].rest_global_inverse).xform(bonesptr[b.parent].rest_global_inverse.affine_inverse().origin);
  330. Vector3 v2=(b.pose_global * b.rest_global_inverse).xform(b.rest_global_inverse.affine_inverse().origin);
  331. surface_tool->add_vertex(v1);
  332. surface_tool->add_vertex(v2);
  333. }
  334. return surface_tool->commit();
  335. }
  336. void Skeleton::localize_rests() {
  337. for(int i=bones.size()-1;i>=0;i--) {
  338. if (bones[i].parent>=0)
  339. set_bone_rest(i,bones[bones[i].parent].rest.affine_inverse() * bones[i].rest);
  340. }
  341. }
  342. void Skeleton::_bind_methods() {
  343. ObjectTypeDB::bind_method(_MD("add_bone","name"),&Skeleton::add_bone);
  344. ObjectTypeDB::bind_method(_MD("find_bone","name"),&Skeleton::find_bone);
  345. ObjectTypeDB::bind_method(_MD("get_bone_name","bone_idx"),&Skeleton::get_bone_name);
  346. ObjectTypeDB::bind_method(_MD("get_bone_parent","bone_idx"),&Skeleton::get_bone_parent);
  347. ObjectTypeDB::bind_method(_MD("set_bone_parent","bone_idx","parent_idx"),&Skeleton::set_bone_parent);
  348. ObjectTypeDB::bind_method(_MD("get_bone_count"),&Skeleton::get_bone_count);
  349. ObjectTypeDB::bind_method(_MD("get_bone_rest","bone_idx"),&Skeleton::get_bone_rest);
  350. ObjectTypeDB::bind_method(_MD("set_bone_rest","bone_idx","rest"),&Skeleton::set_bone_rest);
  351. ObjectTypeDB::bind_method(_MD("bind_child_node_to_bone","bone_idx","node:Node"),&Skeleton::bind_child_node_to_bone);
  352. ObjectTypeDB::bind_method(_MD("unbind_child_node_from_bone","bone_idx","node:Node"),&Skeleton::unbind_child_node_from_bone);
  353. ObjectTypeDB::bind_method(_MD("get_bound_child_nodes_to_bone","bone_idx"),&Skeleton::_get_bound_child_nodes_to_bone);
  354. ObjectTypeDB::bind_method(_MD("clear_bones"),&Skeleton::clear_bones);
  355. ObjectTypeDB::bind_method(_MD("get_bone_pose","bone_idx"),&Skeleton::get_bone_pose);
  356. ObjectTypeDB::bind_method(_MD("set_bone_pose","bone_idx","pose"),&Skeleton::set_bone_pose);
  357. ObjectTypeDB::bind_method(_MD("get_bone_global_pose","bone_idx"),&Skeleton::get_bone_global_pose);
  358. ObjectTypeDB::bind_method(_MD("get_bone_custom_pose","bone_idx"),&Skeleton::get_bone_custom_pose);
  359. ObjectTypeDB::bind_method(_MD("set_bone_custom_pose","bone_idx","custom_pose"),&Skeleton::set_bone_custom_pose);
  360. ObjectTypeDB::bind_method(_MD("get_bone_transform","bone_idx"),&Skeleton::get_bone_transform);
  361. BIND_CONSTANT( NOTIFICATION_UPDATE_SKELETON );
  362. }
  363. Skeleton::Skeleton() {
  364. rest_global_inverse_dirty=true;
  365. dirty=false;
  366. skeleton=VisualServer::get_singleton()->skeleton_create();
  367. }
  368. Skeleton::~Skeleton() {
  369. VisualServer::get_singleton()->free( skeleton );
  370. }