Class MathUtils
Namespace: ValveResourceFormat.Utils
Assembly: ValveResourceFormat.dll
Shared scalar, vector and integer math helpers.
public static class MathUtilsInheritance
object ← MathUtils
Fields
BitsPerWord
Number of bits held by one word of a uint bitfield.
public const int BitsPerWord = 32Field Value
int
LuminanceWeights
The BT.709-1 weights of the red, green and blue channels in a color's luminance, as the game shaders use them.
public static readonly Vector3 LuminanceWeightsField Value
Rec709LuminanceWeights
The Rec. 709 weights of the red, green and blue channels in a color's luminance, as the engine uses them for cubemap normalization.
public static readonly Vector3 Rec709LuminanceWeightsField Value
Methods
AlignUp(int, int)
Rounds value up to a multiple of alignment, which must be a power of two.
public static int AlignUp(int value, int alignment)Parameters
value int
alignment int
Returns
int
AngleBetween(Vector3, Vector3)
The angle in radians between two unit vectors.
public static float AngleBetween(Vector3 a, Vector3 b)Parameters
a Vector3
b Vector3
Returns
float
AngleBetween(Quaternion, Quaternion)
The angle in radians between two rotations, the shorter way round.
public static float AngleBetween(Quaternion a, Quaternion b)Parameters
Returns
float
Approach(float, float, float)
Moves value toward target by at most step, without overshooting.
public static float Approach(float value, float target, float step)Parameters
value float
target float
step float
Returns
float
AxisScale(Matrix4x4, int)
The length of one of a transform's basis vectors: how much it scales along that axis. axis is 0 for X, 1 for Y, 2 for Z.
public static float AxisScale(this Matrix4x4 m, int axis)Parameters
axis int
Returns
float
Clamp<T>(T, T, T)
Clamps a value between two bounds given in either order, unlike Math.Clamp which throws when min is greater than max.
public static T Clamp<T>(T value, T min, T max) where T : INumber<T>Parameters
value T
min T
max T
Returns
T
Type Parameters
T
ClampLength(Vector3, float)
Scales value down to a non-negative maxLength when it is longer, and returns it unchanged otherwise.
public static Vector3 ClampLength(Vector3 value, float maxLength)Parameters
value Vector3
maxLength float
Returns
CreateBasis(Vector3, Vector3, Vector3)
The rotation matrix with x, y and z as its rows, which carries +X, +Y and +Z onto those axes.
public static Matrix4x4 CreateBasis(Vector3 x, Vector3 y, Vector3 z)Parameters
x Vector3
y Vector3
z Vector3
Returns
CubicBezier(Vector3, Vector3, Vector3, Vector3, float)
Evaluates a cubic Bezier curve at t in [0, 1].
public static Vector3 CubicBezier(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)Parameters
p0 Vector3
p1 Vector3
p2 Vector3
p3 Vector3
t float
Returns
DecibelsToLinear(float)
Converts a decibel value to a linear amplitude multiplier (0 dB is 1.0, -6 dB is roughly half).
public static float DecibelsToLinear(float decibels)Parameters
decibels float
Returns
float
DiagonalizeSymmetric(Matrix4x4, out Vector3)
Diagonalizes a symmetric 3x3 matrix with Jacobi rotations accumulated in a quaternion. Each iteration rotates away the largest off-diagonal entry, for at most 24 iterations.
public static Quaternion DiagonalizeSymmetric(Matrix4x4 matrix, out Vector3 eigenvalues)Parameters
matrix Matrix4x4
The symmetric matrix in its upper-left 3x3 block.
eigenvalues Vector3
The diagonal of the rotated matrix, unsorted.
Returns
The rotation whose matrix (Matrix4x4.CreateFromQuaternion) has the eigenvectors as its rows, in the order of eigenvalues.
DistanceSquaredToSegment(Vector3, Vector3, Vector3)
Returns the squared distance from point to the segment from start to end.
public static float DistanceSquaredToSegment(Vector3 point, Vector3 start, Vector3 end)Parameters
point Vector3
start Vector3
end Vector3
Returns
float
DivideRoundUp(int, int)
Divides two positive integers, rounding the result up.
public static int DivideRoundUp(int value, int divisor)Parameters
value int
divisor int
Returns
int
ExponentialSmoothing(float, float)
The lerp factor that closes a fixed fraction of the distance to a target every timeConstant seconds, no matter how deltaTime is chopped up.
public static float ExponentialSmoothing(float deltaTime, float timeConstant)Parameters
deltaTime float
timeConstant float
Returns
float
Fract(float)
Returns the fractional part of a value (x - floor(x)).
public static float Fract(float x)Parameters
x float
Returns
float
GetBit(ReadOnlySpan<uint>, int)
Gets whether bit index of a packed bitfield is set.
public static bool GetBit(ReadOnlySpan<uint> bits, int index)Parameters
bits ReadOnlySpan<uint>
The bitfield, 32 bits per word.
index int
Index of the bit, counted across the whole field.
Returns
bool
GetBit(ReadOnlySpan<byte>, int)
Gets whether bit index of a packed bitfield is set.
public static bool GetBit(ReadOnlySpan<byte> bits, int index)Parameters
bits ReadOnlySpan<byte>
The bitfield, 8 bits per byte.
index int
Index of the bit, counted across the whole field.
Returns
bool
Lerp(float, float, float)
Interpolates as from + (to - from) * amount. Unlike float.Lerp, which weights the two ends, it gives from exactly when both ends are equal, so a collapsed range keeps its value through a later truncation.
public static float Lerp(float from, float to, float amount)Parameters
from float
to float
amount float
Returns
float
Lerp(Vector3, Vector3, float)
Interpolates as from + (to - from) * amount. Unlike float.Lerp, which weights the two ends, it gives from exactly when both ends are equal, so a collapsed range keeps its value through a later truncation.
public static Vector3 Lerp(Vector3 from, Vector3 to, float amount)Parameters
from Vector3
to Vector3
amount float
Returns
Lerp(Vector3, Vector3, Vector3)
Interpolates as from + (to - from) * amount. Unlike float.Lerp, which weights the two ends, it gives from exactly when both ends are equal, so a collapsed range keeps its value through a later truncation.
public static Vector3 Lerp(Vector3 from, Vector3 to, Vector3 amount)Parameters
from Vector3
to Vector3
amount Vector3
Returns
LerpAngle(float, float, float)
Linearly interpolates between two angles in radians, taking the shortest path around the circle.
public static float LerpAngle(float from, float to, float amount)Parameters
from float
Start angle in radians.
to float
End angle in radians.
amount float
Interpolation weight (0.0 = from, 1.0 = to).
Returns
float
Interpolated angle in radians.
Luminance(Vector3)
BT.709-1 luminance of a linear RGB color.
public static float Luminance(Vector3 color)Parameters
color Vector3
Returns
float
Max(float, float, float)
The largest of the given values, compared with MathF.Max.
public static float Max(float a, float b, float c)Parameters
a float
b float
c float
Returns
float
Max(float, float, float, float)
The largest of the given values, compared with MathF.Max.
public static float Max(float a, float b, float c, float d)Parameters
a float
b float
c float
d float
Returns
float
MaxAxisScale(Matrix4x4)
The largest per-axis scale baked into a transform.
public static float MaxAxisScale(this Matrix4x4 m)Parameters
Returns
float
MaxComponent(Vector3)
The largest of a vector's three components.
public static float MaxComponent(this Vector3 v)Parameters
v Vector3
Returns
float
Min(float, float, float)
The smallest of the given values, compared with MathF.Min.
public static float Min(float a, float b, float c)Parameters
a float
b float
c float
Returns
float
Min(float, float, float, float)
The smallest of the given values, compared with MathF.Min.
public static float Min(float a, float b, float c, float d)Parameters
a float
b float
c float
d float
Returns
float
MinComponent(Vector3)
The smallest of a vector's three components.
public static float MinComponent(this Vector3 v)Parameters
v Vector3
Returns
float
MipLevelSize(int, int)
The size of a mip level: size halved level times, never less than 1.
public static int MipLevelSize(int size, int level)Parameters
size int
level int
Returns
int
Perpendicular(Vector3)
A unit vector perpendicular to the unit vector direction, picked by a fixed rule, so the same direction always gives the same perpendicular: +X for +Z and +Y, -Z for +X.
public static Vector3 Perpendicular(Vector3 direction)Parameters
direction Vector3
Returns
ProjectOntoPlane(Vector3, Vector3)
Removes from value its component along unitNormal, leaving the part that lies in the plane the normal describes.
public static Vector3 ProjectOntoPlane(Vector3 value, Vector3 unitNormal)Parameters
value Vector3
unitNormal Vector3
Returns
RadicalInverse(int, int)
The radical inverse of a non-negative index in base radix: its digits mirrored about the radix point, giving a value in 0, 1). Successive indices in bases 2, 3 and 5 give the coordinates of a Halton sequence.
public static float RadicalInverse(int index, int radix)Parameters
index [int
radix int
Returns
float
Remap(float, float, float)
Remaps a value from input range to 0-1.
public static float Remap(float x, float inputMin, float inputMax)Parameters
x float
Value to remap.
inputMin float
Input range minimum.
inputMax float
Input range maximum.
Returns
float
Value remapped to 0-1 range.
RemapRange(float, float, float, float, float)
Remaps a value from one range to another.
public static float RemapRange(float x, float inputMin, float inputMax, float outputMin, float outputMax)Parameters
x float
Value to remap.
inputMin float
Input range minimum.
inputMax float
Input range maximum.
outputMin float
Output range minimum.
outputMax float
Output range maximum.
Returns
float
Value remapped to output range.
RemapValClamped(float, float, float, float, float)
Remaps a value from one range to another, holding the output at the range ends for inputs outside the input range.
public static float RemapValClamped(float x, float inputMin, float inputMax, float outputMin, float outputMax)Parameters
x float
Value to remap.
inputMin float
Input range minimum.
inputMax float
Input range maximum.
outputMin float
Output range minimum.
outputMax float
Output range maximum.
Returns
float
Value remapped to the output range, clamped to it.
SafeAcos(float)
Arc cosine that clamps its input to [-1, 1] first, so a cosine that drifted just outside the range through rounding gives an angle rather than NaN.
public static float SafeAcos(float cosine)Parameters
cosine float
Returns
float
SafeNormalize(Vector3)
Scales value to unit length, returning Vector3.Zero for a vector with no length rather than the NaN a plain divide would give.
public static Vector3 SafeNormalize(Vector3 value)Parameters
value Vector3
Returns
SafeNormalize(Vector3, Vector3, float)
Scales value to unit length, returning fallback when its squared length does not exceed minimumLengthSquared.
public static Vector3 SafeNormalize(Vector3 value, Vector3 fallback, float minimumLengthSquared = 0)Parameters
value Vector3
fallback Vector3
minimumLengthSquared float
Returns
Saturate(float)
Clamps a value to [0, 1].
public static float Saturate(float x)Parameters
x float
Returns
float
SetBit(Span<uint>, int)
Sets bit index of a packed bitfield.
public static void SetBit(Span<uint> bits, int index)Parameters
bits Span<uint>
The bitfield, 32 bits per word.
index int
Index of the bit, counted across the whole field.
Smoothstep(float, float, float)
GLSL's smoothstep: 0 below edge0, 1 above edge1, and a Hermite ease between them. Edges may be given in either order, so a descending pair produces a falling curve.
public static float Smoothstep(float edge0, float edge1, float x)Parameters
edge0 float
Value at which the result reaches 0.
edge1 float
Value at which the result reaches 1.
x float
Value to interpolate.
Returns
float
The eased value in the 0-1 range.
ToPerceptualVolume(float)
Maps a linear 0-1 value onto an exponential curve approximating perceptual loudness, so the middle of a volume control (or a linear distance falloff) does not sound louder than it should.
public static float ToPerceptualVolume(float linear)Parameters
linear float
Returns
float
TriangleArea(Vector3, Vector3, Vector3)
The area of the triangle with corners a, b and c.
public static float TriangleArea(Vector3 a, Vector3 b, Vector3 c)Parameters
a Vector3
b Vector3
c Vector3
Returns
float
TriangleCross(Vector3, Vector3, Vector3)
The cross product of a triangle's two edges leaving a: its face normal, with a length of twice the triangle's area.
public static Vector3 TriangleCross(Vector3 a, Vector3 b, Vector3 c)Parameters
a Vector3
b Vector3
c Vector3
Returns
Wrap(float, float, float)
Wraps a value into the half-open range lowBounds, highBounds).
public static float Wrap(float x, float lowBounds, float highBounds)Parameters
x [float
lowBounds float
highBounds float
Returns
float
WrapIndex(int, int)
Wraps index into 0, count), so a negative index counts back from the end.
public static int WrapIndex(int index, int count)Parameters
index [int
count int
Returns
int

