Search Unity

Chroma keying then blur

Discussion in 'Shaders' started by Benoit Dufresne, Aug 28, 2015.

  1. Benoit Dufresne

    Benoit Dufresne

    Joined:
    Feb 26, 2013
    Posts:
    4
    Hey there,

    I've written a chroma keying shader that works well, but I'd like to be able to take it one step further and blur then edges of the resulting image.

    I'm new to writing shaders, and am not sure how to do this. I've seen plenty of shaders doing a grabpass and blurring whats behind the object, but never the object itself, even less the result of a previous pass. Also what I'm looking to blur is the alpha channel rather than the colors (I just want smooth edges, not a blurry picture!).

    Here's the code so far:

    Code (CSharp):
    1. Shader "Custom/YCrCb_Dist" {
    2.  
    3.     Properties {
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _thresh ("Threshold", Range (0, 255)) = 80
    6.         _slope ("Slope", Range (0, 1)) = 0.2
    7.         _keyingColor ("Key Color", Color) = (0.8,1,0.8,1)
    8.     }
    9.    
    10.     SubShader {
    11.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    12.         LOD 100
    13.         Lighting Off
    14.         ZWrite Off
    15.         AlphaTest Off
    16.         Blend SrcAlpha OneMinusSrcAlpha
    17.        
    18.         Pass {
    19.             CGPROGRAM
    20.                 #pragma vertex vert_img
    21.                 #pragma fragment frag
    22.                 #pragma fragmentoption ARB_precision_hint_fastest
    23.  
    24.                 sampler2D _MainTex;
    25.                 float3 _keyingColor;
    26.                 float _thresh;
    27.                 float _slope;
    28.                 float2 _keyingCrCb;
    29.                
    30.  
    31.                  #include "UnityCG.cginc"
    32.                 float4 frag(v2f_img i) : COLOR {
    33.                     float3 ic = tex2D(_MainTex, i.uv).rgb;
    34.                    
    35.                     //transform the keying color to YCrCb (just the CrCb part)
    36.                     _keyingCrCb = float2(128-37.945*_keyingColor.x - 74.494*_keyingColor.y + 112.439*_keyingColor.z, 128+112.439*_keyingColor.x-94.154*_keyingColor.y-18.285*_keyingColor.z);
    37.                     //transform the current pixel's color to YCrCb (just the CrCb part)
    38.                     float2 crcb = float2(128-37.945*ic.x - 74.494*ic.y + 112.439*ic.z, 128+112.439*ic.x-94.154*ic.y-18.285*ic.z);
    39.                  
    40.                     //euclidian cutoff
    41.                     float d = abs(length(abs(_keyingCrCb - crcb)));
    42.        
    43.                     //little slope to be more forgiving
    44.                     float edge0 = _thresh * (1.0 - _slope);
    45.                     float alpha = smoothstep(edge0, _thresh, d);
    46.                    
    47.                     //slap the alpha value onto the original color
    48.                     return float4(ic, alpha);
    49.                 }
    50.             ENDCG
    51.         }
    52.  
    53.     }
    54.    
    55.     FallBack "Unlit"
    56. }

    How would I go about blurring the resulting texture's alpha channel?