Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

RGB Split Shader

Discussion in 'Shaders' started by Hesham, Dec 30, 2013.

  1. Hesham

    Hesham

    Joined:
    May 29, 2008
    Posts:
    147
    I'm nit a shader programmer, but would appreciate if someone can point in the right direction. I want a shader that does an RGB Split like in the video posted below.

    I wouldn't mind to buy it off the asset store but I checked most of the packages and none seem to have it.

     
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    My "aubergines postprocess effects" pack has this effect. Its the _Lightwave shader in the package.
    You need unity pro for it.
     
  3. Hesham

    Hesham

    Joined:
    May 29, 2008
    Posts:
    147
    excellent .. buying now
     
  4. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    I have my email in the readme file, let me know when you need customization or extra features as its pretty generic out of the box.
     
  5. Hesham

    Hesham

    Joined:
    May 29, 2008
    Posts:
    147
  6. WGermany

    WGermany

    Joined:
    Jun 27, 2013
    Posts:
    78
    For those who want to know the basis for how its done, heres a free example below

    Reference : http://blog.jahfer.com/2012/04/02/experimenting-shaders-openframeworks/

    Code (csharp):
    1. Shader "Custom/ChromaticAberration" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     }
    5.     SubShader
    6.     {
    7.         Pass
    8.         {
    9.        
    10.         CGPROGRAM
    11.         #pragma vertex vert_img
    12.         #pragma fragment frag
    13.         #pragma fragmentoption ARB_precision_hint_fastest
    14.         #include "UnityCG.cginc"
    15.  
    16.         uniform sampler2D _MainTex;
    17.         uniform float _AberrationOffset;
    18.  
    19.         float4 frag(v2f_img i) : COLOR
    20.         {
    21.  
    22.             float2 coords = i.uv.xy;
    23.            
    24.             _AberrationOffset /= 300.0f;
    25.            
    26.             //Red Channel
    27.             float4 red = tex2D(_MainTex , coords.xy - _AberrationOffset);
    28.             //Green Channel
    29.             float4 green = tex2D(_MainTex, coords.xy );
    30.             //Blue Channel
    31.             float4 blue = tex2D(_MainTex, coords.xy + _AberrationOffset);
    32.            
    33.             float4 finalColor = float4(red.r, green.g, blue.b, 1.0f);
    34.             return finalColor;
    35.        
    36.         }
    37.  
    38.         ENDCG
    39.         }
    40.     }
    41. }


    C# code, gets attached to the camera

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. public class ChromaticAberration : MonoBehaviour {
    6.    
    7.     #region Variables
    8.     public Shader curShader;
    9.     public float ChromaticAbberation = 1.0f;
    10.     private Material curMaterial;
    11.     #endregion
    12.    
    13.     #region Properties
    14.     Material material
    15.     {
    16.         get
    17.         {
    18.             if(curMaterial == null)
    19.             {
    20.                 curMaterial = new Material(curShader);
    21.                 curMaterial.hideFlags = HideFlags.HideAndDontSave; 
    22.             }
    23.             return curMaterial;
    24.         }
    25.     }
    26.     #endregion
    27.     // Use this for initialization
    28.     void Start ()
    29.     {
    30.         if(!SystemInfo.supportsImageEffects)
    31.         {
    32.             enabled = false;
    33.             return;
    34.         }
    35.     }
    36.    
    37.     void OnRenderImage (RenderTexture sourceTexture, RenderTexture destTexture)
    38.     {
    39.         if(curShader != null)
    40.         {
    41.             material.SetFloat("_AberrationOffset", ChromaticAbberation);
    42.             Graphics.Blit(sourceTexture, destTexture, material);
    43.         }
    44.         else
    45.         {
    46.             Graphics.Blit(sourceTexture, destTexture); 
    47.         }
    48.        
    49.        
    50.     }
    51.    
    52.     // Update is called once per frame
    53.     void Update ()
    54.     {
    55.        
    56.     }
    57.    
    58.     void OnDisable ()
    59.     {
    60.         if(curMaterial)
    61.         {
    62.             DestroyImmediate(curMaterial); 
    63.         }
    64.        
    65.     }
    66.    
    67.    
    68. }
     
    ViicEsquivel likes this.
  7. bfowle

    bfowle

    Joined:
    Oct 1, 2012
    Posts:
    23
  8. Thomas-Pasieka

    Thomas-Pasieka

    Joined:
    Sep 19, 2005
    Posts:
    2,174
  9. bfowle

    bfowle

    Joined:
    Oct 1, 2012
    Posts:
    23
    Thanks for the heads up! Much appreciated.