Jump to content

Please help me to understand cacheAsBitmap


ForgeableSum
 Share

Recommended Posts

Can someone please help me understand cacheAsBitmap. I can't understand for the death of my and I've probably read this paragraph 1000 times:

 

Set if this display object is cached as a bitmap. This basically takes a snap shot of the display object as it is at that moment. It can provide a performance benefit for complex static displayObjects. To remove simply set this property to 'null'

 

 

Does it get "set" when you change the property to true or is it an internal thing? If it does get added to cache, what key is it assigned? When would you ever use this? 

Link to comment
Share on other sites

I've used it to improve performance when I had a graphics object that was made up of many different sprites, but that wouldn't change over time. I put all of them in a group and set cacheToBitmap to true on the Group. That's down to 1 draw call from literally hundreds. It caches the screenshot not in the cache, but in itself, and at render time the renderer checks to see if there's one there (and cacheAsBitmap is on) and if so, uses that instead of rendering the whole structure bit by bit

Link to comment
Share on other sites

If you have a 100 objects moving around insides a container, Pixi has 100 things it needs to keep track of.

That's a lot of work for Pixi to do.

`cacheAsBitmap` (temporarily) turns all of those objects into one object.

That means Pixi now has 99 fewer things to do, so it saves you a bit of processing overhead.

 

You can turn on `cacheAsBitmap` for any Pixi Container or Sprite like this:

anyContainer.cacheAsBitmap = true;

One side effect is that if those 100 objects are moving around, `cacheAsBitmap` freezes their movement.

(That's why it's like taking a snapshot.)

But, if you set `cacheAsBitmap` back to `false`, all those objects will start moving again. 

 

Here's a working example:

http://pixijs.github.io/examples/index.html?s=demos&f=cacheAsBitmap.js&title=CacheAsBitmap

Link to comment
Share on other sites

If you have a 100 objects moving around insides a container, Pixi has 100 things it needs to keep track of.

That's a lot of work for Pixi to do.

`cacheAsBitmap` (temporarily) turns all of those objects into one object.

That means Pixi now has 99 fewer things to do, so it saves you a bit of processing overhead.

 

You can turn on `cacheAsBitmap` for any Pixi Container or Sprite like this:

anyContainer.cacheAsBitmap = true;

One side effect is that if those 100 objects are moving around, `cacheAsBitmap` freezes their movement.

(That's why it's like taking a snapshot.)

But, if you set `cacheAsBitmap` back to `false`, all those objects will start moving again. 

 

Here's a working example:

http://pixijs.github.io/examples/index.html?s=demos&f=cacheAsBitmap.js&title=CacheAsBitmap

So basically, you should always use cacheAsBitmap for images that are static and not moving?

 

This has nothing to do with Phaser.Cache? 

Link to comment
Share on other sites

Okay, it's finally making sense to me now. I guess what threw me off is that it's more something that applies to containers of objects and sprites aren't typically containers (unless they have child sprites). 

 

But the basic idea is that if you have a large number of objects in the same container (sprite or group), you can save a lot of memory if you cache them as a bitmap. but you can only do that when they are not moving/being altered in any way. Correct?

 

Shit I can think of a lot of scenarios where this is useful. I wonder if it saves on performance just for physics-enabled objects or does it also save on performance for objects without physics? 

 

And this is something happening at the rendering level, correct? So I can still perform calculations on my cached objects in the same way I would have if they were not cached? 

Link to comment
Share on other sites

not quite correct... you are saving cpu time at the expense of memory, not the other way around :) also, working with physics is going to be tricky because iirc the transformations for all children of a cached object are not applied separately (otherwise, why bother caching). that includes physics stuff. so it's not simply a magic turbo button

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