Search Unity

Graphics.DrawMeshNow now working right?

Discussion in 'High Definition Render Pipeline' started by DuvE, Dec 3, 2021.

  1. DuvE

    DuvE

    Joined:
    May 22, 2016
    Posts:
    168
    Hi,

    I need to do a simple thing, draw selected mesh into a texture, but don't draw it on the main camera. So I used my old setups from Built-in pipeline:


    Code (CSharp):
    1. void Update()
    2.     {
    3.         Graphics.SetRenderTarget(renderTexture);
    4.         GL.PushMatrix();
    5.         GL.modelview = dummyCam.worldToCameraMatrix;
    6.         GL.LoadProjectionMatrix(dummyCam.projectionMatrix);
    7.         GL.Clear(true, true, Color.clear);
    8.         for (int i = 0; i < meshesToRender.Length; i++)
    9.         {
    10.             meshesToRender[i].material.SetPass(0);
    11.             // localToWorldMatrix is from a disabled Renderer component
    12.             Graphics.DrawMeshNow(meshesToRender[i].gameObject.GetComponent<MeshFilter>().mesh, meshesToRender[i].localToWorldMatrix);
    13.         }
    14.         GL.PopMatrix();
    15.         Graphics.SetRenderTarget(null);
    16.     }
    But in HDRP it is now working properly, here is the example screenshot. It only renders the unlit standard material and in white color, not its red color. Is this approach is not available in HDRP and I should use Custom Pass instead for tasks like this one?

    upload_2021-12-3_20-11-26.png

    EDIT_01 Using CustomPass:

    I'm trying to do the same thing with CustomPass, mostly working, but why "CustomPassUtils.DrawRenderers" is not drawing objects from a layer, that is hidden in the Camera? Is this normal behavior? My goal is to render simple unlit mesh to a RenderTexture, that is in the layer not rendered in the Camera.

    Here is the effect I'm testing:

    upload_2021-12-4_7-57-18.png

    As you can see, the original sharp white mesh with Fresnel is under this fake hologram, because it works only if this original sharp mesh is rendered to the Camera too. In the Capsule silhouette, I just sampled this texture with some effects.

    Setup is really simple:


    Code (CSharp):
    1. CoreUtils.SetRenderTarget(ctx.cmd, maskBuffer, ClearFlag.All);
    2. CustomPassUtils.DrawRenderers(ctx, maskLayer);
    3. var scale = RTHandles.rtHandleProperties.rtHandleScale;
    4. ctx.cmd.Blit(maskBuffer, hologramRt, new Vector2(scale.x, scale.y), Vector2.zero, 0, 0);
    EDIT_02 overrideMaterial:

    I tried to set the opacity to 0 of Camera rendered material and use overrideMaterial to draw renderers with the same Fresnel material but with the opacity of 1. For some reason, when using:
    Code (CSharp):
    1. CustomPassUtils.DrawRenderers(ctx, maskLayer, overrideMaterial: materialForOverride);
    materials with ShaderGraph Unlit shader are not rendered at all. And example Unlit standard material is rendered, but with white color (I set it to orange in the settings).

    EDIT_03:
    I'm currently encountering one issue with the shader template for CustomPass Renderer, posInput.positionWS is always (0,0,0) for some reason.

    PS: Unity 2021.2.3f1
     
    Last edited: Dec 4, 2021