I am designing a Spritebatch class, for static and dynamic objects with WebGL. A problem I'm facing is that I have read several different opinions on sprite batching but with one goal.
- Put all vertices in one buffer and render them
So in WebGL I'm using gl.bufferData(....,new Float32Array(size), gl.STEAM_DRAW)
.
And here is my first problem: when the spritebatch
should be dynamic then size
is not fixed and will increase, which leads to recreating the Float32Array
which I found out is very expensive for n ≥ 10000 (at least on my machine).
Second Problem, let's say I give the Float32Array(maxElements)
a maximum size, and I add 4 Sprites then I have allocated much more than I actually need.
Other way: I create Spritebatch when I know how many sprites I have. So basically I put 4 Sprites into my scene, then before rendering I create a Spritebatch with size 4 * numVerticesPerSprite
. However let's say I want to update Sprite 2, I need some kind of index structure in my Spritebatch class to identify at which position the vertices of Sprite 2 are stored inside the SpriteBatch (inside Float32Array()
).
So my question is :
How is a dynamic Spritebatch supposed to work and can you think of an indexing method to identify Sprite X's vertices inside the Float32Array
to update/remove them? (Additionally: Do particle systems solve what i am looking for? I assume they are also some kind of sprite batching)