Struct PixelScale
Defines the relationship between world-space length and screen-space pixels, encoding both the scale factor and the world unit together.
public readonly record struct PixelScale : IEquatable<PixelScale>
- Implements
- Inherited Members
Remarks
A PixelScale answers the question: "how many pixels does one world unit
occupy on screen?" For example, PixelScale.PerMeter(32) means one metre in world
space occupies 32 pixels on screen.
Changing the PixelScale of a Camera2 is how zoom is implemented. More pixels per unit means the world appears larger (zoomed in); fewer pixels per unit means the world appears smaller (zoomed out).
// Common case: 32 pixels per metre
var scale = PixelScale.PerMeter(32);
// Non-metre world: 2 pixels per centimetre
var scale = Length.FromCentimeters(1).ToPixels(2);
// Smooth zoom in a game update loop
_currentScale = PixelScale.Lerp(_currentScale, _targetScale, deltaTime * smoothSpeed);
Constructors
PixelScale(Length, double)
Defines the relationship between world-space length and screen-space pixels, encoding both the scale factor and the world unit together.
public PixelScale(Length WorldLength, double Pixels)
Parameters
WorldLengthLengthThe world-space length that corresponds to
Pixelspixels. Its unit determines Unit.PixelsdoubleThe number of screen pixels that
WorldLengthoccupies.
Remarks
A PixelScale answers the question: "how many pixels does one world unit
occupy on screen?" For example, PixelScale.PerMeter(32) means one metre in world
space occupies 32 pixels on screen.
Changing the PixelScale of a Camera2 is how zoom is implemented. More pixels per unit means the world appears larger (zoomed in); fewer pixels per unit means the world appears smaller (zoomed out).
// Common case: 32 pixels per metre
var scale = PixelScale.PerMeter(32);
// Non-metre world: 2 pixels per centimetre
var scale = Length.FromCentimeters(1).ToPixels(2);
// Smooth zoom in a game update loop
_currentScale = PixelScale.Lerp(_currentScale, _targetScale, deltaTime * smoothSpeed);
Properties
Pixels
The number of screen pixels that WorldLength occupies.
public double Pixels { get; init; }
Property Value
PixelsPerUnit
The number of pixels per one Unit in world space.
For example, 32.0 means one Unit occupies 32 pixels.
public double PixelsPerUnit { get; }
Property Value
Unit
The world-space UnitsNet.Units.LengthUnit that PixelsPerUnit is measured in. Determined by the unit of WorldLength.
public LengthUnit Unit { get; }
Property Value
- LengthUnit
WorldLength
The world-space length that corresponds to Pixels pixels.
Its unit determines Unit.
public Length WorldLength { get; init; }
Property Value
- Length
Methods
Lerp(PixelScale, PixelScale, double)
Linearly interpolates between two PixelScale values.
public static PixelScale Lerp(PixelScale a, PixelScale b, double t)
Parameters
aPixelScaleThe start scale, returned when
tis0.bPixelScaleThe end scale, returned when
tis1.tdoubleThe interpolation factor.
0returnsa;1returnsb; values in between return a proportional blend.
Returns
- PixelScale
A new PixelScale whose PixelsPerUnit is linearly interpolated between
aandb. The result Unit matchesa's unit. Ifaandbuse different units,bis normalised toa's unit before interpolating.
Remarks
Calling Lerp(PixelScale, PixelScale, double) every frame with a small t produces
smooth, asymptotic zoom — fast at first, slowing as the scale approaches the target.
// In a game update loop — smooth zoom toward a target scale
_currentScale = PixelScale.Lerp(_currentScale, _targetScale, deltaTime * smoothSpeed);
camera = camera with { Scale = _currentScale };
PerMeter(double)
Creates a PixelScale where one metre in world space occupies
pixels pixels on screen.
public static PixelScale PerMeter(double pixels)
Parameters
pixelsdoubleThe number of pixels per metre.
Returns
- PixelScale
A PixelScale with Unit set to metres.
Examples
// 32 pixels represent one metre in world space
var scale = PixelScale.PerMeter(32);
ZoomBy(double)
Returns a new PixelScale with the pixel density multiplied by
factor. The WorldLength and Unit
are unchanged.
public PixelScale ZoomBy(double factor)
Parameters
factordoubleThe zoom multiplier. Values greater than
1.0zoom in (more pixels per unit); values between0and1.0zoom out (fewer pixels per unit).
Returns
- PixelScale
A new PixelScale with the adjusted pixel density.
Examples
var base = PixelScale.PerMeter(32);
var zoomedIn = base.ZoomBy(2.0); // 64 px/m
var zoomedOut = base.ZoomBy(0.5); // 16 px/m