Open ewrogers opened 1 year ago
For those wondering how to do this properly, setting the scale
value for the projection
when creating a Camera2dBundle
is the way to go:
const REF_WIDTH: f32 = 640.;
pub fn setup_camera(mut commands: Commands, primary_window: Query<&Window, With<PrimaryWindow>>) {
let window = primary_window.single();
let (width, height) = (window.resolution.width(), window.resolution.height());
// Scale the camera to match the reference resolution
// This is done to retain the pixel-art look and sharpness
let scale = window.resolution.width() / REF_WIDTH;
commands.spawn((
Camera2dBundle {
projection: OrthographicProjection { scale, ..default() },
..default()
},
MainCamera,
));
info!(
"Camera initialized for resolution: {}x{} (scale: {:.1})",
width, height, scale
);
}
You don't need to mess with the window.resolution.scale_factor_override
at this point.
Probably a good thing to add to docs somewhere for those going for pixel-art aesthetics and integer scaling on various resolutions.
This is specific to MonitorSelection::Current
.
It seems like this would be relatively easy to fix for the case where you are positioning the window after initialization as shown in the issue description.
But it's not as clear when the position is configured in the plugin like
app.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
resolution: WindowResolution::default().with_scale_factor_override(1.4),
position: WindowPosition::Centered(MonitorSelection::Primary),
..default()
}),
..default()
}));
Because the "current monitor" can't be known before the window is created.
However, the full-screen WindowModes
seem to make the assumption that the window should be shown on the primary monitor, which we could perhaps do here.
The current_monitor
seems to be immediately available after building the winit window on macos but I'm not sure if that's the case one very platform. But perhaps window centering could be moved just after that.
Bevy version
0.11.2
[Optional] Relevant system information
What you did
DefaultPlugins
primary_window
to 640x360 in theWindowPlugin
initializationStartup
system forsetup_resolution
that adjustswindow.resolution.set_scale_factor_override
window.position.center(MonitorSelection::Current)
to center the windowscale_factor
instead of overrideWhat went wrong
The window should be centered using the new
scale_factor_override
instead of the currentscale_factor
from the OS.Ex:
Additional information
Only tested on macOS currently, not sure if works properly on Windows due to how scale factor works.
I encountered this by working on a pixel-art game with a reference resolution of 640x360 and want to apply a scale factor to retain the sharpness via integer scaling (1x, 2x, 3x, 4x, etc). I don't know if this the correct way, but it did expose this bug.