How to use UV3 to send coordinates to the Custom Sheder (blending textures and having shadows)

Hi! I have a procedurally generated objects with two textures: first for the base color, second for the shading.

Both textures have separate UV coordinates for each triangle, and use UV and UV2 for sending them to this custom shader:

Shader "Blended Textures" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _BlendTex ("Alpha Blended (RGBA) ", 2D) = "white" {}
    }
    SubShader {
        Pass {
            BindChannels {
               Bind "Vertex", vertex
               Bind "texcoord", texcoord0
               Bind "texcoord1", texcoord1
            }
            SetTexture [_MainTex] {
                combine texture
            }
            SetTexture [_BlendTex] {
                combine texture lerp (texture) previous
            }
        }
    }
}

This works, but objects do not cast or receive shadows, and i tried to fix it by re-writing my shader and combining it with Unity’s standard shader.

Because Unity uses UV2 for lightprobes, i have to move second texture coordinates onto UV3, but how do i do that?

No matter how i change my shader, second texture keeps using the same coordinates as primary texture

130213-unity-help-04.png

I even send second UV coordintates to diferent UV’s:

mesh.uv = uvone; // works
mesh.uv2 = uvtwo; // do not work
mesh.uv3 = uvtwo; // do not work
mesh.uv4 = uvtwo; // do not work

mesh.SetUVs(2, UVs2); // do not work
mesh.SetUVs(3, UVs2); // do not work

Now i have this mess of code, that only partially works, how do i fix it?

Shader "Custom/TestBlending 2"
     {
         Properties
         {
             _MainTex("Base (RGB)", 2D) = "white" {}
             _Texture2("Alpha Blended (RGBA) ", 2D) = "white" {}
    		 _Color ("Color", Color) = (1,1,1,1)
         }
         
         SubShader
         {
             Tags
             {
                 "RenderType"="Opaque" 
             }
             
                 
             BindChannels
             {
                 Bind "Color", color
                 Bind "Vertex", vertex
                 Bind "TexCoord", texcoord
    			 Bind "Texcoord2", texcoord2
    			 Bind "Texcoord3", texcoord3
             }
             
             Pass
             {
                 SetTexture[_MainTex]
    			 
                 SetTexture[_Texture2]
    			 {
    				combine texture lerp (texture) previous
    			 }	 
             }
    		
    		CGPROGRAM
    		// Physically based Standard lighting model, and enable shadows on all light types
    		#pragma surface surf Standard fullforwardshadows
    
    		// Use shader model 3.0 target, to get nicer looking lighting
    		#pragma target 3.0
    
    		sampler2D _MainTex;
    		sampler2D _Texture2;
    		sampler2D _Blended;
    		
    		struct appdata
            {
                float4 vertex    : POSITION;  // The vertex position in model space.
                float3 normal    : NORMAL;    // The vertex normal in model space.
                float4 texcoord  : TEXCOORD0; // The first UV coordinate.
                float4 texcoord1 : TEXCOORD1; // The second UV coordinate.
                float4 texcoord2 : TEXCOORD2; // The third UV coordinate.
                float4 texcoord3 : TEXCOORD3; // The fourfth UV coordinate.
            };
    		
    		struct Input {
    			float2 uv_MainTex: TEXCOORD0;
    			float2 uv_Texture2 : TEXCOORD2;
    			float2 uv_Blended;
    		};
    		
    		void vert(inout appdata v, out Input o) {
                UNITY_INITIALIZE_OUTPUT(Input, o);
               
                o.uv_MainTex = v.texcoord.xy;
     
                o.uv_Texture2 = v.texcoord3.xy;
                
            }
    
    		fixed4 _Color;
    
    		UNITY_INSTANCING_BUFFER_START(Props)
    			
    		UNITY_INSTANCING_BUFFER_END(Props)
    
    		void surf (Input IN, inout SurfaceOutputStandard o) {
    		
    			fixed4 t1  = tex2D( _MainTex, IN.uv_MainTex );
    			fixed4 t2  = tex2D( _Texture2, IN.uv_Texture2 ) * 1;
    			o.Albedo  = lerp( t1, t2, 0.5 ) ;
    			
    		}
    		ENDCG
         }
    	 FallBack "Diffuse"
     }

1 Answer

1

Fixed the issue!

Turns out, to use uv’s you need to name them correctly, like this:

UV%Number%_%TextureName%

uv3_TestTexture

uv4_AnotherTexture

130224-unity-help-05.png


Full shader:

Shader "Custom/BlendedTexturesWithShadows"
{
	Properties
	{
		_MainTex("Base (RGB)", 2D) = "white" {}
		_Texture2("AO (RGB)", 2D) = "white" {}
	}
     
	SubShader
	{
	 
		Tags
		{
			"RenderType"="Opaque"           

		}

		CGPROGRAM
		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Standard fullforwardshadows

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0
		
		#include "AutoLight.cginc"
		
		sampler2D _MainTex;
		sampler2D _Texture2;
				
		
		
		struct Input 
		{
			float2 uv_MainTex: TEXCOORD0;
			float2 uv4_Texture2: TEXCOORD3;
		};
		
		
		#pragma instancing_options assumeuniformscaling
		UNITY_INSTANCING_BUFFER_START(Props)
			
		UNITY_INSTANCING_BUFFER_END(Props)

		void surf (Input IN, inout SurfaceOutputStandard o) {
		
			fixed4 t1  = tex2D( _MainTex, IN.uv_MainTex.xy );
			fixed4 t2  = tex2D( _Texture2, IN.uv4_Texture2.xy ) * 1;
			fixed4 c = lerp( t1, t2, 0.5 ) ;
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
     }
	 FallBack "Diffuse"
 }