@ chroma key shader with any colours

Two things into one:

  1. Can there be a chroma key shader that I choose what solid colour (even under restrictions) will turn to transparent by entering key code values from inspector? Example not green but also custom pink, yellow, etc.
  2. Can there be a chroma key shader that can “key” two different colours to transparent? example. Green and Blue?
  3. Can there be a shader that combines two two above (example picture)?

Please anyone have a shader script of any, or instructions will help because I don;t understand how to write shader code.
Thanx.

  1. Yes. You can set chroma keying to be done for any color. I’ve personally used even a full texture for keying rather than a particular color. If your camera is fixed you can have it take a picture of the set without your subject, and then use each pixel of that as your key.

  2. If you wanna key out 2 particular colors per pixel then the cost grows, but yes, it’s doable.

  3. I think your example kind of asks more for what I described in 1, unless your camera has to move. Either way, yes.

The way to perform keying accurately is to convert each RGB color space pixel into “Lab” color space values, and then find the “distance” between the values. You can find more specifics here: Welcome to Bruce Lindbloom's Web Site

The steps you’d need to run through are as follows:

  1. Convert your destination color (key color) to XYZ, and then to Lab.
  2. Do the same with your source color (texture you want parts of keyed out).
  3. Use Delta E (CIE 1994) to calculate the overall difference in the color to determine how transparent the pixel should be. Lower values approaching 0 should be more transparent, higher values more opaque. There are other Delta E that improve on accuracy at the cost of performance. It’s up to you to choose which one you can afford to use.
  4. Return your source color with the newly calculated alpha.

Done! :slight_smile:

1 Like

Thank you very much.
Very interesting what your (Goal2112) replying. These are concepts I have never though I should take under consideration.
Still I don’t get it, and moreover, I don’t get how to apply the above in unity.
I also found this tool: CIE94 Delta-E Calculator - ColorMine.org
Never-mind. I think I’ll get it as soon as I manage to understand all the previous steps and the mechanism behind it.

In a more simple version that I don’t really understand what I did and need further assistance, I have modified this standard shader chroma key script for green screen:

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 200
 
  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 >= 0.67f && c.r <= 0.65f && c.b <= 0.65f && _AlphaValue == 0.0)
         {
          o.Alpha = 0.0;
         }
         else
         {
          o.Alpha = c.a;
         }
  }
  ENDCG
}
FallBack "Diffuse"
}

by changing the “if” statement to:

if ((c.g <= 0.65f && c.r >= 0.67f && c.b <= 0.65f && _AlphaValue == 0.0)|| (c.g >= 0.67f && c.r <= 0.65f && c.b <= 0.65f && _AlphaValue == 0.0))

that made unity to set alpha == 0 for green and red colour.
I can assume that c.g / c.r / c.b stands for rgb green / red / blue values in a range of [0,1] instead of [0,255].

My question are

  1. How to set / calculate these equations lets say for pink colour.
  2. What is the memory cost? Is it just almost the same with the original script or it is double the cost?
  3. The author of the original script refers that his equation “…leaves a minor green glow” . How can we control / omit this glow, as well as other colour attributes?

I figured it out. Here is the code, btw I have no idea of how to do shaders and I wanted this as well. After looking at various syntaxes, I figured it out myself:

Shader "Custom/ChromaKey" {
 
Properties {
  _MainTex ("Base (RGB)", 2D) = "white" {}
  _AlphaValue ("Alpha Value", Range(0.0,1.0)) = 1.0
  _Color("Color", Color) = (1,1,1,1)

}
 
SubShader {
  Tags { "Queue"="Transparent" "RenderType"="Transparent" }
  LOD 200
  
  CGPROGRAM
  #pragma surface surf Lambert alpha
  sampler2D _MainTex;
  float _AlphaValue;
  float4 _Color;

  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
   // Note how the green value operator check is greater than and rest is less than
   if (c.g >= _Color.g && c.r <= _Color.r && c.b <= _Color.b)
         {
          o.Alpha = 0.0;
         }
         else
         {
          o.Alpha = c.a;
         }
  }
  ENDCG
}
FallBack "Diffuse"
}

Now you can choose any color and it will work.

That is a very poor approach to a chroma key shader. Like goal2112 said, you want to enable partial transparency and you want to use a color space other than rgb to make the comparison more stable under varying shadows and lighting.