Search Unity

Resolved Black screen in Scene View and after pressing Ctrl + S in Game View - Custom Renderer Feature

Discussion in 'Universal Render Pipeline' started by ChichoRD, Aug 4, 2022.

  1. ChichoRD

    ChichoRD

    Joined:
    Jul 31, 2020
    Posts:
    19
    I was trying to recreate a Sharpness effect using URP Custom Renderer Features. To do so, I wrote a 3 pass shader which performs the following operations:
    1. Horizontal Gaussian Blur Pass
    2. Vertical Gaussian Blur Pass
    3. Apply Sharpness formula using previous results
    Note: The formula I'm using is:
    color + (color - blur) * sharpness

    Everything works fine when seen from the Game View, the effect works, I can even enter playmode and still works; however as soon as I want to save my changes or enter Scene View the whole view turns black.

    I'm not an expert in shader-coding but while I am in the Game View everything seems to produce the desired effect. Then my guess is that I'm missing some non-related but required command in my ScriptableRenderPass class, maybe in the Execute method.
    Moreover, I am aware of the solution that involves checking if the view is from cameraPreview so that you don't proccess the effect, but Unity is able to do so with their other features like SSAO.

    I will post the relevant code of the shader and class, thank you in advance if you find anything that might be causing the bug.

    This is from the class pass
    Code (CSharp):
    1.        
    2. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    3.         {          
    4.             CommandBuffer cmd = CommandBufferPool.Get();
    5.  
    6.             //Horizontal Blur Pass
    7.             cmd.Blit(_colorBuffer, _blurBuffer1, _material, 0);
    8.             //Vertical Blur Pass
    9.             cmd.Blit(_blurBuffer1, _blurBuffer2, _material, 1);
    10.  
    11.             //Storing the original color in a temporary texture
    12.             cmd.SetGlobalTexture("_TempTex", _colorBuffer);
    13.             //Blitting back to the original color buffer with sharpness effect applied
    14.             cmd.Blit(_blurBuffer2, _colorBuffer, _material, 2);
    15.  
    16.             context.ExecuteCommandBuffer(cmd);
    17.             CommandBufferPool.Release(cmd);
    18.         }
    19.  
    This are the passes of the shader
    Code (Boo):
    1.        
    2. Pass
    3.         {
    4.             Name "Horizontal Blur"
    5.            
    6.             HLSLPROGRAM
    7.            
    8.             half4 frag (v2f i) : SV_Target
    9.             {
    10.                 return GaussianBlurHorizontal(_MainTex, sampler_MainTex, _MainTex_TexelSize, i.uv, _Sigma, _Radius);
    11.             }
    12.            
    13.             ENDHLSL
    14.         }
    15.        
    16.         Pass
    17.         {
    18.             Name "Vertical Blur"
    19.  
    20.             HLSLPROGRAM
    21.  
    22.             half4 frag (v2f i) : SV_Target
    23.             {  
    24.                 return GaussianBlurVertical(_MainTex, sampler_MainTex, _MainTex_TexelSize, i.uv, _Sigma, _Radius);
    25.             }
    26.            
    27.             ENDHLSL
    28.         }
    29.  
    30.         Pass
    31.         {
    32.             Name "Sharpness"
    33.            
    34.             HLSLPROGRAM
    35.            
    36.             half4 frag (v2f i) : SV_Target
    37.             {
    38.                 half4 b = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    39.                 half4 c = SAMPLE_TEXTURE2D(_TempTex, sampler_TempTex, i.uv);
    40.                 return c + (c - b) * _Sharpness;
    41.             }
    42.            
    43.             ENDHLSL
    44.         }
    45.  
     
  2. ChichoRD

    ChichoRD

    Joined:
    Jul 31, 2020
    Posts:
    19
    Solution:

    Turns out that it is key to use the Blit method where you pass the command buffer as an argument. Do not use the cmd.Blit one as it seems to malfunction causing the black screen on save or edit view.