Introduction: Why SubViewport Matters
When creating games in Godot Engine, expressing everything on a single screen has limitations. For example, how can you achieve advanced visual effects like displaying a minimap in a screen corner, embedding a 3D model window in a 2D game, or reusing parts of the screen as dynamic textures?
The key to this is Godot's powerful SubViewport node.
SubViewport is a node for creating "virtual screens" independent from the main viewport. This virtual screen can have its own scene tree, camera, and rendering settings, and its output can be obtained as a texture and displayed anywhere on the main screen. This dramatically enhances Godot's expressiveness and flexibility.
This article thoroughly explains from basic SubViewport concepts to three main practical applications useful in game production (minimaps, 3D in 2D, render textures) with concrete code examples, targeting beginners to intermediate developers.
SubViewport Basic Concepts and ViewportTexture
The SubViewport node is a container with its own independent rendering pipeline. Scenes placed as children of this node are rendered separately from the main screen.
To obtain this independent rendering result, use ViewportTexture. Enabling RenderTarget in SubViewport properties makes that viewport's output available as a texture. This texture can be integrated into the main scene by applying it to TextureRect, Sprite2D, or 3D materials.
Basic SubViewport Settings
| Property | Setting Value | Purpose |
|---|---|---|
| Size | Any resolution | Determines viewport rendering size. |
| Usage | RenderTarget | Enables using this viewport as a texture. |
| Update Mode | Always / WhenVisible | Sets rendering update timing. Choose Always when constantly updated info is needed like minimaps. |
Application 1: Practical Minimap Implementation
Minimaps are one of the most common and practical applications of SubViewport.
Implementation Steps
- Scene Preparation: Add a
SubViewportnode to the main game scene (e.g.,World). - Camera Placement: Place a minimap-dedicated
Camera2D(for 2D games) orCamera3D(for 3D games) as a child ofSubViewport. - Camera Setup: Configure the minimap camera independently from the main camera to look down from above at the player or specific area.
- Display: Add
TextureRectas a sibling node ofSubViewport, set "NewViewportTexture" in itsTextureproperty, and select the createdSubViewportfrom dropdown.
Camera Following Example with GDScript
Simple GDScript for making the minimap camera follow the player:
# minimap_camera.gd (attach to Camera2D inside SubViewport)
extends Camera2D
@export var target_node_path: NodePath
var target: Node2D
func _ready():
# Get target node from path set in editor
target = get_node_or_null(target_node_path)
if target == null:
print("Error: Target node not found for minimap camera.")
set_process(false) # Stop processing
func _process(delta):
if target:
# Move camera to target's position
global_position = target.global_position
# Adjust zoom level as needed
zoom = Vector2(0.5, 0.5)
Application 2: Incorporating 3D Elements into 2D Games (3D in 2D)
Godot excels at hybrid 2D and 3D development, and SubViewport enables seamless embedding of 3D scenes as 2D UI elements.
Implementation Steps
- SubViewport Setup: Place a
SubViewportnode in the 2D scene's UI layer and enableRenderTarget. - 3D Scene Construction: Add
Node3D,Camera3D, and theMeshInstance3Dyou want to display (e.g., player's 3D model, rotating item) as children ofSubViewport. - Camera and Lighting: Position
Camera3Dto properly capture 3D elements, and don't forget to place light sources likeDirectionalLight3D. - Display: Set
ViewportTextureinTextureRectto display 3D scene rendering results on the 2D screen.
This technique is very effective for displaying player equipment in 3D on inventory screens or rotating 3D models as hints in 2D puzzle games.
Application 3: Applications as Render Textures
Using SubViewport output as textures goes beyond mere UI display to enable more advanced graphic effects. This is using it as render textures.
Use Case: Dynamic Portals or Security Camera Screens
Portals, mirrors, and security camera screens are achieved by real-time rendering of different locations or viewpoints and pasting them as textures on objects.
-
Portal/Mirror Creation:
- Create a
SubViewportresponsible for the portal destination viewpoint. - Place a
Camera3Dinside thatSubViewportto capture the portal destination scene. - Apply
ViewportTextureto the material of the 3D object (e.g.,MeshInstance3D) that becomes the portal.
- Create a
-
Shader Integration:
ViewportTexturecan be used as shader input textures. For example, apply post-process effects like noise or distortion to security camera footage using shaders for more realistic expressions.
# Example dynamically setting texture to 3D object material
extends MeshInstance3D
@export var target_viewport: SubViewport
func _ready():
# Generate ViewportTexture and set SubViewport output
var viewport_texture = ViewportTexture.new()
viewport_texture.set_viewport_path(target_viewport.get_path())
# Get material and set texture
var material = get_surface_override_material(0)
if material is StandardMaterial3D:
material.albedo_texture = viewport_texture
elif material is ShaderMaterial:
# Set texture to shader uniform
material.set_shader_parameter("screen_texture", viewport_texture)
Summary
The SubViewport node is the foundation of advanced rendering techniques in Godot Engine.
| Application | Purpose | Main Components |
|---|---|---|
| Minimap | Display game world overview in screen corner. | SubViewport + Camera2D/3D + TextureRect |
| 3D in 2D | Embed 3D models or scenes within 2D game UI. | SubViewport + Node3D + Camera3D |
| Render Texture | Real-time reuse of different viewpoints or scenes as textures. | SubViewport + ViewportTexture + MeshInstance3D (material) |
Mastering these techniques makes your games more refined and capable of providing players with rich experiences. Please utilize SubViewport in your own projects and explore the infinite possibilities Godot holds.