MoveTool

The MoveTool will be the only script that requires ARKit-specific calls. In the Mixed Reality Toolkit, we cast a ray from the camera into the scene and looked for a hit point where it intersects with the spatial map of the environment. For ARKit, we do something similar, but instead of using Unity physics to do the calculation, we use the underlying ARKit SDK directly (from C#):

  1. Open the MoveTool.cs script and modify it to use mouse events.
  2. Remove all references to HoloToolkit and spatialMapping.

Then replace the Update() function as follows:

File: MoveTool.cs
void Update() {
List<ARHitTestResult> hitResults;
ARPoint point;

if (isEditing) {
Vector3 screenPosition = Camera.main.ScreenToViewportPoint(Input.mousePosition);
point.x = screenPosition.x;
point.y = screenPosition.y;

hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest( point,
ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent);

if (hitResults.Count == 0) {
hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest( point,
ARHitTestResultType.ARHitTestResultTypeVerticalPlane);
}
if (hitResults.Count == 0) {
hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest( point,
ARHitTestResultType.ARHitTestResultTypeFeaturePoint);
}

if (hitResults.Count > 0) {
picture.transform.position = UnityARMatrixOps.GetPosition( hitResults[0].worldTransform);
picture.transform.rotation = UnityARMatrixOps.GetRotation( hitResults[0].worldTransform);
}
}
if (!Input.GetMouseButton(0)) {
DoneEdit();
}
}

We've seen similar code before, in the SolarSystemHitHandler.cs script we wrote in Chapter 5, AR Solar System, and the ARHitHandler.cs script in Chapter 7, Augmenting the Instruction Manual, where we call the ARKIT HitTest looking for a world space position based on the position of the screen touch. In this case, we first look for any existing object in the foreground (ARHitTestResultTypeExistingPlaneUsingExtent), then allow any vertical plane (ARHitTestResultTypeVerticalPlane), and if that fails, try to guess a plane based on a feature point (ARHitTestResultTypeFeaturePoint). Feel free to play with these options to see which ones work best for you and in which order of priority.

Then replace the input handler with mouse (touch screen) versions:

    private void OnMouseDown() { 
        if (!isEditing) { 
            BeginEdit(); 
        } 
    } 
 
    private void OnMouseUp() { 
        if (isEditing) { 
            DoneEdit(); 
        } 
    } 

Save the script. Now, when you run the app and press the Move tool, it will drag the picture with your touch until you let up.

That's it! Build And Run the app for your device. The following is a photo of my family room wall with one real-life picture augmented with two additional virtual ones on the iPad with ARKit:

Building for Android using Google ARCore
Please refer to the the GitHub repository for this book for the implementation notes and code using Google ARCore for Android: https://github.com/ARUnityBook/. The principles are very similar to ARKit but the Unity SDK and components are different.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset