GitHub快速开始

API 参考取自源码中的 Go 文档注释,以英文书写。

package spriteframes

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

概述

Package spriteframes is experimental: its API and documented formats may change in a minor release. See the API stability policy. turns a run of raster frames from a renderer, a video model or a capture into atlas-ready frames: chroma key with despill, loop seam selection, a shared anchor and nearest-neighbor scaling. Every function is pure and deterministic and returns new images, leaving its input untouched.

索引

函数

func Anchor(frames []*image.NRGBA, mode AnchorMode) ([]*image.NRGBA, error)

Anchor crops each frame to its non-transparent bounding box and places it in a cell as large as the largest box, at the same anchor in every frame, so a subject that drifts inside its canvas stays put. The cell's pivot is (0.5, 1) for AnchorBottomCenter and (0.5, 0.5) for AnchorCenter. A fully transparent frame is refused.

func ChromaKey(frames []*image.NRGBA, opts KeyOptions) ([]*image.NRGBA, error)

ChromaKey returns copies of frames with the key colour made transparent. Each pixel is keyed on its own, so key-coloured holes enclosed by the subject (between an arm and the body, say) are cleared as well as the background around it.

func LoopSeam(frames []*image.NRGBA, opts SeamOptions) (start, end int, loop []*image.NRGBA, err error)

LoopSeam finds the start and end frames that are most alike, so playing frames[start:end] in a loop jumps from end-1 back to start as smoothly as the source moved from end-1 to end. It compares the frames as alpha-weighted luminance thumbnails at most 96 pixels on a side. The end frame itself is dropped, which removes a final frame that duplicates the first. Ties keep the longer loop, then the earlier start. All frames must be the same size.

func ResizeNearest(frames []*image.NRGBA, width, height int) ([]*image.NRGBA, error)

ResizeNearest scales each frame to width by height with nearest-neighbor sampling, which keeps hard pixel edges when reducing a render to a pixel-art grid.

类型

type AnchorMode string

AnchorMode is where Anchor places each frame's subject in the shared cell.

const (
	// AnchorBottomCenter puts the bottom edge of each subject on the cell's bottom edge, centered horizontally: feet stay planted.
	AnchorBottomCenter AnchorMode = "bottom-center"
	// AnchorCenter centers each subject in the cell.
	AnchorCenter AnchorMode = "center"
)
type KeyOptions struct {
	// Color is the background colour to remove; its alpha is ignored.
	Color color.NRGBA
	// Tolerance is the RGB Euclidean distance from Color at or below which a pixel becomes fully transparent.
	Tolerance uint8
	// Softness is the width of the linear alpha ramp above Tolerance, so pixels between Tolerance and Tolerance+Softness keep part of their alpha.
	Softness uint8
	// Despill is the strength, from 0 (off) to 1, with which the key's dominant channel is pulled down to the larger of the other two channels on edge pixels: pixels left partly transparent and opaque pixels next to a removed one.
	Despill float64
}

KeyOptions controls ChromaKey.

type SeamOptions struct {
	// MinFrames is the fewest frames the loop keeps (2 when below 2).
	MinFrames int
	// MaxFrames is the most frames the loop keeps (no limit when 0).
	MaxFrames int
}

SeamOptions bounds the loop LoopSeam picks.