Skip to content

Commit 53fd428

Browse files
committed
TerrainWorker in Terrain
1 parent 2912a03 commit 53fd428

File tree

9 files changed

+101
-120
lines changed

9 files changed

+101
-120
lines changed

coffee/client/index.coffee

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Bundle.js
1+
22
scene=null;materials=null;parameters=null;canvas=null;renderer=null;camera=null;terrain=null;cursor=null;FPC=null;socket=null;stats=null;worker=null;playerObject=null;inv_bar=null
33
import * as THREE from './module/build/three.module.js'
44
import {SkeletonUtils} from './module/jsm/utils/SkeletonUtils.js'
@@ -12,45 +12,9 @@ import {AnimatedTextureAtlas} from './module/AnimatedTextureAtlas.js'
1212
import {Players} from './module/Players.js'
1313
import {RandomNick} from './module/RandomNick.js'
1414

15-
class TerrainWorker
16-
constructor: (options)->
17-
@worker=new Worker "module/TerrainWorker.js", {type:'module'}
18-
@worker.onmessage=(message)->
19-
terrain.updateCell message.data
20-
@worker.postMessage {
21-
type:'init'
22-
data:{
23-
models:{
24-
anvil:{
25-
al.get("anvil").children[0].geometry.attributes...
26-
}
27-
}
28-
blocks: al.get "blocks"
29-
blocksMapping: al.get "blocksMapping"
30-
toxelSize: 27
31-
cellSize: 16
32-
}
33-
}
34-
setVoxel: (voxelX,voxelY,voxelZ,value)->
35-
@worker.postMessage {
36-
type:"setVoxel"
37-
data:[voxelX,voxelY,voxelZ,value]
38-
}
39-
genCellGeo: (cellX,cellY,cellZ)->
40-
cellX=parseInt cellX
41-
cellY=parseInt cellY
42-
cellZ=parseInt cellZ
43-
@worker.postMessage {
44-
type:"genCellGeo"
45-
data:[cellX,cellY,cellZ]
46-
}
47-
4815
init = ()->
4916

50-
#Terrain worker
51-
worker=new TerrainWorker
52-
53-
chunkWorker=new Worker "module/ChunkWorker.js", {type:'module'}
17+
chunkWorker=new Worker "./module/ChunkWorker.js", {type:'module'}
5418

5519
#canvas,renderer,camera,lights
5620
canvas=document.querySelector '#c'
@@ -89,12 +53,10 @@ init = ()->
8953
terrain=new Terrain({
9054
toxelSize:27
9155
cellSize:16
92-
blocks:al.get "blocks"
93-
blocksMapping:al.get "blocksMapping"
9456
material:ATA.material
9557
scene
9658
camera
97-
worker
59+
al
9860
})
9961

10062
#Socket.io setup
@@ -129,7 +91,7 @@ init = ()->
12991
socket.on "firstLoad",(v)->
13092
console.log "Otrzymano pakiet świata!"
13193
terrain.replaceWorld v
132-
worker.genCellGeo(0,0,0)
94+
terrain._genCellGeo(0,0,0)
13395
$(".initLoading").css "display","none"
13496
stats = new Stats();
13597
stats.showPanel(0);

coffee/client/module/ChunkWorker.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ class ChunkDecoder
6262
base=[]
6363
for x in [0..15]
6464
for y in [0..15]
65-
for z in [0..255]
65+
for z in [0..15]
6666
base.push(data.get(@getBlockIndex({x,y,z})))
67-
console.log "Computed chunk section "+packet.x+" "+packet.z+" "+num
67+
console.log "Computed chunk section "+packet.x+" "+packet.z+" "+num, base
6868

6969

7070
addEventListener "message", (e)->

coffee/client/module/Terrain.coffee

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,38 @@ import {CellTerrain} from './CellTerrain.js'
33

44
class Terrain
55
constructor: (options) ->
6-
@cellSize=options.cellSize
7-
@cellTerrain=new CellTerrain {
8-
cellSize:@cellSize
9-
}
106
@cellsData={}
11-
@blocks=options.blocks
12-
@blocksMapping=options.blocksMapping
13-
@material=options.material
147
@cells={}
158
@models={}
9+
@cellSize=options.cellSize
10+
@material=options.material
1611
@camera=options.camera
1712
@scene=options.scene
1813
@toxelSize=options.toxelSize
14+
@al=options.al
15+
@cellTerrain=new CellTerrain {
16+
cellSize:@cellSize
17+
}
1918
@neighbours=[[-1, 0, 0],[1, 0, 0],[0, -1, 0],[0, 1, 0],[0, 0, -1],[0, 0, 1]]
20-
@worker=options.worker
19+
_this=@
20+
@worker=new Worker "./module/TerrainWorker.js", {type:'module'}
21+
console.log @worker
22+
@worker.onmessage=(message)->
23+
_this.updateCell message.data
24+
@worker.postMessage {
25+
type:'init'
26+
data:{
27+
models:{
28+
anvil:{
29+
@al.get("anvil").children[0].geometry.attributes...
30+
}
31+
}
32+
blocks: @al.get "blocks"
33+
blocksMapping: @al.get "blocksMapping"
34+
toxelSize: @toxelSize
35+
cellSize: @cellSize
36+
}
37+
}
2138
computeVoxelOffset: (voxelX,voxelY,voxelZ) ->
2239
x=voxelX %% @cellSize|0
2340
y=voxelY %% @cellSize|0
@@ -34,7 +51,7 @@ class Terrain
3451
z=parseInt z
3552
return "#{x}:#{y}:#{z}"
3653
setVoxel: (voxelX,voxelY,voxelZ,value) ->
37-
@worker.setVoxel voxelX,voxelY,voxelZ,value
54+
@_setVoxel voxelX,voxelY,voxelZ,value
3855
voff=@computeVoxelOffset(voxelX,voxelY,voxelZ)
3956
cell=@computeCellForVoxel(voxelX,voxelY,voxelZ)
4057
cellId=@vec3(cell...)
@@ -67,7 +84,7 @@ class Terrain
6784
_this=@
6885
Object.keys(@cellsData).forEach (id)->
6986
if _this.cellsData[id].needsUpdate
70-
_this.worker.genCellGeo id.split(":")...
87+
_this._genCellGeo id.split(":")...
7188
return
7289
return
7390
updateCell: (data)->
@@ -176,5 +193,17 @@ class Terrain
176193
return {posPlace,posBreak}
177194
else
178195
return false
179-
196+
_setVoxel: (voxelX,voxelY,voxelZ,value)->
197+
@worker.postMessage {
198+
type:"setVoxel"
199+
data:[voxelX,voxelY,voxelZ,value]
200+
}
201+
_genCellGeo: (cellX,cellY,cellZ)->
202+
cellX=parseInt cellX
203+
cellY=parseInt cellY
204+
cellZ=parseInt cellZ
205+
@worker.postMessage {
206+
type:"genCellGeo"
207+
data:[cellX,cellY,cellZ]
208+
}
180209
export {Terrain}

coffee/server.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module.exports=(config)->
7171
return
7272
socket.on "playerUpdate",(data)->
7373
players[socket.id]=data
74-
io.sockets.emit "playerUpdate", data
74+
io.sockets.emit "playerUpdate", players
7575
socket.on "blockUpdate",(block)->
7676
world["#{block[0]}:#{block[1]}:#{block[2]}"]=block[3]
7777
if block[3] is 0

src/client/index.js

Lines changed: 4 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client/module/ChunkWorker.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client/module/Terrain.js

Lines changed: 46 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/savedWorld.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/server.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)