canvas_layers.rst 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. Canvas Layers
  2. =============
  3. Viewport and Canvas Items
  4. -------------------------
  5. Regular 2D nodes, such as
  6. `Node2D <https://github.com/okamstudio/godot/wiki/class_node2d>`__ or
  7. `Control <https://github.com/okamstudio/godot/wiki/class_control>`__
  8. both inherit from
  9. `CanvasItem <https://github.com/okamstudio/godot/wiki/class_canvasitem>`__,
  10. which is the base for all 2D nodes. CanvasItems can be arranged in trees
  11. and they will inherit their transform. This means that, moving the
  12. parent, the children will be moved too.
  13. | These nodes are placed as direct or indirect children to a
  14. `Viewport <https://github.com/okamstudio/godot/wiki/class_viewport>`__,
  15. and will be displayed through it.
  16. | Viewport has a property "canvas\_transform"
  17. (`Viewport.set\_canvas\_transform() <https://github.com/okamstudio/godot/wiki/class_viewport#set_canvas_transform)>`__,
  18. which allows to transform all the CanvasItem hierarchy by a custom
  19. `Matrix32 <https://github.com/okamstudio/godot/wiki/class_matrix32>`__
  20. transform. Nodes such as
  21. `Camera2D <https://github.com/okamstudio/godot/wiki/class_camera2d>`__,
  22. work by changing that transform.
  23. Changing the canvas transform is useful because it is a lot more
  24. efficient than moving the root canvas item (and hence the whole scene).
  25. Canvas transform is a simple matrix that offsets the whole 2D drawing,
  26. so it's the most efficient way to do scrolling.
  27. Not Enough..
  28. ------------
  29. But this is not enough. There are often situations where the game or
  30. application may not want *everything* transformed by the canvas
  31. transform. Examples of this are:
  32. - **Parallax Backgrounds**: Backgrounds that move slower than the rest
  33. of the stage.
  34. - **HUD**: Head's up display, or user interface. If the world moves,
  35. the life counter, points, etc must stay static.
  36. - **Transitions**: Effects used for transitions (fades, blends) may
  37. also want it to remain at a fixed location.
  38. How can these problems be solved in a single scene tree?
  39. CanvasLayers
  40. ------------
  41. The answer is
  42. `CanvasLayer <https://github.com/okamstudio/godot/wiki/class_canvaslayer>`__,
  43. which is a node that adds a separate 2D rendering layer for all it's
  44. children and grand-children. Viewport children will draw by default at
  45. layer "0", while a CanvasLayer will draw at any numeric layer. Layers
  46. with a greater number will be drawn above those with a smaller number.
  47. CanvasLayers also have their own transform, and do not depend of the
  48. transform of other layers. This allows the UI to be fixed in-place,
  49. while the word moves.
  50. An example of this is creating a parallax background. This can be done
  51. with a CanvasLayer at layer "-1". The screen with the points, life
  52. counter and pause button can also be created at layer "1".
  53. Here's a diagram of how it looks:
  54. .. image:: /img/canvaslayers.png
  55. CanvasLayers are independent of tree order, and they only depend on
  56. their layer number, so they can be instantiated when needed.
  57. Performance
  58. -----------
  59. Even though there shouldn't be any performance limitation, it is not
  60. advised to use excessive amount of layers to arrange drawing order of
  61. nodes. The most optimal way will always be arranging them by tree order.
  62. In the future, nodes will also have a priority or sub-layer index which
  63. should aid for this.
  64. *Juan Linietsky, Ariel Manzur, Distributed under the terms of the `CC
  65. By <https://creativecommons.org/licenses/by/3.0/legalcode>`__ license.*