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

Question How do you make weapons not clip through geometry with deferred rendering path?

Discussion in '2021.2 Beta' started by thiskidcalledtom, Oct 15, 2021.

  1. thiskidcalledtom

    thiskidcalledtom

    Joined:
    Nov 9, 2017
    Posts:
    35
    Hello! I'm used to stacking cameras to make weapons not clip through geometry.

    im using deferred rendering on 2021 0b14 and when trying to stack cameras... it says `overlay camera cannot be used with deferred rendering`

    So my question is: How do i make certain objects render ontop of everything else without camera stacking? (easily) Thank you.!
     
  2. Saniell

    Saniell

    Joined:
    Oct 24, 2015
    Posts:
    194
    I did in a kind of a non optimal way, but it seems to be working (I'm using BuiltIn RP). The idea is to draw all hand-related meshes first using depth clear shader, which looks like this:

    Code (csharp):
    1.  
    2. Pass
    3. {
    4.             Name "DEPTH CLEAR"
    5.             Tags { "LightMode" = "DepthClear"}
    6.             ZWrite On
    7.             ZTest Always
    8.             //ColorMask 0
    9.  
    10.             // This allows to work nicely with skyboxes
    11.             Stencil
    12.             {
    13.                 Ref 192
    14.                 WriteMask 207
    15.                 Comp Always
    16.                 Pass Replace
    17.                 Fail Keep
    18.                 ZFail Keep
    19.             }
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.             #pragma target 2.0
    24.             #include "UnityCG.cginc"
    25.    
    26.             float4 vert (float4 vertex : POSITION) : SV_POSITION
    27.             {
    28.                 return UnityObjectToClipPos(vertex);
    29.             }
    30.    
    31.             float frag (float4 clipPos : SV_POSITION) : SV_Depth
    32.             {
    33.                 #if defined(UNITY_REVERSED_Z)
    34.                     return 0;
    35.                 #else
    36.                     return 1;
    37.                 #endif
    38.             }
    39.             ENDCG
    40. }
    41.  
    Then make a CommandBuffer, set it to CameraEvent.AfterGBuffer. Draw all of your hand geometry with Graphics.DrawRenderer using this depth-clear shader. Then render all geometry again using their default materials, like so:

    Code (CSharp):
    1.  
    2. foreach (var renderer in renderers)
    3. {
    4.        buffer.DrawRenderer(renderer, m_ClearMaterial, 0, depthClearPassID);
    5. }
    6.  
    7.  foreach (var renderer in renderers)
    8.  {
    9.      var deferredPassID = renderer.sharedMaterial.FindPass("DEFERRED");
    10.       buffer.DrawRenderer(renderer, renderer.sharedMaterial, 0, deferredPassID);
    11.  }
    Except you also shouldn't turn off original mesh renderers. Otherwise unity won't update animations on them, so you will techically draw these hands 3 times. But since there are not many meshes, it shouldn't be a problem.
     
  3. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    @thiskidcalledtom you can overlay a forward rendering camera on top of deferred base camera.
    I'm interested to hear some thoughts about stacking deferred on top of deferred, we could discuss supporting it.