Exemplo n.º 1
0
    bool HitTestWithResultType(ARPoint point, ARHitTestResultType resultTypes)
    {
        List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);

        if (hitResults.Count > 0)
        {
            foreach (var hitResult in hitResults)
            {
                Debug.Log("Got hit!");
                ResetRotateAnim();
                rb.position = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                rb.rotation = Quaternion.LookRotation(GetLookVector());
                return(true);
            }
        }
        return(false);
    }
Exemplo n.º 2
0
    bool HitTestWithResultType(ARPoint point, ARHitTestResultType resultTypes)
    {
        List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);

        if (hitResults.Count > 0)
        {
            foreach (var hitResult in hitResults)
            {
                hitTransform.position = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                hitTransform.rotation = UnityARMatrixOps.GetRotation(hitResult.worldTransform);
                catControl.MoveTo(hitTransform.position, -0.15f);
                Show();
                return(true);
            }
        }
        return(false);
    }
Exemplo n.º 3
0
    bool HitTestWithResultType1(ARPoint point, ARHitTestResultType resultTypes)
    {
        List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);

        if (hitResults.Count > 0)
        {
            foreach (var hitResult in hitResults)
            {
                //TODO: get the position and rotations to spawn the hat. Done:
                Vector3    pos      = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                Quaternion rotation = UnityARMatrixOps.GetRotation(hitResult.worldTransform);
                spawnedObjects.Add(Instantiate(Terrain, pos, rotation));                    // in order to use for shuffling
                return(true);
            }
        }
        return(false);
    }
Exemplo n.º 4
0
    bool HitTestWithResultType(ARPoint point, ARHitTestResultType resultTypes)
    {
        List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);

        if (hitResults.Count > 0)
        {
            foreach (var hitResult in hitResults)
            {
                Debug.Log("Got hit!");
                GameObject newFood = GameObject.Instantiate(foodTemplate);
                newFood.transform.position = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                foods.Add(newFood);
                return(true);
            }
        }
        return(false);
    }
Exemplo n.º 5
0
    bool HitTestWithResultType(ARPoint point, ARHitTestResultType resultTypes)
    {
        List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);

        if (hitResults.Count > 0)
        {
            foreach (var hitResult in hitResults)
            {
                Debug.Log("Got hit!");
                m_HitTransform.position = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                m_HitTransform.rotation = UnityARMatrixOps.GetRotation(hitResult.worldTransform);
                Debug.Log(string.Format("x:{0:0.######} y:{1:0.######} z:{2:0.######}", m_HitTransform.position.x, m_HitTransform.position.y, m_HitTransform.position.z));
                return(true);
            }
        }
        return(false);
    }
Exemplo n.º 6
0
    public void PlaceOnCamera(Camera camera)
    {
        Vector3 viewPortPos = Vector3.zero;

        switch (_component)
        {
        case ARKitOriginComponent.Origin:
            viewPortPos = Camera.main.ScreenToViewportPoint(Vector3.forward);
            break;

        case ARKitOriginComponent.Right:
            viewPortPos = Camera.main.ScreenToViewportPoint(new Vector3(0.5f, 0.0f, 1.0f));
            break;

        case ARKitOriginComponent.Forward:
            viewPortPos = Camera.main.ScreenToViewportPoint(new Vector3(0.0f, 0.5f, 1.0f));
            break;
        }
        Vector3 screenPosition = Camera.main.ScreenToViewportPoint(viewPortPos);
        ARPoint point          = new ARPoint
        {
            x = screenPosition.x,
            y = screenPosition.y
        };

        // prioritize reults types
        ARHitTestResultType[] resultTypes =
        {
            ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
            ARHitTestResultType.ARHitTestResultTypeHorizontalPlane,
            ARHitTestResultType.ARHitTestResultTypeFeaturePoint
        };
        foreach (ARHitTestResultType resultType in resultTypes)
        {
            UnityARSessionNativeInterface session    = UnityARSessionNativeInterface.GetARSessionNativeInterface();
            List <ARHitTestResult>        hitResults = session.HitTest(point, resultType);
            if (hitResults.Count > 0)
            {
                foreach (var hitResult in hitResults)
                {
                    m_HitTransform.position = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                    m_HitTransform.rotation = UnityARMatrixOps.GetRotation(hitResult.worldTransform);
                }
            }
        }
    }
