forked from Chippington/Quixel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nodes.cs
1171 lines (1023 loc) · 41.4 KB
/
Nodes.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;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System;
using System.IO;
namespace Quixel
{
/// <summary>
/// Controls the 27 top-level nodes. Handles node searching.
/// </summary>
internal static class NodeManager
{
#region Fields
public static string worldName;
public static Vector3I curBottomNode = new Vector3I(-111, 0, 0);
public static Vector3I curTopNode = new Vector3I(0, 0, 0);
public static Vector3I[] viewChunkPos;
//public static Node[,,] topNodes = new Node[3,3,3];
public static Node[, ,] topNodes = new Node[3, 3, 3];
/// <summary> Mask used for positioning subnodes</summary>
public static Vector3[] mask = new Vector3[8] {
new Vector3(0, 0, 0),
new Vector3(1, 0, 0),
new Vector3(0, 1, 0),
new Vector3(0, 0, 1),
new Vector3(1, 1, 0),
new Vector3(1, 0, 1),
new Vector3(0, 1, 1),
new Vector3(1, 1, 1)
};
/// <summary>Refers to the tri size of each vertex</summary>
public static int[] LODSize = new int[11] {
1,
2,
4,
8, 16, 32, 64, 128, 256, 512, 1024
};
//DEPRECATED
/// <summary>Radius for each LOD value</summary>
public static int[] LODRange = new int[11] {
0,
7,
12,
26, 50, 100, 210, 400, 700, 1200, 2400
};
public static int[] nodeCount = new int[11];
/// <summary>Density array size</summary>
public static int nodeSize = 16;
/// <summary>The maximum allowed LOD. The top level nodes will be this LOD.</summary>
public static int maxLOD = 10;
#endregion
/// <summary>
/// Initializes the node manager.
/// </summary>
public static void init(string worldName)
{
NodeManager.worldName = worldName;
float nSize = LODSize[maxLOD] * nodeSize;
for (int x = -1; x < 2; x++)
{
for (int y = -1; y < 2; y++)
{
for (int z = -1; z < 2; z++)
{
topNodes[x + 1, y + 1, z + 1] = new Node(null,
new Vector3(x * nSize,
y * nSize,
z * nSize),
0, maxLOD, Node.RenderType.FRONT);
}
}
}
viewChunkPos = new Vector3I[maxLOD + 1];
for (int i = 0; i <= maxLOD; i++)
viewChunkPos[i] = new Vector3I();
}
/// <summary>
/// Sets the view position, and checks if chunks need to be updated
/// </summary>
/// <param name="pos"></param>
public static void setViewPosition(Vector3 pos)
{
for (int i = 0; i <= maxLOD; i++)
{
float nWidth = LODSize[i] * nodeSize;
viewChunkPos[i].x = (int)(pos.x / nWidth);
viewChunkPos[i].y = (int)(pos.y / nWidth);
viewChunkPos[i].z = (int)(pos.z / nWidth);
}
float sWidth = LODSize[0] * nodeSize * 0.5f;
Vector3I newPos = new Vector3I((int)(pos.x / sWidth), (int)(pos.y / sWidth), (int)(pos.z / sWidth));
if (!curTopNode.Equals(getTopNode(pos)))
{
float nodeWidth = LODSize[maxLOD] * nodeSize;
Vector3I diff = getTopNode(pos).Subtract(curTopNode);
curTopNode = getTopNode(pos);
while (diff.x > 0)
{
for (int y = 0; y < 3; y++)
{
for (int z = 0; z < 3; z++)
{
topNodes[0, y, z].dispose();
topNodes[0, y, z] = topNodes[1, y, z];
topNodes[1, y, z] = topNodes[2, y, z];
topNodes[2, y, z] = new Node(null,
new Vector3((curTopNode.x * nodeWidth) + nodeWidth,
(curTopNode.y * nodeWidth) + ((y - 1) * nodeWidth),
(curTopNode.z * nodeWidth) + ((z - 1) * nodeWidth)),
0, maxLOD, Node.RenderType.FRONT);
}
}
diff.x--;
}
while (diff.x < 0)
{
for (int y = 0; y < 3; y++)
{
for (int z = 0; z < 3; z++)
{
topNodes[2, y, z].dispose();
topNodes[2, y, z] = topNodes[1, y, z];
topNodes[1, y, z] = topNodes[0, y, z];
topNodes[0, y, z] = new Node(null,
new Vector3((curTopNode.x * nodeWidth) - nodeWidth,
(curTopNode.y * nodeWidth) + ((y - 1) * nodeWidth),
(curTopNode.z * nodeWidth) + ((z - 1) * nodeWidth)),
0, maxLOD, Node.RenderType.FRONT);
}
}
diff.x++;
}
while (diff.y > 0)
{
for (int x = 0; x < 3; x++)
{
for (int z = 0; z < 3; z++)
{
topNodes[x, 0, z].dispose();
topNodes[x, 0, z] = topNodes[x, 1, z];
topNodes[x, 1, z] = topNodes[x, 2, z];
topNodes[x, 2, z] = new Node(null,
new Vector3((curTopNode.x * nodeWidth) + ((x - 1) * nodeWidth),
(curTopNode.y * nodeWidth) + nodeWidth,
(curTopNode.z * nodeWidth) + ((z - 1) * nodeWidth)),
0, maxLOD, Node.RenderType.FRONT);
}
}
diff.y--;
}
while (diff.y < 0)
{
for (int x = 0; x < 3; x++)
{
for (int z = 0; z < 3; z++)
{
topNodes[x, 2, z].dispose();
topNodes[x, 2, z] = topNodes[x, 1, z];
topNodes[x, 1, z] = topNodes[x, 0, z];
topNodes[x, 0, z] = new Node(null,
new Vector3((curTopNode.x * nodeWidth) + ((x - 1) * nodeWidth),
(curTopNode.y * nodeWidth) - nodeWidth,
(curTopNode.z * nodeWidth) + ((z - 1) * nodeWidth)),
0, maxLOD, Node.RenderType.FRONT);
}
}
diff.y++;
}
while (diff.z > 0)
{
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
topNodes[x, y, 0].dispose();
topNodes[x, y, 0] = topNodes[x, y, 1];
topNodes[x, y, 1] = topNodes[x, y, 2];
topNodes[x, y, 2] = new Node(null,
new Vector3((curTopNode.x * nodeWidth) + ((x - 1) * nodeWidth),
(curTopNode.y * nodeWidth) + ((y - 1) * nodeWidth),
(curTopNode.z * nodeWidth) + nodeWidth),
0, maxLOD, Node.RenderType.FRONT);
}
}
diff.z--;
}
while (diff.z < 0)
{
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
topNodes[x, y, 2].dispose();
topNodes[x, y, 2] = topNodes[x, y, 1];
topNodes[x, y, 1] = topNodes[x, y, 0];
topNodes[x, y, 0] = new Node(null,
new Vector3((curTopNode.x * nodeWidth) + ((x - 1) * nodeWidth),
(curTopNode.y * nodeWidth) + ((y - 1) * nodeWidth),
(curTopNode.z * nodeWidth) - nodeWidth),
0, maxLOD, Node.RenderType.FRONT);
}
}
diff.z++;
}
}
if (curBottomNode.x != newPos.x || curBottomNode.y != newPos.y || curBottomNode.z != newPos.z)
{
Vector3 setPos = new Vector3(newPos.x * sWidth + (sWidth / 1f), newPos.y * sWidth + (sWidth / 1f), newPos.z * sWidth + (sWidth / 1f));
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
for (int z = 0; z < 3; z++)
{
topNodes[x, y, z].viewPosChanged(setPos);
}
curBottomNode = newPos;
}
}
/// <summary>
/// Returns a node containing the point as close as possible to the requested LOD.
/// </summary>
/// <param name="pos"></param>
/// <param name="searchLOD"></param>
/// <returns>Null if no such node exists.</returns>
public static Node[] searchNodeContainingDensity(Vector3 pos, int searchLOD)
{
Node[] ret = new Node[8];
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
for (int z = 0; z < 3; z++)
{
if (topNodes[x, y, z] != null)
topNodes[x, y, z].searchNodeCreate(pos, searchLOD, ref ret);
}
}
}
return ret;
}
/// <summary>
/// Returns a node containing the point as close as possible to the requested LOD.
/// </summary>
/// <param name="pos"></param>
/// <param name="searchLOD"></param>
/// <returns>Null if no such node exists.</returns>
public static Node searchNode(Vector3 pos, int searchLOD)
{
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
for (int z = 0; z < 3; z++)
{
if (topNodes[x, y, z] != null)
if (topNodes[x, y, z].containsPoint(pos))
return topNodes[x, y, z].searchNode(pos, searchLOD);
}
}
}
return null;
}
/// <summary>
/// Calculates the offset position given a parent's node and the subnode ID.
/// </summary>
/// <param name="parentNode">Parent that contains t</param>
/// <param name="subNodeID">Index of the node in the subnode array</param>
/// <returns></returns>
public static Vector3 getOffsetPosition(Node parentNode, int subNodeID)
{
//Vector3 ret = new Vector3(parentNode.position.x, parentNode.position.y, parentNode.position.z);
int parentWidth = nodeSize * LODSize[parentNode.LOD];
return new Vector3
{
x = parentNode.position.x + ((parentWidth / 2) * mask[subNodeID].x),
y = parentNode.position.y + ((parentWidth / 2) * mask[subNodeID].y),
z = parentNode.position.z + ((parentWidth / 2) * mask[subNodeID].z)
};
}
/// <summary>
/// Returns a 3d integer vector position of the "top" (highest LOD) node that contains the given position.
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public static Vector3I getTopNode(Vector3 pos)
{
Vector3I ret = new Vector3I();
ret.x = (int)Mathf.Floor(pos.x / (NodeManager.LODSize[maxLOD] * nodeSize));
ret.y = (int)Mathf.Floor(pos.y / (NodeManager.LODSize[maxLOD] * nodeSize));
ret.z = (int)Mathf.Floor(pos.z / (NodeManager.LODSize[maxLOD] * nodeSize));
return ret;
}
/// <summary>
/// Used to draw the chunk wirecubes in scene view
/// </summary>
/// <returns></returns>
public static int debugDraw()
{
int ct = 0;
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
for (int z = 0; z < 3; z++)
{
if (topNodes[x, y, z] != null)
ct += topNodes[x, y, z].debugDraw();
}
}
}
return ct;
}
}
/// <summary>
/// Octree node (chunk)
/// </summary>
internal class Node
{
#region Fields
/// <summary> When we are already generating a new mesh and need a new regeneration. </summary>
public bool regenFlag = false;
private bool regenReq = false;
public bool permanent = false;
public bool hasDensityChangeData = false;
/// <summary> Density array that contains the mesh data. </summary>
public DensityData densityData;
public DensityData densityChangeData;
/// <summary>Index of this node in the parent node's subnode array</summary>
public int subNodeID;
/// <summary>Level of Detail value</summary>
public int LOD;
/// <summary> The integer position of the chunk (Not real) </summary>
public Vector3I chunkPos;
/// <summary>Real position</summary>
public Vector3 position;
/// <summary>Subnodes under this parent node</summary>
public Node[] subNodes = new Node[8];
/// <summary>
/// Neighbor nodes of the same LOD
/// Null means the neighbor node either doesn't exist or hasn't been allocated yet.
/// </summary>
public Node[] neighborNodes = new Node[6];
/// <summary>The parent that owns this node. Null if top-level</summary>
public Node parent;
/// <summary>Gameobject that contains the mesh</summary>
private GameObject chunk;
/// <summary> Center of the chunk in real pos </summary>
public Vector3 center;
public bool disposed = false;
public bool hasMesh = false;
public bool collides = false;
public bool empty = true;
public RenderType renderType;
public enum RenderType
{
FRONT, BACK
}
/// <summary> Mask used for positioning subnodes</summary>
public static Vector3[] neighborMask = new Vector3[6] {
new Vector3(-1, 0, 0),
new Vector3(0, -1, 0),
new Vector3(0, 0, -1),
new Vector3(1, 0, 0),
new Vector3(0, 1, 0),
new Vector3(0, 0, 1),
};
public static int[] oppositeNeighbor = new int[6] {
3, 4, 5, 0, 1, 2
};
#endregion
public Node(Node parent, Vector3 position, int subNodeID, int LOD, RenderType renderType)
{
densityChangeData = new DensityData();
this.parent = parent;
this.position = position;
this.subNodeID = subNodeID;
this.LOD = LOD;
float chunkWidth = (NodeManager.LODSize[LOD] * NodeManager.nodeSize) / 2f;
center = new Vector3(position.x + chunkWidth,
position.y + chunkWidth,
position.z + chunkWidth);
setRenderType(renderType);
if (parent != null && parent.permanent)
permanent = true;
NodeManager.nodeCount[LOD]++;
float nWidth = NodeManager.LODSize[LOD] * NodeManager.nodeSize;
chunkPos.x = (int)(center.x / nWidth);
chunkPos.y = (int)(center.y / nWidth);
chunkPos.z = (int)(center.z / nWidth);
if (LOD == 0)
{
string dir = getDirectory();
if (Directory.Exists(dir) && File.Exists(dir + "\\densities.txt"))
MeshFactory.requestLoad(this);
}
regenReq = true;
MeshFactory.requestMesh(this);
}
/// <summary>
/// Called when the viewpoint has changed.
/// </summary>
/// <param name="pos"></param>
public void viewPosChanged(Vector3 pos)
{
//float sep = 10f;
//float distance = ((center.x - pos.x) * (center.x - pos.x) +
//(center.y - pos.y) * (center.y - pos.y) +
//(center.z - pos.z) * (center.z - pos.z));
//if (distance < (((float)NodeManager.LODRange[LOD]) * sep) * (((float)NodeManager.LODRange[LOD]) * sep))
Vector3I viewPos = NodeManager.viewChunkPos[LOD];
int size = 1;
if ((viewPos.x >= chunkPos.x - size && viewPos.x <= chunkPos.x + size)
&& (viewPos.y >= chunkPos.y - size && viewPos.y <= chunkPos.y + size)
&& (viewPos.z >= chunkPos.z - size && viewPos.z <= chunkPos.z + size))
{
if (isBottomLevel())
createSubNodes(RenderType.FRONT);
for (int i = 0; i < 8; i++)
{
if (subNodes[i] != null)
subNodes[i].viewPosChanged(pos);
}
}
//else if (!permanent)
else
{
size += 2;
if (LOD < 3 && (viewPos.x < chunkPos.x - size || viewPos.x > chunkPos.x + size)
|| (viewPos.y < chunkPos.y - size || viewPos.y > chunkPos.y + size)
|| (viewPos.z < chunkPos.z - size || viewPos.z > chunkPos.z + size))
{
for (int i = 0; i < 8; i++)
{
if (subNodes[i] != null)
{
subNodes[i].dispose();
subNodes[i] = null;
}
}
}
else if (LOD >= 3)
{
for (int i = 0; i < 8; i++)
{
if (subNodes[i] != null)
{
subNodes[i].dispose();
subNodes[i] = null;
}
}
}
}
if (LOD == 0)
{
float nodeSize = (float)NodeManager.LODSize[0] * (float)NodeManager.nodeSize;
Vector3I viewChunk = new Vector3I((int)(pos.x / nodeSize),
(int)(pos.y / nodeSize),
(int)(pos.z / nodeSize));
Vector3I curChunk = new Vector3I((int)(position.x / nodeSize),
(int)(position.y / nodeSize),
(int)(position.z / nodeSize));
if (curChunk.x >= viewChunk.x - 3 && curChunk.x <= viewChunk.x + 3 &&
curChunk.y >= viewChunk.y - 3 && curChunk.y <= viewChunk.y + 3 &&
curChunk.z >= viewChunk.z - 3 && curChunk.z <= viewChunk.z + 3)
{
collides = true;
if (chunk != null)
{
chunk.GetComponent<MeshCollider>().sharedMesh = chunk.GetComponent<MeshFilter>().sharedMesh;
}
}
}
renderCheck();
}
/// <summary>
/// Searches for a node containing the given point and LOD, creating it if none is found.
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public void searchNodeCreate(Vector3 pos, int searchLOD, ref Node[] list)
{
if (containsDensityPoint(pos))
{
if (LOD == searchLOD)
{
for (int i = 0; i < list.Length; i++)
if (list[i] == null)
{
list[i] = this;
return;
}
Debug.Log("A");
}
else
{
if (isBottomLevel())
createSubNodes(RenderType.FRONT);
for (int i = 0; i < 8; i++)
{
subNodes[i].searchNodeCreate(pos, searchLOD, ref list);
}
}
}
}
/// <summary>
/// Searches for a node containing the given point and LOD.
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public Node searchNode(Vector3 pos, int searchLOD)
{
if (containsDensityPoint(pos))
{
if (searchLOD == LOD)
return this;
if (!isBottomLevel())
for (int i = 0; i < 8; i++)
if (subNodes[i] != null)
{
if (subNodes[i].containsPoint(pos))
return subNodes[i].searchNode(pos, searchLOD);
}
return this;
}
if (parent != null)
return parent.searchNode(pos, searchLOD);
return NodeManager.searchNode(pos, searchLOD);
}
/// <summary>
/// Returns whether or not the point is within the chunk.
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public bool containsPoint(Vector3 pos)
{
float chunkWidth = NodeManager.LODSize[LOD] * NodeManager.nodeSize;
if ((pos.x >= position.x && pos.y >= position.y && pos.z >= position.z)
&& (pos.x <= position.x + chunkWidth && pos.y <= position.y + chunkWidth && pos.z <= position.z + chunkWidth))
{
return true;
}
return false;
}
/// <summary>
/// Returns whether or not the point is within the chunk.
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public bool containsDensityPoint(Vector3 pos)
{
float chunkWidth = NodeManager.LODSize[LOD] * NodeManager.nodeSize;
Vector3 corner1 = new Vector3(position.x - NodeManager.LODSize[LOD],
position.y - NodeManager.LODSize[LOD],
position.z - NodeManager.LODSize[LOD]);
Vector3 corner2 = new Vector3(position.x + chunkWidth + NodeManager.LODSize[LOD],
position.y + chunkWidth + NodeManager.LODSize[LOD],
position.z + chunkWidth + NodeManager.LODSize[LOD]);
if((pos.x >= corner1.x && pos.y >= corner1.y && pos.z >= corner1.z) &&
(pos.x <= corner2.x && pos.y <= corner2.y && pos.z <= corner2.z)) {
return true;
}
return false;
}
/// <summary>
/// Checks whether this chunk should render and where.
/// </summary>
public void renderCheck()
{
if (disposed)
{
if (chunk != null)
chunk.GetComponent<MeshFilter>().renderer.enabled = false;
return;
}
if (chunk != null)
if (isBottomLevel())
{
chunk.GetComponent<MeshFilter>().renderer.enabled = true;
setRenderType(RenderType.FRONT);
}
else
{
bool render = false;
for (int i = 0; i < 8; i++)
if (subNodes[i] != null && !subNodes[i].hasMesh)
{
render = true;
setRenderType(RenderType.FRONT);
}
chunk.GetComponent<MeshFilter>().renderer.enabled = render;
}
}
/// <summary>
/// Called when a mesh has been generated
/// </summary>
/// <param name="mesh">Mesh data</param>
public void setMesh(MeshData meshData)
{
densityData.setChangeData(densityChangeData);
regenReq = false;
if (regenFlag)
{
regenFlag = false;
regenReq = true;
MeshFactory.requestMesh(this);
}
hasMesh = true;
if (meshData.indexArray.Length == 0)
return;
if (chunk == null)
{
chunk = ChunkPool.getChunk();
if (LOD > 2)
chunk.transform.position = position - new Vector3(0f, (NodeManager.LODSize[LOD] / 2f), 0f);
else
chunk.transform.position = position;
//chunk.GetComponent<MeshFilter>().mesh.subMeshCount = QuixelEngine.materials.Length;
chunk.GetComponent<MeshRenderer>().materials = QuixelEngine.materials;
}
empty = false;
Mesh mesh = new Mesh();
mesh.subMeshCount = QuixelEngine.materials.Length;
mesh.vertices = meshData.triangleArray;
for (int i = 0; i < QuixelEngine.materials.Length; i++)
{
if (meshData.indexArray[i].Length > 0)
mesh.SetTriangles(meshData.indexArray[i], i);
}
//mesh.triangles = meshData.indexArray;
mesh.uv = meshData.uvArray;
mesh.normals = meshData.normalArray;
//mesh.RecalculateBounds();
mesh.Optimize();
chunk.GetComponent<MeshFilter>().mesh = mesh;
if (LOD == 0 && collides)
chunk.GetComponent<MeshCollider>().sharedMesh = mesh;
meshData.dispose();
renderCheck();
switch (renderType)
{
case RenderType.BACK:
if (chunk != null)
chunk.layer = 9;
break;
case RenderType.FRONT:
if (chunk != null)
chunk.layer = 8;
break;
}
if (parent != null)
parent.renderCheck();
}
/// <summary>
/// Sets the render type of the chunk.
/// Front will be rendered last, on top of Back.
/// </summary>
/// <param name="r"></param>
public void setRenderType(RenderType r)
{
switch (r)
{
case RenderType.BACK:
if (chunk != null)
chunk.layer = 9;
break;
case RenderType.FRONT:
if (chunk != null)
chunk.layer = 8;
break;
}
renderType = r;
}
/// <summary>
/// Returns whether or not this is the bottom level of the tree.
/// </summary>
/// <returns></returns>
public bool isBottomLevel()
{
for (int i = 0; i < 8; i++)
{
if (subNodes[i] == null)
return true;
else if (subNodes[i].disposed)
return true;
}
return false;
}
/// <summary>
/// Checks if this is a transitional node.
/// True if an adjacent node of the same LOD has subnodes.
/// </summary>
/// <returns></returns>
public bool isTransitional()
{
if (LOD == 1 || LOD == 0)
return false;
for (int i = 0; i < 6; i++)
{
if (neighborNodes[i] != null)
if (neighborNodes[i].LOD == this.LOD && neighborNodes[i].isBottomLevel() && !isBottomLevel())
return true;
}
return false;
}
/// <summary>
/// Populates the subnode array
/// </summary>
public void createSubNodes(RenderType type)
{
if (LOD == 0)
return;
if (subNodes[0] != null)
{
for (int i = 0; i < 8; i++)
{
if (subNodes[i].renderType != type)
subNodes[i].setRenderType(type);
subNodes[i].disposed = false;
}
return;
}
for (int i = 0; i < 8; i++)
{
if (subNodes[i] == null)
subNodes[i] = new Node(this, NodeManager.getOffsetPosition(this, i), i, LOD - 1, type);
}
}
/// <summary>
/// Sets the density of a point, given a world pos.
/// </summary>
/// <param name="worldPos"></param>
public void setDensityFromWorldPos(Vector3 worldPos, float val)
{
worldPos = worldPos - position;
Vector3I arrayPos = new Vector3I((int)Math.Round(worldPos.x) / NodeManager.LODSize[LOD],
(int)Math.Round(worldPos.y) / NodeManager.LODSize[LOD],
(int)Math.Round(worldPos.z) / NodeManager.LODSize[LOD]);
if (arrayPos.x < -1 || arrayPos.x > 17 ||
arrayPos.y < -1 || arrayPos.y > 17 ||
arrayPos.z < -1 || arrayPos.z > 17)
{
Debug.Log("Wrong node. " + arrayPos + ":" + worldPos + ":" + containsDensityPoint(worldPos).ToString());
return;
}
densityChangeData.set(arrayPos.x, arrayPos.y, arrayPos.z, val);
setPermanence(true);
hasDensityChangeData = true;
MeshFactory.requestSave(this);
}
/// <summary>
/// Sets the material of the voxel at the given world position.
/// </summary>
/// <param name="worldPos"></param>
/// <param name="val"></param>
public void setMaterialFromWorldPos(Vector3 worldPos, byte val)
{
worldPos = worldPos - position;
Vector3I arrayPos = new Vector3I((int)Math.Round(worldPos.x) / NodeManager.LODSize[LOD],
(int)Math.Round(worldPos.y) / NodeManager.LODSize[LOD],
(int)Math.Round(worldPos.z) / NodeManager.LODSize[LOD]);
if (arrayPos.x < -1 || arrayPos.x > 17 ||
arrayPos.y < -1 || arrayPos.y > 17 ||
arrayPos.z < -1 || arrayPos.z > 17)
{
Debug.Log("Wrong node. " + arrayPos);
return;
}
bool change = (densityChangeData.getMaterial(arrayPos.x, arrayPos.y, arrayPos.z) != val);
densityChangeData.setMaterial(arrayPos.x, arrayPos.y, arrayPos.z, val);
if (change)
{
setPermanence(true);
hasDensityChangeData = true;
MeshFactory.requestSave(this);
}
}
/// <summary>
/// Regenerates the chunk without threading.
/// </summary>
public void regenerateChunk()
{
if (regenReq)
{
regenFlag = true;
}
else
{
MeshFactory.requestMesh(this);
regenReq = true;
}
}
/// <summary>
/// Saves changes, if any.
/// </summary>
public void saveChanges()
{
if (!permanent)
return;
string dir = getDirectory();
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
int[] matData = densityChangeData.compressMaterialData();
float[] data = densityChangeData.compressDensityData();
StreamWriter writer = new StreamWriter(dir + "\\materials.txt");
for (int i = 0; i < matData.Length; i += 2)
writer.WriteLine(matData[i] + "," + matData[i + 1]);
writer.Close();
writer = new StreamWriter(dir + "\\densities.txt");
for (int i = 0; i < data.Length; i += 2)
writer.WriteLine(data[i] + "," + data[i + 1]);
writer.Close();
}
/// <summary>
/// Attempts to load density changes.
/// </summary>
public bool loadChanges()
{
string dir = getDirectory();
if (!Directory.Exists(dir))
return false;
if (!File.Exists(dir + "\\densities.txt"))
return false;
List<string> fileData = new List<string>();
StreamReader reader = new StreamReader(dir + "\\densities.txt");
while (!reader.EndOfStream)
fileData.Add(reader.ReadLine());
float[] data = new float[fileData.Count * 2];
string[] split = new string[2];
for (int i = 0; i < fileData.Count; i++)
{
split = fileData[i].Split(',');
data[(i * 2)] = float.Parse(split[0]);
data[(i * 2) + 1] = float.Parse(split[1]);
}
reader.Close();
densityChangeData.decompressDensityData(data);
fileData = new List<string>();
reader = new StreamReader(dir + "\\materials.txt");
while (!reader.EndOfStream)
fileData.Add(reader.ReadLine());
int[] mdata = new int[fileData.Count * 2];
split = new string[2];
for (int i = 0; i < fileData.Count; i++)
{
split = fileData[i].Split(',');
mdata[(i * 2)] = int.Parse(split[0]);
mdata[(i * 2) + 1] = int.Parse(split[1]);
}
reader.Close();
densityChangeData.decompressMaterialData(mdata);
hasDensityChangeData = true;
return true;
}
/// <summary>
/// Disposes of subnodes
/// </summary>
public void dispose()
{
//If already disposed, exit.
if (disposed)
return;
disposed = true;
for (int i = 0; i < 8; i++)
{
if (subNodes[i] != null)
{
subNodes[i].dispose();
subNodes[i] = null;
}
}
if (permanent)
{
if (chunk != null)