视图

简介

Viewports 想成投影游戏的荧幕。 为了看到游戏,我们需要有一个表面来绘制它,这个表面是作为根节点的 Viewport

../../_images/viewportnode.png

Viewports 也可以添加到场景中,以便绘制多个区域。 当我们绘制到一个不是根节点的 Viewport 时, 我们将该视区称为渲染目标。 我们可以通过访问它对应的 texture 属性来访问渲染目标的内容。 将任一 Viewport 作为渲染目标时,我们要么可以同时渲染多个场景,要么可以渲染到场景中某个对象的 :ref:`texture <class_ViewportTexture>`上 ,例如渲染到动态天空盒的材质上。

Viewports have a variety of use cases, including:

  • 在2D游戏中渲染3D物体
  • 在3D游戏中渲染2D元素
  • 渲染动态纹理
  • 在游戏运行时生成过程纹理
  • 在同一场景中渲染多个摄像机

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.

../../_images/input.png

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!

摄像机 (2D和3D)

当使用 Camera / Camera2D 时,摄像机将始终显示在最近的父节点上 Viewport (朝向根节点)。 例如,在下面的层次结构中:

../../_images/cameras.png

CameraA将显示根节点的 Viewport ,它将绘制MeshA。 CameraB将被 Viewport 节点以及MeshB捕获。 即使MeshB在场景层次结构中,它仍然不会被绘制到根节点的 Viewport 中。 类似地,在 Viewport 节点中不会看到MeshA,因为 Viewport 节点仅捕获层次结构中它下面的节点。

每个视区只能有一个激活的摄像机, 因此, 如果有多个摄像机时, 请确保您需要的那个摄像机的“current”属性被设置上,或者通过调用以下语句来使其成为当前摄像机:

  1. camera.make_current()

By default, cameras will render all objects in their world. In 3D, cameras can use their cull_mask property combined with the VisualInstance’s layer property to restrict which objects are rendered.

缩放和拉伸

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:

  1. viewport.set_size_override(true, Vector2(width, height)) # Custom size for 2D.
  2. viewport.set_size_override_stretch(true) # Enable stretch for custom size.

根节点的 Viewport 用到项目设置中的拉伸选项。 有关缩放和拉伸的更多信息,请访问 Multiple Resolutions Tutorial

世界

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).

作为您想要创建的情况的帮助 Viewports 显示单个对象而不想创建 WorldViewport 可以选择使用自己的 World。 当您想要在2D World 中实例化3D角色或对象时,这非常有用。

对于2D,每个 Viewport 总是包含它自己的 World2D 。 这在大多数情况下都足够了,但是如果需要共享它们,可以手动设置 Viewport的 World2D

For an example of how this works, see the demo projects 3D in 2D and 2D in 3D respectively.

捕获

It is possible to query a capture of the Viewport contents. For the root Viewport, this is effectively a screen capture. This is done with the following code:

  1. # Retrieve the captured Image using get_data().
  2. var img = get_viewport().get_texture().get_data()
  3. # Flip on the Y axis.
  4. # You can also set "V Flip" to true if not on the root Viewport.
  5. img.flip_y()
  6. # Convert Image to ImageTexture.
  7. var tex = ImageTexture.new()
  8. tex.create_from_image(img)
  9. # Set Sprite Texture.
  10. $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):

  1. # Wait until the frame has finished before getting the texture.
  2. yield(VisualServer, "frame_post_draw")
  3. # You can get the image after this.

视区容器

如果 ViewportViewportContainer 的子节点,它将变为活动状态并显示其内部的任何内容。 布局看起来像这样:

../../_images/container.png

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.

渲染

由于以下事实 Viewport 是进入另一个渲染表面的入口,它会暴露一些可能与项目设置不同的渲染属性。第一个是MSAA,您可以选择为每个使用不同级别的MSAA Viewport,默认行为是DISABLED。您还可以设置 Viewport 以使用HDR,当您想要在纹理中存储超出0.0 - 1.0范围的值时,HDR非常有用。

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.

Godot还提供了一种自定义内部绘制方式的方法 Viewports 使用“Debug Draw”。 Debug Draw允许您指定以下四个选项之一 Viewport 将显示在其中绘制的内容。 默认情况下禁用Debug Draw。

../../_images/default_scene.png

禁用Debug Draw绘制的场景

其他三个选项是Unhaded,Overdraw和Wireframe。 无阴影在不使用光照信息的情况下绘制场景,因此所有对象都显示为其反照率颜色的扁平颜色。

../../_images/unshaded.png

The same scene with Debug Draw set to Unshaded

Overdraw使用添加剂混合绘制半透明的网格,以便您可以看到网格重叠的方式。

../../_images/overdraw.png

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):

  1. # This gives us the ViewportTexture.
  2. var rtt = viewport.get_texture()
  3. sprite.texture = rtt

或者可以通过选择“New ViewportTexture”在编辑器中指定它

../../_images/texturemenu.png

然后选择您想要使用的 :ref:`Viewport <class_Viewport>。

../../_images/texturepath.png

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.

一定要查看Viewport演示! 可以下载演示档案中的Viewport文件夹,或https://github.com/godotengine/godot-demo-projects/tree/master/viewport