-
Notifications
You must be signed in to change notification settings - Fork 0
/
NavigationHelper.cs
318 lines (282 loc) · 11.2 KB
/
NavigationHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NavigationHelper : MonoBehaviour {
enum TPlanes { XZ, XY, YZ };
int segments;
GameObject helperTranslationTrajectoryGO;
GameObject helperCameraGO;
GameObject navigationAssistantsGO;
GameObject planeGO;
GameObject helperPointToLookCameraGO;
GameObject helperEvoluteCusp1GO;
GameObject helperEvoluteCusp2GO;
LineRenderer lineTranslationEllipse;
LineRenderer lineRotationEllipse;
LineRenderer lineHorizontalFrameworkEllipse;
LineRenderer lineVerticalFrameworkEllipse;
LineRenderer helperCameraViewLine;
bool activated;
// Use this for initialization
void Awake ()
{
activated = false;
foreach (Transform child in transform)
{
if (child.name == "HelperTranslationTrajectory")
{
helperTranslationTrajectoryGO = child.gameObject;
lineTranslationEllipse = child.GetComponentInChildren<LineRenderer>();
}
if (child.name == "HelperCamera")
{
helperCameraGO = child.gameObject;
helperCameraViewLine = child.GetComponentInChildren<LineRenderer>();
}
if (child.name == "Plane")
{
planeGO = child.gameObject;
}
}
navigationAssistantsGO = GameObject.Find("NavigationAssistants");
foreach (Transform child in navigationAssistantsGO.transform)
{
if (child.name == "HelperRotationTrajectory")
{
lineRotationEllipse = child.GetComponentInChildren<LineRenderer>();
}
if (child.name == "HelperFrameworkEllipseH")
{
lineHorizontalFrameworkEllipse = child.GetComponentInChildren<LineRenderer>();
}
if (child.name == "HelperFrameworkEllipseV")
{
lineVerticalFrameworkEllipse = child.GetComponentInChildren<LineRenderer>();
}
if (child.name == "HelperPointToLookCamera")
{
helperPointToLookCameraGO = child.gameObject;
}
if (child.name == "HelperPointEvoluteCusp1")
{
helperEvoluteCusp1GO = child.gameObject;
}
if (child.name == "HelperPointEvoluteCusp2")
{
helperEvoluteCusp2GO = child.gameObject;
}
}
segments = 3600;
//if (hom3r.state.platform == THom3rPlatform.Editor || hom3r.state.platform == THom3rPlatform.Windows) { SetActivatedNavigationHelper(true); }
//else { SetActivatedNavigationHelper(false); }
SetActivatedNavigationHelper(true);
}
public void SetActivatedNavigationHelper(bool _activated)
{
if (_activated && activated) { return; }
activated = _activated;
if (_activated)
{
helperTranslationTrajectoryGO.SetActive(true);
helperCameraGO.SetActive(true);
helperPointToLookCameraGO.SetActive(true);
helperEvoluteCusp1GO.SetActive(true);
helperEvoluteCusp2GO.SetActive(true);
planeGO.SetActive(true);
navigationAssistantsGO.SetActive(true);
InitLineRenderer(lineTranslationEllipse, 1.2f, segments);
InitLineRenderer(lineRotationEllipse, 1.2f, segments);
InitLineRenderer(helperCameraViewLine, 1f, 1);
InitLineRenderer(lineHorizontalFrameworkEllipse, 0.9f, segments);
InitLineRenderer(lineVerticalFrameworkEllipse, 0.9f, segments);
}
else
{
helperTranslationTrajectoryGO.SetActive(false);
helperCameraGO.SetActive(false);
planeGO.SetActive(false);
navigationAssistantsGO.SetActive(false);
helperPointToLookCameraGO.SetActive(false);
helperEvoluteCusp1GO.SetActive(false);
helperEvoluteCusp2GO.SetActive(false);
}
}
private void InitLineRenderer(LineRenderer lineRenderer, float widthMultiplier, int positions)
{
lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
lineRenderer.widthMultiplier = widthMultiplier;
lineRenderer.positionCount = (positions + 1);
lineRenderer.useWorldSpace = false;
}
public void InitNavigationAssistantsPosition(Vector3 position)
{
navigationAssistantsGO.transform.position = position;
}
public void SetBiggerLines()
{
if (!activated) { return; }
lineTranslationEllipse.widthMultiplier += 0.1f;
lineRotationEllipse.widthMultiplier += 0.1f;
lineHorizontalFrameworkEllipse.widthMultiplier +=0.1f;
lineVerticalFrameworkEllipse.widthMultiplier += 0.1f;
helperCameraViewLine.widthMultiplier += 0.1f;
}
public void SetSmallerLines()
{
if (!activated) { return; }
lineTranslationEllipse.widthMultiplier -= 0.1f;
lineRotationEllipse.widthMultiplier -= 0.1f;
lineHorizontalFrameworkEllipse.widthMultiplier -= 0.1f;
lineVerticalFrameworkEllipse.widthMultiplier -= 0.1f;
helperCameraViewLine.widthMultiplier -= 0.1f;
}
public void SetBiggerCamera()
{
if (!activated) { return; }
Vector3 newScale = helperCameraGO.transform.localScale * 1.1f;
helperCameraGO.transform.localScale = newScale;
}
public void SetSmallerCamera()
{
if (!activated) { return; }
Vector3 newScale = helperCameraGO.transform.localScale * 0.9f;
helperCameraGO.transform.localScale = newScale;
}
public void SetBiggerPointToLook()
{
if (!activated) { return; }
Vector3 newScale = helperPointToLookCameraGO.transform.localScale * 1.1f;
helperPointToLookCameraGO.transform.localScale = newScale;
}
public void SetSmallerPointToLook()
{
if (!activated) { return; }
Vector3 newScale = helperPointToLookCameraGO.transform.localScale * 0.9f;
helperPointToLookCameraGO.transform.localScale = newScale;
}
////////////////////////////////
// Draw Ellipses Methods
////////////////////////////////
/// <summary>
///
/// </summary>
/// <param name="xRadius"></param>
/// <param name="zRadius"></param>
public void DrawTranslationEllipse(float xRadius, float zRadius)
{
if (activated)
//if (hom3r.state.platform == THom3rPlatform.Editor)
{
DrawEllipse(xRadius, zRadius, 0f, TPlanes.XZ, lineTranslationEllipse);
}
}
public void DrawRotationEllipse(float zRadius, float yRadius, float offset)
{
if (activated)
// if (hom3r.state.platform == THom3rPlatform.Editor)
{
DrawEllipse(zRadius, yRadius, -offset, TPlanes.YZ, lineRotationEllipse);
}
}
public void DrawHorizontalFrameworkEllipse(float xRadius, float zRadius)
{
if (activated)
//if (hom3r.state.platform == THom3rPlatform.Editor)
{
DrawEllipse(xRadius, zRadius, 0f, TPlanes.XZ, lineHorizontalFrameworkEllipse);
}
}
public void DrawVerticalFrameworkEllipse(float xRadius, float yRadius)
{
if (activated)
// if (hom3r.state.platform == THom3rPlatform.Editor)
{
DrawEllipse(xRadius, yRadius, 0f, TPlanes.XY, lineVerticalFrameworkEllipse);
}
}
public void MoveCameraHelper(Vector3 cameraPosition)
{
if (activated)
//if (hom3r.state.platform == THom3rPlatform.Editor)
{
// Move Camera
helperCameraGO.transform.localPosition = cameraPosition;
}
}
public void SetCameraOrientationHelper(Vector3 pointToLookWorld)
{
if (activated)
//if (hom3r.state.platform == THom3rPlatform.Editor)
{
// Rotate Fake Camera
helperCameraGO.transform.localEulerAngles = Camera.main.transform.localEulerAngles;
// Rotate Fake Camera
//helperCamera.transform.LookAt(pointToLook);
// Draw look direction line
helperCameraViewLine.SetPosition(0, Vector3.zero);
Vector3 poinToLook_local = helperCameraGO.transform.InverseTransformPoint(pointToLookWorld);
helperCameraViewLine.SetPosition(1, poinToLook_local);
helperPointToLookCameraGO.GetComponent<Transform>().SetPositionAndRotation(pointToLookWorld, Quaternion.identity);
}
}
public void SetEvoluteCuspHelper(Vector3 pointEvoluteCusp1, Vector3 pointEvoluteCusp2)
{
Vector3 pointEvoluteCusp1World = hom3r.quickLinks.navigationSystemObject.transform.TransformPoint(pointEvoluteCusp1);
helperEvoluteCusp1GO.GetComponent<Transform>().SetPositionAndRotation(pointEvoluteCusp1World, Quaternion.identity);
Vector3 pointEvoluteCusp2World = hom3r.quickLinks.navigationSystemObject.transform.TransformPoint(pointEvoluteCusp2);
helperEvoluteCusp2GO.GetComponent<Transform>().SetPositionAndRotation(pointEvoluteCusp2World, Quaternion.identity);
}
/// <summary>
/// Draw a ellipse, in one plane, using a lineRenderer of the scene
/// </summary>
/// <param name="a">Semi-major axis of the ellipse</param>
/// <param name="b">Semi-minos axis of the ellipse</param>
/// <param name="offset">Offset of the ellipse from the origin</param>
/// <param name="plane">Plane where the plane is going to be drawn</param>
/// <param name="lineRenderer">Line rendeder that is going to used to draw the ellipse
/// </param>
private void DrawEllipse(float a, float b, float offset, TPlanes plane, LineRenderer lineRenderer)
{
List<Vector3> points = new List<Vector3>();
float x = 0f;
float y = 0f;
float z = 0f;
float angle = 0f;
for (int i = 0; i < (segments + 1); i++)
{
if (plane == TPlanes.XZ)
{
x = Mathf.Cos(Mathf.Deg2Rad * angle) * a;
z = Mathf.Sin(Mathf.Deg2Rad * angle) * b;
y = offset;
}
else if (plane == TPlanes.YZ)
{
z = Mathf.Cos(Mathf.Deg2Rad * angle) * a;
y = Mathf.Sin(Mathf.Deg2Rad * angle) * b;
x = offset;
} else if (plane == TPlanes.XY)
{
x = Mathf.Cos(Mathf.Deg2Rad * angle) * a;
y = Mathf.Sin(Mathf.Deg2Rad * angle) * b;
z = offset;
}
points.Add(new Vector3(x, y, z));
angle += (360f / segments);
}
lineRenderer.SetPositions(points.ToArray());
}
public void InitHelpPlaneSize(Bounds boundingBox)
{
if (!activated) { return; }
if (planeGO != null)
{
planeGO.transform.localScale = new Vector3(boundingBox.extents.x * .5f, 1, boundingBox.extents.z * .5f);
}
}
public void InitNavigationHelperRotation(Vector3 rotationEulerAngles)
{
if (!activated) { return; }
navigationAssistantsGO.transform.eulerAngles = rotationEulerAngles;
}
}