forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimatedTextureAtlas.js
More file actions
131 lines (122 loc) · 3.68 KB
/
AnimatedTextureAtlas.js
File metadata and controls
131 lines (122 loc) · 3.68 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
var AnimatedTextureAtlas, TextureAtlasCreator;
import * as THREE from './../build/three.module.js';
TextureAtlasCreator = class TextureAtlasCreator {
constructor(options) {
this.textureX = options.textureX;
this.textureMapping = options.textureMapping;
this.size = 36;
this.willSize = 27;
}
gen(tick) {
var canvasx, ctx, i, lol, multi, texmap, toxelX, toxelY, xd;
multi = {};
for (i in this.textureMapping) {
if (i.includes("@")) {
xd = this.decodeName(i);
if (multi[xd.pref] === void 0) {
multi[xd.pref] = xd;
} else {
multi[xd.pref].x = Math.max(multi[xd.pref].x, xd.x);
multi[xd.pref].y = Math.max(multi[xd.pref].y, xd.y);
}
}
}
canvasx = document.createElement('canvas');
ctx = canvasx.getContext("2d");
canvasx.width = this.willSize * 16;
canvasx.height = this.willSize * 16;
toxelX = 1;
toxelY = 1;
for (i in this.textureMapping) {
if (i.includes("@")) {
xd = this.decodeName(i);
if (multi[xd.pref].loaded === void 0) {
multi[xd.pref].loaded = true;
lol = this.getToxelForTick(tick, multi[xd.pref].x + 1, multi[xd.pref].y + 1);
texmap = this.textureMapping[`${xd.pref}@${lol.col}@${lol.row}`];
ctx.drawImage(this.textureX, (texmap.x - 1) * 16, (texmap.y - 1) * 16, 16, 16, (toxelX - 1) * 16, (toxelY - 1) * 16, 16, 16);
toxelX++;
if (toxelX > this.willSize) {
toxelX = 1;
toxelY++;
}
}
} else {
ctx.drawImage(this.textureX, (this.textureMapping[i].x - 1) * 16, (this.textureMapping[i].y - 1) * 16, 16, 16, (toxelX - 1) * 16, (toxelY - 1) * 16, 16, 16);
toxelX++;
if (toxelX > this.willSize) {
toxelX = 1;
toxelY++;
}
}
}
return canvasx;
}
decodeName(i) {
var j, k, l, m, m2, pref, ref, ref1, sub, x, y;
m = null;
for (j = k = 0, ref = i.length - 1; (0 <= ref ? k <= ref : k >= ref); j = 0 <= ref ? ++k : --k) {
if (i[j] === "@") {
m = j;
break;
}
}
pref = i.substr(0, m);
sub = i.substr(m, i.length);
m2 = null;
for (j = l = 0, ref1 = sub.length - 1; (0 <= ref1 ? l <= ref1 : l >= ref1); j = 0 <= ref1 ? ++l : --l) {
if (sub[j] === "@") {
m2 = j;
}
}
x = parseInt(sub.substr(1, m2 - 1));
y = parseInt(sub.substr(m2 + 1, sub.length));
return {pref, x, y};
}
getToxelForTick(tick, w, h) {
var col, row;
tick = tick % (w * h) + 1;
//option1
col = (tick - 1) % w;
row = Math.ceil(tick / w) - 1;
//option2
col = Math.ceil(tick / h) - 1;
row = (tick - 1) % h;
return {row, col};
}
};
AnimatedTextureAtlas = class AnimatedTextureAtlas {
constructor(options) {
var _this, i, k, savedTextures, t, tekstura, tickq;
_this = this;
this.al = options.al;
this.material = new THREE.MeshStandardMaterial({
side: 0,
map: null,
vertexColors: true
});
this.atlasCreator = new TextureAtlasCreator({
textureX: this.al.get("blocksAtlasFull"),
textureMapping: this.al.get("blocksMappingFull")
});
savedTextures = [];
for (i = k = 0; k <= 9; i = ++k) {
t = this.atlasCreator.gen(i).toDataURL();
tekstura = new THREE.TextureLoader().load(t);
tekstura.magFilter = THREE.NearestFilter;
savedTextures.push(tekstura);
}
tickq = 0;
setInterval(function() {
var tekst;
tickq++;
tekst = savedTextures[tickq % 9];
_this.material.map = tekst;
_this.material.map.needsUpdate = true;
}, 100);
}
};
export {
AnimatedTextureAtlas
};