|
| 1 | +var AtlasCreator, Canvas, fs, path; |
1 | 2 |
|
2 | | - var AtlasCreator, Canvas, fs, path; |
| 3 | +path = require("path"); |
3 | 4 |
|
4 | | - path = require("path"); |
| 5 | +fs = require("fs"); |
5 | 6 |
|
6 | | - fs = require("fs"); |
| 7 | +Canvas = require("canvas"); |
7 | 8 |
|
8 | | - Canvas = require("canvas"); |
| 9 | +AtlasCreator = class AtlasCreator { |
| 10 | + constructor(options) { |
| 11 | + this.pref = options.pref; |
| 12 | + this.oneFrame = options.oneFrame; |
| 13 | + this.toxelSize = options.toxelSize; |
| 14 | + this.loadPath = options.loadPath; |
| 15 | + this.buildPath = options.buildPath; |
| 16 | + this.atlasSize = options.atlasSize; |
| 17 | + this.canvas = Canvas.createCanvas( |
| 18 | + this.atlasSize * this.toxelSize, |
| 19 | + this.atlasSize * this.toxelSize |
| 20 | + ); |
| 21 | + this.ctx = this.canvas.getContext("2d"); |
| 22 | + this.toxelX = 1; |
| 23 | + this.toxelY = 1; |
| 24 | + this.loadedImages = 0; |
| 25 | + this.images = {}; |
| 26 | + this.textureMapping = {}; |
| 27 | + this.emptyDir(); |
| 28 | + this.firstLoad(); |
| 29 | + return; |
| 30 | + } |
9 | 31 |
|
10 | | - AtlasCreator = class AtlasCreator { |
11 | | - constructor(options) { |
12 | | - this.pref = options.pref; |
13 | | - this.oneFrame = options.oneFrame; |
14 | | - this.toxelSize = options.toxelSize; |
15 | | - this.loadPath = options.loadPath; |
16 | | - this.buildPath = options.buildPath; |
17 | | - this.atlasSize = options.atlasSize; |
18 | | - this.canvas = Canvas.createCanvas(this.atlasSize * this.toxelSize, this.atlasSize * this.toxelSize); |
19 | | - this.ctx = this.canvas.getContext('2d'); |
20 | | - this.toxelX = 1; |
21 | | - this.toxelY = 1; |
22 | | - this.loadedImages = 0; |
23 | | - this.images = {}; |
24 | | - this.textureMapping = {}; |
25 | | - this.emptyDir(); |
26 | | - this.firstLoad(); |
27 | | - return; |
28 | | - } |
29 | | - |
30 | | - emptyDir() { |
31 | | - if (!fs.existsSync(this.buildPath)) { |
32 | | - fs.mkdirSync(this.buildPath); |
33 | | - } |
| 32 | + emptyDir() { |
| 33 | + if (!fs.existsSync(this.buildPath)) { |
| 34 | + fs.mkdirSync(this.buildPath); |
34 | 35 | } |
| 36 | + } |
35 | 37 |
|
36 | | - firstLoad() { |
37 | | - var _this; |
38 | | - _this = this; |
39 | | - fs.readdir(this.loadPath, function(err, files) { |
40 | | - var totalImages; |
41 | | - totalImages = 0; |
42 | | - files.forEach(function(file) { |
43 | | - var filePath; |
44 | | - filePath = `${_this.loadPath}/${file}`; |
45 | | - if (path.extname(file) === ".png") { |
46 | | - totalImages += 1; |
47 | | - } |
48 | | - }); |
49 | | - _this.totalImages = totalImages; |
50 | | - files.forEach(function(file) { |
51 | | - var filePath; |
52 | | - filePath = `${_this.loadPath}/${file}`; |
53 | | - if (path.extname(file) === ".png") { |
54 | | - // console.log filePath |
55 | | - _this.addImageToLoad(filePath, file); |
56 | | - } |
57 | | - }); |
| 38 | + firstLoad() { |
| 39 | + var _this; |
| 40 | + _this = this; |
| 41 | + fs.readdir(this.loadPath, function (err, files) { |
| 42 | + var totalImages; |
| 43 | + totalImages = 0; |
| 44 | + files.forEach(function (file) { |
| 45 | + if (path.extname(file) === ".png") { |
| 46 | + totalImages += 1; |
| 47 | + } |
58 | 48 | }); |
59 | | - } |
60 | | - |
61 | | - addImageToLoad(filePath, name) { |
62 | | - var _this, img; |
63 | | - _this = this; |
64 | | - img = new Canvas.Image(); |
65 | | - img.onload = function() { |
66 | | - _this.images[name] = img; |
67 | | - _this.loadedImages++; |
68 | | - if (_this.loadedImages === _this.totalImages) { |
69 | | - return _this.forEachToxel(); |
| 49 | + _this.totalImages = totalImages; |
| 50 | + files.forEach(function (file) { |
| 51 | + var filePath; |
| 52 | + filePath = `${_this.loadPath}/${file}`; |
| 53 | + if (path.extname(file) === ".png") { |
| 54 | + // console.log filePath |
| 55 | + _this.addImageToLoad(filePath, file); |
70 | 56 | } |
71 | | - }; |
72 | | - img.src = filePath; |
73 | | - } |
74 | | - |
75 | | - forEachToxel() { |
76 | | - var _this; |
77 | | - _this = this; |
78 | | - Object.keys(this.images).forEach(function(name) { |
79 | | - var img; |
80 | | - img = _this.images[name]; |
81 | | - _this.addToxelToAtlas(img, name); |
82 | 57 | }); |
83 | | - return this.updateAtlas(); |
84 | | - } |
| 58 | + }); |
| 59 | + } |
85 | 60 |
|
86 | | - addToxelToAtlas(img, name) { |
87 | | - var h, i, j, k, l, ref, ref1, w; |
88 | | - w = img.width / this.toxelSize; |
89 | | - h = img.height / this.toxelSize; |
90 | | - if (this.oneFrame) { |
91 | | - this.ctx.drawImage(img, 0, 0, this.toxelSize, this.toxelSize, (this.toxelX - 1) * this.toxelSize, (this.toxelY - 1) * this.toxelSize, this.toxelSize, this.toxelSize); |
92 | | - this.textureMapping[`${name.substr(0, name.length - 4)}`] = { |
93 | | - x: this.toxelX, |
94 | | - y: this.toxelY |
95 | | - }; |
96 | | - this.moveToxel(); |
97 | | - } else { |
98 | | - if (w > 1 || h > 1) { |
99 | | - for (i = k = 0, ref = w - 1; (0 <= ref ? k <= ref : k >= ref); i = 0 <= ref ? ++k : --k) { |
100 | | - for (j = l = 0, ref1 = h - 1; (0 <= ref1 ? l <= ref1 : l >= ref1); j = 0 <= ref1 ? ++l : --l) { |
101 | | - this.ctx.drawImage(img, i * this.toxelSize, j * this.toxelSize, this.toxelSize, this.toxelSize, (this.toxelX - 1) * this.toxelSize, (this.toxelY - 1) * this.toxelSize, this.toxelSize, this.toxelSize); |
102 | | - this.textureMapping[`${name.substr(0, name.length - 4)}@${i}@${j}`] = { |
103 | | - x: this.toxelX, |
104 | | - y: this.toxelY |
105 | | - }; |
106 | | - this.moveToxel(); |
107 | | - } |
108 | | - } |
109 | | - } else { |
110 | | - this.ctx.drawImage(img, (this.toxelX - 1) * this.toxelSize, (this.toxelY - 1) * this.toxelSize, this.toxelSize, this.toxelSize); |
111 | | - this.textureMapping[name.substr(0, name.length - 4)] = { |
112 | | - x: this.toxelX, |
113 | | - y: this.toxelY |
114 | | - }; |
115 | | - this.moveToxel(); |
116 | | - } |
| 61 | + addImageToLoad(filePath, name) { |
| 62 | + var _this, img; |
| 63 | + _this = this; |
| 64 | + img = new Canvas.Image(); |
| 65 | + img.onload = function () { |
| 66 | + _this.images[name] = img; |
| 67 | + _this.loadedImages++; |
| 68 | + if (_this.loadedImages === _this.totalImages) { |
| 69 | + return _this.forEachToxel(); |
117 | 70 | } |
118 | | - } |
| 71 | + }; |
| 72 | + img.src = filePath; |
| 73 | + } |
| 74 | + |
| 75 | + forEachToxel() { |
| 76 | + var _this; |
| 77 | + _this = this; |
| 78 | + Object.keys(this.images).forEach(function (name) { |
| 79 | + var img; |
| 80 | + img = _this.images[name]; |
| 81 | + _this.addToxelToAtlas(img, name); |
| 82 | + }); |
| 83 | + return this.updateAtlas(); |
| 84 | + } |
119 | 85 |
|
120 | | - moveToxel() { |
121 | | - if (this.toxelX === this.atlasSize) { |
122 | | - this.toxelX = 1; |
123 | | - this.toxelY += 1; |
| 86 | + addToxelToAtlas(img, name) { |
| 87 | + var h, i, j, k, l, ref, ref1, w; |
| 88 | + w = img.width / this.toxelSize; |
| 89 | + h = img.height / this.toxelSize; |
| 90 | + if (this.oneFrame) { |
| 91 | + this.ctx.drawImage( |
| 92 | + img, |
| 93 | + 0, |
| 94 | + 0, |
| 95 | + this.toxelSize, |
| 96 | + this.toxelSize, |
| 97 | + (this.toxelX - 1) * this.toxelSize, |
| 98 | + (this.toxelY - 1) * this.toxelSize, |
| 99 | + this.toxelSize, |
| 100 | + this.toxelSize |
| 101 | + ); |
| 102 | + this.textureMapping[`${name.substr(0, name.length - 4)}`] = { |
| 103 | + x: this.toxelX, |
| 104 | + y: this.toxelY, |
| 105 | + }; |
| 106 | + this.moveToxel(); |
| 107 | + } else { |
| 108 | + if (w > 1 || h > 1) { |
| 109 | + for ( |
| 110 | + i = k = 0, ref = w - 1; |
| 111 | + 0 <= ref ? k <= ref : k >= ref; |
| 112 | + i = 0 <= ref ? ++k : --k |
| 113 | + ) { |
| 114 | + for ( |
| 115 | + j = l = 0, ref1 = h - 1; |
| 116 | + 0 <= ref1 ? l <= ref1 : l >= ref1; |
| 117 | + j = 0 <= ref1 ? ++l : --l |
| 118 | + ) { |
| 119 | + this.ctx.drawImage( |
| 120 | + img, |
| 121 | + i * this.toxelSize, |
| 122 | + j * this.toxelSize, |
| 123 | + this.toxelSize, |
| 124 | + this.toxelSize, |
| 125 | + (this.toxelX - 1) * this.toxelSize, |
| 126 | + (this.toxelY - 1) * this.toxelSize, |
| 127 | + this.toxelSize, |
| 128 | + this.toxelSize |
| 129 | + ); |
| 130 | + this.textureMapping[ |
| 131 | + `${name.substr(0, name.length - 4)}@${i}@${j}` |
| 132 | + ] = { |
| 133 | + x: this.toxelX, |
| 134 | + y: this.toxelY, |
| 135 | + }; |
| 136 | + this.moveToxel(); |
| 137 | + } |
| 138 | + } |
124 | 139 | } else { |
125 | | - this.toxelX += 1; |
| 140 | + this.ctx.drawImage( |
| 141 | + img, |
| 142 | + (this.toxelX - 1) * this.toxelSize, |
| 143 | + (this.toxelY - 1) * this.toxelSize, |
| 144 | + this.toxelSize, |
| 145 | + this.toxelSize |
| 146 | + ); |
| 147 | + this.textureMapping[name.substr(0, name.length - 4)] = { |
| 148 | + x: this.toxelX, |
| 149 | + y: this.toxelY, |
| 150 | + }; |
| 151 | + this.moveToxel(); |
126 | 152 | } |
127 | 153 | } |
| 154 | + } |
128 | 155 |
|
129 | | - updateAtlas(path) { |
130 | | - console.log(`\x1b[33m[${this.pref} Atlas]`); |
131 | | - console.log(`\x1b[32mTotal images: ${this.totalImages}`); |
132 | | - fs.writeFileSync(`${this.buildPath}/${this.pref}-Atlas.png`, this.canvas.toBuffer('image/png')); |
133 | | - console.log(`\x1b[33mFull atlas: ${this.buildPath}/${this.pref}-Atlas.png`); |
134 | | - fs.writeFileSync(`${this.buildPath}/${this.pref}-Mapping.json`, JSON.stringify(this.textureMapping, null, 2)); |
135 | | - console.log(`\x1b[33mFull atlas mapping: ${this.buildPath}/${this.pref}-Mapping.json`); |
136 | | - console.log(`\x1b[32mSuccessfully generated ${this.canvas.width}x${this.canvas.height} Texture Atlas!\n\x1b[0m`); |
| 156 | + moveToxel() { |
| 157 | + if (this.toxelX === this.atlasSize) { |
| 158 | + this.toxelX = 1; |
| 159 | + this.toxelY += 1; |
| 160 | + } else { |
| 161 | + this.toxelX += 1; |
137 | 162 | } |
| 163 | + } |
138 | 164 |
|
139 | | - }; |
140 | | - |
141 | | - module.exports = AtlasCreator; |
| 165 | + updateAtlas() { |
| 166 | + console.log(`\x1b[33m[${this.pref} Atlas]`); |
| 167 | + console.log(`\x1b[32mTotal images: ${this.totalImages}`); |
| 168 | + fs.writeFileSync( |
| 169 | + `${this.buildPath}/${this.pref}-Atlas.png`, |
| 170 | + this.canvas.toBuffer("image/png") |
| 171 | + ); |
| 172 | + console.log(`\x1b[33mFull atlas: ${this.buildPath}/${this.pref}-Atlas.png`); |
| 173 | + fs.writeFileSync( |
| 174 | + `${this.buildPath}/${this.pref}-Mapping.json`, |
| 175 | + JSON.stringify(this.textureMapping, null, 2) |
| 176 | + ); |
| 177 | + console.log( |
| 178 | + `\x1b[33mFull atlas mapping: ${this.buildPath}/${this.pref}-Mapping.json` |
| 179 | + ); |
| 180 | + console.log( |
| 181 | + `\x1b[32mSuccessfully generated ${this.canvas.width}x${this.canvas.height} Texture Atlas!\n\x1b[0m` |
| 182 | + ); |
| 183 | + } |
| 184 | +}; |
142 | 185 |
|
| 186 | +module.exports = AtlasCreator; |
0 commit comments