AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ScaleByDistance.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 
5 using HoloToolkit.Unity;
6 using UnityEngine;
7 
8 namespace HoloToolkit.Examples.Prototyping
9 {
14  public class ScaleByDistance : MonoBehaviour
15  {
16  [Tooltip("The object's distance to scale against, default: Main Camera")]
17  public GameObject ReferenceObject;
18 
19  [Tooltip("The object to scale")]
20  public GameObject TargetObject;
21 
22  [Tooltip("A game object that contains an Interactive to handle air taps")]
23  public GameObject ReferenceInteractive;
24 
25  [Tooltip("How far away should the object be at 100%")]
26  public float ScaleDistance = 1;
27 
28  [Tooltip("Auto start? or status")]
29  public bool IsScaling;
30 
31  [Tooltip("scaling speed : higher is faster")]
32  public float ScaleSpeed = 3;
33 
34  [Tooltip("Minimum scale")]
35  public float MinimumScale = 0.3f;
36 
37  // the cached start scale
38  private Vector3 mStartScale;
39  // the current scale through the transformation
40  private float mCurrentScale = 1;
41  // scale difference
42  private float mDeltaScale;
43  // the cached starting difference
44  private float mStartDistance;
45 
49  void Start()
50  {
51  if (TargetObject == null)
52  {
53  TargetObject = this.gameObject;
54  }
55 
56  if (ReferenceObject == null)
57  {
58  ReferenceObject = CameraCache.Main.gameObject;
59  }
60  }
61 
65  public void StartRunning(bool state = false)
66  {
67  mStartScale = TargetObject.transform.localScale;
68  mStartDistance = Vector3.Distance(TargetObject.transform.position, ReferenceObject.transform.position);
69  IsScaling = true;
70 
71  if (!state)
72  {
73  mCurrentScale = mDeltaScale;
74  }
75 
76  if (ReferenceInteractive != null)
77  {
78  InputManager.Instance.PushModalInputHandler(ReferenceInteractive);
79  }
80  }
81 
85  public void StopRunning()
86  {
87 
88  if (ReferenceInteractive != null)
89  {
90  InputManager.Instance.PopModalInputHandler();
91  }
92  IsScaling = false;
93  }
94 
95  // set the scale value
96  void Update()
97  {
98  if (IsScaling)
99  {
100  float ratio = (Vector3.Distance(TargetObject.transform.position, ReferenceObject.transform.position) - mStartDistance) / ScaleDistance;
101  mDeltaScale = Mathf.Max(mCurrentScale + ratio, MinimumScale);
102  Vector3 targetScale = mStartScale * mDeltaScale;
103  TargetObject.transform.localScale = Vector3.Lerp(TargetObject.transform.localScale, targetScale, Time.deltaTime * ScaleSpeed);
104 
105  }
106  }
107  }
108 }
Input Manager is responsible for managing input sources and dispatching relevant events to the approp...
Definition: InputManager.cs:19
Animates the scale of an object based on it's distance to the distance object. Could be used for scal...
void StartRunning(bool state=false)
Start the scaling animation based on distance
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't been cached yet...
Definition: CameraCache.cs:20