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
|
// This file is automatically generated by a script based on the CommandBuilder API.
// This file adds additional overloads to the CommandBuilder API with convenience parameters like colors.
using Unity.Burst;
using UnityEngine;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Mathematics;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
namespace Pathfinding.Drawing {
public partial struct CommandBuilder {
/// <summary>\copydocref{Line(float3,float3)}</summary>
public void Line (float3 a, float3 b, Color color) {
Reserve<Color32, LineData>();
Add(Command.Line | Command.PushColorInline);
Add(ConvertColor(color));
Add(new LineData { a = a, b = b });
}
/// <summary>\copydocref{Ray(float3,float3)}</summary>
public void Ray (float3 origin, float3 direction, Color color) {
Line(origin, origin + direction, color);
}
/// <summary>\copydocref{Ray(Ray,float)}</summary>
public void Ray (Ray ray, float length, Color color) {
Line(ray.origin, ray.origin + ray.direction * length, color);
}
/// <summary>\copydocref{Arc(float3,float3,float3)}</summary>
/// <param name="color">Color of the object</param>
public void Arc (float3 center, float3 start, float3 end, Color color) {
PushColor(color);
var d1 = start - center;
var d2 = end - center;
var normal = math.cross(d2, d1);
if (math.any(normal != 0) && math.all(math.isfinite(normal))) {
var m = Matrix4x4.TRS(center, Quaternion.LookRotation(d1, normal), Vector3.one);
var angle = Vector3.SignedAngle(d1, d2, normal) * Mathf.Deg2Rad;
PushMatrix(m);
CircleXZInternal(float3.zero, math.length(d1), 90 * Mathf.Deg2Rad, 90 * Mathf.Deg2Rad - angle);
PopMatrix();
}
PopColor();
}
/// <summary>\copydocref{CircleXZ(float3,float,float,float)}</summary>
/// <param name="color">Color of the object</param>
[System.Obsolete("Use Draw.xz.Circle instead")]
public void CircleXZ (float3 center, float radius, float startAngle, float endAngle, Color color) {
CircleXZInternal(center, radius, startAngle, endAngle, color);
}
/// <summary>\copydocref{CircleXZ(float3,float,float,float)}</summary>
/// <param name="color">Color of the object</param>
[System.Obsolete("Use Draw.xz.Circle instead")]
public void CircleXZ (float3 center, float radius, Color color) {
CircleXZ(center, radius, 0f, 2 * Mathf.PI, color);
}
/// <summary>\copydocref{Circle(float3,float3,float)}</summary>
public void Circle (float3 center, float3 normal, float radius, Color color) {
Reserve<Color32, CircleData>();
Add(Command.Circle | Command.PushColorInline);
Add(ConvertColor(color));
Add(new CircleData { center = center, normal = normal, radius = radius });
}
/// <summary>\copydocref{WireCylinder(float3,float3,float)}</summary>
public void WireCylinder (float3 bottom, float3 top, float radius, Color color) {
WireCylinder(bottom, top - bottom, math.length(top - bottom), radius, color);
}
/// <summary>\copydocref{WireCylinder(float3,float3,float,float)}</summary>
/// <param name="color">Color of the object</param>
public void WireCylinder (float3 position, float3 up, float height, float radius, Color color) {
up = math.normalizesafe(up);
if (math.all(up == 0) || math.any(math.isnan(up)) || math.isnan(height) || math.isnan(radius)) return;
PushColor(color);
OrthonormalBasis(up, out var basis1, out var basis2);
PushMatrix(new float4x4(
new float4(basis1 * radius, 0),
new float4(up * height, 0),
new float4(basis2 * radius, 0),
new float4(position, 1)
));
CircleXZInternal(float3.zero, 1);
if (height > 0) {
CircleXZInternal(new float3(0, 1, 0), 1);
Line(new float3(1, 0, 0), new float3(1, 1, 0));
Line(new float3(-1, 0, 0), new float3(-1, 1, 0));
Line(new float3(0, 0, 1), new float3(0, 1, 1));
Line(new float3(0, 0, -1), new float3(0, 1, -1));
}
PopMatrix();
PopColor();
}
/// <summary>\copydocref{WireCapsule(float3,float3,float)}</summary>
/// <param name="color">Color of the object</param>
public void WireCapsule (float3 start, float3 end, float radius, Color color) {
PushColor(color);
var dir = end - start;
var length = math.length(dir);
if (length < 0.0001) {
// The endpoints are the same, we can't draw a capsule from this because we don't know its orientation.
// Draw a sphere as a fallback
WireSphere(start, radius);
} else {
var normalized_dir = dir / length;
WireCapsule(start - normalized_dir*radius, normalized_dir, length + 2*radius, radius);
}
PopColor();
}
/// <summary>\copydocref{WireCapsule(float3,float3,float,float)}</summary>
/// <param name="color">Color of the object</param>
public void WireCapsule (float3 position, float3 direction, float length, float radius, Color color) {
direction = math.normalizesafe(direction);
if (math.all(direction == 0) || math.any(math.isnan(direction)) || math.isnan(length) || math.isnan(radius)) return;
PushColor(color);
if (radius <= 0) {
Line(position, position + direction * length);
} else {
length = math.max(length, radius*2);
OrthonormalBasis(direction, out var basis1, out var basis2);
PushMatrix(new float4x4(
new float4(basis1, 0),
new float4(direction, 0),
new float4(basis2, 0),
new float4(position, 1)
));
CircleXZInternal(new float3(0, radius, 0), radius);
PushMatrix(XZtoXYPlaneMatrix);
CircleXZInternal(new float3(0, 0, radius), radius, Mathf.PI, 2 * Mathf.PI);
PopMatrix();
PushMatrix(XZtoYZPlaneMatrix);
CircleXZInternal(new float3(radius, 0, 0), radius, Mathf.PI*0.5f, Mathf.PI*1.5f);
PopMatrix();
if (length > 0) {
var upperY = length - radius;
var lowerY = radius;
CircleXZInternal(new float3(0, upperY, 0), radius);
PushMatrix(XZtoXYPlaneMatrix);
CircleXZInternal(new float3(0, 0, upperY), radius, 0, Mathf.PI);
PopMatrix();
PushMatrix(XZtoYZPlaneMatrix);
CircleXZInternal(new float3(upperY, 0, 0), radius, -Mathf.PI*0.5f, Mathf.PI*0.5f);
PopMatrix();
Line(new float3(radius, lowerY, 0), new float3(radius, upperY, 0));
Line(new float3(-radius, lowerY, 0), new float3(-radius, upperY, 0));
Line(new float3(0, lowerY, radius), new float3(0, upperY, radius));
Line(new float3(0, lowerY, -radius), new float3(0, upperY, -radius));
}
PopMatrix();
}
PopColor();
}
/// <summary>\copydocref{WireSphere(float3,float)}</summary>
public void WireSphere (float3 position, float radius, Color color) {
PushColor(color);
SphereOutline(position, radius);
Circle(position, new float3(1, 0, 0), radius);
Circle(position, new float3(0, 1, 0), radius);
Circle(position, new float3(0, 0, 1), radius);
PopColor();
}
/// <summary>\copydocref{Polyline(List<Vector3>,bool)}</summary>
/// <param name="color">Color of the object</param>
[BurstDiscard]
public void Polyline (List<Vector3> points, bool cycle, Color color) {
PushColor(color);
for (int i = 0; i < points.Count - 1; i++) {
Line(points[i], points[i+1]);
}
if (cycle && points.Count > 1) Line(points[points.Count - 1], points[0]);
PopColor();
}
/// <summary>\copydocref{Polyline(List<Vector3>,bool)}</summary>
/// <param name="color">Color of the object</param>
[BurstDiscard]
public void Polyline (List<Vector3> points, Color color) {
Polyline(points, false, color);
}
/// <summary>\copydocref{Polyline(Vector3[],bool)}</summary>
/// <param name="color">Color of the object</param>
[BurstDiscard]
public void Polyline (Vector3[] points, bool cycle, Color color) {
PushColor(color);
for (int i = 0; i < points.Length - 1; i++) {
Line(points[i], points[i+1]);
}
if (cycle && points.Length > 1) Line(points[points.Length - 1], points[0]);
PopColor();
}
/// <summary>\copydocref{Polyline(Vector3[],bool)}</summary>
/// <param name="color">Color of the object</param>
[BurstDiscard]
public void Polyline (Vector3[] points, Color color) {
Polyline(points, false, color);
}
/// <summary>\copydocref{Polyline(float3[],bool)}</summary>
/// <param name="color">Color of the object</param>
[BurstDiscard]
public void Polyline (float3[] points, bool cycle, Color color) {
PushColor(color);
for (int i = 0; i < points.Length - 1; i++) {
Line(points[i], points[i+1]);
}
if (cycle && points.Length > 1) Line(points[points.Length - 1], points[0]);
PopColor();
}
/// <summary>\copydocref{Polyline(float3[],bool)}</summary>
/// <param name="color">Color of the object</param>
[BurstDiscard]
public void Polyline (float3[] points, Color color) {
Polyline(points, false, color);
}
/// <summary>\copydocref{Polyline(NativeArray<float3>,bool)}</summary>
/// <param name="color">Color of the object</param>
public void Polyline (NativeArray<float3> points, bool cycle, Color color) {
PushColor(color);
for (int i = 0; i < points.Length - 1; i++) {
Line(points[i], points[i+1]);
}
if (cycle && points.Length > 1) Line(points[points.Length - 1], points[0]);
PopColor();
}
/// <summary>\copydocref{Polyline(NativeArray<float3>,bool)}</summary>
/// <param name="color">Color of the object</param>
public void Polyline (NativeArray<float3> points, Color color) {
Polyline(points, false, color);
}
/// <summary>\copydocref{WireBox(float3,float3)}</summary>
/// <param name="color">Color of the object</param>
public void WireBox (float3 center, float3 size, Color color) {
Reserve<Color32, BoxData>();
Add(Command.WireBox | Command.PushColorInline);
Add(ConvertColor(color));
Add(new BoxData { center = center, size = size });
}
/// <summary>\copydocref{WireBox(float3,quaternion,float3)}</summary>
/// <param name="color">Color of the object</param>
public void WireBox (float3 center, quaternion rotation, float3 size, Color color) {
PushColor(color);
PushMatrix(float4x4.TRS(center, rotation, size));
WireBox(float3.zero, new float3(1, 1, 1));
PopMatrix();
PopColor();
}
/// <summary>\copydocref{WireBox(Bounds)}</summary>
public void WireBox (Bounds bounds, Color color) {
WireBox(bounds.center, bounds.size, color);
}
/// <summary>\copydocref{WireMesh(Mesh)}</summary>
public void WireMesh (Mesh mesh, Color color) {
#if UNITY_2020_1_OR_NEWER
if (mesh == null) throw new System.ArgumentNullException();
PushColor(color);
// Use a burst compiled function to draw the lines
// This is significantly faster than pure C# (about 5x).
var meshDataArray = Mesh.AcquireReadOnlyMeshData(mesh);
var meshData = meshDataArray[0];
JobWireMesh.JobWireMeshFunctionPointer(ref meshData, ref this);
meshDataArray.Dispose();
#else
Debug.LogError("The WireMesh method is only suppored in Unity 2020.1 or later");
#endif
PopColor();
}
/// <summary>\copydocref{WireMesh(NativeArray<float3>,NativeArray<int>)}</summary>
public void WireMesh (NativeArray<float3> vertices, NativeArray<int> triangles, Color color) {
PushColor(color);
#if UNITY_2020_1_OR_NEWER
unsafe {
JobWireMesh.WireMesh((float3*)vertices.GetUnsafeReadOnlyPtr(), (int*)triangles.GetUnsafeReadOnlyPtr(), vertices.Length, triangles.Length, ref this);
}
#else
Debug.LogError("The WireMesh method is only suppored in Unity 2020.1 or later");
#endif
PopColor();
}
/// <summary>\copydocref{Cross(float3,float)}</summary>
public void Cross (float3 position, float size, Color color) {
PushColor(color);
size *= 0.5f;
Line(position - new float3(size, 0, 0), position + new float3(size, 0, 0));
Line(position - new float3(0, size, 0), position + new float3(0, size, 0));
Line(position - new float3(0, 0, size), position + new float3(0, 0, size));
PopColor();
}
/// <summary>\copydocref{Cross(float3,float)}</summary>
public void Cross (float3 position, Color color) {
Cross(position, 1, color);
}
/// <summary>\copydocref{CrossXZ(float3,float)}</summary>
[System.Obsolete("Use Draw.xz.Cross instead")]
public void CrossXZ (float3 position, float size, Color color) {
PushColor(color);
size *= 0.5f;
Line(position - new float3(size, 0, 0), position + new float3(size, 0, 0));
Line(position - new float3(0, 0, size), position + new float3(0, 0, size));
PopColor();
}
/// <summary>\copydocref{CrossXZ(float3,float)}</summary>
[System.Obsolete("Use Draw.xz.Cross instead")]
public void CrossXZ (float3 position, Color color) {
CrossXZ(position, 1, color);
}
/// <summary>\copydocref{CrossXY(float3,float)}</summary>
[System.Obsolete("Use Draw.xy.Cross instead")]
public void CrossXY (float3 position, float size, Color color) {
PushColor(color);
size *= 0.5f;
Line(position - new float3(size, 0, 0), position + new float3(size, 0, 0));
Line(position - new float3(0, size, 0), position + new float3(0, size, 0));
PopColor();
}
/// <summary>\copydocref{CrossXY(float3,float)}</summary>
[System.Obsolete("Use Draw.xy.Cross instead")]
public void CrossXY (float3 position, Color color) {
CrossXY(position, 1, color);
}
/// <summary>\copydocref{Bezier(float3,float3,float3,float3)}</summary>
/// <param name="color">Color of the object</param>
public void Bezier (float3 p0, float3 p1, float3 p2, float3 p3, Color color) {
PushColor(color);
float3 prev = p0;
for (int i = 1; i <= 20; i++) {
float t = i/20.0f;
float3 p = EvaluateCubicBezier(p0, p1, p2, p3, t);
Line(prev, p);
prev = p;
}
PopColor();
}
/// <summary>\copydocref{Arrow(float3,float3)}</summary>
/// <param name="color">Color of the object</param>
public void Arrow (float3 from, float3 to, Color color) {
ArrowRelativeSizeHead(from, to, DEFAULT_UP, 0.2f, color);
}
/// <summary>\copydocref{Arrow(float3,float3,float3,float)}</summary>
/// <param name="color">Color of the object</param>
public void Arrow (float3 from, float3 to, float3 up, float headSize, Color color) {
PushColor(color);
var length_sq = math.lengthsq(to - from);
if (length_sq > 0.000001f) {
ArrowRelativeSizeHead(from, to, up, headSize * math.rsqrt(length_sq));
}
PopColor();
}
/// <summary>\copydocref{ArrowRelativeSizeHead(float3,float3,float3,float)}</summary>
/// <param name="color">Color of the object</param>
public void ArrowRelativeSizeHead (float3 from, float3 to, float3 up, float headFraction, Color color) {
PushColor(color);
Line(from, to);
var dir = to - from;
var normal = math.cross(dir, up);
// Pick a different up direction if the direction happened to be colinear with that one.
if (math.all(normal == 0)) normal = math.cross(new float3(1, 0, 0), dir);
// Pick a different up direction if up=(1,0,0) and thus the above check would have generated a zero vector again
if (math.all(normal == 0)) normal = math.cross(new float3(0, 1, 0), dir);
normal = math.normalizesafe(normal) * math.length(dir);
Line(to, to - (dir + normal) * headFraction);
Line(to, to - (dir - normal) * headFraction);
PopColor();
}
/// <summary>\copydocref{ArrowheadArc(float3,float3,float,float)}</summary>
/// <param name="color">Color of the object</param>
public void ArrowheadArc (float3 origin, float3 direction, float offset, float width, Color color) {
if (!math.any(direction)) return;
if (offset < 0) throw new System.ArgumentOutOfRangeException(nameof(offset));
if (offset == 0) return;
PushColor(color);
var rot = Quaternion.LookRotation(direction, DEFAULT_UP);
PushMatrix(Matrix4x4.TRS(origin, rot, Vector3.one));
var a1 = math.PI * 0.5f - width * (0.5f * Mathf.Deg2Rad);
var a2 = math.PI * 0.5f + width * (0.5f * Mathf.Deg2Rad);
CircleXZInternal(float3.zero, offset, a1, a2);
var p1 = new float3(math.cos(a1), 0, math.sin(a1)) * offset;
var p2 = new float3(math.cos(a2), 0, math.sin(a2)) * offset;
const float sqrt2 = 1.4142f;
var p3 = new float3(0, 0, sqrt2 * offset);
Line(p1, p3);
Line(p3, p2);
PopMatrix();
PopColor();
}
/// <summary>\copydocref{ArrowheadArc(float3,float3,float,float)}</summary>
/// <param name="color">Color of the object</param>
public void ArrowheadArc (float3 origin, float3 direction, float offset, Color color) {
ArrowheadArc(origin, direction, offset, 60, color);
}
/// <summary>\copydocref{WireGrid(float3,quaternion,int2,float2)}</summary>
/// <param name="color">Color of the object</param>
public void WireGrid (float3 center, quaternion rotation, int2 cells, float2 totalSize, Color color) {
PushColor(color);
cells = math.max(cells, new int2(1, 1));
PushMatrix(float4x4.TRS(center, rotation, new Vector3(totalSize.x, 0, totalSize.y)));
int w = cells.x;
int h = cells.y;
for (int i = 0; i <= w; i++) Line(new float3(i/(float)w - 0.5f, 0, -0.5f), new float3(i/(float)w - 0.5f, 0, 0.5f));
for (int i = 0; i <= h; i++) Line(new float3(-0.5f, 0, i/(float)h - 0.5f), new float3(0.5f, 0, i/(float)h - 0.5f));
PopMatrix();
PopColor();
}
/// <summary>\copydocref{WireRectangle(float3,quaternion,float2)}</summary>
public void WireRectangle (float3 center, quaternion rotation, float2 size, Color color) {
WirePlane(center, rotation, size, color);
}
/// <summary>\copydocref{WireRectangle(Rect)}</summary>
[System.Obsolete("Use Draw.xy.WireRectangle instead")]
public void WireRectangle (Rect rect, Color color) {
xy.WireRectangle(rect, color);
}
/// <summary>\copydocref{WirePlane(float3,float3,float2)}</summary>
/// <param name="color">Color of the object</param>
public void WirePlane (float3 center, float3 normal, float2 size, Color color) {
PushColor(color);
if (math.any(normal)) {
WirePlane(center, Quaternion.LookRotation(calculateTangent(normal), normal), size);
}
PopColor();
}
/// <summary>\copydocref{WirePlane(float3,quaternion,float2)}</summary>
/// <param name="color">Color of the object</param>
public void WirePlane (float3 center, quaternion rotation, float2 size, Color color) {
Reserve<Color32, PlaneData>();
Add(Command.WirePlane | Command.PushColorInline);
Add(ConvertColor(color));
Add(new PlaneData { center = center, rotation = rotation, size = size });
}
/// <summary>\copydocref{SolidBox(float3,float3)}</summary>
/// <param name="color">Color of the object</param>
public void SolidBox (float3 center, float3 size, Color color) {
Reserve<Color32, BoxData>();
Add(Command.Box | Command.PushColorInline);
Add(ConvertColor(color));
Add(new BoxData { center = center, size = size });
}
/// <summary>\copydocref{SolidBox(Bounds)}</summary>
/// <param name="color">Color of the object</param>
public void SolidBox (Bounds bounds, Color color) {
SolidBox(bounds.center, bounds.size, color);
}
/// <summary>\copydocref{SolidBox(float3,quaternion,float3)}</summary>
/// <param name="color">Color of the object</param>
public void SolidBox (float3 center, quaternion rotation, float3 size, Color color) {
PushColor(color);
PushMatrix(float4x4.TRS(center, rotation, size));
SolidBox(float3.zero, Vector3.one);
PopMatrix();
PopColor();
}
}
}
|