-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
voxel.agc
1477 lines (1264 loc) · 50.3 KB
/
voxel.agc
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
//~#import_plugin OpenSimplexNoise as Noise
//~#include "threadnoise.agc"
// Data Types able to represent any AGK mesh
type Vec4Data
X#
Y#
Z#
W#
endtype
type Vec3Data
X#
Y#
Z#
endtype
type Vec2Data
X#
Y#
endtype
type Int3Data
X as integer
Y as integer
Z as integer
endtype
type Int2Data
X as integer
Z as integer
endtype
type RGBAData
Red
Green
Blue
Alpha
endtype
type VertexData
Pos as Vec3Data
UV as Vec2Data
Color as RGBAData
Normal as Vec3Data
Tangent as Vec3Data
Binormal as Vec3Data
endtype
type ObjectData
Vertex as VertexData[]
Index as integer[]
endtype
#constant FaceFront 1
#constant FaceBack 2
#constant FaceLeft 3
#constant FaceRight 4
#constant FaceUp 5
#constant FaceDown 6
type WorldData
TerrainWidth as integer
TerrainHeight as integer
TerrainDepth as integer
Terrain as TerrainData[-1,-1,-1]
Chunk as ChunkData[-1,-1]
Height as integer[-1,-1]
endtype
type TerrainData
BlockType as integer
BlockLight as integer
SunLight as integer
endtype
type SubimageData
X as integer
Y as integer
Width as integer
Height as integer
endtype
type FaceIndexData
FrontID as integer
BackID as integer
UpID as integer
DownID as integer
LeftID as integer
RightID as integer
endtype
type FaceimageData
Subimages as SubimageData[]
FaceimageIndices as FaceIndexData[]
endtype
type BorderData
Min as Int2Data
Max as Int2Data
endtype
type ChunkData
Border as BorderData
ObjectID as integer
Visible as integer
endtype
type MemblockData
GrassMemblockID as integer
CaveMemblockID as integer
IronMemblockID as integer
endtype
type ChunkListData
Hash as integer
X as integer
Z as integer
endtype
type SpreadLightData
X as integer
Y as integer
Z as Integer
Light as Integer
endtype
global Voxel_ChunkMemblock as MemblockData[]
global Voxel_Neighbors as Int3Data[5]
global Voxel_LoadChunkList as ChunkListData[]
global Voxel_UnloadChunkList as ChunkListData[]
global Voxel_TempChunk as ChunkListData
global Voxel_TempSubimages as SubimageData[5]
global Voxel_WorldSize as Int2Data
global Voxel_ChunkView as BorderData
global Voxel_SpreadLight as SpreadLightData[]
global Voxel_AmbientLight as integer
global Voxel_ChunkSize as integer
global Voxel_DiffuseImageID as integer
global Voxel_NormalImageID as integer
global Voxel_ShaderID as integer
global Voxel_TempID as integer
global Voxel_UpdateTimer# as float
global Voxel_CameraChunkOldX as integer
global Voxel_CameraChunkOldZ as integer
global Voxel_WorldName$ as string
global Voxel_DebugMeshBuildingTime# as float
global Voxel_DebugSaveTime# as float
global Voxel_DebugCounter as integer
// Functions
// Initialise the Voxel Engine
function Voxel_Init(World ref as WorldData,ChunkSize,TerrainSizeX,TerrainSizeY,TerrainSizeZ,File$,WorldName$)
Voxel_DiffuseImageID=LoadImage(File$)
//~ Voxel_NormalImageID=LoadImage(StringInsertAtDelemiter(File$,"_n.","."))
Voxel_ShaderID=LoadShader("shader/vertex.vs","shader/fragment.ps")
Voxel_WorldName$=WorldName$
Voxel_ChunkSize=ChunkSize
Voxel_WorldSize.X=trunc((TerrainSizeX+1)/Voxel_ChunkSize)-1
Voxel_WorldSize.Z=trunc((TerrainSizeZ+1)/Voxel_ChunkSize)-1
Voxel_AmbientLight=2
World.TerrainWidth=TerrainSizeX
World.TerrainHeight=TerrainSizeY
World.TerrainDepth=TerrainSizeZ
World.Height.length=World.TerrainWidth+1
World.Terrain.length=World.TerrainWidth+1
for X=0 to World.TerrainWidth+1
World.Height[X].length=World.TerrainDepth+1
World.Terrain[X].length=World.TerrainHeight+1
for Y=0 to World.TerrainHeight+1
World.Terrain[X,Y].length=World.TerrainDepth+1
next Y
next X
World.Chunk.length=Voxel_WorldSize.X
for X=0 to Voxel_WorldSize.X
World.Chunk[X].length=Voxel_WorldSize.Z
next X
Voxel_Neighbors[0].x=0
Voxel_Neighbors[0].y=1
Voxel_Neighbors[0].z=0
Voxel_Neighbors[1].x=0
Voxel_Neighbors[1].y=-1
Voxel_Neighbors[1].z=0
Voxel_Neighbors[2].x=1
Voxel_Neighbors[2].y=0
Voxel_Neighbors[2].z=0
Voxel_Neighbors[3].x=-1
Voxel_Neighbors[3].y=0
Voxel_Neighbors[3].z=0
Voxel_Neighbors[4].x=0
Voxel_Neighbors[4].y=0
Voxel_Neighbors[4].z=1
Voxel_Neighbors[5].x=0
Voxel_Neighbors[5].y=0
Voxel_Neighbors[5].z=-1
endfunction
function Voxel_ReadFaceImages(FaceImagesFile$, Faceimages ref as FaceimageData)
local string$ as string
string$ = Voxel_JSON_Load(FaceImagesFile$)
Faceimages.fromJSON(string$)
endfunction
function Voxel_SaveFaceImages(FaceIMagesFile$, Faceimages as FaceimageData)
local string$ as string
string$ = Faceimages.toJSON()
Voxel_JSON_Save(string$ , FaceIMagesFile$)
endfunction
function Voxel_ReadWorld(WorldFile$, World ref as WorldData)
local string$ as string
string$ = Voxel_JSON_Load(WorldFile$)
World.fromJSON(string$)
endfunction
function Voxel_SaveWorld(WorldFile$, World as WorldData)
local string$ as string
string$ = World.toJSON()
Voxel_JSON_Save(string$ , WorldFile$)
endfunction
function Voxel_SaveChunk(World ref as WorldData,Border ref as BorderData,ChunkX,ChunkZ)
StartTime#=Timer()
local RunLength as integer
local OldBlockType as integer
OpenToWrite(1,Voxel_WorldName$+"/Chunk_"+str(ChunkX)+"_"+str(ChunkZ)+".bin",0)
for CubeX=Border.Min.X to Border.Max.X
for CubeZ=Border.Min.Z to Border.Max.Z
RunLength=0
Height=World.Height[CubeX,CubeZ]
WriteByte(1,Height)
for CubeY=1 to Height
inc RunLength
if World.Terrain[CubeX,CubeY,CubeZ].BlockType<>OldBlockType
OldBlockType=World.Terrain[CubeX,CubeY,CubeZ].BlockType
WriteByte(1,RunLength)
WriteByte(1,World.Terrain[CubeX,CubeY,CubeZ].BlockType)
RunLength=0
endif
next CubeY
WriteByte(1,RunLength)
WriteByte(1,World.Terrain[CubeX,CubeY,CubeZ].BlockType)
next CubeZ
next CubeX
CloseFile(1)
Voxel_DebugSaveTime#=Timer()-StartTime#
endfunction
function Voxel_ChunkFileExists(ChunkX,ChunkZ)
local Result as integer
Result = GetFileExists(Voxel_WorldName$+"/Chunk_"+str(ChunkX)+"_"+str(ChunkZ))
endfunction Result
function Voxel_AddChunktoLoadList(ChunkX,ChunkZ)
if ChunkX<Voxel_ChunkView.Min.X or ChunkX>Voxel_ChunkView.Max.X or ChunkZ<Voxel_ChunkView.Min.Z or ChunkZ>Voxel_ChunkView.Max.Z then exitfunction
Voxel_TempChunk.X=ChunkX
Voxel_TempChunk.Z=ChunkZ
Voxel_TempChunk.Hash=ChunkX+(ChunkZ*Voxel_WorldSize.X)
if Voxel_LoadChunkList.IndexOf(Voxel_TempChunk.Hash)=-1 and Voxel_UnloadChunkList.IndexOf(Voxel_TempChunk.Hash)=-1 then Voxel_LoadChunkList.insert(Voxel_TempChunk)
endfunction
function Voxel_AddChunktoUnloadList(ChunkX,ChunkZ)
Voxel_TempChunk.X=ChunkX
Voxel_TempChunk.Z=ChunkZ
Voxel_TempChunk.Hash=ChunkX+(ChunkZ*Voxel_WorldSize.X)
if Voxel_UnloadChunkList.IndexOf(Voxel_TempChunk.Hash)=-1 then Voxel_UnloadChunkList.insert(Voxel_TempChunk)
endfunction
function Voxel_UpdateChunks(FaceImages ref as FaceimageData,World ref as WorldData,CameraX,CameraZ,ViewDistance)
Voxel_CameraChunkX=trunc(CameraX/Voxel_ChunkSize)
Voxel_CameraChunkZ=trunc(CameraZ/Voxel_ChunkSize)
if Voxel_CameraChunkX<>Voxel_CameraChunkOldX or Voxel_CameraChunkZ<>Voxel_CameraChunkOldZ
CameraDirX=Voxel_CameraChunkX-Voxel_CameraChunkOldX
CameraDirZ=Voxel_CameraChunkZ-Voxel_CameraChunkOldZ
Voxel_CameraChunkOldX=Voxel_CameraChunkX
Voxel_CameraChunkOldZ=Voxel_CameraChunkZ
for Dist=0 to ViewDistance
Voxel_ChunkView.Min.X=Core_Clamp(Voxel_CameraChunkX-Dist,0,Voxel_WorldSize.X)
Voxel_ChunkView.Min.Z=Core_Clamp(Voxel_CameraChunkZ-Dist,0,Voxel_WorldSize.Z)
Voxel_ChunkView.Max.X=Core_Clamp(Voxel_CameraChunkX+Dist,0,Voxel_WorldSize.X)
Voxel_ChunkView.Max.Z=Core_Clamp(Voxel_CameraChunkZ+Dist,0,Voxel_WorldSize.Z)
for ChunkX=Voxel_ChunkView.Min.X to Voxel_ChunkView.Max.X
for ChunkZ=Voxel_ChunkView.Min.Z to Voxel_ChunkView.Max.Z
if World.Chunk[ChunkX,ChunkZ].ObjectID=0 then Voxel_AddChunktoLoadList(ChunkX,ChunkZ)
next ChunkZ
next ChunkX
next Dist
//>--- unkomment this to unload chunks outside of the view range ----<
//~ MinX=Core_Clamp(Voxel_CameraChunkX-ViewDistance-1,0,Voxel_WorldSize.X)
//~ MinZ=Core_Clamp(Voxel_CameraChunkZ-ViewDistance-1,0,Voxel_WorldSize.Z)
//~ MaxX=Core_Clamp(Voxel_CameraChunkX+ViewDistance+1,0,Voxel_WorldSize.X)
//~ MaxZ=Core_Clamp(Voxel_CameraChunkZ+ViewDistance+1,0,Voxel_WorldSize.Z)
//~ if CameraDirX=1
//~ for ChunkZ=MinZ to MaxZ
//~ if World.Chunk[MinX,ChunkZ].Visible=1
//~ Voxel_AddChunktoUnloadList(MinX,ChunkZ)
//~ World.Chunk[MinX,ChunkZ].Visible=0
//~ endif
//~ next ChunkZ
//~ elseif CameraDirX-1
//~ for ChunkZ=MinZ to MaxZ
//~ if World.Chunk[MaxX,ChunkZ].Visible=1
//~ Voxel_AddChunktoUnloadList(MaxX,ChunkZ)
//~ World.Chunk[MaxX,ChunkZ].Visible=0
//~ endif
//~ next ChunkZ
//~ endif
//~ if CameraDirZ=1
//~ for ChunkX=MinX to MaxX
//~ if World.Chunk[ChunkX,MinZ].Visible=1
//~ Voxel_AddChunktoUnloadList(ChunkX,MinZ)
//~ World.Chunk[ChunkX,MinZ].Visible=0
//~ endif
//~ next ChunkX
//~ elseif CameraDirZ-1
//~ for ChunkX=MinX to MaxX
//~ if World.Chunk[ChunkX,MaxZ].Visible=1
//~ Voxel_AddChunktoUnloadList(ChunkX,MaxZ)
//~ World.Chunk[ChunkX,MaxZ].Visible=0
//~ endif
//~ next ChunkX
//~ endif
endif
Timer#=Timer()
if Timer#>Voxel_UpdateTimer#
Voxel_UpdateTimer#=Timer#+0.1
if Voxel_LoadChunkList.length>-1
ChunkX=Voxel_LoadChunkList[0].X
ChunkZ=Voxel_LoadChunkList[0].Z
if World.Chunk[ChunkX,ChunkZ].ObjectID=0
World.Chunk[ChunkX,ChunkZ].Border.Min.X=ChunkX*Voxel_ChunkSize+1
World.Chunk[ChunkX,ChunkZ].Border.Min.Z=ChunkZ*Voxel_ChunkSize+1
World.Chunk[ChunkX,ChunkZ].Border.Max.X=ChunkX*Voxel_ChunkSize+Voxel_ChunkSize
World.Chunk[ChunkX,ChunkZ].Border.Max.Z=ChunkZ*Voxel_ChunkSize+Voxel_ChunkSize
Voxel_CreateTerrain(World.Chunk[ChunkX,ChunkZ].Border,World)
Voxel_UpdateChunkSunLight(World.Chunk[ChunkX,ChunkZ].Border,World,15)
Voxel_CreateChunk(Faceimages,World.Chunk[ChunkX,ChunkZ],World)
World.Chunk[ChunkX,ChunkZ].Visible=1
else
Voxel_UpdateChunkSunLight(World.Chunk[ChunkX,ChunkZ].Border,World,15)
Voxel_UpdateChunk(Faceimages,World.Chunk[ChunkX,ChunkZ],World)
endif
Voxel_LoadChunkList.remove(0)
endif
//>--- unkomment this to unload chunks outside of the view range ----<
//~ if Voxel_UnloadChunkList.length>-1
//~ ChunkX=Voxel_UnloadChunkList[0].X
//~ ChunkZ=Voxel_UnloadChunkList[0].Z
//~ Voxel_SaveChunk(World,World.Chunk[ChunkX,ChunkZ].Border,ChunkX,ChunkZ)
//~ Voxel_DeleteObject(World.Chunk[ChunkX,ChunkZ])
//~ Voxel_UnloadChunkList.remove(0)
//~ endif
endif
endfunction
function Voxel_DeleteObject(Chunk ref as ChunkData)
DeleteObject(Chunk.ObjectID)
Chunk.ObjectID=0
endfunction
function Voxel_CreateChunk(FaceImages ref as FaceimageData,Chunk ref as ChunkData,World ref as WorldData)
local Object as ObjectData
for CubeX=Chunk.Border.Min.X to Chunk.Border.Max.X
for CubeZ=Chunk.Border.Min.Z to Chunk.Border.Max.Z
for CubeY=1 to World.Height[CubeX,CubeZ]
Voxel_GenerateCubeFaces(Object,Faceimages,World,CubeX,CubeY,CubeZ)
next CubeY
next CubeZ
next CubeX
if Object.Vertex.length>1
MemblockID=Voxel_CreateMeshMemblock(Object.Vertex.length+1,Object.Index.length+1)
Voxel_WriteMeshMemblock(MemblockID,Object)
Chunk.ObjectID=CreateObjectFromMeshMemblock(MemblockID)
DeleteMemblock(MemblockID)
Object.Index.length=-1
Object.Vertex.length=-1
SetObjectPosition(Chunk.ObjectID,Chunk.Border.Min.X-1,0,Chunk.Border.Min.Z-1)
SetObjectImage(Chunk.ObjectID,Voxel_DiffuseImageID,0)
SetObjectShader(Chunk.ObjectID,Voxel_ShaderID)
Chunk.Visible=1
endif
endfunction Chunk.ObjectID
function Voxel_UpdateChunk(Faceimages ref as FaceimageData,Chunk ref as ChunkData,World ref as WorldData)
StartTime#=Timer()
local Object as ObjectData
for CubeX=Chunk.Border.Min.X to Chunk.Border.Max.X
for CubeZ=Chunk.Border.Min.Z to Chunk.Border.Max.Z
for CubeY=1 to World.Height[CubeX,CubeZ]
Voxel_GenerateCubeFaces(Object,Faceimages,World,CubeX,CubeY,CubeZ)
next CubeY
next CubeZ
next CubeX
if Object.Vertex.length>1
MemblockID=Voxel_CreateMeshMemblock(Object.Vertex.length+1,Object.Index.length+1)
Voxel_WriteMeshMemblock(MemblockID,Object)
SetObjectMeshFromMemblock(Chunk.ObjectID,1,MemblockID)
DeleteMemblock(MemblockID)
Object.Index.length=-1
Object.Vertex.length=-1
endif
Voxel_DebugMeshBuildingTime#=Timer()-StartTime#
endfunction
function Voxel_CreateTerrain(Border ref as BorderData,World ref as WorldData)
Frecueny2D#=32.0
Frecueny3DCave#=12.0
Frecueny3DIron#=2.0
DirtLayerHeight=3
WorldHeight=World.Terrain[0].length
GrassStart=WorldHeight*0.4
for X=Border.Min.X-1 to Border.Max.X+1
for Z=Border.Min.Z-1 to Border.Max.Z+1
Value1#=GetNoiseXY(X/Frecueny2D#,Z/Frecueny2D#)*WorldHeight
GrassLayer=GrassStart+Value1#/12.0
World.Height[X,Z]=GrassLayer
for Y=World.Terrain[X].length to 1 step -1
if Y=GrassLayer
World.Terrain[X,Y,Z].BlockType=1
elseif Y>=GrassLayer-DirtLayerHeight and Y<GrassLayer
World.Terrain[X,Y,Z].BlockType=3
elseif Y<GrassLayer-DirtLayerHeight
World.Terrain[X,Y,Z].BlockType=2
endif
Cave#=GetNoiseXYZ(X/Frecueny3DCave#,Y/Frecueny3DCave#,Z/Frecueny3DCave#)
if Cave#>0.5
World.Terrain[X,Y,Z].BlockType=0
if Y=World.Height[X,Z] then World.Height[X,Z]=Y-1
endif
World.Terrain[X,Y,Z].SunLight=Voxel_AmbientLight
World.Terrain[X,Y,Z].BlockLight=Voxel_AmbientLight
if Y>World.Height[X,Z]
World.Terrain[X,Y,Z].SunLight=15
endif
next Y
next Z
next X
endfunction
function Voxel_CreateBlockLight(World ref as WorldData,CubeX,CubeY,CubeZ)
LightIntensitiy=Voxel_IsLightBlock(World,CubeX,CubeY,CubeZ)
if LightIntensitiy>Voxel_AmbientLight
Voxel_UpdateBlockLight(World,CubeX,CubeY,CubeZ,LightIntensitiy)
endif
endfunction
function Voxel_UpdateBlockLight(World ref as WorldData,StartX,StartY,StartZ,StartBlockLight as integer)
local FrontierTemp as Int3Data
local Frontier as Int3Data[]
FrontierTemp.X=StartX
FrontierTemp.Y=StartY
FrontierTemp.Z=StartZ
Frontier.insert(FrontierTemp)
World.Terrain[StartX,StartY,StartZ].BlockLight=StartBlockLight
while Frontier.length>=0
CubeX=Frontier[0].X
CubeY=Frontier[0].Y
CubeZ=Frontier[0].Z
Frontier.remove(0)
for NeighbourID=0 to 5
NeighbourX=CubeX+Voxel_Neighbors[NeighbourID].X
NeighbourY=CubeY+Voxel_Neighbors[NeighbourID].Y
NeighbourZ=CubeZ+Voxel_Neighbors[NeighbourID].Z
if Voxel_IsTransparentBlock(World,NeighbourX,NeighbourY,NeighbourZ)=1
if World.Terrain[CubeX,CubeY,CubeZ].BlockLight>World.Terrain[NeighbourX,NeighbourY,NeighbourZ].BlockLight+1
FrontierTemp.X=NeighbourX
FrontierTemp.Y=NeighbourY
FrontierTemp.Z=NeighbourZ
Frontier.insert(FrontierTemp)
World.Terrain[NeighbourX,NeighbourY,NeighbourZ].BlockLight=World.Terrain[CubeX,CubeY,CubeZ].BlockLight-1
ChunkX=trunc((NeighbourX-1)/Voxel_ChunkSize)
ChunkZ=trunc((NeighbourZ-1)/Voxel_ChunkSize)
Voxel_AddChunktoLoadList(ChunkX,ChunkZ)
endif
endif
next NeighbourID
endwhile
endfunction
function Voxel_UpdateBlockShadow(World ref as WorldData,StartX,StartY,StartZ)
local FrontierTemp as Int3Data
local Frontier as Int3Data[]
local TempBlockLight as integer
local TempSpreadLight as SpreadLightData
FrontierTemp.X=StartX
FrontierTemp.Y=StartY
FrontierTemp.Z=StartZ
Frontier.insert(FrontierTemp)
while Frontier.length>=0
CubeX=Frontier[0].X
CubeY=Frontier[0].Y
CubeZ=Frontier[0].Z
Frontier.remove(0)
TempBlockLight=World.Terrain[CubeX,CubeY,CubeZ].BlockLight
if TempBlockLight<>Voxel_AmbientLight
for NeighbourID=0 to 5
NeighbourX=CubeX+Voxel_Neighbors[NeighbourID].X
NeighbourY=CubeY+Voxel_Neighbors[NeighbourID].Y
NeighbourZ=CubeZ+Voxel_Neighbors[NeighbourID].Z
if Voxel_IsTransparentBlock(World,NeighbourX,NeighbourY,NeighbourZ)=1
if TempBlockLight>World.Terrain[NeighbourX,NeighbourY,NeighbourZ].BlockLight
FrontierTemp.X=NeighbourX
FrontierTemp.Y=NeighbourY
FrontierTemp.Z=NeighbourZ
Frontier.insert(FrontierTemp)
World.Terrain[CubeX,CubeY,CubeZ].BlockLight=Voxel_AmbientLight
ChunkX=trunc((NeighbourX-1)/Voxel_ChunkSize)
ChunkZ=trunc((NeighbourZ-1)/Voxel_ChunkSize)
Voxel_AddChunktoLoadList(ChunkX,ChunkZ)
else
TempSpreadLight.X=NeighbourX
TempSpreadLight.Y=NeighbourY
TempSpreadLight.Z=NeighbourZ
TempSpreadLight.Light=World.Terrain[NeighbourX,NeighbourY,NeighbourZ].BlockLight
Voxel_SpreadLight.insert(TempSpreadLight)
endif
endif
next NeighbourID
endif
endwhile
for ID=0 to Voxel_SpreadLight.length-1
CubeX=Voxel_SpreadLight[ID].X
CubeY=Voxel_SpreadLight[ID].Y
CubeZ=Voxel_SpreadLight[ID].Z
Light=Voxel_SpreadLight[ID].Light
Voxel_UpdateBlockLight(World,CubeX,CubeY,CubeZ,Light)
next ID
Voxel_SpreadLight.length=-1
endfunction
function Voxel_UpdateSunLight(World ref as WorldData,StartX,StartY,StartZ,StartSunLight as integer)
local FrontierTemp as Int3Data
local Frontier as Int3Data[]
FrontierTemp.X=StartX
FrontierTemp.Y=StartY
FrontierTemp.Z=StartZ
Frontier.insert(FrontierTemp)
World.Terrain[StartX,StartY,StartZ].SunLight=StartSunLight
while Frontier.length>=0
CubeX=Frontier[0].X
CubeY=Frontier[0].Y
CubeZ=Frontier[0].Z
Frontier.remove(0)
for NeighbourID=0 to 5
NeighbourX=CubeX+Voxel_Neighbors[NeighbourID].X
NeighbourY=CubeY+Voxel_Neighbors[NeighbourID].Y
NeighbourZ=CubeZ+Voxel_Neighbors[NeighbourID].Z
if NeighbourY<=World.Height[NeighbourX,NeighbourZ]
if Voxel_IsTransparentBlock(World,NeighbourX,NeighbourY,NeighbourZ)=1
if World.Terrain[CubeX,CubeY,CubeZ].SunLight>World.Terrain[NeighbourX,NeighbourY,NeighbourZ].SunLight+1
FrontierTemp.X=NeighbourX
FrontierTemp.Y=NeighbourY
FrontierTemp.Z=NeighbourZ
Frontier.insert(FrontierTemp)
World.Terrain[NeighbourX,NeighbourY,NeighbourZ].SunLight=World.Terrain[CubeX,CubeY,CubeZ].SunLight-1
ChunkX=trunc((NeighbourX-1)/Voxel_ChunkSize)
ChunkZ=trunc((NeighbourZ-1)/Voxel_ChunkSize)
Voxel_AddChunktoLoadList(ChunkX,ChunkZ)
endif
endif
endif
next NeighbourID
endwhile
endfunction
function Voxel_UpdateChunkSunLight(Border ref as BorderData,World ref as WorldData,StartSunLight as integer)
local FrontierTemp as Int3Data
local Frontier as Int3Data[]
for CubeX=Border.Min.X to Border.Max.X
for CubeZ=Border.Min.Z to Border.Max.Z
CubeY=World.Height[CubeX,CubeZ]+1
FrontierTemp.X=CubeX
FrontierTemp.Y=CubeY
FrontierTemp.Z=CubeZ
Frontier.insert(FrontierTemp)
World.Terrain[CubeX,CubeY,CubeZ].SunLight=StartSunLight
next CubeZ
next CubeX
while Frontier.length>=0
CubeX=Frontier[0].X
CubeY=Frontier[0].Y
CubeZ=Frontier[0].Z
Frontier.remove(0)
for NeighbourID=0 to 5
NeighbourX=CubeX+Voxel_Neighbors[NeighbourID].X
NeighbourY=CubeY+Voxel_Neighbors[NeighbourID].Y
NeighbourZ=CubeZ+Voxel_Neighbors[NeighbourID].Z
if Voxel_IsTransparentBlock(World,NeighbourX,NeighbourY,NeighbourZ)=1
if World.Terrain[CubeX,CubeY,CubeZ].SunLight>World.Terrain[NeighbourX,NeighbourY,NeighbourZ].SunLight+1
FrontierTemp.X=NeighbourX
FrontierTemp.Y=NeighbourY
FrontierTemp.Z=NeighbourZ
Frontier.insert(FrontierTemp)
World.Terrain[NeighbourX,NeighbourY,NeighbourZ].SunLight=World.Terrain[CubeX,CubeY,CubeZ].SunLight-1
ChunkX=trunc((NeighbourX-1)/Voxel_ChunkSize)
ChunkZ=trunc((NeighbourZ-1)/Voxel_ChunkSize)
Voxel_AddChunktoLoadList(ChunkX,ChunkZ)
endif
endif
next NeighbourID
endwhile
endfunction
function Voxel_UpdateSunShadow(World ref as WorldData,StartX,StartY,StartZ)
local FrontierTemp as Int3Data
local Frontier as Int3Data[]
local TempSunLight as integer
local TempSpreadLight as SpreadLightData
FrontierTemp.X=StartX
FrontierTemp.Y=StartY
FrontierTemp.Z=StartZ
Frontier.insert(FrontierTemp)
while Frontier.length>=0
CubeX=Frontier[0].X
CubeY=Frontier[0].Y
CubeZ=Frontier[0].Z
Frontier.remove(0)
TempSunLight=World.Terrain[CubeX,CubeY,CubeZ].SunLight
if TempSunLight<>Voxel_AmbientLight
for NeighbourID=0 to 5
NeighbourX=CubeX+Voxel_Neighbors[NeighbourID].X
NeighbourY=CubeY+Voxel_Neighbors[NeighbourID].Y
NeighbourZ=CubeZ+Voxel_Neighbors[NeighbourID].Z
if Voxel_IsTransparentBlock(World,NeighbourX,NeighbourY,NeighbourZ)=1
if TempSunLight>World.Terrain[NeighbourX,NeighbourY,NeighbourZ].SunLight
FrontierTemp.X=NeighbourX
FrontierTemp.Y=NeighbourY
FrontierTemp.Z=NeighbourZ
Frontier.insert(FrontierTemp)
World.Terrain[CubeX,CubeY,CubeZ].SunLight=Voxel_AmbientLight
ChunkX=trunc((NeighbourX-1)/Voxel_ChunkSize)
ChunkZ=trunc((NeighbourZ-1)/Voxel_ChunkSize)
Voxel_AddChunktoLoadList(ChunkX,ChunkZ)
else
TempSpreadLight.X=NeighbourX
TempSpreadLight.Y=NeighbourY
TempSpreadLight.Z=NeighbourZ
TempSpreadLight.Light=World.Terrain[NeighbourX,NeighbourY,NeighbourZ].SunLight
Voxel_SpreadLight.insert(TempSpreadLight)
endif
endif
next NeighbourID
endif
endwhile
for ID=0 to Voxel_SpreadLight.length-1
CubeX=Voxel_SpreadLight[ID].X
CubeY=Voxel_SpreadLight[ID].Y
CubeZ=Voxel_SpreadLight[ID].Z
Light=Voxel_SpreadLight[ID].Light
Voxel_UpdateBlockLight(World,CubeX,CubeY,CubeZ,Light)
next ID
Voxel_SpreadLight.length=-1
endfunction
function Voxel_UpdateHeightAndSunlight(World ref as WorldData,X,Y,Z,LightValue)
repeat
World.Terrain[X,Y,Z].SunLight=LightValue
Y=Y-1
if Y<=1 then Exitfunction
until Voxel_IsTransparentBlock(World,X,Y,Z)=0
World.Height[X,Z]=Y
if LightValue>Voxel_AmbientLight
Voxel_UpdateSunLight(World,X,Y,Z,LightValue)
else
Voxel_UpdateSunShadow(World,X,Y,Z)
endif
endfunction
function Voxel_IsLightBlock(World ref as WorldData,X,Y,Z)
select World.Terrain[X,Y,Z].BlockType
case 11
exitfunction 14
endcase
endselect
endfunction 0
function Voxel_IsTransparentBlock(World ref as WorldData,X,Y,Z)
select World.Terrain[X,Y,Z].BlockType
case 0
exitfunction 1
endcase
endselect
endfunction 0
function Voxel_AddCubeToObject(World ref as WorldData,X,Y,Z,BlockType)
X=Core_Clamp(X,1,World.TerrainWidth-1)
Y=Core_Clamp(Y,1,World.TerrainHeight-1)
Z=Core_Clamp(Z,1,World.TerrainDepth-1)
if Y>World.Height[X,Z]
Voxel_UpdateHeightAndSunlight(World,X,Y,Z,Voxel_AmbientLight)
World.Height[X,Z]=Y
endif
World.Terrain[X,Y,Z].BlockType=BlockType
//~ World.Terrain[X,Y,Z].BlockLight=Voxel_AmbientLight
//~ World.Terrain[X,Y,Z].SunLight=Voxel_AmbientLight
Voxel_CreateBlockLight(World,X,Y,Z)
ChunkX=trunc((X-1)/Voxel_ChunkSize)
ChunkZ=trunc((Z-1)/Voxel_ChunkSize)
Voxel_AddChunktoLoadList(ChunkX,ChunkZ)
CubeX=1+Mod(X-1,Voxel_ChunkSize)
CubeZ=1+Mod(Z-1,Voxel_ChunkSize)
if CubeX=Voxel_ChunkSize
if ChunkX+1<=Voxel_WorldSize.X then Voxel_AddChunktoLoadList(ChunkX+1,ChunkZ)
endif
if CubeX=1
if ChunkX-1>=0 then Voxel_AddChunktoLoadList(ChunkX-1,ChunkZ)
endif
if CubeZ=Voxel_ChunkSize
if ChunkZ+1<=Voxel_WorldSize.Z then Voxel_AddChunktoLoadList(ChunkX,ChunkZ+1)
endif
if CubeZ=1
if ChunkZ-1>=0 then Voxel_AddChunktoLoadList(ChunkX,ChunkZ-1)
endif
endfunction
function Voxel_RemoveCubeFromObject(World ref as WorldData,X,Y,Z)
X=Core_Clamp(X,1,World.TerrainWidth-1)
Y=Core_Clamp(Y,1,World.TerrainHeight-1)
Z=Core_Clamp(Z,1,World.TerrainDepth-1)
if Y=World.Height[X,Z]
Voxel_UpdateHeightAndSunlight(World,X,Y,Z,15)
elseif Y<World.Height[X,Z]
TempHeight=World.Height[X,Z]
Voxel_UpdateHeightAndSunlight(World,X,Y,Z,Voxel_AmbientLight)
World.Height[X,Z]=TempHeight
endif
BlockType=World.Terrain[X,Y,Z].BlockType
World.Terrain[X,Y,Z].BlockType=0
Voxel_UpdateBlockShadow(World,X,Y,Z)
ChunkX=trunc((X-1)/Voxel_ChunkSize)
ChunkZ=trunc((Z-1)/Voxel_ChunkSize)
Voxel_AddChunktoLoadList(ChunkX,ChunkZ)
CubeX=1+Mod(X-1,Voxel_ChunkSize)
CubeZ=1+Mod(Z-1,Voxel_ChunkSize)
if CubeX=Voxel_ChunkSize
if ChunkX+1<=Voxel_WorldSize.X then Voxel_AddChunktoLoadList(ChunkX+1,ChunkZ)
endif
if CubeX=1
if ChunkX-1>=0 then Voxel_AddChunktoLoadList(ChunkX-1,ChunkZ)
endif
if CubeZ=Voxel_ChunkSize
if ChunkZ+1<=Voxel_WorldSize.Z then Voxel_AddChunktoLoadList(ChunkX,ChunkZ+1)
endif
if CubeZ=1
if ChunkZ-1>=0 then Voxel_AddChunktoLoadList(ChunkX,ChunkZ-1)
endif
endfunction BlockType
function Voxel_RemoveCubeListFromObject(World ref as WorldData,CubeList as Int3Data[])
for index=0 to CubeList.length
X=Core_Clamp(CubeList[index].X,1,World.TerrainWidth-1)
Y=Core_Clamp(CubeList[index].Y,1,World.TerrainHeight-1)
Z=Core_Clamp(CubeList[index].Z,1,World.TerrainDepth-1)
if Y=World.Height[X,Z] then Voxel_UpdateHeightAndSunlight(World,X,Y,Z,15)
World.Terrain[X,Y,Z].BlockType=0
ChunkX=trunc((X-1)/Voxel_ChunkSize)
ChunkZ=trunc((Z-1)/Voxel_ChunkSize)
Voxel_AddChunktoLoadList(ChunkX,ChunkZ)
CubeX=1+Mod(X-1,Voxel_ChunkSize)
CubeZ=1+Mod(Z-1,Voxel_ChunkSize)
if CubeX=Voxel_ChunkSize
if ChunkX+1<=Voxel_WorldSize.X then Voxel_AddChunktoLoadList(ChunkX+1,ChunkZ)
endif
if CubeX=1
if ChunkX-1>=0 then Voxel_AddChunktoLoadList(ChunkX-1,ChunkZ)
endif
if CubeZ=Voxel_ChunkSize
if ChunkZ+1<=Voxel_WorldSize.Z then Voxel_AddChunktoLoadList(ChunkX,ChunkZ+1)
endif
if CubeZ=1
if ChunkZ-1>=0 then Voxel_AddChunktoLoadList(ChunkX,ChunkZ-1)
endif
next index
endfunction
function Voxel_GenerateCubeFaces(Object ref as ObjectData,Faceimages ref as FaceimageData,World ref as WorldData,X,Y,Z)
if World.Terrain[X,Y,Z].BlockType>0
Index=World.Terrain[X,Y,Z].BlockType-1
local LightValue as integer
Voxel_TempSubimages[0]=Faceimages.Subimages[Faceimages.FaceimageIndices[Index].FrontID]
Voxel_TempSubimages[1]=Faceimages.Subimages[Faceimages.FaceimageIndices[Index].BackID]
Voxel_TempSubimages[2]=Faceimages.Subimages[Faceimages.FaceimageIndices[Index].RightID]
Voxel_TempSubimages[3]=Faceimages.Subimages[Faceimages.FaceimageIndices[Index].LeftID]
Voxel_TempSubimages[4]=Faceimages.Subimages[Faceimages.FaceimageIndices[Index].UpID]
Voxel_TempSubimages[5]=Faceimages.Subimages[Faceimages.FaceimageIndices[Index].DownID]
CubeX=1+Mod(X-1,Voxel_ChunkSize)
CubeY=Y
CubeZ=1+Mod(Z-1,Voxel_ChunkSize)
if World.Terrain[X,Y,Z+1].BlockType=0
side1=(World.Terrain[X,Y+1,Z+1].BlockType=0)
side2=(World.Terrain[X-1,Y,Z+1].BlockType=0)
corner=(World.Terrain[X-1,Y+1,Z+1].BlockType=0)
AO0=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
side1=(World.Terrain[X,Y+1,Z+1].BlockType=0)
side2=(World.Terrain[X+1,Y,Z+1].BlockType=0)
corner=(World.Terrain[X+1,Y+1,Z+1].BlockType=0)
AO1=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
side1=(World.Terrain[X,Y-1,Z+1].BlockType=0)
side2=(World.Terrain[X+1,Y,Z+1].BlockType=0)
corner=(World.Terrain[X+1,Y-1,Z+1].BlockType=0)
AO2=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
side1=(World.Terrain[X,Y-1,Z+1].BlockType=0)
side2=(World.Terrain[X-1,Y,Z+1].BlockType=0)
corner=(World.Terrain[X-1,Y-1,Z+1].BlockType=0)
AO3=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
if AO0+AO2>AO1+AO3 then Flipped=1
LightValue=Core_Max(World.Terrain[X,Y,Z+1].BlockLight,World.Terrain[X,Y,Z+1].SunLight)/15.0*255
AO0=LightValue-AO0
AO1=LightValue-AO1
AO2=LightValue-AO2
AO3=LightValue-AO3
Voxel_AddFaceToObject(Object,Voxel_TempSubimages[0],CubeX,CubeY,CubeZ,FaceFront,AO0,AO1,AO2,AO3,Flipped)
endif
if World.Terrain[X,Y,Z-1].BlockType=0
side1=(World.Terrain[X,Y+1,Z-1].BlockType=0)
side2=(World.Terrain[X+1,Y,Z-1].BlockType=0)
corner=(World.Terrain[X+1,Y+1,Z-1].BlockType=0)
AO0=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
side1=(World.Terrain[X,Y+1,Z-1].BlockType=0)
side2=(World.Terrain[X-1,Y,Z-1].BlockType=0)
corner=(World.Terrain[X-1,Y+1,Z-1].BlockType=0)
AO1=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
side1=(World.Terrain[X,Y-1,Z-1].BlockType=0)
side2=(World.Terrain[X-1,Y,Z-1].BlockType=0)
corner=(World.Terrain[X-1,Y-1,Z-1].BlockType=0)
AO2=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
side1=(World.Terrain[X,Y-1,Z-1].BlockType=0)
side2=(World.Terrain[X+1,Y,Z-1].BlockType=0)
corner=(World.Terrain[X+1,Y-1,Z-1].BlockType=0)
AO3=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
if AO0+AO2>AO1+AO3 then Flipped=1
LightValue=Core_Max(World.Terrain[X,Y,Z-1].BlockLight,World.Terrain[X,Y,Z-1].SunLight)/15.0*255
AO0=LightValue-AO0
AO1=LightValue-AO1
AO2=LightValue-AO2
AO3=LightValue-AO3
Voxel_AddFaceToObject(Object,Voxel_TempSubimages[1],CubeX,CubeY,CubeZ,FaceBack,AO0,AO1,AO2,AO3,Flipped)
endif
if World.Terrain[X+1,Y,Z].BlockType=0
side1=(World.Terrain[X+1,Y+1,Z].BlockType=0)
side2=(World.Terrain[X+1,Y,Z+1].BlockType=0)
corner=(World.Terrain[X+1,Y+1,Z+1].BlockType=0)
AO0=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
side1=(World.Terrain[X+1,Y+1,Z].BlockType=0)
side2=(World.Terrain[X+1,Y,Z-1].BlockType=0)
corner=(World.Terrain[X+1,Y+1,Z-1].BlockType=0)
AO1=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
side1=(World.Terrain[X+1,Y-1,Z].BlockType=0)
side2=(World.Terrain[X+1,Y,Z-1].BlockType=0)
corner=(World.Terrain[X+1,Y-1,Z-1].BlockType=0)
AO2=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
side1=(World.Terrain[X+1,Y-1,Z].BlockType=0)
side2=(World.Terrain[X+1,Y,Z+1].BlockType=0)
corner=(World.Terrain[X+1,Y-1,Z+1].BlockType=0)
AO3=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
if AO0+AO2>AO1+AO3 then Flipped=1
LightValue=Core_Max(World.Terrain[X+1,Y,Z].BlockLight,World.Terrain[X+1,Y,Z].SunLight)/15.0*255
AO0=LightValue-AO0
AO1=LightValue-AO1
AO2=LightValue-AO2
AO3=LightValue-AO3
Voxel_AddFaceToObject(Object,Voxel_TempSubimages[2],CubeX,CubeY,CubeZ,FaceRight,AO0,AO1,AO2,AO3,Flipped)
endif
if World.Terrain[X-1,Y,Z].BlockType=0
side1=(World.Terrain[X-1,Y+1,Z].BlockType=0)
side2=(World.Terrain[X-1,Y,Z-1].BlockType=0)
corner=(World.Terrain[X-1,Y+1,Z-1].BlockType=0)
AO0=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
side1=(World.Terrain[X-1,Y+1,Z].BlockType=0)
side2=(World.Terrain[X-1,Y,Z+1].BlockType=0)
corner=(World.Terrain[X-1,Y+1,Z+1].BlockType=0)
AO1=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
side1=(World.Terrain[X-1,Y-1,Z].BlockType=0)
side2=(World.Terrain[X-1,Y,Z+1].BlockType=0)
corner=(World.Terrain[X-1,Y-1,Z+1].BlockType=0)
AO2=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
side1=(World.Terrain[X-1,Y-1,Z].BlockType=0)
side2=(World.Terrain[X-1,Y,Z-1].BlockType=0)
corner=(World.Terrain[X-1,Y-1,Z-1].BlockType=0)
AO3=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
if AO0+AO2>AO1+AO3 then Flipped=1
LightValue=Core_Max(World.Terrain[X-1,Y,Z].BlockLight,World.Terrain[X-1,Y,Z].SunLight)/15.0*255
AO0=LightValue-AO0
AO1=LightValue-AO1
AO2=LightValue-AO2
AO3=LightValue-AO3
Voxel_AddFaceToObject(Object,Voxel_TempSubimages[3],CubeX,CubeY,CubeZ,FaceLeft,AO0,AO1,AO2,AO3,Flipped)
endif
if World.Terrain[X,Y+1,Z].BlockType=0
side1=(World.Terrain[X,Y+1,Z+1].BlockType=0)
side2=(World.Terrain[X+1,Y+1,Z].BlockType=0)
corner=(World.Terrain[X+1,Y+1,Z+1].BlockType=0)
AO0=Voxel_GetVertexAO(side1,side2,corner)/3.0*255
side1=(World.Terrain[X,Y+1,Z+1].BlockType=0)
side2=(World.Terrain[X-1,Y+1,Z].BlockType=0)
corner=(World.Terrain[X-1,Y+1,Z+1].BlockType=0)
AO1=Voxel_GetVertexAO(side1,side2,corner)/3.0*255