Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        public virtual void InitVRContext()
        {
            //
            if (m_IsInited)
            {
                return;
            }
            m_IsInited = true;
            // Init Hmd SDK.
            bool needClone = true;

            if (vrDevice == null)
            {
#if (UNITY_EDITOR || UNITY_STANDALONE) && OPENVR_SDK
                vrDevice  = new GameObject("OpenVRDevice", typeof(OpenVRDevice)).GetComponent <OpenVRDevice>();
                needClone = false;
#else
                GameObject go = PlayerPrefsEx.GetObject("VRDevice.source", null) as GameObject;
                if (go == null || (vrDevice = go.GetComponent <VRDevice>()) == null)
                {
                }
                else
                {
                }
#endif
            }
            //
            if (needClone)
            {
                string name = vrDevice.name;
                vrDevice      = Instantiate(vrDevice) as VRDevice;
                vrDevice.name = name;
            }
            //
            Transform t = vrDevice.transform;
            t.SetParent(transform);
            t.localPosition = Vector3.zero;
            t.localRotation = Quaternion.identity;
            t.localScale    = Vector3.one;
            // The center eye may be replaced in this function.
            vrDevice.InitDevice(this);
            //
            // Check existed anchors
            Transform trackingSpace   = GetAnchor(VRNode.TrackingSpace, null);
            Transform centerEyeAnchor = GetAnchor(VRNode.CenterEye, null);
            // Auto fill the trackingSpace field if it is null.
            if (trackingSpace == null)
            {
                if (centerEyeAnchor != null)
                {
                    SetAnchor(VRNode.TrackingSpace, trackingSpace = centerEyeAnchor.parent);
                }
            }
            // Instantiate anchors.
            if (trackingSpace != null)
            {
                for (int i = 0, imax = m_Anchors.Length; i < imax; ++i)
                {
                    switch (m_Anchors[i].key)
                    {
                    // Ignored anchors.
                    case VRNode.TrackingSpace:
                    case VRNode.Count:
                        break;

                    //
                    default:
                        if (m_Anchors[i].value != null)
                        {
                            if (!InScene(m_Anchors[i].value))                                     // This anchor is a prefab.
                            {
                                Transform copy = Instantiate <Transform>(m_Anchors[i].value);
                                copy.SetParent(trackingSpace);
                                copy.name = m_Anchors[i].key.ToString() + "Anchor";
                                CopyTransform(m_Anchors[i].value, copy);
                                //
                                m_Anchors[i].value = copy;
                            }
                        }
                        break;
                    }
                }
            }
            if (vrDevice.family != "Dummy")
            {
                if (trackingSpace == null && centerEyeAnchor == null)
                {
                    Ximmerse.Log.e("VRContext", "trackingSpace==null&&centerEyeAnchor==null at InitVRContext()");
                }
                else if (centerEyeAnchor == null)
                {
                    Ximmerse.Log.e("VRContext", "centerEyeAnchor==null at InitVRContext()");
                }
            }
            // Optional :
            if (PlayerPrefsEx.GetBool("VRDevice.useFade", false))
            {
                if (fadeFx != null)
                {
                    fadeFx.FadeOut();
                }
            }
            // Optional :
            if (PlayerPrefsEx.GetBool("VRDevice.useReticle", false))
            {
                GameObject go = PlayerPrefsEx.GetObject("VRDevice.reticleObject", null) as GameObject;
                if (go != null)
                {
                    Transform rawT = go.transform;
                    go = Instantiate(go);
                    Transform newT = go.transform;
                    //
                    newT.SetParent(uiRootVR);
                    CopyTransform(rawT, newT);
                }
            }
            //
            // Send the initialization message.
            XDevicePlugin.Init();
            vrDevice.OnVRContextInited(this);
            for (int i = 0, imax = m_Anchors.Length; i < imax; ++i)
            {
                if (m_Anchors[i].value != null)
                {
                    m_Anchors[i].value.SendMessage("OnVRContextInited", this, SendMessageOptions.DontRequireReceiver);
                }
            }
        }
Beispiel #2
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);
        }
 public static bool isStartAnimFinished()
 {
     return(PlayerPrefsEx.GetBool(START_ANIM_FINISHED));
 }
    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();
    }
 public static bool GetBoolValue(string key, bool default_val)
 {
     return(PlayerPrefsEx.GetBool(START_ANIM_FINISHED));
 }