示例#1
0
    void Update()
    {
        List <XRNodeState> nodes = new List <XRNodeState>();

        InputTracking.GetNodeStates(nodes);

        bool notfound = true;

        foreach (XRNodeState node in nodes)
        {
            string name = InputTracking.GetNodeName(node.uniqueID);
            if (name.Contains("OpenVR Controller") && name.Contains(hand))
            {
                notfound = false;
                Vector3    pos;
                Quaternion ang;
                bool       ok = node.TryGetPosition(out pos);
                ok &= node.TryGetRotation(out ang);
                if (ok)
                {
                    transform.localPosition = pos;
                    transform.localRotation = ang;
                }
            }
        }

        if (notfound)
        {
            print("Couldn't find controller. You must be in OpenVR (not Oculus) mode.");
        }
    }
        private Actor CreateActor(XRNodeState state)
        {
            // some names are a bit too complex
            String name = InputTracking.GetNodeName(state.uniqueID);

            name = name.Replace("Windows Mixed Reality", "WMR");
            name = name.Replace(" ", "").Replace("-", "");

            // create actor
            Actor actor = new Actor(scene, name);

            actor.bones    = new Bone[1];
            actor.bones[0] = new Bone(actor, "root", 0);
            scene.actors.Add(actor);
            actors.Add(state.uniqueID, actor);
            return(actor);
        }
示例#3
0
            public string toString()
            {
                string a = "";

                if (xRNodeStates.Count > 0)
                {
                    for (int i = 0; i < xRNodeStates.Count; i++)
                    {
                        Vector3 position;
                        xRNodeStates[i].TryGetPosition(out position);
                        a += "\nName =" + InputTracking.GetNodeName(xRNodeStates[i].uniqueID) + "Type = " + xRNodeStates[i].nodeType + " position = " + position.ToString();
                    }
                }
                else
                {
                    a = "Failed to get Input Tracking\n";
                }
                return(a);
            }
        private void UpdateTrackedDevice(TrackedDeviceState deviceState, XRNodeState?possibleNodeState, string use)
        {
            if (possibleNodeState == null)
            {
                if (deviceState.Found)
                {
                    deviceState.Position  = default;
                    deviceState.Rotation  = default;
                    deviceState.Found     = false;
                    deviceState.NodeState = default;
                    Plugin.logger.Info($"Lost device with ID {deviceState.NodeState.uniqueID} that was used as {use}");
                    deviceRemoved?.Invoke(deviceState);
                }

                return;
            }

            var   nodeState  = (XRNodeState)possibleNodeState;
            ulong previousId = deviceState.NodeState.uniqueID;

            Vector3    origin         = BeatSaberUtil.GetRoomCenter();
            Quaternion originRotation = BeatSaberUtil.GetRoomRotation();

            if (nodeState.TryGetPosition(out Vector3 position))
            {
                deviceState.Position = origin + originRotation * position;
            }

            if (nodeState.TryGetRotation(out Quaternion rotation))
            {
                deviceState.Rotation = originRotation * rotation;
            }

            deviceState.Found     = true;
            deviceState.NodeState = nodeState;

            if (nodeState.uniqueID != previousId)
            {
                Plugin.logger.Info($"Using device \"{InputTracking.GetNodeName(nodeState.uniqueID)}\" ({nodeState.uniqueID}) as {use}");
                deviceAdded?.Invoke(deviceState);
            }
        }
示例#5
0
        private void AssignTrackedDevice(TrackedDeviceState deviceState, XRNodeState?possibleNodeState, string use)
        {
            if (possibleNodeState.HasValue && !deviceState.found)
            {
                XRNodeState nodeState = possibleNodeState.Value;

                Plugin.logger.Info($"Using device \"{InputTracking.GetNodeName(nodeState.uniqueID)}\" ({nodeState.uniqueID}) as {use}");

                deviceState.uniqueID = nodeState.uniqueID;
                deviceState.found    = true;

                deviceAdded?.Invoke(deviceState);
            }

            if (!possibleNodeState.HasValue && deviceState.found)
            {
                Plugin.logger.Info($"Lost device with ID {deviceState.uniqueID} that was used as {use}");

                deviceState.uniqueID = default;
                deviceState.found    = false;

                deviceRemoved?.Invoke(deviceState);
            }
        }
        private bool TryGetAndTouchNodeDeviceIndex(XRNodeState nodeState, out uint deviceIndex)
        {
            // only tracking certain type of node (some nodes share same uniqueID)
            if (!IsTrackingDeviceNode(nodeState))
            {
                deviceIndex = INVALID_DEVICE_INDEX; return(false);
            }
            //Debug.Log(Time.frameCount + " TryGetNodeDeviceIndex " + nodeState.nodeType + " tracked=" + nodeState.tracked + " id=" + nodeState.uniqueID + " name=" + (InputTracking.GetNodeName(nodeState.uniqueID) ?? string.Empty));
            if (!m_node2Index.TryGetValue(nodeState.uniqueID, out deviceIndex))
            {
                // FIXME: 0ul is invalid id?
                if (nodeState.uniqueID == 0ul)
                {
                    return(false);
                }

                var validIndexFound = false;

                if (nodeState.nodeType == XRNode.Head)
                {
                    if (m_index2nodeValidity[0])
                    {
                        //Debug.LogWarning("[" + Time.frameCount + "] Multiple Head node found! drop node id:" + nodeState.uniqueID.ToString("X8") + " type:" + nodeState.nodeType + " name:" + InputTracking.GetNodeName(nodeState.uniqueID) + " tracked=" + nodeState.tracked);
                        deviceIndex = INVALID_DEVICE_INDEX;
                        return(false);
                    }

                    validIndexFound         = true;
                    m_index2nodeID[0]       = nodeState.uniqueID;
                    m_index2nodeValidity[0] = true;
                    m_node2Index.Add(nodeState.uniqueID, 0u);
                    deviceIndex = 0;
                }
                else
                {
                    for (uint i = 1u, imax = (uint)m_index2nodeValidity.Length; i < imax; ++i)
                    {
                        if (!m_index2nodeValidity[i])
                        {
                            validIndexFound         = true;
                            m_index2nodeID[i]       = nodeState.uniqueID;
                            m_index2nodeValidity[i] = true;
                            m_node2Index.Add(nodeState.uniqueID, i);
                            deviceIndex = i;

                            break;
                        }
                    }
                }

                if (!validIndexFound)
                {
                    Debug.LogWarning("[" + Time.frameCount + "] XRNode added, but device index out of range, drop the node id:" + nodeState.uniqueID.ToString("X8") + " type:" + nodeState.nodeType + " name:" + InputTracking.GetNodeName(nodeState.uniqueID) + " tracked=" + nodeState.tracked);
                    deviceIndex = INVALID_DEVICE_INDEX;
                    return(false);
                }

                //Debug.Log("[" + Time.frameCount + "] Add node device index [" + deviceIndex + "] id=" + nodeState.uniqueID.ToString("X8") + " type=" + nodeState.nodeType + " tracked=" + nodeState.tracked);
            }

            m_index2nodeTouched[deviceIndex] = true;
            return(true);
        }
