Drawing InstancedMesh with an offset


I’m curious if it’s somehow possible to specify an offset inside the instanceMatrix attribute of a InstancedMesh so that rendering the instances start from there.

Here’s a pen showing what I mean. Essentially I need to draw an intance several times, but with different materials. It’s perfectly fine to make multiple calls to drawArraysInstanced or drawElementsInstanced, but between those calls I need the call to vertexAttribPointer with desired the offset.

Looking into three’s source code for instancing I don’t seem to find a direct approach, but maybe there’s something I’m missing.


1 Like

I think you have to use one InstancedMesh per material, since there’s no starting offset, only count. Also threndering one group, then changing material, and rendering the next group… may stall the pipeline, vs just putting multiple instancedmeshes into the scenegraph and letting it rip.
just my 2c.

Materials or just colors?
If colors, then InstancedMesh has .setColorAt() method :thinking:

Command wise, here’s what I think the differences and resemblances between what I’m suggesting and having multiple instanced meshes are:

  • Because you need to draw with multiple programs, you get the same amount of context switching
  • Having multiple instanced meshes, means each will have it’s own instanceMatrix attribute, which needs to be bound, enabled, and the location and data specified via glVertexAttribPointer. Of course, because three uses VAO by default, all these calls would be encapsulated in a bindVertexArray call
  • Whereas having a single InstancedMesh means you have a single instanceMatrix attribute buffer which is bound once, then all you need is a call to glVertexAttribPointer with the desired offset before each draw command. If you do a gl trace on the pen I mentioned in my first post, you’ll see this happening. You can also create VAOs for this approach in case you have multiple attribute buffers that need to be offset-ed (so to speak)

The only possible source of stalling might be calling glVertexAttribPointer multiple times on the same bound buffer for whatever reason. I looked into this a bit and nothing stood out as definitely being slow or not recommended.

I’ve already moved to having multiple InstancedMesh in my scene and just doing it that way because I saw no way of doing it with thee’s instancing, but I find this particular topic interesting and am especially curious to find out what are the performance differences between the two approaches. If I can find enough time, I’ll test both of them out and post my findings here


It’s potentially different materials entirely, so I can’t rely on setColorAt unfortunately

Maybe u need different textures and not different materials like one mesh is phongMaterial, second mesh toonMaterial, third mesh standardMaterial