- Home /
Second UV set as coordinates for a different texture?
Greetings. I'm writing a custom shader (for the first time ever), based on the built-in Diffuse, which attempts to incorporate a second ("overlay") texture in it. As I'm using this material onto procedurally-generated meshes, I reckoned I'd use the second UV set of the mesh as UV coordinates for the overlay texture. (Mostly because I've got no idea how to pass UVs to a different texture in a material during runtime.) In any case, I seem to be unable to use IN.uv2_MainTex as a parameter to my tex2D for _OverlayTex. Here's my code (not all of it, the rest of the usual stuff is not included in this snippet):
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
sampler2D _OverlayTex;
fixed4 _Color;
struct Input {
float2 uv_MainTex;
float2 uv2_MainTex;
float4 _Color;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
fixed4 c2 = tex2D(_OverlayTex, IN.uv2_MainTex) * _Color;
o.Albedo = lerp(c.rgb, c2.rgb, c2.a);
o.Alpha = c.a;
}
ENDCG
}
I'm having problems with the "fixed4 c2 = " line: "Shader error in 'Diffuse-OverlayTex': Program 'frag_surf', declaration of "_MainTex_ST" conflicts with previous declaration at (51) at line 51", and one for "vert_surf", accordingly. And given that I mostly have got no idea what I'm doing, can someone please tell me what I've got to do to get this to work?
Answer by gnp89 · May 27, 2014 at 08:20 PM
The answer is in http://answers.unity3d.com/questions/712989/using-texcoord1-in-surface-shader.html You should do it like this:
struct Input {
float2 uv_MainTex;
float2 uv2_OverlayTex;
// removed float4 _Color because it isn't used in the surf function
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
fixed4 c2 = tex2D(_OverlayTex, IN.uv2_OverlayTex) * _Color;
o.Albedo = lerp(c.rgb, c2.rgb, c2.a);
o.Alpha = c.a;
}
Answer by Lumos · Apr 28, 2014 at 02:42 PM
Turns out that the second UV set is automatically passed on as the first uv set for the second texture. I am probably not getting something, I must find my own answers.
Answer by NGC6543 · Aug 16, 2019 at 03:20 AM
I know this is an old qustion, but I've encountered the similar case so leave a reply here :
In my case, we needed to use a separate uv channel for normal on a character cloth. The model was exported from the 3ds Max. And it had no problem until our 3d artist done a skinning job.
It turned out that the modifier stack order can cause this issue. You should order the modifier stack so that 'Skin' modifier is on top of any 'UVW Unwrap' modifier.
Hope this helps someone in future.