Description
The Viewport
property of the Graphics
class in the Sandbox API represents the pixel dimensions of the area where rendering operations are performed. This static property provides the size and position of the rendering target, which is crucial for determining where and how graphics are drawn on the screen.
Usage
Use the Viewport
property to retrieve the current rendering area dimensions. This can be useful when you need to perform operations that depend on the size of the rendering target, such as setting up camera views or aligning UI elements.
Example
// Example of accessing the Viewport property
Rect currentViewport = Graphics.Viewport;
// Use the viewport dimensions for rendering calculations
float width = currentViewport.Width;
float height = currentViewport.Height;
// Example: Adjusting a camera's view based on the viewport size
Camera camera = new Camera();
camera.SetProjectionMatrix(Matrix4x4.CreatePerspectiveFieldOfView(
MathF.PI / 4, // 45 degrees field of view
width / height, // Aspect ratio
0.1f, // Near plane
1000f // Far plane
));