我已经研究 ARCore Unity 的代码有一段时间了,我想做一个简单的任务,即有一个切换按钮,以便用户可以在场景中放置一个对象,同时知道在跟踪平面可见时将其放置在哪里一旦用户放置了对象,他就可以选择仅在视觉上禁用跟踪的平面,使其看起来更真实。我可以在 Android Studio 中通过在主 HelloArActivity.java 中执行类似的操作来完成此操作:
if (planeToggle) {
mPlaneRenderer.drawPlanes(mSession.getAllPlanes(), frame.getPose(), projmtx);
}
这真的很简单。我创建了一个名为planeToggle 的布尔值,并将 mPlaneRenderer.drawPlanes 函数放置在 if 条件中。当布尔为 true 时,它显示飞机,当 bool 为 false 时,它不显示......
然而,对于 Unity 我很困惑。我在 HelloARController.cs 中做了类似的事情:
我做了一个按钮来切换飞机。
为其设置一个事件侦听器以切换布尔变量并执行如下操作:
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
} //
}
当我按下切换按钮时什么也没有发生。
我的另一个选择是在 TrackedPlaneVisualizer.cs 中进行更改,我在其中做了如下操作:
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
}
}
这确实有效。但我在切换时遇到延迟,有时如果渲染了两个不同的平面,它们就会开始在它们之间切换(如果启用了一个,则另一个被禁用)。所以我想这也不是一个选择......有人可以帮忙吗?
请注意,我是 Unity 的初学者。