iamnotarobot
iamnotarobot

Reputation: 41

drawing specific instances in gl.drawArraysInstanced

I'm using the instancing technique for rendering lots of rectangles and it's working great to draw everything in one go with gl.drawArraysInstanced. However, I'd also like more control of it and to be able to draw a specific or a set of specific rectangle instances.

I've researched quite a while and I didn't find any API call in webgl2 that would help me do it. It seems that the current specification is dated and it doesn't offer an API call that would allow me to take advantage of selectively drawing instances. It looks like that I could use .glDrawArraysInstancedBaseInstance but it's not available in webgl2.

The only solution I found was to create an additional buffer for every rectangle, selectively copy data from the instancing buffer to them, and then draw using the additional buffer with gl.drawArrays when I need only to render specific rectangles.

Does anyone have a better solution?

Upvotes: 2

Views: 457

Answers (1)

iamnotarobot
iamnotarobot

Reputation: 41

I found a better solution, not ideal though.

For specific draws, I bind the instancing vao as usual, then I offset the buffer pointer to the desired instanceID data region with gl.vertexAttribPointer and issue gl.drawArraysInstanced with only one instance.

Alternatively, When I need to draw all instances, I set the offset back to the beginning of the buffer and issue the drawing operations with all instances.

Upvotes: 2

Related Questions