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
147 lines (141 loc) · 4.54 KB
/
AnimatedTextureAtlas.js
File metadata and controls
147 lines (141 loc) · 4.54 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
139
140
141
142
143
144
145
146
147
import * as THREE from "three";
class TextureAtlasCreator {
constructor(options) {
this.textureX = options.textureX;
this.textureMapping = options.textureMapping;
this.size = 36;
this.willSize = 27;
}
gen(tick) {
var multi = {};
for (let i in this.textureMapping) {
if (i.includes("@")) {
var 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);
}
}
}
var canvasx = document.createElement("canvas");
var ctx = canvasx.getContext("2d");
canvasx.width = this.willSize * 16;
canvasx.height = this.willSize * 16;
var toxelX = 1;
var toxelY = 1;
for (let i in this.textureMapping) {
if (i.includes("@")) {
xd = this.decodeName(i);
if (multi[xd.pref].loaded === void 0) {
multi[xd.pref].loaded = true;
var lol = this.getToxelForTick(
tick,
multi[xd.pref].x + 1,
multi[xd.pref].y + 1
);
var 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 m = null;
for (let j = 0; j < i.length; j++) {
if (i[j] === "@") {
m = j;
break;
}
}
var pref = i.substr(0, m);
var sub = i.substr(m, i.length);
var m2 = null;
for (let j = 0; j < sub.length; j++) {
if (sub[j] === "@") {
m2 = j;
}
}
var x = parseInt(sub.substr(1, m2 - 1));
var y = parseInt(sub.substr(m2 + 1, sub.length));
return { pref, x, y };
}
getToxelForTick(tick, w, h) {
tick = (tick % (w * h)) + 1;
//option1
var col = (tick - 1) % w;
var row = Math.ceil(tick / w) - 1;
//option2
col = Math.ceil(tick / h) - 1;
row = (tick - 1) % h;
return { row, col };
}
}
class AnimatedTextureAtlas {
constructor(game) {
this.game = game;
this.material = new THREE.MeshStandardMaterial({
side: 0,
map: null,
vertexColors: true,
transparent: true,
alphaTest: 0.1,
});
this.atlasCreator = new TextureAtlasCreator({
textureX: this.game.al.get("blocksAtlasFull"),
textureMapping: this.game.al.get("blocksMappingFull"),
});
var savedTextures = [];
for (var i = 0; i < 10; i++) {
var t = this.atlasCreator.gen(i).toDataURL();
var tekstura = new THREE.TextureLoader().load(t);
tekstura.magFilter = THREE.NearestFilter;
tekstura.minFilter = THREE.NearestFilter;
savedTextures.push(tekstura);
}
var tickq = 0;
setInterval(() => {
tickq++;
var tekst = savedTextures[tickq % 9];
this.material.map = tekst;
this.material.map.needsUpdate = true;
}, 100);
}
}
export { AnimatedTextureAtlas, TextureAtlasCreator };