Search Unity

Question Image Effect before Post-Process

Discussion in 'Shaders' started by oukibt_unity, Sep 13, 2022.

  1. oukibt_unity

    oukibt_unity

    Joined:
    May 6, 2022
    Posts:
    19
    Hello. How can I use my FX Shader (Image Effect) before Post-Processing?

    Shader:
    Code (CSharp):
    1. Shader "Custom/TestShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "black" {}
    6.         _Del("Delimiter", Float) = 64.0
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.         Tags
    12.         {
    13.             "Queue"="Geometry"
    14.         }
    15.  
    16.         Pass
    17.         {
    18.             CGPROGRAM
    19.  
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.  
    23.             #include "UnityCG.cginc"
    24.  
    25.             sampler2D _MainTex;
    26.             float4 _MainTex_ST;
    27.             float _Del;
    28.  
    29.             struct v2f
    30.             {
    31.                 float4 pos : SV_POSITION;
    32.                 float2 uv : TEXCOORD0;
    33.             };
    34.  
    35.             v2f vert(appdata_base v)
    36.             {
    37.                 v2f o;
    38.  
    39.                 o.pos = UnityObjectToClipPos(v.vertex);
    40.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    41.  
    42.                 return o;
    43.             }
    44.  
    45.             fixed4 frag(v2f i) : SV_Target
    46.             {
    47.                 float r = tex2D(_MainTex, i.uv).r;
    48.                 float g = tex2D(_MainTex, i.uv).g;
    49.                 float b = tex2D(_MainTex, i.uv).b;
    50.  
    51.                 int val = 20;
    52.  
    53.                 float3 color = float3(r - ((int)(r * _Del) % val) / 255.0, g - ((int)(g * _Del) % val) / 255.0, b - ((int)(b * _Del) % val) / 255.0);
    54.  
    55.                 return fixed4(color.r, color.g, color.b, 1.0);
    56.             }
    57.  
    58.             ENDCG
    59.         }
    60.     }
    61.     FallBack "Diffuse"
    62. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5.  
    6. [ExecuteInEditMode]
    7. [ImageEffectAllowedInSceneView]
    8. public class TestShader : MonoBehaviour
    9. {
    10.     private Shader shader;
    11.     private Material material;
    12.  
    13.     [Range(1, 512)]
    14.     public int m_delimiter = 64;
    15.     private int m_last_delimiter;
    16.  
    17.     public void Start()
    18.     {
    19.         m_last_delimiter = m_delimiter - 1;
    20.  
    21.         //
    22.  
    23.         shader = Shader.Find("Custom/TestShader");
    24.         material = new Material(shader);
    25.  
    26.         if (!SystemInfo.supportsImageEffects)
    27.         {
    28.             enabled = false;
    29.             return;
    30.         }
    31.  
    32.         if (!shader && !shader.isSupported)
    33.         {
    34.             enabled = false;
    35.         }
    36.     }
    37.  
    38.     public void OnRenderImage(RenderTexture inTexture, RenderTexture outTexture)
    39.     {
    40.         if (shader != null)
    41.         {
    42.             if (m_delimiter != m_last_delimiter)
    43.             {
    44.                 m_last_delimiter = m_delimiter;
    45.                 material.SetFloat("_Del", m_delimiter);
    46.             }
    47.  
    48.             Graphics.Blit(inTexture, outTexture, material);
    49.         }
    50.         else
    51.         {
    52.             Graphics.Blit(inTexture, outTexture);
    53.         }
    54.     }
    55. }

    Result without Post-Processing:


    Result with Post-Processing that include Vignette and SSAO:


    Let's just focus on the vignette effect for now. My FX Shader limits the number of colors on screen (int val = 20 in shader code). But we can see that the vignette effect is also affected by my shader. How do I get the vignette effect and SSAO applied after my shader?