Two shader questions (have a pic!!) :)))

Hi all! :smile: :slight_smile:

Have got two issues with half self made shader: :

  1. See to Red round line on picture. When source texture (Text RGB) of the second layer have some colored pixels on self border, it ends blurs across all base texture (Base RGB).

If I make one pixel space from border on Text RGB texture, the blurred black trace disappeared. Which error in shader have this symptoms?

  1. Why when I import shader with this code
string Textshader = " SOME ... CGPROGRAM  ENDCG
 (ALL SHADER TEXT SEE BELOW)";
Material material = new Material(Textshader);

this error appears:

Shader error in ‘TextShader’: Parse error: syntax error at line 23
where “CGPROGRAM” places.

WHY?

Shader code:

Shader "TextShader"
{
	Properties
	{
		_BaseColor ("Base Color", Color) = (1,1,1,1)
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_FontColor ("Font Color", Color) = (1,1,1,1)
		_FontTex ("Font (RGB)", Rect) = "black" {}
		_SetFont ("Draw font", float) = 0
		_SizeW ("Size font Width", float) = 1
		_SizeH ("Size font Height", float) = 1
		_Offset_x ("Position Text X", float) = 0
		_Offset_y ("Position Text Y", float) = 0
	}
	SubShader
	{
		Tags { "Queue" = "Geometry+1" }
		
		Pass
		{
			Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off
            Lighting Off
            //Fog { Mode Off }
            
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag	
			#include "UnityCG.cginc"
			
			sampler2D _MainTex;
			sampler2D _FontTex;
			float4 _BaseColor;
			float4 _FontColor;
			float _SizeW;
			float _SizeH;
			float _Offset_x;
			float _Offset_y;
			float _SetFont;
			
			struct appdata 
			{
			    float4 vertex : SV_POSITION;
			    float2 texcoord : TEXCOORD0;
			    float2 texcoord1 : TEXCOORD1;
			};
			  
			struct v2f 
			{
			    float4 pos : SV_POSITION;
			    float2 uv : TEXCOORD0;
			    float2 uv1 : TEXCOORD1;
			};
			
			v2f vert (appdata v) 
			{
			    v2f o;
			    o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
			    o.uv = v.texcoord;
			    o.uv1 = v.texcoord1 - float2(_Offset_x, _Offset_y);
			    return o;
			}
			half4 frag( v2f i ) : COLOR 
			{
			    half4 texcol = tex2D (_MainTex, i.uv);
			    float4 col1 = texcol * _BaseColor;
				float4 result = col1;
				if(_SetFont == 1)
				{
					float2 uvFont = float2(i.uv1.x / _SizeW, i.uv1.y / _SizeH);
				    half4 fontcol = tex2D (_FontTex, uvFont);
				    float4 col2 = fontcol * _FontColor;
					result = float4( col1.r * (1 - col2.a) + col2.r * col2.a,
									 col1.g * (1 - col2.a) + col2.g * col2.a,
									 col1.b * (1 - col2.a) + col2.b * col2.a,
									 col1.a * (1 - col2.a) + col2.a * col2.a);
				}
				return result;
			}
			ENDCG
		}
	}
}

The first effect you are seeing is clamped texture sampling. If you sample UV coordinates outside [0,1], the returned value is defined by the source texture’s wrap mode. Repeat will use the fractional component of the coordinate, effectively tiling your texture. Clamp will clamp the coordinate to [0,1], giving you the result you see above. If you include a border of a certain colour, that border will be smeared out as far as you sample.

The second problem is likely with your shader source. Have you tried compiling it as a standalone shader? You might get better feedback from the compiler there. Chances are it’s something small like a missing brace.

thx!

  1. So, is transparent border only way to remove this effect?

  2. I simple copy all shader to string value by this method

