Skip to main content
Notice removed Authoritative reference needed by greedsin
Bounty Ended with Jimmy's answer chosen by greedsin
reserve code markup for actual code. terminology doesn't need to be in code. also, it is "I am" or "I'm" (which is a contraction of "I am"), it is not correct to write "I'am"
Source Link
doppelgreener
  • 7.3k
  • 7
  • 43
  • 69

I'amI am designing a Spritebatch classSpritebatch class, for static and dynamic objects with WebGLWebGL. A problem I'amI'm facing is that I have read several different opinions on sprite batchingsprite batching but with one goal.

  • Put all vertices in one buffer and render them

So in WebGLWebGL I'amI'm using gl.bufferData(....,new Float32Array(size), gl.STEAM_DRAW). And

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 >= 10000n ≥ 10000 (at least on my machine).

Second Problem, let's say iI give the Float32Array(maxElements) a maximum size, and iI add 4 Sprites4 Sprites then I have allocated much more than I actually need.

Other way  : I create SpritebatchSpritebatch when I know how many Spritessprites I have. So basically I put4 Sprites 4 Sprites into my scene, then before rendering I create a SpritebatchSpritebatch with size 4*numVerticesPerSprite4 * numVerticesPerSprite. However letslet's say I want to update Sprite 2Sprite 2, I need some kind of index structure in my Spritebatch classSpritebatch class to identify at which position the verticesvertices of Sprite 2Sprite 2 are stored inside the SpriteBatchSpriteBatch (inside Float32Array()).

So my question is :

How is a dynamic SpritebatchSpritebatch supposed to work and can you think of an indexing method to identify Sprites XSprite X's vertices inside the Float32Array to update/remove them? (Additionally: Do particle systemsparticle systems solve what i am looking for? I assume they are also some kind of sprite batchingsprite batching)

I'am designing a Spritebatch class, for static and dynamic objects with WebGL. A problem I'am 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'am 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 put4 Sprites into my scene, then before rendering I create a Spritebatch with size 4*numVerticesPerSprite. However lets 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 Sprites X 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)

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)

Notice added Authoritative reference needed by greedsin
Bounty Started worth 50 reputation by greedsin
Source Link
greedsin
  • 118
  • 1
  • 9

Removing/Updating Spritebatch

I'am designing a Spritebatch class, for static and dynamic objects with WebGL. A problem I'am 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'am 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 put4 Sprites into my scene, then before rendering I create a Spritebatch with size 4*numVerticesPerSprite. However lets 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 Sprites X 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)