Search Unity

Feature Request Unity.Mathematics is incomplete. Here's what's missing (and few custom ones)

Discussion in 'Scripting' started by StuwuStudio, Jan 28, 2023.

  1. StuwuStudio

    StuwuStudio

    Joined:
    Feb 4, 2015
    Posts:
    165
    Feel free to share more missing functions from the package. I don't get why they wouldn't be included, as they are all pretty useful.
    I'm planning to write some more as soon as I need them.

    I tested some of them but there might be some edge cases and issues with some of these functions.

    Vector3.ProjectOnPlane
    Quaternion.FromToRotation
    Quaternion.Angle

    Code (CSharp):
    1. const float epsilon = 1E-6f;
    2. // Same as from-to rotation, but with an in-between vector in case the two vectors are opposites.
    3. [MethodImpl(MethodImplOptions.AggressiveInlining)]
    4. public static quaternion FromToRotationSafe (float3 dir1, float3 dir2, float3 middlePointIfOpposite)
    5. {
    6.     float r = 1f + math.dot(dir1, dir2);
    7.  
    8.     if (r < epsilon)
    9.     {
    10.         return math.mul(FromToRotation(dir1, middlePointIfOpposite), FromToRotation(middlePointIfOpposite, dir2));
    11.     }
    12.     else
    13.     {
    14.         return FromToRotation(dir1, dir2);
    15.     }
    16. }
    17.  
    18. [MethodImpl(MethodImplOptions.AggressiveInlining)]
    19. public static quaternion FromToRotation (float3 fromDirection, float3 toDirection)
    20. {
    21.     float3 cross = math.cross(fromDirection, toDirection);
    22.     float dot = math.dot(fromDirection, toDirection);
    23.  
    24.     var q = new quaternion(
    25.         cross.x,
    26.         cross.y,
    27.         cross.z,
    28.         dot + math.sqrt(math.length(fromDirection) * math.length(toDirection))
    29.     );
    30.  
    31.     return math.normalize(q);
    32. }
    33.  
    34. [MethodImpl(MethodImplOptions.AggressiveInlining)]
    35. public static float3 ProjectOnPlane (float3 vector, float3 planeNormal)
    36. {
    37.     float invSqrMag = 1f / math.dot(planeNormal, planeNormal);
    38.     if (math.isnan(invSqrMag))
    39.         return vector;
    40.     else
    41.     {
    42.         var dot = math.dot(vector, planeNormal);
    43.         return new float3(
    44.             vector.x - planeNormal.x * dot * invSqrMag,
    45.             vector.y - planeNormal.y * dot * invSqrMag,
    46.             vector.z - planeNormal.z * dot * invSqrMag);
    47.     }
    48. }
    49.  
    50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
    51. public static float Angle (quaternion a, quaternion b)
    52. {
    53.     float dot = math.min(math.abs(math.dot(a, b)), 1.0f);
    54.     return IsEqualUsingDot(dot) ? 0.0f : math.degrees(math.acos(dot) * 2.0f);
    55. }
    56.  
    57. // Is the dot product of two quaternions within tolerance for them to be considered equal?
    58. [MethodImpl(MethodImplOptions.AggressiveInlining)]
    59. private static bool IsEqualUsingDot (float dot)
    60. {
    61.     // Returns false in the presence of NaN values.
    62.     return dot > 1.0f - epsilon;
    63. }
    64.  
    65.  
     
  2. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    721
    Some probability distributions would be handy. I keep re-implementing the Gaussian function...
     
    StuwuStudio likes this.