Skip to content

Commit 92fabfb

Browse files
committed
faster chunk parsing
1 parent 53fd428 commit 92fabfb

16 files changed

+121
-819
lines changed

coffee/client/index.coffee

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ init = ()->
1616

1717
chunkWorker=new Worker "./module/ChunkWorker.js", {type:'module'}
1818

19+
chunkWorker.onmessage=(data)->
20+
# console.log "Recieved chunk column", data.data
21+
1922
#canvas,renderer,camera,lights
2023
canvas=document.querySelector '#c'
2124
renderer=new THREE.WebGLRenderer {
@@ -74,7 +77,7 @@ init = ()->
7477
}
7578
return
7679
socket.on "blockUpdate",(block)->
77-
terrain.setVoxel block...
80+
terrain.setBlock block...
7881
return
7982
socket.on "mapChunk", (sections,x,z)->
8083
# console.log("Recieved mapChunk "+x+" "+z)
@@ -89,9 +92,8 @@ init = ()->
8992
players.update data
9093
return
9194
socket.on "firstLoad",(v)->
92-
console.log "Otrzymano pakiet świata!"
95+
console.log "First Load packet recieved!"
9396
terrain.replaceWorld v
94-
terrain._genCellGeo(0,0,0)
9597
$(".initLoading").css "display","none"
9698
stats = new Stats();
9799
stats.showPanel(0);

coffee/client/module/CellTerrain.coffee

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ class CellTerrain
22
constructor: (options)->
33
@cellSize=options.cellSize
44
@cells={}
5-
@neighbours=[[-1, 0, 0],[1, 0, 0],[0, -1, 0],[0, 1, 0],[0, 0, -1],[0, 0, 1]]
65
vec3: (x,y,z)->
76
return "#{x}:#{y}:#{z}"
87
computeVoxelOffset: (voxelX,voxelY,voxelZ) ->
@@ -27,19 +26,19 @@ class CellTerrain
2726
return @cells[cellId]
2827
setVoxel:(voxelX,voxelY,voxelZ,value)->
2928
cell=@getCellForVoxel voxelX,voxelY,voxelZ
30-
if not cell
29+
if not cell
3130
cell=@addCellForVoxel voxelX,voxelY,voxelZ
3231
voff=@computeVoxelOffset voxelX,voxelY,voxelZ
3332
cell[voff]=value
3433
return
3534
getVoxel:(voxelX,voxelY,voxelZ)->
3635
cell=@getCellForVoxel voxelX,voxelY,voxelZ
37-
if not cell
36+
if not cell
3837
return 0
3938
voff=@computeVoxelOffset voxelX,voxelY,voxelZ
4039
return cell[voff]
4140
getBuffer:(x,y,z)->
4241
console.log(@cells[@vec3(x,y,z)])
4342
return
44-
45-
export {CellTerrain}
43+
44+
export {CellTerrain}

coffee/client/module/ChunkWorker.coffee

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class ChunkDecoder
4848
computeSections: (packet)->
4949
sections=packet.sections
5050
num=0
51+
result=[]
5152
for i in sections
5253
num+=1
5354
if i isnt null
@@ -63,8 +64,16 @@ class ChunkDecoder
6364
for x in [0..15]
6465
for y in [0..15]
6566
for z in [0..15]
66-
base.push(data.get(@getBlockIndex({x,y,z})))
67-
console.log "Computed chunk section "+packet.x+" "+packet.z+" "+num, base
67+
base.push(palette[data.get(@getBlockIndex({x,y,z}))])
68+
result.push {
69+
x:packet.x
70+
y:num
71+
z:packet.z,
72+
data:base
73+
}
74+
else
75+
result.push(null)
76+
return result
6877

6978

7079
addEventListener "message", (e)->
@@ -78,5 +87,7 @@ cd=new ChunkDecoder
7887

7988
handlers={
8089
computeSections:(data)->
81-
cd.computeSections data
90+
postMessage {
91+
result:cd.computeSections data
92+
}
8293
}

coffee/client/module/FirstPersonControls.coffee

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ class FirstPersonControls
5656
_this.keys[z.keyCode] = true
5757
#If click escape
5858
if z.keyCode is 27
59-
console.log _this.gameState
6059
if _this.gameState is "menu"
6160
_this.canvas.requestPointerLock()
6261
else
@@ -67,18 +66,15 @@ class FirstPersonControls
6766
delete _this.keys[z.keyCode]
6867
return
6968
$(".gameOn").click ->
70-
console.log "clicked!"
7169
_this.canvas.requestPointerLock()
7270
return
7371
lockChangeAlert=()->
7472
if document.pointerLockElement is _this.canvas or document.mozPointerLockElement is _this.canvas
7573
_this.gameState="game"
7674
$(".gameMenu").css "display", "none"
77-
console.log("GAME")
7875
else
7976
_this.gameState="menu"
8077
$(".gameMenu").css "display", "block"
81-
console.log("MENU")
8278
return
8379
document.addEventListener 'pointerlockchange', lockChangeAlert, false
8480
document.addEventListener 'mozpointerlockchange', lockChangeAlert, false

