22console .log " SECTIONS WORKER STARTED!"
33
44class BitArray
5- constructor : (options )->
6- if options is null
7- return
8- if not options .bitsPerValue > 0
9- console .error ' bits per value must at least 1'
10- if not (options .bitsPerValue <= 32 )
11- console .error ' bits per value exceeds 32'
12- valuesPerLong = Math .floor 64 / options .bitsPerValue
13- length = Math .ceil (options .capacity / valuesPerLong)
14- if not options .data
15- options .data = Array (length * 2 ).fill (0 )
16- valueMask = (1 << options .bitsPerValue ) - 1
5+ constructor : (options )->
6+ if options is null
7+ return
8+ if not options .bitsPerValue > 0
9+ console .error ' bits per value must at least 1'
10+ if not (options .bitsPerValue <= 32 )
11+ console .error ' bits per value exceeds 32'
12+ valuesPerLong = Math .floor 64 / options .bitsPerValue
13+ length = Math .ceil (options .capacity / valuesPerLong)
14+ if not options .data
15+ options .data = Array (length * 2 ).fill (0 )
16+ valueMask = (1 << options .bitsPerValue ) - 1
1717
18- @data = options .data
19- @capacity = options .capacity
20- @bitsPerValue = options .bitsPerValue
21- @valuesPerLong = valuesPerLong
22- @valueMask = valueMask
23- return
24- get : (index )->
25- if not (index >= 0 && index < @capacity )
26- console .error ' index is out of bounds'
27- startLongIndex = Math .floor index / @valuesPerLong
28- indexInLong = (index - startLongIndex * @valuesPerLong ) * @bitsPerValue
29- if indexInLong >= 32
30- indexInStartLong = indexInLong - 32
31- startLong = @data [startLongIndex * 2 + 1 ]
32- return (startLong >>> indexInStartLong) & @valueMask
33- startLong = @data [startLongIndex * 2 ]
34- indexInStartLong = indexInLong
35- result = startLong >>> indexInStartLong
36- endBitOffset = indexInStartLong + @bitsPerValue
37- if endBitOffset > 32
38- endLong = @data [startLongIndex * 2 + 1 ]
39- result |= endLong << (32 - indexInStartLong)
40- return result & @valueMask
18+ @data = options .data
19+ @capacity = options .capacity
20+ @bitsPerValue = options .bitsPerValue
21+ @valuesPerLong = valuesPerLong
22+ @valueMask = valueMask
23+ return
24+ get : (index )->
25+ if not (index >= 0 && index < @capacity )
26+ console .error ' index is out of bounds'
27+ startLongIndex = Math .floor index / @valuesPerLong
28+ indexInLong = (index - startLongIndex * @valuesPerLong ) * @bitsPerValue
29+ if indexInLong >= 32
30+ indexInStartLong = indexInLong - 32
31+ startLong = @data [startLongIndex * 2 + 1 ]
32+ return (startLong >>> indexInStartLong) & @valueMask
33+ startLong = @data [startLongIndex * 2 ]
34+ indexInStartLong = indexInLong
35+ result = startLong >>> indexInStartLong
36+ endBitOffset = indexInStartLong + @bitsPerValue
37+ if endBitOffset > 32
38+ endLong = @data [startLongIndex * 2 + 1 ]
39+ result |= endLong << (32 - indexInStartLong)
40+ return result & @valueMask
4141
4242class ChunkDecoder
43- getBlockIndex : (pos )->
44- return (pos .y << 8 ) | (pos .z << 4 ) | pos .x
45- cvo : (voxelX ,voxelY ,voxelZ ) ->
46- x = voxelX %% 16 | 0
47- y = voxelY %% 16 | 0
48- z = voxelZ %% 16 | 0
49- return y* 16 * 16 + z* 16 + x
50- computeSections : (packet )->
51- sections = packet .sections
52- num = 0
53- result = []
54- for i in sections
55- num += 1
56- if i isnt null
57- solidBlockCount = i .solidBlockCount
58- palette = i .palette
59- data = new BitArray i .data
60- pos = {
61- x : 0
62- y : 0
63- z : 0
64- }
65- cell = new Uint32Array 16 * 16 * 16
66- for x in [0 .. 15 ]
67- for y in [0 .. 15 ]
68- for z in [0 .. 15 ]
69- cell[@ cvo (x,y,z)]= palette[data .get (@ getBlockIndex ({x,y,z}))]
70- result .push {
71- x : packet .x
72- y : num
73- z : packet .z
74- cell
75- }
76- else
77- result .push (null )
78- return result
43+ getBlockIndex : (pos )->
44+ return (pos .y << 8 ) | (pos .z << 4 ) | pos .x
45+ cvo : (voxelX ,voxelY ,voxelZ ) ->
46+ x = voxelX %% 16 | 0
47+ y = voxelY %% 16 | 0
48+ z = voxelZ %% 16 | 0
49+ return y* 16 * 16 + z* 16 + x
50+ computeSections : (packet )->
51+ sections = packet .sections
52+ num = 0
53+ result = []
54+ for i in sections
55+ num += 1
56+ if i isnt null
57+ solidBlockCount = i .solidBlockCount
58+ palette = i .palette
59+ data = new BitArray i .data
60+ pos = {
61+ x : 0
62+ y : 0
63+ z : 0
64+ }
65+ cell = new Uint32Array 16 * 16 * 16
66+ for x in [0 .. 15 ]
67+ for y in [0 .. 15 ]
68+ for z in [0 .. 15 ]
69+ cell[@ cvo (x,y,z)]= palette[data .get (@ getBlockIndex ({x,y,z}))]
70+ result .push {
71+ x : packet .x
72+ y : num
73+ z : packet .z
74+ cell
75+ }
76+ else
77+ result .push (null )
78+ return result
7979
8080addEventListener " message" , (e )->
8181 fn = handlers[e .data .type ]
@@ -87,8 +87,8 @@ addEventListener "message", (e)->
8787cd = new ChunkDecoder
8888
8989handlers = {
90- computeSections : (data )->
91- postMessage {
92- result : cd .computeSections data
93- }
90+ computeSections : (data )->
91+ postMessage {
92+ result : cd .computeSections data
93+ }
9494}
0 commit comments