Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  3. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

(SRP) Scene Window breaking when selecting the main camera

Discussion in 'Graphics Experimental Previews' started by cjddmut, Jul 13, 2022.

  1. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    179
    I'm experimenting with the SRP and have pretty simple one written. So far things are working pretty well except for when I select the main camera then the Scene tab doesn't draw correctly.

    Not Selected:
    upload_2022-7-12_16-56-6.png

    Selected:
    upload_2022-7-12_16-56-42.png

    Note that the tabs are also not drawing for the editor and all the grid lines are missing. The main camera is also the standard built in one which is drawing in play mode and a build just fine.

    Below is the code I've written so far:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3.  
    4. public class ExampleRenderPipelineInstance : RenderPipeline
    5. {
    6. #if UNITY_EDITOR
    7.     private static readonly ShaderTagId[] LEGACY_SHADER_TAGS =
    8.     {
    9.         new ShaderTagId("Always"),
    10.         new ShaderTagId("ForwardBase"),
    11.         new ShaderTagId("PrepassBase"),
    12.         new ShaderTagId("Vertex"),
    13.         new ShaderTagId("VertexLMRGBM"),
    14.         new ShaderTagId("VertexLM"),
    15.     };
    16.  
    17.     private static Material _errorMaterial;
    18.  
    19. #endif
    20.  
    21.     private static ShaderTagId _standardDrawTag = new ShaderTagId("SRPDefaultUnlit");
    22.  
    23.     protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    24.     {
    25.         CommandBuffer cmd = CommandBufferPool.Get();
    26.         cmd.ClearRenderTarget(true, true, Color.cyan * 0.25f);
    27.      
    28.         context.ExecuteCommandBuffer(cmd);
    29.      
    30.         cmd.Clear();
    31.         CommandBufferPool.Release(cmd);
    32.      
    33.         for (int i = 0; i < cameras.Length; i++)
    34.         {
    35.             Camera camera = cameras[i];
    36.  
    37. #if UNITY_EDITOR
    38.             if (camera.cameraType == CameraType.SceneView)
    39.             {
    40.                 ScriptableRenderContext.EmitWorldGeometryForSceneView(camera);
    41.             }
    42. #endif
    43.          
    44.             camera.TryGetCullingParameters(out ScriptableCullingParameters cullingParams);
    45.             CullingResults cullingResults = context.Cull(ref cullingParams);
    46.          
    47.             context.SetupCameraProperties(camera);
    48.              
    49.             ShaderTagId shaderTagId = _standardDrawTag;
    50.  
    51.             SortingSettings sortingSettings = new SortingSettings(camera);
    52.  
    53.             DrawingSettings drawingSettings = new DrawingSettings(shaderTagId, sortingSettings);
    54.              
    55.             FilteringSettings filteringSettings = FilteringSettings.defaultValue;
    56.              
    57.             context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings);
    58.          
    59. #if UNITY_EDITOR
    60.             drawingSettings = new DrawingSettings(LEGACY_SHADER_TAGS[0], sortingSettings);
    61.  
    62.             if (_errorMaterial == null)
    63.             {
    64.                 _errorMaterial = new Material(Shader.Find("Hidden/InternalErrorShader"));
    65.             }
    66.  
    67.             drawingSettings.overrideMaterial = _errorMaterial;
    68.  
    69.             for (int j = 1; j < LEGACY_SHADER_TAGS.Length; j++)
    70.             {
    71.                 drawingSettings.SetShaderPassName(j, LEGACY_SHADER_TAGS[j]);
    72.             }
    73.          
    74.             filteringSettings = FilteringSettings.defaultValue;
    75.             context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings);
    76.          
    77.  
    78.             if (UnityEditor.Handles.ShouldRenderGizmos())
    79.             {
    80.                 context.DrawGizmos(camera, GizmoSubset.PreImageEffects);
    81.                 context.DrawWireOverlay(camera);
    82.                 context.DrawGizmos(camera, GizmoSubset.PostImageEffects);
    83.             }
    84. #endif
    85.  
    86.         }
    87.      
    88.         context.Submit();
    89.     }
    90. }
    Unity Version: 2020.3.30f1
    SRP Core Version: 10.8.1

    I'm sure I'm just missing some simple API call. Any help would be greatly appreciated :)
     

    Attached Files:

  2. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    179
    Resolved by experimenting pretty quickly. Turns out when you click on a camera the sceneview and the preview camera are instead rendering to render textures and this SRP code isn't respecting that. Below is what I've done to deal with that.

    Code (CSharp):
    1. // Clearing the render target removed up here
    2.  
    3. for (int i = 0; i < cameras.Length; i++)
    4. {
    5.     Camera camera = cameras[i];
    6.  
    7.     if (camera.clearFlags == CameraClearFlags.Color)
    8.     {
    9.         CommandBuffer cmd = CommandBufferPool.Get();
    10.  
    11.         if (camera.targetTexture != null)
    12.         {
    13.             cmd.SetRenderTarget(camera.targetTexture);
    14.         }
    15.      
    16.         cmd.ClearRenderTarget(true, true, camera.backgroundColor);
    17.      
    18.         context.ExecuteCommandBuffer(cmd);
    19.  
    20.         cmd.Clear();
    21.         CommandBufferPool.Release(cmd);
    22.     }
    23.     // ...
    24. }