44namespace Unity . Netcode
55{
66 /// <summary>
7- /// A Smallest Three Quaternion Compressor Implementation
7+ /// The Smallest Three Quaternion Compressor Implementation
88 /// </summary>
99 /// <remarks>
1010 /// Explanation of why "The smallest three":
@@ -26,14 +26,14 @@ public static class QuaternionCompressor
2626
2727 // We can further improve the encoding compression by dividing k_SqrtTwoOverTwo into 1.0f and multiplying that
2828 // by the precision mask (minor reduction of runtime calculations)
29- private const float k_CompressionEcodingMask = ( 1.0f / k_SqrtTwoOverTwoEncoding ) * k_PrecisionMask ;
29+ private const float k_CompressionEncodingMask = ( 1.0f / k_SqrtTwoOverTwoEncoding ) * k_PrecisionMask ;
3030
3131 // Used to shift the negative bit to the 10th bit position when compressing and encoding
3232 private const ushort k_ShiftNegativeBit = 9 ;
3333
3434 // We can do the same for our decoding and decompression by dividing k_PrecisionMask into 1.0 and multiplying
3535 // that by k_SqrtTwoOverTwo (minor reduction of runtime calculations)
36- private const float k_DcompressionDecodingMask = ( 1.0f / k_PrecisionMask ) * k_SqrtTwoOverTwoEncoding ;
36+ private const float k_DecompressionDecodingMask = ( 1.0f / k_PrecisionMask ) * k_SqrtTwoOverTwoEncoding ;
3737
3838 // The sign bit position (10th bit) used when decompressing and decoding
3939 private const ushort k_NegShortBit = 0x200 ;
@@ -59,7 +59,7 @@ public static uint CompressQuaternion(ref Quaternion quaternion)
5959 // Get the largest element value of the quaternion to know what the remaining "Smallest Three" values are
6060 var quatMax = Mathf . Max ( quatAbsValue0 , quatAbsValue1 , quatAbsValue2 , quatAbsValue3 ) ;
6161
62- // Find the index of the largest element so we can skip that element while compressing and decompressing
62+ // Find the index of the largest element, so we can skip that element while compressing and decompressing
6363 var indexToSkip = ( ushort ) ( quatAbsValue0 == quatMax ? 0 : quatAbsValue1 == quatMax ? 1 : quatAbsValue2 == quatMax ? 2 : 3 ) ;
6464
6565 // Get the sign of the largest element which is all that is needed when calculating the sum of squares of a normalized quaternion.
@@ -68,14 +68,14 @@ public static uint CompressQuaternion(ref Quaternion quaternion)
6868 // Start with the index to skip which will be shifted to the highest two bits
6969 var compressed = ( uint ) indexToSkip ;
7070
71- // Step 2 : If we are on the index to skip preserve the current compressed value, otherwise proceed to step 3 and 4
72- // Step 3 : Get the sign of the element we are processing. If it is the not the same as the largest value's sign bit then we set the bit
73- // Step 4 : Get the compressed and encoded value by multiplying the absolute value of the current element by k_CompressionEcodingMask and round that result up
74- compressed = 0 != indexToSkip ? ( compressed << 10 ) | ( uint ) ( ( quaternion [ 0 ] < 0 ? k_True : k_False ) != quatMaxSign ? k_True : k_False ) << k_ShiftNegativeBit | ( ushort ) Mathf . Round ( k_CompressionEcodingMask * quatAbsValue0 ) : compressed ;
75- // Repeat the last 3 steps for the remaining elements
76- compressed = 1 != indexToSkip ? ( compressed << 10 ) | ( uint ) ( ( quaternion [ 1 ] < 0 ? k_True : k_False ) != quatMaxSign ? k_True : k_False ) << k_ShiftNegativeBit | ( ushort ) Mathf . Round ( k_CompressionEcodingMask * quatAbsValue1 ) : compressed ;
77- compressed = 2 != indexToSkip ? ( compressed << 10 ) | ( uint ) ( ( quaternion [ 2 ] < 0 ? k_True : k_False ) != quatMaxSign ? k_True : k_False ) << k_ShiftNegativeBit | ( ushort ) Mathf . Round ( k_CompressionEcodingMask * quatAbsValue2 ) : compressed ;
78- compressed = 3 != indexToSkip ? ( compressed << 10 ) | ( uint ) ( ( quaternion [ 3 ] < 0 ? k_True : k_False ) != quatMaxSign ? k_True : k_False ) << k_ShiftNegativeBit | ( ushort ) Mathf . Round ( k_CompressionEcodingMask * quatAbsValue3 ) : compressed ;
71+ // Step 1 : If we are on the index to skip, preserve the current compressed value, otherwise proceed to step 2 and 3
72+ // Step 2 : Get the sign of the element we are processing. If it is not the same as the largest value's sign bit then we set the bit
73+ // Step 3 : Get the compressed and encoded value by multiplying the absolute value of the current element by k_CompressionEncodingMask and round that result up
74+ compressed = 0 != indexToSkip ? ( compressed << 10 ) | ( uint ) ( ( quaternion [ 0 ] < 0 ? k_True : k_False ) != quatMaxSign ? k_True : k_False ) << k_ShiftNegativeBit | ( ushort ) Mathf . Round ( k_CompressionEncodingMask * quatAbsValue0 ) : compressed ;
75+ // Repeat the 3 steps for the remaining elements
76+ compressed = 1 != indexToSkip ? ( compressed << 10 ) | ( uint ) ( ( quaternion [ 1 ] < 0 ? k_True : k_False ) != quatMaxSign ? k_True : k_False ) << k_ShiftNegativeBit | ( ushort ) Mathf . Round ( k_CompressionEncodingMask * quatAbsValue1 ) : compressed ;
77+ compressed = 2 != indexToSkip ? ( compressed << 10 ) | ( uint ) ( ( quaternion [ 2 ] < 0 ? k_True : k_False ) != quatMaxSign ? k_True : k_False ) << k_ShiftNegativeBit | ( ushort ) Mathf . Round ( k_CompressionEncodingMask * quatAbsValue2 ) : compressed ;
78+ compressed = 3 != indexToSkip ? ( compressed << 10 ) | ( uint ) ( ( quaternion [ 3 ] < 0 ? k_True : k_False ) != quatMaxSign ? k_True : k_False ) << k_ShiftNegativeBit | ( ushort ) Mathf . Round ( k_CompressionEncodingMask * quatAbsValue3 ) : compressed ;
7979
8080 // Return the compress quaternion
8181 return compressed ;
@@ -101,7 +101,7 @@ public static void DecompressQuaternion(ref Quaternion quaternion, uint compress
101101 continue ;
102102 }
103103 // Check the negative bit and multiply that result with the decompressed and decoded value
104- quaternion [ i ] = ( ( compressed & k_NegShortBit ) > 0 ? - 1.0f : 1.0f ) * ( ( compressed & k_PrecisionMask ) * k_DcompressionDecodingMask ) ;
104+ quaternion [ i ] = ( ( compressed & k_NegShortBit ) > 0 ? - 1.0f : 1.0f ) * ( ( compressed & k_PrecisionMask ) * k_DecompressionDecodingMask ) ;
105105 sumOfSquaredMagnitudes += quaternion [ i ] * quaternion [ i ] ;
106106 compressed = compressed >> 10 ;
107107 }
0 commit comments