AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
LineUnity.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.UX
8 {
9  [UseWith(typeof(LineBase))]
10  public class LineUnity : LineRendererBase
11  {
12  const string DefaultLineShader = "Particles/Alpha Blended";
13  const string DefaultLineShaderColor = "_TintColor";
14 
15  [Header("LineUnity Settings")]
16  [Tooltip("The material to use for the Unity LineRenderer (will be auto-generated if null)")]
17  public Material LineMaterial;
18 
19  public bool RoundedEdges = true;
20  public bool RoundedCaps = true;
21 
22  [SerializeField]
23  private UnityEngine.LineRenderer lineRenderer;
24 
25  private Vector3[] positions;
26 
27  protected void OnEnable()
28  {
29  // If we haven't specified a line renderer
30  if (lineRenderer == null)
31  {
32  // Get or create one that's attached to this gameObject
33  lineRenderer = gameObject.EnsureComponent<UnityEngine.LineRenderer>();
34  }
35 
36  if (LineMaterial == null)
37  {
38  LineMaterial = new Material(Shader.Find(DefaultLineShader));
39  LineMaterial.SetColor(DefaultLineShaderColor, Color.white);
40  }
41 
42  StartCoroutine(UpdateLineUnity());
43  }
44 
45  private IEnumerator UpdateLineUnity()
46  {
47  while (isActiveAndEnabled)
48  {
49  if (!source.enabled)
50  {
51  lineRenderer.enabled = false;
52  }
53  else
54  {
55  lineRenderer.enabled = true;
56 
57  switch (StepMode)
58  {
59  case StepModeEnum.FromSource:
60  lineRenderer.positionCount = source.NumPoints;
61  if (positions == null || positions.Length != source.NumPoints)
62  {
63  positions = new Vector3[source.NumPoints];
64  }
65  for (int i = 0; i < positions.Length; i++)
66  {
67  positions[i] = source.GetPoint(i);
68  }
69  break;
70 
71  case StepModeEnum.Interpolated:
72  lineRenderer.positionCount = NumLineSteps;
73  if (positions == null || positions.Length != NumLineSteps)
74  {
75  positions = new Vector3[NumLineSteps];
76  }
77  for (int i = 0; i < positions.Length; i++)
78  {
79  float normalizedDistance = (1f / (NumLineSteps - 1)) * i;
80  positions[i] = source.GetPoint(normalizedDistance);
81  }
82  break;
83  }
84 
85  // Set line renderer properties
86  lineRenderer.loop = source.Loops;
87  lineRenderer.numCapVertices = RoundedCaps ? 8 : 0;
88  lineRenderer.numCornerVertices = RoundedEdges ? 8 : 0;
89  lineRenderer.useWorldSpace = true;
90  lineRenderer.startWidth = 1;
91  lineRenderer.endWidth = 1;
92  lineRenderer.startColor = Color.white;
93  lineRenderer.endColor = Color.white;
94  lineRenderer.sharedMaterial = LineMaterial;
95  lineRenderer.widthCurve = LineWidth;
96  lineRenderer.widthMultiplier = WidthMultiplier;
97  lineRenderer.colorGradient = LineColor;
98  lineRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
99  lineRenderer.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
100  // Set positions
101  lineRenderer.positionCount = positions.Length;
102  lineRenderer.SetPositions(positions);
103  }
104  yield return null;
105  }
106  }
107 
108 #if UNITY_EDITOR
109  [UnityEditor.CustomEditor(typeof(LineUnity))]
110  public class CustomEditor : MRTKEditor { }
111 #endif
112 
113  }
114 }
StepModeEnum
Default options for how to generate points in a line renderer
Definition: LineUtility.cs:41