Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

ViewportTexture

继承: Texture2D < Texture < Resource < RefCounted < Object

以动态纹理的形式提供 Viewport 的内容。

描述

ViewportTexture 以动态 Texture2D 的形式提供 Viewport 的内容,可以用来结合 ControlNode2DNode3D 等节点的渲染。例如你可以用该纹理在 TextureRect 中显示 3D 场景,也可以在 Sprite3D 中显示 2D 覆盖层。

要在代码中获取 ViewportTexture,请在目标视口上使用 Viewport.get_texture() 方法。

注意:ViewportTexture 始终是局部于其场景的(见 Resource.resource_local_to_scene)。如果场景根节点尚未就绪,就可能返回不正确的数据(见 Node.ready)。

注意:实例化包含高分辨率 ViewportTexture 的场景可能引起显著的卡顿。

注意:如果使用的 ViewportViewport.use_hdr_2d 设为了 true,则返回的纹理是在线性空间中编码的 HDR 图像。这种图像在屏幕上直接显示时看上去比较暗。你可以用下面的方法将其转换至 Gamma 空间:

img.convert(Image.FORMAT_RGBA8)
imb.linear_to_srgb()

注意:DecalLight3DPointLight2D 等节点并不支持直接使用 ViewportTexture。要在这些节点中使用 ViewportTexture 的纹理数据,你需要对 ViewportTexture 调用 Texture2D.get_image() 获取 ImageTexture,将这个结果传给 ImageTexture.create_from_image()。这种转换操作比较慢,因此不应该每帧都执行。

教程

属性

NodePath

viewport_path

NodePath("")


属性说明

NodePath viewport_path = NodePath("") 🔗

  • void set_viewport_path_in_scene(value: NodePath)

  • NodePath get_viewport_path_in_scene()

要显示的 Viewport 节点的路径。相对于本地场景的根节点(见 Resource.get_local_scene()),并非相对于使用该纹理的节点。

注意:在编辑器中,目标视口或其祖级节点发生重命名或移动时会自动更新这个路径。在运行时,该路径可能无法自动更新,因为无法确定场景的根节点。