Exemplo n.º 1
0
        /// <summary>
        /// We will lose the VR context,when reloading level.
        /// Calling this function per frame can ensure that the anchor is alive.
        /// </summary>
        public virtual bool EnsureAnchor()
        {
            // <!-- TODO: VR Legacy Mode. -->
            // If the X-Hawk isn't connected,the game will run as legacy VR mode(Gets input events with GearVR touchpad).
            if (XDevicePlugin.GetInt(m_Handle, XDevicePlugin.kField_ConnectionState, 0) != (int)DeviceConnectionState.Connected)
            {
                XDevicePlugin.SetInt(m_HmdHandle, XDevicePlugin.kField_ConnectionState, (int)DeviceConnectionState.Disconnected);
                return(false);
            }
            //
            if (trackingSpace == null)
            {
                trackingSpace = VRContext.GetAnchor(VRNode.TrackingSpace);
            }
            //
            if (anchor == null)
            {
                Transform centerEyeAnchor = VRContext.GetAnchor(VRNode.CenterEye);
                if (m_IsRequestVR && centerEyeAnchor == null)
                {
                    return(false);
                }
                else
                {
                    //
                    switch ((XDevicePlugin.GetInt(-1, XDevicePlugin.kField_CtxDeviceVersion, 0x4000) & 0xF000))
                    {
                    case 0x3000:
                        anchor = new GameObject("XHawkAnchor").transform;
                        anchor.SetParent(centerEyeAnchor);
                        anchor.localPosition = anchorPosition;
                        anchor.localRotation = Quaternion.Euler(anchorRotation);
                        //
                        m_UseAnchorProjection = !PlayerPrefsEx.GetBool("XimmerseTracker[Inside-out].fixShake", false);                             //!modifyHierarchy;
                        VRContext.SetAnchor(VRNode.TrackerDefault, anchor);
                        // TODO :

                        /*if(modifyHierarchy) {
                         *      for(int i=0;i<2;++i) {
                         *              Transform hand=VRContext.GetAnchor(VRNode.LeftHand+i);
                         *              if(hand!=null) {
                         *                      hand.SetParent(anchor,true);
                         *              }
                         *      }
                         * }*/
                        if (PlayerPrefsEx.GetBool("XimmerseTracker[Inside-out].fixShake", false))
                        {
                            m_AnchorMatrixQueue = new List <Matrix4x4>(8);
                            m_M2pFrameIndex     = Mathf.CeilToInt(PlayerPrefsEx.GetFloat("XimmerseTracker[Inside-out].m2pLatency", 0.0f) * VRContext.refreshRate);
                            Log.i("XHawkInput", "m_M2pFrameIndex=" + m_M2pFrameIndex);
                        }
                        else
                        {
                            m_AnchorMatrixQueue = null;
                        }
                        break;

                    case 0x4000:
                        //
                        float trackerHeight, trackerDepth, trackerPitch;
                        XDevicePlugin.GetTrackerPose(m_Handle, out trackerHeight, out trackerDepth, out trackerPitch);
                        //
                        anchor = (anchorPrefab == null || !m_IsRequestVR?                          // in 2D mode.
                                  new GameObject():
                                  Object.Instantiate(anchorPrefab)
                                  ).transform;
                        anchor.name = "TrackerAnchor(X-Hawk DK4)";
                        anchor.SetParent(trackingSpace);
                        //
#if XDEVICE_RELEASE
                        if (true)
                        {
                            UpdateAnchorFromPlugin();
                        }
                        else
#endif
                        {
                            anchor.localPosition = new Vector3(0f, VRContext.trackingOrigin == TrackingOrigin.FloorLevel?trackerHeight:0.0f, trackerDepth);
                            anchor.localRotation = Quaternion.Euler(-trackerPitch, 180f, 0f);
                        }
                        //
                        m_TrackerFrustum = anchor.GetComponentInChildren <TrackerFrustum>();
                        //
//#if XDEVICE_RELEASE
//							m_UseAnchorProjection=false;
//#else
                        m_UseAnchorProjection = true;
//#endif
                        VRContext.SetAnchor(VRNode.TrackerDefault, anchor);
                        m_AnchorMatrixQueue = null;
                        break;

                    default:
                        anchor = new GameObject("Dummy Anchor").transform;
                        anchor.SetParent(trackingSpace, false);
                        break;
                    }
                    // Override anchor info.
                    if (m_AnchorInfoCached)
                    {
                        anchor.localPosition = m_AnchorPosition;
                        anchor.localRotation = m_AnchorRotation;
                    }
                    //
                    if (m_IsRequestVR)
                    {
                        VRContext.main.onRecenter -= RecenterOnVRContextRecenter;
                        VRContext.main.onRecenter += RecenterOnVRContextRecenter;
                    }
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This function will be called by ControllerInputManager or Start().
        /// XCobra controller's position will be overwritten by this class.
        /// </summary>
        public virtual void Launch(bool checkOthers)
        {
            if (m_IsLaunched)
            {
                return;
            }
            m_IsLaunched = true;
            //
            deviceName = "XHawk-0";
            if (checkOthers)
            {
                XHawkInput other = FindAll(deviceName).Find((x) => (x != this)) as XHawkInput;
                if (other != null)
                {
                    // This will be not found by TrackingInput.Find(string).
                    gameObject.SetActive(false);
                    other.Launch(false);
                    //Log.i("XHawkInput","Use the default TrackingInput in scene.");
                    return;
                }
            }
            //
            XDevicePlugin.Init();
            if (m_Handle == -1)
            {
                m_Handle = XDevicePlugin.GetInputDeviceHandle(deviceName);
            }
            m_HmdHandle = XDevicePlugin.GetInputDeviceHandle("VRDevice");
            XDevicePlugin.SetInt(m_Handle, XDevicePlugin.kField_TrackingOrigin, (int)VRContext.trackingOrigin);
            // Set values to the plugin
            if (overridePlugin)             //&&m_Handle>0) {
            {
                if ((XDevicePlugin.GetInt(-1, XDevicePlugin.kField_CtxDeviceVersion, 0x4000) & 0xF000) == 0x4000)
                {
                    Vector3 trackerPos = PlayerPrefsEx.GetVector3("XimmerseTracker[Outside-in].position", new Vector3(0.0f, 1.675f, 1.5f));
                    Vector3 trackerRot = PlayerPrefsEx.GetVector3("XimmerseTracker[Outside-in].rotation", new Vector3(15.0f, 180.0f, 0.0f));
                    XDevicePlugin.SetTrackerPose(m_Handle, trackerPos.y, trackerPos.z, -trackerRot.x);
                }
            }
            // TODO :
            XDevicePlugin.SendMessage(m_Handle, XDevicePlugin.kMessage_RecenterSensor, 0, 0);
            //
#if XDEVICE_RELEASE
            //if((XDevicePlugin.GetInt(-1,XDevicePlugin.kField_CtxDeviceVersion,0x4000)&0xF000)!=0x4000)
#endif
            {
                int i = 0, imax = controllers.Length;
                inputs = new XCobraInput[imax];
                //
                ControllerInputManager mgr = ControllerInputManager.instance;
                ControllerInput        ci;
                if (mgr != null)
                {
                    for (; i < imax; ++i)
                    {
                        ci = mgr.GetControllerInput(controllers[i].key);
                        if (ci is XCobraInput)
                        {
                            inputs[i] = ci as XCobraInput;
                        }
                        else
                        {
                            inputs[i] = new XCobraInput(this, controllers[i].key, controllers[i].value);
                            mgr.AddControllerInput(inputs[i].name, inputs[i]);
                        }
                        //
                    }
                }
            }
            m_HmdInput = ControllerInputManager.GetInput(ControllerType.Hmd);
            // VRContext must have a CenterEyeAnchor at least.
            m_IsRequestVR = (VRContext.GetAnchor(VRNode.CenterEye) != null);
            EnsureAnchor();
            //
            if (XDevicePlugin.GetInt(-1, XDevicePlugin.kField_CtxDeviceVersion, 0) == 0x1010)
            {
                XDevicePlugin.SetInt(m_Handle, XDevicePlugin.kField_ConnectionState, (int)DeviceConnectionState.Connected);
                m_AsDaydreamEmulator = true;
                LoadHandConfigs();
            }
            //
            Log.i("XHawkInput", "Initialize successfully.");
        }
Exemplo n.º 3
0
        public override bool CreateAnchor()
        {
            if (anchor != null)
            {
                return(false);
            }
            //
            anchor = (anchorPrefab == null || !m_IsRequestVR?      // in 2D mode.
                      new GameObject():
                      Object.Instantiate(anchorPrefab)
                      ).transform;
            anchor.name = "TrackerAnchor(Outside-In)";
            anchor.SetParent(trackingSpace);
            //
            UpdateAnchorFromPlugin();
            m_PlayAreaHelper = anchor.GetComponentInChildren <PlayAreaHelper>();
            //
            m_UseAnchorProjection = true;
            VRContext.SetAnchor(VRNode.TrackerDefault, anchor);
            //
            if (m_IsRequestVR && useAutoRecenter)
            {
                // Recenter hmd firstly,for aligning rotation pivot of vr device and position pivot.
                // In outside-in case,we never use yaw offset(Recenter() will fix the value) in hmd sdk.
                XDevicePlugin.SetBool(m_HmdInput.handle, XDevicePlugin.kField_IsAbsRotationBool, false);
                VRContext.Recenter(false);
                XDevicePlugin.SetBool(m_HmdInput.handle, XDevicePlugin.kField_IsAbsRotationBool, true);
                //
                if (PlayerPrefsEx.GetBool("VRDevice.forceFadeOnBadTracking"))
                {
                    VRContext.FadeOut(1.0f, -1.0f);
                }
            }

            if (m_CenterEye == null)
            {
                m_CenterEye = VRContext.GetAnchor(VRNode.CenterEye);
                m_VRDevice  = VRContext.currentDevice;
            }

            if (uiAutoRecenter != null)
            {
                goUIAutoRecenter = Instantiate(uiAutoRecenter) as GameObject;
                Transform originTranformAuto = uiAutoRecenter.transform;
                goUIAutoRecenter.transform.SetParent(VRContext.uiRootVR);
                goUIAutoRecenter.transform.localPosition = originTranformAuto.localPosition;
                goUIAutoRecenter.transform.localRotation = originTranformAuto.localRotation;
                goUIAutoRecenter.transform.localScale    = originTranformAuto.localScale;
                _needAutoRecenter = true;
            }
            // TODO : Align Notice
            if (alignNeedTime > 0.0f)
            {
                m_UiAlign = new GameObject("Wait for Aligning", typeof(Image), typeof(UIFade)).GetComponent <Image>();
                m_UiAlign.gameObject.SetActive(false);
                //
                m_UiAlign.material   = Resources.Load <Material>("UI/MatNotice");
                m_UiAlign.sprite     = uiAlignSprite;
                m_UiAlign.type       = Image.Type.Filled;
                m_UiAlign.fillMethod = Image.FillMethod.Radial360;
                m_UiAlign.fillOrigin = 2;
                m_UiAlign.fillAmount = 0.0f;
                //
                RectTransform t = m_UiAlign.GetComponent <RectTransform>();
                t.SetParent(VRContext.uiRootVR);
                t.localPosition = Vector3.forward * 0.8f;
                t.localRotation = Quaternion.identity;
                t.localScale    = Vector3.one * 0.0016f;
                t.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 80f);
                t.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 80f);
                //
                m_UiAlignFade             = m_UiAlign.GetComponent <UIFade>();
                m_UiAlignFade.durationOut = 0.25f;
                m_UiAlignFade.onBecameInvisible.AddListener(() => { m_UiAlign.gameObject.SetActive(false); });
            }
            // TODO : LookAt Notice
            if (true)
            {
                m_UiLookAtFade = new GameObject("Look At XHawk", typeof(UIFade), typeof(Image)).GetComponent <UIFade>();
                m_UiLookAtFade.GetComponent <Image>().sprite   = uiXHawkSprite;
                m_UiLookAtFade.GetComponent <Image>().material = Resources.Load <Material>("UI/MatNotice");
                m_UiLookAtFade.durationIn  = .25f;
                m_UiLookAtFade.durationOut = .16f;
                m_UiLookAtFade.onBecameInvisible.AddListener(() => { m_UiLookAtFade.gameObject.SetActive(false); });
                m_UiLookAtFade.gameObject.SetActive(false);

                RectTransform t = m_UiLookAtFade.GetComponent <RectTransform>();
                t.SetParent(VRContext.uiRootVR);
                t.localPosition = Vector3.forward * 0.85f;
                t.localRotation = Quaternion.identity;
                t.localScale    = Vector3.one * 0.0016f;
                t.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 80f);
                t.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 80f);
                //
                m_UiLookAt = new GameObject("Dummy-LookAt").transform;
                m_UiLookAt.SetParent(t);
                m_UiLookAt.localPosition = Vector3.zero;
                m_UiLookAt.localRotation = Quaternion.identity;
                m_UiLookAt.localScale    = Vector3.one;
                //
                t = new GameObject("LookAt", typeof(Image)).GetComponent <RectTransform>();
                t.GetComponent <Image>().sprite   = uiLookAtSprite;
                t.GetComponent <Image>().material = Resources.Load <Material>("UI/MatNotice");
                t.SetParent(m_UiLookAt);
                t.localPosition = Vector3.forward * 80.0f;
                t.localRotation = Quaternion.Euler(new Vector3(90.0f, 0.0f, -90.0f));
                t.localScale    = Vector3.one;
                t.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 40f);
                t.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 40f);
            }
            return(true);
        }
