Skip to content

Commit e0bb0d3

Browse files
committed
remove sections worker and add sectionComputer module
1 parent 1bb81fb commit e0bb0d3

File tree

2 files changed

+96
-17
lines changed

2 files changed

+96
-17
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
class BitArray
3+
constructor: (options)->
4+
if options is null
5+
return
6+
if not options.bitsPerValue > 0
7+
console.error 'bits per value must at least 1'
8+
if not (options.bitsPerValue <= 32)
9+
console.error 'bits per value exceeds 32'
10+
valuesPerLong = Math.floor 64 / options.bitsPerValue
11+
length = Math.ceil(options.capacity / valuesPerLong)
12+
if not options.data
13+
options.data = Array(length * 2).fill(0)
14+
valueMask = (1 << options.bitsPerValue) - 1
15+
16+
@data = options.data
17+
@capacity = options.capacity
18+
@bitsPerValue = options.bitsPerValue
19+
@valuesPerLong = valuesPerLong
20+
@valueMask = valueMask
21+
return
22+
get:(index)->
23+
if not (index >= 0 && index < @capacity)
24+
console.error 'index is out of bounds'
25+
startLongIndex = Math.floor index / @valuesPerLong
26+
indexInLong = (index - startLongIndex * @valuesPerLong) * @bitsPerValue
27+
if indexInLong >= 32
28+
indexInStartLong = indexInLong - 32
29+
startLong = @data[startLongIndex * 2 + 1]
30+
return (startLong >>> indexInStartLong) & @valueMask
31+
startLong = @data[startLongIndex * 2]
32+
indexInStartLong = indexInLong
33+
result = startLong >>> indexInStartLong
34+
endBitOffset = indexInStartLong + @bitsPerValue
35+
if endBitOffset > 32
36+
endLong = @data[startLongIndex * 2 + 1]
37+
result |= endLong << (32 - indexInStartLong)
38+
return result & @valueMask
39+
40+
class ChunkDecoder
41+
getBlockIndex: (pos)->
42+
return (pos.y << 8) | (pos.z << 4) | pos.x
43+
cvo: (voxelX,voxelY,voxelZ) ->
44+
x=voxelX %% 16|0
45+
y=voxelY %% 16|0
46+
z=voxelZ %% 16|0
47+
return y*16*16+z*16+x
48+
computeSections: (packet)->
49+
sections=packet.sections
50+
num=0
51+
result=[]
52+
for i in sections
53+
num+=1
54+
if i isnt null
55+
solidBlockCount=i.solidBlockCount
56+
palette=i.palette
57+
data=new BitArray i.data
58+
pos={
59+
x:0
60+
y:0
61+
z:0
62+
}
63+
cell=new Uint32Array 16*16*16
64+
for x in [0..15]
65+
for y in [0..15]
66+
for z in [0..15]
67+
cell[@cvo(x,y,z)]=palette[data.get(@getBlockIndex({x,y,z}))]
68+
result.push {
69+
x:packet.x
70+
y:num
71+
z:packet.z
72+
cell
73+
}
74+
else
75+
result.push(null)
76+
return result
77+
78+
addEventListener "message", (e)->
79+
fn = handlers[e.data.type]
80+
if not fn
81+
throw new Error('no handler for type: ' + e.data.type)
82+
fn(e.data.data)
83+
return
84+
85+
cd=new ChunkDecoder
86+
87+
SectionComputer=(data)->
88+
return cd.computeSections data
89+
90+
export {SectionComputer}
91+

src/client/scripts/World/World.coffee

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import * as THREE from 'three'
22
import {CellTerrain} from './CellTerrain.coffee'
33
import {AnimatedTextureAtlas} from './AnimatedTextureAtlas.coffee'
4-
4+
import {SectionComputer} from "./SectionComputer.coffee"
55
import chunkWorker from "./chunk.worker.coffee"
6-
import sectionsWorker from "./sections.worker.coffee"
76

87
class World
98
constructor: (game) ->
@@ -42,14 +41,6 @@ class World
4241
blocksDef: @blocksDef
4342
}
4443
}
45-
46-
#Utworzenie Workera do przekształcania bufforów otrzymanych z serwera
47-
@sectionsWorker=new sectionsWorker
48-
@sectionsWorker.onmessage=(data)->
49-
result=data.data.result
50-
for i in result
51-
if i isnt null
52-
_this.setCell i.x,i.y,i.z,i.cell
5344
return
5445
setCell: (cellX,cellY,cellZ,buffer)->
5546
@_setCell cellX,cellY,cellZ,buffer
@@ -223,11 +214,8 @@ class World
223214
}
224215
return
225216
_computeSections: (sections,x,z,biomes)->
226-
#Wysyłanie do SectionsWorkera Buffora, który ma przekształcić w łatwiejszą postać
227-
@sectionsWorker.postMessage {
228-
type:"computeSections"
229-
data:{
230-
sections,x,z,biomes
231-
}
232-
}
217+
result=SectionComputer {sections,x,z,biomes}
218+
for i in result
219+
if i isnt null
220+
@setCell i.x,i.y,i.z,i.cell
233221
export {World}

0 commit comments

Comments
 (0)