| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901 |
- using System.Runtime.CompilerServices;
- using static Unity.Mathematics.math;
- namespace Unity.Mathematics
- {
- public partial struct float2x2
- {
- /// <summary>Returns a float2x2 matrix representing a counter-clockwise rotation of angle degrees.</summary>
- public static float2x2 Rotate(float angle)
- {
- float s, c;
- sincos(angle, out s, out c);
- return float2x2(c, -s,
- s, c);
- }
- /// <summary>Returns a float2x2 matrix representing a uniform scaling of both axes by s.</summary>
- public static float2x2 Scale(float s)
- {
- return float2x2(s, 0.0f,
- 0.0f, s);
- }
- /// <summary>Returns a float2x2 matrix representing a non-uniform axis scaling by x and y.</summary>
- public static float2x2 Scale(float x, float y)
- {
- return float2x2(x, 0.0f,
- 0.0f, y);
- }
- /// <summary>Returns a float2x2 matrix representing a non-uniform axis scaling by the components of the float2 vector v.</summary>
- public static float2x2 Scale(float2 v)
- {
- return Scale(v.x, v.y);
- }
- }
- public partial struct float3x3
- {
- /// <summary>Constructs a float3x3 matrix from a unit quaternion.</summary>
- public float3x3(quaternion q)
- {
- float4 v = q.value;
- float4 v2 = v + v;
- uint3 npn = uint3(0x80000000, 0x00000000, 0x80000000);
- uint3 nnp = uint3(0x80000000, 0x80000000, 0x00000000);
- uint3 pnn = uint3(0x00000000, 0x80000000, 0x80000000);
- c0 = v2.y * asfloat(asuint(v.yxw) ^ npn) - v2.z * asfloat(asuint(v.zwx) ^ pnn) + float3(1, 0, 0);
- c1 = v2.z * asfloat(asuint(v.wzy) ^ nnp) - v2.x * asfloat(asuint(v.yxw) ^ npn) + float3(0, 1, 0);
- c2 = v2.x * asfloat(asuint(v.zwx) ^ pnn) - v2.y * asfloat(asuint(v.wzy) ^ nnp) + float3(0, 0, 1);
- }
- /// <summary>
- /// Returns a float3x3 matrix representing a rotation around a unit axis by an angle in radians.
- /// The rotation direction is clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- public static float3x3 AxisAngle(float3 axis, float angle)
- {
- float sina, cosa;
- math.sincos(angle, out sina, out cosa);
- float3 u = axis;
- float3 u_yzx = u.yzx;
- float3 u_zxy = u.zxy;
- float3 u_inv_cosa = u - u * cosa; // u * (1.0f - cosa);
- float4 t = float4(u * sina, cosa);
- uint3 ppn = uint3(0x00000000, 0x00000000, 0x80000000);
- uint3 npp = uint3(0x80000000, 0x00000000, 0x00000000);
- uint3 pnp = uint3(0x00000000, 0x80000000, 0x00000000);
- return float3x3(
- u.x * u_inv_cosa + asfloat(asuint(t.wzy) ^ ppn),
- u.y * u_inv_cosa + asfloat(asuint(t.zwx) ^ npp),
- u.z * u_inv_cosa + asfloat(asuint(t.yxw) ^ pnp)
- );
- /*
- return float3x3(
- cosa + u.x * u.x * (1.0f - cosa), u.y * u.x * (1.0f - cosa) - u.z * sina, u.z * u.x * (1.0f - cosa) + u.y * sina,
- u.x * u.y * (1.0f - cosa) + u.z * sina, cosa + u.y * u.y * (1.0f - cosa), u.y * u.z * (1.0f - cosa) - u.x * sina,
- u.x * u.z * (1.0f - cosa) - u.y * sina, u.y * u.z * (1.0f - cosa) + u.x * sina, cosa + u.z * u.z * (1.0f - cosa)
- );
- */
- }
- /// <summary>
- /// Returns a float3x3 rotation matrix constructed by first performing a rotation around the x-axis, then the y-axis and finally the z-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
- public static float3x3 EulerXYZ(float3 xyz)
- {
- // return mul(rotateZ(xyz.z), mul(rotateY(xyz.y), rotateX(xyz.x)));
- float3 s, c;
- sincos(xyz, out s, out c);
- return float3x3(
- c.y * c.z, c.z * s.x * s.y - c.x * s.z, c.x * c.z * s.y + s.x * s.z,
- c.y * s.z, c.x * c.z + s.x * s.y * s.z, c.x * s.y * s.z - c.z * s.x,
- -s.y, c.y * s.x, c.x * c.y
- );
- }
- /// <summary>
- /// Returns a float3x3 rotation matrix constructed by first performing a rotation around the x-axis, then the z-axis and finally the y-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
- public static float3x3 EulerXZY(float3 xyz)
- {
- // return mul(rotateY(xyz.y), mul(rotateZ(xyz.z), rotateX(xyz.x))); }
- float3 s, c;
- sincos(xyz, out s, out c);
- return float3x3(
- c.y * c.z, s.x * s.y - c.x * c.y * s.z, c.x * s.y + c.y * s.x * s.z,
- s.z, c.x * c.z, -c.z * s.x,
- -c.z * s.y, c.y * s.x + c.x * s.y * s.z, c.x * c.y - s.x * s.y * s.z
- );
- }
- /// <summary>
- /// Returns a float3x3 rotation matrix constructed by first performing a rotation around the y-axis, then the x-axis and finally the z-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
- public static float3x3 EulerYXZ(float3 xyz)
- {
- // return mul(rotateZ(xyz.z), mul(rotateX(xyz.x), rotateY(xyz.y)));
- float3 s, c;
- sincos(xyz, out s, out c);
- return float3x3(
- c.y * c.z - s.x * s.y * s.z, -c.x * s.z, c.z * s.y + c.y * s.x * s.z,
- c.z * s.x * s.y + c.y * s.z, c.x * c.z, s.y * s.z - c.y * c.z * s.x,
- -c.x * s.y, s.x, c.x * c.y
- );
- }
- /// <summary>
- /// Returns a float3x3 rotation matrix constructed by first performing a rotation around the y-axis, then the z-axis and finally the x-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
- public static float3x3 EulerYZX(float3 xyz)
- {
- // return mul(rotateX(xyz.x), mul(rotateZ(xyz.z), rotateY(xyz.y)));
- float3 s, c;
- sincos(xyz, out s, out c);
- return float3x3(
- c.y * c.z, -s.z, c.z * s.y,
- s.x * s.y + c.x * c.y * s.z, c.x * c.z, c.x * s.y * s.z - c.y * s.x,
- c.y * s.x * s.z - c.x * s.y, c.z * s.x, c.x * c.y + s.x * s.y * s.z
- );
- }
- /// <summary>
- /// Returns a float3x3 rotation matrix constructed by first performing a rotation around the z-axis, then the x-axis and finally the y-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// This is the default order rotation order in Unity.
- /// </summary>
- /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
- public static float3x3 EulerZXY(float3 xyz)
- {
- // return mul(rotateY(xyz.y), mul(rotateX(xyz.x), rotateZ(xyz.z)));
- float3 s, c;
- sincos(xyz, out s, out c);
- return float3x3(
- c.y * c.z + s.x * s.y * s.z, c.z * s.x * s.y - c.y * s.z, c.x * s.y,
- c.x * s.z, c.x * c.z, -s.x,
- c.y * s.x * s.z - c.z * s.y, c.y * c.z * s.x + s.y * s.z, c.x * c.y
- );
- }
- /// <summary>
- /// Returns a float3x3 rotation matrix constructed by first performing a rotation around the z-axis, then the y-axis and finally the x-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
- public static float3x3 EulerZYX(float3 xyz)
- {
- // return mul(rotateX(xyz.x), mul(rotateY(xyz.y), rotateZ(xyz.z)));
- float3 s, c;
- sincos(xyz, out s, out c);
- return float3x3(
- c.y * c.z, -c.y * s.z, s.y,
- c.z * s.x * s.y + c.x * s.z, c.x * c.z - s.x * s.y * s.z, -c.y * s.x,
- s.x * s.z - c.x * c.z * s.y, c.z * s.x + c.x * s.y * s.z, c.x * c.y
- );
- }
- /// <summary>
- /// Returns a float3x3 rotation matrix constructed by first performing a rotation around the x-axis, then the y-axis and finally the z-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="x">The rotation angle around the x-axis in radians.</param>
- /// <param name="y">The rotation angle around the y-axis in radians.</param>
- /// <param name="z">The rotation angle around the z-axis in radians.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float3x3 EulerXYZ(float x, float y, float z) { return EulerXYZ(float3(x, y, z)); }
- /// <summary>
- /// Returns a float3x3 rotation matrix constructed by first performing a rotation around the x-axis, then the z-axis and finally the y-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="x">The rotation angle around the x-axis in radians.</param>
- /// <param name="y">The rotation angle around the y-axis in radians.</param>
- /// <param name="z">The rotation angle around the z-axis in radians.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float3x3 EulerXZY(float x, float y, float z) { return EulerXZY(float3(x, y, z)); }
- /// <summary>
- /// Returns a float3x3 rotation matrix constructed by first performing a rotation around the y-axis, then the x-axis and finally the z-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="x">The rotation angle around the x-axis in radians.</param>
- /// <param name="y">The rotation angle around the y-axis in radians.</param>
- /// <param name="z">The rotation angle around the z-axis in radians.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float3x3 EulerYXZ(float x, float y, float z) { return EulerYXZ(float3(x, y, z)); }
- /// <summary>
- /// Returns a float3x3 rotation matrix constructed by first performing a rotation around the y-axis, then the z-axis and finally the x-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="x">The rotation angle around the x-axis in radians.</param>
- /// <param name="y">The rotation angle around the y-axis in radians.</param>
- /// <param name="z">The rotation angle around the z-axis in radians.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float3x3 EulerYZX(float x, float y, float z) { return EulerYZX(float3(x, y, z)); }
- /// <summary>
- /// Returns a float3x3 rotation matrix constructed by first performing a rotation around the z-axis, then the x-axis and finally the y-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// This is the default order rotation order in Unity.
- /// </summary>
- /// <param name="x">The rotation angle around the x-axis in radians.</param>
- /// <param name="y">The rotation angle around the y-axis in radians.</param>
- /// <param name="z">The rotation angle around the z-axis in radians.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float3x3 EulerZXY(float x, float y, float z) { return EulerZXY(float3(x, y, z)); }
- /// <summary>
- /// Returns a float3x3 rotation matrix constructed by first performing a rotation around the z-axis, then the y-axis and finally the x-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="x">The rotation angle around the x-axis in radians.</param>
- /// <param name="y">The rotation angle around the y-axis in radians.</param>
- /// <param name="z">The rotation angle around the z-axis in radians.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float3x3 EulerZYX(float x, float y, float z) { return EulerZYX(float3(x, y, z)); }
- /// <summary>
- /// Returns a float3x3 rotation matrix constructed by first performing 3 rotations around the principal axes in a given order.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// When the rotation order is known at compile time, it is recommended for performance reasons to use specific
- /// Euler rotation constructors such as EulerZXY(...).
- /// </summary>
- /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
- /// <param name="order">The order in which the rotations are applied.</param>
- public static float3x3 Euler(float3 xyz, RotationOrder order = RotationOrder.Default)
- {
- switch (order)
- {
- case RotationOrder.XYZ:
- return EulerXYZ(xyz);
- case RotationOrder.XZY:
- return EulerXZY(xyz);
- case RotationOrder.YXZ:
- return EulerYXZ(xyz);
- case RotationOrder.YZX:
- return EulerYZX(xyz);
- case RotationOrder.ZXY:
- return EulerZXY(xyz);
- case RotationOrder.ZYX:
- return EulerZYX(xyz);
- default:
- return float3x3.identity;
- }
- }
- /// <summary>
- /// Returns a float3x3 rotation matrix constructed by first performing 3 rotations around the principal axes in a given order.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// When the rotation order is known at compile time, it is recommended for performance reasons to use specific
- /// Euler rotation constructors such as EulerZXY(...).
- /// </summary>
- /// <param name="x">The rotation angle around the x-axis in radians.</param>
- /// <param name="y">The rotation angle around the y-axis in radians.</param>
- /// <param name="z">The rotation angle around the z-axis in radians.</param>
- /// <param name="order">The order in which the rotations are applied.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float3x3 Euler(float x, float y, float z, RotationOrder order = RotationOrder.Default)
- {
- return Euler(float3(x, y, z), order);
- }
- /// <summary>Returns a float4x4 matrix that rotates around the x-axis by a given number of radians.</summary>
- /// <param name="angle">The clockwise rotation angle when looking along the x-axis towards the origin in radians.</param>
- public static float3x3 RotateX(float angle)
- {
- // {{1, 0, 0}, {0, c_0, -s_0}, {0, s_0, c_0}}
- float s, c;
- sincos(angle, out s, out c);
- return float3x3(1.0f, 0.0f, 0.0f,
- 0.0f, c, -s,
- 0.0f, s, c);
- }
- /// <summary>Returns a float4x4 matrix that rotates around the y-axis by a given number of radians.</summary>
- /// <param name="angle">The clockwise rotation angle when looking along the y-axis towards the origin in radians.</param>
- public static float3x3 RotateY(float angle)
- {
- // {{c_1, 0, s_1}, {0, 1, 0}, {-s_1, 0, c_1}}
- float s, c;
- sincos(angle, out s, out c);
- return float3x3(c, 0.0f, s,
- 0.0f, 1.0f, 0.0f,
- -s, 0.0f, c);
- }
- /// <summary>Returns a float4x4 matrix that rotates around the z-axis by a given number of radians.</summary>
- /// <param name="angle">The clockwise rotation angle when looking along the z-axis towards the origin in radians.</param>
- public static float3x3 RotateZ(float angle)
- {
- // {{c_2, -s_2, 0}, {s_2, c_2, 0}, {0, 0, 1}}
- float s, c;
- sincos(angle, out s, out c);
- return float3x3(c, -s, 0.0f,
- s, c, 0.0f,
- 0.0f, 0.0f, 1.0f);
- }
- //<summary>Returns a float3x3 matrix representing a uniform scaling of all axes by s.</summary>
- public static float3x3 Scale(float s)
- {
- return float3x3(s, 0.0f, 0.0f,
- 0.0f, s, 0.0f,
- 0.0f, 0.0f, s);
- }
- /// <summary>Returns a float3x3 matrix representing a non-uniform axis scaling by x, y and z.</summary>
- public static float3x3 Scale(float x, float y, float z)
- {
- return float3x3(x, 0.0f, 0.0f,
- 0.0f, y, 0.0f,
- 0.0f, 0.0f, z);
- }
- /// <summary>Returns a float3x3 matrix representing a non-uniform axis scaling by the components of the float3 vector v.</summary>
- public static float3x3 Scale(float3 v)
- {
- return Scale(v.x, v.y, v.z);
- }
- /// <summary>
- /// Returns a float3x3 view rotation matrix given a unit length forward vector and a unit length up vector.
- /// The two input vectors are assumed to be unit length and not collinear.
- /// If these assumptions are not met use float3x3.LookRotationSafe instead.
- /// </summary>
- public static float3x3 LookRotation(float3 forward, float3 up)
- {
- float3 t = normalize(cross(up, forward));
- return float3x3(t, cross(forward, t), forward);
- }
- /// <summary>
- /// Returns a float3x3 view rotation matrix given a forward vector and an up vector.
- /// The two input vectors are not assumed to be unit length.
- /// If the magnitude of either of the vectors is so extreme that the calculation cannot be carried out reliably or the vectors are collinear,
- /// the identity will be returned instead.
- /// </summary>
- public static float3x3 LookRotationSafe(float3 forward, float3 up)
- {
- float forwardLengthSq = dot(forward, forward);
- float upLengthSq = dot(up, up);
-
- forward *= rsqrt(forwardLengthSq);
- up *= rsqrt(upLengthSq);
- float3 t = cross(up, forward);
- float tLengthSq = dot(t, t);
- t *= rsqrt(tLengthSq);
- float mn = min(min(forwardLengthSq, upLengthSq), tLengthSq);
- float mx = max(max(forwardLengthSq, upLengthSq), tLengthSq);
- bool accept = mn > 1e-35f && mx < 1e35f && isfinite(forwardLengthSq) && isfinite(upLengthSq) && isfinite(tLengthSq);
- return float3x3(
- select(float3(1,0,0), t, accept),
- select(float3(0,1,0), cross(forward, t), accept),
- select(float3(0,0,1), forward, accept));
- }
- }
- public partial struct float4x4
- {
- /// <summary>Constructs a float4x4 from a float3x3 rotation matrix and a float3 translation vector.</summary>
- public float4x4(float3x3 rotation, float3 translation)
- {
- c0 = float4(rotation.c0, 0.0f);
- c1 = float4(rotation.c1, 0.0f);
- c2 = float4(rotation.c2, 0.0f);
- c3 = float4(translation, 1.0f);
- }
- /// <summary>Constructs a float4x4 from a quaternion and a float3 translation vector.</summary>
- public float4x4(quaternion rotation, float3 translation)
- {
- float3x3 rot = float3x3(rotation);
- c0 = float4(rot.c0, 0.0f);
- c1 = float4(rot.c1, 0.0f);
- c2 = float4(rot.c2, 0.0f);
- c3 = float4(translation, 1.0f);
- }
- /// <summary>Constructs a float4x4 from a RigidTransform.</summary>
- public float4x4(RigidTransform transform)
- {
- float3x3 rot = float3x3(transform.rot);
- c0 = float4(rot.c0, 0.0f);
- c1 = float4(rot.c1, 0.0f);
- c2 = float4(rot.c2, 0.0f);
- c3 = float4(transform.pos, 1.0f);
- }
- /// <summary>
- /// Returns a float4x4 matrix representing a rotation around a unit axis by an angle in radians.
- /// The rotation direction is clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- public static float4x4 AxisAngle(float3 axis, float angle)
- {
- float sina, cosa;
- math.sincos(angle, out sina, out cosa);
- float4 u = float4(axis, 0.0f);
- float4 u_yzx = u.yzxx;
- float4 u_zxy = u.zxyx;
- float4 u_inv_cosa = u - u * cosa; // u * (1.0f - cosa);
- float4 t = float4(u.xyz * sina, cosa);
- uint4 ppnp = uint4(0x00000000, 0x00000000, 0x80000000, 0x00000000);
- uint4 nppp = uint4(0x80000000, 0x00000000, 0x00000000, 0x00000000);
- uint4 pnpp = uint4(0x00000000, 0x80000000, 0x00000000, 0x00000000);
- uint4 mask = uint4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000);
- return float4x4(
- u.x * u_inv_cosa + asfloat((asuint(t.wzyx) ^ ppnp) & mask),
- u.y * u_inv_cosa + asfloat((asuint(t.zwxx) ^ nppp) & mask),
- u.z * u_inv_cosa + asfloat((asuint(t.yxwx) ^ pnpp) & mask),
- float4(0.0f, 0.0f, 0.0f, 1.0f)
- );
- }
- /// <summary>
- /// Returns a float4x4 rotation matrix constructed by first performing a rotation around the x-axis, then the y-axis and finally the z-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
- public static float4x4 EulerXYZ(float3 xyz)
- {
- // return mul(rotateZ(xyz.z), mul(rotateY(xyz.y), rotateX(xyz.x)));
- float3 s, c;
- sincos(xyz, out s, out c);
- return float4x4(
- c.y * c.z, c.z * s.x * s.y - c.x * s.z, c.x * c.z * s.y + s.x * s.z, 0.0f,
- c.y * s.z, c.x * c.z + s.x * s.y * s.z, c.x * s.y * s.z - c.z * s.x, 0.0f,
- -s.y, c.y * s.x, c.x * c.y, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f
- );
- }
- /// <summary>
- /// Returns a float4x4 rotation matrix constructed by first performing a rotation around the x-axis, then the z-axis and finally the y-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
- public static float4x4 EulerXZY(float3 xyz)
- {
- // return mul(rotateY(xyz.y), mul(rotateZ(xyz.z), rotateX(xyz.x))); }
- float3 s, c;
- sincos(xyz, out s, out c);
- return float4x4(
- c.y * c.z, s.x * s.y - c.x * c.y * s.z, c.x * s.y + c.y * s.x * s.z, 0.0f,
- s.z, c.x * c.z, -c.z * s.x, 0.0f,
- -c.z * s.y, c.y * s.x + c.x * s.y * s.z, c.x * c.y - s.x * s.y * s.z, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f
- );
- }
- /// <summary>
- /// Returns a float4x4 rotation matrix constructed by first performing a rotation around the y-axis, then the x-axis and finally the z-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
- public static float4x4 EulerYXZ(float3 xyz)
- {
- // return mul(rotateZ(xyz.z), mul(rotateX(xyz.x), rotateY(xyz.y)));
- float3 s, c;
- sincos(xyz, out s, out c);
- return float4x4(
- c.y * c.z - s.x * s.y * s.z, -c.x * s.z, c.z * s.y + c.y * s.x * s.z, 0.0f,
- c.z * s.x * s.y + c.y * s.z, c.x * c.z, s.y * s.z - c.y * c.z * s.x, 0.0f,
- -c.x * s.y, s.x, c.x * c.y, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f
- );
- }
- /// <summary>
- /// Returns a float4x4 rotation matrix constructed by first performing a rotation around the y-axis, then the z-axis and finally the x-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
- public static float4x4 EulerYZX(float3 xyz)
- {
- // return mul(rotateX(xyz.x), mul(rotateZ(xyz.z), rotateY(xyz.y)));
- float3 s, c;
- sincos(xyz, out s, out c);
- return float4x4(
- c.y * c.z, -s.z, c.z * s.y, 0.0f,
- s.x * s.y + c.x * c.y * s.z, c.x * c.z, c.x * s.y * s.z - c.y * s.x, 0.0f,
- c.y * s.x * s.z - c.x * s.y, c.z * s.x, c.x * c.y + s.x * s.y * s.z, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f
- );
- }
- /// <summary>
- /// Returns a float4x4 rotation matrix constructed by first performing a rotation around the z-axis, then the x-axis and finally the y-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// This is the default order rotation order in Unity.
- /// </summary>
- /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
- public static float4x4 EulerZXY(float3 xyz)
- {
- // return mul(rotateY(xyz.y), mul(rotateX(xyz.x), rotateZ(xyz.z)));
- float3 s, c;
- sincos(xyz, out s, out c);
- return float4x4(
- c.y * c.z + s.x * s.y * s.z, c.z * s.x * s.y - c.y * s.z, c.x * s.y, 0.0f,
- c.x * s.z, c.x * c.z, -s.x, 0.0f,
- c.y * s.x * s.z - c.z * s.y, c.y * c.z * s.x + s.y * s.z, c.x * c.y, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f
- );
- }
- /// <summary>
- /// Returns a float4x4 rotation matrix constructed by first performing a rotation around the z-axis, then the y-axis and finally the x-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
- public static float4x4 EulerZYX(float3 xyz)
- {
- // return mul(rotateX(xyz.x), mul(rotateY(xyz.y), rotateZ(xyz.z)));
- float3 s, c;
- sincos(xyz, out s, out c);
- return float4x4(
- c.y * c.z, -c.y * s.z, s.y, 0.0f,
- c.z * s.x * s.y + c.x * s.z, c.x * c.z - s.x * s.y * s.z, -c.y * s.x, 0.0f,
- s.x * s.z - c.x * c.z * s.y, c.z * s.x + c.x * s.y * s.z, c.x * c.y, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f
- );
- }
- /// <summary>
- /// Returns a float4x4 rotation matrix constructed by first performing a rotation around the x-axis, then the y-axis and finally the z-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="x">The rotation angle around the x-axis in radians.</param>
- /// <param name="y">The rotation angle around the y-axis in radians.</param>
- /// <param name="z">The rotation angle around the z-axis in radians.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float4x4 EulerXYZ(float x, float y, float z) { return EulerXYZ(float3(x, y, z)); }
- /// <summary>
- /// Returns a float4x4 rotation matrix constructed by first performing a rotation around the x-axis, then the z-axis and finally the y-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="x">The rotation angle around the x-axis in radians.</param>
- /// <param name="y">The rotation angle around the y-axis in radians.</param>
- /// <param name="z">The rotation angle around the z-axis in radians.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float4x4 EulerXZY(float x, float y, float z) { return EulerXZY(float3(x, y, z)); }
- /// <summary>
- /// Returns a float4x4 rotation matrix constructed by first performing a rotation around the y-axis, then the x-axis and finally the z-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="x">The rotation angle around the x-axis in radians.</param>
- /// <param name="y">The rotation angle around the y-axis in radians.</param>
- /// <param name="z">The rotation angle around the z-axis in radians.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float4x4 EulerYXZ(float x, float y, float z) { return EulerYXZ(float3(x, y, z)); }
- /// <summary>
- /// Returns a float4x4 rotation matrix constructed by first performing a rotation around the y-axis, then the z-axis and finally the x-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="x">The rotation angle around the x-axis in radians.</param>
- /// <param name="y">The rotation angle around the y-axis in radians.</param>
- /// <param name="z">The rotation angle around the z-axis in radians.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float4x4 EulerYZX(float x, float y, float z) { return EulerYZX(float3(x, y, z)); }
- /// <summary>
- /// Returns a float4x4 rotation matrix constructed by first performing a rotation around the z-axis, then the x-axis and finally the y-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// This is the default order rotation order in Unity.
- /// </summary>
- /// <param name="x">The rotation angle around the x-axis in radians.</param>
- /// <param name="y">The rotation angle around the y-axis in radians.</param>
- /// <param name="z">The rotation angle around the z-axis in radians.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float4x4 EulerZXY(float x, float y, float z) { return EulerZXY(float3(x, y, z)); }
- /// <summary>
- /// Returns a float4x4 rotation matrix constructed by first performing a rotation around the z-axis, then the y-axis and finally the x-axis.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// </summary>
- /// <param name="x">The rotation angle around the x-axis in radians.</param>
- /// <param name="y">The rotation angle around the y-axis in radians.</param>
- /// <param name="z">The rotation angle around the z-axis in radians.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float4x4 EulerZYX(float x, float y, float z) { return EulerZYX(float3(x, y, z)); }
- public static float4x4 Euler(float3 xyz, RotationOrder order = RotationOrder.Default)
- {
- switch (order)
- {
- case RotationOrder.XYZ:
- return EulerXYZ(xyz);
- case RotationOrder.XZY:
- return EulerXZY(xyz);
- case RotationOrder.YXZ:
- return EulerYXZ(xyz);
- case RotationOrder.YZX:
- return EulerYZX(xyz);
- case RotationOrder.ZXY:
- return EulerZXY(xyz);
- case RotationOrder.ZYX:
- return EulerZYX(xyz);
- default:
- return float4x4.identity;
- }
- }
- /// <summary>
- /// Returns a float4x4 rotation matrix constructed by first performing 3 rotations around the principal axes in a given order.
- /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
- /// When the rotation order is known at compile time, it is recommended for performance reasons to use specific
- /// Euler rotation constructors such as EulerZXY(...).
- /// </summary>
- /// <param name="x">The rotation angle around the x-axis in radians.</param>
- /// <param name="y">The rotation angle around the y-axis in radians.</param>
- /// <param name="z">The rotation angle around the z-axis in radians.</param>
- /// <param name="order">The order in which the rotations are applied.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float4x4 Euler(float x, float y, float z, RotationOrder order = RotationOrder.Default)
- {
- return Euler(float3(x, y, z), order);
- }
- /// <summary>Returns a float4x4 matrix that rotates around the x-axis by a given number of radians.</summary>
- /// <param name="angle">The clockwise rotation angle when looking along the x-axis towards the origin in radians.</param>
- public static float4x4 RotateX(float angle)
- {
- // {{1, 0, 0}, {0, c_0, -s_0}, {0, s_0, c_0}}
- float s, c;
- sincos(angle, out s, out c);
- return float4x4(1.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, c, -s, 0.0f,
- 0.0f, s, c, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f);
- }
- /// <summary>Returns a float4x4 matrix that rotates around the y-axis by a given number of radians.</summary>
- /// <param name="angle">The clockwise rotation angle when looking along the y-axis towards the origin in radians.</param>
- public static float4x4 RotateY(float angle)
- {
- // {{c_1, 0, s_1}, {0, 1, 0}, {-s_1, 0, c_1}}
- float s, c;
- sincos(angle, out s, out c);
- return float4x4(c, 0.0f, s, 0.0f,
- 0.0f, 1.0f, 0.0f, 0.0f,
- -s, 0.0f, c, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f);
- }
- /// <summary>Returns a float4x4 matrix that rotates around the z-axis by a given number of radians.</summary>
- /// <param name="angle">The clockwise rotation angle when looking along the z-axis towards the origin in radians.</param>
- public static float4x4 RotateZ(float angle)
- {
- // {{c_2, -s_2, 0}, {s_2, c_2, 0}, {0, 0, 1}}
- float s, c;
- sincos(angle, out s, out c);
- return float4x4(c, -s, 0.0f, 0.0f,
- s, c, 0.0f, 0.0f,
- 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f);
- }
- /// <summary>Returns a float4x4 scale matrix given 3 axis scales.</summary>
- public static float4x4 Scale(float s)
- {
- return float4x4(s, 0.0f, 0.0f, 0.0f,
- 0.0f, s, 0.0f, 0.0f,
- 0.0f, 0.0f, s, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f);
- }
- /// <summary>Returns a float4x4 scale matrix given a float3 vector containing the 3 axis scales.</summary>
- public static float4x4 Scale(float x, float y, float z)
- {
- return float4x4(x, 0.0f, 0.0f, 0.0f,
- 0.0f, y, 0.0f, 0.0f,
- 0.0f, 0.0f, z, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f);
- }
- /// <summary>Returns a float4x4 scale matrix given a float3 vector containing the 3 axis scales.</summary>
- public static float4x4 Scale(float3 scales)
- {
- return Scale(scales.x, scales.y, scales.z);
- }
- /// <summary>Returns a float4x4 translation matrix given a float3 translation vector.</summary>
- public static float4x4 Translate(float3 vector)
- {
- return float4x4(float4(1.0f, 0.0f, 0.0f, 0.0f),
- float4(0.0f, 1.0f, 0.0f, 0.0f),
- float4(0.0f, 0.0f, 1.0f, 0.0f),
- float4(vector.x, vector.y, vector.z, 1.0f));
- }
- /// <summary>
- /// Returns a float4x4 view matrix given an eye position, a target point and a unit length up vector.
- /// The up vector is assumed to be unit length, the eye and target points are assumed to be distinct and
- /// the vector between them is assumes to be collinear with the up vector.
- /// If these assumptions are not met use float4x4.LookRotationSafe instead.
- /// </summary>
- public static float4x4 LookAt(float3 eye, float3 target, float3 up)
- {
- float3x3 rot = float3x3.LookRotation(normalize(target - eye), up);
- float4x4 matrix;
- matrix.c0 = float4(rot.c0, 0.0F);
- matrix.c1 = float4(rot.c1, 0.0F);
- matrix.c2 = float4(rot.c2, 0.0F);
- matrix.c3 = float4(eye, 1.0F);
- return matrix;
- }
- /// <summary>
- /// Returns a float4x4 centered orthographic projection matrix.
- /// </summary>
- /// <param name="width">The width of the view volume.</param>
- /// <param name="height">The height of the view volume.</param>
- /// <param name="near">The distance to the near plane.</param>
- /// <param name="far">The distance to the far plane.</param>
- public static float4x4 Ortho(float width, float height, float near, float far)
- {
- float rcpdx = 1.0f / width;
- float rcpdy = 1.0f / height;
- float rcpdz = 1.0f / (far - near);
- return float4x4(
- 2.0f * rcpdx, 0.0f, 0.0f, 0.0f,
- 0.0f, 2.0f * rcpdy, 0.0f, 0.0f,
- 0.0f, 0.0f, -2.0f * rcpdz, -(far + near) * rcpdz,
- 0.0f, 0.0f, 0.0f, 1.0f
- );
- }
- /// <summary>
- /// Returns a float4x4 off-center orthographic projection matrix.
- /// </summary>
- /// <param name="left">The minimum x-coordinate of the view volume.</param>
- /// <param name="right">The maximum x-coordinate of the view volume.</param>
- /// <param name="bottom">The minimum y-coordinate of the view volume.</param>
- /// <param name="top">The minimum y-coordinate of the view volume.</param>
- /// <param name="near">The distance to the near plane.</param>
- /// <param name="far">The distance to the far plane.</param>
- public static float4x4 OrthoOffCenter(float left, float right, float bottom, float top, float near, float far)
- {
- float rcpdx = 1.0f / (right - left);
- float rcpdy = 1.0f / (top - bottom);
- float rcpdz = 1.0f / (far - near);
- return float4x4(
- 2.0f * rcpdx, 0.0f, 0.0f, -(right + left) * rcpdx,
- 0.0f, 2.0f * rcpdy, 0.0f, -(top + bottom) * rcpdy,
- 0.0f, 0.0f, -2.0f * rcpdz, -(far + near) * rcpdz,
- 0.0f, 0.0f, 0.0f, 1.0f
- );
- }
- /// <summary>
- /// Returns a float4x4 perspective projection matrix based on field of view.
- /// </summary>
- /// <param name="verticalFov">Vertical Field of view in radians.</param>
- /// <param name="aspect">X:Y aspect ratio.</param>
- /// <param name="near">Distance to near plane. Must be greater than zero.</param>
- /// <param name="far">Distance to far plane. Must be greater than zero.</param>
- public static float4x4 PerspectiveFov(float verticalFov, float aspect, float near, float far)
- {
- float cotangent = 1.0f / tan(verticalFov * 0.5f);
- float rcpdz = 1.0f / (near - far);
- return float4x4(
- cotangent / aspect, 0.0f, 0.0f, 0.0f,
- 0.0f, cotangent, 0.0f, 0.0f,
- 0.0f, 0.0f, (far + near) * rcpdz, 2.0f * near * far * rcpdz,
- 0.0f, 0.0f, -1.0f, 0.0f
- );
- }
- /// <summary>
- /// Returns a float4x4 off-center perspective projection matrix.
- /// </summary>
- /// <param name="left">The x-coordinate of the left side of the clipping frustum at the near plane.</param>
- /// <param name="right">The x-coordinate of the right side of the clipping frustum at the near plane.</param>
- /// <param name="bottom">The y-coordinate of the bottom side of the clipping frustum at the near plane.</param>
- /// <param name="top">The y-coordinate of the top side of the clipping frustum at the near plane.</param>
- /// <param name="near">Distance to the near plane. Must be greater than zero.</param>
- /// <param name="far">Distance to the far plane. Must be greater than zero.</param>
- public static float4x4 PerspectiveOffCenter(float left, float right, float bottom, float top, float near, float far)
- {
- float rcpdz = 1.0f / (near - far);
- float rcpWidth = 1.0f / (right - left);
- float rcpHeight = 1.0f / (top - bottom);
- return float4x4(
- 2.0f * near * rcpWidth, 0.0f, (left + right) * rcpWidth, 0.0f,
- 0.0f, 2.0f * near * rcpHeight, (bottom + top) * rcpHeight, 0.0f,
- 0.0f, 0.0f, (far + near) * rcpdz, 2.0f * near * far * rcpdz,
- 0.0f, 0.0f, -1.0f, 0.0f
- );
- }
- /// <summary>
- /// Returns a float4x4 matrix representing a combined scale-, rotation- and translation transform.
- /// Equivalent to mul(translationTransform, mul(rotationTransform, scaleTransform)).
- /// </summary>
- public static float4x4 TRS(float3 translation, quaternion rotation, float3 scale)
- {
- float3x3 r = float3x3(rotation);
- return float4x4( float4(r.c0 * scale.x, 0.0f),
- float4(r.c1 * scale.y, 0.0f),
- float4(r.c2 * scale.z, 0.0f),
- float4(translation, 1.0f));
- }
- }
- partial class math
- {
- /// <summary>Returns a float3x3 matrix constructed from a quaternion.</summary>
- public static float3x3 float3x3(quaternion rotation)
- {
- return new float3x3(rotation);
- }
- /// <summary>Returns a float4x4 constructed from a float3x3 rotation matrix and a float3 translation vector.</summary>
- public static float4x4 float4x4(float3x3 rotation, float3 translation)
- {
- return new float4x4(rotation, translation);
- }
- /// <summary>Returns a float4x4 constructed from a quaternion and a float3 translation vector.</summary>
- public static float4x4 float4x4(quaternion rotation, float3 translation)
- {
- return new float4x4(rotation, translation);
- }
- /// <summary>Returns a float4x4 constructed from a RigidTransform.</summary>
- public static float4x4 float4x4(RigidTransform transform)
- {
- return new float4x4(transform);
- }
- /// <summary>Returns an orthonormalized version of a float3x3 matrix.</summary>
- public static float3x3 orthonormalize(float3x3 i)
- {
- float3x3 o;
- float3 u = i.c0;
- float3 v = i.c1 - i.c0 * math.dot(i.c1, i.c0);
- float lenU = math.length(u);
- float lenV = math.length(v);
- bool c = lenU > 1e-30f && lenV > 1e-30f;
- o.c0 = math.select(float3(1, 0, 0), u / lenU, c);
- o.c1 = math.select(float3(0, 1, 0), v / lenV, c);
- o.c2 = math.cross(o.c0, o.c1);
- return o;
- }
- }
- }
|