/// <summary> /// Writes a unit vector (ie. a vector of length 1.0, for example a surface normal) /// using specified number of bits /// </summary> public static void WriteUnitVector3(this NetBuffer message, Vector3 unitVector, int numberOfBits) { float x = unitVector.x; float y = unitVector.y; float z = unitVector.z; double invPi = 1.0 / Math.PI; float phi = (float)(Math.Atan2(x, y) * invPi); float theta = (float)(Math.Atan2(z, Math.Sqrt(x * x + y * y)) * (invPi * 2)); int halfBits = numberOfBits / 2; message.WriteSignedSingle(phi, halfBits); message.WriteSignedSingle(theta, numberOfBits - halfBits); }
/// <summary> /// Writes a unit quaternion using the specified number of bits per element /// for a total of 4 x bitsPerElements bits. Suggested value is 8 to 24 bits. /// </summary> public static void WriteRotation(this NetBuffer message, Quaternion quaternion, int bitsPerElement) { if (quaternion.x > 1.0f) { quaternion.x = 1.0f; } if (quaternion.y > 1.0f) { quaternion.y = 1.0f; } if (quaternion.z > 1.0f) { quaternion.z = 1.0f; } if (quaternion.w > 1.0f) { quaternion.w = 1.0f; } if (quaternion.x < -1.0f) { quaternion.x = -1.0f; } if (quaternion.y < -1.0f) { quaternion.y = -1.0f; } if (quaternion.z < -1.0f) { quaternion.z = -1.0f; } if (quaternion.w < -1.0f) { quaternion.w = -1.0f; } message.WriteSignedSingle(quaternion.x, bitsPerElement); message.WriteSignedSingle(quaternion.y, bitsPerElement); message.WriteSignedSingle(quaternion.z, bitsPerElement); message.WriteSignedSingle(quaternion.w, bitsPerElement); }
/// <summary> /// Writes a unit quaternion using the specified number of bits per element /// for a total of 4 x bitsPerElements bits. Suggested value is 8 to 24 bits. /// </summary> public static void WriteRotation(this NetBuffer message, Quaternion quaternion, int bitsPerElement) { if (quaternion.X > 1.0f) { quaternion.X = 1.0f; } if (quaternion.Y > 1.0f) { quaternion.Y = 1.0f; } if (quaternion.Z > 1.0f) { quaternion.Z = 1.0f; } if (quaternion.W > 1.0f) { quaternion.W = 1.0f; } if (quaternion.X < -1.0f) { quaternion.X = -1.0f; } if (quaternion.Y < -1.0f) { quaternion.Y = -1.0f; } if (quaternion.Z < -1.0f) { quaternion.Z = -1.0f; } if (quaternion.W < -1.0f) { quaternion.W = -1.0f; } message.WriteSignedSingle(quaternion.X, bitsPerElement); message.WriteSignedSingle(quaternion.Y, bitsPerElement); message.WriteSignedSingle(quaternion.Z, bitsPerElement); message.WriteSignedSingle(quaternion.W, bitsPerElement); }