- Home /
Is there a way to set an alpha color?
I have a bunch of bmp files with a specific solid background color that isn't intended to be displayed. Is there a way to set that color to be considered as an alpha of 0?
It might be easier to convert them to a format that has transparency and remove the background color using some graphics software.
Or a custom shader could do it I guess - couldn't find one with a quick Google though.
Yea, I've been looking into how to do this with a custom shader. I just can't seem to find a way to compare 1 color against another and do something in response to that in a shader.
I don't know much (read hardly anything) about shaders but it should be the Vector4 co$$anonymous$$g out of the texture compare it to a Vector4 that represents your alpha.
You could do something similar to the chroma key shader discussed here: http://forum.unity3d.com/threads/48938-chroma-key-shader
Answer by homer_3 · Jul 01, 2012 at 09:28 PM
Finally got it. I added this before the Alphatest in one of the built in transparancy shaders (Of course, I also had to add the _AlphaColor and _AlphaColorCutoff params to the top of the shader)
CGPROGRAM // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs. #pragma exclude_renderers gles #pragma fragment frag #include "UnityCG.cginc"
uniform sampler2D _MainTex; uniform fixed4 _AlphaColor; uniform fixed _AlphaColorCutoff;
struct v2f { float2 uv : TEXCOORD0; };
float4 frag(v2f i) : COLOR {
fixed4 texcol = tex2D( _MainTex, i.uv );
if((abs(texcol.r - _AlphaColor.r) < _AlphaColorCutoff) &&
(abs(texcol.g - _AlphaColor.g) < _AlphaColorCutoff) &&
(abs(texcol.b - _AlphaColor.b) < _AlphaColorCutoff))
{
texcol.a = 0;
}
return texcol;
}
ENDCG
Ah cool - I just wrote my first shader to see if I could :) Also worked - slightly different but there you go!
Shader "Custom/$$anonymous$$ike's Shader" {
Properties {
_$$anonymous$$ainTex ("Base (RGB)", 2D) = "white" {}
_Color ("AlphaColor", Color) = (0,0,0,1)
}
SubShader {
Tags { "RenderType"="transparent" }
LOD 200
CGPROGRA$$anonymous$$
#pragma surface surf Lambert alpha
sampler2D _$$anonymous$$ainTex;
struct Input {
float2 uv_$$anonymous$$ainTex;
};
float4 _Color;
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex);
o.Albedo = c.rgb;
if(c.r == _Color.r && c.g == _Color.g && c.b == _Color.b)
o.Alpha = 0;
else
o.Alpha = 1;
}
ENDCG
}
FallBack "Diffuse"
}
Answer by virt · Aug 23, 2012 at 11:14 AM
How to make it work on IOS?I suppose that the RGBA texture on iOS is not set up automatically as in android.(how to resolve that?)