AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
FixedAngularSize.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 {
13  public class FixedAngularSize : MonoBehaviour
14  {
18  [Tooltip("Offsets the scale ratio so that text does not scale down too much. (Set to zero for linear scaling)")]
19  [Range(0.0f, 1.0f)]
20  [SerializeField]
21  private float sizeRatio = 0.0f;
22  public float SizeRatio
23  {
24  get { return sizeRatio; }
25  set
26  {
27  // Set the value of sizeRatio, while maintaining the supported range.
28  if (value < 0.0f)
29  {
30  sizeRatio = 0.0f;
31  }
32  else if (value > 1.0f)
33  {
34  sizeRatio = 1.0f;
35  }
36  else
37  {
38  sizeRatio = value;
39  }
40  }
41  }
42 
46  private Vector3 startingScale;
47 
51  private float startingDistance;
52 
53  private void Start()
54  {
55  // Calculate the XYZ ratios for the transform's localScale over its
56  // initial distance from the camera.
57  startingDistance = Vector3.Distance(CameraCache.Main.transform.position, transform.position);
58  startingScale = transform.localScale;
59 
60  SetSizeRatio(SizeRatio);
61  }
62 
67  public void SetSizeRatio(float ratio)
68  {
69  if (ratio == 0)
70  {
71  if (startingDistance > 0.0f)
72  {
73  // set to a linear scale ratio
74  SizeRatio = 1 / startingDistance;
75  }
76  else
77  {
78  // If the transform and the camera are both in the same
79  // position (that is, the distance between them is zero),
80  // disable this Behaviour so we don't get a DivideByZero
81  // error later on.
82  enabled = false;
83 #if UNITY_EDITOR
84  Debug.LogWarning("The object and the camera are in the same position at Start(). The attached FixedAngularSize Behaviour is now disabled.");
85 #endif // UNITY_EDITOR
86  }
87  }
88  else
89  {
90  SizeRatio = ratio;
91  }
92  }
93 
97  private void LateUpdate()
98  {
99  float distanceToHologram = Vector3.Distance(CameraCache.Main.transform.position, transform.position);
100  // create an offset ratio based on the starting position. This value creates a new angle that pivots
101  // on the starting position that is more or less drastic than the normal scale ratio.
102  float curvedRatio = 1 - startingDistance * SizeRatio;
103  transform.localScale = startingScale * (distanceToHologram * SizeRatio + curvedRatio);
104  }
105  }
106 }
void SetSizeRatio(float ratio)
Manually update the OverrideSizeRatio during runtime or through UnityEvents in the editor ...
Causes a Hologram to maintain a fixed angular size, which is to say it occupies the same pixels in th...
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