private void UpdateWaveVRModel()
        {
            if (!ChangeProp.Set(ref m_currentDeviceIndex, GetCurrentDeviceIndex()))
            {
                return;
            }

            var hasValidModel = false;
            var handType      = default(wvr.WVR_DeviceType);

            if (VRModule.IsValidDeviceIndex(m_currentDeviceIndex))
            {
                if (m_currentDeviceIndex == VRModule.GetRightControllerDeviceIndex())
                {
                    hasValidModel = true;
                    handType      = wvr.WVR_DeviceType.WVR_DeviceType_Controller_Right;
                }
                else if (m_currentDeviceIndex == VRModule.GetLeftControllerDeviceIndex())
                {
                    hasValidModel = true;
                    handType      = wvr.WVR_DeviceType.WVR_DeviceType_Controller_Left;
                }
            }

            // NOTE: load renderModel only if it hasen't been loaded or user changes handType
            if (hasValidModel)
            {
                if (m_modelObj != null)
                {
                    if (!m_waveVRModelLoaded)
                    {
                        // Clean up model that created by other module
                        CleanUpModelObj();
                    }
                    else if (m_currentWaveVRHandType != handType)
                    {
                        // Clean up model if changed to different hand
                        CleanUpModelObj();
                    }
                }

                m_currentWaveVRHandType = handType;

                if (!m_waveVRModelLoaded)
                {
                    // Create WaveVR_ControllerLoader silently (to avoid Start and OnEnable)
                    var loaderGO = new GameObject("Loader");
                    loaderGO.transform.SetParent(transform, false);
                    loaderGO.SetActive(false);
                    var loader = loaderGO.AddComponent <WaveVR_ControllerLoader>();
                    loader.enabled       = false;
                    loader.TrackPosition = false;
                    loader.TrackRotation = false;
                    loader.showIndicator = false;
                    loaderGO.SetActive(true);
                    // Call onLoadController to create model (chould be Finch/Link/Pico/QIYIVR)
                    switch (handType)
                    {
                    case wvr.WVR_DeviceType.WVR_DeviceType_Controller_Right:
                        loader.WhichHand = WaveVR_ControllerLoader.ControllerHand.Controller_Right;
                        loaderGO.SendMessage("onLoadController", wvr.WVR_DeviceType.WVR_DeviceType_Controller_Right);
                        break;

                    case wvr.WVR_DeviceType.WVR_DeviceType_Controller_Left:
                        loader.WhichHand = WaveVR_ControllerLoader.ControllerHand.Controller_Left;
                        loaderGO.SendMessage("onLoadController", wvr.WVR_DeviceType.WVR_DeviceType_Controller_Left);
                        break;
                    }

                    // Find transform that only contains controller model (include animator, exclude PoseTracker/Beam/UIPointer)
                    // and remove other unnecessary objects
                    var ctrllerActions = FindWaveVRControllerActionsObjInChildren();
                    if (ctrllerActions != null)
                    {
                        ctrllerActions.transform.SetParent(transform, false);
                        ctrllerActions.transform.SetAsFirstSibling();
                        for (int i = transform.childCount - 1; i >= 1; --i)
                        {
                            Destroy(transform.GetChild(i).gameObject);
                        }
                        ctrllerActions.gameObject.SetActive(true);
                        m_modelObj = ctrllerActions.gameObject;
                    }
                    else
                    {
                        Debug.LogWarning("FindWaveVRControllerActionsObjInChildren failed");
                        for (int i = transform.childCount - 1; i >= 0; --i)
                        {
                            Destroy(transform.GetChild(i).gameObject);
                        }
                    }

                    m_waveVRModelLoaded = true;
                }
                else
                {
                    if (m_modelObj != null)
                    {
                        m_modelObj.SetActive(true);
                    }
                }
            }
            else
            {
                if (m_modelObj != null)
                {
                    m_modelObj.SetActive(false);
                }
            }
        }
