Jump to content

Preload all textures?


pdiddles03
 Share

Recommended Posts

Should I create all the textures I need at the render of the game when it first starts or should i render them as I need them?  I am not talking about preload, but the PIXI.Texture.fromImage type thing.  If it is ok to render all of them it would solve a lot of issues for me.

Link to comment
Share on other sites

PIXI.loader will automatically create all textures for you from loaded images. You just need to pick one from PIXI.loader.resources and you can use it whenever you want to

 

 

1st: preload images

var loader = PIXI.loader			.add('image1', 'image1.png')			.add('image2', 'image2.png')			.once('complete', function(loader, resources) {				init();			})			.load();

2nd: use it

function init() {    var sprite1 = new PIXI.Sprite(PIXI.loader.resources.image1.texture);    var sprite2 = new PIXI.Sprite(PIXI.loader.resources.image2.texture);}
Link to comment
Share on other sites

Or:

PIXI.loader  .add("images/textureOne.png")  .add("images/textureTwo.png")  .load(setup);function setup() {    var spriteOne = PIXI.Sprite.fromImage("images/textureOne.png");    var spriteTwo = PIXI.Sprite.fromImage("images/textureTwo.png");}
Link to comment
Share on other sites

 

Or:

PIXI.loader  .add("images/textureOne.png")  .add("images/textureTwo.png")  .load(setup);function setup() {    var spriteOne = PIXI.Sprite.fromImage("images/textureOne.png");    var spriteTwo = PIXI.Sprite.fromImage("images/textureTwo.png");}

 

No! Please don't encourage people to mix the loader and convenience methods. I spend half my time on this forum explaining why they don't mix well and why code is often broken when you try to mix them.

 

Just use the loader values if you are using the loader. If you don't want to preload and want to just create a sprite from arbitrary string urls, then use the convenience methods. DON'T MIX THEM PLEASE!! It only works under very specific circumstances and most of the time will be broken or duplicate objects in memory. The two APIs are not related, using the loader to warm the internal texture cache for use with those methods is a hack and should not be used in production code. The texture cache for convenience methods is private and could change at anytime DO NOT RELY ON THIS BEHAVIOR.

Link to comment
Share on other sites

The two APIs are not related, using the loader to warm the internal texture cache for use with those methods is a hack and should not be used in production code.

I didn't know that!  :o  I naively assumed the `from` methods were just dumb, higher lever wrappers that accessed the texture cache with the convenience of less code to type.

 

Is there a way of using the loader values without having to create a string identifier for the textures?

For example, instead of this:

.add('image1', 'image1.png')

Could I use this:

.add('image1.png')

And, if I can, how would I access it in the loader?

var sprite1 = new PIXI.Sprite(PIXI.loader.????????);
Link to comment
Share on other sites

Another question:

What's the best way to access frame ids from a loaded texture atlas on the `resources` object?

 

Exactly how they are written out in the json. That is usually filename if using JSON Hash format, or numeric index if using JSON Array format.

 

It will look like this:

PIXI.loader.add("sheet", "mysheet.json").load(function (loader, resources) {    resources.sheet.textures['some_frame.png'];});
Link to comment
Share on other sites

Thanks so much, Xerver,! So they're all in the JSON resource's `textures` object - I'll look for them there.

 

I have two more questions related to this topic.

 

1. If I'm using pre-loading textures with the loader, is it still acceptable to create a sprite's by referencing the texture cache, like this?

var sprite = new PIXI.Sprite(PIXI.utils.TextureCache["anyTexture.png"]);

2. Should I access video textures in the same way, like this?

var sprite = new PIXI.Sprite(PIXI.loader.resources['video.mp4'].texture);

... and is there an alternative to using `fromVideoUrl` - or is that `from` method OK because it doesn't reference a loaded texture?

Link to comment
Share on other sites

1. I would just avoid the TextureCache, you don't need to use it and it won't support all the features in the loader. Just treat the resources object like the texture cache, its easier to use anyway.

2. The loader does not currently have support for loading videos and parsing them into video textures.

To be clear all the `.from*()` methods are OK, nothing wrong with using them. Just don't try to mix them with the loader and be clever about preloading usages of those APIs. Assume if you do `.from*()` it will make a request for the url you pass in.

Link to comment
Share on other sites

  • 5 months later...
On 30.7.2015 at 3:47 AM, xerver said:

 

Exactly how they are written out in the json. That is usually filename if using JSON Hash format, or numeric index if using JSON Array format.

 

It will look like this:


PIXI.loader.add("sheet", "mysheet.json").load(function (loader, resources) {    resources.sheet.textures['some_frame.png'];});

Is there a way to get the image without knowing what sheet it was in without using texturecache? I have a situation where I optimize the size of sheets by building them based on color and then storing them as 8bit images whenever color amount is small enough, which means that the images swap sheets pretty often.

I could build my own cache by going through the assets in sheets and mapping them, but seeing as the textureCache already contains that information that would seem a bit of a waste to do it twice. Is textureCache the correct way to get images in this situation?

Link to comment
Share on other sites

1) For now you can use Sprite.fromImage and Texture.fromImage if you dont know which sheet has it. thats correct way for pixiv3. Those two methods work through global  texture cache.

2) @xerver way is different but not features were implemented for it: add special middleware to loader that will create cache for that specific loader.

@xerver thinks that global cache is evil. I admit, it is true for big applications with lots of dynamic textures :) 

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