void Update()
    {
        if (Input.GetMouseButtonDown(0) && CheckPanel.CheckPanel())
        {
            Ray        mRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit mHit;
            //射线检验
            LayerMask mask = 1 << LayerMask.NameToLayer("Sphere");//只在Sphere层检测
            if (Physics.Raycast(mRay, out mHit, 20f, mask.value) || CheckUIClick.OnClickUI())
            {
                ;
            }
            else
            {
                var touch = Input.GetTouch(0);
                if (touch.phase == TouchPhase.Began)
                {
                    Vector3 screenPos = Camera.main.ScreenToViewportPoint(touch.position);
                    ARPoint point     = new ARPoint
                    {
                        x = screenPos.x,
                        y = screenPos.y
                    };

                    List <ARHitTestResult> hitTestResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, ARHitTestResultType.ARHitTestResultTypeExistingPlane);
                    if (flag == false && hitTestResults.Count > 0)
                    {
                        UnityARCameraManager.CloseDetection();
                        flag = true;
                        GeneratePlane.GetComponent <FocusSquare>().enabled = false;
                        FocusSquare.SetActive(false);
                        HintController.ShowElement(3);
                    }
                    else if (flag && hitTestResults.Count > 0)
                    {
                        HintController.ShowElement(4);
                        hintFinished = true;
                    }

                    /* == 第一次点击的时候关闭平面检测 */
                    if (currentObj[0].activeSelf == false)
                    {
                        //currentObj[0].transform.position = UnityARMatrixOps.GetPosition(hitTestResults[hitTestResults.Count - 1].worldTransform) + Vector3.up * currentObj[0].transform.localScale.y*0.1f;

                        //currentObj[0].transform.position = UnityARMatrixOps.GetPosition(hitTestResults[hitTestResults.Count - 1].worldTransform);
                        //currentObj[0].transform.rotation = UnityARMatrixOps.GetRotation(hitTestResults[hitTestResults.Count - 1].worldTransform);

                        var player = ClientScene.localPlayers[0].gameObject.GetComponent <Player>();
                        player.CheckAuthority(GetComponent <NetworkIdentity>(), player.GetComponent <NetworkIdentity>());
                        CmdChangeTransform(0, UnityARMatrixOps.GetPosition(hitTestResults[hitTestResults.Count - 1].worldTransform), UnityARMatrixOps.GetRotation(hitTestResults[hitTestResults.Count - 1].worldTransform));
                        //currentObj[0].GetComponent<Touch>().ActiveBall();

                        //if (currentObj[1].activeSelf == true) currentObj[0].transform.position = new Vector3(currentObj[0].transform.position.x, currentObj[1].transform.position.y, currentObj[0].transform.position.z);
                    }
                    else if (currentObj[1].activeSelf == false)
                    {
                        //currentObj[1].transform.position = UnityARMatrixOps.GetPosition(hitTestResults[hitTestResults.Count - 1].worldTransform) + Vector3.up * currentObj[1].transform.localScale.y*0.1f;

                        //currentObj[1].transform.position = UnityARMatrixOps.GetPosition(hitTestResults[hitTestResults.Count - 1].worldTransform);
                        //currentObj[1].transform.rotation = UnityARMatrixOps.GetRotation(hitTestResults[hitTestResults.Count - 1].worldTransform);

                        var player = ClientScene.localPlayers[0].gameObject.GetComponent <Player>();
                        player.CheckAuthority(GetComponent <NetworkIdentity>(), player.GetComponent <NetworkIdentity>());
                        CmdChangeTransform(1, UnityARMatrixOps.GetPosition(hitTestResults[hitTestResults.Count - 1].worldTransform), UnityARMatrixOps.GetRotation(hitTestResults[hitTestResults.Count - 1].worldTransform));
                        //currentObj[1].GetComponent<Touch>().ActiveBall();

                        //if (currentObj[0].activeSelf == true) currentObj[1].transform.position = new Vector3(currentObj[1].transform.position.x, currentObj[0].transform.position.y, currentObj[1].transform.position.z);
                    }
                    else
                    {
                        if (NoneUIClickEvent != null)
                        {
                            NoneUIClickEvent();
                        }
                    }
                }
            }
        }

        if (hintFinished)
        {
            time += Time.deltaTime;
            if (time > thresholdTime)
            {
                HintController.HideElement();
                hintFinished = false;// not really unfinished
            }
        }
    }