Exemplo n.º 7
0
        bool HitTestWithResultType(ARPoint point, ARHitTestResultType resultTypes)
        {
            List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);

            if (hitResults.Count > 0)
            {
                foreach (var hitResult in hitResults)
                {
                    Debug.Log("Unity: Got hit!");
                    controller.transform.position = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                    //controller.transform.rotation = UnityARMatrixOps.GetRotation(hitResult.worldTransform);
                    Debug.Log("Unity: " + controller.transform.position + " ; " + controller.transform.eulerAngles);
                    return(true);
                }
            }
            return(false);
        }
    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount > 0)
        {
            var touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
            {
                var     screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
                ARPoint point          = new ARPoint {
                    x = screenPosition.x,
                    y = screenPosition.y
                };

                onHit(point);
            }
        }
    }
Exemplo n.º 9
0
    // Update is called once per frame
    void Update()
    {
        //use center of screen for focusing
        //Vector3 center = new Vector3(Screen.width / 2, Screen.height / 2, findingSquareDist);
        Vector3 center         = centerDot.transform.position;
        var     screenPosition = Camera.main.ScreenToViewportPoint(center);

#if UNITY_EDITOR
        Ray        ray = Camera.main.ScreenPointToRay(center);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, maxRayDistance, collisionLayer))
        {
            hitPoint    = hit.point;
            SquareState = FocusState.Found;
            return;
        }
#else
        ARPoint point = new ARPoint
        {
            x = screenPosition.x,
            y = screenPosition.y
        };

        // prioritize reults types
        ARHitTestResultType[] resultTypes =
        {
            ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
            // if you want to use infinite planes use this:
            //ARHitTestResultType.ARHitTestResultTypeExistingPlane,
            //ARHitTestResultType.ARHitTestResultTypeHorizontalPlane,
            //ARHitTestResultType.ARHitTestResultTypeFeaturePoint
        };

        foreach (ARHitTestResultType resultType in resultTypes)
        {
            if (HitTestWithResultType(point, resultType))
            {
                SquareState = FocusState.Found;
                return;
            }
        }
#endif
        //if you got here, we have not found a plane, so if camera is facing below horizon, display the focus "finding" square
        SquareState = FocusState.Finding;
    }
Exemplo n.º 10
0
    // Update is called once per frame
    void Update()
    {
                #if UNITY_EDITOR   //we will only use this script on the editor side, though there is nothing that would prevent it from working on device
        if (Input.GetMouseButtonDown(0))
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            //we'll try to hit one of the plane collider gameobjects that were generated by the plugin
            //effectively similar to calling HitTest with ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent
            if (Physics.Raycast(ray, out hit, maxRayDistance, collisionLayer))
            {
                CreateBall(new Vector3(hit.point.x, hit.point.y + createHeight, hit.point.z));

                //we're going to get the position from the contact point
                Debug.Log(string.Format("x:{0:0.######} y:{1:0.######} z:{2:0.######}", hit.point.x, hit.point.y, hit.point.z));
            }
        }
                #else
        if (Input.touchCount > 0)
        {
            var touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Began)
            {
                var     screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
                ARPoint point          = new ARPoint {
                    x = screenPosition.x,
                    y = screenPosition.y
                };

                List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point,
                                                                                                                        ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent);
                if (hitResults.Count > 0)
                {
                    foreach (var hitResult in hitResults)
                    {
                        Vector3 position = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                        CreateBall(new Vector3(position.x, position.y + createHeight, position.z));
                        break;
                    }
                }
            }
        }
                #endif
    }
