我希望用 3D 平面替换 ARPlane 的区域。
ARMeshVisualizer
做得非常好,但我需要在那里有一个实际的 3D 对象而不是网格,这样我就可以在上面使用 ARAnchor(非常重要)。
我尝试过类似的事情,但没有运气
void PlaceObjectOnPlane(ARPlane plane)
{
Vector3 position = plane.center;
Quaternion rotation = plane.transform.rotation;
Vector2 planeSize = plane.size;
Vector3 objectScale = new Vector3(planeSize.x, 1f, planeSize.y);
if (objectToPlace != null && !objectToPlace.activeInHierarchy)
{
objectToPlace.SetActive(true);
objectToPlace.transform.position = position;
objectToPlace.transform.rotation = rotation;
objectToPlace.transform.localScale = objectScale;
}
}
谢谢
您可以使用 ARAnchorManager 将锚点附加到平面。 像这样:
void PlaceObjectOnPlane(ARPlane plane)
{
// Get ARAnchorManager
ARAnchorManager anchorManager = FindObjectOfType<ARAnchorManager>();
// Attach an anchor to the plane
ARAnchor anchor = anchorManager.AttachAnchor(plane, new
Pose(plane.center, plane.transform.rotation));
if (anchor == null)
{
Debug.LogError("Failed to attach anchor to the ARPlane.");
return;
}
// Set up the object
if (objectToPlace != null)
{
objectToPlace.transform.SetParent(anchor.transform, false);
objectToPlace.transform.localPosition = Vector3.zero;
objectToPlace.transform.localRotation = Quaternion.identity;
Vector2 planeSize = plane.size;
objectToPlace.transform.localScale = new Vector3(planeSize.x, 1f,
planeSize.y);
objectToPlace.SetActive(true);
}
}
解释
这可确保物体正确替换 ARPlane,同时保持 AR 跟踪稳定性。