Jump to content

Loading uniform sprite sheets (not texturePacker based)


Recommended Posts



Since I use a lot of uniform sprite sheets (same image sizes without a texture packer description file).
I noticed that Pixi don't provide a standard way to load such sprite sheet (or maybe I didn't found it ?).

so here is a piece of code that do the job.

I tried to remain as compatible as possible with PIXI, by adding the frames to TextureCache.


function loadFramedSpriteSheet(textureUrl, textureName, frameWidth, frameHeight, cb){    var image = new PIXI.ImageLoader(textureUrl);    var texture = image.texture.baseTexture;    var frames=[];    image.addEventListener("loaded", function (event) {        var cols = Math.floor(texture.width / frameWidth);        var rows = Math.floor(texture.height / frameHeight);        var i=0;        for (var y=0; y<rows; y++)        {                    for (var x=0; x<cols; x++,i++)            {                        PIXI.TextureCache[textureName+'-'+i] = new PIXI.Texture(texture, {                    x: x*frameWidth,                    y: y*frameHeight,                    width: frameWidth,                    height: frameHeight                });                    frames.push(PIXI.TextureCache[textureName+'-'+i]);            }        }                if (typeof cb == 'function')        {            cb(frames);        }    });    image.load();    return frames;}


var stage = new PIXI.Stage(0xFFEEFF);var renderer = PIXI.autoDetectRenderer(400, 400);document.body.appendChild(renderer.view);loadFramedSpriteSheet('tileset.png', 'terrain', 64, 64, function(frames) {    var terrain = new PIXI.Sprite(frames[8]);    //we can also create the sprite from textureCache :    //var terrain = PIXI.Sprite.fromFrame('terrain-8');    terrain.position.x = 0;    terrain.position.y = 0;    stage.addChild(terrain);    renderer.render(stage);});
Link to comment
Share on other sites

  • 2 weeks later...

Hmm. What if I would like to split up the binary texture into multiple textures in order to use it together with the TilingSprite feature?
Atm you still use one binary copy of the data with different texture containers pointing to theyr parts.


But in fact I would like rlly split it up into different textures. Otherwise TilingSprite wont work in its current state.


Any sugestions?

Link to comment
Share on other sites

Yeah sure .. but somehow I rlly doupt this would be an efficient solution used in hight numbers. Then I would reather load multiple files from the beginning :/


I wouldn't assume it is bad performance before even trying it. Either way it is going to draw all those images to a canvas, I don't see much difference.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...