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

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

问题描述

我一直在寻找 ARCore Unity 的代码一段时间,我想做一个简单的任务,即有一个切换按钮,以便用户可以在场景中放置一个对象,同时知道在跟踪时将其放置在哪里平面是可见的,一旦用户放置对象,他就可以选择仅在视觉上禁用跟踪平面,使其看起来更逼真.通过在主 HelloArActivity.java 中执行类似操作,我能够在 Android Studio 中执行此操作:

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 的 bool,并将 mPlaneRenderer.drawPlanes 函数置于 if 条件中.当 bool 为真时,它显示平面,当它为假时,它不...

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 平面的游戏对象集合.最简单的方法是给游戏对象添加一个标签:

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 预制件并选择它.然后在属性检查器中,下拉 Tag 下拉菜单并添加一个名为 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 组件并根据切换启用或禁用它们.你需要做这两个组件;Renderer 绘制平面,TrackedPlaneVisualizer 重新启用 Renderer(理想情况下它会尊重 Renderer 的状态).

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天全站免登陆