Skip to content

Instantly share code, notes, and snippets.

@maluoi
Created November 6, 2022 23:42
Show Gist options
  • Save maluoi/cd4f94922487c210725ea50df7719b6d to your computer and use it in GitHub Desktop.
Save maluoi/cd4f94922487c210725ea50df7719b6d to your computer and use it in GitHub Desktop.
namespace StereoKit
{
/// <summary>Some utility functions for converting coordinate data to and
/// from some commonly used tools!
///
/// StereoKit uses a right-handed coordinate system, where +x is to the
/// right, +y is upwards, and -z is forward. This is the exact same
/// coordinate system as OpenXR, so no conversions are necessary there :)
///
/// Unity uses a left-handed coordinate system, where +x is to the right,
/// +y is upwards, and +z is forward.
///
/// Blender uses a right-handed coordinate system, where +x is to the
/// right, +z is upward, and -z is forward.
/// </summary>
public static class Coordinates
{
/// <summary>Convert a point from Unity's coordinate system to
/// StereoKit's.</summary>
/// <param name="unityPoint">A point in Unity's coordinate system.
/// </param>
/// <returns>A point in StereoKit's coordinate system.</returns>
public static Vec3 FromUnity(Vec3 unityPoint) => new Vec3(unityPoint.x, unityPoint.y, -unityPoint.z);
public static Quat FromUnity(Quat unityRotation) => new Quat(unityRotation.x, unityRotation.y, -unityRotation.z, -unityRotation.w);
public static Pose FromUnity(Pose unityPose) => new Pose(FromUnity(unityPose.position), FromUnity(unityPose.orientation));
public static Vec3 ToUnity(Vec3 stereokitPoint) => new Vec3(stereokitPoint.x, stereokitPoint.y, -stereokitPoint.z);
public static Quat ToUnity(Quat stereokitRotation) => new Quat(stereokitRotation.x, stereokitRotation.y, -stereokitRotation.z, -stereokitRotation.w);
public static Pose ToUnity(Pose stereokitPose) => new Pose(ToUnity(stereokitPose.position), ToUnity(stereokitPose.orientation));
/// <summary>Convert a point from Blender's coordinate system to
/// StereoKit's.</summary>
/// <param name="blenderPoint">A point in Blender's coordinate system.
/// </param>
/// <returns>A point in StereoKit's coordinate system.</returns>
public static Vec3 FromBlender(Vec3 blenderPoint) => new Vec3(blenderPoint.x, blenderPoint.z, -blenderPoint.y);
public static Quat FromBlender(Quat blenderRotation) => new Quat(blenderRotation.x, blenderRotation.z, -blenderRotation.y, blenderRotation.w);
public static Pose FromBlender(Pose blenderPose) => new Pose(FromBlender(blenderPose.position), FromBlender(blenderPose.orientation));
public static Vec3 ToBlender(Vec3 stereokitPoint) => new Vec3(stereokitPoint.x, stereokitPoint.z, -stereokitPoint.y);
public static Quat ToBlender(Quat stereokitRotation) => new Quat(stereokitRotation.x, stereokitRotation.z, -stereokitRotation.y, stereokitRotation.w);
public static Pose ToBlender(Pose stereokitPose) => new Pose(ToBlender(stereokitPose.position), ToBlender(stereokitPose.orientation));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment