- Home /
Is it possible to avoid writing to certain g-buffers?
I'm working on a shader that must write an object's worldspace normals and depth to the corresponding buffers, but leave all remaining g-buffers untouched.
Is this possible?
I've added a snippet of my code below to demonstrate what I'm trying to achieve. The problem with this approach is Unity still does write to the other g-buffers (with default values).
 Shader "Write Depth and WS-Normals"
 {   
     SubShader
     {
         Tags
         {
             "RenderType" = "Opaque"
             "Queue" = "Geometry"
             "LightMode" = "Deferred"
         }      
         Pass
         {
             ZWrite On
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag           
             #include "UnityCG.cginc"
             
             struct appdata
             {
                 float3 normal    : NORMAL;
                 float4 vertex    : POSITION;
             };
             
             struct v2f
             {
                 float3 normal    : TEXCOORD0;
                 float4 vertex    : SV_POSITION;
             };
             
             v2f vert ( appdata v )
             {
                 v2f o;
                 o.normal = mul( unity_ObjectToWorld, float4( v.normal, 0 ) ).xyz;
                 o.vertex = UnityObjectToClipPos( v.vertex );
                 return o;
             }
             
             void frag ( v2f i, out float depthOut : DEPTH, out float4 normalsOut : SV_Target2 )
             {
                 normalsOut = float4( i.normal * 0.5 + 0.5, 1 );
                 depthOut = 1; // Depth established here.
                 float alpha = 1; // Alpha established here.
 
                 clip( alpha );
             }
             ENDCG
         }
     }
 }
Answer by xibanya · Dec 24, 2019 at 07:22 AM
You could try making a shader with two passes, one that writes each texture you want, then make a script that uses a command buffer to blit each pass to the corresponding gbuffer texture. Get the example project here https://blogs.unity3d.com/2015/02/06/extending-unity-5-rendering-pipeline-command-buffers/ and look at how they do the decals. Something like that would probably do the trick.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                