Search Unity

Question DrawRenderers Custom Pass rendering black at certain viewing angles

Discussion in 'High Definition Render Pipeline' started by Armageddon104, Nov 23, 2020.

  1. Armageddon104

    Armageddon104

    Joined:
    Sep 14, 2014
    Posts:
    23
    Hello, I'm trying to render a lowres version of some objects in my game to create a pixelation/censor effect. I'm getting an issue though where my objects get overriden by black when viewing from certain distances and angles, it looks like a corrupted shadowmask texture as rotating the view moves the black mask a little bit, here's a screenshot:

    ShadowmaskIssues.png

    After a certain distance it's entirely black though, as if it's culling lights or a depth issue.

    I also observed that my objects sometimes don't get lit by certain lights unless they're in the right position, whereas if I turn off the Custom Pass and add my object layer back to the camera's culling layers they will render fine. I think this is because Custom Passes only handle Forward rendering so there's a light limit per object? But even in areas with less than four lights it happens. Is there a way to render Custom Passes with Deferred lighting yet?

    I thought it might be an issue with skinned meshes but the backpack is not apart of the character mesh and is its own object.

    Here's my code, I think it might have something to do with the rendererConfiguration data in the RendererListDesc:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Experimental.Rendering;
    3. using UnityEngine.Rendering;
    4. using UnityEngine.Rendering.HighDefinition;
    5.  
    6. public class CustomPassSprites : CustomPass {
    7.  
    8.     public LayerMask layerMask = 1;
    9.     public SortingCriteria sortingCriteria = SortingCriteria.CommonOpaque;
    10.     public RenderTexture targetTexture = null;
    11.     RTHandle targetDepthTextureHandle;
    12.     ShaderTagId[] shaderTags;
    13.  
    14.     protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd) {
    15.         shaderTags = new ShaderTagId[4] {
    16.             new ShaderTagId("Forward"), // HD Lit shader
    17.                 new ShaderTagId("ForwardOnly"), // HD Unlit shader
    18.                 new ShaderTagId("SRPDefaultUnlit"), // Cross SRP Unlit shader
    19.                 new ShaderTagId(""), // Empty
    20.         };
    21.  
    22.         if (targetDepthTextureHandle == null) {
    23.             targetDepthTextureHandle = RTHandles.Alloc(
    24.                 targetTexture.width, targetTexture.height, 1, dimension : TextureDimension.Tex2D,
    25.                 colorFormat : GraphicsFormat.R16_UInt, filterMode: FilterMode.Point, useDynamicScale : false,
    26.                 name: "Sprite Depth Mask", depthBufferBits : DepthBits.Depth32
    27.             );
    28.         }
    29.     }
    30.  
    31.     protected override void AggregateCullingParameters(ref ScriptableCullingParameters cullingParameters, HDCamera camera) => cullingParameters.cullingMask |= (uint)layerMask.value;
    32.  
    33.     protected override void Execute(CustomPassContext ctx) {
    34.  
    35.         var result = new RendererListDesc(shaderTags, ctx.cullingResults, ctx.hdCamera.camera) {
    36.             rendererConfiguration = PerObjectData.LightProbe | PerObjectData.LightProbeProxyVolume | PerObjectData.Lightmaps | PerObjectData.ShadowMask,
    37.                 renderQueueRange = RenderQueueRange.opaque,
    38.                 sortingCriteria = sortingCriteria,
    39.                 excludeObjectMotionVectors = false,
    40.                 layerMask = layerMask,
    41.                 stateBlock = new RenderStateBlock(RenderStateMask.Depth) { depthState = new DepthState(true, CompareFunction.LessEqual) },
    42.         };
    43.  
    44.         CoreUtils.SetRenderTarget(ctx.cmd, targetTexture, targetDepthTextureHandle, ClearFlag.All);
    45.         HDUtils.DrawRendererList(ctx.renderContext, ctx.cmd, RendererList.Create(result));
    46.     }
    47.  
    48.     protected override void Cleanup() {
    49.         // Cleanup code
    50.         targetDepthTextureHandle.Release();
    51.         targetTexture.Release();
    52.     }
    53. }
    54.  
    And here are the settings for the RenderTexture my material is using:

    Screenshot (9047).png

    Thank you!