Drawing multiple instanced objects with unique positions in vertex shader

#1

Hi, I am trying to figure out how to pass in an array of of3dPrimative objects into a vertex shader using drawInstanced and then set the position of each object in a unique position so they are not overlapping each other. I’ve spent a week trawling through examples but everything seems to only use one 3d primitive.

For example, in the image below i’m calling draw instanced on a cube and a sphere and then setting the positions into a grid using gl_instanceID in the vertex shader… What I want to be able to do is say:
if (gl_InstanceID % 2 == 0) drawSphere();
if (gl_InstanceID % 2 == 1) drawCube();

Is there any way of distinguishing between the different objects in the vertex shader to do this? Thanks.

#2

not sure i understand what you want to do but the gl_InstanceID will be the same when drawing one geometry or the other, it only tells you which instance is being drawn. If you want to do different things for sphere and cube you cna just use a uniform or even better have 2 shaders using the same code with ifdefs

#3

@arturo nice that did the trick. Was trying to over complicate things. Thanks for the tip!

#4

Hi! can you share this piece of cod?e

#5

Hi @pxt. Here is the project I ended up making with this technique.

You need to pass in an active index state into each one and then check against gl_InstanceID in your shader to see if you should draw or skip that index.