AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
BaseScalable.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 System.Collections;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity.InputModule.Examples.Grabbables
8 {
12  public abstract class BaseScalable : MonoBehaviour
13  {
14  [SerializeField]
15  private bool scaleByDistance = true;
16 
17  [SerializeField]
18  private BaseGrabbable grabbable;
19 
20  private bool readyToScale;
21  private float snapShotOfScale;
22  private int minScalarNumForScale = 2;
23  private bool currentlyScaling;
24  private float snapShotDistance;
25 
26  protected virtual void Awake()
27  {
28  if (grabbable == null)
29  {
30  grabbable = gameObject.GetComponent<BaseGrabbable>();
31  }
32  }
33 
37  protected virtual void OnEnable()
38  {
39  grabbable.OnGrabbed += OnGrabbed;
40  }
41 
42  protected virtual void OnDisable()
43  {
44  grabbable.OnGrabbed -= OnGrabbed;
45  }
46 
51  public void AttemptScale()
52  {
53  Debug.Log("Attempt scale");
54  BaseGrabber[] activeGrabbers = GetComponent<BaseGrabbable>().ActiveGrabbers;
55 
56  if (GetComponent<BaseGrabbable>().ActiveGrabbers.Length >= minScalarNumForScale)
57  {
58  // Distance
59  // snapshot a standard distance that the controls are when the scalable object is engaged
60  // That standard distance between controllers corresponds to the localScale * scaleMultiplier
61  if (scaleByDistance)
62  {
63  if (activeGrabbers.Length >= minScalarNumForScale)
64  {
65  //later this should be average distance between all controllers attached.
66  float dist = Vector3.Distance(activeGrabbers[0].GrabHandle.position, activeGrabbers[1].GrabHandle.position);
67  snapShotDistance = dist;
68  //TODO: scale should not be recorded from x axis alone
69  snapShotOfScale = transform.localScale.x;
70  currentlyScaling = true;
71  StartCoroutine(PerformScaling());
72  }
73  }
74  }
75  }
76 
81  public void OnGrabbed(BaseGrabbable baseGrab)
82  {
83  if (!currentlyScaling)
84  {
85  AttemptScale();
86  }
87  }
88 
94  public virtual IEnumerator PerformScaling()
95  {
96  currentlyScaling = true;
97 
98  while (currentlyScaling)
99  {
100  // let go
101  if (grabbable.GrabState == GrabStateEnum.Inactive)
102  {
103  currentlyScaling = false;
104  yield break;
105  }
106 
107  BaseGrabber[] activeGrabbers = grabbable.ActiveGrabbers;
108 
109  // If enough grabbers have grabbed
110  if (activeGrabbers.Length >= minScalarNumForScale)
111  {
112  float currDistance = Vector3.Distance(activeGrabbers[0].GrabHandle.position, activeGrabbers[1].GrabHandle.position);
113  transform.localScale = Vector3.one * ((currDistance / snapShotDistance) * snapShotOfScale) /*scaleMultiplier */ /* distFromUser*/;
114 
115  }
116 
117  yield return 0;
118  }
119 
120  currentlyScaling = false;
121  }
122  }
123 }
void AttemptScale()
We have two options when we attempt to scale: the first is by velocity and the second is based on the...
Definition: BaseScalable.cs:51
GrabStateEnum GrabState
Changes based on how many grabbers are grabbing this object
Transform GrabHandle
If not grab attach point is specified, use the GameObject transform by default
Definition: BaseGrabber.cs:60
Intended usage: scripts that inherit from this can be attached to the controller, or any object with ...
Definition: BaseGrabber.cs:18
//Intended Usage// Attach a "grabbable_x" script (a script that inherits from this) to any object tha...
virtual void OnEnable()
scale needs to subscribe to grab events in order to add more scalars to the list of scalars ...
Definition: BaseScalable.cs:37
void OnGrabbed(BaseGrabbable baseGrab)
Adding a grabber object to the list of scalars means adding it to the list of scalars and always atte...
Definition: BaseScalable.cs:81
class responsible for two hand scale. Objects with a child of this class attached ...
Definition: BaseScalable.cs:12
virtual IEnumerator PerformScaling()
scaling can be amplified by increasing the scaling multiplier scaling functionality can also be modif...
Definition: BaseScalable.cs:94