Skip to content

Commit 500068a

Browse files
committed
Queue microtask
1 parent 219952c commit 500068a

File tree

2 files changed

+29
-43
lines changed

2 files changed

+29
-43
lines changed

src/client/scripts/Setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function Setup(game, cb) {
2727
game.camera = new THREE.PerspectiveCamera(game.fov.normal, 2, 0.1, 1000);
2828
game.camera.rotation.order = "YXZ";
2929
game.camera.position.set(26, 26, 26);
30-
game.scene.add(new THREE.AmbientLight(0xffffff));
30+
game.scene.add(new THREE.AmbientLight(0xdddddd));
3131
game.stats = new Stats();
3232
game.drawcalls = game.stats.addPanel(
3333
new Stats.Panel("calls", "#ff8", "#221")

src/client/scripts/World/chunk.worker.js

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,47 @@ import { ChunkMesher } from "./ChunkMesher.js";
44
var terrain = null;
55

66
class TerrainManager {
7-
constructor(options) {
7+
constructor(data) {
88
this.cellSize = 16;
99
this.chunkTerrain = new ChunkTerrain({
10-
blocksDef: options.blocksDef,
10+
blocksDef: data.blocksDef,
1111
});
1212
this.cellNeedsUpdate = {};
1313
this.loadedMeshes = {};
14-
this.neighbours = {
15-
px: [-1, 0, 0],
16-
nx: [1, 0, 0],
17-
ny: [0, -1, 0],
18-
py: [0, 1, 0],
19-
pz: [0, 0, 1],
20-
nz: [0, 0, -1],
21-
};
2214
this.chunkMesher = new ChunkMesher({
23-
blocksTex: options.blocksTex,
24-
blocksMapping: options.blocksMapping,
25-
toxelSize: options.toxelSize,
15+
blocksTex: data.blocksTex,
16+
blocksMapping: data.blocksMapping,
17+
toxelSize: data.toxelSize,
2618
chunkTerrain: this.chunkTerrain,
2719
});
2820
}
2921
}
3022

3123
var handlers = {
3224
init: function (data) {
33-
terrain = new TerrainManager({
34-
models: data.models,
35-
blocks: data.blocks,
36-
blocksMapping: data.blocksMapping,
37-
toxelSize: data.toxelSize,
38-
cellSize: data.cellSize,
39-
blocksTex: data.blocksTex,
40-
blocksDef: data.blocksDef,
41-
});
25+
terrain = new TerrainManager(data);
4226
},
4327
setVoxel: function (data) {
44-
var cellId, l, len, nei, neiCellId, neighbours;
4528
terrain.chunkTerrain.setVoxel(...data);
46-
cellId = terrain.chunkTerrain.vec3(
29+
var cellId = terrain.chunkTerrain.vec3(
4730
...terrain.chunkTerrain.computeCellForVoxel(
4831
data[0],
4932
data[1],
5033
data[2]
5134
)
5235
);
5336
terrain.cellNeedsUpdate[cellId] = true;
54-
neighbours = [
37+
var neighbours = [
5538
[-1, 0, 0],
5639
[1, 0, 0],
5740
[0, -1, 0],
5841
[0, 1, 0],
5942
[0, 0, -1],
6043
[0, 0, 1],
6144
];
62-
for (l = 0, len = neighbours.length; l < len; l++) {
63-
nei = neighbours[l];
64-
neiCellId = terrain.chunkTerrain.vec3(
45+
for (var l = 0; l < neighbours.length; l++) {
46+
var nei = neighbours[l];
47+
var neiCellId = terrain.chunkTerrain.vec3(
6548
...terrain.chunkTerrain.computeCellForVoxel(
6649
data[0] + nei[0],
6750
data[1] + nei[1],
@@ -72,20 +55,23 @@ var handlers = {
7255
}
7356
},
7457
genChunkGeo: function (data) {
75-
if (
76-
terrain.chunkTerrain.vec3(...data) in terrain.chunkTerrain.cells ===
77-
true
78-
) {
79-
var geo = terrain.chunkMesher.genChunkGeo(...data);
80-
postMessage({
81-
type: "cellGeo",
82-
data: {
83-
cell: geo,
84-
info: data,
85-
p: performance.now(),
86-
},
87-
});
88-
}
58+
queueMicrotask(() => {
59+
if (
60+
terrain.chunkTerrain.vec3(...data) in
61+
terrain.chunkTerrain.cells ===
62+
true
63+
) {
64+
var geo = terrain.chunkMesher.genChunkGeo(...data);
65+
postMessage({
66+
type: "cellGeo",
67+
data: {
68+
cell: geo,
69+
info: data,
70+
p: performance.now(),
71+
},
72+
});
73+
}
74+
});
8975
},
9076
setCell: function (data) {
9177
var neighbours = [

0 commit comments

Comments
 (0)