Exemple #2
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);

                #if UNITY_EDITOR
        Ray        ray = Camera.main.ScreenPointToRay(center);
        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, collisionLayerMask))
        {
            //we're going to get the position from the contact point
            foundSquare.transform.position = hit.point;
            Debug.Log(string.Format("x:{0:0.######} y:{1:0.######} z:{2:0.######}", foundSquare.transform.position.x, foundSquare.transform.position.y, foundSquare.transform.position.z));

            //and the rotation from the transform of the plane collider
            SquareState = FocusState.Found;
            foundSquare.transform.rotation = hit.transform.rotation;
            return;
        }
        #else
        var     screenPosition = Camera.main.ScreenToViewportPoint(center);
        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;
                HintController.ShowElement(2);
                return;
            }
        }
        #endif
        //if you got here, we have not found a plane, so if camera is facing below horizon, display the focus "finding" square
        if (trackingInitialized)
        {
            SquareState = FocusState.Finding;

            //check camera forward is facing downward
            if (Vector3.Dot(Camera.main.transform.forward, Vector3.down) > 0)
            {
                //position the focus finding square a distance from camera and facing up
                findingSquare.transform.position = Camera.main.ScreenToWorldPoint(center);

                //vector from camera to focussquare
                Vector3 vecToCamera = findingSquare.transform.position - Camera.main.transform.position;

                //find vector that is orthogonal to camera vector and up vector
                Vector3 vecOrthogonal = Vector3.Cross(vecToCamera, Vector3.up);

                //find vector orthogonal to both above and up vector to find the forward vector in basis function
                Vector3 vecForward = Vector3.Cross(vecOrthogonal, Vector3.up);


                findingSquare.transform.rotation = Quaternion.LookRotation(vecForward, Vector3.up);
            }
            else
            {
                //we will not display finding square if camera is not facing below horizon
                findingSquare.SetActive(false);
            }
        }
    }
