Viewport

Inherits: Node < Object

Category: Core

Brief Description

Creates a sub-view into the screen.

Properties

boolarvr
boolaudio_listener_enable_2d
boolaudio_listener_enable_3d
Transform2Dcanvas_transform
DebugDrawdebug_draw
booldisable_3d
Transform2Dglobal_canvas_transform
boolgui_disable_input
boolgui_snap_controls_to_pixels
boolhandle_input_locally
boolhdr
boolkeep_3d_linear
MSAAmsaa
boolown_world
boolphysics_object_picking
ClearModerender_target_clear_mode
UpdateModerender_target_update_mode
boolrender_target_v_flip
ShadowAtlasQuadrantSubdivshadow_atlas_quad_0
ShadowAtlasQuadrantSubdivshadow_atlas_quad_1
ShadowAtlasQuadrantSubdivshadow_atlas_quad_2
ShadowAtlasQuadrantSubdivshadow_atlas_quad_3
intshadow_atlas_size
Vector2size
booltransparent_bg
Usageusage
Worldworld
World2Dworld_2d

Methods

Worldfind_world ( ) const
World2Dfind_world_2d ( ) const
Cameraget_camera ( ) const
Transform2Dget_final_transform ( ) const
Controlget_modal_stack_top ( ) const
Vector2get_mouse_position ( ) const
intget_render_info ( RenderInfo info )
Vector2get_size_override ( ) const
ViewportTextureget_texture ( ) const
RIDget_viewport_rid ( ) const
Rect2get_visible_rect ( ) const
Variantgui_get_drag_data ( ) const
boolgui_has_modal_stack ( ) const
boolgui_is_dragging ( ) const
voidinput ( InputEvent local_event )
boolis_input_handled ( ) const
boolis_size_override_enabled ( ) const
boolis_size_override_stretch_enabled ( ) const
voidset_attach_to_screen_rect ( Rect2 rect )
voidset_input_as_handled ( )
voidset_size_override ( bool enable, Vector2 size=Vector2( -1, -1 ), Vector2 margin=Vector2( 0, 0 ) )
voidset_size_override_stretch ( bool enabled )
voidunhandled_input ( InputEvent local_event )
voidupdate_worlds ( )
voidwarp_mouse ( Vector2 to_position )

Signals

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

  • UPDATE_DISABLED = 0 — Do not update the render target.
  • UPDATE_ONCE = 1 — Update the render target once, then switch to UPDATE_DISABLED.
  • UPDATE_WHEN_VISIBLE = 2 — Update the render target only when it is visible. This is the default value.
  • UPDATE_ALWAYS = 3 — Always update the render target.

enum ShadowAtlasQuadrantSubdiv:

  • SHADOW_ATLAS_QUADRANT_SUBDIV_DISABLED = 0
  • SHADOW_ATLAS_QUADRANT_SUBDIV_1 = 1
  • SHADOW_ATLAS_QUADRANT_SUBDIV_4 = 2
  • SHADOW_ATLAS_QUADRANT_SUBDIV_16 = 3
  • SHADOW_ATLAS_QUADRANT_SUBDIV_64 = 4
  • SHADOW_ATLAS_QUADRANT_SUBDIV_256 = 5
  • SHADOW_ATLAS_QUADRANT_SUBDIV_1024 = 6
  • SHADOW_ATLAS_QUADRANT_SUBDIV_MAX = 7 — Represents the size of the ShadowAtlasQuadrantSubdiv enum.

enum RenderInfo:

  • RENDER_INFO_OBJECTS_IN_FRAME = 0 — Amount of objects in frame.
  • RENDER_INFO_VERTICES_IN_FRAME = 1 — Amount of vertices in frame.
  • RENDER_INFO_MATERIAL_CHANGES_IN_FRAME = 2 — Amount of material changes in frame.
  • RENDER_INFO_SHADER_CHANGES_IN_FRAME = 3 — Amount of shader changes in frame.
  • RENDER_INFO_SURFACE_CHANGES_IN_FRAME = 4 — Amount of surface changes in frame.
  • RENDER_INFO_DRAW_CALLS_IN_FRAME = 5 — Amount of draw calls in frame.
  • RENDER_INFO_MAX = 6 — Represents the size of the RenderInfo enum.

enum DebugDraw:

  • DEBUG_DRAW_DISABLED = 0 — Objects are displayed normally.
  • DEBUG_DRAW_UNSHADED = 1 — Objects are displayed without light information.
  • DEBUG_DRAW_OVERDRAW = 2 — Objected are displayed semi-transparent with additive blending so you can see where they intersect.
  • DEBUG_DRAW_WIREFRAME = 3 — Objects are displayed in wireframe style.

enum MSAA:

  • MSAA_DISABLED = 0 — Multisample anti-aliasing mode disabled. This is the default value.
  • MSAA_2X = 1
  • MSAA_4X = 2
  • MSAA_8X = 3
  • MSAA_16X = 4

enum Usage:

  • USAGE_2D = 0
  • USAGE_2D_NO_SAMPLING = 1
  • USAGE_3D = 2
  • USAGE_3D_NO_EFFECTS = 3

enum ClearMode:

  • CLEAR_MODE_ALWAYS = 0 — Always clear the render target before drawing.
  • CLEAR_MODE_NEVER = 1 — Never clear the render target.
  • CLEAR_MODE_ONLY_NEXT_FRAME = 2 — Clear the render target next frame, then switch to CLEAR_MODE_NEVER.

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, it will automatically take up its 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.

