@@ -5,24 +5,18 @@ Canvas=require "canvas"
55class AtlasCreator
66 constructor : (options )->
77 @pref = options .pref
8- @size = options .size
9- @xpath = options .xpath
8+ @oneFrame = options .oneFrame
9+ @toxelSize = options .toxelSize
10+ @loadPath = options .loadPath
1011 @buildPath = options .buildPath
11- @totalImages = options .totalImages
1212 @atlasSize = options .atlasSize
13- @mini = options .mini
14- @miniAtlasSize = options .miniAtlasSize
15-
16- @canvas = Canvas .createCanvas @atlasSize * @size ,@atlasSize * @size
13+ @canvas = Canvas .createCanvas @atlasSize * @toxelSize ,@atlasSize * @toxelSize
1714 @ctx = @canvas .getContext ' 2d'
1815 @toxelX = 1
1916 @toxelY = 1
20- @miniX = 1
21- @miniY = 1
2217 @loadedImages = 0
2318 @images = {}
2419 @textureMapping = {}
25- @miniMapping = {}
2620 @ emptyDir ()
2721 @ firstLoad ()
2822 return
@@ -32,13 +26,21 @@ class AtlasCreator
3226 return
3327 firstLoad : ()->
3428 _this = @
35- fs .readdir @xpath , (err , files )->
29+ fs .readdir @loadPath , (err , files )->
30+ totalImages = 0
31+ files .forEach (file)->
32+ filePath = " #{ _this .loadPath } /#{ file} "
33+ if path .extname (file) is " .png"
34+ totalImages += 1
35+ return
36+ _this .totalImages = totalImages
3637 files .forEach (file)->
37- filePath = " #{ _this .xpath } /#{ file} "
38+ filePath = " #{ _this .loadPath } /#{ file} "
3839 if path .extname (file) is " .png"
3940 # console.log filePath
4041 _this .addImageToLoad filePath,file
4142 return
43+
4244 return
4345 return
4446 addImageToLoad : (filePath ,name )->
@@ -59,25 +61,23 @@ class AtlasCreator
5961 return
6062 @ updateAtlas ()
6163 addToxelToAtlas : (img ,name )->
62- w = img .width / @size
63- h = img .height / @size
64- if w> 1 or h> 1
65- for i in [0 .. w- 1 ]
66- for j in [0 .. h- 1 ]
67- @ctx .drawImage img,i* @size ,j* @size ,@size ,@size ,(@toxelX - 1 )* @size ,(@toxelY - 1 )* @size , @size ,@size
68- @textureMapping [" #{ name .substr (0 ,name .length - 4 )} @#{ i} @#{ j} " ]= {x : @toxelX ,y : @toxelY }
69- @ moveToxel ()
70- else
71- @ctx .drawImage img,(@toxelX - 1 )* @size ,(@toxelY - 1 )* @size ,@size ,@size
72- @textureMapping [name .substr (0 ,name .length - 4 )]= {x : @toxelX ,y : @toxelY }
64+ w = img .width / @toxelSize
65+ h = img .height / @toxelSize
66+ if @oneFrame
67+ @ctx .drawImage img,0 ,0 ,@toxelSize ,@toxelSize ,(@toxelX - 1 )* @toxelSize ,(@toxelY - 1 )* @toxelSize , @toxelSize ,@toxelSize
68+ @textureMapping [" #{ name .substr (0 ,name .length - 4 )} " ]= {x : @toxelX ,y : @toxelY }
7369 @ moveToxel ()
74- if @mini
75- @miniMapping [name .substr (0 ,name .length - 4 )]= {x : @miniX ,y : @miniY }
76- if @miniX is @miniAtlasSize
77- @miniX = 1
78- @miniY += 1
70+ else
71+ if w> 1 or h> 1
72+ for i in [0 .. w- 1 ]
73+ for j in [0 .. h- 1 ]
74+ @ctx .drawImage img,i* @toxelSize ,j* @toxelSize ,@toxelSize ,@toxelSize ,(@toxelX - 1 )* @toxelSize ,(@toxelY - 1 )* @toxelSize , @toxelSize ,@toxelSize
75+ @textureMapping [" #{ name .substr (0 ,name .length - 4 )} @#{ i} @#{ j} " ]= {x : @toxelX ,y : @toxelY }
76+ @ moveToxel ()
7977 else
80- @miniX += 1
78+ @ctx .drawImage img,(@toxelX - 1 )* @toxelSize ,(@toxelY - 1 )* @toxelSize ,@toxelSize ,@toxelSize
79+ @textureMapping [name .substr (0 ,name .length - 4 )]= {x : @toxelX ,y : @toxelY }
80+ @ moveToxel ()
8181 return
8282 moveToxel : ()->
8383 if @toxelX is @atlasSize
@@ -87,13 +87,13 @@ class AtlasCreator
8787 @toxelX += 1
8888 return
8989 updateAtlas : (path )->
90- fs . writeFileSync " #{ @buildPath } / #{ @pref } Atlas-full.png " , @canvas . toBuffer ( ' image/png ' )
91- console .log " \x1b [33mFull atlas: #{ @buildPath } / #{ @ pref} Atlas-full.png "
92- fs . writeFileSync " #{ @buildPath } / #{ @pref } Mapping-full.json " , JSON . stringify ( @textureMapping , null , 2 )
93- console . log " \x1b [33mFull atlas mapping: #{ @buildPath } /#{ @pref } Mapping-full.json "
94- if @mini
95- fs .writeFileSync " #{ @buildPath } /#{ @pref } Mapping.json" ,JSON .stringify (@miniMapping ,null ,2 )
96- console .log " \x1b [33mMini atlas mapping: #{ @buildPath } /#{ @pref } Mapping.json"
90+
91+ console .log " \x1b [33m[ #{ @pref } Atlas] "
92+ console . log " \x1b [32mTotal images: #{ @totalImages } "
93+ fs . writeFileSync " #{ @buildPath } /#{ @pref } -Atlas.png " , @canvas . toBuffer ( ' image/png ' )
94+ console . log " \x1b [33mFull atlas: #{ @buildPath } / #{ @pref } -Atlas.png "
95+ fs .writeFileSync " #{ @buildPath } /#{ @pref } - Mapping.json" ,JSON .stringify (@textureMapping ,null ,2 )
96+ console .log " \x1b [33mFull atlas mapping: #{ @buildPath } /#{ @pref } - Mapping.json"
9797 console .log " \x1b [32mSuccessfully generated #{ @canvas .width } x#{ @canvas .height } Texture Atlas!\n\x1b [0m"
9898 return
9999
0 commit comments