Friday, October 21, 2022
HomeGame Developmentrendering - Setting the Stencil Check Perform and Stencil Operation for subsequent...

rendering – Setting the Stencil Check Perform and Stencil Operation for subsequent draw calls in Unity SRP


I am utilizing HDRP and I wish to lengthen it with out really modifying HDRP code or writting my very own SRP.

I might wish to:

  1. Render some objects with a customized shader
  2. Have these objects improve a stencil worth
  3. Setup the stencil state for the subsequent digicam within the listing in order that the principle scene solely renders once more when the stencil matches that worth

Here is my pseudo-code:

utilizing System.Collections;
utilizing System.Collections.Generic;

utilizing UnityEngine;
utilizing UnityEngine.Rendering;
utilizing RenderPipeline = UnityEngine.Rendering.RenderPipelineManager;

public class MyRenderTest : MonoBehaviour
{
    non-public ushort m_StencilValue = 0; // The scene is simply rendered if the stencil equals to this worth

    void OnEnable()
    {
        RenderPipeline.beginContextRendering += OnBeginContextRendering;
        RenderPipeline.beginCameraRendering += OnBeginCameraRendering;
        RenderPipeline.endCameraRendering += OnEndCameraRendering;
    }

    void OnDisable()
    {
        RenderPipeline.endCameraRendering -= OnEndCameraRendering;
        RenderPipeline.beginCameraRendering -= OnBeginCameraRendering;
        RenderPipeline.beginContextRendering -= OnBeginContextRendering;
    }

    void OnBeginContextRendering(ScriptableRenderContext context, Checklist<Digicam> cameras)
    {
        m_StencilValue = 0;

        // Unity clears the principle stencil buffer earlier than this name, so no must do it ourselves
    }

    void OnBeginCameraRendering(ScriptableRenderContext context, Digicam digicam)
    {
        SetStencilFunction(EQUAL, m_StencilValue); // Solely render when the stencil equals m_StencilValue
        SetStencilOperation(KEEP); // Preserve the worth for the HDRP digicam render

        // After this, HDRP would render the digicam solely the place the stencil operation is verified
    }

    void OnEndCameraRendering(ScriptableRenderContext context, Digicam digicam)
    {
        SetStencilOperation(INCREMENT);
        DrawCustomObjects(); // Drawing the customized objects ought to increment the stencil worth

        m_StencilValue++;
    }
}

I additionally considered hooking up Customized Passes however the issue is similar, I am unable to discover a approach to really set the Stencil Check Perform and Stencil Operation.

Is that this doable?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments