在ARCore unity中禁用/切换跟踪平面的可视化 [英] Disable/Toggle visualization of tracked planes in ARCore unity

查看:155
本文介绍了在ARCore unity中禁用/切换跟踪平面的可视化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在寻找ARCore Unity的代码,我想做一个简单的任务,即拥有一个切换按钮,以便用户可以将对象放置在场景中,同时知道被跟踪的对象在哪里放置.飞机是可见的,并且一旦用户放置了对象,便可以在视觉上禁用跟踪的飞机,从而使它看起来更逼真.我可以在Android Studio中通过在主HelloArActivity.java中执行以下操作来做到这一点:

I have been looking on the code for ARCore Unity for a while and I want to do one simple task that is, to have a toggle button so user can place an object in the scene while knowing where to place it while the tracked planes are visible and once the user places the object, he is given the option of just visually disabling the tracked planes so it looks more realistic. I was able to do this in Android Studio by doing something like this in the main HelloArActivity.java:

if (planeToggle) {
                mPlaneRenderer.drawPlanes(mSession.getAllPlanes(), frame.getPose(), projmtx);
            }

这真的很简单.我制作了一个名为planeToggle的布尔型,并将mPlaneRenderer.drawPlanes函数置于if条件中.当布尔值为true时,它显示飞机,而布尔值为false时,它不显示飞机.

this was really simple. I made a bool named planeToggle and just placed the mPlaneRenderer.drawPlanes function in an if condition. When the bool is true it displays the planes and when its false, it does not...

但是,我对Unity感到困惑.我在HelloARController.cs中做了这样的事情:

However, with Unity I am confused. I did something like this in the HelloARController.cs :

我做了一个按钮来切换飞机. 设置一个事件侦听器以切换一个布尔变量,并执行以下操作:

I made a button to togglePlanes. Set an event listener to it to toggle a boolean variable and did something like this :

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.

                GameObject planeObject = Instantiate(m_trackedPlanePrefab, Vector3.zero, Quaternion.identity,
                        transform);
                    planeObject.GetComponent<TrackedPlaneVisualizer>().SetTrackedPlane(m_newPlanes[i]);

                    m_planeColors[0].a = 0;

                    // Apply a random color and grid rotation.
                    planeObject.GetComponent<Renderer>().material.SetColor("_GridColor", m_planeColors[0]);
                    planeObject.GetComponent<Renderer>().material.SetFloat("_UvRotation", Random.Range(0.0f, 360.0f));


        if (togglePlanes == false){    // my code

          planeObject.SetActive(false); // my code

          } //
  }

当我按下切换按钮时什么也没发生.

Nothing happens when I press the toggle button.

我的另一个选择是在TrackedPlaneVisualizer.cs中进行如下更改:

The other option I had was to make changes in the TrackedPlaneVisualizer.cs where I did something like this :

for (int i = 0; i < planePolygonCount; ++i)
            {
                Vector3 v = m_meshVertices[i];

                // Vector from plane center to current point
                Vector3 d = v - planeCenter;

                float scale = 1.0f - Mathf.Min((FEATHER_LENGTH / d.magnitude), FEATHER_SCALE);
                m_meshVertices.Add(scale * d + planeCenter);

                if (togglePlanesbool == true) // my code
                {
                    m_meshColors.Add(new Color(0.0f, 0.0f, 0.0f, 1.0f)); // my code
                }

                else
                {
                    m_meshColors.Add(new Color(0.0f, 0.0f, 0.0f, 0.0f)); // my code
                }
     }

这确实有效.但是我遇到了切换的延迟,有时如果渲染了两个不同的平面,它们将开始在它们自己之间切换(如果启用了一个,则禁用了另一个).所以我想这也是不可行的选择....任何可以帮助您的人吗?

This did work. But I am experiencing delays in toggling and sometimes if two different planes have been rendered they start toggling between themselves(if one is enabled, other gets disabled). So I guess this is also not the option to go for....Anyone who can help?

请注意,我是Unity的初学者.

Note that I am a beginner in Unity.

推荐答案

该示例并非真正用于隐藏和显示飞机,因此您必须添加一些内容.

The sample isn't really designed to hide and show the planes, so you have to add a couple things.

首先,没有代表ARCore平面的GameObjects的集合.最简单的方法是在游戏对象中添加标签:

First, there is no collection of the GameObjects that represent the ARCore planes. The easiest way to do this is to add a tag to the game objects:

在Unity编辑器中,找到TrackedPlaneVisualizer预制并选择它.然后在属性检查器中,下拉标签下拉菜单,并添加一个名为 plane 的标签.

In the Unity editor, find the TrackedPlaneVisualizer prefab and select it. Then in the property inspector, drop down the Tag dropdown and add a tag named plane.

接下来,在Toggle处理程序方法中,您需要找到所有带有"plane"标记的游戏对象.然后获取Renderer和TrackedPlaneVisualizer组件,并根据切换启用或禁用它们.您需要同时做这两个组成部分.渲染器绘制平面,TrackedPlaneVisualizer重新启用渲染器(理想情况下,它将接受渲染器的状态).

Next, in the Toggle handler method, you need to find all the game objects with the "plane" tag. Then get both the Renderer and TrackedPlaneVisualizer components and enable or disable them based on the toggle. You need to do both components; the Renderer draws the plane, and the TrackedPlaneVisualizer re-enables the Renderer (ideally it would honor the Renderer's state).

    public void OnTogglePlanes(bool flag) {
        showPlanes = flag;
        foreach (GameObject plane in GameObject.FindGameObjectsWithTag ("plane")) {
            Renderer r = plane.GetComponent<Renderer> ();
            TrackedPlaneVisualizer t = plane.GetComponent<TrackedPlaneVisualizer>();
            r.enabled = flag;
            t.enabled = flag;
        }
    }

您也可以在实例化GameObject的地方做类似的事情,因此新飞机可以使用拨动开关.

You can also do a similar thing where the GameObject is instantiated, so new planes honor the toggle.

这篇关于在ARCore unity中禁用/切换跟踪平面的可视化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