Documentation
¶
Overview ¶
Package pack packs a folder of PNG images into sprite atlas pages and TexturePacker JSON-hash metadata that sprite.LoadAtlas reads directly.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Frame ¶
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 ¶
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 ¶
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 ¶
func DefaultOptions() Options
DefaultOptions returns Options with the packer's defaults: 1px padding, no extrusion, 2048x2048 pages, scale 1 and trimming enabled.
type Page ¶
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 ¶
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 ¶
type Result struct {
Pages []*Page
}
Result holds all atlas pages produced by PackDir or PackImages.
func PackDir ¶
PackDir walks dir for PNG images in deterministic slash-separated relative path order and packs them with opts.
func PackImages ¶
PackImages trims and packs inputs into one or more atlas pages with opts.
Example ¶
PackImages packs sprites onto atlas pages and writes the TexturePacker JSON that sprite.LoadAtlas reads.
package main
import (
"fmt"
"image"
"image/color"
"github.com/nsgo-dev/game/pkg/tooling/pack"
)
func filled(w, h int) image.Image {
img := image.NewNRGBA(image.Rect(0, 0, w, h))
for y := range h {
for x := range w {
img.SetNRGBA(x, y, color.NRGBA{R: 200, A: 255})
}
}
return img
}
// PackImages packs sprites onto atlas pages and writes the TexturePacker JSON that sprite.LoadAtlas reads.
func main() {
res, err := pack.PackImages([]pack.Input{
{Name: "hero.png", Image: filled(32, 32)},
{Name: "coin.png", Image: filled(8, 8)},
{Name: "door.png", Image: filled(16, 32)},
}, pack.Options{Name: "sprites", Padding: 1, MaxWidth: 64, MaxHeight: 64})
if err != nil {
fmt.Println(err)
return
}
page := res.Pages[0]
fmt.Println(len(res.Pages), page.ImageName, page.JSONName)
for _, f := range page.Frames {
fmt.Println(f.Name, f.Frame.Dx(), f.Frame.Dy())
}
}
Output: 1 sprites.png sprites.json coin.png 8 8 door.png 16 32 hero.png 32 32