Rendering objects on scene using only ScriptableRenderContext.DrawRenderers

Hi,

I’m currently using ScriptableRenderContext.DrawRenderers in custom ScriptableRenderPass to render some opaque objects (filtered by layer) twice - with a different shader than normal.
And it works fine:

  • First all objects are rendered in standard Unity pipeline.
  • And then they are rendered for a second time by a ScriptableRenderPass.

Now what I’d like to do, is to render these objects twice - but both times by different ScriptableRenderPasses - skipping normal Unity rendering process. However to use DrawRenderers they must be already present on the scene (as GameObjects, with Renderers and so), which results in rendering them automatically by Unity render pipeline - what I’d like to avoid.
I could remove them from scene and render each mesh manually, but this would break SRP Batching, which is critical for me - thus limit to DrawRenderers.

I tried removing them from camera Culling Mask, but this also removes them from culling results and makes invisible for custom passes. Is there any way to do this?

Yes there is:

  1. At any time before rendering begins you remove the relevant layers from the culling mask of the camera;
  2. You schedule your scriptable render pass after the relevant rendering pass by Unity, so presumably ‘after rendering opaques’ or ‘after rendering transparents’;
  3. At the beginning of your custom pass, you add the layers back to the culling mask of the camera;
  4. You manually perform culling again, which will collect the renderers to be drawn based on your new culling mask, like this:
ScriptableCullingParameters cullingParameters;
renderingData.cameraData.camera.TryGetCullingParameters(out cullingParameters);
renderingData.cullResults = context.Cull(ref cullingParameters);
  1. You draw those renderers twice with DrawRenderers;

Then repeat from step 1.

Just remove them from rendering in render feature list and render only from your custom render feature
See how this done in RenderObjects render feature.