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

Text2D placement and size are wrong (window scale factor) #10407

Closed
ManevilleF opened this issue Nov 6, 2023 · 5 comments · Fixed by #10741
Closed

Text2D placement and size are wrong (window scale factor) #10407

ManevilleF opened this issue Nov 6, 2023 · 5 comments · Fixed by #10741
Labels
A-Rendering Drawing game state to the screen A-Windowing Platform-agnostic interface layer to run your app in C-Bug An unexpected or incorrect behavior
Milestone

Comments

@ManevilleF
Copy link
Contributor

ManevilleF commented Nov 6, 2023

Bevy version

Bevy 0.12

[Optional] Relevant system information

I'm using MacOS and rust 1.73

AdapterInfo { name: "AMD Radeon Pro 5300M", vendor: 0, device: 0, device_type: DiscreteGpu, driver: "", driver_info: "", backend: Metal }

What you did

In this hexx example I'm spawning Text 2D bundles to display the grid coordinates.

What went wrong

In my migration PR for bevy 0.12 I noticed the text is very small and moved on the bottom left instead of being centered

Screen.Recording.2023-11-06.at.10.59.49.mp4

But When I enable the world inspector, and simply touch the font size, suddenly the text refreshes and appears at the right size and centered.

Thanks to @rparrett It seems that the underlying issue is related to windows with a scale_factor different than 1, like on my MacBook.

@ManevilleF ManevilleF added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Nov 6, 2023
@rparrett
Copy link
Contributor

rparrett commented Nov 6, 2023

Bisected to #9826. (But seemingly not fixed by #10389). I'm also on a Macbook.

To reproduce, cargo run --example text2d on a screen with scale_factor != 1.0, or with the following patch:

diff --git a/examples/2d/text2d.rs b/examples/2d/text2d.rs
index 18a959bef..2988e4bc7 100644
--- a/examples/2d/text2d.rs
+++ b/examples/2d/text2d.rs
@@ -9,11 +9,18 @@ use bevy::{
     prelude::*,
     sprite::Anchor,
     text::{BreakLineOn, Text2dBounds},
+    window::WindowResolution,
 };
 
 fn main() {
     App::new()
-        .add_plugins(DefaultPlugins)
+        .add_plugins(DefaultPlugins.set(WindowPlugin {
+            primary_window: Some(Window {
+                resolution: WindowResolution::default().with_scale_factor_override(2.0),
+                ..default()
+            }),
+            ..default()
+        }))
         .add_systems(Startup, setup)
         .add_systems(
             Update,

I seem to recall a similar issue popping up / maybe getting fixed where if a window was not available when the text was created, a default scale factor would be used, and it would never be updated after the window was created.

@rparrett rparrett added A-Rendering Drawing game state to the screen A-Windowing Platform-agnostic interface layer to run your app in and removed S-Needs-Triage This issue needs to be labelled labels Nov 6, 2023
@rparrett rparrett added this to the 0.12.1 milestone Nov 6, 2023
@rparrett
Copy link
Contributor

rparrett commented Nov 9, 2023

It seems that the resolution / scale_factor of Window are at their default values until frame 2.

@rparrett
Copy link
Contributor

rparrett commented Nov 9, 2023

Here's a new repro of the underlying bug (if you are on a display with scale_factor != 1.):

use bevy::{core::FrameCount, prelude::*, window::PrimaryWindow};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Update, dbg)
        .run();
}

fn dbg(query: Query<&Window, With<PrimaryWindow>>, frame: Res<FrameCount>) {
    if frame.0 > 1 {
        return;
    }
    for window in &query {
        info!("F{} {:?}", frame.0, window.resolution);
    }
}

This outputs (on my scale_factor: 2. display on macos):

// pre-9826
2023-11-09T18:20:51.732131Z  INFO bevy_winit::system: Creating new window "App" (0v0)
2023-11-09T18:20:51.818735Z  INFO windowbug: F0 WindowResolution { physical_width: 2560, physical_height: 1440, scale_factor_override: None, scale_factor: 2.0 }
2023-11-09T18:20:51.832487Z  INFO windowbug: F1 WindowResolution { physical_width: 2560, physical_height: 1440, scale_factor_override: None, scale_factor: 2.0 }

