Problem using two textures

Hi,

I’m really new to shader and I’m having a problem trying to “blend” two texture in a simple shader.

Shader "Blend2Texture"
{
	Properties
	{
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_SecondTex("Scene (RGB)", 2D) = "white" {}
	}
	
	SubShader
	{
		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
		
			sampler2D _MainTex;
			sampler2D _SecondTex;

			struct InputFragment
			{
				float4 pos : SV_POSITION;
				float2 uv_MainTex : TEXCOORD0;
				float2 uv_SecondTex : TEXCOORD1;
			};
			
			float4 _MainTex_ST;
			float4 _SecondTex_ST;
			
			InputFragment vert(appdata_base v)
			{
				InputFragment output;
				
				output.pos = mul(UNITY_MATRIX_MVP, v.vertex);
				
				output.uv_MainTex     = TRANSFORM_TEX(v.texcoord, _MainTex);
				output.uv_SecondTex = TRANSFORM_TEX(v.texcoord, _SecondTex);
				
				return output;
			}

			float4 frag(InputFragment input) : COLOR
			{
				float4 texMainColor = tex2D(_MainTex, input.uv_MainTex);
				if (texMainColor.w != 0)
					return texMainColor;
				
				float4 texSecondColor = tex2D(_SecondTex, input.uv_SecondTex);
				return texSecondColor;
			}
		
			ENDCG
		}
	} 
	FallBack "Diffuse"
}

So In the Vertex program I fill my two TEXCOORD with the result of TRANSFORM_TEX and in the fragment shader I use tex2D to sample my textures.
The problem is that I get an error while trying to test :
if (texMainColor.w != 0)

When the shader is compiled I get the error :
Shader error in ‘Blend2Texture’: D3D shader assembly failed with: (11): error X5204: Read of uninitialized components(*) in r1: *r/x/0 *g/y/1 *b/z/2 *a/w/3

I don’t really understand why my float4 wouldn’t be initialized so if someone has any idea or something… :slight_smile:

Thanks

	float4 texMainColor = tex2D(_MainTex, input.uv_MainTex);
	float4 texSecondColor = tex2D(_SecondTex, input.uv_SecondTex);
	float4 result = lerp(texMainColor.rgb, texScondColor.rgb, texMainColor.a);
	return result;

Try this lerping method instead, it’ll blend between the first and second texture depending on the alpha channel of the first texture.

Hum actually when I said blend it wasn’t really a blending :slight_smile:
I would like to return the color of the first texture if it’s not black or return the color of the second texture so the lerp isn’t the right way to do it isn’t it ?