@@ -7,180 +7,182 @@ fs = require("fs");
77Canvas = require ( "canvas" ) ;
88
99AtlasCreator = 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- }
31-
32- emptyDir ( ) {
33- if ( ! fs . existsSync ( this . buildPath ) ) {
34- fs . mkdirSync ( this . buildPath ) ;
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 ;
3530 }
36- }
3731
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- }
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 ) ;
32+ emptyDir ( ) {
33+ if ( ! fs . existsSync ( this . buildPath ) ) {
34+ fs . mkdirSync ( this . buildPath ) ;
5635 }
57- } ) ;
58- } ) ;
59- }
36+ }
6037
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 ( ) ;
70- }
71- } ;
72- img . src = filePath ;
73- }
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+ }
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+ } ) ;
58+ } ) ;
59+ }
7460
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- }
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 ( ) ;
70+ }
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+ }
8585
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- ) {
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 ) {
11991 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
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
129101 ) ;
130- this . textureMapping [
131- `${ name . substr ( 0 , name . length - 4 ) } @${ i } @${ j } `
132- ] = {
133- x : this . toxelX ,
134- y : this . toxelY ,
102+ this . textureMapping [ `${ name . substr ( 0 , name . length - 4 ) } ` ] = {
103+ x : this . toxelX ,
104+ y : this . toxelY ,
135105 } ;
136106 this . moveToxel ( ) ;
137- }
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+ }
139+ } else {
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 ( ) ;
152+ }
138153 }
139- } else {
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 ( ) ;
152- }
153154 }
154- }
155155
156- moveToxel ( ) {
157- if ( this . toxelX === this . atlasSize ) {
158- this . toxelX = 1 ;
159- this . toxelY += 1 ;
160- } else {
161- this . toxelX += 1 ;
156+ moveToxel ( ) {
157+ if ( this . toxelX === this . atlasSize ) {
158+ this . toxelX = 1 ;
159+ this . toxelY += 1 ;
160+ } else {
161+ this . toxelX += 1 ;
162+ }
162163 }
163- }
164164
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- }
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 (
173+ `\x1b[33mFull atlas: ${ this . buildPath } /${ this . pref } -Atlas.png`
174+ ) ;
175+ fs . writeFileSync (
176+ `${ this . buildPath } /${ this . pref } -Mapping.json` ,
177+ JSON . stringify ( this . textureMapping , null , 2 )
178+ ) ;
179+ console . log (
180+ `\x1b[33mFull atlas mapping: ${ this . buildPath } /${ this . pref } -Mapping.json`
181+ ) ;
182+ console . log (
183+ `\x1b[32mSuccessfully generated ${ this . canvas . width } x${ this . canvas . height } Texture Atlas!\n\x1b[0m`
184+ ) ;
185+ }
184186} ;
185187
186188module . exports = AtlasCreator ;
0 commit comments