示例#7
0
 public virtual string GetNodeName(XRNodeState node)
 {
     return(InputTracking.GetNodeName(node.uniqueID));
 }
        public void Update()
        {
            var nodeStates        = new List <XRNodeState>();
            var unassignedDevices = new Queue <XRNodeState>();
            var serialNumbers     = new string[0];

            InputTracking.GetNodeStates(nodeStates);

            if (OpenVRActionManager.isRunning)
            {
                serialNumbers = OpenVRWrapper.GetTrackedDeviceSerialNumbers();
            }

            XRNodeState?headNodeState      = null;
            XRNodeState?leftHandNodeState  = null;
            XRNodeState?rightHandNodeState = null;
            XRNodeState?leftFootNodeState  = null;
            XRNodeState?rightFootNodeState = null;
            XRNodeState?waistNodeState     = null;

            int trackerCount = 0;

            foreach (XRNodeState nodeState in nodeStates)
            {
                if (!_foundDevices.Contains(nodeState.uniqueID))
                {
                    _foundDevices.Add(nodeState.uniqueID);
                    Plugin.logger.Debug($"Found new XR device of type \"{nodeState.nodeType}\" named \"{InputTracking.GetNodeName(nodeState.uniqueID)}\" with ID {nodeState.uniqueID}");
                }

                switch (nodeState.nodeType)
                {
                case XRNode.CenterEye:
                    headNodeState = nodeState;
                    break;

                case XRNode.LeftHand:
                    leftHandNodeState = nodeState;
                    break;

                case XRNode.RightHand:
                    rightHandNodeState = nodeState;
                    break;

                case XRNode.HardwareTracker:
                    if (OpenVRActionManager.isRunning)
                    {
                        // try to figure out tracker role using OpenVR
                        string deviceName     = InputTracking.GetNodeName(nodeState.uniqueID);
                        int    openVRDeviceId = Array.FindIndex(serialNumbers, s => !string.IsNullOrEmpty(s) && deviceName.Contains(s));
                        var    role           = TrackedDeviceType.Unknown;

                        if (openVRDeviceId != -1)
                        {
                            role = OpenVRWrapper.GetTrackedDeviceType((uint)openVRDeviceId);
                        }

                        switch (role)
                        {
                        case TrackedDeviceType.LeftFoot:
                            leftFootNodeState = nodeState;
                            break;

                        case TrackedDeviceType.RightFoot:
                            rightFootNodeState = nodeState;
                            break;

                        case TrackedDeviceType.Waist:
                            waistNodeState = nodeState;
                            break;

                        default:
                            unassignedDevices.Enqueue(nodeState);
                            break;
                        }
                    }
                    else
                    {
                        unassignedDevices.Enqueue(nodeState);
                    }

                    trackerCount++;

                    break;
                }
            }

            // fallback if OpenVR tracker roles aren't set/supported
            if (leftFootNodeState == null && trackerCount >= 2 && unassignedDevices.Count > 0)
            {
                leftFootNodeState = unassignedDevices.Dequeue();
            }

            if (rightFootNodeState == null && trackerCount >= 2 && unassignedDevices.Count > 0)
            {
                rightFootNodeState = unassignedDevices.Dequeue();
            }

            if (waistNodeState == null && unassignedDevices.Count > 0)
            {
                waistNodeState = unassignedDevices.Dequeue();
            }

            UpdateTrackedDevice(head, headNodeState, nameof(head));
            UpdateTrackedDevice(leftHand, leftHandNodeState, nameof(leftHand));
            UpdateTrackedDevice(rightHand, rightHandNodeState, nameof(rightHand));
            UpdateTrackedDevice(leftFoot, leftFootNodeState, nameof(leftFoot));
            UpdateTrackedDevice(rightFoot, rightFootNodeState, nameof(rightFoot));
            UpdateTrackedDevice(waist, waistNodeState, nameof(waist));

            foreach (ulong id in _foundDevices.ToList())
            {
                if (!nodeStates.Exists(n => n.uniqueID == id))
                {
                    Plugin.logger.Debug($"Lost XR device with ID " + id);
                    _foundDevices.Remove(id);
                }
            }
        }
示例#9
0
 private void InputTracking_nodeAdded(XRNodeState obj)
 {
     AddDevice(InputTracking.GetNodeName(obj.uniqueID));
 }
示例#10
0
 private void InputTracking_nodeRemoved(XRNodeState obj)
 {
     Debug.Log("node removed: " + obj.uniqueID + "-" + InputTracking.GetNodeName(obj.uniqueID));
     RemoveDevice(InputTracking.GetNodeName(obj.uniqueID));
 }
