Unity3d 如何在增强现实中显示模型放置位置的跟踪线?

Unity3d 如何在增强现实中显示模型放置位置的跟踪线?,unity3d,augmented-reality,arkit,arcore,Unity3d,Augmented Reality,Arkit,Arcore,我希望在我的应用程序中显示模型放置位置的线条,以便用户知道模型在现实世界中的位置。当用户将设备摄像机从模型上移开时,该线将打开以显示模型所在的位置。同样,当检测到模型时,它将关闭。我已经从一个类似的应用程序中附加了一些图片,白色虚线显示了路径。请注意,当检测到模型时,线是如何消失的 LineRenderer lins; public GameObject Lineprefab; private GameObject newline; public Transform startpoint

我希望在我的应用程序中显示模型放置位置的线条,以便用户知道模型在现实世界中的位置。当用户将设备摄像机从模型上移开时,该线将打开以显示模型所在的位置。同样,当检测到模型时,它将关闭。我已经从一个类似的应用程序中附加了一些图片,白色虚线显示了路径。请注意,当检测到模型时,线是如何消失的

 LineRenderer lins;
 public GameObject Lineprefab;
 private GameObject newline;
 public Transform startpoint;

 public Renderer m_rend1;

       bool HitTestWithResultType (ARPoint point, ARHitTestResultType resultTypes) 
       {

          List<ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface ().HitTest (point, resultTypes);

           if (hitResults.Count > 0 && check==true) 
             {

              foreach (var hitResult in hitResults)

                {


            Debug.Log ("Got hit!");
            //obj.Hideplane();
            Genplanes.SetActive(false);

            if (Select == 0) {
                Debug.Log("hit-zero!");
                Instantiate(Instaobj[0], ForSelect);
                check = false;
            }

            if (Select == 1) {
                Debug.Log("hit-one!");
                Instantiate(Instaobj[1], ForSelect);
                check = false;
            }

            if (Select == 2) {
                Debug.Log("hit-two!");
                Instantiate(Instaobj[2], ForSelect);
                check = false;
            }

                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));

                obj.StopPlaneTracking();


                    }


              }
    return false;
        }


       private void Start()
    {

       spawngenerator();
        newline.SetActive(false);
        m_rend1 = GetComponent<MeshRenderer>();


    }







        void spawngenerator()

          {

           GameObject newline = Instantiate(Lineprefab);
           lins = newline.GetComponent<LineRenderer>();


          }


    private void LateUpdate()
        {
        lins.SetPosition(0, startpoint.position);
        lins.SetPosition(1, m_HitTransform.position);

        if( m_rend1.isVisible==true)
        {
            Debug.Log("Render is Visible");
            newline.SetActive(false);   

        }
        else if( m_rend1.isVisible==false)
        {

                newline.SetActive(true);
                Debug.Log("It is InVisible");

            Debug.Log("Render is InVisible");

        }

        }



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)) {
                //we're going to get the position from the contact point
                m_HitTransform.position = hit.point;
                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));

                //and the rotation from the transform of the plane collider
                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) &&  !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
            {
                var screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
                ARPoint point = new ARPoint {
                    x = screenPosition.x,
                    y = screenPosition.y
                };

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

                foreach (ARHitTestResultType resultType in resultTypes)
                {
                    if (HitTestWithResultType (point, resultType))
                    {
                        return;
                    }
                }
            }
        }
        #endif


    }


首先,我先检查模型是否在相机的边界框内

如果对象不可见isVisible==false,则从对象位置到其应结束的位置创建一个线渲染器


终点可以是它前面的一个摄影机子位置,因此它看起来像是从用户到对象的起点。

好的,向我们展示您的代码和问题所在,以便我们提供帮助@Canato更新了代码。线条渲染器问题已解决,但在模型不在屏幕中时显示线条的问题仍然存在。当我点击“播放”按钮时,rend组件m_Rend1在inspector中显示为空。现在线条正在显示…但线条在开始时显示。我只需要在看到渲染器时显示。因为最初渲染器是空的看不见它显示线条。我必须触摸并放置对象,以便最初渲染器不可见。