예제 #2
0
            public override void UpdateRenderModel()
            {
                if (!ChangeProp.Set(ref m_index, hook.GetModelDeviceIndex()))
                {
                    return;
                }

                var hasValidModel = false;
                var handType      = default(WVR_DeviceType);

                if (VRModule.IsValidDeviceIndex(m_index))
                {
                    if (m_index == VRModule.GetRightControllerDeviceIndex())
                    {
                        hasValidModel = true;
                        handType      = WVR_DeviceType.WVR_DeviceType_Controller_Right;
                    }
                    else if (m_index == VRModule.GetLeftControllerDeviceIndex())
                    {
                        hasValidModel = true;
                        handType      = WVR_DeviceType.WVR_DeviceType_Controller_Left;
                    }
                }

                // NOTE: load renderModel only if it hasn't been loaded or user changes handType
                if (hasValidModel)
                {
                    if (m_model != null && m_loadedHandType != handType)
                    {
                        CleanUpRenderModel();
                    }

                    if (m_model == null)
                    {
                        // Create WaveVR_ControllerLoader silently (to avoid Start and OnEnable)
                        var loaderGO = new GameObject("Loader");
                        loaderGO.transform.SetParent(hook.transform, false);
                        loaderGO.SetActive(false);
                        var loader = loaderGO.AddComponent <WaveVR_ControllerLoader>();
                        loader.TrackPosition = false;
                        loader.TrackRotation = false;
                        loader.showIndicator = false;
                        // Call onLoadController to create model (chould be Finch/Link/Pico/QIYIVR)
                        switch (handType)
                        {
                        case WVR_DeviceType.WVR_DeviceType_Controller_Right:
#if VIU_WAVEVR_3_0_0_OR_NEWER
                            loader.WhichHand = s_moduleInstance.m_deviceHands[RIGHT_INDEX];
#else
                            loader.WhichHand = WaveVR_ControllerLoader.ControllerHand.Controller_Right;
#endif
                            loaderGO.SetActive(true);

                            if (WaveVR.Instance.getDeviceByType(handType).pose.pose.Is6DoFPose&& WaveVR_Controller.IsLeftHanded)
                            {
                                loaderGO.SendMessage("onLoadController", WVR_DeviceType.WVR_DeviceType_Controller_Left);
                            }
                            else
                            {
                                loaderGO.SendMessage("onLoadController", WVR_DeviceType.WVR_DeviceType_Controller_Right);
                            }
                            break;

                        case WVR_DeviceType.WVR_DeviceType_Controller_Left:
#if VIU_WAVEVR_3_0_0_OR_NEWER
                            loader.WhichHand = s_moduleInstance.m_deviceHands[LEFT_INDEX];
#else
                            if (Interop.WVR_GetWaveRuntimeVersion() >= 3 && WaveVR_Controller.IsLeftHanded)
                            {
                                loader.WhichHand = WaveVR_ControllerLoader.ControllerHand.Controller_Right;
                            }
                            else
                            {
                                loader.WhichHand = WaveVR_ControllerLoader.ControllerHand.Controller_Left;
                            }
#endif
                            loaderGO.SetActive(true);

                            if (WaveVR.Instance.getDeviceByType(handType).pose.pose.Is6DoFPose&& WaveVR_Controller.IsLeftHanded)
                            {
                                loaderGO.SendMessage("onLoadController", WVR_DeviceType.WVR_DeviceType_Controller_Right);
                            }
                            else
                            {
                                loaderGO.SendMessage("onLoadController", WVR_DeviceType.WVR_DeviceType_Controller_Left);
                            }
                            break;
                        }

                        // Find transform that only contains controller model (include animator, exclude PoseTracker/Beam/UIPointer)
                        // and remove other unnecessary objects
                        var ctrllerActions = FindWaveVRControllerActionsObjInChildren();
                        if (ctrllerActions != null)
                        {
                            ctrllerActions.transform.SetParent(hook.transform, false);
                            ctrllerActions.transform.SetAsFirstSibling();
                            for (int i = hook.transform.childCount - 1; i >= 1; --i)
                            {
                                Object.Destroy(hook.transform.GetChild(i).gameObject);
                            }
                            ctrllerActions.gameObject.SetActive(true);
                            m_model = ctrllerActions.gameObject;
                        }
                        else
                        {
                            Debug.LogWarning("FindWaveVRControllerActionsObjInChildren failed");
                            for (int i = hook.transform.childCount - 1; i >= 0; --i)
                            {
                                Object.Destroy(hook.transform.GetChild(i).gameObject);
                            }
                        }

                        m_loadedHandType = handType;
                    }

                    m_model.SetActive(true);
                }
                else
                {
                    if (m_model != null)
                    {
                        m_model.SetActive(false);
                    }
                }
            }
