Set one color transparent in texture / shader


I have a webcamtexture (have set it as guitexture, or also as game object texture) and want to set one specific color transparent for green box effect. I can’t find a shader or any texture/material definition that would do that. Obviously I do not have pngs with alpha channel, since it is webcamtexture - can anyone help?

thank you!

What about something like this? I’ve done it for a surface shader, though you could do something similar with a fragment shader or something else. The key is that I provide a transparent colour and a threshold. If the shader detects the texture colour is too close to the transparent one, it discards the pixels. In theory, if you wanted to get cleverer you could make it modify the alpha based on closeness, then fade out elegantly as you get close to the transparent colour. Should get you started though :slight_smile:

Shader "Custom/transparent_col" {
	Properties {
		_Color ("Color", Color) = (1,1,1,1)
		_TransparentColor ("Transparent Color", Color) = (1,1,1,1)
		_Threshold ("Threshhold", Float) = 0.1
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
	SubShader {
		Tags { "Queue"="Transparent" "RenderType"="Transparent" }
		LOD 200
		#pragma surface surf Lambert alpha

		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;

		fixed4 _Color;
		fixed4 _TransparentColor;
		half _Threshold;

		void surf (Input IN, inout SurfaceOutput o) {
			// Read color from the texture
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			// Output colour will be the texture color * the vertex colour
			half4 output_col = c * _Color;
			//calculate the difference between the texture color and the transparent color
			//note: we use 'dot' instead of length(transparent_diff) as its faster, and
			//although it'll really give the length squared, its good enough for our purposes!
			half3 transparent_diff = -;
			half transparent_diff_squared = dot(transparent_diff,transparent_diff);
			//if colour is too close to the transparent one, discard it.
			//note: you could do cleverer things like fade out the alpha
			if(transparent_diff_squared < _Threshold)
			//output albedo and alpha just like a normal shader
			o.Albedo = output_col.rgb;
			o.Alpha = output_col.a;
	FallBack "Diffuse"