Viewport

    Creates a sub-view into the screen.

    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 , 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

    Methods

    World ( ) const
    World2D ( ) const
    Camera ( ) const
    Transform2D ( ) const
    Control ( ) const
    Vector2 ( ) const
    int ( RenderInfo info )
    get_shadow_atlas_quadrant_subdiv ( quadrant ) const
    Vector2 ( ) const
    ViewportTexture ( ) const
    RID ( ) const
    Rect2 ( ) const
    Variant ( ) const
    bool ( ) const
    bool ( ) const
    voidinput ( local_event )
    bool ( ) const
    bool ( ) const
    voidset_attach_to_screen_rect ( rect )
    voidset_input_as_handled ( )
    void ( int quadrant, subdiv )
    voidset_size_override ( enable, Vector2 size=Vector2( -1, -1 ), margin=Vector2( 0, 0 ) )
    voidunhandled_input ( local_event )
    voidupdate_worlds ( )
    void ( Vector2 to_position )
    • gui_focus_changed ( 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:

    • UPDATE_DISABLED = 0 —- Do not update the render target.
    • UPDATE_ONCE = 1 —- Update the render target once, then switch to .
    • 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 —- This quadrant will not be used.
    • SHADOW_ATLAS_QUADRANT_SUBDIV_1 = 1 —- This quadrant will only be used by one shadow map.
    • SHADOW_ATLAS_QUADRANT_SUBDIV_4 = 2 —- This quadrant will be split in 4 and used by up to 4 shadow maps.
    • SHADOW_ATLAS_QUADRANT_SUBDIV_16 = 3 —- This quadrant will be split 16 ways and used by up to 16 shadow maps.
    • SHADOW_ATLAS_QUADRANT_SUBDIV_64 = 4 —- This quadrant will be split 64 ways and used by up to 64 shadow maps.
    • 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.
    • SHADOW_ATLAS_QUADRANT_SUBDIV_1024 = 6 —- This quadrant will be split 1024 ways and used by up to 1024 shadow maps. Unless the is very high, the shadows in this quadrant will be very low resolution.
    • 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_2D_DRAW_CALLS_IN_FRAME = 7 —- Amount of draw calls in frame.
    • RENDER_INFO_MAX = 8 —- Represents the size of the 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 —- Use 2x Multisample Antialiasing.
    • MSAA_4X = 2 —- Use 4x Multisample Antialiasing.
    • MSAA_8X = 3 —- Use 8x Multisample Antialiasing. Likely unsupported on low-end and older hardware.
    • MSAA_16X = 4 —- Use 16x Multisample Antialiasing. Likely unsupported on medium and low-end hardware.

    enum 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_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_3D = 2 —- Allocates full buffers for drawing 3D scenes and all 3D effects including buffers needed for 2D scenes and effects.
    • 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:

    • 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 .
    Default
    Setterset_use_arvr(value)
    Getteruse_arvr()

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


    • audio_listener_enable_2d
    Defaultfalse
    Setterset_as_audio_listener_2d(value)
    Getteris_audio_listener_2d()

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


    • bool audio_listener_enable_3d
    Defaultfalse
    Setterset_as_audio_listener(value)
    Getteris_audio_listener()

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


    • canvas_transform
    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.


    • debanding
    Defaultfalse
    Setterset_use_debanding(value)
    Getterget_use_debanding()

    If true, uses a fast post-processing filter to make banding significantly less visible. 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.


    • debug_draw
    Default0
    Setterset_debug_draw(value)
    Getterget_debug_draw()

    The overlay mode for test rendered geometry in debug purposes.


    Defaultfalse
    Setterset_disable_3d(value)
    Getteris_3d_disabled()

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


    • fxaa
    Defaultfalse
    Setterset_use_fxaa(value)
    Getterget_use_fxaa()

    Setterset_global_canvas_transform(value)
    Getterget_global_canvas_transform()

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


    • gui_disable_input

    If true, the viewport will not receive input events.


    • bool gui_snap_controls_to_pixels
    Defaulttrue
    Setterset_snap_controls_to_pixels(value)
    Getteris_snap_controls_to_pixels_enabled()

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


    • handle_input_locally
    Defaulttrue
    Setterset_handle_input_locally(value)
    Getteris_handling_input_locally()

    Defaulttrue
    Setterset_hdr(value)
    Getterget_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 16 bits, meaning it does not store the full range of a floating point number.

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


    • bool keep_3d_linear
    Defaultfalse
    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.


    • msaa
    Default0
    Setterset_msaa(value)
    Getterget_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.


    Defaultfalse
    Setterset_use_own_world(value)
    Getteris_using_own_world()

    If true, the viewport will use defined in world property.


    • bool physics_object_picking
    Defaultfalse
    Setterset_physics_object_picking(value)
    Getterget_physics_object_picking()

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


    • render_direct_to_screen
    Defaultfalse
    Setterset_use_render_direct_to_screen(value)
    Getteris_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.


    Default0
    Setterset_clear_mode(value)
    Getterget_clear_mode()

    The clear mode when viewport used as a render target.

    Note: This property is intended for 2D usage.


    • render_target_update_mode
    Default2
    Setterset_update_mode(value)
    Getterget_update_mode()

    The update mode when viewport used as a render target.


    • bool render_target_v_flip

    If true, the result of rendering will be flipped vertically.


    • shadow_atlas_quad_0
    Default2
    Setterset_shadow_atlas_quadrant_subdiv(value)
    Getterget_shadow_atlas_quadrant_subdiv()

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


    Default2
    Setterset_shadow_atlas_quadrant_subdiv(value)
    Getterget_shadow_atlas_quadrant_subdiv()

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


    • shadow_atlas_quad_2
    Default3
    Setterset_shadow_atlas_quadrant_subdiv(value)
    Getterget_shadow_atlas_quadrant_subdiv()

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


    Default4
    Setterset_shadow_atlas_quadrant_subdiv(value)
    Getterget_shadow_atlas_quadrant_subdiv()

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


    • shadow_atlas_size
    Default0
    Setterset_shadow_atlas_size(value)
    Getterget_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, shadows won’t be visible. Since user-created viewports default to a value of 0, this value must be set above 0 manually.


    DefaultVector2( 0, 0 )
    Setterset_size(value)
    Getterget_size()

    The width and height of viewport.


    • size_override_stretch
    Defaultfalse
    Setterset_size_override_stretch(value)
    Getteris_size_override_stretch_enabled()

    If true, the size override affects stretch as well.


    • bool transparent_bg
    Defaultfalse
    Setterset_transparent_background(value)
    Getterhas_transparent_background()

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


    • usage
    Default2
    Setterset_usage(value)
    Getterget_usage()

    The rendering mode of viewport.


    Setterset_world(value)
    Getterget_world()

    • world_2d

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

    Method Descriptions

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


    • get_camera ( ) const

    Returns the active 3D camera.


    Returns the total transform of the viewport.


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


    • get_shadow_atlas_quadrant_subdiv ( int quadrant ) const

    Returns the of the specified quadrant.


    • Vector2 get_size_override ( ) const

    Returns the size override set with .


    Returns the viewport’s texture.

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


    • RID get_viewport_rid ( ) const

    Returns the viewport’s RID from the .


    • Rect2 get_visible_rect ( ) const

    Returns the visible rectangle in global screen coordinates.


    • gui_get_drag_data ( ) const

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


    • gui_has_modal_stack ( ) const

    Returns true if there are visible modals on-screen.


    • bool gui_is_dragging ( ) const

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


    • void input ( local_event )

    • bool is_input_handled ( ) const

    • is_size_override_enabled ( ) const

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


    • void set_attach_to_screen_rect ( 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.


    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 ( enable, Vector2 size=Vector2( -1, -1 ), 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 , it won’t update the size.



    Forces update of the 2D and 3D worlds.


    • void warp_mouse ( to_position )

    Warps the mouse to a position relative to the viewport.