Issue with Transparent - Diffuse shader framebuffer alpha channel

Hello,

I’m working on an application that uses unity as a rendering engine and sends the result to a windows forms program. My problem is that the framebuffer from which I ReadPixels() doesn’t seem to have an alpha channel. I’ve read that this is because of:

#pragma surface surf Lambert alpha

which sets ColorMask RGB. I have written a custom shader that looks like this:

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

	SubShader 
	{
		Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
		LOD 200
		
		Alphatest Greater 0
 		ZWrite Off
 		//ColorMask RGBA
 		Blend SrcAlpha OneMinusSrcAlpha

		CGPROGRAM
		#pragma surface surf Lambert
		
		sampler2D _MainTex;

		struct Input 
		{
			float2 uv_MainTex;
		};

		void surf (Input IN, inout SurfaceOutput o) 
		{
			fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	}
}

This yields some weird artifacts surrounding the objects rendered with transparency. My question is: how does this shader differ from the one that ships with unity (Transparent - Diffuse)? Or how could I write a shader that does what Transparent - Diffuse does, but also get the alpha channel correctly?

EDIT: This happens only when using point lights with Render Mode set to important, when the light is close to the object. Others have also had this issue: http://answers.unity3d.com/questions/152183/override-colormask-in-a-surface-shader.html

I found a solution/workaround if anyone is interested. First I use a script that exports the compiled version of my surface shaders (I used the transparent ones that ship with Unity). Then I modify this line in the compiled shader: “ColorMask RGB” to “ColorMask RGBA”. Below I post the code for the editor script, the source shader, and the first few lines of the compiled shader.

using UnityEngine;
using UnityEditor;

using System.IO;
using System.Linq;

public static class MenuItemExportShaders
{
    [MenuItem("Window/Export Compiled Shaders")]
    public static void ExportCompiledShaders()
    {
        string folderPath = Application.dataPath + "/../Assets/ExportedShaders/";
        if (!Directory.Exists(folderPath))
            Directory.CreateDirectory(folderPath);

        var allShaders = AssetDatabase.GetAllAssetPaths().Where(x => x.Contains(".shader")  !x.Contains("Compiled"));

        foreach (var shaderPath in allShaders)
        {
            var shader = (Shader)AssetDatabase.LoadAssetAtPath(shaderPath, typeof(Shader));
            var compiledShaderString = (new SerializedObject(shader)).FindProperty("m_Script").stringValue;

            if (compiledShaderString == null)
                continue;

            var outputPath = folderPath + Path.GetFileNameWithoutExtension(shaderPath) + " Compiled.shader";
            using (var sw = File.CreateText(outputPath))
            {
                sw.Write(compiledShaderString);
                sw.Flush();
                sw.Close();
            }
        }

        EditorUtility.DisplayDialog("Success...", "Exported all shader assets...", "OK");
    }
}

This was taken from here: Resume - Start Bootstrap Theme

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

	SubShader 
	{
		Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
		LOD 200
		
		//Alphatest Greater 0
 		//ZWrite Off
 		//ColorMask RGBA 
 		//Blend SrcAlpha OneMinusSrcAlpha

		CGPROGRAM
		#pragma surface surf Lambert alpha

		sampler2D _MainTex;

		struct Input 
		{
			float2 uv_MainTex;
		};

		void surf (Input IN, inout SurfaceOutput o) 
		{
			fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	}
}

Taken from the built-in shader source from here: http://unity3d.com/unity/download/archive

Shader "Custom/Transparent/Diffuse RGBA" 
{
	Properties 
	{
	_MainTex("Base (RGB) Trans (A)", 2D)="white"{}
	}

	SubShader 
	{
		Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
		LOD 200
		
		//Alphatest Greater 0
 		//ZWrite Off
 		//ColorMask RGBA 
 		//Blend SrcAlpha OneMinusSrcAlpha

			Alphatest Greater 0 ZWrite Off ColorMask RGBA
	
	Pass {
		Name "FORWARD"
		Tags { "LightMode" = "ForwardBase" }
		Blend SrcAlpha OneMinusSrcAlpha

Which is the beginning of the modified compiled shader.