Is it possible to render the scene with a single material?

I am trying to render to texture in a way that lets me determine which objects are visible to the camera. Since I have far more than 255 objects the stencil buffer is not sufficient for this, so I was thinking of a material that renders each object’s world position for every visible pixel. But rolling through all the objects, setting their material to my custom material, rendering to texture, then rolling through again to revert to their proper materials is very expensive.
Is there a way to render the entire scene with a single material?