Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What format is expected for data provided to build a Pixi Spritesheet?

I would like to take advantage of PIXI.Spritesheet to get some kind of collection of textures or sprites to use in a pixi application.

let spritesheetTexture = PIXI.utils.TextureCache["my-spritesheet-image.png"];
let data = ??????
let spritesheet = new PIXI.Spritesheet(spritesheetTexture, data);

Unfortunately the pixi documentation gives no indication of what format the data parameter needs to be in, besides "object" and "Spritesheet image data". I also cannot find any examples of this class in action.

like image 761
Luke Avatar asked Mar 06 '18 23:03

Luke


2 Answers

For anyone still looking for an official answer to this, the typescript defs in the current (Oct., 2020) Pixi.js repository (see Spritesheet.ts) contains the complete schema. These typedefs do not make it into node_modules when you import the package, but if you look through the code, it does look like this describes all of the data pixijs looks for when parsing a spritesheet object.

export interface ISpritesheetFrameData {
    frame: {
        x: number;
        y: number;
        w: number;
        h: number;
    };
    trimmed?: boolean;
    rotated?: boolean;
    sourceSize?: {
        w: number;
        h: number;
    };
    spriteSourceSize?: {
        x: number;
        y: number;
    };
    anchor?: IPointData;
}

/**
 * Atlas format.
 */
export interface ISpritesheetData {
    frames: Dict<ISpritesheetFrameData>;
    animations?: Dict<string[]>;
    meta: {
        scale: string;
    };
}
like image 113
Andrew Avatar answered Nov 09 '22 23:11

Andrew


The data argument should be a JSON-like object listing all frames of a texture atlas (spritesheet) and should look like this:

{
    "meta": {
        "image": "atlas.png"
    },
    "frames": {
        "icon_1.png": {
            "frame": {"x":0, "y":0, "w":32, "h":32},
            "sourceSize": {"w": 32, "h": 32}
        },
        "icon_2.png": {
            "frame": {"x":32, "y":0, "w":64, "h":64},
            "sourceSize":{"w": 64, "h": 64}
        },
        ...
    }
}

But I don't think you should write that by hand nor use PIXI.Spritesheet directly (unless you are doing something more advanced than just display images from a texture atlas). This JSON structure should be exported by a software that packs multiple images to a single texture (e.g. TexturePacker) and such accompanying JSON file which then should be loaded using PIXI's loader like this:

PIXI.loader
    .add('atlas', 'atlas.json')
    .load(onAssetsLoaded);

function onAssetsLoaded() {
    ...
}

See also this example.

like image 25
HankMoody Avatar answered Nov 09 '22 23:11

HankMoody