AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
DistorterBulge.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 System.Collections.Generic;
6 using UnityEngine;
7 
8 namespace HoloToolkit.Unity.UX
9 {
10  public class DistorterBulge : Distorter
11  {
12  public Vector3 BulgeCenter
13  {
14  get
15  {
16  return transform.TransformPoint(bulgeCenter);
17  }
18  set
19  {
20  bulgeCenter = transform.InverseTransformPoint(value);
21  }
22  }
23 
24  [SerializeField]
25  private Vector3 bulgeCenter = Vector3.zero;
26  [SerializeField]
27  private AnimationCurve bulgeFalloff = new AnimationCurve();
28  [SerializeField]
29  private float bulgeRadius = 1f;
30  [SerializeField]
31  private float scaleDistort = 2f;
32  [SerializeField]
33  private float bulgeStrength = 1f;
34 
35  protected override Vector3 DistortPointInternal (Vector3 point, float strength)
36  {
37  float distanceToCenter = Vector3.Distance(point, BulgeCenter);
38  if (distanceToCenter < bulgeRadius)
39  {
40  float distortion = (1f - (bulgeFalloff.Evaluate(distanceToCenter / bulgeRadius))) * bulgeStrength;
41  Vector3 direction = (point - BulgeCenter).normalized;
42  point = point + (direction * distortion * bulgeStrength);
43  }
44  return point;
45  }
46 
47  protected override Vector3 DistortScaleInternal(Vector3 point, float strength)
48  {
49  float distanceToCenter = Vector3.Distance(point, BulgeCenter);
50  if (distanceToCenter < bulgeRadius)
51  {
52  float distortion = (1f - (bulgeFalloff.Evaluate(distanceToCenter / bulgeRadius))) * bulgeStrength;
53  return Vector3.one + (Vector3.one * distortion * scaleDistort);
54  }
55  return Vector3.one;
56  }
57 
58  private void OnDrawGizmos()
59  {
60  Vector3 bulgePoint = transform.TransformPoint(bulgeCenter);
61  Color gColor = Color.red;
62  gColor.a = 0.5f;
63  Gizmos.color = gColor;
64  Gizmos.DrawWireSphere(bulgePoint, bulgeRadius);
65  int steps = 8;
66  for (int i = 0; i < steps; i++)
67  {
68  float normalizedStep = (1f / steps) * i;
69  gColor.a = (1f - bulgeFalloff.Evaluate(normalizedStep)) * 0.5f;
70  Gizmos.color = gColor;
71  Gizmos.DrawSphere(bulgePoint, bulgeRadius * bulgeFalloff.Evaluate(normalizedStep));
72  }
73  }
74  }
75 }
override Vector3 DistortScaleInternal(Vector3 point, float strength)
Internal function where scale distortion is done
override Vector3 DistortPointInternal(Vector3 point, float strength)
Internal function where position distortion is done