Table of Contents

Class Viewport

Namespace
Godot
Assembly
GodotSharp.dll

A Viewport creates a different view into the screen, or a sub-view inside another viewport. Child 2D nodes will display on it, and child Camera3D 3D nodes will render on it too.

Optionally, a viewport can have its own 2D or 3D world, so it doesn't share what it draws with other viewports.

Viewports can also choose to be audio listeners, so they generate positional audio depending on a 2D or 3D camera child of it.

Also, viewports can be assigned to different screens in case the devices have multiple screens.

Finally, viewports can also behave as render targets, in which case they will not be visible unless the associated texture is used to draw.

public class Viewport : Node, IDisposable
Inheritance
Viewport
Implements
Derived
Inherited Members

Properties

AudioListenerEnable2D

If true, the viewport will process 2D audio streams.

public bool AudioListenerEnable2D { get; set; }

Property Value

bool

AudioListenerEnable3D

If true, the viewport will process 3D audio streams.

public bool AudioListenerEnable3D { get; set; }

Property Value

bool

CanvasCullMask

The rendering layers in which this Viewport renders CanvasItem nodes.

public uint CanvasCullMask { get; set; }

Property Value

uint

CanvasItemDefaultTextureFilter

Sets the default filter mode used by CanvasItems in this Viewport. See Viewport.DefaultCanvasItemTextureFilter for options.

public Viewport.DefaultCanvasItemTextureFilter CanvasItemDefaultTextureFilter { get; set; }

Property Value

Viewport.DefaultCanvasItemTextureFilter

CanvasItemDefaultTextureRepeat

Sets the default repeat mode used by CanvasItems in this Viewport. See Viewport.DefaultCanvasItemTextureRepeat for options.

public Viewport.DefaultCanvasItemTextureRepeat CanvasItemDefaultTextureRepeat { get; set; }

Property Value

Viewport.DefaultCanvasItemTextureRepeat

CanvasTransform

The canvas transform of the viewport, useful for changing the on-screen positions of all child CanvasItems. This is relative to the global canvas transform of the viewport.

public Transform2D CanvasTransform { get; set; }

Property Value

Transform2D

DebugDraw

The overlay mode for test rendered geometry in debug purposes.

public Viewport.DebugDrawEnum DebugDraw { get; set; }

Property Value

Viewport.DebugDrawEnum

Disable3D

Disable 3D rendering (but keep 2D rendering).

public bool Disable3D { get; set; }

Property Value

bool

FsrSharpness

Determines how sharp the upscaled image will be when using the FSR upscaling mode. Sharpness halves with every whole number. Values go from 0.0 (sharpest) to 2.0. Values above 2.0 won't make a visible difference.

To control this property on the root viewport, set the ProjectSettings.rendering/scaling_3d/fsr_sharpness project setting.

public float FsrSharpness { get; set; }

Property Value

float

GlobalCanvasTransform

The global canvas transform of the viewport. The canvas transform is relative to this.

public Transform2D GlobalCanvasTransform { get; set; }

Property Value

Transform2D

GuiDisableInput

If true, the viewport will not receive input events.

public bool GuiDisableInput { get; set; }

Property Value

bool

GuiEmbedSubwindows

If true, sub-windows (popups and dialogs) will be embedded inside application window as control-like nodes. If false, they will appear as separate windows handled by the operating system.

public bool GuiEmbedSubwindows { get; set; }

Property Value

bool

GuiSnapControlsToPixels

If true, the GUI controls on the viewport will lay pixel perfectly.

public bool GuiSnapControlsToPixels { get; set; }

Property Value

bool

HandleInputLocally

If true, this viewport will mark incoming input events as handled by itself. If false, this is instead done by the first parent viewport that is set to handle input locally.

A SubViewportContainer will automatically set this property to false for the Viewport contained inside of it.

See also SetInputAsHandled() and IsInputHandled().

public bool HandleInputLocally { get; set; }

Property Value

bool

MeshLodThreshold

