Multitexture Terrain shader for mobile

hi everyone, i just found out the shader i created for a custom terrain does not work on iOS because it using more than 8… something :frowning: and even if i use less texture i get a huge frame drop just by using this shader instead of a normal one (from 30fps to 15fps), i have almost no experience in creating shaders so i just copy pasted some parts of other shaders till i got what i needed

what i need is a shader i can apply on a single Quad that has 4 different textures (128x128) and somehow mask them using a single image or 3 of them (the mask is 32x32)

this is what i had so far before discovering the iOS problem

Shader "Custom/teste" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_Texture2 ("mask 1", 2D) = "white" {}
		_Texture3 ("over 1", 2D) = "white" {}
		_Texture4 ("mask 2", 2D) = "black" {}
		_Texture5 ("over 2", 2D) = "white" {}
		_Texture6 ("mask 3", 2D) = "black" {}
		_Texture7 ("over 3", 2D) = "white" {}
		_Texture8 ("mask 4", 2D) = "black" {}
		_Texture9 ("over 4", 2D) = "white" {}
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert

		sampler2D _MainTex;
		sampler2D _Texture2;
		sampler2D _Texture3;
		sampler2D _Texture4;
		sampler2D _Texture5;
		sampler2D _Texture6;
		sampler2D _Texture7;
		sampler2D _Texture8;
		sampler2D _Texture9;

		struct Input {
			float2 uv_MainTex;
			float2 uv_Texture2;
		};

		void surf (Input IN, inout SurfaceOutput o) {
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			half4 m = tex2D (_Texture2, IN.uv_Texture2);
			half4 z = tex2D (_Texture3, IN.uv_MainTex);
			half4 output = lerp(c, z, m);
			half4 m2 = tex2D (_Texture4, IN.uv_Texture2);
			half4 z2 = tex2D (_Texture5, IN.uv_MainTex);
			output = lerp(output, z2, m2);
			half4 m3 = tex2D (_Texture6, IN.uv_Texture2);
			half4 z3 = tex2D (_Texture7, IN.uv_MainTex);
			output = lerp(output, z3, m3);
			half4 m4 = tex2D (_Texture8, IN.uv_Texture2);
			half4 z4 = tex2D (_Texture9, IN.uv_MainTex);
			output = lerp(output, z4, m4);
			o.Albedo = output.rgb;
			o.Alpha = output.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

could you guys give me a hand on how to optimize this for iOS, both in terms of compatibility and performance?

instead of having 4 separate mask textures, you only need to have one mask texture and the use each channel to mask your main textures… and you’ll need to get a little tricky with how you produce your mask texture in Photoshop… I just quickly changed your code to show you how to use the different channels (but haven’t tested this):

half4 c = tex2D (_MainTex, IN.uv_MainTex);
half4 m = tex2D (_Texture2, IN.uv_Texture2);
half4 z = tex2D (_Texture3, IN.uv_MainTex);
half4 output = lerp(c, z, m.r);
half4 z2 = tex2D (_Texture5, IN.uv_MainTex);
output = lerp(output, z2, m.g);
half4 z3 = tex2D (_Texture7, IN.uv_MainTex);
output = lerp(output, z3, m.b);
half4 m4 = tex2D (_Texture8, IN.uv_Texture2);
half4 z4 = tex2D (_Texture9, IN.uv_MainTex);
output = lerp(output, z4, m.a);