forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk.worker.js
More file actions
168 lines (159 loc) · 4.15 KB
/
chunk.worker.js
File metadata and controls
168 lines (159 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* eslint-env worker */
import { ChunkTerrain } from './ChunkTerrain.js'
import { ChunkMesher } from './ChunkMesher.js'
import raf from 'raf'
import vec3 from 'vec3'
self.requestAnimationFrame = raf
let terrain = null
class TerrainManager {
constructor (data) {
this.chunkTerrain = new ChunkTerrain({
blocksDef: data.blocksDef
})
this.chunkMesher = new ChunkMesher({
blocksTex: data.blocksTex,
blocksMapping: data.blocksMapping,
toxelSize: data.toxelSize,
chunkTerrain: this.chunkTerrain
})
this.neighbours = [
[-1, 0, 0],
[1, 0, 0],
[0, -1, 0],
[0, 1, 0],
[0, 0, -1],
[0, 0, 1]
]
this.chunkNeedsUpdate = {}
this.generatedChunks = {}
this.renderRadius = 10
this.playerChunk = [0, 0, 0]
this.loop()
}
distance (chunkId) {
const data = this.chunkTerrain.strToVec(chunkId)
const chunk = vec3(...data)
const chunkP = vec3(...this.playerChunk)
return chunkP.distanceTo(chunk)
}
setVoxel (data) {
this.chunkTerrain.setVoxel(...data)
const chunkId = this.chunkTerrain.vecToStr(
...terrain.chunkTerrain.computeChunkForVoxel(
data[0],
data[1],
data[2]
)
)
this.chunkNeedsUpdate[chunkId] = true
for (let l = 0; l < this.neighbours.length; l++) {
const nei = this.neighbours[l]
const neiChunkId = this.chunkTerrain.vecToStr(
...this.chunkTerrain.computeChunkForVoxel(
data[0] + nei[0],
data[1] + nei[1],
data[2] + nei[2]
)
)
this.chunkNeedsUpdate[neiChunkId] = true
}
}
setChunk (data) {
this.chunkTerrain.setChunk(data[0], data[1], data[2], data[3])
const chunkId = terrain.chunkTerrain.vecToStr(
data[0],
data[1],
data[2]
)
this.chunkNeedsUpdate[chunkId] = true
for (let l = 0; l < this.neighbours.length; l++) {
const nei = this.neighbours[l]
const neiChunkId = this.chunkTerrain.vecToStr(
data[0] + nei[0],
data[1] + nei[1],
data[2] + nei[2]
)
this.chunkNeedsUpdate[neiChunkId] = true
}
}
genNearestChunk () {
let nearestChunkId = ''
let nearestDistance = -1
let isNearest = false
for (const chunkId in this.chunkNeedsUpdate) {
const dist = this.distance(chunkId)
if (
(nearestDistance === -1 || nearestDistance > dist) &&
dist <= this.renderRadius
) {
isNearest = true
nearestDistance = dist
nearestChunkId = chunkId
}
}
if (isNearest) {
const data = this.chunkTerrain.strToVec(nearestChunkId)
this.generatedChunks[nearestChunkId] = true
postMessage({
type: 'cellGeo',
data: {
cell: this.chunkMesher.genChunkGeo(...data),
info: data,
p: performance.now()
}
})
delete this.chunkNeedsUpdate[nearestChunkId]
}
}
removeChunks () {
for (const chunkId in this.generatedChunks) {
const dist = this.distance(chunkId)
if (dist > this.renderRadius) {
delete this.generatedChunks[chunkId]
this.chunkNeedsUpdate[chunkId] = true
postMessage({
type: 'removeCell',
data: chunkId
})
}
}
}
loop () {
this.removeChunks()
this.genNearestChunk()
self.requestAnimationFrame(() => {
this.loop()
})
}
updateChunksAroundPlayer (data) {
this.playerChunk = data[0]
this.renderRadius = data[1]
}
}
addEventListener('message', function (e) {
const type = e.data.type
const data = e.data.data
switch (type) {
case 'init':
// FIXME: This is a hack to avoid reinstantiating terrain because `init` seems to be called twice
if (terrain) return
terrain = new TerrainManager(data)
break
case 'setVoxel':
terrain.setVoxel(data)
break
case 'genChunkGeo':
terrain.genChunkGeo(data)
break
case 'setChunk':
terrain.setChunk(data)
break
case 'resetWorld':
console.log('RESET WORLD!')
terrain.chunkTerrain.reset()
break
case 'updateChunksAroundPlayer':
terrain.updateChunksAroundPlayer(data)
break
}
})