InstancedMesh vs BatchedMesh

So, I was content using BatchMesh for lots of text until the DirectX9 issue of 256 max vertex uniforms comes up. Then the solution quickly degrades the size of a model to the GPUs/DX9:MAX_VERTEX_UNIFORM_VECTORS max ( which is not always set by the GPU ). Regardless of what sets the vertex cap the shader has to bus too many uniforms to the GPU.

Of course in instancedMesh this no real issue.

Like BatchedMesh, is there a way to add instances to an instancedMesh and merge them?

Referring back to BatchMesh Memory Issue - #4 by donmccurdy, the term “BatchedMesh” doesn’t really have any specific meaning – if you can be more specific instead of using this word that may help us to understand what you’re asking. If you’re using mergeBufferGeometries, I don’t know of any reason it should use more uniforms than any other Mesh. Are you using multiple materials maybe? Sharing code or a demo may also help.

For lots of text the best thing would be an SDF library like Troika-3d-text: library for SDF text rendering.

Hi Don! Yes, I’m referring to specifically, mergedBufferGeometry and passing the mat4 array to the shader. This is causing the issue.

Simple test that you can do is simply create an InstancedMesh of a word of text. Create 5000 instances of the same word. No issues.

Now create a mergedBufferGeometry of the same word 5000 times with a mat4 passed for each one and send them to the shader.

You will hit the max vertex buffer.

Of course, BatchedMesh is for adding a bunch of different geometries to a parent mesh. InstancedMesh is intended to recreate a clone of a mesh n times. Can InstancedMesh be used like BatchMesh where multiple
different geometries can be added (merged) to the parent mesh?

Is it simply a diff in how InstancedMesh handles the indexes via the InstancedMesh matrix? Can a mergedBufferGeometry handle this without passing the mat4 position array to the shader? How exactly is InstancedMesh getting such great performance and avoiding the vertex count?

Thanks!

InstancedMesh uses GPU instance attributes for the matrix, rather than uniforms. As you mention, the number of uniforms is limited.

It sounds like you’re writing custom shaders for your merged geometry? If that’s the case you could put transforms into vertex attributes instead of uniforms. Geometry may be limited to 16 vertex attributes, but should be enough room for an extra mat4 (4 x vec4 uniforms).

1 Like

Thanks a lot @gkjohnson @donmccurdy and rest of team. The new implementation of BatchedMeshes that allow to add instances too is so cool¡¡¡