Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How can I add support for more than 4 Textures in a custom Terrain Shader

Discussion in 'Shaders' started by blueteak, Jan 25, 2023.

  1. blueteak

    blueteak

    Joined:
    Feb 19, 2013
    Posts:
    139
    Hello! I have this custom terrain shader that works great in the standard render pipeline, but doesn't support more than 4 terrain textures (after 4 they just don't show up). Is there an easy way to add the multi-pass support that doesn't involve invoking the built-in terrain shader splat/cgincs?

    Code (CSharp):
    1. Shader "Tome/Tome Terrain"
    2. {
    3.     Properties
    4.     {
    5.         [Header(Greyscale Area)]
    6.         _GreyMin ("Grey Min", Range(0,1)) = 0.05
    7.         _GreyMax ("Grey Max", Range(0,1)) = 0.5
    8.     }
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Opaque" "TerrainCompatible"="True"}
    12.         LOD 200
    13.  
    14.         CGPROGRAM
    15.         #pragma surface surf Lambert fullforwardshadows
    16.         #pragma target 3.0
    17.         #include "TomeGrey.cginc"
    18.  
    19.         //Splat Map
    20.         sampler2D _Control;
    21.         // Splat Textures
    22.         sampler2D _Splat0, _Splat1, _Splat2, _Splat3;
    23.         float4 _Splat0_ST, _Splat1_ST, _Splat2_ST, _Splat3_ST;
    24.         sampler2D _Normal0, _Normal1, _Normal2, _Normal3;
    25.         float _NormalScale0, _NormalScale1, _NormalScale2, _NormalScale3;
    26.         float _GreyMin, _GreyMax;
    27.         struct Input
    28.         {
    29.             float2 uv_Control;
    30.             float3 worldPos;
    31.             float3 worldNormal;
    32.             INTERNAL_DATA
    33.         };
    34.  
    35.         fixed4 ApplySplats(Input IN, inout SurfaceOutput o)
    36.         {
    37.             fixed4 splatControl = tex2D(_Control, IN.uv_Control);
    38.             fixed4 col = splatControl.r * tex2D (_Splat0, IN.uv_Control * _Splat0_ST.xy);
    39.             col += splatControl.g * tex2D(_Splat1, IN.uv_Control * _Splat1_ST.xy);
    40.             col += splatControl.b * tex2D (_Splat2, IN.uv_Control * _Splat2_ST.xy);
    41.             col += splatControl.a * tex2D (_Splat3, IN.uv_Control * _Splat3_ST.xy);
    42.            
    43.             o.Normal = splatControl.r * UnpackNormalWithScale(tex2D(_Normal0, IN.uv_Control * _Splat0_ST.xy), _NormalScale0);
    44.             o.Normal += splatControl.g * UnpackNormalWithScale(tex2D(_Normal1, IN.uv_Control * _Splat1_ST.xy), _NormalScale1);
    45.             o.Normal += splatControl.b * UnpackNormalWithScale(tex2D(_Normal2, IN.uv_Control * _Splat2_ST.xy), _NormalScale2);
    46.             o.Normal += splatControl.a * UnpackNormalWithScale(tex2D(_Normal3, IN.uv_Control * _Splat3_ST.xy), _NormalScale3);
    47.            
    48.             return col;
    49.         }
    50.  
    51.         void surf (Input IN, inout SurfaceOutput o)
    52.         {
    53.             fixed4 col = ApplySplats(IN, o);
    54.            
    55.             col = GreyscaleArea(col, IN.worldPos, _Time.y, _GreyMin, _GreyMax);
    56.             o.Albedo = col.rgb;
    57.             o.Alpha = col.a;
    58.         }
    59.         ENDCG  
    60.     }
    61.  
    62.     FallBack "Diffuse"
    63. }
     
  2. backwheelbates

    backwheelbates

    Joined:
    Jan 14, 2014
    Posts:
    225
    Im running into the same issue with ShaderGraph terrain shaders, @blueteak , did you find a solution? Thanks!!