forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorld.js
More file actions
337 lines (335 loc) · 11.2 KB
/
World.js
File metadata and controls
337 lines (335 loc) · 11.2 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import * as THREE from "three";
import { CellTerrain } from "./CellTerrain.js";
import { AnimatedTextureAtlas } from "./AnimatedTextureAtlas.js";
import { SectionComputer } from "./SectionComputer.js";
import vec3 from "vec3";
import chunkWorker from "./chunk.worker.js";
/** Class to manage world (chunks,generating terrain,etc.) */
var World = class World {
/**
* World init function
* @param game - Object of main game
*/
constructor(game) {
var _this = this;
this.game = game;
this.cellBlackList = {};
this.cellMesh = {};
this.cellNeedsUpdate = {};
this.blocksDef = this.game.al.get("blocksDef");
this.models = {};
this.cellTerrain = new CellTerrain({
cellSize: this.game.cellSize,
blocksDef: this.blocksDef,
});
this.ATA = new AnimatedTextureAtlas(this.game);
this.material = this.ATA.material;
this.cellUpdateTime = null;
this.renderTime = 100;
this.neighbours = [
[-1, 0, 0],
[1, 0, 0],
[0, -1, 0],
[0, 1, 0],
[0, 0, -1],
[0, 0, 1],
];
this.chunkWorker = new chunkWorker();
this.chunkWorker.onmessage = function (message) {
if (message.data.type === "cellGeo") {
return _this.updateCell(message.data.data);
} else if (message.data.type === "removeCell") {
if (_this.cellMesh[message.data.data] !== void 0) {
_this.cellMesh[message.data.data].geometry.dispose();
_this.game.scene.remove(_this.cellMesh[message.data.data]);
delete _this.cellMesh[message.data.data];
return _this.game.renderer.renderLists.dispose();
}
}
};
this.chunkWorker.postMessage({
type: "init",
data: {
blocksMapping: this.game.al.get("blocksMapping"),
toxelSize: this.game.toxelSize,
cellSize: this.game.cellSize,
blocksTex: this.game.al.get("blocksTex"),
blocksDef: this.blocksDef,
},
});
this.lastPlayerChunk = "";
this.blocksUpdate = false;
}
/**
* Updates render order of chunk meshes
* @param cell - player cell
*/
updateRenderOrder(cell) {
var c = new vec3(...cell);
for (var i in this.cellMesh) {
var n = i.split(":");
var x = new vec3(parseInt(n[0]), parseInt(n[1]), parseInt(n[2]));
this.cellMesh[i].renderOrder = -c.distanceTo(x);
}
}
/**
* Sets custom cell buffer
* @param cellX - cell X coord
* @param cellY - cell Y coord
* @param cellZ - cell Z coord
* @param buffer - cell Buffer
*/
setCell(cellX, cellY, cellZ, buffer) {
this.cellUpdateTime = performance.now();
this.chunkWorker.postMessage({
type: "setCell",
data: [cellX, cellY, cellZ, buffer],
});
this.cellTerrain.setCell(cellX, cellY, cellZ, buffer);
}
/**
* Sets custom block to some value
* @param voxelX - block X coord
* @param voxelY - block Y coord
* @param voxelZ - block Z coord
* @param value - new value of block
*/
setBlock(voxelX, voxelY, voxelZ, value) {
voxelX = parseInt(voxelX);
voxelY = parseInt(voxelY);
voxelZ = parseInt(voxelZ);
this.blocksUpdate = true;
if (this.cellTerrain.getVoxel(voxelX, voxelY, voxelZ) !== value) {
this.chunkWorker.postMessage({
type: "setVoxel",
data: [voxelX, voxelY, voxelZ, value],
});
this.cellTerrain.setVoxel(voxelX, voxelY, voxelZ, value);
}
}
/**
* Resets all chunk meshes
*/
resetWorld() {
for (var i in this.cellMesh) {
if (this.cellMesh[i].geometry !== void 0) {
this.cellMesh[i].geometry.dispose();
this.game.scene.remove(this.cellMesh[i]);
}
delete this.cellMesh[i];
}
this.cellTerrain.cells = {};
this.chunkWorker.postMessage({
type: "resetWorld",
data: null,
});
}
/**
* Updates cell
* @param data - cell Data
*/
updateCell(data) {
var _this = this;
var cellId = this.cellTerrain.vec3(...data.info);
var cell = data.cell;
var mesh = this.cellMesh[cellId];
var geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(cell.positions), 3)
);
geometry.setAttribute(
"normal",
new THREE.BufferAttribute(new Float32Array(cell.normals), 3)
);
geometry.setAttribute(
"uv",
new THREE.BufferAttribute(new Float32Array(cell.uvs), 2)
);
geometry.setAttribute(
"color",
new THREE.BufferAttribute(new Float32Array(cell.colors), 3)
);
geometry.matrixAutoUpdate = false;
if (mesh === void 0) {
this.cellMesh[cellId] = new THREE.Mesh(geometry, this.material);
this.cellMesh[cellId].matrixAutoUpdate = false;
this.cellMesh[cellId].frustumCulled = false;
this.cellMesh[cellId].onAfterRender = function () {
_this.cellMesh[cellId].frustumCulled = true;
_this.cellMesh[cellId].onAfterRender = function () {};
};
this.game.scene.add(this.cellMesh[cellId]);
this.updateRenderOrder(JSON.parse(this.lastPlayerChunk));
} else {
this.cellMesh[cellId].geometry = geometry;
}
}
/**
* Intersect raycast vector
* @param start - vector start
* @param end - vector end
*/
intersectsRay(start, end) {
start.x += 0.5;
start.y += 0.5;
start.z += 0.5;
end.x += 0.5;
end.y += 0.5;
end.z += 0.5;
var dx = end.x - start.x;
var dy = end.y - start.y;
var dz = end.z - start.z;
var lenSq = dx * dx + dy * dy + dz * dz;
var len = Math.sqrt(lenSq);
dx /= len;
dy /= len;
dz /= len;
var t = 0.0;
var ix = Math.floor(start.x);
var iy = Math.floor(start.y);
var iz = Math.floor(start.z);
var stepX = dx > 0 ? 1 : -1;
var stepY = dy > 0 ? 1 : -1;
var stepZ = dz > 0 ? 1 : -1;
var txDelta = Math.abs(1 / dx);
var tyDelta = Math.abs(1 / dy);
var tzDelta = Math.abs(1 / dz);
var xDist = stepX > 0 ? ix + 1 - start.x : start.x - ix;
var yDist = stepY > 0 ? iy + 1 - start.y : start.y - iy;
var zDist = stepZ > 0 ? iz + 1 - start.z : start.z - iz;
var txMax = txDelta < 2e308 ? txDelta * xDist : 2e308;
var tyMax = tyDelta < 2e308 ? tyDelta * yDist : 2e308;
var tzMax = tzDelta < 2e308 ? tzDelta * zDist : 2e308;
var steppedIndex = -1;
while (t <= len) {
var block = this.cellTerrain.getBlock(ix, iy, iz);
var voxel;
if (
block.name === "air" ||
block.name === "cave_air" ||
block.name === "void_air" ||
block.name === "water"
) {
voxel = 0;
} else {
voxel = 1;
}
if (voxel) {
return {
position: [
start.x + t * dx,
start.y + t * dy,
start.z + t * dz,
],
normal: [
steppedIndex === 0 ? -stepX : 0,
steppedIndex === 1 ? -stepY : 0,
steppedIndex === 2 ? -stepZ : 0,
],
voxel,
};
}
if (txMax < tyMax) {
if (txMax < tzMax) {
ix += stepX;
t = txMax;
txMax += txDelta;
steppedIndex = 0;
} else {
iz += stepZ;
t = tzMax;
tzMax += tzDelta;
steppedIndex = 2;
}
} else {
if (tyMax < tzMax) {
iy += stepY;
t = tyMax;
tyMax += tyDelta;
steppedIndex = 1;
} else {
iz += stepZ;
t = tzMax;
tzMax += tzDelta;
steppedIndex = 2;
}
}
}
return null;
}
/**
* Get Block player is pointing at
* @returns Pointing block
*/
getRayBlock() {
var start = new THREE.Vector3().setFromMatrixPosition(
this.game.camera.matrixWorld
);
var end = new THREE.Vector3().set(0, 0, 1).unproject(this.game.camera);
var intersection = this.intersectsRay(start, end);
if (intersection) {
var posPlace = intersection.position.map(function (v, ndx) {
return Math.floor(v + intersection.normal[ndx] * 0.5);
});
var posBreak = intersection.position.map(function (v, ndx) {
return Math.floor(v + intersection.normal[ndx] * -0.5);
});
return { posPlace, posBreak };
} else {
return false;
}
}
/**
* Update chunks around player in radius
* @param radius - radius from player
*/
updateCellsAroundPlayer(radius) {
var pos = this.game.camera.position;
var cell = this.cellTerrain.computeCellForVoxel(
Math.floor(pos.x + 0.5),
Math.floor(pos.y + 0.5),
Math.floor(pos.z + 0.5)
);
if (this.blocksUpdate) {
this.blocksUpdate = false;
this.chunkWorker.postMessage({
type: "updateCellsAroundPlayer",
data: [cell, radius],
});
} else if (this.lastPlayerChunk != JSON.stringify(cell)) {
if (
this.cellUpdateTime !== null &&
performance.now() - this.cellUpdateTime > this.renderTime
) {
this.updateRenderOrder(cell);
this.lastPlayerChunk = JSON.stringify(cell);
this.chunkWorker.postMessage({
type: "updateCellsAroundPlayer",
data: [cell, radius],
});
}
}
}
/**
* Computes Buffer sent from server to readable section
* @param sections - section buffer
* @param x - section x
* @param z - section z
*/
computeSections(sections, x, z) {
var result = SectionComputer({ sections, x, z });
// console.log(result);
var results = [];
for (var i in result) {
var j = result[i];
if (j !== null) {
results.push(this.setCell(j.x, j.y, j.z, j.cell));
} else {
results.push(void 0);
}
}
return results;
}
};
export { World };