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
|
using System.Collections.Generic;
using Unity.Burst;
using Unity.Collections;
using Unity.Mathematics;
using UnityEngine;
using static Pathfinding.Drawing.CommandBuilder;
namespace Pathfinding.Drawing {
/// <summary>
/// 2D wrapper for a <see cref="CommandBuilder"/>.
///
/// <code>
/// var p1 = new Vector2(0, 1);
/// var p2 = new Vector2(5, 7);
///
/// // Draw it in the XY plane
/// Draw.xy.Line(p1, p2);
///
/// // Draw it in the XZ plane
/// Draw.xz.Line(p1, p2);
/// </code>
///
/// See: 2d-drawing (view in online documentation for working links)
/// See: <see cref="Draw.xy"/>
/// See: <see cref="Draw.xz"/>
/// </summary>
public partial struct CommandBuilder2D {
/// <summary>The wrapped command builder</summary>
private CommandBuilder draw;
/// <summary>True if drawing in the XY plane, false if drawing in the XZ plane</summary>
bool xy;
static readonly float3 XY_UP = new float3(0, 0, 1);
static readonly float3 XZ_UP = new float3(0, 1, 0);
static readonly quaternion XY_TO_XZ_ROTATION = quaternion.RotateX(-math.PI*0.5f);
static readonly quaternion XZ_TO_XZ_ROTATION = quaternion.identity;
static readonly float4x4 XZ_TO_XY_MATRIX = new float4x4(new float4(1, 0, 0, 0), new float4(0, 0, 1, 0), new float4(0, 1, 0, 0), new float4(0, 0, 0, 1));
public CommandBuilder2D(CommandBuilder draw, bool xy) {
this.draw = draw;
this.xy = xy;
}
/// <summary>
/// Draws a line between two points.
///
/// [Open online documentation to see images]
///
/// <code>
/// void Update () {
/// Draw.Line(Vector3.zero, Vector3.up);
/// }
/// </code>
/// </summary>
public void Line (float2 a, float2 b) {
draw.Reserve<LineData>();
// Add(Command.Line);
// Add(new LineData { a = a, b = b });
// The code below is equivalent to the commented out code above.
// But drawing lines is the most common operation so it needs to be really fast.
// Having this hardcoded improves line rendering performance by about 8%.
unsafe {
var buffer = draw.buffer;
var bufferSize = buffer->Length;
var newLen = bufferSize + 4 + 24;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
UnityEngine.Assertions.Assert.IsTrue(newLen <= buffer->Capacity);
#endif
var ptr = (byte*)buffer->Ptr + bufferSize;
*(Command*)ptr = Command.Line;
var lineData = (LineData*)(ptr + 4);
if (xy) {
lineData->a = new float3(a, 0);
lineData->b = new float3(b, 0);
} else {
lineData->a = new float3(a.x, 0, a.y);
lineData->b = new float3(b.x, 0, b.y);
}
buffer->Length = newLen;
}
}
/// <summary>
/// Draws a line between two points.
///
/// [Open online documentation to see images]
///
/// <code>
/// void Update () {
/// Draw.Line(Vector3.zero, Vector3.up);
/// }
/// </code>
/// </summary>
public void Line (float2 a, float2 b, Color color) {
draw.Reserve<Color32, LineData>();
// Add(Command.Line);
// Add(new LineData { a = a, b = b });
// The code below is equivalent to the commented out code above.
// But drawing lines is the most common operation so it needs to be really fast.
// Having this hardcoded improves line rendering performance by about 8%.
unsafe {
var buffer = draw.buffer;
var bufferSize = buffer->Length;
var newLen = bufferSize + 4 + 24 + 4;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
UnityEngine.Assertions.Assert.IsTrue(newLen <= buffer->Capacity);
#endif
var ptr = (byte*)buffer->Ptr + bufferSize;
*(Command*)ptr = Command.Line | Command.PushColorInline;
*(uint*)(ptr + 4) = CommandBuilder.ConvertColor(color);
var lineData = (LineData*)(ptr + 8);
if (xy) {
lineData->a = new float3(a, 0);
lineData->b = new float3(b, 0);
} else {
lineData->a = new float3(a.x, 0, a.y);
lineData->b = new float3(b.x, 0, b.y);
}
buffer->Length = newLen;
}
}
/// <summary>
/// Draws a line between two points.
///
/// [Open online documentation to see images]
///
/// <code>
/// void Update () {
/// Draw.Line(Vector3.zero, Vector3.up);
/// }
/// </code>
/// </summary>
public void Line (float3 a, float3 b) {
draw.Line(a, b);
}
/// <summary>
/// Draws a circle.
///
/// You can draw an arc by supplying the startAngle and endAngle parameters.
///
/// [Open online documentation to see images]
///
/// See: <see cref="Circle(float3,float3,float)"/>
/// See: <see cref="Arc(float3,float3,float3)"/>
/// </summary>
/// <param name="center">Center of the circle or arc.</param>
/// <param name="radius">Radius of the circle or arc.</param>
/// <param name="startAngle">Starting angle in radians. 0 corrsponds to the positive X axis.</param>
/// <param name="endAngle">End angle in radians.</param>
public void Circle (float2 center, float radius, float startAngle = 0f, float endAngle = 2 * math.PI) {
Circle(xy ? new float3(center, 0) : new float3(center.x, 0, center.y), radius, startAngle, endAngle);
}
/// <summary>
/// Draws a circle.
///
/// You can draw an arc by supplying the startAngle and endAngle parameters.
///
/// [Open online documentation to see images]
///
/// See: <see cref="Circle(float3,float3,float)"/>
/// See: <see cref="Arc(float3,float3,float3)"/>
/// </summary>
/// <param name="center">Center of the circle or arc.</param>
/// <param name="radius">Radius of the circle or arc.</param>
/// <param name="startAngle">Starting angle in radians. 0 corrsponds to the positive X axis.</param>
/// <param name="endAngle">End angle in radians.</param>
public void Circle (float3 center, float radius, float startAngle = 0f, float endAngle = 2 * math.PI) {
if (xy) {
draw.PushMatrix(XZ_TO_XY_MATRIX);
draw.CircleXZInternal(new float3(center.x, center.z, center.y), radius, startAngle, endAngle);
draw.PopMatrix();
} else {
draw.CircleXZInternal(center, radius, startAngle, endAngle);
}
}
/// <summary>\copydocref{SolidCircle(float3,float,float,float)}</summary>
public void SolidCircle (float2 center, float radius, float startAngle = 0f, float endAngle = 2 * math.PI) {
SolidCircle(xy ? new float3(center, 0) : new float3(center.x, 0, center.y), radius, startAngle, endAngle);
}
/// <summary>
/// Draws a disc.
///
/// You can draw an arc by supplying the startAngle and endAngle parameters.
///
/// [Open online documentation to see images]
///
/// See: <see cref="Draw.SolidCircle(float3,float3,float)"/>
/// See: <see cref="SolidArc(float3,float3,float3)"/>
/// </summary>
/// <param name="center">Center of the disc or solid arc.</param>
/// <param name="radius">Radius of the disc or solid arc.</param>
/// <param name="startAngle">Starting angle in radians. 0 corrsponds to the positive X axis.</param>
/// <param name="endAngle">End angle in radians.</param>
public void SolidCircle (float3 center, float radius, float startAngle = 0f, float endAngle = 2 * math.PI) {
if (xy) draw.PushMatrix(XZ_TO_XY_MATRIX);
draw.SolidCircleXZInternal(new float3(center.x, -center.z, center.y), radius, startAngle, endAngle);
if (xy) draw.PopMatrix();
}
/// <summary>
/// Draws a wire pill in 2D.
///
/// <code>
/// Draw.xy.WirePill(new float2(-0.5f, -0.5f), new float2(0.5f, 0.5f), 0.5f, color);
/// </code>
///
/// [Open online documentation to see images]
///
/// See: <see cref="WirePill(float2,float2,float,float)"/>
/// </summary>
/// <param name="a">Center of the first circle of the capsule.</param>
/// <param name="b">Center of the second circle of the capsule.</param>
/// <param name="radius">Radius of the capsule.</param>
public void WirePill (float2 a, float2 b, float radius) {
WirePill(a, b - a, math.length(b - a), radius);
}
/// <summary>
/// Draws a wire pill in 2D.
///
/// <code>
/// Draw.xy.WirePill(new float2(-0.5f, -0.5f), new float2(1, 1), 1, 0.5f, color);
/// </code>
///
/// [Open online documentation to see images]
///
/// See: <see cref="WirePill(float2,float2,float)"/>
/// </summary>
/// <param name="position">Center of the first circle of the capsule.</param>
/// <param name="direction">The main axis of the capsule. Does not have to be normalized. If zero, a circle will be drawn.</param>
/// <param name="length">Length of the main axis of the capsule, from circle center to circle center. If zero, a circle will be drawn.</param>
/// <param name="radius">Radius of the capsule.</param>
public void WirePill (float2 position, float2 direction, float length, float radius) {
direction = math.normalizesafe(direction);
if (radius <= 0) {
Line(position, position + direction * length);
} else if (length <= 0 || math.all(direction == 0)) {
Circle(position, radius);
} else {
float4x4 m;
if (xy) {
m = new float4x4(
new float4(direction, 0, 0),
new float4(math.cross(new float3(direction, 0), XY_UP), 0),
new float4(0, 0, 1, 0),
new float4(position, 0, 1)
);
} else {
m = new float4x4(
new float4(direction.x, 0, direction.y, 0),
new float4(0, 1, 0, 0),
new float4(math.cross(new float3(direction.x, 0, direction.y), XZ_UP), 0),
new float4(position.x, 0, position.y, 1)
);
}
draw.PushMatrix(m);
Circle(new float2(0, 0), radius, 0.5f * math.PI, 1.5f * math.PI);
Line(new float2(0, -radius), new float2(length, -radius));
Circle(new float2(length, 0), radius, -0.5f * math.PI, 0.5f * math.PI);
Line(new float2(0, radius), new float2(length, radius));
draw.PopMatrix();
}
}
/// <summary>\copydocref{CommandBuilder.Polyline(List<Vector3>,bool)}</summary>
[BurstDiscard]
public void Polyline (List<Vector2> points, bool cycle = false) {
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]);
}
/// <summary>\copydocref{CommandBuilder.Polyline(Vector3[],bool)}</summary>
[BurstDiscard]
public void Polyline (Vector2[] points, bool cycle = false) {
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]);
}
/// <summary>\copydocref{CommandBuilder.Polyline(float3[],bool)}</summary>
[BurstDiscard]
public void Polyline (float2[] points, bool cycle = false) {
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]);
}
/// <summary>\copydocref{CommandBuilder.Polyline(NativeArray<float3>,bool)}</summary>
public void Polyline (NativeArray<float2> points, bool cycle = false) {
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]);
}
/// <summary>
/// Draws a 2D cross.
///
/// <code>
/// Draw.xz.Cross(float3.zero, color);
/// </code>
/// [Open online documentation to see images]
///
/// See: <see cref="Draw.Cross"/>
/// </summary>
public void Cross (float2 position, float size = 1) {
size *= 0.5f;
Line(position - new float2(size, 0), position + new float2(size, 0));
Line(position - new float2(0, size), position + new float2(0, size));
}
/// <summary>
/// Draws a rectangle outline.
/// The rectangle will be oriented along the rotation's X and Z axes.
///
/// <code>
/// Draw.xz.WireRectangle(new Vector3(0f, 0, 0), new Vector2(1, 1), Color.black);
/// </code>
/// [Open online documentation to see images]
///
/// This is identical to <see cref="Draw.WirePlane(float3,quaternion,float2)"/>, but this name is added for consistency.
///
/// See: <see cref="Draw.WirePolygon"/>
/// </summary>
public void WireRectangle (float3 center, float2 size) {
draw.WirePlane(center, xy ? XY_TO_XZ_ROTATION : XZ_TO_XZ_ROTATION, size);
}
/// <summary>
/// Draws a rectangle outline.
/// This is particularly useful when combined with <see cref="InScreenSpace"/>.
///
/// <code>
/// using (Draw.InScreenSpace(Camera.main)) {
/// Draw.xy.WireRectangle(new Rect(10, 10, 100, 100), Color.black);
/// }
/// </code>
/// [Open online documentation to see images]
///
/// See: <see cref="Draw.WireRectangle(float3,quaternion,float2)"/>
/// See: <see cref="Draw.WirePolygon"/>
/// </summary>
public void WireRectangle (Rect rect) {
float2 min = rect.min;
float2 max = rect.max;
Line(new float2(min.x, min.y), new float2(max.x, min.y));
Line(new float2(max.x, min.y), new float2(max.x, max.y));
Line(new float2(max.x, max.y), new float2(min.x, max.y));
Line(new float2(min.x, max.y), new float2(min.x, min.y));
}
/// <summary>
/// Draws a solid rectangle.
/// This is particularly useful when combined with <see cref="InScreenSpace"/>.
///
/// Behind the scenes this is implemented using <see cref="Draw.SolidPlane"/>.
///
/// <code>
/// using (Draw.InScreenSpace(Camera.main)) {
/// Draw.xy.SolidRectangle(new Rect(10, 10, 100, 100), Color.black);
/// }
/// </code>
/// [Open online documentation to see images]
///
/// See: <see cref="WireRectangle"/>
/// See: <see cref="Draw.WireRectangle(float3,quaternion,float2)"/>
/// See: <see cref="Draw.SolidBox"/>
/// </summary>
public void SolidRectangle (Rect rect) {
draw.SolidPlane(new float3(rect.center.x, rect.center.y, 0.0f), xy ? XY_TO_XZ_ROTATION : XZ_TO_XZ_ROTATION, new float2(rect.width, rect.height));
}
/// <summary>
/// Draws a grid of lines.
///
/// <code>
/// Draw.xz.WireGrid(Vector3.zero, new int2(3, 3), new float2(1, 1), color);
/// </code>
/// [Open online documentation to see images]
///
/// See: <see cref="Draw.WireGrid"/>
/// </summary>
/// <param name="center">Center of the grid</param>
/// <param name="cells">Number of cells of the grid. Should be greater than 0.</param>
/// <param name="totalSize">Total size of the grid along the X and Z axes.</param>
public void WireGrid (float2 center, int2 cells, float2 totalSize) {
draw.WireGrid(xy ? new float3(center, 0) : new float3(center.x, 0, center.y), xy ? XY_TO_XZ_ROTATION : XZ_TO_XZ_ROTATION, cells, totalSize);
}
/// <summary>
/// Draws a grid of lines.
///
/// <code>
/// Draw.xz.WireGrid(Vector3.zero, new int2(3, 3), new float2(1, 1), color);
/// </code>
/// [Open online documentation to see images]
///
/// See: <see cref="Draw.WireGrid"/>
/// </summary>
/// <param name="center">Center of the grid</param>
/// <param name="cells">Number of cells of the grid. Should be greater than 0.</param>
/// <param name="totalSize">Total size of the grid along the X and Z axes.</param>
public void WireGrid (float3 center, int2 cells, float2 totalSize) {
draw.WireGrid(center, xy ? XY_TO_XZ_ROTATION : XZ_TO_XZ_ROTATION, cells, totalSize);
}
}
}
|