- Home /
Set one color transparent in texture / shader
Hi,
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!
Answer by wibble82 · Sep 05, 2015 at 11:16 AM
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 :)
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
CGPROGRAM
#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 = c.xyz - _TransparentColor.xyz;
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)
discard;
//output albedo and alpha just like a normal shader
o.Albedo = output_col.rgb;
o.Alpha = output_col.a;
}
ENDCG
}
FallBack "Diffuse"
}
Great! Do you know how to make the texture not affected by light/turn it to unlit? I added Lighting Off before CGProgram but it don't work.
I'm using that script on unity 2017.2.0f3 and it works but as output I've only black and alpha image where black is all that is not discarded... how I can have original color * _color ins$$anonymous$$d of black?
@Noxury a little late but change that line o.Albedo = output_col.rgb; to that o.Emission = output_col.rgb;
Having an issue with HDRP where it was rendering the background regardless of culling. Tried this, and it works! The background is transparent, but every other object is black. Any advice?
Your answer
![](https://koobas.hobune.stream/wayback/20220612002622im_/https://answers.unity.com/themes/thub/images/avi.jpg)