Search Unity

addshadows doesnt work properly

Discussion in 'Shaders' started by Kantic, Jan 31, 2021.

  1. Kantic

    Kantic

    Joined:
    Jan 8, 2020
    Posts:
    11
    Hi, it's the third thread of the week that i post on the same subject, as you can see i'm a noo... new to the subject.

    I am making a terrain surface shader, everything works except one thing: shadow.
    I am displacing the vertices in the vertex shader and the problem is that the shadow of the terrain itself and other objects are being casted on the original shape.
    Accordig to a lot of threads this is fixed by adding "addshadows" to the #pragma line, wich i did.
    But it gives this weird ressault where the surface on wich the shadows re casted is the one of the terrain but
    scaled down to the original mesh(the bounds are larger than the terrain itself so i dont think this is the problem).
    shortVideo.gif

    Here is my code:
    Code (CSharp):
    1. Shader "Example/terrain3" {
    2.     Properties
    3.     {
    4.         frequency ("frequency", Range(0, 0.3)) = 0.03
    5.         amplitude ("amplitude", Range (0, 50)) = 5
    6.         sLength("terrain side length", Range (10, 300)) = 30
    7.  
    8.        
    9.         sTex1 ("side Texture1", 2D) = "Default-checker" {}
    10.         sHTex1 ("side height Texture1", 2D) = "defaulttexture" {}
    11.         shW1 ("side height wheight1", Range (0, 1)) = 1
    12.         sTex2 ("side Texture2", 2D) = "defaulttexture" {}
    13.         sHTex2 ("side height Texture2", 2D) = "defaulttexture" {}
    14.         shW2 ("side height wheight2", Range (0, 1)) = 1
    15.        
    16.         uTex1 ("upward Texture1", 2D) = "defaulttexture" {}
    17.         uHTex1 ("upward height Texture1", 2D) = "defaulttexture" {}
    18.         uhW1 ("upward height wheight1", Range (0, 1)) = 1
    19.         uTex2 ("upward Texture2", 2D) = "defaulttexture" {}
    20.         uHTex2 ("upward height Texture2", 2D) = "defaulttexture" {}
    21.         uhW2 ("upward height wheight2", Range (0, 1)) = 1
    22.  
    23.         test("heightBias", Range (-1, 1)) = 0
    24.         dotS("dot sharpness", Range (1, 5)) = 2
    25.     }
    26.     SubShader
    27.     {
    28.         Tags { "RenderType" = "Opaque" }
    29.         CGPROGRAM
    30.         #pragma surface surf Lambert vertex:vert addshadow
    31.  
    32.            
    33.         float random (float2 uv)
    34.         {return frac(sin(dot(uv,float2(12.9898,78.233))) * 43758.5453123);}
    35.         float s(float t)
    36.         {return 6 * pow(t, 5) - 15 * pow(t, 4) + 10 * pow(t, 3);}
    37.         float lerp(float t, float a, float b)
    38.         {return a + t * (b - a);}
    39.         float4 lerp(float t, float4 a, float4 b)
    40.         {return float4(lerp(t, a.x, b.x), lerp(t, a.y, b.y), lerp(t, a.z, b.z), lerp(t, a.w, b.w));}
    41.         float slerp(float t, float a, float b)
    42.         { return lerp(s(t), a, b); }
    43.         float d(float t)
    44.         { return 30 * pow(t, 4) - 60 * pow(t, 3) + 30 * pow (t, 2); }
    45.         float D(float t, float a, float b, float amp, float f)
    46.         { return (d(t) * (b - a) * amp) * f; }
    47.  
    48.         struct noiseI
    49.         {
    50.             float val;
    51.             float3 normal;
    52.         };
    53.  
    54.         noiseI noise2D(float2 uv, float freq, float amp)
    55.         {
    56.             uv *= freq;
    57.             float2 bottomLeft = floor(uv);
    58.  
    59.             float a = random(bottomLeft);
    60.             float b = random(bottomLeft  + float2(1, 0));
    61.             float d = random(bottomLeft + 1);
    62.             float c = random(bottomLeft  + float2(0, 1));
    63.  
    64.             float2 pis = frac(uv);//Pos In Square
    65.                
    66.             float aD = D(pis.x, a, b, amp, freq);
    67.             float bD = D(pis.x, c, d, amp, freq);
    68.             a = slerp(pis.x, a, b);
    69.             b = slerp(pis.x, c, d);
    70.  
    71.             noiseI ni;
    72.             ni.normal = normalize(float3(-slerp(pis.y, aD, bD),1, -D(pis.y, a, b, amp, freq)));
    73.             ni.val = slerp(pis.y, a, b) * amp;
    74.             return ni;
    75.         }
    76.         float frequency;
    77.         float amplitude;
    78.         float sLength;
    79.         struct Input
    80.         {
    81.             float3 position;
    82.             float3 normal;
    83.         };
    84.         void vert (inout appdata_full v, out Input o)
    85.         {
    86.             v.vertex *= sLength;
    87.             noiseI ni = noise2D(v.vertex.xz, abs(frequency), amplitude);
    88.             v.vertex.y = ni.val;
    89.             v.normal = ni.normal;
    90.             o.position = v.vertex;
    91.             o.normal = v.normal;
    92.         }
    93.  
    94.        
    95.         sampler2D uTex1;
    96.         sampler2D uHTex1;
    97.         float uhW1;
    98.         float4 uTex1_ST;
    99.         sampler2D uTex2;
    100.         sampler2D uHTex2;
    101.         float uhW2;
    102.         float4 uTex2_ST;
    103.            
    104.         sampler2D sTex1;
    105.         sampler2D sHTex1;
    106.         float shW1;
    107.         float4 sTex1_ST;
    108.         sampler2D sTex2;
    109.         sampler2D sHTex2;
    110.         float shW2;
    111.         float4 sTex2_ST;
    112.  
    113.         float test;
    114.         float dotS;
    115.         float sharpS(float t, int sharpness)
    116.         {
    117.             while(sharpness-- > 1)
    118.                 t = s(t);
    119.             return t;
    120.         }
    121.         float4 blend(float4 s1, float w1, float4 s2, float w2)
    122.         {return lerp(sharpS(0.5+(w2-w1)/2, 7), s1, s2);}
    123.         float4 blendS(float4 s1, float w1, float4 s2, float w2, int sharpness)
    124.         {return lerp(sharpS(0.5+(w2-w1)/2, sharpness), s1, s2);}
    125.         float4 samp(float2 uv, sampler2D tex, float4 trans)
    126.         {return tex2D(tex, trans.zw + uv/trans.xy);}
    127.         float4 sampleFromSides(float3 pos, sampler2D tex, float4 trans , float3 normal)
    128.         {
    129.             normal = abs(normal);
    130.             float4 front = samp(pos.xy, tex, trans);
    131.             float4 right = samp(pos.zy, tex, trans);
    132.             float w1 = dot(normal, float3(0, 0, 1));
    133.             float w2 = dot(normal, float3(1, 0, 0));
    134.             return blendS(front, w1, right, w2, 4);
    135.         }
    136.         void surf (Input i, inout SurfaceOutput o)
    137.         {
    138.             float3 pos = i.position;
    139.             float heightWeight = clamp(pos.y / amplitude + test, 0, 1);
    140.             //upward
    141.             float4 s1 = samp(pos.xz, uTex1, uTex1_ST);
    142.             float h1 = samp(pos.xz, uHTex1, uTex1_ST);
    143.             float w1 = h1 * uhW1 * (1 - heightWeight);
    144.  
    145.             float4 s2 = samp(pos.xz, uTex2, uTex2_ST);
    146.             float h2 = samp(pos.xz, uHTex2, uTex2_ST);
    147.             float w2 = h2 * uhW2 * heightWeight;
    148.             s1 = blendS(s1, w1, s2, w2, 6);
    149.             h1 = blendS(h1, w1, h2, w2, 6);
    150.  
    151.             //sides
    152.             s2 = sampleFromSides(pos, sTex1, sTex1_ST, i.normal);
    153.             h2 = sampleFromSides(pos, sHTex1, sTex1_ST, i.normal);
    154.             w1 = h2 * shW1 * (1 - heightWeight);
    155.  
    156.             float4 s3 = sampleFromSides(pos, sTex2, sTex2_ST, i.normal);
    157.             float h3 = sampleFromSides(pos, sHTex2, sTex2_ST, i.normal);
    158.             w2 = h3 * shW2 * heightWeight;
    159.                
    160.             s2 = blend(s2, w1, s3, w2);
    161.             h2 = blend(h2, w1, h3, w2);
    162.             //return s2;
    163.             float dotUP = dot(float3(0, 1, 0), i.normal);
    164.             o.Albedo = blendS(s1, h1 * pow(dotUP, dotS), s2, h2 * (1 - pow(dotUP, dotS)), 5);
    165.         }
    166.         ENDCG
    167.     }
    168.     Fallback "Diffuse"
    169. }
    170.  
    171. //struct SurfaceOutput
    172. //{
    173. //    fixed3 Albedo;  // diffuse color
    174. //    fixed3 Normal;  // tangent space normal, if written
    175. //    fixed3 Emission;
    176. //    half Specular;  // specular power in 0..1 range
    177. //    fixed Gloss;    // specular intensity
    178. //    fixed Alpha;    // alpha for transparencies
    179. //};
    Thanks in advance for your help!
     

    Attached Files: