最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What format is expected for data provided to build a Pixi Spritesheet? - Stack Overflow

programmeradmin5浏览0评论

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.

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.

Share Improve this question edited Oct 2, 2019 at 15:21 user128511 asked Mar 6, 2018 at 23:43 LukeLuke 20.1k16 gold badges110 silver badges120 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

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 plete 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;
    };
}

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 acpanying 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.

发布评论

评论列表(0)

  1. 暂无评论