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.

Cant use Shader which needs tangent geometry on terrain

Discussion in 'Shaders' started by Zaine7673, Nov 20, 2019.

  1. Zaine7673

    Zaine7673

    Joined:
    Feb 15, 2018
    Posts:
    238
    Hi all,

    So I've created a custom shader using the Unity manual in order to apply it to a terrain to try and create some sort of tracks in the snow or footprints sort of effect. However, when I apply it to the terrain i get the following error:

    upload_2019-11-20_18-8-51.png

    I'm new to shaders, this is the first one I've messed around with. I have researched this and got the understanding that it's something to do with normal maps but couldn't find anything that made sense or worked.

    Can anyone point me in the right direction or explain why the error is occurring in layman's terms for me.

    The shader code is:

    Code (CSharp):
    1.  
    2. Shader "Tracks" {
    3.  
    4.     Properties {
    5.         _GroundColor ("Ground Color", color) = (1,1,1,1)
    6.         _GroundTex ("Ground Texture", 2D) = "white" {}
    7.    
    8.         _TrackColor ("Displacement Color", color) = (1,1,1,0)
    9.         _TrackTex ("Displacement Texture", 2D) = "white" {}
    10.    
    11.         _Splat ("Track Texture", 2D) = "black" {}
    12.  
    13.         _Displacement ("Displacement", Range(0, 30)) = 0
    14.  
    15.         _Tess ("Tessellation", Range(1,30)) = 4
    16.         _TessDist ("Tessolation Distance", Range(0, 1000)) = 0.0
    17.  
    18.         _SpecColor ("Specular Color", color) = (0.5,0.5,0.5,0.5)
    19.         _SpecAmount ("Specular Amount", Range(0.1, 100)) = 0.2
    20.         _GlossAmount ("Reflective Amount", Range(0, 100)) = 0.1
    21.     }
    22.  
    23.     SubShader {
    24.         Tags { "RenderType"="Opaque" }
    25.         LOD 200
    26.        
    27.         CGPROGRAM
    28.         #pragma surface surf BlinnPhong addshadow fullforwardshadows vertex:disp tessellate:tessDistance nolightmap
    29.         #pragma target 4.6
    30.         #include "Tessellation.cginc"
    31.  
    32.         struct appdata {
    33.             float4 vertex : POSITION;
    34.             float4 tangent : TANGENT;
    35.             float3 normal : NORMAL;
    36.             float2 texcoord : TEXCOORD0;
    37.         };
    38.  
    39.  
    40.         //Tesselation
    41.         float _Tess;
    42.         float _TessDist;
    43.  
    44.         float4 tessDistance (appdata v0, appdata v1, appdata v2) {
    45.             float minDist = 0;
    46.             float maxDist = _TessDist;
    47.             return UnityDistanceBasedTess(v0.vertex, v1.vertex, v2.vertex, minDist, maxDist, _Tess);
    48.         }
    49.  
    50.         //displacement
    51.         sampler2D _Splat;
    52.         float _Displacement;
    53.  
    54.         void disp (inout appdata v)
    55.         {
    56.             float d = tex2Dlod(_Splat, float4(v.texcoord.xy,0,0)).r * _Displacement;
    57.             v.vertex.xyz -= v.normal * d;
    58.             v.vertex.xyz += v.normal * _Displacement;
    59.         }
    60.  
    61.         //blendeding the two textures together based on height/displacement
    62.         fixed4 _GroundColor;
    63.         sampler2D _GroundTex;
    64.         fixed4 _TrackColor;
    65.         sampler2D _TrackTex;
    66.  
    67.         struct Input {
    68.             float2 uv_GroundTex;
    69.             float2 uv_TrackTex;
    70.             float2 uv_Splat;
    71.         };
    72.  
    73.         float _SpecAmount;
    74.         float _GlossAmount;
    75.  
    76.         void surf (Input IN, inout SurfaceOutput o) {
    77.        
    78.             half amount = tex2Dlod(_Splat, float4(IN.uv_Splat,0,0)).r;
    79.  
    80.             fixed4 c = lerp(tex2D(_GroundTex, IN.uv_GroundTex) * _GroundColor, tex2D(_TrackTex, IN.uv_TrackTex) * _TrackColor, amount);
    81.  
    82.             o.Albedo = c.rgb;
    83.             o.Specular = _SpecAmount;
    84.             o.Gloss = _GlossAmount;
    85.             o.Alpha = c.a;
    86.         }
    87.         ENDCG
    88.     }
    89.     FallBack "Diffuse"
    90. }
    91.  
     
    Last edited: Nov 21, 2019
  2. HunterAhlquist

    HunterAhlquist

    Joined:
    Oct 6, 2013
    Posts:
    91
    I've seen so many posts about this on Google, and none have a comprehensive answer!
     
    M_R_M likes this.
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    11,901