Exemplo n.º 11
0
        protected override void Update()
        {
            base.Update();

            LinkedList <ARPlaneAnchorGameObject> arpags = unityARAnchorManager.GetCurrentPlaneAnchors();
            Touch touch;

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

            Vector3 screenPosition = mainCamera.ScreenToViewportPoint(touch.position);
            ARPoint point          = new ARPoint
            {
                x = screenPosition.x,
                y = screenPosition.y
            };

            // prioritize reults types
            ARHitTestResultType[] resultTypes =
            {
                ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
                // if you want to use infinite planes use this:
                //ARHitTestResultType.ARHitTestResultTypeExistingPlane,
                ARHitTestResultType.ARHitTestResultTypeEstimatedHorizontalPlane,
                ARHitTestResultType.ARHitTestResultTypeEstimatedVerticalPlane,
                ARHitTestResultType.ARHitTestResultTypeFeaturePoint
                //ARHitTestResultType.ARHitTestResultTypeFeaturePoint
            };

            for (int i = 0; i < resultTypes.Length; ++i)
            {
                List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes[i]);
                if (hitResults.Count > 0)
                {
                    Matrix4x4 matrix        = hitResults[0].worldTransform;
                    var       touchedObject = Instantiate(AppearTouchPrefab, UnityARMatrixOps.GetPosition(matrix), UnityARMatrixOps.GetRotation(matrix));
                    touchedObject.transform.LookAt(mainCamera.transform);
                    touchedObject.transform.rotation = Quaternion.Euler(0.0f, touchedObject.transform.rotation.eulerAngles.y, touchedObject.transform.rotation.z);
                    touchedObject.transform.parent   = fieldObjectAnchorRoot.transform;
                    break;
                }
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Performs a Raycast against a plane.
        /// </summary>
        /// <param name="camera">The AR camera being used.</param>
        /// <param name="x">The x screen position.</param>
        /// <param name="y">The y screen position.</param>
        /// <param name="hitPose">The resulting hit pose if the method returns <c>true</c>.</param>
        /// <returns><c>true</c> if a plane was hit. Otherwise <c>false</c>.</returns>
        public bool RaycastPlane(Camera camera, float x, float y, out Pose hitPose)
        {
            hitPose = new Pose();
#if ARCORE_IOS_SUPPORT
            var session = UnityARSessionNativeInterface.GetARSessionNativeInterface();

            var     viewportPoint = camera.ScreenToViewportPoint(new Vector2(x, y));
            ARPoint arPoint       = new ARPoint
            {
                x = viewportPoint.x,
                y = viewportPoint.y
            };

            m_HitResultList = session.HitTest(arPoint, ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent);
            if (m_HitResultList.Count > 0)
            {
                int minDistanceIndex = 0;
                for (int i = 1; i < m_HitResultList.Count; i++)
                {
                    if (m_HitResultList[i].distance < m_HitResultList[minDistanceIndex].distance)
                    {
                        minDistanceIndex = i;
                    }
                }

                hitPose.position = UnityARMatrixOps.GetPosition(m_HitResultList[minDistanceIndex].worldTransform);

                // Original ARKit hit pose is the plane rotation.
                Quaternion planeRotation = UnityARMatrixOps.GetRotation(
                    m_HitResultList[minDistanceIndex].worldTransform);

                // Try to match the hit rotation to the one ARCore uses.
                Vector3 planeNormal     = planeRotation * Vector3.up;
                Vector3 rayDir          = camera.ViewportPointToRay(viewportPoint).direction;
                Vector3 planeProjection = Vector3.ProjectOnPlane(rayDir, planeNormal);
                Vector3 forwardDir      = -planeProjection.normalized;

                Quaternion hitRotation = Quaternion.LookRotation(forwardDir, planeNormal);
                hitPose.rotation = hitRotation;

                return(true);
            }
#endif
            return(false);
        }
Exemplo n.º 13
0
    bool HitTestWithResultType(ARPoint point, ARHitTestResultType resultTypes)
    {
        List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);

        if (hitResults.Count > 0)
        {
            foreach (var hitResult in hitResults)
            {
                //TODO: get the position and rotations to spawn the hat
                Vector3    pos = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                Quaternion rot = UnityARMatrixOps.GetRotation(hitResult.worldTransform);
                GameObject go  = Instantiate(hitPrefab, pos, rot);
                spawnedObjects.Add(go);
                return(true);
            }
        }
        return(false);
    }
