forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsections.worker.js
More file actions
131 lines (119 loc) · 3.7 KB
/
sections.worker.js
File metadata and controls
131 lines (119 loc) · 3.7 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
// Generated by CoffeeScript 2.5.1
(function() {
var BitArray, ChunkDecoder, cd, handlers,
modulo = function(a, b) { return (+a % (b = +b) + b) % b; };
console.log("SECTIONS WORKER STARTED!");
BitArray = class BitArray {
constructor(options) {
var length, valueMask, valuesPerLong;
if (options === null) {
return;
}
if (!options.bitsPerValue > 0) {
console.error('bits per value must at least 1');
}
if (!(options.bitsPerValue <= 32)) {
console.error('bits per value exceeds 32');
}
valuesPerLong = Math.floor(64 / options.bitsPerValue);
length = Math.ceil(options.capacity / valuesPerLong);
if (!options.data) {
options.data = Array(length * 2).fill(0);
}
valueMask = (1 << options.bitsPerValue) - 1;
this.data = options.data;
this.capacity = options.capacity;
this.bitsPerValue = options.bitsPerValue;
this.valuesPerLong = valuesPerLong;
this.valueMask = valueMask;
return;
}
get(index) {
var endBitOffset, endLong, indexInLong, indexInStartLong, result, startLong, startLongIndex;
if (!(index >= 0 && index < this.capacity)) {
console.error('index is out of bounds');
}
startLongIndex = Math.floor(index / this.valuesPerLong);
indexInLong = (index - startLongIndex * this.valuesPerLong) * this.bitsPerValue;
if (indexInLong >= 32) {
indexInStartLong = indexInLong - 32;
startLong = this.data[startLongIndex * 2 + 1];
return (startLong >>> indexInStartLong) & this.valueMask;
}
startLong = this.data[startLongIndex * 2];
indexInStartLong = indexInLong;
result = startLong >>> indexInStartLong;
endBitOffset = indexInStartLong + this.bitsPerValue;
if (endBitOffset > 32) {
endLong = this.data[startLongIndex * 2 + 1];
result |= endLong << (32 - indexInStartLong);
}
return result & this.valueMask;
}
};
ChunkDecoder = class ChunkDecoder {
getBlockIndex(pos) {
return (pos.y << 8) | (pos.z << 4) | pos.x;
}
cvo(voxelX, voxelY, voxelZ) {
var x, y, z;
x = modulo(voxelX, 16) | 0;
y = modulo(voxelY, 16) | 0;
z = modulo(voxelZ, 16) | 0;
return y * 16 * 16 + z * 16 + x;
}
computeSections(packet) {
var cell, data, i, j, k, l, len, m, num, palette, pos, result, sections, solidBlockCount, x, y, z;
sections = packet.sections;
num = 0;
result = [];
for (j = 0, len = sections.length; j < len; j++) {
i = sections[j];
num += 1;
if (i !== null) {
solidBlockCount = i.solidBlockCount;
palette = i.palette;
data = new BitArray(i.data);
pos = {
x: 0,
y: 0,
z: 0
};
cell = new Uint8Array(16 * 16 * 16);
for (x = k = 0; k <= 15; x = ++k) {
for (y = l = 0; l <= 15; y = ++l) {
for (z = m = 0; m <= 15; z = ++m) {
cell[this.cvo(x, y, z)] = palette[data.get(this.getBlockIndex({x, y, z}))];
}
}
}
result.push({
x: packet.x,
y: num,
z: packet.z,
cell
});
} else {
result.push(null);
}
}
return result;
}
};
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);
});
cd = new ChunkDecoder();
handlers = {
computeSections: function(data) {
return postMessage({
result: cd.computeSections(data)
});
}
};
}).call(this);