Unity3D's Mathf port
This is js port of Unity3D's Mathf class. It has been achieved by reverse engineering the work of original class and all the ported methods work exactly the same (except PerlinNoise
method, cuz of different random realization and seed value). You can use this library in game development or in any other cases where you find existing methods useful.
Originally it is node
plugin serving by npm
and using available ES6 features, but you can easily adopt it to work in browser with help of Babel or another ES6 transpiler.
Useless within javascript methods:
CeilToInt
FloorToInt
RoundToInt
Built-in js native methods and constants:
Infinity
PI
Abs
Acos
Asin
Atan
Atan2
Ceil
Cos
Exp
Floor
Log
Log10
Max
Min
Pow
Sin
Sqrt
Tan
Note: Unity3D's
Mathf.Round
has different behavior then native javascript'sMath.round
, so this method has own implementationMathf.round
This port includes almost (read below why) all non default (for native js
) methods and properties available in the Unity3D's Mathf
class. Namely:
Degrees-to-radians conversion constant
A tiny floating point value
A representation of negative infinity
Radians-to-degrees conversion constant
Compares two floating point values if they are similar
Clamps a value between a minimum float and maximum float value
Clamps value between 0 and 1 and returns value
Returns the closest power of two value
Calculates the shortest difference between two given angles given in degrees
Converts the given value from gamma (sRGB) to linear color space
Calculates the linear parameter t that produces the interpolant value within the range [a, b]
Returns true if the value is power of two
Linearly interpolates between a and b by t
Same as Lerp but makes sure the values interpolate correctly when they wrap around 360 degrees
Linearly interpolates between a and b by t
Converts the given value from linear to gamma (sRGB) color space
Moves a value current towards target
Returns the next power of two value
Generate 2D Perlin noise
PingPongs the value t, so that it is never larger than length and never smaller than 0
Loops the value t, so that it is never larger than length and never smaller than 0
Returns f rounded to the nearest integer
Returns the sign of f
I didn't find proper implementations for them, so if you know it welcome to PR
Same as MoveTowards but makes sure the values interpolate correctly when they wrap around 360 degrees
Gradually changes a value towards a desired goal over time
Gradually changes an angle given in degrees towards a desired goal angle over time
Interpolates between min and max with smoothing at the limits
Same as closestPowerOfTwo
, but deals well with big numbers. Use it if you really need to work with big numbers
As simple as usual node
module
-
Install it:
npm install mathf
-
Require it:
let Mathf = require('mathf');
let rad = 2;
console.log(rad + ' radians are equal to ' + (rad * Mathf.Rad2Deg) + ' degrees');
// 2 radians are equal to 114.59155902616465 degrees