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.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Grass shader problem

Discussion in 'Shaders' started by Taavet, Feb 23, 2018.

  1. Taavet

    Taavet

    Joined:
    Dec 14, 2017
    Posts:
    2
    Hey, I copied a basic grass shader and tried to add a shadow to it. Now I have this weird problem.


    Is there an easy way to fix it. I have tried everything.

    Shader code:
    Code (CSharp):
    1. Shader "Custom/Grass Geometry Shader"
    2. {
    3.     Properties
    4.     {
    5.         [NoScaleOffset] _MainTex("Grass Texture", 2D) = "white" {}
    6.         _Cutoff("Cutoff", Range(0,1)) = 0.25
    7.         _GrassHeight("Grass Height", Float) = 0.25
    8.         _GrassWidth("Grass Width", Float) = 0.25
    9.         _WindSpeed("Wind Speed", Float) = 100
    10.         _WindStength("Wind Strength", Float) = 0.05
    11.     }
    12.     SubShader
    13.     {
    14.         Tags{"LightMode" = "ForwardBase" }
    15.         LOD 200
    16.  
    17.         Pass
    18.         {
    19.             CULL OFF
    20.  
    21.             CGPROGRAM
    22.             #pragma target 4.0
    23.             #pragma vertex vert
    24.             #pragma fragment frag
    25.             #pragma geometry geom
    26.             #include "UnityCG.cginc"
    27.             #include "Lighting.cginc"
    28.             #include "AutoLight.cginc"
    29.             #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
    30.  
    31.             sampler2D _MainTex;
    32.             half _Cutoff;
    33.             half _GrassHeight;
    34.             half _GrassWidth;
    35.             half _WindStength;
    36.             half _WindSpeed;
    37.  
    38.             struct g2f
    39.             {
    40.                 float4 pos : SV_POSITION;
    41.                 float3 norm : NORMAL;
    42.                 float2 uv : TEXCOORD0;
    43.                 SHADOW_COORDS(1)
    44.                 fixed3 diff : COLOR0;
    45.                 fixed3 ambient : COLOR1;
    46.             };
    47.  
    48.             void createQuad(float3 points[4], inout TriangleStream<g2f> triStream, appdata_full a)
    49.             {
    50.                 g2f OUT;
    51.                 float3 faceNormal = cross(points[1] - points[0], points[2] - points[0]);
    52.                 for (uint i = 0; i < 4; i++)
    53.                 {
    54.                     OUT.pos = UnityObjectToClipPos(points[i]);
    55.                     OUT.norm = faceNormal;
    56.                     OUT.uv = float2(i % 2, (uint)i / 2);
    57.  
    58.                     half3 worldNormal = UnityObjectToWorldNormal(a.normal);
    59.                     half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
    60.                     OUT.diff = nl * _LightColor0.rgb;
    61.                     OUT.ambient = ShadeSH9(half4(worldNormal, 1));
    62.  
    63.                     TRANSFER_SHADOW(OUT)
    64.                     triStream.Append(OUT);
    65.                 }
    66.                 triStream.RestartStrip();
    67.             }
    68.  
    69.             appdata_full vert(appdata_full v)
    70.             {
    71.                 return v;
    72.             }
    73.  
    74.             [maxvertexcount(12)]
    75.             void geom(point appdata_full i[1], inout TriangleStream<g2f> tristream)
    76.             {
    77.                 float3 perpendicularAngle = float3(0, 0, 1);
    78.                 float3 faceNormal = cross(perpendicularAngle, i[0].normal);
    79.  
    80.                 float3 v0 = i[0].vertex.xyz;
    81.                 float3 v1 = i[0].vertex.xyz + i[0].normal * _GrassHeight;
    82.  
    83.                 float3 wind = float3(sin(_Time.x * _WindSpeed + v0.x) + sin(_Time.x * _WindSpeed + v0.z * 2) + sin(_Time.x * _WindSpeed * 0.1 + v0.x), 0,
    84.                     cos(_Time.x * _WindSpeed + v0.x * 2) + cos(_Time.x * _WindSpeed + v0.z));
    85.                 v1 += wind * _WindStength;
    86.  
    87.                 float sin60 = 0.866f;
    88.                 float cos60 = 0.5;
    89.  
    90.                 float3 quad1[4] = {
    91.                     v0 + perpendicularAngle * 0.5 * _GrassWidth,
    92.                     v0 - perpendicularAngle * 0.5 * _GrassWidth,
    93.                     v1 + perpendicularAngle * 0.5 * _GrassWidth,
    94.                     v1 - perpendicularAngle * 0.5 * _GrassWidth
    95.                 };
    96.  
    97.                 createQuad(quad1, tristream, i[0]);
    98.  
    99.                 float3 quad2[4] = {
    100.                     v0 + float3(sin60, 0, -cos60) * 0.5 * _GrassWidth,
    101.                     v0 - float3(sin60, 0, -cos60) * 0.5 * _GrassWidth,
    102.                     v1 + float3(sin60, 0, -cos60) * 0.5 * _GrassWidth,
    103.                     v1 - float3(sin60, 0, -cos60) * 0.5 * _GrassWidth
    104.                 };
    105.  
    106.                 createQuad(quad2, tristream, i[0]);
    107.  
    108.                 float3 quad3[4] = {
    109.                     v0 + float3(sin60, 0, cos60) * 0.5 * _GrassWidth,
    110.                     v0 - float3(sin60, 0, cos60) * 0.5 * _GrassWidth,
    111.                     v1 + float3(sin60, 0, cos60) * 0.5 * _GrassWidth,
    112.                     v1 - float3(sin60, 0, cos60) * 0.5 * _GrassWidth
    113.                 };
    114.  
    115.                 createQuad(quad3, tristream, i[0]);
    116.             }
    117.  
    118.             half4 frag(g2f i) : COLOR
    119.             {
    120.                 fixed4 col = tex2D(_MainTex, i.uv);
    121.                 fixed shadow = SHADOW_ATTENUATION(i);
    122.                 fixed3 lighting = i.diff * shadow + i.ambient;
    123.                 col.rgb *= lighting;
    124.                 clip(col.a - _Cutoff);
    125.                 return col;
    126.             }
    127.      
    128.             ENDCG
    129.         }
    130.  
    131.         UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
    132.     }
    133. }
    Also this happens if I add the material on a plane.
     
  2. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    243
    Your shader doesnt write depth correctly. You need to add your own custom shadowcaster pass and repeat all the vertex processing/geometry generation in there.
     
  3. Taavet

    Taavet

    Joined:
    Dec 14, 2017
    Posts:
    2
    hmm, alright. Is there any examples how to pass shadowcaster separately. I'm very new to shaders.
    Still, thank you!
     
  4. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    243