Exemplo n.º 14
0
    bool GetGroundPlane(ARPoint point, ARHitTestResultType resultTypes)
    {
        List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);

        if (hitResults.Count > 0)
        {
            foreach (var hitResult in hitResults)
            {
                groundPlaneAnchorIdentifier = hitResult.anchorIdentifier;
                groundPlanePosition         = new Vector3(0f, UnityARMatrixOps.GetPosition(hitResult.worldTransform).y, 0f);
                CreateGroundPlane(groundPlanePosition);
                aimTarget.SetActive(true);
                return(true);
            }
        }

        return(false);
    }
Exemplo n.º 15
0
    bool HitTestWithResultType(ARPoint point, ARHitTestResultType resultTypes)
    {
        Camera camera = Camera.main;
        List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);

        if (hitResults.Count > 0)
        {
            anchor.position = UnityARMatrixOps.GetPosition(hitResults[0].worldTransform);
            //anchor.rotation = UnityARMatrixOps.GetRotation (hitResult.worldTransform);

            anchor.LookAt(anchor.position + camera.transform.rotation * Vector3.forward,
                          camera.transform.rotation * Vector3.up);

            Debug.Log(string.Format("x:{0:0.######} y:{1:0.######} z:{2:0.######}", anchor.position.x, anchor.position.y, anchor.position.z));
            return(true);
        }
        return(false);
    }
Exemplo n.º 16
0
    void Update()
    {
                        #if UNITY_EDITOR
        if (Input.GetMouseButtonDown(0))
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, maxRayDistance, collisionLayer))
            {
                m_HitTransform.position = hit.point;
                m_HitTransform.rotation = hit.transform.rotation;
            }
        }
                        #else
        if (Input.touchCount > 0 && m_HitTransform != null)
        {
            var touch = Input.GetTouch(0);
            if ((touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) && !created)
            {
                var     screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
                ARPoint point          = new ARPoint {
                    x = screenPosition.x,
                    y = screenPosition.y
                };

                ARHitTestResultType[] resultTypes =
                {
                    ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
                    ARHitTestResultType.ARHitTestResultTypeHorizontalPlane,
                    ARHitTestResultType.ARHitTestResultTypeFeaturePoint
                };

                foreach (ARHitTestResultType resultType in resultTypes)
                {
                    if (HitTestWithResultType(point, resultType))
                    {
                        return;
                    }
                }
            }
        }
                        #endif
    }