예제 #3
0
        private void MappingLeftRightHands()
        {
            // assign left/right controllers according to the hint
            var rightIndex = VRModule.GetRightControllerDeviceIndex();
            var leftIndex  = VRModule.GetLeftControllerDeviceIndex();

            if (VRModule.GetCurrentDeviceState(rightIndex).isConnected)
            {
                MappingRoleIfUnbound(HandRole.RightHand, rightIndex);
                rightIndex = RoleMap.GetMappedDeviceByRole(HandRole.RightHand);
            }
            else if (RoleMap.IsRoleBound(HandRole.RightHand))
            {
                rightIndex = RoleMap.GetMappedDeviceByRole(HandRole.RightHand);
            }
            else
            {
                rightIndex = VRModule.INVALID_DEVICE_INDEX;
            }

            if (VRModule.GetCurrentDeviceState(leftIndex).isConnected&& leftIndex != rightIndex)
            {
                MappingRoleIfUnbound(HandRole.LeftHand, leftIndex);
                leftIndex = RoleMap.GetMappedDeviceByRole(HandRole.LeftHand);
            }
            else if (RoleMap.IsRoleBound(HandRole.LeftHand))
            {
                leftIndex = RoleMap.GetMappedDeviceByRole(HandRole.LeftHand);
            }
            else
            {
                leftIndex = VRModule.INVALID_DEVICE_INDEX;
            }

            // if not both left/right controllers are assigned, find and assign them with left/right most controller
            if (!VRModule.IsValidDeviceIndex(rightIndex) || !VRModule.IsValidDeviceIndex(leftIndex))
            {
                // find right to left sorted controllers
                // FIXME: GetSortedTrackedDeviceIndicesOfClass doesn't return correct devices count rightafter device connected
#if __VIU_STEAMVR
                if (VRModule.activeModule == SupportedVRModule.SteamVR)
                {
                    var count  = 0;
                    var system = Valve.VR.OpenVR.System;
                    if (system != null)
                    {
                        count = (int)system.GetSortedTrackedDeviceIndicesOfClass(Valve.VR.ETrackedDeviceClass.Controller, m_sortedDevices, Valve.VR.OpenVR.k_unTrackedDeviceIndex_Hmd);
                    }

                    foreach (var deviceIndex in m_sortedDevices)
                    {
                        if (m_sortedDeviceList.Count >= count)
                        {
                            break;
                        }
                        if (IsController(deviceIndex) && deviceIndex != rightIndex && deviceIndex != leftIndex && !RoleMap.IsDeviceConnectedAndBound(deviceIndex))
                        {
                            m_sortedDeviceList.Add(deviceIndex);
                        }
                    }
                }
                else
#endif
                {
                    for (uint deviceIndex = 1u; deviceIndex < VRModule.MAX_DEVICE_COUNT; ++deviceIndex)
                    {
                        if (IsController(deviceIndex) && deviceIndex != rightIndex && deviceIndex != leftIndex && !RoleMap.IsDeviceConnectedAndBound(deviceIndex))
                        {
                            m_sortedDeviceList.Add(deviceIndex);
                        }
                    }

                    if (m_sortedDeviceList.Count > 1)
                    {
                        SortDeviceIndicesByDirection(m_sortedDeviceList, VRModule.GetCurrentDeviceState(VRModule.HMD_DEVICE_INDEX).pose);
                    }
                }

                if (m_sortedDeviceList.Count > 0 && !VRModule.IsValidDeviceIndex(rightIndex))
                {
                    rightIndex = m_sortedDeviceList[0];
                    m_sortedDeviceList.RemoveAt(0);
                    // mapping right most controller
                    MappingRole(HandRole.RightHand, rightIndex);
                }

                if (m_sortedDeviceList.Count > 0 && !VRModule.IsValidDeviceIndex(leftIndex))
                {
                    leftIndex = m_sortedDeviceList[m_sortedDeviceList.Count - 1];
                    // mapping left most controller
                    MappingRole(HandRole.LeftHand, leftIndex);
                }

                m_sortedDeviceList.Clear();
            }

            if (!VRModule.IsValidDeviceIndex(rightIndex))
            {
                UnmappingRole(HandRole.RightHand);
            }
            if (!VRModule.IsValidDeviceIndex(leftIndex))
            {
                UnmappingRole(HandRole.LeftHand);
            }
        }
예제 #4
0
        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.GetLeftControllerDeviceIndex();
                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();
            }
        }