Any idea why the 5th texture does not work?

Hi guys
Me and Jon snow are alike when it comes to shaders we both know nothing.
Been reading up on them last 2 days and have a better grasp on some of it but lost mostly.

Anyway i got this shader off the asset store( https://www.assetstore.unity3d.com/en/?mkt_tok=3RkMMJWWfF9wsRovuKjPZKXonjHpfsX57%2BgtXaG0hIkz2EFye%2BLIHETpodcMTcJhMr3YDBceEJhqyQJxPr3BLNINw8ZuRhDnDg%3D%3D#!/content/23017) and it has slots to place 5 textures on a mesh but the 5th does nothing was wondering if this is a limit of unity,my gpu or just an error. in the code

If any shader Guru could enlighten would appreciate it.
Thx.

Shader "Custom/5TEX" {
    Properties {
    _Splat4 ("Layer 4 (A)", 2D) = "Black" {}
    _Splat3 ("Layer 3 (B)", 2D) = "Black" {}
    _Splat2 ("Layer 2 (G)", 2D) = "Black" {}
    _Splat1 ("Layer 1 (R)", 2D) = "Black" {}
    _Splat0 ("Layer 0 (-)", 2D) = "Black" {}//****THIS IS THE TEXTURE THAT DOES NO SEEM TO DO ANYTHING***
    // used in fallback on old cards & base map
    _MainTex ("Fallback texture", 2D) = "Black" {}
    //controlls edge blurryness
    _Fade ("Fade length", Range( 0.001,0.5)) = 0.1
}
   
SubShader {
    Tags {
    }
CGPROGRAM
#pragma surface surf Lambert
#pragma target 3.0
struct Input {
    float2 uv_Splat0 : TEXCOORD0;
    float2 uv_Splat1 : TEXCOORD1;
    float2 uv_Splat2 : TEXCOORD2;
    float2 uv_Splat3 : TEXCOORD3;
    float2 uv_Splat4 : TEXCOORD4;
    float4 color : COLOR;
};

sampler2D _Splat0,_Splat1,_Splat2,_Splat3,_Splat4;
float _Fade;

void surf (Input IN, inout SurfaceOutput o) {
    float4 splat_control = IN.color;
    fixed4 tex0 = tex2D (_Splat0, IN.uv_Splat0);
    fixed4 tex1 = tex2D (_Splat1, IN.uv_Splat1);
    fixed4 tex2 = tex2D (_Splat2, IN.uv_Splat2);
    fixed4 tex3 = tex2D (_Splat3, IN.uv_Splat3);
    fixed4 tex4 = tex2D (_Splat4, IN.uv_Splat4);
    //splat_control *= 0.5.xxxx;
    fixed nothing = clamp(1-splat_control.r - splat_control.g - splat_control.b - splat_control.a,0,1);
    //nothing = nothing *2;
    splat_control += fixed4(tex1.a * splat_control.r, tex2.a * splat_control.g, tex3.a * splat_control.b, tex4.a * splat_control.a);
   
    fixed4 sc2;
        sc2.r =    clamp((splat_control.r - nothing)+_Fade,0,1) * clamp((splat_control.r - splat_control.g)+_Fade,0,1) * clamp((splat_control.r - splat_control.b)+_Fade,0,1) * clamp((splat_control.r - splat_control.a)+_Fade,0,1);
        sc2.g = clamp((splat_control.g - nothing)+_Fade,0,1) * clamp((splat_control.g - splat_control.r)+_Fade,0,1) * clamp((splat_control.g - splat_control.b)+_Fade,0,1) * clamp((splat_control.g - splat_control.a)+_Fade,0,1);
        sc2.b = clamp((splat_control.b - nothing)+_Fade,0,1) * clamp((splat_control.b - splat_control.r)+_Fade,0,1) * clamp((splat_control.b - splat_control.g)+_Fade,0,1) * clamp((splat_control.b - splat_control.a)+_Fade,0,1);
        sc2.a = clamp((splat_control.a - nothing)+_Fade,0,1) * clamp((splat_control.a - splat_control.r)+_Fade,0,1) * clamp((splat_control.a - splat_control.g)+_Fade,0,1) * clamp((splat_control.a - splat_control.b)+_Fade,0,1);
        fixed alpha = clamp((nothing - splat_control.r)+_Fade,0,1) * clamp((nothing - splat_control.g)+_Fade,0,1) * clamp((nothing - splat_control.b)+_Fade,0,1) * clamp((nothing - splat_control.a)+_Fade,0,1);
    half sum = sc2.r + sc2.g + sc2.b + sc2.a + alpha;
    sc2 = sc2 / sum;
    alpha = alpha / sum;
    o.Albedo = alpha * tex0 +  tex1 * sc2.r + tex2 * sc2.g + tex3 * sc2.b + tex4 * sc2.a;
    o.Normal = UnpackNormal(fixed4(0.5,0.5,0.5,0.5));

    o.Alpha = 0.0;
}
ENDCG 
}


Fallback "Mobile/Diffuse"
}
1 Like

It’s hard to follow what that shader is trying to do… but it looks like the shader is blending textures based on vertex color values.

You can see the first 4 textures are sampled and the sampled color is stored in variables tex1 through tex4. Those textures are then used to make a “sc2” float4, which I believe stands for “splat control 2”. The texture that isn’t working for you is not used for sc2, but instead for a single float called “alpha”, and the calculation is involved is quite different from the calculations used on the other textures. I can’t tell what they’re trying to do differently, but you can see in the following lines that the last calculation doesn’t follow the same pattern:

fixed4 sc2;
        sc2.r =    clamp((splat_control.r - nothing)+_Fade,0,1) * clamp((splat_control.r - splat_control.g)+_Fade,0,1) * clamp((splat_control.r - splat_control.b)+_Fade,0,1) * clamp((splat_control.r - splat_control.a)+_Fade,0,1);
        sc2.g = clamp((splat_control.g - nothing)+_Fade,0,1) * clamp((splat_control.g - splat_control.r)+_Fade,0,1) * clamp((splat_control.g - splat_control.b)+_Fade,0,1) * clamp((splat_control.g - splat_control.a)+_Fade,0,1);
        sc2.b = clamp((splat_control.b - nothing)+_Fade,0,1) * clamp((splat_control.b - splat_control.r)+_Fade,0,1) * clamp((splat_control.b - splat_control.g)+_Fade,0,1) * clamp((splat_control.b - splat_control.a)+_Fade,0,1);
        sc2.a = clamp((splat_control.a - nothing)+_Fade,0,1) * clamp((splat_control.a - splat_control.r)+_Fade,0,1) * clamp((splat_control.a - splat_control.g)+_Fade,0,1) * clamp((splat_control.a - splat_control.b)+_Fade,0,1);
        fixed alpha = clamp((nothing - splat_control.r)+_Fade,0,1) * clamp((nothing - splat_control.g)+_Fade,0,1) * clamp((nothing - splat_control.b)+_Fade,0,1) * clamp((nothing - splat_control.a)+_Fade,0,1);

Again, this is just a guess, but I think that texture slot is used to alter how the textures blend together (examples: linear, jagged edges, pixelated, etc.)

Your guess is much better than mine. Only word i recognize is “nothing” and that is only because its what me an Jon have in common as relates to knowledge concerning shaders. Thx for the reply glad to know it is at least complicated and not just me:smile:. Kudos to you mate…

Still wonder is there a limit to how many textures you can have at once this has 4 like unity terrain before having to redraw do shader have the same limitation?or o they just draw once regardless of tex amounts?

Texture limits are imposed by the Shader Model. SM 3.0 has an upper limit of 16 texture samplers. You see 4 being used here due to the 4 channel (RGBA) splat look-up. Each channel represents the output of 1 texture giving a maximum of 4 textures per splat.

Now, if you want bump maps that’s a further 4 textures amounting to (4 color + 4 bump + 1 splat) = 9 textures. If you want an additional 4 textures you’ll need another splat plus the new textures & bumps, equating to a total of 18 samplers - which is why the terrain shader would rendering this in two passes.

Well it also uses a textures for the 5th slot which i don’t see effecting anything but Gambit thinks might be for blending plus a sixth for fallback . I have been trying and failing to add normals to the shader its pretty bland without them . Thx