Can't get alpha to work in my shader

I’m am experimenting with shaders and modified one I found in a tutorial. I wanted to get an effect that made an object have an opaque color in the center and gradually fade out to the sides. The lighting seems to work now, but I haven’t got it to fade out yet. Instead it goes from colored to black. I wanted to set the alpha to the color (the darker the color gets the lower the alpha is) but I can’t get it to work. If I set the alpha in the LightingRamp function nothing happens, and if I set it in the surf function it has an equal alpha for the entire object. The _Ramp2D texture I use is a gradient that goes from red to the right to black on the left (attached to this post).

Shader "Test/NewShader" 
{
	Properties 
	{
		_Ramp2D ("BRDF Ramp", 2D) = "gray" {}
	}
	
	SubShader 
	{
		Tags
		{   
	            "RenderType" = "Tranparent"
        	    "Queue" = "Transparent"
	        }
        
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Ramp alpha
		#pragma target 3.0

		sampler2D _Ramp2D;

		struct Input 
		{
			float2 uv_MainTex;
		};

		half4 LightingRamp (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
		{
			float NdotL = dot(s.Normal, lightDir);
			float NdotE = dot(s.Normal, viewDir);
			
			float diff = (NdotL * 0.5) + 0.5;
			if (NdotE > 1.0) NdotE = 1.0;
			if (NdotE < 0.1) NdotE = 0.1;
			float2 brdfUV = float2(NdotE, diff);
			float3 BRDF = tex2D(_Ramp2D, brdfUV.xy).rgb;
			
			float4 c;
			c.rgb = BRDF;
			c.a = 0.5;
			return c;
		}

		void surf (Input IN, inout SurfaceOutput o) 
		{
			half4 c = float4(0,0,0,1);
			o.Albedo = c.rgb;
			o.Alpha = 1;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

How do I influence alpha in the surf function, or how do I get the alpha value to work in the LightingRamp function?

1071630–40009–$BlackRedGradient.7z (483 Bytes)

Well, you assign a value to o.Alpha in the surf function, but I think you want to ask how to set it dependently on the light sources. That might be impossible with a surface function unless you access specific light source information in the surf function.

As far as I remember, the alpha channel is used in the standard lighting functions to compute the brightest intensities for the gloom and bloom effect. (You might want to look at the code in Lighting.cginc in the folder CGIncludes to check this.) If that is so, the alpha in the lighting functions only affects the alpha channel of the framebuffer. I assume that only the o.Alpha that is set in the surf function is used for blending the fragment color with the framebuffer color.

Thanks for the answers!

How would I go about accessing the light source information in the surf function? Is that something that is done in other shaders too, or am I trying to reinvent the wheel here?

I’ll go check out the the Lighting.cginc code to see if it can tell me something. If I understand your explanation correctly though, the alpha in lighting is not about transparancy, but about making a blend of the fragment color and framebuffer color? And will not produce the result that I need?

I’m actually not sure how you access that information in a surface shader. You can access it in Cg or GLSL vertex and fragments shaders but it is a bit more tricky in surface shaders as far as I remember. Also, I’m not sure why you would change the opacity depending on the light source. Usually, a similar effect would only depend on the surface normal and the view direction: http://en.wikibooks.org/wiki/Cg_Programming/Unity/Silhouette_Enhancement

No, transparency is all about blending fragment color and framebuffer color. That is how transparency is implemented on the shader level.
It is Unity’s standard lighting which “abuses” the alpha channel for other effects. I guess all this doesn’t make sense until you learn more about how transparency is implemented by the GPU.