Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update viewport scaling demo to 4.0. #891

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions viewport/3d_scaling/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
# 3D Viewport Scaling
# 3D Resolution Scaling

This demo shows how to scale the 3D viewport rendering without affecting 2D
elements such as the HUD. It also demonstrates how to toggle filtering on a
viewport. This technique can be useful in 2D games as well. For instance, it can
be used to have a "pixel art" viewport for the main game area and a
non-pixel-art viewport for HUD elements.
This demo shows how to downscale the 3D resolution without affecting 2D
elements, to improve performance without making the UI blurry.

Language: GDScript

Renderer: GLES 2

Check out this demo on the asset library: https://godotengine.org/asset-library/asset/586

## Screenshots

![Screenshot](screenshots/high.png)
Expand Down
46 changes: 16 additions & 30 deletions viewport/3d_scaling/hud.gd
Original file line number Diff line number Diff line change
@@ -1,42 +1,28 @@
extends Control

# The 3D viewport's scale factor. For instance, 1.0 is full resolution,
# 0.5 is half resolution and 2.0 is double resolution. Higher values look
# sharper but are slower to render. Values above 1 can be used for supersampling
# (SSAA), but filtering must be enabled for supersampling to work.
var scale_factor = 1.0

@onready var viewport_container = $SubViewportContainer
@onready var viewport = $SubViewportContainer/SubViewport

# The 3D viewport's shrink factor. For instance, 1 is full resolution,
rcorre marked this conversation as resolved.
Show resolved Hide resolved
# 2 is half resolution and 4 is quarter resolution. Lower values look
# sharper but are slower to render.
var scale_factor = 1
var filter_mode = Viewport.SCALING_3D_MODE_BILINEAR

@onready var viewport = get_tree().root
@onready var scale_label = $VBoxContainer/Scale
@onready var filter_label = $VBoxContainer/Filter

func _ready():
viewport_container.texture_filter = CanvasItem.TEXTURE_FILTER_LINEAR

# Required to change the 3D viewport's size when the window is resized.
viewport.size_changed.connect(self._root_viewport_size_changed)
func _ready():
rcorre marked this conversation as resolved.
Show resolved Hide resolved
viewport.scaling_3d_mode = Viewport.SCALING_3D_MODE_BILINEAR
rcorre marked this conversation as resolved.
Show resolved Hide resolved


func _unhandled_input(event):
if event.is_action_pressed("cycle_viewport_resolution"):
scale_factor = wrapf(scale_factor + 0.25, 0.25, 2.25)
viewport.size = get_viewport().size * scale_factor
scale_label.text = "Scale: %s%%" % str(scale_factor * 100)
scale_factor = wrapi(scale_factor + 1, 1, 5)
viewport.scaling_3d_scale = 1.0 / scale_factor
scale_label.text = "Scale: %3.0f%%" % (100.0 / scale_factor)

if event.is_action_pressed("toggle_filtering"):
# Toggle the Filter flag on the ViewportTexture.
if viewport_container.texture_filter == CanvasItem.TEXTURE_FILTER_LINEAR:
viewport_container.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
filter_label.text = "Filter: Disabled"
else:
viewport_container.texture_filter = CanvasItem.TEXTURE_FILTER_LINEAR
filter_label.text = "Filter: Enabled"


# Called when the root's viewport size changes (i.e. when the window is resized).
# This is done to handle multiple resolutions without losing quality.
func _root_viewport_size_changed():
# The viewport is resized depending on the window height.
# To compensate for the larger resolution, the viewport sprite is scaled down.
viewport.size = get_viewport().size * scale_factor
filter_mode = wrapi(filter_mode + 1, Viewport.SCALING_3D_MODE_BILINEAR, Viewport.SCALING_3D_MODE_MAX) as Viewport.Scaling3DMode
viewport.scaling_3d_mode = filter_mode
filter_label.text = ClassDB.class_get_enum_constants("Viewport", "Scaling3DMode")[filter_mode].capitalize()
28 changes: 8 additions & 20 deletions viewport/3d_scaling/hud.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,15 @@ layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
theme = SubResource("2")
script = ExtResource("3")

[node name="SubViewportContainer" type="SubViewportContainer" parent="."]
texture_filter = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
stretch = true

[node name="SubViewport" type="SubViewport" parent="SubViewportContainer"]
handle_input_locally = false
size = Vector2i(1152, 648)
render_target_update_mode = 4

[node name="Cubes" parent="SubViewportContainer/SubViewport" instance=ExtResource("2")]
[node name="Cubes" parent="." instance=ExtResource("2")]

[node name="Help" type="Label" parent="."]
anchors_preset = 2
layout_mode = 0
anchor_top = 1.0
anchor_bottom = 1.0
offset_left = 16.0
Expand All @@ -40,18 +30,16 @@ Press Space to adjust the 3D viewport's resolution scaling.
Press Enter to toggle filtering."

[node name="VBoxContainer" type="VBoxContainer" parent="."]
layout_mode = 0
offset_left = 16.0
offset_top = 16.0
offset_right = 124.0
offset_bottom = 76.0

[node name="Scale" type="Label" parent="VBoxContainer"]
offset_right = 111.0
offset_bottom = 26.0
layout_mode = 2
text = "Scale: 100%"

[node name="Filter" type="Label" parent="VBoxContainer"]
offset_top = 30.0
offset_right = 111.0
offset_bottom = 56.0
text = "Filter: Enabled"
layout_mode = 2
text = "Scaling 3d Mode Fsr"
Binary file modified viewport/3d_scaling/screenshots/high.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified viewport/3d_scaling/screenshots/low.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.