viewports.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. .. _doc_viewports:
  2. Viewports
  3. =========
  4. Introduction
  5. ------------
  6. Godot has a small but useful feature called viewports. Viewports
  7. are, as the 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 its 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 its 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 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) # 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 especially 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 its own World. This is useful when you want to
  82. instance 3D characters or objects in the 2D world.
  83. For 2D, each Viewport always contains its 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. # Retrieve the captured Image using get_data()
  93. var img = get_viewport().get_texture().get_data()
  94. # Also remember to flip the texture (because it's flipped)
  95. img.flip_y()
  96. # Convert Image to ImageTexture
  97. var tex = ImageTexture.new()
  98. tex.create_from_image(img)
  99. # Set Sprite Texture
  100. $sprite.texture = tex
  101. But if you use this in _ready() or from the first frame of the viewport's initialization
  102. you will get an empty texture cause there is nothing to get as texture. You can deal with
  103. it using (for example):
  104. ::
  105. # Let two frames pass to make sure the screen can be captured
  106. yield(get_tree(), "idle_frame")
  107. yield(get_tree(), "idle_frame")
  108. # You can get the image after this
  109. If the returned image is empty, capture still didn't happen, wait a
  110. little more, as this API is asynchronous.
  111. Sub-viewport
  112. ------------
  113. If the viewport is a child of a :ref:`ViewportContainer <class_viewportcontainer>`, it will become active and
  114. display anything it has inside. The layout is something like this:
  115. - ViewportContainer
  116. - Viewport
  117. The viewport will cover the area of its parent control completely, if stretch is set to true in Viewport Container.
  118. But you will have to setup the Viewport Size to get the the appropriate part of the Viewport.
  119. And Viewport Container can not be smaller than the size of the Viewport.
  120. .. image:: img/subviewport.png
  121. Render target
  122. -------------
  123. To set as a render target, toggle the "render target" property of
  124. the viewport to enabled. Note that whatever is inside will not be
  125. visible in the scene editor. To display the contents, the method remains the same.
  126. This can be requested via code using (for example):
  127. ::
  128. #This gets us the render_target texture
  129. var rtt = viewport.get_texture()
  130. sprite.texture = rtt
  131. By default, re-rendering of the render target happens when the render
  132. target texture has been drawn in a frame. If visible, it will be
  133. rendered, otherwise it will not. This behavior can be changed to manual
  134. rendering (once), or always render, no matter if visible or not.
  135. ``TODO: Review the doc, change outdated and add more images.``
  136. Make sure to check the viewport demos! Viewport folder in the demos
  137. archive available to download, or
  138. https://github.com/godotengine/godot-demo-projects/tree/master/viewport