Esempio n. 1
0
        public void Refresh()
        {
            m_sortedDevices.Clear();

            UnmappingAll();

            MappingRoleIfUnbound(BodyRole.Head, 0u);

            // get related poses and record controller/tracker devices
            var hmdPose = VivePose.GetPose(0u);

            // preserve only y-axis rotation
            hmdPose.rot = Quaternion.Euler(0f, hmdPose.rot.eulerAngles.y, 0f);
            // move center to half height
            hmdPose.pos = Vector3.Scale(hmdPose.pos, new Vector3(1f, 0.5f, 1f));
            var halfHeight        = hmdPose.pos.y;
            var centerPoseInverse = hmdPose.GetInverse();

            for (uint i = 1; i < VRModule.MAX_DEVICE_COUNT; ++i)
            {
                if (!IsTrackingDevice(i))
                {
                    continue;
                }

                var relatedCenterPos = centerPoseInverse.InverseTransformPoint(VRModule.GetCurrentDeviceState(i).pose.pos);
                m_directionPoint[i] = HandRoleHandler.GetDirectionPoint(new Vector2(relatedCenterPos.x, -relatedCenterPos.y));
                m_distanceSqr[i]    = relatedCenterPos.sqrMagnitude / (halfHeight * halfHeight);

                m_sortedDevices.Add(i);
            }

            if (m_sortedDevices.Count == 0)
            {
                return;
            }

            var index = m_sortedDevices.Count - 1; // pointing last index

            // find 2 feet, should be most farest 2 devices
            m_sortedDevices.Sort(CompareDistance);
            if (IsFoot(m_sortedDevices[index]))
            {
                if (m_sortedDevices.Count <= 1)
                {
                    MappingRoleIfUnbound(BodyRole.RightFoot, m_sortedDevices[index]);
                    return;
                }

                if (!IsFoot(m_sortedDevices[index - 1]))
                {
                    // only 1 foot found
                    MappingRoleIfUnbound(BodyRole.RightFoot, m_sortedDevices[index]);
                    m_sortedDevices.RemoveAt(index--);
                    if (index < 0)
                    {
                        return;
                    }
                }
                else
                {
                    // 2 feet found, determine lef/right foot
                    if (m_directionPoint[m_sortedDevices[index]] < m_directionPoint[m_sortedDevices[index - 1]])
                    {
                        MappingRoleIfUnbound(BodyRole.RightFoot, m_sortedDevices[index]);
                        MappingRoleIfUnbound(BodyRole.LeftFoot, m_sortedDevices[index - 1]);
                    }
                    else
                    {
                        MappingRoleIfUnbound(BodyRole.RightFoot, m_sortedDevices[index - 1]);
                        MappingRoleIfUnbound(BodyRole.LeftFoot, m_sortedDevices[index]);
                    }

                    m_sortedDevices.RemoveAt(index--);
                    m_sortedDevices.RemoveAt(index--);
                    if (index < 0)
                    {
                        return;
                    }
                }
            }

            // find 2 hands, should be most left and most right device
            m_sortedDevices.Sort(CompareDirection);

            // right most device as right hand
            MappingRoleIfUnbound(BodyRole.RightHand, m_sortedDevices[0]);
            if (m_sortedDevices.Count == 1)
            {
                return;
            }

            // left most device as left hand
            MappingRoleIfUnbound(BodyRole.LeftHand, m_sortedDevices[index]);
            if (m_sortedDevices.Count == 2)
            {
                return;
            }

            // middle one as hip
            MappingRoleIfUnbound(BodyRole.Hip, m_sortedDevices[index / 2]);
        }
        public void Refresh()
        {
            // find right/left controller index
            var rightIndex = VRModule.INVALID_DEVICE_INDEX;
            var leftIndex  = VRModule.INVALID_DEVICE_INDEX;

            if (RoleMap.IsRoleValueBound((int)ControllerRole.RightHand))
            {
                rightIndex = RoleMap.GetMappedDeviceByRoleValue((int)ControllerRole.RightHand);
            }
            else
            {
                var index = VRModule.GetRightControllerDeviceIndex();
                if (VRModule.GetDeviceState(index).deviceClass == VRModuleDeviceClass.Controller)
                {
                    rightIndex = index;
                }
            }
            if (RoleMap.IsRoleValueBound((int)ControllerRole.LeftHand))
            {
                leftIndex = RoleMap.GetMappedDeviceByRoleValue((int)ControllerRole.LeftHand);
            }
            else
            {
                var index = VRModule.GetRightControllerDeviceIndex();
                if (VRModule.GetDeviceState(index).deviceClass == VRModuleDeviceClass.Controller)
                {
                    leftIndex = index;
                }
            }
            if (leftIndex == rightIndex)
            {
                leftIndex = VRModule.INVALID_DEVICE_INDEX;
            }

            // find all other unbound controller class devices
            for (uint i = 0u, imax = VRModule.GetDeviceStateCount(); i < imax; ++i)
            {
                if (i == rightIndex)
                {
                    continue;
                }
                if (i == leftIndex)
                {
                    continue;
                }

                var device = VRModule.GetDeviceState(i);
                if (!device.isConnected)
                {
                    continue;
                }
                if (device.deviceClass != VRModuleDeviceClass.Controller)
                {
                    continue;
                }
                if (RoleMap.IsDeviceBound(device.serialNumber))
                {
                    continue;
                }

                m_sortedDeviceList.Add(i);
            }

            // if module didn't hint left/right controllers
            // find left/right most device in m_sortedDeviceList and assigned to leftIndex/rightIndex
            if (m_sortedDeviceList.Count > 0)
            {
                HandRoleHandler.SortDeviceIndicesByDirection(m_sortedDeviceList, VRModule.GetCurrentDeviceState(VRModule.HMD_DEVICE_INDEX).pose);

                if (rightIndex == VRModule.INVALID_DEVICE_INDEX && !RoleMap.IsRoleBound(ControllerRole.RightHand))
                {
                    rightIndex = m_sortedDeviceList[0];
                    m_sortedDeviceList.RemoveAt(0);
                }

                if (m_sortedDeviceList.Count > 0 && leftIndex == VRModule.INVALID_DEVICE_INDEX && !RoleMap.IsRoleBound(ControllerRole.RightHand))
                {
                    leftIndex = m_sortedDeviceList[m_sortedDeviceList.Count - 1];
                    m_sortedDeviceList.RemoveAt(m_sortedDeviceList.Count - 1);
                }
            }

            if (rightIndex != VRModule.INVALID_DEVICE_INDEX)
            {
                MappingRoleIfUnbound(ControllerRole.RightHand, rightIndex);
            }

            if (leftIndex != VRModule.INVALID_DEVICE_INDEX)
            {
                MappingRoleIfUnbound(ControllerRole.LeftHand, leftIndex);
            }

            if (m_sortedDeviceList.Count > 0)
            {
                var otherCtrlIndex = -1;
                var otherRole      = ControllerRole.Controller3 - 1;
                while (NextUnmappedSortedDevice(ref otherCtrlIndex) && NextUnmappedRole(ref otherRole))
                {
                    MappingRole(otherRole, m_sortedDeviceList[otherCtrlIndex]);
                }

                m_sortedDeviceList.Clear();
            }
        }