Jump to content

Spritesheet


GiL_TheB
 Share

Recommended Posts

Hello, I'm new to pixi.js, I allready made great things, but I don't seem to be able to make an array of sprites from a spritesheet, so I can stamp them on a renderTexture later.

I did try different things but the result is wrong each time.

   j=0;
while(j<sheetRow){
  i=0;
while(i<sheetCol){

//only shows the last frame when display
  let rect = new PIXI.Rectangle(i*spritetRes,j*spritetRes, spritetRes, spritetRes);
  texture.frame = rect;
  let g = new PIXI.Sprite(texture);

//same as above
  let rect = new PIXI.Rectangle(i*spritetRes,j*spritetRes, spritetRes, spritetRes);
  texture.frame = rect;
  let g = new PIXI.Sprite(texture,rect);

//shows the whole texture as sprite
  let rect = new PIXI.Rectangle(i*spritetRes,j*spritetRes, spritetRes, spritetRes);
  let g = new PIXI.Sprite(texture,rect);

/* is out of order when display
0,0 1,0 2,0 3,0     0,0 3,0 2,0 1,0
0,1 1,1 2,1 3,1  => 0,1 3,2 2,2 1,2
0,2 1,2 2,2 3,2     0,2 3,1 2,1 1,1
*/
  let g=new PIXI.extras.TilingSprite(texture,spritetRes,spritetRes);
  g.tilePosition.set(i*spritetRes,j*spritetRes);

  graphics.push(g);
  i++;}
  j++;}

Thanks for PIXI I have great fun, even if things are very puzzling. Why not set a texture on a quad and set its UV in this case?

Link to comment
Share on other sites

So ... I made it work somehow, still I'd like to know is there is a better method, and I dont understand why the TilingSprite doesn't work.

I setup a Sprite just before rendering it. It is kind of weird you can't define a sprite beforehand, I must miss something obvious.

  let g = new PIXI.Sprite(texture);

//display the spriteSheet as sprites
  i=0;
while(i<sheetCol*sheetRow){
  texture.frame =  new PIXI.Rectangle((i%sheetCol)*spritetRes,((i/sheetCol)|0)*spritetRes, spritetRes, spritetRes);
  g.position=new PIXI.Point((i%sheetCol)*spritetRes,((i/sheetCol)|0)*spritetRes);
  renderer.render(g,renderTexture);
  i++;}

//reset the texture.frame
 texture.frame =  new PIXI.Rectangle(0,0,sheetCol*spritetRes,sheetRow*spritetRes);

.

Link to comment
Share on other sites

This works too, it is a bit counter-intuitive (see the little minus sign that change all).

  i=0;
while(i<sheetCol*sheetRow){
  let g=new PIXI.extras.TilingSprite(texture,spritetRes,spritetRes);
  g.tilePosition.set(
    -(i%sheetCol)*spritetRes,
    -((i/sheetCol)|0)*spritetRes
    );
  graphics.push(g);
  i++;}

 

Link to comment
Share on other sites

1 hour ago, GiL_TheB said:

This works too, it is a bit counter-intuitive (see the little minus sign that change all).

I think you are over-complicating things. https://github.com/kittykatattack/learningPixi also has a part about spritesheets.

Its not counter-intuitive: "tilePosition" is the position of (0,0) point of texture inside the TilingSprite, you are moving the background repeating image. TilingSprite was made for different purpose. It is also less performant: every instance is eats a drawcall, while sprites are batched up to several thousands per drawcall.

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.

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

  • Recently Browsing   0 members

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