davidfig / pixi-cull

a library to visibly cull objects designed to work with pixi.js
MIT License
109 stars 15 forks source link

Example using SpatialHash? #14

Open PabloNeirotti opened 3 years ago

PabloNeirotti commented 3 years ago

Hi there. Given the large number of objects I am testing this with, it's very clear to me that I need to cull objects in chunks, since the process of culling alone tanks performance quite a bit.

I can't figure out how to use SpatialHash though. Is there any example you could share, or a test you have laying around of it? Or some use instructions at least.

I'm currently getting errors like not having containers (but if I have to create containers myself, what's the difference than using the regular SimpleHash and passing containers to that instead?).

Any clarification would be appreciated.

Thank you for your work.

davidfig commented 3 years ago

The sample code in docs/ works with the SpatialHash.

The spatial hash does not place objects into pixi-containers (although that is certainly a way to implement it). I wanted more flexibility where objects with different parents can be culled. The implementation uses "virtual" containers to perform the culling. Children within the virtual containers are culled based on whether the container itself is within bounds.

For addContainer(), the spatial hash adds the container to an array, and then when cull() is called, it iterates through its children (think of addContainer as a convenience function.

I hope that helps. Let me know if you need additional info.

PabloNeirotti commented 3 years ago

Thank you for the response.

Do you mean this code?

var cull = new Cull.Simple();
cull.addList(viewport.children);
cull.cull(viewport.getVisibleBounds());

Because if so, I tried to use it by switching the Simple for SpatialHash. However several things don't work. addList does not exist, and using another method to add objects (I forgot which) it crashes the code because it says there is no container created yet (the error was something like this.containers[0] is undefined)

Thank you!

davidfig commented 3 years ago

I was thinking more of this file: https://github.com/davidfig/pixi-cull/blob/master/docs/index.js, which is from the demo. But looking at it, it doesn't do much except call spatialHash.addContainer()

Try:

const cull = new Cull.SpatialHash()
cull.addContainer(viewport)
cull.cull(viewport.getVisibleBounds())

Note that addContainer is dynamic, so if you change the children in viewport, then it will automatically cull the new children. Since it's expecting a PIXI.Container, you don't pass the container.children.