Unwanted color changing with mesh instances

The example at Babylon.js Playground has some mesh instances with different colors. If you zoom in and out, the colors change, where I expect each instance to stay the same color. This only happens within a certain radius; the colors stay the same outside that.

What’s going on? How do I fix my code so the colors don’t change?

Thanks.

Basically the size of the color buffer can not be fixed due to clipping and so on. Instances not on screen would not be drawn to accelerate the draw.

In your case this is impractical to rebuild the buffer every frame.

You should consider keep all the instances always enabled :slight_smile: https://playground.babylonjs.com/#T8Y26J#1

1 Like

Another way is to use the second method explained in Use Instances - Babylon.js Documentation (registerInstancedBuffer):

https://playground.babylonjs.com/#T8Y26J#3

2 Likes

Thank you @Evgeni_Popov and @sebavan; registerInstancedBuffer and alwaysSelectAsActiveMesh work nicely.

The API documentation at registerInstancedBuffer doesn’t mention what kind of buffers can be registered. Is this documented somewhere I haven’t found?

You can register any kind of buffers, but you will need to create a custom shader to be able to use them, except for “color” which is used by the engine for the vertex colors and is already taken into account in the standard materials (and pbr).