// post-9826
2023-11-09T18:19:16.527579Z  INFO windowbug: F0 WindowResolution { physical_width: 1280, physical_height: 720, scale_factor_override: None, scale_factor: 1.0 }
2023-11-09T18:19:16.554404Z  INFO bevy_winit::system: Creating new window "App" (0v0)
2023-11-09T18:19:16.610642Z  INFO windowbug: F1 WindowResolution { physical_width: 2560, physical_height: 1440, scale_factor_override: None, scale_factor: 2.0 }

It seems to get slightly weirder when there's a scale factor override involved (physical_width/height never update to match either the overridden or actual scale factor and this was the case even before 9826, but you can still see the bug in scale_factor)

/// pre-9826
2023-11-09T18:35:27.367530Z  INFO bevy_winit::system: Creating new window "App" (0v0)
2023-11-09T18:35:27.435296Z  INFO windowbug: F0 WindowResolution { physical_width: 500, physical_height: 300, scale_factor_override: Some(1.9), scale_factor: 2.0 }
2023-11-09T18:35:27.450425Z  INFO windowbug: F1 WindowResolution { physical_width: 500, physical_height: 300, scale_factor_override: Some(1.9), scale_factor: 2.0 }

// post-9826
2023-11-09T18:34:53.438184Z  INFO windowbug: F0 WindowResolution { physical_width: 500, physical_height: 300, scale_factor_override: Some(1.9), scale_factor: 1.0 }
2023-11-09T18:34:53.463914Z  INFO bevy_winit::system: Creating new window "App" (0v0)
2023-11-09T18:34:53.523100Z  INFO windowbug: F1 WindowResolution { physical_width: 500, physical_height: 300, scale_factor_override: Some(1.9), scale_factor: 2.0 }

@ManevilleF ManevilleF changed the title Text2D placement and size are wrong Text2D placement and size are wrong (window scale factor) Nov 10, 2023
@bytemunch
Copy link
Contributor

Using rparrett's repro I have different behaviour on X11

Lower DPI screen:

2023-11-10T12:41:24.206101Z  INFO bevy_winit::system: Creating new window "App" (0v0)
...
2023-11-10T12:41:24.908423Z  INFO scale_factor: F0 WindowResolution { physical_width: 1386, physical_height: 780, scale_factor_override: None, scale_factor: 1.0833333333333333 }
2023-11-10T12:41:24.931864Z  INFO scale_factor: F1 WindowResolution { physical_width: 1387, physical_height: 780, scale_factor_override: None, scale_factor: 1.0833333333333333 }

Higher DPI screen:

2023-11-10T12:41:32.651726Z  INFO bevy_winit::system: Creating new window "App" (0v0)
...
2023-11-10T12:41:33.354007Z  INFO scale_factor: F0 WindowResolution { physical_width: 2133, physical_height: 1200, scale_factor_override: None, scale_factor: 1.6666666666666667 }
2023-11-10T12:41:33.378956Z  INFO scale_factor: F1 WindowResolution { physical_width: 1916, physical_height: 1025, scale_factor_override: None, scale_factor: 1.6666666666666667 }

My system seems to create the window before any code runs.

@johnbchron
Copy link
Contributor

Do you have any workarounds? I'm experiencing the same issue.

github-merge-queue bot pushed a commit that referenced this issue Nov 26, 2023
# Objective

- Window size, scale and position are not correct on the first execution
of the systems
- Fixes #10407,  fixes #10642

## Solution

- Don't run `update` before we get a chance to create the window in
winit
- Finish reverting #9826 after #10389
cart pushed a commit that referenced this issue Nov 30, 2023
# Objective

- Window size, scale and position are not correct on the first execution
of the systems
- Fixes #10407,  fixes #10642

## Solution

- Don't run `update` before we get a chance to create the window in
winit
- Finish reverting #9826 after #10389
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen A-Windowing Platform-agnostic interface layer to run your app in C-Bug An unexpected or incorrect behavior
Projects
None yet
4 participants