The automatic LOD bias to use for meshes rendered within the Viewport (this is analogous to MeshLodThreshold). Higher values will use less detailed versions of meshes that have LOD variations generated. If set to 0.0, automatic LOD is disabled. Increase MeshLodThreshold to improve performance at the cost of geometry detail.

To control this property on the root viewport, set the ProjectSettings.rendering/mesh_lod/lod_change/threshold_pixels project setting.

Note: MeshLodThreshold does not affect GeometryInstance3D visibility ranges (also known as "manual" LOD or hierarchical LOD).

public float MeshLodThreshold { get; set; }

Property Value

float

Msaa2D

The multisample anti-aliasing mode for 2D/Canvas rendering. A higher number results in smoother edges at the cost of significantly worse performance. A value of 2 or 4 is best unless targeting very high-end systems. This has no effect on shader-induced aliasing or texture aliasing.

public Viewport.Msaa Msaa2D { get; set; }

Property Value

Viewport.Msaa

Msaa3D

The multisample anti-aliasing mode for 3D rendering. A higher number results in smoother edges at the cost of significantly worse performance. A value of 2 or 4 is best unless targeting very high-end systems. See also bilinear scaling 3d Scaling3DMode for supersampling, which provides higher quality but is much more expensive. This has no effect on shader-induced aliasing or texture aliasing.

public Viewport.Msaa Msaa3D { get; set; }

Property Value

Viewport.Msaa

OwnWorld3D

If true, the viewport will use a unique copy of the World3D defined in World3D.

public bool OwnWorld3D { get; set; }

Property Value

bool

PhysicsObjectPicking

If true, the objects rendered by viewport become subjects of mouse picking process.

Note: The number of simultaneously pickable objects is limited to 64 and they are selected in a non-deterministic order, which can be different in each picking process.

public bool PhysicsObjectPicking { get; set; }

Property Value

bool

PhysicsObjectPickingSort

If true, objects receive mouse picking events sorted primarily by their ZIndex and secondarily by their position in the scene tree. If false, the order is undetermined.

Note: This setting is disabled by default because of its potential expensive computational cost.

Note: Sorting happens after selecting the pickable objects. Because of the limitation of 64 simultaneously pickable objects, it is not guaranteed that the object with the highest ZIndex receives the picking event.

public bool PhysicsObjectPickingSort { get; set; }

Property Value

bool

PositionalShadowAtlas16Bits

Use 16 bits for the omni/spot shadow depth map. Enabling this results in shadows having less precision and may result in shadow acne, but can lead to performance improvements on some devices.

public bool PositionalShadowAtlas16Bits { get; set; }

Property Value

bool

PositionalShadowAtlasQuad0

The subdivision amount of the first quadrant on the shadow atlas.

public Viewport.PositionalShadowAtlasQuadrantSubdiv PositionalShadowAtlasQuad0 { get; set; }

Property Value

Viewport.PositionalShadowAtlasQuadrantSubdiv

PositionalShadowAtlasQuad1

The subdivision amount of the second quadrant on the shadow atlas.

public Viewport.PositionalShadowAtlasQuadrantSubdiv PositionalShadowAtlasQuad1 { get; set; }

Property Value

Viewport.PositionalShadowAtlasQuadrantSubdiv

PositionalShadowAtlasQuad2

The subdivision amount of the third quadrant on the shadow atlas.

public Viewport.PositionalShadowAtlasQuadrantSubdiv PositionalShadowAtlasQuad2 { get; set; }

Property Value

Viewport.PositionalShadowAtlasQuadrantSubdiv

PositionalShadowAtlasQuad3

The subdivision amount of the fourth quadrant on the shadow atlas.

public Viewport.PositionalShadowAtlasQuadrantSubdiv PositionalShadowAtlasQuad3 { get; set; }

Property Value

Viewport.PositionalShadowAtlasQuadrantSubdiv

PositionalShadowAtlasSize

The shadow atlas' resolution (used for omni and spot lights). The value is rounded up to the nearest power of 2.

Note: If this is set to 0, no positional shadows will be visible at all. This can improve performance significantly on low-end systems by reducing both the CPU and GPU load (as fewer draw calls are needed to draw the scene without shadows).

public int PositionalShadowAtlasSize { get; set; }