Exemplo n.º 17
0
    //-----------------------------------
    // Update function checks for hittest
    //-----------------------------------

    void Update()
    {
        // Check if the screen is touched
        //-----------------------------------

        if (Input.touchCount > 0)
        {
            var touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Began)
            {
                if (EventSystem.current.currentSelectedGameObject == null)
                {
                    Debug.Log("Not touching a UI button. Moving on.");

                    // add new shape
                    var     screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
                    ARPoint point          = new ARPoint
                    {
                        x = screenPosition.x,
                        y = screenPosition.y
                    };

                    // prioritize reults types
                    ARHitTestResultType[] resultTypes =
                    {
                        //ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
                        //ARHitTestResultType.ARHitTestResultTypeExistingPlane,
                        //ARHitTestResultType.ARHitTestResultTypeEstimatedHorizontalPlane,
                        //ARHitTestResultType.ARHitTestResultTypeEstimatedVerticalPlane,
                        ARHitTestResultType.ARHitTestResultTypeFeaturePoint
                    };

                    foreach (ARHitTestResultType resultType in resultTypes)
                    {
                        if (HitTestWithResultType(point, resultType))
                        {
                            Debug.Log("Found a hit test result");
                            return;
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 18
0
    bool HitTestWithResultType(ARPoint point, ARHitTestResultType resultTypes)
    {
        List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);

        if (hitResults.Count > 0)
        {
            foreach (var hitResult in hitResults)
            {
                Vector3 pos = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                placementPrompt.SetHit(pos);
                placementPrompt.gameObject.SetActive(true);
                introText.gameObject.SetActive(false);
                introTextPanel.gameObject.SetActive(false);
                MenuBTN.gameObject.SetActive(true);
                return(true);
            }
        }
        return(false);
    }
Exemplo n.º 19
0
 /// <summary>
 /// checks hits in order of priority
 /// returns true on first valid hit and saves in out variables
 /// </summary>
 /// <param name="viewportPoint"></param>
 /// <param name="resultTypesPrioriy"></param>
 /// <param name="hitPos"></param>
 /// <param name="hitRotaion"></param>
 /// <returns></returns>
 public static bool getHitData(ARPoint viewportPoint, ARHitTestResultType[] resultTypesPrioriy, out Vector3 hitPos, out Quaternion hitRotaion)
 {
     foreach (var resultType in resultTypesPrioriy)
     {
         List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(viewportPoint, resultType);
         if (hitResults.Count > 0)
         {
             foreach (var hitResult in hitResults)
             {
                 hitPos     = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                 hitRotaion = UnityARMatrixOps.GetRotation(hitResult.worldTransform);
                 return(true);
             }
         }
     }
     hitRotaion = Quaternion.identity;
     hitPos     = Vector3.zero;
     return(false);
 }
Exemplo n.º 20
0
    bool HitTestWithResultType(ARPoint point, ARHitTestResultType resultTypes, GameObject target)
    {
        List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);

        if (hitResults.Count > 0)
        {
            foreach (var hitResult in hitResults)
            {
                Debug.Log("Got hit!");
                target.transform.position = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                Vector3 up      = Vector3.up;
                Vector3 right   = Vector3.Cross(up, Camera.main.transform.forward).normalized;
                Vector3 forward = -Vector3.Cross(right, up).normalized;
                target.transform.rotation = Quaternion.LookRotation(forward, up);
                return(true);
            }
        }
        return(false);
    }
Exemplo n.º 21
0
    void Update()
    {
        if (Input.touchCount > 0)
        {
            var touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Began)
            {
                var     screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
                ARPoint arPoint        = new ARPoint
                {
                    x = screenPosition.x,
                    y = screenPosition.y
                };

                // 平面との当たり判定
                HitTest(arPoint);
            }
        }
    }
Exemplo n.º 22
0
    private bool HitTestWithResultType(ARPoint point, ARHitTestResultType arHitTestResultTypes)
    {
        List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, arHitTestResultTypes);

        if (hitResults.Count > 0)
        {
            anchorSet = true;

            ChildrenSetActive(parentGameObject, true);

            parentGameObject.transform.position = UnityARMatrixOps.GetPosition(hitResults[0].worldTransform);
            parentGameObject.transform.rotation = UnityARMatrixOps.GetRotation(hitResults[0].worldTransform);
            parentGameObject.transform.LookAt(new Vector3(mainCamera.transform.position.x, parentGameObject.transform.position.y, mainCamera.transform.position.z));

            return(true);
        }

        return(false);
    }
Exemplo n.º 23
0
    /// <summary>
    /// 基于Arkit的触碰移动类型
    /// </summary>
    private void TouchMoveType1()
    {
        Touch   touch          = Input.GetTouch(0);
        Vector3 screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
        ARPoint point          = new ARPoint
        {
            x = screenPosition.x,
            y = screenPosition.y
        };

        ARHitTestResultType[] resultTypes =
        {
            ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
            // if you want to use infinite planes use this:
            //ARHitTestResultType.ARHitTestResultTypeExistingPlane,
            ARHitTestResultType.ARHitTestResultTypeHorizontalPlane,
            ARHitTestResultType.ARHitTestResultTypeFeaturePoint
        };
        if (touch.phase == TouchPhase.Began)
        {
            Debug.Log("TouchPhase.Began touchOffset=" + touchOffset);
            foreach (ARHitTestResultType resultType in resultTypes)
            {
                if (hitStartShowPerfab(point, resultType))
                {
                    return;
                }
            }
        }
        if (touch.phase == TouchPhase.Moved)
        {
            foreach (ARHitTestResultType resultType in resultTypes)
            {
                if (hitMoveShowPerfab(point, resultType))
                {
                    return;
                }
            }
        }
        if (touch.phase == TouchPhase.Ended)
        {
        }
    }
Exemplo n.º 24
0
    void Spawn()
    {
        //屏幕坐标转视口坐标
        //手指点击位置
        //Vector3 screenPos = Camera.main.ScreenToViewportPoint(Input.GetTouch(0).position);
        //屏幕中心位置
        Vector3 screenPos = Camera.main.ScreenToViewportPoint(new Vector2(Screen.width / 2, Screen.height / 2));

        ARPoint point = new ARPoint
        {
            x = screenPos.x,
            y = screenPos.y
        };

        // 处理捕捉类型,点还是面,控制虚拟物体可以在哪里创建
        ARHitTestResultType[] resultTypes =
        {
            //ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingGeometry, //考虑尺寸和形状(没三角面),必须在Plane内创建。Plane带形状的??
            //ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent, //尊重平面有限尺寸,只能创建在Plane中,同一高度会自动合并
            // if you want to use infinite planes use this:
            ARHitTestResultType.ARHitTestResultTypeExistingPlane, //不考虑屏幕大小,必须先扫出至少一个Plane,可以创建在Plane外面,同一高度临近平面自动合并
            //ARHitTestResultType.ARHitTestResultTypeEstimatedHorizontalPlane, //搜索过程中??
            //ARHitTestResultType.ARHitTestResultTypeEstimatedVerticalPlane, //搜索过程中??
            ARHitTestResultType.ARHitTestResultTypeFeaturePoint //Plane会出现,没有意义,可以在任意空中创建
        };

        foreach (var type in resultTypes)
        {
            List <ARHitTestResult> hitResults = m_session.HitTest(point, type);
            Debug.Log(type.ToString() + ": " + hitResults.Count);
            if (hitResults.Count > 0)
            {
                if (avatar == null)
                {
                    avatar = Instantiate(prefab);
                }
                avatar.transform.position             = UnityARMatrixOps.GetPosition(hitResults[0].worldTransform);
                avatar.transform.rotation             = UnityARMatrixOps.GetRotation(hitResults[0].worldTransform);
                avatar.GetComponent <TextMesh>().text = result;
            }
        }
    }
Exemplo n.º 25
0
    // Update is called once per frame
    void Update()
    {
        Vector3 center         = new Vector3(Screen.width / 2, Screen.height / 2, 0);
        var     screenPosition = Camera.main.ScreenToViewportPoint(center);

#if UNITY_EDITOR
        Ray        ray = Camera.main.ScreenPointToRay(center);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, maxRayDistance, collisionLayer))
        {
            hitPoint           = hit.point;
            transform.position = hitPoint;
            AdjustFoundcircleDeriction(center);
            return;
        }