示例#11
0
        static VRControllerType GetActiveControllersType()
        {
            var nodeStates = new List <XRNodeState>();

            InputTracking.GetNodeStates(nodeStates);

            var controllers = nodeStates
                              .Where(node => node.nodeType == XRNode.LeftHand || node.nodeType == XRNode.RightHand)
                              .Select(node => InputTracking.GetNodeName(node.uniqueID))
                              .Where(controller => !string.IsNullOrWhiteSpace(controller));

            foreach (var controller in controllers)
            {
                /*
                 * Known Vive controller names:
                 *   Vive. Controller MV
                 *   Vive. Controller DVT
                 */
                if (controller.IndexOf(@"Vive", StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    return(VRControllerType.Vive);
                }

                /*
                 * Known Oculus controller names:
                 *   Oculus Rift CV1
                 */
                if (controller.IndexOf(@"Oculus Rift", StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    return(VRControllerType.Touch);
                }

                /*
                 * Known Oculus Store controller names:
                 *   Oculus Touch Controller
                 */
                if (controller.IndexOf(@"Oculus Touch", StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    return(VRControllerType.OculusStoreTouch);
                }

                /*
                 * Known WMR controller names:
                 *   WindowsMR: 0x045e/0x065b/0/2
                 */
                if (controller.IndexOf(@"WindowsMR", StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    return(VRControllerType.WMR);
                }

                /*
                 * Known Knuckles controller names:
                 *   Knuckles EV1.3
                 *   Knuckles EV3.0
                 */
                if (controller.IndexOf(@"Knuckles", StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    return(VRControllerType.Knuckles);
                }

                Plugin.Log.Error("Discovering controller: " + controller.ToString() + "failed! please open an issue with this log statement"
                                 );
            }

            return(VRControllerType.Unknown);
        }
示例#12
0
        private bool TryGetNodeDeviceIndex(VRNodeState nodeState, out uint deviceIndex)
        {
            // only tracking certain type of node (some nodes share same uniqueID)
            if (!IsTrackingDeviceNode(nodeState))
            {
                deviceIndex = 0; return(false);
            }
            //Debug.Log(Time.frameCount + " TryGetNodeDeviceIndex " + nodeState.nodeType + " tracked=" + nodeState.tracked + " id=" + nodeState.uniqueID + " name=" + (InputTracking.GetNodeName(nodeState.uniqueID) ?? string.Empty));
            if (!m_node2Index.TryGetValue(nodeState.uniqueID, out deviceIndex))
            {
                // FIXME: 0ul is invalid id?
                if (nodeState.uniqueID == 0ul)
                {
                    return(false);
                }

                var validIndexFound = false;

                if (nodeState.nodeType == VRNode.Head)
                {
                    if (m_nodeStatesValid[0])
                    {
                        //Debug.LogWarning("[" + Time.frameCount + "] Multiple Head node found! drop node id:" + nodeState.uniqueID.ToString("X8") + " type:" + nodeState.nodeType + " name:" + InputTracking.GetNodeName(nodeState.uniqueID) + " tracked=" + nodeState.tracked);
                        return(false);
                    }

                    validIndexFound      = true;
                    m_nodeStatesValid[0] = true;
                    m_node2Index.Add(nodeState.uniqueID, 0u);
                    deviceIndex = 0;
                }
                else
                {
                    for (uint i = 1; i < MAX_DEVICE_COUNT; ++i)
                    {
                        if (!m_nodeStatesValid[i])
                        {
                            validIndexFound      = true;
                            m_nodeStatesValid[i] = true;
                            m_node2Index.Add(nodeState.uniqueID, i);

                            switch (nodeState.nodeType)
                            {
                            case VRNode.RightHand: m_rightIndex = i; break;

                            case VRNode.LeftHand: m_leftIndex = i; break;
                            }

                            deviceIndex = i;

                            break;
                        }
                    }
                }

                if (!validIndexFound)
                {
                    Debug.LogWarning("[" + Time.frameCount + "] VRNode added, but device index out of range, drop the node id:" + nodeState.uniqueID.ToString("X8") + " type:" + nodeState.nodeType + " name:" + InputTracking.GetNodeName(nodeState.uniqueID) + " tracked=" + nodeState.tracked);
                    return(false);
                }

                //Debug.Log("[" + Time.frameCount + "] Add node device index [" + deviceIndex + "] id=" + nodeState.uniqueID.ToString("X8") + " type=" + nodeState.nodeType + " tracked=" + nodeState.tracked);
            }

            return(true);
        }
    void ShowNodeNames()
    {
        string displayTextAccumulator = "";
        int    nodeNumber             = 0;

        List <XRNodeState> nodeStates = new List <XRNodeState>();

        InputTracking.GetNodeStates(nodeStates);

        foreach (XRNodeState node in nodeStates)
        {
            displayTextAccumulator += ("<" + nodeNumber + "> " + node.nodeType + " - \"" + InputTracking.GetNodeName(node.uniqueID) + "\"\n");
            nodeNumber++;
        }
        nodeNamesText.text = displayTextAccumulator;
    }