Viewport

Inherits: Node < Object

Creates a sub-view into the screen.

Description

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

Optionally, a viewport can have its own 2D or 3D world, so they don't share what they draw with other viewports.

If a viewport is a child of a ViewportContainer, the viewport will automatically take up the container's size, otherwise it must be set manually.

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.

Note: By default, a newly created Viewport in Godot 3.x will appear to be upside down. Enabling render_target_v_flip will display the Viewport with the correct orientation.

Tutorials

Properties

bool

arvr

false

bool

audio_listener_enable_2d

false

bool

audio_listener_enable_3d

false

Transform2D

canvas_transform

bool

debanding

false

DebugDraw

debug_draw

0

bool

disable_3d

false

bool

fxaa

false

Transform2D

global_canvas_transform

bool

gui_disable_input

false

bool

gui_snap_controls_to_pixels

true

bool

handle_input_locally

true

bool

hdr

true

bool

keep_3d_linear

false

MSAA

msaa

0

bool

own_world

false

bool

physics_object_picking

false

bool

render_direct_to_screen

false

ClearMode

render_target_clear_mode

0

UpdateMode

render_target_update_mode

2

bool

render_target_v_flip

false

ShadowAtlasQuadrantSubdiv

shadow_atlas_quad_0

2

ShadowAtlasQuadrantSubdiv

shadow_atlas_quad_1

2

ShadowAtlasQuadrantSubdiv

shadow_atlas_quad_2

3

ShadowAtlasQuadrantSubdiv

shadow_atlas_quad_3

4

int

shadow_atlas_size

0

float

sharpen_intensity

0.0

Vector2

size

Vector2( 0, 0 )

bool

size_override_stretch

false

bool

transparent_bg

false

Usage

usage

2

bool

use_32_bpc_depth

false

World

world

World2D

world_2d

Methods

World

find_world ( ) const

World2D

find_world_2d ( ) const

Camera

get_camera ( ) const

Transform2D

get_final_transform ( ) const

Control

get_modal_stack_top ( ) const

Vector2

get_mouse_position ( ) const

int

get_render_info ( RenderInfo info )

ShadowAtlasQuadrantSubdiv

get_shadow_atlas_quadrant_subdiv ( int quadrant ) const

Vector2

get_size_override ( ) const

ViewportTexture

get_texture ( ) const

RID

get_viewport_rid ( ) const

Rect2

get_visible_rect ( ) const

Variant

gui_get_drag_data ( ) const

bool

gui_has_modal_stack ( ) const

bool

gui_is_drag_successful ( ) const

bool

gui_is_dragging ( ) const

void

input ( InputEvent local_event )

bool

is_input_handled ( ) const

bool

is_size_override_enabled ( ) const

void

set_attach_to_screen_rect ( Rect2 rect )

void

set_input_as_handled ( )

void

set_shadow_atlas_quadrant_subdiv ( int quadrant, ShadowAtlasQuadrantSubdiv subdiv )

void

set_size_override ( bool enable, Vector2 size=Vector2( -1, -1 ), Vector2 margin=Vector2( 0, 0 ) )

void

unhandled_input ( InputEvent local_event )

void

update_worlds ( )

void

warp_mouse ( Vector2 to_position )


Signals

gui_focus_changed ( Control node )

Emitted when a Control node grabs keyboard focus.


size_changed ( )

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


Enumerations

enum UpdateMode:

UpdateMode UPDATE_DISABLED = 0

Do not update the render target.

UpdateMode UPDATE_ONCE = 1

Update the render target once, then switch to UPDATE_DISABLED.

UpdateMode UPDATE_WHEN_VISIBLE = 2

Update the render target only when it is visible. This is the default value.

UpdateMode UPDATE_ALWAYS = 3

Always update the render target.


enum ShadowAtlasQuadrantSubdiv:

ShadowAtlasQuadrantSubdiv SHADOW_ATLAS_QUADRANT_SUBDIV_DISABLED = 0

This quadrant will not be used.

ShadowAtlasQuadrantSubdiv SHADOW_ATLAS_QUADRANT_SUBDIV_1 = 1

This quadrant will only be used by one shadow map.

ShadowAtlasQuadrantSubdiv SHADOW_ATLAS_QUADRANT_SUBDIV_4 = 2

This quadrant will be split in 4 and used by up to 4 shadow maps.

ShadowAtlasQuadrantSubdiv SHADOW_ATLAS_QUADRANT_SUBDIV_16 = 3

This quadrant will be split 16 ways and used by up to 16 shadow maps.

ShadowAtlasQuadrantSubdiv SHADOW_ATLAS_QUADRANT_SUBDIV_64 = 4

This quadrant will be split 64 ways and used by up to 64 shadow maps.

