Esempio n. 1
0
        internal bool TryGetUncalibratedPose(DeviceUse use, out Pose pose)
        {
            if (!_deviceManager.TryGetDeviceState(use, out TrackedDevice device) || !device.isTracking)
            {
                pose = Pose.identity;
                return(false);
            }

            pose = new Pose(device.position, device.rotation);

            _trackingHelper.ApplyRoomAdjust(ref pose.position, ref pose.rotation);

            SpawnedAvatar spawnedAvatar = _avatarManager.currentlySpawnedAvatar;

            switch (use)
            {
            case DeviceUse.Head:
            case DeviceUse.LeftHand:
            case DeviceUse.RightHand:
                _trackingHelper.ApplyFloorOffset(spawnedAvatar, ref pose.position);
                break;

            default:
                _trackingHelper.ApplyTrackerFloorOffset(spawnedAvatar, ref pose.position);
                break;
            }

            return(true);
        }
Esempio n. 2
0
        public bool TryGetFingerCurl(DeviceUse use, out FingerCurl curl)
        {
            SkeletalInput handAnim;

            switch (use)
            {
            case DeviceUse.LeftHand:
                handAnim = _leftHandAnimAction;
                break;

            case DeviceUse.RightHand:
                handAnim = _rightHandAnimAction;
                break;

            default:
                throw new InvalidOperationException($"{nameof(TryGetFingerCurl)} only supports {nameof(DeviceUse.LeftHand)} and {nameof(DeviceUse.RightHand)}");
            }

            if (!handAnim.isActive || handAnim.summaryData == null)
            {
                curl = null;
                return(false);
            }

            curl = new FingerCurl(handAnim.summaryData.thumbCurl, handAnim.summaryData.indexCurl, handAnim.summaryData.middleCurl, handAnim.summaryData.ringCurl, handAnim.summaryData.littleCurl);
            return(true);
        }
Esempio n. 3
0
        public bool TryGetPose(DeviceUse use, out Pose pose)
        {
            switch (use)
            {
            case DeviceUse.Head:
                return(TryGetUncalibratedPose(use, out pose));

            case DeviceUse.LeftHand:
            case DeviceUse.RightHand:
                return(TryGetCalibratedHandPose(use, out pose));

            case DeviceUse.Waist:
                return(TryGetCalibratedWaistPose(out pose));

            case DeviceUse.LeftFoot:
                return(TryGetCalibratedLeftFootPose(out pose));

            case DeviceUse.RightFoot:
                return(TryGetCalibratedRightFootPose(out pose));

            default:
                pose = Pose.identity;
                return(false);
            }
        }
        public bool TryGetDeviceState(DeviceUse use, out TrackedDevice device)
        {
            switch (use)
            {
            case DeviceUse.Head:
                device = GetDevice(ref _head);
                return(true);

            case DeviceUse.LeftHand:
                device = GetDevice(ref _leftHand);
                return(true);

            case DeviceUse.RightHand:
                device = GetDevice(ref _rightHand);
                return(true);

            case DeviceUse.Waist:
                device = GetDevice(ref _waist);
                return(true);

            case DeviceUse.LeftFoot:
                device = GetDevice(ref _leftFoot);
                return(true);

            case DeviceUse.RightFoot:
                device = GetDevice(ref _rightFoot);
                return(true);

            default:
                device = default;
                return(false);
            }
        }
Esempio n. 5
0
 public TrackedDevice(string id, DeviceUse deviceUse, bool isTracking, Vector3 position, Quaternion rotation)
 {
     this.id         = id;
     this.deviceUse  = deviceUse;
     this.isTracking = isTracking;
     this.position   = position;
     this.rotation   = rotation;
 }
        #pragma warning restore IDE0051
        #endregion

        private void SetPose(DeviceUse use, Transform target)
        {
            if (!target || !_input.TryGetPose(use, out Pose pose))
            {
                return;
            }

            _trackingHelper.SetLocalPose(pose.position, pose.rotation, target, transform.parent);
        }
Esempio n. 7
0
        public bool TryGetPose(DeviceUse use, out Pose pose)
        {
            if (!_internalPlayerInput.TryGetPose(use, out pose))
            {
                return(false);
            }

            _trackingHelper.ApplyRoomAdjust(ref pose.position, ref pose.rotation);

            return(true);
        }
