viewports.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. .. _doc_viewports:
  2. Viewports
  3. =========
  4. Introduction
  5. ------------
  6. Godot has a small but very useful feature called viewports. Viewports
  7. are, as they name implies, rectangles where the world is drawn. They
  8. have three main uses, but can flexibly adapted to a lot more. All this
  9. is done via the :ref:`Viewport <class_Viewport>` node.
  10. .. image:: /img/viewportnode.png
  11. The main uses in question are:
  12. - **Scene Root**: The root of the active scene is always a Viewport.
  13. This is what displays the scenes created by the user. (You should
  14. know this by having read previous tutorials!)
  15. - **Sub-Viewports**: These can be created when a Viewport is a child of
  16. a :ref:`Control <class_Control>`.
  17. - **Render Targets**: Viewports can be set to "RenderTarget" mode. This
  18. means that the viewport is not directly visible, but it's contents
  19. can be accessed via a :ref:`Texture <class_Texture>`.
  20. Input
  21. -----
  22. Viewports are also responsible of delivering properly adjusted and
  23. scaled input events to all it's children nodes. Both the root viewport
  24. and sub-viewports do this automatically, but render targets do not.
  25. Because of this, the user must do it manually via the
  26. :ref:`Viewport.input() <class_Viewport_input>` function if needed.
  27. Listener
  28. --------
  29. Godot supports 3D sound (in both 2D and 3D nodes), more on this can be
  30. found in another tutorial (one day..). For this type of sound to be
  31. audible, the viewport needs to be enabled as a listener (for 2D or 3D).
  32. If you are using a custom viewport to display your world, don't forget
  33. to enable this!
  34. Cameras (2D & 3D)
  35. -----------------
  36. When using a 2D or 3D :ref:`Camera <class_Camera>` /
  37. :ref:`Camera2D <class_Camera2D>`, cameras will always display on the
  38. closest parent viewport (going towards the root). For example, in the
  39. following hierarchy:
  40. - Viewport
  41. - Camera
  42. Camera will display on the parent viewport, but in the following one:
  43. - Camera
  44. - Viewport
  45. It will not (or may display in the root viewport if this is a subscene).
  46. There can be only one active camera per viewport, so if there is more
  47. than one, make sure that the desired one has the "current" property set,
  48. or make it the current camera by calling:
  49. ::
  50. camera.make_current()
  51. Scale & stretching
  52. ------------------
  53. Viewports have a "rect" property. X and Y are often not used (only the
  54. root viewport really uses them), while WIDTH AND HEIGHT represent the
  55. size of the viewport in pixels. For Sub-Viewports, these values are
  56. overridden by the ones from the parent control, but for render targets
  57. this sets their resolution.
  58. It is also possible to scale the 2D content and make it believe the
  59. viewport resolution is other than the one specified in the rect, by
  60. calling:
  61. ::
  62. viewport.set_size_override(w,h) #custom size for 2D
  63. viewport.set_size_override_stretch(true/false) #enable stretch for custom size
  64. The root viewport uses this for the stretch options in the project
  65. settings.
  66. Worlds
  67. ------
  68. For 3D, a Viewport will contain a :ref:`World <class_World>`. This
  69. is basically the universe that links physics and rendering together.
  70. Spatial-base nodes will register using the World of the closest
  71. viewport. By default, newly created viewports do not contain a World but
  72. use the same as a parent viewport (root viewport does contain one
  73. though, which is the one objects are rendered to by default). A world can
  74. be set in a viewport using the "world" property, and that will separate
  75. all children nodes of that viewport from interacting with the parent
  76. viewport world. This is specially useful in scenarios where, for
  77. example, you might want to show a separate character in 3D imposed over
  78. the game (like in Starcraft).
  79. As a helper for situations where you want to create viewports that
  80. display single objects and don't want to create a world, viewport has
  81. the option to use it's own World. This is very useful when you want to
  82. instance 3D characters or objects in the 2D world.
  83. For 2D, each Viewport always contains it's own :ref:`World2D <class_World2D>`.
  84. This suffices in most cases, but in case sharing them may be desired, it
  85. is possible to do so by calling the viewport API manually.
  86. Capture
  87. -------
  88. It is possible to query a capture of the viewport contents. For the root
  89. viewport this is effectively a screen capture. This is done with the
  90. following API:
  91. ::
  92. # queues a screen capture, will not happen immediately
  93. viewport.queue_screen_capture()
  94. After a frame or two (check _process()), the capture will be ready,
  95. get it back by using:
  96. ::
  97. var capture = viewport.get_screen_capture()
  98. If the returned image is empty, capture still didn't happen, wait a
  99. little more, as this API is asyncronous.
  100. Sub-viewport
  101. ------------
  102. If the viewport is a child of a control, it will become active and
  103. display anything it has inside. The layout is something like this:
  104. - Control
  105. - Viewport
  106. The viewport will cover the area of it's parent control completely.
  107. .. image:: /img/subviewport.png
  108. Render target
  109. -------------
  110. To set as a render target, just toggle the "render target" property of
  111. the viewport to enabled. Note that whatever is inside will not be
  112. visible in the scene editor. To display the contents, the render target
  113. texture must be used. This can be requested via code using (for
  114. example):
  115. ::
  116. var rtt = viewport.get_render_target_texture()
  117. sprite.set_texture(rtt)
  118. By default, re-rendering of the render target happens when the render
  119. target texture has been drawn in a frame. If visible, it will be
  120. rendered, otherwise it will not. This behavior can be changed to manual
  121. rendering (once), or always render, no matter if visible or not.
  122. A few classes are created to make this easier in most common cases
  123. inside the editor:
  124. - :ref:`ViewportSprite <class_ViewportSprite>` (for 2D).
  125. - ViewportQuad (for 3D).
  126. - ViewportFrame (for GUI).
  127. *TODO: Review the doc, ViewportQuad and ViewportFrame don't exist in 2.0.*
  128. Make sure to check the viewport demos! Viewport folder in the demos
  129. archive available to download, or
  130. https://github.com/godotengine/godot/tree/master/demos/viewport