Skip to content

Commit 5bd49b2

Browse files
committed
Faster Chunk Loading and waiting for spawn
1 parent 5442ffc commit 5bd49b2

File tree

12 files changed

+101
-88
lines changed

12 files changed

+101
-88
lines changed

coffee/client/module/World/BlockGeo.coffee

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,42 +35,36 @@ class BlockGeo
3535
pos:[-0.5+pos[0], -0.5+pos[1], 0.5+pos[2],0.5+pos[0], -0.5+pos[1], 0.5+pos[2],-0.5+pos[0], 0.5+pos[1], 0.5+pos[2],-0.5+pos[0], 0.5+pos[1], 0.5+pos[2],0.5+pos[0], -0.5+pos[1], 0.5+pos[2],0.5+pos[0], 0.5+pos[1], 0.5+pos[2]]
3636
norm:[0, 0, 1,0, 0, 1,0, 0, 1,0, 0, 1,0, 0, 1,0, 0, 1]
3737
uv:[uv[0]...,uv[2]...,uv[1]...,uv[1]...,uv[2]...,uv[3]...]
38-
color:[]
3938
}
4039
when "nx"
4140
return {
4241
pos:[ 0.5+pos[0], -0.5+pos[1], 0.5+pos[2], 0.5+pos[0], -0.5+pos[1], -0.5+pos[2],0.5+pos[0], 0.5+pos[1], 0.5+pos[2], 0.5+pos[0], 0.5+pos[1], 0.5+pos[2],0.5+pos[0], -0.5+pos[1], -0.5+pos[2], 0.5+pos[0], 0.5+pos[1], -0.5+pos[2]]
4342
norm:[ 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]
4443
uv:[uv[0]...,uv[2]...,uv[1]...,uv[1]...,uv[2]...,uv[3]...]
45-
color:[]
4644
}
4745
when "nz"
4846
return {
4947
pos:[ 0.5+pos[0], -0.5+pos[1], -0.5+pos[2],-0.5+pos[0], -0.5+pos[1], -0.5+pos[2],0.5+pos[0], 0.5+pos[1], -0.5+pos[2], 0.5+pos[0], 0.5+pos[1], -0.5+pos[2],-0.5+pos[0], -0.5+pos[1], -0.5+pos[2],-0.5+pos[0], 0.5+pos[1], -0.5+pos[2]]
5048
norm:[ 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1]
5149
uv:[uv[0]...,uv[2]...,uv[1]...,uv[1]...,uv[2]...,uv[3]...]
52-
color:[]
5350
}
5451
when "px"
5552
return {
5653
pos:[-0.5+pos[0], -0.5+pos[1], -0.5+pos[2],-0.5+pos[0], -0.5+pos[1], 0.5+pos[2],-0.5+pos[0], 0.5+pos[1], -0.5+pos[2],-0.5+pos[0], 0.5+pos[1], -0.5+pos[2],-0.5+pos[0], -0.5+pos[1], 0.5+pos[2],-0.5+pos[0], 0.5+pos[1], 0.5+pos[2]]
5754
norm:[-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0]
5855
uv:[uv[0]...,uv[2]...,uv[1]...,uv[1]...,uv[2]...,uv[3]...]
59-
color:[]
6056
}
6157
when "py"
6258
return {
6359
pos:[ 0.5+pos[0], 0.5+pos[1], -0.5+pos[2],-0.5+pos[0], 0.5+pos[1], -0.5+pos[2],0.5+pos[0], 0.5+pos[1], 0.5+pos[2], 0.5+pos[0], 0.5+pos[1], 0.5+pos[2],-0.5+pos[0], 0.5+pos[1], -0.5+pos[2],-0.5+pos[0], 0.5+pos[1], 0.5+pos[2]]
6460
norm:[ 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0]
6561
uv:[uv[0]...,uv[2]...,uv[1]...,uv[1]...,uv[2]...,uv[3]...]
66-
color:[]
6762
}
6863
when "ny"
6964
return {
7065
pos:[ 0.5+pos[0], -0.5+pos[1], 0.5+pos[2],-0.5+pos[0], -0.5+pos[1], 0.5+pos[2],0.5+pos[0], -0.5+pos[1], -0.5+pos[2], 0.5+pos[0], -0.5+pos[1], -0.5+pos[2],-0.5+pos[0], -0.5+pos[1], 0.5+pos[2],-0.5+pos[0], -0.5+pos[1], -0.5+pos[2]]
7166
norm:[ 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0]
7267
uv:[uv[0]...,uv[2]...,uv[1]...,uv[1]...,uv[2]...,uv[3]...]
73-
color:[]
7468
}
7569