Esempio n. 8
0
        private bool TryGetCalibratedHandPose(DeviceUse use, out Pose pose)
        {
            if (!TryGetUncalibratedPose(use, out pose))
            {
                pose = Pose.identity;
                return(false);
            }

            _beatSaberUtilities.AdjustPlatformSpecificControllerPose(use, ref pose);

            return(true);
        }
        /// <summary>
        /// 工序相关设备是否剩余产能
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="pId"></param>
        /// <param name="capacityList"></param>
        /// <param name="productType">是否设备</param>
        /// <param name="deviceList">设备</param>
        /// <param name="operatorList">人员</param>
        /// <param name="waitIndex">等待消耗的产能</param>
        /// <returns>设备id或人员id  型号 剩余产能 / 流程工序  人员等级 人员id</returns>
        public Dictionary <int, Tuple <int, decimal> > ProcessLeftCapacityIndex(int categoryId, int pId,
                                                                                SmartCapacityListDetail capacityList,
                                                                                IEnumerable <SmartDevice> deviceList,
                                                                                IEnumerable <SmartOperator> operatorList,
                                                                                int productType,
                                                                                decimal waitIndex)
        {
            var defaultIndex = 1;
            //设备id或人员id  型号 剩余产能
            var result = new Dictionary <int, Tuple <int, decimal> >();

            if (productType == 0)
            {
                //设备类型  设备型号 设备id
                if (DeviceUse.ContainsKey(categoryId))
                {
                    var infos = DeviceUse[categoryId].Where(d => capacityList.DeviceList.Any(cd => cd.ModelId == d.Item));
                    foreach (var device in infos)
                    {
                        if (!result.ContainsKey(device.Id))
                        {
                            result.Add(device.Id, new Tuple <int, decimal>(device.Item, defaultIndex - waitIndex));
                        }
                        foreach (var arrange in device.Arranges)
                        {
                            result[device.Id] = new Tuple <int, decimal>(device.Item, result[device.Id].Item2 - arrange.CapacityIndex);
                        }
                    }
                }
            }
            else if (productType == 1)
            {
                //流程工序  人员等级 人员id
                if (OperatorUse.ContainsKey(pId))
                {
                    var infos = OperatorUse[pId].Where(d => capacityList.OperatorList.Any(cd => cd.LevelId == d.Item));
                    foreach (var op in infos)
                    {
                        if (!result.ContainsKey(op.Id))
                        {
                            result.Add(op.Id, new Tuple <int, decimal>(op.Item, defaultIndex - waitIndex));
                        }
                        foreach (var arrange in op.Arranges)
                        {
                            result[op.Id] = new Tuple <int, decimal>(op.Item, result[op.Id].Item2 - arrange.CapacityIndex);
                        }
                    }
                }
            }
            return(result.Where(x => x.Value.Item2 > 0).ToDictionary(y => y.Key, y => new Tuple <int, decimal>(y.Value.Item1, y.Value.Item2)));
        }
Esempio n. 10
0
        internal bool TryGetUncalibratedPoseForAvatar(DeviceUse use, SpawnedAvatar spawnedAvatar, out Pose pose)
        {
            if (!TryGetUncalibratedPose(use, out pose))
            {
                return(false);
            }

            if (spawnedAvatar)
            {
                _trackingHelper.ApplyLocalPose(ref pose.position, ref pose.rotation, spawnedAvatar.transform.parent);
            }

            return(true);
        }
