Search Unity

Question Custom Pass objects to texture

Discussion in 'High Definition Render Pipeline' started by Phantom_X, Jan 15, 2020.

  1. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    314
    Hey,
    I'm trying to render some objects to a render texture using custom pass and layer mask. I can't seem to get it working.

    my texture is completely blank. In the frame debugger I can see the correct objects being rendered, but they are flickering.

    any ideas why?

    here's my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering.HighDefinition;
    3. using UnityEngine.Rendering;
    4. using UnityEngine.Experimental.Rendering;
    5.  
    6. class MyCustomPass : CustomPass
    7. {
    8.     public LayerMask maskLayer;
    9.     public Renderer renderer;
    10.  
    11.     private RTHandle _rt;
    12.     private RTHandle _rtDepth;
    13.     private ShaderTagId[] _shaderTags;
    14.  
    15.  
    16.     protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
    17.     {
    18.         _shaderTags = new ShaderTagId[6]
    19.         {
    20.             new ShaderTagId("Forward"),
    21.             new ShaderTagId("ForwardOnly"),
    22.             new ShaderTagId("SRPDefaultUnlit"),
    23.             new ShaderTagId("FirstPass"),
    24.             new ShaderTagId("DepthOnly"),
    25.             new ShaderTagId("DepthForwardOnly"),
    26.         };
    27.     }
    28.  
    29.     protected override void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera camera, CullingResults cullingResult)
    30.     {
    31.  
    32.         AllocateBuffers();
    33.  
    34.         // Render the objects in the layer blur mask into a mask buffer with their materials so we keep the alpha-clip and transparency if there is any.
    35.         var result = new RendererListDesc(_shaderTags, cullingResult, camera.camera)
    36.         {
    37.             rendererConfiguration = PerObjectData.None,
    38.             renderQueueRange = RenderQueueRange.all,
    39.             sortingCriteria = SortingCriteria.BackToFront,
    40.             excludeObjectMotionVectors = false,
    41.             layerMask = maskLayer,
    42.         };
    43.  
    44.         CoreUtils.SetRenderTarget(cmd, _rt, _rtDepth, ClearFlag.Color);
    45.         HDUtils.DrawRendererList(renderContext, cmd, RendererList.Create(result));
    46.  
    47.         if (renderer != null)
    48.         {
    49.             var block = new MaterialPropertyBlock();
    50.             block.SetTexture("_MainTex", _rt);
    51.  
    52.             renderer.SetPropertyBlock(block);
    53.         }
    54.     }
    55.  
    56.     protected override void Cleanup()
    57.     {
    58.         // Cleanup code
    59.         _rt?.Release();
    60.         _rtDepth?.Release();
    61.     }
    62.  
    63.     private void AllocateBuffers()
    64.     {
    65.         if (_rt == null)
    66.         {
    67.             _rt = RTHandles.Alloc(
    68.                 Vector2.one, TextureXR.slices, dimension: TextureXR.dimension,
    69.                 filterMode:FilterMode.Bilinear,
    70.                 colorFormat: GraphicsFormat.B10G11R11_UFloatPack32,
    71.                 useDynamicScale: true, name: "Color Copy"
    72.             );
    73.         }
    74.  
    75.         if (_rtDepth == null)
    76.         {
    77.             _rtDepth = RTHandles.Alloc(
    78.                 Vector2.one, TextureXR.slices, dimension: TextureXR.dimension,
    79.                 colorFormat: GraphicsFormat.R16_UInt, useDynamicScale: true, isShadowMap: true,
    80.                 name: "Blur Depth Mask", depthBufferBits: DepthBits.Depth16
    81.             );
    82.         }
    83.     }
    84. }