Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question URP: Applying Stencil to Camera

Discussion in 'Multiplatform Dev Blitz Day 2023 - Q&A' started by bigpants, Jun 21, 2023.

  1. bigpants

    bigpants

    Joined:
    Dec 9, 2009
    Posts:
    48
    Using Unity 2021.3.12f1 LTS and XR
    I'm trying to implement a Portal using Stencils WITHOUT using RenderTextures.
    Everyone says this can be done, but I CANNOT find any examples.
    Note: I can find examples of Stencils being used with Render Object Layers, but as far as I know I can't apply that approach to a portal camera

    I have a main camera and a portal camera.
    Both are base cameras (portal camera is not an overlay) so portal camera totally overlaps main camera.
    I want to apply a stencil shader to the portal camera,
    so overlap only renders on a quad with a stencil shader of 1s.
    I CANNOT figure out how to apply a stencil shader to the portal camera.

    My Current Non-Working Code
    RenderFeature: ScriptableRenderPass

    public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) {
    var drawSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortFlags);
    context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref filterSettings, ref RenderStateBlock);

    Can I use RenderFeatures to apply a Stencil to a base camera?
    Can you provide some specific code or point me to some information?
    Alternately, do I need to create my own SRP (based on URP) to do this?
    That appears to be what they did on a previous project at my company.

    Important: If there's another approach entirely, that implements portal without using RenderTextures (Blitter? URP Replacement Shaders? Change Portal Camera matrix to render directly to a Quad?), I'm all ears!

    THANKYOU!
     
    Last edited: Jun 21, 2023
  2. ali_mohebali

    ali_mohebali

    Unity Technologies

    Joined:
    Apr 8, 2020
    Posts:
    119
    It's possible but depending on the complexity of your portal requirements, you MIGHT hit some roadblocks and you might be better off customizing the URP renderer.

    Anyhow, attached is a quick sample scene I created for you for reference without customizing the package (disclaimer: I just did this quickly in 21LTS so it's not perfect, just a dirty reference):
    • It has a scene with a main camera and an overlay portal camera. The cameras are set up to use URP camera stacking. The cameras use layer masks to avoid rendering scene objects that are not relevant to them (check Layers Room 1 and Room 2)
    • Each camera uses a different URP Renderer configuration. The main camera uses "Renderer" which is pretty much the default setting, and the overlay portal camera uses "Renderer Stencil Test" which overrides Stencil operations.
    • I just used a plane to render the portal shape to stencil with the first camera. For this one, you can use RenderObjects renderer feature with Stencil ops override and a standard URP material. But if you want your own custom shader, unfortunately, you have to write a simple HLSL shader to set Stencil operations, Shader Graph doesn't expose setting stencil operations yet. I did the sample with a custom shader (for reference I copied URP unlit HLSL shader and made the necessary adjustments).
    Hope this gives some hints for what you are after.
     

    Attached Files:

    Last edited: Jul 3, 2023
    bigpants likes this.
  3. bigpants

    bigpants

    Joined:
    Dec 9, 2009
    Posts:
    48
    @ali_mohebali Thankyou so much. I've been playing around with the sample project and it's EXTREMELY helpful. It does some things I didn't know you could do.
     
    ali_mohebali likes this.