ShadowAtlasQuadrantSubdiv SHADOW_ATLAS_QUADRANT_SUBDIV_256 = 5

This quadrant will be split 256 ways and used by up to 256 shadow maps. Unless the shadow_atlas_size is very high, the shadows in this quadrant will be very low resolution.

ShadowAtlasQuadrantSubdiv SHADOW_ATLAS_QUADRANT_SUBDIV_1024 = 6

This quadrant will be split 1024 ways and used by up to 1024 shadow maps. Unless the shadow_atlas_size is very high, the shadows in this quadrant will be very low resolution.

ShadowAtlasQuadrantSubdiv SHADOW_ATLAS_QUADRANT_SUBDIV_MAX = 7

Represents the size of the ShadowAtlasQuadrantSubdiv enum.


enum RenderInfo:

RenderInfo RENDER_INFO_OBJECTS_IN_FRAME = 0

Amount of objects in frame.

RenderInfo RENDER_INFO_VERTICES_IN_FRAME = 1

Amount of vertices in frame.

RenderInfo RENDER_INFO_MATERIAL_CHANGES_IN_FRAME = 2

Amount of material changes in frame.

RenderInfo RENDER_INFO_SHADER_CHANGES_IN_FRAME = 3

Amount of shader changes in frame.

RenderInfo RENDER_INFO_SURFACE_CHANGES_IN_FRAME = 4

Amount of surface changes in frame.

RenderInfo RENDER_INFO_DRAW_CALLS_IN_FRAME = 5

Amount of draw calls in frame.

RenderInfo RENDER_INFO_2D_ITEMS_IN_FRAME = 6

Amount of items or joined items in frame.

RenderInfo RENDER_INFO_2D_DRAW_CALLS_IN_FRAME = 7

Amount of draw calls in frame.

RenderInfo RENDER_INFO_MAX = 8

Represents the size of the RenderInfo enum.


enum DebugDraw:

DebugDraw DEBUG_DRAW_DISABLED = 0

Objects are displayed normally.

DebugDraw DEBUG_DRAW_UNSHADED = 1

Objects are displayed without light information.

DebugDraw DEBUG_DRAW_OVERDRAW = 2

Objected are displayed semi-transparent with additive blending so you can see where they intersect.

DebugDraw DEBUG_DRAW_WIREFRAME = 3

Objects are displayed in wireframe style.


enum MSAA:

MSAA MSAA_DISABLED = 0

Multisample anti-aliasing mode disabled. This is the default value.

MSAA MSAA_2X = 1

Use 2x Multisample Antialiasing.

MSAA MSAA_4X = 2

Use 4x Multisample Antialiasing.

MSAA MSAA_8X = 3

Use 8x Multisample Antialiasing. Likely unsupported on low-end and older hardware.

MSAA MSAA_16X = 4

Use 16x Multisample Antialiasing. Likely unsupported on medium and low-end hardware.


enum Usage:

Usage USAGE_2D = 0

Allocates all buffers needed for drawing 2D scenes. This takes less VRAM than the 3D usage modes. Note that 3D rendering effects such as glow and HDR are not available when using this mode.

Usage USAGE_2D_NO_SAMPLING = 1

Allocates buffers needed for 2D scenes without allocating a buffer for screen copy. Accordingly, you cannot read from the screen. Of the Usage types, this requires the least VRAM. Note that 3D rendering effects such as glow and HDR are not available when using this mode.

Usage USAGE_3D = 2

Allocates full buffers for drawing 3D scenes and all 3D effects including buffers needed for 2D scenes and effects.

Usage USAGE_3D_NO_EFFECTS = 3

Allocates buffers needed for drawing 3D scenes. But does not allocate buffers needed for reading from the screen and post-processing effects. Saves some VRAM.


enum ClearMode:

ClearMode CLEAR_MODE_ALWAYS = 0

Always clear the render target before drawing.

ClearMode CLEAR_MODE_NEVER = 1

Never clear the render target.

ClearMode CLEAR_MODE_ONLY_NEXT_FRAME = 2

Clear the render target next frame, then switch to CLEAR_MODE_NEVER.


Property Descriptions

bool arvr = false

  • void set_use_arvr ( bool value )

  • bool use_arvr ( )

If true, the viewport will be used in AR/VR process.


bool audio_listener_enable_2d = false

  • void set_as_audio_listener_2d ( bool value )

  • bool is_audio_listener_2d ( )

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


bool audio_listener_enable_3d = false

  • void set_as_audio_listener ( bool value )

  • bool is_audio_listener ( )

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


Transform2D canvas_transform

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.


bool debanding = false

  • void set_use_debanding ( bool value )

  • bool get_use_debanding ( )

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 Environment.background_mode is Environment.BG_CANVAS. In this case, usage must also be set to USAGE_3D. See also ProjectSettings.rendering/quality/filters/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.

