Search Unity

Question I made a shader,but can not work in unity terrain

Discussion in 'Shaders' started by wechat_os_Qy0989W4msb7Ot5jx6_iGbPvk, Sep 20, 2022.

  1. wechat_os_Qy0989W4msb7Ot5jx6_iGbPvk

    wechat_os_Qy0989W4msb7Ot5jx6_iGbPvk

    Joined:
    Nov 3, 2020
    Posts:
    10
    I made a shader,but can not work in unity terrain.It looks as shown below.Here is my shader script.
    Code (CSharp):
    1. Shader "Nature/Terrain/Diffuse" {
    2.     Properties{
    3.       _Control("Control (RGBA)", 2D) = "red" {}
    4.       _Splat3("Layer 3 (A)", 2D) = "white" {}
    5.       _Splat2("Layer 2 (B)", 2D) = "white" {}
    6.       _Splat1("Layer 1 (G)", 2D) = "white" {}
    7.       _Splat0("Layer 0 (R)", 2D) = "white" {}
    8.         // used in fallback on old cards & base map
    9.       _MainTex("BaseMap (RGB)", 2D) = "white" {}
    10.       _Color("Main Color", Color) = (1,1,1,1)
    11.     }
    12.  
    13.         SubShader{
    14.             Tags {
    15.                 "SplatCount" = "4"
    16.                 "Queue" = "Geometry-100"
    17.                 "RenderType" = "Opaque"
    18.             }
    19.         CGPROGRAM
    20.         #pragma surface surf Lambert
    21.         #pragma target 4.0
    22.         struct Input {
    23.             float2 uv_Control : TEXCOORD0;
    24.             float2 uv_Splat0 : TEXCOORD1;
    25.             float2 uv_Splat1 : TEXCOORD2;
    26.             float2 uv_Splat2 : TEXCOORD3;
    27.             float2 uv_Splat3 : TEXCOORD4;
    28.         };
    29.  
    30.         sampler2D _Control;
    31.         sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
    32.  
    33.         float4 hash4(float2 p) {
    34.             return frac(sin(float4(1.0 + dot(p, float2(37.0, 17.0)),
    35.  
    36.                 2.0 + dot(p, float2(11.0, 47.0)),
    37.  
    38.                 3.0 + dot(p, float2(41.0, 29.0)),
    39.  
    40.                 4.0 + dot(p, float2(23.0, 31.0)))) * 103.0);
    41.         }
    42.  
    43.         fixed4 texNoTileTech2(sampler2D tex, float2 uv) {
    44.             float2 iuv = floor(uv);
    45.             float2 fuv = frac(uv);
    46.  
    47.             // Compute the correct derivatives for mipmapping
    48.             float2 dx = ddx(uv);
    49.             float2 dy = ddy(uv);
    50.  
    51.             // Voronoi contribution
    52.             float4 va = 0.0;
    53.             float wt = 0.0;
    54.             float blur = -(0 + 0.5) * 30.0;
    55.             for (int j = -1; j <= 1; j++) {
    56.                 for (int i = -1; i <= 1; i++) {
    57.                     float2 g = float2((float)i, (float)j);
    58.                     float4 o = hash4(iuv + g);
    59.                     // Compute the blending weight proportional to a gaussian fallof
    60.                     float2 r = g - fuv + o.xy;
    61.                     float d = dot(r, r);
    62.                     float w = exp(blur * d);
    63.                     float4 c = tex2D(tex, uv + o.zw, dx, dy);
    64.                     va += w * c;
    65.                     wt += w;
    66.                 }
    67.             }
    68.             // Normalization
    69.             return va / wt;
    70.         }
    71.  
    72.         void surf(Input IN, inout SurfaceOutput o) {
    73.             fixed4 splat_control = tex2D(_Control, IN.uv_Control);
    74.             fixed3 col;
    75.             col = splat_control.r * texNoTileTech2(_Splat0, IN.uv_Splat0).rgb;
    76.             col += splat_control.g * texNoTileTech2(_Splat1, IN.uv_Splat1).rgb;
    77.             col += splat_control.b * texNoTileTech2(_Splat2, IN.uv_Splat2).rgb;
    78.             col = col * clamp(pow(1 - splat_control.a * 1.1 - splat_control.a * texNoTileTech2(_Splat3, IN.uv_Splat3).a, 3.4), 0, 1) + texNoTileTech2(_Splat3, IN.uv_Splat3).rgb * clamp(splat_control.a * 3.0 + splat_control.a * texNoTileTech2(_Splat3, IN.uv_Splat3).a * 4, 0.0, 1.0);
    79.             o.Albedo = col;
    80.             o.Alpha = 0.0;
    81.         }
    82.         ENDCG
    83.         }
    84.  
    85.             Dependency "AddPassShader" = "Hidden/TerrainEngine/Splatmap/Lightmap-AddPass"
    86.             Dependency "BaseMapShader" = "Diffuse"
    87.             Dependency "Details0" = "Hidden/TerrainEngine/Details/Vertexlit"
    88.             Dependency "Details1" = "Hidden/TerrainEngine/Details/WavingDoublePass"
    89.             Dependency "Details2" = "Hidden/TerrainEngine/Details/BillboardWavingDoublePass"
    90.             Dependency "Tree0" = "Hidden/TerrainEngine/BillboardTree"
    91.  
    92.             // Fallback to Diffuse
    93.             Fallback "Diffuse"
    94. }
     

    Attached Files: