Search Unity

[SOLVED]Cannot draw with the command buffers on the background (empty pixels)

Discussion in 'Image Effects' started by Andromede, Dec 9, 2016.

  1. Andromede

    Andromede

    Joined:
    Dec 9, 2016
    Posts:
    2
    Hi,

    here's my first post. :)

    I can't find any answer on Google about it. Maybe you can help me guys.

    I want to render a full screen image that fills the color buffer, the depth buffer and the normal buffer to get depth occlusion and lighting on it. I've tried many ways: dealing with the graphics command buffers seems the best solution.

    So I'm rendering in deferred mode. My command buffer is after the gbuffer.

    Everything works but I don't know why my image is not drawn on the background at the end of the pipeline (empty pixels without object drawn).

    Unity seems using an other buffer to apply a mask in the lighting pass. But when I look the light pass in the frame debug window, there is no entry texture/mask used to apply the mask. :/

    Here's what I got:

    My code:
    The script attached to the camera:
    Code (CSharp):
    1. public void OnPreRender()
    2. {
    3.        bool act = gameObject.activeInHierarchy && enabled;
    4.        if (!act)
    5.        {
    6.           OnDisable();
    7.           return;
    8.         }
    9.         var cam = Camera.current;
    10.         if (!cam)
    11.           return;
    12.         CommandBuffer buf = null;
    13.         if (m_Cameras.ContainsKey(cam))
    14.         {
    15.             buf = m_Cameras[cam];
    16.             buf.Clear();
    17.         }
    18.         else
    19.         {
    20.              buf = new CommandBuffer();
    21.              buf.name = "DEFERRED CUSTOM";
    22.              m_Cameras[cam] = buf;
    23.              cam.AddCommandBuffer(CameraEvent.AfterGBuffer, buf);
    24.         }
    25.        // copy g-buffer normals into a temporary RT
    26.         int normalsID = Shader.PropertyToID("_NormalsCopy");
    27.         buf.GetTemporaryRT(normalsID, -1, -1);
    28.         buf.Blit(BuiltinRenderTextureType.GBuffer2, normalsID);
    29.  
    30.  
    31.         int depthID = Shader.PropertyToID("_DepthCopy");
    32.         buf.GetTemporaryRT(depthID, -1, -1);
    33.         buf.Blit(BuiltinRenderTextureType.Depth, depthID);
    34.  
    35.         RenderTargetIdentifier[] mrt = { BuiltinRenderTextureType.GBuffer0,
    36.                                            BuiltinRenderTextureType.GBuffer1,
    37.                                            BuiltinRenderTextureType.GBuffer2,
    38.                                            BuiltinRenderTextureType.GBuffer3};
    39.         buf.SetRenderTarget(mrt, BuiltinRenderTextureType.CameraTarget);
    40.  
    41.         buf.DrawMesh(m_CubeMesh, transform.localToWorldMatrix, m_Material);
    42.         // release temporary normals RT
    43.         buf.ReleaseTemporaryRT(depthID);
    44.         buf.ReleaseTemporaryRT(normalsID);
    45. }
    46.  
    And the shader used to render the image:
    Code (csharp):
    1.  
    2. Shader "Custom/Deferred"
    3. {
    4.     Properties
    5.     {
    6.        _MainTex ("Image", 2D) = "white" {}
    7.        _DepthTex ("Depth", 2D) = "white" {}
    8.     }
    9.     SubShader
    10.     {
    11.       Pass
    12.       {
    13.          Tags { "Queue"="Background" }
    14.           Fog { Mode Off } // no fog in g-buffers pass
    15.           ZWrite On
    16.           ZTest Off
    17.           Cull Off
    18.           Blend SrcAlpha Zero
    19.  
    20.           CGPROGRAM
    21.  
    22.           #pragma target 3.0
    23.           #pragma vertex vert
    24.           #pragma fragment frag
    25.           #pragma exclude_renderers nomrt
    26.  
    27.           #include "UnityCG.cginc"
    28.  
    29.           struct v2f
    30.           {
    31.              float4 pos : SV_POSITION;
    32.              float4 screenUV : TEXCOORD0;
    33.           };
    34.  
    35.          v2f vert (float3 v : POSITION)
    36.          {
    37.            v2f o;
    38.            o.pos = float4(v.x*2.0,v.y*2.0,1,1);
    39.            o.screenUV = float4(v.x-0.5,v.y-0.5,0,1);
    40.            return o;
    41.          }
    42.  
    43.          CBUFFER_START(UnityPerCamera2)
    44.          // float4x4 _CameraToWorld;
    45.          CBUFFER_END
    46.  
    47.          sampler2D _MainTex;
    48.          sampler2D _DepthTex;
    49.          uniform float4 _DepthTex_TexelSize;
    50.          sampler2D _NormalsCopy;
    51.          sampler2D _DepthCopy;
    52.  
    53.          void frag(v2f i,
    54.                         out half4 outColor : COLOR0,
    55.                         out half4 outSpecRoughness : COLOR1,
    56.                         out half4 outNormal : COLOR2,
    57.                         out half4 outEmission : COLOR3,
    58.                        out float outDepth:DEPTH)
    59.          {
    60.              float2 uv = i.screenUV.xy / i.screenUV.w;
    61.              fixed4 depthCustom = tex2D (_DepthTex, uv).xyzw;
    62.              outColor = tex2D (_MainTex, uv).bgra;
    63.              outColor.a = 1;
    64.  
    65.              outSpecRoughness = half4(1,1,1,0);
    66.              outNormal = half4(0,1,0,1); // for debug purpose
    67.              float4 depth = UnityViewToClipPos(depthCustom.rgb);
    68.              outEmission = half4(1,1,1,1);
    69.              outDepth = depth.z / depth.w;
    70.           }
    71.           ENDCG
    72.        }
    73.     }
    74.  
    75.  FallBack "Diffuse"
    76. }
    77.  
    I hope you can help me. Thanks :)
     
    DxStd_IgnatPribylov likes this.
  2. Andromede

    Andromede

    Joined:
    Dec 9, 2016
    Posts:
    2
    Never mind. I've found the answer by my own ;)

    I had to write in the stencil buffer a value between [128,255].

    Code (CSharp):
    1. SubShader
    2.     {
    3.         Pass
    4.         {
    5.             Stencil {
    6.                 Ref 128
    7.                 Comp always
    8.                 Pass replace
    9.             }
    10.             Tags { "RenderType"="Opaque" "Queue"="Geometry" }
    11. .
    12. .
    13. .
    Sorry for this stupid thread :D
     
    customphase likes this.
  3. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    246
    Thanks for sharing the solution! And it wasnt stupid at all, i was breaking my head over this for a long time, until i found your post. :)