示例#1
0
 public static void WriteRotation(NetBuffer buffer, CompressedRotation rotation)
 {
     buffer.Write((uint)rotation.LargestComponentType, LargestComponentIndicatorBitSize);
     buffer.Write(rotation.A, rotation.ComponentPrecisionInBits);
     buffer.Write(rotation.B, rotation.ComponentPrecisionInBits);
     buffer.Write(rotation.C, rotation.ComponentPrecisionInBits);
 }
示例#2
0
        public static Quaternion ToQuaternion(this CompressedRotation compressedRotation)
        {
            float scale        = (1 << compressedRotation.ComponentPrecisionInBits) - 1;
            float inverseScale = 1.0f / scale;

            float a = compressedRotation.A * inverseScale * (Maximum - Minimum) + Minimum;
            float b = compressedRotation.B * inverseScale * (Maximum - Minimum) + Minimum;
            float c = compressedRotation.C * inverseScale * (Maximum - Minimum) + Minimum;

            Quaternion rotation;

            switch (compressedRotation.LargestComponentType)
            {
            case ComponentType.X:
                // (?) y z w
                rotation.x = Mathf.Sqrt(1 - a * a
                                        - b * b
                                        - c * c);
                rotation.y = a;
                rotation.z = b;
                rotation.w = c;
                break;

            case ComponentType.Y:
                // x (?) z w
                rotation.x = a;
                rotation.y = Mathf.Sqrt(1 - a * a
                                        - b * b
                                        - c * c);
                rotation.z = b;
                rotation.w = c;
                break;

            case ComponentType.Z:
                // x y (?) w
                rotation.x = a;
                rotation.y = b;
                rotation.z = Mathf.Sqrt(1 - a * a
                                        - b * b
                                        - c * c);
                rotation.w = c;
                break;

            case ComponentType.W:
                // x y z (?)
                rotation.x = a;
                rotation.y = b;
                rotation.z = c;
                rotation.w = Mathf.Sqrt(1 - a * a
                                        - b * b
                                        - c * c);
                break;

            default:
                // Should never happen!
                throw new ArgumentOutOfRangeException("Unknown rotation component type: " +
                                                      compressedRotation.LargestComponentType);
            }

            return(rotation);
        }