Exemplo n.º 4
0
        public override Vector3 GetLocalPosition(int node)
        {
            Vector3 position = Vector3.zero;

            if (node == m_Controllers[2].value)
            {
                //
                if (useExperimental)
                {
                    //
                    UpdateState();
                    //
                    XDevicePlugin.GetSmoothPosition(2, ref position);
                    //
                    position.Scale(sensitivity);
                    if (m_UseAnchorProjection && anchor != null)
                    {
                        position = m_AnchorMatrix.MultiplyPoint3x4(position);
                    }
                }
                else
                {
                    position = base.GetLocalPosition(node);
                }
                //
                if (m_CenterEye == null)
                {
                    m_CenterEye = VRContext.GetAnchor(VRNode.CenterEye);
                    m_VRDevice  = VRContext.currentDevice;
                }
                if (m_CenterEye != null && m_VRDevice != null)
                {
                    Quaternion rotation = m_VRDevice.GetRotation();
                    //
                    if (useAutoRecenter)
                    {
                        rotation             = Quaternion.AngleAxis(m_YawOffset, Vector3.up) * rotation;
                        m_VRDevice.yawOffset = m_YawOffset;
                    }
                    else
                    {
                    }
                    //
                    position -= rotation * (m_VRDevice.neckToEye + m_VRDevice.outsideInMarkPose.position);
                }
            }
            else
            {
                //
                if (m_EnableEmulatedHand)
                {
                    if ((XDevicePlugin.GetNodePosition(m_Handle, 0, node, null) & TrackingResult.PositionTracked) == 0)
                    {
                        return(GetEmulatedHandPosition(node));
                    }
                }
                //
                if (useExperimental)
                {
                    //
                    UpdateState();
                    //
                    XDevicePlugin.GetSmoothPosition(node, ref position);
                    //
                    position.Scale(sensitivity);
                    if (m_UseAnchorProjection && anchor != null)
                    {
                        position = m_AnchorMatrix.MultiplyPoint3x4(position);
                    }
                }
                else
                {
                    position = base.GetLocalPosition(node);
                }
                //
            }
            return(position);
        }
Exemplo n.º 5
0
    protected virtual void Start()
    {
        //
        if (checkParent)
        {
            Transform p = VRContext.GetAnchor(VRNode.TrackingSpace);
            if (p != null)
            {
                target.SetParent(p, true);
            }
        }
        //
        if (source == ControllerType.None && string.IsNullOrEmpty(sourceName))
        {
            if (name.ToLower().IndexOf("left") != -1)
            {
                source = ControllerType.LeftController;
            }
            else if (name.ToLower().IndexOf("right") != -1)
            {
                source = ControllerType.RightController;
            }
        }
        if (source != ControllerType.None)
        {
            m_ControllerInput = ControllerInputManager.instance.GetControllerInput(source);
        }
        else if (!string.IsNullOrEmpty(sourceName))
        {
            m_ControllerInput = ControllerInputManager.instance.GetControllerInput(sourceName);
        }
        //
        VRNode node = VRNode.None;

        switch (source)
        {
        case ControllerType.LeftController:
            node = VRNode.LeftHand;
            break;

        case ControllerType.RightController:
            node = VRNode.RightHand;
            break;
        }
        if (node != VRNode.None)
        {
            VRContext.SetAnchor(node, target);
        }
        //
        if (m_ControllerInput != null && canRecenter)
        {
            // Like SteamVR,hmd and controllers don't need to reset yaw angle.
            canRecenter = !m_ControllerInput.isAbsRotation;
        }
        //
        if (canRecenter)
        {
            // Invoke Recenter() on VRContext recenter event.
            VRContext ctx = VRContext.main;
            if (ctx != null)
            {
                ctx.onRecenter += Recenter;
            }
        }
        //
        return;

        // TODO:
        enabled = false;
        UpdatePoses.instance.onUpdatePoses += Update;
    }
    protected virtual void Start()
    {
        m_Transform = transform;
        //
        m_TrackerHandle = -1;
        if (showOnTrackingLost)
        {
            XDevicePlugin.Init();
            m_TrackerHandle = XDevicePlugin.GetInputDeviceHandle(trackerName);
            if (m_TrackerHandle < 0)
            {
                showOnTrackingLost = false;
            }
        }
        //
        m_ReadyForModel = (controlPoints.Length == 8);
        // Cache values.

        /*
         * m_CosFovX=Mathf.Cos(Mathf.Deg2Rad*fovX*.5f);
         * m_CosFovY=Mathf.Cos(Mathf.Deg2Rad*fovY*.5f);
         * m_SqrNearZ=nearZ*nearZ;
         * m_SqrFarZ=farZ*farZ;
         *
         * m_SqrOrthographicSize=orthographicSize*.5f;
         * m_SqrOrthographicSize=Vector2.Scale(m_SqrOrthographicSize,m_SqrOrthographicSize);
         */
        //
        if (m_Corners != null && m_Corners.Length != 0)
        {
            m_CachedCorners = m_Corners;
        }
        m_Corners = null;
        //
        m_GroundAlpha      = PlayerPrefsEx.GetInt("PlayArea.drawGround", 1);
        m_WallAlpha        = PlayerPrefsEx.GetInt("PlayArea.drawWall", 1);
        autoCreatePlayArea = PlayerPrefsEx.GetBool("PlayArea.enabled", autoCreatePlayArea);
        showCameraModel    = PlayerPrefsEx.GetBool("PlayArea.showCameraModel", showCameraModel);
        if (m_cameraModel != null)
        {
            m_cameraModel.SetActive(showCameraModel);
        }
        warningDistance = PlayerPrefsEx.GetFloat("PlayArea.warningDistance", 0.5f);
        //
        if (autoCreatePlayArea)
        {
            Transform t = null;
            Transform trackingSpaceAnchor = VRContext.GetAnchor(VRNode.TrackingSpace);

            if (m_PlayArea == null)
            {
                m_PlayArea = trackingSpaceAnchor.GetComponentInChildren <PlayAreaRenderer>();
                if (m_PlayArea == null)
                {
                    GameObject go = PlayerPrefsEx.GetObject("PlayAreaRenderer") as GameObject;
                    m_PlayArea      = Instantiate(go).GetComponent <PlayAreaRenderer>();
                    m_PlayArea.name = go.name;
                }
            }
            m_PlayArea.handedness = -1;
            //
            t = m_PlayArea.transform;
            t.SetParent(trackingSpaceAnchor);
            t.localPosition = (VRContext.trackingOrigin == TrackingOrigin.FloorLevel) ? Vector3.zero :
                              new Vector3(0f, -XDevicePlugin.GetFloat(m_TrackerHandle, XDevicePlugin.kField_TrackerHeightFloat, 0f), 0f);
            t.localRotation = Quaternion.identity;
            t.localScale    = Vector3.one;
        }
        //
        UpdateModel();
    }