AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ToolTipBackgroundBlob.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // Licensed under the MIT License. See LICENSE in the project root for license information.
4 //
5 using UnityEngine;
6 
7 namespace HoloToolkit.UX.ToolTips
8 {
14  {
19  [Header("Transform targets")]
20  [SerializeField]
21  private Transform positionTarget = null;
22 
23  [SerializeField]
24  private Transform rotationTarget = null;
25 
26  [SerializeField]
27  private Transform distortionTarget = null;
28 
29  [SerializeField]
30  private Transform attachPointOffset = null;
31 
32  [Header("Blob settings")]
33  [SerializeField]
34  [Range(0f, 5f)]
35  private float blobInertia = 0.5f;
39  public float BlobInertia
40  {
41  get
42  {
43  return blobInertia;
44  }
45  set
46  {
47  blobInertia = Mathf.Clamp(value, 0, maxInertia);
48  }
49  }
50 
51  [SerializeField]
52  [Range(0f, 1f)]
53  private float blobDistortion = 0.1f;
57  public float BlobDistortion
58  {
59  get
60  {
61  return blobDistortion;
62  }
63  set
64  {
65  blobDistortion = Mathf.Clamp(value, 0, maxDistortion);
66  }
67  }
68 
69  [SerializeField]
70  [Range(0f, 1f)]
71  private float blobRotation = 0.1f;
75  public float BlobRotation
76  {
77  get
78  {
79  return blobRotation;
80  }
81  set
82  {
83  blobRotation = Mathf.Clamp(value, 0, maxRotation);
84  }
85  }
86 
87  [SerializeField]
88  [Range(0.1f, 5f)]
89  private float positionCorrectionStrength = 1f;
93  public float PositionCorrectionStrength
94  {
95  get
96  {
97  return positionCorrectionStrength;
98  }
99  set
100  {
101  positionCorrectionStrength = Mathf.Clamp(value, minPositionCorrection, maxPositionCorrection);
102  }
103  }
104 
105  [SerializeField]
106  [Range(0.1f, 5f)]
107  private float distortionCorrectionStrength = 1f;
111  public float DistortionCorrectionStrength
112  {
113  get
114  {
115  return distortionCorrectionStrength;
116  }
117  set
118  {
119  distortionCorrectionStrength = Mathf.Clamp(value, minDistortionCorrection, maxDistortionCorrection);
120  }
121  }
122 
123  [SerializeField]
124  [Range(0.1f, 5f)]
125  private float rotationCorrectionStrength = 1f;
129  public float RotationCorrectionStrength
130  {
131  get
132  {
133  return rotationCorrectionStrength;
134  }
135  set
136  {
137  rotationCorrectionStrength = Mathf.Clamp(value, minRotationCorrection, maxRotationCorrection);
138  }
139  }
140 
141  [SerializeField]
142  private Vector3 blobOffset;
146  public Vector3 BlobOffset
147  {
148  get
149  {
150  return blobOffset;
151  }
152  set
153  {
154  blobOffset = value;
155  }
156  }
157 
158  private const float maxInertia = 5f;
159  private const float maxDistortion = 1f;
160  private const float maxRotation = 1f;
161  private const float minPositionCorrection = 0.1f;
162  private const float minDistortionCorrection = 0.1f;
163  private const float minRotationCorrection = 0.1f;
164  private const float maxPositionCorrection = 5f;
165  private const float maxDistortionCorrection = 5f;
166  private const float maxRotationCorrection = 5f;
167 
168  private Bounds defaultBounds = new Bounds(Vector3.zero, Vector3.one);
169  private MeshFilter backgroundRendererMeshFilter;
170  private Vector3 lastPosition;
171  private Vector3 velocity;
172  private Vector3 distortion;
173  private Vector3 rotation;
174  private Bounds localContentBounds;
175  private Bounds inertialContentBounds;
176 
180  public MeshRenderer BackgroundRenderer;
181 
182  protected override void OnEnable()
183  {
184  base.OnEnable();
185  lastPosition = positionTarget.position;
186  inertialContentBounds = defaultBounds;
187  localContentBounds = defaultBounds;
188  velocity = Vector3.zero;
189  backgroundRendererMeshFilter = BackgroundRenderer.GetComponent<MeshFilter>();
190  }
191 
192  protected override void ScaleToFitContent()
193  {
194  // Get the local size of the content - this is the scale of the text under the content parent
195  Vector3 localContentSize = toolTip.LocalContentSize;
196  Vector3 localContentOffset = toolTip.LocalContentOffset;
197 
198  // Get the size of the mesh and use this to adjust the local content size on the x / y axis
199  // This will accomodate meshes that aren't built to 1,1 scale
200  Bounds meshBounds = backgroundRendererMeshFilter.sharedMesh.bounds;
201  localContentSize.x /= meshBounds.size.x;
202  localContentSize.y /= meshBounds.size.y;
203  localContentSize.z = 1;
204 
205  localContentBounds.size = Vector3.one; //localContentSize;
206  localContentBounds.center = localContentOffset;
207  }
208 
209  private void Update()
210  {
211  // Adjust center and size by velocity
212  Vector3 currentPosition = positionTarget.position;
213  velocity = Vector3.Lerp(velocity, lastPosition - currentPosition, 1f / blobInertia * Time.deltaTime);
214  Vector3 currentDistortion = -velocity * blobDistortion;
215  distortion = Vector3.Lerp(distortion, currentDistortion, 1f / blobDistortion * Time.deltaTime);
216 
217  inertialContentBounds.center = inertialContentBounds.center + velocity;
218  Vector3 size = inertialContentBounds.size + distortion;
219  inertialContentBounds.size = size;
220 
221  Vector3 currentRotation = Vector3.zero;
222  currentRotation.x = velocity.x * 360;
223  currentRotation.z = velocity.z * 360;
224  currentRotation.y = velocity.y * 360;
225  currentRotation = rotationTarget.TransformDirection(currentRotation);
226  rotation = Vector3.Lerp (rotation, currentRotation, 1f / blobRotation * Time.deltaTime);
227 
228  // Correct the center and size
229  inertialContentBounds.center = Vector3.Lerp(inertialContentBounds.center, localContentBounds.center, Time.deltaTime * positionCorrectionStrength);
230  inertialContentBounds.size = Vector3.Lerp(inertialContentBounds.size, localContentBounds.size, Time.deltaTime * distortionCorrectionStrength);
231  rotationTarget.localRotation = Quaternion.Lerp(positionTarget.localRotation, Quaternion.identity, Time.deltaTime * rotationCorrectionStrength);
232 
233  // Apply the center and size
234  positionTarget.localPosition = inertialContentBounds.center + blobOffset;
235  distortionTarget.localScale = inertialContentBounds.size;
236  rotationTarget.Rotate(velocity * blobRotation * 360);
237 
238  // Adjust the tool tip attach position
239  toolTip.AttachPointPosition = attachPointOffset.position;
240 
241  lastPosition = currentPosition;
242  }
243  }
244 }
A background with &#39;fake&#39; inertia Useful for soft or liquid objects
MeshRenderer BackgroundRenderer
Mesh renderer for mesh background.
Base class for a tool tip background Automatically finds a ToolTip and subscribes to ContentChange ac...