Property Value

int

Scaling3DMode

Sets scaling 3d mode. Bilinear scaling renders at different resolution to either undersample or supersample the viewport. FidelityFX Super Resolution 1.0, abbreviated to FSR, is an upscaling technology that produces high quality images at fast framerates by using a spatially aware upscaling algorithm. FSR is slightly more expensive than bilinear, but it produces significantly higher image quality. FSR should be used where possible.

To control this property on the root viewport, set the ProjectSettings.rendering/scaling_3d/mode project setting.

public Viewport.Scaling3DModeEnum Scaling3DMode { get; set; }

Property Value

Viewport.Scaling3DModeEnum

Scaling3DScale

Scales the 3D render buffer based on the viewport size uses an image filter specified in ProjectSettings.rendering/scaling_3d/mode to scale the output image to the full viewport size. Values lower than 1.0 can be used to speed up 3D rendering at the cost of quality (undersampling). Values greater than 1.0 are only valid for bilinear mode and can be used to improve 3D rendering quality at a high performance cost (supersampling). See also ProjectSettings.rendering/anti_aliasing/quality/msaa_3d for multi-sample antialiasing, which is significantly cheaper but only smooths the edges of polygons.

When using FSR upscaling, AMD recommends exposing the following values as preset options to users "Ultra Quality: 0.77", "Quality: 0.67", "Balanced: 0.59", "Performance: 0.5" instead of exposing the entire scale.

To control this property on the root viewport, set the ProjectSettings.rendering/scaling_3d/scale project setting.

public float Scaling3DScale { get; set; }

Property Value

float

ScreenSpaceAA

Sets the screen-space antialiasing method used. Screen-space antialiasing works by selectively blurring edges in a post-process shader. It differs from MSAA which takes multiple coverage samples while rendering objects. Screen-space AA methods are typically faster than MSAA and will smooth out specular aliasing, but tend to make scenes appear blurry.

public Viewport.ScreenSpaceAAEnum ScreenSpaceAA { get; set; }

Property Value

Viewport.ScreenSpaceAAEnum

SdfOversize

public Viewport.SdfOversizeEnum SdfOversize { get; set; }

Property Value

Viewport.SdfOversizeEnum

SdfScale

public Viewport.SdfScaleEnum SdfScale { get; set; }

Property Value

Viewport.SdfScaleEnum

Snap2DTransformsToPixel

public bool Snap2DTransformsToPixel { get; set; }

Property Value

bool

Snap2DVerticesToPixel

public bool Snap2DVerticesToPixel { get; set; }

Property Value

bool

TextureMipmapBias

Affects the final texture sharpness by reading from a lower or higher mipmap (also called "texture LOD bias"). Negative values make mipmapped textures sharper but grainier when viewed at a distance, while positive values make mipmapped textures blurrier (even when up close).

Enabling temporal antialiasing (UseTaa) will automatically apply a -0.5 offset to this value, while enabling FXAA (ScreenSpaceAA) will automatically apply a -0.25 offset to this value. If both TAA and FXAA are enabled at the same time, an offset of -0.75 is applied to this value.

Note: If Scaling3DScale is lower than 1.0 (exclusive), TextureMipmapBias is used to adjust the automatic mipmap bias which is calculated internally based on the scale factor. The formula for this is log2(scaling_3d_scale) + mipmap_bias.

To control this property on the root viewport, set the ProjectSettings.rendering/textures/default_filters/texture_mipmap_bias project setting.

public float TextureMipmapBias { get; set; }

Property Value

float

TransparentBg

If true, the viewport should render its background as transparent.

public bool TransparentBg { get; set; }

Property Value

bool

UseDebanding

If true, uses a fast post-processing filter to make banding significantly less visible in 3D. 2D rendering is not affected by debanding unless the BackgroundMode is Canvas. See also ProjectSettings.rendering/anti_aliasing/quality/use_debanding.

In some cases, debanding may introduce a slightly noticeable dithering pattern. It's recommended to enable debanding only when actually needed since the dithering pattern will make lossless-compressed screenshots larger.

