-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathVector.cs
More file actions
1852 lines (1639 loc) · 84.9 KB
/
Vector.cs
File metadata and controls
1852 lines (1639 loc) · 84.9 KB
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Version 1.4
// ©2011 Starscene Software. All rights reserved. Redistribution without permission not allowed.
using UnityEngine;
namespace Engine.Graphics.Vector {
public class Vector {
private static Camera cam;
private static Transform camTransform;
private static Camera cam3D;
public static Vector3 oldPosition;
public static Vector3 oldRotation;
private static int _vectorLayer = 31;
public static int vectorLayer {
get {
return _vectorLayer;
}
set {
_vectorLayer = value;
if (_vectorLayer > 31) _vectorLayer = 31;
else if (_vectorLayer < 0) _vectorLayer = 0;
}
}
private static int _vectorLayer3D = 0;
public static int vectorLayer3D {
get {
return _vectorLayer3D;
}
set {
_vectorLayer3D = value;
if (_vectorLayer > 31) _vectorLayer3D = 31;
else if (_vectorLayer < 0) _vectorLayer3D = 0;
}
}
private static float zDist;
private static bool useOrthoCam;
private const float cutoff = .15f;
private static bool error = false;
private static bool lineManagerCreated = false;
private static LineManager _lineManager;
public static LineManager lineManager {
get {
// This prevents OnDestroy functions that reference VectorManager from creating LineManager again when editor play mode is stopped
// Checking _lineManager == null can randomly fail, since the order of objects being Destroyed is undefined
if (!lineManagerCreated) {
lineManagerCreated = true;
var lineManagerGO = new GameObject("LineManager");
_lineManager = lineManagerGO.AddComponent(typeof(LineManager)) as LineManager;
_lineManager.enabled = false;
GameObjectBehavior.DontDestroyOnLoad(_lineManager);
}
return _lineManager;
}
}
private static int widthIdxAdd;
public static void LogError(string errorString) {
LogUtil.LogError(errorString);
error = true;
}
public static Vector3 camTransformPosition {
get {
return camTransform.position;
}
}
public static Vector3 camTransformEulerAngles {
get {
return camTransform.eulerAngles;
}
}
public static bool camTransformExists {
get {
if (camTransform) return true;
return false;
}
}
public static Camera SetCamera() {
return SetCamera(CameraClearFlags.Depth, false);
}
public static Camera SetCamera(bool useOrtho) {
return SetCamera(CameraClearFlags.Depth, useOrtho);
}
public static Camera SetCamera(CameraClearFlags clearFlags) {
return SetCamera(clearFlags, false);
}
public static Camera SetCamera(CameraClearFlags clearFlags, bool useOrtho) {
if (Camera.main == null) {
LogError("Vector.SetCamera: no camera tagged \"Main Camera\" found");
return null;
}
return SetCamera(Camera.main, clearFlags, useOrtho);
}
public static Camera SetCamera(Camera thisCamera) {
return SetCamera(thisCamera, CameraClearFlags.Depth, false);
}
public static Camera SetCamera(Camera thisCamera, bool useOrtho) {
return SetCamera(thisCamera, CameraClearFlags.Depth, useOrtho);
}
public static Camera SetCamera(Camera thisCamera, CameraClearFlags clearFlags) {
return SetCamera(thisCamera, clearFlags, false);
}
public static Camera SetCamera(Camera thisCamera, CameraClearFlags clearFlags, bool useOrtho) {
if (!cam) {
cam = new GameObject("VectorCam", typeof(Camera)).GetComponent<Camera>();
GameObjectBehavior.DontDestroyOnLoad(cam);
}
cam.depth = thisCamera.depth + 1;
cam.clearFlags = clearFlags;
cam.orthographic = useOrtho;
useOrthoCam = useOrtho;
if (useOrtho) {
cam.orthographicSize = Screen.height / 2;
cam.farClipPlane = 101.1f;
cam.nearClipPlane = .9f;
}
else {
cam.fieldOfView = 90.0f;
cam.farClipPlane = Screen.height / 2 + .0101f;
cam.nearClipPlane = Screen.height / 2 - .0001f;
}
cam.transform.position = new Vector3(Screen.width / 2 - .5f, Screen.height / 2 - .5f, 0.0f);
cam.transform.eulerAngles = Vector3.zero;
cam.cullingMask = 1 << _vectorLayer;
cam.backgroundColor = thisCamera.backgroundColor;
thisCamera.cullingMask = thisCamera.cullingMask & (-1 ^ (1 << _vectorLayer));
camTransform = thisCamera.transform;
cam3D = thisCamera;
oldPosition = camTransform.position + Vector3.one;
oldRotation = camTransform.eulerAngles + Vector3.one;
return cam;
}
public static void SetCamera3D() {
if (Camera.main == null) {
LogError("Vector.SetCamera3D: no camera tagged \"Main Camera\" found. Please call SetCamera3D with a specific camera instead.");
return;
}
SetCamera3D(Camera.main);
}
public static void SetCamera3D(Camera thisCamera) {
camTransform = thisCamera.transform;
cam3D = thisCamera;
oldPosition = camTransform.position + Vector3.one;
oldRotation = camTransform.eulerAngles + Vector3.one;
}
public static void SetVectorCamDepth(int depth) {
cam.depth = depth;
}
public static int GetSegmentNumber(VectorLine line) {
if (line.continuousLine) {
return (line.points2 == null) ? line.points3.Length - 1 : line.points2.Length - 1;
}
else {
return (line.points2 == null) ? line.points3.Length / 2 : line.points2.Length / 2;
}
}
private static int GetPointsLength(VectorLine line) {
return line.points2 != null ? line.points2.Length : line.points3.Length;
}
private static string[] functionNames = { "Vector: SetColors: Length of color", "Vector: SetColorsSmooth: Length of color", "Vector: SetWidths: Length of line widths", "MakeCurveInLine", "MakeSplineInLine", "MakeEllipseInLine" };
private enum FunctionName { SetColors, SetColorsSmooth, SetWidths, MakeCurveInLine, MakeSplineInLine, MakeEllipseInLine }
private static bool WrongArrayLength(VectorLine line, int arrayLength, FunctionName functionName) {
int pointsLength = GetPointsLength(line);
if (line.continuousLine) {
if (arrayLength != pointsLength - 1) {
LogError(functionNames[(int)functionName] + " array for " + line.vectorObject.name + " must be length of points array minus one for a continuous line (one entry per line segment)");
return true;
}
}
else if (arrayLength != pointsLength / 2) {
LogError(functionNames[(int)functionName] + " array in " + line.vectorObject.name + " must be exactly half the length of points array for a discrete line (one entry per line segment)");
return true;
}
return false;
}
private static bool CheckArrayLength(VectorLine line, FunctionName functionName, int segments, int index) {
if (segments < 1) {
LogError("Vector: " + functionNames[(int)functionName] + " needs at least 1 segment");
return false;
}
int linePoints = (line.points2 == null) ? line.points3.Length : line.points2.Length;
if (line.isPoints) {
if (index + segments > linePoints) {
if (index == 0) {
LogError("Vector: " + functionNames[(int)functionName] + ": The number of segments cannot exceed the number of points in the array for " + line.vectorObject.name);
return false;
}
LogError("Vector: Calling " + functionNames[(int)functionName] + " with an index of " + index + " would exceed the length of the Vector array for " + line.vectorObject.name);
return false;
}
return true;
}
if (line.continuousLine) {
if (index + (segments + 1) > linePoints) {
if (index == 0) {
LogError("Vector: " + functionNames[(int)functionName] + ": The length of the array for continuous lines needs to be at least the number of segments plus one for " + line.vectorObject.name);
return false;
}
LogError("Vector: Calling " + functionNames[(int)functionName] + " with an index of " + index + " would exceed the length of the Vector array for " + line.vectorObject.name);
return false;
}
}
else {
if (index + segments * 2 > linePoints) {
if (index == 0) {
LogError("Vector: " + functionNames[(int)functionName] + ": The length of the array for discrete lines needs to be at least twice the number of segments for " + line.vectorObject.name);
return false;
}
LogError("Vector: Calling " + functionNames[(int)functionName] + " with an index of " + index + " would exceed the length of the Vector array for " + line.vectorObject.name);
return false;
}
}
return true;
}
public static void SetColor(VectorLine line, Color color) {
if (line.lineColors == null) {
line.SetupVertexColors();
}
int end = line.lineColors.Length;
for (int i = 0; i < end; i++) {
line.lineColors[i] = color;
}
line.mesh.colors = line.lineColors;
}
public static void SetColors(VectorLine line, Color[] lineColors) {
if (line.lineColors == null) {
line.SetupVertexColors();
}
if (!line.isPoints) {
if (WrongArrayLength(line, lineColors.Length, FunctionName.SetColors)) {
return;
}
}
else if (lineColors.Length != GetPointsLength(line)) {
LogError("Vector: SetColors: Length of lineColors array in " + line.vectorObject.name + " must be same length as points array");
return;
}
int start = 0;
int end = lineColors.Length;
SetStartAndEnd(line, ref start, ref end);
int idx = start * 4;
for (int i = start; i < end; i++) {
line.lineColors[idx] = lineColors[i];
line.lineColors[idx + 1] = lineColors[i];
line.lineColors[idx + 2] = lineColors[i];
line.lineColors[idx + 3] = lineColors[i];
idx += 4;
}
line.mesh.colors = line.lineColors;
}
public static void SetColorsSmooth(VectorLine line, Color[] lineColors) {
if (line.isPoints) {
LogError("Vector: SetColorsSmooth must be used with a line rather than points");
return;
}
if (line.lineColors == null || line.lineColors.Length == 0) {
LogError("Vector: SetColorsSmooth needs line colors to work");
return;
}
if (WrongArrayLength(line, lineColors.Length, FunctionName.SetColorsSmooth)) {
return;
}
int start = 0;
int end = lineColors.Length;
SetStartAndEnd(line, ref start, ref end);
int idx = start * 4;
line.lineColors[idx++] = lineColors[start];
line.lineColors[idx++] = lineColors[start];
line.lineColors[idx++] = lineColors[start];
line.lineColors[idx++] = lineColors[start];
for (int i = start + 1; i < end; i++) {
line.lineColors[idx] = lineColors[i - 1];
line.lineColors[idx + 1] = lineColors[i - 1];
line.lineColors[idx + 2] = lineColors[i];
line.lineColors[idx + 3] = lineColors[i];
idx += 4;
}
line.mesh.colors = line.lineColors;
}
private static void SetStartAndEnd(VectorLine line, ref int start, ref int end) {
start = (line.minDrawIndex == 0) ? 0 : (line.continuousLine) ? line.minDrawIndex : line.minDrawIndex / 2;
if (line.maxDrawIndex > 0) {
if (line.continuousLine) {
end = line.maxDrawIndex;
}
else {
end = line.maxDrawIndex / 2;
if (line.maxDrawIndex % 2 != 0) {
end++;
}
}
}
}
public static void SetWidths(VectorLine line, float[] lineWidths) {
if (lineWidths == null) {
LogError("Vector: SetWidths: line widths array must not be null");
return;
}
if (line.isPoints) {
if (lineWidths.Length != GetPointsLength(line)) {
LogError("Vector: SetWidths: line widths array must be the same length as the points array for \"" + line.vectorObject.name + "\"");
return;
}
}
else if (WrongArrayLength(line, lineWidths.Length, FunctionName.SetWidths)) {
return;
}
int end = lineWidths.Length;
line.lineWidths = new float[end];
for (int i = 0; i < end; i++) {
line.lineWidths[i] = lineWidths[i] * .5f;
}
}
private static Material lineMaterial;
private static float lineWidth;
private static int lineDepth;
private static float capLength;
private static Color lineColor;
private static LineType lineType;
private static Joins joins;
private static bool set = false;
private static Vector3 v1 = Vector3.zero;
private static Vector3 v2 = Vector3.zero;
public static void SetLineParameters(Color color, Material mat, float width, float cap, int depth, LineType thisLineType, Joins thisJoins) {
lineColor = color;
lineMaterial = mat;
lineWidth = width;
lineDepth = depth;
capLength = cap;
lineType = thisLineType;
joins = thisJoins;
set = true;
}
private static void PrintMakeLineError() {
LogError("Vector: Must call SetLineParameters before using MakeLine with these parameters");
}
public static VectorLine MakeLine(string name, Vector3[] points, Color[] colors) {
if (!set) {
PrintMakeLineError();
return null;
}
var line = new VectorLine(name, points, colors, lineMaterial, lineWidth, lineType, joins);
line.capLength = capLength;
line.depth = lineDepth;
return line;
}
public static VectorLine MakeLine(string name, Vector2[] points, Color[] colors) {
if (!set) {
PrintMakeLineError();
return null;
}
var line = new VectorLine(name, points, colors, lineMaterial, lineWidth, lineType, joins);
line.capLength = capLength;
line.depth = lineDepth;
return line;
}
public static VectorLine MakeLine(string name, Vector3[] points, Color color) {
if (!set) {
PrintMakeLineError();
return null;
}
var line = new VectorLine(name, points, color, lineMaterial, lineWidth, lineType, joins);
line.capLength = capLength;
line.depth = lineDepth;
return line;
}
public static VectorLine MakeLine(string name, Vector2[] points, Color color) {
if (!set) {
PrintMakeLineError();
return null;
}
var line = new VectorLine(name, points, color, lineMaterial, lineWidth, lineType, joins);
line.capLength = capLength;
line.depth = lineDepth;
return line;
}
public static VectorLine MakeLine(string name, Vector3[] points) {
if (!set) {
PrintMakeLineError();
return null;
}
var line = new VectorLine(name, points, lineColor, lineMaterial, lineWidth, lineType, joins);
line.capLength = capLength;
line.depth = lineDepth;
return line;
}
public static VectorLine MakeLine(string name, Vector2[] points) {
if (!set) {
PrintMakeLineError();
return null;
}
var line = new VectorLine(name, points, lineColor, lineMaterial, lineWidth, lineType, joins);
line.capLength = capLength;
line.depth = lineDepth;
return line;
}
public static VectorLine SetLine(Color color, params Vector2[] points) {
return SetLine(color, 0.0f, points);
}
public static VectorLine SetLine(Color color, float time, params Vector2[] points) {
if (points.Length < 2) {
LogError("Vector.SetLine needs at least two points");
return null;
}
VectorLine line = new VectorLine("Line", points, color, null, 1.0f, LineType.Continuous, Joins.None);
if (time > 0.0f) {
lineManager.DisableLine(line, time);
}
DrawLine(line);
return line;
}
public static VectorLine SetLine(Color color, params Vector3[] points) {
return SetLine(color, 0.0f, points);
}
public static VectorLine SetLine(Color color, float time, params Vector3[] points) {
if (points.Length < 2) {
LogError("Vector.SetLine needs at least two points");
return null;
}
VectorLine line = new VectorLine("SetLine", points, color, null, 1.0f, LineType.Continuous, Joins.None);
if (time > 0.0f) {
lineManager.DisableLine(line, time);
}
DrawLine(line);
return line;
}
public static VectorLine SetLine3D(Color color, params Vector3[] points) {
return SetLine3D(color, 0.0f, points);
}
public static VectorLine SetLine3D(Color color, float time, params Vector3[] points) {
if (points.Length < 2) {
LogError("Vector.SetLine3D needs at least two points");
return null;
}
VectorLine line = new VectorLine("SetLine3D", points, color, null, 1.0f, LineType.Continuous, Joins.None);
DrawLine3DAuto(line, time);
return line;
}
public static VectorLine SetRay(Color color, Vector3 origin, Vector3 direction) {
return SetRay(color, 0.0f, origin, direction);
}
public static VectorLine SetRay(Color color, float time, Vector3 origin, Vector3 direction) {
VectorLine line = new VectorLine("SetRay", new Vector3[] { origin, new Ray(origin, direction).GetPoint(direction.magnitude) }, color, null, 1.0f, LineType.Continuous, Joins.None);
if (time > 0.0f) {
lineManager.DisableLine(line, time);
}
DrawLine(line);
return line;
}
public static VectorLine SetRay3D(Color color, Vector3 origin, Vector3 direction) {
return SetRay3D(color, 0.0f, origin, direction);
}
public static VectorLine SetRay3D(Color color, float time, Vector3 origin, Vector3 direction) {
VectorLine line = new VectorLine("SetRay3D", new Vector3[] { origin, new Ray(origin, direction).GetPoint(direction.magnitude) }, color, null, 1.0f, LineType.Continuous, Joins.None);
DrawLine3DAuto(line, time);
return line;
}
public static void DrawLine(VectorLine line) {
DrawLine(line, null);
}
public static void DrawLine(VectorLine line, Transform thisTransform) {
if (error || !line.active) return;
if (line == null || line.vectorObject == null) {
LogError("Vector.DrawLine: the line must not be null");
return;
}
if (!cam) {
SetCamera();
if (!cam) { // If that didn't work (no camera tagged "Main Camera")
LogError("Vector.DrawLine: You must call SetCamera before calling DrawLine for " + line.vectorObject.name);
return;
}
}
if (line.smoothWidth && line.lineWidths.Length == 1 && GetPointsLength(line) > 2) {
LogError("Vector: DrawLine called with smooth line widths for " + line.vectorObject.name + ", but Vector.SetWidths has not been used");
return;
}
var useTransformMatrix = (thisTransform == null) ? false : true;
var thisMatrix = useTransformMatrix ? thisTransform.localToWorldMatrix : Matrix4x4.identity;
zDist = useOrthoCam ? 101 - line.depth : Screen.height / 2 + ((100.0f - line.depth) * .0001f);
int end = 0;
if (line.points2 != null) {
end = (line.maxDrawIndex == 0) ? line.points2.Length - 1 : line.maxDrawIndex;
Line2D(line, end, thisMatrix, useTransformMatrix);
}
else {
end = (line.maxDrawIndex == 0) ? line.points3.Length - 1 : line.maxDrawIndex;
if (line.continuousLine) {
Line3DContinuous(line, end, thisMatrix, useTransformMatrix);
}
else {
Line3DDiscrete(line, end, thisMatrix, useTransformMatrix);
}
}
line.mesh.vertices = line.lineVertices;
if (line.mesh.bounds.center.x != Screen.width / 2) {
SetLineMeshBounds(line);
}
}
private static void SetLineMeshBounds(VectorLine line) {
var bounds = new Bounds();
if (!useOrthoCam) {
bounds.center = new Vector3(Screen.width / 2, Screen.height / 2, Screen.height / 2);
bounds.extents = new Vector3(Screen.width * 100, Screen.height * 100, .1f);
}
else {
bounds.center = new Vector3(Screen.width / 2, Screen.height / 2, 50.5f);
bounds.extents = new Vector3(Screen.width * 100, Screen.height * 100, 51.0f);
}
line.mesh.bounds = bounds;
}
public static void DrawLine3D(VectorLine line) {
DrawLine3D(line, null);
}
public static void DrawLine3D(VectorLine line, Transform thisTransform) {
if (error || !line.active) return;
if (line == null || line.vectorObject == null) {
LogError("Vector.DrawLine3D: the line must not be null");
return;
}
if (!cam3D) {
SetCamera3D();
if (!cam3D) {
LogError("Vector.DrawLine3D: You must call SetCamera or SetCamera3D before calling DrawLine3D for " + line.vectorObject.name);
return;
}
}
if (line.points3 == null) {
LogError("Vector: DrawLine3D can only be used with a Vector3 array, which " + line.vectorObject.name + " doesn't have");
return;
}
if (line.smoothWidth && line.lineWidths.Length == 1 && line.points3.Length > 2) {
LogError("Vector: DrawLine3D called with smooth line widths for " + line.vectorObject.name + ", but Vector.SetWidths has not been used");
return;
}
if (line.layer == -1) {
line.vectorObject.layer = _vectorLayer3D;
line.layer = _vectorLayer3D;
}
var useTransformMatrix = (thisTransform == null) ? false : true;
var thisMatrix = useTransformMatrix ? thisTransform.localToWorldMatrix : Matrix4x4.identity;
int end = (line.maxDrawIndex == 0) ? line.points3.Length - 1 : line.maxDrawIndex;
int add = line.continuousLine ? 1 : 2;
int idx = line.minDrawIndex * 4;
int widthIdx = 0;
widthIdxAdd = 0;
if (line.lineWidths.Length > 1) {
widthIdx = line.minDrawIndex;
widthIdxAdd = 1;
}
if (!line.continuousLine) {
idx /= 2;
widthIdx /= 2;
}
var pos1 = Vector3.zero;
var pos2 = Vector3.zero;
for (int i = line.minDrawIndex; i < end; i += add) {
if (useTransformMatrix) {
pos1 = cam3D.WorldToScreenPoint(thisMatrix.MultiplyPoint3x4(line.points3[i]));
pos2 = cam3D.WorldToScreenPoint(thisMatrix.MultiplyPoint3x4(line.points3[i + 1]));
}
else {
pos1 = cam3D.WorldToScreenPoint(line.points3[i]);
pos2 = cam3D.WorldToScreenPoint(line.points3[i + 1]);
}
v1.x = pos2.y; v1.y = pos1.x;
v2.x = pos1.y; v2.y = pos2.x;
Vector3 thisLine = (v1 - v2).normalized;
Vector3 perpendicular = thisLine * line.lineWidths[widthIdx];
line.lineVertices[idx] = cam3D.ScreenToWorldPoint(pos1 - perpendicular);
line.lineVertices[idx + 1] = cam3D.ScreenToWorldPoint(pos1 + perpendicular);
if (line.smoothWidth && i < end - add) {
perpendicular = thisLine * line.lineWidths[widthIdx + 1];
}
line.lineVertices[idx + 2] = cam3D.ScreenToWorldPoint(pos2 - perpendicular);
line.lineVertices[idx + 3] = cam3D.ScreenToWorldPoint(pos2 + perpendicular);
idx += 4;
widthIdx += widthIdxAdd;
}
if (line.weldJoins) {
if (line.continuousLine) {
WeldJoins3D(line, line.minDrawIndex * 4 + 4, end * 4, line.points3[0] == line.points3[line.points3.Length - 1]
&& line.minDrawIndex == 0 && (line.maxDrawIndex == line.points3.Length - 1 || line.maxDrawIndex == 0));
}
else {
WeldJoinsDiscrete3D(line, line.minDrawIndex + 1, end, line.points3[0] == line.points3[line.points3.Length - 1]
&& line.minDrawIndex == 0 && (line.maxDrawIndex == line.points3.Length - 1 || line.maxDrawIndex == 0));
}
}
line.mesh.vertices = line.lineVertices;
line.mesh.RecalculateBounds();
}
public static void LineManagerCheckDistance() {
lineManager.StartCheckDistance();
}
public static void LineManagerDisable() {
lineManager.DisableIfUnused();
}
public static void LineManagerEnable() {
lineManager.EnableIfUsed();
}
public static void DrawLine3DAuto(VectorLine line) {
DrawLine3DAuto(line, 0.0f, null);
}
public static void DrawLine3DAuto(VectorLine line, float time) {
DrawLine3DAuto(line, time, null);
}
public static void DrawLine3DAuto(VectorLine line, Transform thisTransform) {
DrawLine3DAuto(line, 0.0f, thisTransform);
}
public static void DrawLine3DAuto(VectorLine line, float time, Transform thisTransform) {
if (time < 0.0f) time = 0.0f;
lineManager.AddLine(line, thisTransform, time);
DrawLine3D(line, thisTransform);
}
public static void StopDrawingLine3DAuto(VectorLine line) {
lineManager.RemoveLine(line);
}
private static void Line2D(VectorLine line, int end, Matrix4x4 thisMatrix, bool doTransform) {
Vector3 p1 = new Vector3(0.0f, 0.0f, 0.0f);
Vector3 p2 = new Vector3(0.0f, 0.0f, 0.0f);
int add = line.continuousLine ? 1 : 2;
int widthIdx = 0;
widthIdxAdd = 0;
if (line.lineWidths.Length > 1) {
widthIdx = line.minDrawIndex;
widthIdxAdd = 1;
}
int idx = line.minDrawIndex * 4;
if (!line.continuousLine) {
idx /= 2;
widthIdx /= 2;
}
if (line.capLength == 0.0f) {
for (int i = line.minDrawIndex; i < end; i += add) {
if (doTransform) {
p1 = thisMatrix.MultiplyPoint3x4(line.points2[i]);
p2 = thisMatrix.MultiplyPoint3x4(line.points2[i + 1]);
}
else {
p1 = line.points2[i];
p2 = line.points2[i + 1];
}
p1.z = zDist;
if (p1.x == p2.x && p1.y == p2.y) { Skip(line, ref idx, ref widthIdx, ref p1); continue; }
p2.z = zDist;
v1.x = p2.y; v1.y = p1.x;
v2.x = p1.y; v2.y = p2.x;
Vector3 perpendicular = v1 - v2;
float normalizedDistance = (1.0f / Mathf.Sqrt((perpendicular.x * perpendicular.x) + (perpendicular.y * perpendicular.y)));
perpendicular *= normalizedDistance * line.lineWidths[widthIdx];
line.lineVertices[idx] = p1 - perpendicular;
line.lineVertices[idx + 1] = p1 + perpendicular;
if (line.smoothWidth && i < end - add) {
perpendicular = v1 - v2;
perpendicular *= normalizedDistance * line.lineWidths[widthIdx + 1];
}
line.lineVertices[idx + 2] = p2 - perpendicular;
line.lineVertices[idx + 3] = p2 + perpendicular;
idx += 4;
widthIdx += widthIdxAdd;
}
if (line.weldJoins) {
if (line.continuousLine) {
WeldJoins(line, line.minDrawIndex * 4 + 4, end * 4, line.points2[0] == line.points2[line.points2.Length - 1]
&& line.minDrawIndex == 0 && (line.maxDrawIndex == line.points2.Length - 1 || line.maxDrawIndex == 0));
}
else {
WeldJoinsDiscrete(line, line.minDrawIndex + 1, end, line.points2[0] == line.points2[line.points2.Length - 1]
&& line.minDrawIndex == 0 && (line.maxDrawIndex == line.points2.Length - 1 || line.maxDrawIndex == 0));
}
}
}
else {
for (int i = line.minDrawIndex; i < end; i += add) {
if (doTransform) {
p1 = thisMatrix.MultiplyPoint3x4(line.points2[i]);
p2 = thisMatrix.MultiplyPoint3x4(line.points2[i + 1]);
}
else {
p1 = line.points2[i];
p2 = line.points2[i + 1];
}
p1.z = zDist;
if (p1.x == p2.x && p1.y == p2.y) { Skip(line, ref idx, ref widthIdx, ref p1); continue; }
p2.z = zDist;
Vector3 thisLine = p2 - p1;
thisLine *= (1.0f / Mathf.Sqrt((thisLine.x * thisLine.x) + (thisLine.y * thisLine.y)));
p1 -= thisLine * line.capLength;
p2 += thisLine * line.capLength;
v1.x = thisLine.y; v1.y = -thisLine.x;
thisLine = v1 * line.lineWidths[widthIdx];
line.lineVertices[idx] = p1 - thisLine;
line.lineVertices[idx + 1] = p1 + thisLine;
if (line.smoothWidth && i < end - add) {
thisLine = v1 * line.lineWidths[widthIdx + 1];
}
line.lineVertices[idx + 2] = p2 - thisLine;
line.lineVertices[idx + 3] = p2 + thisLine;
idx += 4;
widthIdx += widthIdxAdd;
}
}
}
private static void WeldJoins(VectorLine line, int start, int end, bool connectFirstAndLast) {
if (connectFirstAndLast) {
var lineLength = line.lineVertices.Length;
SetIntersectionPoint(line, lineLength - 4, lineLength - 2, 0, 2);
SetIntersectionPoint(line, lineLength - 3, lineLength - 1, 1, 3);
}
for (int i = start; i < end; i += 4) {
SetIntersectionPoint(line, i - 4, i - 2, i, i + 2);
SetIntersectionPoint(line, i - 3, i - 1, i + 1, i + 3);
}
}
private static void WeldJoinsDiscrete(VectorLine line, int start, int end, bool connectFirstAndLast) {
if (connectFirstAndLast) {
var lineLength = line.lineVertices.Length;
SetIntersectionPoint(line, lineLength - 4, lineLength - 2, 0, 2);
SetIntersectionPoint(line, lineLength - 3, lineLength - 1, 1, 3);
}
int idx = (start + 1) / 2 * 4;
if (line.points2 != null) {
for (int i = start; i < end; i += 2) {
if (line.points2[i] == line.points2[i + 1]) {
SetIntersectionPoint(line, idx - 4, idx - 2, idx, idx + 2);
SetIntersectionPoint(line, idx - 3, idx - 1, idx + 1, idx + 3);
}
idx += 4;
}
}
else {
for (int i = start; i < end; i += 2) {
if (line.points3[i] == line.points3[i + 1]) {
SetIntersectionPoint(line, idx - 4, idx - 2, idx, idx + 2);
SetIntersectionPoint(line, idx - 3, idx - 1, idx + 1, idx + 3);
}
idx += 4;
}
}
}
private static void SetIntersectionPoint(VectorLine line, int p1, int p2, int p3, int p4) {
var l1a = line.lineVertices[p1]; var l1b = line.lineVertices[p2];
var l2a = line.lineVertices[p3]; var l2b = line.lineVertices[p4];
float d = (l2b.y - l2a.y) * (l1b.x - l1a.x) - (l2b.x - l2a.x) * (l1b.y - l1a.y);
if (d == 0.0f) return; // Parallel lines
float n = ((l2b.x - l2a.x) * (l1a.y - l2a.y) - (l2b.y - l2a.y) * (l1a.x - l2a.x)) / d;
line.lineVertices[p2].x = l1a.x + (n * (l1b.x - l1a.x));
line.lineVertices[p2].y = l1a.y + (n * (l1b.y - l1a.y));
line.lineVertices[p3] = line.lineVertices[p2];
}
private static void WeldJoins3D(VectorLine line, int start, int end, bool connectFirstAndLast) {
if (connectFirstAndLast) {
var lineLength = line.lineVertices.Length;
SetIntersectionPoint3D(line, lineLength - 4, lineLength - 2, 0, 2);
SetIntersectionPoint3D(line, lineLength - 3, lineLength - 1, 1, 3);
}
for (int i = start; i < end; i += 4) {
SetIntersectionPoint3D(line, i - 4, i - 2, i, i + 2);
SetIntersectionPoint3D(line, i - 3, i - 1, i + 1, i + 3);
}
}
private static void WeldJoinsDiscrete3D(VectorLine line, int start, int end, bool connectFirstAndLast) {
if (connectFirstAndLast) {
var lineLength = line.lineVertices.Length;
SetIntersectionPoint3D(line, lineLength - 4, lineLength - 2, 0, 2);
SetIntersectionPoint3D(line, lineLength - 3, lineLength - 1, 1, 3);
}
int idx = (start + 1) / 2 * 4;
for (int i = start; i < end; i += 2) {
if (line.points3[i] == line.points3[i + 1]) {
SetIntersectionPoint3D(line, idx - 4, idx - 2, idx, idx + 2);
SetIntersectionPoint3D(line, idx - 3, idx - 1, idx + 1, idx + 3);
}
idx += 4;
}
}
private static void SetIntersectionPoint3D(VectorLine line, int p1, int p2, int p3, int p4) {
var l1a = cam3D.WorldToScreenPoint(line.lineVertices[p1]); var l1b = cam3D.WorldToScreenPoint(line.lineVertices[p2]);
var l2a = cam3D.WorldToScreenPoint(line.lineVertices[p3]); var l2b = cam3D.WorldToScreenPoint(line.lineVertices[p4]);
float d = (l2b.y - l2a.y) * (l1b.x - l1a.x) - (l2b.x - l2a.x) * (l1b.y - l1a.y);
if (d == 0.0f) return; // Parallel lines
float n = ((l2b.x - l2a.x) * (l1a.y - l2a.y) - (l2b.y - l2a.y) * (l1a.x - l2a.x)) / d;
line.lineVertices[p2].x = l1a.x + (n * (l1b.x - l1a.x));
line.lineVertices[p2].y = l1a.y + (n * (l1b.y - l1a.y));
line.lineVertices[p2] = cam3D.ScreenToWorldPoint(line.lineVertices[p2]);
line.lineVertices[p3] = line.lineVertices[p2];
}
private static void Line3DContinuous(VectorLine line, int end, Matrix4x4 thisMatrix, bool doTransform) {
var pos1 = Vector3.zero;
var pos2 = doTransform ? cam3D.WorldToScreenPoint(thisMatrix.MultiplyPoint3x4(line.points3[0])) :
cam3D.WorldToScreenPoint(line.points3[0]);
pos2.z = pos2.z < cutoff ? -zDist : zDist;
var perpendicular = Vector3.zero;
float normalizedDistance = 0.0f;
int widthIdx = 0;
widthIdxAdd = 0;
if (line.lineWidths.Length > 1) {
widthIdx = line.minDrawIndex;
widthIdxAdd = 1;
}
int idx = line.minDrawIndex * 4;
if (!line.continuousLine) idx /= 2;
for (int i = line.minDrawIndex; i < end; i++) {
pos1 = pos2;
pos2 = doTransform ? cam3D.WorldToScreenPoint(thisMatrix.MultiplyPoint3x4(line.points3[i + 1])) :
cam3D.WorldToScreenPoint(line.points3[i + 1]);
if (pos1.x == pos2.x && pos1.y == pos2.y) { Skip(line, ref idx, ref widthIdx, ref pos1); continue; }
pos2.z = pos2.z < cutoff ? -zDist : zDist;
v1.x = pos2.y; v1.y = pos1.x;
v2.x = pos1.y; v2.y = pos2.x;
perpendicular = v1 - v2;
normalizedDistance = 1.0f / Mathf.Sqrt((perpendicular.x * perpendicular.x) + (perpendicular.y * perpendicular.y));
perpendicular *= normalizedDistance * line.lineWidths[widthIdx];
line.lineVertices[idx] = pos1 - perpendicular;
line.lineVertices[idx + 1] = pos1 + perpendicular;
if (line.smoothWidth && i < end - 1) {
perpendicular = v1 - v2;
perpendicular *= normalizedDistance * line.lineWidths[widthIdx + 1];
}
line.lineVertices[idx + 2] = pos2 - perpendicular;
line.lineVertices[idx + 3] = pos2 + perpendicular;
idx += 4;
widthIdx += widthIdxAdd;
}
if (line.weldJoins) {
WeldJoins(line, line.minDrawIndex * 4 + 4, end * 4, line.points3[0] == line.points3[line.points3.Length - 1]
&& line.minDrawIndex == 0 && (line.maxDrawIndex == line.points3.Length - 1 || line.maxDrawIndex == 0));
}
}
private static void Line3DDiscrete(VectorLine line, int end, Matrix4x4 thisMatrix, bool doTransform) {
var pos1 = Vector3.zero;
var pos2 = Vector3.zero;
var perpendicular = Vector3.zero;
float normalizedDistance = 0.0f;
int widthIdx = 0;
widthIdxAdd = 0;
if (line.lineWidths.Length > 1) {
widthIdx = line.minDrawIndex;
widthIdxAdd = 1;
}
int idx = line.minDrawIndex * 4;
if (!line.continuousLine) {
idx /= 2;
widthIdx /= 2;
}
for (int i = line.minDrawIndex; i < end; i += 2) {
if (doTransform) {
pos1 = cam3D.WorldToScreenPoint(thisMatrix.MultiplyPoint3x4(line.points3[i]));
pos2 = cam3D.WorldToScreenPoint(thisMatrix.MultiplyPoint3x4(line.points3[i + 1]));
}
else {
pos1 = cam3D.WorldToScreenPoint(line.points3[i]);
pos2 = cam3D.WorldToScreenPoint(line.points3[i + 1]);
}
pos1.z = pos1.z < cutoff ? -zDist : zDist;
if (pos1.x == pos2.x && pos1.y == pos2.y) { Skip(line, ref idx, ref widthIdx, ref pos1); continue; }
pos2.z = pos2.z < cutoff ? -zDist : zDist;
v1.x = pos2.y; v1.y = pos1.x;
v2.x = pos1.y; v2.y = pos2.x;
perpendicular = v1 - v2;
normalizedDistance = 1.0f / Mathf.Sqrt((perpendicular.x * perpendicular.x) + (perpendicular.y * perpendicular.y));
perpendicular *= normalizedDistance * line.lineWidths[widthIdx];
line.lineVertices[idx] = pos1 - perpendicular;
line.lineVertices[idx + 1] = pos1 + perpendicular;
if (line.smoothWidth && i < end - 2) {
perpendicular = v1 - v2;
perpendicular *= normalizedDistance * line.lineWidths[widthIdx + 1];
}
line.lineVertices[idx + 2] = pos2 - perpendicular;
line.lineVertices[idx + 3] = pos2 + perpendicular;
idx += 4;
widthIdx += widthIdxAdd;
}
if (line.weldJoins) {
WeldJoinsDiscrete(line, line.minDrawIndex + 1, end, line.points3[0] == line.points3[line.points3.Length - 1]
&& line.minDrawIndex == 0 && (line.maxDrawIndex == line.points3.Length - 1 || line.maxDrawIndex == 0));
}
}
public static void SetTextureScale(VectorLine line, float textureScale) {
SetTextureScale(line, null, textureScale, 0.0f);
}
public static void SetTextureScale(VectorLine line, Transform thisTransform, float textureScale) {
SetTextureScale(line, thisTransform, textureScale, 0.0f);
}
public static void SetTextureScale(VectorLine line, float textureScale, float offset) {
SetTextureScale(line, null, textureScale, offset);
}
public static void SetTextureScale(VectorLine line, Transform thisTransform, float textureScale, float offset) {
int pointsLength = GetPointsLength(line);
int end = line.continuousLine ? pointsLength - 1 : pointsLength;
int add = line.continuousLine ? 1 : 2;
int idx = 0;
int widthIdx = 0;
widthIdxAdd = line.lineWidths.Length == 1 ? 0 : 1;
float thisScale = 1.0f / textureScale;
if (line.points2 != null) {