working_with_3d_skeletons.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. .. _doc_working_with_3d_skeletons:
  2. Working with 3D skeletons
  3. =========================
  4. Godot 3D skeleton support is currently quite rudimentary. The
  5. :ref:`class_Skeleton` node and class were designed mainly to support importing
  6. skeletal animations as a set of transformation matrices.
  7. Skeleton node
  8. -------------
  9. The Skeleton node can be directly added anywhere you want on a scene. Usually
  10. mesh is a child of Skeleton, as it easier to manipulate this way, as
  11. Transforms within a skeleton are relative to where the Skeleton is. But you
  12. can specify a Skeleton node in every MeshInstance.
  13. Being obvious, Skeleton is intended to deform meshes, and consists of
  14. structures called "bones". Each "bone" is represented as a Transform, which is
  15. applied to a group of vertices within a mesh. You can directly control a group
  16. of vertices from Godot. For that please reference the :ref:`class_MeshDataTool`
  17. class and its method :ref:`set_vertex_bones <class_MeshDataTool_set_vertex_bones>`.
  18. The "bones" are organized hierarchically, every bone, except for root
  19. bone(s) have a parent. Every bone has an associated name you can use to
  20. refer to it (e.g. "root" or "hand.L", etc.). Also all bones are 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 an arm mesh with 2 bones -
  29. upperarm and lowerarm, with the lowerarm bone parented to the upperarm.
  30. Skeleton class
  31. --------------
  32. You can view Godots internal help for descriptions of all functions.
  33. Basically all operations on bones are done using their numeric ID. You
  34. can convert from a name to a numeric ID and vice versa.
  35. **To find the number of bones in a skeleton we use the 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 the ID of a bone, use the 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 the ID, not just printing it.
  55. Also, we might need additional information - finding bone parents to
  56. complete chains, etc. This is done with the get/set_bone\_\* functions.
  57. **To find the parent of a bone we use the 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. The bone transforms are the things of our interest here. There are 3 kind of
  68. transforms - local, global, custom.
  69. **To find the local Transform of a bone 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 get a 3x4 matrix there, with the first column filled with 1s. What can we do
  82. with this matrix? It is a Transform, so we can do everything we can do with
  83. Transforms, basically translate, rotate and scale. We could also multiply
  84. transforms to have more complex transforms. Remember, "bones" in Godot are
  85. just Transforms over a group of vertices. We could also 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(delta):
  101. var t = skel.get_bone_pose(id)
  102. t = t.rotated(Vector3(0.0, 1.0, 0.0), 0.1 * delta)
  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 check the results.
  106. What we used here was the local pose. By default all bones are not modified.
  107. But this Transform tells us nothing about the relationship between bones.
  108. This information is needed for quite a number of tasks. How can we get
  109. it? Here the global transform comes into play:
  110. **To find the bone global Transform we use get_bone_global_pose(id)
  111. function:**
  112. Let's find the global Transform for the 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 can see, this transform is not zeroed. While being called global, it
  125. is actually relative to the Skeleton origin. For a root bone, origin is always
  126. at 0 if not modified. Lets print the 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 the 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 the bone tip? We can't know things like the bone length,
  142. which we need for many things, without knowing the tip location. For all
  143. bones in a chain except for the last one we can calculate the tip location - it is
  144. simply a child bone's 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. A 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 an extra bone to the chain or just
  150. calculating the length of the leaf bone in Blender and storing the value in your
  151. script.
  152. Using 3D "bones" for mesh control
  153. ---------------------------------
  154. Now as you know the 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 the Camera so that the arm is properly visible. Rotate DirectionLight
  167. so that the arm is properly lit while in scene play mode.
  168. Now we need to create a new script under main:
  169. First we define the setup parameters:
  170. ::
  171. var lowerarm_angle = Vector3()
  172. var upperarm_angle = Vector3()
  173. Now we need to setup a way to change them. Let us use keys for that.
  174. Please create 7 actions under project settings -> Input Map:
  175. - **selext_x** - bind to X key
  176. - **selext_y** - bind to Y key
  177. - **selext_z** - bind to Z key
  178. - **select_upperarm** - bind to key 1
  179. - **select_lowerarm** - bind to key 2
  180. - **increment** - bind to key numpad +
  181. - **decrement** - bind to key numpad -
  182. So now we want to adjust the above parameters. Therefore we create code
  183. which does that:
  184. ::
  185. func _ready():
  186. set_process(true)
  187. var bone = "upperarm"
  188. var coordinate = 0
  189. func _process(delta):
  190. if Input.is_action_pressed("select_x"):
  191. coordinate = 0
  192. elif Input.is_action_pressed("select_y"):
  193. coordinate = 1
  194. elif Input.is_action_pressed("select_z"):
  195. coordinate = 2
  196. elif Input.is_action_pressed("select_upperarm"):
  197. bone = "upperarm"
  198. elif Input.is_action_pressed("select_lowerarm"):
  199. bone = "lowerarm"
  200. elif Input.is_action_pressed("increment"):
  201. if bone == "lowerarm":
  202. lowerarm_angle[coordinate] += 1
  203. elif bone == "upperarm":
  204. upperarm_angle[coordinate] += 1
  205. The full code for arm control is this:
  206. ::
  207. extends Spatial
  208. # member variables here, example:
  209. # var a=2
  210. # var b="textvar"
  211. var upperarm_angle = Vector3()
  212. var lowerarm_angle = Vector3()
  213. var skel
  214. func _ready():
  215. skel = get_node("arm/Armature/Skeleton")
  216. set_process(true)
  217. var bone = "upperarm"
  218. var coordinate = 0
  219. func set_bone_rot(bone, ang):
  220. var b = skel.find_bone(bone)
  221. var rest = skel.get_bone_rest(b)
  222. var newpose = rest.rotated(Vector3(1.0, 0.0, 0.0), ang.x)
  223. var newpose = newpose.rotated(Vector3(0.0, 1.0, 0.0), ang.y)
  224. var newpose = newpose.rotated(Vector3(0.0, 0.0, 1.0), ang.z)
  225. skel.set_bone_pose(b, newpose)
  226. func _process(delta):
  227. if Input.is_action_pressed("select_x"):
  228. coordinate = 0
  229. elif Input.is_action_pressed("select_y"):
  230. coordinate = 1
  231. elif Input.is_action_pressed("select_z"):
  232. coordinate = 2
  233. elif Input.is_action_pressed("select_upperarm"):
  234. bone = "upperarm"
  235. elif Input.is_action_pressed("select_lowerarm"):
  236. bone = "lowerarm"
  237. elif Input.is_action_pressed("increment"):
  238. if bone == "lowerarm":
  239. lowerarm_angle[coordinate] += 1
  240. elif bone == "upperarm":
  241. upperarm_angle[coordinate] += 1
  242. elif Input.is_action_pressed("decrement"):
  243. if bone == "lowerarm":
  244. lowerarm_angle[coordinate] -= 1
  245. elif bone == "upperarm":
  246. upperarm_angle[coordinate] -= 1
  247. set_bone_rot("lowerarm", lowerarm_angle)
  248. set_bone_rot("upperarm", upperarm_angle)
  249. Pressing keys 1/2 selects upperarm/lowerarm, select the axis by pressing x,
  250. y, z, rotate using numpad "+"/"-"
  251. This way you fully control your arm in FK mode using 2 bones. You can
  252. add additional bones and/or improve the "feel" of the interface by using
  253. coefficients for the change. I recommend you play with this example a
  254. lot before going to next part.
  255. You can clone the demo code for this chapter using
  256. ::
  257. git clone [email protected]:slapin/godot-skel3d.git
  258. cd demo1
  259. Or you can browse it using the web-interface:
  260. https://github.com/slapin/godot-skel3d
  261. Using 3D "bones" to implement Inverse Kinematics
  262. ------------------------------------------------
  263. See :ref:`doc_inverse_kinematics`.
  264. Using 3D "bones" to implement ragdoll-like physics
  265. --------------------------------------------------
  266. TODO.