canvas_layers.rst 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. .. _doc_canvas_layers:
  2. Canvas layers
  3. =============
  4. Viewport and Canvas items
  5. -------------------------
  6. Regular 2D nodes, such as :ref:`Node2D <class_Node2D>` or
  7. :ref:`Control <class_Control>` both inherit from
  8. :ref:`CanvasItem <class_CanvasItem>`, which is the base for all 2D
  9. nodes. CanvasItems can be arranged in trees and they will inherit
  10. their transform. This means that when moving the parent, the children
  11. will be moved too.
  12. These nodes are placed as direct or indirect children to a
  13. :ref:`Viewport <class_Viewport>`, and will be displayed through it.
  14. Viewport has the property
  15. :ref:`Viewport.canvas_transform <class_Viewport_canvas_transform>`,
  16. which allows to transform all the CanvasItem hierarchy by a custom
  17. :ref:`Transform2D <class_Transform2D>` transform. Nodes such as
  18. :ref:`Camera2D <class_Camera2D>`, work by changing that transform.
  19. Changing the canvas transform is useful because it is a lot more
  20. efficient than moving the root canvas item (and hence the whole scene).
  21. Canvas transform is a simple matrix that offsets the whole 2D drawing,
  22. so it's the most efficient way to do scrolling.
  23. Not enough...
  24. -------------
  25. But this is not enough. There are often situations where the game or
  26. application may not want *everything* transformed by the canvas
  27. transform. Examples of this are:
  28. - **Parallax Backgrounds**: Backgrounds that move slower than the rest
  29. of the stage.
  30. - **HUD**: Heads-up display, or user interface. If the world moves,
  31. the life counter, score, etc. must stay static.
  32. - **Transitions**: Effects used for transitions (fades, blends) may
  33. also want it to remain at a fixed location.
  34. How can these problems be solved in a single scene tree?
  35. CanvasLayers
  36. ------------
  37. The answer is :ref:`CanvasLayer <class_CanvasLayer>`,
  38. which is a node that adds a separate 2D rendering layer for all its
  39. children and grand-children. Viewport children will draw by default at
  40. layer "0", while a CanvasLayer will draw at any numeric layer. Layers
  41. with a greater number will be drawn above those with a smaller number.
  42. CanvasLayers also have their own transform and do not depend on the
  43. transform of other layers. This allows the UI to be fixed in-place
  44. while the world moves.
  45. An example of this is creating a parallax background. This can be done
  46. with a CanvasLayer at layer "-1". The screen with the points, life
  47. counter and pause button can also be created at layer "1".
  48. Here's a diagram of how it looks:
  49. .. image:: img/canvaslayers.png
  50. CanvasLayers are independent of tree order, and they only depend on
  51. their layer number, so they can be instantiated when needed.
  52. Performance
  53. -----------
  54. Even though there shouldn't be any performance limitation, it is not
  55. advised to use excessive amount of layers to arrange drawing order of
  56. nodes. The most optimal way will always be arranging them by tree order.
  57. 2d nodes also have a property for controlling their drawing order
  58. (see :ref:`Node2D.z_index <class_Node2D_z_index>`).