Building the object outliner

We will build an ObjectOutliner class to handle the outlining for us. Follow along as we build the pieces to turn the outline on and off as the user selects an object:

  1. Create a new C# script called ObjectOutliner in the Assets/ARCoreDesign/Scripts folder.
  2. Replace all of the pregenerated script with the following:
namespace Packt.ARCoreDesign
{
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectOutliner : MonoBehaviour
{
public int MaterialSlot;
public Material DefaultMaterial;
public Material OutlineMaterial;
public bool outlineOn;
public void Outline()
{
outlineOn = !outlineOn;
var renderer = GetComponent<MeshRenderer>();
Material[] mats = renderer.materials;
if (outlineOn)
{
mats[MaterialSlot] = OutlineMaterial;
}
else
{
mats[MaterialSlot] = DefaultMaterial;
}
renderer.materials = mats;
}
}
}
  1. This class basically just swaps the material of an object with its outlined or default material every time Outline is called.
  2. Next, open the SceneController.cs script in your code editor. We have to wrap the Session Raycast call in the Update method with our own Physics Raycast. Add the following code around the highlighted code section, as follows:
RaycastHit rayHit;
if (Physics.Raycast(FirstPersonCamera.ScreenPointToRay(touch.position), out rayHit, 2))
{
var outliner = rayHit.collider.gameObject.GetComponent<ObjectOutliner>();
if (outliner != null)
{
outliner.Outline();
}
}
else
{
// Raycast against the location the player touched to search for planes.
TrackableHit hit;
TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
TrackableHitFlags.FeaturePointWithSurfaceNormal;

if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit))
{
var andyObject = Instantiate(AndyAndroidPrefab, hit.Pose.position, hit.Pose.rotation);
m_sceneObjects.Add(andyObject);
// Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
// world evolves.
var anchor = hit.Trackable.CreateAnchor(hit.Pose);

// Andy should look at the camera but still be flush with the plane.
if ((hit.Flags & TrackableHitFlags.PlaneWithinPolygon) != TrackableHitFlags.None)
{
// Get the camera position and match the y-component with the hit position.
Vector3 cameraPositionSameY = FirstPersonCamera.transform.position;
cameraPositionSameY.y = hit.Pose.position.y;

// Have Andy look toward the camera respecting his "up" perspective, which may be from ceiling.
andyObject.transform.LookAt(cameraPositionSameY, andyObject.transform.up);
}

// Make Andy model a child of the anchor.
andyObject.transform.parent = anchor.transform;
}/end of Frame.Raycast
}
  1. This section of code uses the Raycast method of the Physics object. Physics is the object that encapsulates the Unity physics engine. Raycast is a method we use, just like Frame.Raycast we saw earlier, to cast a ray and check for any collisions. Normally, you filter out objects to test before you run a ray cast operation, because it is so expensive. You can see how this is done with Session in the setup of the raycastFilter, where the filter is set to test for planes, but you can also set this point as well. This will allow you to easily apply wall coverings, for instance. In our case, since we are using Physics to do the Raycast, we can ensure that you only get physics objects. The ARCore planes don't have physics objects attached to them.
  2. Save the file and return to Unity.
  3. Locate the armchair prefab in the Assets/ARCoreDesign/Prefabs folder and expand it to see the inner model.
  4. Select the armchair model and then, in the Inspector window, click on Add Component. Add a Box Collider to the object; the Box Collider will automatically adjust its size to surround the model. The Physics engine just tests for collisions against a collider and not the object. This is why we don't have to worry about our ARCore planes and points. If you add other models and want them selectable, then always use the simplest collider that best fits your shape. By simple, we mean less polygons. For instance, don't use a sphere collider when a Box Collider will do.
  5. Click on the Add Component button again and this time, add our new Object Outliner Script to the object and set its properties to what is shown in the following excerpt:
Setting up the Object Outliner properties
  1. Default Material represents the base look of the model. Then, we set the Outline Material to our outline material we created earlier. Lastly, we set the slot we want to replace. The element we want to replace is Element 1, so we put 1 in the Material Slot property.
  2. Save the project, build, and run. Place a chair and then select it.

Now you can place a chair, select it, and then deselect it. If you note that it is difficult to select an object, ensure that you check that the collider is sufficiently large to engulf the object. In our example, the automatically created collider for the armchair is slightly off; perhaps we can fix that issue with one of the exercise questions.

..................Content has been hidden....................

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