Shader is black after upgrading to 5.0 :(

Hi all,

I’m in real trouble here. I got this very simple shader (I’m not a shader programmer). Taking a texture and a threshold and out-putting pure red if the pixel is higher than the given threshold (and I ignore 10% of the border).

Everything worked in 4.6.x and below, but updating to 5.0, everything is black. :frowning:

I use it like this:

ImgProcMaterial.SetFloat("_Threshold", threshold);

RenderTexture.active = ImgProcRenderTex;
ImgProcRenderTex.MarkRestoreExpected(); // To remove warnings.
Graphics.Blit(CameraTexture, ImgProcRenderTex, ImgProcMaterial);

ImgProcTex.ReadPixels(new Rect(0, 0, ImgProcTex.width, ImgProcTex.height), 0, 0, false);

RawColorData = ImgProcTex.GetPixels32();

And the Shader:

Shader "Custom/ImagePreProcessing"
{	
	Properties
	{
		_MainTex ("Main Texture", 2D) = "white" {}
		_Threshold ("threshold", float) = 1.0
	}
 
	SubShader
	{
		Tags { "RenderType"="Solid" "Queue"="Transparent" "AllowProjectors"="False" }
 
		CGPROGRAM
		#pragma surface surf NoLighting
 
		fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
		{
			return fixed4(s.Albedo, s.Alpha);
		}
 
		sampler2D _MainTex;
		uniform float _Threshold;

		struct Input
		{
			float2 uv_MainTex;
		};
 
		void surf (Input IN, inout SurfaceOutput o)
		{
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
 
			float lumen = c.r +  c.g +  c.b;

			if(IN.uv_MainTex.x < 0.10 || IN.uv_MainTex.x > 0.9 || IN.uv_MainTex.y < 0.10 || IN.uv_MainTex.y > 0.9)
			{
				o.Albedo = half3 (0.0,0.0,0.0);
				o.Alpha = 1;
				return;
			}

			if((lumen / 3) > _Threshold * 1.40)
			{
				o.Albedo = half3 (1.0,0.0,0.0);
				o.Alpha = 1;
			}
			else
			{
				o.Albedo = half3 (0.0,0.0,0.0);
				o.Alpha = 1;
			}
		}
			ENDCG
	} 
	FallBack "Diffuse"
}

I have tried setting the Albedo to non black colors for all possible outputs, but still everything is black.

Hope someone knows what has changed.

Thanks.

  • Henning

After many, many tries working blind as the only output I got was black. I finally decided to try and converted the surface shader to a fragment shader, and suddenly everything worked? :slight_smile:

So still don’t know why the old surface shader isn’t working, but as long as I found an alternative I’m happy :slight_smile:

Shader "Custom/ImagePreProcessing"
{	
	Properties
	{
		_MainTex ("", 2D) = "white" {}
		_Threshold ("threshold", float) = 1.0
	}

	SubShader
	{
		ZTest Always Cull Off ZWrite Off Fog { Mode Off } //Rendering settings

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc" 
			//we include "UnityCG.cginc" to use the appdata_img struct

			struct v2f
			{
				float4 pos : POSITION;
				half2 uv : TEXCOORD0;
			};

			//Our Vertex Shader 
			v2f vert (appdata_img v)
			{
				v2f o;
				o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
				o.uv = MultiplyUV (UNITY_MATRIX_TEXTURE0, v.texcoord.xy);
				return o; 
			}

			sampler2D _MainTex; //Reference in Pass is necessary to let us use this variable in shaders
			uniform float _Threshold;

			//Our Fragment Shader
			fixed4 frag (v2f i) : COLOR
			{
				fixed4 orgCol = tex2D(_MainTex, i.uv); //Get the orginal rendered color 

				float lumen = orgCol.r +  orgCol.g +  orgCol.b;

				if(i.uv.x < 0.10 || i.uv.x > 0.9 || i.uv.y < 0.10 || i.uv.y > 0.9)
				{
					return fixed4(0.0,0.0,0.0,1.0);
				}

				if((lumen / 3) > _Threshold * 1.40)
				{
					return fixed4(1.0,0.0,0.0,1.0);
				}

				return fixed4(0.0,0.0,0.0,1.0);
			}
			ENDCG
		}
	} 
	FallBack "Diffuse"
}