public bool UseDebanding { get; set; }

Property Value

bool

UseHdr2D

If true, 2D rendering will use an high dynamic range (HDR) format framebuffer matching the bit depth of the 3D framebuffer. When using the Forward+ renderer this will be a RGBA16 framebuffer, while when using the Mobile renderer it will be a RGB10_A2 framebuffer. Additionally, 2D rendering will take place in linear color space and will be converted to sRGB space immediately before blitting to the screen (if the Viewport is attached to the screen). Practically speaking, this means that the end result of the Viewport will not be clamped into the 0-1 range and can be used in 3D rendering without color space adjustments. This allows 2D rendering to take advantage of effects requiring high dynamic range (e.g. 2D glow) as well as substantially improves the appearance of effects requiring highly detailed gradients.

Note: This setting will have no effect when using the GL Compatibility renderer as the GL Compatibility renderer always renders in low dynamic range for performance reasons.

public bool UseHdr2D { get; set; }

Property Value

bool

UseOcclusionCulling

If true, OccluderInstance3D nodes will be usable for occlusion culling in 3D for this viewport. For the root viewport, ProjectSettings.rendering/occlusion_culling/use_occlusion_culling must be set to true instead.

Note: Enabling occlusion culling has a cost on the CPU. Only enable occlusion culling if you actually plan to use it, and think whether your scene can actually benefit from occlusion culling. Large, open scenes with few or no objects blocking the view will generally not benefit much from occlusion culling. Large open scenes generally benefit more from mesh LOD and visibility ranges (VisibilityRangeBegin and VisibilityRangeEnd) compared to occlusion culling.

Note: Due to memory constraints, occlusion culling is not supported by default in Web export templates. It can be enabled by compiling custom Web export templates with module_raycast_enabled=yes.

public bool UseOcclusionCulling { get; set; }

Property Value

bool

UseTaa

Enables Temporal Anti-Aliasing for this viewport. TAA works by jittering the camera and accumulating the images of the last rendered frames, motion vector rendering is used to account for camera and object motion.

Note: The implementation is not complete yet, some visual instances such as particles and skinned meshes may show artifacts.

public bool UseTaa { get; set; }

Property Value

bool

UseXR

If true, the viewport will use the primary XR interface to render XR output. When applicable this can result in a stereoscopic image and the resulting render being output to a headset.

public bool UseXR { get; set; }

Property Value

bool

VrsMode

The Variable Rate Shading (VRS) mode that is used for this viewport. Note, if hardware does not support VRS this property is ignored.

public Viewport.VrsModeEnum VrsMode { get; set; }

Property Value

Viewport.VrsModeEnum

VrsTexture

Texture to use when VrsMode is set to Texture.

The texture must use a lossless compression format so that colors can be matched precisely. The following VRS densities are mapped to various colors, with brighter colors representing a lower level of shading precision:

- 1x1 = rgb(0, 0, 0)     - #000000
  - 1x2 = rgb(0, 85, 0)    - #005500
  - 2x1 = rgb(85, 0, 0)    - #550000
  - 2x2 = rgb(85, 85, 0)   - #555500
  - 2x4 = rgb(85, 170, 0)  - #55aa00
  - 4x2 = rgb(170, 85, 0)  - #aa5500
  - 4x4 = rgb(170, 170, 0) - #aaaa00
  - 4x8 = rgb(170, 255, 0) - #aaff00 - Not supported on most hardware
  - 8x4 = rgb(255, 170, 0) - #ffaa00 - Not supported on most hardware
  - 8x8 = rgb(255, 255, 0) - #ffff00 - Not supported on most hardware
public Texture2D VrsTexture { get; set; }

Property Value

Texture2D

World2D

The custom World2D which can be used as 2D environment source.

public World2D World2D { get; set; }

Property Value

World2D

World3D

The custom World3D which can be used as 3D environment source.

public World3D World3D { get; set; }

Property Value

World3D

Methods

FindWorld2D()

Returns the first valid World2D for this viewport, searching the World2D property of itself and any Viewport ancestor.

public World2D FindWorld2D()

Returns

World2D

FindWorld3D()