coffee/client/module/Terrain.coffee

Lines changed: 35 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import {CellTerrain} from './CellTerrain.js'
33

44
class Terrain
55
constructor: (options) ->
6-
@cellsData={}
7-
@cells={}
6+
_this=@
7+
@cellMesh={}
8+
@cellNeedsUpdate={}
89
@models={}
910
@cellSize=options.cellSize
1011
@material=options.material
@@ -16,9 +17,7 @@ class Terrain
1617
cellSize:@cellSize
1718
}
1819
@neighbours=[[-1, 0, 0],[1, 0, 0],[0, -1, 0],[0, 1, 0],[0, 0, -1],[0, 0, 1]]
19-
_this=@
2020
@worker=new Worker "./module/TerrainWorker.js", {type:'module'}
21-
console.log @worker
2221
@worker.onmessage=(message)->
2322
_this.updateCell message.data
2423
@worker.postMessage {
@@ -35,75 +34,42 @@ class Terrain
3534
cellSize: @cellSize
3635
}
3736
}
38-
computeVoxelOffset: (voxelX,voxelY,voxelZ) ->
39-
x=voxelX %% @cellSize|0
40-
y=voxelY %% @cellSize|0
41-
z=voxelZ %% @cellSize|0
42-
return [x,y,z]
43-
computeCellForVoxel: (voxelX,voxelY,voxelZ) ->
44-
cellX = Math.floor voxelX / @cellSize
45-
cellY = Math.floor voxelY / @cellSize
46-
cellZ = Math.floor voxelZ / @cellSize
47-
return [cellX,cellY,cellZ]
48-
vec3: (x,y,z) ->
49-
x=parseInt x
50-
y=parseInt y
51-
z=parseInt z
52-
return "#{x}:#{y}:#{z}"
53-
setVoxel: (voxelX,voxelY,voxelZ,value) ->
54-
@_setVoxel voxelX,voxelY,voxelZ,value
55-
voff=@computeVoxelOffset(voxelX,voxelY,voxelZ)
56-
cell=@computeCellForVoxel(voxelX,voxelY,voxelZ)
57-
cellId=@vec3(cell...)
58-
if @cellsData[cellId] is undefined
59-
@cellsData[cellId]={
60-
[@vec3 voff...]:value
61-
}
62-
else
63-
prevVox=@cellsData[cellId][@vec3(voff...)]
64-
if prevVox isnt value
65-
@cellsData[cellId][@vec3(voff...)]=value
66-
@cellsData[cellId].needsUpdate=true
67-
for nei in @neighbours
68-
neiCellId=@vec3 @computeCellForVoxel(voxelX+nei[0],voxelY+nei[1],voxelZ+nei[2])...
69-
try
70-
@cellsData[neiCellId].needsUpdate=true
71-
@cellsData[cellId].needsUpdate=true
37+
@neighbours=[[-1, 0, 0],[1, 0, 0],[0, -1, 0],[0, 1, 0],[0, 0, -1],[0, 0, 1]]
38+
setBlock: (voxelX,voxelY,voxelZ,value) ->
39+
voxelX=parseInt voxelX
40+
voxelY=parseInt voxelY
41+
voxelZ=parseInt voxelZ
42+
if (@cellTerrain.getVoxel voxelX,voxelY,voxelZ) isnt value
43+
@_setVoxel voxelX,voxelY,voxelZ,value
44+
@cellTerrain.setVoxel voxelX,voxelY,voxelZ,value
45+
cellId=@cellTerrain.vec3 @cellTerrain.computeCellForVoxel(voxelX,voxelY,voxelZ)...
46+
@cellNeedsUpdate[cellId]=true
47+
for nei in @neighbours
48+
neiCellId=@cellTerrain.vec3 @cellTerrain.computeCellForVoxel(voxelX+nei[0],voxelY+nei[1],voxelZ+nei[2])...
49+
@cellNeedsUpdate[neiCellId]=true
7250
return
73-
getVoxel: (voxelX,voxelY,voxelZ) ->
74-
cell=@computeCellForVoxel(voxelX,voxelY,voxelZ)
75-
cellId=@vec3(cell...)
76-
voff=@computeVoxelOffset(voxelX,voxelY,voxelZ)
77-
voxId=@vec3(voff...)
78-
if @cellsData[cellId] isnt undefined
79-
voxel=@cellsData[cellId][voxId]
80-
if voxel isnt undefined
81-
return voxel
82-
return 0;
51+
getBlock: (voxelX,voxelY,voxelZ) ->
52+
return @cellTerrain.getVoxel voxelX,voxelY,voxelZ
8353
updateCells: ->
8454
_this=@
85-
Object.keys(@cellsData).forEach (id)->
86-
if _this.cellsData[id].needsUpdate
55+
Object.keys(@cellNeedsUpdate).forEach (id)->
56+
if _this.cellNeedsUpdate[id]
8757
_this._genCellGeo id.split(":")...
88-
return
58+
delete _this.cellNeedsUpdate[id]
8959
return
9060
updateCell: (data)->
91-
# console.warn "SENDING cell: #{data.info}"
92-
cellId=@vec3 data.info...
61+
cellId=@cellTerrain.vec3 data.info...
9362
cell=data.cell
94-
if @cellsData[cellId]!=undefined
95-
if @cellsData[cellId].needsUpdate
96-
mesh=@cells[cellId]
97-
geometry=new THREE.BufferGeometry;
98-
geometry.setAttribute 'position',new THREE.BufferAttribute(new Float32Array(cell.positions), 3)
99-
geometry.setAttribute 'normal',new THREE.BufferAttribute(new Float32Array(cell.normals), 3)
100-
geometry.setAttribute 'uv',new THREE.BufferAttribute(new Float32Array(cell.uvs), 2)
101-
if mesh is undefined
102-
@cells[cellId]=new THREE.Mesh geometry,@material
103-
@scene.add @cells[cellId]
104-
else
105-
@cells[cellId].geometry=geometry
106-
@cellsData[cellId].needsUpdate=false
63+
mesh=@cellMesh[cellId]
64+
geometry=new THREE.BufferGeometry;
65+
geometry.setAttribute 'position',new THREE.BufferAttribute(new Float32Array(cell.positions), 3)
66+
geometry.setAttribute 'normal',new THREE.BufferAttribute(new Float32Array(cell.normals), 3)
67+
geometry.setAttribute 'uv',new THREE.BufferAttribute(new Float32Array(cell.uvs), 2)
68+
if mesh is undefined
69+
@cellMesh[cellId]=new THREE.Mesh geometry,@material
70+
@scene.add @cellMesh[cellId]
71+
else
72+
@cellMesh[cellId].geometry=geometry
10773
return
10874
intersectsRay: (start,end) ->
10975
start.x+=0.5
@@ -138,7 +104,7 @@ class Terrain
138104
tzMax = if tzDelta < Infinity then tzDelta * zDist else Infinity
139105
steppedIndex = -1
140106
while t <= len
141-
voxel = @getVoxel ix, iy, iz
107+
voxel = @getBlock ix, iy, iz
142108
if voxel
143109
return {
144110
position: [
@@ -179,8 +145,8 @@ class Terrain
179145
replaceWorld: (voxels)->
180146
_this=@
181147
Object.keys(voxels).forEach (id)->
182-
if voxels[id] isnt _this.getVoxel id.split(":")...
183-
_this.setVoxel id.split(":")...,voxels[id]
148+
if voxels[id] isnt _this.getBlock id.split(":")...
149+
_this.setBlock id.split(":")...,voxels[id]
184150
getRayBlock: ->
185151
start = new THREE.Vector3().setFromMatrixPosition(@camera.matrixWorld)
186152
end = new THREE.Vector3().set(0,0, 1).unproject(@camera)

coffee/client/module/TerrainWorker.coffee

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ terrain=null
168168
handlers={
169169
init:(data)->
170170
State.init=data
171-
console.log(State)
172171
terrain=new TerrainManager {
173172
models:data.models
174173
blocks:data.blocks

coffee/server.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports=(config)->
4141

4242
#Trying to run special functions
4343
socket.on "initClient",(data)->
44-
console.log "NEW: "+socket.id
44+
console.log "[+] "+socket.id
4545
#init socketInfo
4646
socketInfo[socket.id]=data
4747

@@ -79,7 +79,7 @@ module.exports=(config)->
7979
io.sockets.emit "blockUpdate",block
8080
saveWorld()
8181
socket.on "disconnect", ->
82-
console.log "DIS: "+socket.id
82+
console.log "[-] "+socket.id
8383

8484
#end bot session
8585
socketInfo[socket.id].bot.end()

0 commit comments

Comments
 (0)