viewports.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. .. _doc_viewports:
  2. Viewports
  3. =========
  4. Introduction
  5. ------------
  6. Think of :ref:`Viewports <class_Viewport>` as a screen that the game is projected onto. In order
  7. to see the game we need to have a surface to draw it on, this surface is
  8. the Root :ref:`Viewport <class_Viewport>`.
  9. .. image:: img/viewportnode.png
  10. :ref:`Viewports <class_Viewport>` can also be added to the scene so that there
  11. are multiple surfaces to draw on. When we are drawing to a :ref:`Viewport <class_Viewport>`
  12. that is not the Root we call it a render target. We can access the contents
  13. of a render target by accessing its corresponding :ref:`texture <class_ViewportTexture>`.
  14. By using a :ref:`Viewport <class_Viewport>` as a render target
  15. we can either render multiple scenes simultaneously or we can render to
  16. a :ref:`texture <class_ViewportTexture>` which is applied to an object in the scene, for example a dynamic
  17. skybox.
  18. :ref:`Viewports <class_Viewport>` have a variety of use cases including:
  19. - Rendering 3d objects within a 2d game
  20. - Rendering 2d elements in a 3d game
  21. - Rendering dynamic textures
  22. - Generating procedural textures at runtime
  23. - Rendering multiple cameras in the same scene
  24. What all these use cases have in common is that you are given the ability to
  25. draw objects to a texture as if it were another screen and then you can choose
  26. what to do with the resulting texture.
  27. Input
  28. -----
  29. :ref:`Viewports <class_Viewport>` are also responsible for delivering properly adjusted and
  30. scaled input events to all their children nodes. Typically input is received by the
  31. nearest :ref:`Viewport <class_Viewport>` in the tree, but you can set :ref:`Viewports <class_Viewport>` to not recieve input by checking
  32. 'Disable Input' to 'on', this will allow the next nearest :ref:`Viewport <class_Viewport>` in the tree to capture
  33. the input.
  34. .. image:: img/input.png
  35. For more information on how Godot handles input please read the :ref:`Input Event Tutorial<doc_inputevent>`.
  36. Listener
  37. --------
  38. Godot supports 3D sound (in both 2D and 3D nodes), more on this can be
  39. found in the :ref:`Audio Streams Tutorial<doc_audio-streams>`. For this type of sound to be
  40. audible, the :ref:`Viewport <class_Viewport>` needs to be enabled as a listener (for 2D or 3D).
  41. If you are using a custom :ref:`Viewport <class_Viewport>` to display your :ref:`World <class_World>`, don't forget
  42. to enable this!
  43. Cameras (2D & 3D)
  44. -----------------
  45. When using a :ref:`Camera <class_Camera>` /
  46. :ref:`Camera2D <class_Camera2D>`, cameras will always display on the
  47. closest parent :ref:`Viewport <class_Viewport>` (going towards the root). For example, in the
  48. following hierarchy:
  49. .. image:: img/cameras.png
  50. CameraA will display on the Root :ref:`Viewport <class_Viewport>` and it will draw MeshA. CameraB
  51. will be captured by the :ref:`Viewport <class_Viewport>` Node along with MeshB. Even though MeshB is in the scene
  52. heirarchy, it will still not be drawn to the Root :ref:`Viewport <class_Viewport>`. Similarly MeshA will not
  53. be visible from the :ref:`Viewport <class_Viewport>` node becuase :ref:`Viewport <class_Viewport>` nodes only capture nodes below them
  54. in the heirarchy.
  55. There can only be one active camera per :ref:`Viewport <class_Viewport>`, so if there is more
  56. than one, make sure that the desired one has the "current" property set,
  57. or make it the current camera by calling:
  58. ::
  59. camera.make_current()
  60. Scale & stretching
  61. ------------------
  62. :ref:`Viewports <class_Viewport>` have a "size" property which represents the size of the :ref:`Viewport <class_Viewport>`
  63. in pixels. For :ref:`Viewports <class_Viewport>` which are children of :ref:`ViewportContainers <class_viewportcontainer>`,
  64. these values are overridden, but for all others this sets their resolution.
  65. It is also possible to scale the 2D content and make the :ref:`Viewport <class_Viewport>` resolution
  66. different than the one specified in size, by calling:
  67. ::
  68. viewport.set_size_override(w, h) # custom size for 2D
  69. viewport.set_size_override_stretch(true) # enable stretch for custom size
  70. The root :ref:`Viewport <class_Viewport>` uses this for the stretch options in the project
  71. settings. For more information on scaling and stretching visit the :ref:`Multiple Resolutions Tutorial <doc_multiple_resolutions>`
  72. Worlds
  73. ------
  74. For 3D, a :ref:`Viewport <class_Viewport>` will contain a :ref:`World <class_World>`. This
  75. is basically the universe that links physics and rendering together.
  76. Spatial-base nodes will register using the :ref:`World <class_World>` of the closest
  77. :ref:`Viewport <class_Viewport>`. By default, newly created :ref:`Viewports <class_Viewport>` do not contain a :ref:`World <class_World>` but
  78. use the same as their parent :ref:`Viewport <class_Viewport>` (root :ref:`Viewport <class_Viewport>` always contains a
  79. :ref:`World <class_World>`, which is the one objects are rendered to by default). A :ref:`World <class_World>` can
  80. be set in a :ref:`Viewport <class_Viewport>` using the "world" property, and that will separate
  81. all children nodes of that :ref:`Viewport <class_Viewport>` from interacting with the parent
  82. :ref:`Viewport's <class_Viewport>` :ref:`World <class_World>`. This is especially useful in scenarios where, for
  83. example, you might want to show a separate character in 3D imposed over
  84. the game (like in Starcraft).
  85. As a helper for situations where you want to create :ref:`Viewports <class_Viewport>` that
  86. display single objects and don't want to create a :ref:`World <class_World>`, :ref:`Viewport <class_Viewport>` has
  87. the option to use its own :ref:`World <class_World>`. This is useful when you want to
  88. instance 3D characters or objects in a 2D :ref:`World <class_World2D>`.
  89. For 2D, each :ref:`Viewport <class_Viewport>` always contains its own :ref:`World2D <class_World2D>`.
  90. This suffices in most cases, but in case sharing them may be desired, it
  91. is possible to do so by setting the :ref:`Viewport's <class_Viewport>` :ref:`World2D <class_World2D>` manually.
  92. For an example of how this works see the demo projects `3D in 2D <https://github.com/godotengine/godot-demo-projects/tree/master/viewport/3d_in_2d>`_ and `2D in 3D <https://github.com/godotengine/godot-demo-projects/tree/master/viewport/2d_in_3d>`_ respectively.
  93. Capture
  94. -------
  95. It is possible to query a capture of the :ref:`Viewport <class_Viewport>` contents. For the root
  96. :ref:`Viewport <class_Viewport>` this is effectively a screen capture. This is done with the
  97. following code:
  98. ::
  99. # Retrieve the captured Image using get_data()
  100. var img = get_viewport().get_texture().get_data()
  101. # Flip on the y axis
  102. # You can also set "V Flip" to true if not on the Root Viewport
  103. img.flip_y()
  104. # Convert Image to ImageTexture
  105. var tex = ImageTexture.new()
  106. tex.create_from_image(img)
  107. # Set Sprite Texture
  108. $sprite.texture = tex
  109. But if you use this in _ready() or from the first frame of the :ref:`Viewport's <class_Viewport>` initialization
  110. you will get an empty texture cause there is nothing to get as texture. You can deal with
  111. it using (for example):
  112. ::
  113. # Let two frames pass to make sure the screen can be captured
  114. yield(get_tree(), "idle_frame")
  115. yield(get_tree(), "idle_frame")
  116. # You can get the image after this
  117. If the returned image is empty, capture still didn't happen, wait a
  118. little more, as Godot's rendering API is asynchronous. For a working example of this
  119. check out the `Screen Capture example <https://github.com/godotengine/godot-demo-projects/tree/master/viewport/screen_capture>`_ in the demo projects
  120. Viewport Container
  121. ------------------
  122. If the :ref:`Viewport <class_Viewport>` is a child of a :ref:`ViewportContainer <class_viewportcontainer>`, it will become active and
  123. display anything it has inside. The layout looks like this:
  124. .. image:: img/container.png
  125. The :ref:`Viewport <class_Viewport>` will cover the area of its parent :ref:`ViewportContainer <class_viewportcontainer>` completely
  126. if stretch is set to true in :ref:`ViewportContainer <class_viewportcontainer>`.
  127. Note: The size of the :ref:`ViewportContainer <class_viewportcontainer>` cannot be smaller than the size of the :ref:`Viewport <class_Viewport>`.
  128. Rendering
  129. ---------
  130. Due to the fact that the :ref:`Viewport <class_Viewport>` is an entryway into another rendering surface, it exposes a few
  131. rendering properties that can be different from the project settings. The first is MSAA, you can
  132. choose to use a different level of MSAA for each :ref:`Viewport <class_Viewport>`, the default behavior is DISABLED.
  133. You can also set the :ref:`Viewport <class_Viewport>` to use HDR, HDR is very useful for when you want to store values in the
  134. texture that are outside the range 0.0 - 1.0.
  135. If you know how the :ref:`Viewport <class_Viewport>` is going to be used, you can set its Usage to either 3D or 2D. Godot will then
  136. restrict how the :ref:`Viewport <class_Viewport>` is drawn to in accordance with your choice, default is 3D.
  137. Godot also provides a way of customizing how everything is drawn inside :ref:`Viewports <class_Viewport>` using “Debug Draw”.
  138. Debug Draw allows you to specify one of four options for how the :ref:`Viewport <class_Viewport>` will display things drawn
  139. inside it. Debug Draw is disabled by default.
  140. .. image:: img/default_scene.png
  141. *A scene drawn with Debug Draw disabled*
  142. The other three options are Unshaded, Overdraw, and Wireframe. Unshaded draws the scene
  143. without using lighting information so all the objects appear flatly colored the color of
  144. their albedo.
  145. .. image:: img/unshaded.png
  146. *The same scene with Debug Draw set to Unshaded*
  147. Overdraw draws the meshes semi-transparent with an additive blend so you can see how the meshes overlap.
  148. .. image:: img/overdraw.png
  149. *The same scene with Debug Draw set to Overdraw*
  150. Lastly, Wireframe draws the scene using only the edges of triangles in the meshes. NOTE: as of the
  151. writting of this (v3.0.2) wireframe is broken and currently just renders the scene normally.
  152. Render target
  153. -------------
  154. When rendering to a :ref:`Viewport <class_Viewport>` whatever is inside will not be
  155. visible in the scene editor. To display the contents, you have to draw the :ref:`Viewport's <class_Viewport>` :ref:`ViewportTexture <class_ViewportTexture>` somewhere.
  156. This can be requested via code using (for example):
  157. ::
  158. #This gets us the ViewportTexture
  159. var rtt = viewport.get_texture()
  160. sprite.texture = rtt
  161. Or it can be assigned in the editor by selecting "New ViewportTexture"
  162. .. image:: img/texturemenu.png
  163. and then selecting the :ref:`Viewport <class_Viewport>` you want to use.
  164. .. image:: img/texturepath.png
  165. Every frame the :ref:`Viewport <class_Viewport>`'s texture is cleared away with the default clear color (or a transparent
  166. color if Transparent BG is set to true). This can be changed by setting Clear Mode to Never or Next Frame.
  167. As the name implies, Never means the texture will never be cleared while next frame will
  168. clear the texture on the next frame and then set itself to Never.
  169. By default, re-rendering of the :ref:`Viewport <class_Viewport>` happens when the
  170. :ref:`Viewport <class_Viewport>`'s :ref:`ViewportTexture <class_ViewportTexture>` has been drawn in a frame. If visible, it will be
  171. rendered, otherwise it will not. This behavior can be changed to manual
  172. rendering (once), or always render, no matter if visible or not. This flexibility
  173. allows users to render an image once and then use the texture without
  174. incurring the cost of rendering every frame.
  175. Make sure to check the Viewport demos! Viewport folder in the demos
  176. archive available to download, or
  177. https://github.com/godotengine/godot-demo-projects/tree/master/viewport