What is the maximum number of textures a shader can have? From what I’ve been able to find, its video card dependent with 16 being the most. Let me tell you what I’m trying to do. I’m trying to make one mesh that has hundreds of textures whose colors can be controlled independently. Is something like this even possible? Thanks in advance.
As far as I know, the number of different textures sampled in a programmable shader isn’t limited. The only bounds are the amount of video memory you have available.
However, sampling hundreds of textures in a single shader won’t be cheap, especially if you’re bandwidth limited. Realize that every rendered pixel of that object will have to read from all the textures.
So I believe it is possible, but it’s definitely not the way you should be doing it. What do you need it for?
Yes the maximum number of texture samplers is 16 in Shader Model 3. To get around this, you will have to break your shader up into different passes and compose the results together.
Thanks for the responses. Dolkar - I’m trying to simulate hundreds of lights shining through a translucent material. jRocket - How do you compose different passes together? I’m searching the web, but coming up empty. Thanks again guys.
If youre on Unity Pro you can render to a render texture and then later combine those.
If you want to additively blend them, you just put Blend One One as the first line in your Pass { }. More info here → http://docs.unity3d.com/Documentation/Components/SL-Blend.html
SubShaders love them to bits!
So this is a version (of many) of what I’m trying to do. This try is stacking surface passes on top of each other until i have 225 textures. Unfortunately, as you can see in the picture, I have a line where the textures aren’t blending properly between surface passes. Keep in mind, i didn’t list all surface passes because, as you can imagine, this file was gigantic. Any idea how to blend properly between passes?
SubShader
{
//main texture/ base texture
Pass
{
Tags{"Queue" = "Geometry"}
CGPROGRAM
#pragma fragmentoption ARB_precision_fastest
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
struct vertexInput
{
float4 vertex : POSITION;
half2 texcoord : TEXCOORD0;
};
struct fragmentInput
{
float4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
};
fragmentInput vert(vertexInput i)
{
fragmentInput o;
o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
o.uv = i.texcoord;
return o;
}
half4 frag(fragmentInput i) : COLOR
{
half4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}//pass end
//first set of textures (1 - 14)
Tags {"RenderType" = "Transparent" "Queue" = "Transparent"}
LOD 200
//Blend SrcAlpha OneMinusSrcAlpha
Blend One One
BlendOp Add
ZWrite Off
CGPROGRAM
#pragma target 3.0
#pragma surface surf Lambert alpha
#include "UnityCG.cginc"
float4 _Texture1_color;
sampler2D _Texture1;
float4 _Texture2_color;
sampler2D _Texture2;
float4 _Texture3_color;
sampler2D _Texture3;
float4 _Texture4_color;
sampler2D _Texture4;
float4 _Texture5_color;
sampler2D _Texture5;
float4 _Texture6_color;
sampler2D _Texture6;
float4 _Texture7_color;
sampler2D _Texture7;
float4 _Texture8_color;
sampler2D _Texture8;
float4 _Texture9_color;
sampler2D _Texture9;
float4 _Texture10_color;
sampler2D _Texture10;
float4 _Texture11_color;
sampler2D _Texture11;
float4 _Texture12_color;
sampler2D _Texture12;
float4 _Texture13_color;
sampler2D _Texture13;
float4 _Texture14_color;
sampler2D _Texture14;
struct Input
{
float2 uv_Texture1 : TEXCOORD0;
};
void surf (Input IN, inout SurfaceOutput o)
{
half4 tex_1 = tex2D (_Texture1, IN.uv_Texture1) * _Texture1_color;
half4 tex_2 = tex2D (_Texture2, IN.uv_Texture1) * _Texture2_color;
half4 tex_3 = tex2D (_Texture3, IN.uv_Texture1) * _Texture3_color;
half4 tex_4 = tex2D (_Texture4, IN.uv_Texture1) * _Texture4_color;
half4 tex_5 = tex2D (_Texture5, IN.uv_Texture1) * _Texture5_color;
half4 tex_6 = tex2D (_Texture6, IN.uv_Texture1) * _Texture6_color;
half4 tex_7 = tex2D (_Texture7, IN.uv_Texture1) * _Texture7_color;
half4 tex_8 = tex2D (_Texture8, IN.uv_Texture1) * _Texture8_color;
half4 tex_9 = tex2D (_Texture9, IN.uv_Texture1) * _Texture9_color;
half4 tex_10 = tex2D (_Texture10, IN.uv_Texture1) * _Texture10_color;
half4 tex_11 = tex2D (_Texture11, IN.uv_Texture1) * _Texture11_color;
half4 tex_12 = tex2D (_Texture12, IN.uv_Texture1) * _Texture12_color;
half4 tex_13 = tex2D (_Texture13, IN.uv_Texture1) * _Texture13_color;
half4 tex_14 = tex2D (_Texture14, IN.uv_Texture1) * _Texture14_color;
tex_1.a = GreatestColorValue(tex_1.rgb);
tex_2.a = GreatestColorValue(tex_2.rgb);
tex_3.a = GreatestColorValue(tex_3.rgb);
tex_4.a = GreatestColorValue(tex_4.rgb);
tex_5.a = GreatestColorValue(tex_5.rgb);
tex_6.a = GreatestColorValue(tex_6.rgb);
tex_7.a = GreatestColorValue(tex_7.rgb);
tex_8.a = GreatestColorValue(tex_8.rgb);
tex_9.a = GreatestColorValue(tex_9.rgb);
tex_10.a = GreatestColorValue(tex_10.rgb);
tex_11.a = GreatestColorValue(tex_11.rgb);
tex_12.a = GreatestColorValue(tex_12.rgb);
tex_13.a = GreatestColorValue(tex_13.rgb);
tex_14.a = GreatestColorValue(tex_14.rgb);
float3 finalColor = tex_1.rgb +
tex_2.rgb +
tex_3.rgb +
tex_4.rgb +
tex_5.rgb +
tex_6.rgb +
tex_7.rgb +
tex_8.rgb +
tex_9.rgb +
tex_10.rgb +
tex_11.rgb +
tex_12.rgb +
tex_13.rgb +
tex_14.rgb;
float3 finalAlpha = tex_1.a +
tex_2.a +
tex_3.a +
tex_4.a +
tex_5.a +
tex_6.a +
tex_7.a +
tex_8.a +
tex_9.a +
tex_10.a +
tex_11.a +
tex_12.a +
tex_13.a +
tex_14.a;
float4 finalTotal = float4(finalColor, finalAlpha);
o.Emission = finalTotal.rgb;
o.Alpha = finalTotal.a;
}
ENDCG
Tags {"RenderType" = "Transparent" "Queue" = "Transparent"}
LOD 200
Blend One One
BlendOp Add
ZWrite Off
CGPROGRAM
#pragma target 3.0
#pragma surface surf Lambert alpha
#include "UnityCG.cginc"
float4 _Texture15_color;
sampler2D _Texture15;
float4 _Texture16_color;
sampler2D _Texture16;
float4 _Texture17_color;
sampler2D _Texture17;
float4 _Texture18_color;
sampler2D _Texture18;
float4 _Texture19_color;
sampler2D _Texture19;
float4 _Texture20_color;
sampler2D _Texture20;
float4 _Texture21_color;
sampler2D _Texture21;
float4 _Texture22_color;
sampler2D _Texture22;
float4 _Texture23_color;
sampler2D _Texture23;
float4 _Texture24_color;
sampler2D _Texture24;
float4 _Texture25_color;
sampler2D _Texture25;
float4 _Texture26_color;
sampler2D _Texture26;
float4 _Texture27_color;
sampler2D _Texture27;
float4 _Texture28_color;
sampler2D _Texture28;
struct Input
{
float2 uv_Texture1 : TEXCOORD0;
};
void surf (Input IN, inout SurfaceOutput o)
{
half4 tex_15 = tex2D (_Texture15, IN.uv_Texture1) * _Texture15_color;
half4 tex_16 = tex2D (_Texture16, IN.uv_Texture1) * _Texture16_color;
half4 tex_17 = tex2D (_Texture17, IN.uv_Texture1) * _Texture17_color;
half4 tex_18 = tex2D (_Texture18, IN.uv_Texture1) * _Texture18_color;
half4 tex_19 = tex2D (_Texture19, IN.uv_Texture1) * _Texture19_color;
half4 tex_20 = tex2D (_Texture20, IN.uv_Texture1) * _Texture20_color;
half4 tex_21 = tex2D (_Texture21, IN.uv_Texture1) * _Texture21_color;
half4 tex_22 = tex2D (_Texture22, IN.uv_Texture1) * _Texture22_color;
half4 tex_23 = tex2D (_Texture23, IN.uv_Texture1) * _Texture23_color;
half4 tex_24 = tex2D (_Texture24, IN.uv_Texture1) * _Texture24_color;
half4 tex_25 = tex2D (_Texture25, IN.uv_Texture1) * _Texture25_color;
half4 tex_26 = tex2D (_Texture26, IN.uv_Texture1) * _Texture26_color;
half4 tex_27 = tex2D (_Texture27, IN.uv_Texture1) * _Texture27_color;
half4 tex_28 = tex2D (_Texture28, IN.uv_Texture1) * _Texture28_color;
tex_15.a = GreatestColorValue(tex_15.rgb);
tex_16.a = GreatestColorValue(tex_16.rgb);
tex_17.a = GreatestColorValue(tex_17.rgb);
tex_18.a = GreatestColorValue(tex_18.rgb);
tex_19.a = GreatestColorValue(tex_19.rgb);
tex_20.a = GreatestColorValue(tex_20.rgb);
tex_21.a = GreatestColorValue(tex_21.rgb);
tex_22.a = GreatestColorValue(tex_22.rgb);
tex_23.a = GreatestColorValue(tex_23.rgb);
tex_24.a = GreatestColorValue(tex_24.rgb);
tex_25.a = GreatestColorValue(tex_25.rgb);
tex_26.a = GreatestColorValue(tex_26.rgb);
tex_27.a = GreatestColorValue(tex_27.rgb);
tex_28.a = GreatestColorValue(tex_28.rgb);
float3 finalColor = tex_15.rgb +
tex_16.rgb +
tex_17.rgb +
tex_18.rgb +
tex_19.rgb +
tex_20.rgb +
tex_21.rgb +
tex_22.rgb +
tex_23.rgb +
tex_24.rgb +
tex_25.rgb +
tex_26.rgb +
tex_27.rgb +
tex_28.rgb;
float3 finalAlpha = tex_15.a +
tex_16.a +
tex_17.a +
tex_18.a +
tex_19.a +
tex_20.a +
tex_21.a +
tex_22.a +
tex_23.a +
tex_24.a +
tex_25.a +
tex_26.a +
tex_27.a +
tex_28.a;
float4 finalTotal = float4(finalColor, finalAlpha);
o.Emission = finalTotal.rgb;
o.Alpha = finalTotal.a;
}
ENDCG
I have been trying to figure out how to combine render textures, do you have a link?
I believe you can use Graphics.Blit(…) and give a Material as one of the arguments – it’ll copy over the source Texture into the destination, basically filtering it through the vertex/fragment shader, and should work with RenderTextures. And you no longer need Unity Plus/Pro for any of this