Exemple #1
0
        /// <summary>
        /// The Unity Update() method.
        /// </summary>
        public void Update()
        {
            if (Input.GetKey(KeyCode.Escape))
            {
                Application.Quit();
            }

            //_QuitOnConnectionErrors();

            // Check that motion tracking is tracking.
            if (EazyARSession.Status != SessionStatus.Tracking)
            {
                const int lostTrackingSleepTimeout = 15;
                Screen.sleepTimeout = lostTrackingSleepTimeout;
                if (!m_IsQuitting && EazyARSession.Status.IsARValid())
                {
                    searchingForPlaneUI.SetActive(true);
                }

                return;
            }

            Screen.sleepTimeout = SleepTimeout.NeverSleep;

            // Iterate over planes found in this frame and instantiate corresponding GameObjects to visualize them.
            m_NewPlanes = EazyARSession.GetTrackablePlanes(TrackableQueryFilter.New);
            for (int i = 0; i < m_NewPlanes.Count; i++)
            {
                // Instantiate a plane visualization prefab and set it to track the new plane. The transform is set to
                // the origin with an identity rotation since the mesh for our prefab is updated in Unity World
                // coordinates.
                EazyARCoreInterface.CreateDetectedPlane(m_NewPlanes[i]);
            }

            // Disable the snackbar UI when no planes are valid.
            m_AllPlanes = EazyARSession.GetTrackablePlanes();
            bool showSearchingUI = true;

            for (int i = 0; i < m_AllPlanes.Count; i++)
            {
                if (m_AllPlanes[i].TrackingState == TrackingState.Tracking)
                {
                    showSearchingUI = false;
                    break;
                }
            }

            searchingForPlaneUI.SetActive(showSearchingUI);

            // If the player has not touched the screen, we are done with this update.
            Vector3 inputPos = Vector3.zero;

#if UNITY_EDITOR
            if (!Input.GetMouseButtonDown(0))
            {
                return;
            }
            else
            {
                inputPos = Input.mousePosition;
            }
#elif UNITY_ANDROID
            Touch touch;
            if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
            {
                return;
            }
            inputPos = Input.GetTouch(0).position;
#endif

            // Raycast against the location the player touched to search for planes.
            EazyARRaycastHit hit;
            if (EazyARCoreInterface.ARRaycast(inputPos.x, inputPos.y, out hit))
            {
                GameObject andyObj = Instantiate(robotPrefab, hit.Pose.position, hit.Pose.rotation);

                // Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
                // world evolves.
                Anchor anchor = EazyARCoreInterface.CreateAnchor(hit.Trackable, hit.Pose);

                // Andy should look at the camera but still be flush with the plane.
                if ((hit.Flags & TrackableHitFlags.PlaneWithinPolygon) != TrackableHitFlags.None)
                {
                    // Get the camera position and match the y-component with the hit position.
                    Vector3 cameraPositionSameY = EazyARCoreInterface.ARCamera.transform.position;
                    cameraPositionSameY.y = hit.Pose.position.y;

                    // Have Andy look toward the camera respecting his "up" perspective, which may be from ceiling.
                    Vector3 direction  = EazyARCoreInterface.ARCamera.transform.position - andyObj.transform.position;
                    float   dot        = Vector3.Dot(direction, Vector3.up);
                    bool    orthogonal = ((dot == 1f) || (dot == -1f));
                    if (orthogonal)
                    {
                        return;
                    }

                    Vector3    fwd    = Vector3.ProjectOnPlane(direction, Vector3.up);
                    Quaternion fwdRot = Quaternion.LookRotation(fwd, Vector3.up);
                    andyObj.transform.localRotation = andyObj.transform.localRotation * fwdRot;
                }

                // Make Andy model a child of the anchor.
                andyObj.transform.parent = anchor.transform;
            }
        }
Exemple #2
0
    public void Update()
    {
        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }

        if (EazyARSession.Status != SessionStatus.Tracking)
        {
            const int lostTrackingSleepTimeout = 15;
            Screen.sleepTimeout = lostTrackingSleepTimeout;
            if (EazyARSession.Status.IsARValid())
            {
                this.searchingForPlaneUI.SetActive(true);
            }

            return;
        }

        Screen.sleepTimeout = SleepTimeout.NeverSleep;

        this.newPlanes = EazyARSession.GetTrackablePlanes(TrackableQueryFilter.New);
        for (int i = 0; i < this.newPlanes.Count; i++)
        {
            EazyARCoreInterface.CreateDetectedPlane(this.newPlanes[i]);
        }

        this.allPlanes = EazyARSession.GetTrackablePlanes();
        bool showSearchingUI = true;

        for (int i = 0; i < this.allPlanes.Count; i++)
        {
            if (this.allPlanes[i].TrackingState == TrackingState.Tracking)
            {
                showSearchingUI = false;
                break;
            }
        }

        this.searchingForPlaneUI.SetActive(showSearchingUI);

        this.inputPos = Vector3.zero;
#if UNITY_EDITOR
        inputPos = Input.mousePosition;
        if (Input.GetMouseButton(0))
        {
            this.Tap();
        }
        else
        {
            this.Release();
            return;
        }
#elif UNITY_ANDROID
        if (Input.touchCount > 0)
        {
            this.inputPos = Input.GetTouch(0).position;
            this.Tap();
        }
        else
        {
            this.Release();
            return;
        }
#endif

        if (trash == null)
        {
            EazyARRaycastHit arHit;
            if (EazyARCoreInterface.ARRaycast(inputPos.x, inputPos.y, out arHit))
            {
                this.InstantiateTrash(arHit);
            }
        }
        else if (item != null)
        {
            Ray ray = Camera.main.ScreenPointToRay(inputPos);
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider != null && hit.collider.CompareTag("Props"))
                {
                    this.draggedItem = hit.collider.GetComponent <ItemHandler>();
                }
            }
        }
    }