// Ximmerse Controller|SDK_Ximmerse|004 namespace VRTK { #if VRTK_DEFINE_SDK_XIMMERSE using UnityEngine; using System.Collections.Generic; using Ximmerse.InputSystem; using Ximmerse.VR; #endif /// /// The Ximmerse Controller SDK script provides a bridge to SDK methods that deal with the input devices. /// [SDK_Description(typeof(SDK_XimmerseSystem))] [SDK_Description(typeof(SDK_XimmerseSystem), 1)] public class SDK_XimmerseController #if VRTK_DEFINE_SDK_XIMMERSE : SDK_BaseController #else : SDK_FallbackController #endif { #if VRTK_DEFINE_SDK_XIMMERSE protected TrackedObject cachedLeftTrackedObject; protected TrackedObject cachedRightTrackedObject; protected Quaternion[] previousControllerRotations = new Quaternion[2]; protected Quaternion[] currentControllerRotations = new Quaternion[2]; protected Vector3[] previousControllerPositions = new Vector3[2]; protected Vector3[] currentControllerPositions = new Vector3[2]; protected bool[] previousHairTriggerState = new bool[2]; protected bool[] currentHairTriggerState = new bool[2]; protected bool[] previousHairGripState = new bool[2]; protected bool[] currentHairGripState = new bool[2]; protected float[] hairTriggerLimit = new float[2]; protected float[] hairGripLimit = new float[2]; /// /// The ProcessUpdate method enables an SDK to run logic for every Unity Update /// /// The reference for the controller. /// A dictionary of generic options that can be used to within the update. public override void ProcessUpdate(VRTK_ControllerReference controllerReference, Dictionary options) { if (controllerReference != null && controllerReference.IsValid()) { uint index = VRTK_ControllerReference.GetRealIndex(controllerReference); TrackedObject device = GetTrackedObject(controllerReference.actual); if (device != null) { previousControllerRotations[index] = currentControllerRotations[index]; currentControllerRotations[index] = device.transform.rotation; previousControllerPositions[index] = currentControllerPositions[index]; currentControllerPositions[index] = device.transform.position; } UpdateHairValues(index, GetButtonAxis(ButtonTypes.Trigger, controllerReference).x, GetButtonHairlineDelta(ButtonTypes.Trigger, controllerReference), ref previousHairTriggerState[index], ref currentHairTriggerState[index], ref hairTriggerLimit[index]); UpdateHairValues(index, GetButtonAxis(ButtonTypes.Grip, controllerReference).x, GetButtonHairlineDelta(ButtonTypes.Grip, controllerReference), ref previousHairGripState[index], ref currentHairGripState[index], ref hairGripLimit[index]); } } /// /// The ProcessFixedUpdate method enables an SDK to run logic for every Unity FixedUpdate /// /// The reference for the controller. /// A dictionary of generic options that can be used to within the fixed update. public override void ProcessFixedUpdate(VRTK_ControllerReference controllerReference, Dictionary options) { } /// /// The GetCurrentControllerType method returns the current used ControllerType based on the SDK and headset being used. /// /// The reference to the controller to get type of. /// The ControllerType based on the SDK and headset being used. public override ControllerType GetCurrentControllerType(VRTK_ControllerReference controllerReference = null) { return ControllerType.Ximmerse_Flip; } /// /// The GetControllerDefaultColliderPath returns the path to the prefab that contains the collider objects for the default controller of this SDK. /// /// The controller hand to check for /// A path to the resource that contains the collider GameObject. public override string GetControllerDefaultColliderPath(ControllerHand hand) { return "ControllerColliders/XimmerseCobra02"; } /// /// The GetControllerElementPath returns the path to the game object that the given controller element for the given hand resides in. /// /// The controller element to look up. /// The controller hand to look up. /// Whether to get the initial path or the full path to the element. /// A string containing the path to the game object that the controller element resides in. public override string GetControllerElementPath(ControllerElements element, ControllerHand hand, bool fullPath = false) { string suffix = (fullPath ? "" : ""); string handName = (hand == ControllerHand.Left) ? "cobra02-L" : "cobra02-R"; string path = handName + "/Dummy"; switch (element) { case ControllerElements.AttachPoint: return path + "/object_8"; case ControllerElements.Trigger: return path + "/object_2" + suffix; case ControllerElements.GripLeft: return path + "/object_5" + suffix; case ControllerElements.GripRight: return path + "/object_4" + suffix; case ControllerElements.Touchpad: return path + "/Cylinder001" + suffix; case ControllerElements.ButtonOne: return path + "/Cylinder002" + suffix; case ControllerElements.ButtonTwo: return path + "/Cylinder003" + suffix; case ControllerElements.SystemMenu: return path + "/Cylinder003" + suffix; case ControllerElements.Body: return path + "/object_1"; } return ""; } /// /// The GetControllerIndex method returns the index of the given controller. /// /// The GameObject containing the controller. /// The index of the given controller. public override uint GetControllerIndex(GameObject controller) { TrackedObject trackedObject = GetTrackedObject(controller); if (trackedObject == null) { return uint.MaxValue; } else { switch (trackedObject.source) { case ControllerType.LeftController: return 0; case ControllerType.RightController: return 1; } } return uint.MaxValue; } /// /// The GetControllerByIndex method returns the GameObject of a controller with a specific index. /// /// The index of the controller to find. /// If true it will return the actual controller, if false it will return the script alias controller GameObject. /// public override GameObject GetControllerByIndex(uint index, bool actual = false) { SetTrackedControllerCaches(); VRTK_SDKManager sdkManager = VRTK_SDKManager.instance; if (sdkManager != null) { if (cachedLeftTrackedObject != null && (uint)cachedLeftTrackedObject.controllerInput.handle == index) { return (actual ? sdkManager.loadedSetup.actualLeftController : sdkManager.scriptAliasLeftController); } if (cachedRightTrackedObject != null && (uint)cachedRightTrackedObject.controllerInput.handle == index) { return (actual ? sdkManager.loadedSetup.actualRightController : sdkManager.scriptAliasRightController); } } return null; } /// /// The GetControllerOrigin method returns the origin of the given controller. /// /// The reference to the controller to retrieve the origin from. /// A Transform containing the origin of the controller. public override Transform GetControllerOrigin(VRTK_ControllerReference controllerReference) { TrackedObject trackedObject = GetTrackedObject(controllerReference.actual); if (trackedObject != null) { return trackedObject.transform ? trackedObject.transform : trackedObject.transform.parent; } return null; } /// /// The GenerateControllerPointerOrigin method can create a custom pointer origin Transform to represent the pointer position and forward. /// /// A generated Transform that contains the custom pointer origin. [System.Obsolete("GenerateControllerPointerOrigin has been deprecated and will be removed in a future version of VRTK.")] public override Transform GenerateControllerPointerOrigin(GameObject parent) { return null; } /// /// The GetControllerLeftHand method returns the GameObject containing the representation of the left hand controller. /// /// If true it will return the actual controller, if false it will return the script alias controller GameObject. /// The GameObject containing the left hand controller. public override GameObject GetControllerLeftHand(bool actual = false) { GameObject controller = GetSDKManagerControllerLeftHand(actual); if (controller == null && actual) { controller = VRTK_SharedMethods.FindEvenInactiveGameObject("TrackingSpace/LeftHandAnchor", true); } return controller; } /// /// The GetControllerRightHand method returns the GameObject containing the representation of the right hand controller. /// /// If true it will return the actual controller, if false it will return the script alias controller GameObject. /// The GameObject containing the right hand controller. public override GameObject GetControllerRightHand(bool actual = false) { GameObject controller = GetSDKManagerControllerRightHand(actual); if (controller == null && actual) { controller = VRTK_SharedMethods.FindEvenInactiveGameObject("TrackingSpace/RightHandAnchor", true); } return controller; } /// /// The IsControllerLeftHand/1 method is used to check if the given controller is the the left hand controller. /// /// The GameObject to check. /// Returns true if the given controller is the left hand controller. public override bool IsControllerLeftHand(GameObject controller) { return CheckActualOrScriptAliasControllerIsLeftHand(controller); } /// /// The IsControllerRightHand/1 method is used to check if the given controller is the the right hand controller. /// /// The GameObject to check. /// Returns true if the given controller is the right hand controller. public override bool IsControllerRightHand(GameObject controller) { return CheckActualOrScriptAliasControllerIsRightHand(controller); } /// /// The IsControllerLeftHand/2 method is used to check if the given controller is the the left hand controller. /// /// The GameObject to check. /// If true it will check the actual controller, if false it will check the script alias controller. /// Returns true if the given controller is the left hand controller. public override bool IsControllerLeftHand(GameObject controller, bool actual) { return CheckControllerLeftHand(controller, actual); } /// /// The IsControllerRightHand/2 method is used to check if the given controller is the the right hand controller. /// /// The GameObject to check. /// If true it will check the actual controller, if false it will check the script alias controller. /// Returns true if the given controller is the right hand controller. public override bool IsControllerRightHand(GameObject controller, bool actual) { return CheckControllerRightHand(controller, actual); } /// /// The WaitForControllerModel method determines whether the controller model for the given hand requires waiting to load in on scene start. /// /// The hand to determine if the controller model will be ready for. /// Returns true if the controller model requires loading in at runtime and therefore needs waiting for. Returns false if the controller model will be available at start. public override bool WaitForControllerModel(ControllerHand hand) { return false; } /// /// The GetControllerModel method returns the model alias for the given GameObject. /// /// The GameObject to get the model alias for. /// The GameObject that has the model alias within it. public override GameObject GetControllerModel(GameObject controller) { return GetControllerModelFromController(controller); } /// /// The GetControllerModel method returns the model alias for the given controller hand. /// /// The hand enum of which controller model to retrieve. /// The GameObject that has the model alias within it. public override GameObject GetControllerModel(ControllerHand hand) { GameObject model = GetSDKManagerControllerModelForHand(hand); if (model == null) { GameObject controller = null; switch (hand) { case ControllerHand.Left: controller = GetControllerLeftHand(true); break; case ControllerHand.Right: controller = GetControllerRightHand(true); break; } if (controller != null) { model = controller; } } return model; } /// /// The GetControllerRenderModel method gets the game object that contains the given controller's render model. /// /// The reference to the controller to check. /// A GameObject containing the object that has a render model for the controller. public override GameObject GetControllerRenderModel(VRTK_ControllerReference controllerReference) { MeshRenderer renderModel = controllerReference.actual.GetComponentInChildren(); return (renderModel != null ? renderModel.gameObject : null); } /// /// The SetControllerRenderModelWheel method sets the state of the scroll wheel on the controller render model. /// /// The GameObject containing the controller render model. /// If true and the render model has a scroll wheen then it will be displayed, if false then the scroll wheel will be hidden. public override void SetControllerRenderModelWheel(GameObject renderModel, bool state) { } /// /// The HapticPulse/2 method is used to initiate a simple haptic pulse on the tracked object of the given controller reference. /// /// The reference to the tracked object to initiate the haptic pulse on. /// The intensity of the rumble of the controller motor. `0` to `1`. public override void HapticPulse(VRTK_ControllerReference controllerReference, float strength = 0.5f) { if (VRTK_ControllerReference.IsValid(controllerReference)) { uint index = VRTK_ControllerReference.GetRealIndex(controllerReference); int convertedStrength = Mathf.RoundToInt(1000f * strength); ControllerInput input = null; switch (index) { case 0: input = ControllerInputManager.instance.GetControllerInput(ControllerType.LeftController); break; case 1: input = ControllerInputManager.instance.GetControllerInput(ControllerType.RightController); break; } input.StartVibration(convertedStrength, 0.5f); } } /// /// The HapticPulse/2 method is used to initiate a haptic pulse based on an audio clip on the tracked object of the given controller reference. /// /// The reference to the tracked object to initiate the haptic pulse on. /// The audio clip to use for the haptic pattern. public override bool HapticPulse(VRTK_ControllerReference controllerReference, AudioClip clip) { //Ximmerse doesn't support audio haptics so return false to do a fallback. return false; } /// /// The GetHapticModifiers method is used to return modifiers for the duration and interval if the SDK handles it slightly differently. /// /// An SDK_ControllerHapticModifiers object with a given `durationModifier` and an `intervalModifier`. public override SDK_ControllerHapticModifiers GetHapticModifiers() { SDK_ControllerHapticModifiers modifiers = new SDK_ControllerHapticModifiers(); modifiers.durationModifier = 0.8f; modifiers.intervalModifier = 1f; return modifiers; } /// /// The GetVelocity method is used to determine the current velocity of the tracked object on the given controller reference. /// /// The reference to the tracked object to check for. /// A Vector3 containing the current velocity of the tracked object. public override Vector3 GetVelocity(VRTK_ControllerReference controllerReference) { if (!VRTK_ControllerReference.IsValid(controllerReference)) { return Vector3.zero; } uint index = VRTK_ControllerReference.GetRealIndex(controllerReference); ControllerInput device = GetControllerInputByIndex(index); if (device == null) { return Vector3.zero; } Vector3 deltaMovement = currentControllerPositions[index] - previousControllerPositions[index]; deltaMovement /= Time.deltaTime; return deltaMovement; } /// /// The GetAngularVelocity method is used to determine the current angular velocity of the tracked object on the given controller reference. /// /// The reference to the tracked object to check for. /// A Vector3 containing the current angular velocity of the tracked object. public override Vector3 GetAngularVelocity(VRTK_ControllerReference controllerReference) { if (!VRTK_ControllerReference.IsValid(controllerReference)) { return Vector3.zero; } uint index = VRTK_ControllerReference.GetRealIndex(controllerReference); Quaternion deltaRotation = currentControllerRotations[index] * Quaternion.Inverse(previousControllerRotations[index]); return new Vector3(Mathf.DeltaAngle(0, deltaRotation.eulerAngles.x), Mathf.DeltaAngle(0, deltaRotation.eulerAngles.y), Mathf.DeltaAngle(0, deltaRotation.eulerAngles.z)); } /// /// The IsTouchpadStatic method is used to determine if the touchpad is currently not being moved. /// /// /// /// /// Returns true if the touchpad is not currently being touched or moved. public override bool IsTouchpadStatic(bool isTouched, Vector2 currentAxisValues, Vector2 previousAxisValues, int compareFidelity) { return (!isTouched || VRTK_SharedMethods.Vector2ShallowCompare(currentAxisValues, previousAxisValues, compareFidelity)); } /// /// The GetButtonAxis method retrieves the current X/Y axis values for the given button type on the given controller reference. /// /// The type of button to check for the axis on. /// The reference to the controller to check the button axis on. /// A Vector2 of the X/Y values of the button axis. If no axis values exist for the given button, then a Vector2.Zero is returned. public override Vector2 GetButtonAxis(ButtonTypes buttonType, VRTK_ControllerReference controllerReference) { if (!VRTK_ControllerReference.IsValid(controllerReference)) { return Vector2.zero; } uint index = VRTK_ControllerReference.GetRealIndex(controllerReference); ControllerInput controllerInputDevice = GetControllerInputByIndex(index); TrackedObject trackedObjectDevice = GetTrackedObject(controllerReference.actual); switch (buttonType) { case ButtonTypes.Touchpad: if (controllerInputDevice != null) { Vector2 rawTouchpad = controllerInputDevice.GetTouchPos() - new Vector2(0.5f, 0.5f); return new Vector2(rawTouchpad.x, -rawTouchpad.y); } break; case ButtonTypes.Trigger: if (controllerInputDevice != null) { return new Vector2(controllerInputDevice.GetAxis(ControllerRawAxis.LeftTrigger), 0f); } break; case ButtonTypes.Grip: if (trackedObjectDevice != null) { return new Vector2((trackedObjectDevice.controllerInput.GetButton(DaydreamButton.Grip) ? 1f : 0f), 0f); } break; } return Vector2.zero; } /// /// The GetButtonSenseAxis method retrieves the current sense axis value for the given button type on the given controller reference. /// /// The type of button to check for the sense axis on. /// The reference to the controller to check the sense axis on. /// The current sense axis value. public override float GetButtonSenseAxis(ButtonTypes buttonType, VRTK_ControllerReference controllerReference) { return 0f; } /// /// The GetButtonHairlineDelta method is used to get the difference between the current button press and the previous frame button press. /// /// The type of button to get the hairline delta for. /// The reference to the controller to get the hairline delta for. /// The delta between the button presses. public override float GetButtonHairlineDelta(ButtonTypes buttonType, VRTK_ControllerReference controllerReference) { //TODO: This doesn't seem correct, surely this should be storing the previous button press value and getting the delta. return (VRTK_ControllerReference.IsValid(controllerReference) ? 0.1f : 0f); } /// /// The GetControllerButtonState method is used to determine if the given controller button for the given press type on the given controller reference is currently taking place. /// /// The type of button to check for the state of. /// The button state to check for. /// The reference to the controller to check the button state on. /// Returns true if the given button is in the state of the given press type on the given controller reference. public override bool GetControllerButtonState(ButtonTypes buttonType, ButtonPressTypes pressType, VRTK_ControllerReference controllerReference) { if (!VRTK_ControllerReference.IsValid(controllerReference)) { return false; } uint index = VRTK_ControllerReference.GetRealIndex(controllerReference); switch (buttonType) { case ButtonTypes.Trigger: return IsButtonPressed(index, pressType, ControllerRawButton.LeftTrigger); case ButtonTypes.TriggerHairline: if (pressType == ButtonPressTypes.PressDown) { return (currentHairTriggerState[index] && !previousHairTriggerState[index]); } else if (pressType == ButtonPressTypes.PressUp) { return (!currentHairTriggerState[index] && previousHairTriggerState[index]); } break; case ButtonTypes.Grip: return IsButtonPressed(index, pressType, ControllerRawButton.LeftShoulder) || IsButtonPressed(index, pressType, ControllerRawButton.RightShoulder); case ButtonTypes.GripHairline: if (pressType == ButtonPressTypes.PressDown) { return (currentHairGripState[index] && !previousHairGripState[index]); } else if (pressType == ButtonPressTypes.PressUp) { return (!currentHairGripState[index] && previousHairGripState[index]); } break; case ButtonTypes.Touchpad: return IsButtonPressed(index, pressType, ControllerRawButton.LeftThumb); case ButtonTypes.ButtonOne: return IsButtonPressed(index, pressType, ControllerRawButton.Back); case ButtonTypes.ButtonTwo: return IsButtonPressed(index, pressType, ControllerRawButton.Start); } return false; } protected override void Awake() { base.Awake(); SetTrackedControllerCaches(true); } protected virtual void OnTrackedDeviceRoleChanged(T ignoredArgument) { SetTrackedControllerCaches(true); } protected virtual void SetTrackedControllerCaches(bool forceRefresh = false) { if (forceRefresh) { cachedLeftTrackedObject = null; cachedRightTrackedObject = null; } VRTK_SDKManager sdkManager = VRTK_SDKManager.instance; if (sdkManager != null) { if (cachedLeftTrackedObject == null && sdkManager.loadedSetup.actualLeftController) { cachedLeftTrackedObject = sdkManager.loadedSetup.actualLeftController.GetComponent(); } if (cachedRightTrackedObject == null && sdkManager.loadedSetup.actualRightController) { cachedRightTrackedObject = sdkManager.loadedSetup.actualRightController.GetComponent(); } } } protected virtual TrackedObject GetTrackedObject(GameObject controller) { SetTrackedControllerCaches(); TrackedObject trackedObject = null; if (IsControllerLeftHand(controller)) { trackedObject = cachedLeftTrackedObject; } else if (IsControllerRightHand(controller)) { trackedObject = cachedRightTrackedObject; } return trackedObject; } protected virtual bool IsButtonPressed(uint index, ButtonPressTypes type, ControllerRawButton button) { ControllerInput device = GetControllerInputByIndex(index); if (device == null) { return false; } switch (type) { case ButtonPressTypes.Press: return device.GetButton(button); case ButtonPressTypes.PressDown: return device.GetButtonDown(button); case ButtonPressTypes.PressUp: return device.GetButtonUp(button); case ButtonPressTypes.Touch: return device.GetButton(button); case ButtonPressTypes.TouchDown: return device.GetButtonDown(button); case ButtonPressTypes.TouchUp: return device.GetButtonUp(button); } return false; } protected virtual void UpdateHairValues(uint index, float axisValue, float hairDelta, ref bool previousState, ref bool currentState, ref float hairLimit) { previousState = currentState; float value = axisValue; if (currentState) { if (value < (hairLimit - hairDelta) || value <= 0f) { currentState = false; } } else { if (value > (hairLimit + hairDelta) || value >= 1f) { currentState = true; } } hairLimit = (currentState ? Mathf.Max(hairLimit, value) : Mathf.Min(hairLimit, value)); } protected virtual ControllerInput GetControllerInputByIndex(uint index) { ControllerInput result = null; switch (index) { case 0: result = ControllerInputManager.instance.GetControllerInput(ControllerType.LeftController); break; case 1: result = ControllerInputManager.instance.GetControllerInput(ControllerType.RightController); break; } return result; } #endif } }