working_with_3d_skeletons.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. Working with 3D skeletons
  2. =========================
  3. Godot 3D skeleton support is currently quite rudimentary. Skeleton node
  4. and class were disigned mainly to support importing skeletal animations
  5. as set of transformation matrices.
  6. Skeleton node
  7. -------------
  8. Skeleton node can be directly added anywhere you want on scene. Usually
  9. mesh is a child of Skeleton, as it easier to manipulate this way, as
  10. Transforms within skeleton are relative to where Skeleton is. But you
  11. can specify Skeleton node in every MeshInstance.
  12. Being obvious, Skeleton is intended to deform meshes, and consists of
  13. structures called "bones". Each "bone" is represented as Transform,
  14. which is applied to a group of vertices within a mesh. You can directly
  15. control a group of vertices from Godot. For that please reference
  16. MeshDataTool class, method set\_vertex\_bones. This class is very
  17. powerful but not documented.
  18. The "bones" are organized in hierarchy, every bone, except for root
  19. bone(s) have parent. Every bone have associated name you can use to
  20. refer to it (e.g. "root" or "hand.L", etc). Also bones are all numbered,
  21. these numbers are bone IDs. Bone parents are referred by their numbered
  22. IDs.
  23. For the rest of the article we consider the following scene
  24. ::
  25. main (Spatial) - script is always here
  26. == skel (Skeleton)
  27. ==== mesh (MeshInstance)
  28. This scene is imported from Blender. It contains arm mesh with 2 bones -
  29. upperarm and lowerarm, with lowerarm parented to upperarm
  30. Skeleton class
  31. --------------
  32. You can view Godot internal help for descriptions of every function.
  33. Basically all operations on bones are done using their numeric ID. You
  34. can convert from name to numeric ID and vise versa.
  35. **To find number of bones in skeleton we use get\_bone\_count()
  36. function**
  37. ::
  38. extends Spatial
  39. var skel
  40. func _ready():
  41. skel = get_node("skel")
  42. var id = skel.find_bone("upperarm")
  43. print("bone id:", id)
  44. var parent = skel.get_bone_parent(id)
  45. print("bone parent id:", id)
  46. **to find ID for the bone, use find\_bone() function**
  47. ::
  48. extends Spatial
  49. var skel
  50. func _ready():
  51. skel = get_node("skel")
  52. var id = skel.find_bone("upperarm")
  53. print("bone id:", id)
  54. Now, we want to do something interesting with ID except for printing it.
  55. Also, we might need additional information - to find bone parents to
  56. complete chain, etc. This all is done with get/set\_bone\_\* functions.
  57. **To find bone parent we use get\_bone\_parent(id) function**
  58. ::
  59. extends Spatial
  60. var skel
  61. func _ready():
  62. skel = get_node("skel")
  63. var id = skel.find_bone("upperarm")
  64. print("bone id:", id)
  65. var parent = skel.get_bone_parent(id)
  66. print("bone parent id:", id)
  67. Bone transforms is the thing why we're here at all. There are 3 kind of
  68. transforms - local, global, custom.
  69. **To find bone local Transform we use get\_bone\_pose(id) function**
  70. ::
  71. extends Spatial
  72. var skel
  73. func _ready():
  74. skel = get_node("skel")
  75. var id = skel.find_bone("upperarm")
  76. print("bone id:", id)
  77. var parent = skel.get_bone_parent(id)
  78. print("bone parent id:", id)
  79. var t = skel.get_bone_pose(id)
  80. print("bone transform: ", t)
  81. So we see 3x4 matrix there, with first column of 1s. What can we do
  82. about that? it is Transform, so we can do everything we can do with
  83. Transform, basically translate, rotate and scale. Also we can multiply
  84. transforms to have complex transforms. Remember, "bones" in Godot are
  85. just Transforms over a group of vertices. Also we can copy Transforms of
  86. other objects there. So lets rotate our "upperarm" bone:
  87. ::
  88. extends Spatial
  89. var skel
  90. var id
  91. func _ready():
  92. skel = get_node("skel")
  93. id = skel.find_bone("upperarm")
  94. print("bone id:", id)
  95. var parent = skel.get_bone_parent(id)
  96. print("bone parent id:", id)
  97. var t = skel.get_bone_pose(id)
  98. print("bone transform: ", t)
  99. set_process(true)
  100. func _process(dt):
  101. var t = skel.get_bone_pose(id)
  102. t = t.rotated(Vector3(0.0, 1.0, 0.0), 0.1 * dt)
  103. skel.set_bone_pose(id, t)
  104. Now we can rotate individual bones. The same happens for scale and
  105. translate - try these on your own and see results.
  106. What we used now was local pose. By default all bones are not modified.
  107. But this Transform tells us nothing about relationship between bones.
  108. This information is needed for quite a number of tasks. How can we get
  109. it? here comes global transform:
  110. **To find bone global Transform we use get\_bone\_global\_pose(id)
  111. function**
  112. We will find global Transform for lowerarm bone
  113. ::
  114. extends Spatial
  115. var skel
  116. func _ready():
  117. skel = get_node("skel")
  118. var id = skel.find_bone("lowerarm")
  119. print("bone id:", id)
  120. var parent = skel.get_bone_parent(id)
  121. print("bone parent id:", id)
  122. var t = skel.get_bone_global_pose(id)
  123. print("bone transform: ", t)
  124. As you see, this transform is not zeroed. While being called global, it
  125. is actually relative to Skeleton origin. For root bone, origin is always
  126. at 0 if not modified. Lets print origin for our lowerarm bone:
  127. ::
  128. extends Spatial
  129. var skel
  130. func _ready():
  131. skel = get_node("skel")
  132. var id = skel.find_bone("lowerarm")
  133. print("bone id:", id)
  134. var parent = skel.get_bone_parent(id)
  135. print("bone parent id:", id)
  136. var t = skel.get_bone_global_pose(id)
  137. print("bone origin: ", t.origin)
  138. You will see a number. What does this number mean? It is a rotation
  139. point of Transform. So it is base part of the bone. In Blender you can
  140. go to Pose mode and try there to rotate bones - they will rotate around
  141. their origin. But what about tip? We can't know things like bone length,
  142. which we need for many things, without knowing tip location. For all
  143. bones in chain except for last one we can calculate tip location - it is
  144. simply a child bone origin. Yes, there are situations when this is not
  145. true, for non-connected bones. But that is OK for us for now, as it is
  146. not important regarding Transforms. But the leaf bone tip is nowhere to
  147. be found. Leaf bone is a bone without children. So you don't have any
  148. information about its tip. But this is not a showstopper. You can
  149. overcome this by either adding extra bone to the chain or just
  150. calculating leaf bone length in Blender and store the value in your
  151. script.
  152. Using 3D "bones" for mesh control
  153. ---------------------------------
  154. Now as you know basics we can apply these to make full FK-control of our
  155. arm (FK is forward-kinematics)
  156. To fully control our arm we need the following parameters:
  157. - Upperarm angle x, y, z
  158. - Lowerarm angle x, y, z
  159. All of these parameters can be set, incremented and decremented.
  160. Create the following node tree:
  161. ::
  162. main (Spatial) <- script is here
  163. +-arm (arm scene)
  164. + DirectionLight (DirectionLight)
  165. + Camera
  166. | Set up Camera so that arm is properly visible. Rotate DirectionLight
  167. so that arm is properly lit
  168. | while in scene play mode.
  169. Now we need to create new script under main:
  170. First we setup parameters:
  171. ::
  172. var lowerarm_angle = Vector3()
  173. var upperarm_angle = Vector3()
  174. | Now we need to setup way to change them. Just lets use keys for that.
  175. | Please create 7 actions under project settings:
  176. - **selext\_x** - bind to X key
  177. - **selext\_y** - bind to Y key
  178. - **selext\_z** - bind to Z key
  179. - **select\_upperarm** - bind to key 1
  180. - **select\_lowerarm** - bind to key 2
  181. - **increment** - bind to key numpad +
  182. - **decrement** - bind to key numpad -
  183. So now we want to adjust the above parameters. Therefore we create code
  184. which does that:
  185. ::
  186. func _ready():
  187. set_process(true)
  188. var bone = "upperarm"
  189. var coordinate = 0
  190. func _process(dt):
  191. if Input.is_action_pressed("select_x"):
  192. coordinate = 0
  193. elif Input.is_action_pressed("select_y"):
  194. coordinate = 1
  195. elif Input.is_action_pressed("select_z"):
  196. coordinate = 2
  197. elif Input.is_action_pressed("select_upperarm"):
  198. bone = "upperarm"
  199. elif Input.is_action_pressed("select_lowerarm"):
  200. bone = "lowerarm"
  201. elif Input.is_action_pressed("increment"):
  202. if bone == "lowerarm":
  203. lowerarm_angle[coordinate] += 1
  204. elif bone == "upperarm":
  205. upperarm_angle[coordinate] += 1
  206. The full code for arm control is this:
  207. ::
  208. extends Spatial
  209. # member variables here, example:
  210. # var a=2
  211. # var b="textvar"
  212. var upperarm_angle = Vector3()
  213. var lowerarm_angle = Vector3()
  214. var skel
  215. func _ready():
  216. skel = get_node("arm/Armature/Skeleton")
  217. set_process(true)
  218. var bone = "upperarm"
  219. var coordinate = 0
  220. func set_bone_rot(bone, ang):
  221. var b = skel.find_bone(bone)
  222. var rest = skel.get_bone_rest(b)
  223. var newpose = rest.rotated(Vector3(1.0, 0.0, 0.0), ang.x)
  224. var newpose = newpose.rotated(Vector3(0.0, 1.0, 0.0), ang.y)
  225. var newpose = newpose.rotated(Vector3(0.0, 0.0, 1.0), ang.z)
  226. skel.set_bone_pose(b, newpose)
  227. func _process(dt):
  228. if Input.is_action_pressed("select_x"):
  229. coordinate = 0
  230. elif Input.is_action_pressed("select_y"):
  231. coordinate = 1
  232. elif Input.is_action_pressed("select_z"):
  233. coordinate = 2
  234. elif Input.is_action_pressed("select_upperarm"):
  235. bone = "upperarm"
  236. elif Input.is_action_pressed("select_lowerarm"):
  237. bone = "lowerarm"
  238. elif Input.is_action_pressed("increment"):
  239. if bone == "lowerarm":
  240. lowerarm_angle[coordinate] += 1
  241. elif bone == "upperarm":
  242. upperarm_angle[coordinate] += 1
  243. elif Input.is_action_pressed("decrement"):
  244. if bone == "lowerarm":
  245. lowerarm_angle[coordinate] -= 1
  246. elif bone == "upperarm":
  247. upperarm_angle[coordinate] -= 1
  248. set_bone_rot("lowerarm", lowerarm_angle)
  249. set_bone_rot("upperarm", upperarm_angle)
  250. Pressing keys 1/2 select upperarm/lowerarm, select axis by pressing x,
  251. y, z, rotate using numpad "+"/"-"
  252. | This way you fully control your arm in FK mode using 2 bones. You can
  253. add additional bones and/or improve "feel" of the interface by using
  254. coefficients for the change. I recommend you play with this example a
  255. lot before going to next part.
  256. | You can clone the demo code for this chapter using
  257. ::
  258. git clone [email protected]:slapin/godot-skel3d.git
  259. cd demo1
  260. | Or you can browse it using web-interface:
  261. | https://github.com/slapin/godot-skel3d
  262. Using 3D "bones" to implement Inverse Kinematics
  263. ------------------------------------------------
  264. {{include(Inverse Kinematics)}}
  265. Using 3D "bones" to implement ragdoll-like physics
  266. --------------------------------------------------
  267. TBD