Hello… again! It’s me, with yet another sprite-related issue.
I finally managed to finish my animation system, using the APIs provided by BABYLON.SpritePackedManager
and BABYLON.Sprite
. Unfortunately, there’s a huge problem: It’s incredibly slow when adding more than a couple dozen sprite instances: Performance degrades rapidly after just 100-150 sprites (even on my 4.4 GHz Desktop PC) and it’s nowhere near enough for what I had in mind.
Some of that might be lack of optimization on my part, but seeing how other engines and games can render tens of thousands of sprite instances without issues, it appears there’s something fundamentally “wrong” (for lack of a better word) with the way my animations are implemented.
After doing a lot of research, this is my understanding so far:
-
Sprites instantiated are all part of the same mesh represented by the sprite manager, and all rendered in one draw call (since it uses just one texture)
-
Whenever the position of any one of those instances changes, BJS must rebuild the mesh/resend the vertex data for the entire mesh? This would mean, with a large number of animated sprites, it’d have to do this virtually very frame, explaining the serious slowdown I’m experiencing
-
It should be possible to use hardware instancing for sprites just like it’s used for regular meshes and particles; if sprites are simply rendered on a plane and “animation” is merely changing the position and/or UV data of the mesh’s vertices, then it’s quite similar, no?
-
From some old forum threads, it appears BJS doesn’t support instancing for sprites, and implementing it would require some shady, I mean, shader stuff which I didn’t really understand
Please do correct me if I got something wrong!
Now, this makes me wonder:
-
Is it in fact possible to do exactly what BJS’s sprite APIs do, except using instances?
-
If so, why does the sprite manager use just one mesh instead of proper instancing?
-
If not, how could this be achieved? It must be possible since I’ve seen it in other engines, but I’m unsure how they did it
-
How is this different from the
SolidParticleSystem
? Clearly it’s capable of rendering lots of particles, but it doesn’t seem to have any support for sprite-like animations
The goal would be one draw call per texture AND benefiting from instanced meshes to allow rendering thousands of individual sprites [edit: at 60 FPS, obviously].
All the sprites I’m using are similar in the sense that they represent the same entities, but not identical, meaning they need to be animated independently by changing UVs/cellIndex, position, alpha, visibility, rotation, and scaling/size.
Ideally, if there’s a way to do this it should be just as easy as using the sprite manager/sprite APIs, since that’s (presumably) why they exist in the first place. Is there anything like it, and if not how would one best go about creating it? Hopefully one of you has some ideas
Thank you for your time!