0 votes

I want to keep the aspect ratio of the game world (2d) for multiple reasons (most important one being related to where the player can and cannot go) but I also want to use the excess space (what whould be black bars if the project was configured to actually keep the aspect ratio) between the world and the window edge for ui rendering.

My current idea for this is rendering the world in a Viewport then using BoxContainers for automatic backgrounds and just rendering the UI above everything else here, repositioning and rotating it as convenient.

The problem with this approach is that the Viewport doesn't scale it's resolution, so it ends up being pixelated on higher window sizes... anyone has a better way to implement this or knows how to handle dynamically changing the Viewport resolution without zooming out the camera?

Godot version v3.5.stable.arch_linux
in Engine by (15 points)

1 Answer

+1 vote
Best answer

I had to do this too, and didn't find any way to mark viewports explicitly as being 2D like in the ProjectSettings.

I had to dig in the engine code (https://github.com/godotengine/godot/blob/b38ac3f09fb5386ba8236db2b3efcf654a9e27d8/scene/main/scene_tree.cpp#L1314) to figure out what those settings do, and remake them in GDScript. So, for having a Viewport act as 2D, check for changes in the main viewport size, and then apply those settings to it:

onready var n_viewport : Viewport = $ViewportContainer/MyViewport
onready var viewport_orig_size := n_viewport.size

func _ready():
    get_viewport().connect("size_changed", self, "_on_vp_size_changed")

func _on_vp_size_changed() -> void:
    print("New size: ", get_viewport().size)
    n_viewport.size = get_viewport().size
    n_viewport.size_override_stretch = true
    n_viewport.set_size_override(true, viewport_orig_size)
by (286 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.