forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatlas.js
More file actions
142 lines (112 loc) · 3.6 KB
/
atlas.js
File metadata and controls
142 lines (112 loc) · 3.6 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
// Generated by CoffeeScript 2.5.1
(function() {
var Canvas, Image, addImageToLoad, addToxelToAtlas, atlasSize, canvas, createCanvas, ctx, firstLoad, forEachToxel, fs, images, loadImage, loadedImages, miniAtlasSize, miniMapping, miniX, miniY, moveToxel, path, path1, path2, path3, textureMapping, totalImages, toxelX, toxelY, updateAtlas;
path = require("path");
fs = require("fs");
Canvas = require("canvas");
createCanvas = Canvas.createCanvas;
loadImage = Canvas.loadImage;
Image = Canvas.Image;
toxelX = 1;
toxelY = 1;
miniX = 1;
miniY = 1;
totalImages = 694;
loadedImages = 0;
atlasSize = 36;
miniAtlasSize = 27;
path1 = __dirname + "/client/assets/blocks/blocksAtlas-full.png";
path2 = __dirname + "/client/assets/blocks/blocksMapping-full.json";
path3 = __dirname + "/client/assets/blocks/blocksMapping.json";
canvas = createCanvas(atlasSize * 16, atlasSize * 16);
ctx = canvas.getContext('2d');
images = {};
textureMapping = {};
miniMapping = {};
firstLoad = function() {
var directoryPath, folderName;
folderName = "client/assets/blocks/images";
directoryPath = __dirname + "/" + folderName;
fs.readdir(directoryPath, function(err, files) {
files.forEach(function(file) {
var filePath;
filePath = `${__dirname}/${folderName}/${file}`;
if (path.extname(file) === ".png") {
addImageToLoad(filePath, file);
}
});
});
};
addImageToLoad = function(filePath, name) {
var img;
img = new Image();
img.onload = function() {
images[name] = img;
loadedImages++;
if (loadedImages === totalImages) {
return forEachToxel();
}
};
return img.src = filePath;
};
forEachToxel = function() {
Object.keys(images).forEach(function(name) {
var img;
img = images[name];
return addToxelToAtlas(img, name);
});
return updateAtlas();
};
addToxelToAtlas = function(img, name) {
var h, i, j, k, l, ref, ref1, w;
w = img.width / 16;
h = img.height / 16;
if (w > 1 || h > 1) {
for (i = k = 0, ref = w - 1; (0 <= ref ? k <= ref : k >= ref); i = 0 <= ref ? ++k : --k) {
for (j = l = 0, ref1 = h - 1; (0 <= ref1 ? l <= ref1 : l >= ref1); j = 0 <= ref1 ? ++l : --l) {
ctx.drawImage(img, i * 16, j * 16, 16, 16, (toxelX - 1) * 16, (toxelY - 1) * 16, 16, 16);
textureMapping[name.substr(0, name.length - 4) + `@${i}@${j}`] = {
x: toxelX,
y: toxelY
};
moveToxel();
}
}
} else {
ctx.drawImage(img, (toxelX - 1) * 16, (toxelY - 1) * 16, 16, 16);
textureMapping[name.substr(0, name.length - 4)] = {
x: toxelX,
y: toxelY
};
moveToxel();
}
miniMapping[name.substr(0, name.length - 4)] = {
x: miniX,
y: miniY
};
if (miniX === miniAtlasSize) {
miniX = 1;
return miniY += 1;
} else {
return miniX += 1;
}
};
moveToxel = function() {
if (toxelX === atlasSize) {
toxelX = 1;
return toxelY += 1;
} else {
return toxelX += 1;
}
};
updateAtlas = function(path) {
fs.writeFileSync(path1, canvas.toBuffer('image/png'));
console.log(`\x1b[33mSAVING: ${path1}`);
fs.writeFileSync(path2, JSON.stringify(textureMapping, null, 2));
console.log(`\x1b[33mSAVING: ${path2}`);
fs.writeFileSync(path3, JSON.stringify(miniMapping, null, 2));
console.log(`\x1b[33mSAVING: ${path3}`);
return console.log(`\x1b[32mSuccessfully created ${canvas.width}x${canvas.height} Texture Atlas!\n`);
};
firstLoad();
}).call(this);