Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Retrieving Gbuffer RenderTexture for ImageEffect

Discussion in 'Image Effects' started by wmpunk, Jul 24, 2017.

  1. wmpunk

    wmpunk

    Joined:
    Sep 5, 2015
    Posts:
    71
    Hello, I am having some trouble trying to retrieve render textures from the gbuffer. I am trying to get the gbuffer normal texture with a command buffer to overlay it on the main screen. I am pretty new to shaders and command buffers so If any one has time to take a look at this and tell me what I am doing wrong it would be greatly appreciated :)

    code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5.  
    6. public class TestImageEffect : MonoBehaviour {
    7.  
    8.     public Material material;
    9.     [Range(0,1)]
    10.     public float blend;
    11.  
    12.     public RenderTexture normalRT;
    13.  
    14.     CommandBuffer cBuff;
    15.     Camera cam;
    16.  
    17.     private void OnDisable()
    18.     {
    19.         cam = GetComponent<Camera>();
    20.         if (cBuff != null)
    21.         {
    22.             cam.RemoveCommandBuffer(CameraEvent.AfterGBuffer, cBuff);
    23.         }
    24.     }
    25.  
    26.     private void OnEnable()
    27.     {
    28.        
    29.         cam = GetComponent<Camera>();
    30.         normalRT = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGB2101010);
    31.  
    32.         if (cBuff == null)
    33.         {
    34.            
    35.             cBuff = new UnityEngine.Rendering.CommandBuffer();    
    36.             cBuff.name = "GetNormalRender";
    37.             cBuff.Blit( BuiltinRenderTextureType.GBuffer2, normalRT);
    38.             cam.AddCommandBuffer(CameraEvent.AfterGBuffer, cBuff);
    39.         }
    40.     }
    41.  
    42.  
    43.     [ImageEffectOpaque]
    44.     private void OnRenderImage(RenderTexture source, RenderTexture destination)
    45.     {
    46.         if (material && normalRT)
    47.         {
    48.             material.SetFloat("_Blend", blend);
    49.             material.SetTexture("_Normal", normalRT);
    50.             Graphics.Blit(source, destination, material);        
    51.         }
    52.         else Graphics.Blit(source, destination);    
    53.     }
    54.  
    55.  
    56. }
    And this is the shader I am using made with Amplify

    Code (CSharp):
    1. Shader "TestImageEffect"
    2. {
    3.     Properties
    4.     {
    5.         [HideInInspector] __dirty( "", Int ) = 1
    6.         _MaskClipValue( "Mask Clip Value", Float ) = 0.5
    7.         [NoScaleOffset]_MainTex("MainTex", 2D) = "white" {}
    8.         _Blend("Blend", Range( 0 , 1)) = 0
    9.         _Normal("Normal", 2D) = "bump" {}
    10.         [HideInInspector] _texcoord( "", 2D ) = "white" {}
    11.     }
    12.  
    13.     SubShader
    14.     {
    15.         Tags{ "RenderType" = "Overlay"  "Queue" = "Overlay+0" "IsEmissive" = "true"  }
    16.         Cull Off
    17.         ZWrite Off
    18.         ZTest Always
    19.         CGPROGRAM
    20.         #pragma target 5.0
    21.         #pragma only_renderers d3d9 d3d11 glcore gles gles3 wiiu
    22.         #pragma surface surf Unlit keepalpha noshadow noambient novertexlights nolightmap  nodynlightmap nodirlightmap nofog nometa noforwardadd
    23.         struct Input
    24.         {
    25.             float2 uv_texcoord;
    26.         };
    27.  
    28.         uniform sampler2D _MainTex;
    29.         uniform sampler2D _Normal;
    30.         uniform float4 _Normal_ST;
    31.         uniform float _Blend;
    32.         uniform float _MaskClipValue = 0.5;
    33.  
    34.         inline fixed4 LightingUnlit( SurfaceOutput s, half3 lightDir, half atten )
    35.         {
    36.             return fixed4 ( 0, 0, 0, s.Alpha );
    37.         }
    38.  
    39.         void surf( Input i , inout SurfaceOutput o )
    40.         {
    41.             float2 uv_MainTex = i.uv_texcoord;
    42.             float2 uv_Normal = i.uv_texcoord * _Normal_ST.xy + _Normal_ST.zw;
    43.             o.Emission = lerp( tex2D( _MainTex, uv_MainTex ) , float4( UnpackNormal( tex2D( _Normal, uv_Normal ) ) , 0.0 ) , _Blend ).rgb;
    44.             o.Alpha = 1;
    45.         }
    46.  
    47.         ENDCG
    48.     }
    49.     CustomEditor "ASEMaterialInspector"
    50. }