godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.48k stars 21.26k forks source link

Change window size at runtime will shrink viewport size in android #98088

Open AllenDang opened 1 month ago

AllenDang commented 1 month ago

Tested versions

4.3 Stable

System information

Android

Issue description

I'm implementing a viewport setting for my game, to let user select and change the viewport size at runtime.

With following project settings of window image

Change window.size and window.scaling_3d_scale at runtime, will shrink the viewport size, but when I put the game in background and switch back, viewport size will scale to full screen size.

Here is the code I used

        var viewport_width := 960
    var viewport_height := 540
    var scale := 1.0

    match visual:
        HM_VISUAL.VISUAL_LOW:
            viewport_width = 960
            viewport_height = 540
            scale = 1.0
        HM_VISUAL.VISUAL_MED:
            viewport_width = 1280
            viewport_height = 720
            scale = 1.33
        HM_VISUAL.VISUAL_HIGH:
            viewport_width = 1920
            viewport_height = 1080
            scale = 2.0

    var win := get_window()
    win.size = Vector2i(viewport_width, viewport_height)
    win.scaling_3d_scale = scale

Here is a video to show the effect

https://github.com/user-attachments/assets/7eaac904-aa4b-4409-8b3d-d739e80e98be

Steps to reproduce

Like the video shows

  1. change the visual to 'low'
  2. put the game in background
  3. switch back to the game

Minimal reproduction project (MRP)

repoissue98088.zip

Calinou commented 1 month ago

Which Android device model did you test this on, and which rendering method are you using in the project?

Also, I'm curious why you need to change both the viewport size and the 3D scaling property. Typically, you change the viewport size (for 2D games) or the 3D scaling (for 3D games), not both.

PS: If you're using the disabled or canvas_items stretch mode, changing the root viewport size is not supported as it'll be overridden on window resize events. This includes restoring the activity on Android, due to possible orientation or display folding changes. You need to use the viewport stretch mode to allow this instead, but viewport sizes that are not a divisor of the screen size will result in uneven pixel scaling (which looks bad).

llama-nl commented 1 month ago

Tested versions

4.3 Stable

System information

Android

Issue description

I'm implementing a viewport setting for my game, to let user select and change the viewport size at runtime.

With following project settings of window image

Change window.size and window.scaling_3d_scale at runtime, will shrink the viewport size, but when I put the game in background and switch back, viewport size will scale to full screen size.

Here is the code I used

        var viewport_width := 960
  var viewport_height := 540
  var scale := 1.0

  match visual:
      HM_VISUAL.VISUAL_LOW:
          viewport_width = 960
          viewport_height = 540
          scale = 1.0
      HM_VISUAL.VISUAL_MED:
          viewport_width = 1280
          viewport_height = 720
          scale = 1.33
      HM_VISUAL.VISUAL_HIGH:
          viewport_width = 1920
          viewport_height = 1080
          scale = 2.0

  var win := get_window()
  win.size = Vector2i(viewport_width, viewport_height)
  win.scaling_3d_scale = scale

Here is a video to show the effect

20241011-211821.mp4

Steps to reproduce

Like the video shows

  1. change the visual to 'low'
  2. put the game in background
  3. switch back to the game

Minimal reproduction project (MRP)

N/A

Provide a example project to test.

AllenDang commented 1 month ago

Which Android device model did you test this on, and which rendering method are you using in the project?

Also, I'm curious why you need to change both the viewport size and the 3D scaling property. Typically, you change the viewport size (for 2D games) or the 3D scaling (for 3D games), not both.

PS: If you're using the disabled or canvas_items stretch mode, changing the root viewport size is not supported as it'll be overridden on window resize events. This includes restoring the activity on Android, due to possible orientation or display folding changes. You need to use the viewport stretch mode to allow this instead, but viewport sizes that are not a divisor of the screen size will result in uneven pixel scaling (which looks bad).

@Calinou

The issue happens on all our android devices, for stretch mode, like the screen shot I provided in the issue, are set to:

Stretch mode: viewport Aspect: expand Render method: mobile

For the reason why I change viewport size and 3d scaling, check https://docs.godotengine.org/en/stable/tutorials/3d/resolution_scaling.html#resolution-scaling-options, I do it for better performance.

AllenDang commented 1 month ago

@UnderGamer05 Sure, I added the MVP project in the post, you can also download it here. repoissue98088.zip

llama-nl commented 1 month ago

Ok, this is happening with me either.

AllenDang commented 3 weeks ago

Any update?

syntaxerror247 commented 1 week ago

@AllenDang window_set_size is not supported on Android. The viewport size change is a bug. I’ll work on a fix and submit a PR soon.

syntaxerror247 commented 5 days ago

Can Someone confirm if this issue occurs on Wayland and iOS?