Returns the first valid World3D for this viewport, searching the World3D property of itself and any Viewport ancestor.

public World3D FindWorld3D()

Returns

World3D

GetCamera2D()

Returns the currently active 2D camera. Returns null if there are no active cameras.

public Camera2D GetCamera2D()

Returns

Camera2D

GetCamera3D()

Returns the currently active 3D camera.

public Camera3D GetCamera3D()

Returns

Camera3D

GetCanvasCullMaskBit(uint)

Returns an individual bit on the rendering layer mask.

public bool GetCanvasCullMaskBit(uint layer)

Parameters

layer uint

Returns

bool

GetEmbeddedSubwindows()

Returns a list of the visible embedded Windows inside the viewport.

Note: Windows inside other viewports will not be listed.

public Array<Window> GetEmbeddedSubwindows()

Returns

Array<Window>

GetFinalTransform()

Returns the transform from the viewport's coordinate system to the embedder's coordinate system.

public Transform2D GetFinalTransform()

Returns

Transform2D

GetMousePosition()

Returns the mouse's position in this Viewport using the coordinate system of this Viewport.

public Vector2 GetMousePosition()

Returns

Vector2

GetRenderInfo(RenderInfoType, RenderInfo)

Returns rendering statistics of the given type. See Viewport.RenderInfoType and Viewport.RenderInfo for options.

public int GetRenderInfo(Viewport.RenderInfoType type, Viewport.RenderInfo info)

Parameters

type Viewport.RenderInfoType
info Viewport.RenderInfo

Returns

int

GetScreenTransform()

Returns the transform from the Viewport's coordinates to the screen coordinates of the containing window manager window.

public Transform2D GetScreenTransform()

Returns

Transform2D

GetTexture()

Returns the viewport's texture.

Note: When trying to store the current texture (e.g. in a file), it might be completely black or outdated if used too early, especially when used in e.g. _Ready(). To make sure the texture you get is correct, you can await FramePostDraw signal.

func _ready():
      await RenderingServer.frame_post_draw
      $Viewport.get_texture().get_image().save_png("user://Screenshot.png")
public ViewportTexture GetTexture()

Returns

ViewportTexture

GetViewportRid()

Returns the viewport's RID from the RenderingServer.

public Rid GetViewportRid()

Returns

Rid

GetVisibleRect()

Returns the visible rectangle in global screen coordinates.

public Rect2 GetVisibleRect()

Returns

Rect2

GuiGetDragData()

Returns the drag data from the GUI, that was previously returned by _GetDragData(Vector2).

public Variant GuiGetDragData()

Returns

Variant

GuiGetFocusOwner()

Returns the Control having the focus within this viewport. If no Control has the focus, returns null.

public Control GuiGetFocusOwner()

Returns

Control

GuiIsDragSuccessful()

Returns true if the drag operation is successful.

public bool GuiIsDragSuccessful()

Returns

bool

GuiIsDragging()

Returns true if the viewport is currently performing a drag operation.

Alternative to NotificationDragBegin and NotificationDragEnd when you prefer polling the value.

public bool GuiIsDragging()

Returns

bool

GuiReleaseFocus()

Removes the focus from the currently focused Control within this viewport. If no Control has the focus, does nothing.

public void GuiReleaseFocus()

HasGodotClassMethod(in godot_string_name)

Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.

protected override bool HasGodotClassMethod(in godot_string_name method)

Parameters

method godot_string_name

Name of the method to check for.

Returns

bool

HasGodotClassSignal(in godot_string_name)

Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.

protected override bool HasGodotClassSignal(in godot_string_name signal)

Parameters

signal godot_string_name

Name of the signal to check for.

Returns

bool

InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)

Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.

protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)

Parameters

method godot_string_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

IsInputHandled()

Returns whether the current InputEvent has been handled. Input events are not handled until SetInputAsHandled() has been called during the lifetime of an InputEvent.

This is usually done as part of input handling methods like _Input(InputEvent), _GuiInput(InputEvent) or others, as well as in corresponding signal handlers.