#else
        ARPoint point = new ARPoint
        {
            x = screenPosition.x,
            y = screenPosition.y
        };

        // prioritize reults types
        ARHitTestResultType[] resultTypes =
        {
            ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
            // if you want to use infinite planes use this:
            //ARHitTestResultType.ARHitTestResultTypeExistingPlane,
            //ARHitTestResultType.ARHitTestResultTypeHorizontalPlane,
            //ARHitTestResultType.ARHitTestResultTypeFeaturePoint
        };

        foreach (ARHitTestResultType resultType in resultTypes)
        {
            if (HitTestWithResultType(point, resultType))
            {
                AdjustFoundcircleDeriction(center);
                return;
            }
        }
#endif
    }
Exemplo n.º 26
0
    bool HitTestWithResultType(ARPoint point, ARHitTestResultType resultTypes)
    {
        List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);

        if (hitResults.Count > 0)
        {
            foreach (var hitResult in hitResults)
            {
                Vector3    pos    = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                Quaternion rot    = UnityARMatrixOps.GetRotation(hitResult.worldTransform);
                Vector3    normal = rot * Vector3.up;
                currDoor.transform.position = pos + normal * 1.451f;
                currDoor.transform.rotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(pos - mainCamera.transform.position, rot * Vector3.up));

                currDoor.GetComponentInParent <Portal>().Source.transform.localPosition = currDoor.transform.position;
                return(true);
            }
        }
        return(false);
    }
