├── .github └── workflows │ ├── linux.yml │ ├── macos.yml │ └── windows.yml ├── .gitignore ├── AssetPacker.n ├── LICENSE ├── README.md ├── Sources ├── AssetPacker.hx └── Log.hx ├── build.hxml ├── fmt.sh ├── run.bat └── run.sh /.github/workflows/linux.yml: -------------------------------------------------------------------------------- 1 | name: Linux 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | 11 | jobs: 12 | build: 13 | runs-on: ubuntu-latest 14 | 15 | steps: 16 | - uses: actions/checkout@v3 17 | - uses: krdlab/setup-haxe@v1 18 | with: 19 | haxe-version: 4.2.5 20 | - name: Install haxelibs 21 | run: haxelib install format --always && haxelib install bin-packing --always && haxelib install tink_cli --always && haxelib install hxcpp --always 22 | - name: Compile 23 | run: ./run.sh 24 | - name: Upload build 25 | uses: actions/upload-artifact@v3 26 | with: 27 | name: build-linux 28 | path: | 29 | build/AssetPacker 30 | -------------------------------------------------------------------------------- /.github/workflows/macos.yml: -------------------------------------------------------------------------------- 1 | name: MacOS 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | 11 | jobs: 12 | build: 13 | runs-on: macos-latest 14 | 15 | steps: 16 | - uses: actions/checkout@v3 17 | - uses: krdlab/setup-haxe@v1 18 | with: 19 | haxe-version: 4.2.5 20 | - name: Install haxelibs 21 | run: haxelib install format --always && haxelib install bin-packing --always && haxelib install tink_cli --always && haxelib install hxcpp --always 22 | - name: Compile 23 | run: ./run.sh 24 | - name: Upload build 25 | uses: actions/upload-artifact@v3 26 | with: 27 | name: build-macos 28 | path: | 29 | build/AssetPacker 30 | -------------------------------------------------------------------------------- /.github/workflows/windows.yml: -------------------------------------------------------------------------------- 1 | name: Windows 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | 11 | jobs: 12 | build: 13 | runs-on: windows-latest 14 | 15 | steps: 16 | - uses: actions/checkout@v3 17 | - uses: krdlab/setup-haxe@v1 18 | with: 19 | haxe-version: 4.2.5 20 | - name: Install haxelibs 21 | run: haxelib install format --always && haxelib install bin-packing --always && haxelib install tink_cli --always && haxelib install hxcpp --always 22 | - name: Compile 23 | run: .\run.bat 24 | - name: Upload build 25 | uses: actions/upload-artifact@v3 26 | with: 27 | name: build-windows 28 | path: | 29 | .\build\AssetPacker.exe 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | assets/* 3 | output.png 4 | data.json 5 | -------------------------------------------------------------------------------- /AssetPacker.n: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/5Mixer/Asset-Manager/56c7775b13227759232e74f09c8960125b0b34ed/AssetPacker.n -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Daniel Blaker 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Asset-Manager 2 | A CLI application that packs multiple source textures into a single output texture, alongside a JSON file describe the image layout. 3 | 4 | [![Linux](https://github.com/5Mixer/Asset-Manager/actions/workflows/linux.yml/badge.svg)](https://github.com/5Mixer/Asset-Manager/actions/workflows/linux.yml) 5 | [![MacOS](https://github.com/5Mixer/Asset-Manager/actions/workflows/macos.yml/badge.svg)](https://github.com/5Mixer/Asset-Manager/actions/workflows/macos.yml) 6 | [![Windows](https://github.com/5Mixer/Asset-Manager/actions/workflows/windows.yml/badge.svg)](https://github.com/5Mixer/Asset-Manager/actions/workflows/windows.yml) 7 | 8 | ### Made with: 9 | - Haxe 10 | - Packing algorithms from https://github.com/Tw1ddle/Rectangle-Bin-Packing 11 | - PNG generation with https://github.com/HaxeFoundation/format. 12 | - CLI flags with https://github.com/haxetink/tink_cli 13 | 14 | ### Features 15 | - The ability to watch the filesystem. Adding/removing/editing files triggers texture packing. 16 | - Simple JSON output. 17 | - Optional recursive discovery of images in source folder. 18 | 19 | ### Demo 20 | 21 | https://user-images.githubusercontent.com/8501694/212623092-c9ee89c3-6cc7-466c-bb54-25063deaa5b6.mp4 22 | 23 | ``` 24 | █████╗ ███████╗███████╗███████╗████████╗ 25 | ██╔══██╗██╔════╝██╔════╝██╔════╝╚══██╔══╝ 26 | ███████║███████╗███████╗█████╗ ██║ 27 | ██╔══██║╚════██║╚════██║██╔══╝ ██║ 28 | ██║ ██║███████║███████║███████╗ ██║ 29 | ╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝ ╚═╝ 30 | ███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗██████╗ 31 | ████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝██╔══██╗ 32 | ██╔████╔██║███████║██╔██╗ ██║███████║██║ ███╗█████╗ ██████╔╝ 33 | ██║╚██╔╝██║██╔══██║██║╚██╗██║██╔══██║██║ ██║██╔══╝ ██╔══██╗ 34 | ██║ ╚═╝ ██║██║ ██║██║ ╚████║██║ ██║╚██████╔╝███████╗██║ ██║ 35 | ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ 36 | 37 | 38 | Execute AssetPacker with flags 39 | 40 | Subcommands: 41 | help : Print help text 42 | 43 | Flags: 44 | --sourcePath, -p : Directory that source images are located in 45 | --destinationPath, -o : File path that the packed texture will be written to 46 | --jsonPath, -j : File path that the JSON describe the sprite layout will be written to 47 | --deepSearch, -d : Flag to recurse through subdirectories in asset path 48 | --quiet, -q : Flag to disable logging 49 | --watch, -w : Flag to continue to watch for changes after packing 50 | --maxWidth, -x : The maximum width of the output image 51 | --maxHeight, -y : The maximum height of the output image 52 | 53 | ``` 54 | 55 | -------------------------------------------------------------------------------- /Sources/AssetPacker.hx: -------------------------------------------------------------------------------- 1 | package; 2 | 3 | import tink.Cli; 4 | import binpacking.SimplifiedMaxRectsPacker; 5 | import binpacking.Rect; 6 | 7 | typedef PlacedImage = { 8 | var x:Int; 9 | var y:Int; 10 | var width:Int; 11 | var height:Int; 12 | var bytes:haxe.io.Bytes; 13 | } 14 | 15 | typedef OutputSprite = { 16 | var x:Int; 17 | var y:Int; 18 | var width:Int; 19 | var height:Int; 20 | } 21 | 22 | class AssetPacker { 23 | /** Directory that source images are located in **/ 24 | @:alias('p') 25 | public var sourcePath = "assets"; 26 | 27 | /** File path that the packed texture will be written to **/ 28 | @:alias('o') 29 | public var destinationPath = "output.png"; 30 | 31 | /** File path that the JSON describe the sprite layout will be written to **/ 32 | @:alias('j') 33 | public var jsonPath = "sprites.json"; 34 | 35 | /** Flag to recurse through subdirectories in asset path **/ 36 | @:alias('d') 37 | public var deepSearch = false; 38 | 39 | /** Flag to disable logging **/ 40 | @:alias('q') 41 | public var quiet = false; 42 | 43 | /** Flag to continue to watch for changes after packing **/ 44 | @:alias('w') 45 | public var watch = false; 46 | 47 | /** The maximum width of the output image**/ 48 | @:alias('x') 49 | public var maxWidth = 4096; 50 | 51 | /** The maximum height of the output image **/ 52 | @:alias('y') 53 | public var maxHeight = 4096; 54 | 55 | final watchTimePauseSeconds = 0.2; 56 | var lastFolderStructure:Array; 57 | var lastPack = Sys.time(); 58 | 59 | public function new() {} 60 | 61 | /** Execute AssetPacker with flags **/ 62 | @:defaultCommand 63 | public function run() { 64 | Log.init(); 65 | Log.enabled = !quiet; 66 | 67 | Log.logAsciiArt(); 68 | 69 | Log.log("Building assets.\n"); 70 | 71 | var pngPaths = findAllPngsIn(sourcePath, deepSearch); 72 | 73 | Log.log("Found " + pngPaths.length + " pngs."); 74 | 75 | packImages(pngPaths); 76 | 77 | lastFolderStructure = pngPaths; 78 | 79 | if (watch) { 80 | Log.log(Log.Colour.BOLD_BLUE + "Watching for changes"); 81 | watchForChanges(); 82 | } 83 | } 84 | 85 | /** Print help text **/ 86 | @:command 87 | public function help() { 88 | Log.init(); 89 | Log.logAsciiArt(); 90 | Log.log(Cli.getDoc(this)); 91 | } 92 | 93 | function watchForChanges() { 94 | while (true) { 95 | var folderContents = findAllPngsIn(sourcePath, deepSearch); 96 | if (lastFolderStructure.length != folderContents.length) { 97 | Log.log("File changes detected, repacking"); 98 | packImages(folderContents); 99 | } else { 100 | for (file in folderContents) { 101 | if (!sys.FileSystem.exists(file)) { 102 | Log.log("File " + file + " removed, skipping."); 103 | continue; 104 | } 105 | if (sys.FileSystem.stat(file).mtime.getTime() / 1000 > lastPack) { 106 | Log.log(Log.Colour.BOLD_CYAN + "Filesystem change at " + file + ", repacking!"); 107 | packImages(folderContents); 108 | } 109 | } 110 | } 111 | lastFolderStructure = folderContents.copy(); 112 | 113 | Sys.sleep(watchTimePauseSeconds); 114 | } 115 | } 116 | 117 | function packImages(filePaths:Array) { 118 | lastPack = Sys.time(); 119 | 120 | // To crop off unused image space. 121 | var rightMostPixel = 0; 122 | var lowestPixel = 0; 123 | 124 | // Packer and output data 125 | var packer = new SimplifiedMaxRectsPacker(maxWidth, maxHeight); 126 | var images = new Array(); 127 | var outputData:Map = []; 128 | 129 | var index = 0; 130 | for (filePath in filePaths) { 131 | index++; 132 | Log.log(Log.Colour.BOLD_CYAN + "Processing: " + filePath); 133 | 134 | if (!sys.FileSystem.exists(filePath)) { 135 | Log.log("File removed."); 136 | continue; 137 | } 138 | 139 | var file:sys.io.FileInput = null; 140 | var data:format.png.Data = null; 141 | try { 142 | file = sys.io.File.read(filePath, true); 143 | data = new format.png.Reader(file).read(); 144 | } catch (e:Dynamic) { 145 | if (file != null) 146 | file.close(); 147 | Log.log("Problem reading file " + filePath + ", perhaps it is a corrupt png? Error: " + e); 148 | } 149 | if (file == null || data == null) 150 | continue; 151 | var headerData = format.png.Tools.getHeader(data); 152 | var bytes = format.png.Tools.extract32(data); 153 | file.close(); 154 | 155 | // Start packing rectangles 156 | var rectWidth:Int = headerData.width; 157 | var rectHeight:Int = headerData.height; 158 | var rect:Rect = packer.insert(rectWidth, rectHeight); 159 | if (rect == null) { 160 | Log.log(Log.Colour.RED + "Failed to pack!"); 161 | continue; 162 | } 163 | Log.log("Placed " + rectWidth + "x" + rectHeight + " image at " + rect.x + ", " + rect.y); 164 | 165 | // Expand image size if need be. 166 | rightMostPixel = Math.ceil(Math.max(rightMostPixel, rect.x + rectWidth)); 167 | lowestPixel = Math.ceil(Math.max(lowestPixel, rect.y + rectHeight)); 168 | 169 | // Record this images placement 170 | outputData[filePath] = { 171 | x: Math.floor(rect.x), 172 | y: Math.floor(rect.y), 173 | width: headerData.width, 174 | height: headerData.height 175 | }; 176 | images.push({ 177 | x: Math.floor(rect.x), 178 | y: Math.floor(rect.y), 179 | width: headerData.width, 180 | height: headerData.height, 181 | bytes: bytes 182 | }); 183 | 184 | // Debug 185 | var bar = ""; 186 | var barLength = 20; 187 | for (i in 0...barLength) 188 | bar += (index / filePaths.length > i / barLength) ? '=' : ' '; 189 | 190 | Log.log('[' + bar + '] ' + index + " / " + filePaths.length + "\n"); 191 | } 192 | 193 | // Construct the output image. 194 | var width = rightMostPixel; 195 | var height = lowestPixel; 196 | var bytes = new haxe.io.BytesOutput(); 197 | 198 | // Loop through every pixel in image, see if there is an image placed there and 199 | // get appropriate pixel. 200 | for (y in 0...height) { 201 | for (x in 0...width) { 202 | // col is the colour at this pixel. 203 | // It's initial value below is the 'background' colour of the packed image. 204 | var col = colour(255, 255, 255, 0); 205 | for (image in images) { 206 | if (x > image.x && y > image.y && x < (image.x + image.width) && y < (image.y + image.height)) { 207 | var c = get(image.width, image.bytes, x - image.x, y - image.y); 208 | col = colour(c.r, c.g, c.b, c.a); 209 | } 210 | } 211 | bytes.writeInt32(col); 212 | } 213 | } 214 | 215 | // Write out image. 216 | var outputImage = format.png.Tools.build32BGRA(width, height, bytes.getBytes()); 217 | var outWriter = sys.io.File.write(destinationPath, true); 218 | new format.png.Writer(outWriter).write(outputImage); 219 | outWriter.close(); 220 | 221 | // Write out JSON data. 222 | sys.io.File.saveContent(jsonPath, haxe.Json.stringify(outputData)); 223 | 224 | Log.log(Log.Colour.BOLD + 'Finished and exported to $destinationPath and $jsonPath'); 225 | } 226 | 227 | function findAllPngsIn(folder:String, deep = true):Array { 228 | var pngs:Array = []; 229 | 230 | if (!sys.FileSystem.exists(folder)) { 231 | Log.log('Failed to read non-existent directory $folder'); 232 | throw "Cannot pack assets if root directory does not exist"; 233 | return []; 234 | } 235 | 236 | var directoryPaths = try { 237 | sys.FileSystem.readDirectory(folder); 238 | } catch (e:Any) { 239 | Log.log('Failed to read directory $folder, exception $e'); 240 | throw "Cannot pack assets if root directory cannot be read"; 241 | } 242 | 243 | for (path in directoryPaths) { 244 | var absolutePath = folder + "/" + path; 245 | if (!sys.FileSystem.exists(absolutePath)) { 246 | Log.log("Path removed."); 247 | continue; 248 | } 249 | 250 | if (sys.FileSystem.isDirectory(absolutePath)) { 251 | if (deep) { 252 | pngs = pngs.concat(findAllPngsIn(absolutePath)); 253 | } 254 | } else { 255 | if (path.substr(path.length - 4) == ".png") { 256 | pngs.push(absolutePath); 257 | } 258 | } 259 | } 260 | return pngs; 261 | } 262 | 263 | // Get pixel at x,y of image. Needs image width and image in haxe bytes. 264 | inline function get(w:Int, bytes:haxe.io.Bytes, x, y) { 265 | var i = ((y * w) + (x)) * 4; 266 | return { 267 | a: bytes.get(i + 3), 268 | r: bytes.get(i + 2), 269 | g: bytes.get(i + 1), 270 | b: bytes.get(i) 271 | }; 272 | } 273 | 274 | // Get hexidecimal representation of colours. Keep in mind this may be a weird for PNG format. 275 | inline function colour(r:Int, g:Int, b:Int, a:Int) { 276 | return ((a) << 24) + (r << 16) + (g << 8) + (b); 277 | } 278 | 279 | static function main() { 280 | Cli.process(Sys.args(), new AssetPacker()).handle(Cli.exit); 281 | } 282 | } 283 | -------------------------------------------------------------------------------- /Sources/Log.hx: -------------------------------------------------------------------------------- 1 | package; 2 | 3 | class Log { 4 | public static final Colour = { 5 | NORMAL: "", 6 | RESET: "\033[m", 7 | BOLD: "\033[1m", 8 | RED: "\033[31m", 9 | GREEN: "\033[32m", 10 | YELLOW: "\033[33m", 11 | BLUE: "\033[34m", 12 | MAGENTA: "\033[35m", 13 | CYAN: "\033[36m", 14 | BOLD_RED: "\033[1;31m", 15 | BOLD_GREEN: "\033[1;32m", 16 | BOLD_YELLOW: "\033[1;33m", 17 | BOLD_BLUE: "\033[1;34m", 18 | BOLD_MAGENTA: "\033[1;35m", 19 | BOLD_CYAN: "\033[1;36m", 20 | BG_RED: "\033[41m", 21 | BG_GREEN: "\033[42m", 22 | BG_YELLOW: "\033[43m", 23 | BG_BLUE: "\033[44m", 24 | BG_MAGENTA: "\033[45m", 25 | BG_CYAN: "\033[46m" 26 | }; 27 | 28 | public static var enabled = true; 29 | 30 | public static function init() { 31 | // Set console encoding to show colours and ASCII on Windows. 32 | if (Sys.systemName() == "Windows") { 33 | Sys.command("@ECHO OFF >NUL"); 34 | Sys.command("@chcp 65001>NUL"); 35 | } 36 | } 37 | 38 | public static function logAsciiArt() { 39 | Log.log('${Colour.BOLD + Colour.GREEN} 40 | █████╗ ███████╗███████╗███████╗████████╗ 41 | ██╔══██╗██╔════╝██╔════╝██╔════╝╚══██╔══╝ 42 | ███████║███████╗███████╗█████╗ ██║ 43 | ██╔══██║╚════██║╚════██║██╔══╝ ██║ 44 | ██║ ██║███████║███████║███████╗ ██║ 45 | ╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝ ╚═╝ 46 | ███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗██████╗ 47 | ████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝██╔══██╗ 48 | ██╔████╔██║███████║██╔██╗ ██║███████║██║ ███╗█████╗ ██████╔╝ 49 | ██║╚██╔╝██║██╔══██║██║╚██╗██║██╔══██║██║ ██║██╔══╝ ██╔══██╗ 50 | ██║ ╚═╝ ██║██║ ██║██║ ╚████║██║ ██║╚██████╔╝███████╗██║ ██║ 51 | ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝\n'); 52 | } 53 | 54 | public static function log(str:String) { 55 | if (enabled) { 56 | Sys.println(str + Colour.RESET); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /build.hxml: -------------------------------------------------------------------------------- 1 | -lib format 2 | -lib bin-packing 3 | -lib tink_cli 4 | --dce full 5 | -cp Sources 6 | -main AssetPacker 7 | -cpp build 8 | -------------------------------------------------------------------------------- /fmt.sh: -------------------------------------------------------------------------------- 1 | haxelib install formatter 2 | haxelib run formatter -s . 3 | 4 | -------------------------------------------------------------------------------- /run.bat: -------------------------------------------------------------------------------- 1 | haxe build.hxml -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | haxe build.hxml 2 | 3 | --------------------------------------------------------------------------------