- Home /
Best Practice to Combine many textures
I'm trying to combine a lot of textures with the help of a shader, but the problem is that I would like to reuse code, with the help of arrays or maybe cycles. I have more of 20 texture to use.
Basically I have a background and secondary textures that have a object of the background with a different color and the rest is transparent, this serves to create a color change effect.
This is my shader:
Shader "Unlit/Combinar"{
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Main Texture Color (RGB)", 2D) = "white" {}
_Text1 ("Overlay Texture Color (RGB) Alpha (A)", 2D) = "white" {}
_Text2 ("Overlay Texture Color (RGB) Alpha (A)", 2D) = "white" {}
}
SubShader {
Pass {
}
LOD 200
CGPROGRAM
#pragma surface surf Standard alpha fullforwardshadows
#pragma target 3.5
sampler2D _MainTex;
sampler2D _Text1;
sampler2D _Text2;
struct Input {
float2 uv_MainTex;
};
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o) {
float4 mainTex = tex2D (_MainTex, IN.uv_MainTex);
float4 overlayTex1 = tex2D (_Text1, IN.uv_MainTex);
float4 overlayTex2 = tex2D (_Text2, IN.uv_MainTex);
half3 mainTexVisible = mainTex.rgb * (1 - overlayTex1.a - overlayTex2.a );
half3 overlayTexVisible1 = overlayTex1.rgb * (overlayTex1.a);
half3 overlayTexVisible2 = overlayTex2.rgb * (overlayTex2.a);
float3 finalColor = (mainTexVisible + overlayTexVisible1 + overlayTexVisible2 ) * _Color ;
o.Albedo = finalColor.rgb;
o.Alpha = 3.5; ( mainTex.a + overlayTex.a );
}
ENDCG
}
}
In this shader combine the background and two textures the idea is to do it in an optimal way with 20 or more textures.
The values of the textures are changed from Unity in the following way:
material.SetTexture ("_Text1", myText);
Also, secondary textures should not be displayed in some cases, I've thought about using an flags [20]; with 1 in the position flags [i] will use the texture and with 0 it will not use it.
Maybe using any like this: https://docs.unity3d.com/Manual/SL-TextureArrays.html ; https://gamedev.stackexchange.com/questions/125334/unity-passing-an-array-in-a-shader
Thanks for any advice, regards.