Exemple #3
0
    // Update is called once per frame
    void Update()
    {
        //CheckButton();
        //Debug.Log("模型ActiveFalse");
        if (Model.activeSelf == false)
        {
            if (Input.GetMouseButtonDown(0))
            {
                var touch = Input.GetTouch(0);
                if (touch.phase == TouchPhase.Began)
                {
                    //Debug.Log("执行HitTest");
                    Vector3 screenPos = Camera.main.ScreenToViewportPoint(touch.position);
                    ARPoint point     = new ARPoint
                    {
                        x = screenPos.x,
                        y = screenPos.y
                    };
                    List <ARHitTestResult> hitTestResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, ARHitTestResultType.ARHitTestResultTypeExistingPlane);
                    if (flag == false && hitTestResults.Count > 0)
                    {
                        UnityARCameraManager.CloseDetection();
                        flag = true;
                        GeneratePlane.GetComponent <FocusSquare>().enabled = false;
                        FocusSquare.SetActive(false);
                        HintController.ShowElement(3);
                    }
                    /* == 第一次点击的时候关闭平面检测 */

                    //Debug.Log("放置模型");

                    /* UNET */
                    var player = ClientScene.localPlayers[0].gameObject.GetComponent <Player>();
                    player.CheckAuthority(GetComponent <NetworkIdentity>(), player.GetComponent <NetworkIdentity>());
                    CmdChangeTransform(UnityARMatrixOps.GetPosition(hitTestResults[hitTestResults.Count - 1].worldTransform), UnityARMatrixOps.GetRotation(hitTestResults[hitTestResults.Count - 1].worldTransform));
                    CmdSetModelActive();
                    CmdSetConfirmButtonActive();
                    /* == UNET */

                    //Button.SetActive(true);
                }
            }
        }
        else if (adjustable)
        {
            if (Input.touchCount == 2)
            {
                var touch1 = Input.GetTouch(0);
                var touch2 = Input.GetTouch(1);
                if (touch1.phase == TouchPhase.Moved && touch2.phase == TouchPhase.Moved)
                {
                    Vector3 screenPos = Camera.main.ScreenToViewportPoint((touch1.position + touch2.position) / 2);
                    ARPoint point     = new ARPoint
                    {
                        x = screenPos.x,
                        y = screenPos.y
                    };
                    List <ARHitTestResult> hitTestResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, ARHitTestResultType.ARHitTestResultTypeExistingPlane);
                    //Model.transform.position = UnityARMatrixOps.GetPosition(hitTestResults[hitTestResults.Count - 1].worldTransform);

                    /* UNET */
                    var player = ClientScene.localPlayers[0].gameObject.GetComponent <Player>();
                    player.CheckAuthority(GetComponent <NetworkIdentity>(), player.GetComponent <NetworkIdentity>());
                    CmdChangeTransform(UnityARMatrixOps.GetPosition(hitTestResults[hitTestResults.Count - 1].worldTransform), Model.transform.rotation);
                    /* == UNET */
                }

                //保证重新开始双指触摸记录的初始位置不是松开手时的位置,而是初始触摸的位置
                if (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(1).phase == TouchPhase.Began)
                {
                    //记录初始位置
                    oldPosition1 = Input.GetTouch(0).position;
                    oldPosition2 = Input.GetTouch(1).position;
                }

                if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
                {
                    //计算出当前两点触摸点的位置
                    var tempPosition1 = Input.GetTouch(0).position;
                    var tempPosition2 = Input.GetTouch(1).position;

                    float currentTouchDistance = Vector2.Distance(tempPosition1, tempPosition2);
                    float lastTouchDistance    = Vector2.Distance(oldPosition1, oldPosition2);

                    //计算上次和这次双指触摸之间的距离差距
                    //然后去更改摄像机的距离
                    float   distance    = currentTouchDistance - lastTouchDistance;
                    float   scaleFactor = distance / 200f;
                    Vector3 localScale  = Model.transform.localScale;
                    Vector3 scale       = new Vector3(localScale.x + scaleFactor,
                                                      localScale.y + scaleFactor,
                                                      localScale.z + scaleFactor);
                    //在什么情况下进行缩放
                    if (scale.x >= 0.2f && scale.y >= 0.2f && scale.z >= 0.2f)
                    {
                        //Model.transform.localScale = scale;
                        var player = ClientScene.localPlayers[0].gameObject.GetComponent <Player>();
                        player.CheckAuthority(GetComponent <NetworkIdentity>(), player.GetComponent <NetworkIdentity>());
                        CmdChangeScale(scale);
                    }
                    //备份上一次触摸点的位置,用于对比
                    //也是松开手时的位置
                    oldPosition1 = tempPosition1;
                    oldPosition2 = tempPosition2;
                }

                //if (Vector2.Dot(touch1.deltaPosition,touch2.deltaPosition)<=0 && touch1.position.y < touch2.position.y)
                //{
                //    Model.transform.Rotate(Vector3.down * touch1.deltaPosition.x * 0.75f, Space.World);
                //}
                //else if (Vector2.Dot(touch1.deltaPosition, touch2.deltaPosition) <= 0 && touch1.position.y > touch2.position.y)
                //{
                //    Model.transform.Rotate(Vector3.down * touch2.deltaPosition.x * 0.75f, Space.World);
                //}
            }
            else if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                Vector2 deltaPos = Input.GetTouch(0).deltaPosition;
                //Model.transform.Rotate(Vector3.down * deltaPos.x * 0.5f, Space.World);
                var player = ClientScene.localPlayers[0].gameObject.GetComponent <Player>();
                player.CheckAuthority(GetComponent <NetworkIdentity>(), player.GetComponent <NetworkIdentity>());
                CmdRotate(deltaPos);
            }
        }
        if (hintFinished)
        {
            time += Time.deltaTime;
            if (time > thresholdTime)
            {
                HintController.HideElement();
                hintFinished = false;// not really unfinished
            }
        }
    }