Class MathUtils
Namespace: ValveResourceFormat.Utils
Assembly: ValveResourceFormat.dll
Shared scalar, vector and integer math helpers.
public static class MathUtilsInheritance
Fields
BitsPerWord
Number of bits held by one word of a bitfield.
public const int BitsPerWord = 32Field 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
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
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
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
Clamp<T>(T, T, T)
Clamps a value between two bounds given in either order, unlike 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
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
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
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
Fract(float)
Returns the fractional part of a value (x - floor(x)).
public static float Fract(float x)Parameters
x float
Returns
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
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
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
Interpolated angle in radians.
Max(float, float, float)
The largest of the given values, compared with .
public static float Max(float a, float b, float c)Parameters
a float
b float
c float
Returns
Max(float, float, float, float)
The largest of the given values, compared with .
public static float Max(float a, float b, float c, float d)Parameters
a float
b float
c float
d float
Returns
MaxAxisScale(Matrix4x4)
The largest per-axis scale baked into a transform.
public static float MaxAxisScale(this Matrix4x4 m)Parameters
Returns
MaxComponent(Vector3)
The largest of a vector's three components.
public static float MaxComponent(this Vector3 v)Parameters
v Vector3
Returns
Min(float, float, float)
The smallest of the given values, compared with .
public static float Min(float a, float b, float c)Parameters
a float
b float
c float
Returns
Min(float, float, float, float)
The smallest of the given values, compared with .
public static float Min(float a, float b, float c, float d)Parameters
a float
b float
c float
d float
Returns
MinComponent(Vector3)
The smallest of a vector's three components.
public static float MinComponent(this Vector3 v)Parameters
v Vector3
Returns
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
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
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
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
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. Mirrors Source's RemapValClamped.
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
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
SafeNormalize(Vector3)
Scales value to unit length, returning 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
SetBit(Span<uint>, int)
Sets bit index of a packed bitfield.
public static void SetBit(Span<uint> bits, int index)Parameters
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
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
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

