InstancedMesh with two different geometries?

What I’m essentially trying to achieve is a “particle grid” consisting of 2-3 different platonic solids, which can then be interpolated between to morph the positions.

This is pretty much my first time working with InstancedMeshes so please forgive my ignorance. I haven’t been able to find any code examples of this so I’m turning to the forums in hopes that somebody will be able to at least point me in the right direction.

Well dat’d be kinda against the entire idea and definition of instancing.

If there’s no crazy amount of different geometries - just use a separate InstancedMesh for each geometry type.

If there is a crazy amount of different geometries - consider other types of optimisation, like LOD and imposters.

Thank you for the quick reply!

Here’s an example of a website that achieves the effect I’m looking for
https://solutions.centogene.com/ (Scroll down to / Click on “Knowledge Solutions”)

I’ve skimmed through the maze of minified code, and it looks like they make extensive use out of instanced meshes, be it for this particular mesh or the ones before.
So you’re basically saying that there isn’t a straight forward way of doing this in a single InstancedMesh with two draw calls? I would instead have to create two separate meshes and handle the interpolation of positions inside the javascript, as opposed to the shader?
Idk what’s considered a crazy amount of geometries when instancing, but I’m aiming for around 50k points.

You could render both and mask one…

Once upon a time, I tried to make an object of two instanced meshes: R E T R O B O Y (Tetrahedral deconstruction)

Another option is to use a so-called “batched geometry”, built of merged geometries, where each part has its own index so you can do some stuff in shaders, using that index. Something close, I could quickly recall of, is this: Tetrahedra (fat lines, batching, texelFetch)

1 Like

Here’s how I’d approach it:

Merge all of your geometry (each one of your platonic solids) into one giant BufferGeometry. Then, for each vertex, set an attribute (call it “geoIndex”) representing the platonic solid the vertex belongs to.

Then, in your instanced mesh, you can set a per-instance attribute that corresponds to which platonic solid you want your instance to be - call it (instanceIndex)

Then, in your vertex shader, compare the attribute (geoIndex) of the vertex to the instanced attribute (instanceIndex) and if they aren’t equal, discard the vertex via gl_Position = vec4(0.0/0.0). This lets you selectively choose the vertices you are interested in on a per-instance basis.

However, this technique is not without cost. It forces the gpu to process an excess of vertices, and for each rendered instance, the GPU has to process the sum of all possible geo vertices instead of just the required amount. As a result, I’d strongly recommend using a few draw calls with different instanced meshes for each platonic solid. However, If you really want to only have 1 draw call, the above technique will work (though it probably won’t be faster).

3 Likes

Tried it with a batched geometry (merged geometries), along with DataTexture to store the data of formations.

5 Likes

The option with rotation: https://codepen.io/prisoner849/full/yLQyPav

PS This is just an example, not the ultimate solution.

5 Likes

I was just in the middle of trying to set it up, tyvm for including the pen!

If merged geometries is as good as it gets, then oh well might as well settle for it. Perhaps dubois method could work, as it’d reduce the initial calculations and free up ‘a lot’ of memory.
If not, I’m happy with that solution, thank you!

1 Like

@N8Three elaborated on my short input. It’s the same suggestion. Masking would be done by not rasterizing the masked part of the mesh.