Exemplo n.º 1
0
        /// <summary>
        /// Update handler in Manager mode.
        /// </summary>
        void ManagerWork()
        {
            // Debug.Log("Manager");
            // If the player has not touched the screen, we are done with this update.
            Touch touch;

            if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
            {
                return;
            }

            // Should not handle input if the player is pointing on UI.
            if (EventSystem.current.IsPointerOverGameObject(touch.fingerId))
            {
                return;
            }

            // Raycast against the location the player touched to search for planes.
            TrackableHit      hit;
            TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
                                              TrackableHitFlags.FeaturePointWithSurfaceNormal;

            if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit))
            {
                // Use hit pose and camera pose to check if hittest is from the
                // back of the plane, if it is, no need to create the anchor.
                if ((hit.Trackable is DetectedPlane) &&
                    Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position,
                                hit.Pose.rotation * Vector3.up) < 0)
                {
                    Debug.Log("Hit at back of the current DetectedPlane");
                }
                else
                {
                    // Choose the prefab based on the Trackable that got hit.
                    Debug.Log("hitted");
                    GameObject prefab;
                    if (hit.Trackable is FeaturePoint)
                    {
                        prefab = GameObjectPointPrefab;
                    }
                    else if (hit.Trackable is DetectedPlane)
                    {
                        DetectedPlane detectedPlane = hit.Trackable as DetectedPlane;
                        if (detectedPlane.PlaneType == DetectedPlaneType.Vertical)
                        {
                            prefab = GameObjectVerticalPlanePrefab;
                        }
                        else
                        {
                            prefab = GameObjectHorizontalPlanePrefab;
                        }
                    }
                    else
                    {
                        prefab = GameObjectHorizontalPlanePrefab;
                    }

                    // Instantiate prefab at the hit pose.
                    var AnchorGameObject = Instantiate(prefab, hit.Pose.position, hit.Pose.rotation);
                    AnchorGameObject.transform.parent = GameObject.Find("Root").transform.Find("Anchor");

                    // Compensate for the hitPose rotation facing away from the raycast (i.e.
                    // camera).
                    AnchorGameObject.transform.Rotate(0, k_PrefabRotation, 0, Space.Self);

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

                    anchor.transform.parent = GameObject.Find("Root").transform.Find("Anchor");

                    // Make game object a child of the anchor.
                    AnchorGameObject.transform.parent    = anchor.transform;
                    AnchorGameObject.transform.rotation *= Quaternion.Euler(0, 180, 0);

                    Vector3 temp;
                    if (model.GetAnchorsInCurrentRoute().Count == 0)
                    {
                        temp = anchor.transform.position;
                    }
                    else
                    {
                        temp = model.GetAnchorsInCurrentRoute().Last()._postion;
                    }
                    model.AddAnchorToCurrentRouter(anchor);
                    CreateArrow(temp, anchor.transform.position);

                    AnchorGameObject.GetComponentInChildren <TextMesh>().text = model.GetAnchorsInCurrentRoute().Count.ToString();//顯示編號
                }
            }
        }