Skip to content

Commit 4adaaa2

Browse files
committed
Simplify the 3D scaling demo setup, enable filtering by default
It turns out using a TextureRect node isn't necessary :) The typical ViewportContainer + Viewport setup can be used just fine to enable filtering on the ViewportTexture returned by the Viewport. The performance of the new method is equivalent to the old one.
1 parent b946d20 commit 4adaaa2

File tree

4 files changed

+28
-51
lines changed

4 files changed

+28
-51
lines changed

viewport/3d_scaling/README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
# 3D Viewport Scaling
22

3-
This demo shows how to scale the 3D viewport rendering without affecting
4-
2D elements such as the HUD. It also demonstrates how to toggle filtering
5-
on a viewport by using TextureRect to display the ViewportTexture
6-
delivered by the Viewport node. This technique can be useful in 2D games
7-
as well. For instance, it can be used to have a "pixel art" viewport for
8-
the main game area and a non-pixel-art viewport for HUD elements.
9-
10-
ViewportContainer can also be used to display a viewport in a GUI, but it
11-
doesn't offer the ability to enable filtering.
3+
This demo shows how to scale the 3D viewport rendering without affecting 2D
4+
elements such as the HUD. It also demonstrates how to toggle filtering on a
5+
viewport. This technique can be useful in 2D games as well. For instance, it can
6+
be used to have a "pixel art" viewport for the main game area and a
7+
non-pixel-art viewport for HUD elements.
128

139
Language: GDScript
1410

viewport/3d_scaling/hud.gd

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
extends Control
22

3-
# The viewport is displayed using a TextureRect node instead of a ViewportContainer.
4-
# This allows filtering the texture that's displayed in the root viewport.
5-
63
# The 3D viewport's scale factor. For instance, 1.0 is full resolution,
74
# 0.5 is half resolution and 2.0 is double resolution. Higher values look
85
# sharper but are slower to render. Values above 1 can be used for supersampling
9-
# (SSAA), but filtering must be enabled for this to work.
6+
# (SSAA), but filtering must be enabled for supersampling to work.
107
var scale_factor = 1.0
118

12-
onready var texture_rect = $TextureRect
13-
onready var viewport = $Viewport
9+
onready var viewport = $ViewportContainer/Viewport
1410
onready var scale_label = $VBoxContainer/Scale
1511
onready var filter_label = $VBoxContainer/Filter
1612

1713
func _ready():
14+
viewport.get_texture().flags = Texture.FLAG_FILTER
15+
1816
# Required to change the 3D viewport's size when the window is resized.
1917
# warning-ignore:return_value_discarded
2018
get_viewport().connect("size_changed", self, "_root_viewport_size_changed")
@@ -28,9 +26,9 @@ func _unhandled_input(event):
2826

2927
if event.is_action_pressed("toggle_filtering"):
3028
# Toggle the Filter flag on the ViewportTexture.
31-
texture_rect.texture.flags ^= ImageTexture.FLAG_FILTER
29+
viewport.get_texture().flags ^= Texture.FLAG_FILTER
3230

33-
var filter_enabled = texture_rect.texture.flags & ImageTexture.FLAG_FILTER
31+
var filter_enabled = viewport.get_texture().flags & Texture.FLAG_FILTER
3432
filter_label.text = "Filter: %s" % ("Enabled" if filter_enabled else "Disabled")
3533

3634

viewport/3d_scaling/hud.tscn

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[gd_scene load_steps=7 format=2]
1+
[gd_scene load_steps=6 format=2]
22

33
[ext_resource path="res://noto_sans_ui_regular.ttf" type="DynamicFontData" id=1]
44
[ext_resource path="res://cubes.tscn" type="PackedScene" id=2]
@@ -11,9 +11,6 @@ font_data = ExtResource( 1 )
1111
[sub_resource type="Theme" id=2]
1212
default_font = SubResource( 1 )
1313

14-
[sub_resource type="ViewportTexture" id=3]
15-
viewport_path = NodePath("Viewport")
16-
1714
[node name="HUD" type="Control"]
1815
anchor_right = 1.0
1916
anchor_bottom = 1.0
@@ -23,33 +20,23 @@ __meta__ = {
2320
"_edit_use_anchors_": false
2421
}
2522

26-
[node name="Viewport" type="Viewport" parent="."]
27-
size = Vector2( 1024, 600 )
28-
handle_input_locally = false
29-
usage = 3
30-
render_target_update_mode = 3
31-
shadow_atlas_size = 4096
32-
33-
[node name="Cubes" parent="Viewport" instance=ExtResource( 2 )]
34-
35-
[node name="TextureRect" type="TextureRect" parent="."]
36-
anchor_right = 1.0
37-
anchor_bottom = 1.0
38-
texture = SubResource( 3 )
39-
expand = true
40-
flip_v = true
41-
__meta__ = {
42-
"_edit_use_anchors_": false,
43-
"_editor_description_": ""
44-
}
45-
4623
[node name="ViewportContainer" type="ViewportContainer" parent="."]
4724
anchor_right = 1.0
4825
anchor_bottom = 1.0
26+
stretch = true
4927
__meta__ = {
5028
"_edit_use_anchors_": false
5129
}
5230

31+
[node name="Viewport" type="Viewport" parent="ViewportContainer"]
32+
size = Vector2( 1024, 600 )
33+
handle_input_locally = false
34+
usage = 3
35+
render_target_update_mode = 3
36+
shadow_atlas_size = 4096
37+
38+
[node name="Cubes" parent="ViewportContainer/Viewport" instance=ExtResource( 2 )]
39+
5340
[node name="Help" type="Label" parent="."]
5441
anchor_top = 1.0
5542
anchor_bottom = 1.0
@@ -98,7 +85,7 @@ custom_colors/font_color_shadow = Color( 0, 0, 0, 0.752941 )
9885
custom_constants/shadow_offset_x = 2
9986
custom_constants/shadow_offset_y = 2
10087
custom_constants/shadow_as_outline = 0
101-
text = "Filter: Disabled"
88+
text = "Filter: Enabled"
10289
__meta__ = {
10390
"_edit_use_anchors_": false
10491
}

viewport/3d_scaling/project.godot

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@ _global_script_class_icons={
1616
[application]
1717

1818
config/name="3D Viewport Scaling"
19-
config/description="This demo shows how to scale the 3D viewport rendering without affecting
20-
2D elements such as the HUD. It also demonstrates how to toggle filtering
21-
on a viewport by using TextureRect to display the ViewportTexture
22-
delivered by the Viewport node. This technique can be useful in 2D games
23-
as well. For instance, it can be used to have a 'pixel art' viewport for
24-
the main game area and a non-pixel-art viewport for HUD elements.
25-
26-
ViewportContainer can also be used to display a viewport in a GUI, but it
27-
doesn't offer the ability to enable filtering."
19+
config/description="This demo shows how to scale the 3D viewport rendering without affecting 2D
20+
elements such as the HUD. It also demonstrates how to toggle filtering on a
21+
viewport. This technique can be useful in 2D games as well. For instance, it can
22+
be used to have a \"pixel art\" viewport for the main game area and a
23+
non-pixel-art viewport for HUD elements."
2824
run/main_scene="res://hud.tscn"
2925
config/icon="res://icon.png"
3026

0 commit comments

Comments
 (0)