Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Is this the Correct way to write a ScriptableRendererFeature & Pass

Discussion in 'General Graphics' started by iamvideep, May 12, 2022.

  1. iamvideep

    iamvideep

    Joined:
    Oct 27, 2017
    Posts:
    118
    Hi everyone,
    I've been following Unity's docs and other blogs, but am unsure if this is the right way to write renderer features and passes. Because if this is right, then I am unable to update/change the material at runtime on mobile devices. It just is stuck with rendering the pass with the material it is assigned at the beginning. Adding some code here:

    Code (CSharp):
    1. public class FadeFeature : ScriptableRendererFeature
    2. {
    3.     public RenderPassEvent rpe;
    4.     public RFAsset asset;
    5.     public Material mat;
    6.  
    7.     FadePass fp;
    8.  
    9.     public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    10.     {
    11.         fp.setup(mat, renderer.cameraColorTarget);
    12.         fp.renderPassEvent = rpe;
    13.         renderer.EnqueuePass(fp);
    14.     }
    15.  
    16.     public override void Create()
    17.     {
    18.         fp = new FadePass();
    19.     }
    20. }
    21.  
    22. public class FadePass : ScriptableRenderPass
    23. {
    24.     public Material material;
    25.     public RenderTargetIdentifier source;
    26.     public RenderTargetHandle tempTextureHandle;
    27.  
    28.     public FadePass():base()
    29.     {
    30.         tempTextureHandle.Init("TEMP_HueTexture");
    31.     }
    32.  
    33.     public void setup(Material mat, RenderTargetIdentifier src)
    34.     {
    35.         material = mat;
    36.         source = src;
    37.     }
    38.  
    39.     public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    40.     {
    41.         CommandBuffer cmd = CommandBufferPool.Get("FadePass");
    42.  
    43.         RenderTextureDescriptor camTexDesciptor = renderingData.cameraData.cameraTargetDescriptor;
    44.         cmd.GetTemporaryRT(tempTextureHandle.id, camTexDesciptor, FilterMode.Bilinear);
    45.  
    46.         Blit(cmd, source, tempTextureHandle.Identifier(), material);
    47.         Blit(cmd, tempTextureHandle.Identifier(), source);
    48.  
    49.         context.ExecuteCommandBuffer(cmd);
    50.         CommandBufferPool.Release(cmd);
    51.     }
    52. }