How to remove complete green background of video (671963)

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

Shader "Custom/ChromaKey" {
Properties {
  _MainTex ("Base (RGB)", 2D) = "white" {}
  _AlphaValue ("Alpha Value", Range(0.0,1.0)) = 1.0
}
SubShader {
  Tags { "Queue"="Transparent" "RenderType"="Transparent" }
  LOD 800
  
  CGPROGRAM
  #pragma surface surf Lambert alpha

  sampler2D _MainTex;
  float _AlphaValue;

  struct Input {
   float2 uv_MainTex;
  };

  void surf (Input IN, inout SurfaceOutput o) {
   half4 c = tex2D (_MainTex, IN.uv_MainTex);
   o.Emission = c.rgb;
  
   // Green screen level - leaves minor green glow
   if (c.g == 1) {
       o.Alpha = 0.0;
   }else{
       o.Alpha = c.a;
   }

  }
  ENDCG
}
FallBack "Diffuse"
}

The ‘Getting Started’ section is intended for those just starting out with a bit more basic concepts. Shaders are a dark art that confuse the crap out of many, including me.

I’d recommend trying the specific Shaders subforum. It’s probably not as active as here, so you’ll have to wait a bit longer for a response, but they’re better equipped over there to handle this problem.

2 Likes

The general trick with cutouts is to cut slightly inside the object, which removes any bleed between the background and the object. You could do this by checking a few pixels in each direction, and if any of these are green, then discard the pixel.

There would be performance considerations here, checking five pixels is significantly more expensive then checking a single pixel. There might be a magic trick to solve this in a more performant manner.

As an alternative you could drop your reject threshold from 1 to .95 or .9. This would drop pixels that are mostly green, but also risk dropping good pixels. Make it a property and then mess with it in the inspector to get the right threshold value.

Remind me to write a basic intro tutorial at some point. Shaders are evil black magic. But its possible to learn to do evil black magic with the right guide.

1 Like