Scale or zoom only background, not HUD (pop-up menu etc)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mjtorn

Hi!

I’m involved with the Escoria adventure game framework, trying to get some features that make for modern games, and am struggling with zooming.

The use-cases include talking to NPCs so that the camera closes in on the conversation and having the player move to a part of the screen and the camera pushes in.

Thus far my best results have been calculating the game ratio and using

func set_zoom_height(zoom_height):
  var game_size_ratio = game_size.x / game_size.y
  zoom_viewport = Vector2(zoom_height * game_size_ratio, zoom_height)
  viewport.set_size_override(true, zoom_viewport)

However, when I pass in a completely other zoom_height, the other things in the screen, like tooltips or the right-mouse-button menu, are also zoomed in!

I have not been able to find an approach to fixing this, so does anyone here have leads for me?

I have tried using a different viewport than /root (and a separate Camera2D as its child), but apparently it doesn’t matter - anything rendered in the viewport always gets scaled. Is this a correct understanding?

Also I dabbled in calling .set_size() on child elements, but this does not seem to scale them at all, as if the call has no effect! Should it?

This solution would probably perform very badly, because the engine would constantly have to check if the scaling has already been applied. Am I right?

Is it possible to have two different viewports overlap, only one of which gets scaled?

Would there be a completely different approach I haven’t tried yet?

Thanks!

Interesting question! I worked with Camera2D plus extra CanvasLayers for GUI so I never encountered such a problem. But when talking about tooltips and the right-mouse-button menu I can understand your problem. As far as I know there is no specific option for this purpose in the Camera or Viewport classes. If you don’t receive any working answer here you should create an issue in the github repo (and maybe don’t mention that you are working with escoria framework because it could seem to be an escoria related question - which it is not). Would be nice if you could keep us updated here.

rolfpancake | 2018-03-06 09:25

Hi!

Thanks for the reply and sorry for taking so long to get back to this.

All my theories of viewports were wrong and the correct implementation is about layering the HUD into a separate CanvasLayer.

There is a related problem, although some code was merged here

Merge pull request #80 from mjtorn/gh-mjtorn-flesk-camera-zoom · godot-escoria/escoria-demo-game@895a533 · GitHub

When scaling the scene, the camera appears to be off.

You can see, a couple of lines up at Merge pull request #80 from mjtorn/gh-mjtorn-flesk-camera-zoom · godot-escoria/escoria-demo-game@895a533 · GitHub that a transform is applied to the canvas.

The purpose of this is to “clamp” the camera to stay within its given parameters, and there is a PR to make the code a bit clearer and more modern.

Anyway, the camera does not remain on its given target position, eg. the player character’s position, but “zooms in” somewhere beside it. I believe the arithmetic requires something extra, but I do not understand what it might be. Trying to multiply or divide by the scale leads to some results, but never the correct ones.

Do you have any ideas or experience in this?

Thanks!

mjtorn | 2018-03-15 09:04