TextShader = "Shader \"TextShader\"\n" +
                            "{\n" +
                            "    Properties\n" +
                            "    {\n" +
                            "        _BaseColor (\"Base Color\", Color) = (1,1,1,1)\n" +
                            "        _MainTex (\"Base (RGB)\", 2D) = \"white\" {}\n" +
                            "        _FontColor (\"Font Color\", Color) = (1,1,1,1)\n" +
                            "        _FontTex (\"Font (RGB)\", Rect) = \"black\" {}\n" +
                            "        _SetFont (\"Draw font\", float) = 0\n" +
                            "        _SizeW (\"Size font Width\", float) = 1\n" +
                            "        _SizeH (\"Size font Height\", float) = 1\n" +
                            "        _Offset_x (\"Position Text X\", float) = 0\n" +
                            "        _Offset_y (\"Position Text Y\", float) = 0\n" +
                            "    }\n" +
                            "    SubShader\n" +
                            "    {\n" +
                            "        Tags { \"Queue\" = \"Geometry+1\" }\n" +

                            "        Pass\n" +
                            "        {\n" +
                            "            Blend SrcAlpha OneMinusSrcAlpha\n" +
                            "            ZWrite Off\n" +
                            "            Lighting Off\n" +
                //Fog { Mode Off }

                            "            CGPROGRAM\n" +
                            "            #pragma vertex vert\n" +
                            "            #pragma fragment frag\n" +
                            "            #include \"UnityCG.cginc\"\n" +

                            "            sampler2D _MainTex;\n" +
                            "            sampler2D _FontTex;\n" +
                            "            float4 _BaseColor;\n" +
                            "            float4 _FontColor;\n" +
                            "            float _SizeW;\n" +
                            "            float _SizeH;\n" +
                            "            float _Offset_x;\n" +
                            "            float _Offset_y;\n" +
                            "            float _SetFont;\n" +

                            "            struct appdata\n" +
                            "            {\n" +
                            "                float4 vertex : SV_POSITION;\n" +
                            "                float2 texcoord : TEXCOORD0;\n" +
                            "                float2 texcoord1 : TEXCOORD1;\n" +
                            "            };\n" +

                            "            struct v2f \n" +
                            "            {\n" +
                            "                float4 pos : SV_POSITION;\n" +
                            "                float2 uv : TEXCOORD0;\n" +
                            "                float2 uv1 : TEXCOORD1;\n" +
                            "            };\n" +

                            "            v2f vert (appdata v)\n" +
                            "            {\n" +
                            "                v2f o;\n" +
                            "                o.pos = mul (UNITY_MATRIX_MVP, v.vertex);\n" +
                            "                o.uv = v.texcoord;\n" +
                            "                o.uv1 = v.texcoord1 - float2(_Offset_x, _Offset_y);\n" +
                            "                return o;\n" +
                            "            }\n" +
                            "            half4 frag( v2f i ) : COLOR \n" +
                            "            {\n" +
                            "                half4 texcol = tex2D (_MainTex, i.uv);\n" +
                            "                float4 col1 = texcol * _BaseColor;\n" +
                            "                float4 result = col1;\n" +
                            "                if(_SetFont == 1)\n" +
                            "                {\n" +
                            "                    float2 uvFont = float2(i.uv1.x / _SizeW, i.uv1.y / _SizeH);\n" +
                            "                    half4 fontcol = tex2D (_FontTex, uvFont);\n" +
                            "                    float4 col2 = fontcol * _FontColor;\n" +
                            "                    result = float4( col1.r * (1 - col2.a) + col2.r * col2.a,\n" +
                            "                               col1.g * (1 - col2.a) + col2.g * col2.a,\n" +
                            "                               col1.b * (1 - col2.a) + col2.b * col2.a,\n" +
                            "                               col1.a * (1 - col2.a) + col2.a * col2.a);\n" +
                            "                }\n" +
                            "                return result;\n" +
                            "            }\n" +
                            "            ENDCG\n" +
                            "       }\n" +
                            "    }\n" +
                            "}\n";

after this a was get message
Shader error in ‘TextShader’: Parse error: syntax error at line 23

Can’t see any mistakes and as you told me, I placed code in to Visual Studio and had no errors. But Unity drops error every time when scene starts :frowning:

There are one opinion for this error:

This error appears in any tested shader when it contains CGPROGRAM section, because compiled program have no way to compile CGPROGRAM section “on the fly”. Other sections have only options which not needed to compile.

Am I right?

P.S. if so, than maybe exists some way to precompile shaders without adding they to project in *.shader files?