AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
Bezier.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;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity.UX
8 {
9  public class Bezier : LineBase
10  {
11  [Serializable]
12  private struct PointSet
13  {
14  public PointSet(float spread) {
15  Point1 = Vector3.right * spread * 0.5f;
16  Point2 = Vector3.right * spread * 0.25f;
17  Point3 = Vector3.left * spread * 0.25f;
18  Point4 = Vector3.left * spread * 0.5f;
19  }
20 
21  public Vector3 Point1;
22  public Vector3 Point2;
23  public Vector3 Point3;
24  public Vector3 Point4;
25  }
26 
27  [Header("Bezier Settings")]
28  [SerializeField]
29  private PointSet points = new PointSet(0.5f);
30 
31  public override int NumPoints {
32  get {
33  return 4;
34  }
35  }
36 
37  protected override Vector3 GetPointInternal(int pointIndex) {
38  switch (pointIndex) {
39  case 0:
40  return points.Point1;
41 
42  case 1:
43  return points.Point2;
44 
45  case 2:
46  return points.Point3;
47 
48  case 3:
49  return points.Point4;
50 
51  default:
52  return Vector3.zero;
53  }
54  }
55 
56  protected override void SetPointInternal(int pointIndex, Vector3 point) {
57  switch (pointIndex) {
58  case 0:
59  points.Point1 = point;
60  break;
61 
62  case 1:
63  points.Point2 = point;
64  break;
65 
66  case 2:
67  points.Point3 = point;
68  break;
69 
70  case 3:
71  points.Point4 = point;
72  break;
73 
74  default:
75  break;
76  }
77  }
78 
79  protected override Vector3 GetPointInternal(float normalizedDistance) {
80  return LineUtils.InterpolateBezeirPoints(points.Point1, points.Point2, points.Point3, points.Point4, normalizedDistance);
81  }
82 
83  protected override float GetUnclampedWorldLengthInternal() {
84  // Crude approximation
85  // TODO optimize
86  float distance = 0f;
87  Vector3 last = GetUnclampedPoint(0f);
88  for (int i = 1; i < 10; i++) {
89  Vector3 current = GetUnclampedPoint((float)i / 10);
90  distance += Vector3.Distance(last, current);
91  }
92  return distance;
93  }
94 
95  protected override Vector3 GetUpVectorInternal(float normalizedLength) {
96  // Bezier up vectors just use transform up
97  return transform.up;
98  }
99 
100 #if UNITY_EDITOR
101  [UnityEditor.CustomEditor(typeof(Bezier))]
102  public class CustomEditor : LineBaseEditor
103  {
104  protected override void DrawCustomSceneGUI() {
105  base.DrawCustomSceneGUI();
106 
107  Bezier line = (Bezier)target;
108 
109  line.SetPoint(0, SphereMoveHandle(line.GetPoint(0)));
110  line.SetPoint(1, SquareMoveHandle(line.GetPoint(1)));
111  line.SetPoint(2, SquareMoveHandle(line.GetPoint(2)));
112  line.SetPoint(3, SphereMoveHandle(line.GetPoint(3)));
113 
114  UnityEditor.Handles.color = handleColorTangent;
115  UnityEditor.Handles.DrawLine(line.GetPoint(0), line.GetPoint(1));
116  UnityEditor.Handles.DrawLine(line.GetPoint(2), line.GetPoint(3));
117  }
118  }
119 #endif
120  }
121 }
override Vector3 GetPointInternal(int pointIndex)
Get a point based on point index Point index will be pre-clamped
Definition: Bezier.cs:37
override Vector3 GetUpVectorInternal(float normalizedLength)
Gets the up vector at a normalized length along line (used for rotation)
Definition: Bezier.cs:95
override float GetUnclampedWorldLengthInternal()
Get the UNCLAMPED world length of the line
Definition: Bezier.cs:83
override void SetPointInternal(int pointIndex, Vector3 point)
Definition: Bezier.cs:56
static Vector3 InterpolateBezeirPoints(Vector3 point1, Vector3 point2, Vector3 point3, Vector3 point4, float normalizedLength)
Definition: LineUtility.cs:225
override Vector3 GetPointInternal(float normalizedDistance)
Get a point based on normalized distance along line Normalized distance will be pre-clamped ...
Definition: Bezier.cs:79
void SetPoint(int pointIndex, Vector3 point)
Sets a point in the line This function is not guaranteed to have an effect
Definition: LineBase.cs:329
Vector3 GetPoint(float normalizedLength)
Gets a point along the line at the specified length
Definition: LineBase.cs:291