MotionTracking

Demonstrate how to use motion tracking.

  • Demonstrate how to fallback to ARKit/ARCore when EasyAR motion tracker not available

How to Use

../../_images/image_38.png
Mark 1: Display system status and operation hint.
Mark 2: Continue plane detection after a cube is put on the plane.
Mark 3: Switch world center mode.

The sample will detect horizontal plane at beginning. Plane detection will be stopped when then cube is placed on a plane. You can click on the Unlock Plane button to continue detect a new plane.

The cube in the scene can be moved on the plane with one finger. Two finger pinch will scale the cube and two finger horizontal move will rotate the cube.

How It Works

VIO device strategy

VIOCameraDeviceUnion is a Union which can be MotionTracker or ARKit or ARCore depending on the system and DeviceChooseStrategy.

VIOCameraDeviceUnion.DeviceStrategy is set to DeviceChooseStrategy.EasyARMotionTrackerFirst in this sample to use EasyAR motion tracking first if available.

../../_images/image_s9_1.png

Objects under world root

WorldRoot is designed to do these things,

  • Control show/hide of objects when tracking status change.

  • Move together against camera according to ARSession.CenterMode.

You can ignore WorldRoot If you can make sure all above is handled by yourself.

In this sample, WorldRootController.ActiveControl is set to ActiveControlStrategy.HideWhenNotTracking, so the cube will hide when tracking fails.

../../_images/image_s9_2.png

Plane detection

This part is only available when EasyAR motion tracking is running using EasyAR Sense 4.1 and later.

VIOCameraDeviceUnion.HitTestAgainstHorizontalPlane is used to get a plane location in the world. Place a plane at that location.

var viewPoint = new Vector2(0.5f, 0.333f);
var points = vioCamera.HitTestAgainstHorizontalPlane(viewPoint);
if (points.Count > 0)
{
    Plane.transform.position = points[0];
    ...
}

Keep the plane when a plane is detected until it is about to go beyond sight.

var viewportPoint = Camera.main.WorldToViewportPoint(Plane.transform.position);
if (!Plane.activeSelf || viewportPoint.x < 0 || viewportPoint.x > 1 || viewportPoint.y < 0 || viewportPoint.y > 1)
{
    ...
}

Move object on the plane

This part is only available when EasyAR motion tracking is running using EasyAR Sense 4.1 and later.

Raycast against the plane object and move the cube on it.

Ray ray = Camera.main.ScreenPointToRay(touch.position);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
    TouchControl.transform.position = hitInfo.point;
    ...
}

Center mode

Two mode of ARSession.CenterMode are valid in world sensing.

In ARSession.ARCenterMode.WorldRoot the camera will move automatically when the device moves, and the WorldRoot stay. In ARSession.ARCenterMode.Camera, the camera do not automatically move when the device moves. It is suggested to use ARSession.ARCenterMode.WorldRoot in most cases.