GitHub快速開始

API 參考取自原始碼中的 Go 文件註解,以英文撰寫。

package pack

import "github.com/nsgo-dev/game/pkg/tooling/pack"

概覽

Package pack packs a folder of PNG images into sprite atlas pages and TexturePacker JSON-hash metadata that sprite.LoadAtlas reads directly.

索引

函式

func ScanDir(dir string) ([]string, error)

ScanDir returns the PNG images PackDir packs from dir: the slash-separated paths relative to dir of every file with a .png extension in any letter case, sorted, skipping files and directories whose names start with a dot. It fails when dir is not a directory or holds no PNG image.

型別

type Frame struct {
	// Name is the frame's key in the atlas JSON.
	Name string
	// Frame is the frame's pixel rectangle inside the atlas page image, excluding any extrusion or padding.
	Frame image.Rectangle
	// SpriteSourceSize is where the trimmed content sits inside the untrimmed source image.
	SpriteSourceSize image.Rectangle
	// SourceSize is the untrimmed source image's pixel dimensions.
	SourceSize image.Point
	// Trimmed reports whether transparent borders were removed from the source image.
	Trimmed bool
	// Page is the zero-based page index holding this frame.
	Page int
	// Pivot is the normalized frame pivot when set on the input, or nil when omitted.
	Pivot *Pivot
}

Frame describes where one input image landed in a packed atlas page.

type Input struct {
	// Name is the frame name written to the atlas JSON, such as "hero.png" or "enemies/slime.png".
	Name string
	// Image is the source picture.
	Image image.Image
	// Pivot is the optional normalized pivot written into the frame's TexturePacker JSON pivot field; nil omits it so sprite.LoadAtlas uses (0, 0).
	Pivot *Pivot
}

Input is one named image to pack into an atlas.

type Options struct {
	// Name is the base name for each page's PNG and JSON files ("atlas" when empty).
	Name string
	// Padding is the number of transparent pixels kept between adjacent frames.
	Padding int
	// Extrude is the number of pixels each frame's border is repeated outward to prevent texture bleeding.
	Extrude int
	// MaxWidth and MaxHeight limit each atlas page in pixels (2048 when 0).
	MaxWidth  int
	MaxHeight int
	// Scale is written to meta.scale so sprite.LoadAtlas converts pixels to points (1 when 0).
	Scale int
	// Trim removes transparent borders around each input image before packing.
	Trim bool
}

Options controls how images are trimmed, padded, extruded and packed into atlas pages.

func DefaultOptions() Options

DefaultOptions returns Options with the packer's defaults: 1px padding, no extrusion, 2048x2048 pages, scale 1 and trimming enabled.

type Page struct {
	// Index is the page's zero-based index.
	Index int
	// ImageName is the PNG file name written to meta.image (for example "atlas.png" or "atlas-0.png").
	ImageName string
	// JSONName is the JSON file name (for example "atlas.json" or "atlas-0.json").
	JSONName string
	// Image is the packed RGBA8888 atlas image.
	Image *image.NRGBA
	// JSON is the encoded TexturePacker JSON-hash metadata for this page.
	JSON []byte
	// Frames lists the frames packed onto this page, sorted by Name.
	Frames []Frame
}

Page is one packed atlas image and its TexturePacker JSON-hash metadata.

type Pivot struct {
	X float64
	Y float64
}

Pivot is a frame's normalized pivot as fractions of its untrimmed source size: (0, 0) is top-left, (0.5, 0.5) is center, and (0.5, 1) is bottom-center.

type Result struct {
	Pages []*Page
}

Result holds all atlas pages produced by PackDir or PackImages.

func PackDir(dir string, opts Options) (*Result, error)

PackDir walks dir for PNG images in deterministic slash-separated relative path order and packs them with opts.

func PackImages(inputs []Input, opts Options) (*Result, error)

PackImages trims and packs inputs into one or more atlas pages with opts.

func (r *Result) Write(dir string) ([]string, error)

Write writes every page's PNG image and JSON metadata into dir, creating dir if needed, and returns the paths written.