Search Unity

How to simply render the camera to RT via custom pass in HDRP

Discussion in 'High Definition Render Pipeline' started by econt, Jun 17, 2021.

  1. econt

    econt

    Joined:
    Apr 8, 2019
    Posts:
    52
    Hi I am trying to understand the custom pass in HDRP step by step.
    I would like to write a custom pass that simply gives out the acutal main camera to a RT - here is the script:

    public RenderTexture _rt;
    public LayerMask renderingMask3 = -1;
    protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
    {

    }
    protected override void Execute(CustomPassContext ctx)
    {
    CoreUtils.SetRenderTarget(ctx.cmd, _rt, ClearFlag.Color);
    CustomPassUtils.RenderFromCamera(ctx, ctx.hdCamera.camera, renderingMask3);
    }
    protected override void Cleanup()
    {
    _rt.Release();
    }

    The camera showes the normal scene and but the RT (as Raw Image stays black) - what could be the solution for my script?

    Thanks in advance

    Using: HDRP 10.5 Unity Editor 2020.3.8f1
     
  2. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    262
    Hello,

    I think you have the same issue as in this thread: https://forum.unity.com/threads/a-little-guidance-needed-custom-pass-rthandle.1107608/#post-7130027

    There is a custom pass script to output the camera color in a RenderTexture in my reply also :)

    Note that we typically use `CustomPassUtils.RenderFromCamera` for rendering from a different camera than the current, and also that it renders a certain subset of objects in the camera view (i.e. you can't get the camera color just with this function).

    If you want to learn more about custom passes, you can check out this sample repository, there is a lot of different effects: https://github.com/alelievr/HDRP-Custom-Passes
     
    Ruchir and econt like this.
  3. econt

    econt

    Joined:
    Apr 8, 2019
    Posts:
    52
    Hello,
    thank you very much - it worked! - I had a look to the HDRP-Custom-Passes on git

    I have new problem. - I want to render all objects from a certain layer to an RT, give the objects a diffent material which works so far in my code. But I want to give it a new color defined in the custom pass script (I dont want to use the click solution in the selection examples - I want to do i by code). I am setting the material property color to black or any other. - The problem is the objects are always in blue color and don't use the new color. - What could be the solution?

    public LayerMask outlineLayer = 0;
    [ColorUsage(false, true)]
    public Color outlineColor = Color.black;
    public float threshold = 1;
    public RenderTexture _rt;

    [SerializeField, HideInInspector]
    public Material _unlit_mat;

    protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
    {
    }

    protected override void Execute(CustomPassContext ctx)
    {
    CoreUtils.SetRenderTarget(ctx.cmd, _rt, ClearFlag.All);
    ctx.propertyBlock.SetColor("Color", outlineColor);
    CustomPassUtils.DrawRenderers(ctx, outlineLayer, overrideMaterial: _unlit_mat);
    }

    protected override void Cleanup()
    {
    base.Cleanup();
    }

    Thanks in Advance!
     
  4. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    262
    I think you forgot the override material pass index parameter of CustomPassUtils.DrawRenderers. Without that param you don't know which pass the draw will take from the shader. There are also example of this in the custom pass repo.
     
  5. mgeorgedeveloper

    mgeorgedeveloper

    Joined:
    Jul 10, 2012
    Posts:
    324
    I'm sorry to revive this one, but I'm really baffled. I tried to follow along above, but there seems to be no clear answer, with regards to how to actually render directly to a render texture, in a custom pass.

    I don't wish to render to the built-in Custom Buffer (or Camera Color Buffer) and then copy it to RT. I need to simply render to the RT like this:

    CustomPass-Render-To-RT.png

    Is there a simple answer (Yes or No) whether or not this is possible? And if possible, does anyone know how my lines of code might be tweaked to make it work?

    I'm just looking for some direct assistance rather than being sent off into the void of the "various custom passes" sample, or some other thread this doesn't answer the question directly :)

    Any help appreciated, so thanks in advance!
     
  6. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    262
    One possible issue that you can encounter while doing this kind of effect is that the rendered objects need the camera depth buffer to render properly (this is for opaque objects). This is because we expect that there is a depth prepass in HDRP.
    If you want to render objects with a clear depth buffer, you need to override the depth settings in the DrawRenderers, this can be done by providing the `overrideRenderState` parameter to the function. I have an example of this here: https://github.com/alelievr/HDRP-Cu...enes/CameraDepthBaking/CameraDepthBake.cs#L39
     
    mgeorgedeveloper likes this.
  7. nchernyadev

    nchernyadev

    Joined:
    Aug 8, 2023
    Posts:
    2
    I'm sorry for reviving this post once again, but I'm struggling with a similar thing right now. I'm trying to optimize rendering from multiple points of view with a custom pass using:
    CustomPassUtils.RenderFromCamera()


    There was a problem of duplicated lighting, but it was solved by overriding this value:
    CameraRelativeRendering = 0


    The problem is that shadows and lighting are still computed/culled with respect to the pose of the initial camera of the pass. I've been wondering if it's possible to recalculate shadows and lights culling with respect to the new camera because it is quite far from the initial camera.

    Thank you in advance! :)
     
    Last edited: Oct 26, 2023
  8. mgeorgedeveloper

    mgeorgedeveloper

    Joined:
    Jul 10, 2012
    Posts:
    324
    This is the same issue for which I'm awaiting an answer here:

    Bug - Custom pass rendering from disabled camera has incorrect culling results - Unity Forum

    So far, no official response from Unity. I'm still hoping :)
     
    nchernyadev likes this.
  9. nchernyadev

    nchernyadev

    Joined:
    Aug 8, 2023
    Posts:
    2
  10. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    262
    The RenderFromCamera API was mainly designed to render objects from any camera in the scene using a custom shader that doesn't rely on HDRP camera features or to render objects within the camera view (for example FPS foreground objects with fixed FoV).

    If you need all the lighting/shadow features of HDRP for your additional cameras, then it can't be achieved with this API and you'll need to use a real camera. You can still try to optimize the cost of this camera by tweaking the frame settings and disabling everything you don't need, scaling the output resolution down, or even making your camera render every other frame.

    You can also check this thread as there are many good advices there: https://forum.unity.com/threads/meg...-to-better-runtime-unity-performance.1169837/
     
    nchernyadev likes this.