Note: Only available on the GLES3 backend. hdr must also be true for debanding to be effective.


DebugDraw debug_draw = 0

The overlay mode for test rendered geometry in debug purposes.


bool disable_3d = false

  • void set_disable_3d ( bool value )

  • bool is_3d_disabled ( )

If true, the viewport will disable 3D rendering. To actually disable allocation of 3D buffers, set usage instead.


bool fxaa = false

  • void set_use_fxaa ( bool value )

  • bool get_use_fxaa ( )

Enables fast approximate antialiasing. FXAA is a popular screen-space antialiasing method, which is fast but will make the image look blurry, especially at lower resolutions. It can still work relatively well at large resolutions such as 1440p and 4K. Some of the lost sharpness can be recovered by enabling contrast-adaptive sharpening (see sharpen_intensity).


Transform2D global_canvas_transform

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


bool gui_disable_input = false

  • void set_disable_input ( bool value )

  • bool is_input_disabled ( )

If true, the viewport will not receive input events.


bool gui_snap_controls_to_pixels = true

  • void set_snap_controls_to_pixels ( bool value )

  • bool is_snap_controls_to_pixels_enabled ( )

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


bool handle_input_locally = true

  • void set_handle_input_locally ( bool value )

  • bool is_handling_input_locally ( )

There is currently no description for this property. Please help us by contributing one!


bool hdr = true

  • void set_hdr ( bool value )

  • bool get_hdr ( )

If true, the viewport rendering will receive benefits from High Dynamic Range algorithm. High Dynamic Range allows the viewport to receive values that are outside the 0-1 range. In Godot, HDR uses half floating-point precision (16-bit) by default. To use full floating-point precision (32-bit), enable use_32_bpc_depth.

Note: Requires usage to be set to USAGE_3D or USAGE_3D_NO_EFFECTS, since HDR is not supported for 2D.

Note: Only available on the GLES3 backend.


bool keep_3d_linear = false

  • void set_keep_3d_linear ( bool value )

  • bool get_keep_3d_linear ( )

If true, the result after 3D rendering will not have a linear to sRGB color conversion applied. This is important when the viewport is used as a render target where the result is used as a texture on a 3D object rendered in another viewport. It is also important if the viewport is used to create data that is not color based (noise, heightmaps, pickmaps, etc.). Do not enable this when the viewport is used as a texture on a 2D object or if the viewport is your final output. For the GLES2 driver this will convert the sRGB output to linear, this should only be used for VR plugins that require input in linear color space!


MSAA msaa = 0

  • void set_msaa ( MSAA value )

  • MSAA get_msaa ( )

The multisample anti-aliasing mode. A higher number results in smoother edges at the cost of significantly worse performance. A value of 4 is best unless targeting very high-end systems.


bool own_world = false

  • void set_use_own_world ( bool value )

  • bool is_using_own_world ( )

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


bool physics_object_picking = false

  • void set_physics_object_picking ( bool value )

  • bool get_physics_object_picking ( )

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


bool render_direct_to_screen = false

  • void set_use_render_direct_to_screen ( bool value )

  • bool is_using_render_direct_to_screen ( )

If true, renders the Viewport directly to the screen instead of to the root viewport. Only available in GLES2. This is a low-level optimization and should not be used in most cases. If used, reading from the Viewport or from SCREEN_TEXTURE becomes unavailable. For more information see VisualServer.viewport_set_render_direct_to_screen.


ClearMode render_target_clear_mode = 0

The clear mode when viewport used as a render target.

Note: This property is intended for 2D usage.


UpdateMode render_target_update_mode = 2

The update mode when viewport used as a render target.


bool render_target_v_flip = false

  • void set_vflip ( bool value )

  • bool get_vflip ( )

If true, the result of rendering will be flipped vertically. Since Viewports in Godot 3.x render upside-down, it's recommended to set this to true in most situations.


ShadowAtlasQuadrantSubdiv shadow_atlas_quad_0 = 2

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


ShadowAtlasQuadrantSubdiv shadow_atlas_quad_1 = 2

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


ShadowAtlasQuadrantSubdiv shadow_atlas_quad_2 = 3

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


ShadowAtlasQuadrantSubdiv shadow_atlas_quad_3 = 4

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


int shadow_atlas_size = 0

  • void set_shadow_atlas_size ( int value )

  • int get_shadow_atlas_size ( )

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

Note: If this is set to 0, both point and directional shadows won't be visible. Since user-created viewports default to a value of 0, this value must be set above 0 manually (typically at least 256).


float sharpen_intensity = 0.0

  • void set_sharpen_intensity ( float value )

  • float get_sharpen_intensity ( )

If set to a value greater than 0.0, contrast-adaptive sharpening will be applied to the 3D viewport. This has a low performance cost and can be used to recover some of the sharpness lost from using FXAA. Values around 0.5 generally give the best results. See also fxaa.