Esempio n. 11
0
        private bool TryGetTrackerPose(DeviceUse use, Pose previousPose, Pose correction, Settings.TrackedPointSmoothing smoothing, out Pose pose)
        {
            if (!_shouldTrackFullBody || !_deviceManager.TryGetDeviceState(use, out TrackedDevice device) || !device.isTracking)
            {
                pose = Pose.identity;
                return(false);
            }

            Vector3    position = device.position + device.rotation * correction.position; // correction is forward-facing by definition
            Quaternion rotation = device.rotation * correction.rotation;

            _trackingHelper.ApplyRoomAdjust(ref position, ref rotation);
            _trackingHelper.ApplyTrackerFloorOffset(_avatarManager.currentlySpawnedAvatar, ref position);

            pose = new Pose(Vector3.Lerp(previousPose.position, position, smoothing.position), Quaternion.Slerp(previousPose.rotation, rotation, smoothing.rotation));

            return(true);
        }
        public void Init(IEnumerable <SmartDevice> deviceList, IEnumerable <SmartOperatorDetail> operatorList)
        {
            if (deviceList != null && deviceList.Any())
            {
                //设备类型
                var categoryIds = deviceList.GroupBy(x => x.CategoryId).Select(y => y.Key);
                foreach (var categoryId in categoryIds)
                {
                    //设备类型  设备型号 设备id
                    if (!DeviceUse.ContainsKey(categoryId))
                    {
                        DeviceUse.Add(categoryId, new List <ArrangeInfo>());
                    }

                    var devices = deviceList.Where(x => x.CategoryId == categoryId).OrderBy(y => y.Priority);
                    foreach (var device in devices)
                    {
                        DeviceUse[categoryId].Add(new ArrangeInfo(device.ModelId, device.Id));
                    }
                }
            }

            if (operatorList != null && operatorList.Any())
            {
                //流程工序
                var processIds = operatorList.GroupBy(x => x.ProcessId).Select(y => y.Key);
                foreach (var processId in processIds)
                {
                    //流程工序  人员等级 人员id
                    if (!OperatorUse.ContainsKey(processId))
                    {
                        OperatorUse.Add(processId, new List <ArrangeInfo>());
                    }

                    var operators = operatorList.Where(x => x.ProcessId == processId).OrderBy(y => y.Order).ThenBy(y => y.Priority);;
                    foreach (var op in operators)
                    {
                        OperatorUse[processId].Add(new ArrangeInfo(op.LevelId, op.Id));
                    }
                }
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Similar to the various implementations of <see cref="IVRPlatformHelper.AdjustControllerTransform(UnityEngine.XR.XRNode, Transform, Vector3, Vector3)"/> except it updates a pose instead of adjusting a transform.
        /// </summary>
        public void AdjustPlatformSpecificControllerPose(DeviceUse use, ref Pose pose)
        {
            if (use != DeviceUse.LeftHand && use != DeviceUse.RightHand)
            {
                return;
            }

            Vector3 position = _mainSettingsModel.controllerPosition;
            Vector3 rotation = _mainSettingsModel.controllerRotation;

            if (_vrPlatformHelper.vrPlatformSDK == VRPlatformSDK.Oculus)
            {
                rotation += new Vector3(-40f, 0f, 0f);
                position += new Vector3(0f, 0f, 0.055f);
            }
            else if (_vrPlatformHelper.vrPlatformSDK == VRPlatformSDK.OpenVR)
            {
                if (_vrControllerManufacturerNameGetter((OpenVRHelper)_vrPlatformHelper) == OpenVRHelper.VRControllerManufacturerName.Valve)
                {
                    rotation += new Vector3(-16.3f, 0f, 0f);
                    position += new Vector3(0f, 0.022f, -0.01f);
                }
                else
                {
                    rotation += new Vector3(-4.3f, 0f, 0f);
                    position += new Vector3(0f, -0.008f, 0f);
                }
            }

            // mirror across YZ plane for left hand
            if (use == DeviceUse.LeftHand)
            {
                rotation.y = -rotation.y;
                rotation.z = -rotation.z;

                position.x = -position.x;
            }

            pose.rotation *= Quaternion.Euler(rotation);
            pose.position += pose.rotation * position;
        }
Esempio n. 14
0
        public bool TryGetPose(DeviceUse use, out Pose pose)
        {
            switch (use)
            {
            case DeviceUse.Head:
                pose = head;
                return(true);

            case DeviceUse.RightHand:
                pose = rightHand;
                return(true);

            case DeviceUse.LeftHand:
                pose = leftHand;
                return(true);

            default:
                pose = Pose.identity;
                return(false);
            }
        }
        private void UpdateTrackedDevice(TrackedDeviceState deviceState, InputDevice?possibleInputDevice, DeviceUse use)
        {
            if (!possibleInputDevice.HasValue)
            {
                return;
            }

            var inputDevice = possibleInputDevice.Value;

            if (!inputDevice.TryGetFeatureValue(CommonUsages.isTracked, out bool isTracked) || !isTracked)
            {
                if (deviceState.tracked)
                {
                    Plugin.logger.Info($"Lost tracking of device \"{deviceState.name}\"");
                    deviceState.tracked = false;
                    deviceTrackingLost?.Invoke(deviceState, use);
                }

                return;
            }

            if (!deviceState.tracked)
            {
                Plugin.logger.Info($"Acquired tracking of device \"{deviceState.name}\"");
                deviceState.tracked = true;
                deviceTrackingAcquired?.Invoke(deviceState, use);
            }

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

            if (inputDevice.TryGetFeatureValue(CommonUsages.devicePosition, out Vector3 position))
            {
                deviceState.position = origin + originRotation * position;
            }

            if (inputDevice.TryGetFeatureValue(CommonUsages.deviceRotation, out Quaternion rotation))
            {
                deviceState.rotation = originRotation * rotation;

                // Driver4VR rotation correction
                if (deviceState.name?.StartsWith("d4vr_tracker_") == true && (use == DeviceUse.LeftFoot || �use == DeviceUse.RightFoot))
                {
                    deviceState.rotation *= Quaternion.Euler(-90, 180, 0);
                }

                // KinectToVR rotation correction
                if (deviceState.role == TrackedDeviceRole.KinectToVrTracker)
                {
                    if (use == DeviceUse.Waist)
                    {
                        deviceState.rotation *= Quaternion.Euler(-90, 180, 0);
                    }

                    if (use == DeviceUse.LeftFoot || use == DeviceUse.RightFoot)
                    {
                        deviceState.rotation *= Quaternion.Euler(0, 180, 0);
                    }
                }
            }
        }
Esempio n. 16
0
        public void GetDevices(Dictionary <string, TrackedDevice> devices)
        {
            devices.Clear();

            var  inputDevices   = new List <InputDevice>();
            bool changeDetected = false;

            InputDevices.GetDevices(inputDevices);

            foreach (InputDevice inputDevice in inputDevices)
            {
                if (!inputDevice.isValid)
                {
                    return;
                }

                DeviceUse use = DeviceUse.Unknown;

                if (inputDevice.characteristics.HasFlag(InputDeviceCharacteristics.HeadMounted))
                {
                    use = DeviceUse.Head;
                }
                else if (inputDevice.characteristics.HasFlag(InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Left))
                {
                    use = DeviceUse.LeftHand;
                }
                else if (inputDevice.characteristics.HasFlag(InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Right))
                {
                    use = DeviceUse.RightHand;
                }

                inputDevice.TryGetFeatureValue(CommonUsages.isTracked, out bool isTracked);
                inputDevice.TryGetFeatureValue(CommonUsages.devicePosition, out Vector3 position);
                inputDevice.TryGetFeatureValue(CommonUsages.deviceRotation, out Quaternion rotation);

                if (_inputDevices.ContainsKey(inputDevice.name))
                {
                    if (inputDevice.characteristics != _inputDevices[inputDevice.name].characteristics)
                    {
                        _logger.Info($"Characteristics of device '{inputDevice.name}' changed from {_inputDevices[inputDevice.name].characteristics} to {inputDevice.characteristics}");
                        changeDetected = true;
                    }

                    if (isTracked && (!_inputDevices[inputDevice.name].TryGetFeatureValue(CommonUsages.isTracked, out bool previouslyTracked) || isTracked != previouslyTracked))
                    {
                        if (isTracked)
                        {
                            _logger.Info($"Acquired tracking of device '{inputDevice.name}'");
                        }
                        else
                        {
                            _logger.Info($"Lost tracking of device '{inputDevice.name}'");
                        }

                        changeDetected = true;
                    }

                    _inputDevices[inputDevice.name] = inputDevice;
                }
                else
                {
                    _logger.Info($"Device '{inputDevice.name}' connected with characteristics {inputDevice.characteristics}");

                    _inputDevices.Add(inputDevice.name, inputDevice);

                    changeDetected = true;
                }

                devices.Add(inputDevice.name, new TrackedDevice(inputDevice.name, use, isTracked, position, rotation));
            }

            if (changeDetected)
            {
                devicesChanged?.Invoke();
            }
        }
 private void OnInputDevicesChanged(TrackedDeviceState state, DeviceUse use)
 {
     UpdateCalibrationButtons(_avatarManager.currentlySpawnedAvatar.avatar);
 }
Esempio n. 18
0
 public bool TryGetFingerCurl(DeviceUse use, out FingerCurl curl)
 {
     return(_internalPlayerInput.TryGetFingerCurl(use, out curl));
 }
Esempio n. 19
0
        public bool GetDevices(Dictionary <string, TrackedDevice> devices)
        {
            devices.Clear();

            var  inputDevices   = new List <InputDevice>();
            bool changeDetected = _deviceRemovedSinceLastCall;

            InputDevices.GetDevices(inputDevices);

            foreach (InputDevice inputDevice in inputDevices)
            {
                if (!inputDevice.isValid)
                {
                    continue;
                }

                DeviceUse use = DeviceUse.Unknown;

                if (inputDevice.characteristics.HasFlag(InputDeviceCharacteristics.HeadMounted))
                {
                    use = DeviceUse.Head;
                }
                else if (inputDevice.characteristics.HasFlag(InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Left))
                {
                    use = DeviceUse.LeftHand;
                }
                else if (inputDevice.characteristics.HasFlag(InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Right))
                {
                    use = DeviceUse.RightHand;
                }

                inputDevice.TryGetFeatureValue(CommonUsages.isTracked, out bool isTracked);
                inputDevice.TryGetFeatureValue(CommonUsages.devicePosition, out Vector3 position);
                inputDevice.TryGetFeatureValue(CommonUsages.deviceRotation, out Quaternion rotation);

                string id;

                if (_devices.TryGetValue(inputDevice.name, out UnityXRDevice existingDevice))
                {
                    id = existingDevice.id;

                    if (inputDevice.characteristics != existingDevice.characteristics)
                    {
                        _logger.Info($"Characteristics of device '{existingDevice.id}' changed from {existingDevice.characteristics} to {inputDevice.characteristics}");
                        changeDetected = true;
                    }

                    if (existingDevice.isTracked != isTracked)
                    {
                        if (isTracked)
                        {
                            _logger.Info($"Acquired tracking of device '{existingDevice.id}'");
                        }
                        else
                        {
                            _logger.Info($"Lost tracking of device '{existingDevice.id}'");
                        }

                        changeDetected = true;
                    }

                    _devices[inputDevice.name] = new UnityXRDevice(existingDevice.id, true, isTracked, inputDevice.characteristics);
                }
                else
                {
                    Match match = kSerialNumberRegex.Match(inputDevice.name);

                    if (match.Success)
                    {
                        id = match.Groups[1].Value + (uint)match.Groups[2].Value.GetHashCode() + match.Groups[3].Value;
                    }
                    else
                    {
                        id = inputDevice.name;
                    }

                    _logger.Info($"Device '{id}' connected with characteristics {inputDevice.characteristics}");

                    _devices.Add(inputDevice.name, new UnityXRDevice(id, true, isTracked, inputDevice.characteristics));

                    changeDetected = true;
                }

                devices.Add(inputDevice.name, new TrackedDevice(id, use, isTracked, position, rotation));
            }

            _deviceRemovedSinceLastCall = false;

            return(changeDetected);
        }
Esempio n. 20
0
        private void AssignTrackedDevice(TrackedDeviceState deviceState, InputDevice?possibleInputDevice, DeviceUse use, TrackedDeviceRole deviceRole)
        {
            if ((!possibleInputDevice.HasValue && deviceState.found) || (possibleInputDevice.HasValue && deviceState.found && possibleInputDevice.Value.name != deviceState.name))
            {
                _logger.Info($"Removing device '{deviceState.name}' that was used as {use}");

                deviceState.name         = null;
                deviceState.serialNumber = null;
                deviceState.found        = false;
                deviceState.tracked      = false;
                deviceState.role         = TrackedDeviceRole.Unknown;

                deviceRemoved?.Invoke(deviceState, use);
            }

            if (possibleInputDevice.HasValue && (!deviceState.found || possibleInputDevice.Value.name != deviceState.name))
            {
                InputDevice inputDevice = possibleInputDevice.Value;

                _logger.Info($"Using device '{inputDevice.name}' as {use}");

                deviceState.name         = inputDevice.name;
                deviceState.serialNumber = inputDevice.serialNumber;
                deviceState.found        = true;
                deviceState.role         = deviceRole;

                deviceAdded?.Invoke(deviceState, use);
            }
        }
        private void AssignTrackedDevice(TrackedDeviceState deviceState, InputDevice?possibleInputDevice, DeviceUse use, TrackedDeviceRole deviceRole)
        {
            if (possibleInputDevice.HasValue && !deviceState.found)
            {
                InputDevice inputDevice = possibleInputDevice.Value;

                Plugin.logger.Info($"Using device \"{inputDevice.name}\" as {use}");

                deviceState.name         = inputDevice.name;
                deviceState.serialNumber = inputDevice.serialNumber;
                deviceState.found        = true;
                deviceState.role         = deviceRole;

                deviceAdded?.Invoke(deviceState, use);
            }

            if (!possibleInputDevice.HasValue && deviceState.found)
            {
                Plugin.logger.Info($"Lost device \"{deviceState.name}\" that was used as {use}");

                deviceState.name         = null;
                deviceState.serialNumber = null;
                deviceState.found        = false;
                deviceState.role         = TrackedDeviceRole.Unknown;

                deviceRemoved?.Invoke(deviceState, use);
            }
        }
Esempio n. 22
0
 public bool TryGetFingerCurl(DeviceUse use, out FingerCurl curl)
 {
     curl = new FingerCurl(0f, 0f, 0f, 0f, 0f);
     return(false);
 }
Esempio n. 23
0
        public void GetDevices(Dictionary <string, TrackedDevice> devices)
        {
            devices.Clear();
            bool changeDetected = false;

            _openVRFacade.GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin.TrackingUniverseStanding, _poses);

            for (uint i = 0; i < _poses.Length; i++)
            {
                DeviceUse use = DeviceUse.Unknown;

                bool   isConnected  = _devices[i].isConnected;
                string modelName    = _devices[i].modelName;
                string serialNumber = _devices[i].serialNumber;

                ETrackedDeviceClass    deviceClass    = _openVRFacade.GetTrackedDeviceClass(i);
                ETrackedControllerRole controllerRole = _openVRFacade.GetControllerRoleForTrackedDeviceIndex(i);
                string role = _openVRFacade.GetStringTrackedDeviceProperty(i, ETrackedDeviceProperty.Prop_ControllerType_String);

                if (_poses[i].bDeviceIsConnected != isConnected)
                {
                    isConnected = _poses[i].bDeviceIsConnected;

                    if (_poses[i].bDeviceIsConnected)
                    {
                        modelName    = _openVRFacade.GetStringTrackedDeviceProperty(i, ETrackedDeviceProperty.Prop_ModelNumber_String);
                        serialNumber = _openVRFacade.GetStringTrackedDeviceProperty(i, ETrackedDeviceProperty.Prop_SerialNumber_String);

                        _logger.Info($"Device '{modelName}' (class '{deviceClass}', serial number '{serialNumber}') connected at index {i}");
                    }
                    else
                    {
                        _logger.Info($"Device '{modelName}' (class '{deviceClass}', serial number '{serialNumber}') disconnected from index {i}");

                        modelName    = null;
                        serialNumber = null;
                    }

                    changeDetected = true;
                }

                if (deviceClass != _devices[i].deviceClass)
                {
                    _logger.Trace($"Device '{serialNumber}' class changed from '{_devices[i].deviceClass}' to '{deviceClass}'");

                    changeDetected = true;
                }

                if (controllerRole != _devices[i].controllerRole)
                {
                    _logger.Trace($"Device '{serialNumber}' role changed from '{_devices[i].controllerRole}' to '{controllerRole}'");

                    changeDetected = true;
                }

                if (role != _devices[i].role)
                {
                    if (role == null)
                    {
                        _logger.Trace($"Device '{serialNumber}' role unset from '{_devices[i].role}'");
                    }
                    else if (_devices[i].role == null)
                    {
                        _logger.Trace($"Device '{serialNumber}' role set to '{role}'");
                    }
                    else
                    {
                        _logger.Trace($"Device '{serialNumber}' role changed from '{_devices[i].role}' to '{role}'");
                    }

                    changeDetected = true;
                }

                switch (deviceClass)
                {
                case ETrackedDeviceClass.HMD:
                    use = DeviceUse.Head;
                    break;

                case ETrackedDeviceClass.Controller:
                    switch (controllerRole)
                    {
                    case ETrackedControllerRole.LeftHand:
                        use = DeviceUse.LeftHand;
                        break;

                    case ETrackedControllerRole.RightHand:
                        use = DeviceUse.RightHand;
                        break;
                    }

                    break;

                case ETrackedDeviceClass.GenericTracker:
                    switch (role)
                    {
                    case "vive_tracker_waist":
                        use = DeviceUse.Waist;
                        break;

                    case "vive_tracker_left_foot":
                        use = DeviceUse.LeftFoot;
                        break;

                    case "vive_tracker_right_foot":
                        use = DeviceUse.RightFoot;
                        break;
                    }

                    break;
                }

                bool isTracking = _poses[i].bPoseIsValid && _validTrackingResults.Contains(_poses[i].eTrackingResult);

                if (_devices[i].isTracking != isTracking)
                {
                    if (isTracking)
                    {
                        _logger.Info($"Acquired tracking of device '{serialNumber}'");
                    }
                    else
                    {
                        _logger.Info($"Lost tracking of device '{serialNumber}'");
                    }

                    changeDetected = true;
                }

                Vector3    position = Vector3.zero;
                Quaternion rotation = Quaternion.identity;

                if (isTracking)
                {
                    position = _openVRFacade.GetPosition(_poses[i].mDeviceToAbsoluteTracking);
                    rotation = _openVRFacade.GetRotation(_poses[i].mDeviceToAbsoluteTracking);

                    // Driver4VR rotation correction
                    if (role.StartsWith("d4vr_tracker_") && (use == DeviceUse.LeftFoot || use == DeviceUse.RightFoot))
                    {
                        rotation *= Quaternion.Euler(-90, 180, 0);
                    }

                    // KinectToVR rotation correction
                    if (role == "kinect_device")
                    {
                        if (use == DeviceUse.Waist)
                        {
                            rotation *= Quaternion.Euler(-90, 180, 0);
                        }

                        if (use == DeviceUse.LeftFoot || use == DeviceUse.RightFoot)
                        {
                            rotation *= Quaternion.Euler(0, 180, 0);
                        }
                    }
                }

                _devices[i] = new OpenVRDevice(isConnected, isTracking, controllerRole, deviceClass, modelName, serialNumber, role);

                if (isConnected)
                {
                    devices.Add(serialNumber, new TrackedDevice(serialNumber, use, isTracking, position, rotation));
                }
            }

            if (changeDetected)
            {
                devicesChanged?.Invoke();
            }
        }
        private void UpdateTrackingMarker(GameObject sphere, GameObject rod, Transform avatarTarget, DeviceUse deviceUse)
        {
            if (_playerInput.TryGetUncalibratedPoseForAvatar(deviceUse, _avatarManager.currentlySpawnedAvatar, out Pose pose))
            {
                sphere.SetActive(true);
                sphere.transform.position = pose.position;
                sphere.transform.rotation = pose.rotation;

                rod.SetActive(true);
                Vector3 trackerToPoint = pose.position - avatarTarget.position;
                Vector3 pivot          = (pose.position + avatarTarget.position) * 0.5f;
                Vector3 localScale     = rod.transform.localScale;
                rod.transform.position   = pivot;
                rod.transform.rotation   = Quaternion.LookRotation(trackerToPoint) * Quaternion.Euler(90, 0, 0);
                rod.transform.localScale = new Vector3(localScale.x, trackerToPoint.magnitude * 0.5f, localScale.z);
            }
            else
            {
                sphere.SetActive(false);
                rod.SetActive(false);
            }
        }
Esempio n. 25
0
        public bool GetDevices(Dictionary <string, TrackedDevice> devices)
        {
            devices.Clear();
            bool changeDetected = false;

            _openVRFacade.GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin.TrackingUniverseStanding, _poses);

            for (uint i = 0; i < _poses.Length; i++)
            {
                OpenVRDevice        device = _devices[i];
                TrackedDevicePose_t pose   = _poses[i];

                DeviceUse use = DeviceUse.Unknown;

                string id          = device.id;
                bool   isConnected = device.isConnected;
                string modelName   = device.modelName;

                if (pose.bDeviceIsConnected != isConnected)
                {
                    isConnected = pose.bDeviceIsConnected;

                    if (pose.bDeviceIsConnected)
                    {
                        string serialNumber = _openVRFacade.GetStringTrackedDeviceProperty(i, ETrackedDeviceProperty.Prop_SerialNumber_String);

                        modelName = _openVRFacade.GetStringTrackedDeviceProperty(i, ETrackedDeviceProperty.Prop_ModelNumber_String);
                        id        = string.Concat(modelName ?? "Unknown", " ", (uint)serialNumber?.GetHashCode(), "@", i);

                        _logger.Info($"Device '{id}' connected");
                    }
                    else
                    {
                        _logger.Info($"Device '{id}' disconnected");

                        id        = null;
                        modelName = null;
                    }

                    changeDetected = true;
                }

                if (!isConnected)
                {
                    _devices[i] = default;
                    continue;
                }

                ETrackedDeviceClass    deviceClass    = _openVRFacade.GetTrackedDeviceClass(i);
                ETrackedControllerRole controllerRole = _openVRFacade.GetControllerRoleForTrackedDeviceIndex(i);
                string role = _openVRFacade.GetStringTrackedDeviceProperty(i, ETrackedDeviceProperty.Prop_ControllerType_String);

                if (deviceClass != device.deviceClass)
                {
                    _logger.Trace($"Device '{id}' class changed from '{device.deviceClass}' to '{deviceClass}'");

                    changeDetected = true;
                }

                if (controllerRole != device.controllerRole)
                {
                    _logger.Trace($"Device '{id}' controller role changed from '{device.controllerRole}' to '{controllerRole}'");

                    changeDetected = true;
                }

                if (role != device.role)
                {
                    if (role == null)
                    {
                        _logger.Trace($"Device '{id}' role unset from '{device.role}'");
                    }
                    else if (device.role == null)
                    {
                        _logger.Trace($"Device '{id}' role set to '{role}'");
                    }
                    else
                    {
                        _logger.Trace($"Device '{id}' role changed from '{device.role}' to '{role}'");
                    }

                    changeDetected = true;
                }

                switch (deviceClass)
                {
                case ETrackedDeviceClass.HMD:
                    use = DeviceUse.Head;
                    break;

                case ETrackedDeviceClass.Controller:
                    switch (controllerRole)
                    {
                    case ETrackedControllerRole.LeftHand:
                        use = DeviceUse.LeftHand;
                        break;

                    case ETrackedControllerRole.RightHand:
                        use = DeviceUse.RightHand;
                        break;
                    }

                    break;

                case ETrackedDeviceClass.GenericTracker:
                    switch (role)
                    {
                    case "vive_tracker_waist":
                        use = DeviceUse.Waist;
                        break;

                    case "vive_tracker_left_foot":
                        use = DeviceUse.LeftFoot;
                        break;

                    case "vive_tracker_right_foot":
                        use = DeviceUse.RightFoot;
                        break;
                    }

                    break;
                }

                bool isTracking = pose.bPoseIsValid && kValidTrackingResults.Contains(pose.eTrackingResult);

                if (device.isTracking != isTracking)
                {
                    if (isTracking)
                    {
                        _logger.Info($"Acquired tracking of device '{id}'");
                    }
                    else
                    {
                        _logger.Info($"Lost tracking of device '{id}'");
                    }

                    changeDetected = true;
                }

                Vector3    position = Vector3.zero;
                Quaternion rotation = Quaternion.identity;

                if (isTracking)
                {
                    _openVRFacade.GetPositionAndRotation(pose.mDeviceToAbsoluteTracking, out position, out rotation);

                    // Driver4VR rotation correction
                    if (role.StartsWith("d4vr_tracker_") && (use == DeviceUse.LeftFoot || use == DeviceUse.RightFoot))
                    {
                        rotation *= Quaternion.Euler(-90, 180, 0);
                    }

                    // KinectToVR rotation correction
                    if (role == "kinect_device")
                    {
                        if (use == DeviceUse.Waist)
                        {
                            rotation *= Quaternion.Euler(-90, 180, 0);
                        }

                        if (use == DeviceUse.LeftFoot || use == DeviceUse.RightFoot)
                        {
                            rotation *= Quaternion.Euler(0, 180, 0);
                        }
                    }
                }

                _devices[i] = new OpenVRDevice(id, isConnected, isTracking, controllerRole, deviceClass, modelName, role);
                devices.Add(id, new TrackedDevice(id, use, isTracking, position, rotation));
            }

            return(changeDetected);
        }
 private void OnDevicesUpdated(TrackedDeviceState state, DeviceUse use)
 {
     inputChanged?.Invoke();
 }