Unity.Mathematics
& many useful methods !
Extension Library for Package name : com.ltmx.mathematics.mathx
#️⃣ Using Declaration
using static Unity.Mathematics.mathx;
♾ Chained Method Syntax
return anyVector.length().clamp(0, 10).cos().sq().cube().sum().cmul().rotate(anyQuaternion).clint().div(3.2f).rcp();
📢 HELP NEEDED !
- Documentation website generation with docfx ;) ... I can't seem to make it work and compile anything
📈 A Few Neat Features
- Matrix Truncation =>
float3x2(float4x4)
// Truncates the input matrix to the desired size... can also be written as :float4x4.f3x2()
- Burst Compiled Function Pointers
/// sets the value of x to f and returns f // Useful for modifying a variable in line.
public static T set<T>(this T f, out T x) { x = f; return f; }
// Example :
float3 x = new float3(1,1,1);
// here x is set before computing lengthsq()
var x = x.dim(4.2f).shuffle().set(out x).lengthsq() + x;
// we would have to write two lines instead
x = x.dim(4.2f).shuffle();
x = x.lengthsq() + x;
🌱 Contribute !
👉 Guidelines
- All methods should exist in the
Unity.Mathematics.mathx
class (To prevent multiple using declarations) - All Methods should follow a lower case syntax (shader like syntax)
- All methods names should be as short as possible while conserving their meaning or naming convension
- Everything must be Open Source
- Credits should (if the author can be found) figure above code snipets or in the file header (if reusing existing code)
- file mames should follow this convention : mathx..
Example : mathx.interpolation.common (common methods for interpolation) or mathx.logix.floatx (float type related logic functions)
- File names for base types such as
bounds
orbyte2
should only have their type as a title : bounds.cs // byte2.cs
- File names for base types such as
- Every method should be static (if applicable)
- Dependencies should not exist if applicable
- Code must be rewritten and optimized for Unity.Mathematics, compatibility checked
- Unification is key : if some functions are already available in math or Unity.Mathematics.math (sometimes under another name), use them !
- Documentation should be inherited from Unity.Mathematics.math methods for direct extension method translations
⬇️ Install
- Copy Git Package URL :
https://github.com/LTMX/Unity.mathx.git
- In Unity :
Window > Package Manager > Add Package From Git URL
- Download the package in releases,
- Unzip the file
- Unity :
Window > Package Manager > Add Package From Disk
- Select the
.json
file inside the unzipped package
🏛 Roadmap
- Fast Functions
- Constants (PI, HPI, EULER, TAU,
-
Mathf
functions missing fromUnity.Mathematics
- Random Extensions (
Random.range
and others) - Component based functions (
cmax
,cmin
,cmul
,cmaxAxis
,cminAxis
,sum
) - Signed Distance Functions
- Component based logic (
any
,all
,select
,approx
,odd
,even
,isnan
,anynan
) - Multidimentional Array data accessors =>
anyfloat4[,,].Get(anyInt3)
- Interpolation Functions (
InOutCubic
,smoothstep
,smoothstep11
,smoothstep9
, and others) -
smoothmin
,smoothmax
- Shorthands (
3D Directions
,2D Directions
, and others) - Data Construction (
append
,float2.xyzw()
, matrix construction, etc) - Data Conversion (
anyColortArray.tofloat4Array()
, and others) - Noise Functions
WIP
(Simplex
,Perlin
,Whorley
,Layered
,Voronoi
) - Documentation
80% Complete
- Job Helpers
WIP
- Burst Compiled Function Pointers
WIP 50%
- Function Iterators (prevents nested loops)
WIP 50%
- Hashing Functions
WIP 80%
- Vector Function Builders
WIP 50%
- Generic Jobs
WIP 50%
- Mesh Processing
WIP 0%
🎇 New Structs
struct bounds; // UnityEngine translation compatible with Unity.Mathematics (implicit cast to "UnityEngine.Bounds")
struct ray; // UnityEngine translation compatible with Unity.Mathematics (implicit cast to "UnityEngine.Ray")
struct color; // UnityEngine translation compatible with Unity.Mathematics (implicit cast to "UnityEngine.Color")
struct byte4; // Useful for Color32 to byte conversion, Useful for image file export (implicit cast to "UnityEngine.Color32") //For Unity.QOI
struct byte3; // For Unity.QOI
struct byte2;
struct byte1;
🎇 New Methods in 1.3.0
anyType.dim(otherType) => anyType* otherType // to add functionality missing from internal operator overloads // named dim to not confuse with mul()
anyType.greater(otherType) => anyType > otherType
anyType.less(otherType) => anyType < otherType
anyType.greatereq(otherType) => anyType >= otherType
anyType.lesseq(otherType) => anyType <= otherType
anyType.eq(otherType) => anyType == otherType
anyType.neq(otherType) => anyType != otherType
randseed(seed) => random float generated from a seed // internally : Random.Init(seed).Nextfloat()
randseed2(seed) => random float2 generated from a seed // internally : Random.Init(seed).Nextfloat()
randseed3(seed) => random float3 generated from a seed // internally : Random.Init(seed).Nextfloat()
randseed4(seed) => random float4 generated from a seed // internally : Random.Init(seed).Nextfloat()
anyType.append()
anyType.m2n1() => anyType* 2 - 1 // remaps anything from [0, 1] to [-1, 1]
quaternion generation functions
matrix generation functions
transformation functions
dot() // for int types
value.lerp(MatrixA, MatrixB) // functionality to interpolate any matrix
anyType.dim(otherType) => anyType * otherType // to add functionality for missing from operator overloads // 'dim' to not confuse with mul()
anyType.div(otherType) => anyType / otherType
anyType.add(otherType) => anyType + otherType
anyType.sub(otherType) => anyType - otherType
anyType.shuffle() // only for float2, float3 and float4
anyType.hash() // math.hash(anyType)
type generation methods float4(), float2(), float4x4(), etc
asuint() // new overloads
asbool() // new overloads
Method List (from 1.3.0... needs updating)
// most methods have at least a dozen overloads
asint asbool asfloat asdouble ascolor asfloat4 asfloat3 toVectorList
tofloatList toColorList tocolorList toColorArray tocolorArray tofloat4List
tofloat3List tofloat4Array tofloat3Array toVectorArray tofloatArray
toVectorIE tofloatIE cast List xx xxx xxxx append xy xyz xyzw
f4x4 f4x3 f4x2 f3x4 f3x3 f3x2 f2x4 f2x3 f2x2
b4x4 b4x3 b4x2 b3x4 b3x3 b3x2 b2x4 b2x3 b2x2
i4x4 i4x3 i4x2 i3x4 i3x3 i3x2 i2x4 i2x3 i2x2
u4x4 u4x3 u4x2 u3x4 u3x3 u3x2 u2x4 u2x3 u2x2
fcos veryFastCos ultraFastCos CosLoop SinLoop sfcos fsqrt fdistance flength
log2int fastmodinv mod frac fexp sfastsine sfastcosine fastsine fastcosine
smootherstep smoothstepcos eerp uneerp smoothstep unlerp lerp lerpAngle
remap step smin smax smax_exp smax_exp2 smax_expOP smin_exp smin_pow
smin_root smin_polynomial smin_quadratic smin_cubic smin_factor
smin_cubic_factor smin_N_factor easeInSine easeOutSine easeInOutSine
easeInQuad easeOutQuad easeInOutQuad easeInCubic easeOutCubic easeInOutCubic
easeInQuart easeOutQuart easeInOutQuart easeInQuint easeOutQuint easeInOutQuint
easeInExpo easeOutExpo easeInOutExpo easeInCirc easeOutCirc easeInOutCirc
easeInBack easeOutBack easeInOutBack easeInElastic easeOutElastic
easeInOutElastic easeInBounce easeOutBounce easeInOutBounce smooth
smoothstepD smooth5 smooth5D smooth7 smooth7D smooth9 smooth9D smooth11
smooth11D smoothD smoother7D smoothdamp anglerad angledeg fastangle
straightsignedangle preciseangle signedangle fastatan2 rotate rotateAxisAngle
rotateRad rotateDeg quaternion sign abs sum cmul inv neg rcp pow sq
cube pow4 pow5 exp nexp exp2 exp10 ln log2 log10 any all select approx
odd even get anynan isnan isgreaterthan init rand varyrand addrand
randomint randomInSphere randomInCircle randomDir3D randomDir2D
randomrotation round rint clamp min max ceil clint floor flint saturate
npsaturate p n under1 cmax cmin cmaxAxis cminAxis sin cos tan sec cot
csc asin acos atan atan2 acot asec acsc sin2 cos2 tan2 sec2 deg rad
cossin sincos sinh cosh tanh sech coth csch acosh asinh atanh acoth
asech acsch norm normsafe distance distancesq cross orthonormalize
length lengthsq sqrt cbrt rcbrt rsqrt dot reflect refract project
projectsafe repeat pingpong SampleParabola matrix inverse float3x4
rotation translation transform mul sub add mult div transpose
openSimplex2_ImproveXY openSimplex2SDerivatives_ImproveXY byte2
hash hashwide byte3 byte4 color shuffle gammatolinear lineartogama
movetowards Erf Erfc ErfInv ErfcInv GammaLn Gamma DiGamma DiGammaInv
📜 LICENSING
This project is licensed under the MIT License (License)