Search Unity

How to remove complete green background of video

Discussion in 'Shaders' started by ssuresh1295, Jul 24, 2017.

  1. ssuresh1295

    ssuresh1295

    Joined:
    Jul 13, 2017
    Posts:
    20
    Hi,
    I am developing an app with unity. In that, I want to remove the green background of the video and should make transparent. I have used shader script to remove the background. It's removing background but still, it's not clearing border of the content.

    I used this script please tell any correction in this or tell me any other best way to do that

    Code (CSharp):
    1. Shader "Custom/ChromaKey" {
    2. Properties {
    3.   _MainTex ("Base (RGB)", 2D) = "white" {}
    4.   _AlphaValue ("Alpha Value", Range(0.0,1.0)) = 1.0
    5. }
    6. SubShader {
    7.   Tags { "Queue"="Transparent" "RenderType"="Transparent" }
    8.   LOD 800
    9.   CGPROGRAM
    10.   #pragma surface surf Lambert alpha
    11.   sampler2D _MainTex;
    12.   float _AlphaValue;
    13.   struct Input {
    14.    float2 uv_MainTex;
    15.   };
    16.   void surf (Input IN, inout SurfaceOutput o) {
    17.    half4 c = tex2D (_MainTex, IN.uv_MainTex);
    18.    o.Emission = c.rgb;
    19.    // Green screen level - leaves minor green glow
    20.    if (c.g == 1) {
    21.        o.Alpha = 0.0;
    22.    }else{
    23.        o.Alpha = c.a;
    24.    }
    25.   }
    26.   ENDCG
    27. }
    28. FallBack "Diffuse"
    29. }
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Hi ssuresh1295,

    if it's one-pixel border that this doesn't remove (and it seems so to me) then you probably need to sample more texels than just the current one. The border will likely not be completely green, so you can take a look on some texels around the one you're sampling already (3x3 area, for example). If the majority of the pixels are green, this pixel can be considered part of the background.
    Btw, the check you have will work for fully white pixels as well, so it's probably a good idea to check full rgb value.
     
    Kiwasi likes this.
  3. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Using c.g == 1 to determine green is pretty ridiculous. It for one thing considers pure yellow or white to also be green. But #00FE00 would be considered not green at all.

    What you want is to specify a reference color and falloff. And fade transparency based on that:
    Code (csharp):
    1.  
    2. half alpha_pass = saturate(distance(c.rgb, _AlphaColor) / _AlphaFalloff); // Amount of not matching the color
    3. c.a *= alpha_pass; // Adjustment of alpha
    4. half alpha_pass_limit = max(0.1, alpha_pass); // Prevents a division by zero
    5. c.rgb = (c.rgb - (1 - alpha_pass_limit) * _AlphaColor) / alpha_pass_limit; // Adjustment of color
    6.  
    Where _AlphaColor is a half3 color input for the color to remove and _AlphaFalloff a half input for the range to remove. I just made up those last two lines to adjust the color, but if my math is right, it should work. Note that this is still a pretty simple approach to a green screen shader, but it should be half decent.
     
  4. ssuresh1295

    ssuresh1295

    Joined:
    Jul 13, 2017
    Posts:
    20
    Hi jvo3dc,

    Where should I add this code in my script.
     
  5. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Between half4 c=.... and o.Emission=....

    The whole if below that should then be replaced by just:
    Code (csharp):
    1.  
    2. o.Alpha = c.a;
    3.  
     
  6. ssuresh1295

    ssuresh1295

    Joined:
    Jul 13, 2017
    Posts:
    20
    Hi jvo3dc,

    I used your code but now video not even showing. Fully came with the white screen only audio is coming. I used your code like this please tell any wrong on this

    Code (CSharp):
    1. Shader "Custom/ChromaKey" {
    2.  
    3. Properties {
    4.   _MainTex ("Base (RGB)", 2D) = "white" {}
    5.   _AlphaValue ("Alpha Value", Range(0.0,1.0)) = 1.0
    6.   _Threshold ("Threshold Value", Range(0.0,1.0)) = 1.0
    7. }
    8.  
    9. SubShader {
    10.   Tags { "Queue"="Transparent" "RenderType"="Transparent" }
    11.   LOD 800
    12.  
    13.   CGPROGRAM
    14.   #pragma surface surf Lambert alpha
    15.   sampler2D _MainTex;
    16.   float _AlphaValue;
    17.   struct Input {
    18.    float2 uv_MainTex;
    19.   };
    20.   void surf (Input IN, inout SurfaceOutput o) {
    21.    half4 c = tex2D (_MainTex, IN.uv_MainTex);
    22.    o.Emission = c.rgb;
    23.    
    24.    // Green screen level - leaves minor green glow
    25.    //c.g >= 0.39215686274f && c.r <= 0.0f && c.b <= 0.0f && _AlphaValue == 0.0
    26.    if (c.g == 1) {
    27.        o.Alpha = 0.0;
    28.        //o.glow = 1;
    29.    }else{
    30.        //o.Alpha = c.a;
    31.         half alpha_pass = saturate(distance(c.rgb, _AlphaColor) / _AlphaFalloff); // Amount of not matching the color
    32.         c.a *= alpha_pass; // Adjustment of alpha
    33.         half alpha_pass_limit = max(0.1, alpha_pass); // Prevents a division by zero
    34.         c.rgb = (c.rgb - (1 - alpha_pass_limit) * _AlphaColor) / alpha_pass_limit; // Adjustment of color
    35.    }
    36.   }
    37.   ENDCG
    38. }
    39. FallBack "Diffuse"
    40. }
     
  7. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Code (csharp):
    1.  
    2. void surf (Input IN, inout SurfaceOutput o) {
    3.    half4 c = tex2D (_MainTex, IN.uv_MainTex);
    4.    half alpha_pass = saturate(distance(c.rgb, _AlphaColor) / _AlphaFalloff); // Amount of not matching the color
    5.    c.a *= alpha_pass; // Adjustment of alpha
    6.    half alpha_pass_limit = max(0.1, alpha_pass); // Prevents a division by zero
    7.    c.rgb = (c.rgb - (1 - alpha_pass_limit) * _AlphaColor) / alpha_pass_limit; // Adjustment of color
    8.    o.Emission = c.rgb;
    9.    o.Alpha = c.a;
    10. }
    11.  
     
  8. ssuresh1295

    ssuresh1295

    Joined:
    Jul 13, 2017
    Posts:
    20
    Tried this also but same white screen only coming. If there is no if conditio then how can I check my green color to remove.
     
    Last edited: Jul 31, 2017
  9. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Well, you're doing something else wrong, since your code before just said:
    Code (csharp):
    1.  
    2. half4 c = tex2D (_MainTex, IN.uv_MainTex);
    3. o.Emission = c.rgb;
    4.  
    After you've fixed whatever is going wrong, what are your values of _AlphaColor and _AlphaFallof?

    Finally, there is no reason to use an if to replace a color with transparency. It's like fuzzy logic.
     
  10. ssuresh1295

    ssuresh1295

    Joined:
    Jul 13, 2017
    Posts:
    20
    I want remove r= 0, g=255 , b=0 This rgb value I want to remove. How to give value for that.
     
  11. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Set _AlphaColor to 0, 1, 0 and then play with the _AlphaFalloff value. (Between 0 and 1 roughly)
     
  12. ssuresh1295

    ssuresh1295

    Joined:
    Jul 13, 2017
    Posts:
    20
    @jvo3dc
    I have tried Like this

    Code (CSharp):
    1.  half4 c = tex2D (_MainTex, IN.uv_MainTex);
    2.    half alpha_pass = saturate(distance(c.rgb, 0, 1, 0) / 1); // Amount of not matching the color
    And this

    Code (CSharp):
    1. half4 c = tex2D (_MainTex, IN.uv_MainTex);
    2.    half alpha_pass = saturate(distance(0, 1, 0, 0) / 1); // Amount of not matching the color
    But I am getting following error at compile time for both

    Shader error in 'Custom/ChromaKeyWhite': unable to find compatible overloaded function "distance(half3, int, int, int)" unable to find compatible overloaded function "saturate(error)" at line 39 (on d3d9)
     
    Last edited: Aug 28, 2017
  13. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Well, distance takes 2 parameters of the same vector type and not 4 parameters. So
    distance (c.rgb, half3 (0.0, 1.0, 0.0))
     
  14. Wattosan

    Wattosan

    Joined:
    Mar 22, 2013
    Posts:
    460
    I managed to get it working but my model shows white instead of its normal albedo colors.