Add a warning for the use of Viewport with Window targets (#21830)

# Objective

This PR adds a warning in the Viewport portion of the Camera class to
highlight the need to scale to physical pixels. This PR fixes #21828

## Solution

Adds a warning block in the class documentation

## Testing

I build the documentation and it renders to the following:

<img width="827" height="496" alt="Camrea_Viewport_Warning"
src="https://github.com/user-attachments/assets/d81b82c6-565b-450c-89ed-f6643f22ea23"
/>

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: ickshonpe <david.curthoys@googlemail.com>
Co-authored-by: François Mockers <francois.mockers@vleue.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
This commit is contained in:
Justace Clutter
2026-05-05 23:35:37 -04:00
committed by GitHub
parent e761a1799b
commit 15b23b4e4a
+33
View File
@@ -24,6 +24,39 @@ use wgpu_types::{BlendState, TextureUsages};
/// The viewport defines the area on the render target to which the camera renders its image.
/// You can overlay multiple cameras in a single window using viewports to create effects like
/// split screen, minimaps, and character viewers.
///
/// <div class="warning">
///
/// Note that the physical position is in actual screen coordinates and not virtual pixels for window targets.
/// You should use the scaling factor reported by the window, which on some OS's defaults to a value other than 1.
/// Please see the example code (which assumes a single camera and window)
///
/// ```no_run
/// # use bevy_camera::{Camera, Projection, Viewport};
/// # use bevy_transform::prelude::Transform;
/// # use bevy_ecs::prelude::*;
/// # use bevy_math::UVec2;
/// # use bevy_window::Window;
/// # use bevy_utils::default;
///
/// fn update_viewport(
/// mut camera_query: Query<(&mut Camera, &mut Transform, &mut Projection)>,
/// windows: Query<&Window>
/// ) {
/// let Ok((mut camera, _, _)) = camera_query.single_mut() else { return; };
///
/// let window = windows.single().expect("Window not found");
/// let scale = window.resolution.scale_factor();
///
/// camera.viewport = Some(Viewport {
/// physical_position: (UVec2::new(10, 10).as_vec2() * scale).as_uvec2(),
/// physical_size: (UVec2::new(100, 100).as_vec2() * scale).as_uvec2(),
/// ..default()
/// });
/// }
/// ```
///
/// </div>
#[derive(Reflect, Debug, Clone)]
#[reflect(Default, Clone)]
pub struct Viewport {