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
138 lines (127 loc) · 4.42 KB
/
chunk.worker.js
File metadata and controls
138 lines (127 loc) · 4.42 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
// Generated by CoffeeScript 2.5.1
var State, TerrainManager, handlers, terrain, time;
import {
CellTerrain
} from './CellTerrain.js';
import {
BlockGeo
} from './BlockGeo.js';
console.log("CHUNK WORKER STARTED!");
TerrainManager = class TerrainManager {
constructor(options) {
this.cellSize = options.cellSize;
this.cellTerrain = new CellTerrain({
cellSize: this.cellSize
});
this.BlockGeo = new BlockGeo({
toxelSize: options.toxelSize,
blocksMapping: options.blocksMapping
});
}
genCellGeo(cellX, cellY, cellZ) {
var _this, addFace, colors, i, j, k, l, m, n, normals, pos, positions, ref, ref1, ref2, uvs;
_this = this;
positions = [];
normals = [];
uvs = [];
colors = [];
addFace = function(type, pos) {
var faceVertex;
faceVertex = _this.BlockGeo.genBlockFace(type, _this.cellTerrain.getBlock(...pos), pos);
positions.push(...faceVertex.pos);
normals.push(...faceVertex.norm);
uvs.push(...faceVertex.uv);
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);
};
for (i = l = 0, ref = this.cellSize - 1; (0 <= ref ? l <= ref : l >= ref); i = 0 <= ref ? ++l : --l) {
for (j = m = 0, ref1 = this.cellSize - 1; (0 <= ref1 ? m <= ref1 : m >= ref1); j = 0 <= ref1 ? ++m : --m) {
for (k = n = 0, ref2 = this.cellSize - 1; (0 <= ref2 ? n <= ref2 : n >= ref2); k = 0 <= ref2 ? ++n : --n) {
pos = [cellX * this.cellSize + i, cellY * this.cellSize + j, cellZ * this.cellSize + k];
if (this.cellTerrain.getBlock(...pos).boundingBox === "block") {
if (this.cellTerrain.getBlock(pos[0] + 1, pos[1], pos[2]).boundingBox !== "block") {
addFace("nx", pos);
}
if (this.cellTerrain.getBlock(pos[0] - 1, pos[1], pos[2]).boundingBox !== "block") {
addFace("px", pos);
}
if (this.cellTerrain.getBlock(pos[0], pos[1] - 1, pos[2]).boundingBox !== "block") {
addFace("ny", pos);
}
if (this.cellTerrain.getBlock(pos[0], pos[1] + 1, pos[2]).boundingBox !== "block") {
addFace("py", pos);
}
if (this.cellTerrain.getBlock(pos[0], pos[1], pos[2] + 1).boundingBox !== "block") {
addFace("pz", pos);
}
if (this.cellTerrain.getBlock(pos[0], pos[1], pos[2] - 1).boundingBox !== "block") {
addFace("nz", pos);
}
} else if (this.cellTerrain.getBlock(...pos).name === "water") {
if (this.cellTerrain.getBlock(pos[0] + 1, pos[1], pos[2]).name === "air") {
addFace("nx", pos);
}
if (this.cellTerrain.getBlock(pos[0] - 1, pos[1], pos[2]).name === "air") {
addFace("px", pos);
}
if (this.cellTerrain.getBlock(pos[0], pos[1] - 1, pos[2]).name === "air") {
addFace("ny", pos);
}
if (this.cellTerrain.getBlock(pos[0], pos[1] + 1, pos[2]).name === "air") {
addFace("py", pos);
}
if (this.cellTerrain.getBlock(pos[0], pos[1], pos[2] + 1).name === "air") {
addFace("pz", pos);
}
if (this.cellTerrain.getBlock(pos[0], pos[1], pos[2] - 1).name === "air") {
addFace("nz", pos);
}
}
}
}
}
return {positions, normals, uvs, colors};
}
};
addEventListener("message", function(e) {
var fn;
fn = handlers[e.data.type];
if (!fn) {
throw new Error('no handler for type: ' + e.data.type);
}
fn(e.data.data);
});
State = {
init: null,
world: {}
};
terrain = null;
time = 0;
handlers = {
init: function(data) {
State.init = data;
terrain = new TerrainManager({
models: data.models,
blocks: data.blocks,
blocksMapping: data.blocksMapping,
toxelSize: data.toxelSize,
cellSize: data.cellSize
});
},
setVoxel: function(data) {
return terrain.cellTerrain.setVoxel(...data);
},
genCellGeo: function(data) {
var geo;
if (((terrain.cellTerrain.vec3(...data)) in terrain.cellTerrain.cells) === true) {
geo = terrain.genCellGeo(...data);
return postMessage({
cell: geo,
info: data
});
}
},
setCell: function(data) {
terrain.cellTerrain.setCell(data[0], data[1], data[2], data[3]);
return terrain.cellTerrain.setBiome(data[0], data[1], data[2], data[4]);
}
};