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