If HandleInputLocally is set to false, this method will try finding the first parent viewport that is set to handle input locally, and return its value for IsInputHandled() instead.

public bool IsInputHandled()

Returns

bool

PushInput(InputEvent, bool)

Triggers the given event in this Viewport. This can be used to pass an InputEvent between viewports, or to locally apply inputs that were sent over the network or saved to a file.

If inLocalCoords is false, the event's position is in the embedder's coordinates and will be converted to viewport coordinates. If inLocalCoords is true, the event's position is in viewport coordinates.

While this method serves a similar purpose as ParseInputEvent(InputEvent), it does not remap the specified event based on project settings like ProjectSettings.input_devices/pointing/emulate_touch_from_mouse.

Calling this method will propagate calls to child nodes for following methods in the given order:

- _Input(InputEvent)

- _GuiInput(InputEvent) for Control nodes

- _ShortcutInput(InputEvent)

- _UnhandledKeyInput(InputEvent)

- _UnhandledInput(InputEvent)

If an earlier method marks the input as handled via SetInputAsHandled(), any later method in this list will not be called.

If none of the methods handle the event and PhysicsObjectPicking is true, the event is used for physics object picking.

public void PushInput(InputEvent @event, bool inLocalCoords = false)

Parameters

event InputEvent
inLocalCoords bool

PushTextInput(string)

Helper method which calls the set_text() method on the currently focused Control, provided that it is defined (e.g. if the focused Control is Button or LineEdit).

public void PushTextInput(string text)

Parameters

text string

PushUnhandledInput(InputEvent, bool)

Triggers the given InputEvent in this Viewport. This can be used to pass input events between viewports, or to locally apply inputs that were sent over the network or saved to a file.

If inLocalCoords is false, the event's position is in the embedder's coordinates and will be converted to viewport coordinates. If inLocalCoords is true, the event's position is in viewport coordinates.

While this method serves a similar purpose as ParseInputEvent(InputEvent), it does not remap the specified event based on project settings like ProjectSettings.input_devices/pointing/emulate_touch_from_mouse.

Calling this method will propagate calls to child nodes for following methods in the given order:

- _ShortcutInput(InputEvent)

- _UnhandledKeyInput(InputEvent)

- _UnhandledInput(InputEvent)

If an earlier method marks the input as handled via SetInputAsHandled(), any later method in this list will not be called.

If none of the methods handle the event and PhysicsObjectPicking is true, the event is used for physics object picking.

Note: This method doesn't propagate input events to embedded Windows or SubViewports.

Deprecated. Use PushInput(InputEvent, bool) instead.

[Obsolete("This method is deprecated.")]
public void PushUnhandledInput(InputEvent @event, bool inLocalCoords = false)

Parameters

event InputEvent
inLocalCoords bool

SetCanvasCullMaskBit(uint, bool)

Set/clear individual bits on the rendering layer mask. This simplifies editing this Viewport's layers.

public void SetCanvasCullMaskBit(uint layer, bool enable)

Parameters

layer uint
enable bool

SetInputAsHandled()

Stops the input from propagating further down the SceneTree.

Note: This does not affect the methods in Input, only the way events are propagated.

public void SetInputAsHandled()

UpdateMouseCursorState()

Force instantly updating the display based on the current mouse cursor position. This includes updating the mouse cursor shape and sending necessary MouseEntered, MouseEntered, MouseEntered and MouseEntered signals and their respective mouse_exited counterparts.

public void UpdateMouseCursorState()

WarpMouse(Vector2)

Moves the mouse pointer to the specified position in this Viewport using the coordinate system of this Viewport.

Note: WarpMouse(Vector2) is only supported on Windows, macOS and Linux. It has no effect on Android, iOS and Web.

public void WarpMouse(Vector2 position)

Parameters

position Vector2

Events

GuiFocusChanged

Emitted when a Control node grabs keyboard focus.

public event Viewport.GuiFocusChangedEventHandler GuiFocusChanged

Event Type

Viewport.GuiFocusChangedEventHandler

SizeChanged

Emitted when the size of the viewport is changed, whether by resizing of window, or some other means.

public event Action SizeChanged

Event Type

Action