Understanding Shaders Alpha

Hi there,
I am new to shaders, and need to better understand them.

I currently have a predicament which can be seen here…http://forum.unity3d.com/threads/160064-Need-50-Aplha-Shader-Questions

What I would like to know is, can I add an alpha channel to any shader? Or is what inside the shader limit that ability?

Thanks.

If you are willing to do some shader programming, you can add an alpha channel to practically any shader. (For the built-in shaders, you would have to download the zip file with Unity’s built-in shaders, edit them and then use the edited shader in your project.)

Otherwise you are limited by whatever the shader allows you to do.

Right,
But what would adding a Alpha channel look like in terms of code?

Why would an Alpha channel be left out? Simply efficiency? The said shader is for mobile.

It is a good idea to distinguish between alpha channels (which are just the fourth channel of a texture, or the frame buffer), and transparency. One type of transparency is achieved using what is called alpha blending, where the colour contribution of a shader is linearly interpolated with that of the destination, usually (but not necessarily) by a value from some alpha channel.

Transparent objects are rendered quite differently from opaque ones, and this process comes with a number of issues. Reduced efficiency and difficulty of sorting are the top issues with transparency in 3D games. If you don’t need transparency, you should use an opaque shader to avoid these problems.

That said, modifying an existing shader to render using alpha blending is usually a fairly simply process. It is documented withe examples in the manual (and here for surface shaders).

Here is the code of the built-in transparent diffuse shader with 3 comments marking the important characteristics of a semitransparent shader:

Shader "Transparent/Diffuse" {
	Properties {
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
	}

	SubShader {
		Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
			// most important for semitransparent surfaces: "Queue"="Transparent"
		LOD 200
	
		CGPROGRAM
		#pragma surface surf Lambert alpha
			// most important for semitransparent surfaces: alpha
	
		sampler2D _MainTex;
		fixed4 _Color;
	
		struct Input {
			float2 uv_MainTex;
		};
	
		void surf (Input IN, inout SurfaceOutput o) {
			fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			o.Alpha = c.a;
				// most important for semintransparent surfaces: setting of SurfaceOutput.Alpha
		}
		ENDCG
	}

	Fallback "Transparent/VertexLit"
}

Yes, mainly it’s just efficiency, i.e. opaque surfaces can be rendered much(!) faster than semitransparent surfaces.

Don’t forget all the sorting issues that you avoid with opaque shaders. This forum would have ten times as many sorting questions if Unity only shipped with alpha blended shaders.

I agree but from the point of view of Unity programmers that, too, often boils down to an efficiency issue: semitransparent surfaces often require sorting of objects, which requires some performance.

If only object sorting could fix transparency order on non-convex meshes!

Thank you guys. I appreciate this very much.

I am a one man show in terms of programming my current title and do not see myself being able to study and learn shaders until I complete the project in the near future, that being said, I need a shader which will support semi transperancy now. When I do use shaders that do support this, they turn my object black. See pics here http://forum.unity3d.com/threads/160064-Need-50-Aplha-Shader-Questions

So what I am wondering. For a 2D sprite, what is a shader I can use that will not effect the color? Also, why is it effecting the color?

EDIT.
After some advive I have tried the Unlit Shaders, but no luck. Below is a pic of the Transparent Diffused Shader working on a sprite, with identical settings as the yellow sprite which will not work. I am perplexed as to why it works on one, but not the other.

works on this texture, material.

turns this texture, black (normal color is yellow)

Can you post the texture that’s giving you trouble, and perhaps a picture of your material settings and texture import settings?

Sure and thank you.

Here is the sprite as it should appear, but at full opacity. Its Shader is 2D Art for the iPhone.

Here are its import settings.

This is the same object, but with a Transparent Diffuse Shader

Ah, I misread. Why are you still using transparent/diffuse if you aren’t using real-time lighting? As I said above, most shaders require lights in order to not appear black or dark.

Yes, I tired, if you read my other post, all the unlits. No positive results. They either, turn the object black as above, completely transparent, or some other odd thing. Nothing is working like how the blue object, in above pic is. Which I see as odd because the blue objects shares all the same properties as the yellow.

I’m not sure what’s going wrong. One thing I noticed is that you appear to tend to put the objects very close together; this is not a good idea with transparent shaders because of the sorting issues Daniel mentioned. Make sure that there is lots of space between objects with transparent shaders.

Could you try the following shader which is supposed to be a transparent self-illuminated shader?

      Shader "Transparent Self-Illuminated" {

          Properties {

              _Color ("Main Color", Color) = (1,1,1,1)

              _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}

          }

       

          SubShader {

              Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}

                  // most important for semitransparent surfaces: "Queue"="Transparent"

         

              CGPROGRAM

              #pragma surface surf Lambert alpha

                  // most important for semitransparent surfaces: alpha

         

              sampler2D _MainTex;

              fixed4 _Color;

         

              struct Input {

                  float2 uv_MainTex;

              };

         

              void surf (Input IN, inout SurfaceOutput o) {

                  fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;

                  o.Emission = c.rgb;

                      // for self-illumination

                  o.Alpha = c.a;

                      // most important for semintransparent surfaces: setting of SurfaceOutput.Alpha

              }

              ENDCG

          }

       

          Fallback "Transparent/Diffuse"

     }

Alpha is controlled by the product of the alpha of the specified color and the alpha of the texture. I.e. if one of them is 0, the surface will be completely transparent.

EDIT: Oops, forgot a "

I have read all your posts, but unless you are crystal clear about what is working or not right now, it’s not reasonable for me to assume that everything is the same as it was a few posts ago.

Can you please post a picture of your material settings (not the texture import settings, which you’ve already posted) for one of the situations in which you get black instead of yellow?

I found a solution, just use a psd file set to 50% opacity. I was using a png’s.

Cheers.

There’s nothing inherently wrong with using PNGs. They’re often better than PSDs because Photoshop does a better job of filling the RGB values of transparent pixels in PNGs than it does in PSDs. It’s good that you’ve found a solution, but would you be able to post the original source texture so someone can figure out why it was giving you trouble?

Hmm, if that’s your solution then I have to wonder whether you know how to change the alpha value of a color in Unity’s color selector?