blending textures

hi,

i found this shader on the forum from a old post.
http://forum.unity3d.com/threads/35049-how-many-UV-channel-unity-support

it’s almost what i need. i did try a lot of things to let the second texture work also for transparency.
but i can’t get it to work properly.
can someone help me?

this is what i need:
a shader that works with 2 uv sets.
with texture 1 to work on uv-set 1… just a plain texture.
the second texture got a Alpha, but this one is from uv-set 2, and it has to work as a transparency.

can someone help me?
many thanks!

greetz,

This should do the trick.

Shader "ColorAlpha" {
Properties {
	_Color ("Main Color", Color) = (1,1,1,1)
	_MainTex ("Base ", 2D) = "white" {}
	_Alpha ("Alpha", 2D) = "white" {}
	
}
SubShader {
	Tags { "RenderType"="Transparent" "Queue" = "Transparent"}
	
CGPROGRAM
#pragma surface surf BlinnPhong alpha

sampler2D _MainTex;
sampler2D _Alpha;
float4 _Color;

struct Input {
	float2 uv_MainTex;
	float2 uv_Alpha;
};

void surf (Input IN, inout SurfaceOutput o) {
	o.Albedo = tex2D(_MainTex, IN.uv_MainTex);
	float3 alpha = tex2D(_Alpha, IN.uv_Alpha).rgb;
	o.Alpha = (alpha.r + alpha.g + alpha.b)/3;
}
ENDCG
} 
FallBack "Diffuse"
}

I am basically averaging the three color channels of an RGB image to determine the alpha value. If you would rather use the alpha channel of the image, try this:

Shader "ColorAlpha" {
Properties {
	_Color ("Main Color", Color) = (1,1,1,1)
	_MainTex ("Base ", 2D) = "white" {}
	_Alpha ("Alpha", 2D) = "white" {}
	
}
SubShader {
	Tags { "RenderType"="Transparent" "Queue" = "Transparent"}
	
CGPROGRAM
#pragma surface surf BlinnPhong alpha

sampler2D _MainTex;
sampler2D _Alpha;
float4 _Color;

struct Input {
	float2 uv_MainTex;
	float2 uv_Alpha;
};

void surf (Input IN, inout SurfaceOutput o) {
	o.Albedo = tex2D(_MainTex, IN.uv_MainTex);
	o.Alpha = tex2D(_Alpha, IN.uv_Alpha).a;
}
ENDCG
} 
FallBack "Diffuse"
}

Try experimenting with the code as well! Once you have a handle on the surface shader syntax, it’s pretty easy to bend it to your will - within reason, of course :wink:

yup! that did the trick.

well i’m realy gona try coding with all kinds of shaders…
this is the first time i did try something like this.

1 question tough… how did you manage to let the alpha get it from the second uv-set?

edit: another question… why can’t transparent object receive shadow?
if it can… how?

this is what i have now: (i have built in a bump and used the color for the diffuse texture)

Shader "ColorAlphaBump" 
{
	Properties 
	{
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex ("Base ", 2D) = "white" {}
		_BumpMap ("Bumpmap", 2D) = "bump" {}
		_Alpha ("Alpha", 2D) = "white" {}
	}
	
	SubShader 
	{
		Tags { "RenderType"="Transparent" "Queue" = "Transparent"}
		
		CGPROGRAM
		#pragma surface surf Lambert alpha

		sampler2D _MainTex;
		sampler2D _BumpMap;
		sampler2D _Alpha;
		float4 _Color;

		struct Input 
		{
			float2 uv_MainTex;
			float2 uv_BumpMap;
			float2 uv_Alpha;
		};

		void surf (Input IN, inout SurfaceOutput o) 
		{
			o.Albedo = tex2D(_MainTex, IN.uv_MainTex);

			o.Albedo *= _Color; // Color affects MainTex

			float3 alpha = tex2D(_Alpha, IN.uv_Alpha).rgb;
			o.Alpha = (alpha.r + alpha.g + alpha.b)/3;
			o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
		}
		
		ENDCG
	} 
	
	FallBack "Diffuse"
}

THNX!

I’m not sure on your shadow question.

As for the uv set question, any variable in the Input struct that begins with 'uv" will have uv texture coordinates be bound to it’s corresponding image, if there is one. So, if you have a normal map named “_NormalMap”, and in the input structure you put a “uv_NormalMap”, then the uv texture coordinate you set for the “_NormalMap” image in the inspector window will correspond to the “uv_NormalMap” uv coordinates in the Input struct. This will only work to a certain point, however, then you run out of interpolators.

*** Disclaimer *** - I am not 100% sure on this, but that’s how it has worded according to my experience. Read here for more info