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 的內容,可以用來結合 Control、Node2D、Node3D 等節點的渲染。例如你可以用該紋理在 TextureRect 中顯示 3D 場景,也可以在 Sprite3D 中顯示 2D 覆蓋層。

要在程式碼中取得 ViewportTexture,請在目標視埠上使用 Viewport.get_texture() 方法。

注意:ViewportTexture 始終是區域於其場景的(見 Resource.resource_local_to_scene)。如果場景根節點尚未就緒,就可能回傳不正確的資料(見 Node.ready)。

注意:實例化包含高解析度 ViewportTexture 的場景可能引起顯著的卡頓。

注意:如果使用的 Viewport 將 Viewport.use_hdr_2d 設為了 true,則回傳的紋理將是使用線性編碼的 HDR 影像。這種影像在螢幕上直接顯示時看上去比較暗。要將其轉換為非線性 sRGB 編碼,可以執行以下操作:

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

注意:Decal、Light3D、PointLight2D 等節點並不支援直接使用 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()),並非相對於使用該紋理的節點。

注意:在編輯器中,目標視埠或其祖級節點發生重新命名或移動時會自動更新這個路徑。在執行時,該路徑可能無法自動更新,因為無法確定場景的根節點。