Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Surprisingly effective renderer for 2D games

Discussion in 'Graphics Experimental Previews' started by adbourdages, Nov 28, 2018.

  1. adbourdages

    adbourdages

    Joined:
    Jul 8, 2012
    Posts:
    55
    I have been playing around with the render pipeline for performance and found that the following renderer actually performs a lot of task for such simple code. I have a 2D game and it does everything I need: UI, Sprite Renderers, Particles, TextMesh Pro GUI. For the game, it reduces frame processing time by ~5%.

    Other folks on the forums have 2D renderer to share or tips on how to improve the code below further?

    Code (CSharp):
    1.  
    2. public class TransparentAssetPipeInstance : RenderPipeline
    3. {
    4.     private static readonly ShaderPassName UnlitShaderPassName = new ShaderPassName("SRPDefaultUnlit");
    5.     private readonly FilterRenderersSettings _filterRendererSettingsOpaque;
    6.     private readonly FilterRenderersSettings _filterRendererSettingsTransparent;
    7.  
    8.     public TransparentAssetPipeInstance()
    9.     {
    10.         _filterRendererSettingsOpaque = new FilterRenderersSettings(true) { renderQueueRange = RenderQueueRange.opaque };
    11.         _filterRendererSettingsTransparent = new FilterRenderersSettings(true) { renderQueueRange = RenderQueueRange.transparent };
    12.     }
    13.  
    14.     public override void Render(ScriptableRenderContext context, Camera[] cameras)
    15.     {
    16.         base.Render(context, cameras);
    17.  
    18.         for( int i = 0; i < cameras.Length; i++ )
    19.         {
    20.             Camera camera = cameras[i];
    21.             // Culling
    22.             ScriptableCullingParameters cullingParams;
    23.             if (!CullResults.GetCullingParameters(camera, out cullingParams))
    24.                 continue;
    25.  
    26. #if UNITY_EDITOR
    27.             // Emit scene view UI
    28.             if (camera.cameraType == CameraType.SceneView)
    29.                 ScriptableRenderContext.EmitWorldGeometryForSceneView(camera);
    30. #endif
    31.  
    32.             // Generates 192B of memory allocation
    33.             CullResults cull = CullResults.Cull(ref cullingParams, context);
    34.  
    35.             // Setup camera for rendering (sets render target, view/projection matrices and other
    36.             // per-camera built-in shader variables).
    37.             context.SetupCameraProperties(camera);
    38.  
    39.             // clear depth buffer (another 48B of allocation)
    40.             var cmd = new CommandBuffer();
    41.             cmd.ClearRenderTarget(true, false, Color.black);
    42.             context.ExecuteCommandBuffer(cmd);
    43.             cmd.Release();
    44.  
    45.             // Draw opaque objects using BasicPass shader pass
    46.             var settings = new DrawRendererSettings(camera, UnlitShaderPassName);
    47.            
    48.             // Opaque
    49.             settings.sorting.flags = SortFlags.CommonOpaque;
    50.             context.DrawRenderers(cull.visibleRenderers, ref settings, _filterRendererSettingsOpaque);
    51.  
    52.             // Transparent
    53.             settings.sorting.flags = SortFlags.CommonTransparent;
    54.             context.DrawRenderers(cull.visibleRenderers, ref settings, _filterRendererSettingsTransparent);
    55.  
    56.             context.Submit();
    57.         }
    58.     }
    59. }
     
unityunity