7670
export {BlockGeo}

coffee/client/module/World/CellTerrain.coffee

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ class CellTerrain
4747
return @cells[@vec3(x,y,z)]
4848
setCell:(cellX,cellY,cellZ,buffer)->
4949
@cells[@vec3(cellX,cellY,cellZ)]=buffer
50-
getBlock:(blockX,blockY,blockZ)->
51-
if @loadedBlocks[@getVoxel(blockX,blockY,blockZ)] is undefined
52-
@loadedBlocks[@getVoxel(blockX,blockY,blockZ)]=new Block.fromStateId @getVoxel(blockX,blockY,blockZ), @getBlockBiome(blockX,blockY,blockZ)
53-
return @loadedBlocks[@getVoxel(blockX,blockY,blockZ)]
5450
setBiome:(cellX,cellY,cellZ,biome)->
5551
@biomes[@vec3(cellX,cellY,cellZ)]=biome
5652
getBlockBiome:(blockX,blockY,blockZ)->
@@ -59,4 +55,8 @@ class CellTerrain
5955
return null
6056
voff=@computeVoxelOffset blockX,blockY,blockZ
6157
return biome[voff]
58+
getBlock:(blockX,blockY,blockZ)->
59+
if @loadedBlocks[@getVoxel(blockX,blockY,blockZ)] is undefined
60+
@loadedBlocks[@getVoxel(blockX,blockY,blockZ)]=new Block.fromStateId @getVoxel(blockX,blockY,blockZ), @getBlockBiome(blockX,blockY,blockZ)
61+
return @loadedBlocks[@getVoxel(blockX,blockY,blockZ)]
6262
export {CellTerrain}

coffee/client/module/World/ChunkWorker.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class TerrainManager
2424
positions.push faceVertex.pos...
2525
normals.push faceVertex.norm...
2626
uvs.push faceVertex.uv...
27-
colors.push(0.5,0.5,0.5,0.5,0.5,0.5,1,1,1,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5)
27+
colors.push(0.5,0.5,0.5,0.5,0.5,0.5,1,1,1,1,1,1,0.5,0.5,0.5,0.5,0.5,0.5)
2828
return
2929
for i in [0..@cellSize-1]
3030
for j in [0..@cellSize-1]

