using System.Runtime.InteropServices; using UnityEngine.XR.OpenXR.NativeTypes; #if UNITY_EDITOR using UnityEditor; #endif namespace UnityEngine.XR.OpenXR.Features { /// /// This enables the use of SpaceWarp in OpenXR without /// the need for other 3rd party packages. /// #if UNITY_EDITOR && UNITY_6000_1_OR_NEWER [UnityEditor.XR.OpenXR.Features.OpenXRFeature( UiName = k_UiName, Desc = @"Application SpaceWarp feature", Company = "Unity", DocumentationLink = "", OpenxrExtensionStrings = k_OpenXRRequestedExtensions, Version = "1.0.0", BuildTargetGroups = new[] { BuildTargetGroup.Android }, FeatureId = k_SpaceWarpFeatureId)] #endif public class SpaceWarpFeature : OpenXRFeature { /// /// The UI name that shows on the XR Plug-in Management panel. /// public const string k_UiName = "Application SpaceWarp"; /// /// The required OpenXR extension. /// public const string k_OpenXRRequestedExtensions = "XR_FB_space_warp"; /// /// The feature ID string. This is used to give the feature a well known ID for reference. /// public const string k_SpaceWarpFeatureId = "com.unity.openxr.feature.spacewarp"; #if SPACEWARP_RIGHT_HAND_NDC_SUPPORTED /// /// SpaceWarp NDC space handedness (default is false, for left-handed NDC space). /// [SerializeField] [Tooltip("Check this box if motion vector uses right-handed normalized device coordinates")] internal bool m_UseRightHandedNDC = false; /// /// Selects the NDC space handedness for SpaceWarp. /// If true, motion vector data must use the right-handed NDC space. /// If false, motion vector data must use the left-handed NDC space. /// public bool useRightHandedNDC { get => m_UseRightHandedNDC; set { if (OpenXRLoaderBase.Instance != null) { bool success = Internal_SetSpaceWarpRightHandedNDC(value); if (success) m_UseRightHandedNDC = value; } } } #endif // SPACEWARP_RIGHT_HAND_NDC_SUPPORTED /// protected internal override bool OnInstanceCreate(ulong xrInstance) { return OpenXRRuntime.IsExtensionEnabled(k_OpenXRRequestedExtensions) #if SPACEWARP_RIGHT_HAND_NDC_SUPPORTED && Internal_SetSpaceWarpRightHandedNDC(m_UseRightHandedNDC) #endif // SPACEWARP_RIGHT_HAND_NDC_SUPPORTED && base.OnInstanceCreate(xrInstance); } /// /// Enable or disable SpaceWarp. /// /// The flag to enable or disable SpaceWarp. /// Always returns . public static bool SetSpaceWarp(bool enabled) { return MetaSetSpaceWarp(enabled) == XrResult.Success; } /// /// Update SpaceWarp with the camera's position. /// /// Camera's position as a . /// Always returns . /// /// Depending on the headset, SpaceWarp may not need to be updated with the main camera’s position. /// Refer to the headset’s specifications to determine if it’s required. /// public static bool SetAppSpacePosition(Vector3 position) { return MetaSetAppSpacePosition(position.x, position.y, position.z) == XrResult.Success; } /// /// Update SpaceWarp with the camera's rotation. /// /// Camera's rotation as a . /// Always returns . /// /// Depending on the headset, SpaceWarp may not need to be updated with the main camera’s rotation. /// Refer to the headset’s specifications to determine if it’s required. /// public static bool SetAppSpaceRotation(Quaternion rotation) { return MetaSetAppSpaceRotation(rotation.x, rotation.y, rotation.z, rotation.w) == XrResult.Success; } #if UNITY_EDITOR && SPACEWARP_RIGHT_HAND_NDC_SUPPORTED [CustomEditor(typeof(SpaceWarpFeature))] internal class SpaceWarpFeatureSettingsEditor : Editor { private SerializedProperty useRightHandedNDC; static GUIContent s_UseRightHandedNDC = EditorGUIUtility.TrTextContent("Use Right Handed NDC"); void OnEnable() { useRightHandedNDC = serializedObject.FindProperty("m_UseRightHandedNDC"); } public override void OnInspectorGUI() { serializedObject.Update(); EditorGUILayout.PropertyField(useRightHandedNDC, s_UseRightHandedNDC); serializedObject.ApplyModifiedProperties(); } } #endif // UNITY_EDITOR && SPACEWARP_RIGHT_HAND_NDC_SUPPORTED // Import functions from the UnityOpenXR dll private const string k_Library = "UnityOpenXR"; [DllImport(k_Library, EntryPoint = "MetaSetSpaceWarp")] private static extern XrResult MetaSetSpaceWarp([MarshalAs(UnmanagedType.I1)] bool enabled); [DllImport(k_Library, EntryPoint = "MetaSetAppSpacePosition")] private static extern XrResult MetaSetAppSpacePosition(float x, float y, float z); [DllImport(k_Library, EntryPoint = "MetaSetAppSpaceRotation")] private static extern XrResult MetaSetAppSpaceRotation(float x, float y, float z, float w); #if SPACEWARP_RIGHT_HAND_NDC_SUPPORTED [DllImport(k_Library, EntryPoint = "NativeConfig_SetSpaceWarpRightHandedNDC")] [return: MarshalAs(UnmanagedType.U1)] private static extern bool Internal_SetSpaceWarpRightHandedNDC([MarshalAs(UnmanagedType.I1)] bool useRightHandedNDC); #endif // SPACEWARP_RIGHT_HAND_NDC_SUPPORTED } }