Jump to content

Should I generate all textures on load, or should I instead just rely on Pixi's cache?


Tobi Reif
 Share

Recommended Posts

Am I right in the assumption that calling PIXI.Texture.fromImage often (and inside animation frames) is OK (perf-wise) because the loader (on load) placed the textures in the cache?

 

Or should I continue to create all textures on assets loaded?

eg as in

https://github.com/tobireif/boomchat/blob/master/web/js/main.js#L710 -> function onAssetsLoaded()

where I keep the textures in my own array/object, in order to save computation later (for perf).

 

But does this mean that each texture gets generated twice? Once by me and once by Pixi?

 

Should I instead only rely on Pixi's cache, or can there be a performance hit? (eg if the respective texture is not in Pixi's cache anymore)

 

(Info: I'm using Pixi v2 but I plan to switch to v3.)

Link to comment
Share on other sites

Bumped. This is a good question.

 

To me, I see this.

 

Pixi.Texture.fromImage() as a literal process.  To take some image, and to convert it into a Pixi Texture (uploading to GPU? Who knows). I do not expect any cache here because I am literally generating a new Texture from an image. And I expect it to do that.

But. Lets say:

var texture = PIXI.Texture.fromImage()

This is a reference to the texture.  In a loop..... such as (i < 1000), everything will reference "texture".  In other words, reuse texture. 

PIXI.Texture.fromImage() would create 1000 identical textures, rather than referencing the one that we originally created. I see this as 1000 times slower. 

I would love clarification. 

Link to comment
Share on other sites

fromImage does cache:

https://github.com/GoodBoyDigital/pixi.js/blob/master/src/core/textures/Texture.js#L278-L289

This can cause weirdness if you don't know it because many things using the same url there *will* share a texture object.

I wish we could remove these convience functions because they are causing more harm than good IMO. Really you should be loading things using the loader, then just use the textures and data it gives to you to create what you need via constructors. The `.from*` methods are really for simple stuff that doesn't even use the loader, or that you don't care about how it is made.
 

Or should I continue to create all textures on assets loaded?

 
Ahh! No! The loader creates textures for you! When you load an image, that resource has a `texture` property that is a shiny new pixi texture for that image, just use it.
 
For example:

PIXI.loader.add('derp', 'myimage.png').load(function (loader, resources) {    // use the texture that was created for us     var derpSprite = new PIXI.Sprite(resources.derp.texture);});

These are created for you by the textureParser.js middleware that creates a texture for any loaded Image objects.

Link to comment
Share on other sites

Thanks for the reply!

 

The case where the expected texture is not in Pixi's cache anymore can't occur, right? ("Cache" typically implies that yes it might still be there but otherwise create it.)

 

What I mean is - is the following true?

For every loaded image there will be a texture in resources.foo (after load) , and that texture will always be there.

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