coffee/client/module/World/World.coffee

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class World
1616
@cellTerrain=new CellTerrain {cellSize:@cellSize}
1717
@ATA=new AnimatedTextureAtlas {al:@al}
1818
@material=@ATA.material
19-
@material2=@ATA.material2
19+
@cellUpdateTime=null
2020
@neighbours=[[-1, 0, 0],[1, 0, 0],[0, -1, 0],[0, 1, 0],[0, 0, -1],[0, 0, 1]]
2121
@chunkWorker=new Worker "/module/World/ChunkWorker.js", {type:'module'}
2222
@chunkWorker.onmessage=(message)->
@@ -65,18 +65,24 @@ class World
6565
getBlock: (voxelX,voxelY,voxelZ) ->
6666
return @cellTerrain.getVoxel voxelX,voxelY,voxelZ
6767
updateCellsAroundPlayer: (pos,radius)->
68-
for k,v of @cellMesh
69-
v.visible=false
70-
cell=@cellTerrain.computeCellForVoxel (Math.floor pos.x),(Math.floor pos.y),(Math.floor pos.z)
71-
for i in [-radius..radius]
72-
for j in [-radius..radius]
73-
for k in [-radius..radius]
74-
pcell=[cell[0]+i,cell[1]+j,cell[2]+k]
75-
if @cellMesh[@cellTerrain.vec3(pcell...)]
76-
@cellMesh[@cellTerrain.vec3(pcell...)].visible=true
77-
if @cellNeedsUpdate[@cellTerrain.vec3(pcell...)]
78-
@_genCellGeo pcell...
79-
delete @cellNeedsUpdate[@cellTerrain.vec3(pcell...)]
68+
_this=@
69+
if @cellUpdateTime isnt null and (performance.now()-@cellUpdateTime>1000)
70+
for k,v of @cellMesh
71+
v.visible=false
72+
cell=@cellTerrain.computeCellForVoxel (Math.floor pos.x),(Math.floor pos.y),(Math.floor pos.z)
73+
up=(x,y,z)->
74+
pcell=[cell[0]+x,cell[1]+y,cell[2]+z]
75+
if _this.cellMesh[_this.cellTerrain.vec3(pcell...)]
76+
_this.cellMesh[_this.cellTerrain.vec3(pcell...)].visible=true
77+
if _this.cellNeedsUpdate[_this.cellTerrain.vec3(pcell...)]
78+
_this._genCellGeo pcell...
79+
delete _this.cellNeedsUpdate[_this.cellTerrain.vec3(pcell...)]
80+
return
81+
up(0,0,0)
82+
for i in [-radius..radius]
83+
for j in [-radius..radius]
84+
for k in [-radius..radius]
85+
up i,j,k
8086
updateCell: (data)->
8187
cellId=@cellTerrain.vec3 data.info...
8288
cell=data.cell
@@ -87,7 +93,6 @@ class World
8793
geometry.setAttribute 'uv',new THREE.BufferAttribute(new Float32Array(cell.uvs), 2)
8894
geometry.setAttribute 'color',new THREE.BufferAttribute(new Float32Array(cell.colors), 3)
8995
if mesh is undefined
90-
console.log geometry
9196
@cellMesh[cellId]=new THREE.Mesh geometry,@material
9297
@scene.add @cellMesh[cellId]
9398
else
@@ -177,6 +182,7 @@ class World
177182
else
178183
return false
179184
_setCell: (cellX,cellY,cellZ,buffer,biome)->
185+
@cellUpdateTime=performance.now()
180186
@chunkWorker.postMessage {
181187
type:"setCell"
182188
data:[cellX,cellY,cellZ,buffer,biome]

coffee/client/module/index.coffee

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {AssetLoader} from './AssetLoader.js'
1010
import {InventoryBar} from './InventoryBar.js'
1111
import {RandomNick} from './RandomNick.js'
1212
import {GUI} from './jsm/libs/dat.gui.module.js'
13+
1314
init = ()->
1415
canvas=document.querySelector '#c'
1516
renderer=new THREE.WebGLRenderer {
@@ -54,7 +55,7 @@ init = ()->
5455
socket=io.connect "#{al.get("host")}:#{al.get("websocket-port")}"
5556
socket.on "connect",()->
5657
console.log "Połączono z serverem!"
57-
$('.loadingText').text "Wczytywanie terenu..."
58+
$('.loadingText').text "Za chwilę dołączysz do gry..."
5859
nick=document.location.hash.substring(1,document.location.hash.length)
5960
if nick is ""
6061
nick=RandomNick()
@@ -63,12 +64,13 @@ init = ()->
6364
socket.emit "initClient", {
6465
nick:nick
6566
}
66-
# world.replaceWorld v
67-
$(".initLoading").css "display","none"
6867
return
6968
socket.on "blockUpdate",(block)->
7069
world.setBlock block[0],block[1]+16,block[2],block[3]
7170
return
71+
socket.on "spawn", (sections,x,z,biomes)->
72+
console.log "Gracz dołączył do gry!"
73+
$(".initLoading").css "display","none"
7274
socket.on "mapChunk", (sections,x,z,biomes)->
7375
world._computeSections sections,x,z,biomes
7476
socket.on "hp",(points)->
@@ -158,7 +160,7 @@ render = ->
158160
else
159161
cursor.visible=false
160162

161-
world.updateCellsAroundPlayer camera.position,3
163+
world.updateCellsAroundPlayer camera.position,5
162164
TWEEN.update();
163165
renderer.render scene, camera
164166
return

coffee/server.coffee

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ module.exports=(config)->
6161
io.to(socket.id).emit "hp",socketInfo[socket.id].bot.health
6262
io.to(socket.id).emit "food",socketInfo[socket.id].bot.food
6363
return
64+
socketInfo[socket.id].bot.on 'spawn',()->
65+
try
66+
io.to(socket.id).emit "spawn"
67+
return
6468
socketInfo[socket.id].bot.on 'message',(msg)->
6569
try
6670
io.to(socket.id).emit "msg",convert.toHtml(msg.toAnsi());

src/client/module/World/BlockGeo.js

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

src/client/module/World/CellTerrain.js

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

src/client/module/World/ChunkWorker.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.

src/client/module/World/World.js

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

0 commit comments

Comments
 (0)