|
| 1 | +path=require "path" |
| 2 | +fs=require "fs" |
| 3 | +Canvas=require "canvas" |
| 4 | + |
| 5 | +module.exports=(pref,size,xpath,buildPath,totalImages,atlasSize,mini,miniAtlasSize)-> |
| 6 | + createCanvas=Canvas.createCanvas |
| 7 | + loadImage=Canvas.loadImage |
| 8 | + Image=Canvas.Image |
| 9 | + |
| 10 | + toxelX=1 |
| 11 | + toxelY=1 |
| 12 | + miniX=1 |
| 13 | + miniY=1 |
| 14 | + loadedImages=0 |
| 15 | + |
| 16 | + if not fs.existsSync buildPath |
| 17 | + fs.mkdirSync buildPath |
| 18 | + |
| 19 | + |
| 20 | + canvas=createCanvas atlasSize*size,atlasSize*size |
| 21 | + ctx=canvas.getContext '2d' |
| 22 | + |
| 23 | + images={} |
| 24 | + textureMapping={} |
| 25 | + miniMapping={} |
| 26 | + |
| 27 | + firstLoad=-> |
| 28 | + fs.readdir xpath, (err, files)-> |
| 29 | + files.forEach (file)-> |
| 30 | + filePath="#{xpath}/#{file}" |
| 31 | + if path.extname(file) is ".png" |
| 32 | + addImageToLoad filePath,file |
| 33 | + return |
| 34 | + return |
| 35 | + return |
| 36 | + addImageToLoad=(filePath,name)-> |
| 37 | + img=new Image |
| 38 | + img.onload=-> |
| 39 | + images[name]=img |
| 40 | + loadedImages++ |
| 41 | + if loadedImages is totalImages |
| 42 | + forEachToxel() |
| 43 | + img.src=filePath |
| 44 | + forEachToxel=-> |
| 45 | + Object.keys(images).forEach (name)-> |
| 46 | + img=images[name] |
| 47 | + addToxelToAtlas img,name |
| 48 | + updateAtlas() |
| 49 | + addToxelToAtlas=(img,name)-> |
| 50 | + w=img.width/size |
| 51 | + h=img.height/size |
| 52 | + if w>1 or h>1 |
| 53 | + for i in [0..w-1] |
| 54 | + for j in [0..h-1] |
| 55 | + ctx.drawImage img,i*size,j*size,size,size,(toxelX-1)*size, (toxelY-1)*size, size,size |
| 56 | + textureMapping[name.substr(0,name.length-4)+"@#{i}@#{j}"]={x:toxelX,y:toxelY} |
| 57 | + moveToxel() |
| 58 | + else |
| 59 | + ctx.drawImage img, (toxelX-1)*size, (toxelY-1)*size, size,size |
| 60 | + textureMapping[name.substr(0,name.length-4)]={x:toxelX,y:toxelY} |
| 61 | + moveToxel() |
| 62 | + if mini |
| 63 | + miniMapping[name.substr(0,name.length-4)]={x:miniX,y:miniY} |
| 64 | + if miniX is miniAtlasSize |
| 65 | + miniX=1 |
| 66 | + miniY+=1 |
| 67 | + else |
| 68 | + miniX+=1 |
| 69 | + moveToxel=-> |
| 70 | + if toxelX is atlasSize |
| 71 | + toxelX=1 |
| 72 | + toxelY+=1 |
| 73 | + else |
| 74 | + toxelX+=1 |
| 75 | + updateAtlas=(path)-> |
| 76 | + fs.writeFileSync "#{buildPath}/#{pref}Atlas-full.png", canvas.toBuffer('image/png') |
| 77 | + console.log "\x1b[33mSAVING: #{buildPath}/#{pref}Atlas-full.png" |
| 78 | + |
| 79 | + fs.writeFileSync "#{buildPath}/#{pref}Mapping-full.json",JSON.stringify(textureMapping,null,2) |
| 80 | + console.log "\x1b[33mSAVING: #{buildPath}/#{pref}Mapping-full.json" |
| 81 | + if mini |
| 82 | + fs.writeFileSync "#{buildPath}/#{pref}Mapping.json",JSON.stringify(miniMapping,null,2) |
| 83 | + console.log "\x1b[33mSAVING: #{buildPath}/#{pref}Mapping.json" |
| 84 | + |
| 85 | + console.log "\x1b[32mSuccessfully created #{canvas.width}x#{canvas.height} Texture Atlas!\n\x1b[0m" |
| 86 | + firstLoad() |
| 87 | + return |
0 commit comments