Viewports can also be added to the scene so that there are multiple surfaces to draw on. When we are drawing to a Viewport that is not the Root, we call it a render target. We can access the contents of a render target by accessing its corresponding texture. By using a Viewport as a render target, we can either render multiple scenes simultaneously or we can render to a texture which is applied to an object in the scene, for example a dynamic skybox.
Viewports have a variety of use cases, including:
What all these use cases have in common is that you are given the ability to draw objects to a texture as if it were another screen and can then choose what to do with the resulting texture.
Viewports are also responsible for delivering properly adjusted and scaled input events to all their children nodes. Typically, input is received by the nearest Viewport in the tree, but you can set Viewports not to receive input by checking 'Disable Input' to 'on'; this will allow the next nearest Viewport in the tree to capture the input.
For more information on how Godot handles input, please read the Input Event Tutorial.
Godot supports 3D sound (in both 2D and 3D nodes); more on this can be found in the Audio Streams Tutorial. For this type of sound to be audible, the Viewport needs to be enabled as a listener (for 2D or 3D). If you are using a custom Viewport to display your World, don't forget to enable this!
每个视区只能有一个激活的摄像机, 因此, 如果有多个摄像机时, 请确保您需要的那个摄像机的“current”属性被设置上，或者通过调用以下语句来使其成为当前摄像机:
Viewports have a "size" property, which represents the size of the Viewport in pixels. For Viewports which are children of ViewportContainers, these values are overridden, but for all others, this sets their resolution.
It is also possible to scale the 2D content and make the Viewport resolution different from the one specified in size, by calling:
viewport.set_size_override(true, Vector2(width, height)) # Custom size for 2D viewport.set_size_override_stretch(true) # Enable stretch for custom size.
For 3D, a Viewport will contain a World. This is basically the universe that links physics and rendering together. Spatial-based nodes will register using the World of the closest Viewport. By default, newly created Viewports do not contain a World but use the same as their parent Viewport (the root Viewport always contains a World, which is the one objects are rendered to by default). A World can be set in a Viewport using the "world" property, and that will separate all children nodes of that Viewport from interacting with the parent Viewport's World. This is especially useful in scenarios where, for example, you might want to show a separate character in 3D imposed over the game (like in StarCraft).
# Retrieve the captured Image using get_data(). var img = get_viewport().get_texture().get_data() # Flip on the Y axis. # You can also set "V Flip" to true if not on the root Viewport. img.flip_y() # Convert Image to ImageTexture. var tex = ImageTexture.new() tex.create_from_image(img) # Set Sprite Texture. $sprite.texture = tex
But if you use this in
_ready() or from the first frame of the Viewport's initialization,
you will get an empty texture because there is nothing to get as texture. You can deal with
it using (for example):
# Wait until the frame has finished before getting the texture yield(VisualServer, "frame_post_draw") # You can get the image after this.
The Viewport will cover the area of its parent ViewportContainer completely
if Stretch is set to
true in ViewportContainer.
Note: The size of the ViewportContainer cannot be smaller than the size of the Viewport.
If you know how the Viewport is going to be used, you can set its Usage to either 3D or 2D. Godot will then restrict how the Viewport is drawn to in accordance with your choice; default is 3D. The 2D usage mode is slightly faster and uses less memory compared to the 3D one. It's a good idea to set the Viewport's Usage property to 2D if your viewport doesn't render anything in 3D.
If you need to render 3D shadows in the viewport, make sure to set the viewport's Shadow Atlas Size property to a value higher than 0. Otherwise, shadows won't be rendered. For reference, the Project Settings define it to 4096 by default.
The same scene with Debug Draw set to Unshaded
The same scene with Debug Draw set to Overdraw
Lastly, Wireframe draws the scene using only the edges of triangles in the meshes.
The effects of the Wireframe mode are only visible in the editor, not while the project is running.
When rendering to a Viewport, whatever is inside will not be visible in the scene editor. To display the contents, you have to draw the Viewport's ViewportTexture somewhere. This can be requested via code using (for example):
# This gives us the ViewportTexture. var rtt = viewport.get_texture() sprite.texture = rtt
然后选择您想要使用的 :ref:`Viewport <class_Viewport>。
Every frame, the Viewport's texture is cleared away with the default clear color (or a transparent
color if Transparent Bg is set to
true). This can be changed by setting Clear Mode to Never or Next Frame.
As the name implies, Never means the texture will never be cleared, while next frame will
clear the texture on the next frame and then set itself to Never.
By default, re-rendering of the Viewport happens when the Viewport's ViewportTexture has been drawn in a frame. If visible, it will be rendered; otherwise, it will not. This behavior can be changed to manual rendering (once), or always render, no matter if visible or not. This flexibility allows users to render an image once and then use the texture without incurring the cost of rendering every frame.