@@ -42,13 +42,6 @@ public static class QuaternionCompressor
4242 private const ushort k_True = 1 ;
4343 private const ushort k_False = 0 ;
4444
45- // Used to store the absolute value of the 4 quaternion elements
46- private static Quaternion s_QuatAbsValues = Quaternion . identity ;
47- #if UNITY_EDITOR
48- [ RuntimeInitializeOnLoadMethod ( RuntimeInitializeLoadType . SubsystemRegistration ) ]
49- private static void ResetStaticsOnLoad ( ) => s_QuatAbsValues = Quaternion . identity ;
50- #endif
51-
5245 /// <summary>
5346 /// Compresses a Quaternion into an unsigned integer
5447 /// </summary>
@@ -58,38 +51,31 @@ public static class QuaternionCompressor
5851 public static uint CompressQuaternion ( ref Quaternion quaternion )
5952 {
6053 // Store off the absolute value for each Quaternion element
61- s_QuatAbsValues [ 0 ] = Mathf . Abs ( quaternion [ 0 ] ) ;
62- s_QuatAbsValues [ 1 ] = Mathf . Abs ( quaternion [ 1 ] ) ;
63- s_QuatAbsValues [ 2 ] = Mathf . Abs ( quaternion [ 2 ] ) ;
64- s_QuatAbsValues [ 3 ] = Mathf . Abs ( quaternion [ 3 ] ) ;
54+ var quatAbsValue0 = Mathf . Abs ( quaternion [ 0 ] ) ;
55+ var quatAbsValue1 = Mathf . Abs ( quaternion [ 1 ] ) ;
56+ var quatAbsValue2 = Mathf . Abs ( quaternion [ 2 ] ) ;
57+ var quatAbsValue3 = Mathf . Abs ( quaternion [ 3 ] ) ;
6558
6659 // Get the largest element value of the quaternion to know what the remaining "Smallest Three" values are
67- var quatMax = Mathf . Max ( s_QuatAbsValues [ 0 ] , s_QuatAbsValues [ 1 ] , s_QuatAbsValues [ 2 ] , s_QuatAbsValues [ 3 ] ) ;
60+ var quatMax = Mathf . Max ( quatAbsValue0 , quatAbsValue1 , quatAbsValue2 , quatAbsValue3 ) ;
6861
6962 // Find the index of the largest element so we can skip that element while compressing and decompressing
70- var indexToSkip = ( ushort ) ( s_QuatAbsValues [ 0 ] == quatMax ? 0 : s_QuatAbsValues [ 1 ] == quatMax ? 1 : s_QuatAbsValues [ 2 ] == quatMax ? 2 : 3 ) ;
63+ var indexToSkip = ( ushort ) ( quatAbsValue0 == quatMax ? 0 : quatAbsValue1 == quatMax ? 1 : quatAbsValue2 == quatMax ? 2 : 3 ) ;
7164
7265 // Get the sign of the largest element which is all that is needed when calculating the sum of squares of a normalized quaternion.
73-
7466 var quatMaxSign = ( quaternion [ indexToSkip ] < 0 ? k_True : k_False ) ;
7567
7668 // Start with the index to skip which will be shifted to the highest two bits
7769 var compressed = ( uint ) indexToSkip ;
7870
79- // Step 1: Start with the first element
80- var currentIndex = 0 ;
81-
8271 // Step 2: If we are on the index to skip preserve the current compressed value, otherwise proceed to step 3 and 4
8372 // 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
8473 // 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
85- compressed = currentIndex != indexToSkip ? ( compressed << 10 ) | ( uint ) ( ( quaternion [ currentIndex ] < 0 ? k_True : k_False ) != quatMaxSign ? k_True : k_False ) << k_ShiftNegativeBit | ( ushort ) Mathf . Round ( k_CompressionEcodingMask * s_QuatAbsValues [ currentIndex ] ) : compressed ;
86- currentIndex ++ ;
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 ;
8775 // Repeat the last 3 steps for the remaining elements
88- compressed = currentIndex != indexToSkip ? ( compressed << 10 ) | ( uint ) ( ( quaternion [ currentIndex ] < 0 ? k_True : k_False ) != quatMaxSign ? k_True : k_False ) << k_ShiftNegativeBit | ( ushort ) Mathf . Round ( k_CompressionEcodingMask * s_QuatAbsValues [ currentIndex ] ) : compressed ;
89- currentIndex ++ ;
90- compressed = currentIndex != indexToSkip ? ( compressed << 10 ) | ( uint ) ( ( quaternion [ currentIndex ] < 0 ? k_True : k_False ) != quatMaxSign ? k_True : k_False ) << k_ShiftNegativeBit | ( ushort ) Mathf . Round ( k_CompressionEcodingMask * s_QuatAbsValues [ currentIndex ] ) : compressed ;
91- currentIndex ++ ;
92- compressed = currentIndex != indexToSkip ? ( compressed << 10 ) | ( uint ) ( ( quaternion [ currentIndex ] < 0 ? k_True : k_False ) != quatMaxSign ? k_True : k_False ) << k_ShiftNegativeBit | ( ushort ) Mathf . Round ( k_CompressionEcodingMask * s_QuatAbsValues [ currentIndex ] ) : compressed ;
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 ;
9379
9480 // Return the compress quaternion
9581 return compressed ;
0 commit comments