How to make a mesh invisible but still write to the custom depth buffer

Hi everyone,

I’m working with a complex anatomical model of the human body, which consists of about 5,000 individual meshes (bones, veins, organs, etc.). To optimize rendering, I’ve merged large sections (e.g., all bones, all veins) into single meshes. I still need to interact with individual parts, so I’ve kept the unmerged mesh for collision detection during ray casting. This setup lets me click on the merged model and get the correct part of the body in the console.

Previously, I had a setup using individual meshes, where I wrote to a custom depth buffer to highlight selected parts, following this tutorial, and it worked great.

Now, I’ve tried combining both approaches—highlighting individual parts while displaying the merged model. However, this caused the “picker mesh” to be rendered on top of the merged model. To fix this, I disabled the picker mesh from the main scene pass, but then the picker mesh appeared black and was still quite expensive to render. On the plus side, the highlighting worked.

To improve FPS, I made the picker mesh completely transparent. Performance improved significantly, and I could still click and get the right intersection, but the highlighting no longer worked.

Could someone help me figure out how to get the highlighting working again without losing the performance boost?

TL;DR:

I’m using a complex human body model with merged meshes for performance and an unmerged mesh for ray casting. Highlighting worked when using separate meshes, but combining both approaches led to issues. I made the picker mesh transparent for performance, but now highlighting doesn’t work. Looking for help to restore highlighting while keeping good performance.