AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
Billboard.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
7 {
8  public enum PivotAxis
9  {
10  // Most common options, preserving current functionality with the same enum order.
11  XY,
12  Y,
13  // Rotate about an individual axis.
14  X,
15  Z,
16  // Rotate about a pair of axes.
17  XZ,
18  YZ,
19  // Rotate about all axes.
20  Free
21  }
22 
26  public class Billboard : MonoBehaviour
27  {
31  [Tooltip("Specifies the axis about which the object will rotate.")]
32  [SerializeField]
33  private PivotAxis pivotAxis = PivotAxis.XY;
34  public PivotAxis PivotAxis
35  {
36  get { return pivotAxis; }
37  set { pivotAxis = value; }
38  }
39 
43  [Tooltip("Specifies the target we will orient to. If no target is specified, the main camera will be used.")]
44  [SerializeField]
45  private Transform targetTransform;
46  public Transform TargetTransform
47  {
48  get { return targetTransform; }
49  set { targetTransform = value; }
50  }
51 
52  private void OnEnable()
53  {
54  if (TargetTransform == null)
55  {
56  if (CameraCache.Main != null)
57  {
58  TargetTransform = CameraCache.Main.transform;
59  }
60  }
61  }
62 
66  private void LateUpdate()
67  {
68  if (TargetTransform == null)
69  {
70  if (CameraCache.Main != null)
71  {
72  TargetTransform = CameraCache.Main.transform;
73  }
74  else
75  {
76  return;
77  }
78  }
79 
80  // Get a Vector that points from the target to the main camera.
81  Vector3 directionToTarget = TargetTransform.position - transform.position;
82 
83  bool useCameraAsUpVector = true;
84 
85  // Adjust for the pivot axis.
86  switch (PivotAxis)
87  {
88  case PivotAxis.X:
89  directionToTarget.x = 0.0f;
90  useCameraAsUpVector = false;
91  break;
92 
93  case PivotAxis.Y:
94  directionToTarget.y = 0.0f;
95  useCameraAsUpVector = false;
96  break;
97 
98  case PivotAxis.Z:
99  directionToTarget.x = 0.0f;
100  directionToTarget.y = 0.0f;
101  break;
102 
103  case PivotAxis.XY:
104  useCameraAsUpVector = false;
105  break;
106 
107  case PivotAxis.XZ:
108  directionToTarget.x = 0.0f;
109  break;
110 
111  case PivotAxis.YZ:
112  directionToTarget.y = 0.0f;
113  break;
114 
115  case PivotAxis.Free:
116  default:
117  // No changes needed.
118  break;
119  }
120 
121  // If we are right next to the camera the rotation is undefined.
122  if (directionToTarget.sqrMagnitude < 0.001f)
123  {
124  return;
125  }
126 
127  // Calculate and apply the rotation required to reorient the object
128  if (useCameraAsUpVector)
129  {
130  transform.rotation = Quaternion.LookRotation(-directionToTarget, CameraCache.Main.transform.up);
131  }
132  else
133  {
134  transform.rotation = Quaternion.LookRotation(-directionToTarget);
135  }
136  }
137  }
138 }
The purpose of this class is to provide a cached reference to the main camera. Calling Camera...
Definition: CameraCache.cs:12
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
The Billboard class implements the behaviors needed to keep a GameObject oriented towards the user...
Definition: Billboard.cs:26