This repository has been archived by the owner on Jul 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
/
GSDRoad.cs
1476 lines (1329 loc) · 48.1 KB
/
GSDRoad.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
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
using UnityEngine;
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using GSD.Roads;
using GSD;
using UnityEditor;
#endif
[ExecuteInEditMode]
public class GSDRoad : MonoBehaviour{
#if UNITY_EDITOR
public GameObject MainMeshes;
public GameObject MeshRoad;
public GameObject MeshShoR;
public GameObject MeshShoL;
public GameObject MeshiLanes;
public GameObject MeshiLanes0;
public GameObject MeshiLanes1;
public GameObject MeshiLanes2;
public GameObject MeshiLanes3;
public GameObject MeshiMainPlates;
public GameObject MeshiMarkerPlates;
[System.NonSerialized]
public string EditorTitleString = "";
public GSDSplineC GSDSpline;
public int MostRecentNodeCount = -1;
// private bool bMostRecentCheck = false;
public GameObject GSDSplineObj;
public GSDRoadSystem GSDRS;
public GSDSplineC[] PiggyBacks = null;
public bool bEditorProgressBar = false;
public string UID; //Unique ID
[SerializeField]
public List<GSDTerrainHistoryMaker> TerrainHistory;
public string TerrainHistoryByteSize = "";
[System.NonSerialized]
public bool bUpdateSpline = false;
//Road editor options:
public float opt_LaneWidth = 5f; //Done.
public int opt_roadType = 0;
public bool opt_bShouldersEnabled = true; //Disabled for now. Comprimises integrity of roads.
public float opt_ShoulderWidth = 3f; //Done.
public int opt_Lanes = 2; //Done.
public float opt_RoadDefinition = 5f; //Done.
public bool opt_RoadCornerDefinition = false; //Disable for now. No point.
public bool opt_bRoadCuts = true;
public bool opt_bShoulderCuts = true;
public bool opt_bDynamicCuts = false;
public bool opt_bMaxGradeEnabled = true;
public float opt_MaxGrade = 0.08f;
public bool opt_UseDefaultMaterials = true;
public bool opt_AutoUpdateInEditor = true;
public float opt_TerrainSubtract_Match = 0.01f;
public bool opt_bGSDRoadRaise = false;
public float opt_MatchHeightsDistance = 50f;
public float opt_ClearDetailsDistance = 30f;
public float opt_ClearDetailsDistanceHeight = 5f;
public float opt_ClearTreesDistance = 30f;
public float opt_ClearTreesDistanceHeight = 50f;
public bool opt_HeightModEnabled = true;
public bool opt_DetailModEnabled = true;
public bool opt_TreeModEnabled = true;
public bool opt_SaveTerrainHistoryOnDisk = true;
public float opt_MagnitudeThreshold = 300f;
public bool opt_GizmosEnabled = true;
public bool opt_bMultithreading = true;
public bool opt_bSaveMeshes = false;
public bool opt_bUseMeshColliders = true;
public bool opt_bIsStatic = false;
public bool opt_bIsLightmapped = false;
public enum RoadMaterialDropdownEnum {
Asphalt,
Dirt,
Brick,
Cobblestone
};
public enum RoadType { Local, Freeway };
public RoadMaterialDropdownEnum opt_tRoadMaterialDropdown = RoadMaterialDropdownEnum.Asphalt;
public RoadMaterialDropdownEnum tRoadMaterialDropdownOLD = RoadMaterialDropdownEnum.Asphalt;
public Material RoadMaterial1;
public Material RoadMaterial2;
public Material RoadMaterial3;
public Material RoadMaterial4;
public Material RoadMaterialMarker1;
public Material RoadMaterialMarker2;
public Material RoadMaterialMarker3;
public Material RoadMaterialMarker4;
public Material ShoulderMaterial1;
public Material ShoulderMaterial2;
public Material ShoulderMaterial3;
public Material ShoulderMaterial4;
public Material ShoulderMaterialMarker1;
public Material ShoulderMaterialMarker2;
public Material ShoulderMaterialMarker3;
public Material ShoulderMaterialMarker4;
public PhysicMaterial RoadPhysicMaterial;
public PhysicMaterial ShoulderPhysicMaterial;
#region "Road Construction"
[System.NonSerialized]
public GSD.Threaded.TerrainCalcs TerrainCalcsJob;
[System.NonSerialized]
public GSD.Threaded.RoadCalcs1 RoadCalcsJob1;
[System.NonSerialized]
public GSD.Threaded.RoadCalcs2 RoadCalcsJob2;
[System.NonSerialized]
public RoadConstructorBufferMaker RCS;
public string tName = "";
public bool bProfiling = false;
public bool bSkipStore = true;
[System.NonSerialized]
public float EditorConstructionStartTime = 0f;
void CleanRunTime(){
//Make sure unused items are not using memory space in runtime:
TerrainHistory = null;
RCS = null;
}
public bool bEditorError = false;
public System.Exception tError = null;
void OnEnable(){
if(!Application.isEditor){ return; }
// if(Application.isEditor && !UnityEditor.EditorApplication.isPlaying){
Editor_bIsConstructing = false;
UnityEditor.EditorApplication.update += delegate { EditorUpdate(); };
#if UNITY_2018_1_OR_NEWER
UnityEditor.EditorApplication.hierarchyChanged += delegate { hWindowChanged(); };
#else
UnityEditor.EditorApplication.hierarchyWindowChanged += delegate { hWindowChanged(); };
#endif
// }
if (GSDSpline == null || GSDSpline.mNodes == null){
MostRecentNodeCount = 0;
}else{
MostRecentNodeCount = GSDSpline.GetNodeCount();
}
tRoadMaterialDropdownOLD = opt_tRoadMaterialDropdown;
CheckMats();
}
public void Awake() {
if(GSDSpline == null || GSDSpline.mNodes == null){
MostRecentNodeCount = 0;
}else{
MostRecentNodeCount = GSDSpline.GetNodeCount();
}
}
int EditorTimer=0;
int EditorTimerMax=0;
int EditorTimerSpline = 0;
const int EditorTimerSplineMax = 2;
[System.NonSerialized]
public int EditorProgress = 0;
const int GizmoNodeTimerMax = 2;
public bool EditorUpdateMe = false;
public bool bTriggerGC = false;
bool bTriggerGC_Happening;
float TriggerGC_End = 0f;
private void EditorUpdate(){
if(!Application.isEditor){
UnityEditor.EditorApplication.update -= delegate { EditorUpdate(); };
}
if(this == null){
UnityEditor.EditorApplication.update -= delegate { EditorUpdate(); };
Editor_bIsConstructing = false;
EditorUtility.ClearProgressBar();
return;
}
//Custom garbage collection demands for editor:
if(bTriggerGC){
bTriggerGC = false;
TriggerGC_End = Time.realtimeSinceStartup + 1f;
bTriggerGC_Happening = true;
}
if(bTriggerGC_Happening){
if(Time.realtimeSinceStartup > TriggerGC_End){
bTriggerGC_Happening = false;
GSDRootUtil.ForceCollection();
TriggerGC_End = 200000f;
}
}
if(Editor_bIsConstructing){ // && !Application.isPlaying && !UnityEditor.EditorApplication.isPlaying){
if(GSDRS != null){
if(GSDRS.opt_bMultithreading){
EditorTimer+=1;
if(EditorTimer > EditorTimerMax){
if((Time.realtimeSinceStartup - EditorConstructionStartTime) > 180f){
Editor_bIsConstructing = false;
EditorUtility.ClearProgressBar();
Debug.Log ("Update shouldn't take longer than 180 seconds. Aborting update.");
}
EditorTimer=0;
if(bEditorError){
Editor_bIsConstructing = false;
EditorUtility.ClearProgressBar();
bEditorError = false;
if(tError != null){
throw tError;
}
}
if(TerrainCalcsJob != null && TerrainCalcsJob.Update()){
ConstructRoad2();
}else if(RoadCalcsJob1 != null && RoadCalcsJob1.Update()){
ConstructRoad3();
}else if(RoadCalcsJob2 != null && RoadCalcsJob2.Update()){
ConstructRoad4();
}
}
}
}
}else{
if(EditorUpdateMe && !Editor_bIsConstructing){
EditorUpdateMe = false;
GSDSpline.Setup_Trigger();
}
}
if(Editor_bIsConstructing){
RoadUpdateProgressBar();
}else if(bEditorProgressBar){
RoadUpdateProgressBar();
}
if(!Application.isPlaying && bUpdateSpline && !UnityEditor.EditorApplication.isPlaying){
EditorTimerSpline += 1;
if(EditorTimerSpline > EditorTimerSplineMax){
EditorTimerSpline = 0;
bUpdateSpline = false;
GSDSpline.Setup_Trigger();
MostRecentNodeCount = GSDSpline.mNodes.Count;
}
}
if(bEditorCameraMoving && EditorCameraNextMove < EditorApplication.timeSinceStartup){
EditorCameraNextMove = (float)EditorApplication.timeSinceStartup+EditorCameraTimeUpdateInterval;
DoEditorCameraLoop();
}
}
[System.NonSerialized]
public bool bEditorCameraMoving = false;
[System.NonSerialized]
public float EditorCameraPos = 0f;
// float EditorCameraPos_Full = 0f;
const float EditorCameraTimeUpdateInterval = 0.015f;
float EditorCameraNextMove = 0f;
bool bEditorCameraSetup = false;
float EditorCameraStartPos = 0f;
float EditorCameraEndPos = 1f;
float EditorCameraIncrementDistance = 0f;
float EditorCameraIncrementDistance_Full = 0f;
public float EditorCameraMetersPerSecond = 60f;
public bool bEditorCameraRotate = false;
Vector3 EditorCameraV1 = default(Vector3);
Vector3 EditorCameraV2 = default(Vector3);
[System.NonSerialized]
public Vector3 EditorCameraOffset = new Vector3(0f,5f,0f);
[System.NonSerialized]
public Camera EditorPlayCamera = null;
Vector3 EditorCameraBadVec = default(Vector3);
public void DoEditorCameraLoop(){
if(!bEditorCameraSetup){
bEditorCameraSetup = true;
if(GSDSpline.bSpecialEndControlNode){ //If control node, start after the control node:
EditorCameraEndPos = GSDSpline.mNodes[GSDSpline.GetNodeCount()-2].tTime;
}
if(GSDSpline.bSpecialStartControlNode){ //If ends in control node, end construction before the control node:
EditorCameraStartPos = GSDSpline.mNodes[1].tTime;
}
// EditorCameraPos_Full = 0f;
ChangeEditorCameraMetersPerSec();
}
if(!Selection.Contains(this.transform.gameObject)){
QuitEditorCamera();
return;
}
// EditorCameraPos_Full+=EditorCameraIncrementDistance_Full;
// if(EditorCameraPos_Full > GSDSpline.distance){ EditorCameraPos = EditorCameraStartPos; bEditorCameraMoving = false; bEditorCameraSetup = false; EditorCameraPos_Full = 0f; return; }
// EditorCameraPos = GSDSpline.TranslateDistBasedToParam(EditorCameraPos_Full);
EditorCameraPos += EditorCameraIncrementDistance;
if(EditorCameraPos > EditorCameraEndPos){
QuitEditorCamera();
return;
}
if(EditorCameraPos < EditorCameraStartPos){
EditorCameraPos = EditorCameraStartPos;
}
GSDSpline.GetSplineValue_Both(EditorCameraPos,out EditorCameraV1, out EditorCameraV2);
if(EditorApplication.isPlaying){
if(EditorPlayCamera != null){
EditorPlayCamera.transform.position = EditorCameraV1;
if(bEditorCameraRotate){
EditorPlayCamera.transform.position += EditorCameraOffset;
if(EditorCameraV2 != EditorCameraBadVec){
EditorPlayCamera.transform.rotation = Quaternion.LookRotation(EditorCameraV2);
}
}
}
}else{
SceneView.lastActiveSceneView.pivot = EditorCameraV1;
if(bEditorCameraRotate){
SceneView.lastActiveSceneView.pivot += EditorCameraOffset;
if(EditorCameraV2 != EditorCameraBadVec){
SceneView.lastActiveSceneView.rotation = Quaternion.LookRotation(EditorCameraV2);
}
}
SceneView.lastActiveSceneView.Repaint();
}
}
public void EditorCameraSetSingle(){
if(EditorPlayCamera == null){
Camera[] EditorCams = (Camera[])GameObject.FindObjectsOfType(typeof(Camera));
if(EditorCams != null && EditorCams.Length == 1){
EditorPlayCamera = EditorCams[0];
}
}
}
public void QuitEditorCamera(){
EditorCameraPos = EditorCameraStartPos;
bEditorCameraMoving = false;
bEditorCameraSetup = false;
// EditorCameraPos_Full = 0f;
}
public void ChangeEditorCameraMetersPerSec(){
EditorCameraIncrementDistance_Full = (EditorCameraMetersPerSecond/60);
EditorCameraIncrementDistance = (EditorCameraIncrementDistance_Full/GSDSpline.distance);
}
private void hWindowChanged(){
if(!Application.isEditor){
#if UNITY_2018_1_OR_NEWER
UnityEditor.EditorApplication.hierarchyChanged -= delegate { hWindowChanged(); };
#else
UnityEditor.EditorApplication.hierarchyWindowChanged -= delegate { hWindowChanged(); };
#endif
}
if (Application.isPlaying || !Application.isEditor){ return; }
if(Application.isEditor && UnityEditor.EditorApplication.isPlaying){ return; }
if(Application.isEditor && UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode){ return; }
int tCount = 0;
if(GSDSpline != null && GSDSpline.mNodes != null){
tCount = GSDSpline.GetNodeCountNonNull();
}
if(tCount != MostRecentNodeCount){
bUpdateSpline = true;
}
}
void RoadUpdateProgressBar(){
if(Editor_bIsConstructing){
EditorUtility.DisplayCancelableProgressBar(
"GSD Road Update",
EditorTitleString,
EditorProgress / 100f);
}else if(bEditorProgressBar){
//bEditorProgressBar = false;
//EditorUtility.ClearProgressBar();
}
}
public void UpdateRoad(RoadUpdateTypeEnum tUpdateType = RoadUpdateTypeEnum.Full){
if (!GSDRS.opt_bAllowRoadUpdates) {
GSDSpline.Setup();
Editor_bIsConstructing = false;
return;
}
if(Editor_bIsConstructing){
return;
}
SetupUniqueIdentifier();
if(bProfiling){ UnityEngine.Profiling.Profiler.BeginSample("UpdateRoadPrelim"); }
opt_RoadDefinition = Mathf.Clamp(opt_RoadDefinition,1f,50f);
opt_LaneWidth = Mathf.Clamp(opt_LaneWidth,0.2f,500f);
EditorConstructionStartTime = Time.realtimeSinceStartup;
EditorTitleString = "Updating " + transform.name + "...";
System.GC.Collect();
if(opt_SaveTerrainHistoryOnDisk){
ConstructRoad_LoadTerrainHistory();
}
CheckMats();
EditorUtility.ClearProgressBar();
bProfiling = true;
if(GSDRS.opt_bMultithreading){ bProfiling = false; }
//Set all terrains to height 0:
GSD.Roads.GSDTerraforming.CheckAllTerrainsHeight0();
EditorProgress = 20;
bEditorProgressBar = true;
if(Editor_bIsConstructing){
if(TerrainCalcsJob != null){ TerrainCalcsJob.Abort(); TerrainCalcsJob = null; }
if(RoadCalcsJob1 != null){ RoadCalcsJob1.Abort(); RoadCalcsJob1 = null; }
if(RoadCalcsJob2 != null){ RoadCalcsJob2.Abort(); RoadCalcsJob2 = null; }
Editor_bIsConstructing = false;
}
// if(Application.isPlaying || !Application.isEditor){ return; }
// if(Application.isEditor && UnityEditor.EditorApplication.isPlaying){ return; }
// if(Application.isEditor && UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode){ return; }
//In here for intersection patching purposes:
int mCount = GSDSpline.GetNodeCount();
GSDSplineN tNode = null;
GSDSplineN tNode1 = null;
GSDSplineN tNode2 = null;
if(GSDSpline.CheckInvalidNodeCount()){
GSDSpline.Setup();
mCount = GSDSpline.GetNodeCount();
}
if(mCount > 1){
for(int i=0;i<mCount;i++){
// try{
tNode = GSDSpline.mNodes[i];
// }catch{
// Editor_bIsConstructing = false;
// EditorUpdateMe = true;
// return;
// }
//If node is intersection with an invalid GSDRI, mark it at non-intersection. Just-in-case.
if(tNode.bIsIntersection && tNode.GSDRI == null){
tNode.bIsIntersection = false;
tNode.id_intersection_othernode = -1;
tNode.Intersection_OtherNode = null;
}
//If node is intersection, re-setup:
if(tNode.bIsIntersection && tNode.GSDRI != null){
tNode1 = tNode.GSDRI.Node1;
tNode2 = tNode.GSDRI.Node2;
tNode.GSDRI.Setup(tNode1,tNode2);
tNode.GSDRI.DeleteRelevantChildren(tNode, tNode.GSDSpline.tRoad.transform.name);
//If primary node on intersection, do more re-setup:
if(tNode.GSDRI.Node1 == tNode){
tNode.GSDRI.Lanes = opt_Lanes;
tNode.GSDRI.name = tNode.GSDRI.transform.name;
}
//Setup construction objects:
tNode.GSDRI.Node1.iConstruction = new GSD.Roads.GSDIntersections.iConstructionMaker();
tNode.GSDRI.Node2.iConstruction = new GSD.Roads.GSDIntersections.iConstructionMaker();
}
//Store materials and physical materials for road and or shoulder cuts on each node, if necessary:
tNode.StoreCuts();
}
}
name = transform.name;
GSDSpline.RoadWidth = RoadWidth();
// if(bProfiling){ UnityEngine.Profiling.Profiler.BeginSample("SplineSetup"); }
GSDSpline.Setup();
// if(bProfiling){ UnityEngine.Profiling.Profiler.EndSample(); }
mCount = GSDSpline.GetNodeCount();
if(GSDSpline == null || GSDSpline.mNodes == null){
MostRecentNodeCount = 0;
}else{
MostRecentNodeCount = GSDSpline.GetNodeCount();
}
if(opt_UseDefaultMaterials){
SetDefaultMats();
}
if(opt_UseDefaultMaterials){
if(DetectInvalidDefaultMatsForUndo()){
SetAllCutsToCurrentMaterials();
}
}
//Hiding in hierarchy:
for(int i=0;i<mCount;i++){
tNode = GSDSpline.mNodes[i];
if(tNode != null){
if(tNode.bIsIntersection || tNode.bSpecialEndNode){
tNode.ToggleHideFlags(true);
}else{
tNode.ToggleHideFlags(false);
}
}
}
int cCount = transform.childCount;
GameObject tMainMeshes = null;
List<GameObject> tObjs = new List<GameObject>();
for(int i=0;i<cCount;i++){
if(transform.GetChild(i).transform.name.ToLower().Contains("mainmeshes")){
tMainMeshes = transform.GetChild(i).transform.gameObject;
tObjs.Add(tMainMeshes);
}
}
for(int i=(tObjs.Count-1);i>=0;i--){
tMainMeshes = tObjs[i];
Object.DestroyImmediate(tMainMeshes);
}
if(mCount < 2){
//Delete old objs and return:
if(MainMeshes != null){ Object.DestroyImmediate(MainMeshes); }
if(MeshRoad != null){ Object.DestroyImmediate(MeshRoad); }
if(MeshShoR != null){ Object.DestroyImmediate(MeshShoR); }
if(MeshShoL != null){ Object.DestroyImmediate(MeshShoL); }
if(MeshiLanes != null){ Object.DestroyImmediate(MeshiLanes); }
if(MeshiLanes0 != null){ Object.DestroyImmediate(MeshiLanes0); }
if(MeshiLanes1 != null){ Object.DestroyImmediate(MeshiLanes1); }
if(MeshiLanes2 != null){ Object.DestroyImmediate(MeshiLanes2); }
if(MeshiLanes3 != null){ Object.DestroyImmediate(MeshiLanes3); }
if(MeshiMainPlates != null){ Object.DestroyImmediate(MeshiMainPlates); }
if(MeshiMarkerPlates != null){ Object.DestroyImmediate(MeshiMarkerPlates); }
if(bProfiling){ UnityEngine.Profiling.Profiler.EndSample(); }
return;
}
GSDSpline.HeightHistory = new List<KeyValuePair<float, float>>();
if(GSDRS == null){ GSDRS = transform.parent.GetComponent<GSDRoadSystem>(); } //Compatibility update.
if(GSDRS.opt_bMultithreading){
Editor_bIsConstructing = true;
}else{
Editor_bIsConstructing = false;
}
Editor_bConstructionID = 0;
//Check if road takes place on only 1 terrain:
Terrain tTerrain = GSD.Roads.GSDRoadUtil.GetTerrain(GSDSpline.mNodes[0].pos);
bool bSameTerrain = true;
for(int i=1;i<mCount;i++){
if(tTerrain != GSD.Roads.GSDRoadUtil.GetTerrain(GSDSpline.mNodes[0].pos)){
bSameTerrain = false;
break;
}
}
RCS = new RoadConstructorBufferMaker(this, tUpdateType);
if(bSameTerrain){
RCS.tTerrain = tTerrain;
}else{
RCS.tTerrain = null;
}
tTerrain = null;
if(bProfiling){ UnityEngine.Profiling.Profiler.EndSample(); }
if(GSDRS.opt_bMultithreading){
if(RCS.bTerrainOn || TerrainHistory == null){
GSDTerraforming.ProcessRoad_Terrain_Hook1(GSDSpline,this);
}else{
ConstructRoad2();
}
}else{
UpdateRoad_NoMultiThreading();
}
}
#region "Terrain history"
public void ConstructRoad_StoreTerrainHistory(bool bDiskOnly = false){
if(!bDiskOnly){
GSDRoad tRoad = this;
GSDRoadUtil.ConstructRoad_StoreTerrainHistory(ref tRoad);
}
if(opt_SaveTerrainHistoryOnDisk && TerrainHistory != null && TerrainHistory.Count > 0){
if(bProfiling){ UnityEngine.Profiling.Profiler.BeginSample("TerrainHistory_Save"); }
GSDGeneralEditor.TerrainHistory_Save(TerrainHistory,this);
if(bProfiling){ UnityEngine.Profiling.Profiler.EndSample(); }
TerrainHistory.Clear();
TerrainHistory = null;
}else{
if(TerrainHistory != null && TerrainHistory.Count > 0){
int tSize = 0;
for(int i=0;i<TerrainHistory.Count;i++){
tSize += TerrainHistory[i].GetSize();
}
TerrainHistoryByteSize = (tSize*0.001f).ToString("n0") + " kb";
}else{
TerrainHistoryByteSize = "0 bytes";
}
}
}
public void ConstructRoad_ResetTerrainHistory(){
GSDRoad tRoad = this;
if(opt_SaveTerrainHistoryOnDisk && TerrainHistory != null){
GSDGeneralEditor.TerrainHistory_Delete(this);
}else{
GSDRoadUtil.ConstructRoad_ResetTerrainHistory(ref tRoad);
}
}
public void ConstructRoad_LoadTerrainHistory(bool bForce = false){
if(opt_SaveTerrainHistoryOnDisk || bForce){
if(TerrainHistory != null){
TerrainHistory.Clear();
TerrainHistory = null;
}
TerrainHistory = GSDGeneralEditor.TerrainHistory_Load(this);
}
if(bForce){
GSDGeneralEditor.TerrainHistory_Delete(this);
}
}
#endregion
#region "Construction process"
#region "No multithread"
private void UpdateRoad_NoMultiThreading(){
if(opt_HeightModEnabled || opt_DetailModEnabled || opt_TreeModEnabled){
if(bProfiling){ UnityEngine.Profiling.Profiler.BeginSample("RoadCon_Terrain"); }
if(RCS.bTerrainOn || TerrainHistory == null){
GSDTerraforming.ProcessRoad_Terrain_Hook1(GSDSpline,this,false);
GSDTerraforming.ProcessRoad_Terrain_Hook2(GSDSpline,ref EditorTTDList);
ConstructRoad_StoreTerrainHistory();//Store history.
int EditorTTDListCount = EditorTTDList.Count;
for(int i=0;i<EditorTTDListCount;i++){
EditorTTDList[i] = null;
}
EditorTTDList = null;
System.GC.Collect();
}
if(bProfiling){
UnityEngine.Profiling.Profiler.EndSample();
}
}
EditorProgress = 50;
GSDRoad tRoad = this;
if(bProfiling){
UnityEngine.Profiling.Profiler.BeginSample("RoadCon_RoadPrelim");
}
EditorProgress = 80;
GSD.Threaded.GSDRoadCreationT.RoadJob_Prelim(ref tRoad);
if(bProfiling){
UnityEngine.Profiling.Profiler.EndSample();
UnityEngine.Profiling.Profiler.BeginSample("RoadCon_Road1");
}
EditorProgress = 90;
GSD.Threaded.RoadCalcs1_static.RunMe(ref RCS);
if(bProfiling){ UnityEngine.Profiling.Profiler.EndSample(); }
if(bProfiling){ UnityEngine.Profiling.Profiler.BeginSample("MeshSetup1"); }
EditorProgress = 92;
RCS.MeshSetup1();
if(bProfiling){
UnityEngine.Profiling.Profiler.EndSample();
UnityEngine.Profiling.Profiler.BeginSample("RoadCon_Road2");
}
EditorProgress = 94;
GSD.Threaded.RoadCalcs2_static.RunMe(ref RCS);
if(bProfiling){ UnityEngine.Profiling.Profiler.EndSample(); }
if(bProfiling){ UnityEngine.Profiling.Profiler.BeginSample("MeshSetup2"); }
EditorProgress = 96;
RCS.MeshSetup2();
if(bProfiling){ UnityEngine.Profiling.Profiler.EndSample(); }
Construction_Cleanup();
}
#endregion
private void ConstructRoad2(){
EditorProgress = 40;
if(RCS.bTerrainOn){
//Store history:
GSDTerraforming.ProcessRoad_Terrain_Hook2(GSDSpline,ref EditorTTDList);
ConstructRoad_StoreTerrainHistory();
int EditorTTDListCount = EditorTTDList.Count;
for(int i=0;i<EditorTTDListCount;i++){
EditorTTDList[i] = null;
}
EditorTTDList = null;
System.GC.Collect();
} EditorProgress = 60;
if(TerrainCalcsJob != null){ TerrainCalcsJob.Abort(); TerrainCalcsJob = null; }
GSDRoad tRoad = this;
EditorProgress = 72;
RoadCalcsJob1 = new GSD.Threaded.RoadCalcs1();
RoadCalcsJob1.Setup(ref RCS, ref tRoad);
RoadCalcsJob1.Start();
}
private void ConstructRoad3(){
EditorProgress = 84;
RCS.MeshSetup1();
EditorProgress = 96;
if(RoadCalcsJob1 != null){ RoadCalcsJob1.Abort(); RoadCalcsJob1 = null; }
RoadCalcsJob2 = new GSD.Threaded.RoadCalcs2();
RoadCalcsJob2.Setup(ref RCS);
RoadCalcsJob2.Start();
EditorProgress = 98;
}
private void ConstructRoad4(){
RCS.MeshSetup2();
Construction_Cleanup();
}
#endregion
private void Construction_Cleanup(){
FixZ();
if(TerrainCalcsJob != null){ TerrainCalcsJob.Abort(); TerrainCalcsJob = null; }
if(RoadCalcsJob1 != null){ RoadCalcsJob1.Abort(); RoadCalcsJob1 = null; }
if(RoadCalcsJob2 != null){ RoadCalcsJob2.Abort(); RoadCalcsJob2 = null; }
Editor_bIsConstructing = false;
int mCount = GSDSpline.GetNodeCount();
GSDSplineN tNode;
for(int i=0;i<mCount;i++){
tNode = GSDSpline.mNodes[i];
if(tNode.bIsIntersection){
if(tNode.iConstruction != null){
tNode.iConstruction.Nullify();
tNode.iConstruction = null;
}
}
tNode.SetupSplinationLimits();
tNode.SetupEdgeObjects(false);
tNode.SetupSplinatedMeshes(false);
}
if(GSDSpline.HeightHistory != null){ GSDSpline.HeightHistory.Clear(); GSDSpline.HeightHistory = null; }
if(RCS != null){
RCS.Nullify();
RCS = null;
}
if(GSDRS.opt_bSaveMeshes){
UnityEditor.AssetDatabase.SaveAssets();
}
bEditorProgressBar = false;
EditorUtility.ClearProgressBar();
//Make sure terrain history out of memory if necessary (redudant but keep):
if(opt_SaveTerrainHistoryOnDisk && TerrainHistory != null){
TerrainHistory.Clear();
TerrainHistory = null;
}
//Collect:
bTriggerGC = true;
if (tRoadMaterialDropdownOLD != opt_tRoadMaterialDropdown) {
tRoadMaterialDropdownOLD = opt_tRoadMaterialDropdown;
SetAllCutsToCurrentMaterials();
}
if(PiggyBacks != null && PiggyBacks.Length > 0){
for(int i=0;i<PiggyBacks.Length;i++){
if(PiggyBacks[i] == null){
PiggyBacks = null;
break;
}
}
if(PiggyBacks != null){
GSDSplineC tPiggy = PiggyBacks[0];
GSDSplineC[] NewPiggys = null;
PiggyBacks[0] = null;
if(PiggyBacks.Length > 1){
NewPiggys = new GSDSplineC[PiggyBacks.Length-1];
for(int i=1;i<PiggyBacks.Length;i++){
NewPiggys[i-1] = PiggyBacks[i];
}
}
if(NewPiggys != null){
tPiggy.tRoad.PiggyBacks = NewPiggys;
}
NewPiggys = null;
tPiggy.Setup_Trigger();
}
}
}
public List<GSDTerraforming.TempTerrainData> EditorTTDList;
public void EditorTerrainCalcs(ref List<GSDTerraforming.TempTerrainData> tList){
EditorTTDList = tList;
}
#endregion
#region "Gizmos"
public bool Editor_bIsConstructing = false;
public int Editor_bConstructionID = 0;
public bool Editor_bSelected = false;
public bool Editor_MouseTerrainHit = false;
public Vector3 Editor_MousePos = new Vector3(0f,0f,0f);
public readonly Color Color_NodeDefaultColor = new Color(0f,1f,1f,0.75f);
public readonly Color Color_NodeConnColor = new Color(0f,1f,0f,0.75f);
public readonly Color Color_NodeInter = new Color(0f,1f,0f,0.75f);
void OnDrawGizmosSelected(){
if(Editor_MouseTerrainHit){
Gizmos.color = Color.red;
Gizmos.DrawCube(Editor_MousePos, new Vector3(10f,4f,10f));
}
}
#endregion
public float RoadWidth(){
return (opt_LaneWidth * (float)opt_Lanes);
}
public float EditorCameraTimer = 0f;
float EditorTestTimer = 0f;
bool bEditorTestTimer = true;
void Update(){
if(Application.isEditor && bEditorCameraMoving){
EditorCameraTimer+=Time.deltaTime;
if(EditorCameraTimer > EditorCameraTimeUpdateInterval){
EditorCameraTimer = 0f;
DoEditorCameraLoop();
}
}
if(bEditorTestTimer){
if(transform.name == "Road1"){
EditorTestTimer += Time.deltaTime;
if(EditorTestTimer > 2f){
// UpdateRoad(RoadUpdateTypeEnum.Full);
// akjsdfkajlgffdghfsdghsdf();
bEditorTestTimer = false;
}
}else{
bEditorTestTimer = false;
}
}
}
static void akjsdfkajlgffdghfsdghsdf(){
int LoopMax = 1000;
DoShort(LoopMax);
DoInt(LoopMax);
DoLong(LoopMax);
}
static void DoShort(int LoopMax){
ushort[] tSubject = new ushort[25000];
// int tInt = 0;
for(int i=0;i<LoopMax;i++){
for(int j=0;j<25000;j++){
tSubject[j] = (ushort)(j+1);
// int xTemp = (int)tSubject[j];
}
}
}
static void DoInt(int LoopMax){
int[] tSubject = new int[25000];
// int tInt = 0;
for(int i=0;i<LoopMax;i++){
for(int j=0;j<25000;j++){
tSubject[j] = j+1;
// int xTemp = tSubject[j];
}
}
}
static void DoLong(int LoopMax){
long[] tSubject = new long[25000];
// int tInt = 0;
for(int i=0;i<LoopMax;i++){
for(int j=0;j<25000;j++){
tSubject[j] = (long)(j+1);
// int xTemp = (int)tSubject[j];
}
}
}
#region "Default materials retrieval"
public bool DetectInvalidDefaultMatsForUndo(){
string tNameLower = "";
int tCounter = 0;
if(!MeshRoad){ return false; }
MeshRenderer[] MRs = MeshRoad.GetComponentsInChildren<MeshRenderer>();
Material tMat2 = GSD.Roads.GSDRoadUtilityEditor.GiveMaterial("Assets/RoadArchitect/Materials/Markers/GSDWhiteYellowDouble.mat");
Material tMat4 = GSD.Roads.GSDRoadUtilityEditor.GiveMaterial("Assets/RoadArchitect/Materials/Markers/GSDWhiteYellowDouble-4L.mat");
Material tMat6 = GSD.Roads.GSDRoadUtilityEditor.GiveMaterial("Assets/RoadArchitect/Materials/Markers/GSDWhiteYellowDouble-6L.mat");
foreach(MeshRenderer MR in MRs){
tNameLower = MR.transform.name.ToLower();
if(tNameLower.Contains("marker")){
if(opt_Lanes == 2){
if(MR.sharedMaterials[0] == tMat4){
tCounter+=1;
}else if(MR.sharedMaterials[0] == tMat6){
tCounter+=1;
}
}else if(opt_Lanes == 4){
if(MR.sharedMaterials[0] == tMat2){
tCounter+=1;
}else if(MR.sharedMaterials[0] == tMat6){
tCounter+=1;
}
}else if(opt_Lanes == 6){
if(MR.sharedMaterials[0] == tMat2){
tCounter+=1;
}else if(MR.sharedMaterials[0] == tMat4){
tCounter+=1;
}
}
}
if(tCounter > 1){
return true;
}
}
return false;
}
public void SetAllCutsToCurrentMaterials(){
string tNameLower = "";
if(!MeshRoad){ return; }
MeshRenderer[] MRs = MeshRoad.GetComponentsInChildren<MeshRenderer>();
Material[] tMats_World = GetMaterials_RoadWorld();
Material[] tMats_Marker = GetMaterials_RoadMarker();
foreach(MeshRenderer MR in MRs){
tNameLower = MR.transform.name.ToLower();
if(tNameLower.Contains("marker")){
if(tMats_Marker != null){
MR.sharedMaterials = tMats_Marker;
}
}else if(tNameLower.Contains("cut")){
if(tMats_World != null){
MR.sharedMaterials = tMats_World;
}
}
}
if(opt_bShouldersEnabled && MeshShoL != null){
MRs = MeshShoL.GetComponentsInChildren<MeshRenderer>();
tMats_World = GetMaterials_ShoulderWorld();
tMats_Marker = GetMaterials_ShoulderMarker();
foreach(MeshRenderer MR in MRs){
tNameLower = MR.transform.name.ToLower();
if(tNameLower.Contains("marker")){
if(tMats_Marker != null){
MR.sharedMaterials = tMats_Marker;
}
}else if(tNameLower.Contains("cut")){
if(tMats_World != null){
MR.sharedMaterials = tMats_World;
}
}
}
}
if(opt_bShouldersEnabled && MeshShoR != null){
MRs = MeshShoR.GetComponentsInChildren<MeshRenderer>();
foreach(MeshRenderer MR in MRs){
tNameLower = MR.transform.name.ToLower();
if(tNameLower.Contains("marker")){
if(tMats_Marker != null){
MR.sharedMaterials = tMats_Marker;
}
}else if(tNameLower.Contains("cut")){
if(tMats_World != null){
MR.sharedMaterials = tMats_World;
}
}
}
}
}