spritepacker

command module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2025 License: MIT Imports: 5 Imported by: 0

README

🧩 SpritePacker

SpritePacker is a 2D sprite sheet packing tool designed for game developers.


🛠 Usage

CLI Command
go install github.com/91xusir/spritepacker@latest
spritepacker -help
🛠️ Packing Options
Parameter Description
-input Input directory containing sprite images (required)
-output Output directory (default: output)
-maxw Maximum atlas width (default: 2048)
-maxh Maximum atlas height (default: 2048)
-padding Padding between sprites (default: 2)
-rotate Enable sprite rotation for better space utilization
-pot Force power-of-two dimensions for atlas
-name Base name for output atlas (default: atlas)
-sort Sort images before packing (enabled by default)
-trim Trim transparent edges (enabled by default)
-tolerance Alpha tolerance for trimming (0-255, default: 1)
-same Enable duplicate image detection (enabled by default)
-algo Packing algorithm: 0 = Basic, 1 = Skyline, 2 = MaxRects
-heuristic MaxRects heuristic (only for -algo 2): 0 = BestShortSideFit, 1 = BestLongSideFit, 2 = BestAreaFit, 3 = BottomLeft, 4 = ContactPoint
🛠️ Unpacking Options
Parameter Description
-unpack Path to atlas JSON file (required)
-img Path to atlas image (optional, inferred from JSON)
-output Output directory (optional, inferred from JSON)
📦 Examples
# Pack all images in ./sprites with default settings
spritepacker -input ./sprites -output ./atlases

# Custom atlas size, padding, and enable rotation
spritepacker -input ./sprites -maxw 1024 -maxh 1024 -trim -

# Unpack atlas using atlas.json
spritepacker -unpack ./atlases/atlas.json -output ./unpacked

🧪 API

go get -u github.com/91xusir/spritepacker

Example Usage:

package main

import (
	"github.com/91xusir/spritepacker/pack"
	"encoding/json"
	"os"
	"path/filepath"
)

func Pack() {
	// Step 1: Create packing options and set the maximum atlas size to 4096x4096
	options, _ := (pack.NewOptions().
		MaxSize(4096, 4096)).
	    Trim(true).
		Sort(true).
		Padding(-1).
		//AllowRotate(true).
		//Algorithm(pack.AlgoSkyline).
		//Heuristic(pack.ContactPointFit).
		//AutoSize(true).
		//PowerOfTwo(true).
		Validate()	// Optional: Validate the options. If invalid, an error will be returned.

	// Step 2: Create the sprite packer using the configured options
	spritePacker := pack.NewPacker(options)

	// Step 3: Collect all sprite image paths
	spriteImgPaths, _ := pack.GetFilesInDirectory("input")

	// Step 4: Pack all sprite images and generate atlas metadata and images
	spriteAtlasInfo, atlasImages, _ := spritePacker.PackSprites(spriteImgPaths)

	// Step 5: Save each atlas image to the "output" directory
	for i := range atlasImages {
		outputPath := filepath.Join("output", spriteAtlasInfo.Atlases[i].Name)
		_ = pack.SaveImg(outputPath, atlasImages[i], pack.PNG, pack.WithCLV(pack.DefaultCompression))
	}
	// Step 6: Save the atlas metadata as a JSON file
	jsonBytes, _ := json.MarshalIndent(spriteAtlasInfo, "", "  ")
	_ = os.WriteFile("output/atlas.json", jsonBytes, os.ModePerm)

}

func Unpack() {
	// default output and atlas images path is the same as the atlas.json path
	// if you want to change this, you can use the following code:
	// pack.UnpackSprites("output/atlas.json", pack.WithAtlasImgPath("output"), pack.WithOutputPath("output"))
	err := pack.UnpackSprites("output/atlas.json")
	if err != nil {
		return
	}
}

✅ TODO List

  • Custom output format using Go templates
  • more format support
  • GUI

Documentation

Overview

Package main provides a command-line tool for sprite atlas packing and unpacking.

This tool supports: - Creating optimized sprite atlases from individual images - Unpacking existing sprite atlases back to individual images - Multiple packing algorithms and heuristics - Various sprite processing options

Usage:

spritepacker -input <dir> [options]       # Pack mode
spritepacker -unpack <json> [options]     # Unpack mode

Packing Options:

-I string      Input directory containing sprite images (required for packing)
-o string     Output directory (default "output")
-maxw int          Maximum atlas width (default 2048)
-maxh int          Maximum atlas height (default 2048)
-pad int       Padding between sprites (default 2)
-rot            Allow sprite rotation to save space
-pot               Force power-of-two atlas dimensions
-name string       Base name for output files (default "atlas")
-sort              Sorts sprites before packing (default true)
-trim              Trims transparent edges (default true)
-tol int     Transparency tolerance for trimming (0-255, default 1)
-same              Enable identical image detection (default true)
-algo int          Packing algorithm (0=Basic, 1=Skyline, 2=MaxRects)
-heur int     MaxRects heuristic (0-4, see docs)

Unpacking Options:

-u string     JSON file to unpack (required for unpacking)
-img string        Atlas image path (optional, will search by name)
-o string     Output directory for unpacked sprites

Examples:

# Pack sprites with default settings
spritepacker -input ./sprites -output ./atlases

# Pack with custom settings
spritepacker -input ./sprites -maxw 1024 -maxh 1024 -padding 4 -rotate

# Unpack atlas
spritepacker -unpack ./atlases/atlas.json -output ./unpacked

The package includes: - Multiple packing algorithms (Basic, Skyline, MaxRects) - Support for common image formats (PNG, JPEG, BMP, TIFF) - Sprite trimming and optimization features - JSON metadata generation

Directories

Path Synopsis
Package pack provides texture packing utilities, including support for rotation, transparent pixel trimming, automatic resizing and so on.
Package pack provides texture packing utilities, including support for rotation, transparent pixel trimming, automatic resizing and so on.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL