Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by homer_3 · Jul 01, 2012 at 05:57 PM · shadercoloralphacgtransparancy

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?

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Jul 01, 2012 at 06:05 PM 1
Share

It might be easier to convert them to a format that has transparency and remove the background color using some graphics software.

avatar image whydoidoit · Jul 01, 2012 at 06:06 PM 0
Share

Or a custom shader could do it I guess - couldn't find one with a quick Google though.

avatar image homer_3 · Jul 01, 2012 at 07:13 PM 0
Share

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.

avatar image whydoidoit · Jul 01, 2012 at 07:32 PM 0
Share

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.

avatar image insominx · Jul 01, 2012 at 08:21 PM 1
Share

You could do something similar to the chroma key shader discussed here: http://forum.unity3d.com/threads/48938-chroma-key-shader

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Jul 01, 2012 at 09:37 PM 0
Share

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"
 }
avatar image Inferis · Nov 29, 2012 at 11:10 PM 0
Share

Ah nice, this works on mobile too. ^^

avatar image
0

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?)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting the world position of the pixel in the fragment shader. 1 Answer

custom Standard Shader alpha issues. 1 Answer

Colors tint at shader 0 Answers

Shader with zwrite, shadows and alpha (special alpha) 2 Answers

Get color of pixel before pass: prefer cg, shaderlab is last resort 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges