ARFoundation平面检测与射线检测遇到的问题
关于ARFoundation平面检测和射线检测的具体用法这里不细细的说了,这个链接里说的比较明白了
https://blog.youkuaiyun.com/yolon3000/article/details/95897049
现在说说我想实现的功能以及遇到的问题及其解决办法
1.功能:
利用平面检测和射线检测在手指选中的平面位置上放置一个物体,然后隐藏其他平面,并且可以在放置该物体的平面上可以自由拖拽滑动该物体。
2.遇到的问题:
拖拽物体的时候目标平面的trackableID不确定,平面即使是隐藏了还是会被射线检测检测到。
3.解决办法
射线检测返回的信息是用一个List<ARRaycastHit保存的,被隐藏的平面被检测到的话平面的信息也会保存在这个list里,所以遍历这个list找到和目标平面ID一致的那个平面,用这个平面的位置信息来做拖拽。
代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARKit;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.EventSystems;
public class ARRaycastTest : MonoBehaviour
{
public ARPlaneManager aRPlaneManager;
public ARRaycastManager arRaycastManager;
public GameObject obj;
public RectTransform resetButton;
public Text currentID;
public Text targetID;
static List<ARRaycastHit> s_Hits = new List<ARRaycastHit>();
public GameObject spawnedObject { get; private set; }
TrackableId currentTrackID ;
// Update is called once per frame
void Update()
{
currentID.gameObject.SetActive(false);
if (!TryGetTouchPosition(out Vector2 touchPosition))
return;
if (Input.touchCount > 0 && EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) return;
if (RectTransformUtility.RectangleContainsScreenPoint(resetButton, touchPosition)) return;
if (spawnedObject != null)
{
//如果放置过物体,在这个平面及其延展面上拖拽,TrackableType.PlaneWithinInfinity表示平面的延展面
if (arRaycastManager.Raycast(touchPosition, s_Hits,TrackableType.PlaneWithinInfinity|TrackableType.PlaneWithinPolygon))
{
//遍历s_Hits找到和目标ID一致的平面,用它的pos的坐标赋值给物体
for (int i = 0; i < s_Hits.Count; i++)
{
if (s_Hits[i].trackableId == currentTrackID) {
currentID.gameObject.SetActive(true);
currentID.text = s_Hits[i].trackableId.ToString();
Pose hitPose = s_Hits[i].pose;
spawnedObject.transform.position = hitPose.position;
}
}
}
}
else {
//如果没有放置过物体,在这个平面内部放置物体, TrackableType.PlaneWithinPolygon表示在平面包围内
if (arRaycastManager.Raycast(touchPosition, s_Hits, TrackableType.PlaneWithinPolygon))
{
Pose hitPose = s_Hits[0].pose;
spawnedObject = Instantiate(obj, hitPose.position, hitPose.rotation);
currentTrackID = s_Hits[0].trackableId;//保存当前平面的ID
targetID.text = s_Hits[0].trackableId.ToString();
aRPlaneManager.enabled = false;
HideOtherPlanesActive();
}
}
}
public void ResetAR()
{
aRPlaneManager.enabled = true;
SetAllPlanesActive(true);
if (spawnedObject != null) {
Destroy(spawnedObject);
spawnedObject = null;
}
}
bool TryGetTouchPosition(out Vector2 touchPosition)
{
#if UNITY_EDITOR
if (Input.GetMouseButton(0))
{
var mousePosition = Input.mousePosition;
touchPosition = new Vector2(mousePosition.x, mousePosition.y);
return true;
}
#else
if (Input.touchCount > 0)
{
touchPosition = Input.GetTouch(0).position;
return true;
}
#endif
touchPosition = default;
return false;
}
public void SetAllPlanesActive(bool value)
{
foreach (var plane in aRPlaneManager.trackables) {
plane.gameObject.SetActive(value);
}
}
//隐藏和目标ID不一致的平面
public void HideOtherPlanesActive()
{
foreach (var plane in aRPlaneManager.trackables)
{
if (plane.trackableId != currentTrackID)
{
plane.gameObject.SetActive(false);
}
}
}
}