AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
LineRendererBase.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 HoloToolkit.Unity;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity.UX
8 {
9  public abstract class LineRendererBase : MonoBehaviour
10  {
11  [SerializeField]
12  [Tooltip("The source Line this component will render")]
13  protected LineBase source;
14 
15  [Header("Visual Settings")]
16  [Tooltip("Color gradient applied to line's normalized length")]
17  public Gradient LineColor;
18 
19  public AnimationCurve LineWidth = AnimationCurve.Linear(0f, 0.05f, 1f, 0.05f);
20  [Range(0f, 10f)]
21  public float WidthMultiplier = 0.25f;
22 
23  [Header("Offsets")]
24  [Range(0f, 10f)]
25  [Tooltip("Normalized offset for color gradient")]
26  public float ColorOffset = 0f;
27  [Range(0f, 10f)]
28  [Tooltip("Normalized offset for width curve")]
29  public float WidthOffset = 0f;
30  [Range(0f, 10f)]
31  [Tooltip("Normalized offset for rotation offset")]
32  public float RotationOffset = 0f;
33 
34  [Header("Point Placement")]
35  [Tooltip("Method for gathering points along line. Interpolated uses normalized length. FromSource uses line's base points. (FromSource may not look right for all Line types.)")]
36  public StepModeEnum StepMode = StepModeEnum.Interpolated;
37  [Range(0, 2048)]
38  [Tooltip("Number of steps to interpolate along line in Interpolated step mode")]
39  [ShowIfEnumValue("StepMode", StepModeEnum.Interpolated)]
40  public int NumLineSteps = 10;
41 
42  [FeatureInProgress]
43  public InterpolationModeEnum InterpolationMode = InterpolationModeEnum.FromLength;
44  [FeatureInProgress]
45  [Range(0.001f, 1f)]
46  public float StepLength = 0.05f;
47  [FeatureInProgress]
48  [Range(1, 2048)]
49  public int MaxLineSteps = 2048;
50  [FeatureInProgress]
51  public AnimationCurve StepLengthCurve = AnimationCurve.Linear(0f, 1f, 1f, 0.5f);
52 
53  public virtual LineBase Source
54  {
55  get
56  {
57  if (source == null)
58  {
59  source = GetComponent<LineBase>();
60  }
61  return source;
62  }
63  set
64  {
65  source = value;
66  if (source == null)
67  {
68  enabled = false;
69  }
70  }
71  }
72 
73  protected virtual Color GetColor(float normalizedLength)
74  {
75  if (LineColor == null)
76  {
77  LineColor = new Gradient();
78  }
79 
80  return LineColor.Evaluate(Mathf.Repeat(normalizedLength + ColorOffset, 1f));
81  }
82 
83  protected virtual float GetWidth(float normalizedLength)
84  {
85  if (LineWidth == null)
86  {
87  LineWidth = AnimationCurve.Linear(0f, 1f, 1f, 1f);
88  }
89 
90  return LineWidth.Evaluate(Mathf.Repeat(normalizedLength + WidthOffset, 1f)) * WidthMultiplier;
91  }
92 
93  private float[] normalizedLengths;
94 
95 #if UNITY_EDITOR
96  protected virtual void OnDrawGizmos()
97  {
98  if (Application.isPlaying)
99  return;
100 
101  if (source == null)
102  source = gameObject.GetComponent<LineBase>();
103  if (source == null || !source.enabled)
104  return;
105 
106  GizmosDrawLineRenderer(source, this);
107  }
108 
109  public static void GizmosDrawLineRenderer(LineBase source, LineRendererBase renderer)
110  {
111  switch (renderer.StepMode)
112  {
113  case StepModeEnum.FromSource:
114  GizmosDrawLineFromSource(source, renderer);
115  break;
116 
117  case StepModeEnum.Interpolated:
118  GizmosDrawLineInterpolated(source, renderer);
119  break;
120  }
121  }
122 
123  public static void GizmosDrawLineFromSource(LineBase source, LineRendererBase renderer)
124  {
125  Vector3 firstPos = source.GetPoint(0);
126  Vector3 lastPos = firstPos;
127  Color gColor = renderer.GetColor(0);
128 
129  gColor.a = 0.5f;
130  Gizmos.color = gColor;
131  Gizmos.DrawSphere(firstPos, renderer.GetWidth(0) / 2);
132  for (int i = 1; i < source.NumPoints; i++)
133  {
134  float normalizedLength = (1f / (source.NumPoints)) * i;
135  Vector3 currentPos = source.GetPoint(i);
136  gColor = renderer.GetColor(normalizedLength);
137  gColor.a = gColor.a * 0.5f;
138  Gizmos.color = gColor;
139  Gizmos.DrawLine(lastPos, currentPos);
140  Gizmos.DrawSphere(currentPos, renderer.GetWidth(normalizedLength) / 2);
141  lastPos = currentPos;
142  }
143 
144  if (source.Loops)
145  {
146  Gizmos.DrawLine(lastPos, firstPos);
147  }
148  }
149 
150  public static void GizmosDrawLineInterpolated(LineBase source, LineRendererBase renderer)
151  {
152  Vector3 firstPos = source.GetPoint(0f);
153  Vector3 lastPos = firstPos;
154  Color gColor = renderer.GetColor(0);
155 
156  gColor.a = 0.5f;
157  Gizmos.color = gColor;
158  Gizmos.DrawSphere(firstPos, renderer.GetWidth(0) / 2);
159  for (int i = 1; i <= renderer.NumLineSteps; i++)
160  {
161  float normalizedLength = (1f / (renderer.NumLineSteps)) * i;
162  Vector3 currentPos = source.GetPoint(normalizedLength);
163  gColor = renderer.GetColor(normalizedLength);
164  gColor.a = gColor.a * 0.5f;
165  Gizmos.color = gColor;
166  Gizmos.DrawLine(lastPos, currentPos);
167  Gizmos.DrawSphere(currentPos, renderer.GetWidth(normalizedLength) / 2);
168  lastPos = currentPos;
169  }
170 
171  if (source.Loops)
172  {
173  Gizmos.DrawLine(lastPos, firstPos);
174  }
175  }
176 #endif
177 
178  }
179 }
StepModeEnum
Default options for how to generate points in a line renderer
Definition: LineUtility.cs:41
virtual Color GetColor(float normalizedLength)
InterpolationModeEnum
Default options for how to distribute interpolated points in a line renderer
Definition: LineUtility.cs:50
virtual float GetWidth(float normalizedLength)
Vector3 GetPoint(float normalizedLength)
Gets a point along the line at the specified length
Definition: LineBase.cs:291