Sign in to follow this  
Ezelia

Loading uniform sprite sheets (not texturePacker based)

Recommended Posts

Hello,

 

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.

code

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

Usage

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

Share this post


Link to post
Share on other sites

I'm asking if you would contribute back the changes you made to the original repository. The common way to do that is via a Pull Request (if the code is on GitHub), which is often shorten to PR.

 

Sorry, I forget that not everyone uses GitHub all the time!  :P

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Good point. But since the main goal was to reduce javascript object and DOM complexity im not quite sure about it.

Another Option could be to use a pixel shader, instead of the on board "tiled" flag, and pass a rect of the source to it.

Share this post


Link to post
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.

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

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.