Pure black -> Alpha

Hello,

I have been struggling with this issue all day! I have a bunch of textures in which pure black (0, 0, 0) is actually supposed to be rendered as transparent. I have tried converting all pure black to alpha using the methods described here: Unity - Manual: Importing Textures
But I end up getting the problems described in this thread: http://forum.unity3d.com/threads/156453-Texture-Problem

I found a shader that perfectly produces exactly what I am looking for here: http://wiki.unity3d.com/index.php?title=TextureMask
The only problem with this shader though is: it requires 2 textures.

So now I turning to the ShaderLab Unity forums. Is there a way to put 1 texture that has NO ALPHA into a shader.
-The shader will look for all pure black - rgb: (0, 0, 0) pixels
-The shader will then render these pixels as alpha (or use a cutoff to get rid of them)

Or, if you do have a solution to my thread here: http://forum.unity3d.com/threads/156453-Texture-Problem I would like that as well.

Thank you!

Shader "Custom/NewShader" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
        _Threshold ("Cutout threshold", Range(0,1)) = 0.1
        _Softness ("Cutout softness", Range(0,0.5)) = 0.0
	}
	SubShader {
		Tags { "RenderType"="Transparent" "Queue"="Transparent" }
		LOD 200

		CGPROGRAM
		#pragma surface surf Lambert alpha

		sampler2D _MainTex;
        float _Threshold;
        float _Softness;

		struct Input {
			float2 uv_MainTex;
		};

		void surf (Input IN, inout SurfaceOutput o) {
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			o.Albedo = c.rgb;
            o.Alpha = smoothstep(_Threshold, _Threshold + _Softness, 
               0.333 * (c.r + c.g + c.b));
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

Your problem is called chroma keying. Start using the shader with softness = 0 and adjusting the threshold such that everything is transparent that you want to have transparent. If necessary increase softness to soften the edges.

Hint: Apparently you don’t know much about shader programming. Which is OK, but it helps in this forum to at least pretend to be interested in shader programming. :wink:

1 Like