Vector2 size = Vector2( 0, 0 )

The width and height of viewport. Must be set to a value greater than or equal to 2 pixels on both dimensions. Otherwise, nothing will be displayed.


bool size_override_stretch = false

  • void set_size_override_stretch ( bool value )

  • bool is_size_override_stretch_enabled ( )

If true, the size override affects stretch as well.


bool transparent_bg = false

  • void set_transparent_background ( bool value )

  • bool has_transparent_background ( )

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


Usage usage = 2

  • void set_usage ( Usage value )

  • Usage get_usage ( )

The viewport's rendering mode. This controls which buffers are allocated for the viewport (2D only, or 2D + 3D). 2D-only options can reduce memory usage and improve performance slightly, especially on low-end devices.

Note: If set to USAGE_2D or USAGE_2D_NO_SAMPLING, hdr will have no effect when enabled since HDR is not supported for 2D.


bool use_32_bpc_depth = false

  • void set_use_32_bpc_depth ( bool value )

  • bool get_use_32_bpc_depth ( )

If true, allocates the viewport's framebuffer with full floating-point precision (32-bit) instead of half floating-point precision (16-bit). Only effective when hdr is also enabled.

Note: Enabling this setting does not improve rendering quality. Using full floating-point precision is slower, and is generally only needed for advanced shaders that require a high level of precision. To reduce banding, enable debanding instead.

Note: Only available on the GLES3 backend.


World world

  • void set_world ( World value )

  • World get_world ( )

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


World2D world_2d

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


Method Descriptions

World find_world ( ) const

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


World2D find_world_2d ( ) const

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


Camera get_camera ( ) const

Returns the active 3D camera.


Transform2D get_final_transform ( ) const

Returns the total transform of the viewport.


Control get_modal_stack_top ( ) const

Returns the topmost modal in the stack.


Vector2 get_mouse_position ( ) const

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


int get_render_info ( RenderInfo info )

Returns information about the viewport from the rendering pipeline.


ShadowAtlasQuadrantSubdiv get_shadow_atlas_quadrant_subdiv ( int quadrant ) const

Returns the ShadowAtlasQuadrantSubdiv of the specified quadrant.


Vector2 get_size_override ( ) const

Returns the size override set with set_size_override.


ViewportTexture get_texture ( ) const

Returns the viewport's texture.

Note: Due to the way OpenGL works, the resulting ViewportTexture is flipped vertically. You can use Image.flip_y on the result of Texture.get_data to flip it back, for example:

var img = get_viewport().get_texture().get_data()
img.flip_y()

RID get_viewport_rid ( ) const

Returns the viewport's RID from the VisualServer.


Rect2 get_visible_rect ( ) const

Returns the visible rectangle in global screen coordinates.


Variant gui_get_drag_data ( ) const

Returns the drag data from the GUI, that was previously returned by Control.get_drag_data.


bool gui_has_modal_stack ( ) const

Returns true if there are visible modals on-screen.


bool gui_is_drag_successful ( ) const

Returns true if the drag operation is successful.


bool gui_is_dragging ( ) const

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

Alternative to Node.NOTIFICATION_DRAG_BEGIN and Node.NOTIFICATION_DRAG_END when you prefer polling the value.


void input ( InputEvent local_event )

There is currently no description for this method. Please help us by contributing one!


bool is_input_handled ( ) const

There is currently no description for this method. Please help us by contributing one!


bool is_size_override_enabled ( ) const

Returns true if the size override is enabled. See set_size_override.


void set_attach_to_screen_rect ( Rect2 rect )

Attaches this Viewport to the root Viewport with the specified rectangle. This bypasses the need for another node to display this Viewport but makes you responsible for updating the position of this Viewport manually.


void set_input_as_handled ( )

Stops the input from propagating further down the SceneTree.


void set_shadow_atlas_quadrant_subdiv ( int quadrant, ShadowAtlasQuadrantSubdiv subdiv )

Sets the number of subdivisions to use in the specified quadrant. A higher number of subdivisions allows you to have more shadows in the scene at once, but reduces the quality of the shadows. A good practice is to have quadrants with a varying number of subdivisions and to have as few subdivisions as possible.


void set_size_override ( bool enable, Vector2 size=Vector2( -1, -1 ), Vector2 margin=Vector2( 0, 0 ) )

Sets the size override of the viewport. If the enable parameter is true the override is used, otherwise it uses the default size. If the size parameter is (-1, -1), it won't update the size.


void unhandled_input ( InputEvent local_event )

There is currently no description for this method. Please help us by contributing one!


void update_worlds ( )

Forces update of the 2D and 3D worlds.


void warp_mouse ( Vector2 to_position )

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