Search Unity

Chroma Key

Discussion in 'Shaders' started by Xanatos38, Sep 22, 2014.

  1. Xanatos38

    Xanatos38

    Joined:
    Mar 31, 2014
    Posts:
    4
    Hi,
    I am not confortable with Shaders, but what I want to do needs them.

    I want to create a chroma key shader to remove all the green part of a texture and replace it with alpha.

    Actually, I managed to remove all the green in the texture, but it removes all the green layer. I only need to keep the green layer on the picture inside my texture. (like a green background in the movies)

    Code (csharp):
    1.  
    2. Shader "Custom/ChromaKey" {
    3.  
    4.     Properties {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Color ("Color", Color) = (1,1,1,1)
    7.     }
    8.  
    9.     SubShader {
    10.         Tags {
    11.             "RenderType" = "Transparent"
    12.         }
    13.      
    14.         Lighting Off
    15.         ZWrite Off
    16.         Blend SrcAlpha OneMinusSrcAlpha
    17.      
    18.         CGPROGRAM
    19.         #pragma surface surf Lambert
    20.  
    21.         sampler2D _MainTex;
    22.         float4 _Color;
    23.  
    24.         struct Input {
    25.             float2 uv_MainTex;
    26.         };
    27.  
    28.         void surf (Input IN, inout SurfaceOutput o) {
    29.             half4 c = tex2D(_MainTex, IN.uv_MainTex);
    30.             o.Albedo = c.rgb;
    31.             o.Alpha = lerp(o.Albedo, _Color, c.g);
    32.         }
    33.         ENDCG
    34.      
    35.     }
    36.  
    37.     FallBack "Diffuse"
    38. }
    39.  

    Thank you in advance for your help.
     
    Last edited: Sep 22, 2014
  2. frankrs

    frankrs

    Joined:
    Aug 29, 2009
    Posts:
    300
    you probably want to clip the transparency rather than blend it