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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
///
/// Draw lines at runtime
/// by Nothke
/// unlicensed, aka do whatever you want with it
/// made during Stugan 2016 :)
/// ..(it's midnight, and Robbie clicks A LOT, LOUDLY!)
///
/// Important:
/// - Should be called in OnPostRender() (after everything else has been drawn)
/// therefore the script that calls it must be attached to the camera
/// - Use someMaterial.SetPass(0) to set that material for line rendering
/// If no material is SetPass'd, Unity default material will be used
/// - Use Draw.color = someColor to set line color
/// - Most parameters are passed as screen 0-1 points (see function summaries)
/// To input as pixel coordinates use PixelsToScreen(screenV2Coordinate)
///
/// See function summaries for what this script can do
/// and check DrawExaple.cs for examples
///
using UnityEngine;
using System.Collections.Generic;
namespace Rigging.Debugging
{
public static class Draw
{
public static Color color = Color.white;
#region Line
/// <summary>
/// Draws line between 2 screen points. Use Draw.PixelToScreen to convert from pixels to screen points
/// </summary>
public static void Line(Vector2 p1, Vector2 p2)
{
Prepare();
// Vertices
GL.Vertex(p1);
GL.Vertex(p2);
Postpare();
}
/// <summary>
/// Draws line between 2 world points
/// </summary>
public static void Line3D(Vector3 p1, Vector3 p2)
{
Prepare3D();
GL.Vertex(p1);
GL.Vertex(p2);
Postpare();
}
#endregion
#region Circles and Ellipses
/// <summary>
/// Draws circle using a screen point center, and pixel radius. Use Draw.ScreenToPixel to convert screen points to pixels
/// </summary>
public static void Circle(Vector2 center, float pixelRadius)
{
Vector2 size = new Vector2(pixelRadius / Screen.width, pixelRadius / Screen.height);
Ellipse(center, size);
}
/// <summary>
/// Draws dashed circle using a screen point center, and pixel radius. Use Draw.ScreenToPixel to convert screen points to pixels
/// The dashes are not very nice, just skipping every other point..
/// </summary>
public static void CircleDashed(Vector2 center, float radius)
{
Prepare();
float radX = radius / Screen.width;
float radY = radius / Screen.height;
// Vertices
for (float theta = 0.0f; theta < (2 * Mathf.PI); theta += 0.01f)
{
Vector2 ci = new Vector2(center.x + (Mathf.Cos(theta) * radX), center.y + (Mathf.Sin(theta) * radY));
GL.Vertex(ci);
}
Postpare();
}
/// <summary>
/// Draws an ellipse using a center, and size (in screen fractions). Use Draw.ScreenToPixel to convert screen points to pixels
/// </summary>
public static void Ellipse(Vector2 center, Vector2 size)
{
Prepare();
float radX = size.x;
float radY = size.y;
// Vertices
for (float theta = 0.0f; theta < (2 * Mathf.PI); theta += 0.01f)
{
Vector2 ci = new Vector2(center.x + (Mathf.Cos(theta) * radX), center.y + (Mathf.Sin(theta) * radY));
GL.Vertex(ci);
if (theta != 0)
GL.Vertex(ci);
}
Postpare();
}
/// <summary>
/// Draws a circle in world space
/// </summary>
public static void Circle3D(Vector3 center, float radius, Vector3 normal)
{
Prepare3D();
normal = normal.normalized;
Vector3 forward = normal == Vector3.up ?
Vector3.ProjectOnPlane(Vector3.forward, normal).normalized :
Vector3.ProjectOnPlane(Vector3.up, normal);
Vector3 right = Vector3.Cross(normal, forward);
for (float theta = 0.0f; theta < (2 * Mathf.PI); theta += 0.01f)
{
Vector3 ci = center + forward * Mathf.Cos(theta) * radius + right * Mathf.Sin(theta) * radius;
GL.Vertex(ci);
if (theta != 0)
GL.Vertex(ci);
}
Postpare();
}
/// <summary>
/// Draws an elliptic orbit using eccentricity and semi-major axis in pixels
/// </summary>
public static void Orbit(Vector2 center, float eccentricity, float semiMajorAxis, float dir = 0)
{
Prepare();
eccentricity = Mathf.Clamp01(eccentricity);
Vector2 up = new Vector2(Mathf.Cos(dir), Mathf.Sin(dir));
Vector2 right = Vector3.Cross(up, Vector3.forward);
for (float theta = 0.0f; theta < (2 * Mathf.PI); theta += 0.01f)
{
float r = (semiMajorAxis * (1 - eccentricity * eccentricity)) / (1 + eccentricity * Mathf.Cos(theta));
Vector2 point = PixelToScreen((right * Mathf.Cos(theta) * r) + (up * Mathf.Sin(theta) * r));
Vector2 ci = center + point;
GL.Vertex(ci);
if (theta != 0)
GL.Vertex(ci);
}
Postpare();
}
/// <summary>
/// Draws an elliptic orbit using periapsis and apoapsis in pixels
/// </summary>
public static void OrbitApses(Vector2 center, float periapsis, float apoapsis, float dir = 0)
{
float a = (periapsis + apoapsis) / 2;
float e = (apoapsis - periapsis) / (apoapsis + periapsis);
Orbit(center, e, a, dir);
}
/// <summary>
/// Draws an elliptic orbit in world space using eccentricity and semi-major axis
/// </summary>
public static void Orbit3D(Vector3 center, float eccentricity, float semiMajorAxis, Vector3 normal, Vector3 forward)
{
Prepare3D();
eccentricity = Mathf.Clamp01(eccentricity);
forward = Vector3.ProjectOnPlane(forward, normal).normalized;
Vector3 right = Vector3.Cross(forward, normal).normalized;
for (float theta = 0.0f; theta < (2 * Mathf.PI); theta += 0.01f)
{
float r = (semiMajorAxis * (1 - eccentricity * eccentricity)) / (1 + eccentricity * Mathf.Cos(theta));
Vector3 point = (right * Mathf.Cos(theta) * r) + (forward * Mathf.Sin(theta) * r);
Vector3 ci = center + point;
GL.Vertex(ci);
if (theta != 0)
GL.Vertex(ci);
}
Postpare();
}
/// <summary>
/// Draws an elliptic orbit in world space using periapsis and apoapsis
/// </summary>
public static void Orbit3DApses(Vector3 center, float periapsis, float apoapsis, Vector3 normal, Vector3 forward)
{
float a = (periapsis + apoapsis) / 2;
float e = (apoapsis - periapsis) / (apoapsis + periapsis);
Orbit3D(center, e, a, normal, forward);
}
#endregion
#region Rectangle
/// <summary>
/// Draws rectangle on screen using a Rect (in pixels)
/// </summary>
public static void Rect(Rect rect)
{
Rect(rect.x, rect.y, rect.width, rect.height);
}
/// <summary>
/// Draws rectangle on screen (in screen 0-1 fractions)
/// </summary>
public static void RectScreen(float x, float y, float width, float height)
{
Prepare();
GL.Vertex(new Vector3(x, y));
GL.Vertex(new Vector3(x + width, y));
GL.Vertex(new Vector3(x + width, y));
GL.Vertex(new Vector3(x + width, y + height));
GL.Vertex(new Vector3(x + width, y + height));
GL.Vertex(new Vector3(x, y + height));
GL.Vertex(new Vector3(x, y + height));
GL.Vertex(new Vector3(x, y));
Postpare();
}
/// <summary>
/// Draws rectangle on screen (in pixels)
/// </summary>
public static void Rect(float x, float y, float width, float height)
{
PreparePixel();
GL.Vertex(new Vector3(x, y));
GL.Vertex(new Vector3(x + width, y));
GL.Vertex(new Vector3(x + width, y));
GL.Vertex(new Vector3(x + width, y + height));
GL.Vertex(new Vector3(x + width, y + height));
GL.Vertex(new Vector3(x, y + height));
GL.Vertex(new Vector3(x, y + height));
GL.Vertex(new Vector3(x, y));
Postpare();
}
#endregion
#region Grid
/// <summary>
/// Draws a horizontal grid
/// </summary>
public static void Grid(Vector3 center, float edgeLength, int lines = 10)
{
Grid(center, edgeLength, lines, Vector3.forward, Vector3.up);
}
/// <summary>
/// Draws a gird with custom orientantion
/// </summary>
public static void Grid(Vector3 center, float edgeLength, int lines, Vector3 forward, Vector3 normal)
{
if (lines <= 1) return;
Prepare3D();
forward = forward.normalized;
normal = Vector3.ProjectOnPlane(normal, forward).normalized;
Vector3 right = Vector3.Cross(forward, normal);
// forward lines
for (int i = 0; i < lines; i++)
{
Vector3 fDir = forward * edgeLength * 0.5f;
Vector3 rDir = right * (-(edgeLength * 0.5f) + (i * edgeLength / (lines - 1)));
GL.Vertex(center - fDir + rDir);
GL.Vertex(center + fDir + rDir);
}
// sideways lines
for (int i = 0; i < lines; i++)
{
Vector3 rDir = right * edgeLength * 0.5f;
Vector3 fDir = forward * (-(edgeLength * 0.5f) + (i * edgeLength / (lines - 1)));
GL.Vertex(center - rDir + fDir);
GL.Vertex(center + rDir + fDir);
}
Postpare();
}
#endregion
#region 3D Primitives
/// <summary>
/// Draws a sphere in world space. Similar to Gizmos.DrawWireSphere, but with the ability to add radial and vertical segments
/// </summary>
public static void Sphere(Vector3 center, float radius, int verticalSegments = 1, int radialSegments = 2)
{
if (radialSegments > 2)
{
for (int i = 0; i < radialSegments; i++)
{
Vector3 normal = new Vector3(Mathf.Sin((i * Mathf.PI) / radialSegments), 0, Mathf.Cos((i * Mathf.PI) / radialSegments));
Circle3D(center, radius, normal);
}
}
else
{
Circle3D(center, radius, Vector3.forward);
Circle3D(center, radius, Vector3.right);
}
if (verticalSegments > 1)
{
for (int i = 1; i < verticalSegments; i++)
{
Vector3 c = center + Vector3.up * (-radius + (i * 2 * (radius / (verticalSegments))));
// Radius of base circle is a=sqrt(h(2R-h)),
float height = ((float)i / verticalSegments) * radius * 2;
float ra = Mathf.Sqrt(height * (2 * radius - height));
Circle3D(c, ra, Vector3.up);
}
}
else
Circle3D(center, radius, Vector3.up);
}
public static void Cube(Vector3 center, Vector3 size)
{
Cube(center, size, Vector3.forward, Vector3.up);
}
public static void Cube(Vector3 center, Vector3 size, Vector3 forward, Vector3 up)
{
Prepare3D();
forward = forward.normalized;
up = Vector3.ProjectOnPlane(up, forward).normalized;
Vector3 right = Vector3.Cross(forward, up);
Vector3 frw = forward * size.z * 0.5f;
Vector3 rgt = right * size.x * 0.5f;
Vector3 upw = up * size.y * 0.5f;
// vertical lines
GL.Vertex(center - frw - rgt - upw);
GL.Vertex(center - frw - rgt + upw);
GL.Vertex(center - frw + rgt - upw);
GL.Vertex(center - frw + rgt + upw);
GL.Vertex(center + frw - rgt - upw);
GL.Vertex(center + frw - rgt + upw);
GL.Vertex(center + frw + rgt - upw);
GL.Vertex(center + frw + rgt + upw);
// horizontal lines
GL.Vertex(center - frw - rgt - upw);
GL.Vertex(center - frw + rgt - upw);
GL.Vertex(center - frw - rgt + upw);
GL.Vertex(center - frw + rgt + upw);
GL.Vertex(center + frw - rgt - upw);
GL.Vertex(center + frw + rgt - upw);
GL.Vertex(center + frw - rgt + upw);
GL.Vertex(center + frw + rgt + upw);
// forward lines
GL.Vertex(center - frw - rgt - upw);
GL.Vertex(center + frw - rgt - upw);
GL.Vertex(center - frw + rgt - upw);
GL.Vertex(center + frw + rgt - upw);
GL.Vertex(center - frw - rgt + upw);
GL.Vertex(center + frw - rgt + upw);
GL.Vertex(center - frw + rgt + upw);
GL.Vertex(center + frw + rgt + upw);
Postpare();
}
#endregion
#region Wireframe
public struct Edge
{
public int i1;
public int i2;
public Edge(int i1, int i2)
{
this.i1 = i1;
this.i2 = i2;
}
public bool Match(Edge e)
{
return (e.i1 == i1 && e.i2 == i2) || (e.i1 == i2 && e.i2 == i1);
}
}
/// <summary>
/// Draws mesh wireframe. Use Draw.GetEdgePointsFromMesh() to get edgePoints, preferably only once
/// </summary>
public static void Wireframe(Transform t, Vector3[] edgePoints)
{
if (edgePoints == null) return;
if (edgePoints.Length < 2) return;
Prepare3D();
for (int i = 0; i < edgePoints.Length; i++)
GL.Vertex(t.TransformPoint(edgePoints[i]));
Postpare();
}
/// <summary>
/// Gets edge points from a mesh.
/// Call this once, and then use Wireframe() to draw the lines
/// </summary>
public static Vector3[] GetEdgePointsFromMesh(Mesh mesh)
{
Edge[] edges = GetEdges(mesh);
return EdgesToVertices(edges, mesh);
}
/// <summary>
/// Gets edge points from a mesh.
/// In case you want both the shaded model and wireframe to show, you can use normalPush to offset edges from the mesh so it doesn't intersect with it.
/// Call this once, and then use Wireframe() to draw the lines
/// </summary>
public static Vector3[] GetEdgePointsFromMesh(Mesh mesh, float normalPush)
{
Edge[] edges = GetEdges(mesh);
return EdgesToVertices(edges, mesh, normalPush);
}
static Edge[] GetEdges(Mesh mesh)
{
int[] tris = mesh.triangles;
List<Edge> edges = new List<Edge>();
for (int i = 0; i < tris.Length; i += 3)
{
Edge e1 = new Edge(tris[i], tris[i + 1]);
Edge e2 = new Edge(tris[i + 1], tris[i + 2]);
Edge e3 = new Edge(tris[i + 2], tris[i]);
// if line already exists, skip
foreach (var edge in edges)
if (edge.Match(e1)) goto NoE1;
edges.Add(e1);
NoE1:
foreach (var edge in edges)
if (edge.Match(e2)) goto NoE2;
edges.Add(e2);
NoE2:
foreach (var edge in edges)
if (edge.Match(e3)) goto NoE3;
edges.Add(e3);
NoE3:;
}
return edges.ToArray();
}
static Vector3[] EdgesToVertices(Edge[] edges, Mesh mesh, float normalPush = 0)
{
Vector3[] vertices = mesh.vertices;
Vector3[] edgesV3 = new Vector3[edges.Length * 2];
Vector3[] normals = null;
if (normalPush != 0)
normals = mesh.normals;
for (int i = 0; i < edges.Length; i++)
{
edgesV3[i * 2] = vertices[edges[i].i1];
edgesV3[i * 2 + 1] = vertices[edges[i].i2];
if (normalPush != 0)
{
edgesV3[i * 2] += normals[edges[i].i1] * normalPush;
edgesV3[i * 2 + 1] += normals[edges[i].i2] * normalPush;
}
}
return edgesV3;
}
#endregion Wireframe
#region Utils
/// <summary>
/// Converts a coordinate in pixels to screen 0-1 fraction point.
/// Example: 400, 300, on a 800x600 screen will output 0.5, 0.5 (middle of the screen)
/// </summary>
public static Vector2 PixelToScreen(Vector2 pos)
{
return new Vector2(pos.x / Screen.width, pos.y / Screen.height);
}
/// <summary>
/// Converts a coordinate in pixels to screen 0-1 fraction point.
/// Example: 400, 300, on a 800x600 screen will output 0.5, 0.5 (middle of the screen)
/// </summary>
public static Vector2 PixelToScreen(float x, float y)
{
return new Vector2(x / Screen.width, y / Screen.height);
}
static void Prepare()
{
GL.PushMatrix();
GL.LoadOrtho();
GL.Begin(GL.LINES);
GL.Color(color);
}
static void PreparePixel()
{
GL.PushMatrix();
GL.LoadPixelMatrix();
GL.Begin(GL.LINES);
GL.Color(color);
}
static void Prepare3D()
{
GL.PushMatrix();
GL.Begin(GL.LINES);
GL.Color(color);
}
static void Postpare()
{
GL.End();
GL.PopMatrix();
}
#endregion
}
}
|