工具:
Unity, Vuforia
首先采用上篇博文中的方案二来实现AR的Extanded Tracking。
同时,使用一个空GameObject(命名为AR Objects)来与ImageTarget的transform进行同步。使用AR Objects的child(命名为AR Children)作为漫游中需要执行移动的对象。
在AR Children中挂载两个脚本:MoveAgainstCam.cs和PinchZoom.cs。
MoveAgainstCam.cs用来移动AR Children,模拟漫游效果。PinchZoom.cs用来处理手指输入,放大操作类似于向前移动,缩小操作类似于后退。(效果类似于“放大”和“缩小”)
代码如下:
MoveAgainsCam.cs
namespace Will
{
public class MoveAgainstCam : MonoBehaviour
{
public GameObject m_Camera;
public float m_MoveSpeed = 1.0f;
private Transform m_CamTrans;
//--------------------- Life Cycle -----------------------//
private void Awake()
{
m_CamTrans = m_Camera.transform;
}
//--------------------- Interface -----------------------//
public void MoveForward(float fraction)
{
if(fraction == 0)
{
fraction = 1;
}
transform.position -= m_CamTrans.forward * m_MoveSpeed * fraction;
}
}
}
PinchZoom.cs:
namespace Will
{
public class PinchZoom : MonoBehaviour
{
public float ZoomSpeed = 0.01f; // The rate of change of the field of view in perspective mode.
public float RotateSpeed = 30.0f;
private MoveAgainstCam m_MoveAgainstCamScript;
private void Awake()
{
m_MoveAgainstCamScript = GetComponent();
}
void Update()
{
// If there are two touches on the device...
if (Input.touchCount == 2)
{
// Store both touches.
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
/*------------------------ Zoom --------------------------*/
// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
// Find the difference in the distances between each frame.
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
// Move
m_MoveAgainstCamScript.MoveForward(-deltaMagnitudeDiff);
}
}
}
}
AR 如何在扫描到AR图标之后进行场景(摄像头)漫游