Tutorials

Property Descriptions

Setterset_use_arvr(value)
Getteruse_arvr()

If true, the viewport will be used in AR/VR process. Default value: false.


  • bool audio_listener_enable_2d
Setterset_as_audio_listener_2d(value)
Getteris_audio_listener_2d()

If true, the viewport will process 2D audio streams. Default value: false.


  • bool audio_listener_enable_3d
Setterset_as_audio_listener(value)
Getteris_audio_listener()

If true, the viewport will process 3D audio streams. Default value: false.


Setterset_canvas_transform(value)
Getterget_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.


Setterset_debug_draw(value)
Getterget_debug_draw()

The overlay mode for test rendered geometry in debug purposes. Default value: DEBUG_DRAW_DISABLED.


Setterset_disable_3d(value)
Getteris_3d_disabled()

If true, the viewport will disable 3D rendering. For actual disabling use usage. Default value: false.


Setterset_global_canvas_transform(value)
Getterget_global_canvas_transform()

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


  • bool gui_disable_input
Setterset_disable_input(value)
Getteris_input_disabled()

If true, the viewport will not receive input event. Default value: false.


  • bool gui_snap_controls_to_pixels
Setterset_snap_controls_to_pixels(value)
Getteris_snap_controls_to_pixels_enabled()

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


  • bool handle_input_locally
Setterset_handle_input_locally(value)
Getteris_handling_input_locally()

Setterset_hdr(value)
Getterget_hdr()

If true, the viewport rendering will receive benefits from High Dynamic Range algorithm. Default value: true.


  • bool keep_3d_linear
Setterset_keep_3d_linear(value)
Getterget_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.


Setterset_msaa(value)
Getterget_msaa()

The multisample anti-aliasing mode. Default value: MSAA_DISABLED.


Setterset_use_own_world(value)
Getteris_using_own_world()

If true, the viewport will use World defined in world property. Default value: false.


  • bool physics_object_picking
Setterset_physics_object_picking(value)
Getterget_physics_object_picking()

If true, the objects rendered by viewport become subjects of mouse picking process. Default value: false.


Setterset_clear_mode(value)
Getterget_clear_mode()

The clear mode when viewport used as a render target. Default value: CLEAR_MODE_ALWAYS.


Setterset_update_mode(value)
Getterget_update_mode()

The update mode when viewport used as a render target. Default value: UPDATE_WHEN_VISIBLE.


  • bool render_target_v_flip
Setterset_vflip(value)
Getterget_vflip()

If true, the result of rendering will be flipped vertically. Default value: false.


Setterset_shadow_atlas_quadrant_subdiv(value)
Getterget_shadow_atlas_quadrant_subdiv()

The subdivision amount of first quadrant on shadow atlas. Default value: SHADOW_ATLAS_QUADRANT_SUBDIV_4.


Setterset_shadow_atlas_quadrant_subdiv(value)
Getterget_shadow_atlas_quadrant_subdiv()

The subdivision amount of second quadrant on shadow atlas. Default value: SHADOW_ATLAS_QUADRANT_SUBDIV_4.


Setterset_shadow_atlas_quadrant_subdiv(value)
Getterget_shadow_atlas_quadrant_subdiv()

The subdivision amount of third quadrant on shadow atlas. Default value: SHADOW_ATLAS_QUADRANT_SUBDIV_16.


Setterset_shadow_atlas_quadrant_subdiv(value)
Getterget_shadow_atlas_quadrant_subdiv()

The subdivision amount of fourth quadrant on shadow atlas. Default value: SHADOW_ATLAS_QUADRANT_SUBDIV_64.


  • int shadow_atlas_size
Setterset_shadow_atlas_size(value)
Getterget_shadow_atlas_size()

The resolution of shadow atlas. Both width and height is equal to one value.


Setterset_size(value)
Getterget_size()

The width and height of viewport.


  • bool transparent_bg
Setterset_transparent_background(value)
Getterhas_transparent_background()

If true, the viewport should render its background as transparent. Default value: false.


Setterset_usage(value)
Getterget_usage()

The rendering mode of viewport. Default value: USAGE_3D.


Setterset_world(value)
Getterget_world()

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


Setterset_world_2d(value)
Getterget_world_2d()

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

Method Descriptions

  • World find_world ( ) const

Returns the 3D world of the viewport, or if none the world of the parent viewport.


Returns the 2D world of the viewport.


Returns the active 3D camera.


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 position relative to the viewport.


Returns information about the viewport from the rendering pipeline.


  • Vector2 get_size_override ( ) const

Returns the size override set with set_size_override.


Returns the viewport’s texture. Note that 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:

  1. var img = get_viewport().get_texture().get_data()
  2. 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_dragging ( ) const


  • bool is_input_handled ( ) const

  • bool is_size_override_enabled ( ) const

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


  • bool is_size_override_stretch_enabled ( ) const

  • void set_attach_to_screen_rect ( Rect2 rect )

  • void set_input_as_handled ( )

  • 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 set_size_override_stretch ( bool enabled )


  • void update_worlds ( )

Forces update of the 2D and 3D worlds.


  • void warp_mouse ( Vector2 to_position )

Warps the mouse to a position relative to the viewport.