Custom sprite Shader works in 5.5, not 5.6

Let me start by saying that I’m not good with shaders, but I successfully wrote a simple custom shader while using Unity 5.5. Its job is to take a sprite, colour it fully white, and allow me to change the colour in code to red or green (I use it for radar signatures of space ships).

Shader code here

It looks like this.

Simple enough. But since upgrading to Unity 5.6, it comes out like this. It loses respect for the dimensions of the sprite (seems worse on convex shaped sprites) and also seems to colour it grey/transparent, not opaque white.

The shader hasn’t changed at all, I checked. So it’s not like Unity rewrote something in my shader, but maybe elsewhere in the new graphics settings?

Thanks very much.

I think I’ve solved it…

I found that removing this line

#pragma multi_compile _ ETC1_EXTERNAL_ALPHA

brings what you see in the second image back in line with the 1st. I’m not entirely sure what this is, and don’t know what doing this will break (if anything). But since Android is referenced later in the shader with the External_Alpha and I’m not building for mobile, I’m happy enough to make the change, personally.

What I must reiterate however is that in 5.5 I had this line in the project and got my desired result, so something has changed under the hood in 5.6.0 and it may be something that Unity patches later and re-breaks on me while they solve it for others.

Hope this helps you though.

In 5.6, Unity seemed to have refactored some of the Sprite shader functionality to UnitySprites.cginc (download the shaders here to look: Download Archive)

I’ve gotten better results without disabling that line (just in case I need to release on Android), but by overriding the default vert or pixel shaders. Those are all in UnitySprites.cginc for reference.

Using the hint that Alice_Funo gave (thanks Alice), I did the following to resolve the error without disabling that line (I am working on an android project as it is):

Under Pass, under CGPROGRAM, under the pragma and include statements, I added the CBUFFER bit, so it looks like the following:

			#pragma vertex vert
			#pragma fragment frag
			#pragma target 2.0
			#pragma multi_compile _ PIXELSNAP_ON
			#pragma multi_compile _ ETC1_EXTERNAL_ALPHA
			#pragma multi_compile_fog
			#include "UnityCG.cginc"

			fixed4 _RendererColor;
			float4 _Flip;
			float _EnableExternalAlpha;

My “fixed4 SampleSpriteTexture” function was replaced with the following:

fixed4 SampleSpriteTexture (float2 uv)
				fixed4 color = tex2D (_MainTex, uv);

				fixed4 alpha = tex2D (_AlphaTex, uv);
				color.a = lerp (color.a, alpha.r, _EnableExternalAlpha);

				return color;

That returned things to normal for me. Still need to test it on a mobile phone however.