2d_skeletons.rst 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. .. _doc_2d_skeletons:
  2. 2D skeletons
  3. ============
  4. .. warning::
  5. There are known issues with 2D skeletons on mobile and web platforms with the GLES2 renderer. We
  6. recommend using the GLES3 renderer if your project relies on Skeleton2D for now.
  7. Introduction
  8. ------------
  9. When working with 3D, skeletal deforms are common for characters and creatures
  10. and most 3D modelling applications support it. For 2D, as this function is not
  11. used as often, it's difficult to find mainstream software aimed for this.
  12. One option is to create animations in third-party software such as Spine or
  13. Dragonbones. From Godot 3.1 onwards, though, this functionality is supported
  14. built-in.
  15. Why would you want to do skeletal animations directly in Godot? The answer is
  16. that there are many advantages to it:
  17. * Better integration with the engine, so less hassle importing and editing from
  18. an external tool.
  19. * Ability to control particle systems, shaders, sounds, call scripts, colors,
  20. transparency, etc. in animations.
  21. * The built-in skeletal system in Godot is very efficient and designed for
  22. performance.
  23. The following tutorial will, then, explain 2D skeletal deformations.
  24. Setup
  25. -----
  26. .. seealso::
  27. Before starting, we recommend you to go through the
  28. :ref:`doc_cutout_animation` tutorial to gain a general understanding of
  29. animating within Godot.
  30. For this tutorial, we will be using a single image to construct our character.
  31. Download it from :download:`gBot_pieces.png <img/gBot_pieces.png>` or save the
  32. image below.
  33. .. image:: img/gBot_pieces.png
  34. It is also advised to download the final character image
  35. :download:`gBot_complete.png <img/gBot_complete.png>` to have a good reference
  36. for putting the different pieces together.
  37. .. image:: img/gBot_complete.png
  38. Creating the polygons
  39. ---------------------
  40. Create a new scene for your model (if it's going to be an animated character,
  41. you may want to use a ``KinematicBody2D``). For ease of use, an empty 2D node is
  42. created as a root for the polygons.
  43. Begin with a ``Polygon2D`` node. There is no need to place it anywhere in the
  44. scene for now, so simply create it like this:
  45. .. image:: img/skel2d1.png
  46. Select it and assign the texture with the character pieces you have downloaded
  47. before:
  48. .. image:: img/skel2d2.png
  49. Drawing a polygon directly is not advised. Instead, open the "UV" dialog for the
  50. polygon:
  51. .. image:: img/skel2d3.png
  52. Head over to the *Points* mode, select the pencil and draw a polygon around the
  53. desired piece:
  54. .. image:: img/skel2d4.png
  55. Duplicate the polygon node and give it a proper name. Then, enter the "UV"
  56. dialog again and replace the old polygon with another one in the new desired
  57. piece.
  58. When you duplicate nodes and the next piece has a similar shape, you can edit
  59. the previous polygon instead of drawing a new one.
  60. After moving the polygon, remember to update the UV by selecting
  61. **Edit > Copy Polygon to UV** in the Polygon 2D UV Editor.
  62. .. image:: img/skel2d5.png
  63. Keep doing this until you mapped all pieces.
  64. .. image:: img/skel2d6.png
  65. You will notice that pieces for nodes appear in the same layout as they do in
  66. the original texture. This is because by default, when you draw a polygon, the
  67. UV and points are the same.
  68. Rearrange the pieces and build the character. This should be pretty quick. There
  69. is no need to change pivots, so don't bother making sure rotation pivots for
  70. each piece are right; you can leave them be for now.
  71. .. image:: img/skel2d7.png
  72. Ah, the visual order of the pieces is not correct yet, as some are covering
  73. wrong pieces. Rearrange the order of the nodes to fix this:
  74. .. image:: img/skel2d8.png
  75. And there you go! It was definitely much easier than in the cutout tutorial.
  76. Creating the skeleton
  77. ---------------------
  78. Create a ``Skeleton2D`` node as a child of the root node. This will be the base
  79. of our skeleton:
  80. .. image:: img/skel2d9.png
  81. Create a ``Bone2D`` node as a child of the skeleton. Put it on the hip (usually
  82. skeletons start here). The bone will be pointing to the right, but you can
  83. ignore this for now.
  84. .. image:: img/skel2d10.png
  85. Keep creating bones in hierarchy and naming them accordingly.
  86. .. image:: img/skel2d11.png
  87. At the end of this chain, there will be a *jaw* node. It is, again, very short
  88. and pointing to the right. This is normal for bones without children. The length
  89. of *tip* bones can be changed with a property in the inspector:
  90. .. image:: img/skel2d12.png
  91. In this case, we don't need to rotate the bone (coincidentally the jaw points
  92. right in the sprite), but in case you need to, feel free to do it. Again, this
  93. is only really needed for tip bones as nodes with children don't usually need a
  94. length or a specific rotation.
  95. Keep going and build the whole skeleton:
  96. .. image:: img/skel2d13.png
  97. You will notice that all bones raise an annoying warning about a missing rest
  98. pose. This means that it's time to set one. Go to the *skeleton* node and create
  99. a rest pose. This pose is the default one, you can come back to it anytime you
  100. want (which is very handy for animating):
  101. .. image:: img/skel2d14.png
  102. The warnings will go away. If you modify the skeleton (add/remove bones) you
  103. will need to set the rest pose again.
  104. Deforming the polygons
  105. ----------------------
  106. Select the previously created polygons and assign the skeleton node to their
  107. ``Skeleton`` property. This will ensure that they can eventually be deformed by
  108. it.
  109. .. image:: img/skel2d15.png
  110. Click the property highlighted above and select the skeleton node:
  111. .. image:: img/skel2d16.png
  112. Again, open the UV editor for the polygon and go to the *Bones* section.
  113. .. image:: img/skel2d17.png
  114. You will not be able to paint weights yet. For this you need to synchronize the
  115. list of bones from the skeleton with the polygon. This step is done only once
  116. and manually (unless you modify the skeleton by adding/removing/renaming bones).
  117. It ensures that your rigging information is kept in the polygon, even if a
  118. skeleton node is accidentally lost or the skeleton modified. Push the "Sync
  119. Bones to Polygon" button to sync the list.
  120. .. image:: img/skel2d18.png
  121. The list of bones will automatically appear. By default, your polygon has no
  122. weight assigned to any of them. Select the bones you want to assign weight to
  123. and paint them:
  124. .. image:: img/skel2d19.png
  125. Points in white have a full weight assigned, while points in black are not
  126. influenced by the bone. If the same point is painted white for multiple bones,
  127. the influence will be distributed amongst them (so usually there is not that
  128. much need to use shades in-between unless you want to polish the bending
  129. effect).
  130. .. image:: img/skel2d20.gif
  131. After painting the weights, animating the bones (NOT the polygons!) will have
  132. the desired effect of modifying and bending the polygons accordingly. As you
  133. only need to animate bones in this approach, work becomes much easier!
  134. But it's not all roses. Trying to animate bones that bend the polygon will often
  135. yield unexpected results:
  136. .. image:: img/skel2d21.gif
  137. This happens because Godot generates internal triangles that connect the points
  138. when drawing the polygon. They don't always bend the way you would expect. To
  139. solve this, you need to set hints in the geometry to clarify how you expect it
  140. to deform.
  141. Internal vertices
  142. -----------------
  143. Open the UV menu for each bone again and go to the *Points* section. Add some
  144. internal vertices in the regions where you expect the geometry to bend:
  145. .. image:: img/skel2d22.png
  146. Now, go to the *Polygon* section and redraw your own polygons with more detail.
  147. Imagine that, as your polygons bend, you need to make sure they deform the least
  148. possible, so experiment a bit to find the right setup.
  149. .. image:: img/skel2d23.png
  150. Once you start drawing, the original polygon will disappear and you will be free
  151. to create your own:
  152. .. image:: img/skel2d24.png
  153. This amount of detail is usually fine, though you may want to have more
  154. fine-grained control over where triangles go. Experiment by yourself until you
  155. get the results you like.
  156. **Note:** Don't forget that your newly added internal vertices also need weight
  157. painting! Go to the *Bones* section again to assign them to the right bones.
  158. Once you are all set, you will get much better results:
  159. .. image:: img/skel2d25.gif