AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
PlaneTargetGroupPicker.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 using System.Collections;
6 
7 namespace HoloToolkit.Unity.Tests
8 {
9  public class PlaneTargetGroupPicker : Singleton<PlaneTargetGroupPicker>
10  {
11  [Tooltip("In degrees")] public float AngleOfAcceptance = 45.0f;
13 
14  public TextMesh DisplayText;
15  public float TextDisplayTime = 5.0f;
16 
17  private PlaneTargetGroup currentGroup;
18 
19  private Coroutine displayForSecondsCoroutine;
20 
21  public void PickNewTarget()
22  {
23  PlaneTargetGroup newGroup = null;
24  float smallestAngle = float.PositiveInfinity;
25  Transform cameraTransform = CameraCache.Main.transform;
26  // Figure out which group we're looking at
27  foreach (PlaneTargetGroup group in Groups)
28  {
29  Vector3 camToGroup = group.transform.position - cameraTransform.position;
30  float gazeObjectAngle = Vector3.Angle(camToGroup, cameraTransform.forward);
31  if (group.Targets.Length > 0 && gazeObjectAngle < AngleOfAcceptance && gazeObjectAngle < smallestAngle)
32  {
33  smallestAngle = gazeObjectAngle;
34  newGroup = group;
35  }
36  }
37 
38  // Looking at a group!
39  if (newGroup != null)
40  {
41  // If we're already in this group, switch targets
42  if (newGroup == currentGroup)
43  {
44  newGroup.PickNewTarget();
45  }
46  currentGroup = newGroup;
47  StabilizationPlaneModifier.Instance.TargetOverride = currentGroup.CurrentTarget.transform;
48  StabilizationPlaneModifier.Instance.TrackVelocity = currentGroup.UseVelocity;
49  UpdateText();
50  }
51  }
52 
53  private void UpdateText()
54  {
55  DisplayText.text = StabilizationPlaneModifier.Instance.TargetOverride.name;
56  if (StabilizationPlaneModifier.Instance.TrackVelocity)
57  {
58  DisplayText.text += "\r\nvelocity";
59  }
60 
61  if (displayForSecondsCoroutine != null)
62  {
63  StopCoroutine(displayForSecondsCoroutine);
64  }
65  displayForSecondsCoroutine = StartCoroutine(DisplayForSeconds(TextDisplayTime));
66  }
67 
68  private IEnumerator DisplayForSeconds(float displayTime)
69  {
70  yield return new WaitForSeconds(displayTime);
71  DisplayText.text = "";
72  }
73  }
74 }
The purpose of this class is to provide a cached reference to the main camera. Calling Camera...
Definition: CameraCache.cs:12
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
static Camera Main
Returns a cached reference to the main camera and uses Camera.main if it hasn&#39;t been cached yet...
Definition: CameraCache.cs:20
StabilizationPlaneModifier handles the setting of the stabilization plane in several ways...
Singleton behaviour class, used for components that should only have one instance.
Definition: Singleton.cs:14