Skip to content

Commit bc529c0

Browse files
committed
some Chunks cleanup
1 parent 7fd5637 commit bc529c0

File tree

4 files changed

+129
-143
lines changed

4 files changed

+129
-143
lines changed

src/client/scripts/World/ChunkTerrain.js

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ var modulo = function (a, b) {
44

55
class ChunkTerrain {
66
constructor(options) {
7-
this.cellSize = 16;
8-
this.cells = {};
7+
this.chunkSize = 16;
8+
this.chunks = {};
99
this.blocksDef = options.blocksDef;
1010
}
1111

@@ -17,66 +17,61 @@ class ChunkTerrain {
1717
}
1818

1919
computeVoxelOffset(voxelX, voxelY, voxelZ) {
20-
var x, y, z;
21-
x = modulo(voxelX, this.cellSize) | 0;
22-
y = modulo(voxelY, this.cellSize) | 0;
23-
z = modulo(voxelZ, this.cellSize) | 0;
24-
return y * this.cellSize * this.cellSize + z * this.cellSize + x;
20+
var x = modulo(voxelX, this.chunkSize) | 0;
21+
var y = modulo(voxelY, this.chunkSize) | 0;
22+
var z = modulo(voxelZ, this.chunkSize) | 0;
23+
return y * this.chunkSize * this.chunkSize + z * this.chunkSize + x;
2524
}
2625

27-
computeCellForVoxel(voxelX, voxelY, voxelZ) {
28-
var cellX, cellY, cellZ;
29-
cellX = Math.floor(voxelX / this.cellSize);
30-
cellY = Math.floor(voxelY / this.cellSize);
31-
cellZ = Math.floor(voxelZ / this.cellSize);
32-
return [cellX, cellY, cellZ];
26+
computeChunkForVoxel(voxelX, voxelY, voxelZ) {
27+
return [
28+
Math.floor(voxelX / this.chunkSize),
29+
Math.floor(voxelY / this.chunkSize),
30+
Math.floor(voxelZ / this.chunkSize),
31+
];
3332
}
3433

35-
addCellForVoxel(voxelX, voxelY, voxelZ) {
36-
var cell, cellId;
37-
cellId = this.vec3(...this.computeCellForVoxel(voxelX, voxelY, voxelZ));
38-
cell = this.cells[cellId];
34+
addChunkForVoxel(voxelX, voxelY, voxelZ) {
35+
var cellId = this.vec3(
36+
...this.computeChunkForVoxel(voxelX, voxelY, voxelZ)
37+
);
38+
var cell = this.chunks[cellId];
3939
if (!cell) {
4040
cell = new Uint32Array(
41-
this.cellSize * this.cellSize * this.cellSize
41+
this.chunkSize * this.chunkSize * this.chunkSize
4242
);
43-
this.cells[cellId] = cell;
43+
this.chunks[cellId] = cell;
4444
}
4545
return cell;
4646
}
4747

48-
getCellForVoxel(voxelX, voxelY, voxelZ) {
49-
var cellId;
50-
cellId = this.vec3(...this.computeCellForVoxel(voxelX, voxelY, voxelZ));
51-
return this.cells[cellId];
48+
getChunkForVoxel(voxelX, voxelY, voxelZ) {
49+
var cellId = this.vec3(
50+
...this.computeChunkForVoxel(voxelX, voxelY, voxelZ)
51+
);
52+
return this.chunks[cellId];
5253
}
5354

5455
setVoxel(voxelX, voxelY, voxelZ, value) {
55-
var cell, voff;
56-
cell = this.getCellForVoxel(voxelX, voxelY, voxelZ);
56+
var cell = this.getChunkForVoxel(voxelX, voxelY, voxelZ);
5757
if (!cell) {
58-
cell = this.addCellForVoxel(voxelX, voxelY, voxelZ);
58+
cell = this.addChunkForVoxel(voxelX, voxelY, voxelZ);
5959
}
60-
voff = this.computeVoxelOffset(voxelX, voxelY, voxelZ);
60+
var voff = this.computeVoxelOffset(voxelX, voxelY, voxelZ);
6161
cell[voff] = value;
6262
}
6363

6464
getVoxel(voxelX, voxelY, voxelZ) {
65-
var cell, voff;
66-
cell = this.getCellForVoxel(voxelX, voxelY, voxelZ);
65+
var cell = this.getChunkForVoxel(voxelX, voxelY, voxelZ);
6766
if (!cell) {
6867
return 0;
6968
}
70-
voff = this.computeVoxelOffset(voxelX, voxelY, voxelZ);
69+
var voff = this.computeVoxelOffset(voxelX, voxelY, voxelZ);
7170
return cell[voff];
7271
}
7372

74-
getCell(x, y, z) {
75-
return this.cells[this.vec3(x, y, z)];
76-
}
77-
78-
setCell(cellX, cellY, cellZ, buffer) {
79-
return (this.cells[this.vec3(cellX, cellY, cellZ)] = buffer);
73+
setChunk(chunkX, chunkY, chunkZ, buffer) {
74+
this.chunks[this.vec3(chunkX, chunkY, chunkZ)] = buffer;
8075
}
8176

8277
getBlock(blockX, blockY, blockZ) {
@@ -93,6 +88,10 @@ class ChunkTerrain {
9388
return false;
9489
}
9590
}
91+
92+
reset() {
93+
this.chunks = {};
94+
}
9695
}
9796

9897
export { ChunkTerrain };

src/client/scripts/World/World.js

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,13 @@ var World = class World {
2323
this.material = this.ATA.material;
2424
this.cellUpdateTime = null;
2525
this.renderTime = 100;
26-
this.neighbours = [
27-
[-1, 0, 0],
28-
[1, 0, 0],
29-
[0, -1, 0],
30-
[0, 1, 0],
31-
[0, 0, -1],
32-
[0, 0, 1],
33-
];
26+
this.lastPlayerChunk = null;
27+
this.blocksUpdate = false;
28+
3429
this.chunkWorker = new chunkWorker();
3530
this.chunkWorker.onmessage = (message) => {
3631
if (message.data.type === "cellGeo") {
37-
return this.updateCell(message.data.data);
32+
return this.updateChunk(message.data.data);
3833
} else if (message.data.type === "removeCell") {
3934
if (this.cellMesh[message.data.data] !== void 0) {
4035
this.cellMesh[message.data.data].geometry.dispose();
@@ -53,35 +48,25 @@ var World = class World {
5348
blocksDef: this.blocksDef,
5449
},
5550
});
56-
this.lastPlayerChunk = null;
57-
this.blocksUpdate = false;
5851
}
5952
/**
6053
* Updates render order of chunk meshes
6154
* @param cell - player cell
6255
*/
6356
updateRenderOrder(cell) {
64-
var c = new vec3(...cell);
6557
for (var i in this.cellMesh) {
6658
var n = i.split(":");
6759
var x = new vec3(parseInt(n[0]), parseInt(n[1]), parseInt(n[2]));
68-
this.cellMesh[i].renderOrder = -c.distanceTo(x);
60+
this.cellMesh[i].renderOrder = -vec3(...cell).distanceTo(x);
6961
}
7062
}
71-
/**
72-
* Sets custom cell buffer
73-
* @param cellX - cell X coord
74-
* @param cellY - cell Y coord
75-
* @param cellZ - cell Z coord
76-
* @param buffer - cell Buffer
77-
*/
78-
setCell(cellX, cellY, cellZ, buffer) {
63+
setChunk(chunkX, chunkY, chunkZ, buffer) {
7964
this.cellUpdateTime = performance.now();
8065
this.chunkWorker.postMessage({
81-
type: "setCell",
82-
data: [cellX, cellY, cellZ, buffer],
66+
type: "setChunk",
67+
data: [chunkX, chunkY, chunkZ, buffer],
8368
});
84-
this.chunkTerrain.setCell(cellX, cellY, cellZ, buffer);
69+
this.chunkTerrain.setChunk(chunkX, chunkY, chunkZ, buffer);
8570
}
8671
/**
8772
* Sets custom block to some value
@@ -114,7 +99,7 @@ var World = class World {
11499
}
115100
delete this.cellMesh[i];
116101
}
117-
this.chunkTerrain.cells = {};
102+
this.chunkTerrain.reset();
118103
this.chunkWorker.postMessage({
119104
type: "resetWorld",
120105
data: null,
@@ -124,7 +109,7 @@ var World = class World {
124109
* Updates cell
125110
* @param data - cell Data
126111
*/
127-
updateCell(data) {
112+
updateChunk(data) {
128113
var cellId = this.chunkTerrain.vec3(...data.info);
129114
var cell = data.cell;
130115
var mesh = this.cellMesh[cellId];
@@ -281,17 +266,17 @@ var World = class World {
281266
* Update chunks around player in radius
282267
* @param radius - radius from player
283268
*/
284-
updateCellsAroundPlayer(radius) {
269+
updateChunksAroundPlayer(radius) {
285270
var pos = this.game.camera.position;
286-
var cell = this.chunkTerrain.computeCellForVoxel(
271+
var cell = this.chunkTerrain.computeChunkForVoxel(
287272
Math.floor(pos.x + 0.5),
288273
Math.floor(pos.y + 0.5),
289274
Math.floor(pos.z + 0.5)
290275
);
291276
if (this.blocksUpdate) {
292277
this.blocksUpdate = false;
293278
this.chunkWorker.postMessage({
294-
type: "updateCellsAroundPlayer",
279+
type: "updateChunksAroundPlayer",
295280
data: [cell, radius],
296281
});
297282
} else if (this.lastPlayerChunk != JSON.stringify(cell)) {
@@ -302,7 +287,7 @@ var World = class World {
302287
this.updateRenderOrder(cell);
303288
this.lastPlayerChunk = JSON.stringify(cell);
304289
this.chunkWorker.postMessage({
305-
type: "updateCellsAroundPlayer",
290+
type: "updateChunksAroundPlayer",
306291
data: [cell, radius],
307292
});
308293
}
@@ -321,7 +306,7 @@ var World = class World {
321306
for (var i in result) {
322307
var j = result[i];
323308
if (j !== null) {
324-
results.push(this.setCell(j.x, j.y, j.z, j.cell));
309+
results.push(this.setChunk(j.x, j.y, j.z, j.cell));
325310
} else {
326311
results.push(void 0);
327312
}

0 commit comments

Comments
 (0)