Search Unity

Terrain Shader with multiple splat textures

Discussion in 'Shaders' started by IgorAherne, Jun 12, 2017.

  1. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    Hey people, I was looking at a terrain shader which can support multiple splat textures. Just didn't know how to get hold of them. After serfing the webz, I found a post by FlatterZange
    Moin Ich bastel gerade einen alternativen shader für ein Unity terrain von mir das ich als .OBJ model exportiert habe.

    Das schlimmste war alle Shader im inet haben einen maximalen Splat Texture Count von 4.


    Allso habe ich heute mal einen mit 8 gebaut da ich schon mehr als 4 Texturen auf meinem Unity terrain nutze und das auch so bleiben soll^^


    Basieren tut er auf den Internen Unity Terrain shadern nur mit besseren Blending dank Lerp.

    Dieser Shader benötigt 2 SplatMasken findet allso nur anwendung mit mehr als 4 Splat Texturen.


    Wer intresse dran hat:
    Code (CSharp):
    1.  
    2. Shader "TMG/TMG_8SplatShader_Diffuse" {
    3. Properties {
    4. _Control ("Control Mask 1 (RGBA)", 2D) = "red" {}
    5. _Splat0 ("Layer 0 (R)", 2D) = "white" {}
    6. _Splat1 ("Layer 1 (G)", 2D) = "white" {}
    7. _Splat2 ("Layer 2 (", 2D) = "white" {}
    8. _Splat3 ("Layer 3 (A)", 2D) = "white" {}
    9. _Control2 ("Control Mask 2 (RGBA)", 2D) = "red" {}
    10. _Splat4 ("Layer 4 (R)", 2D) = "white" {}
    11. _Splat5 ("Layer 5 (G)", 2D) = "white" {}
    12. _Splat6 ("Layer 6 (", 2D) = "white" {}
    13. _Splat7 ("Layer 7 (A)", 2D) = "white" {}
    14. }
    15.  
    16. SubShader {
    17. Tags {
    18. "SplatCount" = "4"
    19. "Queue" = "Geometry-100"
    20. "IgnoreProjector"="False"
    21. "RenderType" = "Opaque"
    22. }
    23. CGPROGRAM
    24. #pragma surface surf Lambert
    25. struct Input {
    26. float2 uv_Control : TEXCOORD0;
    27. float2 uv_Splat0 : TEXCOORD1;
    28. float2 uv_Splat1 : TEXCOORD2;
    29. float2 uv_Splat2 : TEXCOORD3;
    30. float2 uv_Splat3 : TEXCOORD4;
    31. };
    32. sampler2D _Control;
    33. sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
    34. void surf (Input IN, inout SurfaceOutput o) {
    35. fixed4 splat_control = tex2D (_Control, IN.uv_Control);
    36. fixed3 col  = fixed3(0,0,0);
    37.  
    38. float4 tmp = tex2D (_Splat0, IN.uv_Splat0);
    39. col = lerp(col, tmp.rgb, splat_control.r * tmp.a);
    40. tmp = tex2D (_Splat1, IN.uv_Splat1);
    41. col = lerp(col, tmp.rgb, splat_control.g * tmp.a);
    42. tmp = tex2D (_Splat2, IN.uv_Splat2);
    43. col = lerp(col, tmp.rgb, splat_control.b * tmp.a);
    44. tmp = tex2D (_Splat3, IN.uv_Splat3);
    45. col = lerp(col, tmp.rgb, splat_control.a * tmp.a);
    46.  
    47. o.Albedo = col;
    48. }
    49. ENDCG
    50.  
    51. Tags {
    52. "SplatCount" = "4"
    53. "Queue" = "Geometry-99"
    54. "IgnoreProjector"="False"
    55. "RenderType" = "Opaque"
    56. }
    57. CGPROGRAM
    58. #pragma surface surf Lambert decal:add
    59. struct Input {
    60. float2 uv_Control2 : TEXCOORD5;
    61. float2 uv_Splat4 : TEXCOORD6;
    62. float2 uv_Splat5 : TEXCOORD7;
    63. float2 uv_Splat6 : TEXCOORD8;
    64. float2 uv_Splat7 : TEXCOORD9;
    65. };
    66. sampler2D _Control2;
    67. sampler2D _Splat4,_Splat5,_Splat6,_Splat7;
    68. void surf (Input IN, inout SurfaceOutput o) {
    69. fixed4 splat_control2 = tex2D (_Control2, IN.uv_Control2);
    70. fixed3 col = fixed3(0,0,0);
    71.  
    72. float4 tmp = tex2D (_Splat4, IN.uv_Splat4);
    73. col = lerp(col, tmp.rgb, splat_control2.r * tmp.a);
    74. tmp = tex2D (_Splat5, IN.uv_Splat5);
    75. col = lerp(col, tmp.rgb, splat_control2.g * tmp.a);
    76. tmp = tex2D (_Splat6, IN.uv_Splat6);
    77. col = lerp(col, tmp.rgb, splat_control2.b * tmp.a);
    78. tmp = tex2D (_Splat7, IN.uv_Splat7);
    79. col = lerp(col, tmp.rgb, splat_control2.a * tmp.a);
    80.  
    81. o.Albedo = col;
    82. }
    83. ENDCG
    84. }
    85. // Fallback to Diffuse
    86. Fallback "Diffuse"
    87. }
    88.  
    Ich bau gerade das dazugehörige BumpScript das alle 8 layer auch mit NormalMaps und regelbaren Specular effeckt dargestellt werden können.

    Dazu mehr Später.


    Grüsse.



    After changing the shader a tiny bit (removing "in my opinion" unnecessary uv coords from the Input struct) otherwise it could throw errors "did you want to incude #pragma target 3.0", etc.
    Here it is:

    Code (CSharp):
    1. Shader "Custom/TerrainHeightShader" {
    2.     Properties{
    3.         _Control("Control Mask 1 (RGBA)", 2D) = "red" {}
    4.         _Splat0("Layer 0 (R)", 2D) = "white" {}
    5.         _Splat1("Layer 1 (G)", 2D) = "white" {}
    6.         _Splat2("Layer 2 (B)", 2D) = "white" {}
    7.         _Splat3("Layer 3 (A)", 2D) = "white" {}
    8.         _Control2("Control Mask 2 (RGBA)", 2D) = "red" {}
    9.         _Splat4("Layer 4 (R)", 2D) = "white" {}
    10.         _Splat5("Layer 5 (G)", 2D) = "white" {}
    11.         _Splat6("Layer 6 (B)", 2D) = "white" {}
    12.         _Splat7("Layer 7 (A)", 2D) = "white" {}
    13.      }
    14.  
    15.     SubShader{
    16.         Tags{
    17.             "SplatCount" = "8"
    18.             "Queue" = "Geometry-100"
    19.             "IgnoreProjector" = "False"
    20.             "RenderType" = "Opaque"
    21.         }
    22.         CGPROGRAM
    23.         #pragma surface surf Lambert
    24.         struct Input {
    25.             float2 uv_Control : TEXCOORD0;
    26.             float2 uv_Splat0 : TEXCOORD1;
    27.         };
    28.         sampler2D _Control;
    29.         sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
    30.  
    31.         void surf(Input IN, inout SurfaceOutput o) {
    32.             fixed4 splat_control = tex2D(_Control, IN.uv_Control);
    33.             fixed3 col = fixed3(0,0,0);
    34.  
    35.             float4 tmp = tex2D(_Splat0, IN.uv_Splat0);
    36.             col = lerp(col, tmp.rgb, splat_control.r * tmp.a);
    37.             tmp = tex2D(_Splat1, IN.uv_Splat0);
    38.             col = lerp(col, tmp.rgb, splat_control.g * tmp.a);
    39.             tmp = tex2D(_Splat2, IN.uv_Splat0);
    40.             col = lerp(col, tmp.rgb, splat_control.b * tmp.a);
    41.             tmp = tex2D(_Splat3, IN.uv_Splat0);
    42.             col = lerp(col, tmp.rgb, splat_control.a * tmp.a);
    43.  
    44.             o.Albedo = col;
    45.         }
    46.         ENDCG
    47.  
    48.         Tags{
    49.             "SplatCount" = "4"
    50.             "Queue" = "Geometry-99"
    51.             "IgnoreProjector" = "False"
    52.             "RenderType" = "Opaque"
    53.         }
    54.         CGPROGRAM
    55.             #pragma surface surf Lambert decal:add
    56.             struct Input {
    57.                 float2 uv_Control2 : TEXCOORD5;
    58.                 float2 uv_Splat4 : TEXCOORD6;
    59.             };
    60.             sampler2D _Control2;
    61.             sampler2D _Splat4,_Splat5,_Splat6,_Splat7;
    62.  
    63.             void surf(Input IN, inout SurfaceOutput o) {
    64.                 fixed4 splat_control2 = tex2D(_Control2, IN.uv_Control2);
    65.                 fixed3 col = fixed3(0,0,0);
    66.  
    67.                 float4 tmp = tex2D(_Splat4, IN.uv_Splat4);
    68.                 col = lerp(col, tmp.rgb, splat_control2.r * tmp.a);
    69.                 tmp = tex2D(_Splat5, IN.uv_Splat4);
    70.                 col = lerp(col, tmp.rgb, splat_control2.g * tmp.a);
    71.                 tmp = tex2D(_Splat6, IN.uv_Splat4);
    72.                 col = lerp(col, tmp.rgb, splat_control2.b * tmp.a);
    73.                 tmp = tex2D(_Splat7, IN.uv_Splat4);
    74.                 col = lerp(col, tmp.rgb, splat_control2.a * tmp.a);
    75.  
    76.                 o.Albedo = col;
    77.             }//end surf()
    78.         ENDCG
    79.     }//End SubShader
    80.  
    81.     Fallback "Diffuse"
    82. }
     
    Last edited: Jun 12, 2017
    Opeth001 likes this.
  2. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
  3. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
  4. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    Meh ...Just use MicroSplat from asset store
     
  5. kcastagnini

    kcastagnini

    Joined:
    Dec 14, 2019
    Posts:
    61
    Hello, sorry for the necro bump.
    Is it possible to use a one pass shader with two control textures and make it compatible with the unity terrain painting tool?