Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIXI.utils.TextCache instead of PIXI.loader.resource? #62

Open
leeenx opened this issue Jun 17, 2017 · 7 comments
Open

PIXI.utils.TextCache instead of PIXI.loader.resource? #62

leeenx opened this issue Jun 17, 2017 · 7 comments

Comments

@leeenx
Copy link

leeenx commented Jun 17, 2017

I thought every texture that Pixi create, will be stored in PIXI.utils.TextCache. But as use PIXI.loader, Pixi’s development team recommends use PIXI.loader.resources.

I am confused aboute it. So, I make a demo:

PIXI.loader
	.add([
		"images/1.png", 
		"images/2.png"
	]) 
	.load(function() {
		var sprite = new PIXI.Sprite(PIXI.utils.TextureCache["images/[email protected]"]); 
		app.stage.addChild(sprite); 
	}); 

I found that PIXI.utils.TextCache works well。

Can I always use PIXI.utils.TextCache instead of PIXI.loader.resource.

@ova2
Copy link

ova2 commented Dec 10, 2017

I'm interested in the answer too. @kittykatattack What is the difference between usage of PIXI.utils.TextureCache and PIXI.loader.resources in the load callback?

Thanks in advance.

@RexSkz
Copy link

RexSkz commented Dec 11, 2017

I found something (loader.js#L74-L83) in PIXI's source code, the textureParser calls fromLoader, which can add resource into cache (Texture.js#L452-L453). So once the resource is loaded, you can find them in PIXI.utils.TextureCache.

Then I found the loader of PIXI extends from englercj/resource-loader, this line (Loader.js#L389-L401) means that everything you have loaded will be destroyed once you call loader.reset().

In a word, reset can only destroy resources in Loader, not in TextureCache. So I think at this moment using PIXI.utils.TextureCache instead of PIXI.loader.resources is better, unless you prefer writing a cache system yourself. (See "Update 2".)


Update 1:

Sometimes I have to call reset so that I can load more resources in bad network situation. And reset can destroy all resources I have loaded, so I can't use texture in PIXI.loader.resources anymore, but PIXI.utils.TextureCache still work.


Update 2:

After some searching, I found I was wrong in some words, see the reply below.

@ova2
Copy link

ova2 commented Dec 11, 2017

@RexSkz Thanks for the explanation. But why the Pixi’s development team recommends use PIXI.loader.resources then? :-)

@RexSkz
Copy link

RexSkz commented Dec 11, 2017

@ova2 I did some searching, maybe (or maybe not) you're talking about this article. The developer shows that PIXI provides two ways to load images: PIXI.loader and fromImage, the latter one (which I didn't know before) uses the TextureCache, and when we have loaded the resource, it won't reload again.

So I come to a conclusion:

  • When you're writting a callback function for loader.setup, use PIXI.loader.resources.
  • When you're sure the image has been loaded, use PIXI.Texture.fromImage.
  • DO NOT use PIXI.utils.TextureCache anyway (which shows that my previous reply was wrong, sorry about that).

@kittykatattack
Copy link
Owner

@ova2 Go ahead and use TextureCache, it's fine! 😄
I've never encountered a situation where it doesn't work.

@ova2
Copy link

ova2 commented Dec 11, 2017

Thanks for your answers, guys. @kittykatattack I just bought your PIXI book. It's amazing :-) Do you plan a second edition with last changes and more content? I know API is almost the same, but some advanced content like WebGL Shaders would be great to have in the book.

@kittykatattack
Copy link
Owner

@ova2 Thanks! I I don't have plans for a second edition yet - but maybe when Pixi v5 stabilizes.
(There are some API changes between v3 and v4, but fortunately v4 is backwards compatible so all the v3 code will work - and you'll get messages in the console telling you what the API changes have been.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants