AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
BoundaryVisualizer.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 
6 namespace HoloToolkit.Unity.Boundary.Tests
7 {
11  public class BoundaryVisualizer : MonoBehaviour
12  {
13 #if UNITY_2017_2_OR_NEWER
14  [SerializeField]
15  [Tooltip("Material used to draw the inscribed rectangle bounds.")]
16  private Material boundsMaterial = null;
17 
18  [SerializeField]
19  [Tooltip("Material used to draw items in the tracked area bounds.")]
20  private Material trackedAreaBoundsMaterial = null;
21 
22  private void Start()
23  {
24  AddQuad();
25  AddIndicators();
26  }
27 
31  private void AddQuad()
32  {
33  Vector3 center;
34  float angle;
35  float width;
36  float height;
37  BoundaryManager.Instance.TryGetBoundaryRectangleParams(out center, out angle, out width, out height);
38 
39  var quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
40  quad.transform.SetParent(transform);
41  quad.transform.Translate(center + new Vector3(0.0f, 0.005f, 0.0f)); // Add fudge factor to avoid z-fighting
42  quad.transform.Rotate(new Vector3(90, -angle, 0));
43  quad.transform.localScale = new Vector3(width, height, 1.0f);
44  }
45 
49  private void AddRectangleBounds()
50  {
51  var points = BoundaryManager.Instance.TryGetBoundaryRectanglePoints();
52  if (points == null)
53  {
54  return;
55  }
56 
57  LineRenderer lr = gameObject.AddComponent<LineRenderer>();
58  lr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
59  lr.useWorldSpace = false;
60  lr.loop = true;
61  lr.sharedMaterial = boundsMaterial;
62  lr.startWidth = 0.05f;
63  lr.endWidth = 0.05f;
64  lr.positionCount = points.Length;
65  lr.SetPositions(points);
66  }
67 
72  private void AddIndicators()
73  {
74  const int indicatorCount = 15;
75  const float indicatorDistance = 0.2f;
76  const float dimension = indicatorCount * indicatorDistance;
77 
78  Vector3 center;
79  float angle;
80  float width;
81  float height;
82  if (!BoundaryManager.Instance.TryGetBoundaryRectangleParams(out center, out angle, out width, out height))
83  {
84  return;
85  }
86 
87  Vector3 corner = center - (new Vector3(dimension, 0.0f, dimension) / 2.0f);
88  corner.y += 0.05f;
89  for (int xIndex = 0; xIndex < indicatorCount; ++xIndex)
90  {
91  for (int yIndex = 0; yIndex < indicatorCount; ++yIndex)
92  {
93  var offset = new Vector3(xIndex * indicatorDistance, 0.0f, yIndex * indicatorDistance);
94  var position = corner + offset;
95  var marker = GameObject.CreatePrimitive(PrimitiveType.Sphere);
96  marker.transform.SetParent(transform);
97  marker.transform.position = position;
98  marker.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
99 
100  var markerRenderer = marker.GetComponent<MeshRenderer>();
101 
102  if (BoundaryManager.Instance.ContainsObject(position, UnityEngine.Experimental.XR.Boundary.Type.TrackedArea))
103  {
104  markerRenderer.sharedMaterial = trackedAreaBoundsMaterial;
105  }
106 
107  if (BoundaryManager.Instance.ContainsObject(position, UnityEngine.Experimental.XR.Boundary.Type.PlayArea))
108  {
109  markerRenderer.sharedMaterial = boundsMaterial;
110  }
111  }
112  }
113  }
114 #endif
115  }
116 }
Places a floor quad to ground the scene. Allows you to check if your GameObject is within setup bound...
Demo class to show different ways of using the boundary API.
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26