Exemplo n.º 27
0
    public bool GetWorldPointFromTouchPosition(Vector2 touchposition, out Vector3 worldPosition)
    {
        Vector2 viewPortPoint = Camera.main.ScreenToViewportPoint(touchposition);
        ARPoint point         = new ARPoint
        {
            x = viewPortPoint.x,
            y = viewPortPoint.y
        };

        foreach (ARHitTestResultType resultType in resultTypes)
        {
            if (HitTestWithResultType(point, resultType))
            {
                worldPosition = result;
                return(true);
            }
        }
        worldPosition = result;
        return(false);
    }
Exemplo n.º 28
0
        bool HitTestWithResultType(ARPoint point, ARHitTestResultType resultTypes)
        {
            List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);

            if (hitResults.Count > 0)
            {
                foreach (var hitResult in hitResults)
                {
                    Debug.Log("Got hit!");
                    transform.position = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                    transform.rotation = UnityARMatrixOps.GetRotation(hitResult.worldTransform);
                    // make new placement face camera
                    transform.LookAt(Camera.main.transform.position);
//					transform.eulerAngles = new Vector3 (0, transform.eulerAngles.y, 0);
                    Debug.Log(string.Format("x:{0:0.######} y:{1:0.######} z:{2:0.######}", transform.position.x, transform.position.y, transform.position.z));
                    return(true);
                }
            }
            return(false);
        }
        private Vector3 GetHitPosition_IOS()
        {
            var     screenPosition = Camera.main.ScreenToViewportPoint(new Vector3(0.5f, 0.5f));
            ARPoint point          = new ARPoint
            {
                x = screenPosition.x,
                y = screenPosition.y
            };
            var hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, ARHitTestResultType.ARHitTestResultTypeEstimatedHorizontalPlane | ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent);

            if (hitResults.Count > 0)
            {
                // The hitTest method sorts the resulting list by increasing distance from the camera
                // The first hit result will usually be the most relevant when responding to user input
                ARHitTestResult hitResult = hitResults[0];
                hitPosition = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
            }

            return(hitPosition);
        }
Exemplo n.º 30
0
    public void SetPosition()
    {
        // Project from the middle of the screen to look for a hit point on the detected surfaces.
        var     screenPosition = Camera.main.ScreenToViewportPoint(new Vector2(Screen.width / 2f, Screen.height / 2f));
        ARPoint pt             = new ARPoint {
            x = screenPosition.x,
            y = screenPosition.y
        };

        // Try to hit within the bounds of an existing AR plane.
        List <ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(
            pt,
            ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent);

        if (hitResults.Count > 0)           // If a hit is found, set the position and reset the rotation.
        {
            transform.rotation = Quaternion.Euler(Vector3.zero);
            transform.position = UnityARMatrixOps.GetPosition(hitResults[0].worldTransform);
        }
    }