Adding the AR prompt

We can add the AR prompt as follows:

  1. With Main Canvas selected in Hierarchy, create UI | Panel and name it AR Prompt.
  2. In the gear icon for the image component, select Remove Component.
  3. On AR Prompt, create UI | Image and name it AR Prompt Idle.
  4. Set its Source Image to Circle.
  5. Set its Width and Height to 400, 400.
  6. On AR Prompt, create UI | Text and name it AR Prompt Text.
  7. Set its Text string to Align outline to tire, then press screen.
  8. Set its Anchor Presets to Middle/Stretch, Font Style: Bold, Align: Center, Middle, and Horizontal Overflow: Overflow.

We're prompting the user to touch the screen. We need a script to handle that.

  1. Create a new C# script named ARHitHandler.
  2. Add it as a component to AR Prompt.

Then open the file for editing, as follows:

File: ARHitHandler.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.XR.iOS;

public class ARHitHandler : MonoBehaviour {
public Transform anchor;
public InstructionsController controller;

void Update () {
List<ARHitTestResult> hitResults;
ARPoint point;
float scale;


if (Input.touchCount > 0 && anchor != null) {

var touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began) {
Vector2 center = new Vector2(Screen.width/2, Screen.height/2);
Vector3 screenPosition = Camera.main.ScreenToViewportPoint(center);
point.x = screenPosition.x;
point.y = screenPosition.y;
Vector2 edge = new Vector2(Screen.width, Screen.height/2);
Vector3 screenEdge = Camera.main.ScreenToViewportPoint(edge);
scale = screenPosition.x - screenEdge.x;

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

if (hitResults.Count > 0) {
anchor.position = UnityARMatrixOps.GetPosition( hitResults[0].worldTransform);
anchor.rotation = UnityARMatrixOps.GetRotation( hitResults[0].worldTransform);
anchor.scale = new Vector3(scale, scale, scale);
}
}
}
}
}

This script is very similar to the SolarSystemHitHandler.cs we wrote in Chapter 5, AR Solar System. Please refer to that chapter for an explanation of the UnityARSessionNativeInterface usage code.

The primary difference is we do not use the exact screen touch point to determine where to place the root anchor. Rather, when the user touches anywhere on the screen, we use the center of the screen (width/2, height/2), which is also the center of the circle.

We also use the size of the circle on the screen (approximated to the right edge of the screen) to determine the scale of the graphics in 3D, using the distance between the world coordinate of the center and the world coordinate of the edge.

We've added a public variable to the InstructionsController, so after the root is repositioned, we toggle off the Anchor mode.

Using a screen space prompt may not be the best approach for registering AR graphics to a real-world tire when you have spatial anchoring technology. You may want to consider and try other solutions, such as:

  • Do not use the circle prompt at all, just a small cursor that's positioned on the spatial map surface so you can identify the position, including 3D depth, before clicking, and assume the size of the graphics in a real-world scale.
  • Use a world space canvas or a 3D graphic for the circle prompt. In that case, you might modify ARHitHandler to update the prompt graphic on every frame update while in anchor mode. This will allow the user to preview the graphic extents relative to the real world in real-time.
  • Allow the user to interactively edit the graphic position, scale, and rotation after it is placed. This is a more involved UI, but it gives the most flexibility. See Chapter 8, Room Decoration with AR, for implementation ideas.
..................Content has been hidden....................

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