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 metreScale = PixelScale.PerMeter(32);
// Non-metre world: 2 pixels per centimetre
var cmScale = Length.FromCentimeters(1).ToPixels(2);
// Smooth zoom in a game update loop
var currentScale = PixelScale.PerMeter(32);
var targetScale = PixelScale.PerMeter(64);
currentScale = PixelScale.Lerp(currentScale, targetScale, 0.016 * 5.0);
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 metreScale = PixelScale.PerMeter(32);
// Non-metre world: 2 pixels per centimetre
var cmScale = Length.FromCentimeters(1).ToPixels(2);
// Smooth zoom in a game update loop
var currentScale = PixelScale.PerMeter(32);
var targetScale = PixelScale.PerMeter(64);
currentScale = PixelScale.Lerp(currentScale, targetScale, 0.016 * 5.0);
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
Clamp(double, double)
Returns a new PixelScale with PixelsPerUnit clamped to
[.
WorldLength and Unit are unchanged.minPixelsPerUnit, maxPixelsPerUnit]
public PixelScale Clamp(double minPixelsPerUnit, double maxPixelsPerUnit)
Parameters
minPixelsPerUnitdoubleThe minimum allowed pixels per world unit.
maxPixelsPerUnitdoubleThe maximum allowed pixels per world unit.
Returns
- PixelScale
The same PixelScale if already within range; otherwise a new instance with PixelsPerUnit set to the nearest bound.
Examples
var scale = PixelScale.PerMeter(128);
var clamped = scale.Clamp(16, 64); // ≈ 64 px/m
Remarks
Use Clamp(double, double) to enforce zoom limits on a Camera2.
For a camera-level convenience, see Camera2.ClampZoom.
Clamp(PixelScale, PixelScale)
Returns a new PixelScale with PixelsPerUnit clamped to
[, where both bounds are expressed
as PixelScale values and normalised to the receiver's Unit
before comparison. WorldLength and Unit are unchanged.min, max]
public PixelScale Clamp(PixelScale min, PixelScale max)
Parameters
minPixelScaleThe minimum zoom level. Normalised to the receiver's unit for comparison.
maxPixelScaleThe maximum zoom level. Normalised to the receiver's unit for comparison.
Returns
- PixelScale
The same PixelScale if already within range; otherwise a new instance clamped to the nearest bound.
Examples
var scale = PixelScale.PerMeter(128);
var clamped = scale.Clamp(PixelScale.PerMeter(16), PixelScale.PerMeter(64)); // ≈ 64 px/m
Remarks
Use the typed overload when bounds come from game configuration expressed as full
PixelScale values (e.g. PixelScale.PerMeter(16)).
For raw pixel-per-unit doubles, use Clamp(Double, Double).
Lerp(PixelScale, PixelScale, double)
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.
// Smooth zoom toward a target scale — call each update frame:
var currentScale = PixelScale.PerMeter(32);
var targetScale = PixelScale.PerMeter(64);
currentScale = PixelScale.Lerp(currentScale, targetScale, 0.016 * 5.0);
Lerp(PixelScale, PixelScale, Ratio)
Linearly interpolates between two PixelScale values using a typed UnitsNet.Ratio interpolation parameter.
public static PixelScale Lerp(PixelScale a, PixelScale b, Ratio t)
Parameters
aPixelScaleThe start scale, returned when
tis zero.bPixelScaleThe end scale, returned when
tis one.tRatioThe interpolation factor as a UnitsNet.Ratio. A decimal-fraction value of
0returnsa;1returnsb.
Returns
- PixelScale
A new PixelScale whose PixelsPerUnit is linearly interpolated between
aandb.
Examples
var currentScale = PixelScale.PerMeter(32);
var targetScale = PixelScale.PerMeter(64);
currentScale = PixelScale.Lerp(currentScale, targetScale, Ratio.FromDecimalFractions(0.5));
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);
PerUnit(LengthUnit, double)
Creates a PixelScale where one unit in world space
occupies pixels pixels on screen.
public static PixelScale PerUnit(LengthUnit unit, double pixels)
Parameters
unitLengthUnitThe world-space length unit.
pixelsdoubleThe number of pixels per one
unit.
Returns
- PixelScale
A PixelScale with Unit set to
unit.
Examples
// 2 pixels represent one centimetre in world space
var cmScale = PixelScale.PerUnit(LengthUnit.Centimeter, 2);
Remarks
Use PerMeter(double) for the common metre case. Use PerUnit when the
world coordinate system is defined in a different unit (e.g. centimetres or tiles).
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 original = PixelScale.PerMeter(32);
var zoomedIn = original.ZoomBy(2.0); // 64 px/m
var zoomedOut = original.ZoomBy(0.5); // 16 px/m