Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Blending shader issues

Discussion in 'Shaders' started by kilian277, Nov 12, 2016.

  1. kilian277

    kilian277

    Joined:
    Jul 13, 2012
    Posts:
    54
    Hello everyone

    I'm trying to make a shader that takes a mesh and does a blend between just regular terrain and a single texture on another mesh.

    Since unity has a limit of 16 samplers (and i'm using 16 samplers in one single surface shader) i needed to split it up in 2 passes by just drawing the regular texture and then the terrain blrending on top.

    but something is funky about the end result:




    And here is the code for the blend:

    Code (CSharp):
    1. Shader "Terrain/6lyr_terrain_blend" {
    2.     Properties
    3.     {
    4.         _blend1 ("Blendmap 1", 2D) = "white" {}
    5.         _blend2 ("Blendmap 2", 2D) = "white" {}
    6.         _MainTex ("MainTex", 2D) = "white" {}
    7.         _MainTexn ("MainTex normal", 2D) = "white" {}
    8.         _Layer1 ("Layer1", 2D) = "white" {}
    9.         _Layer1n ("Layer 1 normal", 2D) = "white" {}
    10.         _Layer2 ("Layer2", 2D) = "white" {}
    11.         _Layer2n ("Layer 2 normal", 2D) = "white" {}
    12.         _Layer3 ("Layer3", 2D) = "white" {}
    13.         _Layer3n ("Layer 3 normal", 2D) = "white" {}
    14.         _Layer4 ("Layer4", 2D) = "white" {}
    15.         _Layer4n ("Layer 4 normal", 2D) = "white" {}
    16.         _Layer5 ("Layer5", 2D) = "white" {}
    17.         _Layer5n ("Layer 5 normal", 2D) = "white" {}
    18.         _Layer6 ("Layer6", 2D) = "white" {}
    19.         _Layer6n ("Layer 6 normal", 2D) = "white" {}
    20.     }
    21.     SubShader
    22.     {
    23.         Tags { "Queue"="Geometry" "RenderType" = "Opaque"}
    24.          
    25.         Cull Off
    26.      
    27.         //BLENDMAPS DRAWING
    28.         CGPROGRAM
    29.          
    30.             #pragma surface surf Lambert noforwardadd
    31.             #pragma target 3.0
    32.  
    33.             sampler2D     _MainTex;
    34.             sampler2D    _MainTexn;
    35.  
    36.             struct Input {
    37.                 float2 uv2_MainTex;
    38.                 float4 color : COLOR;
    39.             };
    40.  
    41.             void surf (Input IN, inout SurfaceOutput o)
    42.             {
    43.                 float4 m = tex2D (_MainTex, IN.uv2_MainTex);
    44.                 float3 mn = UnpackNormal(tex2D (_MainTexn, IN.uv2_MainTex));
    45.  
    46.                 o.Albedo = ((m * IN.color.w) * IN.color);
    47.                 o.Normal = (mn * IN.color.w);
    48.             }
    49.         ENDCG
    50.      
    51.         Blend One One
    52.      
    53.      
    54.      
    55.         //BLENDMAPS DRAWING
    56.         CGPROGRAM
    57.          
    58.             #pragma surface surf Lambert noforwardadd
    59.             #pragma target 3.0
    60.             sampler2D   _blend1;
    61.             sampler2D   _blend2;
    62.             sampler2D   _Layer1;
    63.             sampler2D   _Layer1n;
    64.             sampler2D   _Layer2;
    65.             sampler2D   _Layer2n;
    66.             sampler2D   _Layer3;
    67.             sampler2D   _Layer3n;
    68.             sampler2D   _Layer4;
    69.             sampler2D   _Layer4n;
    70.             sampler2D   _Layer5;
    71.             sampler2D   _Layer5n;
    72.             sampler2D   _Layer6;
    73.             sampler2D   _Layer6n;
    74.  
    75.             struct Input {
    76.                 float2 uv_blend1;
    77.                 float2 uv_Layer1;
    78.                 float2 uv2_MainTex;
    79.                 float4 color : COLOR;
    80.             };
    81.  
    82.             void surf (Input IN, inout SurfaceOutput o)
    83.             {
    84.                 float4 b1 = tex2D (_blend1, IN.uv_blend1);
    85.                 float4 b2 = tex2D (_blend2, IN.uv_blend1);
    86.              
    87.                 float4 l1 = tex2D (_Layer1, IN.uv_Layer1);
    88.                 float4 l2 = tex2D (_Layer2, IN.uv_Layer1);
    89.                 float4 l3 = tex2D (_Layer3, IN.uv_Layer1);
    90.                 float3 l1n = UnpackNormal(tex2D (_Layer1n, IN.uv_Layer1));
    91.                 float3 l2n = UnpackNormal(tex2D (_Layer2n, IN.uv_Layer1));
    92.                 float3 l3n = UnpackNormal(tex2D (_Layer3n, IN.uv_Layer1));
    93.              
    94.                 float4 l4 = tex2D (_Layer4, IN.uv_Layer1);
    95.                 float4 l5 = tex2D (_Layer5, IN.uv_Layer1);
    96.                 float4 l6 = tex2D (_Layer6, IN.uv_Layer1);
    97.                 float3 l4n = UnpackNormal(tex2D (_Layer4n, IN.uv_Layer1));
    98.                 float3 l5n = UnpackNormal(tex2D (_Layer5n, IN.uv_Layer1));
    99.                 float3 l6n = UnpackNormal(tex2D (_Layer6n, IN.uv_Layer1));
    100.  
    101.                 float4 r1 = lerp(l1,l1,b2.r);
    102.                 float4 g1 = lerp(r1,l2,b2.g);
    103.                 float4 xb1 = lerp(g1,l3,b2.b);
    104.                 float4 r2 = lerp(xb1,l4,b1.r);
    105.                 float4 g2 = lerp(r2,l5,b1.g);
    106.                 float4 xb2 = lerp(g2,l6,b1.b);
    107.                 o.Albedo = ((xb2 * (1 - IN.color.w)) * IN.color);
    108.              
    109.                 float3 r1n = lerp(l1n,l1n,b2.r);
    110.                 float3 g1n = lerp(r1n,l2n,b2.g);
    111.                 float3 xb1n = lerp(g1n,l3n,b2.b);
    112.                 float3 r2n = lerp(xb1n,l4n,b1.r);
    113.                 float3 g2n = lerp(r2n,l5n,b1.g);
    114.                 float3 xb2n = lerp(g2n,l6n,b1.b);
    115.                 o.Normal = (xb2n * (1 - IN.color.w));
    116.             }
    117.         ENDCG
    118.      
    119.     }
    120. }
     
unityunity