Search Unity

Question Lines in custom surface shader when using a normal map

Discussion in 'General Graphics' started by LdeJonge, Sep 11, 2021.

  1. LdeJonge

    LdeJonge

    Joined:
    Feb 22, 2021
    Posts:
    1
    I am making a custom surface shader for water on top of a sphere.
    When I add the normal map it generates lines.
    upload_2021-9-11_22-32-37.png

    If I use the same texture for my albedo it works just fine.

    upload_2021-9-11_22-34-0.png

    Here is the shadercode I'm using.
    Code (CSharp):
    1. Shader "TestNormToTan" {
    2.     Properties{
    3.         _Color("Main Color", Color) = (1,1,1,1)
    4.         _ColorDeep("Color Deep", Color) = (1,1,1,1)
    5.  
    6.         _Tess("Tessellation", Range(1,320)) = 4
    7.  
    8.         _MainTex("Diffuse (RGB) Alpha (A)", 2D) = "gray" {}
    9.         _BumpMap("Normal Map", 2D) = "bump" {}
    10.         _DispTex("Disp Texture", 2D) = "gray" {}
    11.  
    12.         _Displacement("Displacement", Range(0, 1.0)) = 0.3
    13.     }
    14.         SubShader{
    15.             Tags
    16.             {
    17.                 "RenderType" = "Transparent"
    18.                 "Queue" = "Transparent"
    19.                 "ForceNoShadowCasting" = "True"
    20.             }
    21.             LOD 200
    22.  
    23.             GrabPass { "_Refraction" }
    24.  
    25.  
    26.             CGPROGRAM
    27.             //#pragma surface surf Lambert addshadow fullforwardshadows vertex:disp tessellate:tessFixed nolightmap alpha:auto alphatest:_Cutoff
    28.             #pragma surface surf StandardSpecular vertex:disp tessellate:tessFixed alpha:auto alphatest:_Cutoff
    29.             #pragma target 3.0
    30.  
    31.            struct appdata {
    32.                 float4 vertex : POSITION;
    33.                 float4 tangent : TANGENT;
    34.                 float3 normal : NORMAL;
    35.                 float3 texcoord : TEXCOORD0;
    36.  
    37.                 fixed4 color : COLOR;
    38.  
    39.             };
    40.  
    41.             sampler2D _MainTex;
    42.  
    43.             #define PI 3.141592653589793
    44.          
    45.  
    46.             float _Tess;
    47.  
    48.             float4 tessFixed()
    49.             {
    50.                 return _Tess;
    51.             }
    52.  
    53.             sampler2D _DispTex;
    54.             sampler2D _BumpMap;
    55.             float _Displacement;
    56.            
    57.  
    58.           //  uniform float4x4 _Rotation;
    59.  
    60.             struct Input {
    61.                  float4 uv_BumpMap;
    62.                  float3 worldRefl;
    63.  
    64.                  float3 color : COLOR;
    65.  
    66.                  float3 worldNormal;
    67.                  float3 viewDir;
    68.                  INTERNAL_DATA
    69.             };
    70.  
    71.             float PingPong()
    72.             {
    73.                 float remainder = fmod(floor(_Time.y), 2);
    74.                 return remainder == 1 ? 1 - frac(_Time.y) : frac(_Time.y);
    75.             }
    76.  
    77.             inline float2 RadialCoords(float3 a_coords)
    78.             {
    79.                 float3 a_coords_n = normalize(a_coords);
    80.                 float lon = atan2(a_coords_n.z, a_coords_n.x);
    81.                 float lat = acos(a_coords_n.y);
    82.                 float2 sphereCoords = float2(lon, lat) * (1.0 / PI);
    83.                 return float2(sphereCoords.x * 0.5 + 0.5, 1 - sphereCoords.y);
    84.             }
    85.  
    86.  
    87.             void disp(inout appdata v)
    88.             {
    89.  
    90.                 float2 equiUV = RadialCoords(v.vertex);
    91.  
    92.                 v.color.xyz = v.vertex.xyz;
    93.                 float d = tex2Dlod(_DispTex, float4(equiUV.xy, 0, 0)).r * _Displacement;
    94.  
    95.                 v.vertex.xy += v.normal * d;
    96.             }
    97.  
    98.  
    99.  
    100.             fixed4 _Color;
    101.             fixed4 _ColorDeep;
    102.          
    103.  
    104.             void surf(Input IN, inout SurfaceOutputStandardSpecular o)
    105.             {
    106.                 fixed4 c = _Color;
    107.                 fixed4 col = _ColorDeep;
    108.  
    109.                 float2 equiUV = RadialCoords(IN.worldNormal);
    110.                 float2 equiUV2 = RadialCoords(-IN.worldNormal);
    111.  
    112.                 float3 norm = UnpackNormal(tex2D(_BumpMap, RadialCoords(IN.color.xyz)));
    113.  
    114.                 float fresnel = dot(IN.worldNormal, IN.viewDir);
    115.  
    116.                 fresnel = saturate(1 - fresnel);
    117.  
    118.                 o.Normal = norm;
    119.  
    120.                 o.Albedo = tex2D(_MainTex, equiUV)* lerp(c.rgb, col.rgb, fresnel);
    121.  
    122.                    o.Alpha = c.a;
    123.             }
    124.             ENDCG
    125.  
    126.  
    127.         }
    128.             FallBack "Diffuse"
    129. }